2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2002 Mike Barcroft <mike@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* Default value for MSGVERB. */
35 #define DFLT_MSGVERB "label:severity:text:action:tag"
37 /* Maximum valid size for a MSGVERB. */
38 #define MAX_MSGVERB sizeof(DFLT_MSGVERB)
40 static char *printfmt(char *, long, const char *, int, const char *,
41 const char *, const char *);
42 static char *nextcomp(const char *);
45 static int validmsgverb(const char *);
48 fmtmsg(long class, const char *label, int sev, const char *text,
49 const char *action, const char *tag)
52 char *env, *msgverb, *output;
54 if (class & MM_PRINT) {
55 if ((env = getenv("MSGVERB")) != NULL && *env != '\0' &&
56 strlen(env) <= strlen(DFLT_MSGVERB)) {
57 if ((msgverb = strdup(env)) == NULL)
59 else if (validmsgverb(msgverb) == 0) {
65 if ((msgverb = strdup(DFLT_MSGVERB)) == NULL)
68 output = printfmt(msgverb, class, label, sev, text, action,
75 fprintf(stderr, "%s", output);
79 if (class & MM_CONSOLE) {
80 output = printfmt(DFLT_MSGVERB, class, label, sev, text,
84 if (*output != '\0') {
85 if ((fp = fopen("/dev/console", "ae")) == NULL) {
89 fprintf(fp, "%s", output);
97 #define INSERT_COLON \
98 if (*output != '\0') \
99 strlcat(output, ": ", size)
100 #define INSERT_NEWLINE \
101 if (*output != '\0') \
102 strlcat(output, "\n", size)
103 #define INSERT_SPACE \
104 if (*output != '\0') \
105 strlcat(output, " ", size)
108 * Returns NULL on memory allocation failure, otherwise returns a pointer to
109 * a newly malloc()'d output buffer.
112 printfmt(char *msgverb, long class, const char *label, int sev,
113 const char *text, const char *act, const char *tag)
120 if (label != MM_NULLLBL)
121 size += strlen(label);
122 if ((sevname = sevinfo(sev)) != NULL)
123 size += strlen(sevname);
124 if (text != MM_NULLTXT)
125 size += strlen(text);
126 if (act != MM_NULLACT)
128 if (tag != MM_NULLTAG)
131 if ((output = malloc(size)) == NULL)
134 while ((comp = nextcomp(msgverb)) != NULL) {
135 if (strcmp(comp, "label") == 0 && label != MM_NULLLBL) {
137 strlcat(output, label, size);
138 } else if (strcmp(comp, "severity") == 0 && sevname != NULL) {
140 strlcat(output, sevinfo(sev), size);
141 } else if (strcmp(comp, "text") == 0 && text != MM_NULLTXT) {
143 strlcat(output, text, size);
144 } else if (strcmp(comp, "action") == 0 && act != MM_NULLACT) {
146 strlcat(output, "TO FIX: ", size);
147 strlcat(output, act, size);
148 } else if (strcmp(comp, "tag") == 0 && tag != MM_NULLTAG) {
150 strlcat(output, tag, size);
158 * Returns a component of a colon delimited string. NULL is returned to
159 * indicate that there are no remaining components. This function must be
160 * called until it returns NULL in order for the local state to be cleared.
163 nextcomp(const char *msgverb)
165 static char lmsgverb[MAX_MSGVERB], *state;
168 if (*lmsgverb == '\0') {
169 strlcpy(lmsgverb, msgverb, sizeof(lmsgverb));
170 retval = strtok_r(lmsgverb, ":", &state);
172 retval = strtok_r(NULL, ":", &state);
198 * Returns 1 if the msgverb list is valid, otherwise 0.
201 validmsgverb(const char *msgverb)
203 const char *validlist = "label\0severity\0text\0action\0tag\0";
210 while ((msgcomp = nextcomp(msgverb)) != NULL) {
212 len1 = strlen(msgcomp);
213 for (p = validlist; (len2 = strlen(p)) != 0; p += len2 + 1) {
214 if (len1 == len2 && memcmp(msgcomp, p, len1) == 0)