xref: /titanic_51/usr/src/cmd/oawk/run.c (revision aba1133a5077b2daf9217c517f6aa15731135d8e)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
24*aba1133aSnakanon  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
28dc5a8425Srobbin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29dc5a8425Srobbin /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
31dc5a8425Srobbin #pragma ident	"%Z%%M%	%I%	%E% SMI"
32dc5a8425Srobbin 
337c478bd9Sstevel@tonic-gate #define	DEBUG
347c478bd9Sstevel@tonic-gate #define	tempfree(a)	{if (istemp(a)) {xfree(a->sval); a->tval = 0; }}
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include	"awk.def"
377c478bd9Sstevel@tonic-gate #include	"math.h"
387c478bd9Sstevel@tonic-gate #include	"awk.h"
397c478bd9Sstevel@tonic-gate #include	"stdio.h"
407c478bd9Sstevel@tonic-gate #include "ctype.h"
417c478bd9Sstevel@tonic-gate #include "wctype.h"
427c478bd9Sstevel@tonic-gate #include "awktype.h"
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	RECSIZE BUFSIZ
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	FILENUM	10
487c478bd9Sstevel@tonic-gate struct
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	FILE *fp;
517c478bd9Sstevel@tonic-gate 	int type;
527c478bd9Sstevel@tonic-gate 	wchar_t *fname;
537c478bd9Sstevel@tonic-gate } files[FILENUM];
547c478bd9Sstevel@tonic-gate FILE *popen();
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate extern CELL *execute(), *nodetoobj(), *fieldel(), *dopa2(), *gettemp();
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	PA2NUM	29
597c478bd9Sstevel@tonic-gate int	pairstack[PA2NUM], paircnt;
607c478bd9Sstevel@tonic-gate NODE	*winner = NULL;
617c478bd9Sstevel@tonic-gate #define	MAXTMP	20
627c478bd9Sstevel@tonic-gate CELL	tmps[MAXTMP];
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static CELL	truecell	={ OBOOL, BTRUE, 0, 0, 0.0, NUM, 0 };
657c478bd9Sstevel@tonic-gate CELL	*true	= &truecell;
667c478bd9Sstevel@tonic-gate static CELL	falsecell	={ OBOOL, BFALSE, 0, 0, 0.0, NUM, 0 };
677c478bd9Sstevel@tonic-gate CELL	*false	= &falsecell;
687c478bd9Sstevel@tonic-gate static CELL	breakcell	={ OJUMP, JBREAK, 0, 0, 0.0, NUM, 0 };
697c478bd9Sstevel@tonic-gate CELL	*jbreak	= &breakcell;
707c478bd9Sstevel@tonic-gate static CELL	contcell	={ OJUMP, JCONT, 0, 0, 0.0, NUM, 0 };
717c478bd9Sstevel@tonic-gate CELL	*jcont	= &contcell;
727c478bd9Sstevel@tonic-gate static CELL	nextcell	={ OJUMP, JNEXT, 0, 0, 0.0, NUM, 0 };
737c478bd9Sstevel@tonic-gate CELL	*jnext	= &nextcell;
747c478bd9Sstevel@tonic-gate static CELL	exitcell	={ OJUMP, JEXIT, 0, 0, 0.0, NUM, 0 };
757c478bd9Sstevel@tonic-gate CELL	*jexit	= &exitcell;
767c478bd9Sstevel@tonic-gate static CELL	tempcell	={ OCELL, CTEMP, 0, 0, 0.0, NUM, 0 };
777c478bd9Sstevel@tonic-gate 
78dc5a8425Srobbin static void redirprint(wchar_t *s, int a, NODE *b);
79dc5a8425Srobbin 
80dc5a8425Srobbin void freesymtab(CELL *ap);
81dc5a8425Srobbin void fldbld(void);
82dc5a8425Srobbin 
83dc5a8425Srobbin void
84dc5a8425Srobbin run(NODE *a)
857c478bd9Sstevel@tonic-gate {
86dc5a8425Srobbin 	int i;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	execute(a);
897c478bd9Sstevel@tonic-gate 	/* Wait for children to complete if output to a pipe. */
907c478bd9Sstevel@tonic-gate 	for (i=0; i<FILENUM; i++)
917c478bd9Sstevel@tonic-gate 		if (files[i].fp && files[i].type == '|')
927c478bd9Sstevel@tonic-gate 			pclose(files[i].fp);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 
96dc5a8425Srobbin CELL *
97dc5a8425Srobbin execute(NODE *u)
987c478bd9Sstevel@tonic-gate {
99dc5a8425Srobbin 	CELL *(*proc)();
100dc5a8425Srobbin 	CELL *x;
101dc5a8425Srobbin 	NODE *a;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if (u == NULL)
1047c478bd9Sstevel@tonic-gate 		return (true);
1057c478bd9Sstevel@tonic-gate 	for (a = u; /* dummy */; a = a->nnext) {
1067c478bd9Sstevel@tonic-gate 		if (cantexec(a))
1077c478bd9Sstevel@tonic-gate 			return (nodetoobj(a));
1087c478bd9Sstevel@tonic-gate 		if (notlegal(a->nobj))
1097c478bd9Sstevel@tonic-gate 			error(FATAL, "illegal statement %o", a);
1107c478bd9Sstevel@tonic-gate 		proc = proctab[a->nobj-FIRSTTOKEN];
1117c478bd9Sstevel@tonic-gate 		x = (*proc)(a->narg, a->nobj);
1127c478bd9Sstevel@tonic-gate 		if (isfld(x))
1137c478bd9Sstevel@tonic-gate 			fldbld();
1147c478bd9Sstevel@tonic-gate 		if (isexpr(a))
1157c478bd9Sstevel@tonic-gate 			return (x);
1167c478bd9Sstevel@tonic-gate 		/* a statement, goto next statement */
1177c478bd9Sstevel@tonic-gate 		if (isjump(x))
1187c478bd9Sstevel@tonic-gate 			return (x);
1197c478bd9Sstevel@tonic-gate 		if (a->nnext == (NODE *)NULL)
1207c478bd9Sstevel@tonic-gate 			return (x);
1217c478bd9Sstevel@tonic-gate 		tempfree(x);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 
128dc5a8425Srobbin CELL *
129dc5a8425Srobbin program(NODE **a, int n)
1307c478bd9Sstevel@tonic-gate {
131dc5a8425Srobbin 	CELL *x;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (a[0] != NULL) {
1347c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
1357c478bd9Sstevel@tonic-gate 		if (isexit(x))
1367c478bd9Sstevel@tonic-gate 			return (true);
1377c478bd9Sstevel@tonic-gate 		if (isjump(x))
1387c478bd9Sstevel@tonic-gate 			error(FATAL, "unexpected break, continue or next");
1397c478bd9Sstevel@tonic-gate 		tempfree(x);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	while (getrec()) {
1427c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
1437c478bd9Sstevel@tonic-gate 		if (isexit(x)) {
1447c478bd9Sstevel@tonic-gate 			tempfree(x);
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		tempfree(x);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 	if (a[2] != NULL) {
1507c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
1517c478bd9Sstevel@tonic-gate 		if (isbreak(x) || isnext(x) || iscont(x))
1527c478bd9Sstevel@tonic-gate 			error(FATAL, "unexpected break, continue or next");
1537c478bd9Sstevel@tonic-gate 		tempfree(x);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	return (true);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 
161dc5a8425Srobbin CELL *
162dc5a8425Srobbin getline(void)
1637c478bd9Sstevel@tonic-gate {
164dc5a8425Srobbin 	CELL *x;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	x = gettemp();
1677c478bd9Sstevel@tonic-gate 	setfval(x, (awkfloat) getrec());
1687c478bd9Sstevel@tonic-gate 	return (x);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 
174dc5a8425Srobbin CELL *
175dc5a8425Srobbin array(NODE **a, int n)
1767c478bd9Sstevel@tonic-gate {
177dc5a8425Srobbin 	CELL *x, *y;
1787c478bd9Sstevel@tonic-gate 	extern CELL *arrayel();
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
1817c478bd9Sstevel@tonic-gate 	y = arrayel(a[0], x);
1827c478bd9Sstevel@tonic-gate 	tempfree(x);
1837c478bd9Sstevel@tonic-gate 	return (y);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 
189dc5a8425Srobbin CELL *
190dc5a8425Srobbin arrayel(NODE *a, CELL *b)
1917c478bd9Sstevel@tonic-gate {
192dc5a8425Srobbin 	wchar_t *s;
193dc5a8425Srobbin 	CELL *x;
194dc5a8425Srobbin 	int i;
195dc5a8425Srobbin 	CELL *y;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	s = getsval(b);
1987c478bd9Sstevel@tonic-gate 	x = (CELL *) a;
1997c478bd9Sstevel@tonic-gate 	if (!(x->tval&ARR)) {
2007c478bd9Sstevel@tonic-gate 		xfree(x->sval);
2017c478bd9Sstevel@tonic-gate 		x->tval &= ~STR;
2027c478bd9Sstevel@tonic-gate 		x->tval |= ARR;
2037c478bd9Sstevel@tonic-gate 		x->sval = (wchar_t *) makesymtab();
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	y = setsymtab(s, tostring(L_NULL), 0.0, STR|NUM, x->sval);
2067c478bd9Sstevel@tonic-gate 	y->ctype = OCELL;
2077c478bd9Sstevel@tonic-gate 	y->csub = CVAR;
2087c478bd9Sstevel@tonic-gate 	return (y);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
211dc5a8425Srobbin CELL *
212dc5a8425Srobbin matchop(NODE **a, int n)
2137c478bd9Sstevel@tonic-gate {
214dc5a8425Srobbin 	CELL *x;
215dc5a8425Srobbin 	wchar_t *s;
216dc5a8425Srobbin 	int i;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
2197c478bd9Sstevel@tonic-gate 	s = getsval(x);
2207c478bd9Sstevel@tonic-gate 	tempfree(x);
2217c478bd9Sstevel@tonic-gate 	i = match(a[1], s);
2227c478bd9Sstevel@tonic-gate 	if (n == MATCH && i == 1 || n == NOTMATCH && i == 0)
2237c478bd9Sstevel@tonic-gate 		return (true);
2247c478bd9Sstevel@tonic-gate 	else
2257c478bd9Sstevel@tonic-gate 		return (false);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 
231dc5a8425Srobbin CELL *
232dc5a8425Srobbin boolop(NODE **a, int n)
2337c478bd9Sstevel@tonic-gate {
234dc5a8425Srobbin 	CELL *x, *y;
235dc5a8425Srobbin 	int i;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
2417c478bd9Sstevel@tonic-gate 	i = istrue(x);
2427c478bd9Sstevel@tonic-gate 	tempfree(x);
2437c478bd9Sstevel@tonic-gate 	switch (n) {
2447c478bd9Sstevel@tonic-gate 	case BOR:
2457c478bd9Sstevel@tonic-gate 		if (i) return (true);
2467c478bd9Sstevel@tonic-gate 		y = execute(a[1]);
2477c478bd9Sstevel@tonic-gate 		i = istrue(y);
2487c478bd9Sstevel@tonic-gate 		tempfree(y);
2497c478bd9Sstevel@tonic-gate 		if (i) return (true);
2507c478bd9Sstevel@tonic-gate 		else return (false);
2517c478bd9Sstevel@tonic-gate 	case AND:
2527c478bd9Sstevel@tonic-gate 		if (!i) return (false);
2537c478bd9Sstevel@tonic-gate 		y = execute(a[1]);
2547c478bd9Sstevel@tonic-gate 		i = istrue(y);
2557c478bd9Sstevel@tonic-gate 		tempfree(y);
2567c478bd9Sstevel@tonic-gate 		if (i) return (true);
2577c478bd9Sstevel@tonic-gate 		else return (false);
2587c478bd9Sstevel@tonic-gate 	case NOT:
2597c478bd9Sstevel@tonic-gate 		if (i) return (false);
2607c478bd9Sstevel@tonic-gate 		else return (true);
2617c478bd9Sstevel@tonic-gate 	default:
2627c478bd9Sstevel@tonic-gate 		error(FATAL, "unknown boolean operator %d", n);
2637c478bd9Sstevel@tonic-gate 	}
264dc5a8425Srobbin 	return (false);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 
270dc5a8425Srobbin CELL *
271dc5a8425Srobbin relop(NODE **a, int n)
2727c478bd9Sstevel@tonic-gate {
273dc5a8425Srobbin 	int i;
274dc5a8425Srobbin 	CELL *x, *y;
2757c478bd9Sstevel@tonic-gate 	awkfloat j;
276dc5a8425Srobbin 	wchar_t *xs, *ys;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
2827c478bd9Sstevel@tonic-gate 	y = execute(a[1]);
2837c478bd9Sstevel@tonic-gate 	if (x->tval&NUM && y->tval&NUM) {
2847c478bd9Sstevel@tonic-gate 		j = x->fval - y->fval;
2857c478bd9Sstevel@tonic-gate 		i = j<0? -1: (j>0? 1: 0);
2867c478bd9Sstevel@tonic-gate 	} else {
2877c478bd9Sstevel@tonic-gate 		xs = getsval(x);
2887c478bd9Sstevel@tonic-gate 		ys = getsval(y);
2897c478bd9Sstevel@tonic-gate 		if (xs && ys)
2907c478bd9Sstevel@tonic-gate 			i = wscoll(xs, ys);
2917c478bd9Sstevel@tonic-gate 		else
2927c478bd9Sstevel@tonic-gate 			return (false);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	tempfree(x);
2957c478bd9Sstevel@tonic-gate 	tempfree(y);
2967c478bd9Sstevel@tonic-gate 	switch (n) {
2977c478bd9Sstevel@tonic-gate 	case LT:	if (i<0) return (true);
2987c478bd9Sstevel@tonic-gate 			else return (false);
2997c478bd9Sstevel@tonic-gate 	case LE:	if (i<=0) return (true);
3007c478bd9Sstevel@tonic-gate 			else return (false);
3017c478bd9Sstevel@tonic-gate 	case NE:	if (i!=0) return (true);
3027c478bd9Sstevel@tonic-gate 			else return (false);
3037c478bd9Sstevel@tonic-gate 	case EQ:	if (i == 0) return (true);
3047c478bd9Sstevel@tonic-gate 			else return (false);
3057c478bd9Sstevel@tonic-gate 	case GE:	if (i>=0) return (true);
3067c478bd9Sstevel@tonic-gate 			else return (false);
3077c478bd9Sstevel@tonic-gate 	case GT:	if (i>0) return (true);
3087c478bd9Sstevel@tonic-gate 			else return (false);
3097c478bd9Sstevel@tonic-gate 	default:
3107c478bd9Sstevel@tonic-gate 		error(FATAL, "unknown relational operator %d", n);
3117c478bd9Sstevel@tonic-gate 	}
312dc5a8425Srobbin 	return (false);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 
322dc5a8425Srobbin CELL *
323dc5a8425Srobbin gettemp(void)
3247c478bd9Sstevel@tonic-gate {
325dc5a8425Srobbin 	int i;
326dc5a8425Srobbin 	CELL *x;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	for (i=0; i<MAXTMP; i++)
3327c478bd9Sstevel@tonic-gate 		if (tmps[i].tval == 0)
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 	if (i == MAXTMP)
3357c478bd9Sstevel@tonic-gate 		error(FATAL, "out of temporaries in gettemp");
3367c478bd9Sstevel@tonic-gate 	tmps[i] = tempcell;
3377c478bd9Sstevel@tonic-gate 	x = &tmps[i];
3387c478bd9Sstevel@tonic-gate 	return (x);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 
344dc5a8425Srobbin CELL *
345dc5a8425Srobbin indirect(NODE **a, int n)
3467c478bd9Sstevel@tonic-gate {
347dc5a8425Srobbin 	CELL *x;
348dc5a8425Srobbin 	int m;
3497c478bd9Sstevel@tonic-gate 	CELL *fieldadr();
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
3527c478bd9Sstevel@tonic-gate 	m = getfval(x);
3537c478bd9Sstevel@tonic-gate 	tempfree(x);
3547c478bd9Sstevel@tonic-gate 	x = fieldadr(m);
3557c478bd9Sstevel@tonic-gate 	x->ctype = OCELL;
3567c478bd9Sstevel@tonic-gate 	x->csub = CFLD;
3577c478bd9Sstevel@tonic-gate 	return (x);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 
363dc5a8425Srobbin CELL *
364dc5a8425Srobbin substr(NODE **a, int nnn)
3657c478bd9Sstevel@tonic-gate {
366dc5a8425Srobbin 	int k, m, n;
367dc5a8425Srobbin 	wchar_t *s, temp;
368dc5a8425Srobbin 	CELL *x, *y;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	y = execute(a[0]);
3717c478bd9Sstevel@tonic-gate 	s = getsval(y);
3727c478bd9Sstevel@tonic-gate 	k = wslen(s) + 1;
3737c478bd9Sstevel@tonic-gate 	if (k <= 1) {
3747c478bd9Sstevel@tonic-gate 		x = gettemp();
3757c478bd9Sstevel@tonic-gate 		setsval(x, L_NULL);
3767c478bd9Sstevel@tonic-gate 		return (x);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
3797c478bd9Sstevel@tonic-gate 	m = getfval(x);
3807c478bd9Sstevel@tonic-gate 	if (m <= 0)
3817c478bd9Sstevel@tonic-gate 		m = 1;
3827c478bd9Sstevel@tonic-gate 	else if (m > k)
3837c478bd9Sstevel@tonic-gate 		m = k;
3847c478bd9Sstevel@tonic-gate 	tempfree(x);
3857c478bd9Sstevel@tonic-gate 	if (a[2] != 0) {
3867c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
3877c478bd9Sstevel@tonic-gate 		n = getfval(x);
3887c478bd9Sstevel@tonic-gate 		tempfree(x);
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 	else
3917c478bd9Sstevel@tonic-gate 		n = k - 1;
3927c478bd9Sstevel@tonic-gate 	if (n < 0)
3937c478bd9Sstevel@tonic-gate 		n = 0;
3947c478bd9Sstevel@tonic-gate 	else if (n > k - m)
3957c478bd9Sstevel@tonic-gate 		n = k - m;
3967c478bd9Sstevel@tonic-gate 	dprintf("substr: m=%d, n=%d, s=%ws\n", m, n, s);
3977c478bd9Sstevel@tonic-gate 	x = gettemp();
3987c478bd9Sstevel@tonic-gate 	temp = s[n+m-1];
3997c478bd9Sstevel@tonic-gate 	s[n+m-1] = (wchar_t)0x0;
4007c478bd9Sstevel@tonic-gate 	setsval(x, s + m - 1);
4017c478bd9Sstevel@tonic-gate 	s[n+m-1] = temp;
4027c478bd9Sstevel@tonic-gate 	tempfree(y);
4037c478bd9Sstevel@tonic-gate 	return (x);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 
409dc5a8425Srobbin CELL *
410dc5a8425Srobbin sindex(NODE **a, int nnn)
4117c478bd9Sstevel@tonic-gate {
412dc5a8425Srobbin 	CELL *x;
413dc5a8425Srobbin 	wchar_t *s1, *s2, *p1, *p2, *q;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
4167c478bd9Sstevel@tonic-gate 	s1 = getsval(x);
4177c478bd9Sstevel@tonic-gate 	tempfree(x);
4187c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
4197c478bd9Sstevel@tonic-gate 	s2 = getsval(x);
4207c478bd9Sstevel@tonic-gate 	tempfree(x);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	x = gettemp();
4237c478bd9Sstevel@tonic-gate 	for (p1 = s1; *p1 != (wchar_t)0x0; p1++) {
4247c478bd9Sstevel@tonic-gate 		for (q=p1, p2=s2; *p2 != (wchar_t)0x0 && *q == *p2; q++, p2++)
4257c478bd9Sstevel@tonic-gate 			;
4267c478bd9Sstevel@tonic-gate 		if (*p2 == (wchar_t)0x0) {
4277c478bd9Sstevel@tonic-gate 			setfval(x, (awkfloat) (p1 - s1 + 1));	/* origin 1 */
4287c478bd9Sstevel@tonic-gate 			return (x);
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	setfval(x, 0.0);
4327c478bd9Sstevel@tonic-gate 	return (x);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 
438dc5a8425Srobbin wchar_t *
439dc5a8425Srobbin format(wchar_t *s, NODE *a)
4407c478bd9Sstevel@tonic-gate {
441dc5a8425Srobbin 	wchar_t *buf, *ep, *str;
442dc5a8425Srobbin 	wchar_t *p;
443dc5a8425Srobbin 	char *t;
4447c478bd9Sstevel@tonic-gate 	wchar_t *os;
4457c478bd9Sstevel@tonic-gate 	wchar_t tbuf[2*RECSIZE];
4467c478bd9Sstevel@tonic-gate 	char fmt[200];
447dc5a8425Srobbin 	CELL *x;
4487c478bd9Sstevel@tonic-gate 	int flag = 0;
4497c478bd9Sstevel@tonic-gate 	awkfloat xf;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	os = s;
4527c478bd9Sstevel@tonic-gate 	p = buf= (wchar_t *)malloc(RECSIZE * sizeof (wchar_t));
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	if (p == NULL)
4557c478bd9Sstevel@tonic-gate 		error(FATAL, "out of space in format");
4567c478bd9Sstevel@tonic-gate 	ep = p + RECSIZE;
4577c478bd9Sstevel@tonic-gate 	while (*s) {
4587c478bd9Sstevel@tonic-gate 		if (*s != '%') {
4597c478bd9Sstevel@tonic-gate 			*p++ = *s++;
4607c478bd9Sstevel@tonic-gate 			continue;
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 		if (*(s+1) == '%') {
4637c478bd9Sstevel@tonic-gate 			*p++ = '%';
4647c478bd9Sstevel@tonic-gate 			s += 2;
4657c478bd9Sstevel@tonic-gate 			continue;
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 		for (t=fmt; *s != '\0'; s++)
4687c478bd9Sstevel@tonic-gate 		{
4697c478bd9Sstevel@tonic-gate 			if (*s == 's' || *s == 'c')
4707c478bd9Sstevel@tonic-gate 				*t++ = 'w';
4717c478bd9Sstevel@tonic-gate 			*t++ = *s;
4727c478bd9Sstevel@tonic-gate 			if (*s >= 'a' && *s <= 'z' && *s != 'l')
4737c478bd9Sstevel@tonic-gate 				break;
4747c478bd9Sstevel@tonic-gate 			if (*s == '*') {
4757c478bd9Sstevel@tonic-gate 				if (a == NULL) {
4767c478bd9Sstevel@tonic-gate 					error(FATAL,
4777c478bd9Sstevel@tonic-gate 			"not enough arguments in printf(%ws) or sprintf(%ws)",
4787c478bd9Sstevel@tonic-gate 					os, os);
4797c478bd9Sstevel@tonic-gate 				}
4807c478bd9Sstevel@tonic-gate 				x = execute(a);
4817c478bd9Sstevel@tonic-gate 				a = a->nnext;
4827c478bd9Sstevel@tonic-gate 				sprintf(t-1, "%d", (int) getfval(x));
4837c478bd9Sstevel@tonic-gate 				t = fmt + strlen(fmt);
4847c478bd9Sstevel@tonic-gate 				tempfree(x);
4857c478bd9Sstevel@tonic-gate 			}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		}
4887c478bd9Sstevel@tonic-gate 		*t = '\0';
4897c478bd9Sstevel@tonic-gate 		if (t >= fmt + sizeof (fmt))
4907c478bd9Sstevel@tonic-gate 			error(FATAL, "format item %.20ws... too long", os);
4917c478bd9Sstevel@tonic-gate 		switch (*s) {
4927c478bd9Sstevel@tonic-gate 		case 'f': case 'e': case 'g':
4937c478bd9Sstevel@tonic-gate 			flag = 1;
4947c478bd9Sstevel@tonic-gate 			break;
4957c478bd9Sstevel@tonic-gate 		case 'd':
4967c478bd9Sstevel@tonic-gate 			flag = 2;
4977c478bd9Sstevel@tonic-gate 			if (*(s-1) == 'l') break;
4987c478bd9Sstevel@tonic-gate 			*(t-1) = 'l';
4997c478bd9Sstevel@tonic-gate 			*t = 'd';
5007c478bd9Sstevel@tonic-gate 			*++t = '\0';
5017c478bd9Sstevel@tonic-gate 			break;
5027c478bd9Sstevel@tonic-gate 		case 'o': case 'x':
5037c478bd9Sstevel@tonic-gate 			flag = *(s-1) == 'l' ? 2 : 3;
5047c478bd9Sstevel@tonic-gate 			break;
5057c478bd9Sstevel@tonic-gate 		case 'c':
5067c478bd9Sstevel@tonic-gate 			flag = 3;
5077c478bd9Sstevel@tonic-gate 			break;
5087c478bd9Sstevel@tonic-gate 		case 's':
5097c478bd9Sstevel@tonic-gate 			flag = 4;
5107c478bd9Sstevel@tonic-gate 			break;
5117c478bd9Sstevel@tonic-gate 		default:
5127c478bd9Sstevel@tonic-gate 			flag = 0;
5137c478bd9Sstevel@tonic-gate 			break;
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 		if (flag == 0) {
5167c478bd9Sstevel@tonic-gate 			wsprintf(p, "%s", fmt);
5177c478bd9Sstevel@tonic-gate 			p += wslen(p);
5187c478bd9Sstevel@tonic-gate 			continue;
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 		if (a == NULL) {
5217c478bd9Sstevel@tonic-gate 			error(FATAL,
5227c478bd9Sstevel@tonic-gate 	"not enough arguments in printf(%ws) or sprintf(%ws)", os, os);
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 		x = execute(a);
5257c478bd9Sstevel@tonic-gate 		a = a->nnext;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		/*
5287c478bd9Sstevel@tonic-gate 		 * Get the string to check length; %s is the usual problem;
5297c478bd9Sstevel@tonic-gate 		 * other conversions can cause overrun if they occur when
5307c478bd9Sstevel@tonic-gate 		 * the buffer is almost filled.
5317c478bd9Sstevel@tonic-gate 		 */
5327c478bd9Sstevel@tonic-gate 		if (flag == 4)	{ /* watch out for converting to numbers! */
5337c478bd9Sstevel@tonic-gate 			str = getsval(x);
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 		else {
5367c478bd9Sstevel@tonic-gate 			xf = getfval(x);
5377c478bd9Sstevel@tonic-gate 			if (flag == 1) wsprintf(tbuf, fmt, xf);
5387c478bd9Sstevel@tonic-gate 			else if (flag == 2) wsprintf(tbuf, fmt, (long)xf);
5397c478bd9Sstevel@tonic-gate 			else if (flag == 3) wsprintf(tbuf, fmt, (int)xf);
5407c478bd9Sstevel@tonic-gate 			if (wslen(tbuf) >= RECSIZE)
5417c478bd9Sstevel@tonic-gate 				error(FATAL, "formatted item %s... too long",
5427c478bd9Sstevel@tonic-gate 						tbuf);
5437c478bd9Sstevel@tonic-gate 			str = tbuf;
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 		/*
5467c478bd9Sstevel@tonic-gate 		 * If string overruns the buffer, reallocate;
5477c478bd9Sstevel@tonic-gate 		 * consider length of format string
5487c478bd9Sstevel@tonic-gate 		 */
5497c478bd9Sstevel@tonic-gate 		if (p + wslen(str) + wslen(s) + 1 >= ep) {
5507c478bd9Sstevel@tonic-gate 			int newlen, oldlen;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 			oldlen = p - buf;
5537c478bd9Sstevel@tonic-gate 			/* Add RECSIZE for additional space */
5547c478bd9Sstevel@tonic-gate 			newlen = oldlen + wslen(str) + RECSIZE;
5557c478bd9Sstevel@tonic-gate 			buf = realloc(buf, (unsigned) newlen * sizeof(wchar_t));
5567c478bd9Sstevel@tonic-gate 			if (buf == NULL)
5577c478bd9Sstevel@tonic-gate 				error(FATAL, "out of format space");
5587c478bd9Sstevel@tonic-gate 			p = buf + oldlen;
5597c478bd9Sstevel@tonic-gate 			ep = buf + newlen;
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 		/* Transfer string to buffer */
5627c478bd9Sstevel@tonic-gate 		if (flag == 4)
5637c478bd9Sstevel@tonic-gate 			wsprintf(p, fmt, str);
5647c478bd9Sstevel@tonic-gate 		else
5657c478bd9Sstevel@tonic-gate 			wscpy(p, str);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		tempfree(x);
5687c478bd9Sstevel@tonic-gate 		p += wslen(p);
5697c478bd9Sstevel@tonic-gate 		if (p >= ep)
5707c478bd9Sstevel@tonic-gate 			error(FATAL, "formatted string too long");
5717c478bd9Sstevel@tonic-gate 		s++;
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 	*p = '\0';
5747c478bd9Sstevel@tonic-gate 	return (buf);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 
578dc5a8425Srobbin CELL *
579dc5a8425Srobbin asprintf(NODE **a, int n)
5807c478bd9Sstevel@tonic-gate {
581dc5a8425Srobbin 	CELL *x;
582dc5a8425Srobbin 	NODE *y;
583dc5a8425Srobbin 	wchar_t *s;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	y = a[0]->nnext;
5867c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
5877c478bd9Sstevel@tonic-gate 	s = format(getsval(x), y);
5887c478bd9Sstevel@tonic-gate 	tempfree(x);
5897c478bd9Sstevel@tonic-gate 	x = gettemp();
5907c478bd9Sstevel@tonic-gate 	x->sval = s;
5917c478bd9Sstevel@tonic-gate 	x->tval = STR;
5927c478bd9Sstevel@tonic-gate 	return (x);
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 
596dc5a8425Srobbin CELL *
597dc5a8425Srobbin arith(NODE **a, int n)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate 	awkfloat i, j;
600dc5a8425Srobbin 	CELL *x, *y, *z;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
6037c478bd9Sstevel@tonic-gate 	i = getfval(x);
6047c478bd9Sstevel@tonic-gate 	tempfree(x);
6057c478bd9Sstevel@tonic-gate 	if (n != UMINUS) {
6067c478bd9Sstevel@tonic-gate 		y = execute(a[1]);
6077c478bd9Sstevel@tonic-gate 		j = getfval(y);
6087c478bd9Sstevel@tonic-gate 		tempfree(y);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 	z = gettemp();
6117c478bd9Sstevel@tonic-gate 	switch (n) {
6127c478bd9Sstevel@tonic-gate 	case ADD:
6137c478bd9Sstevel@tonic-gate 		i += j;
6147c478bd9Sstevel@tonic-gate 		break;
6157c478bd9Sstevel@tonic-gate 	case MINUS:
6167c478bd9Sstevel@tonic-gate 		i -= j;
6177c478bd9Sstevel@tonic-gate 		break;
6187c478bd9Sstevel@tonic-gate 	case MULT:
6197c478bd9Sstevel@tonic-gate 		i *= j;
6207c478bd9Sstevel@tonic-gate 		break;
6217c478bd9Sstevel@tonic-gate 	case DIVIDE:
6227c478bd9Sstevel@tonic-gate 		if (j == 0)
6237c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
6247c478bd9Sstevel@tonic-gate 		i /= j;
6257c478bd9Sstevel@tonic-gate 		break;
6267c478bd9Sstevel@tonic-gate 	case MOD:
6277c478bd9Sstevel@tonic-gate 		if (j == 0)
6287c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
6297c478bd9Sstevel@tonic-gate 		i = i - j*(long)(i/j);
6307c478bd9Sstevel@tonic-gate 		break;
6317c478bd9Sstevel@tonic-gate 	case UMINUS:
6327c478bd9Sstevel@tonic-gate 		i = -i;
6337c478bd9Sstevel@tonic-gate 		break;
6347c478bd9Sstevel@tonic-gate 	default:
6357c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal arithmetic operator %d", n);
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 	setfval(z, i);
6387c478bd9Sstevel@tonic-gate 	return (z);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 
644dc5a8425Srobbin CELL *
645dc5a8425Srobbin incrdecr(NODE **a, int n)
6467c478bd9Sstevel@tonic-gate {
647dc5a8425Srobbin 	CELL *x, *z;
648dc5a8425Srobbin 	int k;
6497c478bd9Sstevel@tonic-gate 	awkfloat xf;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
6527c478bd9Sstevel@tonic-gate 	xf = getfval(x);
6537c478bd9Sstevel@tonic-gate 	k = (n == PREINCR || n == POSTINCR) ? 1 : -1;
6547c478bd9Sstevel@tonic-gate 	if (n == PREINCR || n == PREDECR) {
6557c478bd9Sstevel@tonic-gate 		setfval(x, xf + k);
6567c478bd9Sstevel@tonic-gate 		return (x);
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 	z = gettemp();
6597c478bd9Sstevel@tonic-gate 	setfval(z, xf);
6607c478bd9Sstevel@tonic-gate 	setfval(x, xf + k);
6617c478bd9Sstevel@tonic-gate 	tempfree(x);
6627c478bd9Sstevel@tonic-gate 	return (z);
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 
667dc5a8425Srobbin CELL *
668dc5a8425Srobbin assign(NODE **a, int n)
6697c478bd9Sstevel@tonic-gate {
670dc5a8425Srobbin 	CELL *x, *y;
6717c478bd9Sstevel@tonic-gate 	awkfloat xf, yf;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
6777c478bd9Sstevel@tonic-gate 	y = execute(a[1]);
6787c478bd9Sstevel@tonic-gate 	if (n == ASSIGN) {	/* ordinary assignment */
6797c478bd9Sstevel@tonic-gate 		if ((y->tval & (STR|NUM)) == (STR|NUM)) {
6807c478bd9Sstevel@tonic-gate 			setsval(x, y->sval);
6817c478bd9Sstevel@tonic-gate 			x->fval = y->fval;
6827c478bd9Sstevel@tonic-gate 			x->tval |= NUM;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		} else if (y->tval & STR)
6857c478bd9Sstevel@tonic-gate 			setsval(x, y->sval);
6867c478bd9Sstevel@tonic-gate 		else if (y->tval & NUM)
6877c478bd9Sstevel@tonic-gate 			setfval(x, y->fval);
6887c478bd9Sstevel@tonic-gate 		tempfree(y);
6897c478bd9Sstevel@tonic-gate 		return (x);
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 	xf = getfval(x);
6927c478bd9Sstevel@tonic-gate 	yf = getfval(y);
6937c478bd9Sstevel@tonic-gate 	switch (n) {
6947c478bd9Sstevel@tonic-gate 	case ADDEQ:
6957c478bd9Sstevel@tonic-gate 		xf += yf;
6967c478bd9Sstevel@tonic-gate 		break;
6977c478bd9Sstevel@tonic-gate 	case SUBEQ:
6987c478bd9Sstevel@tonic-gate 		xf -= yf;
6997c478bd9Sstevel@tonic-gate 		break;
7007c478bd9Sstevel@tonic-gate 	case MULTEQ:
7017c478bd9Sstevel@tonic-gate 		xf *= yf;
7027c478bd9Sstevel@tonic-gate 		break;
7037c478bd9Sstevel@tonic-gate 	case DIVEQ:
7047c478bd9Sstevel@tonic-gate 		if (yf == 0)
7057c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
7067c478bd9Sstevel@tonic-gate 		xf /= yf;
7077c478bd9Sstevel@tonic-gate 		break;
7087c478bd9Sstevel@tonic-gate 	case MODEQ:
7097c478bd9Sstevel@tonic-gate 		if (yf == 0)
7107c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
7117c478bd9Sstevel@tonic-gate 		xf = xf - yf*(long)(xf/yf);
7127c478bd9Sstevel@tonic-gate 		break;
7137c478bd9Sstevel@tonic-gate 	default:
7147c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal assignment operator %d", n);
7157c478bd9Sstevel@tonic-gate 		break;
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 	tempfree(y);
7187c478bd9Sstevel@tonic-gate 	setfval(x, xf);
7197c478bd9Sstevel@tonic-gate 	return (x);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 
725dc5a8425Srobbin CELL *
726dc5a8425Srobbin cat(NODE **a, int q)
7277c478bd9Sstevel@tonic-gate {
728dc5a8425Srobbin 	CELL *x, *y, *z;
729dc5a8425Srobbin 	int n1, n2;
730dc5a8425Srobbin 	wchar_t *s;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
7367c478bd9Sstevel@tonic-gate 	y = execute(a[1]);
7377c478bd9Sstevel@tonic-gate 	getsval(x);
7387c478bd9Sstevel@tonic-gate 	getsval(y);
7397c478bd9Sstevel@tonic-gate 	n1 = wslen(x->sval);
7407c478bd9Sstevel@tonic-gate 	n2 = wslen(y->sval);
7417c478bd9Sstevel@tonic-gate 	if ((s = (wchar_t *) malloc((n1 + n2 + 1) * sizeof (wchar_t))) == NULL)
7427c478bd9Sstevel@tonic-gate 		error(FATAL, "out of space in cat");
7437c478bd9Sstevel@tonic-gate 	wscpy(s, x->sval);
7447c478bd9Sstevel@tonic-gate 	wscpy(s+n1, y->sval);
7457c478bd9Sstevel@tonic-gate 	tempfree(y);
7467c478bd9Sstevel@tonic-gate 	z = gettemp();
7477c478bd9Sstevel@tonic-gate 	z->sval = s;
7487c478bd9Sstevel@tonic-gate 	z->tval = STR;
7497c478bd9Sstevel@tonic-gate 	tempfree(x);
7507c478bd9Sstevel@tonic-gate 	return (z);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 
756dc5a8425Srobbin CELL *
757dc5a8425Srobbin pastat(NODE **a, int n)
7587c478bd9Sstevel@tonic-gate {
759dc5a8425Srobbin 	CELL *x;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	if (a[0] == 0)
7657c478bd9Sstevel@tonic-gate 		x = true;
7667c478bd9Sstevel@tonic-gate 	else
7677c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
7687c478bd9Sstevel@tonic-gate 	if (istrue(x)) {
7697c478bd9Sstevel@tonic-gate 		tempfree(x);
7707c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 	return (x);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 
778dc5a8425Srobbin CELL *
779dc5a8425Srobbin dopa2(NODE **a, int n)
7807c478bd9Sstevel@tonic-gate {
781dc5a8425Srobbin 	CELL *x;
782dc5a8425Srobbin 	int pair;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	pair = (int) a[3];
7887c478bd9Sstevel@tonic-gate 	if (pairstack[pair] == 0) {
7897c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
7907c478bd9Sstevel@tonic-gate 		if (istrue(x))
7917c478bd9Sstevel@tonic-gate 			pairstack[pair] = 1;
7927c478bd9Sstevel@tonic-gate 		tempfree(x);
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 	if (pairstack[pair] == 1) {
7957c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
7967c478bd9Sstevel@tonic-gate 		if (istrue(x))
7977c478bd9Sstevel@tonic-gate 			pairstack[pair] = 0;
7987c478bd9Sstevel@tonic-gate 		tempfree(x);
7997c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
8007c478bd9Sstevel@tonic-gate 		return (x);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 	return (false);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 
808dc5a8425Srobbin CELL *
809dc5a8425Srobbin aprintf(NODE **a, int n)
8107c478bd9Sstevel@tonic-gate {
811dc5a8425Srobbin 	CELL *x;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	x = asprintf(a, n);
8177c478bd9Sstevel@tonic-gate 	if (a[1] == NULL) {
8187c478bd9Sstevel@tonic-gate 		printf("%ws", x->sval);
8197c478bd9Sstevel@tonic-gate 		tempfree(x);
8207c478bd9Sstevel@tonic-gate 		return (true);
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 	redirprint(x->sval, (int)a[1], a[2]);
8237c478bd9Sstevel@tonic-gate 	return (x);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 
829dc5a8425Srobbin CELL *
830dc5a8425Srobbin split(NODE **a, int nnn)
8317c478bd9Sstevel@tonic-gate {
832dc5a8425Srobbin 	CELL *x;
833dc5a8425Srobbin 	CELL *ap;
834dc5a8425Srobbin 	wchar_t *s, *p, c;
8357c478bd9Sstevel@tonic-gate 	wchar_t *t, temp, num[5];
836dc5a8425Srobbin 	wchar_t sep;
8377c478bd9Sstevel@tonic-gate 	int n, flag;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
8437c478bd9Sstevel@tonic-gate 	s = getsval(x);
8447c478bd9Sstevel@tonic-gate 	tempfree(x);
8457c478bd9Sstevel@tonic-gate 	if (a[2] == 0)
8467c478bd9Sstevel@tonic-gate 		sep = **FS;
8477c478bd9Sstevel@tonic-gate 	else {
8487c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
8497c478bd9Sstevel@tonic-gate 		sep = getsval(x)[0];
8507c478bd9Sstevel@tonic-gate 		tempfree(x);
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 	ap = (CELL *) a[1];
8537c478bd9Sstevel@tonic-gate 	freesymtab(ap);
8547c478bd9Sstevel@tonic-gate 	dprintf("split: s=|%ws|, a=%ws, sep=|%wc|\n", s, ap->nval, sep);
8557c478bd9Sstevel@tonic-gate 	ap->tval &= ~STR;
8567c478bd9Sstevel@tonic-gate 	ap->tval |= ARR;
8577c478bd9Sstevel@tonic-gate 	ap->sval = (wchar_t *) makesymtab();
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	n = 0;
8637c478bd9Sstevel@tonic-gate 	if (sep == ' ')
8647c478bd9Sstevel@tonic-gate 		for (n = 0; /* dummy */; /* dummy */) {
8657c478bd9Sstevel@tonic-gate 			c = *s;
8667c478bd9Sstevel@tonic-gate 			while (iswblank(c) || c == '\t' || c == '\n')
8677c478bd9Sstevel@tonic-gate 				c = *(++s);
8687c478bd9Sstevel@tonic-gate 			if (*s == 0)
8697c478bd9Sstevel@tonic-gate 				break;
8707c478bd9Sstevel@tonic-gate 			n++;
8717c478bd9Sstevel@tonic-gate 			t = s;
8727c478bd9Sstevel@tonic-gate 			do
8737c478bd9Sstevel@tonic-gate 				c = *(++s);
8747c478bd9Sstevel@tonic-gate 			while (! iswblank(c) && c != '\t' &&
8757c478bd9Sstevel@tonic-gate 				c != '\n' && c != '\0');
8767c478bd9Sstevel@tonic-gate 			temp = c;
8777c478bd9Sstevel@tonic-gate 			*s = (wchar_t)0x0;
8787c478bd9Sstevel@tonic-gate 			wsprintf(num, "%d", n);
8797c478bd9Sstevel@tonic-gate 			if (isanumber(t))
8807c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t),
8817c478bd9Sstevel@tonic-gate 						watof(t), STR|NUM, ap->sval);
8827c478bd9Sstevel@tonic-gate 			else
8837c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t), 0.0, STR, ap->sval);
8847c478bd9Sstevel@tonic-gate 			*s = temp;
8857c478bd9Sstevel@tonic-gate 			if (*s != 0)
8867c478bd9Sstevel@tonic-gate 				s++;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	} else if (*s != 0)
8897c478bd9Sstevel@tonic-gate 		for (;;) {
8907c478bd9Sstevel@tonic-gate 			n++;
8917c478bd9Sstevel@tonic-gate 			t = s;
8927c478bd9Sstevel@tonic-gate 			while ((c = *s) != sep && c != '\n' && c != '\0')
8937c478bd9Sstevel@tonic-gate 				s++;
8947c478bd9Sstevel@tonic-gate 			temp = c;
8957c478bd9Sstevel@tonic-gate 			*s = (wchar_t)0x0;
8967c478bd9Sstevel@tonic-gate 			wsprintf(num, "%d", n);
8977c478bd9Sstevel@tonic-gate 			if (isanumber(t))
8987c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t),
8997c478bd9Sstevel@tonic-gate 						watof(t), STR|NUM, ap->sval);
9007c478bd9Sstevel@tonic-gate 			else
9017c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t), 0.0, STR, ap->sval);
9027c478bd9Sstevel@tonic-gate 			*s = temp;
9037c478bd9Sstevel@tonic-gate 			if (*s++ == 0)
9047c478bd9Sstevel@tonic-gate 				break;
9057c478bd9Sstevel@tonic-gate 		}
9067c478bd9Sstevel@tonic-gate 	x = gettemp();
9077c478bd9Sstevel@tonic-gate 	x->tval = NUM;
9087c478bd9Sstevel@tonic-gate 	x->fval = n;
9097c478bd9Sstevel@tonic-gate 	return (x);
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 
915dc5a8425Srobbin CELL *
916dc5a8425Srobbin ifstat(NODE **a, int n)
9177c478bd9Sstevel@tonic-gate {
918dc5a8425Srobbin 	CELL *x;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
9247c478bd9Sstevel@tonic-gate 	if (istrue(x)) {
9257c478bd9Sstevel@tonic-gate 		tempfree(x);
9267c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	} else if (a[2] != 0) {
9297c478bd9Sstevel@tonic-gate 		tempfree(x);
9307c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 	return (x);
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 
938dc5a8425Srobbin CELL *
939dc5a8425Srobbin whilestat(NODE **a, int n)
9407c478bd9Sstevel@tonic-gate {
941dc5a8425Srobbin 	CELL *x;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	for (;;) {
9477c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
9487c478bd9Sstevel@tonic-gate 		if (!istrue(x)) return (x);
9497c478bd9Sstevel@tonic-gate 		tempfree(x);
9507c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
9517c478bd9Sstevel@tonic-gate 		if (isbreak(x)) {
9527c478bd9Sstevel@tonic-gate 			x = true;
9537c478bd9Sstevel@tonic-gate 			return (x);
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 		if (isnext(x) || isexit(x))
9567c478bd9Sstevel@tonic-gate 			return (x);
9577c478bd9Sstevel@tonic-gate 		tempfree(x);
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate }
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 
964dc5a8425Srobbin CELL *
965dc5a8425Srobbin forstat(NODE **a, int n)
9667c478bd9Sstevel@tonic-gate {
967dc5a8425Srobbin 	CELL *x;
968dc5a8425Srobbin 	CELL *z;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	z = execute(a[0]);
9747c478bd9Sstevel@tonic-gate 	tempfree(z);
9757c478bd9Sstevel@tonic-gate 	for (;;) {
9767c478bd9Sstevel@tonic-gate 		if (a[1]!=0) {
9777c478bd9Sstevel@tonic-gate 			x = execute(a[1]);
9787c478bd9Sstevel@tonic-gate 			if (!istrue(x)) return (x);
9797c478bd9Sstevel@tonic-gate 			else tempfree(x);
9807c478bd9Sstevel@tonic-gate 		}
9817c478bd9Sstevel@tonic-gate 		x = execute(a[3]);
9827c478bd9Sstevel@tonic-gate 		if (isbreak(x)) {	/* turn off break */
9837c478bd9Sstevel@tonic-gate 			x = true;
9847c478bd9Sstevel@tonic-gate 			return (x);
9857c478bd9Sstevel@tonic-gate 		}
9867c478bd9Sstevel@tonic-gate 		if (isnext(x) || isexit(x))
9877c478bd9Sstevel@tonic-gate 			return (x);
9887c478bd9Sstevel@tonic-gate 		tempfree(x);
9897c478bd9Sstevel@tonic-gate 		z = execute(a[2]);
9907c478bd9Sstevel@tonic-gate 		tempfree(z);
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 
997dc5a8425Srobbin CELL *
998dc5a8425Srobbin instat(NODE **a, int n)
9997c478bd9Sstevel@tonic-gate {
1000dc5a8425Srobbin 	CELL *vp, *arrayp, *cp, **tp;
1001dc5a8425Srobbin 	CELL *x;
10027c478bd9Sstevel@tonic-gate 	int i;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	vp = (CELL *) a[0];
10087c478bd9Sstevel@tonic-gate 	arrayp = (CELL *) a[1];
10097c478bd9Sstevel@tonic-gate 	if (!(arrayp->tval & ARR))
10107c478bd9Sstevel@tonic-gate 		error(FATAL, "%ws is not an array", arrayp->nval);
10117c478bd9Sstevel@tonic-gate 	tp = (CELL **) arrayp->sval;
10127c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAXSYM; i++) {	/* this routine knows too much */
10137c478bd9Sstevel@tonic-gate 		for (cp = tp[i]; cp != NULL; cp = cp->nextval) {
10147c478bd9Sstevel@tonic-gate 			setsval(vp, cp->nval);
10157c478bd9Sstevel@tonic-gate 			x = execute(a[2]);
10167c478bd9Sstevel@tonic-gate 			if (isbreak(x)) {
10177c478bd9Sstevel@tonic-gate 				x = true;
10187c478bd9Sstevel@tonic-gate 				return (x);
10197c478bd9Sstevel@tonic-gate 			}
10207c478bd9Sstevel@tonic-gate 			if (isnext(x) || isexit(x))
10217c478bd9Sstevel@tonic-gate 				return (x);
10227c478bd9Sstevel@tonic-gate 			tempfree(x);
10237c478bd9Sstevel@tonic-gate 		}
10247c478bd9Sstevel@tonic-gate 	}
10257c478bd9Sstevel@tonic-gate 	return (true);
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 
1031dc5a8425Srobbin CELL *
1032dc5a8425Srobbin jump(NODE **a, int n)
10337c478bd9Sstevel@tonic-gate {
1034dc5a8425Srobbin 	CELL *y;
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	switch (n) {
10407c478bd9Sstevel@tonic-gate 	case EXIT:
10417c478bd9Sstevel@tonic-gate 		if (a[0] != 0) {
10427c478bd9Sstevel@tonic-gate 			y = execute(a[0]);
10437c478bd9Sstevel@tonic-gate 			errorflag = getfval(y);
10447c478bd9Sstevel@tonic-gate 		}
10457c478bd9Sstevel@tonic-gate 		return (jexit);
10467c478bd9Sstevel@tonic-gate 	case NEXT:
10477c478bd9Sstevel@tonic-gate 		return (jnext);
10487c478bd9Sstevel@tonic-gate 	case BREAK:
10497c478bd9Sstevel@tonic-gate 		return (jbreak);
10507c478bd9Sstevel@tonic-gate 	case CONTINUE:
10517c478bd9Sstevel@tonic-gate 		return (jcont);
10527c478bd9Sstevel@tonic-gate 	default:
10537c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal jump type %d", n);
10547c478bd9Sstevel@tonic-gate 	}
1055dc5a8425Srobbin 	return (NULL);
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 
1061dc5a8425Srobbin CELL *
1062dc5a8425Srobbin fncn(NODE **a, int n)
10637c478bd9Sstevel@tonic-gate {
1064dc5a8425Srobbin 	CELL *x;
10657c478bd9Sstevel@tonic-gate 	awkfloat u;
1066dc5a8425Srobbin 	int t;
1067dc5a8425Srobbin 	wchar_t *wp;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	t = (int) a[0];
10707c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
10717c478bd9Sstevel@tonic-gate 	if (t == FLENGTH)
10727c478bd9Sstevel@tonic-gate 		u = (awkfloat) wslen(getsval(x));
10737c478bd9Sstevel@tonic-gate 	else if (t == FLOG)
10747c478bd9Sstevel@tonic-gate 		u = log(getfval(x));
10757c478bd9Sstevel@tonic-gate 	else if (t == FINT)
10767c478bd9Sstevel@tonic-gate 		u = (awkfloat) (long) getfval(x);
10777c478bd9Sstevel@tonic-gate 	else if (t == FEXP)
10787c478bd9Sstevel@tonic-gate 		u = exp(getfval(x));
10797c478bd9Sstevel@tonic-gate 	else if (t == FSQRT)
10807c478bd9Sstevel@tonic-gate 		u = sqrt(getfval(x));
10817c478bd9Sstevel@tonic-gate 	else
10827c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal function type %d", t);
10837c478bd9Sstevel@tonic-gate 	tempfree(x);
10847c478bd9Sstevel@tonic-gate 	x = gettemp();
10857c478bd9Sstevel@tonic-gate 	setfval(x, u);
10867c478bd9Sstevel@tonic-gate 	return (x);
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 
1092dc5a8425Srobbin CELL *
1093dc5a8425Srobbin print(NODE **a, int n)
10947c478bd9Sstevel@tonic-gate {
1095dc5a8425Srobbin 	NODE *x;
1096dc5a8425Srobbin 	CELL *y;
10977c478bd9Sstevel@tonic-gate 	wchar_t s[RECSIZE];
1098*aba1133aSnakanon 	wchar_t *ss, *bp, *ep, *os;
1099*aba1133aSnakanon 	size_t	blen, newlen, sslen, orslen, ofslen, oslen;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	s[0] = '\0';
11027c478bd9Sstevel@tonic-gate 	bp = s;
11037c478bd9Sstevel@tonic-gate 	ep = s + RECSIZE;
1104*aba1133aSnakanon 
1105*aba1133aSnakanon 	blen = 0;
1106*aba1133aSnakanon 	orslen = wcslen(*ORS);
1107*aba1133aSnakanon 	ofslen = wcslen(*OFS);
1108*aba1133aSnakanon 
11097c478bd9Sstevel@tonic-gate 	for (x = a[0]; x != NULL; x = x->nnext) {
11107c478bd9Sstevel@tonic-gate 		y = execute(x);
11117c478bd9Sstevel@tonic-gate 		ss = getsval(y);
11127c478bd9Sstevel@tonic-gate 
1113*aba1133aSnakanon 		/* total new length will be */
1114*aba1133aSnakanon 		sslen = wcslen(ss);
1115*aba1133aSnakanon 		if (x->nnext == NULL) {
1116*aba1133aSnakanon 			os = *ORS;
1117*aba1133aSnakanon 			oslen = orslen;
1118*aba1133aSnakanon 		} else {
1119*aba1133aSnakanon 			os = *OFS;
1120*aba1133aSnakanon 			oslen = ofslen;
1121*aba1133aSnakanon 		}
1122*aba1133aSnakanon 		newlen = blen + sslen + oslen;
1123*aba1133aSnakanon 
1124*aba1133aSnakanon 		/* allocate larger buffer if needed */
1125*aba1133aSnakanon 		if (ep < (bp + newlen + 1)) {
1126*aba1133aSnakanon 			wchar_t	*oldbp = bp;
1127*aba1133aSnakanon 
1128*aba1133aSnakanon 			if (oldbp == s)
1129*aba1133aSnakanon 				bp = NULL;
1130*aba1133aSnakanon 			bp = realloc(bp, sizeof (wchar_t) * (newlen + 1));
11317c478bd9Sstevel@tonic-gate 			if (bp == NULL)
11327c478bd9Sstevel@tonic-gate 				error(FATAL, "out of space in print");
1133*aba1133aSnakanon 			ep = bp + newlen + 1;
1134*aba1133aSnakanon 			if (oldbp == s)
1135*aba1133aSnakanon 				(void) wmemcpy(bp, oldbp, blen);
11367c478bd9Sstevel@tonic-gate 		}
1137*aba1133aSnakanon 		(void) wmemcpy(bp + blen, ss, sslen);
1138*aba1133aSnakanon 		(void) wmemcpy(bp + blen + sslen, os, oslen);
11397c478bd9Sstevel@tonic-gate 		tempfree(y);
1140*aba1133aSnakanon 		blen = newlen;
1141*aba1133aSnakanon 		bp[blen] = '\0';
11427c478bd9Sstevel@tonic-gate 	}
1143*aba1133aSnakanon 	if (a[1] == NULL) {
1144*aba1133aSnakanon 		(void) printf("%ws", bp);
11457c478bd9Sstevel@tonic-gate 		if (bp != s)
11467c478bd9Sstevel@tonic-gate 			free(bp);
11477c478bd9Sstevel@tonic-gate 		return (true);
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	redirprint(bp, (int)a[1], a[2]);
11517c478bd9Sstevel@tonic-gate 	if (bp != s)
11527c478bd9Sstevel@tonic-gate 		free(bp);
11537c478bd9Sstevel@tonic-gate 	return (false);
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 
1158dc5a8425Srobbin CELL *
1159dc5a8425Srobbin nullproc(void)
11607c478bd9Sstevel@tonic-gate {
1161dc5a8425Srobbin 	return (NULL);
1162dc5a8425Srobbin }
1163dc5a8425Srobbin 
1164dc5a8425Srobbin 
1165dc5a8425Srobbin 
1166dc5a8425Srobbin CELL *
1167dc5a8425Srobbin nodetoobj(NODE *a)
1168dc5a8425Srobbin {
1169dc5a8425Srobbin 	CELL *x;
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	x= (CELL *) a->nobj;
11727c478bd9Sstevel@tonic-gate 	x->ctype = OCELL;
11737c478bd9Sstevel@tonic-gate 	x->csub = a->subtype;
11747c478bd9Sstevel@tonic-gate 	if (isfld(x))
11757c478bd9Sstevel@tonic-gate 		fldbld();
11767c478bd9Sstevel@tonic-gate 	return (x);
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate 
1179dc5a8425Srobbin static void
1180dc5a8425Srobbin redirprint(wchar_t *s, int a, NODE *b)
11817c478bd9Sstevel@tonic-gate {
1182dc5a8425Srobbin 	int i;
1183dc5a8425Srobbin 	CELL *x;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	x = execute(b);
11867c478bd9Sstevel@tonic-gate 	getsval(x);
11877c478bd9Sstevel@tonic-gate 	for (i=0; i<FILENUM; i++)
11887c478bd9Sstevel@tonic-gate 		if (files[i].fname && wscmp(x->sval, files[i].fname) == 0)
11897c478bd9Sstevel@tonic-gate 			goto doit;
11907c478bd9Sstevel@tonic-gate 	for (i=0; i<FILENUM; i++)
11917c478bd9Sstevel@tonic-gate 		if (files[i].fp == 0)
11927c478bd9Sstevel@tonic-gate 			break;
11937c478bd9Sstevel@tonic-gate 	if (i >= FILENUM)
11947c478bd9Sstevel@tonic-gate 		error(FATAL, "too many output files %d", i);
11957c478bd9Sstevel@tonic-gate 	if (a == '|')	/* a pipe! */
11967c478bd9Sstevel@tonic-gate 		files[i].fp = popen(toeuccode(x->sval), "w");
11977c478bd9Sstevel@tonic-gate 	else if (a == APPEND)
11987c478bd9Sstevel@tonic-gate 		files[i].fp = fopen(toeuccode(x->sval), "a");
11997c478bd9Sstevel@tonic-gate 	else if (a == GT)
12007c478bd9Sstevel@tonic-gate 		files[i].fp = fopen(toeuccode(x->sval), "w");
12017c478bd9Sstevel@tonic-gate 	else
12027c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal redirection near line %lld", lineno);
12037c478bd9Sstevel@tonic-gate 	if (files[i].fp == NULL)
12047c478bd9Sstevel@tonic-gate 		error(FATAL, "can't open file %ws", x->sval);
12057c478bd9Sstevel@tonic-gate 	files[i].fname = tostring(x->sval);
12067c478bd9Sstevel@tonic-gate 	files[i].type = a;
12077c478bd9Sstevel@tonic-gate doit:
12087c478bd9Sstevel@tonic-gate 	fprintf(files[i].fp, "%ws", s);
12097c478bd9Sstevel@tonic-gate #ifndef gcos
12107c478bd9Sstevel@tonic-gate 	fflush(files[i].fp);	/* in case someone is waiting for the output */
12117c478bd9Sstevel@tonic-gate #endif
12127c478bd9Sstevel@tonic-gate 	tempfree(x);
12137c478bd9Sstevel@tonic-gate }
1214