]>
diplodocus.org Git - nmh/blob - sbr/error.c
7b2f506459c90637173e8d087ec5565d33f2b70a
3 * error.c -- main error handling routines
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <sys/types.h>
16 /* inform calls advertise() with no what and no tail.
17 * Thus the simple "invo_name: fmt\n" results. */
18 void inform(char *fmt
, ...)
23 advertise(NULL
, NULL
, fmt
, ap
);
28 /* advise calls advertise() with no tail to print fmt, and perhaps what,
31 advise (const char *what
, const char *fmt
, ...)
36 advertise (what
, NULL
, fmt
, ap
);
41 /* adios calls advertise() with no tail to print fmt, and perhaps what,
42 * to stderr, and "ends" the program with an error exit status. The
43 * route to exit is via the done function pointer and may not be
44 * straightforward, e.g. longjmp(3), but it must not return to adios().
45 * If it does then it's a bug and adios() will abort(3) as callers do
46 * not expect execution to continue. */
48 adios (const char *what
, const char *fmt
, ...)
53 advertise (what
, NULL
, fmt
, ap
);
60 /* admonish calls advertise() with a tail indicating the program
63 admonish (char *what
, char *fmt
, ...)
68 advertise (what
, "continuing...", fmt
, ap
);
73 /* advertise prints fmt and ap to stderr after flushing stdout.
74 * If invo_name isn't NULL or empty, it precedes the output seperated by ": ".
75 * If what isn't NULL, errno as a string is appended.
76 * A non-empty what separates fmt from errno, surrounded by " " and ": ".
77 * BUG: No space separator before errno if what is "".
78 * If tail isn't NULL or empty then ", " and tail are appended
79 * before the finishing "\n".
80 * In summary: "invo_name: fmt what: errno, tail\n". */
82 advertise (const char *what
, char *tail
, const char *fmt
, va_list ap
)
85 char buffer
[NMH_BUFSIZ
], errbuf
[NMH_BUFSIZ
], *err
;
86 struct iovec iob
[10], *iov
;
91 #define APPEND_IOV(p, len) \
92 iov->iov_base = (p); \
93 iov->iov_len = (len); \
96 #define ADD_LITERAL(s) APPEND_IOV((s), LEN(s))
97 #define ADD_VAR(s) APPEND_IOV((s), strlen(s))
99 if (invo_name
&& *invo_name
) {
104 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
109 ADD_VAR((void *)what
);
112 err
= strerror(eindex
);
114 /* this shouldn't happen, but we'll test for it just in case */
115 snprintf(errbuf
, sizeof(errbuf
), "Error %d", eindex
);
130 assert(niov
<= DIM(iob
));
134 if (writev(fileno(stderr
), iob
, niov
) == -1) {
135 snprintf(buffer
, sizeof buffer
, "%s: write stderr failed: %d\n",
136 invo_name
&& *invo_name
? invo_name
: "nmh", errno
);
137 if (write(2, buffer
, strlen(buffer
)) == -1) {
138 /* Ignore. if-statement needed to shut up compiler. */