1 /* $OpenBSD: errwarn.c,v 1.15 2007/03/02 11:31:17 henning Exp $ */
2 /* $DragonFly: src/sbin/dhclient/errwarn.c,v 1.1 2008/08/30 16:07:58 hasso Exp $ */
4 /* Errors and warnings... */
7 * Copyright (c) 1996 The Internet Software Consortium.
9 * Copyright (c) 1995 RadioMail Corporation. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of RadioMail Corporation, the Internet Software
21 * Consortium nor the names of its contributors may be used to endorse
22 * or promote products derived from this software without specific
23 * prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY RADIOMAIL CORPORATION, THE INTERNET
26 * SOFTWARE CONSORTIUM AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL RADIOMAIL CORPORATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
36 * OF THE POSSIBILITY OF SUCH DAMAGE.
38 * This software was written for RadioMail Corporation by Ted Lemon
39 * under a contract with Vixie Enterprises. Further modifications have
40 * been made for the Internet Software Consortium under a contract
41 * with Vixie Laboratories.
44 #include <sys/types.h>
52 static void do_percentm(char *obuf, size_t size, char *ibuf);
54 static char mbuf[1024];
55 static char fbuf[1024];
57 int warnings_occurred;
60 * Log an error message, then exit.
67 do_percentm(fbuf, sizeof(fbuf), fmt);
70 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
74 syslog(LOG_ERR, "%s", mbuf);
77 /* Also log it to stderr? */
79 write(STDERR_FILENO, mbuf, strlen(mbuf));
80 write(STDERR_FILENO, "\n", 1);
83 syslog(LOG_CRIT, "exiting.");
85 fprintf(stderr, "exiting.\n");
92 * Log a warning message...
95 warning(char *fmt, ...)
99 do_percentm(fbuf, sizeof(fbuf), fmt);
102 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
106 syslog(LOG_ERR, "%s", mbuf);
110 write(STDERR_FILENO, mbuf, strlen(mbuf));
111 write(STDERR_FILENO, "\n", 1);
125 do_percentm(fbuf, sizeof(fbuf), fmt);
128 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
132 syslog(LOG_INFO, "%s", mbuf);
136 write(STDERR_FILENO, mbuf, strlen(mbuf));
137 write(STDERR_FILENO, "\n", 1);
144 * Log a debug message...
147 debug(char *fmt, ...)
151 do_percentm(fbuf, sizeof(fbuf), fmt);
154 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
158 syslog(LOG_DEBUG, "%s", mbuf);
162 write(STDERR_FILENO, mbuf, strlen(mbuf));
163 write(STDERR_FILENO, "\n", 1);
170 * Find %m in the input string and substitute an error message string.
173 do_percentm(char *obuf, size_t size, char *ibuf)
180 int saved_errno = errno;
183 * We wouldn't need this mess if printf handled %m, or if
184 * strerror() had been invented before syslog().
186 for (fmt_left = size; (ch = *s); ++s) {
187 if (ch == '%' && s[1] == 'm') {
189 prlen = snprintf(t, fmt_left, "%s",
190 strerror(saved_errno));
193 else if (prlen >= fmt_left)
194 prlen = fmt_left - 1;
208 parse_warn(char *fmt, ...)
211 static char spaces[] =
217 do_percentm(mbuf, sizeof(mbuf), fmt);
218 snprintf(fbuf, sizeof(fbuf), "%s line %d: %s", tlname, lexline, mbuf);
220 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
224 syslog(LOG_ERR, "%s", mbuf);
225 syslog(LOG_ERR, "%s", token_line);
227 syslog(LOG_ERR, "%*c", lexchar, '^');
231 iov[0].iov_base = mbuf;
232 iov[0].iov_len = strlen(mbuf);
233 iov[1].iov_base = "\n";
235 iov[2].iov_base = token_line;
236 iov[2].iov_len = strlen(token_line);
237 iov[3].iov_base = "\n";
241 iov[4].iov_base = spaces;
242 iov[4].iov_len = lexchar - 1;
243 iov[5].iov_base = "^\n";
247 writev(STDERR_FILENO, iov, iovcnt);
249 warnings_occurred = 1;