dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fm / eversholt / common / out.c
blobef71ec3e94be4b5557fbf565210a20c49f796deb
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * out.c -- some basic output routines
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <stdio.h>
33 #include <fm/fmd_api.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <errno.h>
40 #include <time.h>
41 #include "out.h"
42 #include "stats.h"
43 #include "io.h"
45 /* stats we keep for "out" module */
46 static struct stats *Outcount;
47 static struct stats *Errcount;
48 static struct stats *Warncount;
50 static int Exitcode;
52 static const char *Myname;
53 static FILE *Altfp;
55 /* buffer used to format all prints */
56 #define MAXOUT 8192
57 static char Outbuf[MAXOUT];
58 static int Outidx; /* next unused char in Outbuf[] */
61 * out_init -- initialize this module
63 void
64 out_init(const char *myname)
66 Outcount = stats_new_counter("output.calls", "total calls", 1);
67 Errcount = stats_new_counter("output.errors", "total errors", 0);
68 Warncount = stats_new_counter("output.warnings", "total warnings", 0);
70 if (myname == NULL)
71 return;
73 if ((Myname = strrchr(myname, '/')) == NULL &&
74 (Myname = strrchr(myname, '\\')) == NULL)
75 Myname = myname;
76 else
77 Myname++;
80 void
81 out_fini(void)
83 stats_delete(Outcount);
84 Outcount = NULL;
85 stats_delete(Errcount);
86 Errcount = NULL;
87 stats_delete(Warncount);
88 Warncount = NULL;
92 * out_altfp -- store an alternate fp for O_ALTFP
94 void
95 out_altfp(FILE *fp)
97 Altfp = fp;
101 * voutbufprintf -- like vprintf, but appends to Outbuf
103 static void
104 voutbufprintf(const char *fmt, va_list ap)
106 int len = vsnprintf(&Outbuf[Outidx], MAXOUT - Outidx, fmt, ap);
108 Outidx += len;
109 if (Outidx >= MAXOUT)
110 Outidx = MAXOUT - 1;
114 * outbufprintf -- like printf, but appends to Outbuf
116 /*PRINTFLIKE1*/
117 static void
118 outbufprintf(const char *fmt, ...)
120 va_list ap;
121 va_start(ap, fmt);
122 voutbufprintf(fmt, ap);
123 va_end(ap);
127 * vout -- va_list version of out()
129 * all the output processing work is done here.
131 static void
132 vout(int flags, const char *fmt, va_list ap)
134 int safe_errno = errno;
136 stats_counter_bump(Outcount);
139 * just return if called with a disabled output type. this
140 * prevents debug prints when Debug is off, verbose prints
141 * when Verbose is off, and language warnings when warnings
142 * are quenched (which they are when we're loading a .eft file).
145 if ((flags & O_DEBUG) && Debug == 0)
146 return;
148 if ((flags & O_VERB) && Verbose == 0)
149 return;
151 if ((flags & O_VERB2) && Verbose < 2)
152 return;
154 if ((flags & O_VERB3) && Verbose < 3)
155 return;
157 if ((flags & O_WARN) && Warn == 0)
158 return;
160 if ((flags & O_ALTFP) && Altfp == NULL)
161 return;
163 /* some things only happen at the beginning of a print */
164 if (Outidx == 0) {
165 if (flags & O_USAGE) {
166 Exitcode++;
167 outbufprintf("usage: %s ", Myname);
168 } else {
169 if (Myname && flags & (O_DIE|O_ERR|O_WARN|O_PROG))
170 outbufprintf("%s: ", Myname);
172 if (flags & O_DIE) {
173 Exitcode++;
174 outbufprintf("fatal error: ");
175 } else if (flags & O_ERR) {
176 Exitcode++;
177 stats_counter_bump(Errcount);
178 outbufprintf("error: ");
179 } else if (flags & O_WARN) {
180 stats_counter_bump(Warncount);
181 outbufprintf("warning: ");
186 /* fmt can be NULL if the caller just wanted flags processed */
187 if (fmt != NULL)
188 voutbufprintf(fmt, ap);
190 /* O_SYS means convert errno to a string and append it */
191 if (flags & O_SYS) {
192 const char *msg = strerror(safe_errno);
194 if (Outidx != 0)
195 outbufprintf(": ");
197 if (msg)
198 outbufprintf("%s", msg);
199 else
200 outbufprintf("(error %d)", safe_errno);
203 /* O_STAMP means convert add a timestamp */
204 if (flags & O_STAMP) {
205 time_t clock;
206 char *tmsg;
208 (void) time(&clock);
209 tmsg = ctime(&clock);
210 if (tmsg && *tmsg) {
211 tmsg[strlen(tmsg) - 1] = '\0';
213 if (Outidx != 0)
214 outbufprintf(" ");
216 outbufprintf("%s", tmsg);
220 if (flags & O_NONL)
221 return; /* not done filling Outbuf */
223 /* done filling Outbuf, platform calls will add newline */
224 if (flags & O_ALTFP)
225 (void) fprintf(Altfp, "%s\n", Outbuf);
226 else if (flags & O_ABORT)
227 io_abort(Outbuf);
228 else if (flags & O_DIE)
229 io_die(Outbuf);
230 else if (flags & O_ERR)
231 io_err(Outbuf);
232 else
233 io_out(Outbuf);
235 /* reset output buffer */
236 Outidx = 0;
237 Outbuf[0] = '\0';
241 * out -- spew a line of output, with various options
243 /*PRINTFLIKE2*/
244 void
245 out(int flags, const char *fmt, ...)
247 va_list ap;
248 va_start(ap, fmt);
249 vout(flags, fmt, ap);
250 va_end(ap);
254 * outfl -- spew a filename:linenumber message
256 /*PRINTFLIKE4*/
257 void
258 outfl(int flags, const char *fname, int line, const char *fmt, ...)
260 va_list ap;
261 va_start(ap, fmt);
263 if (fname)
264 out(flags|O_NONL, "%s:%d: ", fname, line);
266 vout(flags, fmt, ap);
268 va_end(ap);
272 * out_exit -- exit the program
274 void
275 out_exit(int code)
277 io_exit(Exitcode + code);
281 * out_errcount -- return the number of O_ERR messages issued so far
284 out_errcount(void)
286 return (stats_counter_value(Errcount));
290 * out_warncount -- return the number of O_WARN messages issued so far
293 out_warncount(void)
295 return (stats_counter_value(Warncount));