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 * Routines to print and adjust options on
33 * error messages.
34 */
35
36 #include "errmsg.h"
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <stdlib.h>
42
43 /*
44 * Internal form, to handle both errtext() and _errmsg()
45 */
46 /* PRINTFLIKE2 */
47 void
__errtext(int severity,char * format,va_list ap)48 __errtext(int severity, char *format, va_list ap)
49 {
50 int puterrno = 0; /* true if an errno msg was printed */
51
52 Err.severity = severity;
53 errverb(getenv("ERRVERB"));
54 errbefore(Err.severity, format, ap);
55
56 if (Err.severity == EIGNORE)
57 goto after;
58
59 if (Err.vbell)
60 (void) fputc('\07', stderr);
61 if (Err.vprefix && Err.prefix) {
62 (void) fputs(Err.prefix, stderr);
63 (void) fputc(' ', stderr);
64 }
65 if (Err.vsource) {
66 if (Err.envsource ||
67 (Err.envsource = getenv("ERRSOURCE"))) {
68 (void) fprintf(stderr, "%s: ", Err.envsource);
69 }
70 }
71 if (Err.vsource && Err.source) {
72 (void) fprintf(stderr, "%s: ", Err.source);
73 }
74 if (Err.vsevmsg) {
75 char **e;
76
77 for (e = Err.sevmsg; *e; e++)
78 ;
79 if (Err.severity < (e - Err.sevmsg))
80 (void) fputs(Err.sevmsg[Err.severity], stderr);
81 else
82 (void) fputs("<UNKNOWN>", stderr);
83 }
84
85 if (Err.vtext) {
86 if (Err.vsyserr && ((int)format == EERRNO)) {
87 (void) fflush(stderr);
88 perror("");
89 puterrno = 1;
90 } else {
91 (void) vfprintf(stderr, format, ap);
92 (void) fputs("\n", stderr);
93 }
94 }
95
96 if ((errno && ((int)format != EERRNO)) &&
97 (Err.vsyserr == EYES || (Err.vsyserr == EDEF &&
98 (Err.severity == EHALT || Err.severity == EERROR)))) {
99 (void) fputc('\t', stderr);
100 (void) fflush(stderr);
101 perror("");
102 puterrno = 1;
103 }
104
105 if (Err.vtag) {
106 if (Err.tagnum)
107 (void) fputc('\t', stderr);
108 else
109 (void) fputs("HELP FACILITY KEY: ", stderr);
110 if (Err.tagstr)
111 (void) fputs(Err.tagstr, stderr);
112 if (Err.tagnum)
113 (void) fprintf(stderr, ", line %d", Err.tagnum);
114 if (puterrno)
115 (void) fprintf(stderr, "\tUXerrno%d", errno);
116 (void) fputc('\n', stderr);
117 }
118
119 if ((Err.vtext || Err.vtag) &&
120 Err.vfix && Err.tofix && !Err.tagnum)
121 (void) fprintf(stderr, "To Fix:\t%s\n", Err.tofix);
122 after:
123 erraction(errafter(Err.severity, format, ap));
124 }
125
126
127 /*
128 * external form, used by errmsg() macro, when tag is not permanently
129 * assigned.
130 */
131 void
errtext(int severity,char * format,...)132 errtext(int severity, char *format, ...)
133 {
134 va_list ap;
135
136 va_start(ap, format);
137 __errtext(severity, format, ap);
138 va_end(ap);
139 }
140
141
142 /*
143 * external form, used when tag is permanently assigned.
144 */
145 void
_errmsg(char * tag,int severity,char * format,...)146 _errmsg(char *tag, int severity, char *format, ...)
147 {
148 va_list ap;
149
150 Err.tagstr = tag;
151 Err.tagnum = 0;
152 va_start(ap, format);
153 __errtext(severity, format, ap);
154 va_end(ap);
155 }
156
157 void
errverb(char * s)158 errverb(char *s)
159 {
160 char buf[ BUFSIZ ];
161 char *token;
162 static char space[] = ",\t\n";
163
164 if (!s)
165 return;
166 (void) strcpy(buf, s);
167 for (token = errstrtok(buf, space); token;
168 token = errstrtok((char *)0, space)) {
169 if (strcmp(token, "nochange") == 0) {
170 Err.vbell = ENO;
171 Err.vtext = EYES;
172 Err.vsource = EYES;
173 Err.vsyserr = EYES;
174 Err.vtag = ENO;
175 Err.vsevmsg = ENO;
176 Err.vfix = ENO;
177 } else if (strcmp(token, "silent") == 0) {
178 Err.vbell = ENO;
179 Err.vprefix = ENO;
180 Err.vtext = ENO;
181 Err.vsource = ENO;
182 Err.vsyserr = ENO;
183 Err.vtag = ENO;
184 Err.vsevmsg = ENO;
185 Err.vfix = ENO;
186 } else if (strcmp(token, "verbose") == 0) {
187 Err.vbell = EYES;
188 Err.vprefix = EYES;
189 Err.vtext = EYES;
190 Err.vsource = EYES;
191 Err.vsyserr = EYES;
192 Err.vtag = EYES;
193 Err.vsevmsg = EYES;
194 Err.vfix = EYES;
195 } else if (strcmp(token, "expert") == 0) {
196 Err.vbell = ENO;
197 Err.vprefix = ENO;
198 Err.vtext = EYES;
199 Err.vsource = EYES;
200 Err.vsyserr = EYES;
201 Err.vtag = ENO;
202 Err.vsevmsg = EYES;
203 Err.vfix = ENO;
204 } else if (strcmp(token, "bell") == 0) {
205 Err.vbell = EYES;
206 } else if (strcmp(token, "nobell") == 0) {
207 Err.vbell = ENO;
208 } else if (strcmp(token, "tag") == 0) {
209 Err.vtag = EYES;
210 } else if (strcmp(token, "notag") == 0) {
211 Err.vtag = ENO;
212 } else if (strcmp(token, "text") == 0) {
213 Err.vtext = EYES;
214 } else if (strcmp(token, "notext") == 0) {
215 Err.vtext = ENO;
216 } else if (strcmp(token, "tofix") == 0) {
217 Err.vfix = EYES;
218 } else if (strcmp(token, "notofix") == 0) {
219 Err.vfix = ENO;
220 } else if (strcmp(token, "syserr") == 0) {
221 Err.vsyserr = EYES;
222 } else if (strcmp(token, "nosyserr") == 0) {
223 Err.vsyserr = ENO;
224 } else if (strcmp(token, "defsyserr") == 0) {
225 Err.vsyserr = EDEF;
226 } else if (strcmp(token, "source")) {
227 Err.vsource = EYES;
228 } else if (strcmp(token, "nosource") == 0) {
229 Err.vsource = ENO;
230 } else if (strcmp(token, "sevmsg") == 0) {
231 Err.vsevmsg = EYES;
232 } else if (strcmp(token, "nosevmsg") == 0) {
233 Err.vsevmsg = ENO;
234 } else if (strcmp(token, "prefix") == 0) {
235 Err.vprefix = EYES;
236 } else if (strcmp(token, "noprefix") == 0) {
237 Err.vprefix = ENO;
238 }
239 }
240 }
241