Update.
[glibc.git] / stdlib / fmtmsg.c
blob1c9e4298cde6f9d990f19e8da2753418115e7a62
1 /* Copyright (C) 1997, 1999, 2000 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 const struct
44 size_t len;
45 /* Adjust the size if new elements are added. */
46 const char name[12];
47 } keywords[] =
49 { 5, "label" },
50 { 8, "severity" },
51 { 4, "text" },
52 { 6, "action"},
53 { 3, "tag" }
55 #define NKEYWORDS (sizeof( keywords) / sizeof (keywords[0]))
58 struct severity_info
60 int severity;
61 const char *string;
62 struct severity_info *next;
66 /* List of known severities. */
67 static const struct severity_info nosev =
69 MM_NOSEV, "", NULL
71 static const struct severity_info haltsev =
73 MM_HALT, "HALT", (struct severity_info *) &nosev
75 static const struct severity_info errorsev =
77 MM_ERROR, "ERROR", (struct severity_info *) &haltsev
79 static const struct severity_info warningsev =
81 MM_WARNING, "WARNING", (struct severity_info *) &errorsev
83 static const struct severity_info infosev =
85 MM_INFO, "INFO", (struct severity_info *) &warningsev
88 /* Start of the list. */
89 static struct severity_info *severity_list = (struct severity_info *) &infosev;
91 /* Mask of values we will print. */
92 static int print;
94 /* Prototypes for local functions. */
95 static void init (void);
96 static int internal_addseverity (int severity, const char *string)
97 internal_function;
101 fmtmsg (long int classification, const char *label, int severity,
102 const char *text, const char *action, const char *tag)
104 __libc_once_define (static, once);
105 int result = MM_OK;
106 struct severity_info *severity_rec;
108 /* make sure everything is initialized. */
109 __libc_once (once, init);
111 /* Start the real work. First check whether the input is ok. */
112 if (label != MM_NULLLBL)
114 /* Must be two fields, separated by a colon. */
115 const char *cp = strchr (label, ':');
116 if (cp == NULL)
117 return MM_NOTOK;
119 /* The first field must not contain more then 10 bytes. */
120 if (cp - label > 10
121 /* The second field must not have more then 14 bytes. */
122 || strlen (cp + 1) > 14)
123 return MM_NOTOK;
126 for (severity_rec = severity_list; severity_rec != NULL;
127 severity_rec = severity_rec->next)
128 if (severity == severity_rec->severity)
129 /* Bingo. */
130 break;
132 /* If we don't know anything about the severity level return an error. */
133 if (severity_rec == NULL)
134 return MM_NOTOK;
137 /* Now we can print. */
138 if (classification & MM_PRINT)
140 int do_label = (print & label_mask) && label != MM_NULLLBL;
141 int do_severity = (print & severity_mask) && severity != MM_NULLSEV;
142 int do_text = (print & text_mask) && text != MM_NULLTXT;
143 int do_action = (print & action_mask) && action != MM_NULLACT;
144 int do_tag = (print & tag_mask) && tag != MM_NULLTAG;
146 if (fprintf (stderr, "%s%s%s%s%s%s%s%s%s%s\n",
147 do_label ? label : "",
148 do_label && (do_severity | do_text | do_action | do_tag)
149 ? ": " : "",
150 do_severity ? severity_rec->string : "",
151 do_severity && (do_text | do_action | do_tag) ? ": " : "",
152 do_text ? text : "",
153 do_text && (do_action | do_tag) ? "\n" : "",
154 do_action ? "TO FIX: " : "",
155 do_action ? action : "",
156 do_action && do_tag ? " " : "",
157 do_tag ? tag : "") == EOF)
158 /* Oh, oh. An error occurred during the output. */
159 result = MM_NOMSG;
162 if (classification & MM_CONSOLE)
164 int do_label = label != MM_NULLLBL;
165 int do_severity = severity != MM_NULLSEV;
166 int do_text = text != MM_NULLTXT;
167 int do_action = action != MM_NULLACT;
168 int do_tag = tag != MM_NULLTAG;
170 syslog (LOG_ERR, "%s%s%s%s%s%s%s%s%s%s\n",
171 do_label ? label : "",
172 do_label && (do_severity | do_text | do_action | do_tag)
173 ? ": " : "",
174 do_severity ? severity_rec->string : "",
175 do_severity && (do_text | do_action | do_tag) ? ": " : "",
176 do_text ? text : "",
177 do_text && (do_action | do_tag) ? "\n" : "",
178 do_action ? "TO FIX: " : "",
179 do_action ? action : "",
180 do_action && do_tag ? " " : "",
181 do_tag ? tag : "");
184 return result;
188 /* Initialize from environment variable content. */
189 static void
190 init (void)
192 const char *msgverb_var = getenv ("MSGVERB");
193 const char *sevlevel_var = getenv ("SEV_LEVEL");
195 if (msgverb_var != NULL && msgverb_var[0] != '\0')
197 /* Using this extra variable allows us to work without locking. */
200 size_t cnt;
202 for (cnt = 0; cnt < NKEYWORDS; ++cnt)
203 if (memcmp (msgverb_var,
204 keywords[cnt].name, keywords[cnt].len) == 0
205 && (msgverb_var[keywords[cnt].len] == ':'
206 || msgverb_var[keywords[cnt].len] == '\0'))
207 break;
209 if (cnt < NKEYWORDS)
211 print |= 1 << cnt;
213 msgverb_var += keywords[cnt].len;
214 if (msgverb_var[0] == ':')
215 ++msgverb_var;
217 else
219 /* We found an illegal keyword in the environment
220 variable. The specifications say that we print all
221 fields. */
222 print = all_mask;
223 break;
226 while (msgverb_var[0] != '\0');
228 else
229 print = all_mask;
232 if (sevlevel_var != NULL)
234 __libc_lock_lock (lock);
236 while (sevlevel_var[0] != '\0')
238 const char *end = __strchrnul (sevlevel_var, ':');
239 int level;
241 /* First field: keyword. This is not used here but it must be
242 present. */
243 while (sevlevel_var < end)
244 if (*sevlevel_var++ == ',')
245 break;
247 if (sevlevel_var < end)
249 /* Second field: severity level, a number. */
250 char *cp;
252 level = strtol (sevlevel_var, &cp, 0);
253 if (cp != sevlevel_var && cp < end && *cp++ == ','
254 && level > MM_INFO)
256 const char *new_string;
258 new_string = __strndup (cp, end - cp);
260 if (new_string != NULL
261 && (internal_addseverity (level, new_string)
262 != MM_OK))
263 free ((char *) new_string);
267 sevlevel_var = end + (*end == ':' ? 1 : 0);
273 /* Add the new entry to the list. */
274 static int
275 internal_function
276 internal_addseverity (int severity, const char *string)
278 struct severity_info *runp, *lastp;
279 int result = MM_OK;
281 /* First see if there is already a record for the severity level. */
282 for (runp = severity_list, lastp = NULL; runp != NULL; runp = runp-> next)
283 if (runp->severity == severity)
284 break;
285 else
286 lastp = runp;
288 if (runp != NULL)
290 /* Release old string. */
291 free ((char *) runp->string);
293 if (string != NULL)
294 /* Change the string. */
295 runp->string = string;
296 else
298 /* Remove the severity class. */
299 if (lastp == NULL)
300 severity_list = runp->next;
301 else
302 lastp->next = runp->next;
304 free (runp);
307 else if (string != NULL)
309 runp = malloc (sizeof (*runp));
310 if (runp == NULL)
311 result = MM_NOTOK;
312 else
314 runp->severity = severity;
315 runp->next = severity_list;
316 runp->string = string;
317 severity_list = runp;
320 else
321 /* We tried to remove a non-existing severity class. */
322 result = MM_NOTOK;
324 return result;
328 /* Add new severity level or remove old one. */
330 addseverity (int severity, const char *string)
332 int result;
333 const char *new_string;
335 /* Prevent illegal SEVERITY values. */
336 if (severity <= MM_INFO)
337 return MM_NOTOK;
339 if (string == NULL)
340 /* We want to remove the severity class. */
341 new_string = NULL;
342 else
344 new_string = __strdup (string);
346 if (new_string == NULL)
347 /* Allocation failed or illegal value. */
348 return MM_NOTOK;
351 /* Protect the global data. */
352 __libc_lock_lock (lock);
354 /* Do the real work. */
355 result = internal_addseverity (severity, string);
357 if (result != MM_OK)
358 /* Free the allocated string. */
359 free ((char *) new_string);
361 /* Release the lock. */
362 __libc_lock_unlock (lock);
364 return result;
368 static void __attribute__ ((unused))
369 free_mem (void)
371 struct severity_info *runp = severity_list;
373 while (runp != NULL)
374 if (runp->severity > MM_INFO)
376 /* This is data we have to release. */
377 struct severity_info *here = runp;
378 free ((char *) runp->string);
379 runp = runp->next;
380 free (here);
382 else
383 runp = runp->next;
385 text_set_element (__libc_subfreeres, free_mem);