]> diplodocus.org Git - nmh/blob - sbr/error.c
92afbe52a81a6e44c4b054d9d9a86a6ceca699cb
[nmh] / sbr / error.c
1 /*
2 * error.c -- main error handling routines
3 *
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.
7 */
8
9 #include <h/mh.h>
10
11 #include <sys/types.h>
12 #include <sys/uio.h>
13
14
15 /* inform calls advertise() with no what and no tail.
16 * Thus the simple "[invo_name: ]fmt\n" results. */
17 void inform(char *fmt, ...)
18 {
19 va_list ap;
20
21 va_start(ap, fmt);
22 advertise(NULL, NULL, fmt, ap);
23 va_end(ap);
24 }
25
26
27 /* advise calls advertise() with no tail to print fmt, and perhaps what,
28 * to stderr.
29 * Thus "[invo_name: ]fmt[[ what]: errno]\n" results. */
30 void
31 advise (const char *what, const char *fmt, ...)
32 {
33 va_list ap;
34
35 va_start(ap, fmt);
36 advertise (what, NULL, fmt, ap);
37 va_end(ap);
38 }
39
40
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. */
49 void
50 adios (const char *what, const char *fmt, ...)
51 {
52 va_list ap;
53
54 va_start(ap, fmt);
55 advertise (what, NULL, fmt, ap);
56 va_end(ap);
57 done (1);
58 abort();
59 }
60
61
62 /* admonish calls advertise() with a tail indicating the program
63 * continues.
64 * Thus "[invo_name: ]fmt[[ what]: errno], continuing...\n" results. */
65 void
66 admonish (char *what, char *fmt, ...)
67 {
68 va_list ap;
69
70 va_start(ap, fmt);
71 advertise (what, "continuing...", fmt, ap);
72 va_end(ap);
73 }
74
75
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". */
83 void
84 advertise (const char *what, char *tail, const char *fmt, va_list ap)
85 {
86 int eindex = errno;
87 char buffer[NMH_BUFSIZ], *err;
88 struct iovec iob[10], *iov;
89 size_t niov;
90
91 iov = iob;
92
93 #define APPEND_IOV(p, len) \
94 iov->iov_base = (p); \
95 iov->iov_len = (len); \
96 iov++
97
98 #define ADD_LITERAL(s) APPEND_IOV((s), LEN(s))
99 #define ADD_VAR(s) APPEND_IOV((s), strlen(s))
100
101 if (invo_name && *invo_name) {
102 ADD_VAR(invo_name);
103 ADD_LITERAL(": ");
104 }
105
106 vsnprintf (buffer, sizeof(buffer), fmt, ap);
107 ADD_VAR(buffer);
108 if (what) {
109 if (*what) {
110 ADD_LITERAL(" ");
111 ADD_VAR((void *)what);
112 }
113 ADD_LITERAL(": ");
114 err = strerror(eindex);
115 ADD_VAR(err);
116 }
117 if (tail && *tail) {
118 ADD_LITERAL(", ");
119 ADD_VAR(tail);
120 }
121 ADD_LITERAL("\n");
122
123 #undef ADD_LITERAL
124 #undef ADD_VAR
125
126 niov = iov - iob;
127 assert(niov <= DIM(iob));
128
129 fflush (stdout);
130 fflush (stderr);
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. */
136 }
137 }
138 }