]>
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 /* advise calls advertise() with no tail to print fmt, and perhaps what,
19 advise (const char *what
, const char *fmt
, ...)
24 advertise (what
, NULL
, fmt
, ap
);
29 /* adios calls advertise() with no tail to print fmt, and perhaps what,
30 * to stderr, and "ends" the program with an error exit status. The
31 * route to exit is via the done function pointer and may not be
33 * FIXME: Document if this function can ever return. If not, perhaps an
34 * abort(3) at the end of the routine would make that more clear. */
36 adios (const char *what
, const char *fmt
, ...)
41 advertise (what
, NULL
, fmt
, ap
);
47 /* admonish calls advertise() with a tail indicating the program
50 admonish (char *what
, char *fmt
, ...)
55 advertise (what
, "continuing...", fmt
, ap
);
60 /* advertise prints fmt and ap to stderr after flushing stdout.
61 * If invo_name isn't NULL or empty, it precedes the output seperated by ": ".
62 * If what isn't NULL, errno as a string is appended.
63 * A non-empty what separates fmt from errno, surrounded by " " and ": ".
64 * BUG: No space separator before errno if what is "".
65 * If tail isn't NULL or empty then ", " and tail are appended
66 * before the finishing "\n".
67 * In summary: "invo_name: fmt what: errno, tail\n". */
69 advertise (const char *what
, char *tail
, const char *fmt
, va_list ap
)
72 char buffer
[NMH_BUFSIZ
], errbuf
[NMH_BUFSIZ
], *err
;
73 struct iovec iob
[10], *iov
;
78 #define APPEND_IOV(p, len) \
79 iov->iov_base = (p); \
80 iov->iov_len = (len); \
83 #define ADD_LITERAL(s) APPEND_IOV((s), LEN(s))
84 #define ADD_VAR(s) APPEND_IOV((s), strlen(s))
86 if (invo_name
&& *invo_name
) {
91 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
96 ADD_VAR((void *)what
);
99 err
= strerror(eindex
);
101 /* this shouldn't happen, but we'll test for it just in case */
102 snprintf(errbuf
, sizeof(errbuf
), "Error %d", eindex
);
117 assert(niov
<= DIM(iob
));
121 if (writev(fileno(stderr
), iob
, niov
) == -1) {
122 snprintf(buffer
, sizeof buffer
, "%s: write stderr failed: %d\n",
123 invo_name
&& *invo_name
? invo_name
: "nmh", errno
);
124 if (write(2, buffer
, strlen(buffer
)) == -1) {
125 /* Ignore. if-statement needed to shut up compiler. */