diff --git a/include/fmtmsg.h b/include/fmtmsg.h new file mode 100644 index 0000000..4bc34d3 --- /dev/null +++ b/include/fmtmsg.h @@ -0,0 +1,47 @@ +#ifndef _FMTMSG_H +#define _FMTMSG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define MM_HARD 0x0001 +#define MM_SOFT 0x0002 +#define MM_FIRM 0x0004 + +#define MM_APPL 0x0008 +#define MM_UTIL 0x0010 +#define MM_OPSYS 0x0020 + +#define MM_PRINT 0x0100 +#define MM_CONSOLE 0x0200 + +#define MM_RECOVER 0x0040 +#define MM_NRECOV 0x0080 + +#define MM_NULLMC 0L + +#define MM_HALT 0x1 +#define MM_ERROR 0x2 +#define MM_WARNING 0x3 +#define MM_INFO 0x4 +#define MM_NOSEV 0x0 + +#define MM_OK 0x00000000 +#define MM_NOTOK 0xffffffff +#define MM_NOMSG 0x00000001 +#define MM_NOCON 0x00000004 + +#define MM_NULLLBL (char*)0 +#define MM_NULLTXT (char*)0 +#define MM_NULLACT (char*)0 +#define MM_NULLTAG (char*)0 +#define MM_NULLSEV 0 + +int fmtmsg(long, const char *, int, const char *, const char *, const char *); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/misc/fmtmsg.c b/src/misc/fmtmsg.c new file mode 100644 index 0000000..98fdaca --- /dev/null +++ b/src/misc/fmtmsg.c @@ -0,0 +1,84 @@ +/* Public domain fmtmsg() + * Written by Isaac Dunham, 2014 + */ +#include +#include +#include +#include +#include +#include + + +/* + * If lstr is the first part of bstr, check that the next char in bstr + * is either \0 or : + */ +static int _strcolcmp(const char *lstr, const char *bstr) +{ + size_t i = 0; + while (lstr[i] && bstr[i] && (bstr[i] == lstr[i])) i++; + if ( lstr[i] || (bstr[i] && bstr[i] != ':')) return 1; + return 0; +} +int fmtmsg(long classification, const char *label, int severity, + const char *text, const char *action, const char *tag) +{ + int ret = 0, i, consolefd = 0, verb = 0; + char *errstring = MM_NULLSEV, *cmsg = getenv("MSGVERB"); + char *const msgs[] = { + "label", "severity", "text", "action", "tag", NULL + }; + + if (classification & MM_CONSOLE) + consolefd = open("/dev/console", O_WRONLY); + if (consolefd < 0) { + classification &= ~MM_CONSOLE; + ret = MM_NOCON; + consolefd = 0; + } + if (severity == MM_HALT) errstring = "HALT: "; + else if (severity == MM_ERROR) errstring = "ERROR: "; + else if (severity == MM_WARNING) errstring = "WARNING: "; + else if (severity == MM_INFO) errstring = "INFO: "; + if (consolefd) { + if (dprintf(consolefd, "%s%s%s%s%s%s%s%s\n", + label?label:"", label?": ":"", + severity?errstring:"", text?text:"", + action?"\nTO FIX: ":"", + action?action:"", action?" ":"", tag?tag:"" )<1) + ret = MM_NOCON; + } + + if (cmsg) { + while (cmsg && cmsg[0]) { + for(i=0; msgs[i]; i++) { + if (!_strcolcmp(msgs[i], cmsg)) break; + } + if (msgs[i] == NULL) { + //ignore MSGVERB-unrecognized component + verb =0xFF; + break; + } else { + verb |= (1 << i); + cmsg = strchr(cmsg, ':'); + if (cmsg) cmsg++; + } + } + } + if (!verb) verb = 0xFF; + if (classification & MM_PRINT) { + if (dprintf(2, "%s%s%s%s%s%s%s%s\n", + (verb&1 && label) ? label : "", + (verb&1 && label) ? ": " : "", + (verb&2 && severity) ? errstring : "", + (verb&4 && text) ? text : "", + (verb&8 && action) ? "\nTO FIX: " : "", + (verb&8 && action) ? action : "", + (verb&8 && action) ? " " : "", + (verb&16 && tag) ? tag : "" ) < 1) + ret |= MM_NOMSG; + } + if ((ret & (MM_NOCON|MM_NOMSG) ) == (MM_NOCON|MM_NOMSG)) + ret = MM_NOTOK; + return ret; +}