1*0a6a1f1dSLionel Sambuc /* $NetBSD: fmtmsg.c,v 1.6 2014/09/18 13:58:20 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 1999 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras * by Klaus Klein.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: fmtmsg.c,v 1.6 2014/09/18 13:58:20 christos Exp $");
352fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
362fe8fb19SBen Gras
372fe8fb19SBen Gras #include <fmtmsg.h>
382fe8fb19SBen Gras #include <paths.h>
392fe8fb19SBen Gras #include <stdio.h>
402fe8fb19SBen Gras #include <stdlib.h>
412fe8fb19SBen Gras #include <string.h>
422fe8fb19SBen Gras
43f14fb602SLionel Sambuc static unsigned int msgverb(const char *);
44f14fb602SLionel Sambuc static const char * severity2str(int);
45f14fb602SLionel Sambuc static int writeit(FILE *, unsigned int, const char *,
462fe8fb19SBen Gras const char *, const char *, const char *,
47f14fb602SLionel Sambuc const char *);
482fe8fb19SBen Gras
492fe8fb19SBen Gras #define MM_VERBLABEL 0x01U
502fe8fb19SBen Gras #define MM_VERBSEVERITY 0x02U
512fe8fb19SBen Gras #define MM_VERBTEXT 0x04U
522fe8fb19SBen Gras #define MM_VERBACTION 0x08U
532fe8fb19SBen Gras #define MM_VERBTAG 0x10U
542fe8fb19SBen Gras #define MM_VERBALL \
552fe8fb19SBen Gras (MM_VERBLABEL | MM_VERBSEVERITY | MM_VERBTEXT | MM_VERBACTION | \
562fe8fb19SBen Gras MM_VERBTAG)
572fe8fb19SBen Gras
582fe8fb19SBen Gras static const struct keyword {
592fe8fb19SBen Gras size_t len; /* strlen(keyword) */
602fe8fb19SBen Gras const char * const keyword;
612fe8fb19SBen Gras } keywords[] = {
622fe8fb19SBen Gras { 5, "label" }, /* log2(MM_VERBLABEL) */
632fe8fb19SBen Gras { 8, "severity" }, /* ... */
642fe8fb19SBen Gras { 4, "text" },
652fe8fb19SBen Gras { 6, "action" },
662fe8fb19SBen Gras { 3, "tag" } /* log2(MM_VERBTAG) */
672fe8fb19SBen Gras };
682fe8fb19SBen Gras
692fe8fb19SBen Gras static const size_t nkeywords = sizeof (keywords) / sizeof (keywords[0]);
702fe8fb19SBen Gras
712fe8fb19SBen Gras /*
722fe8fb19SBen Gras * Convert a colon-separated list of known keywords to a set of MM_VERB*
732fe8fb19SBen Gras * flags, defaulting to `all' if not set, empty, or in presence of unknown
742fe8fb19SBen Gras * keywords.
752fe8fb19SBen Gras */
762fe8fb19SBen Gras static unsigned int
msgverb(const char * str)77f14fb602SLionel Sambuc msgverb(const char *str)
782fe8fb19SBen Gras {
792fe8fb19SBen Gras u_int i;
802fe8fb19SBen Gras unsigned int result;
812fe8fb19SBen Gras
822fe8fb19SBen Gras if (str == NULL)
832fe8fb19SBen Gras return (MM_VERBALL);
842fe8fb19SBen Gras
852fe8fb19SBen Gras result = 0;
862fe8fb19SBen Gras while (*str != '\0') {
872fe8fb19SBen Gras for (i = 0; i < nkeywords; i++) {
882fe8fb19SBen Gras if (memcmp(str, keywords[i].keyword, keywords[i].len)
892fe8fb19SBen Gras == 0 &&
902fe8fb19SBen Gras (*(str + keywords[i].len) == ':' ||
912fe8fb19SBen Gras *(str + keywords[i].len) == '\0'))
922fe8fb19SBen Gras break;
932fe8fb19SBen Gras }
942fe8fb19SBen Gras if (i == nkeywords) {
952fe8fb19SBen Gras result = MM_VERBALL;
962fe8fb19SBen Gras break;
972fe8fb19SBen Gras }
982fe8fb19SBen Gras
992fe8fb19SBen Gras result |= (1 << i);
1002fe8fb19SBen Gras if (*(str += keywords[i].len) == ':')
1012fe8fb19SBen Gras str++; /* Advance */
1022fe8fb19SBen Gras }
1032fe8fb19SBen Gras if (result == 0)
1042fe8fb19SBen Gras result = MM_VERBALL;
1052fe8fb19SBen Gras
1062fe8fb19SBen Gras return (result);
1072fe8fb19SBen Gras }
1082fe8fb19SBen Gras
109f14fb602SLionel Sambuc static const char severities[][8] = {
1102fe8fb19SBen Gras "", /* MM_NONE */
1112fe8fb19SBen Gras "HALT",
1122fe8fb19SBen Gras "ERROR",
1132fe8fb19SBen Gras "WARNING",
1142fe8fb19SBen Gras "INFO"
1152fe8fb19SBen Gras };
1162fe8fb19SBen Gras
1172fe8fb19SBen Gras static const size_t nseverities = sizeof (severities) / sizeof (severities[0]);
1182fe8fb19SBen Gras
1192fe8fb19SBen Gras /*
1202fe8fb19SBen Gras * Returns the string representation associated with the numerical severity
1212fe8fb19SBen Gras * value, defaulting to NULL for an unknown value.
1222fe8fb19SBen Gras */
1232fe8fb19SBen Gras static const char *
severity2str(int severity)124f14fb602SLionel Sambuc severity2str(int severity)
1252fe8fb19SBen Gras {
1262fe8fb19SBen Gras const char *result;
1272fe8fb19SBen Gras
1282fe8fb19SBen Gras if (severity >= 0 &&
1292fe8fb19SBen Gras (u_int) severity < nseverities)
1302fe8fb19SBen Gras result = severities[severity];
1312fe8fb19SBen Gras else
1322fe8fb19SBen Gras result = NULL;
1332fe8fb19SBen Gras
1342fe8fb19SBen Gras return (result);
1352fe8fb19SBen Gras }
1362fe8fb19SBen Gras
1372fe8fb19SBen Gras /*
1382fe8fb19SBen Gras * Format and write the message to the given stream, selecting those
1392fe8fb19SBen Gras * components displayed from msgverb, returning the number of characters
1402fe8fb19SBen Gras * written, or a negative value in case of an error.
1412fe8fb19SBen Gras */
1422fe8fb19SBen Gras static int
writeit(FILE * stream,unsigned int which,const char * label,const char * sevstr,const char * text,const char * action,const char * tag)143f14fb602SLionel Sambuc writeit(FILE *stream, unsigned int which, const char *label,
144f14fb602SLionel Sambuc const char *sevstr, const char *text, const char *action,
145f14fb602SLionel Sambuc const char *tag)
1462fe8fb19SBen Gras {
1472fe8fb19SBen Gras int nwritten;
1482fe8fb19SBen Gras
1492fe8fb19SBen Gras nwritten = fprintf(stream, "%s%s%s%s%s%s%s%s%s%s%s",
1502fe8fb19SBen Gras ((which & MM_VERBLABEL) && label != MM_NULLLBL) ?
1512fe8fb19SBen Gras label : "",
1522fe8fb19SBen Gras ((which & MM_VERBLABEL) && label != MM_NULLLBL) ?
1532fe8fb19SBen Gras ": " : "",
1542fe8fb19SBen Gras (which & MM_VERBSEVERITY) ?
1552fe8fb19SBen Gras sevstr : "",
1562fe8fb19SBen Gras (which & MM_VERBSEVERITY) ?
1572fe8fb19SBen Gras ": " : "",
1582fe8fb19SBen Gras ((which & MM_VERBTEXT) && text != MM_NULLTXT) ?
1592fe8fb19SBen Gras text : "",
1602fe8fb19SBen Gras ((which & MM_VERBLABEL) && label != MM_NULLLBL) ||
1612fe8fb19SBen Gras ((which & MM_VERBSEVERITY)) ||
1622fe8fb19SBen Gras ((which & MM_VERBTEXT) && text != MM_NULLTXT) ?
1632fe8fb19SBen Gras "\n" : "",
1642fe8fb19SBen Gras ((which & MM_VERBACTION) && action != MM_NULLACT) ?
1652fe8fb19SBen Gras "TO FIX: " : "",
1662fe8fb19SBen Gras ((which & MM_VERBACTION) && action != MM_NULLACT) ?
1672fe8fb19SBen Gras action : "",
1682fe8fb19SBen Gras ((which & MM_VERBACTION) && label != MM_NULLACT) ?
1692fe8fb19SBen Gras " " : "",
1702fe8fb19SBen Gras ((which & MM_VERBTAG) && tag != MM_NULLTAG) ?
1712fe8fb19SBen Gras tag : "",
1722fe8fb19SBen Gras ((which & MM_VERBACTION) && action != MM_NULLACT) ||
1732fe8fb19SBen Gras ((which & MM_VERBTAG) && tag != MM_NULLTAG) ?
1742fe8fb19SBen Gras "\n" : "");
1752fe8fb19SBen Gras
1762fe8fb19SBen Gras return (nwritten);
1772fe8fb19SBen Gras }
1782fe8fb19SBen Gras
1792fe8fb19SBen Gras int
fmtmsg(long classification,const char * label,int severity,const char * text,const char * action,const char * tag)180f14fb602SLionel Sambuc fmtmsg(long classification, const char *label, int severity,
181f14fb602SLionel Sambuc const char *text, const char *action, const char *tag)
1822fe8fb19SBen Gras {
1832fe8fb19SBen Gras FILE *console;
1842fe8fb19SBen Gras const char *p, *sevstr;
1852fe8fb19SBen Gras int result;
1862fe8fb19SBen Gras
1872fe8fb19SBen Gras /* Validate label constraints, if not null. */
1882fe8fb19SBen Gras if (label != MM_NULLLBL) {
1892fe8fb19SBen Gras /*
1902fe8fb19SBen Gras * Two fields, separated by a colon. The first field is up to
1912fe8fb19SBen Gras * 10 bytes, the second is up to 14 bytes.
1922fe8fb19SBen Gras */
1932fe8fb19SBen Gras p = strchr(label, ':');
1942fe8fb19SBen Gras if (p == NULL || p - label > 10 || strlen(p + 1) > 14)
1952fe8fb19SBen Gras return (MM_NOTOK);
1962fe8fb19SBen Gras }
1972fe8fb19SBen Gras /* Validate severity argument. */
1982fe8fb19SBen Gras if ((sevstr = severity2str(severity)) == NULL)
1992fe8fb19SBen Gras return (MM_NOTOK);
2002fe8fb19SBen Gras
2012fe8fb19SBen Gras /*
2022fe8fb19SBen Gras * Fact in search for a better place: XSH5 does not define any
2032fe8fb19SBen Gras * functionality for `classification' bits other than the display
2042fe8fb19SBen Gras * subclassification.
2052fe8fb19SBen Gras */
2062fe8fb19SBen Gras
2072fe8fb19SBen Gras result = 0;
2082fe8fb19SBen Gras
2092fe8fb19SBen Gras if (classification & MM_PRINT) {
2102fe8fb19SBen Gras if (writeit(stderr, msgverb(getenv("MSGVERB")),
2112fe8fb19SBen Gras label, sevstr, text, action, tag) < 0)
2122fe8fb19SBen Gras result |= MM_NOMSG;
2132fe8fb19SBen Gras }
2142fe8fb19SBen Gras /* Similar to MM_PRINT but ignoring $MSGVERB. */
2152fe8fb19SBen Gras if (classification & MM_CONSOLE) {
216*0a6a1f1dSLionel Sambuc if ((console = fopen(_PATH_CONSOLE, "we")) != NULL) {
2172fe8fb19SBen Gras if (writeit(console, MM_VERBALL,
2182fe8fb19SBen Gras label, sevstr, text, action, tag) < 0)
2192fe8fb19SBen Gras result |= MM_NOCON;
2202fe8fb19SBen Gras /*
2212fe8fb19SBen Gras * Ignore result: does not constitute ``generate a
2222fe8fb19SBen Gras * console message.''
2232fe8fb19SBen Gras */
2242fe8fb19SBen Gras (void)fclose(console);
2252fe8fb19SBen Gras } else {
2262fe8fb19SBen Gras result |= MM_NOCON;
2272fe8fb19SBen Gras }
2282fe8fb19SBen Gras }
2292fe8fb19SBen Gras
2302fe8fb19SBen Gras if (result == (MM_NOMSG | MM_NOCON))
2312fe8fb19SBen Gras result = MM_NOTOK;
2322fe8fb19SBen Gras
2332fe8fb19SBen Gras return (result == 0 ? MM_OK : result);
2342fe8fb19SBen Gras }
235