]> diplodocus.org Git - nmh/blob - sbr/error.c
pending-release-notes: add mhshow's "-prefer", and mh-format's %(kibi/kilo)
[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 /*
17 * print out error message
18 */
19 void
20 advise (char *what, char *fmt, ...)
21 {
22 va_list ap;
23
24 va_start(ap, fmt);
25 advertise (what, NULL, fmt, ap);
26 va_end(ap);
27 }
28
29
30 /*
31 * print out error message and exit
32 */
33 void
34 adios (char *what, char *fmt, ...)
35 {
36 va_list ap;
37
38 va_start(ap, fmt);
39 advertise (what, NULL, fmt, ap);
40 va_end(ap);
41 done (1);
42 }
43
44
45 /*
46 * admonish the user
47 */
48 void
49 admonish (char *what, char *fmt, ...)
50 {
51 va_list ap;
52
53 va_start(ap, fmt);
54 advertise (what, "continuing...", fmt, ap);
55 va_end(ap);
56 }
57
58
59 /*
60 * main routine for printing error messages.
61 */
62 void
63 advertise (char *what, char *tail, char *fmt, va_list ap)
64 {
65 int eindex = errno;
66 char buffer[BUFSIZ], err[BUFSIZ];
67 struct iovec iob[20], *iov;
68
69 fflush (stdout);
70
71 fflush (stderr);
72 iov = iob;
73
74 if (invo_name && *invo_name) {
75 iov->iov_len = strlen (iov->iov_base = invo_name);
76 iov++;
77 iov->iov_len = strlen (iov->iov_base = ": ");
78 iov++;
79 }
80
81 vsnprintf (buffer, sizeof(buffer), fmt, ap);
82 iov->iov_len = strlen (iov->iov_base = buffer);
83 iov++;
84 if (what) {
85 if (*what) {
86 iov->iov_len = strlen (iov->iov_base = " ");
87 iov++;
88 iov->iov_len = strlen (iov->iov_base = what);
89 iov++;
90 iov->iov_len = strlen (iov->iov_base = ": ");
91 iov++;
92 }
93 if (!(iov->iov_base = strerror (eindex))) {
94 /* this shouldn't happen, but we'll test for it just in case */
95 snprintf (err, sizeof(err), "Error %d", eindex);
96 iov->iov_base = err;
97 }
98 iov->iov_len = strlen (iov->iov_base);
99 iov++;
100 }
101 if (tail && *tail) {
102 iov->iov_len = strlen (iov->iov_base = ", ");
103 iov++;
104 iov->iov_len = strlen (iov->iov_base = tail);
105 iov++;
106 }
107 iov->iov_len = strlen (iov->iov_base = "\n");
108 iov++;
109 if (writev (fileno (stderr), iob, iov - iob) < 0) {
110 advise ("stderr", "writev");
111 }
112 }