fmtmsg.c revision 1.1 1 1.1 kleink /* $NetBSD: fmtmsg.c,v 1.1 1999/09/12 19:04:31 kleink Exp $ */
2 1.1 kleink
3 1.1 kleink /*-
4 1.1 kleink * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 kleink * All rights reserved.
6 1.1 kleink *
7 1.1 kleink * This code is derived from software contributed to The NetBSD Foundation
8 1.1 kleink * by Klaus Klein.
9 1.1 kleink *
10 1.1 kleink * Redistribution and use in source and binary forms, with or without
11 1.1 kleink * modification, are permitted provided that the following conditions
12 1.1 kleink * are met:
13 1.1 kleink * 1. Redistributions of source code must retain the above copyright
14 1.1 kleink * notice, this list of conditions and the following disclaimer.
15 1.1 kleink * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 kleink * notice, this list of conditions and the following disclaimer in the
17 1.1 kleink * documentation and/or other materials provided with the distribution.
18 1.1 kleink * 3. All advertising materials mentioning features or use of this software
19 1.1 kleink * must display the following acknowledgement:
20 1.1 kleink * This product includes software developed by the NetBSD
21 1.1 kleink * Foundation, Inc. and its contributors.
22 1.1 kleink * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 kleink * contributors may be used to endorse or promote products derived
24 1.1 kleink * from this software without specific prior written permission.
25 1.1 kleink *
26 1.1 kleink * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 kleink * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 kleink * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 kleink * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 kleink * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 kleink * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 kleink * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 kleink * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 kleink * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 kleink * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 kleink * POSSIBILITY OF SUCH DAMAGE.
37 1.1 kleink */
38 1.1 kleink
39 1.1 kleink #include <sys/cdefs.h>
40 1.1 kleink #include <fmtmsg.h>
41 1.1 kleink #include <paths.h>
42 1.1 kleink #include <stdio.h>
43 1.1 kleink #include <stdlib.h>
44 1.1 kleink #include <string.h>
45 1.1 kleink
46 1.1 kleink static unsigned int msgverb __P((const char *));
47 1.1 kleink static const char * severity2str __P((int));
48 1.1 kleink static int writeit __P((FILE *, unsigned int, const char *,
49 1.1 kleink const char *, const char *, const char *,
50 1.1 kleink const char *));
51 1.1 kleink
52 1.1 kleink #define MM_VERBLABEL 0x01U
53 1.1 kleink #define MM_VERBSEVERITY 0x02U
54 1.1 kleink #define MM_VERBTEXT 0x04U
55 1.1 kleink #define MM_VERBACTION 0x08U
56 1.1 kleink #define MM_VERBTAG 0x10U
57 1.1 kleink #define MM_VERBALL \
58 1.1 kleink (MM_VERBLABEL | MM_VERBSEVERITY | MM_VERBTEXT | MM_VERBACTION | \
59 1.1 kleink MM_VERBTAG)
60 1.1 kleink
61 1.1 kleink static const struct keyword {
62 1.1 kleink size_t len; /* strlen(keyword) */
63 1.1 kleink const char * const keyword;
64 1.1 kleink } keywords[] = {
65 1.1 kleink { 5, "label" }, /* log2(MM_VERBLABEL) */
66 1.1 kleink { 8, "severity" }, /* ... */
67 1.1 kleink { 4, "text" },
68 1.1 kleink { 6, "action" },
69 1.1 kleink { 3, "tag" } /* log2(MM_VERBTAG) */
70 1.1 kleink };
71 1.1 kleink
72 1.1 kleink static const size_t nkeywords = sizeof (keywords) / sizeof (keywords[0]);
73 1.1 kleink
74 1.1 kleink /*
75 1.1 kleink * Convert a colon-separated list of known keywords to a set of MM_VERB*
76 1.1 kleink * flags, defaulting to `all' if not set, empty, or in presence of unknown
77 1.1 kleink * keywords.
78 1.1 kleink */
79 1.1 kleink static unsigned int
80 1.1 kleink msgverb(str)
81 1.1 kleink const char *str;
82 1.1 kleink {
83 1.1 kleink int i;
84 1.1 kleink unsigned int result;
85 1.1 kleink
86 1.1 kleink if (str == NULL)
87 1.1 kleink return (MM_VERBALL);
88 1.1 kleink
89 1.1 kleink result = 0;
90 1.1 kleink while (*str != '\0') {
91 1.1 kleink for (i = 0; i < nkeywords; i++) {
92 1.1 kleink if (memcmp(str, keywords[i].keyword, keywords[i].len)
93 1.1 kleink == 0 &&
94 1.1 kleink (*(str + keywords[i].len) == ':' ||
95 1.1 kleink *(str + keywords[i].len) == '\0'))
96 1.1 kleink break;
97 1.1 kleink }
98 1.1 kleink if (i == nkeywords) {
99 1.1 kleink result = MM_VERBALL;
100 1.1 kleink break;
101 1.1 kleink }
102 1.1 kleink
103 1.1 kleink result |= (1 << i);
104 1.1 kleink if (*(str += keywords[i].len) == ':')
105 1.1 kleink str++; /* Advance */
106 1.1 kleink }
107 1.1 kleink if (result == 0)
108 1.1 kleink result = MM_VERBALL;
109 1.1 kleink
110 1.1 kleink return (result);
111 1.1 kleink }
112 1.1 kleink
113 1.1 kleink static const char * const severities[] = {
114 1.1 kleink "", /* MM_NONE */
115 1.1 kleink "HALT",
116 1.1 kleink "ERROR",
117 1.1 kleink "WARNING",
118 1.1 kleink "INFO"
119 1.1 kleink };
120 1.1 kleink
121 1.1 kleink static const size_t nseverities = sizeof (severities) / sizeof (severities[0]);
122 1.1 kleink
123 1.1 kleink /*
124 1.1 kleink * Returns the string representation associated with the numerical severity
125 1.1 kleink * value, defaulting to NULL for an unknown value.
126 1.1 kleink */
127 1.1 kleink static const char *
128 1.1 kleink severity2str(severity)
129 1.1 kleink int severity;
130 1.1 kleink {
131 1.1 kleink const char *result;
132 1.1 kleink
133 1.1 kleink if (severity < nseverities)
134 1.1 kleink result = severities[severity];
135 1.1 kleink else
136 1.1 kleink result = NULL;
137 1.1 kleink
138 1.1 kleink return (result);
139 1.1 kleink }
140 1.1 kleink
141 1.1 kleink /*
142 1.1 kleink * Format and write the message to the given stream, selecting those
143 1.1 kleink * components displayed from msgverb, returning the number of characters
144 1.1 kleink * written, or a negative value in case of an error.
145 1.1 kleink */
146 1.1 kleink static int
147 1.1 kleink writeit(stream, which, label, sevstr, text, action, tag)
148 1.1 kleink FILE *stream;
149 1.1 kleink unsigned int which;
150 1.1 kleink const char *label;
151 1.1 kleink const char *sevstr;
152 1.1 kleink const char *text;
153 1.1 kleink const char *action;
154 1.1 kleink const char *tag;
155 1.1 kleink {
156 1.1 kleink int nwritten;
157 1.1 kleink
158 1.1 kleink nwritten = fprintf(stream, "%s%s%s%s%s%s%s%s%s%s%s",
159 1.1 kleink ((which & MM_VERBLABEL) && label != MM_NULLLBL) ?
160 1.1 kleink label : "",
161 1.1 kleink ((which & MM_VERBLABEL) && label != MM_NULLLBL) ?
162 1.1 kleink ": " : "",
163 1.1 kleink (which & MM_VERBSEVERITY) ?
164 1.1 kleink sevstr : "",
165 1.1 kleink (which & MM_VERBSEVERITY) ?
166 1.1 kleink ": " : "",
167 1.1 kleink ((which & MM_VERBTEXT) && text != MM_NULLTXT) ?
168 1.1 kleink text : "",
169 1.1 kleink ((which & MM_VERBLABEL) && label != MM_NULLLBL) ||
170 1.1 kleink ((which & MM_VERBSEVERITY)) ||
171 1.1 kleink ((which & MM_VERBTEXT) && text != MM_NULLTXT) ?
172 1.1 kleink "\n" : "",
173 1.1 kleink ((which & MM_VERBACTION) && action != MM_NULLACT) ?
174 1.1 kleink "TO FIX: " : "",
175 1.1 kleink ((which & MM_VERBACTION) && action != MM_NULLACT) ?
176 1.1 kleink action : "",
177 1.1 kleink ((which & MM_VERBACTION) && label != MM_NULLACT) ?
178 1.1 kleink " " : "",
179 1.1 kleink ((which & MM_VERBTAG) && tag != MM_NULLTAG) ?
180 1.1 kleink tag : "",
181 1.1 kleink ((which & MM_VERBACTION) && action != MM_NULLACT) ||
182 1.1 kleink ((which & MM_VERBTAG) && tag != MM_NULLTAG) ?
183 1.1 kleink "\n" : "");
184 1.1 kleink
185 1.1 kleink return (nwritten);
186 1.1 kleink }
187 1.1 kleink
188 1.1 kleink int
189 1.1 kleink fmtmsg(classification, label, severity, text, action, tag)
190 1.1 kleink long classification;
191 1.1 kleink const char *label;
192 1.1 kleink int severity;
193 1.1 kleink const char *text;
194 1.1 kleink const char *action;
195 1.1 kleink const char *tag;
196 1.1 kleink {
197 1.1 kleink FILE *console;
198 1.1 kleink const char *p, *sevstr;
199 1.1 kleink int result;
200 1.1 kleink
201 1.1 kleink /* Validate label constraints, if not null. */
202 1.1 kleink if (label != MM_NULLLBL) {
203 1.1 kleink /*
204 1.1 kleink * Two fields, separated by a colon. The first field is up to
205 1.1 kleink * 10 bytes, the second is up to 14 bytes.
206 1.1 kleink */
207 1.1 kleink p = strchr(label, ':');
208 1.1 kleink if (p == NULL || p - label > 10 || strlen(p + 1) > 14)
209 1.1 kleink return (MM_NOTOK);
210 1.1 kleink }
211 1.1 kleink /* Validate severity argument. */
212 1.1 kleink if ((sevstr = severity2str(severity)) == NULL)
213 1.1 kleink return (MM_NOTOK);
214 1.1 kleink
215 1.1 kleink /*
216 1.1 kleink * Fact in search for a better place: XSH5 does not define any
217 1.1 kleink * functionality for `classification' bits other than the display
218 1.1 kleink * subclassification.
219 1.1 kleink */
220 1.1 kleink
221 1.1 kleink result = 0;
222 1.1 kleink
223 1.1 kleink if (classification & MM_PRINT) {
224 1.1 kleink if (writeit(stderr, msgverb(getenv("MSGVERB")),
225 1.1 kleink label, sevstr, text, action, tag) < 0)
226 1.1 kleink result |= MM_NOMSG;
227 1.1 kleink }
228 1.1 kleink /* Similar to MM_PRINT but ignoring $MSGVERB. */
229 1.1 kleink if (classification & MM_CONSOLE) {
230 1.1 kleink if ((console = fopen(_PATH_CONSOLE, "w")) != NULL) {
231 1.1 kleink if (writeit(console, MM_VERBALL,
232 1.1 kleink label, sevstr, text, action, tag) < 0)
233 1.1 kleink result |= MM_NOCON;
234 1.1 kleink /*
235 1.1 kleink * Ignore result: does not constitute ``generate a
236 1.1 kleink * console message.''
237 1.1 kleink */
238 1.1 kleink (void)fclose(console);
239 1.1 kleink } else {
240 1.1 kleink result |= MM_NOCON;
241 1.1 kleink }
242 1.1 kleink }
243 1.1 kleink
244 1.1 kleink if (result == (MM_NOMSG | MM_NOCON))
245 1.1 kleink result = MM_NOTOK;
246 1.1 kleink
247 1.1 kleink return (result == 0 ? MM_OK : result);
248 1.1 kleink }
249