]>
diplodocus.org Git - nmh/blob - sbr/error.c
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,
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 /* admonish calls advertise() with a tail indicating the program
65 * Thus "[invo_name: ]fmt[[ what]: errno], continuing...\n" results. */
67 admonish (char *what
, char *fmt
, ...)
72 advertise (what
, "continuing...", fmt
, ap
);
77 /* advertise prints fmt and ap to stderr after flushing stdout.
78 * If invo_name isn't NULL or empty then "invo_name: " precedes fmt.
79 * If what isn't NULL or empty then " what" is appended.
80 * If what isn't NULL then ": errno" is appended.
81 * If tail isn't NULL or empty then ", tail" is appended.
82 * A "\n" finishes the output to stderr.
83 * In summary: "[invo_name: ]fmt[[ what]: errno][, tail]\n". */
85 advertise (const char *what
, char *tail
, const char *fmt
, va_list ap
)
88 char buffer
[NMH_BUFSIZ
], *err
;
89 struct iovec iob
[10], *iov
;
94 #define APPEND_IOV(p, len) \
95 iov->iov_base = (p); \
96 iov->iov_len = (len); \
99 #define ADD_LITERAL(s) APPEND_IOV((s), LEN(s))
100 #define ADD_VAR(s) APPEND_IOV((s), strlen(s))
102 if (invo_name
&& *invo_name
) {
107 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
112 ADD_VAR((void *)what
);
115 err
= strerror(eindex
);
128 assert(niov
<= DIM(iob
));
132 if (writev(fileno(stderr
), iob
, niov
) == -1) {
133 snprintf(buffer
, sizeof buffer
, "%s: write stderr failed: %d\n",
134 invo_name
&& *invo_name
? invo_name
: "nmh", errno
);
135 if (write(2, buffer
, strlen(buffer
)) == -1) {
136 /* Ignore. if-statement needed to shut up compiler. */