]>
git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/misc/fmtmsg.c
1 /* Public domain fmtmsg()
2 * Written by Isaac Dunham, 2014
10 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
15 * If lstr is the first part of bstr, check that the next char in bstr
18 static int _strcolcmp(const char *lstr
, const char *bstr
)
21 while (lstr
[i
] && bstr
[i
] && (bstr
[i
] == lstr
[i
])) i
++;
22 if ( lstr
[i
] || (bstr
[i
] && bstr
[i
] != ':')) return 1;
26 int fmtmsg(long classification
, const char *label
, int severity
,
27 const char *text
, const char *action
, const char *tag
)
29 int ret
= 0, i
, consolefd
, verb
= 0;
30 char *errstring
= MM_NULLSEV
, *cmsg
= getenv("MSGVERB");
31 char *const msgs
[] = {
32 "label", "severity", "text", "action", "tag", NULL
36 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
37 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &cs
);
40 if (severity
== MM_HALT
) errstring
= "HALT: ";
41 else if (severity
== MM_ERROR
) errstring
= "ERROR: ";
42 else if (severity
== MM_WARNING
) errstring
= "WARNING: ";
43 else if (severity
== MM_INFO
) errstring
= "INFO: ";
45 if (classification
& MM_CONSOLE
) {
46 consolefd
= open("/dev/console", O_WRONLY
);
50 if (dprintf(consolefd
, "%s%s%s%s%s%s%s%s\n",
51 label
?label
:"", label
?": ":"",
52 severity
?errstring
:"", text
?text
:"",
53 action
?"\nTO FIX: ":"",
54 action
?action
:"", action
?" ":"",
61 if (classification
& MM_PRINT
) {
62 while (cmsg
&& cmsg
[0]) {
63 for(i
=0; msgs
[i
]; i
++) {
64 if (!_strcolcmp(msgs
[i
], cmsg
)) break;
66 if (msgs
[i
] == NULL
) {
67 //ignore MSGVERB-unrecognized component
72 cmsg
= strchr(cmsg
, ':');
76 if (!verb
) verb
= 0xFF;
77 if (dprintf(2, "%s%s%s%s%s%s%s%s\n",
78 (verb
&1 && label
) ? label
: "",
79 (verb
&1 && label
) ? ": " : "",
80 (verb
&2 && severity
) ? errstring
: "",
81 (verb
&4 && text
) ? text
: "",
82 (verb
&8 && action
) ? "\nTO FIX: " : "",
83 (verb
&8 && action
) ? action
: "",
84 (verb
&8 && action
) ? " " : "",
85 (verb
&16 && tag
) ? tag
: "" ) < 1)
88 if ((ret
& (MM_NOCON
|MM_NOMSG
)) == (MM_NOCON
|MM_NOMSG
))
91 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
92 pthread_setcancelstate(cs
, 0);