]>
diplodocus.org Git - nmh/blob - sbr/error.c
1 /* error.c -- main error handling routines
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
11 #include <sys/types.h>
15 /* inform calls advertise() with no what and no tail.
16 * Thus the simple "[invo_name: ]fmt\n" results. */
18 inform(char *fmt
, ...)
23 advertise(NULL
, NULL
, fmt
, ap
);
28 /* advise calls advertise() with no tail to print fmt, and perhaps what,
30 * Thus "[invo_name: ]fmt[[ what]: errno]\n" results. */
32 advise (const char *what
, const char *fmt
, ...)
37 advertise (what
, NULL
, fmt
, ap
);
42 /* adios is the same as advise(), but then "ends" the program.
43 * It calls advertise() with no tail to print fmt, and perhaps what, to
44 * stderr, and exits the program with an error status.
45 * Thus "[invo_name: ]fmt[[ what]: errno]\n" results.
46 * The route to exit is via the done function pointer and may not be
47 * straightforward, e.g. longjmp(3), but it must not return to adios().
48 * If it does then it's a bug and adios() will abort(3) as callers do
49 * not expect execution to continue. */
51 adios (const char *what
, const char *fmt
, ...)
56 advertise (what
, NULL
, fmt
, ap
);
63 /* die is the same as adios(), but without the what as that's commonly
66 die(const char *fmt
, ...)
71 advertise(NULL
, NULL
, fmt
, ap
);
78 /* admonish calls advertise() with a tail indicating the program
80 * Thus "[invo_name: ]fmt[[ what]: errno], continuing...\n" results. */
82 admonish (char *what
, char *fmt
, ...)
87 advertise (what
, "continuing...", fmt
, ap
);
92 /* advertise prints fmt and ap to stderr after flushing stdout.
93 * If invo_name isn't NULL or empty then "invo_name: " precedes fmt.
94 * If what isn't NULL or empty then " what" is appended.
95 * If what isn't NULL then ": errno" is appended.
96 * If tail isn't NULL or empty then ", tail" is appended.
97 * A "\n" finishes the output to stderr.
98 * In summary: "[invo_name: ]fmt[[ what]: errno][, tail]\n". */
100 advertise (const char *what
, char *tail
, const char *fmt
, va_list ap
)
103 char buffer
[NMH_BUFSIZ
], *err
;
104 struct iovec iob
[10], *iov
;
109 #define APPEND_IOV(p, len) \
110 iov->iov_base = (p); \
111 iov->iov_len = (len); \
114 #define ADD_LITERAL(s) APPEND_IOV((s), LEN(s))
115 #define ADD_VAR(s) APPEND_IOV((s), strlen(s))
117 if (invo_name
&& *invo_name
) {
122 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
127 ADD_VAR((void *)what
);
130 err
= strerror(eindex
);
143 assert(niov
<= DIM(iob
));
147 if (writev(fileno(stderr
), iob
, niov
) == -1) {
148 snprintf(buffer
, sizeof buffer
, "%s: write stderr failed: %d\n",
149 invo_name
&& *invo_name
? invo_name
: "nmh", errno
);
150 if (write(2, buffer
, strlen(buffer
)) == -1) {
151 /* Ignore. if-statement needed to shut up compiler. */