xref: /illumos-gate/usr/src/lib/libc/port/gen/pfmt_print.c (revision 7257d1b4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7257d1b4Sraf  * Common Development and Distribution License (the "License").
6*7257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*7257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * pfmt_print() - format and print
347c478bd9Sstevel@tonic-gate  */
35*7257d1b4Sraf #include "lint.h"
367c478bd9Sstevel@tonic-gate #include "mtlib.h"
377c478bd9Sstevel@tonic-gate #include <pfmt.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <stdarg.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <thread.h>
427c478bd9Sstevel@tonic-gate #include <ctype.h>
437c478bd9Sstevel@tonic-gate #include "pfmt_data.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /* Catalogue for local messages */
467c478bd9Sstevel@tonic-gate #define	fmt_cat		"uxlibc"
477c478bd9Sstevel@tonic-gate #define	def_colon	": "
487c478bd9Sstevel@tonic-gate #define	def_colonid	2
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* Table of default severities */
517c478bd9Sstevel@tonic-gate static const char *sev_list[] = {
527c478bd9Sstevel@tonic-gate 	"SEV = %d",
537c478bd9Sstevel@tonic-gate 	"TO FIX",
547c478bd9Sstevel@tonic-gate 	"ERROR",
557c478bd9Sstevel@tonic-gate 	"HALT",
567c478bd9Sstevel@tonic-gate 	"WARNING",
577c478bd9Sstevel@tonic-gate 	"INFO"
587c478bd9Sstevel@tonic-gate };
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate int
__pfmt_print(FILE * stream,long flag,const char * format,const char ** text_ptr,const char ** sev_ptr,va_list args)617c478bd9Sstevel@tonic-gate __pfmt_print(FILE *stream, long flag, const char *format,
627c478bd9Sstevel@tonic-gate 	const char **text_ptr, const char **sev_ptr, va_list args)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	const char *ptr;
657c478bd9Sstevel@tonic-gate 	char catbuf[DB_NAME_LEN];
667c478bd9Sstevel@tonic-gate 	int i, status;
677c478bd9Sstevel@tonic-gate 	int length = 0;
687c478bd9Sstevel@tonic-gate 	int txtmsgnum = 0;
697c478bd9Sstevel@tonic-gate 	int dofmt = (flag & (long)MM_NOSTD) == 0;
707c478bd9Sstevel@tonic-gate 	long doact = (flag & (long)MM_ACTION);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (format && !(flag & (long)MM_NOGET)) {
737c478bd9Sstevel@tonic-gate 		char c;
747c478bd9Sstevel@tonic-gate 		ptr = format;
757c478bd9Sstevel@tonic-gate 		for (i = 0; i < DB_NAME_LEN - 1 && (c = *ptr++) && c != ':';
76*7257d1b4Sraf 		    i++)
777c478bd9Sstevel@tonic-gate 			catbuf[i] = c;
787c478bd9Sstevel@tonic-gate 		/* Extract the message number */
797c478bd9Sstevel@tonic-gate 		if (i != DB_NAME_LEN - 1 && c) {
807c478bd9Sstevel@tonic-gate 			catbuf[i] = '\0';
817c478bd9Sstevel@tonic-gate 			while (isdigit(c = *ptr++)) {
827c478bd9Sstevel@tonic-gate 				txtmsgnum *= 10;
837c478bd9Sstevel@tonic-gate 				txtmsgnum += c - '0';
847c478bd9Sstevel@tonic-gate 			}
857c478bd9Sstevel@tonic-gate 			if (c != ':')
867c478bd9Sstevel@tonic-gate 				txtmsgnum = -1;
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 		else
897c478bd9Sstevel@tonic-gate 			txtmsgnum = -1;
907c478bd9Sstevel@tonic-gate 		format = __gtxt(catbuf, txtmsgnum, ptr);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (text_ptr)
957c478bd9Sstevel@tonic-gate 		*text_ptr = format;
967c478bd9Sstevel@tonic-gate 	if (dofmt) {
977c478bd9Sstevel@tonic-gate 		char label[MAXLABEL];
987c478bd9Sstevel@tonic-gate 		int severity, sev, d_sev;
997c478bd9Sstevel@tonic-gate 		const char *psev = NULL, *colon;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 		lrw_rdlock(&_rw_pfmt_label);
1027c478bd9Sstevel@tonic-gate 		(void) strlcpy(label, __pfmt_label, MAXLABEL);
1037c478bd9Sstevel@tonic-gate 		lrw_unlock(&_rw_pfmt_label);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		colon = __gtxt(fmt_cat, def_colonid, def_colon);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		if (label[0] != '\0' && stream) {
1087c478bd9Sstevel@tonic-gate 			if ((status = fputs(label, stream)) < 0)
1097c478bd9Sstevel@tonic-gate 				return (-1);
1107c478bd9Sstevel@tonic-gate 			length += status;
1117c478bd9Sstevel@tonic-gate 			if ((status = fputs(colon, stream)) < 0)
1127c478bd9Sstevel@tonic-gate 				return (-1);
1137c478bd9Sstevel@tonic-gate 			length += status;
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		severity = (int)(flag & 0xff);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		if (doact) {
1197c478bd9Sstevel@tonic-gate 			d_sev = sev = 1;
1207c478bd9Sstevel@tonic-gate 		} else if (severity <= MM_INFO) {
1217c478bd9Sstevel@tonic-gate 			sev = severity + 3;
1227c478bd9Sstevel@tonic-gate 			d_sev = severity + 2;
1237c478bd9Sstevel@tonic-gate 		} else {
1247c478bd9Sstevel@tonic-gate 			int i;
1257c478bd9Sstevel@tonic-gate 			lrw_rdlock(&_rw_pfmt_sev_tab);
1267c478bd9Sstevel@tonic-gate 			for (i = 0; i < __pfmt_nsev; i++) {
1277c478bd9Sstevel@tonic-gate 				if (__pfmt_sev_tab[i].severity == severity) {
1287c478bd9Sstevel@tonic-gate 					psev = __pfmt_sev_tab[i].string;
1297c478bd9Sstevel@tonic-gate 					d_sev = sev = -1;
1307c478bd9Sstevel@tonic-gate 					break;
1317c478bd9Sstevel@tonic-gate 				}
1327c478bd9Sstevel@tonic-gate 			}
1337c478bd9Sstevel@tonic-gate 			lrw_unlock(&_rw_pfmt_sev_tab);
1347c478bd9Sstevel@tonic-gate 			if (i == __pfmt_nsev)
1357c478bd9Sstevel@tonic-gate 				d_sev = sev = 0;
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		if (sev >= 0) {
1397c478bd9Sstevel@tonic-gate 			psev = __gtxt(fmt_cat, sev, sev_list[d_sev]);
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		if (sev_ptr)
1437c478bd9Sstevel@tonic-gate 			*sev_ptr = psev;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		if (stream) {
1467c478bd9Sstevel@tonic-gate 			if ((status = fprintf(stream, psev, severity)) < 0)
1477c478bd9Sstevel@tonic-gate 				return (-1);
1487c478bd9Sstevel@tonic-gate 			length += status;
1497c478bd9Sstevel@tonic-gate 			if ((status = fputs(colon, stream)) < 0)
1507c478bd9Sstevel@tonic-gate 				return (-1);
1517c478bd9Sstevel@tonic-gate 			length += status;
1527c478bd9Sstevel@tonic-gate 		} else
1537c478bd9Sstevel@tonic-gate 			return (-1);
1547c478bd9Sstevel@tonic-gate 	} else if (sev_ptr)
1557c478bd9Sstevel@tonic-gate 		*sev_ptr = NULL;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (stream) {
1587c478bd9Sstevel@tonic-gate 		if ((status = vfprintf(stream, format, args)) < 0)
1597c478bd9Sstevel@tonic-gate 			return (-1);
1607c478bd9Sstevel@tonic-gate 		length += status;
1617c478bd9Sstevel@tonic-gate 	} else
1627c478bd9Sstevel@tonic-gate 		return (-1);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	return (length);
1657c478bd9Sstevel@tonic-gate }
166