]>
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
32 * straightforward, e.g. longjmp(3), but it must not return to adios().
33 * If it does then it's a bug and adios() will abort(3) as callers do
34 * not expect execution to continue. */
36 adios (const char *what
, const char *fmt
, ...)
41 advertise (what
, NULL
, fmt
, ap
);
48 /* admonish calls advertise() with a tail indicating the program
51 admonish (char *what
, char *fmt
, ...)
56 advertise (what
, "continuing...", fmt
, ap
);
61 /* advertise prints fmt and ap to stderr after flushing stdout.
62 * If invo_name isn't NULL or empty, it precedes the output seperated by ": ".
63 * If what isn't NULL, errno as a string is appended.
64 * A non-empty what separates fmt from errno, surrounded by " " and ": ".
65 * BUG: No space separator before errno if what is "".
66 * If tail isn't NULL or empty then ", " and tail are appended
67 * before the finishing "\n".
68 * In summary: "invo_name: fmt what: errno, tail\n". */
70 advertise (const char *what
, char *tail
, const char *fmt
, va_list ap
)
73 char buffer
[NMH_BUFSIZ
], errbuf
[NMH_BUFSIZ
], *err
;
74 struct iovec iob
[10], *iov
;
79 #define APPEND_IOV(p, len) \
80 iov->iov_base = (p); \
81 iov->iov_len = (len); \
84 #define ADD_LITERAL(s) APPEND_IOV((s), LEN(s))
85 #define ADD_VAR(s) APPEND_IOV((s), strlen(s))
87 if (invo_name
&& *invo_name
) {
92 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
97 ADD_VAR((void *)what
);
100 err
= strerror(eindex
);
102 /* this shouldn't happen, but we'll test for it just in case */
103 snprintf(errbuf
, sizeof(errbuf
), "Error %d", eindex
);
118 assert(niov
<= DIM(iob
));
122 if (writev(fileno(stderr
), iob
, niov
) == -1) {
123 snprintf(buffer
, sizeof buffer
, "%s: write stderr failed: %d\n",
124 invo_name
&& *invo_name
? invo_name
: "nmh", errno
);
125 if (write(2, buffer
, strlen(buffer
)) == -1) {
126 /* Ignore. if-statement needed to shut up compiler. */