10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22*372Smike_s
230Sstevel@tonic-gate /*
24*372Smike_s * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
250Sstevel@tonic-gate * Use is subject to license terms.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
290Sstevel@tonic-gate /* All Rights Reserved */
300Sstevel@tonic-gate
310Sstevel@tonic-gate
320Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
330Sstevel@tonic-gate
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate * Routines to print and adjust options on
360Sstevel@tonic-gate * error messages.
370Sstevel@tonic-gate */
380Sstevel@tonic-gate
390Sstevel@tonic-gate #include "errmsg.h"
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <stdarg.h>
420Sstevel@tonic-gate #include <string.h>
430Sstevel@tonic-gate #include <errno.h>
440Sstevel@tonic-gate #include <stdlib.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate * Internal form, to handle both errtext() and _errmsg()
480Sstevel@tonic-gate */
49*372Smike_s /* PRINTFLIKE2 */
500Sstevel@tonic-gate void
__errtext(int severity,char * format,va_list ap)510Sstevel@tonic-gate __errtext(int severity, char *format, va_list ap)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate int puterrno = 0; /* true if an errno msg was printed */
540Sstevel@tonic-gate
550Sstevel@tonic-gate Err.severity = severity;
560Sstevel@tonic-gate errverb(getenv("ERRVERB"));
570Sstevel@tonic-gate errbefore(Err.severity, format, ap);
580Sstevel@tonic-gate
590Sstevel@tonic-gate if (Err.severity == EIGNORE)
600Sstevel@tonic-gate goto after;
610Sstevel@tonic-gate
620Sstevel@tonic-gate if (Err.vbell)
63*372Smike_s (void) fputc('\07', stderr);
640Sstevel@tonic-gate if (Err.vprefix && Err.prefix) {
65*372Smike_s (void) fputs(Err.prefix, stderr);
66*372Smike_s (void) fputc(' ', stderr);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate if (Err.vsource) {
690Sstevel@tonic-gate if (Err.envsource ||
700Sstevel@tonic-gate (Err.envsource = getenv("ERRSOURCE"))) {
71*372Smike_s (void) fprintf(stderr, "%s: ", Err.envsource);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate }
740Sstevel@tonic-gate if (Err.vsource && Err.source) {
75*372Smike_s (void) fprintf(stderr, "%s: ", Err.source);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate if (Err.vsevmsg) {
780Sstevel@tonic-gate char **e;
790Sstevel@tonic-gate
800Sstevel@tonic-gate for (e = Err.sevmsg; *e; e++)
810Sstevel@tonic-gate ;
820Sstevel@tonic-gate if (Err.severity < (e - Err.sevmsg))
83*372Smike_s (void) fputs(Err.sevmsg[Err.severity], stderr);
840Sstevel@tonic-gate else
85*372Smike_s (void) fputs("<UNKNOWN>", stderr);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate
880Sstevel@tonic-gate if (Err.vtext) {
890Sstevel@tonic-gate if (Err.vsyserr && ((int)format == EERRNO)) {
90*372Smike_s (void) fflush(stderr);
910Sstevel@tonic-gate perror("");
920Sstevel@tonic-gate puterrno = 1;
930Sstevel@tonic-gate } else {
94*372Smike_s (void) vfprintf(stderr, format, ap);
95*372Smike_s (void) fputs("\n", stderr);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate }
980Sstevel@tonic-gate
990Sstevel@tonic-gate if ((errno && ((int)format != EERRNO)) &&
1000Sstevel@tonic-gate (Err.vsyserr == EYES || (Err.vsyserr == EDEF &&
1010Sstevel@tonic-gate (Err.severity == EHALT || Err.severity == EERROR)))) {
102*372Smike_s (void) fputc('\t', stderr);
103*372Smike_s (void) fflush(stderr);
1040Sstevel@tonic-gate perror("");
1050Sstevel@tonic-gate puterrno = 1;
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate if (Err.vtag) {
1090Sstevel@tonic-gate if (Err.tagnum)
110*372Smike_s (void) fputc('\t', stderr);
1110Sstevel@tonic-gate else
112*372Smike_s (void) fputs("HELP FACILITY KEY: ", stderr);
1130Sstevel@tonic-gate if (Err.tagstr)
114*372Smike_s (void) fputs(Err.tagstr, stderr);
1150Sstevel@tonic-gate if (Err.tagnum)
116*372Smike_s (void) fprintf(stderr, ", line %d", Err.tagnum);
1170Sstevel@tonic-gate if (puterrno)
118*372Smike_s (void) fprintf(stderr, "\tUXerrno%d", errno);
119*372Smike_s (void) fputc('\n', stderr);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate if ((Err.vtext || Err.vtag) &&
1230Sstevel@tonic-gate Err.vfix && Err.tofix && !Err.tagnum)
124*372Smike_s (void) fprintf(stderr, "To Fix:\t%s\n", Err.tofix);
1250Sstevel@tonic-gate after:
1260Sstevel@tonic-gate erraction(errafter(Err.severity, format, ap));
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate * external form, used by errmsg() macro, when tag is not permanently
1320Sstevel@tonic-gate * assigned.
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate void
errtext(int severity,char * format,...)1350Sstevel@tonic-gate errtext(int severity, char *format, ...)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate va_list ap;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate va_start(ap, format);
1400Sstevel@tonic-gate __errtext(severity, format, ap);
1410Sstevel@tonic-gate va_end(ap);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate * external form, used when tag is permanently assigned.
1470Sstevel@tonic-gate */
1480Sstevel@tonic-gate void
_errmsg(char * tag,int severity,char * format,...)1490Sstevel@tonic-gate _errmsg(char *tag, int severity, char *format, ...)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate va_list ap;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate Err.tagstr = tag;
1540Sstevel@tonic-gate Err.tagnum = 0;
1550Sstevel@tonic-gate va_start(ap, format);
1560Sstevel@tonic-gate __errtext(severity, format, ap);
1570Sstevel@tonic-gate va_end(ap);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate void
errverb(char * s)161*372Smike_s errverb(char *s)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate char buf[ BUFSIZ ];
1640Sstevel@tonic-gate char *token;
165*372Smike_s static char space[] = ",\t\n";
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate if (!s)
1680Sstevel@tonic-gate return;
1690Sstevel@tonic-gate (void) strcpy(buf, s);
1700Sstevel@tonic-gate for (token = errstrtok(buf, space); token;
1710Sstevel@tonic-gate token = errstrtok((char *)0, space)) {
1720Sstevel@tonic-gate if (strcmp(token, "nochange") == 0) {
1730Sstevel@tonic-gate Err.vbell = ENO;
1740Sstevel@tonic-gate Err.vtext = EYES;
1750Sstevel@tonic-gate Err.vsource = EYES;
1760Sstevel@tonic-gate Err.vsyserr = EYES;
1770Sstevel@tonic-gate Err.vtag = ENO;
1780Sstevel@tonic-gate Err.vsevmsg = ENO;
1790Sstevel@tonic-gate Err.vfix = ENO;
1800Sstevel@tonic-gate } else if (strcmp(token, "silent") == 0) {
1810Sstevel@tonic-gate Err.vbell = ENO;
1820Sstevel@tonic-gate Err.vprefix = ENO;
1830Sstevel@tonic-gate Err.vtext = ENO;
1840Sstevel@tonic-gate Err.vsource = ENO;
1850Sstevel@tonic-gate Err.vsyserr = ENO;
1860Sstevel@tonic-gate Err.vtag = ENO;
1870Sstevel@tonic-gate Err.vsevmsg = ENO;
1880Sstevel@tonic-gate Err.vfix = ENO;
1890Sstevel@tonic-gate } else if (strcmp(token, "verbose") == 0) {
1900Sstevel@tonic-gate Err.vbell = EYES;
1910Sstevel@tonic-gate Err.vprefix = EYES;
1920Sstevel@tonic-gate Err.vtext = EYES;
1930Sstevel@tonic-gate Err.vsource = EYES;
1940Sstevel@tonic-gate Err.vsyserr = EYES;
1950Sstevel@tonic-gate Err.vtag = EYES;
1960Sstevel@tonic-gate Err.vsevmsg = EYES;
1970Sstevel@tonic-gate Err.vfix = EYES;
1980Sstevel@tonic-gate } else if (strcmp(token, "expert") == 0) {
1990Sstevel@tonic-gate Err.vbell = ENO;
2000Sstevel@tonic-gate Err.vprefix = ENO;
2010Sstevel@tonic-gate Err.vtext = EYES;
2020Sstevel@tonic-gate Err.vsource = EYES;
2030Sstevel@tonic-gate Err.vsyserr = EYES;
2040Sstevel@tonic-gate Err.vtag = ENO;
2050Sstevel@tonic-gate Err.vsevmsg = EYES;
2060Sstevel@tonic-gate Err.vfix = ENO;
2070Sstevel@tonic-gate } else if (strcmp(token, "bell") == 0) {
2080Sstevel@tonic-gate Err.vbell = EYES;
2090Sstevel@tonic-gate } else if (strcmp(token, "nobell") == 0) {
2100Sstevel@tonic-gate Err.vbell = ENO;
2110Sstevel@tonic-gate } else if (strcmp(token, "tag") == 0) {
2120Sstevel@tonic-gate Err.vtag = EYES;
2130Sstevel@tonic-gate } else if (strcmp(token, "notag") == 0) {
2140Sstevel@tonic-gate Err.vtag = ENO;
2150Sstevel@tonic-gate } else if (strcmp(token, "text") == 0) {
2160Sstevel@tonic-gate Err.vtext = EYES;
2170Sstevel@tonic-gate } else if (strcmp(token, "notext") == 0) {
2180Sstevel@tonic-gate Err.vtext = ENO;
2190Sstevel@tonic-gate } else if (strcmp(token, "tofix") == 0) {
2200Sstevel@tonic-gate Err.vfix = EYES;
2210Sstevel@tonic-gate } else if (strcmp(token, "notofix") == 0) {
2220Sstevel@tonic-gate Err.vfix = ENO;
2230Sstevel@tonic-gate } else if (strcmp(token, "syserr") == 0) {
2240Sstevel@tonic-gate Err.vsyserr = EYES;
2250Sstevel@tonic-gate } else if (strcmp(token, "nosyserr") == 0) {
2260Sstevel@tonic-gate Err.vsyserr = ENO;
2270Sstevel@tonic-gate } else if (strcmp(token, "defsyserr") == 0) {
2280Sstevel@tonic-gate Err.vsyserr = EDEF;
2290Sstevel@tonic-gate } else if (strcmp(token, "source")) {
2300Sstevel@tonic-gate Err.vsource = EYES;
2310Sstevel@tonic-gate } else if (strcmp(token, "nosource") == 0) {
2320Sstevel@tonic-gate Err.vsource = ENO;
2330Sstevel@tonic-gate } else if (strcmp(token, "sevmsg") == 0) {
2340Sstevel@tonic-gate Err.vsevmsg = EYES;
2350Sstevel@tonic-gate } else if (strcmp(token, "nosevmsg") == 0) {
2360Sstevel@tonic-gate Err.vsevmsg = ENO;
2370Sstevel@tonic-gate } else if (strcmp(token, "prefix") == 0) {
2380Sstevel@tonic-gate Err.vprefix = EYES;
2390Sstevel@tonic-gate } else if (strcmp(token, "noprefix") == 0) {
2400Sstevel@tonic-gate Err.vprefix = ENO;
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate }
244