xref: /titanic_50/usr/src/lib/libcurses/screen/tparm.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 (c) 1996-1997 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1979 Regents of the University of California */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
38*7c478bd9Sstevel@tonic-gate #include	"curshdr.h"
39*7c478bd9Sstevel@tonic-gate #include	"term.h"
40*7c478bd9Sstevel@tonic-gate #include	<string.h>
41*7c478bd9Sstevel@tonic-gate #include	<setjmp.h>
42*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
43*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #ifndef	_CHCTRL
46*7c478bd9Sstevel@tonic-gate #define	_CHCTRL(c)	((c) & 037)
47*7c478bd9Sstevel@tonic-gate #endif	/* _CHCTRL */
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate char	*_branchto(char *, char);
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate /*
52*7c478bd9Sstevel@tonic-gate  * Routine to perform parameter substitution.
53*7c478bd9Sstevel@tonic-gate  * instring is a string containing printf type escapes.
54*7c478bd9Sstevel@tonic-gate  * The whole thing uses a stack, much like an HP 35.
55*7c478bd9Sstevel@tonic-gate  * The following escapes are defined for substituting row/column:
56*7c478bd9Sstevel@tonic-gate  *
57*7c478bd9Sstevel@tonic-gate  *	%[:[-+ #0]][0-9][.][0-9][dsoxX]
58*7c478bd9Sstevel@tonic-gate  *		print pop() as in printf(3), as defined in the local
59*7c478bd9Sstevel@tonic-gate  *		sprintf(3), except that a leading + or - must be preceded
60*7c478bd9Sstevel@tonic-gate  *		with a colon (:) to distinguish from the plus/minus operators.
61*7c478bd9Sstevel@tonic-gate  *
62*7c478bd9Sstevel@tonic-gate  *	%c	print pop() like %c in printf(3)
63*7c478bd9Sstevel@tonic-gate  *	%l	pop() a string address and push its length.
64*7c478bd9Sstevel@tonic-gate  *	%P[a-z] set dynamic variable a-z
65*7c478bd9Sstevel@tonic-gate  *	%g[a-z] get dynamic variable a-z
66*7c478bd9Sstevel@tonic-gate  *	%P[A-Z] set static variable A-Z
67*7c478bd9Sstevel@tonic-gate  *	%g[A-Z] get static variable A-Z
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  *	%p[1-0]	push ith parm
70*7c478bd9Sstevel@tonic-gate  *	%'c'	char constant c
71*7c478bd9Sstevel@tonic-gate  *	%{nn}	integer constant nn
72*7c478bd9Sstevel@tonic-gate  *
73*7c478bd9Sstevel@tonic-gate  *	%+ %- %* %/ %m		arithmetic (%m is mod): push(pop() op pop())
74*7c478bd9Sstevel@tonic-gate  *	%& %| %^		bit operations:		push(pop() op pop())
75*7c478bd9Sstevel@tonic-gate  *	%= %> %<		logical operations:	push(pop() op pop())
76*7c478bd9Sstevel@tonic-gate  *	%A %O			logical AND, OR		push(pop() op pop())
77*7c478bd9Sstevel@tonic-gate  *	%! %~			unary operations	push(op pop())
78*7c478bd9Sstevel@tonic-gate  *	%%			output %
79*7c478bd9Sstevel@tonic-gate  *	%? expr %t thenpart %e elsepart %;
80*7c478bd9Sstevel@tonic-gate  *				if-then-else, %e elsepart is optional.
81*7c478bd9Sstevel@tonic-gate  *				else-if's are possible ala Algol 68:
82*7c478bd9Sstevel@tonic-gate  *				%? c1 %t %e c2 %t %e c3 %t %e c4 %t %e %;
83*7c478bd9Sstevel@tonic-gate  *	% followed by anything else
84*7c478bd9Sstevel@tonic-gate  *				is not defined, it may output the character,
85*7c478bd9Sstevel@tonic-gate  *				and it may not. This is done so that further
86*7c478bd9Sstevel@tonic-gate  *				enhancements to the format capabilities may
87*7c478bd9Sstevel@tonic-gate  *				be made without worrying about being upwardly
88*7c478bd9Sstevel@tonic-gate  *				compatible from buggy code.
89*7c478bd9Sstevel@tonic-gate  *
90*7c478bd9Sstevel@tonic-gate  * all other characters are ``self-inserting''.  %% gets % output.
91*7c478bd9Sstevel@tonic-gate  *
92*7c478bd9Sstevel@tonic-gate  * The stack structure used here is based on an idea by Joseph Yao.
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate #define	MAX		10
96*7c478bd9Sstevel@tonic-gate #define	MEM_ALLOC_FAIL	1
97*7c478bd9Sstevel@tonic-gate #define	STACK_UNDERFLOW	2
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate typedef struct {
100*7c478bd9Sstevel@tonic-gate 	long	top;
101*7c478bd9Sstevel@tonic-gate 	int	stacksize;
102*7c478bd9Sstevel@tonic-gate 	long	*stack;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate }STACK;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate static jmp_buf env;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate static long
109*7c478bd9Sstevel@tonic-gate tops(STACK *st)
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	if (st->top < 0) {
113*7c478bd9Sstevel@tonic-gate 		longjmp(env, STACK_UNDERFLOW);
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 	return (st->stack[st->top]);
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate static void
119*7c478bd9Sstevel@tonic-gate push(STACK *st, long i)
120*7c478bd9Sstevel@tonic-gate {
121*7c478bd9Sstevel@tonic-gate 	if (st->top >= (st->stacksize - 1)) {
122*7c478bd9Sstevel@tonic-gate 		st->stacksize += MAX;
123*7c478bd9Sstevel@tonic-gate 		if ((st->stack = (void *)realloc(st->stack,
124*7c478bd9Sstevel@tonic-gate 		    (st->stacksize * sizeof (long)))) == NULL) {
125*7c478bd9Sstevel@tonic-gate 			longjmp(env, MEM_ALLOC_FAIL);
126*7c478bd9Sstevel@tonic-gate 		}
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 	st->stack[++st->top] = (i);
129*7c478bd9Sstevel@tonic-gate }
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate static long
132*7c478bd9Sstevel@tonic-gate pop(STACK *st)
133*7c478bd9Sstevel@tonic-gate {
134*7c478bd9Sstevel@tonic-gate 	if (st->top < 0) {
135*7c478bd9Sstevel@tonic-gate 		longjmp(env, STACK_UNDERFLOW);
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 	return (st->stack[st->top--]);
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate /* The following routine was added to make lint shut up about converting from
141*7c478bd9Sstevel@tonic-gate  * a long to a char *.  It is identical to the pop routine, except for the
142*7c478bd9Sstevel@tonic-gate  * cast on the return statement.
143*7c478bd9Sstevel@tonic-gate  */
144*7c478bd9Sstevel@tonic-gate static char *
145*7c478bd9Sstevel@tonic-gate pop_char_p(STACK *st)
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate 	if (st->top < 0) {
148*7c478bd9Sstevel@tonic-gate 		longjmp(env, STACK_UNDERFLOW);
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 	return ((char *)(st->stack[st->top--]));
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate static void
154*7c478bd9Sstevel@tonic-gate init_stack(STACK *st)
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	st->top = -1;
157*7c478bd9Sstevel@tonic-gate 	st->stacksize = MAX;
158*7c478bd9Sstevel@tonic-gate 	if ((st->stack = (void *)malloc(MAX * sizeof (long))) == NULL) {
159*7c478bd9Sstevel@tonic-gate 		longjmp(env, MEM_ALLOC_FAIL);
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate static void
164*7c478bd9Sstevel@tonic-gate free_stack(STACK *st)
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate 	free(st->stack);
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate char	*
171*7c478bd9Sstevel@tonic-gate tparm_p0(char *instring)
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	long	p[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	return (tparm(instring, p[0], p[1], p[2], p[3], p[4], p[5], p[6],
176*7c478bd9Sstevel@tonic-gate 			p[7], p[8]));
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate char	*
180*7c478bd9Sstevel@tonic-gate tparm_p1(char *instring, long l1)
181*7c478bd9Sstevel@tonic-gate {
182*7c478bd9Sstevel@tonic-gate 	long	p[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	p[0] = l1;
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	return (tparm(instring, p[0], p[1], p[2], p[3], p[4], p[5], p[6],
187*7c478bd9Sstevel@tonic-gate 			p[7], p[8]));
188*7c478bd9Sstevel@tonic-gate }
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate char	*
191*7c478bd9Sstevel@tonic-gate tparm_p2(char *instring, long l1, long l2)
192*7c478bd9Sstevel@tonic-gate {
193*7c478bd9Sstevel@tonic-gate 	long	p[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	p[0] = l1;
196*7c478bd9Sstevel@tonic-gate 	p[1] = l2;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	return (tparm(instring, p[0], p[1], p[2], p[3], p[4], p[5], p[6],
199*7c478bd9Sstevel@tonic-gate 			p[7], p[8]));
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate char	*
203*7c478bd9Sstevel@tonic-gate tparm_p3(char *instring, long l1, long l2, long l3)
204*7c478bd9Sstevel@tonic-gate {
205*7c478bd9Sstevel@tonic-gate 	long	p[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	p[0] = l1;
208*7c478bd9Sstevel@tonic-gate 	p[1] = l2;
209*7c478bd9Sstevel@tonic-gate 	p[2] = l3;
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	return (tparm(instring, p[0], p[1], p[2], p[3], p[4], p[5], p[6],
212*7c478bd9Sstevel@tonic-gate 			p[7], p[8]));
213*7c478bd9Sstevel@tonic-gate }
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate char	*
216*7c478bd9Sstevel@tonic-gate tparm_p4(char *instring, long l1, long l2, long l3, long l4)
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate 	long	p[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	p[0] = l1;
221*7c478bd9Sstevel@tonic-gate 	p[1] = l2;
222*7c478bd9Sstevel@tonic-gate 	p[2] = l3;
223*7c478bd9Sstevel@tonic-gate 	p[3] = l4;
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	return (tparm(instring, p[0], p[1], p[2], p[3], p[4], p[5], p[6],
226*7c478bd9Sstevel@tonic-gate 			p[7], p[8]));
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate char	*
230*7c478bd9Sstevel@tonic-gate tparm_p7(char *instring, long l1, long l2, long l3, long l4, long l5, long l6,
231*7c478bd9Sstevel@tonic-gate 	long l7)
232*7c478bd9Sstevel@tonic-gate {
233*7c478bd9Sstevel@tonic-gate 	long	p[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 	p[0] = l1;
236*7c478bd9Sstevel@tonic-gate 	p[1] = l2;
237*7c478bd9Sstevel@tonic-gate 	p[2] = l3;
238*7c478bd9Sstevel@tonic-gate 	p[3] = l4;
239*7c478bd9Sstevel@tonic-gate 	p[4] = l5;
240*7c478bd9Sstevel@tonic-gate 	p[5] = l6;
241*7c478bd9Sstevel@tonic-gate 	p[6] = l7;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	return (tparm(instring, p[0], p[1], p[2], p[3], p[4], p[5], p[6],
244*7c478bd9Sstevel@tonic-gate 			p[7], p[8]));
245*7c478bd9Sstevel@tonic-gate }
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate /* VARARGS */
248*7c478bd9Sstevel@tonic-gate char	*
249*7c478bd9Sstevel@tonic-gate tparm(char *instring, long fp1, long fp2, long p3, long p4,
250*7c478bd9Sstevel@tonic-gate 	long p5, long p6, long p7, long p8, long p9)
251*7c478bd9Sstevel@tonic-gate {
252*7c478bd9Sstevel@tonic-gate 	static	char	result[512];
253*7c478bd9Sstevel@tonic-gate 	static	char	added[100];
254*7c478bd9Sstevel@tonic-gate 	long		vars[26];
255*7c478bd9Sstevel@tonic-gate 	STACK		stk;
256*7c478bd9Sstevel@tonic-gate 	char		*cp = instring;
257*7c478bd9Sstevel@tonic-gate 	char		*outp = result;
258*7c478bd9Sstevel@tonic-gate 	char		c;
259*7c478bd9Sstevel@tonic-gate 	long		op;
260*7c478bd9Sstevel@tonic-gate 	long		op2;
261*7c478bd9Sstevel@tonic-gate 	int		sign;
262*7c478bd9Sstevel@tonic-gate 	int		onrow = 0;
263*7c478bd9Sstevel@tonic-gate 	long		p1 = fp1, p2 = fp2;  /* copy in case < 2 actual parms */
264*7c478bd9Sstevel@tonic-gate 	char		*xp;
265*7c478bd9Sstevel@tonic-gate 	char		formatbuffer[100];
266*7c478bd9Sstevel@tonic-gate 	char		*format;
267*7c478bd9Sstevel@tonic-gate 	int		looping;
268*7c478bd9Sstevel@tonic-gate 	short		*regs = cur_term->_regs;
269*7c478bd9Sstevel@tonic-gate 	int		val;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	if ((val = setjmp(env)) != 0) {
273*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
274*7c478bd9Sstevel@tonic-gate 		switch (val) {
275*7c478bd9Sstevel@tonic-gate 			case MEM_ALLOC_FAIL:
276*7c478bd9Sstevel@tonic-gate 				fprintf(outf, "TPARM: Memory allocation"
277*7c478bd9Sstevel@tonic-gate 				    " failure.");
278*7c478bd9Sstevel@tonic-gate 				break;
279*7c478bd9Sstevel@tonic-gate 			case STACK_UNDERFLOW:
280*7c478bd9Sstevel@tonic-gate 				fprintf(outf, "TPARM: Stack underflow.");
281*7c478bd9Sstevel@tonic-gate 				break;
282*7c478bd9Sstevel@tonic-gate 		}
283*7c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 		if (val == STACK_UNDERFLOW)
286*7c478bd9Sstevel@tonic-gate 			free_stack(&stk);
287*7c478bd9Sstevel@tonic-gate 		return (NULL);
288*7c478bd9Sstevel@tonic-gate 	}
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	init_stack(&stk);
291*7c478bd9Sstevel@tonic-gate 	push(&stk, 0);
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	if (instring == 0) {
294*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
295*7c478bd9Sstevel@tonic-gate 		if (outf)
296*7c478bd9Sstevel@tonic-gate 			fprintf(outf, "TPARM: null arg\n");
297*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
298*7c478bd9Sstevel@tonic-gate 		free_stack(&stk);
299*7c478bd9Sstevel@tonic-gate 		return (NULL);
300*7c478bd9Sstevel@tonic-gate 	}
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 	added[0] = 0;
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 	while ((c = *cp++) != 0) {
305*7c478bd9Sstevel@tonic-gate 		if (c != '%') {
306*7c478bd9Sstevel@tonic-gate 			*outp++ = c;
307*7c478bd9Sstevel@tonic-gate 			continue;
308*7c478bd9Sstevel@tonic-gate 		}
309*7c478bd9Sstevel@tonic-gate 		op = tops(&stk);
310*7c478bd9Sstevel@tonic-gate 		switch (c = *cp++) {
311*7c478bd9Sstevel@tonic-gate 			/* PRINTING CASES */
312*7c478bd9Sstevel@tonic-gate 			case ':':
313*7c478bd9Sstevel@tonic-gate 			case ' ':
314*7c478bd9Sstevel@tonic-gate 			case '#':
315*7c478bd9Sstevel@tonic-gate 			case '0':
316*7c478bd9Sstevel@tonic-gate 			case '1':
317*7c478bd9Sstevel@tonic-gate 			case '2':
318*7c478bd9Sstevel@tonic-gate 			case '3':
319*7c478bd9Sstevel@tonic-gate 			case '4':
320*7c478bd9Sstevel@tonic-gate 			case '5':
321*7c478bd9Sstevel@tonic-gate 			case '6':
322*7c478bd9Sstevel@tonic-gate 			case '7':
323*7c478bd9Sstevel@tonic-gate 			case '8':
324*7c478bd9Sstevel@tonic-gate 			case '9':
325*7c478bd9Sstevel@tonic-gate 			case '.':
326*7c478bd9Sstevel@tonic-gate 			case 'd':
327*7c478bd9Sstevel@tonic-gate 			case 's':
328*7c478bd9Sstevel@tonic-gate 			case 'o':
329*7c478bd9Sstevel@tonic-gate 			case 'x':
330*7c478bd9Sstevel@tonic-gate 			case 'X':
331*7c478bd9Sstevel@tonic-gate 				format = formatbuffer;
332*7c478bd9Sstevel@tonic-gate 				*format++ = '%';
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 			/* leading ':' to allow +/- in format */
335*7c478bd9Sstevel@tonic-gate 			if (c == ':')
336*7c478bd9Sstevel@tonic-gate 				c = *cp++;
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 			/* take care of flags, width and precision */
339*7c478bd9Sstevel@tonic-gate 			looping = 1;
340*7c478bd9Sstevel@tonic-gate 			while (c && looping)
341*7c478bd9Sstevel@tonic-gate 				switch (c) {
342*7c478bd9Sstevel@tonic-gate 					case '-':
343*7c478bd9Sstevel@tonic-gate 					case '+':
344*7c478bd9Sstevel@tonic-gate 					case ' ':
345*7c478bd9Sstevel@tonic-gate 					case '#':
346*7c478bd9Sstevel@tonic-gate 					case '0':
347*7c478bd9Sstevel@tonic-gate 					case '1':
348*7c478bd9Sstevel@tonic-gate 					case '2':
349*7c478bd9Sstevel@tonic-gate 					case '3':
350*7c478bd9Sstevel@tonic-gate 					case '4':
351*7c478bd9Sstevel@tonic-gate 					case '5':
352*7c478bd9Sstevel@tonic-gate 					case '6':
353*7c478bd9Sstevel@tonic-gate 					case '7':
354*7c478bd9Sstevel@tonic-gate 					case '8':
355*7c478bd9Sstevel@tonic-gate 					case '9':
356*7c478bd9Sstevel@tonic-gate 					case '.':
357*7c478bd9Sstevel@tonic-gate 						*format++ = c;
358*7c478bd9Sstevel@tonic-gate 						c = *cp++;
359*7c478bd9Sstevel@tonic-gate 						break;
360*7c478bd9Sstevel@tonic-gate 					default:
361*7c478bd9Sstevel@tonic-gate 						looping = 0;
362*7c478bd9Sstevel@tonic-gate 				}
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 			/* add in the conversion type */
365*7c478bd9Sstevel@tonic-gate 			switch (c) {
366*7c478bd9Sstevel@tonic-gate 				case 'd':
367*7c478bd9Sstevel@tonic-gate 				case 's':
368*7c478bd9Sstevel@tonic-gate 				case 'o':
369*7c478bd9Sstevel@tonic-gate 				case 'x':
370*7c478bd9Sstevel@tonic-gate 				case 'X':
371*7c478bd9Sstevel@tonic-gate 					*format++ = c;
372*7c478bd9Sstevel@tonic-gate 					break;
373*7c478bd9Sstevel@tonic-gate 				default:
374*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
375*7c478bd9Sstevel@tonic-gate 				if (outf)
376*7c478bd9Sstevel@tonic-gate 					fprintf(outf, "TPARM: invalid "
377*7c478bd9Sstevel@tonic-gate 					    "conversion type\n");
378*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
379*7c478bd9Sstevel@tonic-gate 				free_stack(&stk);
380*7c478bd9Sstevel@tonic-gate 				return (NULL);
381*7c478bd9Sstevel@tonic-gate 			}
382*7c478bd9Sstevel@tonic-gate 			*format = '\0';
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 			/*
385*7c478bd9Sstevel@tonic-gate 			 * Pass off the dirty work to sprintf.
386*7c478bd9Sstevel@tonic-gate 			 * It's debatable whether we should just pull in
387*7c478bd9Sstevel@tonic-gate 			 * the appropriate code here. I decided not to for
388*7c478bd9Sstevel@tonic-gate 			 * now.
389*7c478bd9Sstevel@tonic-gate 			 */
390*7c478bd9Sstevel@tonic-gate 			if (c == 's')
391*7c478bd9Sstevel@tonic-gate 				(void) sprintf(outp, formatbuffer,
392*7c478bd9Sstevel@tonic-gate 				    (char *) op);
393*7c478bd9Sstevel@tonic-gate 			else
394*7c478bd9Sstevel@tonic-gate 				(void) sprintf(outp, formatbuffer, op);
395*7c478bd9Sstevel@tonic-gate 			/*
396*7c478bd9Sstevel@tonic-gate 			 * Advance outp past what sprintf just did.
397*7c478bd9Sstevel@tonic-gate 			 * sprintf returns an indication of its length on some
398*7c478bd9Sstevel@tonic-gate 			 * systems, others the first char, and there's
399*7c478bd9Sstevel@tonic-gate 			 * no easy way to tell which. The Sys V on
400*7c478bd9Sstevel@tonic-gate 			 * BSD emulations are particularly confusing.
401*7c478bd9Sstevel@tonic-gate 			 */
402*7c478bd9Sstevel@tonic-gate 				while (*outp)
403*7c478bd9Sstevel@tonic-gate 				    outp++;
404*7c478bd9Sstevel@tonic-gate 				(void) pop(&stk);
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate 				continue;
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 			case 'c':
409*7c478bd9Sstevel@tonic-gate 			/*
410*7c478bd9Sstevel@tonic-gate 			 * This code is worth scratching your head at for a
411*7c478bd9Sstevel@tonic-gate 			 * while.  The idea is that various weird things can
412*7c478bd9Sstevel@tonic-gate 			 * happen to nulls, EOT's, tabs, and newlines by the
413*7c478bd9Sstevel@tonic-gate 			 * tty driver, arpanet, and so on, so we don't send
414*7c478bd9Sstevel@tonic-gate 			 * them if we can help it.  So we instead alter the
415*7c478bd9Sstevel@tonic-gate 			 * place being addessed and then move the cursor
416*7c478bd9Sstevel@tonic-gate 			 * locally using UP or RIGHT.
417*7c478bd9Sstevel@tonic-gate 			 *
418*7c478bd9Sstevel@tonic-gate 			 * This is a kludge, clearly.  It loses if the
419*7c478bd9Sstevel@tonic-gate 			 * parameterized string isn't addressing the cursor
420*7c478bd9Sstevel@tonic-gate 			 * (but hopefully that is all that %c terminals do
421*7c478bd9Sstevel@tonic-gate 			 * with parms).  Also, since tab and newline happen
422*7c478bd9Sstevel@tonic-gate 			 * to be next to each other in ASCII, if tab were
423*7c478bd9Sstevel@tonic-gate 			 * included a loop would be needed.  Finally, note
424*7c478bd9Sstevel@tonic-gate 			 * that lots of other processing is done here, so
425*7c478bd9Sstevel@tonic-gate 			 * this hack won't always work (e.g. the Ann Arbor
426*7c478bd9Sstevel@tonic-gate 			 * 4080, which uses %B and then %c.)
427*7c478bd9Sstevel@tonic-gate 			 */
428*7c478bd9Sstevel@tonic-gate 				switch (op) {
429*7c478bd9Sstevel@tonic-gate 				/*
430*7c478bd9Sstevel@tonic-gate 				* Null.  Problem is that our
431*7c478bd9Sstevel@tonic-gate 				* output is, by convention, null terminated.
432*7c478bd9Sstevel@tonic-gate 				*/
433*7c478bd9Sstevel@tonic-gate 					case 0:
434*7c478bd9Sstevel@tonic-gate 						op = 0200; /* Parity should */
435*7c478bd9Sstevel@tonic-gate 							/* be ignored. */
436*7c478bd9Sstevel@tonic-gate 						break;
437*7c478bd9Sstevel@tonic-gate 				/*
438*7c478bd9Sstevel@tonic-gate 				* Control D.  Problem is that certain very
439*7c478bd9Sstevel@tonic-gate 				* ancient hardware hangs up on this, so the
440*7c478bd9Sstevel@tonic-gate 				* current(!) UNIX tty driver doesn't xmit
441*7c478bd9Sstevel@tonic-gate 				* control D's.
442*7c478bd9Sstevel@tonic-gate 				*/
443*7c478bd9Sstevel@tonic-gate 					case _CHCTRL('d'):
444*7c478bd9Sstevel@tonic-gate 				/*
445*7c478bd9Sstevel@tonic-gate 				* Newline.  Problem is that UNIX will expand
446*7c478bd9Sstevel@tonic-gate 				* this to CRLF.
447*7c478bd9Sstevel@tonic-gate 				*/
448*7c478bd9Sstevel@tonic-gate 					case '\n':
449*7c478bd9Sstevel@tonic-gate 						xp = (onrow ? cursor_down :
450*7c478bd9Sstevel@tonic-gate 						    cursor_right);
451*7c478bd9Sstevel@tonic-gate 					if (onrow && xp && op < lines-1 &&
452*7c478bd9Sstevel@tonic-gate 					    cursor_up) {
453*7c478bd9Sstevel@tonic-gate 						op += 2;
454*7c478bd9Sstevel@tonic-gate 						xp = cursor_up;
455*7c478bd9Sstevel@tonic-gate 					}
456*7c478bd9Sstevel@tonic-gate 					if (xp && instring ==
457*7c478bd9Sstevel@tonic-gate 					    cursor_address) {
458*7c478bd9Sstevel@tonic-gate 						(void) strcat(added, xp);
459*7c478bd9Sstevel@tonic-gate 						op--;
460*7c478bd9Sstevel@tonic-gate 					}
461*7c478bd9Sstevel@tonic-gate 					break;
462*7c478bd9Sstevel@tonic-gate 				/*
463*7c478bd9Sstevel@tonic-gate 				 * Tab used to be in this group too,
464*7c478bd9Sstevel@tonic-gate 				 * because UNIX might expand it to blanks.
465*7c478bd9Sstevel@tonic-gate 				 * We now require that this tab mode be turned
466*7c478bd9Sstevel@tonic-gate 				 * off by any program using this routine,
467*7c478bd9Sstevel@tonic-gate 				 * or using termcap in general, since some
468*7c478bd9Sstevel@tonic-gate 				 * terminals use tab for other stuff, like
469*7c478bd9Sstevel@tonic-gate 				 * nondestructive space.  (Filters like ul
470*7c478bd9Sstevel@tonic-gate 				 * or vcrt will lose, since they can't stty.)
471*7c478bd9Sstevel@tonic-gate 				 * Tab was taken out to get the Ann Arbor
472*7c478bd9Sstevel@tonic-gate 				 * 4080 to work.
473*7c478bd9Sstevel@tonic-gate 				 */
474*7c478bd9Sstevel@tonic-gate 				}
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 				/* LINTED */
477*7c478bd9Sstevel@tonic-gate 				*outp++ = (char)op;
478*7c478bd9Sstevel@tonic-gate 				(void) pop(&stk);
479*7c478bd9Sstevel@tonic-gate 				break;
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 			case 'l':
482*7c478bd9Sstevel@tonic-gate 				xp = pop_char_p(&stk);
483*7c478bd9Sstevel@tonic-gate 				push(&stk, strlen(xp));
484*7c478bd9Sstevel@tonic-gate 				break;
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 			case '%':
487*7c478bd9Sstevel@tonic-gate 				*outp++ = c;
488*7c478bd9Sstevel@tonic-gate 				break;
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 			/*
491*7c478bd9Sstevel@tonic-gate 			* %i: shorthand for increment first two parms.
492*7c478bd9Sstevel@tonic-gate 			* Useful for terminals that start numbering from
493*7c478bd9Sstevel@tonic-gate 			* one instead of zero(like ANSI terminals).
494*7c478bd9Sstevel@tonic-gate 			*/
495*7c478bd9Sstevel@tonic-gate 			case 'i':
496*7c478bd9Sstevel@tonic-gate 				p1++;
497*7c478bd9Sstevel@tonic-gate 				p2++;
498*7c478bd9Sstevel@tonic-gate 				break;
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 			/* %pi: push the ith parameter */
501*7c478bd9Sstevel@tonic-gate 			case 'p':
502*7c478bd9Sstevel@tonic-gate 				switch (c = *cp++) {
503*7c478bd9Sstevel@tonic-gate 					case '1':
504*7c478bd9Sstevel@tonic-gate 						push(&stk, p1);
505*7c478bd9Sstevel@tonic-gate 						break;
506*7c478bd9Sstevel@tonic-gate 					case '2':
507*7c478bd9Sstevel@tonic-gate 						push(&stk, p2);
508*7c478bd9Sstevel@tonic-gate 						break;
509*7c478bd9Sstevel@tonic-gate 					case '3':
510*7c478bd9Sstevel@tonic-gate 						push(&stk, p3);
511*7c478bd9Sstevel@tonic-gate 						break;
512*7c478bd9Sstevel@tonic-gate 					case '4':
513*7c478bd9Sstevel@tonic-gate 						push(&stk, p4);
514*7c478bd9Sstevel@tonic-gate 						break;
515*7c478bd9Sstevel@tonic-gate 					case '5':
516*7c478bd9Sstevel@tonic-gate 						push(&stk, p5);
517*7c478bd9Sstevel@tonic-gate 						break;
518*7c478bd9Sstevel@tonic-gate 					case '6':
519*7c478bd9Sstevel@tonic-gate 						push(&stk, p6);
520*7c478bd9Sstevel@tonic-gate 						break;
521*7c478bd9Sstevel@tonic-gate 					case '7':
522*7c478bd9Sstevel@tonic-gate 						push(&stk, p7);
523*7c478bd9Sstevel@tonic-gate 						break;
524*7c478bd9Sstevel@tonic-gate 					case '8':
525*7c478bd9Sstevel@tonic-gate 						push(&stk, p8);
526*7c478bd9Sstevel@tonic-gate 						break;
527*7c478bd9Sstevel@tonic-gate 					case '9':
528*7c478bd9Sstevel@tonic-gate 						push(&stk, p9);
529*7c478bd9Sstevel@tonic-gate 						break;
530*7c478bd9Sstevel@tonic-gate 					default:
531*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
532*7c478bd9Sstevel@tonic-gate 						if (outf)
533*7c478bd9Sstevel@tonic-gate 							fprintf(outf, "TPARM:"
534*7c478bd9Sstevel@tonic-gate 							    " bad parm"
535*7c478bd9Sstevel@tonic-gate 							    " number\n");
536*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
537*7c478bd9Sstevel@tonic-gate 						free_stack(&stk);
538*7c478bd9Sstevel@tonic-gate 						return (NULL);
539*7c478bd9Sstevel@tonic-gate 				}
540*7c478bd9Sstevel@tonic-gate 			onrow = (c == '1');
541*7c478bd9Sstevel@tonic-gate 			break;
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 			/* %Pi: pop from stack into variable i (a-z) */
544*7c478bd9Sstevel@tonic-gate 			case 'P':
545*7c478bd9Sstevel@tonic-gate 				if (*cp >= 'a' && *cp <= 'z') {
546*7c478bd9Sstevel@tonic-gate 					vars[*cp++ - 'a'] = pop(&stk);
547*7c478bd9Sstevel@tonic-gate 				} else {
548*7c478bd9Sstevel@tonic-gate 					if (*cp >= 'A' && *cp <= 'Z') {
549*7c478bd9Sstevel@tonic-gate 						regs[*cp++ - 'A'] =
550*7c478bd9Sstevel@tonic-gate 							/* LINTED */
551*7c478bd9Sstevel@tonic-gate 							(short) pop(&stk);
552*7c478bd9Sstevel@tonic-gate 					}
553*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
554*7c478bd9Sstevel@tonic-gate 					else if (outf) {
555*7c478bd9Sstevel@tonic-gate 						fprintf(outf, "TPARM: bad"
556*7c478bd9Sstevel@tonic-gate 						    " register name\n");
557*7c478bd9Sstevel@tonic-gate 					}
558*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
559*7c478bd9Sstevel@tonic-gate 				}
560*7c478bd9Sstevel@tonic-gate 				break;
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 			/* %gi: push variable i (a-z) */
563*7c478bd9Sstevel@tonic-gate 			case 'g':
564*7c478bd9Sstevel@tonic-gate 				if (*cp >= 'a' && *cp <= 'z') {
565*7c478bd9Sstevel@tonic-gate 					push(&stk, vars[*cp++ - 'a']);
566*7c478bd9Sstevel@tonic-gate 				} else {
567*7c478bd9Sstevel@tonic-gate 					if (*cp >= 'A' && *cp <= 'Z') {
568*7c478bd9Sstevel@tonic-gate 						push(&stk, regs[*cp++ - 'A']);
569*7c478bd9Sstevel@tonic-gate 					}
570*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
571*7c478bd9Sstevel@tonic-gate 					else if (outf) {
572*7c478bd9Sstevel@tonic-gate 						fprintf(outf, "TPARM: bad"
573*7c478bd9Sstevel@tonic-gate 						    " register name\n");
574*7c478bd9Sstevel@tonic-gate 
575*7c478bd9Sstevel@tonic-gate 					}
576*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
577*7c478bd9Sstevel@tonic-gate 				}
578*7c478bd9Sstevel@tonic-gate 				break;
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 			/* %'c' : character constant */
581*7c478bd9Sstevel@tonic-gate 			case '\'':
582*7c478bd9Sstevel@tonic-gate 				push(&stk, *cp++);
583*7c478bd9Sstevel@tonic-gate 				if (*cp++ != '\'') {
584*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
585*7c478bd9Sstevel@tonic-gate 					if (outf)
586*7c478bd9Sstevel@tonic-gate 						fprintf(outf, "TPARM: missing"
587*7c478bd9Sstevel@tonic-gate 						    " closing quote\n");
588*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
589*7c478bd9Sstevel@tonic-gate 					free_stack(&stk);
590*7c478bd9Sstevel@tonic-gate 					return (NULL);
591*7c478bd9Sstevel@tonic-gate 				}
592*7c478bd9Sstevel@tonic-gate 				break;
593*7c478bd9Sstevel@tonic-gate 
594*7c478bd9Sstevel@tonic-gate 			/* %{nn} : integer constant.  */
595*7c478bd9Sstevel@tonic-gate 			case '{':
596*7c478bd9Sstevel@tonic-gate 				op = 0;
597*7c478bd9Sstevel@tonic-gate 				sign = 1;
598*7c478bd9Sstevel@tonic-gate 				if (*cp == '-') {
599*7c478bd9Sstevel@tonic-gate 					sign = -1;
600*7c478bd9Sstevel@tonic-gate 					cp++;
601*7c478bd9Sstevel@tonic-gate 				} else
602*7c478bd9Sstevel@tonic-gate 					if (*cp == '+')
603*7c478bd9Sstevel@tonic-gate 						cp++;
604*7c478bd9Sstevel@tonic-gate 				while ((c = *cp++) >= '0' && c <= '9') {
605*7c478bd9Sstevel@tonic-gate 					op = 10 * op + c - '0';
606*7c478bd9Sstevel@tonic-gate 				}
607*7c478bd9Sstevel@tonic-gate 				if (c != '}') {
608*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
609*7c478bd9Sstevel@tonic-gate 					if (outf)
610*7c478bd9Sstevel@tonic-gate 						fprintf(outf, "TPARM: missing "
611*7c478bd9Sstevel@tonic-gate 						    "closing brace\n");
612*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
613*7c478bd9Sstevel@tonic-gate 					free_stack(&stk);
614*7c478bd9Sstevel@tonic-gate 					return (NULL);
615*7c478bd9Sstevel@tonic-gate 				}
616*7c478bd9Sstevel@tonic-gate 				push(&stk, (sign * op));
617*7c478bd9Sstevel@tonic-gate 				break;
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 			/* binary operators */
620*7c478bd9Sstevel@tonic-gate 			case '+':
621*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
622*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
623*7c478bd9Sstevel@tonic-gate 				push(&stk, (op + op2));
624*7c478bd9Sstevel@tonic-gate 				break;
625*7c478bd9Sstevel@tonic-gate 			case '-':
626*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
627*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
628*7c478bd9Sstevel@tonic-gate 				push(&stk, (op - op2));
629*7c478bd9Sstevel@tonic-gate 				break;
630*7c478bd9Sstevel@tonic-gate 			case '*':
631*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
632*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
633*7c478bd9Sstevel@tonic-gate 				push(&stk, (op * op2));
634*7c478bd9Sstevel@tonic-gate 				break;
635*7c478bd9Sstevel@tonic-gate 			case '/':
636*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
637*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
638*7c478bd9Sstevel@tonic-gate 				push(&stk, (op / op2));
639*7c478bd9Sstevel@tonic-gate 				break;
640*7c478bd9Sstevel@tonic-gate 			case 'm':
641*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
642*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
643*7c478bd9Sstevel@tonic-gate 				push(&stk, (op % op2));
644*7c478bd9Sstevel@tonic-gate 				break; /* %m: mod */
645*7c478bd9Sstevel@tonic-gate 			case '&':
646*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
647*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
648*7c478bd9Sstevel@tonic-gate 				push(&stk, (op & op2));
649*7c478bd9Sstevel@tonic-gate 				break;
650*7c478bd9Sstevel@tonic-gate 			case '|':
651*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
652*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
653*7c478bd9Sstevel@tonic-gate 				push(&stk, (op | op2));
654*7c478bd9Sstevel@tonic-gate 				break;
655*7c478bd9Sstevel@tonic-gate 			case '^':
656*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
657*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
658*7c478bd9Sstevel@tonic-gate 				push(&stk, (op ^ op2));
659*7c478bd9Sstevel@tonic-gate 				break;
660*7c478bd9Sstevel@tonic-gate 			case '=':
661*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
662*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
663*7c478bd9Sstevel@tonic-gate 				push(&stk, (op == op2));
664*7c478bd9Sstevel@tonic-gate 				break;
665*7c478bd9Sstevel@tonic-gate 			case '>':
666*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
667*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
668*7c478bd9Sstevel@tonic-gate 				push(&stk, (op > op2));
669*7c478bd9Sstevel@tonic-gate 				break;
670*7c478bd9Sstevel@tonic-gate 			case '<':
671*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
672*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
673*7c478bd9Sstevel@tonic-gate 				push(&stk, (op < op2));
674*7c478bd9Sstevel@tonic-gate 				break;
675*7c478bd9Sstevel@tonic-gate 			case 'A':
676*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
677*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
678*7c478bd9Sstevel@tonic-gate 				push(&stk, (op && op2));
679*7c478bd9Sstevel@tonic-gate 				break; /* AND */
680*7c478bd9Sstevel@tonic-gate 			case 'O':
681*7c478bd9Sstevel@tonic-gate 				op2 = pop(&stk);
682*7c478bd9Sstevel@tonic-gate 				op = pop(&stk);
683*7c478bd9Sstevel@tonic-gate 				push(&stk, (op || op2));
684*7c478bd9Sstevel@tonic-gate 				break; /* OR */
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 			/* Unary operators. */
687*7c478bd9Sstevel@tonic-gate 			case '!':
688*7c478bd9Sstevel@tonic-gate 				push(&stk, !pop(&stk));
689*7c478bd9Sstevel@tonic-gate 				break;
690*7c478bd9Sstevel@tonic-gate 			case '~':
691*7c478bd9Sstevel@tonic-gate 				push(&stk, ~pop(&stk));
692*7c478bd9Sstevel@tonic-gate 				break;
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 			/* Sorry, no unary minus, because minus is binary. */
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate 			/*
697*7c478bd9Sstevel@tonic-gate 			* If-then-else.  Implemented by a low level hack of
698*7c478bd9Sstevel@tonic-gate 			* skipping forward until the match is found, counting
699*7c478bd9Sstevel@tonic-gate 			* nested if-then-elses.
700*7c478bd9Sstevel@tonic-gate 			*/
701*7c478bd9Sstevel@tonic-gate 			case '?':	/* IF - just a marker */
702*7c478bd9Sstevel@tonic-gate 				break;
703*7c478bd9Sstevel@tonic-gate 
704*7c478bd9Sstevel@tonic-gate 			case 't':	/* THEN - branch if false */
705*7c478bd9Sstevel@tonic-gate 				if (!pop(&stk))
706*7c478bd9Sstevel@tonic-gate 					cp = _branchto(cp, 'e');
707*7c478bd9Sstevel@tonic-gate 					break;
708*7c478bd9Sstevel@tonic-gate 
709*7c478bd9Sstevel@tonic-gate 			case 'e':	/* ELSE - branch to ENDIF */
710*7c478bd9Sstevel@tonic-gate 				cp = _branchto(cp, ';');
711*7c478bd9Sstevel@tonic-gate 				break;
712*7c478bd9Sstevel@tonic-gate 
713*7c478bd9Sstevel@tonic-gate 			case ';':	/* ENDIF - just a marker */
714*7c478bd9Sstevel@tonic-gate 				break;
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 			default:
717*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
718*7c478bd9Sstevel@tonic-gate 				if (outf)
719*7c478bd9Sstevel@tonic-gate 					fprintf(outf, "TPARM: bad % "
720*7c478bd9Sstevel@tonic-gate 					    "sequence\n");
721*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
722*7c478bd9Sstevel@tonic-gate 				free_stack(&stk);
723*7c478bd9Sstevel@tonic-gate 				return (NULL);
724*7c478bd9Sstevel@tonic-gate 		}
725*7c478bd9Sstevel@tonic-gate 	}
726*7c478bd9Sstevel@tonic-gate 	(void) strcpy(outp, added);
727*7c478bd9Sstevel@tonic-gate 	free_stack(&stk);
728*7c478bd9Sstevel@tonic-gate 	return (result);
729*7c478bd9Sstevel@tonic-gate }
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate char	*
732*7c478bd9Sstevel@tonic-gate _branchto(register char *cp, char to)
733*7c478bd9Sstevel@tonic-gate {
734*7c478bd9Sstevel@tonic-gate 	register	int	level = 0;
735*7c478bd9Sstevel@tonic-gate 	register	char	c;
736*7c478bd9Sstevel@tonic-gate 
737*7c478bd9Sstevel@tonic-gate 	while (c = *cp++) {
738*7c478bd9Sstevel@tonic-gate 		if (c == '%') {
739*7c478bd9Sstevel@tonic-gate 			if ((c = *cp++) == to || c == ';') {
740*7c478bd9Sstevel@tonic-gate 				if (level == 0) {
741*7c478bd9Sstevel@tonic-gate 					return (cp);
742*7c478bd9Sstevel@tonic-gate 				}
743*7c478bd9Sstevel@tonic-gate 			}
744*7c478bd9Sstevel@tonic-gate 			if (c == '?')
745*7c478bd9Sstevel@tonic-gate 				level++;
746*7c478bd9Sstevel@tonic-gate 			if (c == ';')
747*7c478bd9Sstevel@tonic-gate 				level--;
748*7c478bd9Sstevel@tonic-gate 		}
749*7c478bd9Sstevel@tonic-gate 	}
750*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
751*7c478bd9Sstevel@tonic-gate 	if (outf)
752*7c478bd9Sstevel@tonic-gate 		fprintf(outf, "TPARM: no matching ENDIF");
753*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
754*7c478bd9Sstevel@tonic-gate 	return (NULL);
755*7c478bd9Sstevel@tonic-gate }
756