Home | History | Annotate | Line # | Download | only in gen
fmtmsg.c revision 1.2
      1  1.2  kleink /*	$NetBSD: fmtmsg.c,v 1.2 1999/09/13 18:36:02 kleink 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 1999/09/13 18:36:02 kleink 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.1  kleink 	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.1  kleink 	if (severity < nseverities)
    138  1.1  kleink 		result = severities[severity];
    139  1.1  kleink 	else
    140  1.1  kleink 		result = NULL;
    141  1.1  kleink 
    142  1.1  kleink 	return (result);
    143  1.1  kleink }
    144  1.1  kleink 
    145  1.1  kleink /*
    146  1.1  kleink  * Format and write the message to the given stream, selecting those
    147  1.1  kleink  * components displayed from msgverb, returning the number of characters
    148  1.1  kleink  * written, or a negative value in case of an error.
    149  1.1  kleink  */
    150  1.1  kleink static int
    151  1.1  kleink writeit(stream, which, label, sevstr, text, action, tag)
    152  1.1  kleink 	FILE *stream;
    153  1.1  kleink 	unsigned int which;
    154  1.1  kleink 	const char *label;
    155  1.1  kleink 	const char *sevstr;
    156  1.1  kleink 	const char *text;
    157  1.1  kleink 	const char *action;
    158  1.1  kleink 	const char *tag;
    159  1.1  kleink {
    160  1.1  kleink 	int nwritten;
    161  1.1  kleink 
    162  1.1  kleink 	nwritten = fprintf(stream, "%s%s%s%s%s%s%s%s%s%s%s",
    163  1.1  kleink 	    ((which & MM_VERBLABEL) && label != MM_NULLLBL) ?
    164  1.1  kleink 	    label : "",
    165  1.1  kleink 	    ((which & MM_VERBLABEL) && label != MM_NULLLBL) ?
    166  1.1  kleink 	    ": " : "",
    167  1.1  kleink 	    (which & MM_VERBSEVERITY) ?
    168  1.1  kleink 	    sevstr : "",
    169  1.1  kleink 	    (which & MM_VERBSEVERITY) ?
    170  1.1  kleink 	    ": " : "",
    171  1.1  kleink 	    ((which & MM_VERBTEXT) && text != MM_NULLTXT) ?
    172  1.1  kleink 	    text : "",
    173  1.1  kleink 	    ((which & MM_VERBLABEL) && label != MM_NULLLBL) ||
    174  1.1  kleink 	    ((which & MM_VERBSEVERITY)) ||
    175  1.1  kleink 	    ((which & MM_VERBTEXT) && text != MM_NULLTXT) ?
    176  1.1  kleink 	    "\n" : "",
    177  1.1  kleink 	    ((which & MM_VERBACTION) && action != MM_NULLACT) ?
    178  1.1  kleink 	    "TO FIX: " : "",
    179  1.1  kleink 	    ((which & MM_VERBACTION) && action != MM_NULLACT) ?
    180  1.1  kleink 	    action : "",
    181  1.1  kleink 	    ((which & MM_VERBACTION) && label != MM_NULLACT) ?
    182  1.1  kleink 	    " " : "",
    183  1.1  kleink 	    ((which & MM_VERBTAG) && tag != MM_NULLTAG) ?
    184  1.1  kleink 	    tag : "",
    185  1.1  kleink 	    ((which & MM_VERBACTION) && action != MM_NULLACT) ||
    186  1.1  kleink 	    ((which & MM_VERBTAG) && tag != MM_NULLTAG) ?
    187  1.1  kleink 	    "\n" : "");
    188  1.1  kleink 
    189  1.1  kleink 	return (nwritten);
    190  1.1  kleink }
    191  1.1  kleink 
    192  1.1  kleink int
    193  1.1  kleink fmtmsg(classification, label, severity, text, action, tag)
    194  1.1  kleink 	long classification;
    195  1.1  kleink 	const char *label;
    196  1.1  kleink 	int severity;
    197  1.1  kleink 	const char *text;
    198  1.1  kleink 	const char *action;
    199  1.1  kleink 	const char *tag;
    200  1.1  kleink {
    201  1.1  kleink 	FILE *console;
    202  1.1  kleink 	const char *p, *sevstr;
    203  1.1  kleink 	int result;
    204  1.1  kleink 
    205  1.1  kleink 	/* Validate label constraints, if not null. */
    206  1.1  kleink 	if (label != MM_NULLLBL) {
    207  1.1  kleink 		/*
    208  1.1  kleink 		 * Two fields, separated by a colon.  The first field is up to
    209  1.1  kleink 		 * 10 bytes, the second is up to 14 bytes.
    210  1.1  kleink 		 */
    211  1.1  kleink 		p = strchr(label, ':');
    212  1.1  kleink 		if (p ==  NULL || p - label > 10 || strlen(p + 1) > 14)
    213  1.1  kleink 			return (MM_NOTOK);
    214  1.1  kleink 	}
    215  1.1  kleink 	/* Validate severity argument. */
    216  1.1  kleink 	if ((sevstr = severity2str(severity)) == NULL)
    217  1.1  kleink 		return (MM_NOTOK);
    218  1.1  kleink 
    219  1.1  kleink 	/*
    220  1.1  kleink 	 * Fact in search for a better place: XSH5 does not define any
    221  1.1  kleink 	 * functionality for `classification' bits other than the display
    222  1.1  kleink 	 * subclassification.
    223  1.1  kleink 	 */
    224  1.1  kleink 
    225  1.1  kleink 	result = 0;
    226  1.1  kleink 
    227  1.1  kleink 	if (classification & MM_PRINT) {
    228  1.1  kleink 		if (writeit(stderr, msgverb(getenv("MSGVERB")),
    229  1.1  kleink 		    label, sevstr, text, action, tag) < 0)
    230  1.1  kleink 			result |= MM_NOMSG;
    231  1.1  kleink 	}
    232  1.1  kleink 	/* Similar to MM_PRINT but ignoring $MSGVERB. */
    233  1.1  kleink 	if (classification & MM_CONSOLE) {
    234  1.1  kleink 		if ((console = fopen(_PATH_CONSOLE, "w")) != NULL) {
    235  1.1  kleink 			if (writeit(console, MM_VERBALL,
    236  1.1  kleink 			    label, sevstr, text, action, tag) < 0)
    237  1.1  kleink 				result |= MM_NOCON;
    238  1.1  kleink 			/*
    239  1.1  kleink 			 * Ignore result: does not constitute ``generate a
    240  1.1  kleink 			 * console message.''
    241  1.1  kleink 			 */
    242  1.1  kleink 			(void)fclose(console);
    243  1.1  kleink 		} else {
    244  1.1  kleink 			result |= MM_NOCON;
    245  1.1  kleink 		}
    246  1.1  kleink 	}
    247  1.1  kleink 
    248  1.1  kleink 	if (result == (MM_NOMSG | MM_NOCON))
    249  1.1  kleink 		result = MM_NOTOK;
    250  1.1  kleink 
    251  1.1  kleink 	return (result == 0 ? MM_OK : result);
    252  1.1  kleink }
    253