Update.
[glibc.git] / stdlib / fmtmsg.c
blob2ab97b7d9033945a36b6d09da041d56cf73f39a4
1 /* Copyright (C) 1997,1999,2000,2001,2002,2003 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 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>
26 #ifdef USE_IN_LIBIO
27 # include <wchar.h>
28 #endif
31 /* We have global data, protect the modification. */
32 __libc_lock_define_initialized (static, lock)
35 enum
37 label_mask = 0x01,
38 severity_mask = 0x02,
39 text_mask = 0x04,
40 action_mask = 0x08,
41 tag_mask = 0x10,
42 all_mask = label_mask | severity_mask | text_mask | action_mask | tag_mask
45 static const struct
47 size_t len;
48 /* Adjust the size if new elements are added. */
49 const char name[12];
50 } keywords[] =
52 { 5, "label" },
53 { 8, "severity" },
54 { 4, "text" },
55 { 6, "action"},
56 { 3, "tag" }
58 #define NKEYWORDS (sizeof( keywords) / sizeof (keywords[0]))
61 struct severity_info
63 int severity;
64 const char *string;
65 struct severity_info *next;
69 /* List of known severities. */
70 static const struct severity_info nosev =
72 MM_NOSEV, "", NULL
74 static const struct severity_info haltsev =
76 MM_HALT, "HALT", (struct severity_info *) &nosev
78 static const struct severity_info errorsev =
80 MM_ERROR, "ERROR", (struct severity_info *) &haltsev
82 static const struct severity_info warningsev =
84 MM_WARNING, "WARNING", (struct severity_info *) &errorsev
86 static const struct severity_info infosev =
88 MM_INFO, "INFO", (struct severity_info *) &warningsev
91 /* Start of the list. */
92 static struct severity_info *severity_list = (struct severity_info *) &infosev;
94 /* Mask of values we will print. */
95 static int print;
97 /* Prototypes for local functions. */
98 static void init (void);
99 static int internal_addseverity (int severity, const char *string)
100 internal_function;
104 fmtmsg (long int classification, const char *label, int severity,
105 const char *text, const char *action, const char *tag)
107 __libc_once_define (static, once);
108 int result = MM_OK;
109 struct severity_info *severity_rec;
111 /* Make sure everything is initialized. */
112 __libc_once (once, init);
114 /* Start the real work. First check whether the input is ok. */
115 if (label != MM_NULLLBL)
117 /* Must be two fields, separated by a colon. */
118 const char *cp = strchr (label, ':');
119 if (cp == NULL)
120 return MM_NOTOK;
122 /* The first field must not contain more then 10 bytes. */
123 if (cp - label > 10
124 /* The second field must not have more then 14 bytes. */
125 || strlen (cp + 1) > 14)
126 return MM_NOTOK;
129 for (severity_rec = severity_list; severity_rec != NULL;
130 severity_rec = severity_rec->next)
131 if (severity == severity_rec->severity)
132 /* Bingo. */
133 break;
135 /* If we don't know anything about the severity level return an error. */
136 if (severity_rec == NULL)
137 return MM_NOTOK;
140 #ifdef __libc_ptf_call
141 /* We do not want this call to be cut short by a thread
142 cancellation. Therefore disable cancellation for now. */
143 int state = PTHREAD_CANCEL_ENABLE;
144 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
146 #endif
148 /* Now we can print. */
149 if (classification & MM_PRINT)
151 int do_label = (print & label_mask) && label != MM_NULLLBL;
152 int do_severity = (print & severity_mask) && severity != MM_NULLSEV;
153 int do_text = (print & text_mask) && text != MM_NULLTXT;
154 int do_action = (print & action_mask) && action != MM_NULLACT;
155 int do_tag = (print & tag_mask) && tag != MM_NULLTAG;
157 #ifdef USE_IN_LIBIO
158 if (_IO_fwide (stderr, 0) > 0)
160 if (__fwprintf (stderr, L"%s%s%s%s%s%s%s%s%s%s\n",
161 do_label ? label : "",
162 do_label
163 && (do_severity | do_text | do_action | do_tag)
164 ? ": " : "",
165 do_severity ? severity_rec->string : "",
166 do_severity && (do_text | do_action | do_tag)
167 ? ": " : "",
168 do_text ? text : "",
169 do_text && (do_action | do_tag) ? "\n" : "",
170 do_action ? "TO FIX: " : "",
171 do_action ? action : "",
172 do_action && do_tag ? " " : "",
173 do_tag ? tag : "") < 0)
174 /* Oh, oh. An error occurred during the output. */
175 result = MM_NOMSG;
177 else
178 #endif
179 if (fprintf (stderr, "%s%s%s%s%s%s%s%s%s%s\n",
180 do_label ? label : "",
181 do_label && (do_severity | do_text | do_action | do_tag)
182 ? ": " : "",
183 do_severity ? severity_rec->string : "",
184 do_severity && (do_text | do_action | do_tag) ? ": " : "",
185 do_text ? text : "",
186 do_text && (do_action | do_tag) ? "\n" : "",
187 do_action ? "TO FIX: " : "",
188 do_action ? action : "",
189 do_action && do_tag ? " " : "",
190 do_tag ? tag : "") < 0)
191 /* Oh, oh. An error occurred during the output. */
192 result = MM_NOMSG;
195 if (classification & MM_CONSOLE)
197 int do_label = label != MM_NULLLBL;
198 int do_severity = severity != MM_NULLSEV;
199 int do_text = text != MM_NULLTXT;
200 int do_action = action != MM_NULLACT;
201 int do_tag = tag != MM_NULLTAG;
203 syslog (LOG_ERR, "%s%s%s%s%s%s%s%s%s%s\n",
204 do_label ? label : "",
205 do_label && (do_severity | do_text | do_action | do_tag)
206 ? ": " : "",
207 do_severity ? severity_rec->string : "",
208 do_severity && (do_text | do_action | do_tag) ? ": " : "",
209 do_text ? text : "",
210 do_text && (do_action | do_tag) ? "\n" : "",
211 do_action ? "TO FIX: " : "",
212 do_action ? action : "",
213 do_action && do_tag ? " " : "",
214 do_tag ? tag : "");
217 #ifdef __libc_ptf_call
218 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
219 #endif
221 return result;
225 /* Initialize from environment variable content. */
226 static void
227 init (void)
229 const char *msgverb_var = getenv ("MSGVERB");
230 const char *sevlevel_var = getenv ("SEV_LEVEL");
232 if (msgverb_var != NULL && msgverb_var[0] != '\0')
234 /* Using this extra variable allows us to work without locking. */
237 size_t cnt;
239 for (cnt = 0; cnt < NKEYWORDS; ++cnt)
240 if (memcmp (msgverb_var,
241 keywords[cnt].name, keywords[cnt].len) == 0
242 && (msgverb_var[keywords[cnt].len] == ':'
243 || msgverb_var[keywords[cnt].len] == '\0'))
244 break;
246 if (cnt < NKEYWORDS)
248 print |= 1 << cnt;
250 msgverb_var += keywords[cnt].len;
251 if (msgverb_var[0] == ':')
252 ++msgverb_var;
254 else
256 /* We found an illegal keyword in the environment
257 variable. The specifications say that we print all
258 fields. */
259 print = all_mask;
260 break;
263 while (msgverb_var[0] != '\0');
265 else
266 print = all_mask;
269 if (sevlevel_var != NULL)
271 __libc_lock_lock (lock);
273 while (sevlevel_var[0] != '\0')
275 const char *end = __strchrnul (sevlevel_var, ':');
276 int level;
278 /* First field: keyword. This is not used here but it must be
279 present. */
280 while (sevlevel_var < end)
281 if (*sevlevel_var++ == ',')
282 break;
284 if (sevlevel_var < end)
286 /* Second field: severity level, a number. */
287 char *cp;
289 level = strtol (sevlevel_var, &cp, 0);
290 if (cp != sevlevel_var && cp < end && *cp++ == ','
291 && level > MM_INFO)
293 const char *new_string;
295 new_string = __strndup (cp, end - cp);
297 if (new_string != NULL
298 && (internal_addseverity (level, new_string)
299 != MM_OK))
300 free ((char *) new_string);
304 sevlevel_var = end + (*end == ':' ? 1 : 0);
310 /* Add the new entry to the list. */
311 static int
312 internal_function
313 internal_addseverity (int severity, const char *string)
315 struct severity_info *runp, *lastp;
316 int result = MM_OK;
318 /* First see if there is already a record for the severity level. */
319 for (runp = severity_list, lastp = NULL; runp != NULL; runp = runp-> next)
320 if (runp->severity == severity)
321 break;
322 else
323 lastp = runp;
325 if (runp != NULL)
327 /* Release old string. */
328 free ((char *) runp->string);
330 if (string != NULL)
331 /* Change the string. */
332 runp->string = string;
333 else
335 /* Remove the severity class. */
336 if (lastp == NULL)
337 severity_list = runp->next;
338 else
339 lastp->next = runp->next;
341 free (runp);
344 else if (string != NULL)
346 runp = malloc (sizeof (*runp));
347 if (runp == NULL)
348 result = MM_NOTOK;
349 else
351 runp->severity = severity;
352 runp->next = severity_list;
353 runp->string = string;
354 severity_list = runp;
357 else
358 /* We tried to remove a non-existing severity class. */
359 result = MM_NOTOK;
361 return result;
365 /* Add new severity level or remove old one. */
367 addseverity (int severity, const char *string)
369 int result;
370 const char *new_string;
372 /* Prevent illegal SEVERITY values. */
373 if (severity <= MM_INFO)
374 return MM_NOTOK;
376 if (string == NULL)
377 /* We want to remove the severity class. */
378 new_string = NULL;
379 else
381 new_string = __strdup (string);
383 if (new_string == NULL)
384 /* Allocation failed or illegal value. */
385 return MM_NOTOK;
388 /* Protect the global data. */
389 __libc_lock_lock (lock);
391 /* Do the real work. */
392 result = internal_addseverity (severity, string);
394 if (result != MM_OK)
395 /* Free the allocated string. */
396 free ((char *) new_string);
398 /* Release the lock. */
399 __libc_lock_unlock (lock);
401 return result;
405 libc_freeres_fn (free_mem)
407 struct severity_info *runp = severity_list;
409 while (runp != NULL)
410 if (runp->severity > MM_INFO)
412 /* This is data we have to release. */
413 struct severity_info *here = runp;
414 free ((char *) runp->string);
415 runp = runp->next;
416 free (here);
418 else
419 runp = runp->next;