Home | History | Annotate | Line # | Download | only in gen
fmtmsg.c revision 1.2.10.1
      1  1.2.10.1  nathanw /*	$NetBSD: fmtmsg.c,v 1.2.10.1 2002/11/11 22:22:08 nathanw 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.2   kleink #if defined(LIBC_SCCS) && !defined(lint)
     41       1.2   kleink __RCSID("$NetBSD: fmtmsg.c,v 1.2.10.1 2002/11/11 22:22:08 nathanw Exp $");
     42       1.2   kleink #endif /* LIBC_SCCS and not lint */
     43       1.2   kleink 
     44       1.1   kleink #include <fmtmsg.h>
     45       1.1   kleink #include <paths.h>
     46       1.1   kleink #include <stdio.h>
     47       1.1   kleink #include <stdlib.h>
     48       1.1   kleink #include <string.h>
     49       1.1   kleink 
     50       1.1   kleink static unsigned int	msgverb __P((const char *));
     51       1.1   kleink static const char *	severity2str __P((int));
     52       1.1   kleink static int		writeit __P((FILE *, unsigned int, const char *,
     53       1.1   kleink 			    const char *, const char *, const char *,
     54       1.1   kleink 			    const char *));
     55       1.1   kleink 
     56       1.1   kleink #define MM_VERBLABEL		0x01U
     57       1.1   kleink #define MM_VERBSEVERITY		0x02U
     58       1.1   kleink #define MM_VERBTEXT		0x04U
     59       1.1   kleink #define MM_VERBACTION		0x08U
     60       1.1   kleink #define MM_VERBTAG		0x10U
     61       1.1   kleink #define MM_VERBALL	\
     62       1.1   kleink     (MM_VERBLABEL | MM_VERBSEVERITY | MM_VERBTEXT | MM_VERBACTION | \
     63       1.1   kleink      MM_VERBTAG)
     64       1.1   kleink 
     65       1.1   kleink static const struct keyword {
     66       1.1   kleink 	size_t			len;	/* strlen(keyword) */
     67       1.1   kleink 	const char * const	keyword;
     68       1.1   kleink } keywords[] = {
     69       1.1   kleink 	{ 5,	"label"		},	/* log2(MM_VERBLABEL) */
     70       1.1   kleink 	{ 8,	"severity"	},	/* ... */
     71       1.1   kleink 	{ 4,	"text"		},
     72       1.1   kleink 	{ 6,	"action"	},
     73       1.1   kleink 	{ 3,	"tag"		}	/* log2(MM_VERBTAG) */
     74       1.1   kleink };
     75       1.1   kleink 
     76       1.1   kleink static const size_t nkeywords = sizeof (keywords) / sizeof (keywords[0]);
     77       1.1   kleink 
     78       1.1   kleink /*
     79       1.1   kleink  * Convert a colon-separated list of known keywords to a set of MM_VERB*
     80       1.1   kleink  * flags, defaulting to `all' if not set, empty, or in presence of unknown
     81       1.1   kleink  * keywords.
     82       1.1   kleink  */
     83       1.1   kleink static unsigned int
     84       1.1   kleink msgverb(str)
     85       1.1   kleink 	const char *str;
     86       1.1   kleink {
     87  1.2.10.1  nathanw 	u_int i;
     88       1.1   kleink 	unsigned int result;
     89       1.1   kleink 
     90       1.1   kleink 	if (str == NULL)
     91       1.1   kleink 		return (MM_VERBALL);
     92       1.1   kleink 
     93       1.1   kleink 	result = 0;
     94       1.1   kleink 	while (*str != '\0') {
     95       1.1   kleink 		for (i = 0; i < nkeywords; i++) {
     96       1.1   kleink 			if (memcmp(str, keywords[i].keyword, keywords[i].len)
     97       1.1   kleink 			    == 0 &&
     98       1.1   kleink 			    (*(str + keywords[i].len) == ':' ||
     99       1.1   kleink 			     *(str + keywords[i].len) == '\0'))
    100       1.1   kleink 				break;
    101       1.1   kleink 		}
    102       1.1   kleink 		if (i == nkeywords) {
    103       1.1   kleink 			result = MM_VERBALL;
    104       1.1   kleink 			break;
    105       1.1   kleink 		}
    106       1.1   kleink 
    107       1.1   kleink 		result |= (1 << i);
    108       1.1   kleink 		if (*(str += keywords[i].len) == ':')
    109       1.1   kleink 			str++;	/* Advance */
    110       1.1   kleink 	}
    111       1.1   kleink 	if (result == 0)
    112       1.1   kleink 		result = MM_VERBALL;
    113       1.1   kleink 
    114       1.1   kleink 	return (result);
    115       1.1   kleink }
    116       1.1   kleink 
    117       1.1   kleink static const char * const severities[] = {
    118       1.1   kleink 	"",		/* MM_NONE */
    119       1.1   kleink 	"HALT",
    120       1.1   kleink 	"ERROR",
    121       1.1   kleink 	"WARNING",
    122       1.1   kleink 	"INFO"
    123       1.1   kleink };
    124       1.1   kleink 
    125       1.1   kleink static const size_t nseverities = sizeof (severities) / sizeof (severities[0]);
    126       1.1   kleink 
    127       1.1   kleink /*
    128       1.1   kleink  * Returns the string representation associated with the numerical severity
    129       1.1   kleink  * value, defaulting to NULL for an unknown value.
    130       1.1   kleink  */
    131       1.1   kleink static const char *
    132       1.1   kleink severity2str(severity)
    133       1.1   kleink 	int severity;
    134       1.1   kleink {
    135       1.1   kleink 	const char *result;
    136       1.1   kleink 
    137  1.2.10.1  nathanw 	if (severity >= 0 &&
    138  1.2.10.1  nathanw 	    (u_int) severity < nseverities)
    139       1.1   kleink 		result = severities[severity];
    140       1.1   kleink 	else
    141       1.1   kleink 		result = NULL;
    142       1.1   kleink 
    143       1.1   kleink 	return (result);
    144       1.1   kleink }
    145       1.1   kleink 
    146       1.1   kleink /*
    147       1.1   kleink  * Format and write the message to the given stream, selecting those
    148       1.1   kleink  * components displayed from msgverb, returning the number of characters
    149       1.1   kleink  * written, or a negative value in case of an error.
    150       1.1   kleink  */
    151       1.1   kleink static int
    152       1.1   kleink writeit(stream, which, label, sevstr, text, action, tag)
    153       1.1   kleink 	FILE *stream;
    154       1.1   kleink 	unsigned int which;
    155       1.1   kleink 	const char *label;
    156       1.1   kleink 	const char *sevstr;
    157       1.1   kleink 	const char *text;
    158       1.1   kleink 	const char *action;
    159       1.1   kleink 	const char *tag;
    160       1.1   kleink {
    161       1.1   kleink 	int nwritten;
    162       1.1   kleink 
    163       1.1   kleink 	nwritten = fprintf(stream, "%s%s%s%s%s%s%s%s%s%s%s",
    164       1.1   kleink 	    ((which & MM_VERBLABEL) && label != MM_NULLLBL) ?
    165       1.1   kleink 	    label : "",
    166       1.1   kleink 	    ((which & MM_VERBLABEL) && label != MM_NULLLBL) ?
    167       1.1   kleink 	    ": " : "",
    168       1.1   kleink 	    (which & MM_VERBSEVERITY) ?
    169       1.1   kleink 	    sevstr : "",
    170       1.1   kleink 	    (which & MM_VERBSEVERITY) ?
    171       1.1   kleink 	    ": " : "",
    172       1.1   kleink 	    ((which & MM_VERBTEXT) && text != MM_NULLTXT) ?
    173       1.1   kleink 	    text : "",
    174       1.1   kleink 	    ((which & MM_VERBLABEL) && label != MM_NULLLBL) ||
    175       1.1   kleink 	    ((which & MM_VERBSEVERITY)) ||
    176       1.1   kleink 	    ((which & MM_VERBTEXT) && text != MM_NULLTXT) ?
    177       1.1   kleink 	    "\n" : "",
    178       1.1   kleink 	    ((which & MM_VERBACTION) && action != MM_NULLACT) ?
    179       1.1   kleink 	    "TO FIX: " : "",
    180       1.1   kleink 	    ((which & MM_VERBACTION) && action != MM_NULLACT) ?
    181       1.1   kleink 	    action : "",
    182       1.1   kleink 	    ((which & MM_VERBACTION) && label != MM_NULLACT) ?
    183       1.1   kleink 	    " " : "",
    184       1.1   kleink 	    ((which & MM_VERBTAG) && tag != MM_NULLTAG) ?
    185       1.1   kleink 	    tag : "",
    186       1.1   kleink 	    ((which & MM_VERBACTION) && action != MM_NULLACT) ||
    187       1.1   kleink 	    ((which & MM_VERBTAG) && tag != MM_NULLTAG) ?
    188       1.1   kleink 	    "\n" : "");
    189       1.1   kleink 
    190       1.1   kleink 	return (nwritten);
    191       1.1   kleink }
    192       1.1   kleink 
    193       1.1   kleink int
    194       1.1   kleink fmtmsg(classification, label, severity, text, action, tag)
    195       1.1   kleink 	long classification;
    196       1.1   kleink 	const char *label;
    197       1.1   kleink 	int severity;
    198       1.1   kleink 	const char *text;
    199       1.1   kleink 	const char *action;
    200       1.1   kleink 	const char *tag;
    201       1.1   kleink {
    202       1.1   kleink 	FILE *console;
    203       1.1   kleink 	const char *p, *sevstr;
    204       1.1   kleink 	int result;
    205       1.1   kleink 
    206       1.1   kleink 	/* Validate label constraints, if not null. */
    207       1.1   kleink 	if (label != MM_NULLLBL) {
    208       1.1   kleink 		/*
    209       1.1   kleink 		 * Two fields, separated by a colon.  The first field is up to
    210       1.1   kleink 		 * 10 bytes, the second is up to 14 bytes.
    211       1.1   kleink 		 */
    212       1.1   kleink 		p = strchr(label, ':');
    213       1.1   kleink 		if (p ==  NULL || p - label > 10 || strlen(p + 1) > 14)
    214       1.1   kleink 			return (MM_NOTOK);
    215       1.1   kleink 	}
    216       1.1   kleink 	/* Validate severity argument. */
    217       1.1   kleink 	if ((sevstr = severity2str(severity)) == NULL)
    218       1.1   kleink 		return (MM_NOTOK);
    219       1.1   kleink 
    220       1.1   kleink 	/*
    221       1.1   kleink 	 * Fact in search for a better place: XSH5 does not define any
    222       1.1   kleink 	 * functionality for `classification' bits other than the display
    223       1.1   kleink 	 * subclassification.
    224       1.1   kleink 	 */
    225       1.1   kleink 
    226       1.1   kleink 	result = 0;
    227       1.1   kleink 
    228       1.1   kleink 	if (classification & MM_PRINT) {
    229       1.1   kleink 		if (writeit(stderr, msgverb(getenv("MSGVERB")),
    230       1.1   kleink 		    label, sevstr, text, action, tag) < 0)
    231       1.1   kleink 			result |= MM_NOMSG;
    232       1.1   kleink 	}
    233       1.1   kleink 	/* Similar to MM_PRINT but ignoring $MSGVERB. */
    234       1.1   kleink 	if (classification & MM_CONSOLE) {
    235       1.1   kleink 		if ((console = fopen(_PATH_CONSOLE, "w")) != NULL) {
    236       1.1   kleink 			if (writeit(console, MM_VERBALL,
    237       1.1   kleink 			    label, sevstr, text, action, tag) < 0)
    238       1.1   kleink 				result |= MM_NOCON;
    239       1.1   kleink 			/*
    240       1.1   kleink 			 * Ignore result: does not constitute ``generate a
    241       1.1   kleink 			 * console message.''
    242       1.1   kleink 			 */
    243       1.1   kleink 			(void)fclose(console);
    244       1.1   kleink 		} else {
    245       1.1   kleink 			result |= MM_NOCON;
    246       1.1   kleink 		}
    247       1.1   kleink 	}
    248       1.1   kleink 
    249       1.1   kleink 	if (result == (MM_NOMSG | MM_NOCON))
    250       1.1   kleink 		result = MM_NOTOK;
    251       1.1   kleink 
    252       1.1   kleink 	return (result == 0 ? MM_OK : result);
    253       1.1   kleink }
    254