| OLD | NEW |
| 1 /* Public domain fmtmsg() | 1 /* Public domain fmtmsg() |
| 2 * Written by Isaac Dunham, 2014 | 2 * Written by Isaac Dunham, 2014 |
| 3 */ | 3 */ |
| 4 #include <fmtmsg.h> | 4 #include <fmtmsg.h> |
| 5 #include <fcntl.h> | 5 #include <fcntl.h> |
| 6 #include <unistd.h> | 6 #include <unistd.h> |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <pthread.h> | 10 #include <pthread.h> |
| 11 | 11 |
| 12 /* | 12 /* |
| 13 * If lstr is the first part of bstr, check that the next char in bstr | 13 * If lstr is the first part of bstr, check that the next char in bstr |
| 14 * is either \0 or : | 14 * is either \0 or : |
| 15 */ | 15 */ |
| 16 static int _strcolcmp(const char *lstr, const char *bstr) | 16 static int _strcolcmp(const char* lstr, const char* bstr) { |
| 17 { | 17 size_t i = 0; |
| 18 » size_t i = 0; | 18 while (lstr[i] && bstr[i] && (bstr[i] == lstr[i])) |
| 19 » while (lstr[i] && bstr[i] && (bstr[i] == lstr[i])) i++; | 19 i++; |
| 20 » if ( lstr[i] || (bstr[i] && bstr[i] != ':')) return 1; | 20 if (lstr[i] || (bstr[i] && bstr[i] != ':')) |
| 21 » return 0; | 21 return 1; |
| 22 return 0; |
| 22 } | 23 } |
| 23 | 24 |
| 24 int fmtmsg(long classification, const char *label, int severity, | 25 int fmtmsg(long classification, |
| 25 const char *text, const char *action, const char *tag) | 26 const char* label, |
| 26 { | 27 int severity, |
| 27 » int ret = 0, i, consolefd, verb = 0; | 28 const char* text, |
| 28 » char *errstring = MM_NULLSEV, *cmsg = getenv("MSGVERB"); | 29 const char* action, |
| 29 » char *const msgs[] = { | 30 const char* tag) { |
| 30 » » "label", "severity", "text", "action", "tag", NULL | 31 int ret = 0, i, consolefd, verb = 0; |
| 31 » }; | 32 char *errstring = MM_NULLSEV, *cmsg = getenv("MSGVERB"); |
| 32 » int cs; | 33 char* const msgs[] = {"label", "severity", "text", "action", "tag", NULL}; |
| 34 int cs; |
| 33 | 35 |
| 34 » pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); | 36 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); |
| 35 | 37 |
| 36 » if (severity == MM_HALT) errstring = "HALT: "; | 38 if (severity == MM_HALT) |
| 37 » else if (severity == MM_ERROR) errstring = "ERROR: "; | 39 errstring = "HALT: "; |
| 38 » else if (severity == MM_WARNING) errstring = "WARNING: "; | 40 else if (severity == MM_ERROR) |
| 39 » else if (severity == MM_INFO) errstring = "INFO: "; | 41 errstring = "ERROR: "; |
| 42 else if (severity == MM_WARNING) |
| 43 errstring = "WARNING: "; |
| 44 else if (severity == MM_INFO) |
| 45 errstring = "INFO: "; |
| 40 | 46 |
| 41 » if (classification & MM_CONSOLE) { | 47 if (classification & MM_CONSOLE) { |
| 42 » » consolefd = open("/dev/console", O_WRONLY); | 48 consolefd = open("/dev/console", O_WRONLY); |
| 43 » » if (consolefd < 0) { | 49 if (consolefd < 0) { |
| 44 » » » ret = MM_NOCON; | 50 ret = MM_NOCON; |
| 45 » » } else { | 51 } else { |
| 46 » » » if (dprintf(consolefd, "%s%s%s%s%s%s%s%s\n", | 52 if (dprintf(consolefd, "%s%s%s%s%s%s%s%s\n", label ? label : "", |
| 47 » » » label?label:"", label?": ":"", | 53 label ? ": " : "", severity ? errstring : "", |
| 48 » » » severity?errstring:"", text?text:"", | 54 text ? text : "", action ? "\nTO FIX: " : "", |
| 49 » » » action?"\nTO FIX: ":"", | 55 action ? action : "", action ? " " : "", tag ? tag : "") < 1) |
| 50 » » » action?action:"", action?" ":"", | 56 ret = MM_NOCON; |
| 51 » » » tag?tag:"" )<1) | 57 close(consolefd); |
| 52 » » » » ret = MM_NOCON; | 58 } |
| 53 » » » close(consolefd); | 59 } |
| 54 » » } | |
| 55 » } | |
| 56 | 60 |
| 57 » if (classification & MM_PRINT) { | 61 if (classification & MM_PRINT) { |
| 58 » » while (cmsg && cmsg[0]) { | 62 while (cmsg && cmsg[0]) { |
| 59 » » » for(i=0; msgs[i]; i++) { | 63 for (i = 0; msgs[i]; i++) { |
| 60 » » » » if (!_strcolcmp(msgs[i], cmsg)) break; | 64 if (!_strcolcmp(msgs[i], cmsg)) |
| 61 » » » } | 65 break; |
| 62 » » » if (msgs[i] == NULL) { | 66 } |
| 63 » » » » //ignore MSGVERB-unrecognized component | 67 if (msgs[i] == NULL) { |
| 64 » » » » verb = 0xFF; | 68 // ignore MSGVERB-unrecognized component |
| 65 » » » » break; | 69 verb = 0xFF; |
| 66 » » » } else { | 70 break; |
| 67 » » » » verb |= (1 << i); | 71 } else { |
| 68 » » » » cmsg = strchr(cmsg, ':'); | 72 verb |= (1 << i); |
| 69 » » » » if (cmsg) cmsg++; | 73 cmsg = strchr(cmsg, ':'); |
| 70 » » » } | 74 if (cmsg) |
| 71 » » } | 75 cmsg++; |
| 72 » » if (!verb) verb = 0xFF; | 76 } |
| 73 » » if (dprintf(2, "%s%s%s%s%s%s%s%s\n", | 77 } |
| 74 » » (verb&1 && label) ? label : "", | 78 if (!verb) |
| 75 » » (verb&1 && label) ? ": " : "", | 79 verb = 0xFF; |
| 76 » » (verb&2 && severity) ? errstring : "", | 80 if (dprintf(2, "%s%s%s%s%s%s%s%s\n", (verb & 1 && label) ? label : "", |
| 77 » » (verb&4 && text) ? text : "", | 81 (verb & 1 && label) ? ": " : "", |
| 78 » » (verb&8 && action) ? "\nTO FIX: " : "", | 82 (verb & 2 && severity) ? errstring : "", |
| 79 » » (verb&8 && action) ? action : "", | 83 (verb & 4 && text) ? text : "", |
| 80 » » (verb&8 && action) ? " " : "", | 84 (verb & 8 && action) ? "\nTO FIX: " : "", |
| 81 » » (verb&16 && tag) ? tag : "" ) < 1) | 85 (verb & 8 && action) ? action : "", |
| 82 » » » ret |= MM_NOMSG; | 86 (verb & 8 && action) ? " " : "", |
| 83 » } | 87 (verb & 16 && tag) ? tag : "") < 1) |
| 84 » if ((ret & (MM_NOCON|MM_NOMSG)) == (MM_NOCON|MM_NOMSG)) | 88 ret |= MM_NOMSG; |
| 85 » » ret = MM_NOTOK; | 89 } |
| 90 if ((ret & (MM_NOCON | MM_NOMSG)) == (MM_NOCON | MM_NOMSG)) |
| 91 ret = MM_NOTOK; |
| 86 | 92 |
| 87 » pthread_setcancelstate(cs, 0); | 93 pthread_setcancelstate(cs, 0); |
| 88 | 94 |
| 89 » return ret; | 95 return ret; |
| 90 } | 96 } |
| OLD | NEW |