]> diplodocus.org Git - nmh/blob - sbr/error.c
7b2f506459c90637173e8d087ec5565d33f2b70a
[nmh] / sbr / error.c
1
2 /*
3 * error.c -- main error handling routines
4 *
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.
8 */
9
10 #include <h/mh.h>
11
12 #include <sys/types.h>
13 #include <sys/uio.h>
14
15
16 /* inform calls advertise() with no what and no tail.
17 * Thus the simple "invo_name: fmt\n" results. */
18 void inform(char *fmt, ...)
19 {
20 va_list ap;
21
22 va_start(ap, fmt);
23 advertise(NULL, NULL, fmt, ap);
24 va_end(ap);
25 }
26
27
28 /* advise calls advertise() with no tail to print fmt, and perhaps what,
29 * to stderr. */
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 calls advertise() with no tail to print fmt, and perhaps what,
42 * to stderr, and "ends" the program with an error exit status. The
43 * route to exit is via the done function pointer and may not be
44 * straightforward, e.g. longjmp(3), but it must not return to adios().
45 * If it does then it's a bug and adios() will abort(3) as callers do
46 * not expect execution to continue. */
47 void
48 adios (const char *what, const char *fmt, ...)
49 {
50 va_list ap;
51
52 va_start(ap, fmt);
53 advertise (what, NULL, fmt, ap);
54 va_end(ap);
55 done (1);
56 abort();
57 }
58
59
60 /* admonish calls advertise() with a tail indicating the program
61 * continues. */
62 void
63 admonish (char *what, char *fmt, ...)
64 {
65 va_list ap;
66
67 va_start(ap, fmt);
68 advertise (what, "continuing...", fmt, ap);
69 va_end(ap);
70 }
71
72
73 /* advertise prints fmt and ap to stderr after flushing stdout.
74 * If invo_name isn't NULL or empty, it precedes the output seperated by ": ".
75 * If what isn't NULL, errno as a string is appended.
76 * A non-empty what separates fmt from errno, surrounded by " " and ": ".
77 * BUG: No space separator before errno if what is "".
78 * If tail isn't NULL or empty then ", " and tail are appended
79 * before the finishing "\n".
80 * In summary: "invo_name: fmt what: errno, tail\n". */
81 void
82 advertise (const char *what, char *tail, const char *fmt, va_list ap)
83 {
84 int eindex = errno;
85 char buffer[NMH_BUFSIZ], errbuf[NMH_BUFSIZ], *err;
86 struct iovec iob[10], *iov;
87 size_t niov;
88
89 iov = iob;
90
91 #define APPEND_IOV(p, len) \
92 iov->iov_base = (p); \
93 iov->iov_len = (len); \
94 iov++
95
96 #define ADD_LITERAL(s) APPEND_IOV((s), LEN(s))
97 #define ADD_VAR(s) APPEND_IOV((s), strlen(s))
98
99 if (invo_name && *invo_name) {
100 ADD_VAR(invo_name);
101 ADD_LITERAL(": ");
102 }
103
104 vsnprintf (buffer, sizeof(buffer), fmt, ap);
105 ADD_VAR(buffer);
106 if (what) {
107 if (*what) {
108 ADD_LITERAL(" ");
109 ADD_VAR((void *)what);
110 ADD_LITERAL(": ");
111 }
112 err = strerror(eindex);
113 if (!err) {
114 /* this shouldn't happen, but we'll test for it just in case */
115 snprintf(errbuf, sizeof(errbuf), "Error %d", eindex);
116 err = errbuf;
117 }
118 ADD_VAR(err);
119 }
120 if (tail && *tail) {
121 ADD_LITERAL(", ");
122 ADD_VAR(tail);
123 }
124 ADD_LITERAL("\n");
125
126 #undef ADD_LITERAL
127 #undef ADD_VAR
128
129 niov = iov - iob;
130 assert(niov <= DIM(iob));
131
132 fflush (stdout);
133 fflush (stderr);
134 if (writev(fileno(stderr), iob, niov) == -1) {
135 snprintf(buffer, sizeof buffer, "%s: write stderr failed: %d\n",
136 invo_name && *invo_name ? invo_name : "nmh", errno);
137 if (write(2, buffer, strlen(buffer)) == -1) {
138 /* Ignore. if-statement needed to shut up compiler. */
139 }
140 }
141 }