xref: /illumos-gate/usr/src/lib/libc/port/gen/pfmt_print.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
30*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  * pfmt_print() - format and print
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate #include "synonyms.h"
37*7c478bd9Sstevel@tonic-gate #include "mtlib.h"
38*7c478bd9Sstevel@tonic-gate #include <pfmt.h>
39*7c478bd9Sstevel@tonic-gate #include <stdio.h>
40*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
41*7c478bd9Sstevel@tonic-gate #include <string.h>
42*7c478bd9Sstevel@tonic-gate #include <thread.h>
43*7c478bd9Sstevel@tonic-gate #include <ctype.h>
44*7c478bd9Sstevel@tonic-gate #include "pfmt_data.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /* Catalogue for local messages */
47*7c478bd9Sstevel@tonic-gate #define	fmt_cat		"uxlibc"
48*7c478bd9Sstevel@tonic-gate #define	def_colon	": "
49*7c478bd9Sstevel@tonic-gate #define	def_colonid	2
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate /* Table of default severities */
52*7c478bd9Sstevel@tonic-gate static const char *sev_list[] = {
53*7c478bd9Sstevel@tonic-gate 	"SEV = %d",
54*7c478bd9Sstevel@tonic-gate 	"TO FIX",
55*7c478bd9Sstevel@tonic-gate 	"ERROR",
56*7c478bd9Sstevel@tonic-gate 	"HALT",
57*7c478bd9Sstevel@tonic-gate 	"WARNING",
58*7c478bd9Sstevel@tonic-gate 	"INFO"
59*7c478bd9Sstevel@tonic-gate };
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate int
62*7c478bd9Sstevel@tonic-gate __pfmt_print(FILE *stream, long flag, const char *format,
63*7c478bd9Sstevel@tonic-gate 	const char **text_ptr, const char **sev_ptr, va_list args)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	const char *ptr;
66*7c478bd9Sstevel@tonic-gate 	char catbuf[DB_NAME_LEN];
67*7c478bd9Sstevel@tonic-gate 	int i, status;
68*7c478bd9Sstevel@tonic-gate 	int length = 0;
69*7c478bd9Sstevel@tonic-gate 	int txtmsgnum = 0;
70*7c478bd9Sstevel@tonic-gate 	int dofmt = (flag & (long)MM_NOSTD) == 0;
71*7c478bd9Sstevel@tonic-gate 	long doact = (flag & (long)MM_ACTION);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	if (format && !(flag & (long)MM_NOGET)) {
74*7c478bd9Sstevel@tonic-gate 		char c;
75*7c478bd9Sstevel@tonic-gate 		ptr = format;
76*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < DB_NAME_LEN - 1 && (c = *ptr++) && c != ':';
77*7c478bd9Sstevel@tonic-gate 			i++)
78*7c478bd9Sstevel@tonic-gate 			catbuf[i] = c;
79*7c478bd9Sstevel@tonic-gate 		/* Extract the message number */
80*7c478bd9Sstevel@tonic-gate 		if (i != DB_NAME_LEN - 1 && c) {
81*7c478bd9Sstevel@tonic-gate 			catbuf[i] = '\0';
82*7c478bd9Sstevel@tonic-gate 			while (isdigit(c = *ptr++)) {
83*7c478bd9Sstevel@tonic-gate 				txtmsgnum *= 10;
84*7c478bd9Sstevel@tonic-gate 				txtmsgnum += c - '0';
85*7c478bd9Sstevel@tonic-gate 			}
86*7c478bd9Sstevel@tonic-gate 			if (c != ':')
87*7c478bd9Sstevel@tonic-gate 				txtmsgnum = -1;
88*7c478bd9Sstevel@tonic-gate 		}
89*7c478bd9Sstevel@tonic-gate 		else
90*7c478bd9Sstevel@tonic-gate 			txtmsgnum = -1;
91*7c478bd9Sstevel@tonic-gate 		format = __gtxt(catbuf, txtmsgnum, ptr);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	if (text_ptr)
96*7c478bd9Sstevel@tonic-gate 		*text_ptr = format;
97*7c478bd9Sstevel@tonic-gate 	if (dofmt) {
98*7c478bd9Sstevel@tonic-gate 		char label[MAXLABEL];
99*7c478bd9Sstevel@tonic-gate 		int severity, sev, d_sev;
100*7c478bd9Sstevel@tonic-gate 		const char *psev = NULL, *colon;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 		lrw_rdlock(&_rw_pfmt_label);
103*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(label, __pfmt_label, MAXLABEL);
104*7c478bd9Sstevel@tonic-gate 		lrw_unlock(&_rw_pfmt_label);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 		colon = __gtxt(fmt_cat, def_colonid, def_colon);
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 		if (label[0] != '\0' && stream) {
109*7c478bd9Sstevel@tonic-gate 			if ((status = fputs(label, stream)) < 0)
110*7c478bd9Sstevel@tonic-gate 				return (-1);
111*7c478bd9Sstevel@tonic-gate 			length += status;
112*7c478bd9Sstevel@tonic-gate 			if ((status = fputs(colon, stream)) < 0)
113*7c478bd9Sstevel@tonic-gate 				return (-1);
114*7c478bd9Sstevel@tonic-gate 			length += status;
115*7c478bd9Sstevel@tonic-gate 		}
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 		severity = (int)(flag & 0xff);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 		if (doact) {
120*7c478bd9Sstevel@tonic-gate 			d_sev = sev = 1;
121*7c478bd9Sstevel@tonic-gate 		} else if (severity <= MM_INFO) {
122*7c478bd9Sstevel@tonic-gate 			sev = severity + 3;
123*7c478bd9Sstevel@tonic-gate 			d_sev = severity + 2;
124*7c478bd9Sstevel@tonic-gate 		} else {
125*7c478bd9Sstevel@tonic-gate 			int i;
126*7c478bd9Sstevel@tonic-gate 			lrw_rdlock(&_rw_pfmt_sev_tab);
127*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < __pfmt_nsev; i++) {
128*7c478bd9Sstevel@tonic-gate 				if (__pfmt_sev_tab[i].severity == severity) {
129*7c478bd9Sstevel@tonic-gate 					psev = __pfmt_sev_tab[i].string;
130*7c478bd9Sstevel@tonic-gate 					d_sev = sev = -1;
131*7c478bd9Sstevel@tonic-gate 					break;
132*7c478bd9Sstevel@tonic-gate 				}
133*7c478bd9Sstevel@tonic-gate 			}
134*7c478bd9Sstevel@tonic-gate 			lrw_unlock(&_rw_pfmt_sev_tab);
135*7c478bd9Sstevel@tonic-gate 			if (i == __pfmt_nsev)
136*7c478bd9Sstevel@tonic-gate 				d_sev = sev = 0;
137*7c478bd9Sstevel@tonic-gate 		}
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 		if (sev >= 0) {
140*7c478bd9Sstevel@tonic-gate 			psev = __gtxt(fmt_cat, sev, sev_list[d_sev]);
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 		if (sev_ptr)
144*7c478bd9Sstevel@tonic-gate 			*sev_ptr = psev;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 		if (stream) {
147*7c478bd9Sstevel@tonic-gate 			if ((status = fprintf(stream, psev, severity)) < 0)
148*7c478bd9Sstevel@tonic-gate 				return (-1);
149*7c478bd9Sstevel@tonic-gate 			length += status;
150*7c478bd9Sstevel@tonic-gate 			if ((status = fputs(colon, stream)) < 0)
151*7c478bd9Sstevel@tonic-gate 				return (-1);
152*7c478bd9Sstevel@tonic-gate 			length += status;
153*7c478bd9Sstevel@tonic-gate 		} else
154*7c478bd9Sstevel@tonic-gate 			return (-1);
155*7c478bd9Sstevel@tonic-gate 	} else if (sev_ptr)
156*7c478bd9Sstevel@tonic-gate 		*sev_ptr = NULL;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	if (stream) {
159*7c478bd9Sstevel@tonic-gate 		if ((status = vfprintf(stream, format, args)) < 0)
160*7c478bd9Sstevel@tonic-gate 			return (-1);
161*7c478bd9Sstevel@tonic-gate 		length += status;
162*7c478bd9Sstevel@tonic-gate 	} else
163*7c478bd9Sstevel@tonic-gate 		return (-1);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	return (length);
166*7c478bd9Sstevel@tonic-gate }
167