]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/fmtmsg.c
Libc-1439.40.11.tar.gz
[apple/libc.git] / gen / FreeBSD / fmtmsg.c
CommitLineData
59e0d9fe
A
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>
1f2f436a 28__FBSDID("$FreeBSD: src/lib/libc/gen/fmtmsg.c,v 1.6 2009/11/08 14:02:54 brueffer Exp $");
59e0d9fe
A
29
30#include <fmtmsg.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
ad3c9f2a
A
34#include <sys/types.h>
35#include <sys/stat.h>
59e0d9fe
A
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
43static char *printfmt(char *, long, const char *, int, const char *,
44 const char *, const char *);
45static char *nextcomp(const char *);
46static const char
47 *sevinfo(int);
48static int validmsgverb(const char *);
49
6465356a 50static const char * const validlist[] = {
59e0d9fe
A
51 "label", "severity", "text", "action", "tag", NULL
52};
53
54int
55fmtmsg(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;
ad3c9f2a
A
60 int ret = MM_OK;
61
62 if (action == NULL) action = "";
59e0d9fe
A
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 {
74def:
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 }
ad3c9f2a
A
84 if (*output != '\0') {
85 int out_len = fprintf(stderr, "%s", output);
86 if (out_len < 0) {
87 ret = MM_NOMSG;
88 }
89 }
59e0d9fe
A
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') {
ad3c9f2a
A
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
a9aaacca 125 looking in the wrong place (/var/log/console) to do
ad3c9f2a
A
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;
a9aaacca 130 int rc = stat("/var/log/console", &sb);
ad3c9f2a 131 if (rc == 0 && (sb.st_mode & S_IFDIR)) {
a9aaacca 132 cpath = "/var/log/console";
ad3c9f2a
A
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);
59e0d9fe 146 }
59e0d9fe
A
147 }
148 free(output);
149 }
ad3c9f2a 150 return (ret);
59e0d9fe
A
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 */
167static char *
168printfmt(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);
1f2f436a 182 if (act != MM_NULLACT)
59e0d9fe
A
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 */
218static char *
219nextcomp(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
235static const char *
236sevinfo(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 */
256static int
257validmsgverb(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}