fmtmsg.c revision 101403
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: head/lib/libc/gen/fmtmsg.c 101403 2002-08-05 19:36:09Z mike $");
29
30#include <fmtmsg.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35/* Default value for MSGVERB. */
36#define	DFLT_MSGVERB	"label:severity:text:action:tag"
37
38/* Maximum valid size for a MSGVERB. */
39#define	MAX_MSGVERB	sizeof(DFLT_MSGVERB)
40
41static char	*printfmt(char *, long, const char *, int, const char *,
42		    const char *, const char *);
43static char	*nextcomp(const char *);
44static const char
45		*sevinfo(int);
46static int	 validmsgverb(const char *);
47
48static const char *validlist[] = {
49	"label", "severity", "text", "action", "tag", NULL
50};
51
52int
53fmtmsg(long class, const char *label, int sev, const char *text,
54    const char *action, const char *tag)
55{
56	FILE *fp;
57	char *env, *msgverb, *output;
58
59	if (class & MM_PRINT) {
60		if ((env = getenv("MSGVERB")) != NULL && *env != '\0' &&
61		    strlen(env) <= strlen(DFLT_MSGVERB)) {
62			if ((msgverb = strdup(env)) == NULL)
63				return (MM_NOTOK);
64			else if (validmsgverb(msgverb) == 0) {
65				free(msgverb);
66				goto def;
67			}
68		} else {
69def:
70			if ((msgverb = strdup(DFLT_MSGVERB)) == NULL)
71				return (MM_NOTOK);
72		}
73		output = printfmt(msgverb, class, label, sev, text, action,
74		    tag);
75		if (output == NULL)
76			return (MM_NOTOK);
77		if (*output != '\0')
78			fprintf(stderr, "%s", output);
79		free(msgverb);
80		free(output);
81	}
82	if (class & MM_CONSOLE) {
83		output = printfmt(DFLT_MSGVERB, class, label, sev, text,
84		    action, tag);
85		if (output == NULL)
86			return (MM_NOCON);
87		if (*output != '\0') {
88			if ((fp = fopen("/dev/console", "a")) == NULL) {
89				free(output);
90				return (MM_NOCON);
91			}
92			fprintf(fp, "%s", output);
93			fclose(fp);
94		}
95		free(output);
96	}
97	return (MM_OK);
98}
99
100#define INSERT_COLON							\
101	if (*output != '\0')						\
102		strlcat(output, ": ", size)
103#define INSERT_NEWLINE							\
104	if (*output != '\0')						\
105		strlcat(output, "\n", size)
106#define INSERT_SPACE							\
107	if (*output != '\0')						\
108		strlcat(output, " ", size)
109
110/*
111 * Returns NULL on memory allocation failure, otherwise returns a pointer to
112 * a newly malloc()'d output buffer.
113 */
114static char *
115printfmt(char *msgverb, long class, const char *label, int sev,
116    const char *text, const char *act, const char *tag)
117{
118	size_t size;
119	char *comp, *output;
120	const char *sevname;
121
122	size = 32;
123	if (label != MM_NULLLBL)
124		size += strlen(label);
125	if ((sevname = sevinfo(sev)) != NULL)
126		size += strlen(sevname);
127	if (text != MM_NULLTXT)
128		size += strlen(text);
129	if (text != MM_NULLACT)
130		size += strlen(act);
131	if (tag != MM_NULLTAG)
132		size += strlen(tag);
133
134	if ((output = malloc(size)) == NULL)
135		return (NULL);
136	*output = '\0';
137	while ((comp = nextcomp(msgverb)) != NULL) {
138		if (strcmp(comp, "label") == 0 && label != MM_NULLLBL) {
139			INSERT_COLON;
140			strlcat(output, label, size);
141		} else if (strcmp(comp, "severity") == 0 && sevname != NULL) {
142			INSERT_COLON;
143			strlcat(output, sevinfo(sev), size);
144		} else if (strcmp(comp, "text") == 0 && text != MM_NULLTXT) {
145			INSERT_COLON;
146			strlcat(output, text, size);
147		} else if (strcmp(comp, "action") == 0 && act != MM_NULLACT) {
148			INSERT_NEWLINE;
149			strlcat(output, "TO FIX: ", size);
150			strlcat(output, act, size);
151		} else if (strcmp(comp, "tag") == 0 && tag != MM_NULLTAG) {
152			INSERT_SPACE;
153			strlcat(output, tag, size);
154		}
155	}
156	INSERT_NEWLINE;
157	return (output);
158}
159
160/*
161 * Returns a component of a colon delimited string.  NULL is returned to
162 * indicate that there are no remaining components.  This function must be
163 * called until it returns NULL in order for the local state to be cleared.
164 */
165static char *
166nextcomp(const char *msgverb)
167{
168	static char lmsgverb[MAX_MSGVERB], *state;
169	char *retval;
170
171	if (*lmsgverb == '\0') {
172		strlcpy(lmsgverb, msgverb, sizeof(lmsgverb));
173		retval = strtok_r(lmsgverb, ":", &state);
174	} else {
175		retval = strtok_r(NULL, ":", &state);
176	}
177	if (retval == NULL)
178		*lmsgverb = '\0';
179	return (retval);
180}
181
182static const char *
183sevinfo(int sev)
184{
185
186	switch (sev) {
187	case MM_HALT:
188		return ("HALT");
189	case MM_ERROR:
190		return ("ERROR");
191	case MM_WARNING:
192		return ("WARNING");
193	case MM_INFO:
194		return ("INFO");
195	default:
196		return (NULL);
197	}
198}
199
200/*
201 * Returns 1 if the msgverb list is valid, otherwise 0.
202 */
203static int
204validmsgverb(const char *msgverb)
205{
206	char *msgcomp;
207	int i, equality;
208
209	equality = 0;
210	while ((msgcomp = nextcomp(msgverb)) != NULL) {
211		equality--;
212		for (i = 0; validlist[i] != NULL; i++) {
213			if (strcmp(msgcomp, validlist[i]) == 0)
214				equality++;
215		}
216	}
217	return (!equality);
218}
219