xref: /onnv-gate/usr/src/lib/libc/port/gen/pfmt_print.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * pfmt_print() - format and print
340Sstevel@tonic-gate  */
35*6812Sraf #include "lint.h"
360Sstevel@tonic-gate #include "mtlib.h"
370Sstevel@tonic-gate #include <pfmt.h>
380Sstevel@tonic-gate #include <stdio.h>
390Sstevel@tonic-gate #include <stdarg.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate #include <thread.h>
420Sstevel@tonic-gate #include <ctype.h>
430Sstevel@tonic-gate #include "pfmt_data.h"
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /* Catalogue for local messages */
460Sstevel@tonic-gate #define	fmt_cat		"uxlibc"
470Sstevel@tonic-gate #define	def_colon	": "
480Sstevel@tonic-gate #define	def_colonid	2
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /* Table of default severities */
510Sstevel@tonic-gate static const char *sev_list[] = {
520Sstevel@tonic-gate 	"SEV = %d",
530Sstevel@tonic-gate 	"TO FIX",
540Sstevel@tonic-gate 	"ERROR",
550Sstevel@tonic-gate 	"HALT",
560Sstevel@tonic-gate 	"WARNING",
570Sstevel@tonic-gate 	"INFO"
580Sstevel@tonic-gate };
590Sstevel@tonic-gate 
600Sstevel@tonic-gate int
__pfmt_print(FILE * stream,long flag,const char * format,const char ** text_ptr,const char ** sev_ptr,va_list args)610Sstevel@tonic-gate __pfmt_print(FILE *stream, long flag, const char *format,
620Sstevel@tonic-gate 	const char **text_ptr, const char **sev_ptr, va_list args)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	const char *ptr;
650Sstevel@tonic-gate 	char catbuf[DB_NAME_LEN];
660Sstevel@tonic-gate 	int i, status;
670Sstevel@tonic-gate 	int length = 0;
680Sstevel@tonic-gate 	int txtmsgnum = 0;
690Sstevel@tonic-gate 	int dofmt = (flag & (long)MM_NOSTD) == 0;
700Sstevel@tonic-gate 	long doact = (flag & (long)MM_ACTION);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (format && !(flag & (long)MM_NOGET)) {
730Sstevel@tonic-gate 		char c;
740Sstevel@tonic-gate 		ptr = format;
750Sstevel@tonic-gate 		for (i = 0; i < DB_NAME_LEN - 1 && (c = *ptr++) && c != ':';
76*6812Sraf 		    i++)
770Sstevel@tonic-gate 			catbuf[i] = c;
780Sstevel@tonic-gate 		/* Extract the message number */
790Sstevel@tonic-gate 		if (i != DB_NAME_LEN - 1 && c) {
800Sstevel@tonic-gate 			catbuf[i] = '\0';
810Sstevel@tonic-gate 			while (isdigit(c = *ptr++)) {
820Sstevel@tonic-gate 				txtmsgnum *= 10;
830Sstevel@tonic-gate 				txtmsgnum += c - '0';
840Sstevel@tonic-gate 			}
850Sstevel@tonic-gate 			if (c != ':')
860Sstevel@tonic-gate 				txtmsgnum = -1;
870Sstevel@tonic-gate 		}
880Sstevel@tonic-gate 		else
890Sstevel@tonic-gate 			txtmsgnum = -1;
900Sstevel@tonic-gate 		format = __gtxt(catbuf, txtmsgnum, ptr);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	if (text_ptr)
950Sstevel@tonic-gate 		*text_ptr = format;
960Sstevel@tonic-gate 	if (dofmt) {
970Sstevel@tonic-gate 		char label[MAXLABEL];
980Sstevel@tonic-gate 		int severity, sev, d_sev;
990Sstevel@tonic-gate 		const char *psev = NULL, *colon;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 		lrw_rdlock(&_rw_pfmt_label);
1020Sstevel@tonic-gate 		(void) strlcpy(label, __pfmt_label, MAXLABEL);
1030Sstevel@tonic-gate 		lrw_unlock(&_rw_pfmt_label);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 		colon = __gtxt(fmt_cat, def_colonid, def_colon);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 		if (label[0] != '\0' && stream) {
1080Sstevel@tonic-gate 			if ((status = fputs(label, stream)) < 0)
1090Sstevel@tonic-gate 				return (-1);
1100Sstevel@tonic-gate 			length += status;
1110Sstevel@tonic-gate 			if ((status = fputs(colon, stream)) < 0)
1120Sstevel@tonic-gate 				return (-1);
1130Sstevel@tonic-gate 			length += status;
1140Sstevel@tonic-gate 		}
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		severity = (int)(flag & 0xff);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 		if (doact) {
1190Sstevel@tonic-gate 			d_sev = sev = 1;
1200Sstevel@tonic-gate 		} else if (severity <= MM_INFO) {
1210Sstevel@tonic-gate 			sev = severity + 3;
1220Sstevel@tonic-gate 			d_sev = severity + 2;
1230Sstevel@tonic-gate 		} else {
1240Sstevel@tonic-gate 			int i;
1250Sstevel@tonic-gate 			lrw_rdlock(&_rw_pfmt_sev_tab);
1260Sstevel@tonic-gate 			for (i = 0; i < __pfmt_nsev; i++) {
1270Sstevel@tonic-gate 				if (__pfmt_sev_tab[i].severity == severity) {
1280Sstevel@tonic-gate 					psev = __pfmt_sev_tab[i].string;
1290Sstevel@tonic-gate 					d_sev = sev = -1;
1300Sstevel@tonic-gate 					break;
1310Sstevel@tonic-gate 				}
1320Sstevel@tonic-gate 			}
1330Sstevel@tonic-gate 			lrw_unlock(&_rw_pfmt_sev_tab);
1340Sstevel@tonic-gate 			if (i == __pfmt_nsev)
1350Sstevel@tonic-gate 				d_sev = sev = 0;
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 		if (sev >= 0) {
1390Sstevel@tonic-gate 			psev = __gtxt(fmt_cat, sev, sev_list[d_sev]);
1400Sstevel@tonic-gate 		}
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		if (sev_ptr)
1430Sstevel@tonic-gate 			*sev_ptr = psev;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		if (stream) {
1460Sstevel@tonic-gate 			if ((status = fprintf(stream, psev, severity)) < 0)
1470Sstevel@tonic-gate 				return (-1);
1480Sstevel@tonic-gate 			length += status;
1490Sstevel@tonic-gate 			if ((status = fputs(colon, stream)) < 0)
1500Sstevel@tonic-gate 				return (-1);
1510Sstevel@tonic-gate 			length += status;
1520Sstevel@tonic-gate 		} else
1530Sstevel@tonic-gate 			return (-1);
1540Sstevel@tonic-gate 	} else if (sev_ptr)
1550Sstevel@tonic-gate 		*sev_ptr = NULL;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (stream) {
1580Sstevel@tonic-gate 		if ((status = vfprintf(stream, format, args)) < 0)
1590Sstevel@tonic-gate 			return (-1);
1600Sstevel@tonic-gate 		length += status;
1610Sstevel@tonic-gate 	} else
1620Sstevel@tonic-gate 		return (-1);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	return (length);
1650Sstevel@tonic-gate }
166