1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
/*
* error.c -- main error handling routines
*
* This code is Copyright (c) 2002, by the authors of nmh. See the
* COPYRIGHT file in the root directory of the nmh distribution for
* complete copyright information.
*/
#include <h/mh.h>
#include <sys/types.h>
#include <sys/uio.h>
/*
* print out error message
*/
void
advise (char *what, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
advertise (what, NULL, fmt, ap);
va_end(ap);
}
/*
* print out error message and exit
*/
void
adios (char *what, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
advertise (what, NULL, fmt, ap);
va_end(ap);
done (1);
}
/*
* admonish the user
*/
void
admonish (char *what, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
advertise (what, "continuing...", fmt, ap);
va_end(ap);
}
/*
* main routine for printing error messages.
*/
void
advertise (char *what, char *tail, char *fmt, va_list ap)
{
int eindex = errno;
char buffer[BUFSIZ], err[BUFSIZ];
struct iovec iob[20], *iov;
fflush (stdout);
fflush (stderr);
iov = iob;
if (invo_name && *invo_name) {
iov->iov_len = strlen (iov->iov_base = invo_name);
iov++;
iov->iov_len = strlen (iov->iov_base = ": ");
iov++;
}
vsnprintf (buffer, sizeof(buffer), fmt, ap);
iov->iov_len = strlen (iov->iov_base = buffer);
iov++;
if (what) {
if (*what) {
iov->iov_len = strlen (iov->iov_base = " ");
iov++;
iov->iov_len = strlen (iov->iov_base = what);
iov++;
iov->iov_len = strlen (iov->iov_base = ": ");
iov++;
}
if (!(iov->iov_base = strerror (eindex))) {
/* this shouldn't happen, but we'll test for it just in case */
snprintf (err, sizeof(err), "Error %d", eindex);
iov->iov_base = err;
}
iov->iov_len = strlen (iov->iov_base);
iov++;
}
if (tail && *tail) {
iov->iov_len = strlen (iov->iov_base = ", ");
iov++;
iov->iov_len = strlen (iov->iov_base = tail);
iov++;
}
iov->iov_len = strlen (iov->iov_base = "\n");
iov++;
writev (fileno (stderr), iob, iov - iob);
}
|