fmtmsg.c revision 101403
1101353Smike/*-
2101353Smike * Copyright (c) 2002 Mike Barcroft <mike@FreeBSD.org>
3101353Smike * All rights reserved.
4101353Smike *
5101353Smike * Redistribution and use in source and binary forms, with or without
6101353Smike * modification, are permitted provided that the following conditions
7101353Smike * are met:
8101353Smike * 1. Redistributions of source code must retain the above copyright
9101353Smike *    notice, this list of conditions and the following disclaimer.
10101353Smike * 2. Redistributions in binary form must reproduce the above copyright
11101353Smike *    notice, this list of conditions and the following disclaimer in the
12101353Smike *    documentation and/or other materials provided with the distribution.
13101353Smike *
14101353Smike * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15101353Smike * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16101353Smike * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17101353Smike * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18101353Smike * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19101353Smike * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20101353Smike * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21101353Smike * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22101353Smike * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23101353Smike * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24101353Smike * SUCH DAMAGE.
25101353Smike */
26101353Smike
27101353Smike#include <sys/cdefs.h>
28101353Smike__FBSDID("$FreeBSD: head/lib/libc/gen/fmtmsg.c 101403 2002-08-05 19:36:09Z mike $");
29101353Smike
30101353Smike#include <fmtmsg.h>
31101353Smike#include <stdio.h>
32101353Smike#include <stdlib.h>
33101353Smike#include <string.h>
34101353Smike
35101353Smike/* Default value for MSGVERB. */
36101353Smike#define	DFLT_MSGVERB	"label:severity:text:action:tag"
37101353Smike
38101353Smike/* Maximum valid size for a MSGVERB. */
39101353Smike#define	MAX_MSGVERB	sizeof(DFLT_MSGVERB)
40101353Smike
41101353Smikestatic char	*printfmt(char *, long, const char *, int, const char *,
42101353Smike		    const char *, const char *);
43101353Smikestatic char	*nextcomp(const char *);
44101353Smikestatic const char
45101353Smike		*sevinfo(int);
46101353Smikestatic int	 validmsgverb(const char *);
47101353Smike
48101353Smikestatic const char *validlist[] = {
49101353Smike	"label", "severity", "text", "action", "tag", NULL
50101353Smike};
51101353Smike
52101353Smikeint
53101353Smikefmtmsg(long class, const char *label, int sev, const char *text,
54101353Smike    const char *action, const char *tag)
55101353Smike{
56101353Smike	FILE *fp;
57101353Smike	char *env, *msgverb, *output;
58101353Smike
59101353Smike	if (class & MM_PRINT) {
60101353Smike		if ((env = getenv("MSGVERB")) != NULL && *env != '\0' &&
61101353Smike		    strlen(env) <= strlen(DFLT_MSGVERB)) {
62101353Smike			if ((msgverb = strdup(env)) == NULL)
63101353Smike				return (MM_NOTOK);
64101403Smike			else if (validmsgverb(msgverb) == 0) {
65101403Smike				free(msgverb);
66101353Smike				goto def;
67101403Smike			}
68101353Smike		} else {
69101353Smikedef:
70101353Smike			if ((msgverb = strdup(DFLT_MSGVERB)) == NULL)
71101353Smike				return (MM_NOTOK);
72101353Smike		}
73101353Smike		output = printfmt(msgverb, class, label, sev, text, action,
74101353Smike		    tag);
75101353Smike		if (output == NULL)
76101353Smike			return (MM_NOTOK);
77101353Smike		if (*output != '\0')
78101353Smike			fprintf(stderr, "%s", output);
79101353Smike		free(msgverb);
80101353Smike		free(output);
81101353Smike	}
82101353Smike	if (class & MM_CONSOLE) {
83101353Smike		output = printfmt(DFLT_MSGVERB, class, label, sev, text,
84101353Smike		    action, tag);
85101353Smike		if (output == NULL)
86101353Smike			return (MM_NOCON);
87101353Smike		if (*output != '\0') {
88101353Smike			if ((fp = fopen("/dev/console", "a")) == NULL) {
89101353Smike				free(output);
90101353Smike				return (MM_NOCON);
91101353Smike			}
92101353Smike			fprintf(fp, "%s", output);
93101353Smike			fclose(fp);
94101353Smike		}
95101353Smike		free(output);
96101353Smike	}
97101353Smike	return (MM_OK);
98101353Smike}
99101353Smike
100101353Smike#define INSERT_COLON							\
101101353Smike	if (*output != '\0')						\
102101353Smike		strlcat(output, ": ", size)
103101353Smike#define INSERT_NEWLINE							\
104101353Smike	if (*output != '\0')						\
105101353Smike		strlcat(output, "\n", size)
106101353Smike#define INSERT_SPACE							\
107101353Smike	if (*output != '\0')						\
108101353Smike		strlcat(output, " ", size)
109101353Smike
110101353Smike/*
111101353Smike * Returns NULL on memory allocation failure, otherwise returns a pointer to
112101353Smike * a newly malloc()'d output buffer.
113101353Smike */
114101353Smikestatic char *
115101353Smikeprintfmt(char *msgverb, long class, const char *label, int sev,
116101353Smike    const char *text, const char *act, const char *tag)
117101353Smike{
118101353Smike	size_t size;
119101353Smike	char *comp, *output;
120101353Smike	const char *sevname;
121101353Smike
122101353Smike	size = 32;
123101353Smike	if (label != MM_NULLLBL)
124101353Smike		size += strlen(label);
125101353Smike	if ((sevname = sevinfo(sev)) != NULL)
126101353Smike		size += strlen(sevname);
127101353Smike	if (text != MM_NULLTXT)
128101353Smike		size += strlen(text);
129101353Smike	if (text != MM_NULLACT)
130101353Smike		size += strlen(act);
131101353Smike	if (tag != MM_NULLTAG)
132101353Smike		size += strlen(tag);
133101353Smike
134101353Smike	if ((output = malloc(size)) == NULL)
135101353Smike		return (NULL);
136101353Smike	*output = '\0';
137101353Smike	while ((comp = nextcomp(msgverb)) != NULL) {
138101353Smike		if (strcmp(comp, "label") == 0 && label != MM_NULLLBL) {
139101353Smike			INSERT_COLON;
140101353Smike			strlcat(output, label, size);
141101353Smike		} else if (strcmp(comp, "severity") == 0 && sevname != NULL) {
142101353Smike			INSERT_COLON;
143101353Smike			strlcat(output, sevinfo(sev), size);
144101353Smike		} else if (strcmp(comp, "text") == 0 && text != MM_NULLTXT) {
145101353Smike			INSERT_COLON;
146101353Smike			strlcat(output, text, size);
147101353Smike		} else if (strcmp(comp, "action") == 0 && act != MM_NULLACT) {
148101353Smike			INSERT_NEWLINE;
149101353Smike			strlcat(output, "TO FIX: ", size);
150101353Smike			strlcat(output, act, size);
151101353Smike		} else if (strcmp(comp, "tag") == 0 && tag != MM_NULLTAG) {
152101353Smike			INSERT_SPACE;
153101353Smike			strlcat(output, tag, size);
154101353Smike		}
155101353Smike	}
156101353Smike	INSERT_NEWLINE;
157101353Smike	return (output);
158101353Smike}
159101353Smike
160101403Smike/*
161101403Smike * Returns a component of a colon delimited string.  NULL is returned to
162101403Smike * indicate that there are no remaining components.  This function must be
163101403Smike * called until it returns NULL in order for the local state to be cleared.
164101403Smike */
165101353Smikestatic char *
166101353Smikenextcomp(const char *msgverb)
167101353Smike{
168101353Smike	static char lmsgverb[MAX_MSGVERB], *state;
169101353Smike	char *retval;
170101353Smike
171101353Smike	if (*lmsgverb == '\0') {
172101353Smike		strlcpy(lmsgverb, msgverb, sizeof(lmsgverb));
173101353Smike		retval = strtok_r(lmsgverb, ":", &state);
174101353Smike	} else {
175101353Smike		retval = strtok_r(NULL, ":", &state);
176101353Smike	}
177101353Smike	if (retval == NULL)
178101353Smike		*lmsgverb = '\0';
179101353Smike	return (retval);
180101353Smike}
181101353Smike
182101353Smikestatic const char *
183101353Smikesevinfo(int sev)
184101353Smike{
185101353Smike
186101353Smike	switch (sev) {
187101353Smike	case MM_HALT:
188101353Smike		return ("HALT");
189101353Smike	case MM_ERROR:
190101353Smike		return ("ERROR");
191101353Smike	case MM_WARNING:
192101353Smike		return ("WARNING");
193101353Smike	case MM_INFO:
194101353Smike		return ("INFO");
195101353Smike	default:
196101353Smike		return (NULL);
197101353Smike	}
198101353Smike}
199101353Smike
200101353Smike/*
201101353Smike * Returns 1 if the msgverb list is valid, otherwise 0.
202101353Smike */
203101353Smikestatic int
204101353Smikevalidmsgverb(const char *msgverb)
205101353Smike{
206101353Smike	char *msgcomp;
207101403Smike	int i, equality;
208101353Smike
209101403Smike	equality = 0;
210101353Smike	while ((msgcomp = nextcomp(msgverb)) != NULL) {
211101403Smike		equality--;
212101403Smike		for (i = 0; validlist[i] != NULL; i++) {
213101403Smike			if (strcmp(msgcomp, validlist[i]) == 0)
214101403Smike				equality++;
215101353Smike		}
216101353Smike	}
217101403Smike	return (!equality);
218101353Smike}
219