mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] Implement fmtmsg
@ 2014-04-25  5:06 Isaac Dunham
  2014-04-25 14:49 ` Rich Felker
  0 siblings, 1 reply; 14+ messages in thread
From: Isaac Dunham @ 2014-04-25  5:06 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 321 bytes --]

Having noticed commit b427d847, I've implemented fmtmsg().
Here's the patch.

I'm also attaching a small program I used for testing fmtmsg.
This program is not the standard usage and is not really proper (due to
a lack of sanity checking); but it allows me to cover the various edge
cases fairly well.

HTH,
Isaac Dunham

[-- Attachment #2: fmtmsg.diff --]
[-- Type: text/plain, Size: 4050 bytes --]

diff --git a/include/fmtmsg.h b/include/fmtmsg.h
new file mode 100644
index 0000000..bdc4a52
--- /dev/null
+++ b/include/fmtmsg.h
@@ -0,0 +1,53 @@
+#ifndef _FMTMSG_H
+#define _FMTMSG_H
+
+/* Major: source of problem */
+#define	MM_HARD			0x0001
+#define	MM_SOFT			0x0002
+#define	MM_FIRM			0x0004
+
+/* Source subclassification: code encountering the problem */
+#define	MM_APPL			0x0008
+#define	MM_UTIL			0x0010
+#define	MM_OPSYS		0x0020
+
+/* Display subclassification */
+#define	MM_PRINT		0x0100
+#define	MM_CONSOLE		0x0200
+
+/* Can we recover? */
+#define	MM_RECOVER		0x0040
+#define	MM_NRECOV		0x0080
+
+#define	MM_NULLMC		0L
+
+/* Severity */
+#define	MM_HALT			0x1
+#define	MM_ERROR		0x2
+#define	MM_WARNING		0x3
+#define	MM_INFO			0x4
+#define	MM_NOSEV		0x0
+
+/* Return */
+#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
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+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..1cc6fdd
--- /dev/null
+++ b/src/misc/fmtmsg.c
@@ -0,0 +1,113 @@
+/* Public domain fmtmsg()
+ * Written by Isaac Dunham, 2014
+ */
+#include <fmtmsg.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+static char *_msgtok[] = {
+"label", "severity", "text", "action", "tag", NULL
+};
+
+/* 
+ * 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;
+}
+
+static int _validenv(char *evar)
+{
+	size_t i;
+
+	while (evar && evar[0]) {
+		for(i=0; _msgtok[i]; i++) {
+			if (!_strcolcmp(_msgtok[i], evar)) break;
+		}
+		if (_msgtok[i] == NULL) return 0;
+		evar = strchr(evar, ':');
+		if (evar) evar++;
+	}
+	return 1;
+}
+
+static int _writemsg(const char *msg)
+{
+	int buflen = strlen(msg);
+	if (write(2, msg, buflen) < buflen) return MM_NOMSG;
+	return 0;
+}
+
+int fmtmsg(long classification, const char *label, int severity,
+	       	const char *text, const char *action, const char *tag)
+{
+	int ret = 0, i = 0, consolefd = 0;
+	char *errstring = MM_NULLSEV, *cmsg = getenv("MSGVERB");
+
+	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\nTO FIX: %s %s\n", 
+				label, errstring, text, action, tag)<15) 
+			ret = MM_NOCON;
+	}
+
+	if (cmsg && !_validenv(cmsg)) cmsg = (char *)0;
+	while ((classification & MM_PRINT) && (i < 6)) {
+		if (cmsg) i = 0;
+		while (cmsg && _msgtok[i]) {
+			if (!_strcolcmp(_msgtok[i], cmsg)) break;
+			i++;
+		}
+		i++;
+		switch (i) {
+		case 1:
+			ret |= _writemsg(label);
+			ret |= _writemsg(": ");
+			break;
+		case 2:
+		    	if (errstring){
+				ret |= _writemsg(errstring);
+				ret |= _writemsg(": ");
+			}
+			break;
+		case 3:
+			ret |= _writemsg(text);
+			break;
+		case 4:
+			ret |= _writemsg("\nTO FIX: ");
+			ret |= _writemsg(action);
+			break;
+		case 5:
+			ret |= _writemsg(" ");
+			ret |= _writemsg(tag);
+			break;
+		}
+		if (cmsg) {
+			cmsg = strchr(cmsg, ':');
+			if (cmsg) cmsg++;
+			if (!cmsg || !cmsg[0]) i = 6;
+		}
+	}
+	if (classification & MM_PRINT) _writemsg("\n");
+	if ((ret & (MM_NOCON|MM_NOMSG) ) ==  (MM_NOCON|MM_NOMSG))
+		ret = MM_NOTOK;
+	return ret;
+}

[-- Attachment #3: testfmt.c --]
[-- Type: text/plain, Size: 395 bytes --]

#include <fmtmsg.h>

/* fmtmsg test program, by Isaac Dunham
 * Released into the public domain with no guarantees whatsoever.
 *
 * Usage:
 * testfmt LABEL N TEXT ACTION TAG
 */


int main (int argc, char **argv)
{
	int sev = MM_NOSEV;
	//close(2);
	if (argc > 1) sev = atoi(argv[2]);
	fmtmsg(MM_SOFT|MM_APPL|MM_PRINT|MM_CONSOLE|MM_RECOVER, argv[1], sev,
		       argv[3], argv[4], argv[5]);
}

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2014-06-21 23:32 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-25  5:06 [PATCH] Implement fmtmsg Isaac Dunham
2014-04-25 14:49 ` Rich Felker
2014-04-25 16:10   ` [PATCH v2] " Isaac Dunham
2014-04-25 16:12     ` Isaac Dunham
2014-06-20  3:23       ` Rich Felker
2014-06-20  4:46         ` Isaac Dunham
2014-06-20 14:32         ` [PATCH v3] " Isaac Dunham
2014-06-21  4:46           ` Rich Felker
2014-06-21 13:41             ` Isaac Dunham
2014-06-21 14:39               ` Rich Felker
2014-06-21 15:56                 ` Isaac Dunham
2014-06-21 16:18                   ` Rich Felker
2014-06-21 17:13                     ` Isaac Dunham
2014-06-21 23:32                       ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).