xref: /illumos-gate/usr/src/cmd/sgs/tsort/common/errtext.c (revision c65ebfc7045424bd04a6c7719a27b0ad3399ad54)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1988 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 /*
35  *	Routines to print and adjust options on
36  *	error messages.
37  */
38 
39 #include	"errmsg.h"
40 #include	<stdio.h>
41 #include	<stdarg.h>
42 #include	<string.h>
43 #include	<errno.h>
44 #include	<stdlib.h>
45 
46 /*
47  *	Internal form, to handle both errtext() and _errmsg()
48  */
49 /* PRINTFLIKE2 */
50 void
51 __errtext(int severity, char *format, va_list ap)
52 {
53 	int	puterrno = 0;		/* true if an errno msg was printed */
54 
55 	Err.severity = severity;
56 	errverb(getenv("ERRVERB"));
57 	errbefore(Err.severity, format, ap);
58 
59 	if (Err.severity == EIGNORE)
60 		goto after;
61 
62 	if (Err.vbell)
63 		(void) fputc('\07', stderr);
64 	if (Err.vprefix && Err.prefix) {
65 		(void) fputs(Err.prefix, stderr);
66 		(void) fputc(' ', stderr);
67 	}
68 	if (Err.vsource) {
69 		if (Err.envsource ||
70 			(Err.envsource = getenv("ERRSOURCE"))) {
71 			(void) fprintf(stderr, "%s: ", Err.envsource);
72 		}
73 	}
74 	if (Err.vsource && Err.source) {
75 		(void) fprintf(stderr, "%s: ", Err.source);
76 	}
77 	if (Err.vsevmsg) {
78 		char	**e;
79 
80 		for (e = Err.sevmsg; *e; e++)
81 			;
82 		if (Err.severity < (e - Err.sevmsg))
83 			(void) fputs(Err.sevmsg[Err.severity], stderr);
84 		else
85 			(void) fputs("<UNKNOWN>", stderr);
86 	}
87 
88 	if (Err.vtext) {
89 		if (Err.vsyserr && ((int)format == EERRNO)) {
90 			(void) fflush(stderr);
91 			perror("");
92 			puterrno = 1;
93 		} else {
94 			(void) vfprintf(stderr, format, ap);
95 			(void) fputs("\n", stderr);
96 		}
97 	}
98 
99 	if ((errno && ((int)format != EERRNO)) &&
100 	    (Err.vsyserr == EYES || (Err.vsyserr ==  EDEF &&
101 	    (Err.severity == EHALT || Err.severity == EERROR)))) {
102 		(void) fputc('\t', stderr);
103 		(void) fflush(stderr);
104 		perror("");
105 		puterrno = 1;
106 	}
107 
108 	if (Err.vtag) {
109 		if (Err.tagnum)
110 			(void) fputc('\t', stderr);
111 		else
112 			(void) fputs("HELP FACILITY KEY: ", stderr);
113 		if (Err.tagstr)
114 			(void) fputs(Err.tagstr, stderr);
115 		if (Err.tagnum)
116 			(void) fprintf(stderr, ", line %d", Err.tagnum);
117 		if (puterrno)
118 			(void) fprintf(stderr, "\tUXerrno%d", errno);
119 		(void) fputc('\n', stderr);
120 	}
121 
122 	if ((Err.vtext || Err.vtag) &&
123 	    Err.vfix && Err.tofix && !Err.tagnum)
124 		(void) fprintf(stderr, "To Fix:\t%s\n", Err.tofix);
125 	after:
126 	erraction(errafter(Err.severity, format, ap));
127 }
128 
129 
130 /*
131  *	external form, used by errmsg() macro, when tag is not permanently
132  *	assigned.
133  */
134 void
135 errtext(int severity, char *format, ...)
136 {
137 	va_list ap;
138 
139 	va_start(ap, format);
140 	__errtext(severity, format, ap);
141 	va_end(ap);
142 }
143 
144 
145 /*
146  *	external form, used when tag is permanently assigned.
147  */
148 void
149 _errmsg(char *tag, int severity, char *format, ...)
150 {
151 	va_list ap;
152 
153 	Err.tagstr = tag;
154 	Err.tagnum = 0;
155 	va_start(ap, format);
156 	__errtext(severity, format, ap);
157 	va_end(ap);
158 }
159 
160 void
161 errverb(char *s)
162 {
163 	char	buf[ BUFSIZ ];
164 	char   *token;
165 	static char   space[] = ",\t\n";
166 
167 	if (!s)
168 		return;
169 	(void) strcpy(buf, s);
170 	for (token = errstrtok(buf, space);  token;
171 		token = errstrtok((char *)0, space)) {
172 		if (strcmp(token, "nochange") == 0) {
173 			Err.vbell   =  ENO;
174 			Err.vtext   =  EYES;
175 			Err.vsource =  EYES;
176 			Err.vsyserr =  EYES;
177 			Err.vtag    =  ENO;
178 			Err.vsevmsg =  ENO;
179 			Err.vfix	 =  ENO;
180 		} else if (strcmp(token, "silent") == 0) {
181 			Err.vbell   =  ENO;
182 			Err.vprefix =  ENO;
183 			Err.vtext   =  ENO;
184 			Err.vsource =  ENO;
185 			Err.vsyserr =  ENO;
186 			Err.vtag    =  ENO;
187 			Err.vsevmsg =  ENO;
188 			Err.vfix	 =  ENO;
189 		} else if (strcmp(token, "verbose") == 0) {
190 			Err.vbell   =  EYES;
191 			Err.vprefix =  EYES;
192 			Err.vtext   =  EYES;
193 			Err.vsource =  EYES;
194 			Err.vsyserr =  EYES;
195 			Err.vtag    =  EYES;
196 			Err.vsevmsg =  EYES;
197 			Err.vfix	 =  EYES;
198 		} else if (strcmp(token, "expert") == 0) {
199 			Err.vbell   =  ENO;
200 			Err.vprefix =  ENO;
201 			Err.vtext   =  EYES;
202 			Err.vsource =  EYES;
203 			Err.vsyserr =  EYES;
204 			Err.vtag    =  ENO;
205 			Err.vsevmsg =  EYES;
206 			Err.vfix	 =  ENO;
207 		} else if (strcmp(token, "bell") == 0) {
208 			Err.vbell = EYES;
209 		} else if (strcmp(token, "nobell") == 0) {
210 			Err.vbell = ENO;
211 		} else if (strcmp(token, "tag") == 0) {
212 			Err.vtag = EYES;
213 		} else if (strcmp(token, "notag") == 0) {
214 			Err.vtag = ENO;
215 		} else if (strcmp(token, "text") == 0) {
216 			Err.vtext = EYES;
217 		} else if (strcmp(token, "notext") == 0) {
218 			Err.vtext = ENO;
219 		} else if (strcmp(token, "tofix") == 0) {
220 			Err.vfix = EYES;
221 		} else if (strcmp(token, "notofix") == 0) {
222 			Err.vfix = ENO;
223 		} else if (strcmp(token, "syserr") == 0) {
224 			Err.vsyserr = EYES;
225 		} else if (strcmp(token, "nosyserr") == 0) {
226 			Err.vsyserr = ENO;
227 		} else if (strcmp(token, "defsyserr") == 0) {
228 			Err.vsyserr = EDEF;
229 		} else if (strcmp(token, "source")) {
230 			Err.vsource = EYES;
231 		} else if (strcmp(token, "nosource")  == 0) {
232 			Err.vsource = ENO;
233 		} else if (strcmp(token, "sevmsg") == 0) {
234 			Err.vsevmsg = EYES;
235 		} else if (strcmp(token, "nosevmsg") == 0) {
236 			Err.vsevmsg = ENO;
237 		} else if (strcmp(token, "prefix") == 0) {
238 			Err.vprefix = EYES;
239 		} else if (strcmp(token, "noprefix") == 0) {
240 			Err.vprefix = ENO;
241 		}
242 	}
243 }
244