* sysdeps/unix/sysv/linux/m68k/bits/resource.h: New file.
[glibc.git] / stdlib / fmtmsg.c
blobbb8502f00b022587924bb424b09b00882d5de90e
1 /* Copyright (C) 1997, 1999, 2000, 2001 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 /* Now we can print. */
141 if (classification & MM_PRINT)
143 int do_label = (print & label_mask) && label != MM_NULLLBL;
144 int do_severity = (print & severity_mask) && severity != MM_NULLSEV;
145 int do_text = (print & text_mask) && text != MM_NULLTXT;
146 int do_action = (print & action_mask) && action != MM_NULLACT;
147 int do_tag = (print & tag_mask) && tag != MM_NULLTAG;
149 #ifdef USE_IN_LIBIO
150 if (_IO_fwide (stderr, 0) > 0)
152 if (__fwprintf (stderr, L"%s%s%s%s%s%s%s%s%s%s\n",
153 do_label ? label : "",
154 do_label
155 && (do_severity | do_text | do_action | do_tag)
156 ? ": " : "",
157 do_severity ? severity_rec->string : "",
158 do_severity && (do_text | do_action | do_tag)
159 ? ": " : "",
160 do_text ? text : "",
161 do_text && (do_action | do_tag) ? "\n" : "",
162 do_action ? "TO FIX: " : "",
163 do_action ? action : "",
164 do_action && do_tag ? " " : "",
165 do_tag ? tag : "") == WEOF)
166 /* Oh, oh. An error occurred during the output. */
167 result = MM_NOMSG;
169 else
170 #endif
171 if (fprintf (stderr, "%s%s%s%s%s%s%s%s%s%s\n",
172 do_label ? label : "",
173 do_label && (do_severity | do_text | do_action | do_tag)
174 ? ": " : "",
175 do_severity ? severity_rec->string : "",
176 do_severity && (do_text | do_action | do_tag) ? ": " : "",
177 do_text ? text : "",
178 do_text && (do_action | do_tag) ? "\n" : "",
179 do_action ? "TO FIX: " : "",
180 do_action ? action : "",
181 do_action && do_tag ? " " : "",
182 do_tag ? tag : "") == EOF)
183 /* Oh, oh. An error occurred during the output. */
184 result = MM_NOMSG;
187 if (classification & MM_CONSOLE)
189 int do_label = label != MM_NULLLBL;
190 int do_severity = severity != MM_NULLSEV;
191 int do_text = text != MM_NULLTXT;
192 int do_action = action != MM_NULLACT;
193 int do_tag = tag != MM_NULLTAG;
195 syslog (LOG_ERR, "%s%s%s%s%s%s%s%s%s%s\n",
196 do_label ? label : "",
197 do_label && (do_severity | do_text | do_action | do_tag)
198 ? ": " : "",
199 do_severity ? severity_rec->string : "",
200 do_severity && (do_text | do_action | do_tag) ? ": " : "",
201 do_text ? text : "",
202 do_text && (do_action | do_tag) ? "\n" : "",
203 do_action ? "TO FIX: " : "",
204 do_action ? action : "",
205 do_action && do_tag ? " " : "",
206 do_tag ? tag : "");
209 return result;
213 /* Initialize from environment variable content. */
214 static void
215 init (void)
217 const char *msgverb_var = getenv ("MSGVERB");
218 const char *sevlevel_var = getenv ("SEV_LEVEL");
220 if (msgverb_var != NULL && msgverb_var[0] != '\0')
222 /* Using this extra variable allows us to work without locking. */
225 size_t cnt;
227 for (cnt = 0; cnt < NKEYWORDS; ++cnt)
228 if (memcmp (msgverb_var,
229 keywords[cnt].name, keywords[cnt].len) == 0
230 && (msgverb_var[keywords[cnt].len] == ':'
231 || msgverb_var[keywords[cnt].len] == '\0'))
232 break;
234 if (cnt < NKEYWORDS)
236 print |= 1 << cnt;
238 msgverb_var += keywords[cnt].len;
239 if (msgverb_var[0] == ':')
240 ++msgverb_var;
242 else
244 /* We found an illegal keyword in the environment
245 variable. The specifications say that we print all
246 fields. */
247 print = all_mask;
248 break;
251 while (msgverb_var[0] != '\0');
253 else
254 print = all_mask;
257 if (sevlevel_var != NULL)
259 __libc_lock_lock (lock);
261 while (sevlevel_var[0] != '\0')
263 const char *end = __strchrnul (sevlevel_var, ':');
264 int level;
266 /* First field: keyword. This is not used here but it must be
267 present. */
268 while (sevlevel_var < end)
269 if (*sevlevel_var++ == ',')
270 break;
272 if (sevlevel_var < end)
274 /* Second field: severity level, a number. */
275 char *cp;
277 level = strtol (sevlevel_var, &cp, 0);
278 if (cp != sevlevel_var && cp < end && *cp++ == ','
279 && level > MM_INFO)
281 const char *new_string;
283 new_string = __strndup (cp, end - cp);
285 if (new_string != NULL
286 && (internal_addseverity (level, new_string)
287 != MM_OK))
288 free ((char *) new_string);
292 sevlevel_var = end + (*end == ':' ? 1 : 0);
298 /* Add the new entry to the list. */
299 static int
300 internal_function
301 internal_addseverity (int severity, const char *string)
303 struct severity_info *runp, *lastp;
304 int result = MM_OK;
306 /* First see if there is already a record for the severity level. */
307 for (runp = severity_list, lastp = NULL; runp != NULL; runp = runp-> next)
308 if (runp->severity == severity)
309 break;
310 else
311 lastp = runp;
313 if (runp != NULL)
315 /* Release old string. */
316 free ((char *) runp->string);
318 if (string != NULL)
319 /* Change the string. */
320 runp->string = string;
321 else
323 /* Remove the severity class. */
324 if (lastp == NULL)
325 severity_list = runp->next;
326 else
327 lastp->next = runp->next;
329 free (runp);
332 else if (string != NULL)
334 runp = malloc (sizeof (*runp));
335 if (runp == NULL)
336 result = MM_NOTOK;
337 else
339 runp->severity = severity;
340 runp->next = severity_list;
341 runp->string = string;
342 severity_list = runp;
345 else
346 /* We tried to remove a non-existing severity class. */
347 result = MM_NOTOK;
349 return result;
353 /* Add new severity level or remove old one. */
355 addseverity (int severity, const char *string)
357 int result;
358 const char *new_string;
360 /* Prevent illegal SEVERITY values. */
361 if (severity <= MM_INFO)
362 return MM_NOTOK;
364 if (string == NULL)
365 /* We want to remove the severity class. */
366 new_string = NULL;
367 else
369 new_string = __strdup (string);
371 if (new_string == NULL)
372 /* Allocation failed or illegal value. */
373 return MM_NOTOK;
376 /* Protect the global data. */
377 __libc_lock_lock (lock);
379 /* Do the real work. */
380 result = internal_addseverity (severity, string);
382 if (result != MM_OK)
383 /* Free the allocated string. */
384 free ((char *) new_string);
386 /* Release the lock. */
387 __libc_lock_unlock (lock);
389 return result;
393 static void __attribute__ ((unused))
394 free_mem (void)
396 struct severity_info *runp = severity_list;
398 while (runp != NULL)
399 if (runp->severity > MM_INFO)
401 /* This is data we have to release. */
402 struct severity_info *here = runp;
403 free ((char *) runp->string);
404 runp = runp->next;
405 free (here);
407 else
408 runp = runp->next;
410 text_set_element (__libc_subfreeres, free_mem);