xref: /illumos-gate/usr/src/cmd/sgs/tsort/common/errtext.c (revision 2a8bcb4e)
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  *	Routines to print and adjust options on
337c478bd9Sstevel@tonic-gate  *	error messages.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include	"errmsg.h"
377c478bd9Sstevel@tonic-gate #include	<stdio.h>
387c478bd9Sstevel@tonic-gate #include	<stdarg.h>
397c478bd9Sstevel@tonic-gate #include	<string.h>
407c478bd9Sstevel@tonic-gate #include	<errno.h>
417c478bd9Sstevel@tonic-gate #include	<stdlib.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  *	Internal form, to handle both errtext() and _errmsg()
457c478bd9Sstevel@tonic-gate  */
46*d6555420Smike_s /* PRINTFLIKE2 */
477c478bd9Sstevel@tonic-gate void
__errtext(int severity,char * format,va_list ap)487c478bd9Sstevel@tonic-gate __errtext(int severity, char *format, va_list ap)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	int	puterrno = 0;		/* true if an errno msg was printed */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	Err.severity = severity;
537c478bd9Sstevel@tonic-gate 	errverb(getenv("ERRVERB"));
547c478bd9Sstevel@tonic-gate 	errbefore(Err.severity, format, ap);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if (Err.severity == EIGNORE)
577c478bd9Sstevel@tonic-gate 		goto after;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (Err.vbell)
60*d6555420Smike_s 		(void) fputc('\07', stderr);
617c478bd9Sstevel@tonic-gate 	if (Err.vprefix && Err.prefix) {
62*d6555420Smike_s 		(void) fputs(Err.prefix, stderr);
63*d6555420Smike_s 		(void) fputc(' ', stderr);
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 	if (Err.vsource) {
667c478bd9Sstevel@tonic-gate 		if (Err.envsource ||
677c478bd9Sstevel@tonic-gate 			(Err.envsource = getenv("ERRSOURCE"))) {
68*d6555420Smike_s 			(void) fprintf(stderr, "%s: ", Err.envsource);
697c478bd9Sstevel@tonic-gate 		}
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 	if (Err.vsource && Err.source) {
72*d6555420Smike_s 		(void) fprintf(stderr, "%s: ", Err.source);
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	if (Err.vsevmsg) {
757c478bd9Sstevel@tonic-gate 		char	**e;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 		for (e = Err.sevmsg; *e; e++)
787c478bd9Sstevel@tonic-gate 			;
797c478bd9Sstevel@tonic-gate 		if (Err.severity < (e - Err.sevmsg))
80*d6555420Smike_s 			(void) fputs(Err.sevmsg[Err.severity], stderr);
817c478bd9Sstevel@tonic-gate 		else
82*d6555420Smike_s 			(void) fputs("<UNKNOWN>", stderr);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (Err.vtext) {
867c478bd9Sstevel@tonic-gate 		if (Err.vsyserr && ((int)format == EERRNO)) {
87*d6555420Smike_s 			(void) fflush(stderr);
887c478bd9Sstevel@tonic-gate 			perror("");
897c478bd9Sstevel@tonic-gate 			puterrno = 1;
907c478bd9Sstevel@tonic-gate 		} else {
91*d6555420Smike_s 			(void) vfprintf(stderr, format, ap);
92*d6555420Smike_s 			(void) fputs("\n", stderr);
937c478bd9Sstevel@tonic-gate 		}
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if ((errno && ((int)format != EERRNO)) &&
977c478bd9Sstevel@tonic-gate 	    (Err.vsyserr == EYES || (Err.vsyserr ==  EDEF &&
987c478bd9Sstevel@tonic-gate 	    (Err.severity == EHALT || Err.severity == EERROR)))) {
99*d6555420Smike_s 		(void) fputc('\t', stderr);
100*d6555420Smike_s 		(void) fflush(stderr);
1017c478bd9Sstevel@tonic-gate 		perror("");
1027c478bd9Sstevel@tonic-gate 		puterrno = 1;
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (Err.vtag) {
1067c478bd9Sstevel@tonic-gate 		if (Err.tagnum)
107*d6555420Smike_s 			(void) fputc('\t', stderr);
1087c478bd9Sstevel@tonic-gate 		else
109*d6555420Smike_s 			(void) fputs("HELP FACILITY KEY: ", stderr);
1107c478bd9Sstevel@tonic-gate 		if (Err.tagstr)
111*d6555420Smike_s 			(void) fputs(Err.tagstr, stderr);
1127c478bd9Sstevel@tonic-gate 		if (Err.tagnum)
113*d6555420Smike_s 			(void) fprintf(stderr, ", line %d", Err.tagnum);
1147c478bd9Sstevel@tonic-gate 		if (puterrno)
115*d6555420Smike_s 			(void) fprintf(stderr, "\tUXerrno%d", errno);
116*d6555420Smike_s 		(void) fputc('\n', stderr);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if ((Err.vtext || Err.vtag) &&
1207c478bd9Sstevel@tonic-gate 	    Err.vfix && Err.tofix && !Err.tagnum)
121*d6555420Smike_s 		(void) fprintf(stderr, "To Fix:\t%s\n", Err.tofix);
1227c478bd9Sstevel@tonic-gate 	after:
1237c478bd9Sstevel@tonic-gate 	erraction(errafter(Err.severity, format, ap));
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  *	external form, used by errmsg() macro, when tag is not permanently
1297c478bd9Sstevel@tonic-gate  *	assigned.
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate void
errtext(int severity,char * format,...)1327c478bd9Sstevel@tonic-gate errtext(int severity, char *format, ...)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	va_list ap;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1377c478bd9Sstevel@tonic-gate 	__errtext(severity, format, ap);
1387c478bd9Sstevel@tonic-gate 	va_end(ap);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  *	external form, used when tag is permanently assigned.
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate void
_errmsg(char * tag,int severity,char * format,...)1467c478bd9Sstevel@tonic-gate _errmsg(char *tag, int severity, char *format, ...)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	va_list ap;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	Err.tagstr = tag;
1517c478bd9Sstevel@tonic-gate 	Err.tagnum = 0;
1527c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1537c478bd9Sstevel@tonic-gate 	__errtext(severity, format, ap);
1547c478bd9Sstevel@tonic-gate 	va_end(ap);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate void
errverb(char * s)158*d6555420Smike_s errverb(char *s)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	char	buf[ BUFSIZ ];
1617c478bd9Sstevel@tonic-gate 	char   *token;
162*d6555420Smike_s 	static char   space[] = ",\t\n";
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (!s)
1657c478bd9Sstevel@tonic-gate 		return;
1667c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, s);
1677c478bd9Sstevel@tonic-gate 	for (token = errstrtok(buf, space);  token;
1687c478bd9Sstevel@tonic-gate 		token = errstrtok((char *)0, space)) {
1697c478bd9Sstevel@tonic-gate 		if (strcmp(token, "nochange") == 0) {
1707c478bd9Sstevel@tonic-gate 			Err.vbell   =  ENO;
1717c478bd9Sstevel@tonic-gate 			Err.vtext   =  EYES;
1727c478bd9Sstevel@tonic-gate 			Err.vsource =  EYES;
1737c478bd9Sstevel@tonic-gate 			Err.vsyserr =  EYES;
1747c478bd9Sstevel@tonic-gate 			Err.vtag    =  ENO;
1757c478bd9Sstevel@tonic-gate 			Err.vsevmsg =  ENO;
1767c478bd9Sstevel@tonic-gate 			Err.vfix	 =  ENO;
1777c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "silent") == 0) {
1787c478bd9Sstevel@tonic-gate 			Err.vbell   =  ENO;
1797c478bd9Sstevel@tonic-gate 			Err.vprefix =  ENO;
1807c478bd9Sstevel@tonic-gate 			Err.vtext   =  ENO;
1817c478bd9Sstevel@tonic-gate 			Err.vsource =  ENO;
1827c478bd9Sstevel@tonic-gate 			Err.vsyserr =  ENO;
1837c478bd9Sstevel@tonic-gate 			Err.vtag    =  ENO;
1847c478bd9Sstevel@tonic-gate 			Err.vsevmsg =  ENO;
1857c478bd9Sstevel@tonic-gate 			Err.vfix	 =  ENO;
1867c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "verbose") == 0) {
1877c478bd9Sstevel@tonic-gate 			Err.vbell   =  EYES;
1887c478bd9Sstevel@tonic-gate 			Err.vprefix =  EYES;
1897c478bd9Sstevel@tonic-gate 			Err.vtext   =  EYES;
1907c478bd9Sstevel@tonic-gate 			Err.vsource =  EYES;
1917c478bd9Sstevel@tonic-gate 			Err.vsyserr =  EYES;
1927c478bd9Sstevel@tonic-gate 			Err.vtag    =  EYES;
1937c478bd9Sstevel@tonic-gate 			Err.vsevmsg =  EYES;
1947c478bd9Sstevel@tonic-gate 			Err.vfix	 =  EYES;
1957c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "expert") == 0) {
1967c478bd9Sstevel@tonic-gate 			Err.vbell   =  ENO;
1977c478bd9Sstevel@tonic-gate 			Err.vprefix =  ENO;
1987c478bd9Sstevel@tonic-gate 			Err.vtext   =  EYES;
1997c478bd9Sstevel@tonic-gate 			Err.vsource =  EYES;
2007c478bd9Sstevel@tonic-gate 			Err.vsyserr =  EYES;
2017c478bd9Sstevel@tonic-gate 			Err.vtag    =  ENO;
2027c478bd9Sstevel@tonic-gate 			Err.vsevmsg =  EYES;
2037c478bd9Sstevel@tonic-gate 			Err.vfix	 =  ENO;
2047c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "bell") == 0) {
2057c478bd9Sstevel@tonic-gate 			Err.vbell = EYES;
2067c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "nobell") == 0) {
2077c478bd9Sstevel@tonic-gate 			Err.vbell = ENO;
2087c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "tag") == 0) {
2097c478bd9Sstevel@tonic-gate 			Err.vtag = EYES;
2107c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "notag") == 0) {
2117c478bd9Sstevel@tonic-gate 			Err.vtag = ENO;
2127c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "text") == 0) {
2137c478bd9Sstevel@tonic-gate 			Err.vtext = EYES;
2147c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "notext") == 0) {
2157c478bd9Sstevel@tonic-gate 			Err.vtext = ENO;
2167c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "tofix") == 0) {
2177c478bd9Sstevel@tonic-gate 			Err.vfix = EYES;
2187c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "notofix") == 0) {
2197c478bd9Sstevel@tonic-gate 			Err.vfix = ENO;
2207c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "syserr") == 0) {
2217c478bd9Sstevel@tonic-gate 			Err.vsyserr = EYES;
2227c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "nosyserr") == 0) {
2237c478bd9Sstevel@tonic-gate 			Err.vsyserr = ENO;
2247c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "defsyserr") == 0) {
2257c478bd9Sstevel@tonic-gate 			Err.vsyserr = EDEF;
2267c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "source")) {
2277c478bd9Sstevel@tonic-gate 			Err.vsource = EYES;
2287c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "nosource")  == 0) {
2297c478bd9Sstevel@tonic-gate 			Err.vsource = ENO;
2307c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "sevmsg") == 0) {
2317c478bd9Sstevel@tonic-gate 			Err.vsevmsg = EYES;
2327c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "nosevmsg") == 0) {
2337c478bd9Sstevel@tonic-gate 			Err.vsevmsg = ENO;
2347c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "prefix") == 0) {
2357c478bd9Sstevel@tonic-gate 			Err.vprefix = EYES;
2367c478bd9Sstevel@tonic-gate 		} else if (strcmp(token, "noprefix") == 0) {
2377c478bd9Sstevel@tonic-gate 			Err.vprefix = ENO;
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate }
241