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