xref: /illumos-gate/usr/src/cmd/sgs/tsort/common/errtext.c (revision d6555420)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*d6555420Smike_s 
237c478bd9Sstevel@tonic-gate /*
24*d6555420Smike_s  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  *	Routines to print and adjust options on
367c478bd9Sstevel@tonic-gate  *	error messages.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include	"errmsg.h"
407c478bd9Sstevel@tonic-gate #include	<stdio.h>
417c478bd9Sstevel@tonic-gate #include	<stdarg.h>
427c478bd9Sstevel@tonic-gate #include	<string.h>
437c478bd9Sstevel@tonic-gate #include	<errno.h>
447c478bd9Sstevel@tonic-gate #include	<stdlib.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  *	Internal form, to handle both errtext() and _errmsg()
487c478bd9Sstevel@tonic-gate  */
49*d6555420Smike_s /* PRINTFLIKE2 */
507c478bd9Sstevel@tonic-gate void
517c478bd9Sstevel@tonic-gate __errtext(int severity, char *format, va_list ap)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	int	puterrno = 0;		/* true if an errno msg was printed */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	Err.severity = severity;
567c478bd9Sstevel@tonic-gate 	errverb(getenv("ERRVERB"));
577c478bd9Sstevel@tonic-gate 	errbefore(Err.severity, format, ap);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (Err.severity == EIGNORE)
607c478bd9Sstevel@tonic-gate 		goto after;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if (Err.vbell)
63*d6555420Smike_s 		(void) fputc('\07', stderr);
647c478bd9Sstevel@tonic-gate 	if (Err.vprefix && Err.prefix) {
65*d6555420Smike_s 		(void) fputs(Err.prefix, stderr);
66*d6555420Smike_s 		(void) fputc(' ', stderr);
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 	if (Err.vsource) {
697c478bd9Sstevel@tonic-gate 		if (Err.envsource ||
707c478bd9Sstevel@tonic-gate 			(Err.envsource = getenv("ERRSOURCE"))) {
71*d6555420Smike_s 			(void) fprintf(stderr, "%s: ", Err.envsource);
727c478bd9Sstevel@tonic-gate 		}
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	if (Err.vsource && Err.source) {
75*d6555420Smike_s 		(void) fprintf(stderr, "%s: ", Err.source);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	if (Err.vsevmsg) {
787c478bd9Sstevel@tonic-gate 		char	**e;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		for (e = Err.sevmsg; *e; e++)
817c478bd9Sstevel@tonic-gate 			;
827c478bd9Sstevel@tonic-gate 		if (Err.severity < (e - Err.sevmsg))
83*d6555420Smike_s 			(void) fputs(Err.sevmsg[Err.severity], stderr);
847c478bd9Sstevel@tonic-gate 		else
85*d6555420Smike_s 			(void) fputs("<UNKNOWN>", stderr);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (Err.vtext) {
897c478bd9Sstevel@tonic-gate 		if (Err.vsyserr && ((int)format == EERRNO)) {
90*d6555420Smike_s 			(void) fflush(stderr);
917c478bd9Sstevel@tonic-gate 			perror("");
927c478bd9Sstevel@tonic-gate 			puterrno = 1;
937c478bd9Sstevel@tonic-gate 		} else {
94*d6555420Smike_s 			(void) vfprintf(stderr, format, ap);
95*d6555420Smike_s 			(void) fputs("\n", stderr);
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if ((errno && ((int)format != EERRNO)) &&
1007c478bd9Sstevel@tonic-gate 	    (Err.vsyserr == EYES || (Err.vsyserr ==  EDEF &&
1017c478bd9Sstevel@tonic-gate 	    (Err.severity == EHALT || Err.severity == EERROR)))) {
102*d6555420Smike_s 		(void) fputc('\t', stderr);
103*d6555420Smike_s 		(void) fflush(stderr);
1047c478bd9Sstevel@tonic-gate 		perror("");
1057c478bd9Sstevel@tonic-gate 		puterrno = 1;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (Err.vtag) {
1097c478bd9Sstevel@tonic-gate 		if (Err.tagnum)
110*d6555420Smike_s 			(void) fputc('\t', stderr);
1117c478bd9Sstevel@tonic-gate 		else
112*d6555420Smike_s 			(void) fputs("HELP FACILITY KEY: ", stderr);
1137c478bd9Sstevel@tonic-gate 		if (Err.tagstr)
114*d6555420Smike_s 			(void) fputs(Err.tagstr, stderr);
1157c478bd9Sstevel@tonic-gate 		if (Err.tagnum)
116*d6555420Smike_s 			(void) fprintf(stderr, ", line %d", Err.tagnum);
1177c478bd9Sstevel@tonic-gate 		if (puterrno)
118*d6555420Smike_s 			(void) fprintf(stderr, "\tUXerrno%d", errno);
119*d6555420Smike_s 		(void) fputc('\n', stderr);
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if ((Err.vtext || Err.vtag) &&
1237c478bd9Sstevel@tonic-gate 	    Err.vfix && Err.tofix && !Err.tagnum)
124*d6555420Smike_s 		(void) fprintf(stderr, "To Fix:\t%s\n", Err.tofix);
1257c478bd9Sstevel@tonic-gate 	after:
1267c478bd9Sstevel@tonic-gate 	erraction(errafter(Err.severity, format, ap));
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  *	external form, used by errmsg() macro, when tag is not permanently
1327c478bd9Sstevel@tonic-gate  *	assigned.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate void
1357c478bd9Sstevel@tonic-gate errtext(int severity, char *format, ...)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	va_list ap;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1407c478bd9Sstevel@tonic-gate 	__errtext(severity, format, ap);
1417c478bd9Sstevel@tonic-gate 	va_end(ap);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  *	external form, used when tag is permanently assigned.
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate void
1497c478bd9Sstevel@tonic-gate _errmsg(char *tag, int severity, char *format, ...)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	va_list ap;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	Err.tagstr = tag;
1547c478bd9Sstevel@tonic-gate 	Err.tagnum = 0;
1557c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1567c478bd9Sstevel@tonic-gate 	__errtext(severity, format, ap);
1577c478bd9Sstevel@tonic-gate 	va_end(ap);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate void
161*d6555420Smike_s errverb(char *s)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	char	buf[ BUFSIZ ];
1647c478bd9Sstevel@tonic-gate 	char   *token;
165*d6555420Smike_s 	static char   space[] = ",\t\n";
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (!s)
1687c478bd9Sstevel@tonic-gate 		return;
1697c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, s);
1707c478bd9Sstevel@tonic-gate 	for (token = errstrtok(buf, space);  token;
1717c478bd9Sstevel@tonic-gate 		token = errstrtok((char *)0, space)) {
1727c478bd9Sstevel@tonic-gate 		if (strcmp(token, "nochange") == 0) {
1737c478bd9Sstevel@tonic-gate 			Err.vbell   =  ENO;
1747c478bd9Sstevel@tonic-gate 			Err.vtext   =  EYES;
1757c478bd9Sstevel@tonic-gate 			Err.vsource =  EYES;
1767c478bd9Sstevel@tonic-gate 			Err.vsyserr =  EYES;
1777c478bd9Sstevel@tonic-gate 			Err.vtag    =  ENO;
1787c478bd9Sstevel@tonic-gate 			Err.vsevmsg =  ENO;
1797c478bd9Sstevel@tonic-gate 			Err.vfix	 =  ENO;
1807c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "silent") == 0) {
1817c478bd9Sstevel@tonic-gate 			Err.vbell   =  ENO;
1827c478bd9Sstevel@tonic-gate 			Err.vprefix =  ENO;
1837c478bd9Sstevel@tonic-gate 			Err.vtext   =  ENO;
1847c478bd9Sstevel@tonic-gate 			Err.vsource =  ENO;
1857c478bd9Sstevel@tonic-gate 			Err.vsyserr =  ENO;
1867c478bd9Sstevel@tonic-gate 			Err.vtag    =  ENO;
1877c478bd9Sstevel@tonic-gate 			Err.vsevmsg =  ENO;
1887c478bd9Sstevel@tonic-gate 			Err.vfix	 =  ENO;
1897c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "verbose") == 0) {
1907c478bd9Sstevel@tonic-gate 			Err.vbell   =  EYES;
1917c478bd9Sstevel@tonic-gate 			Err.vprefix =  EYES;
1927c478bd9Sstevel@tonic-gate 			Err.vtext   =  EYES;
1937c478bd9Sstevel@tonic-gate 			Err.vsource =  EYES;
1947c478bd9Sstevel@tonic-gate 			Err.vsyserr =  EYES;
1957c478bd9Sstevel@tonic-gate 			Err.vtag    =  EYES;
1967c478bd9Sstevel@tonic-gate 			Err.vsevmsg =  EYES;
1977c478bd9Sstevel@tonic-gate 			Err.vfix	 =  EYES;
1987c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "expert") == 0) {
1997c478bd9Sstevel@tonic-gate 			Err.vbell   =  ENO;
2007c478bd9Sstevel@tonic-gate 			Err.vprefix =  ENO;
2017c478bd9Sstevel@tonic-gate 			Err.vtext   =  EYES;
2027c478bd9Sstevel@tonic-gate 			Err.vsource =  EYES;
2037c478bd9Sstevel@tonic-gate 			Err.vsyserr =  EYES;
2047c478bd9Sstevel@tonic-gate 			Err.vtag    =  ENO;
2057c478bd9Sstevel@tonic-gate 			Err.vsevmsg =  EYES;
2067c478bd9Sstevel@tonic-gate 			Err.vfix	 =  ENO;
2077c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "bell") == 0) {
2087c478bd9Sstevel@tonic-gate 			Err.vbell = EYES;
2097c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "nobell") == 0) {
2107c478bd9Sstevel@tonic-gate 			Err.vbell = ENO;
2117c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "tag") == 0) {
2127c478bd9Sstevel@tonic-gate 			Err.vtag = EYES;
2137c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "notag") == 0) {
2147c478bd9Sstevel@tonic-gate 			Err.vtag = ENO;
2157c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "text") == 0) {
2167c478bd9Sstevel@tonic-gate 			Err.vtext = EYES;
2177c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "notext") == 0) {
2187c478bd9Sstevel@tonic-gate 			Err.vtext = ENO;
2197c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "tofix") == 0) {
2207c478bd9Sstevel@tonic-gate 			Err.vfix = EYES;
2217c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "notofix") == 0) {
2227c478bd9Sstevel@tonic-gate 			Err.vfix = ENO;
2237c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "syserr") == 0) {
2247c478bd9Sstevel@tonic-gate 			Err.vsyserr = EYES;
2257c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "nosyserr") == 0) {
2267c478bd9Sstevel@tonic-gate 			Err.vsyserr = ENO;
2277c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "defsyserr") == 0) {
2287c478bd9Sstevel@tonic-gate 			Err.vsyserr = EDEF;
2297c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "source")) {
2307c478bd9Sstevel@tonic-gate 			Err.vsource = EYES;
2317c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "nosource")  == 0) {
2327c478bd9Sstevel@tonic-gate 			Err.vsource = ENO;
2337c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "sevmsg") == 0) {
2347c478bd9Sstevel@tonic-gate 			Err.vsevmsg = EYES;
2357c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "nosevmsg") == 0) {
2367c478bd9Sstevel@tonic-gate 			Err.vsevmsg = ENO;
2377c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "prefix") == 0) {
2387c478bd9Sstevel@tonic-gate 			Err.vprefix = EYES;
2397c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "noprefix") == 0) {
2407c478bd9Sstevel@tonic-gate 			Err.vprefix = ENO;
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate }
244