xref: /illumos-gate/usr/src/cmd/fm/eversholt/common/out.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * out.c -- some basic output routines
27*7c478bd9Sstevel@tonic-gate  *
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h>
32*7c478bd9Sstevel@tonic-gate #include <unistd.h>
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
35*7c478bd9Sstevel@tonic-gate #include <string.h>
36*7c478bd9Sstevel@tonic-gate #include <strings.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate #include <time.h>
39*7c478bd9Sstevel@tonic-gate #include "out.h"
40*7c478bd9Sstevel@tonic-gate #include "stats.h"
41*7c478bd9Sstevel@tonic-gate #include "io.h"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /* stats we keep for "out" module */
44*7c478bd9Sstevel@tonic-gate static struct stats *Outcount;
45*7c478bd9Sstevel@tonic-gate static struct stats *Errcount;
46*7c478bd9Sstevel@tonic-gate static struct stats *Warncount;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static int Exitcode;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static const char *Myname;
51*7c478bd9Sstevel@tonic-gate static FILE *Altfp;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate /* buffer used to format all prints */
54*7c478bd9Sstevel@tonic-gate #define	MAXOUT 8192
55*7c478bd9Sstevel@tonic-gate static char Outbuf[MAXOUT];
56*7c478bd9Sstevel@tonic-gate static int Outidx;		/* next unused char in Outbuf[] */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * out_init -- initialize this module
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate void
out_init(const char * myname)62*7c478bd9Sstevel@tonic-gate out_init(const char *myname)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	Outcount = stats_new_counter("output.calls", "total calls", 1);
65*7c478bd9Sstevel@tonic-gate 	Errcount = stats_new_counter("output.errors", "total errors", 0);
66*7c478bd9Sstevel@tonic-gate 	Warncount = stats_new_counter("output.warnings", "total warnings", 0);
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	if (myname == NULL)
69*7c478bd9Sstevel@tonic-gate 		return;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	if ((Myname = strrchr(myname, '/')) == NULL &&
72*7c478bd9Sstevel@tonic-gate 	    (Myname = strrchr(myname, '\\')) == NULL)
73*7c478bd9Sstevel@tonic-gate 		Myname = myname;
74*7c478bd9Sstevel@tonic-gate 	else
75*7c478bd9Sstevel@tonic-gate 		Myname++;
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate void
out_fini(void)79*7c478bd9Sstevel@tonic-gate out_fini(void)
80*7c478bd9Sstevel@tonic-gate {
81*7c478bd9Sstevel@tonic-gate 	stats_delete(Outcount);
82*7c478bd9Sstevel@tonic-gate 	Outcount = NULL;
83*7c478bd9Sstevel@tonic-gate 	stats_delete(Errcount);
84*7c478bd9Sstevel@tonic-gate 	Errcount = NULL;
85*7c478bd9Sstevel@tonic-gate 	stats_delete(Warncount);
86*7c478bd9Sstevel@tonic-gate 	Warncount = NULL;
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate /*
90*7c478bd9Sstevel@tonic-gate  * out_altfp -- store an alternate fp for O_ALTFP
91*7c478bd9Sstevel@tonic-gate  */
92*7c478bd9Sstevel@tonic-gate void
out_altfp(FILE * fp)93*7c478bd9Sstevel@tonic-gate out_altfp(FILE *fp)
94*7c478bd9Sstevel@tonic-gate {
95*7c478bd9Sstevel@tonic-gate 	Altfp = fp;
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate /*
99*7c478bd9Sstevel@tonic-gate  * voutbufprintf -- like vprintf, but appends to Outbuf
100*7c478bd9Sstevel@tonic-gate  */
101*7c478bd9Sstevel@tonic-gate static void
voutbufprintf(const char * fmt,va_list ap)102*7c478bd9Sstevel@tonic-gate voutbufprintf(const char *fmt, va_list ap)
103*7c478bd9Sstevel@tonic-gate {
104*7c478bd9Sstevel@tonic-gate 	int len = vsnprintf(&Outbuf[Outidx], MAXOUT - Outidx, fmt, ap);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	Outidx += len;
107*7c478bd9Sstevel@tonic-gate 	if (Outidx >= MAXOUT)
108*7c478bd9Sstevel@tonic-gate 		Outidx = MAXOUT - 1;
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /*
112*7c478bd9Sstevel@tonic-gate  * outbufprintf -- like printf, but appends to Outbuf
113*7c478bd9Sstevel@tonic-gate  */
114*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
115*7c478bd9Sstevel@tonic-gate static void
outbufprintf(const char * fmt,...)116*7c478bd9Sstevel@tonic-gate outbufprintf(const char *fmt, ...)
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	va_list ap;
119*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
120*7c478bd9Sstevel@tonic-gate 	voutbufprintf(fmt, ap);
121*7c478bd9Sstevel@tonic-gate 	va_end(ap);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate /*
125*7c478bd9Sstevel@tonic-gate  * vout -- va_list version of out()
126*7c478bd9Sstevel@tonic-gate  *
127*7c478bd9Sstevel@tonic-gate  * all the output processing work is done here.
128*7c478bd9Sstevel@tonic-gate  */
129*7c478bd9Sstevel@tonic-gate static void
vout(int flags,const char * fmt,va_list ap)130*7c478bd9Sstevel@tonic-gate vout(int flags, const char *fmt, va_list ap)
131*7c478bd9Sstevel@tonic-gate {
132*7c478bd9Sstevel@tonic-gate 	int safe_errno = errno;
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	stats_counter_bump(Outcount);
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	/*
137*7c478bd9Sstevel@tonic-gate 	 * just return if called with a disabled output type.  this
138*7c478bd9Sstevel@tonic-gate 	 * prevents debug prints when Debug is off, verbose prints
139*7c478bd9Sstevel@tonic-gate 	 * when Verbose is off, and language warnings when warnings
140*7c478bd9Sstevel@tonic-gate 	 * are quenched (which they are when we're loading a .eft file).
141*7c478bd9Sstevel@tonic-gate 	 *
142*7c478bd9Sstevel@tonic-gate 	 */
143*7c478bd9Sstevel@tonic-gate 	if ((flags & O_DEBUG) && Debug == 0)
144*7c478bd9Sstevel@tonic-gate 		return;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if ((flags & O_VERB) && Verbose == 0)
147*7c478bd9Sstevel@tonic-gate 		return;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if ((flags & O_VERB2) && Verbose < 2)
150*7c478bd9Sstevel@tonic-gate 		return;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	if ((flags & O_VERB3) && Verbose < 3)
153*7c478bd9Sstevel@tonic-gate 		return;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	if ((flags & O_WARN) && Warn == 0)
156*7c478bd9Sstevel@tonic-gate 		return;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	if ((flags & O_ALTFP) && Altfp == NULL)
159*7c478bd9Sstevel@tonic-gate 		return;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	/* some things only happen at the beginning of a print */
162*7c478bd9Sstevel@tonic-gate 	if (Outidx == 0) {
163*7c478bd9Sstevel@tonic-gate 		if (flags & O_USAGE) {
164*7c478bd9Sstevel@tonic-gate 			Exitcode++;
165*7c478bd9Sstevel@tonic-gate 			outbufprintf("usage: %s ", Myname);
166*7c478bd9Sstevel@tonic-gate 		} else {
167*7c478bd9Sstevel@tonic-gate 			if (Myname && flags & (O_DIE|O_ERR|O_WARN|O_PROG))
168*7c478bd9Sstevel@tonic-gate 				outbufprintf("%s: ", Myname);
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 			if (flags & O_DIE) {
171*7c478bd9Sstevel@tonic-gate 				Exitcode++;
172*7c478bd9Sstevel@tonic-gate 				outbufprintf("fatal error: ");
173*7c478bd9Sstevel@tonic-gate 			} else if (flags & O_ERR) {
174*7c478bd9Sstevel@tonic-gate 				Exitcode++;
175*7c478bd9Sstevel@tonic-gate 				stats_counter_bump(Errcount);
176*7c478bd9Sstevel@tonic-gate 				outbufprintf("error: ");
177*7c478bd9Sstevel@tonic-gate 			} else if (flags & O_WARN) {
178*7c478bd9Sstevel@tonic-gate 				stats_counter_bump(Warncount);
179*7c478bd9Sstevel@tonic-gate 				outbufprintf("warning: ");
180*7c478bd9Sstevel@tonic-gate 			}
181*7c478bd9Sstevel@tonic-gate 		}
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	/* fmt can be NULL if the caller just wanted flags processed */
185*7c478bd9Sstevel@tonic-gate 	if (fmt != NULL)
186*7c478bd9Sstevel@tonic-gate 		voutbufprintf(fmt, ap);
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	/* O_SYS means convert errno to a string and append it */
189*7c478bd9Sstevel@tonic-gate 	if (flags & O_SYS) {
190*7c478bd9Sstevel@tonic-gate 		const char *msg = strerror(safe_errno);
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 		if (Outidx != 0)
193*7c478bd9Sstevel@tonic-gate 			outbufprintf(": ");
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 		if (msg)
196*7c478bd9Sstevel@tonic-gate 			outbufprintf("%s", msg);
197*7c478bd9Sstevel@tonic-gate 		else
198*7c478bd9Sstevel@tonic-gate 			outbufprintf("(error %d)", safe_errno);
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	/* O_STAMP means convert add a timestamp */
202*7c478bd9Sstevel@tonic-gate 	if (flags & O_STAMP) {
203*7c478bd9Sstevel@tonic-gate 		time_t clock;
204*7c478bd9Sstevel@tonic-gate 		char *tmsg;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 		(void) time(&clock);
207*7c478bd9Sstevel@tonic-gate 		tmsg = ctime(&clock);
208*7c478bd9Sstevel@tonic-gate 		if (tmsg && *tmsg) {
209*7c478bd9Sstevel@tonic-gate 			tmsg[strlen(tmsg) - 1] = '\0';
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 			if (Outidx != 0)
212*7c478bd9Sstevel@tonic-gate 				outbufprintf(" ");
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 			outbufprintf("%s", tmsg);
215*7c478bd9Sstevel@tonic-gate 		}
216*7c478bd9Sstevel@tonic-gate 	}
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	if (flags & O_NONL)
219*7c478bd9Sstevel@tonic-gate 		return;		/* not done filling Outbuf */
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	/* done filling Outbuf, platform calls will add newline */
222*7c478bd9Sstevel@tonic-gate 	if (flags & O_ALTFP)
223*7c478bd9Sstevel@tonic-gate 		(void) fprintf(Altfp, "%s\n", Outbuf);
224*7c478bd9Sstevel@tonic-gate 	else if (flags & O_ABORT)
225*7c478bd9Sstevel@tonic-gate 		io_abort(Outbuf);
226*7c478bd9Sstevel@tonic-gate 	else if (flags & O_DIE)
227*7c478bd9Sstevel@tonic-gate 		io_die(Outbuf);
228*7c478bd9Sstevel@tonic-gate 	else if (flags & O_ERR)
229*7c478bd9Sstevel@tonic-gate 		io_err(Outbuf);
230*7c478bd9Sstevel@tonic-gate 	else
231*7c478bd9Sstevel@tonic-gate 		io_out(Outbuf);
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	/* reset output buffer */
234*7c478bd9Sstevel@tonic-gate 	Outidx = 0;
235*7c478bd9Sstevel@tonic-gate 	Outbuf[0] = '\0';
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate /*
239*7c478bd9Sstevel@tonic-gate  * out -- spew a line of output, with various options
240*7c478bd9Sstevel@tonic-gate  */
241*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
242*7c478bd9Sstevel@tonic-gate void
out(int flags,const char * fmt,...)243*7c478bd9Sstevel@tonic-gate out(int flags, const char *fmt, ...)
244*7c478bd9Sstevel@tonic-gate {
245*7c478bd9Sstevel@tonic-gate 	va_list ap;
246*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
247*7c478bd9Sstevel@tonic-gate 	vout(flags, fmt, ap);
248*7c478bd9Sstevel@tonic-gate 	va_end(ap);
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate /*
252*7c478bd9Sstevel@tonic-gate  * outfl -- spew a filename:linenumber message
253*7c478bd9Sstevel@tonic-gate  */
254*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE4*/
255*7c478bd9Sstevel@tonic-gate void
outfl(int flags,const char * fname,int line,const char * fmt,...)256*7c478bd9Sstevel@tonic-gate outfl(int flags, const char *fname, int line, const char *fmt, ...)
257*7c478bd9Sstevel@tonic-gate {
258*7c478bd9Sstevel@tonic-gate 	va_list ap;
259*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 	if (fname)
262*7c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, "%s:%d: ", fname, line);
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	vout(flags, fmt, ap);
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	va_end(ap);
267*7c478bd9Sstevel@tonic-gate }
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate /*
270*7c478bd9Sstevel@tonic-gate  * out_exit -- exit the program
271*7c478bd9Sstevel@tonic-gate  */
272*7c478bd9Sstevel@tonic-gate void
out_exit(int code)273*7c478bd9Sstevel@tonic-gate out_exit(int code)
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate 	io_exit(Exitcode + code);
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate /*
279*7c478bd9Sstevel@tonic-gate  * out_errcount -- return the number of O_ERR messages issued so far
280*7c478bd9Sstevel@tonic-gate  */
281*7c478bd9Sstevel@tonic-gate int
out_errcount(void)282*7c478bd9Sstevel@tonic-gate out_errcount(void)
283*7c478bd9Sstevel@tonic-gate {
284*7c478bd9Sstevel@tonic-gate 	return (stats_counter_value(Errcount));
285*7c478bd9Sstevel@tonic-gate }
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate /*
288*7c478bd9Sstevel@tonic-gate  * out_warncount -- return the number of O_WARN messages issued so far
289*7c478bd9Sstevel@tonic-gate  */
290*7c478bd9Sstevel@tonic-gate int
out_warncount(void)291*7c478bd9Sstevel@tonic-gate out_warncount(void)
292*7c478bd9Sstevel@tonic-gate {
293*7c478bd9Sstevel@tonic-gate 	return (stats_counter_value(Warncount));
294*7c478bd9Sstevel@tonic-gate }
295