xref: /illumos-gate/usr/src/cmd/oawk/run.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d0983205SRoger A. Faulkner  * Common Development and Distribution License (the "License").
6d0983205SRoger A. Faulkner  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
26dc5a8425Srobbin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27dc5a8425Srobbin /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #define	DEBUG
307c478bd9Sstevel@tonic-gate #define	tempfree(a)	{if (istemp(a)) {xfree(a->sval); a->tval = 0; }}
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include	"awk.def"
337c478bd9Sstevel@tonic-gate #include	"math.h"
347c478bd9Sstevel@tonic-gate #include	"awk.h"
357c478bd9Sstevel@tonic-gate #include	"stdio.h"
367c478bd9Sstevel@tonic-gate #include "ctype.h"
377c478bd9Sstevel@tonic-gate #include "wctype.h"
387c478bd9Sstevel@tonic-gate #include "awktype.h"
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	RECSIZE BUFSIZ
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	FILENUM	10
447c478bd9Sstevel@tonic-gate struct
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	FILE *fp;
477c478bd9Sstevel@tonic-gate 	int type;
487c478bd9Sstevel@tonic-gate 	wchar_t *fname;
497c478bd9Sstevel@tonic-gate } files[FILENUM];
507c478bd9Sstevel@tonic-gate FILE *popen();
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate extern CELL *execute(), *nodetoobj(), *fieldel(), *dopa2(), *gettemp();
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	PA2NUM	29
557c478bd9Sstevel@tonic-gate int	pairstack[PA2NUM], paircnt;
567c478bd9Sstevel@tonic-gate NODE	*winner = NULL;
577c478bd9Sstevel@tonic-gate #define	MAXTMP	20
587c478bd9Sstevel@tonic-gate CELL	tmps[MAXTMP];
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static CELL	truecell	={ OBOOL, BTRUE, 0, 0, 0.0, NUM, 0 };
617c478bd9Sstevel@tonic-gate CELL	*true	= &truecell;
627c478bd9Sstevel@tonic-gate static CELL	falsecell	={ OBOOL, BFALSE, 0, 0, 0.0, NUM, 0 };
637c478bd9Sstevel@tonic-gate CELL	*false	= &falsecell;
647c478bd9Sstevel@tonic-gate static CELL	breakcell	={ OJUMP, JBREAK, 0, 0, 0.0, NUM, 0 };
657c478bd9Sstevel@tonic-gate CELL	*jbreak	= &breakcell;
667c478bd9Sstevel@tonic-gate static CELL	contcell	={ OJUMP, JCONT, 0, 0, 0.0, NUM, 0 };
677c478bd9Sstevel@tonic-gate CELL	*jcont	= &contcell;
687c478bd9Sstevel@tonic-gate static CELL	nextcell	={ OJUMP, JNEXT, 0, 0, 0.0, NUM, 0 };
697c478bd9Sstevel@tonic-gate CELL	*jnext	= &nextcell;
707c478bd9Sstevel@tonic-gate static CELL	exitcell	={ OJUMP, JEXIT, 0, 0, 0.0, NUM, 0 };
717c478bd9Sstevel@tonic-gate CELL	*jexit	= &exitcell;
727c478bd9Sstevel@tonic-gate static CELL	tempcell	={ OCELL, CTEMP, 0, 0, 0.0, NUM, 0 };
737c478bd9Sstevel@tonic-gate 
74dc5a8425Srobbin static void redirprint(wchar_t *s, int a, NODE *b);
75dc5a8425Srobbin 
76dc5a8425Srobbin void freesymtab(CELL *ap);
77dc5a8425Srobbin void fldbld(void);
78dc5a8425Srobbin 
79dc5a8425Srobbin void
run(NODE * a)80dc5a8425Srobbin run(NODE *a)
817c478bd9Sstevel@tonic-gate {
82dc5a8425Srobbin 	int i;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	execute(a);
857c478bd9Sstevel@tonic-gate 	/* Wait for children to complete if output to a pipe. */
867c478bd9Sstevel@tonic-gate 	for (i=0; i<FILENUM; i++)
877c478bd9Sstevel@tonic-gate 		if (files[i].fp && files[i].type == '|')
887c478bd9Sstevel@tonic-gate 			pclose(files[i].fp);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 
92dc5a8425Srobbin CELL *
execute(NODE * u)93dc5a8425Srobbin execute(NODE *u)
947c478bd9Sstevel@tonic-gate {
95dc5a8425Srobbin 	CELL *(*proc)();
96dc5a8425Srobbin 	CELL *x;
97dc5a8425Srobbin 	NODE *a;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (u == NULL)
1007c478bd9Sstevel@tonic-gate 		return (true);
1017c478bd9Sstevel@tonic-gate 	for (a = u; /* dummy */; a = a->nnext) {
1027c478bd9Sstevel@tonic-gate 		if (cantexec(a))
1037c478bd9Sstevel@tonic-gate 			return (nodetoobj(a));
1047c478bd9Sstevel@tonic-gate 		if (notlegal(a->nobj))
1057c478bd9Sstevel@tonic-gate 			error(FATAL, "illegal statement %o", a);
1067c478bd9Sstevel@tonic-gate 		proc = proctab[a->nobj-FIRSTTOKEN];
1077c478bd9Sstevel@tonic-gate 		x = (*proc)(a->narg, a->nobj);
1087c478bd9Sstevel@tonic-gate 		if (isfld(x))
1097c478bd9Sstevel@tonic-gate 			fldbld();
1107c478bd9Sstevel@tonic-gate 		if (isexpr(a))
1117c478bd9Sstevel@tonic-gate 			return (x);
1127c478bd9Sstevel@tonic-gate 		/* a statement, goto next statement */
1137c478bd9Sstevel@tonic-gate 		if (isjump(x))
1147c478bd9Sstevel@tonic-gate 			return (x);
1157c478bd9Sstevel@tonic-gate 		if (a->nnext == (NODE *)NULL)
1167c478bd9Sstevel@tonic-gate 			return (x);
1177c478bd9Sstevel@tonic-gate 		tempfree(x);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 
124dc5a8425Srobbin CELL *
program(NODE ** a,int n)125dc5a8425Srobbin program(NODE **a, int n)
1267c478bd9Sstevel@tonic-gate {
127dc5a8425Srobbin 	CELL *x;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (a[0] != NULL) {
1307c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
1317c478bd9Sstevel@tonic-gate 		if (isexit(x))
1327c478bd9Sstevel@tonic-gate 			return (true);
1337c478bd9Sstevel@tonic-gate 		if (isjump(x))
1347c478bd9Sstevel@tonic-gate 			error(FATAL, "unexpected break, continue or next");
1357c478bd9Sstevel@tonic-gate 		tempfree(x);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	while (getrec()) {
1387c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
1397c478bd9Sstevel@tonic-gate 		if (isexit(x)) {
1407c478bd9Sstevel@tonic-gate 			tempfree(x);
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 		tempfree(x);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 	if (a[2] != NULL) {
1467c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
1477c478bd9Sstevel@tonic-gate 		if (isbreak(x) || isnext(x) || iscont(x))
1487c478bd9Sstevel@tonic-gate 			error(FATAL, "unexpected break, continue or next");
1497c478bd9Sstevel@tonic-gate 		tempfree(x);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	return (true);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 
157dc5a8425Srobbin CELL *
getaline(void)158*23a1cceaSRoger A. Faulkner getaline(void)
1597c478bd9Sstevel@tonic-gate {
160dc5a8425Srobbin 	CELL *x;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	x = gettemp();
1637c478bd9Sstevel@tonic-gate 	setfval(x, (awkfloat) getrec());
1647c478bd9Sstevel@tonic-gate 	return (x);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 
170dc5a8425Srobbin CELL *
array(NODE ** a,int n)171dc5a8425Srobbin array(NODE **a, int n)
1727c478bd9Sstevel@tonic-gate {
173dc5a8425Srobbin 	CELL *x, *y;
1747c478bd9Sstevel@tonic-gate 	extern CELL *arrayel();
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
1777c478bd9Sstevel@tonic-gate 	y = arrayel(a[0], x);
1787c478bd9Sstevel@tonic-gate 	tempfree(x);
1797c478bd9Sstevel@tonic-gate 	return (y);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 
185dc5a8425Srobbin CELL *
arrayel(NODE * a,CELL * b)186dc5a8425Srobbin arrayel(NODE *a, CELL *b)
1877c478bd9Sstevel@tonic-gate {
188dc5a8425Srobbin 	wchar_t *s;
189dc5a8425Srobbin 	CELL *x;
190dc5a8425Srobbin 	int i;
191dc5a8425Srobbin 	CELL *y;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	s = getsval(b);
1947c478bd9Sstevel@tonic-gate 	x = (CELL *) a;
1957c478bd9Sstevel@tonic-gate 	if (!(x->tval&ARR)) {
1967c478bd9Sstevel@tonic-gate 		xfree(x->sval);
1977c478bd9Sstevel@tonic-gate 		x->tval &= ~STR;
1987c478bd9Sstevel@tonic-gate 		x->tval |= ARR;
1997c478bd9Sstevel@tonic-gate 		x->sval = (wchar_t *) makesymtab();
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	y = setsymtab(s, tostring(L_NULL), 0.0, STR|NUM, x->sval);
2027c478bd9Sstevel@tonic-gate 	y->ctype = OCELL;
2037c478bd9Sstevel@tonic-gate 	y->csub = CVAR;
2047c478bd9Sstevel@tonic-gate 	return (y);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
207dc5a8425Srobbin CELL *
matchop(NODE ** a,int n)208dc5a8425Srobbin matchop(NODE **a, int n)
2097c478bd9Sstevel@tonic-gate {
210dc5a8425Srobbin 	CELL *x;
211dc5a8425Srobbin 	wchar_t *s;
212dc5a8425Srobbin 	int i;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
2157c478bd9Sstevel@tonic-gate 	s = getsval(x);
2167c478bd9Sstevel@tonic-gate 	tempfree(x);
2177c478bd9Sstevel@tonic-gate 	i = match(a[1], s);
2187c478bd9Sstevel@tonic-gate 	if (n == MATCH && i == 1 || n == NOTMATCH && i == 0)
2197c478bd9Sstevel@tonic-gate 		return (true);
2207c478bd9Sstevel@tonic-gate 	else
2217c478bd9Sstevel@tonic-gate 		return (false);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 
227dc5a8425Srobbin CELL *
boolop(NODE ** a,int n)228dc5a8425Srobbin boolop(NODE **a, int n)
2297c478bd9Sstevel@tonic-gate {
230dc5a8425Srobbin 	CELL *x, *y;
231dc5a8425Srobbin 	int i;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
2377c478bd9Sstevel@tonic-gate 	i = istrue(x);
2387c478bd9Sstevel@tonic-gate 	tempfree(x);
2397c478bd9Sstevel@tonic-gate 	switch (n) {
2407c478bd9Sstevel@tonic-gate 	case BOR:
2417c478bd9Sstevel@tonic-gate 		if (i) return (true);
2427c478bd9Sstevel@tonic-gate 		y = execute(a[1]);
2437c478bd9Sstevel@tonic-gate 		i = istrue(y);
2447c478bd9Sstevel@tonic-gate 		tempfree(y);
2457c478bd9Sstevel@tonic-gate 		if (i) return (true);
2467c478bd9Sstevel@tonic-gate 		else return (false);
2477c478bd9Sstevel@tonic-gate 	case AND:
2487c478bd9Sstevel@tonic-gate 		if (!i) return (false);
2497c478bd9Sstevel@tonic-gate 		y = execute(a[1]);
2507c478bd9Sstevel@tonic-gate 		i = istrue(y);
2517c478bd9Sstevel@tonic-gate 		tempfree(y);
2527c478bd9Sstevel@tonic-gate 		if (i) return (true);
2537c478bd9Sstevel@tonic-gate 		else return (false);
2547c478bd9Sstevel@tonic-gate 	case NOT:
2557c478bd9Sstevel@tonic-gate 		if (i) return (false);
2567c478bd9Sstevel@tonic-gate 		else return (true);
2577c478bd9Sstevel@tonic-gate 	default:
2587c478bd9Sstevel@tonic-gate 		error(FATAL, "unknown boolean operator %d", n);
2597c478bd9Sstevel@tonic-gate 	}
260dc5a8425Srobbin 	return (false);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 
266dc5a8425Srobbin CELL *
relop(NODE ** a,int n)267dc5a8425Srobbin relop(NODE **a, int n)
2687c478bd9Sstevel@tonic-gate {
269dc5a8425Srobbin 	int i;
270dc5a8425Srobbin 	CELL *x, *y;
2717c478bd9Sstevel@tonic-gate 	awkfloat j;
272dc5a8425Srobbin 	wchar_t *xs, *ys;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
2787c478bd9Sstevel@tonic-gate 	y = execute(a[1]);
2797c478bd9Sstevel@tonic-gate 	if (x->tval&NUM && y->tval&NUM) {
2807c478bd9Sstevel@tonic-gate 		j = x->fval - y->fval;
2817c478bd9Sstevel@tonic-gate 		i = j<0? -1: (j>0? 1: 0);
2827c478bd9Sstevel@tonic-gate 	} else {
2837c478bd9Sstevel@tonic-gate 		xs = getsval(x);
2847c478bd9Sstevel@tonic-gate 		ys = getsval(y);
2857c478bd9Sstevel@tonic-gate 		if (xs && ys)
2867c478bd9Sstevel@tonic-gate 			i = wscoll(xs, ys);
2877c478bd9Sstevel@tonic-gate 		else
2887c478bd9Sstevel@tonic-gate 			return (false);
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 	tempfree(x);
2917c478bd9Sstevel@tonic-gate 	tempfree(y);
2927c478bd9Sstevel@tonic-gate 	switch (n) {
2937c478bd9Sstevel@tonic-gate 	case LT:	if (i<0) return (true);
2947c478bd9Sstevel@tonic-gate 			else return (false);
2957c478bd9Sstevel@tonic-gate 	case LE:	if (i<=0) return (true);
2967c478bd9Sstevel@tonic-gate 			else return (false);
2977c478bd9Sstevel@tonic-gate 	case NE:	if (i!=0) return (true);
2987c478bd9Sstevel@tonic-gate 			else return (false);
2997c478bd9Sstevel@tonic-gate 	case EQ:	if (i == 0) return (true);
3007c478bd9Sstevel@tonic-gate 			else return (false);
3017c478bd9Sstevel@tonic-gate 	case GE:	if (i>=0) return (true);
3027c478bd9Sstevel@tonic-gate 			else return (false);
3037c478bd9Sstevel@tonic-gate 	case GT:	if (i>0) return (true);
3047c478bd9Sstevel@tonic-gate 			else return (false);
3057c478bd9Sstevel@tonic-gate 	default:
3067c478bd9Sstevel@tonic-gate 		error(FATAL, "unknown relational operator %d", n);
3077c478bd9Sstevel@tonic-gate 	}
308dc5a8425Srobbin 	return (false);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 
318dc5a8425Srobbin CELL *
gettemp(void)319dc5a8425Srobbin gettemp(void)
3207c478bd9Sstevel@tonic-gate {
321dc5a8425Srobbin 	int i;
322dc5a8425Srobbin 	CELL *x;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	for (i=0; i<MAXTMP; i++)
3287c478bd9Sstevel@tonic-gate 		if (tmps[i].tval == 0)
3297c478bd9Sstevel@tonic-gate 			break;
3307c478bd9Sstevel@tonic-gate 	if (i == MAXTMP)
3317c478bd9Sstevel@tonic-gate 		error(FATAL, "out of temporaries in gettemp");
3327c478bd9Sstevel@tonic-gate 	tmps[i] = tempcell;
3337c478bd9Sstevel@tonic-gate 	x = &tmps[i];
3347c478bd9Sstevel@tonic-gate 	return (x);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 
340dc5a8425Srobbin CELL *
indirect(NODE ** a,int n)341dc5a8425Srobbin indirect(NODE **a, int n)
3427c478bd9Sstevel@tonic-gate {
343dc5a8425Srobbin 	CELL *x;
344dc5a8425Srobbin 	int m;
3457c478bd9Sstevel@tonic-gate 	CELL *fieldadr();
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
3487c478bd9Sstevel@tonic-gate 	m = getfval(x);
3497c478bd9Sstevel@tonic-gate 	tempfree(x);
3507c478bd9Sstevel@tonic-gate 	x = fieldadr(m);
3517c478bd9Sstevel@tonic-gate 	x->ctype = OCELL;
3527c478bd9Sstevel@tonic-gate 	x->csub = CFLD;
3537c478bd9Sstevel@tonic-gate 	return (x);
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 
359dc5a8425Srobbin CELL *
substr(NODE ** a,int nnn)360dc5a8425Srobbin substr(NODE **a, int nnn)
3617c478bd9Sstevel@tonic-gate {
362dc5a8425Srobbin 	int k, m, n;
363dc5a8425Srobbin 	wchar_t *s, temp;
364dc5a8425Srobbin 	CELL *x, *y;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	y = execute(a[0]);
3677c478bd9Sstevel@tonic-gate 	s = getsval(y);
3687c478bd9Sstevel@tonic-gate 	k = wslen(s) + 1;
3697c478bd9Sstevel@tonic-gate 	if (k <= 1) {
3707c478bd9Sstevel@tonic-gate 		x = gettemp();
3717c478bd9Sstevel@tonic-gate 		setsval(x, L_NULL);
3727c478bd9Sstevel@tonic-gate 		return (x);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
3757c478bd9Sstevel@tonic-gate 	m = getfval(x);
3767c478bd9Sstevel@tonic-gate 	if (m <= 0)
3777c478bd9Sstevel@tonic-gate 		m = 1;
3787c478bd9Sstevel@tonic-gate 	else if (m > k)
3797c478bd9Sstevel@tonic-gate 		m = k;
3807c478bd9Sstevel@tonic-gate 	tempfree(x);
3817c478bd9Sstevel@tonic-gate 	if (a[2] != 0) {
3827c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
3837c478bd9Sstevel@tonic-gate 		n = getfval(x);
3847c478bd9Sstevel@tonic-gate 		tempfree(x);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 	else
3877c478bd9Sstevel@tonic-gate 		n = k - 1;
3887c478bd9Sstevel@tonic-gate 	if (n < 0)
3897c478bd9Sstevel@tonic-gate 		n = 0;
3907c478bd9Sstevel@tonic-gate 	else if (n > k - m)
3917c478bd9Sstevel@tonic-gate 		n = k - m;
3927c478bd9Sstevel@tonic-gate 	dprintf("substr: m=%d, n=%d, s=%ws\n", m, n, s);
3937c478bd9Sstevel@tonic-gate 	x = gettemp();
3947c478bd9Sstevel@tonic-gate 	temp = s[n+m-1];
3957c478bd9Sstevel@tonic-gate 	s[n+m-1] = (wchar_t)0x0;
3967c478bd9Sstevel@tonic-gate 	setsval(x, s + m - 1);
3977c478bd9Sstevel@tonic-gate 	s[n+m-1] = temp;
3987c478bd9Sstevel@tonic-gate 	tempfree(y);
3997c478bd9Sstevel@tonic-gate 	return (x);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 
405dc5a8425Srobbin CELL *
sindex(NODE ** a,int nnn)406dc5a8425Srobbin sindex(NODE **a, int nnn)
4077c478bd9Sstevel@tonic-gate {
408dc5a8425Srobbin 	CELL *x;
409dc5a8425Srobbin 	wchar_t *s1, *s2, *p1, *p2, *q;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
4127c478bd9Sstevel@tonic-gate 	s1 = getsval(x);
4137c478bd9Sstevel@tonic-gate 	tempfree(x);
4147c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
4157c478bd9Sstevel@tonic-gate 	s2 = getsval(x);
4167c478bd9Sstevel@tonic-gate 	tempfree(x);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	x = gettemp();
4197c478bd9Sstevel@tonic-gate 	for (p1 = s1; *p1 != (wchar_t)0x0; p1++) {
4207c478bd9Sstevel@tonic-gate 		for (q=p1, p2=s2; *p2 != (wchar_t)0x0 && *q == *p2; q++, p2++)
4217c478bd9Sstevel@tonic-gate 			;
4227c478bd9Sstevel@tonic-gate 		if (*p2 == (wchar_t)0x0) {
4237c478bd9Sstevel@tonic-gate 			setfval(x, (awkfloat) (p1 - s1 + 1));	/* origin 1 */
4247c478bd9Sstevel@tonic-gate 			return (x);
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 	setfval(x, 0.0);
4287c478bd9Sstevel@tonic-gate 	return (x);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 
434dc5a8425Srobbin wchar_t *
format(wchar_t * s,NODE * a)435dc5a8425Srobbin format(wchar_t *s, NODE *a)
4367c478bd9Sstevel@tonic-gate {
437dc5a8425Srobbin 	wchar_t *buf, *ep, *str;
438dc5a8425Srobbin 	wchar_t *p;
439dc5a8425Srobbin 	char *t;
4407c478bd9Sstevel@tonic-gate 	wchar_t *os;
4417c478bd9Sstevel@tonic-gate 	wchar_t tbuf[2*RECSIZE];
4427c478bd9Sstevel@tonic-gate 	char fmt[200];
443dc5a8425Srobbin 	CELL *x;
4447c478bd9Sstevel@tonic-gate 	int flag = 0;
4457c478bd9Sstevel@tonic-gate 	awkfloat xf;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	os = s;
4487c478bd9Sstevel@tonic-gate 	p = buf= (wchar_t *)malloc(RECSIZE * sizeof (wchar_t));
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	if (p == NULL)
4517c478bd9Sstevel@tonic-gate 		error(FATAL, "out of space in format");
4527c478bd9Sstevel@tonic-gate 	ep = p + RECSIZE;
4537c478bd9Sstevel@tonic-gate 	while (*s) {
4547c478bd9Sstevel@tonic-gate 		if (*s != '%') {
4557c478bd9Sstevel@tonic-gate 			*p++ = *s++;
4567c478bd9Sstevel@tonic-gate 			continue;
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 		if (*(s+1) == '%') {
4597c478bd9Sstevel@tonic-gate 			*p++ = '%';
4607c478bd9Sstevel@tonic-gate 			s += 2;
4617c478bd9Sstevel@tonic-gate 			continue;
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 		for (t=fmt; *s != '\0'; s++)
4647c478bd9Sstevel@tonic-gate 		{
4657c478bd9Sstevel@tonic-gate 			if (*s == 's' || *s == 'c')
4667c478bd9Sstevel@tonic-gate 				*t++ = 'w';
4677c478bd9Sstevel@tonic-gate 			*t++ = *s;
4687c478bd9Sstevel@tonic-gate 			if (*s >= 'a' && *s <= 'z' && *s != 'l')
4697c478bd9Sstevel@tonic-gate 				break;
4707c478bd9Sstevel@tonic-gate 			if (*s == '*') {
4717c478bd9Sstevel@tonic-gate 				if (a == NULL) {
4727c478bd9Sstevel@tonic-gate 					error(FATAL,
4737c478bd9Sstevel@tonic-gate 			"not enough arguments in printf(%ws) or sprintf(%ws)",
4747c478bd9Sstevel@tonic-gate 					os, os);
4757c478bd9Sstevel@tonic-gate 				}
4767c478bd9Sstevel@tonic-gate 				x = execute(a);
4777c478bd9Sstevel@tonic-gate 				a = a->nnext;
4787c478bd9Sstevel@tonic-gate 				sprintf(t-1, "%d", (int) getfval(x));
4797c478bd9Sstevel@tonic-gate 				t = fmt + strlen(fmt);
4807c478bd9Sstevel@tonic-gate 				tempfree(x);
4817c478bd9Sstevel@tonic-gate 			}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 		*t = '\0';
4857c478bd9Sstevel@tonic-gate 		if (t >= fmt + sizeof (fmt))
4867c478bd9Sstevel@tonic-gate 			error(FATAL, "format item %.20ws... too long", os);
4877c478bd9Sstevel@tonic-gate 		switch (*s) {
4887c478bd9Sstevel@tonic-gate 		case 'f': case 'e': case 'g':
4897c478bd9Sstevel@tonic-gate 			flag = 1;
4907c478bd9Sstevel@tonic-gate 			break;
4917c478bd9Sstevel@tonic-gate 		case 'd':
4927c478bd9Sstevel@tonic-gate 			flag = 2;
4937c478bd9Sstevel@tonic-gate 			if (*(s-1) == 'l') break;
4947c478bd9Sstevel@tonic-gate 			*(t-1) = 'l';
4957c478bd9Sstevel@tonic-gate 			*t = 'd';
4967c478bd9Sstevel@tonic-gate 			*++t = '\0';
4977c478bd9Sstevel@tonic-gate 			break;
4987c478bd9Sstevel@tonic-gate 		case 'o': case 'x':
4997c478bd9Sstevel@tonic-gate 			flag = *(s-1) == 'l' ? 2 : 3;
5007c478bd9Sstevel@tonic-gate 			break;
5017c478bd9Sstevel@tonic-gate 		case 'c':
5027c478bd9Sstevel@tonic-gate 			flag = 3;
5037c478bd9Sstevel@tonic-gate 			break;
5047c478bd9Sstevel@tonic-gate 		case 's':
5057c478bd9Sstevel@tonic-gate 			flag = 4;
5067c478bd9Sstevel@tonic-gate 			break;
5077c478bd9Sstevel@tonic-gate 		default:
5087c478bd9Sstevel@tonic-gate 			flag = 0;
5097c478bd9Sstevel@tonic-gate 			break;
5107c478bd9Sstevel@tonic-gate 		}
5117c478bd9Sstevel@tonic-gate 		if (flag == 0) {
5127c478bd9Sstevel@tonic-gate 			wsprintf(p, "%s", fmt);
5137c478bd9Sstevel@tonic-gate 			p += wslen(p);
5147c478bd9Sstevel@tonic-gate 			continue;
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 		if (a == NULL) {
5177c478bd9Sstevel@tonic-gate 			error(FATAL,
5187c478bd9Sstevel@tonic-gate 	"not enough arguments in printf(%ws) or sprintf(%ws)", os, os);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 		x = execute(a);
5217c478bd9Sstevel@tonic-gate 		a = a->nnext;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		/*
5247c478bd9Sstevel@tonic-gate 		 * Get the string to check length; %s is the usual problem;
5257c478bd9Sstevel@tonic-gate 		 * other conversions can cause overrun if they occur when
5267c478bd9Sstevel@tonic-gate 		 * the buffer is almost filled.
5277c478bd9Sstevel@tonic-gate 		 */
5287c478bd9Sstevel@tonic-gate 		if (flag == 4)	{ /* watch out for converting to numbers! */
5297c478bd9Sstevel@tonic-gate 			str = getsval(x);
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 		else {
5327c478bd9Sstevel@tonic-gate 			xf = getfval(x);
5337c478bd9Sstevel@tonic-gate 			if (flag == 1) wsprintf(tbuf, fmt, xf);
5347c478bd9Sstevel@tonic-gate 			else if (flag == 2) wsprintf(tbuf, fmt, (long)xf);
5357c478bd9Sstevel@tonic-gate 			else if (flag == 3) wsprintf(tbuf, fmt, (int)xf);
5367c478bd9Sstevel@tonic-gate 			if (wslen(tbuf) >= RECSIZE)
5377c478bd9Sstevel@tonic-gate 				error(FATAL, "formatted item %s... too long",
5387c478bd9Sstevel@tonic-gate 						tbuf);
5397c478bd9Sstevel@tonic-gate 			str = tbuf;
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 		/*
5427c478bd9Sstevel@tonic-gate 		 * If string overruns the buffer, reallocate;
5437c478bd9Sstevel@tonic-gate 		 * consider length of format string
5447c478bd9Sstevel@tonic-gate 		 */
5457c478bd9Sstevel@tonic-gate 		if (p + wslen(str) + wslen(s) + 1 >= ep) {
5467c478bd9Sstevel@tonic-gate 			int newlen, oldlen;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 			oldlen = p - buf;
5497c478bd9Sstevel@tonic-gate 			/* Add RECSIZE for additional space */
5507c478bd9Sstevel@tonic-gate 			newlen = oldlen + wslen(str) + RECSIZE;
5517c478bd9Sstevel@tonic-gate 			buf = realloc(buf, (unsigned) newlen * sizeof(wchar_t));
5527c478bd9Sstevel@tonic-gate 			if (buf == NULL)
5537c478bd9Sstevel@tonic-gate 				error(FATAL, "out of format space");
5547c478bd9Sstevel@tonic-gate 			p = buf + oldlen;
5557c478bd9Sstevel@tonic-gate 			ep = buf + newlen;
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 		/* Transfer string to buffer */
5587c478bd9Sstevel@tonic-gate 		if (flag == 4)
5597c478bd9Sstevel@tonic-gate 			wsprintf(p, fmt, str);
5607c478bd9Sstevel@tonic-gate 		else
5617c478bd9Sstevel@tonic-gate 			wscpy(p, str);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		tempfree(x);
5647c478bd9Sstevel@tonic-gate 		p += wslen(p);
5657c478bd9Sstevel@tonic-gate 		if (p >= ep)
5667c478bd9Sstevel@tonic-gate 			error(FATAL, "formatted string too long");
5677c478bd9Sstevel@tonic-gate 		s++;
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 	*p = '\0';
5707c478bd9Sstevel@tonic-gate 	return (buf);
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 
574dc5a8425Srobbin CELL *
a_sprintf(NODE ** a,int n)575d0983205SRoger A. Faulkner a_sprintf(NODE **a, int n)
5767c478bd9Sstevel@tonic-gate {
577dc5a8425Srobbin 	CELL *x;
578dc5a8425Srobbin 	NODE *y;
579dc5a8425Srobbin 	wchar_t *s;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	y = a[0]->nnext;
5827c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
5837c478bd9Sstevel@tonic-gate 	s = format(getsval(x), y);
5847c478bd9Sstevel@tonic-gate 	tempfree(x);
5857c478bd9Sstevel@tonic-gate 	x = gettemp();
5867c478bd9Sstevel@tonic-gate 	x->sval = s;
5877c478bd9Sstevel@tonic-gate 	x->tval = STR;
5887c478bd9Sstevel@tonic-gate 	return (x);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 
592dc5a8425Srobbin CELL *
arith(NODE ** a,int n)593dc5a8425Srobbin arith(NODE **a, int n)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate 	awkfloat i, j;
596dc5a8425Srobbin 	CELL *x, *y, *z;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
5997c478bd9Sstevel@tonic-gate 	i = getfval(x);
6007c478bd9Sstevel@tonic-gate 	tempfree(x);
6017c478bd9Sstevel@tonic-gate 	if (n != UMINUS) {
6027c478bd9Sstevel@tonic-gate 		y = execute(a[1]);
6037c478bd9Sstevel@tonic-gate 		j = getfval(y);
6047c478bd9Sstevel@tonic-gate 		tempfree(y);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 	z = gettemp();
6077c478bd9Sstevel@tonic-gate 	switch (n) {
6087c478bd9Sstevel@tonic-gate 	case ADD:
6097c478bd9Sstevel@tonic-gate 		i += j;
6107c478bd9Sstevel@tonic-gate 		break;
6117c478bd9Sstevel@tonic-gate 	case MINUS:
6127c478bd9Sstevel@tonic-gate 		i -= j;
6137c478bd9Sstevel@tonic-gate 		break;
6147c478bd9Sstevel@tonic-gate 	case MULT:
6157c478bd9Sstevel@tonic-gate 		i *= j;
6167c478bd9Sstevel@tonic-gate 		break;
6177c478bd9Sstevel@tonic-gate 	case DIVIDE:
6187c478bd9Sstevel@tonic-gate 		if (j == 0)
6197c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
6207c478bd9Sstevel@tonic-gate 		i /= j;
6217c478bd9Sstevel@tonic-gate 		break;
6227c478bd9Sstevel@tonic-gate 	case MOD:
6237c478bd9Sstevel@tonic-gate 		if (j == 0)
6247c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
6257c478bd9Sstevel@tonic-gate 		i = i - j*(long)(i/j);
6267c478bd9Sstevel@tonic-gate 		break;
6277c478bd9Sstevel@tonic-gate 	case UMINUS:
6287c478bd9Sstevel@tonic-gate 		i = -i;
6297c478bd9Sstevel@tonic-gate 		break;
6307c478bd9Sstevel@tonic-gate 	default:
6317c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal arithmetic operator %d", n);
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 	setfval(z, i);
6347c478bd9Sstevel@tonic-gate 	return (z);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 
640dc5a8425Srobbin CELL *
incrdecr(NODE ** a,int n)641dc5a8425Srobbin incrdecr(NODE **a, int n)
6427c478bd9Sstevel@tonic-gate {
643dc5a8425Srobbin 	CELL *x, *z;
644dc5a8425Srobbin 	int k;
6457c478bd9Sstevel@tonic-gate 	awkfloat xf;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
6487c478bd9Sstevel@tonic-gate 	xf = getfval(x);
6497c478bd9Sstevel@tonic-gate 	k = (n == PREINCR || n == POSTINCR) ? 1 : -1;
6507c478bd9Sstevel@tonic-gate 	if (n == PREINCR || n == PREDECR) {
6517c478bd9Sstevel@tonic-gate 		setfval(x, xf + k);
6527c478bd9Sstevel@tonic-gate 		return (x);
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	z = gettemp();
6557c478bd9Sstevel@tonic-gate 	setfval(z, xf);
6567c478bd9Sstevel@tonic-gate 	setfval(x, xf + k);
6577c478bd9Sstevel@tonic-gate 	tempfree(x);
6587c478bd9Sstevel@tonic-gate 	return (z);
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 
663dc5a8425Srobbin CELL *
assign(NODE ** a,int n)664dc5a8425Srobbin assign(NODE **a, int n)
6657c478bd9Sstevel@tonic-gate {
666dc5a8425Srobbin 	CELL *x, *y;
6677c478bd9Sstevel@tonic-gate 	awkfloat xf, yf;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
6737c478bd9Sstevel@tonic-gate 	y = execute(a[1]);
6747c478bd9Sstevel@tonic-gate 	if (n == ASSIGN) {	/* ordinary assignment */
6757c478bd9Sstevel@tonic-gate 		if ((y->tval & (STR|NUM)) == (STR|NUM)) {
6767c478bd9Sstevel@tonic-gate 			setsval(x, y->sval);
6777c478bd9Sstevel@tonic-gate 			x->fval = y->fval;
6787c478bd9Sstevel@tonic-gate 			x->tval |= NUM;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		} else if (y->tval & STR)
6817c478bd9Sstevel@tonic-gate 			setsval(x, y->sval);
6827c478bd9Sstevel@tonic-gate 		else if (y->tval & NUM)
6837c478bd9Sstevel@tonic-gate 			setfval(x, y->fval);
6847c478bd9Sstevel@tonic-gate 		tempfree(y);
6857c478bd9Sstevel@tonic-gate 		return (x);
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	xf = getfval(x);
6887c478bd9Sstevel@tonic-gate 	yf = getfval(y);
6897c478bd9Sstevel@tonic-gate 	switch (n) {
6907c478bd9Sstevel@tonic-gate 	case ADDEQ:
6917c478bd9Sstevel@tonic-gate 		xf += yf;
6927c478bd9Sstevel@tonic-gate 		break;
6937c478bd9Sstevel@tonic-gate 	case SUBEQ:
6947c478bd9Sstevel@tonic-gate 		xf -= yf;
6957c478bd9Sstevel@tonic-gate 		break;
6967c478bd9Sstevel@tonic-gate 	case MULTEQ:
6977c478bd9Sstevel@tonic-gate 		xf *= yf;
6987c478bd9Sstevel@tonic-gate 		break;
6997c478bd9Sstevel@tonic-gate 	case DIVEQ:
7007c478bd9Sstevel@tonic-gate 		if (yf == 0)
7017c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
7027c478bd9Sstevel@tonic-gate 		xf /= yf;
7037c478bd9Sstevel@tonic-gate 		break;
7047c478bd9Sstevel@tonic-gate 	case MODEQ:
7057c478bd9Sstevel@tonic-gate 		if (yf == 0)
7067c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
7077c478bd9Sstevel@tonic-gate 		xf = xf - yf*(long)(xf/yf);
7087c478bd9Sstevel@tonic-gate 		break;
7097c478bd9Sstevel@tonic-gate 	default:
7107c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal assignment operator %d", n);
7117c478bd9Sstevel@tonic-gate 		break;
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 	tempfree(y);
7147c478bd9Sstevel@tonic-gate 	setfval(x, xf);
7157c478bd9Sstevel@tonic-gate 	return (x);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 
721dc5a8425Srobbin CELL *
cat(NODE ** a,int q)722dc5a8425Srobbin cat(NODE **a, int q)
7237c478bd9Sstevel@tonic-gate {
724dc5a8425Srobbin 	CELL *x, *y, *z;
725dc5a8425Srobbin 	int n1, n2;
726dc5a8425Srobbin 	wchar_t *s;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
7327c478bd9Sstevel@tonic-gate 	y = execute(a[1]);
7337c478bd9Sstevel@tonic-gate 	getsval(x);
7347c478bd9Sstevel@tonic-gate 	getsval(y);
7357c478bd9Sstevel@tonic-gate 	n1 = wslen(x->sval);
7367c478bd9Sstevel@tonic-gate 	n2 = wslen(y->sval);
7377c478bd9Sstevel@tonic-gate 	if ((s = (wchar_t *) malloc((n1 + n2 + 1) * sizeof (wchar_t))) == NULL)
7387c478bd9Sstevel@tonic-gate 		error(FATAL, "out of space in cat");
7397c478bd9Sstevel@tonic-gate 	wscpy(s, x->sval);
7407c478bd9Sstevel@tonic-gate 	wscpy(s+n1, y->sval);
7417c478bd9Sstevel@tonic-gate 	tempfree(y);
7427c478bd9Sstevel@tonic-gate 	z = gettemp();
7437c478bd9Sstevel@tonic-gate 	z->sval = s;
7447c478bd9Sstevel@tonic-gate 	z->tval = STR;
7457c478bd9Sstevel@tonic-gate 	tempfree(x);
7467c478bd9Sstevel@tonic-gate 	return (z);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 
752dc5a8425Srobbin CELL *
pastat(NODE ** a,int n)753dc5a8425Srobbin pastat(NODE **a, int n)
7547c478bd9Sstevel@tonic-gate {
755dc5a8425Srobbin 	CELL *x;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	if (a[0] == 0)
7617c478bd9Sstevel@tonic-gate 		x = true;
7627c478bd9Sstevel@tonic-gate 	else
7637c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
7647c478bd9Sstevel@tonic-gate 	if (istrue(x)) {
7657c478bd9Sstevel@tonic-gate 		tempfree(x);
7667c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 	return (x);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 
774dc5a8425Srobbin CELL *
dopa2(NODE ** a,int n)775dc5a8425Srobbin dopa2(NODE **a, int n)
7767c478bd9Sstevel@tonic-gate {
777dc5a8425Srobbin 	CELL *x;
778dc5a8425Srobbin 	int pair;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	pair = (int) a[3];
7847c478bd9Sstevel@tonic-gate 	if (pairstack[pair] == 0) {
7857c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
7867c478bd9Sstevel@tonic-gate 		if (istrue(x))
7877c478bd9Sstevel@tonic-gate 			pairstack[pair] = 1;
7887c478bd9Sstevel@tonic-gate 		tempfree(x);
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 	if (pairstack[pair] == 1) {
7917c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
7927c478bd9Sstevel@tonic-gate 		if (istrue(x))
7937c478bd9Sstevel@tonic-gate 			pairstack[pair] = 0;
7947c478bd9Sstevel@tonic-gate 		tempfree(x);
7957c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
7967c478bd9Sstevel@tonic-gate 		return (x);
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 	return (false);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 
804dc5a8425Srobbin CELL *
aprintf(NODE ** a,int n)805dc5a8425Srobbin aprintf(NODE **a, int n)
8067c478bd9Sstevel@tonic-gate {
807dc5a8425Srobbin 	CELL *x;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 
812d0983205SRoger A. Faulkner 	x = a_sprintf(a, n);
8137c478bd9Sstevel@tonic-gate 	if (a[1] == NULL) {
8147c478bd9Sstevel@tonic-gate 		printf("%ws", x->sval);
8157c478bd9Sstevel@tonic-gate 		tempfree(x);
8167c478bd9Sstevel@tonic-gate 		return (true);
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 	redirprint(x->sval, (int)a[1], a[2]);
8197c478bd9Sstevel@tonic-gate 	return (x);
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 
825dc5a8425Srobbin CELL *
split(NODE ** a,int nnn)826dc5a8425Srobbin split(NODE **a, int nnn)
8277c478bd9Sstevel@tonic-gate {
828dc5a8425Srobbin 	CELL *x;
829dc5a8425Srobbin 	CELL *ap;
830dc5a8425Srobbin 	wchar_t *s, *p, c;
8317c478bd9Sstevel@tonic-gate 	wchar_t *t, temp, num[5];
832dc5a8425Srobbin 	wchar_t sep;
8337c478bd9Sstevel@tonic-gate 	int n, flag;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
8397c478bd9Sstevel@tonic-gate 	s = getsval(x);
8407c478bd9Sstevel@tonic-gate 	tempfree(x);
8417c478bd9Sstevel@tonic-gate 	if (a[2] == 0)
8427c478bd9Sstevel@tonic-gate 		sep = **FS;
8437c478bd9Sstevel@tonic-gate 	else {
8447c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
8457c478bd9Sstevel@tonic-gate 		sep = getsval(x)[0];
8467c478bd9Sstevel@tonic-gate 		tempfree(x);
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 	ap = (CELL *) a[1];
8497c478bd9Sstevel@tonic-gate 	freesymtab(ap);
8507c478bd9Sstevel@tonic-gate 	dprintf("split: s=|%ws|, a=%ws, sep=|%wc|\n", s, ap->nval, sep);
8517c478bd9Sstevel@tonic-gate 	ap->tval &= ~STR;
8527c478bd9Sstevel@tonic-gate 	ap->tval |= ARR;
8537c478bd9Sstevel@tonic-gate 	ap->sval = (wchar_t *) makesymtab();
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	n = 0;
8597c478bd9Sstevel@tonic-gate 	if (sep == ' ')
8607c478bd9Sstevel@tonic-gate 		for (n = 0; /* dummy */; /* dummy */) {
8617c478bd9Sstevel@tonic-gate 			c = *s;
8627c478bd9Sstevel@tonic-gate 			while (iswblank(c) || c == '\t' || c == '\n')
8637c478bd9Sstevel@tonic-gate 				c = *(++s);
8647c478bd9Sstevel@tonic-gate 			if (*s == 0)
8657c478bd9Sstevel@tonic-gate 				break;
8667c478bd9Sstevel@tonic-gate 			n++;
8677c478bd9Sstevel@tonic-gate 			t = s;
8687c478bd9Sstevel@tonic-gate 			do
8697c478bd9Sstevel@tonic-gate 				c = *(++s);
8707c478bd9Sstevel@tonic-gate 			while (! iswblank(c) && c != '\t' &&
8717c478bd9Sstevel@tonic-gate 				c != '\n' && c != '\0');
8727c478bd9Sstevel@tonic-gate 			temp = c;
8737c478bd9Sstevel@tonic-gate 			*s = (wchar_t)0x0;
8747c478bd9Sstevel@tonic-gate 			wsprintf(num, "%d", n);
8757c478bd9Sstevel@tonic-gate 			if (isanumber(t))
8767c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t),
8777c478bd9Sstevel@tonic-gate 						watof(t), STR|NUM, ap->sval);
8787c478bd9Sstevel@tonic-gate 			else
8797c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t), 0.0, STR, ap->sval);
8807c478bd9Sstevel@tonic-gate 			*s = temp;
8817c478bd9Sstevel@tonic-gate 			if (*s != 0)
8827c478bd9Sstevel@tonic-gate 				s++;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	} else if (*s != 0)
8857c478bd9Sstevel@tonic-gate 		for (;;) {
8867c478bd9Sstevel@tonic-gate 			n++;
8877c478bd9Sstevel@tonic-gate 			t = s;
8887c478bd9Sstevel@tonic-gate 			while ((c = *s) != sep && c != '\n' && c != '\0')
8897c478bd9Sstevel@tonic-gate 				s++;
8907c478bd9Sstevel@tonic-gate 			temp = c;
8917c478bd9Sstevel@tonic-gate 			*s = (wchar_t)0x0;
8927c478bd9Sstevel@tonic-gate 			wsprintf(num, "%d", n);
8937c478bd9Sstevel@tonic-gate 			if (isanumber(t))
8947c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t),
8957c478bd9Sstevel@tonic-gate 						watof(t), STR|NUM, ap->sval);
8967c478bd9Sstevel@tonic-gate 			else
8977c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t), 0.0, STR, ap->sval);
8987c478bd9Sstevel@tonic-gate 			*s = temp;
8997c478bd9Sstevel@tonic-gate 			if (*s++ == 0)
9007c478bd9Sstevel@tonic-gate 				break;
9017c478bd9Sstevel@tonic-gate 		}
9027c478bd9Sstevel@tonic-gate 	x = gettemp();
9037c478bd9Sstevel@tonic-gate 	x->tval = NUM;
9047c478bd9Sstevel@tonic-gate 	x->fval = n;
9057c478bd9Sstevel@tonic-gate 	return (x);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 
911dc5a8425Srobbin CELL *
ifstat(NODE ** a,int n)912dc5a8425Srobbin ifstat(NODE **a, int n)
9137c478bd9Sstevel@tonic-gate {
914dc5a8425Srobbin 	CELL *x;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
9207c478bd9Sstevel@tonic-gate 	if (istrue(x)) {
9217c478bd9Sstevel@tonic-gate 		tempfree(x);
9227c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	} else if (a[2] != 0) {
9257c478bd9Sstevel@tonic-gate 		tempfree(x);
9267c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 	return (x);
9297c478bd9Sstevel@tonic-gate }
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 
934dc5a8425Srobbin CELL *
whilestat(NODE ** a,int n)935dc5a8425Srobbin whilestat(NODE **a, int n)
9367c478bd9Sstevel@tonic-gate {
937dc5a8425Srobbin 	CELL *x;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	for (;;) {
9437c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
9447c478bd9Sstevel@tonic-gate 		if (!istrue(x)) return (x);
9457c478bd9Sstevel@tonic-gate 		tempfree(x);
9467c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
9477c478bd9Sstevel@tonic-gate 		if (isbreak(x)) {
9487c478bd9Sstevel@tonic-gate 			x = true;
9497c478bd9Sstevel@tonic-gate 			return (x);
9507c478bd9Sstevel@tonic-gate 		}
9517c478bd9Sstevel@tonic-gate 		if (isnext(x) || isexit(x))
9527c478bd9Sstevel@tonic-gate 			return (x);
9537c478bd9Sstevel@tonic-gate 		tempfree(x);
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 
960dc5a8425Srobbin CELL *
forstat(NODE ** a,int n)961dc5a8425Srobbin forstat(NODE **a, int n)
9627c478bd9Sstevel@tonic-gate {
963dc5a8425Srobbin 	CELL *x;
964dc5a8425Srobbin 	CELL *z;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	z = execute(a[0]);
9707c478bd9Sstevel@tonic-gate 	tempfree(z);
9717c478bd9Sstevel@tonic-gate 	for (;;) {
9727c478bd9Sstevel@tonic-gate 		if (a[1]!=0) {
9737c478bd9Sstevel@tonic-gate 			x = execute(a[1]);
9747c478bd9Sstevel@tonic-gate 			if (!istrue(x)) return (x);
9757c478bd9Sstevel@tonic-gate 			else tempfree(x);
9767c478bd9Sstevel@tonic-gate 		}
9777c478bd9Sstevel@tonic-gate 		x = execute(a[3]);
9787c478bd9Sstevel@tonic-gate 		if (isbreak(x)) {	/* turn off break */
9797c478bd9Sstevel@tonic-gate 			x = true;
9807c478bd9Sstevel@tonic-gate 			return (x);
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 		if (isnext(x) || isexit(x))
9837c478bd9Sstevel@tonic-gate 			return (x);
9847c478bd9Sstevel@tonic-gate 		tempfree(x);
9857c478bd9Sstevel@tonic-gate 		z = execute(a[2]);
9867c478bd9Sstevel@tonic-gate 		tempfree(z);
9877c478bd9Sstevel@tonic-gate 	}
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 
993dc5a8425Srobbin CELL *
instat(NODE ** a,int n)994dc5a8425Srobbin instat(NODE **a, int n)
9957c478bd9Sstevel@tonic-gate {
996dc5a8425Srobbin 	CELL *vp, *arrayp, *cp, **tp;
997dc5a8425Srobbin 	CELL *x;
9987c478bd9Sstevel@tonic-gate 	int i;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	vp = (CELL *) a[0];
10047c478bd9Sstevel@tonic-gate 	arrayp = (CELL *) a[1];
10057c478bd9Sstevel@tonic-gate 	if (!(arrayp->tval & ARR))
10067c478bd9Sstevel@tonic-gate 		error(FATAL, "%ws is not an array", arrayp->nval);
10077c478bd9Sstevel@tonic-gate 	tp = (CELL **) arrayp->sval;
10087c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAXSYM; i++) {	/* this routine knows too much */
10097c478bd9Sstevel@tonic-gate 		for (cp = tp[i]; cp != NULL; cp = cp->nextval) {
10107c478bd9Sstevel@tonic-gate 			setsval(vp, cp->nval);
10117c478bd9Sstevel@tonic-gate 			x = execute(a[2]);
10127c478bd9Sstevel@tonic-gate 			if (isbreak(x)) {
10137c478bd9Sstevel@tonic-gate 				x = true;
10147c478bd9Sstevel@tonic-gate 				return (x);
10157c478bd9Sstevel@tonic-gate 			}
10167c478bd9Sstevel@tonic-gate 			if (isnext(x) || isexit(x))
10177c478bd9Sstevel@tonic-gate 				return (x);
10187c478bd9Sstevel@tonic-gate 			tempfree(x);
10197c478bd9Sstevel@tonic-gate 		}
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 	return (true);
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 
1027dc5a8425Srobbin CELL *
jump(NODE ** a,int n)1028dc5a8425Srobbin jump(NODE **a, int n)
10297c478bd9Sstevel@tonic-gate {
1030dc5a8425Srobbin 	CELL *y;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	switch (n) {
10367c478bd9Sstevel@tonic-gate 	case EXIT:
10377c478bd9Sstevel@tonic-gate 		if (a[0] != 0) {
10387c478bd9Sstevel@tonic-gate 			y = execute(a[0]);
10397c478bd9Sstevel@tonic-gate 			errorflag = getfval(y);
10407c478bd9Sstevel@tonic-gate 		}
10417c478bd9Sstevel@tonic-gate 		return (jexit);
10427c478bd9Sstevel@tonic-gate 	case NEXT:
10437c478bd9Sstevel@tonic-gate 		return (jnext);
10447c478bd9Sstevel@tonic-gate 	case BREAK:
10457c478bd9Sstevel@tonic-gate 		return (jbreak);
10467c478bd9Sstevel@tonic-gate 	case CONTINUE:
10477c478bd9Sstevel@tonic-gate 		return (jcont);
10487c478bd9Sstevel@tonic-gate 	default:
10497c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal jump type %d", n);
10507c478bd9Sstevel@tonic-gate 	}
1051dc5a8425Srobbin 	return (NULL);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 
1057dc5a8425Srobbin CELL *
fncn(NODE ** a,int n)1058dc5a8425Srobbin fncn(NODE **a, int n)
10597c478bd9Sstevel@tonic-gate {
1060dc5a8425Srobbin 	CELL *x;
10617c478bd9Sstevel@tonic-gate 	awkfloat u;
1062dc5a8425Srobbin 	int t;
1063dc5a8425Srobbin 	wchar_t *wp;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	t = (int) a[0];
10667c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
10677c478bd9Sstevel@tonic-gate 	if (t == FLENGTH)
10687c478bd9Sstevel@tonic-gate 		u = (awkfloat) wslen(getsval(x));
10697c478bd9Sstevel@tonic-gate 	else if (t == FLOG)
10707c478bd9Sstevel@tonic-gate 		u = log(getfval(x));
10717c478bd9Sstevel@tonic-gate 	else if (t == FINT)
10727c478bd9Sstevel@tonic-gate 		u = (awkfloat) (long) getfval(x);
10737c478bd9Sstevel@tonic-gate 	else if (t == FEXP)
10747c478bd9Sstevel@tonic-gate 		u = exp(getfval(x));
10757c478bd9Sstevel@tonic-gate 	else if (t == FSQRT)
10767c478bd9Sstevel@tonic-gate 		u = sqrt(getfval(x));
10777c478bd9Sstevel@tonic-gate 	else
10787c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal function type %d", t);
10797c478bd9Sstevel@tonic-gate 	tempfree(x);
10807c478bd9Sstevel@tonic-gate 	x = gettemp();
10817c478bd9Sstevel@tonic-gate 	setfval(x, u);
10827c478bd9Sstevel@tonic-gate 	return (x);
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 
1088dc5a8425Srobbin CELL *
print(NODE ** a,int n)1089dc5a8425Srobbin print(NODE **a, int n)
10907c478bd9Sstevel@tonic-gate {
1091dc5a8425Srobbin 	NODE *x;
1092dc5a8425Srobbin 	CELL *y;
10937c478bd9Sstevel@tonic-gate 	wchar_t s[RECSIZE];
1094aba1133aSnakanon 	wchar_t *ss, *bp, *ep, *os;
1095aba1133aSnakanon 	size_t	blen, newlen, sslen, orslen, ofslen, oslen;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	s[0] = '\0';
10987c478bd9Sstevel@tonic-gate 	bp = s;
10997c478bd9Sstevel@tonic-gate 	ep = s + RECSIZE;
1100aba1133aSnakanon 
1101aba1133aSnakanon 	blen = 0;
1102aba1133aSnakanon 	orslen = wcslen(*ORS);
1103aba1133aSnakanon 	ofslen = wcslen(*OFS);
1104aba1133aSnakanon 
11057c478bd9Sstevel@tonic-gate 	for (x = a[0]; x != NULL; x = x->nnext) {
11067c478bd9Sstevel@tonic-gate 		y = execute(x);
11077c478bd9Sstevel@tonic-gate 		ss = getsval(y);
11087c478bd9Sstevel@tonic-gate 
1109aba1133aSnakanon 		/* total new length will be */
1110aba1133aSnakanon 		sslen = wcslen(ss);
1111aba1133aSnakanon 		if (x->nnext == NULL) {
1112aba1133aSnakanon 			os = *ORS;
1113aba1133aSnakanon 			oslen = orslen;
1114aba1133aSnakanon 		} else {
1115aba1133aSnakanon 			os = *OFS;
1116aba1133aSnakanon 			oslen = ofslen;
1117aba1133aSnakanon 		}
1118aba1133aSnakanon 		newlen = blen + sslen + oslen;
1119aba1133aSnakanon 
1120aba1133aSnakanon 		/* allocate larger buffer if needed */
1121aba1133aSnakanon 		if (ep < (bp + newlen + 1)) {
1122aba1133aSnakanon 			wchar_t	*oldbp = bp;
1123aba1133aSnakanon 
1124aba1133aSnakanon 			if (oldbp == s)
1125aba1133aSnakanon 				bp = NULL;
1126aba1133aSnakanon 			bp = realloc(bp, sizeof (wchar_t) * (newlen + 1));
11277c478bd9Sstevel@tonic-gate 			if (bp == NULL)
11287c478bd9Sstevel@tonic-gate 				error(FATAL, "out of space in print");
1129aba1133aSnakanon 			ep = bp + newlen + 1;
1130aba1133aSnakanon 			if (oldbp == s)
1131aba1133aSnakanon 				(void) wmemcpy(bp, oldbp, blen);
11327c478bd9Sstevel@tonic-gate 		}
1133aba1133aSnakanon 		(void) wmemcpy(bp + blen, ss, sslen);
1134aba1133aSnakanon 		(void) wmemcpy(bp + blen + sslen, os, oslen);
11357c478bd9Sstevel@tonic-gate 		tempfree(y);
1136aba1133aSnakanon 		blen = newlen;
1137aba1133aSnakanon 		bp[blen] = '\0';
11387c478bd9Sstevel@tonic-gate 	}
1139aba1133aSnakanon 	if (a[1] == NULL) {
1140aba1133aSnakanon 		(void) printf("%ws", bp);
11417c478bd9Sstevel@tonic-gate 		if (bp != s)
11427c478bd9Sstevel@tonic-gate 			free(bp);
11437c478bd9Sstevel@tonic-gate 		return (true);
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	redirprint(bp, (int)a[1], a[2]);
11477c478bd9Sstevel@tonic-gate 	if (bp != s)
11487c478bd9Sstevel@tonic-gate 		free(bp);
11497c478bd9Sstevel@tonic-gate 	return (false);
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 
1154dc5a8425Srobbin CELL *
nullproc(void)1155dc5a8425Srobbin nullproc(void)
11567c478bd9Sstevel@tonic-gate {
1157dc5a8425Srobbin 	return (NULL);
1158dc5a8425Srobbin }
1159dc5a8425Srobbin 
1160dc5a8425Srobbin 
1161dc5a8425Srobbin 
1162dc5a8425Srobbin CELL *
nodetoobj(NODE * a)1163dc5a8425Srobbin nodetoobj(NODE *a)
1164dc5a8425Srobbin {
1165dc5a8425Srobbin 	CELL *x;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	x= (CELL *) a->nobj;
11687c478bd9Sstevel@tonic-gate 	x->ctype = OCELL;
11697c478bd9Sstevel@tonic-gate 	x->csub = a->subtype;
11707c478bd9Sstevel@tonic-gate 	if (isfld(x))
11717c478bd9Sstevel@tonic-gate 		fldbld();
11727c478bd9Sstevel@tonic-gate 	return (x);
11737c478bd9Sstevel@tonic-gate }
11747c478bd9Sstevel@tonic-gate 
1175dc5a8425Srobbin static void
redirprint(wchar_t * s,int a,NODE * b)1176dc5a8425Srobbin redirprint(wchar_t *s, int a, NODE *b)
11777c478bd9Sstevel@tonic-gate {
1178dc5a8425Srobbin 	int i;
1179dc5a8425Srobbin 	CELL *x;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	x = execute(b);
11827c478bd9Sstevel@tonic-gate 	getsval(x);
11837c478bd9Sstevel@tonic-gate 	for (i=0; i<FILENUM; i++)
11847c478bd9Sstevel@tonic-gate 		if (files[i].fname && wscmp(x->sval, files[i].fname) == 0)
11857c478bd9Sstevel@tonic-gate 			goto doit;
11867c478bd9Sstevel@tonic-gate 	for (i=0; i<FILENUM; i++)
11877c478bd9Sstevel@tonic-gate 		if (files[i].fp == 0)
11887c478bd9Sstevel@tonic-gate 			break;
11897c478bd9Sstevel@tonic-gate 	if (i >= FILENUM)
11907c478bd9Sstevel@tonic-gate 		error(FATAL, "too many output files %d", i);
11917c478bd9Sstevel@tonic-gate 	if (a == '|')	/* a pipe! */
11927c478bd9Sstevel@tonic-gate 		files[i].fp = popen(toeuccode(x->sval), "w");
11937c478bd9Sstevel@tonic-gate 	else if (a == APPEND)
11947c478bd9Sstevel@tonic-gate 		files[i].fp = fopen(toeuccode(x->sval), "a");
11957c478bd9Sstevel@tonic-gate 	else if (a == GT)
11967c478bd9Sstevel@tonic-gate 		files[i].fp = fopen(toeuccode(x->sval), "w");
11977c478bd9Sstevel@tonic-gate 	else
11987c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal redirection near line %lld", lineno);
11997c478bd9Sstevel@tonic-gate 	if (files[i].fp == NULL)
12007c478bd9Sstevel@tonic-gate 		error(FATAL, "can't open file %ws", x->sval);
12017c478bd9Sstevel@tonic-gate 	files[i].fname = tostring(x->sval);
12027c478bd9Sstevel@tonic-gate 	files[i].type = a;
12037c478bd9Sstevel@tonic-gate doit:
12047c478bd9Sstevel@tonic-gate 	fprintf(files[i].fp, "%ws", s);
12057c478bd9Sstevel@tonic-gate #ifndef gcos
12067c478bd9Sstevel@tonic-gate 	fflush(files[i].fp);	/* in case someone is waiting for the output */
12077c478bd9Sstevel@tonic-gate #endif
12087c478bd9Sstevel@tonic-gate 	tempfree(x);
12097c478bd9Sstevel@tonic-gate }
1210