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