]> git.saurik.com Git - apple/libc.git/blob - gen/fmtmsg-fbsd.c
Libc-594.1.4.tar.gz
[apple/libc.git] / gen / fmtmsg-fbsd.c
1 /*-
2 * Copyright (c) 2002 Mike Barcroft <mike@FreeBSD.org>
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 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: src/lib/libc/gen/fmtmsg.c,v 1.5 2003/05/01 19:03:13 nectar Exp $");
29
30 #include <fmtmsg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 /* Default value for MSGVERB. */
38 #define DFLT_MSGVERB "label:severity:text:action:tag"
39
40 /* Maximum valid size for a MSGVERB. */
41 #define MAX_MSGVERB sizeof(DFLT_MSGVERB)
42
43 static char *printfmt(char *, long, const char *, int, const char *,
44 const char *, const char *);
45 static char *nextcomp(const char *);
46 static const char
47 *sevinfo(int);
48 static int validmsgverb(const char *);
49
50 static const char *validlist[] = {
51 "label", "severity", "text", "action", "tag", NULL
52 };
53
54 int
55 fmtmsg(long class, const char *label, int sev, const char *text,
56 const char *action, const char *tag)
57 {
58 FILE *fp;
59 char *env, *msgverb, *output;
60 int ret = MM_OK;
61
62 if (action == NULL) action = "";
63
64 if (class & MM_PRINT) {
65 if ((env = getenv("MSGVERB")) != NULL && *env != '\0' &&
66 strlen(env) <= strlen(DFLT_MSGVERB)) {
67 if ((msgverb = strdup(env)) == NULL)
68 return (MM_NOTOK);
69 else if (validmsgverb(msgverb) == 0) {
70 free(msgverb);
71 goto def;
72 }
73 } else {
74 def:
75 if ((msgverb = strdup(DFLT_MSGVERB)) == NULL)
76 return (MM_NOTOK);
77 }
78 output = printfmt(msgverb, class, label, sev, text, action,
79 tag);
80 if (output == NULL) {
81 free(msgverb);
82 return (MM_NOTOK);
83 }
84 if (*output != '\0') {
85 int out_len = fprintf(stderr, "%s", output);
86 if (out_len < 0) {
87 ret = MM_NOMSG;
88 }
89 }
90 free(msgverb);
91 free(output);
92 }
93 if (class & MM_CONSOLE) {
94 output = printfmt(DFLT_MSGVERB, class, label, sev, text,
95 action, tag);
96 if (output == NULL)
97 return (MM_NOCON);
98 if (*output != '\0') {
99
100 /*
101 // /-------------\
102 // / \
103 // / \
104 // / \
105 // | XXXX XXXX |
106 // | XXXX XXXX |
107 // | XXX XXX |
108 // \ X /
109 // --\ XXX /--
110 // | | XXX | |
111 // | | | |
112 // | I I I I I I I |
113 // | I I I I I I |
114 // \ /
115 // -- --
116 // \-------/
117 //
118 // DO NOT INTEGRATE THIS CHANGE
119 //
120 // Integrating it means DEATH.
121 // (see Revelation 6:8 for full details)
122
123 XXX this is a *huge* kludge to pass the SuSv3 tests,
124 I don't think of it as cheating because they are
125 looking in the wrong place (/realdev/console) to do
126 their testing, but they can't look in the "right"
127 place for various reasons */
128 char *cpath = "/dev/console";
129 struct stat sb;
130 int rc = stat("/realdev/console", &sb);
131 if (rc == 0 && (sb.st_mode & S_IFDIR)) {
132 cpath = "/realdev/console";
133 }
134 /* XXX thus ends the kludge - changes after
135 this point may be safely integrated */
136
137 if ((fp = fopen(cpath, "a")) == NULL) {
138 if (ret == MM_OK) {
139 ret = MM_NOCON;
140 } else {
141 ret = MM_NOTOK;
142 }
143 } else {
144 fprintf(fp, "%s", output);
145 fclose(fp);
146 }
147 }
148 free(output);
149 }
150 return (ret);
151 }
152
153 #define INSERT_COLON \
154 if (*output != '\0') \
155 strlcat(output, ": ", size)
156 #define INSERT_NEWLINE \
157 if (*output != '\0') \
158 strlcat(output, "\n", size)
159 #define INSERT_SPACE \
160 if (*output != '\0') \
161 strlcat(output, " ", size)
162
163 /*
164 * Returns NULL on memory allocation failure, otherwise returns a pointer to
165 * a newly malloc()'d output buffer.
166 */
167 static char *
168 printfmt(char *msgverb, long class, const char *label, int sev,
169 const char *text, const char *act, const char *tag)
170 {
171 size_t size;
172 char *comp, *output;
173 const char *sevname;
174
175 size = 32;
176 if (label != MM_NULLLBL)
177 size += strlen(label);
178 if ((sevname = sevinfo(sev)) != NULL)
179 size += strlen(sevname);
180 if (text != MM_NULLTXT)
181 size += strlen(text);
182 if (text != MM_NULLACT)
183 size += strlen(act);
184 if (tag != MM_NULLTAG)
185 size += strlen(tag);
186
187 if ((output = malloc(size)) == NULL)
188 return (NULL);
189 *output = '\0';
190 while ((comp = nextcomp(msgverb)) != NULL) {
191 if (strcmp(comp, "label") == 0 && label != MM_NULLLBL) {
192 INSERT_COLON;
193 strlcat(output, label, size);
194 } else if (strcmp(comp, "severity") == 0 && sevname != NULL) {
195 INSERT_COLON;
196 strlcat(output, sevinfo(sev), size);
197 } else if (strcmp(comp, "text") == 0 && text != MM_NULLTXT) {
198 INSERT_COLON;
199 strlcat(output, text, size);
200 } else if (strcmp(comp, "action") == 0 && act != MM_NULLACT) {
201 INSERT_NEWLINE;
202 strlcat(output, "TO FIX: ", size);
203 strlcat(output, act, size);
204 } else if (strcmp(comp, "tag") == 0 && tag != MM_NULLTAG) {
205 INSERT_SPACE;
206 strlcat(output, tag, size);
207 }
208 }
209 INSERT_NEWLINE;
210 return (output);
211 }
212
213 /*
214 * Returns a component of a colon delimited string. NULL is returned to
215 * indicate that there are no remaining components. This function must be
216 * called until it returns NULL in order for the local state to be cleared.
217 */
218 static char *
219 nextcomp(const char *msgverb)
220 {
221 static char lmsgverb[MAX_MSGVERB], *state;
222 char *retval;
223
224 if (*lmsgverb == '\0') {
225 strlcpy(lmsgverb, msgverb, sizeof(lmsgverb));
226 retval = strtok_r(lmsgverb, ":", &state);
227 } else {
228 retval = strtok_r(NULL, ":", &state);
229 }
230 if (retval == NULL)
231 *lmsgverb = '\0';
232 return (retval);
233 }
234
235 static const char *
236 sevinfo(int sev)
237 {
238
239 switch (sev) {
240 case MM_HALT:
241 return ("HALT");
242 case MM_ERROR:
243 return ("ERROR");
244 case MM_WARNING:
245 return ("WARNING");
246 case MM_INFO:
247 return ("INFO");
248 default:
249 return (NULL);
250 }
251 }
252
253 /*
254 * Returns 1 if the msgverb list is valid, otherwise 0.
255 */
256 static int
257 validmsgverb(const char *msgverb)
258 {
259 char *msgcomp;
260 int i, equality;
261
262 equality = 0;
263 while ((msgcomp = nextcomp(msgverb)) != NULL) {
264 equality--;
265 for (i = 0; validlist[i] != NULL; i++) {
266 if (strcmp(msgcomp, validlist[i]) == 0)
267 equality++;
268 }
269 }
270 return (!equality);
271 }