Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 130 lines (116 sloc) 3.778 kB
2467806 @ChrisJohnsen LICENSE: Simplified BSD
authored
1 /*
2 * Copyright (c) 2011, Chris Johnsen <chris_johnsen@pobox.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
32 #include <string.h> /* strlen, strcpy, strerror */
33 #include <stdarg.h> /* va_... */
34 #include <stdio.h> /* vfprintf */
35 #include <stdlib.h> /* malloc, exit, free */
36 #include <sys/errno.h> /* errno */
37
a40ffca @ChrisJohnsen msg: make vmsg and vfmsg declarations match implementation
authored
38 #include "msg.h"
39
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
40 void vfmsg(FILE *f,
eea4ccd @ChrisJohnsen msg: make format arg last named argument
authored
41 const char *pre, const char *suf, const char *fmt,
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
42 va_list ap) {
43 int prelen = 0, fmtlen = 0, suflen = 0;
44 if (pre) prelen = strlen(pre);
45 if (fmt) fmtlen = strlen(fmt);
46 if (suf) suflen = strlen(suf);
fcb99cb @ChrisJohnsen msg: %-double prefix
authored
47 char *newfmt = malloc(
48 prelen*2 + /* %-doubled pre */
49 fmtlen +
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
50 2 + /* ':' and SP */
51 suflen*2 + /* %-doubled suf */
52 2 /* NL and NUL */
53 );
fcb99cb @ChrisJohnsen msg: %-double prefix
authored
54 if (!newfmt)
55 goto finish;
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
56
57 char *newfmt_end = newfmt;
fcb99cb @ChrisJohnsen msg: %-double prefix
authored
58 if (prelen)
59 while(*pre)
60 if ((*newfmt_end++ = *pre++) == '%')
61 *newfmt_end++ = '%';
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
62 if (fmtlen) {
63 strcpy(newfmt_end, fmt);
64 newfmt_end += fmtlen;
65 }
66 if (suflen) {
67 *newfmt_end++ = ':';
68 *newfmt_end++ = ' ';
69 while (*suf)
70 if ((*newfmt_end++ = *suf++) == '%')
71 *newfmt_end++ = '%';
72 }
73 *newfmt_end++ = '\n';
74 *newfmt_end++ = '\0';
fcb99cb @ChrisJohnsen msg: %-double prefix
authored
75 fmt = newfmt;
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
76
fcb99cb @ChrisJohnsen msg: %-double prefix
authored
77 finish:
78 vfprintf(f, fmt, ap);
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
79 fflush(f);
80
fcb99cb @ChrisJohnsen msg: %-double prefix
authored
81 if (newfmt)
82 free(newfmt);
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
83 }
84
85 FILE *msgout = NULL;
86
eea4ccd @ChrisJohnsen msg: make format arg last named argument
authored
87 void vmsg(const char *pre, const char *suf, const char *fmt, va_list ap) {
88 vfmsg(msgout ? msgout : stderr, pre, suf, fmt, ap);
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
89 }
90
91 void msg(const char *fmt, ...) {
92 va_list ap;
93 va_start(ap, fmt);
eea4ccd @ChrisJohnsen msg: make format arg last named argument
authored
94 vmsg(NULL, NULL, fmt, ap);
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
95 va_end(ap);
96 }
97
98 void warn(const char *fmt, ...) {
99 va_list ap;
100 va_start(ap, fmt);
eea4ccd @ChrisJohnsen msg: make format arg last named argument
authored
101 vmsg("warning: ", NULL, fmt, ap);
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
102 va_end(ap);
103 }
104
7420a3f @ChrisJohnsen wrapper: make errors fall through to exec()
authored
105 void warn_errno(const char *fmt, ...) {
106 int err = errno; /* just in case it gets clobbered */
107 va_list ap;
108 va_start(ap, fmt);
109 vmsg("warning: ", strerror(err), fmt, ap);
110 va_end(ap);
111 errno = err;
112 }
113
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
114 void die(int ev, const char *fmt, ...) {
115 va_list ap;
116 va_start(ap, fmt);
eea4ccd @ChrisJohnsen msg: make format arg last named argument
authored
117 vmsg("fatal: ", NULL, fmt, ap);
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
118 va_end(ap);
119 exit(ev);
120 }
121
122 void die_errno(int ev, const char *fmt, ...) {
123 int err = errno; /* just in case it gets clobbered */
124 va_list ap;
125 va_start(ap, fmt);
eea4ccd @ChrisJohnsen msg: make format arg last named argument
authored
126 vmsg("fatal: ", strerror(err), fmt, ap);
fc41630 @ChrisJohnsen wrapper: extract die, etc. into msg.c
authored
127 va_end(ap);
128 exit(ev);
129 }
Something went wrong with that request. Please try again.