Update.
[glibc.git] / stdlib / fmtmsg.c
blob69ed9034a9864599558c48e86a46ce8e85a7da93
1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <fmtmsg.h>
21 #include <bits/libc-lock.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/syslog.h>
28 /* We have global data, protect the modification. */
29 __libc_lock_define_initialized (static, lock)
32 enum
34 label_mask = 0x01,
35 severity_mask = 0x02,
36 text_mask = 0x04,
37 action_mask = 0x08,
38 tag_mask = 0x10,
39 all_mask = label_mask | severity_mask | text_mask | action_mask | tag_mask
42 static struct
44 const char *name;
45 size_t len;
46 } keywords[] =
48 { "label", 5 },
49 { "severity", 8 },
50 { "text", 4 },
51 { "action", 6},
52 { "tag", 3 }
54 #define NKEYWORDS (sizeof( keywords) / sizeof (keywords[0]))
57 struct severity_info
59 int severity;
60 const char *string;
61 struct severity_info *next;
65 /* List of known severities. */
66 static const struct severity_info nosev =
68 MM_NOSEV, "", NULL
70 static const struct severity_info haltsev =
72 MM_HALT, "HALT", (struct severity_info *) &nosev
74 static const struct severity_info errorsev =
76 MM_ERROR, "ERROR", (struct severity_info *) &haltsev
78 static const struct severity_info warningsev =
80 MM_WARNING, "WARNING", (struct severity_info *) &errorsev
82 static const struct severity_info infosev =
84 MM_INFO, "INFO", (struct severity_info *) &warningsev
87 /* Start of the list. */
88 static struct severity_info *severity_list = (struct severity_info *) &infosev;
90 /* Mask of values we will print. */
91 static int print;
93 /* Prototypes for local functions. */
94 static void init (void);
95 static int internal_addseverity (int severity, const char *string)
96 internal_function;
99 int
100 fmtmsg (long int classification, const char *label, int severity,
101 const char *text, const char *action, const char *tag)
103 __libc_once_define (static, once);
104 int result = MM_OK;
105 struct severity_info *severity_rec;
107 /* make sure everything is initialized. */
108 __libc_once (once, init);
110 /* Start the real work. First check whether the input is ok. */
111 if (label != MM_NULLLBL)
113 /* Must be two fields, separated by a colon. */
114 const char *cp = strchr (label, ':');
115 if (cp == NULL)
116 return MM_NOTOK;
118 /* The first field must not contain more then 10 bytes. */
119 if (cp - label > 10
120 /* The second field must not have more then 14 bytes. */
121 || strlen (cp + 1) > 14)
122 return MM_NOTOK;
125 for (severity_rec = severity_list; severity_rec != NULL;
126 severity_rec = severity_rec->next)
127 if (severity == severity_rec->severity)
128 /* Bingo. */
129 break;
131 /* If we don't know anything about the severity level return an error. */
132 if (severity_rec == NULL)
133 return MM_NOTOK;
136 /* Now we can print. */
137 if (classification & MM_PRINT)
139 int do_label = (print & label_mask) && label != MM_NULLLBL;
140 int do_severity = (print & severity_mask) && severity != MM_NULLSEV;
141 int do_text = (print & text_mask) && text != MM_NULLTXT;
142 int do_action = (print & action_mask) && action != MM_NULLACT;
143 int do_tag = (print & tag_mask) && tag != MM_NULLTAG;
145 if (fprintf (stderr, "%s%s%s%s%s%s%s%s%s%s\n",
146 do_label ? label : "",
147 do_label && (do_severity | do_text) ? ": " : "",
148 do_severity ? severity_rec->string : "",
149 do_severity && do_text ? ": " : "",
150 do_text ? text : "",
151 (do_label | do_severity | do_text) && (do_action | do_tag)
152 ? "\n" : "",
153 do_action ? "TO FIX: " : "",
154 do_action ? action : "",
155 do_action && do_tag ? " " : "",
156 do_tag ? tag : "") == EOF)
157 /* Oh, oh. An error occured during the output. */
158 result = MM_NOMSG;
161 if (classification & MM_CONSOLE)
163 int do_label = label != MM_NULLLBL;
164 int do_severity = severity != MM_NULLSEV;
165 int do_text = text != MM_NULLTXT;
166 int do_action = action != MM_NULLACT;
167 int do_tag = tag != MM_NULLTAG;
169 syslog (LOG_ERR, "%s%s%s%s%s%s%s%s%s%s\n",
170 do_label ? label : "",
171 do_label && (do_severity | do_text) ? ": " : "",
172 do_severity ? severity_rec->string : "",
173 do_severity && do_text ? ": " : "",
174 do_text ? text : "",
175 (do_label | do_severity | do_text) && (do_action | do_tag)
176 ? "\n" : "",
177 do_action ? "TO FIX: " : "",
178 do_action ? action : "",
179 do_action && do_tag ? " " : "",
180 do_tag ? tag : "");
183 return result;
187 /* Initialize from environment variable content. */
188 static void
189 init (void)
191 const char *msgverb_var = getenv ("MSGVERB");
192 const char *sevlevel_var = getenv ("SEV_LEVEL");
194 if (msgverb_var != NULL && msgverb_var[0] != '\0')
196 /* Using this extra variable allows us to work without locking. */
199 size_t cnt;
201 for (cnt = 0; cnt < NKEYWORDS; ++cnt)
202 if (memcmp (msgverb_var,
203 keywords[cnt].name, keywords[cnt].len) == 0
204 && (msgverb_var[keywords[cnt].len] == ':'
205 || msgverb_var[keywords[cnt].len] == '\0'))
206 break;
208 if (cnt < NKEYWORDS)
210 print |= 1 << cnt;
212 msgverb_var += keywords[cnt].len;
213 if (msgverb_var[0] == ':')
214 ++msgverb_var;
216 else
218 /* We found an illegal keyword in the environment
219 variable. The specifications say that we print all
220 fields. */
221 print = all_mask;
222 break;
225 while (msgverb_var[0] != '\0');
227 else
228 print = all_mask;
231 if (sevlevel_var != NULL)
233 __libc_lock_lock (lock);
235 while (sevlevel_var[0] != '\0')
237 const char *end = strchr (sevlevel_var, ':');
238 int level;
240 if (end == NULL)
241 end = strchr (sevlevel_var, '\0');
243 /* First field: keyword. This is not used here but it must be
244 present. */
245 while (sevlevel_var < end)
246 if (*sevlevel_var++ == ',')
247 break;
249 if (sevlevel_var < end)
251 /* Second field: severity level, a number. */
252 char *cp;
254 level = strtol (sevlevel_var, &cp, 0);
255 if (cp != sevlevel_var && cp < end && *cp++ == ','
256 && level > MM_INFO)
258 const char *new_string;
260 new_string = __strndup (cp, end - cp);
262 if (new_string != NULL
263 && (internal_addseverity (level, new_string)
264 != MM_OK))
265 free ((char *) new_string);
269 sevlevel_var = end + (*end == ':' ? 1 : 0);
275 /* Add the new entry to the list. */
276 static int
277 internal_function
278 internal_addseverity (int severity, const char *string)
280 struct severity_info *runp, *lastp;
281 int result = MM_OK;
283 /* First see if there is already a record for the severity level. */
284 for (runp = severity_list, lastp = NULL; runp != NULL; runp = runp-> next)
285 if (runp->severity == severity)
286 break;
287 else
288 lastp = runp;
290 if (runp != NULL)
292 /* Release old string. */
293 free ((char *) runp->string);
295 if (string != NULL)
296 /* Change the string. */
297 runp->string = string;
298 else
300 /* Remove the severity class. */
301 if (lastp == NULL)
302 severity_list = runp->next;
303 else
304 lastp->next = runp->next;
306 free (runp);
309 else if (string != NULL)
311 runp = malloc (sizeof (*runp));
312 if (runp == NULL)
313 result = MM_NOTOK;
314 else
316 runp->severity = severity;
317 runp->next = severity_list;
318 runp->string = string;
319 severity_list = runp;
322 else
323 /* We tried to remove a non-existing severity class. */
324 result = MM_NOTOK;
326 return result;
330 /* Add new severity level or remove old one. */
332 addseverity (int severity, const char *string)
334 int result;
335 const char *new_string;
337 /* Prevent illegal SEVERITY values. */
338 if (severity <= MM_INFO)
339 return MM_NOTOK;
341 if (string == NULL)
342 /* We want to remove the severity class. */
343 new_string = NULL;
344 else
346 new_string = __strdup (string);
348 if (new_string == NULL)
349 /* Allocation failed or illegal value. */
350 return MM_NOTOK;
353 /* Protect the global data. */
354 __libc_lock_lock (lock);
356 /* Do the real work. */
357 result = internal_addseverity (severity, string);
359 if (result != MM_OK)
360 /* Free the allocated string. */
361 free ((char *) new_string);
363 /* Release the lock. */
364 __libc_lock_unlock (lock);
366 return result;
370 static void __attribute__ ((unused))
371 free_mem (void)
373 struct severity_info *runp = severity_list;
375 while (runp != NULL)
376 if (runp->severity > MM_INFO)
378 /* This is data we have to release. */
379 struct severity_info *here = runp;
380 free ((char *) runp->string);
381 runp = runp->next;
382 free (here);
384 else
385 runp = runp->next;
387 text_set_element (__libc_subfreeres, free_mem);