]>
diplodocus.org Git - nmh/blob - sbr/error.c
92afbe52a81a6e44c4b054d9d9a86a6ceca699cb
2 * error.c -- main error handling routines
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * 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. */
17 void inform(char *fmt
, ...)
22 advertise(NULL
, NULL
, fmt
, ap
);
27 /* advise calls advertise() with no tail to print fmt, and perhaps what,
29 * Thus "[invo_name: ]fmt[[ what]: errno]\n" results. */
31 advise (const char *what
, const char *fmt
, ...)
36 advertise (what
, NULL
, fmt
, ap
);
41 /* adios is the same as advise(), but then "ends" the program.
42 * It calls advertise() with no tail to print fmt, and perhaps what, to
43 * stderr, and exits the program with an error status.
44 * Thus "[invo_name: ]fmt[[ what]: errno]\n" results.
45 * The route to exit is via the done function pointer and may not be
46 * straightforward, e.g. longjmp(3), but it must not return to adios().
47 * If it does then it's a bug and adios() will abort(3) as callers do
48 * not expect execution to continue. */
50 adios (const char *what
, const char *fmt
, ...)
55 advertise (what
, NULL
, fmt
, ap
);
62 /* admonish calls advertise() with a tail indicating the program
64 * Thus "[invo_name: ]fmt[[ what]: errno], continuing...\n" results. */
66 admonish (char *what
, char *fmt
, ...)
71 advertise (what
, "continuing...", fmt
, ap
);
76 /* advertise prints fmt and ap to stderr after flushing stdout.
77 * If invo_name isn't NULL or empty then "invo_name: " precedes fmt.
78 * If what isn't NULL or empty then " what" is appended.
79 * If what isn't NULL then ": errno" is appended.
80 * If tail isn't NULL or empty then ", tail" is appended.
81 * A "\n" finishes the output to stderr.
82 * In summary: "[invo_name: ]fmt[[ what]: errno][, tail]\n". */
84 advertise (const char *what
, char *tail
, const char *fmt
, va_list ap
)
87 char buffer
[NMH_BUFSIZ
], *err
;
88 struct iovec iob
[10], *iov
;
93 #define APPEND_IOV(p, len) \
94 iov->iov_base = (p); \
95 iov->iov_len = (len); \
98 #define ADD_LITERAL(s) APPEND_IOV((s), LEN(s))
99 #define ADD_VAR(s) APPEND_IOV((s), strlen(s))
101 if (invo_name
&& *invo_name
) {
106 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
111 ADD_VAR((void *)what
);
114 err
= strerror(eindex
);
127 assert(niov
<= DIM(iob
));
131 if (writev(fileno(stderr
), iob
, niov
) == -1) {
132 snprintf(buffer
, sizeof buffer
, "%s: write stderr failed: %d\n",
133 invo_name
&& *invo_name
? invo_name
: "nmh", errno
);
134 if (write(2, buffer
, strlen(buffer
)) == -1) {
135 /* Ignore. if-statement needed to shut up compiler. */