xref: /titanic_54/usr/src/cmd/oawk/run.c (revision d09832051bb4b41ce2b3202c09fceedc089678af)
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
5*d0983205SRoger A. Faulkner  * Common Development and Distribution License (the "License").
6*d0983205SRoger 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*d0983205SRoger A. Faulkner  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27dc5a8425Srobbin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28dc5a8425Srobbin /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #define	DEBUG
317c478bd9Sstevel@tonic-gate #define	tempfree(a)	{if (istemp(a)) {xfree(a->sval); a->tval = 0; }}
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include	"awk.def"
347c478bd9Sstevel@tonic-gate #include	"math.h"
357c478bd9Sstevel@tonic-gate #include	"awk.h"
367c478bd9Sstevel@tonic-gate #include	"stdio.h"
377c478bd9Sstevel@tonic-gate #include "ctype.h"
387c478bd9Sstevel@tonic-gate #include "wctype.h"
397c478bd9Sstevel@tonic-gate #include "awktype.h"
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	RECSIZE BUFSIZ
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	FILENUM	10
457c478bd9Sstevel@tonic-gate struct
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	FILE *fp;
487c478bd9Sstevel@tonic-gate 	int type;
497c478bd9Sstevel@tonic-gate 	wchar_t *fname;
507c478bd9Sstevel@tonic-gate } files[FILENUM];
517c478bd9Sstevel@tonic-gate FILE *popen();
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate extern CELL *execute(), *nodetoobj(), *fieldel(), *dopa2(), *gettemp();
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	PA2NUM	29
567c478bd9Sstevel@tonic-gate int	pairstack[PA2NUM], paircnt;
577c478bd9Sstevel@tonic-gate NODE	*winner = NULL;
587c478bd9Sstevel@tonic-gate #define	MAXTMP	20
597c478bd9Sstevel@tonic-gate CELL	tmps[MAXTMP];
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static CELL	truecell	={ OBOOL, BTRUE, 0, 0, 0.0, NUM, 0 };
627c478bd9Sstevel@tonic-gate CELL	*true	= &truecell;
637c478bd9Sstevel@tonic-gate static CELL	falsecell	={ OBOOL, BFALSE, 0, 0, 0.0, NUM, 0 };
647c478bd9Sstevel@tonic-gate CELL	*false	= &falsecell;
657c478bd9Sstevel@tonic-gate static CELL	breakcell	={ OJUMP, JBREAK, 0, 0, 0.0, NUM, 0 };
667c478bd9Sstevel@tonic-gate CELL	*jbreak	= &breakcell;
677c478bd9Sstevel@tonic-gate static CELL	contcell	={ OJUMP, JCONT, 0, 0, 0.0, NUM, 0 };
687c478bd9Sstevel@tonic-gate CELL	*jcont	= &contcell;
697c478bd9Sstevel@tonic-gate static CELL	nextcell	={ OJUMP, JNEXT, 0, 0, 0.0, NUM, 0 };
707c478bd9Sstevel@tonic-gate CELL	*jnext	= &nextcell;
717c478bd9Sstevel@tonic-gate static CELL	exitcell	={ OJUMP, JEXIT, 0, 0, 0.0, NUM, 0 };
727c478bd9Sstevel@tonic-gate CELL	*jexit	= &exitcell;
737c478bd9Sstevel@tonic-gate static CELL	tempcell	={ OCELL, CTEMP, 0, 0, 0.0, NUM, 0 };
747c478bd9Sstevel@tonic-gate 
75dc5a8425Srobbin static void redirprint(wchar_t *s, int a, NODE *b);
76dc5a8425Srobbin 
77dc5a8425Srobbin void freesymtab(CELL *ap);
78dc5a8425Srobbin void fldbld(void);
79dc5a8425Srobbin 
80dc5a8425Srobbin void
81dc5a8425Srobbin run(NODE *a)
827c478bd9Sstevel@tonic-gate {
83dc5a8425Srobbin 	int i;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	execute(a);
867c478bd9Sstevel@tonic-gate 	/* Wait for children to complete if output to a pipe. */
877c478bd9Sstevel@tonic-gate 	for (i=0; i<FILENUM; i++)
887c478bd9Sstevel@tonic-gate 		if (files[i].fp && files[i].type == '|')
897c478bd9Sstevel@tonic-gate 			pclose(files[i].fp);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 
93dc5a8425Srobbin CELL *
94dc5a8425Srobbin execute(NODE *u)
957c478bd9Sstevel@tonic-gate {
96dc5a8425Srobbin 	CELL *(*proc)();
97dc5a8425Srobbin 	CELL *x;
98dc5a8425Srobbin 	NODE *a;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (u == NULL)
1017c478bd9Sstevel@tonic-gate 		return (true);
1027c478bd9Sstevel@tonic-gate 	for (a = u; /* dummy */; a = a->nnext) {
1037c478bd9Sstevel@tonic-gate 		if (cantexec(a))
1047c478bd9Sstevel@tonic-gate 			return (nodetoobj(a));
1057c478bd9Sstevel@tonic-gate 		if (notlegal(a->nobj))
1067c478bd9Sstevel@tonic-gate 			error(FATAL, "illegal statement %o", a);
1077c478bd9Sstevel@tonic-gate 		proc = proctab[a->nobj-FIRSTTOKEN];
1087c478bd9Sstevel@tonic-gate 		x = (*proc)(a->narg, a->nobj);
1097c478bd9Sstevel@tonic-gate 		if (isfld(x))
1107c478bd9Sstevel@tonic-gate 			fldbld();
1117c478bd9Sstevel@tonic-gate 		if (isexpr(a))
1127c478bd9Sstevel@tonic-gate 			return (x);
1137c478bd9Sstevel@tonic-gate 		/* a statement, goto next statement */
1147c478bd9Sstevel@tonic-gate 		if (isjump(x))
1157c478bd9Sstevel@tonic-gate 			return (x);
1167c478bd9Sstevel@tonic-gate 		if (a->nnext == (NODE *)NULL)
1177c478bd9Sstevel@tonic-gate 			return (x);
1187c478bd9Sstevel@tonic-gate 		tempfree(x);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 
125dc5a8425Srobbin CELL *
126dc5a8425Srobbin program(NODE **a, int n)
1277c478bd9Sstevel@tonic-gate {
128dc5a8425Srobbin 	CELL *x;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (a[0] != NULL) {
1317c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
1327c478bd9Sstevel@tonic-gate 		if (isexit(x))
1337c478bd9Sstevel@tonic-gate 			return (true);
1347c478bd9Sstevel@tonic-gate 		if (isjump(x))
1357c478bd9Sstevel@tonic-gate 			error(FATAL, "unexpected break, continue or next");
1367c478bd9Sstevel@tonic-gate 		tempfree(x);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	while (getrec()) {
1397c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
1407c478bd9Sstevel@tonic-gate 		if (isexit(x)) {
1417c478bd9Sstevel@tonic-gate 			tempfree(x);
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 		tempfree(x);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	if (a[2] != NULL) {
1477c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
1487c478bd9Sstevel@tonic-gate 		if (isbreak(x) || isnext(x) || iscont(x))
1497c478bd9Sstevel@tonic-gate 			error(FATAL, "unexpected break, continue or next");
1507c478bd9Sstevel@tonic-gate 		tempfree(x);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	return (true);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 
158dc5a8425Srobbin CELL *
159dc5a8425Srobbin getline(void)
1607c478bd9Sstevel@tonic-gate {
161dc5a8425Srobbin 	CELL *x;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	x = gettemp();
1647c478bd9Sstevel@tonic-gate 	setfval(x, (awkfloat) getrec());
1657c478bd9Sstevel@tonic-gate 	return (x);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 
171dc5a8425Srobbin CELL *
172dc5a8425Srobbin array(NODE **a, int n)
1737c478bd9Sstevel@tonic-gate {
174dc5a8425Srobbin 	CELL *x, *y;
1757c478bd9Sstevel@tonic-gate 	extern CELL *arrayel();
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
1787c478bd9Sstevel@tonic-gate 	y = arrayel(a[0], x);
1797c478bd9Sstevel@tonic-gate 	tempfree(x);
1807c478bd9Sstevel@tonic-gate 	return (y);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 
186dc5a8425Srobbin CELL *
187dc5a8425Srobbin arrayel(NODE *a, CELL *b)
1887c478bd9Sstevel@tonic-gate {
189dc5a8425Srobbin 	wchar_t *s;
190dc5a8425Srobbin 	CELL *x;
191dc5a8425Srobbin 	int i;
192dc5a8425Srobbin 	CELL *y;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	s = getsval(b);
1957c478bd9Sstevel@tonic-gate 	x = (CELL *) a;
1967c478bd9Sstevel@tonic-gate 	if (!(x->tval&ARR)) {
1977c478bd9Sstevel@tonic-gate 		xfree(x->sval);
1987c478bd9Sstevel@tonic-gate 		x->tval &= ~STR;
1997c478bd9Sstevel@tonic-gate 		x->tval |= ARR;
2007c478bd9Sstevel@tonic-gate 		x->sval = (wchar_t *) makesymtab();
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 	y = setsymtab(s, tostring(L_NULL), 0.0, STR|NUM, x->sval);
2037c478bd9Sstevel@tonic-gate 	y->ctype = OCELL;
2047c478bd9Sstevel@tonic-gate 	y->csub = CVAR;
2057c478bd9Sstevel@tonic-gate 	return (y);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
208dc5a8425Srobbin CELL *
209dc5a8425Srobbin matchop(NODE **a, int n)
2107c478bd9Sstevel@tonic-gate {
211dc5a8425Srobbin 	CELL *x;
212dc5a8425Srobbin 	wchar_t *s;
213dc5a8425Srobbin 	int i;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
2167c478bd9Sstevel@tonic-gate 	s = getsval(x);
2177c478bd9Sstevel@tonic-gate 	tempfree(x);
2187c478bd9Sstevel@tonic-gate 	i = match(a[1], s);
2197c478bd9Sstevel@tonic-gate 	if (n == MATCH && i == 1 || n == NOTMATCH && i == 0)
2207c478bd9Sstevel@tonic-gate 		return (true);
2217c478bd9Sstevel@tonic-gate 	else
2227c478bd9Sstevel@tonic-gate 		return (false);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 
228dc5a8425Srobbin CELL *
229dc5a8425Srobbin boolop(NODE **a, int n)
2307c478bd9Sstevel@tonic-gate {
231dc5a8425Srobbin 	CELL *x, *y;
232dc5a8425Srobbin 	int i;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
2387c478bd9Sstevel@tonic-gate 	i = istrue(x);
2397c478bd9Sstevel@tonic-gate 	tempfree(x);
2407c478bd9Sstevel@tonic-gate 	switch (n) {
2417c478bd9Sstevel@tonic-gate 	case BOR:
2427c478bd9Sstevel@tonic-gate 		if (i) return (true);
2437c478bd9Sstevel@tonic-gate 		y = execute(a[1]);
2447c478bd9Sstevel@tonic-gate 		i = istrue(y);
2457c478bd9Sstevel@tonic-gate 		tempfree(y);
2467c478bd9Sstevel@tonic-gate 		if (i) return (true);
2477c478bd9Sstevel@tonic-gate 		else return (false);
2487c478bd9Sstevel@tonic-gate 	case AND:
2497c478bd9Sstevel@tonic-gate 		if (!i) return (false);
2507c478bd9Sstevel@tonic-gate 		y = execute(a[1]);
2517c478bd9Sstevel@tonic-gate 		i = istrue(y);
2527c478bd9Sstevel@tonic-gate 		tempfree(y);
2537c478bd9Sstevel@tonic-gate 		if (i) return (true);
2547c478bd9Sstevel@tonic-gate 		else return (false);
2557c478bd9Sstevel@tonic-gate 	case NOT:
2567c478bd9Sstevel@tonic-gate 		if (i) return (false);
2577c478bd9Sstevel@tonic-gate 		else return (true);
2587c478bd9Sstevel@tonic-gate 	default:
2597c478bd9Sstevel@tonic-gate 		error(FATAL, "unknown boolean operator %d", n);
2607c478bd9Sstevel@tonic-gate 	}
261dc5a8425Srobbin 	return (false);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 
267dc5a8425Srobbin CELL *
268dc5a8425Srobbin relop(NODE **a, int n)
2697c478bd9Sstevel@tonic-gate {
270dc5a8425Srobbin 	int i;
271dc5a8425Srobbin 	CELL *x, *y;
2727c478bd9Sstevel@tonic-gate 	awkfloat j;
273dc5a8425Srobbin 	wchar_t *xs, *ys;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
2797c478bd9Sstevel@tonic-gate 	y = execute(a[1]);
2807c478bd9Sstevel@tonic-gate 	if (x->tval&NUM && y->tval&NUM) {
2817c478bd9Sstevel@tonic-gate 		j = x->fval - y->fval;
2827c478bd9Sstevel@tonic-gate 		i = j<0? -1: (j>0? 1: 0);
2837c478bd9Sstevel@tonic-gate 	} else {
2847c478bd9Sstevel@tonic-gate 		xs = getsval(x);
2857c478bd9Sstevel@tonic-gate 		ys = getsval(y);
2867c478bd9Sstevel@tonic-gate 		if (xs && ys)
2877c478bd9Sstevel@tonic-gate 			i = wscoll(xs, ys);
2887c478bd9Sstevel@tonic-gate 		else
2897c478bd9Sstevel@tonic-gate 			return (false);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	tempfree(x);
2927c478bd9Sstevel@tonic-gate 	tempfree(y);
2937c478bd9Sstevel@tonic-gate 	switch (n) {
2947c478bd9Sstevel@tonic-gate 	case LT:	if (i<0) return (true);
2957c478bd9Sstevel@tonic-gate 			else return (false);
2967c478bd9Sstevel@tonic-gate 	case LE:	if (i<=0) return (true);
2977c478bd9Sstevel@tonic-gate 			else return (false);
2987c478bd9Sstevel@tonic-gate 	case NE:	if (i!=0) return (true);
2997c478bd9Sstevel@tonic-gate 			else return (false);
3007c478bd9Sstevel@tonic-gate 	case EQ:	if (i == 0) return (true);
3017c478bd9Sstevel@tonic-gate 			else return (false);
3027c478bd9Sstevel@tonic-gate 	case GE:	if (i>=0) return (true);
3037c478bd9Sstevel@tonic-gate 			else return (false);
3047c478bd9Sstevel@tonic-gate 	case GT:	if (i>0) return (true);
3057c478bd9Sstevel@tonic-gate 			else return (false);
3067c478bd9Sstevel@tonic-gate 	default:
3077c478bd9Sstevel@tonic-gate 		error(FATAL, "unknown relational operator %d", n);
3087c478bd9Sstevel@tonic-gate 	}
309dc5a8425Srobbin 	return (false);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 
319dc5a8425Srobbin CELL *
320dc5a8425Srobbin gettemp(void)
3217c478bd9Sstevel@tonic-gate {
322dc5a8425Srobbin 	int i;
323dc5a8425Srobbin 	CELL *x;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	for (i=0; i<MAXTMP; i++)
3297c478bd9Sstevel@tonic-gate 		if (tmps[i].tval == 0)
3307c478bd9Sstevel@tonic-gate 			break;
3317c478bd9Sstevel@tonic-gate 	if (i == MAXTMP)
3327c478bd9Sstevel@tonic-gate 		error(FATAL, "out of temporaries in gettemp");
3337c478bd9Sstevel@tonic-gate 	tmps[i] = tempcell;
3347c478bd9Sstevel@tonic-gate 	x = &tmps[i];
3357c478bd9Sstevel@tonic-gate 	return (x);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 
341dc5a8425Srobbin CELL *
342dc5a8425Srobbin indirect(NODE **a, int n)
3437c478bd9Sstevel@tonic-gate {
344dc5a8425Srobbin 	CELL *x;
345dc5a8425Srobbin 	int m;
3467c478bd9Sstevel@tonic-gate 	CELL *fieldadr();
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
3497c478bd9Sstevel@tonic-gate 	m = getfval(x);
3507c478bd9Sstevel@tonic-gate 	tempfree(x);
3517c478bd9Sstevel@tonic-gate 	x = fieldadr(m);
3527c478bd9Sstevel@tonic-gate 	x->ctype = OCELL;
3537c478bd9Sstevel@tonic-gate 	x->csub = CFLD;
3547c478bd9Sstevel@tonic-gate 	return (x);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 
360dc5a8425Srobbin CELL *
361dc5a8425Srobbin substr(NODE **a, int nnn)
3627c478bd9Sstevel@tonic-gate {
363dc5a8425Srobbin 	int k, m, n;
364dc5a8425Srobbin 	wchar_t *s, temp;
365dc5a8425Srobbin 	CELL *x, *y;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	y = execute(a[0]);
3687c478bd9Sstevel@tonic-gate 	s = getsval(y);
3697c478bd9Sstevel@tonic-gate 	k = wslen(s) + 1;
3707c478bd9Sstevel@tonic-gate 	if (k <= 1) {
3717c478bd9Sstevel@tonic-gate 		x = gettemp();
3727c478bd9Sstevel@tonic-gate 		setsval(x, L_NULL);
3737c478bd9Sstevel@tonic-gate 		return (x);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
3767c478bd9Sstevel@tonic-gate 	m = getfval(x);
3777c478bd9Sstevel@tonic-gate 	if (m <= 0)
3787c478bd9Sstevel@tonic-gate 		m = 1;
3797c478bd9Sstevel@tonic-gate 	else if (m > k)
3807c478bd9Sstevel@tonic-gate 		m = k;
3817c478bd9Sstevel@tonic-gate 	tempfree(x);
3827c478bd9Sstevel@tonic-gate 	if (a[2] != 0) {
3837c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
3847c478bd9Sstevel@tonic-gate 		n = getfval(x);
3857c478bd9Sstevel@tonic-gate 		tempfree(x);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 	else
3887c478bd9Sstevel@tonic-gate 		n = k - 1;
3897c478bd9Sstevel@tonic-gate 	if (n < 0)
3907c478bd9Sstevel@tonic-gate 		n = 0;
3917c478bd9Sstevel@tonic-gate 	else if (n > k - m)
3927c478bd9Sstevel@tonic-gate 		n = k - m;
3937c478bd9Sstevel@tonic-gate 	dprintf("substr: m=%d, n=%d, s=%ws\n", m, n, s);
3947c478bd9Sstevel@tonic-gate 	x = gettemp();
3957c478bd9Sstevel@tonic-gate 	temp = s[n+m-1];
3967c478bd9Sstevel@tonic-gate 	s[n+m-1] = (wchar_t)0x0;
3977c478bd9Sstevel@tonic-gate 	setsval(x, s + m - 1);
3987c478bd9Sstevel@tonic-gate 	s[n+m-1] = temp;
3997c478bd9Sstevel@tonic-gate 	tempfree(y);
4007c478bd9Sstevel@tonic-gate 	return (x);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 
406dc5a8425Srobbin CELL *
407dc5a8425Srobbin sindex(NODE **a, int nnn)
4087c478bd9Sstevel@tonic-gate {
409dc5a8425Srobbin 	CELL *x;
410dc5a8425Srobbin 	wchar_t *s1, *s2, *p1, *p2, *q;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
4137c478bd9Sstevel@tonic-gate 	s1 = getsval(x);
4147c478bd9Sstevel@tonic-gate 	tempfree(x);
4157c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
4167c478bd9Sstevel@tonic-gate 	s2 = getsval(x);
4177c478bd9Sstevel@tonic-gate 	tempfree(x);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	x = gettemp();
4207c478bd9Sstevel@tonic-gate 	for (p1 = s1; *p1 != (wchar_t)0x0; p1++) {
4217c478bd9Sstevel@tonic-gate 		for (q=p1, p2=s2; *p2 != (wchar_t)0x0 && *q == *p2; q++, p2++)
4227c478bd9Sstevel@tonic-gate 			;
4237c478bd9Sstevel@tonic-gate 		if (*p2 == (wchar_t)0x0) {
4247c478bd9Sstevel@tonic-gate 			setfval(x, (awkfloat) (p1 - s1 + 1));	/* origin 1 */
4257c478bd9Sstevel@tonic-gate 			return (x);
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 	setfval(x, 0.0);
4297c478bd9Sstevel@tonic-gate 	return (x);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 
435dc5a8425Srobbin wchar_t *
436dc5a8425Srobbin format(wchar_t *s, NODE *a)
4377c478bd9Sstevel@tonic-gate {
438dc5a8425Srobbin 	wchar_t *buf, *ep, *str;
439dc5a8425Srobbin 	wchar_t *p;
440dc5a8425Srobbin 	char *t;
4417c478bd9Sstevel@tonic-gate 	wchar_t *os;
4427c478bd9Sstevel@tonic-gate 	wchar_t tbuf[2*RECSIZE];
4437c478bd9Sstevel@tonic-gate 	char fmt[200];
444dc5a8425Srobbin 	CELL *x;
4457c478bd9Sstevel@tonic-gate 	int flag = 0;
4467c478bd9Sstevel@tonic-gate 	awkfloat xf;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	os = s;
4497c478bd9Sstevel@tonic-gate 	p = buf= (wchar_t *)malloc(RECSIZE * sizeof (wchar_t));
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (p == NULL)
4527c478bd9Sstevel@tonic-gate 		error(FATAL, "out of space in format");
4537c478bd9Sstevel@tonic-gate 	ep = p + RECSIZE;
4547c478bd9Sstevel@tonic-gate 	while (*s) {
4557c478bd9Sstevel@tonic-gate 		if (*s != '%') {
4567c478bd9Sstevel@tonic-gate 			*p++ = *s++;
4577c478bd9Sstevel@tonic-gate 			continue;
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 		if (*(s+1) == '%') {
4607c478bd9Sstevel@tonic-gate 			*p++ = '%';
4617c478bd9Sstevel@tonic-gate 			s += 2;
4627c478bd9Sstevel@tonic-gate 			continue;
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 		for (t=fmt; *s != '\0'; s++)
4657c478bd9Sstevel@tonic-gate 		{
4667c478bd9Sstevel@tonic-gate 			if (*s == 's' || *s == 'c')
4677c478bd9Sstevel@tonic-gate 				*t++ = 'w';
4687c478bd9Sstevel@tonic-gate 			*t++ = *s;
4697c478bd9Sstevel@tonic-gate 			if (*s >= 'a' && *s <= 'z' && *s != 'l')
4707c478bd9Sstevel@tonic-gate 				break;
4717c478bd9Sstevel@tonic-gate 			if (*s == '*') {
4727c478bd9Sstevel@tonic-gate 				if (a == NULL) {
4737c478bd9Sstevel@tonic-gate 					error(FATAL,
4747c478bd9Sstevel@tonic-gate 			"not enough arguments in printf(%ws) or sprintf(%ws)",
4757c478bd9Sstevel@tonic-gate 					os, os);
4767c478bd9Sstevel@tonic-gate 				}
4777c478bd9Sstevel@tonic-gate 				x = execute(a);
4787c478bd9Sstevel@tonic-gate 				a = a->nnext;
4797c478bd9Sstevel@tonic-gate 				sprintf(t-1, "%d", (int) getfval(x));
4807c478bd9Sstevel@tonic-gate 				t = fmt + strlen(fmt);
4817c478bd9Sstevel@tonic-gate 				tempfree(x);
4827c478bd9Sstevel@tonic-gate 			}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 		*t = '\0';
4867c478bd9Sstevel@tonic-gate 		if (t >= fmt + sizeof (fmt))
4877c478bd9Sstevel@tonic-gate 			error(FATAL, "format item %.20ws... too long", os);
4887c478bd9Sstevel@tonic-gate 		switch (*s) {
4897c478bd9Sstevel@tonic-gate 		case 'f': case 'e': case 'g':
4907c478bd9Sstevel@tonic-gate 			flag = 1;
4917c478bd9Sstevel@tonic-gate 			break;
4927c478bd9Sstevel@tonic-gate 		case 'd':
4937c478bd9Sstevel@tonic-gate 			flag = 2;
4947c478bd9Sstevel@tonic-gate 			if (*(s-1) == 'l') break;
4957c478bd9Sstevel@tonic-gate 			*(t-1) = 'l';
4967c478bd9Sstevel@tonic-gate 			*t = 'd';
4977c478bd9Sstevel@tonic-gate 			*++t = '\0';
4987c478bd9Sstevel@tonic-gate 			break;
4997c478bd9Sstevel@tonic-gate 		case 'o': case 'x':
5007c478bd9Sstevel@tonic-gate 			flag = *(s-1) == 'l' ? 2 : 3;
5017c478bd9Sstevel@tonic-gate 			break;
5027c478bd9Sstevel@tonic-gate 		case 'c':
5037c478bd9Sstevel@tonic-gate 			flag = 3;
5047c478bd9Sstevel@tonic-gate 			break;
5057c478bd9Sstevel@tonic-gate 		case 's':
5067c478bd9Sstevel@tonic-gate 			flag = 4;
5077c478bd9Sstevel@tonic-gate 			break;
5087c478bd9Sstevel@tonic-gate 		default:
5097c478bd9Sstevel@tonic-gate 			flag = 0;
5107c478bd9Sstevel@tonic-gate 			break;
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 		if (flag == 0) {
5137c478bd9Sstevel@tonic-gate 			wsprintf(p, "%s", fmt);
5147c478bd9Sstevel@tonic-gate 			p += wslen(p);
5157c478bd9Sstevel@tonic-gate 			continue;
5167c478bd9Sstevel@tonic-gate 		}
5177c478bd9Sstevel@tonic-gate 		if (a == NULL) {
5187c478bd9Sstevel@tonic-gate 			error(FATAL,
5197c478bd9Sstevel@tonic-gate 	"not enough arguments in printf(%ws) or sprintf(%ws)", os, os);
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 		x = execute(a);
5227c478bd9Sstevel@tonic-gate 		a = a->nnext;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		/*
5257c478bd9Sstevel@tonic-gate 		 * Get the string to check length; %s is the usual problem;
5267c478bd9Sstevel@tonic-gate 		 * other conversions can cause overrun if they occur when
5277c478bd9Sstevel@tonic-gate 		 * the buffer is almost filled.
5287c478bd9Sstevel@tonic-gate 		 */
5297c478bd9Sstevel@tonic-gate 		if (flag == 4)	{ /* watch out for converting to numbers! */
5307c478bd9Sstevel@tonic-gate 			str = getsval(x);
5317c478bd9Sstevel@tonic-gate 		}
5327c478bd9Sstevel@tonic-gate 		else {
5337c478bd9Sstevel@tonic-gate 			xf = getfval(x);
5347c478bd9Sstevel@tonic-gate 			if (flag == 1) wsprintf(tbuf, fmt, xf);
5357c478bd9Sstevel@tonic-gate 			else if (flag == 2) wsprintf(tbuf, fmt, (long)xf);
5367c478bd9Sstevel@tonic-gate 			else if (flag == 3) wsprintf(tbuf, fmt, (int)xf);
5377c478bd9Sstevel@tonic-gate 			if (wslen(tbuf) >= RECSIZE)
5387c478bd9Sstevel@tonic-gate 				error(FATAL, "formatted item %s... too long",
5397c478bd9Sstevel@tonic-gate 						tbuf);
5407c478bd9Sstevel@tonic-gate 			str = tbuf;
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 		/*
5437c478bd9Sstevel@tonic-gate 		 * If string overruns the buffer, reallocate;
5447c478bd9Sstevel@tonic-gate 		 * consider length of format string
5457c478bd9Sstevel@tonic-gate 		 */
5467c478bd9Sstevel@tonic-gate 		if (p + wslen(str) + wslen(s) + 1 >= ep) {
5477c478bd9Sstevel@tonic-gate 			int newlen, oldlen;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 			oldlen = p - buf;
5507c478bd9Sstevel@tonic-gate 			/* Add RECSIZE for additional space */
5517c478bd9Sstevel@tonic-gate 			newlen = oldlen + wslen(str) + RECSIZE;
5527c478bd9Sstevel@tonic-gate 			buf = realloc(buf, (unsigned) newlen * sizeof(wchar_t));
5537c478bd9Sstevel@tonic-gate 			if (buf == NULL)
5547c478bd9Sstevel@tonic-gate 				error(FATAL, "out of format space");
5557c478bd9Sstevel@tonic-gate 			p = buf + oldlen;
5567c478bd9Sstevel@tonic-gate 			ep = buf + newlen;
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 		/* Transfer string to buffer */
5597c478bd9Sstevel@tonic-gate 		if (flag == 4)
5607c478bd9Sstevel@tonic-gate 			wsprintf(p, fmt, str);
5617c478bd9Sstevel@tonic-gate 		else
5627c478bd9Sstevel@tonic-gate 			wscpy(p, str);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		tempfree(x);
5657c478bd9Sstevel@tonic-gate 		p += wslen(p);
5667c478bd9Sstevel@tonic-gate 		if (p >= ep)
5677c478bd9Sstevel@tonic-gate 			error(FATAL, "formatted string too long");
5687c478bd9Sstevel@tonic-gate 		s++;
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 	*p = '\0';
5717c478bd9Sstevel@tonic-gate 	return (buf);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 
575dc5a8425Srobbin CELL *
576*d0983205SRoger A. Faulkner a_sprintf(NODE **a, int n)
5777c478bd9Sstevel@tonic-gate {
578dc5a8425Srobbin 	CELL *x;
579dc5a8425Srobbin 	NODE *y;
580dc5a8425Srobbin 	wchar_t *s;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	y = a[0]->nnext;
5837c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
5847c478bd9Sstevel@tonic-gate 	s = format(getsval(x), y);
5857c478bd9Sstevel@tonic-gate 	tempfree(x);
5867c478bd9Sstevel@tonic-gate 	x = gettemp();
5877c478bd9Sstevel@tonic-gate 	x->sval = s;
5887c478bd9Sstevel@tonic-gate 	x->tval = STR;
5897c478bd9Sstevel@tonic-gate 	return (x);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 
593dc5a8425Srobbin CELL *
594dc5a8425Srobbin arith(NODE **a, int n)
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	awkfloat i, j;
597dc5a8425Srobbin 	CELL *x, *y, *z;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
6007c478bd9Sstevel@tonic-gate 	i = getfval(x);
6017c478bd9Sstevel@tonic-gate 	tempfree(x);
6027c478bd9Sstevel@tonic-gate 	if (n != UMINUS) {
6037c478bd9Sstevel@tonic-gate 		y = execute(a[1]);
6047c478bd9Sstevel@tonic-gate 		j = getfval(y);
6057c478bd9Sstevel@tonic-gate 		tempfree(y);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 	z = gettemp();
6087c478bd9Sstevel@tonic-gate 	switch (n) {
6097c478bd9Sstevel@tonic-gate 	case ADD:
6107c478bd9Sstevel@tonic-gate 		i += j;
6117c478bd9Sstevel@tonic-gate 		break;
6127c478bd9Sstevel@tonic-gate 	case MINUS:
6137c478bd9Sstevel@tonic-gate 		i -= j;
6147c478bd9Sstevel@tonic-gate 		break;
6157c478bd9Sstevel@tonic-gate 	case MULT:
6167c478bd9Sstevel@tonic-gate 		i *= j;
6177c478bd9Sstevel@tonic-gate 		break;
6187c478bd9Sstevel@tonic-gate 	case DIVIDE:
6197c478bd9Sstevel@tonic-gate 		if (j == 0)
6207c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
6217c478bd9Sstevel@tonic-gate 		i /= j;
6227c478bd9Sstevel@tonic-gate 		break;
6237c478bd9Sstevel@tonic-gate 	case MOD:
6247c478bd9Sstevel@tonic-gate 		if (j == 0)
6257c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
6267c478bd9Sstevel@tonic-gate 		i = i - j*(long)(i/j);
6277c478bd9Sstevel@tonic-gate 		break;
6287c478bd9Sstevel@tonic-gate 	case UMINUS:
6297c478bd9Sstevel@tonic-gate 		i = -i;
6307c478bd9Sstevel@tonic-gate 		break;
6317c478bd9Sstevel@tonic-gate 	default:
6327c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal arithmetic operator %d", n);
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 	setfval(z, i);
6357c478bd9Sstevel@tonic-gate 	return (z);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 
641dc5a8425Srobbin CELL *
642dc5a8425Srobbin incrdecr(NODE **a, int n)
6437c478bd9Sstevel@tonic-gate {
644dc5a8425Srobbin 	CELL *x, *z;
645dc5a8425Srobbin 	int k;
6467c478bd9Sstevel@tonic-gate 	awkfloat xf;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
6497c478bd9Sstevel@tonic-gate 	xf = getfval(x);
6507c478bd9Sstevel@tonic-gate 	k = (n == PREINCR || n == POSTINCR) ? 1 : -1;
6517c478bd9Sstevel@tonic-gate 	if (n == PREINCR || n == PREDECR) {
6527c478bd9Sstevel@tonic-gate 		setfval(x, xf + k);
6537c478bd9Sstevel@tonic-gate 		return (x);
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 	z = gettemp();
6567c478bd9Sstevel@tonic-gate 	setfval(z, xf);
6577c478bd9Sstevel@tonic-gate 	setfval(x, xf + k);
6587c478bd9Sstevel@tonic-gate 	tempfree(x);
6597c478bd9Sstevel@tonic-gate 	return (z);
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 
664dc5a8425Srobbin CELL *
665dc5a8425Srobbin assign(NODE **a, int n)
6667c478bd9Sstevel@tonic-gate {
667dc5a8425Srobbin 	CELL *x, *y;
6687c478bd9Sstevel@tonic-gate 	awkfloat xf, yf;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
6747c478bd9Sstevel@tonic-gate 	y = execute(a[1]);
6757c478bd9Sstevel@tonic-gate 	if (n == ASSIGN) {	/* ordinary assignment */
6767c478bd9Sstevel@tonic-gate 		if ((y->tval & (STR|NUM)) == (STR|NUM)) {
6777c478bd9Sstevel@tonic-gate 			setsval(x, y->sval);
6787c478bd9Sstevel@tonic-gate 			x->fval = y->fval;
6797c478bd9Sstevel@tonic-gate 			x->tval |= NUM;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 		} else if (y->tval & STR)
6827c478bd9Sstevel@tonic-gate 			setsval(x, y->sval);
6837c478bd9Sstevel@tonic-gate 		else if (y->tval & NUM)
6847c478bd9Sstevel@tonic-gate 			setfval(x, y->fval);
6857c478bd9Sstevel@tonic-gate 		tempfree(y);
6867c478bd9Sstevel@tonic-gate 		return (x);
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	xf = getfval(x);
6897c478bd9Sstevel@tonic-gate 	yf = getfval(y);
6907c478bd9Sstevel@tonic-gate 	switch (n) {
6917c478bd9Sstevel@tonic-gate 	case ADDEQ:
6927c478bd9Sstevel@tonic-gate 		xf += yf;
6937c478bd9Sstevel@tonic-gate 		break;
6947c478bd9Sstevel@tonic-gate 	case SUBEQ:
6957c478bd9Sstevel@tonic-gate 		xf -= yf;
6967c478bd9Sstevel@tonic-gate 		break;
6977c478bd9Sstevel@tonic-gate 	case MULTEQ:
6987c478bd9Sstevel@tonic-gate 		xf *= yf;
6997c478bd9Sstevel@tonic-gate 		break;
7007c478bd9Sstevel@tonic-gate 	case DIVEQ:
7017c478bd9Sstevel@tonic-gate 		if (yf == 0)
7027c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
7037c478bd9Sstevel@tonic-gate 		xf /= yf;
7047c478bd9Sstevel@tonic-gate 		break;
7057c478bd9Sstevel@tonic-gate 	case MODEQ:
7067c478bd9Sstevel@tonic-gate 		if (yf == 0)
7077c478bd9Sstevel@tonic-gate 			error(FATAL, "division by zero");
7087c478bd9Sstevel@tonic-gate 		xf = xf - yf*(long)(xf/yf);
7097c478bd9Sstevel@tonic-gate 		break;
7107c478bd9Sstevel@tonic-gate 	default:
7117c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal assignment operator %d", n);
7127c478bd9Sstevel@tonic-gate 		break;
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	tempfree(y);
7157c478bd9Sstevel@tonic-gate 	setfval(x, xf);
7167c478bd9Sstevel@tonic-gate 	return (x);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 
722dc5a8425Srobbin CELL *
723dc5a8425Srobbin cat(NODE **a, int q)
7247c478bd9Sstevel@tonic-gate {
725dc5a8425Srobbin 	CELL *x, *y, *z;
726dc5a8425Srobbin 	int n1, n2;
727dc5a8425Srobbin 	wchar_t *s;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
7337c478bd9Sstevel@tonic-gate 	y = execute(a[1]);
7347c478bd9Sstevel@tonic-gate 	getsval(x);
7357c478bd9Sstevel@tonic-gate 	getsval(y);
7367c478bd9Sstevel@tonic-gate 	n1 = wslen(x->sval);
7377c478bd9Sstevel@tonic-gate 	n2 = wslen(y->sval);
7387c478bd9Sstevel@tonic-gate 	if ((s = (wchar_t *) malloc((n1 + n2 + 1) * sizeof (wchar_t))) == NULL)
7397c478bd9Sstevel@tonic-gate 		error(FATAL, "out of space in cat");
7407c478bd9Sstevel@tonic-gate 	wscpy(s, x->sval);
7417c478bd9Sstevel@tonic-gate 	wscpy(s+n1, y->sval);
7427c478bd9Sstevel@tonic-gate 	tempfree(y);
7437c478bd9Sstevel@tonic-gate 	z = gettemp();
7447c478bd9Sstevel@tonic-gate 	z->sval = s;
7457c478bd9Sstevel@tonic-gate 	z->tval = STR;
7467c478bd9Sstevel@tonic-gate 	tempfree(x);
7477c478bd9Sstevel@tonic-gate 	return (z);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 
753dc5a8425Srobbin CELL *
754dc5a8425Srobbin pastat(NODE **a, int n)
7557c478bd9Sstevel@tonic-gate {
756dc5a8425Srobbin 	CELL *x;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	if (a[0] == 0)
7627c478bd9Sstevel@tonic-gate 		x = true;
7637c478bd9Sstevel@tonic-gate 	else
7647c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
7657c478bd9Sstevel@tonic-gate 	if (istrue(x)) {
7667c478bd9Sstevel@tonic-gate 		tempfree(x);
7677c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 	return (x);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 
775dc5a8425Srobbin CELL *
776dc5a8425Srobbin dopa2(NODE **a, int n)
7777c478bd9Sstevel@tonic-gate {
778dc5a8425Srobbin 	CELL *x;
779dc5a8425Srobbin 	int pair;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	pair = (int) a[3];
7857c478bd9Sstevel@tonic-gate 	if (pairstack[pair] == 0) {
7867c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
7877c478bd9Sstevel@tonic-gate 		if (istrue(x))
7887c478bd9Sstevel@tonic-gate 			pairstack[pair] = 1;
7897c478bd9Sstevel@tonic-gate 		tempfree(x);
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 	if (pairstack[pair] == 1) {
7927c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
7937c478bd9Sstevel@tonic-gate 		if (istrue(x))
7947c478bd9Sstevel@tonic-gate 			pairstack[pair] = 0;
7957c478bd9Sstevel@tonic-gate 		tempfree(x);
7967c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
7977c478bd9Sstevel@tonic-gate 		return (x);
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 	return (false);
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 
805dc5a8425Srobbin CELL *
806dc5a8425Srobbin aprintf(NODE **a, int n)
8077c478bd9Sstevel@tonic-gate {
808dc5a8425Srobbin 	CELL *x;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 
813*d0983205SRoger A. Faulkner 	x = a_sprintf(a, n);
8147c478bd9Sstevel@tonic-gate 	if (a[1] == NULL) {
8157c478bd9Sstevel@tonic-gate 		printf("%ws", x->sval);
8167c478bd9Sstevel@tonic-gate 		tempfree(x);
8177c478bd9Sstevel@tonic-gate 		return (true);
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 	redirprint(x->sval, (int)a[1], a[2]);
8207c478bd9Sstevel@tonic-gate 	return (x);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 
826dc5a8425Srobbin CELL *
827dc5a8425Srobbin split(NODE **a, int nnn)
8287c478bd9Sstevel@tonic-gate {
829dc5a8425Srobbin 	CELL *x;
830dc5a8425Srobbin 	CELL *ap;
831dc5a8425Srobbin 	wchar_t *s, *p, c;
8327c478bd9Sstevel@tonic-gate 	wchar_t *t, temp, num[5];
833dc5a8425Srobbin 	wchar_t sep;
8347c478bd9Sstevel@tonic-gate 	int n, flag;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
8407c478bd9Sstevel@tonic-gate 	s = getsval(x);
8417c478bd9Sstevel@tonic-gate 	tempfree(x);
8427c478bd9Sstevel@tonic-gate 	if (a[2] == 0)
8437c478bd9Sstevel@tonic-gate 		sep = **FS;
8447c478bd9Sstevel@tonic-gate 	else {
8457c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
8467c478bd9Sstevel@tonic-gate 		sep = getsval(x)[0];
8477c478bd9Sstevel@tonic-gate 		tempfree(x);
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 	ap = (CELL *) a[1];
8507c478bd9Sstevel@tonic-gate 	freesymtab(ap);
8517c478bd9Sstevel@tonic-gate 	dprintf("split: s=|%ws|, a=%ws, sep=|%wc|\n", s, ap->nval, sep);
8527c478bd9Sstevel@tonic-gate 	ap->tval &= ~STR;
8537c478bd9Sstevel@tonic-gate 	ap->tval |= ARR;
8547c478bd9Sstevel@tonic-gate 	ap->sval = (wchar_t *) makesymtab();
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	n = 0;
8607c478bd9Sstevel@tonic-gate 	if (sep == ' ')
8617c478bd9Sstevel@tonic-gate 		for (n = 0; /* dummy */; /* dummy */) {
8627c478bd9Sstevel@tonic-gate 			c = *s;
8637c478bd9Sstevel@tonic-gate 			while (iswblank(c) || c == '\t' || c == '\n')
8647c478bd9Sstevel@tonic-gate 				c = *(++s);
8657c478bd9Sstevel@tonic-gate 			if (*s == 0)
8667c478bd9Sstevel@tonic-gate 				break;
8677c478bd9Sstevel@tonic-gate 			n++;
8687c478bd9Sstevel@tonic-gate 			t = s;
8697c478bd9Sstevel@tonic-gate 			do
8707c478bd9Sstevel@tonic-gate 				c = *(++s);
8717c478bd9Sstevel@tonic-gate 			while (! iswblank(c) && c != '\t' &&
8727c478bd9Sstevel@tonic-gate 				c != '\n' && c != '\0');
8737c478bd9Sstevel@tonic-gate 			temp = c;
8747c478bd9Sstevel@tonic-gate 			*s = (wchar_t)0x0;
8757c478bd9Sstevel@tonic-gate 			wsprintf(num, "%d", n);
8767c478bd9Sstevel@tonic-gate 			if (isanumber(t))
8777c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t),
8787c478bd9Sstevel@tonic-gate 						watof(t), STR|NUM, ap->sval);
8797c478bd9Sstevel@tonic-gate 			else
8807c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t), 0.0, STR, ap->sval);
8817c478bd9Sstevel@tonic-gate 			*s = temp;
8827c478bd9Sstevel@tonic-gate 			if (*s != 0)
8837c478bd9Sstevel@tonic-gate 				s++;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	} else if (*s != 0)
8867c478bd9Sstevel@tonic-gate 		for (;;) {
8877c478bd9Sstevel@tonic-gate 			n++;
8887c478bd9Sstevel@tonic-gate 			t = s;
8897c478bd9Sstevel@tonic-gate 			while ((c = *s) != sep && c != '\n' && c != '\0')
8907c478bd9Sstevel@tonic-gate 				s++;
8917c478bd9Sstevel@tonic-gate 			temp = c;
8927c478bd9Sstevel@tonic-gate 			*s = (wchar_t)0x0;
8937c478bd9Sstevel@tonic-gate 			wsprintf(num, "%d", n);
8947c478bd9Sstevel@tonic-gate 			if (isanumber(t))
8957c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t),
8967c478bd9Sstevel@tonic-gate 						watof(t), STR|NUM, ap->sval);
8977c478bd9Sstevel@tonic-gate 			else
8987c478bd9Sstevel@tonic-gate 				setsymtab(num, tostring(t), 0.0, STR, ap->sval);
8997c478bd9Sstevel@tonic-gate 			*s = temp;
9007c478bd9Sstevel@tonic-gate 			if (*s++ == 0)
9017c478bd9Sstevel@tonic-gate 				break;
9027c478bd9Sstevel@tonic-gate 		}
9037c478bd9Sstevel@tonic-gate 	x = gettemp();
9047c478bd9Sstevel@tonic-gate 	x->tval = NUM;
9057c478bd9Sstevel@tonic-gate 	x->fval = n;
9067c478bd9Sstevel@tonic-gate 	return (x);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 
912dc5a8425Srobbin CELL *
913dc5a8425Srobbin ifstat(NODE **a, int n)
9147c478bd9Sstevel@tonic-gate {
915dc5a8425Srobbin 	CELL *x;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	x = execute(a[0]);
9217c478bd9Sstevel@tonic-gate 	if (istrue(x)) {
9227c478bd9Sstevel@tonic-gate 		tempfree(x);
9237c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	} else if (a[2] != 0) {
9267c478bd9Sstevel@tonic-gate 		tempfree(x);
9277c478bd9Sstevel@tonic-gate 		x = execute(a[2]);
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 	return (x);
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 
935dc5a8425Srobbin CELL *
936dc5a8425Srobbin whilestat(NODE **a, int n)
9377c478bd9Sstevel@tonic-gate {
938dc5a8425Srobbin 	CELL *x;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	for (;;) {
9447c478bd9Sstevel@tonic-gate 		x = execute(a[0]);
9457c478bd9Sstevel@tonic-gate 		if (!istrue(x)) return (x);
9467c478bd9Sstevel@tonic-gate 		tempfree(x);
9477c478bd9Sstevel@tonic-gate 		x = execute(a[1]);
9487c478bd9Sstevel@tonic-gate 		if (isbreak(x)) {
9497c478bd9Sstevel@tonic-gate 			x = true;
9507c478bd9Sstevel@tonic-gate 			return (x);
9517c478bd9Sstevel@tonic-gate 		}
9527c478bd9Sstevel@tonic-gate 		if (isnext(x) || isexit(x))
9537c478bd9Sstevel@tonic-gate 			return (x);
9547c478bd9Sstevel@tonic-gate 		tempfree(x);
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 
961dc5a8425Srobbin CELL *
962dc5a8425Srobbin forstat(NODE **a, int n)
9637c478bd9Sstevel@tonic-gate {
964dc5a8425Srobbin 	CELL *x;
965dc5a8425Srobbin 	CELL *z;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	z = execute(a[0]);
9717c478bd9Sstevel@tonic-gate 	tempfree(z);
9727c478bd9Sstevel@tonic-gate 	for (;;) {
9737c478bd9Sstevel@tonic-gate 		if (a[1]!=0) {
9747c478bd9Sstevel@tonic-gate 			x = execute(a[1]);
9757c478bd9Sstevel@tonic-gate 			if (!istrue(x)) return (x);
9767c478bd9Sstevel@tonic-gate 			else tempfree(x);
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 		x = execute(a[3]);
9797c478bd9Sstevel@tonic-gate 		if (isbreak(x)) {	/* turn off break */
9807c478bd9Sstevel@tonic-gate 			x = true;
9817c478bd9Sstevel@tonic-gate 			return (x);
9827c478bd9Sstevel@tonic-gate 		}
9837c478bd9Sstevel@tonic-gate 		if (isnext(x) || isexit(x))
9847c478bd9Sstevel@tonic-gate 			return (x);
9857c478bd9Sstevel@tonic-gate 		tempfree(x);
9867c478bd9Sstevel@tonic-gate 		z = execute(a[2]);
9877c478bd9Sstevel@tonic-gate 		tempfree(z);
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate }
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 
994dc5a8425Srobbin CELL *
995dc5a8425Srobbin instat(NODE **a, int n)
9967c478bd9Sstevel@tonic-gate {
997dc5a8425Srobbin 	CELL *vp, *arrayp, *cp, **tp;
998dc5a8425Srobbin 	CELL *x;
9997c478bd9Sstevel@tonic-gate 	int i;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	vp = (CELL *) a[0];
10057c478bd9Sstevel@tonic-gate 	arrayp = (CELL *) a[1];
10067c478bd9Sstevel@tonic-gate 	if (!(arrayp->tval & ARR))
10077c478bd9Sstevel@tonic-gate 		error(FATAL, "%ws is not an array", arrayp->nval);
10087c478bd9Sstevel@tonic-gate 	tp = (CELL **) arrayp->sval;
10097c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAXSYM; i++) {	/* this routine knows too much */
10107c478bd9Sstevel@tonic-gate 		for (cp = tp[i]; cp != NULL; cp = cp->nextval) {
10117c478bd9Sstevel@tonic-gate 			setsval(vp, cp->nval);
10127c478bd9Sstevel@tonic-gate 			x = execute(a[2]);
10137c478bd9Sstevel@tonic-gate 			if (isbreak(x)) {
10147c478bd9Sstevel@tonic-gate 				x = true;
10157c478bd9Sstevel@tonic-gate 				return (x);
10167c478bd9Sstevel@tonic-gate 			}
10177c478bd9Sstevel@tonic-gate 			if (isnext(x) || isexit(x))
10187c478bd9Sstevel@tonic-gate 				return (x);
10197c478bd9Sstevel@tonic-gate 			tempfree(x);
10207c478bd9Sstevel@tonic-gate 		}
10217c478bd9Sstevel@tonic-gate 	}
10227c478bd9Sstevel@tonic-gate 	return (true);
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 
1028dc5a8425Srobbin CELL *
1029dc5a8425Srobbin jump(NODE **a, int n)
10307c478bd9Sstevel@tonic-gate {
1031dc5a8425Srobbin 	CELL *y;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	switch (n) {
10377c478bd9Sstevel@tonic-gate 	case EXIT:
10387c478bd9Sstevel@tonic-gate 		if (a[0] != 0) {
10397c478bd9Sstevel@tonic-gate 			y = execute(a[0]);
10407c478bd9Sstevel@tonic-gate 			errorflag = getfval(y);
10417c478bd9Sstevel@tonic-gate 		}
10427c478bd9Sstevel@tonic-gate 		return (jexit);
10437c478bd9Sstevel@tonic-gate 	case NEXT:
10447c478bd9Sstevel@tonic-gate 		return (jnext);
10457c478bd9Sstevel@tonic-gate 	case BREAK:
10467c478bd9Sstevel@tonic-gate 		return (jbreak);
10477c478bd9Sstevel@tonic-gate 	case CONTINUE:
10487c478bd9Sstevel@tonic-gate 		return (jcont);
10497c478bd9Sstevel@tonic-gate 	default:
10507c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal jump type %d", n);
10517c478bd9Sstevel@tonic-gate 	}
1052dc5a8425Srobbin 	return (NULL);
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 
1058dc5a8425Srobbin CELL *
1059dc5a8425Srobbin fncn(NODE **a, int n)
10607c478bd9Sstevel@tonic-gate {
1061dc5a8425Srobbin 	CELL *x;
10627c478bd9Sstevel@tonic-gate 	awkfloat u;
1063dc5a8425Srobbin 	int t;
1064dc5a8425Srobbin 	wchar_t *wp;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	t = (int) a[0];
10677c478bd9Sstevel@tonic-gate 	x = execute(a[1]);
10687c478bd9Sstevel@tonic-gate 	if (t == FLENGTH)
10697c478bd9Sstevel@tonic-gate 		u = (awkfloat) wslen(getsval(x));
10707c478bd9Sstevel@tonic-gate 	else if (t == FLOG)
10717c478bd9Sstevel@tonic-gate 		u = log(getfval(x));
10727c478bd9Sstevel@tonic-gate 	else if (t == FINT)
10737c478bd9Sstevel@tonic-gate 		u = (awkfloat) (long) getfval(x);
10747c478bd9Sstevel@tonic-gate 	else if (t == FEXP)
10757c478bd9Sstevel@tonic-gate 		u = exp(getfval(x));
10767c478bd9Sstevel@tonic-gate 	else if (t == FSQRT)
10777c478bd9Sstevel@tonic-gate 		u = sqrt(getfval(x));
10787c478bd9Sstevel@tonic-gate 	else
10797c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal function type %d", t);
10807c478bd9Sstevel@tonic-gate 	tempfree(x);
10817c478bd9Sstevel@tonic-gate 	x = gettemp();
10827c478bd9Sstevel@tonic-gate 	setfval(x, u);
10837c478bd9Sstevel@tonic-gate 	return (x);
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 
1089dc5a8425Srobbin CELL *
1090dc5a8425Srobbin print(NODE **a, int n)
10917c478bd9Sstevel@tonic-gate {
1092dc5a8425Srobbin 	NODE *x;
1093dc5a8425Srobbin 	CELL *y;
10947c478bd9Sstevel@tonic-gate 	wchar_t s[RECSIZE];
1095aba1133aSnakanon 	wchar_t *ss, *bp, *ep, *os;
1096aba1133aSnakanon 	size_t	blen, newlen, sslen, orslen, ofslen, oslen;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	s[0] = '\0';
10997c478bd9Sstevel@tonic-gate 	bp = s;
11007c478bd9Sstevel@tonic-gate 	ep = s + RECSIZE;
1101aba1133aSnakanon 
1102aba1133aSnakanon 	blen = 0;
1103aba1133aSnakanon 	orslen = wcslen(*ORS);
1104aba1133aSnakanon 	ofslen = wcslen(*OFS);
1105aba1133aSnakanon 
11067c478bd9Sstevel@tonic-gate 	for (x = a[0]; x != NULL; x = x->nnext) {
11077c478bd9Sstevel@tonic-gate 		y = execute(x);
11087c478bd9Sstevel@tonic-gate 		ss = getsval(y);
11097c478bd9Sstevel@tonic-gate 
1110aba1133aSnakanon 		/* total new length will be */
1111aba1133aSnakanon 		sslen = wcslen(ss);
1112aba1133aSnakanon 		if (x->nnext == NULL) {
1113aba1133aSnakanon 			os = *ORS;
1114aba1133aSnakanon 			oslen = orslen;
1115aba1133aSnakanon 		} else {
1116aba1133aSnakanon 			os = *OFS;
1117aba1133aSnakanon 			oslen = ofslen;
1118aba1133aSnakanon 		}
1119aba1133aSnakanon 		newlen = blen + sslen + oslen;
1120aba1133aSnakanon 
1121aba1133aSnakanon 		/* allocate larger buffer if needed */
1122aba1133aSnakanon 		if (ep < (bp + newlen + 1)) {
1123aba1133aSnakanon 			wchar_t	*oldbp = bp;
1124aba1133aSnakanon 
1125aba1133aSnakanon 			if (oldbp == s)
1126aba1133aSnakanon 				bp = NULL;
1127aba1133aSnakanon 			bp = realloc(bp, sizeof (wchar_t) * (newlen + 1));
11287c478bd9Sstevel@tonic-gate 			if (bp == NULL)
11297c478bd9Sstevel@tonic-gate 				error(FATAL, "out of space in print");
1130aba1133aSnakanon 			ep = bp + newlen + 1;
1131aba1133aSnakanon 			if (oldbp == s)
1132aba1133aSnakanon 				(void) wmemcpy(bp, oldbp, blen);
11337c478bd9Sstevel@tonic-gate 		}
1134aba1133aSnakanon 		(void) wmemcpy(bp + blen, ss, sslen);
1135aba1133aSnakanon 		(void) wmemcpy(bp + blen + sslen, os, oslen);
11367c478bd9Sstevel@tonic-gate 		tempfree(y);
1137aba1133aSnakanon 		blen = newlen;
1138aba1133aSnakanon 		bp[blen] = '\0';
11397c478bd9Sstevel@tonic-gate 	}
1140aba1133aSnakanon 	if (a[1] == NULL) {
1141aba1133aSnakanon 		(void) printf("%ws", bp);
11427c478bd9Sstevel@tonic-gate 		if (bp != s)
11437c478bd9Sstevel@tonic-gate 			free(bp);
11447c478bd9Sstevel@tonic-gate 		return (true);
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	redirprint(bp, (int)a[1], a[2]);
11487c478bd9Sstevel@tonic-gate 	if (bp != s)
11497c478bd9Sstevel@tonic-gate 		free(bp);
11507c478bd9Sstevel@tonic-gate 	return (false);
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 
1155dc5a8425Srobbin CELL *
1156dc5a8425Srobbin nullproc(void)
11577c478bd9Sstevel@tonic-gate {
1158dc5a8425Srobbin 	return (NULL);
1159dc5a8425Srobbin }
1160dc5a8425Srobbin 
1161dc5a8425Srobbin 
1162dc5a8425Srobbin 
1163dc5a8425Srobbin CELL *
1164dc5a8425Srobbin nodetoobj(NODE *a)
1165dc5a8425Srobbin {
1166dc5a8425Srobbin 	CELL *x;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	x= (CELL *) a->nobj;
11697c478bd9Sstevel@tonic-gate 	x->ctype = OCELL;
11707c478bd9Sstevel@tonic-gate 	x->csub = a->subtype;
11717c478bd9Sstevel@tonic-gate 	if (isfld(x))
11727c478bd9Sstevel@tonic-gate 		fldbld();
11737c478bd9Sstevel@tonic-gate 	return (x);
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate 
1176dc5a8425Srobbin static void
1177dc5a8425Srobbin redirprint(wchar_t *s, int a, NODE *b)
11787c478bd9Sstevel@tonic-gate {
1179dc5a8425Srobbin 	int i;
1180dc5a8425Srobbin 	CELL *x;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	x = execute(b);
11837c478bd9Sstevel@tonic-gate 	getsval(x);
11847c478bd9Sstevel@tonic-gate 	for (i=0; i<FILENUM; i++)
11857c478bd9Sstevel@tonic-gate 		if (files[i].fname && wscmp(x->sval, files[i].fname) == 0)
11867c478bd9Sstevel@tonic-gate 			goto doit;
11877c478bd9Sstevel@tonic-gate 	for (i=0; i<FILENUM; i++)
11887c478bd9Sstevel@tonic-gate 		if (files[i].fp == 0)
11897c478bd9Sstevel@tonic-gate 			break;
11907c478bd9Sstevel@tonic-gate 	if (i >= FILENUM)
11917c478bd9Sstevel@tonic-gate 		error(FATAL, "too many output files %d", i);
11927c478bd9Sstevel@tonic-gate 	if (a == '|')	/* a pipe! */
11937c478bd9Sstevel@tonic-gate 		files[i].fp = popen(toeuccode(x->sval), "w");
11947c478bd9Sstevel@tonic-gate 	else if (a == APPEND)
11957c478bd9Sstevel@tonic-gate 		files[i].fp = fopen(toeuccode(x->sval), "a");
11967c478bd9Sstevel@tonic-gate 	else if (a == GT)
11977c478bd9Sstevel@tonic-gate 		files[i].fp = fopen(toeuccode(x->sval), "w");
11987c478bd9Sstevel@tonic-gate 	else
11997c478bd9Sstevel@tonic-gate 		error(FATAL, "illegal redirection near line %lld", lineno);
12007c478bd9Sstevel@tonic-gate 	if (files[i].fp == NULL)
12017c478bd9Sstevel@tonic-gate 		error(FATAL, "can't open file %ws", x->sval);
12027c478bd9Sstevel@tonic-gate 	files[i].fname = tostring(x->sval);
12037c478bd9Sstevel@tonic-gate 	files[i].type = a;
12047c478bd9Sstevel@tonic-gate doit:
12057c478bd9Sstevel@tonic-gate 	fprintf(files[i].fp, "%ws", s);
12067c478bd9Sstevel@tonic-gate #ifndef gcos
12077c478bd9Sstevel@tonic-gate 	fflush(files[i].fp);	/* in case someone is waiting for the output */
12087c478bd9Sstevel@tonic-gate #endif
12097c478bd9Sstevel@tonic-gate 	tempfree(x);
12107c478bd9Sstevel@tonic-gate }
1211