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