xref: /onnv-gate/usr/src/lib/efcode/engine/env.c (revision 0:68f95e015346)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <ctype.h>
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate #include <unistd.h>
32*0Sstevel@tonic-gate #include <stdlib.h>
33*0Sstevel@tonic-gate #include <string.h>
34*0Sstevel@tonic-gate #include <sys/time.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <fcode/private.h>
37*0Sstevel@tonic-gate #include <fcode/log.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate static variable_t verbose_emit;
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate void
43*0Sstevel@tonic-gate do_verbose_emit(fcode_env_t *env)
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate 	verbose_emit ^= 1;
46*0Sstevel@tonic-gate }
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  * Internal "emit".
50*0Sstevel@tonic-gate  * Note log_emit gathers up characters and issues a syslog or write to
51*0Sstevel@tonic-gate  * error log file if enabled.
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate void
54*0Sstevel@tonic-gate do_emit(fcode_env_t *env, uchar_t c)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate 	if (verbose_emit)
57*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "emit(%x)\n", c);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	if (c == '\n') {
60*0Sstevel@tonic-gate 		env->output_column = 0;
61*0Sstevel@tonic-gate 		env->output_line++;
62*0Sstevel@tonic-gate 	} else if (c == '\r')
63*0Sstevel@tonic-gate 		env->output_column = 0;
64*0Sstevel@tonic-gate 	else
65*0Sstevel@tonic-gate 		env->output_column++;
66*0Sstevel@tonic-gate 	if (isatty(fileno(stdout))) {
67*0Sstevel@tonic-gate 		if ((c >= 0x20 && c <= 0x7f) || c == '\n' || c == '\r' ||
68*0Sstevel@tonic-gate 		    c == '\b')
69*0Sstevel@tonic-gate 			putchar(c);
70*0Sstevel@tonic-gate 		else if (c >= 0 && c < 0x20)
71*0Sstevel@tonic-gate 			printf("@%c", c + '@');
72*0Sstevel@tonic-gate 		else
73*0Sstevel@tonic-gate 			printf("\\%x", c);
74*0Sstevel@tonic-gate 		fflush(stdout);
75*0Sstevel@tonic-gate 	}
76*0Sstevel@tonic-gate 	log_emit(c);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate void
80*0Sstevel@tonic-gate system_message(fcode_env_t *env, char *msg)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	throw_from_fclib(env, 1, msg);
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate void
86*0Sstevel@tonic-gate emit(fcode_env_t *env)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	fstack_t d;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "emit");
91*0Sstevel@tonic-gate 	d = POP(DS);
92*0Sstevel@tonic-gate 	do_emit(env, d);
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate #include <sys/time.h>
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate /*
98*0Sstevel@tonic-gate  * 'key?' - abort if stdin is not a tty.
99*0Sstevel@tonic-gate  */
100*0Sstevel@tonic-gate void
101*0Sstevel@tonic-gate keyquestion(fcode_env_t *env)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	struct timeval timeval;
104*0Sstevel@tonic-gate 	fd_set readfds;
105*0Sstevel@tonic-gate 	int ret;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	if (isatty(fileno(stdin))) {
108*0Sstevel@tonic-gate 		FD_ZERO(&readfds);
109*0Sstevel@tonic-gate 		FD_SET(fileno(stdin), &readfds);
110*0Sstevel@tonic-gate 		timeval.tv_sec = 0;
111*0Sstevel@tonic-gate 		timeval.tv_usec = 1000;
112*0Sstevel@tonic-gate 		ret = select(fileno(stdin) + 1, &readfds, NULL, NULL, &timeval);
113*0Sstevel@tonic-gate 		if (FD_ISSET(fileno(stdin), &readfds))
114*0Sstevel@tonic-gate 			PUSH(DS, TRUE);
115*0Sstevel@tonic-gate 		else
116*0Sstevel@tonic-gate 			PUSH(DS, FALSE);
117*0Sstevel@tonic-gate 	} else
118*0Sstevel@tonic-gate 		forth_abort(env, "'key?' called in non-interactive mode");
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate /*
122*0Sstevel@tonic-gate  * 'key' - abort if stdin is not a tty, will block on read if char not avail.
123*0Sstevel@tonic-gate  */
124*0Sstevel@tonic-gate void
125*0Sstevel@tonic-gate key(fcode_env_t *env)
126*0Sstevel@tonic-gate {
127*0Sstevel@tonic-gate 	uchar_t c;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	if (isatty(fileno(stdin))) {
130*0Sstevel@tonic-gate 		read(fileno(stdin), &c, 1);
131*0Sstevel@tonic-gate 		PUSH(DS, c);
132*0Sstevel@tonic-gate 	} else
133*0Sstevel@tonic-gate 		forth_abort(env, "'key' called in non-interactive mode");
134*0Sstevel@tonic-gate }
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate void
137*0Sstevel@tonic-gate type(fcode_env_t *env)
138*0Sstevel@tonic-gate {
139*0Sstevel@tonic-gate 	int len;
140*0Sstevel@tonic-gate 	char *ptr;
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "type");
143*0Sstevel@tonic-gate 	ptr = pop_a_string(env, &len);
144*0Sstevel@tonic-gate 	while (len--)
145*0Sstevel@tonic-gate 		do_emit(env, *ptr++);
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate void
149*0Sstevel@tonic-gate paren_cr(fcode_env_t *env)
150*0Sstevel@tonic-gate {
151*0Sstevel@tonic-gate 	do_emit(env, '\r');
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate void
155*0Sstevel@tonic-gate fc_crlf(fcode_env_t *env)
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate 	do_emit(env, '\n');
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate void
161*0Sstevel@tonic-gate fc_num_out(fcode_env_t *env)
162*0Sstevel@tonic-gate {
163*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)(&env->output_column));
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate void
167*0Sstevel@tonic-gate fc_num_line(fcode_env_t *env)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)(&env->output_line));
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate void
173*0Sstevel@tonic-gate expect(fcode_env_t *env)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	char *buf, *rbuf;
176*0Sstevel@tonic-gate 	int len;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 2, "expect");
179*0Sstevel@tonic-gate 	buf = pop_a_string(env, &len);
180*0Sstevel@tonic-gate 	read_line(env);
181*0Sstevel@tonic-gate 	rbuf = pop_a_string(env, NULL);
182*0Sstevel@tonic-gate 	if (rbuf) {
183*0Sstevel@tonic-gate 		strcpy(buf, rbuf);
184*0Sstevel@tonic-gate 		env->span = strlen(buf);
185*0Sstevel@tonic-gate 	} else
186*0Sstevel@tonic-gate 		env->span = 0;
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate void
190*0Sstevel@tonic-gate span(fcode_env_t *env)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)&env->span);
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate void
196*0Sstevel@tonic-gate do_ms(fcode_env_t *env)
197*0Sstevel@tonic-gate {
198*0Sstevel@tonic-gate 	fstack_t d;
199*0Sstevel@tonic-gate 	timespec_t rqtp;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	CHECK_DEPTH(env, 1, "ms");
202*0Sstevel@tonic-gate 	d = POP(DS);
203*0Sstevel@tonic-gate 	if (d) {
204*0Sstevel@tonic-gate 		rqtp.tv_sec = 0;
205*0Sstevel@tonic-gate 		rqtp.tv_nsec = d*1000*1000;
206*0Sstevel@tonic-gate 		nanosleep(&rqtp, 0);
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate void
211*0Sstevel@tonic-gate do_get_msecs(fcode_env_t *env)
212*0Sstevel@tonic-gate {
213*0Sstevel@tonic-gate 	struct timeval tp;
214*0Sstevel@tonic-gate 	long ms;
215*0Sstevel@tonic-gate 	timespec_t rqtp;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	gettimeofday(&tp, NULL);
218*0Sstevel@tonic-gate 	ms = (tp.tv_usec/1000) + (tp.tv_sec * 1000);
219*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)ms);
220*0Sstevel@tonic-gate 	rqtp.tv_sec = 0;
221*0Sstevel@tonic-gate 	rqtp.tv_nsec = 1000*1000;
222*0Sstevel@tonic-gate 	nanosleep(&rqtp, 0);
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate #define	CMN_MSG_SIZE	256
226*0Sstevel@tonic-gate #define	CMN_MAX_DIGITS	3
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate typedef struct CMN_MSG_T cmn_msg_t;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate struct CMN_MSG_T {
231*0Sstevel@tonic-gate 	char		buf[CMN_MSG_SIZE];
232*0Sstevel@tonic-gate 	int		level;
233*0Sstevel@tonic-gate 	int 		len;
234*0Sstevel@tonic-gate 	cmn_msg_t	*prev;
235*0Sstevel@tonic-gate 	cmn_msg_t	*next;
236*0Sstevel@tonic-gate };
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate typedef struct CMN_FMT_T cmn_fmt_t;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate struct CMN_FMT_T {
241*0Sstevel@tonic-gate 	int	fwidth;	/* format field width */
242*0Sstevel@tonic-gate 	int	cwidth; /* column width specified in format */
243*0Sstevel@tonic-gate 	char	format; /* format type */
244*0Sstevel@tonic-gate };
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate static cmn_msg_t 	*root = NULL;
247*0Sstevel@tonic-gate static int		cmn_msg_level = 0;
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate /*
250*0Sstevel@tonic-gate  *	validfmt()
251*0Sstevel@tonic-gate  *
252*0Sstevel@tonic-gate  * Called by fmt_str() function to validate and extract formatting
253*0Sstevel@tonic-gate  * information from the supplied input buffer.
254*0Sstevel@tonic-gate  *
255*0Sstevel@tonic-gate  * Supported formats are:
256*0Sstevel@tonic-gate  *	%c - character
257*0Sstevel@tonic-gate  *	%d - signed decimal
258*0Sstevel@tonic-gate  *	%x - unsigned hex
259*0Sstevel@tonic-gate  *	%s - string
260*0Sstevel@tonic-gate  *	%ld - signed 64 bit data
261*0Sstevel@tonic-gate  *	%lx - unsigned 64 bit data
262*0Sstevel@tonic-gate  *	%p - unsigned 64 bit data(pointer)
263*0Sstevel@tonic-gate  *	%% - print as single "%" character
264*0Sstevel@tonic-gate  *
265*0Sstevel@tonic-gate  * Return values are:
266*0Sstevel@tonic-gate  *	0  - valid formatting
267*0Sstevel@tonic-gate  *	1  - invalid formatting found in the input buffer
268*0Sstevel@tonic-gate  *	-1 - NULL pointer passed in for caller's receptacle
269*0Sstevel@tonic-gate  *
270*0Sstevel@tonic-gate  *
271*0Sstevel@tonic-gate  * For valid formatting, caller's supplied cmn_fmt_t elements are
272*0Sstevel@tonic-gate  * filled in:
273*0Sstevel@tonic-gate  *	fwidth:
274*0Sstevel@tonic-gate  * 		> 0 - returned value is the field width
275*0Sstevel@tonic-gate  *		< 0 - returned value is negation of field width for
276*0Sstevel@tonic-gate  *			64 bit data formats
277*0Sstevel@tonic-gate  *	cwidth:
278*0Sstevel@tonic-gate  *	  formatted column width(if specified), otherwise 0
279*0Sstevel@tonic-gate  *
280*0Sstevel@tonic-gate  *	format:
281*0Sstevel@tonic-gate  *	  contains the formatting(single) character
282*0Sstevel@tonic-gate  */
283*0Sstevel@tonic-gate static int
284*0Sstevel@tonic-gate validfmt(char *fmt, cmn_fmt_t *cfstr)
285*0Sstevel@tonic-gate {
286*0Sstevel@tonic-gate 	int	isll = 0;
287*0Sstevel@tonic-gate 	int	*fwidth, *cwidth;
288*0Sstevel@tonic-gate 	char	*format;
289*0Sstevel@tonic-gate 	char	*dig1, *dig2;
290*0Sstevel@tonic-gate 	char	cdigs[CMN_MAX_DIGITS+1];
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	if (cfstr == NULL)
293*0Sstevel@tonic-gate 		return (-1);
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	fwidth = &cfstr->fwidth;
296*0Sstevel@tonic-gate 	cwidth = &cfstr->cwidth;
297*0Sstevel@tonic-gate 	format = &cfstr->format;
298*0Sstevel@tonic-gate 	*fwidth = *cwidth = 0;
299*0Sstevel@tonic-gate 	*format = NULL;
300*0Sstevel@tonic-gate 	dig1 = dig2 = NULL;
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	/* check for left justification character */
303*0Sstevel@tonic-gate 	if (*fmt == '-') {
304*0Sstevel@tonic-gate 		fmt++;
305*0Sstevel@tonic-gate 		(*fwidth)++;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 		/* check for column width specification */
308*0Sstevel@tonic-gate 		if (isdigit(*fmt))
309*0Sstevel@tonic-gate 			dig1 = fmt;	/* save ptr to first digit */
310*0Sstevel@tonic-gate 		while (isdigit(*fmt)) {
311*0Sstevel@tonic-gate 			fmt++;
312*0Sstevel@tonic-gate 			(*fwidth)++;
313*0Sstevel@tonic-gate 		}
314*0Sstevel@tonic-gate 		/* if ljust specified w/o size, return format error */
315*0Sstevel@tonic-gate 		if (*fwidth == 1) {
316*0Sstevel@tonic-gate 			return (1);
317*0Sstevel@tonic-gate 		}
318*0Sstevel@tonic-gate 		dig2 = fmt;		/* save ptr to last digit + 1 */
319*0Sstevel@tonic-gate 	} else {
320*0Sstevel@tonic-gate 		/* check for column width specification */
321*0Sstevel@tonic-gate 		if (isdigit(*fmt)) {
322*0Sstevel@tonic-gate 			dig1 = fmt;	/* save ptr to first digit */
323*0Sstevel@tonic-gate 			while (isdigit(*fmt)) {
324*0Sstevel@tonic-gate 				fmt++;
325*0Sstevel@tonic-gate 				(*fwidth)++;
326*0Sstevel@tonic-gate 			}
327*0Sstevel@tonic-gate 			dig2 = fmt;	/* save ptr to last digit + 1 */
328*0Sstevel@tonic-gate 		}
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	/* if a column width was specified, save it in caller's struct */
332*0Sstevel@tonic-gate 	if (dig1) {
333*0Sstevel@tonic-gate 		int nbytes;
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 		nbytes = dig2 - dig1;
336*0Sstevel@tonic-gate 		/* if too many digits in the width return error */
337*0Sstevel@tonic-gate 		if (nbytes > CMN_MAX_DIGITS)
338*0Sstevel@tonic-gate 			return (1);
339*0Sstevel@tonic-gate 		strncpy(cdigs, dig1, nbytes);
340*0Sstevel@tonic-gate 		cdigs[nbytes] = 0;
341*0Sstevel@tonic-gate 		*cwidth = atoi(cdigs);
342*0Sstevel@tonic-gate 	}
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	/* check for long format specifier */
345*0Sstevel@tonic-gate 	if (*fmt == 'l') {
346*0Sstevel@tonic-gate 		fmt++;
347*0Sstevel@tonic-gate 		(*fwidth)++;
348*0Sstevel@tonic-gate 		isll = 1;
349*0Sstevel@tonic-gate 	}
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	/* process by specific format type */
352*0Sstevel@tonic-gate 	switch (*fmt) {
353*0Sstevel@tonic-gate 	case 'c':
354*0Sstevel@tonic-gate 	case 's':
355*0Sstevel@tonic-gate 	case '%':
356*0Sstevel@tonic-gate 		if (isll)
357*0Sstevel@tonic-gate 			return (1);
358*0Sstevel@tonic-gate 	case 'd':
359*0Sstevel@tonic-gate 	case 'x':
360*0Sstevel@tonic-gate 		*format = *fmt;
361*0Sstevel@tonic-gate 		(*fwidth)++;
362*0Sstevel@tonic-gate 		break;
363*0Sstevel@tonic-gate 	case 'p':
364*0Sstevel@tonic-gate 		isll = 1; 		/* uses 64 bit format */
365*0Sstevel@tonic-gate 		*format = *fmt;
366*0Sstevel@tonic-gate 		(*fwidth)++;
367*0Sstevel@tonic-gate 		break;
368*0Sstevel@tonic-gate 	default:
369*0Sstevel@tonic-gate 		return (1);		/* unknown format type */
370*0Sstevel@tonic-gate 	}
371*0Sstevel@tonic-gate 	if (isll) {
372*0Sstevel@tonic-gate 		*fwidth *= -1;
373*0Sstevel@tonic-gate 	}
374*0Sstevel@tonic-gate 	return (0);
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate /*
378*0Sstevel@tonic-gate  *	fmt_args()
379*0Sstevel@tonic-gate  *
380*0Sstevel@tonic-gate  * Called by fmt_str() to setup arguments for subsequent snprintf()
381*0Sstevel@tonic-gate  * calls.  For cases not involving column width limitations, processing
382*0Sstevel@tonic-gate  * simply POPs the data stack as required to setup caller's arg(or
383*0Sstevel@tonic-gate  * llarg, as appropriate). When a column width is specified for output,
384*0Sstevel@tonic-gate  * a temporary buffer is constructed to contain snprintf() generated
385*0Sstevel@tonic-gate  * output for the argument. Testing is then performed to determine if
386*0Sstevel@tonic-gate  * the specified column width will require truncation of the output.
387*0Sstevel@tonic-gate  * If so, truncation of least significant digits is performed as
388*0Sstevel@tonic-gate  * necessary, and caller's arg(or llarg) is adjusted to obtain the
389*0Sstevel@tonic-gate  * specified column width.
390*0Sstevel@tonic-gate  *
391*0Sstevel@tonic-gate  */
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate static void
394*0Sstevel@tonic-gate fmt_args(fcode_env_t *env, int cw, int fw, char format, long *arg,
395*0Sstevel@tonic-gate 	long long *llarg)
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate 	char	*cbuf;
398*0Sstevel@tonic-gate 	char	snf[3];
399*0Sstevel@tonic-gate 	int	cbsize;
400*0Sstevel@tonic-gate 	int	cnv = 10, ndigits = 0;
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 	if (fw > 0) {	/* check for normal (not long) formats */
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 		/* initialize format string for snprintf call */
405*0Sstevel@tonic-gate 		snf[0] = '%';
406*0Sstevel@tonic-gate 		snf[1] = format;
407*0Sstevel@tonic-gate 		snf[2] = 0;
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 		/* process by format type */
410*0Sstevel@tonic-gate 		switch (format) {
411*0Sstevel@tonic-gate 		case 'x':
412*0Sstevel@tonic-gate 			cnv = 16;
413*0Sstevel@tonic-gate 		case 'd':
414*0Sstevel@tonic-gate 		case 'c':
415*0Sstevel@tonic-gate 		case 'p':
416*0Sstevel@tonic-gate 			*arg = POP(DS);
417*0Sstevel@tonic-gate 			break;
418*0Sstevel@tonic-gate 		case 's':
419*0Sstevel@tonic-gate 			POP(DS);
420*0Sstevel@tonic-gate 			*arg = POP(DS);
421*0Sstevel@tonic-gate 			break;
422*0Sstevel@tonic-gate 		case '%':
423*0Sstevel@tonic-gate 			return;
424*0Sstevel@tonic-gate 		default:
425*0Sstevel@tonic-gate 			log_message(MSG_ERROR,
426*0Sstevel@tonic-gate 				"fmt_args:invalid format type! (%s)\n",
427*0Sstevel@tonic-gate 					&format);
428*0Sstevel@tonic-gate 			return;
429*0Sstevel@tonic-gate 		}
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 		/* check if a column width was specified */
432*0Sstevel@tonic-gate 		if (cw) {
433*0Sstevel@tonic-gate 			/* allocate a scratch buffer */
434*0Sstevel@tonic-gate 			cbsize = 2*(sizeof (long long)) + 1;
435*0Sstevel@tonic-gate 			cbuf = MALLOC(cbsize);
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 			if (snprintf(cbuf, cbsize, snf, *arg) < 0)
438*0Sstevel@tonic-gate 				log_message(MSG_ERROR,
439*0Sstevel@tonic-gate 					"fmt_args: snprintf output error\n");
440*0Sstevel@tonic-gate 			while ((cbuf[ndigits] != NULL) &&
441*0Sstevel@tonic-gate 				(ndigits < cbsize))
442*0Sstevel@tonic-gate 				ndigits++;
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 			/* if truncation is necessary, do it */
445*0Sstevel@tonic-gate 			if (ndigits > cw) {
446*0Sstevel@tonic-gate 				cbuf[cw] = 0;
447*0Sstevel@tonic-gate 				if (format == 's') {
448*0Sstevel@tonic-gate 					char *str;
449*0Sstevel@tonic-gate 					str = (char *)*arg;
450*0Sstevel@tonic-gate 					str[cw] = 0;
451*0Sstevel@tonic-gate 				} else
452*0Sstevel@tonic-gate 					*arg = strtol(cbuf,
453*0Sstevel@tonic-gate 						(char **)NULL, cnv);
454*0Sstevel@tonic-gate 			}
455*0Sstevel@tonic-gate 			free(cbuf);
456*0Sstevel@tonic-gate 		}
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	} else {	/* process long formats */
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 		*llarg = POP(DS);
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 		/* check if a column width was specified */
463*0Sstevel@tonic-gate 		if (cw) {
464*0Sstevel@tonic-gate 			/* allocate a scratch buffer */
465*0Sstevel@tonic-gate 			cbsize = 2*(sizeof (long long)) + 1;
466*0Sstevel@tonic-gate 			cbuf = MALLOC(cbsize);
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 			switch (format) {
469*0Sstevel@tonic-gate 			case 'p':
470*0Sstevel@tonic-gate 				cnv = 16;
471*0Sstevel@tonic-gate 				if (snprintf(cbuf, cbsize, "%p", *llarg) < 0)
472*0Sstevel@tonic-gate 					log_message(MSG_ERROR,
473*0Sstevel@tonic-gate 						"fmt_args: snprintf error\n");
474*0Sstevel@tonic-gate 				break;
475*0Sstevel@tonic-gate 			case 'x':
476*0Sstevel@tonic-gate 				cnv = 16;
477*0Sstevel@tonic-gate 				if (snprintf(cbuf, cbsize, "%lx", *llarg) < 0)
478*0Sstevel@tonic-gate 					log_message(MSG_ERROR,
479*0Sstevel@tonic-gate 					    "fmt_args: snprintf error\n");
480*0Sstevel@tonic-gate 				break;
481*0Sstevel@tonic-gate 			case 'd':
482*0Sstevel@tonic-gate 				if (snprintf(cbuf, cbsize, "%ld", *llarg) < 0)
483*0Sstevel@tonic-gate 					log_message(MSG_ERROR,
484*0Sstevel@tonic-gate 						"fmt_args: snprintf error\n");
485*0Sstevel@tonic-gate 				break;
486*0Sstevel@tonic-gate 			default:
487*0Sstevel@tonic-gate 				log_message(MSG_ERROR,
488*0Sstevel@tonic-gate 				    "invalid long format type! (l%s)\n",
489*0Sstevel@tonic-gate 						&format);
490*0Sstevel@tonic-gate 				free(cbuf);
491*0Sstevel@tonic-gate 				return;
492*0Sstevel@tonic-gate 			}
493*0Sstevel@tonic-gate 			while ((cbuf[ndigits] != NULL) &&
494*0Sstevel@tonic-gate 				(ndigits < cbsize)) {
495*0Sstevel@tonic-gate 				ndigits++;
496*0Sstevel@tonic-gate 			}
497*0Sstevel@tonic-gate 			/* if truncation is necessary, do it */
498*0Sstevel@tonic-gate 			if (ndigits > cw) {
499*0Sstevel@tonic-gate 				cbuf[cw] = 0;
500*0Sstevel@tonic-gate 				*llarg = strtoll(cbuf, (char **)NULL, cnv);
501*0Sstevel@tonic-gate 			}
502*0Sstevel@tonic-gate 			free(cbuf);
503*0Sstevel@tonic-gate 		}
504*0Sstevel@tonic-gate 	}
505*0Sstevel@tonic-gate }
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate /*
508*0Sstevel@tonic-gate  *	fmt_str()
509*0Sstevel@tonic-gate  *
510*0Sstevel@tonic-gate  * Extracts text from caller's input buffer, processes explicit
511*0Sstevel@tonic-gate  * formatting as necessary, and outputs formatted text to caller's
512*0Sstevel@tonic-gate  * receptacle.
513*0Sstevel@tonic-gate  *
514*0Sstevel@tonic-gate  *	env  - pointer to caller's fcode environment
515*0Sstevel@tonic-gate  *	fmt  - pointer to caller's input buffr
516*0Sstevel@tonic-gate  *	fmtbuf - ponter to caller's receptacle buffer
517*0Sstevel@tonic-gate  *	bsize - size of caller's fmtbuf buffer
518*0Sstevel@tonic-gate  *
519*0Sstevel@tonic-gate  * This function performs an initial test to determine if caller's
520*0Sstevel@tonic-gate  * input buffer contains formatting(specified by presence of "%")
521*0Sstevel@tonic-gate  * in the buffer.  If so, validfmt() function is called to verify
522*0Sstevel@tonic-gate  * the formatting, after which the buffer is processed according
523*0Sstevel@tonic-gate  * to the field width specified by validfmt() output.  Special
524*0Sstevel@tonic-gate  * processing is required when caller's buffer contains a double
525*0Sstevel@tonic-gate  * "%" ("%%"), in which case the second "%" is accepted as normal
526*0Sstevel@tonic-gate  * text.
527*0Sstevel@tonic-gate  */
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate static void
530*0Sstevel@tonic-gate fmt_str(fcode_env_t *env, char *fmt, char *fmtbuf, int bsize)
531*0Sstevel@tonic-gate {
532*0Sstevel@tonic-gate 	char	tbuf[CMN_MSG_SIZE];
533*0Sstevel@tonic-gate 	char	*fmptr, *pct;
534*0Sstevel@tonic-gate 	int	l, cw, fw, bytes;
535*0Sstevel@tonic-gate 	long	arg;
536*0Sstevel@tonic-gate 	long long llarg;
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 	*fmtbuf = 0;
539*0Sstevel@tonic-gate 	if ((pct = strchr(fmt, '%')) != 0) {
540*0Sstevel@tonic-gate 		cmn_fmt_t	cfstr;
541*0Sstevel@tonic-gate 		int		vferr;
542*0Sstevel@tonic-gate 
543*0Sstevel@tonic-gate 		l = strlen(pct++);
544*0Sstevel@tonic-gate 		vferr = validfmt(pct, &cfstr);
545*0Sstevel@tonic-gate 		if (!vferr) {
546*0Sstevel@tonic-gate 			fw = cfstr.fwidth;
547*0Sstevel@tonic-gate 			cw = cfstr.cwidth;
548*0Sstevel@tonic-gate 			fmptr = &cfstr.format;
549*0Sstevel@tonic-gate 		} else {
550*0Sstevel@tonic-gate 			if (vferr < 0) {
551*0Sstevel@tonic-gate 			log_message(MSG_ERROR,
552*0Sstevel@tonic-gate 			    "fmt_str: NULL ptr supplied to validfmt()\n");
553*0Sstevel@tonic-gate 			return;
554*0Sstevel@tonic-gate 			}
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 			bytes = pct - fmt;
557*0Sstevel@tonic-gate 			strncpy(tbuf, fmt, bytes);
558*0Sstevel@tonic-gate 			strncpy(tbuf+bytes, "%", 1);
559*0Sstevel@tonic-gate 			strncpy(tbuf+bytes+1, fmt+bytes, 1);
560*0Sstevel@tonic-gate 			bytes += 2;
561*0Sstevel@tonic-gate 			tbuf[bytes] = 0;
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 			log_message(MSG_ERROR,
564*0Sstevel@tonic-gate 				"fmt_str: invalid format type! (%s)\n",
565*0Sstevel@tonic-gate 				tbuf+bytes-3);
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 			strncpy(fmtbuf, tbuf, bsize);
568*0Sstevel@tonic-gate 			return;
569*0Sstevel@tonic-gate 		}
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 		if (fw > 0) {	/* process normal (not long) formats */
572*0Sstevel@tonic-gate 			bytes = pct - fmt + fw;
573*0Sstevel@tonic-gate 			strncpy(tbuf, fmt, bytes);
574*0Sstevel@tonic-gate 			tbuf[bytes] = 0;
575*0Sstevel@tonic-gate 		} else {
576*0Sstevel@tonic-gate 			/* if here, fw must be a long format */
577*0Sstevel@tonic-gate 			if (*fmptr == 'p') {
578*0Sstevel@tonic-gate 				bytes = pct - fmt - fw;
579*0Sstevel@tonic-gate 				strncpy(tbuf, fmt, bytes);
580*0Sstevel@tonic-gate 				tbuf[bytes] = 0;
581*0Sstevel@tonic-gate 			} else {
582*0Sstevel@tonic-gate 				bytes = pct - fmt - fw - 2;
583*0Sstevel@tonic-gate 				strncpy(tbuf, fmt, bytes);
584*0Sstevel@tonic-gate 				tbuf[bytes] = 'l';
585*0Sstevel@tonic-gate 				strncpy(tbuf+bytes+1, fmt+bytes, 2);
586*0Sstevel@tonic-gate 				tbuf[bytes+1+2] = 0;
587*0Sstevel@tonic-gate 			}
588*0Sstevel@tonic-gate 		}
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 		/* if more input buffer to process, recurse */
591*0Sstevel@tonic-gate 		if ((l - abs(fw)) != 0) {
592*0Sstevel@tonic-gate 		    fmt_str(env, pct+abs(fw), (tbuf + strlen(tbuf)),
593*0Sstevel@tonic-gate 				CMN_MSG_SIZE - strlen(tbuf));
594*0Sstevel@tonic-gate 		}
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 		/* call to extract args for snprintf() calls below */
597*0Sstevel@tonic-gate 		fmt_args(env, cw, fw, *fmptr, &arg, &llarg);
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 		if (fw > 0) {	/* process normal (not long) formats */
600*0Sstevel@tonic-gate 			switch (*fmptr) {
601*0Sstevel@tonic-gate 			case 'd':
602*0Sstevel@tonic-gate 			case 'x':
603*0Sstevel@tonic-gate 			case 'c':
604*0Sstevel@tonic-gate 			case 's':
605*0Sstevel@tonic-gate 			case 'p':
606*0Sstevel@tonic-gate 				(void) snprintf(fmtbuf, bsize, tbuf, arg);
607*0Sstevel@tonic-gate 				break;
608*0Sstevel@tonic-gate 			case '%':
609*0Sstevel@tonic-gate 				(void) snprintf(fmtbuf, bsize, tbuf);
610*0Sstevel@tonic-gate 				break;
611*0Sstevel@tonic-gate 			default:
612*0Sstevel@tonic-gate 				log_message(MSG_ERROR,
613*0Sstevel@tonic-gate 				    "fmt_str: invalid format (%s)\n",
614*0Sstevel@tonic-gate 					fmptr);
615*0Sstevel@tonic-gate 				return;
616*0Sstevel@tonic-gate 			}
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 		} else	/* process long formats */
619*0Sstevel@tonic-gate 			(void) snprintf(fmtbuf, bsize, tbuf, llarg);
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	} else
622*0Sstevel@tonic-gate 		strncpy(fmtbuf, fmt, bsize);
623*0Sstevel@tonic-gate }
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate /*
626*0Sstevel@tonic-gate  *	fc_cmn_append()
627*0Sstevel@tonic-gate  *
628*0Sstevel@tonic-gate  * Pops data stack to obtain message text, and calls fmt_str()
629*0Sstevel@tonic-gate  * function to perform any message formatting necessary.
630*0Sstevel@tonic-gate  *
631*0Sstevel@tonic-gate  * This function is called from fc_cmn_end() or directly in
632*0Sstevel@tonic-gate  * processing a cmn-append token.  Since a pre-existing message
633*0Sstevel@tonic-gate  * context is assumed, initial checking is performed to verify
634*0Sstevel@tonic-gate  * its existence.
635*0Sstevel@tonic-gate  */
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate void
638*0Sstevel@tonic-gate fc_cmn_append(fcode_env_t *env)
639*0Sstevel@tonic-gate {
640*0Sstevel@tonic-gate 	int len;
641*0Sstevel@tonic-gate 	char *str;
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	if (root == NULL) {
644*0Sstevel@tonic-gate 		log_message(MSG_ERROR,
645*0Sstevel@tonic-gate 			"fc_cmn_append: no message context for append\n");
646*0Sstevel@tonic-gate 		return;
647*0Sstevel@tonic-gate 	}
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 	len = POP(DS);
650*0Sstevel@tonic-gate 	str = (char *)POP(DS);
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 	if ((root->len + len) < CMN_MSG_SIZE) {
653*0Sstevel@tonic-gate 		fmt_str(env, str, root->buf+root->len, CMN_MSG_SIZE -
654*0Sstevel@tonic-gate 			root->len);
655*0Sstevel@tonic-gate 		root->len += len;
656*0Sstevel@tonic-gate 	} else
657*0Sstevel@tonic-gate 		log_message(MSG_ERROR,
658*0Sstevel@tonic-gate 			"fc_cmn_append: append exceeds max msg size\n");
659*0Sstevel@tonic-gate }
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate /*
662*0Sstevel@tonic-gate  *	fc_cmn_end()
663*0Sstevel@tonic-gate  *
664*0Sstevel@tonic-gate  * Process ]cmn-end token to log the message initiated by a preceeding
665*0Sstevel@tonic-gate  * fc_cmn_start() call.
666*0Sstevel@tonic-gate  *
667*0Sstevel@tonic-gate  * Since nested cmn-xxx[ calls are supported, a test is made to determine
668*0Sstevel@tonic-gate  * if this is the final cmn-end of a nested sequence.  If so, or if
669*0Sstevel@tonic-gate  * there was no nesting, log_message() is called with the appropriate
670*0Sstevel@tonic-gate  * text buffer.  Otherwise, the root variable is adjusted to point to
671*0Sstevel@tonic-gate  * the preceeding message in the sequence and links in the list are
672*0Sstevel@tonic-gate  * updated. No logging is performed until the final ]cmn-end of the
673*0Sstevel@tonic-gate  * sequence is processed; then, messages are logged in FIFO order.
674*0Sstevel@tonic-gate  */
675*0Sstevel@tonic-gate void
676*0Sstevel@tonic-gate fc_cmn_end(fcode_env_t *env)
677*0Sstevel@tonic-gate {
678*0Sstevel@tonic-gate 	cmn_msg_t *old;
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 	if (root == 0) {
681*0Sstevel@tonic-gate 		log_message(MSG_ERROR, "]cmn-end call w/o buffer\n");
682*0Sstevel@tonic-gate 		return;
683*0Sstevel@tonic-gate 	}
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 	fc_cmn_append(env);
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 	if (root->prev == 0) {
688*0Sstevel@tonic-gate 		cmn_msg_t *next;
689*0Sstevel@tonic-gate 		do {
690*0Sstevel@tonic-gate 			log_message(root->level, "%s\n", root->buf);
691*0Sstevel@tonic-gate 			next  = root->next;
692*0Sstevel@tonic-gate 			free(root);
693*0Sstevel@tonic-gate 			root = next;
694*0Sstevel@tonic-gate 		} while (root);
695*0Sstevel@tonic-gate 	} else {
696*0Sstevel@tonic-gate 		old = root->prev;
697*0Sstevel@tonic-gate 		old->next = root;
698*0Sstevel@tonic-gate 		root = old;
699*0Sstevel@tonic-gate 	}
700*0Sstevel@tonic-gate }
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate /*
703*0Sstevel@tonic-gate  *	fc_cmn_start()
704*0Sstevel@tonic-gate  *
705*0Sstevel@tonic-gate  * Generic function to begin a common message.
706*0Sstevel@tonic-gate  *
707*0Sstevel@tonic-gate  * Allocates a new cmn_msg_t to associate with the message, and sets
708*0Sstevel@tonic-gate  * up initial text as specified by callers' inputs:
709*0Sstevel@tonic-gate  *
710*0Sstevel@tonic-gate  *	env  - pointer to caller's fcode environment
711*0Sstevel@tonic-gate  *	head - pointer to initial text portion of the message
712*0Sstevel@tonic-gate  *	path - flag to indicate if a device path is to be generated
713*0Sstevel@tonic-gate  */
714*0Sstevel@tonic-gate static void
715*0Sstevel@tonic-gate fc_cmn_start(fcode_env_t *env, char *head, int path)
716*0Sstevel@tonic-gate {
717*0Sstevel@tonic-gate 	cmn_msg_t *new;
718*0Sstevel@tonic-gate 	char		*dpath;
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 	new = MALLOC(sizeof (cmn_msg_t));
721*0Sstevel@tonic-gate 	new->prev = root;
722*0Sstevel@tonic-gate 	if (root != 0)
723*0Sstevel@tonic-gate 		root->next = new;
724*0Sstevel@tonic-gate 	strcpy(new->buf, head);
725*0Sstevel@tonic-gate 	new->len = strlen(head);
726*0Sstevel@tonic-gate 	if (path && env->current_device) {
727*0Sstevel@tonic-gate 		dpath = get_path(env, env->current_device);
728*0Sstevel@tonic-gate 		strcpy(new->buf+new->len, dpath);
729*0Sstevel@tonic-gate 		new->len += strlen(dpath);
730*0Sstevel@tonic-gate 		strncpy(new->buf+new->len++, ": ", 2);
731*0Sstevel@tonic-gate 		++new->len;
732*0Sstevel@tonic-gate 		free(dpath);
733*0Sstevel@tonic-gate 	}
734*0Sstevel@tonic-gate 	new->level = cmn_msg_level;
735*0Sstevel@tonic-gate 	new->next = NULL;
736*0Sstevel@tonic-gate 	root = new;
737*0Sstevel@tonic-gate }
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate /*
740*0Sstevel@tonic-gate  *	fc_cmn_type()
741*0Sstevel@tonic-gate  *
742*0Sstevel@tonic-gate  * Process cmn-type[ token.
743*0Sstevel@tonic-gate  *
744*0Sstevel@tonic-gate  * Invokes fc_cmn_start() to create a message containing blank
745*0Sstevel@tonic-gate  * header and no device path information.
746*0Sstevel@tonic-gate  */
747*0Sstevel@tonic-gate void
748*0Sstevel@tonic-gate fc_cmn_type(fcode_env_t *env)
749*0Sstevel@tonic-gate {
750*0Sstevel@tonic-gate 	cmn_msg_level = MSG_INFO;
751*0Sstevel@tonic-gate 	fc_cmn_start(env, "", 0);
752*0Sstevel@tonic-gate }
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate /*
755*0Sstevel@tonic-gate  *	fc_cmn_msg()
756*0Sstevel@tonic-gate  *
757*0Sstevel@tonic-gate  * Process cmn-msg[ token.
758*0Sstevel@tonic-gate  *
759*0Sstevel@tonic-gate  * Invokes fc_cmn_start() to create a message containing blank
760*0Sstevel@tonic-gate  * header but specifying device path information.
761*0Sstevel@tonic-gate  */
762*0Sstevel@tonic-gate void
763*0Sstevel@tonic-gate fc_cmn_msg(fcode_env_t *env)
764*0Sstevel@tonic-gate {
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 	cmn_msg_level = MSG_INFO;
767*0Sstevel@tonic-gate 	fc_cmn_start(env, "", 1);
768*0Sstevel@tonic-gate }
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate /*
771*0Sstevel@tonic-gate  *	fc_cmn_note()
772*0Sstevel@tonic-gate  *
773*0Sstevel@tonic-gate  * Process cmn-note[ token.
774*0Sstevel@tonic-gate  *
775*0Sstevel@tonic-gate  * Invokes fc_cmn_start() to create a message with NOTICE stamping in
776*0Sstevel@tonic-gate  * the header and specification of device path information.
777*0Sstevel@tonic-gate  */
778*0Sstevel@tonic-gate void
779*0Sstevel@tonic-gate fc_cmn_note(fcode_env_t *env)
780*0Sstevel@tonic-gate {
781*0Sstevel@tonic-gate 	cmn_msg_level = MSG_NOTE;
782*0Sstevel@tonic-gate 	fc_cmn_start(env, "NOTICE: ", 1);
783*0Sstevel@tonic-gate }
784*0Sstevel@tonic-gate 
785*0Sstevel@tonic-gate /*
786*0Sstevel@tonic-gate  *	fc_cmn_warn()
787*0Sstevel@tonic-gate  *
788*0Sstevel@tonic-gate  * Process cmn-warn[ token.
789*0Sstevel@tonic-gate  *
790*0Sstevel@tonic-gate  * Invokes fc_cmn_start() to create a message with WARNING stamping in
791*0Sstevel@tonic-gate  * the header and specification of device path information.
792*0Sstevel@tonic-gate  */
793*0Sstevel@tonic-gate void
794*0Sstevel@tonic-gate fc_cmn_warn(fcode_env_t *env)
795*0Sstevel@tonic-gate {
796*0Sstevel@tonic-gate 	cmn_msg_level = MSG_WARN;
797*0Sstevel@tonic-gate 	fc_cmn_start(env, "WARNING: ", 1);
798*0Sstevel@tonic-gate }
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate /*
801*0Sstevel@tonic-gate  *	fc_cmn_error()
802*0Sstevel@tonic-gate  *
803*0Sstevel@tonic-gate  * Process cmn-error[ token.
804*0Sstevel@tonic-gate  *
805*0Sstevel@tonic-gate  * Invokes fc_cmn_start() to create a message with ERROR stamping in
806*0Sstevel@tonic-gate  * the header and specification of device path information.
807*0Sstevel@tonic-gate  */
808*0Sstevel@tonic-gate void
809*0Sstevel@tonic-gate fc_cmn_error(fcode_env_t *env)
810*0Sstevel@tonic-gate {
811*0Sstevel@tonic-gate 	cmn_msg_level = MSG_ERROR;
812*0Sstevel@tonic-gate 	fc_cmn_start(env, "ERROR: ", 1);
813*0Sstevel@tonic-gate }
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate /*
816*0Sstevel@tonic-gate  *	fc_cmn_fatal()
817*0Sstevel@tonic-gate  *
818*0Sstevel@tonic-gate  * Process cmn-fatal[ token.
819*0Sstevel@tonic-gate  *
820*0Sstevel@tonic-gate  * Invokes fc_cmn_start() to create a message with FATAL stamping in
821*0Sstevel@tonic-gate  * the header and specification of device path information.
822*0Sstevel@tonic-gate  */
823*0Sstevel@tonic-gate void
824*0Sstevel@tonic-gate fc_cmn_fatal(fcode_env_t *env)
825*0Sstevel@tonic-gate {
826*0Sstevel@tonic-gate 	cmn_msg_level = MSG_FATAL;
827*0Sstevel@tonic-gate 	fc_cmn_start(env, "FATAL: ", 1);
828*0Sstevel@tonic-gate }
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate #pragma init(_init)
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate static void
833*0Sstevel@tonic-gate _init(void)
834*0Sstevel@tonic-gate {
835*0Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
836*0Sstevel@tonic-gate 	ASSERT(env);
837*0Sstevel@tonic-gate 	NOTICE;
838*0Sstevel@tonic-gate 
839*0Sstevel@tonic-gate 	ANSI(0x088, 0,		"span",			span);
840*0Sstevel@tonic-gate 	ANSI(0x08a, 0,		"expect",		expect);
841*0Sstevel@tonic-gate 
842*0Sstevel@tonic-gate 	ANSI(0x08d, 0,		"key?",			keyquestion);
843*0Sstevel@tonic-gate 	ANSI(0x08e, 0,		"key",			key);
844*0Sstevel@tonic-gate 	ANSI(0x08f, 0,		"emit",			emit);
845*0Sstevel@tonic-gate 	ANSI(0x090, 0,		"type",			type);
846*0Sstevel@tonic-gate 	ANSI(0x091, 0,		"(cr",			paren_cr);
847*0Sstevel@tonic-gate 	ANSI(0x092, 0,		"cr",			fc_crlf);
848*0Sstevel@tonic-gate 	ANSI(0x093, 0,		"#out",			fc_num_out);
849*0Sstevel@tonic-gate 	ANSI(0x094, 0,		"#line",		fc_num_line);
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	FCODE(0x125, 0,		"get-msecs",		do_get_msecs);
852*0Sstevel@tonic-gate 	FCODE(0x126, 0,		"ms",			do_ms);
853*0Sstevel@tonic-gate 
854*0Sstevel@tonic-gate 	FORTH(0,		"verbose-emit",		do_verbose_emit);
855*0Sstevel@tonic-gate 	FCODE(0x7e9, 0,		"cmn-fatal[",		fc_cmn_fatal);
856*0Sstevel@tonic-gate 	FCODE(0x7ea, 0,		"cmn-error[",		fc_cmn_error);
857*0Sstevel@tonic-gate 	FCODE(0x7eb, 0,		"cmn-warn[",		fc_cmn_warn);
858*0Sstevel@tonic-gate 	FCODE(0x7ec, 0,		"cmn-note[",		fc_cmn_note);
859*0Sstevel@tonic-gate 	FCODE(0x7ed, 0,		"cmn-type[",		fc_cmn_type);
860*0Sstevel@tonic-gate 	FCODE(0x7ee, 0,		"cmn-append",		fc_cmn_append);
861*0Sstevel@tonic-gate 	FCODE(0x7ef, 0,		"]cmn-end",		fc_cmn_end);
862*0Sstevel@tonic-gate 	FCODE(0x7f0, 0,		"cmn-msg[",		fc_cmn_msg);
863*0Sstevel@tonic-gate }
864