xref: /illumos-gate/usr/src/lib/libc/port/gen/pfmt_print.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * pfmt_print() - format and print
327c478bd9Sstevel@tonic-gate  */
33*7257d1b4Sraf #include "lint.h"
347c478bd9Sstevel@tonic-gate #include "mtlib.h"
357c478bd9Sstevel@tonic-gate #include <pfmt.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <stdarg.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <thread.h>
407c478bd9Sstevel@tonic-gate #include <ctype.h>
417c478bd9Sstevel@tonic-gate #include "pfmt_data.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /* Catalogue for local messages */
447c478bd9Sstevel@tonic-gate #define	fmt_cat		"uxlibc"
457c478bd9Sstevel@tonic-gate #define	def_colon	": "
467c478bd9Sstevel@tonic-gate #define	def_colonid	2
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* Table of default severities */
497c478bd9Sstevel@tonic-gate static const char *sev_list[] = {
507c478bd9Sstevel@tonic-gate 	"SEV = %d",
517c478bd9Sstevel@tonic-gate 	"TO FIX",
527c478bd9Sstevel@tonic-gate 	"ERROR",
537c478bd9Sstevel@tonic-gate 	"HALT",
547c478bd9Sstevel@tonic-gate 	"WARNING",
557c478bd9Sstevel@tonic-gate 	"INFO"
567c478bd9Sstevel@tonic-gate };
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate int
__pfmt_print(FILE * stream,long flag,const char * format,const char ** text_ptr,const char ** sev_ptr,va_list args)597c478bd9Sstevel@tonic-gate __pfmt_print(FILE *stream, long flag, const char *format,
607c478bd9Sstevel@tonic-gate 	const char **text_ptr, const char **sev_ptr, va_list args)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	const char *ptr;
637c478bd9Sstevel@tonic-gate 	char catbuf[DB_NAME_LEN];
647c478bd9Sstevel@tonic-gate 	int i, status;
657c478bd9Sstevel@tonic-gate 	int length = 0;
667c478bd9Sstevel@tonic-gate 	int txtmsgnum = 0;
677c478bd9Sstevel@tonic-gate 	int dofmt = (flag & (long)MM_NOSTD) == 0;
687c478bd9Sstevel@tonic-gate 	long doact = (flag & (long)MM_ACTION);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if (format && !(flag & (long)MM_NOGET)) {
717c478bd9Sstevel@tonic-gate 		char c;
727c478bd9Sstevel@tonic-gate 		ptr = format;
737c478bd9Sstevel@tonic-gate 		for (i = 0; i < DB_NAME_LEN - 1 && (c = *ptr++) && c != ':';
747c478bd9Sstevel@tonic-gate 		    i++)
757c478bd9Sstevel@tonic-gate 			catbuf[i] = c;
767c478bd9Sstevel@tonic-gate 		/* Extract the message number */
777c478bd9Sstevel@tonic-gate 		if (i != DB_NAME_LEN - 1 && c) {
787c478bd9Sstevel@tonic-gate 			catbuf[i] = '\0';
797c478bd9Sstevel@tonic-gate 			while (isdigit(c = *ptr++)) {
807c478bd9Sstevel@tonic-gate 				txtmsgnum *= 10;
817c478bd9Sstevel@tonic-gate 				txtmsgnum += c - '0';
827c478bd9Sstevel@tonic-gate 			}
837c478bd9Sstevel@tonic-gate 			if (c != ':')
847c478bd9Sstevel@tonic-gate 				txtmsgnum = -1;
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 		else
877c478bd9Sstevel@tonic-gate 			txtmsgnum = -1;
887c478bd9Sstevel@tonic-gate 		format = __gtxt(catbuf, txtmsgnum, ptr);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (text_ptr)
937c478bd9Sstevel@tonic-gate 		*text_ptr = format;
947c478bd9Sstevel@tonic-gate 	if (dofmt) {
957c478bd9Sstevel@tonic-gate 		char label[MAXLABEL];
967c478bd9Sstevel@tonic-gate 		int severity, sev, d_sev;
977c478bd9Sstevel@tonic-gate 		const char *psev = NULL, *colon;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 		lrw_rdlock(&_rw_pfmt_label);
1007c478bd9Sstevel@tonic-gate 		(void) strlcpy(label, __pfmt_label, MAXLABEL);
1017c478bd9Sstevel@tonic-gate 		lrw_unlock(&_rw_pfmt_label);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		colon = __gtxt(fmt_cat, def_colonid, def_colon);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		if (label[0] != '\0' && stream) {
1067c478bd9Sstevel@tonic-gate 			if ((status = fputs(label, stream)) < 0)
1077c478bd9Sstevel@tonic-gate 				return (-1);
1087c478bd9Sstevel@tonic-gate 			length += status;
1097c478bd9Sstevel@tonic-gate 			if ((status = fputs(colon, stream)) < 0)
1107c478bd9Sstevel@tonic-gate 				return (-1);
1117c478bd9Sstevel@tonic-gate 			length += status;
1127c478bd9Sstevel@tonic-gate 		}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		severity = (int)(flag & 0xff);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		if (doact) {
1177c478bd9Sstevel@tonic-gate 			d_sev = sev = 1;
1187c478bd9Sstevel@tonic-gate 		} else if (severity <= MM_INFO) {
1197c478bd9Sstevel@tonic-gate 			sev = severity + 3;
1207c478bd9Sstevel@tonic-gate 			d_sev = severity + 2;
1217c478bd9Sstevel@tonic-gate 		} else {
1227c478bd9Sstevel@tonic-gate 			int i;
1237c478bd9Sstevel@tonic-gate 			lrw_rdlock(&_rw_pfmt_sev_tab);
1247c478bd9Sstevel@tonic-gate 			for (i = 0; i < __pfmt_nsev; i++) {
1257c478bd9Sstevel@tonic-gate 				if (__pfmt_sev_tab[i].severity == severity) {
1267c478bd9Sstevel@tonic-gate 					psev = __pfmt_sev_tab[i].string;
1277c478bd9Sstevel@tonic-gate 					d_sev = sev = -1;
1287c478bd9Sstevel@tonic-gate 					break;
1297c478bd9Sstevel@tonic-gate 				}
1307c478bd9Sstevel@tonic-gate 			}
1317c478bd9Sstevel@tonic-gate 			lrw_unlock(&_rw_pfmt_sev_tab);
1327c478bd9Sstevel@tonic-gate 			if (i == __pfmt_nsev)
1337c478bd9Sstevel@tonic-gate 				d_sev = sev = 0;
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		if (sev >= 0) {
1377c478bd9Sstevel@tonic-gate 			psev = __gtxt(fmt_cat, sev, sev_list[d_sev]);
1387c478bd9Sstevel@tonic-gate 		}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 		if (sev_ptr)
1417c478bd9Sstevel@tonic-gate 			*sev_ptr = psev;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		if (stream) {
1447c478bd9Sstevel@tonic-gate 			if ((status = fprintf(stream, psev, severity)) < 0)
1457c478bd9Sstevel@tonic-gate 				return (-1);
1467c478bd9Sstevel@tonic-gate 			length += status;
1477c478bd9Sstevel@tonic-gate 			if ((status = fputs(colon, stream)) < 0)
1487c478bd9Sstevel@tonic-gate 				return (-1);
1497c478bd9Sstevel@tonic-gate 			length += status;
1507c478bd9Sstevel@tonic-gate 		} else
1517c478bd9Sstevel@tonic-gate 			return (-1);
1527c478bd9Sstevel@tonic-gate 	} else if (sev_ptr)
1537c478bd9Sstevel@tonic-gate 		*sev_ptr = NULL;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (stream) {
1567c478bd9Sstevel@tonic-gate 		if ((status = vfprintf(stream, format, args)) < 0)
1577c478bd9Sstevel@tonic-gate 			return (-1);
1587c478bd9Sstevel@tonic-gate 		length += status;
1597c478bd9Sstevel@tonic-gate 	} else
1607c478bd9Sstevel@tonic-gate 		return (-1);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	return (length);
1637c478bd9Sstevel@tonic-gate }
164