]>
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.
10 #include <sys/types.h>
14 /* inform calls advertise() with no what and no tail.
15 * Thus the simple "[invo_name: ]fmt\n" results. */
16 void inform(char *fmt
, ...)
21 advertise(NULL
, NULL
, fmt
, ap
);
26 /* advise calls advertise() with no tail to print fmt, and perhaps what,
28 * Thus "[invo_name: ]fmt[[ what]: errno]\n" results. */
30 advise (const char *what
, const char *fmt
, ...)
35 advertise (what
, NULL
, fmt
, ap
);
40 /* adios is the same as advise(), but then "ends" the program.
41 * It calls advertise() with no tail to print fmt, and perhaps what, to
42 * stderr, and exits the program with an error status.
43 * Thus "[invo_name: ]fmt[[ what]: errno]\n" results.
44 * The route to exit is via the done function pointer and may not be
45 * straightforward, e.g. longjmp(3), but it must not return to adios().
46 * If it does then it's a bug and adios() will abort(3) as callers do
47 * not expect execution to continue. */
49 adios (const char *what
, const char *fmt
, ...)
54 advertise (what
, NULL
, fmt
, ap
);
61 /* die is the same as adios(), but without the what as that's commonly
64 die(const char *fmt
, ...)
69 advertise(NULL
, NULL
, fmt
, ap
);
76 /* admonish calls advertise() with a tail indicating the program
78 * Thus "[invo_name: ]fmt[[ what]: errno], continuing...\n" results. */
80 admonish (char *what
, char *fmt
, ...)
85 advertise (what
, "continuing...", fmt
, ap
);
90 /* advertise prints fmt and ap to stderr after flushing stdout.
91 * If invo_name isn't NULL or empty then "invo_name: " precedes fmt.
92 * If what isn't NULL or empty then " what" is appended.
93 * If what isn't NULL then ": errno" is appended.
94 * If tail isn't NULL or empty then ", tail" is appended.
95 * A "\n" finishes the output to stderr.
96 * In summary: "[invo_name: ]fmt[[ what]: errno][, tail]\n". */
98 advertise (const char *what
, char *tail
, const char *fmt
, va_list ap
)
101 char buffer
[NMH_BUFSIZ
], *err
;
102 struct iovec iob
[10], *iov
;
107 #define APPEND_IOV(p, len) \
108 iov->iov_base = (p); \
109 iov->iov_len = (len); \
112 #define ADD_LITERAL(s) APPEND_IOV((s), LEN(s))
113 #define ADD_VAR(s) APPEND_IOV((s), strlen(s))
115 if (invo_name
&& *invo_name
) {
120 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
125 ADD_VAR((void *)what
);
128 err
= strerror(eindex
);
141 assert(niov
<= DIM(iob
));
145 if (writev(fileno(stderr
), iob
, niov
) == -1) {
146 snprintf(buffer
, sizeof buffer
, "%s: write stderr failed: %d\n",
147 invo_name
&& *invo_name
? invo_name
: "nmh", errno
);
148 if (write(2, buffer
, strlen(buffer
)) == -1) {
149 /* Ignore. if-statement needed to shut up compiler. */