mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [PATCH v3] Implement fmtmsg
Date: Sat, 21 Jun 2014 00:46:26 -0400	[thread overview]
Message-ID: <20140621044626.GE179@brightrain.aerifal.cx> (raw)
In-Reply-To: <20140620143200.GA783@muslin>

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

On Fri, Jun 20, 2014 at 07:32:01AM -0700, Isaac Dunham wrote:
> Here's a third revision of fmtmsg.

Thanks, it looks good. I'm doing a bit of cleanup before I commit --
mostly things like mismatches spaces/tabs in indention/alignment,
trailing spaces, etc. Also taking out the useless if around the while
loop that contains the if condition as part of the while condition,
debloating the header a bit (all the hex constants), adding protection
against pthread cancellation, and fixing one error in the header:
0xffffffff is not a valid value for MM_NOTOK since it doesn't fit in
int.

I'm a bit tired now so rather than just commit and possibly have
stupid mistakes, I'm sending my draft revisions here (attached). If
you get a chance please take a look and see if they look ok.

Thanks!

Rich

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

/* 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>
#include <pthread.h>

/* 
 * 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
	};
	int cs;

	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);

	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;
	}

	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;

	pthread_setcancelstate(cs, 0);

	return ret;
}

[-- Attachment #3: fmtmsg.h --]
[-- Type: text/plain, Size: 741 bytes --]

#ifndef _FMTMSG_H
#define _FMTMSG_H

#ifdef __cplusplus
extern "C" {
#endif

#define MM_HARD		1
#define MM_SOFT		2
#define MM_FIRM		4

#define MM_APPL		8
#define MM_UTIL		16
#define MM_OPSYS	32

#define MM_RECOVER	64
#define MM_NRECOV	128

#define MM_PRINT	256
#define MM_CONSOLE	512

#define MM_NULLMC	0L

#define MM_HALT		1
#define MM_ERROR	2
#define MM_WARNING	3
#define MM_INFO		4
#define MM_NOSEV	0

#define MM_OK		0
#define MM_NOTOK	(-1)
#define MM_NOMSG	1
#define MM_NOCON	4

#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

  reply	other threads:[~2014-06-21  4:46 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-25  5:06 [PATCH] " 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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140621044626.GE179@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).