1 /*
2  * Copyright (c) 2003 Gunnar Ritter
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute
10  * it freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  *
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  *    misrepresented as being the original software.
19  *
20  * 3. This notice may not be removed or altered from any source distribution.
21  */
22 /*	Sccsid @(#)vpfmt.c	1.2 (gritter) 9/21/03	*/
23 
24 #include <stdio.h>
25 #include <stdarg.h>
26 
27 #include "pfmt.h"
28 
29 extern char	*pfmt_label__;
30 
31 /*
32  * Strip catalog and msgnum from s, but only if they actually appear.
33  */
34 static const char *
begin(const char * s,long flags)35 begin(const char *s, long flags)
36 {
37 	const char	*sp;
38 
39 	if (flags & MM_NOGET)
40 		return s;
41 	sp = s;
42 	if (*sp && *sp != ':') {
43 		sp++;
44 		while (*sp && *sp != '/' && *sp != ':' && sp - s < 14)
45 			sp++;
46 	}
47 	if (*sp++ != ':')
48 		return s;
49 	while (*sp >= '0' && *sp <= '9')
50 		sp++;
51 	if (*sp++ != ':' || *sp == '\0')
52 		return s;
53 	return sp;
54 }
55 
56 int
vpfmt(FILE * stream,long flags,const char * fmt,va_list ap)57 vpfmt(FILE *stream, long flags, const char *fmt, va_list ap)
58 {
59 	int	n = 0;
60 	const char	*severity = NULL;
61 	char	sevbuf[25];
62 
63 	if ((flags&MM_NOSTD) == 0) {
64 		if (flags & MM_ACTION)
65 			severity = "TO FIX";
66 		else switch (flags & 0377) {
67 		case MM_HALT:
68 			severity = "HALT";
69 			break;
70 		case MM_WARNING:
71 			severity = "WARNING";
72 			break;
73 		case MM_INFO:
74 			severity = "INFO";
75 			break;
76 		case MM_ERROR:
77 			severity = "ERROR";
78 			break;
79 		default:
80 			snprintf(sevbuf, sizeof sevbuf, "SEV=%ld", flags&0377);
81 			severity = sevbuf;
82 		}
83 		if (pfmt_label__)
84 			n = fprintf(stream, "%s: ", pfmt_label__);
85 		if (severity)
86 			n += fprintf(stream, "%s: ", severity);
87 	}
88 	n += vfprintf(stream, begin(fmt, flags), ap);
89 	return n;
90 }
91