17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
539e7390na * Common Development and Distribution License (the "License").
639e7390na * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate
227c478bdstevel@tonic-gate/*
23d6b882aNobutomo Nakano * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24965005cchin * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
27965005cchin/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28965005cchin/*	  All Rights Reserved  	*/
29965005cchin
307c478bdstevel@tonic-gate/*
317c478bdstevel@tonic-gate * UNIX shell
327c478bdstevel@tonic-gate */
337c478bdstevel@tonic-gate
347c478bdstevel@tonic-gate#include	"defs.h"
357c478bdstevel@tonic-gate#include	"sym.h"
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gatestatic struct ionod *	inout();
38965005cchinstatic void	chkword(void);
39965005cchinstatic void	chksym(int);
407c478bdstevel@tonic-gatestatic struct trenod *	term();
417c478bdstevel@tonic-gatestatic struct trenod *	makelist();
427c478bdstevel@tonic-gatestatic struct trenod *	list();
437c478bdstevel@tonic-gatestatic struct regnod *	syncase();
447c478bdstevel@tonic-gatestatic struct trenod *	item();
457c478bdstevel@tonic-gatestatic int	skipnl();
46965005cchinstatic void	prsym(int);
47965005cchinstatic void	synbad(void);
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gate/* ======== storage allocation for functions ======== */
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gateunsigned char *
537c478bdstevel@tonic-gategetstor(asize)
547c478bdstevel@tonic-gate	int asize;
557c478bdstevel@tonic-gate{
567c478bdstevel@tonic-gate	if (fndef)
577c478bdstevel@tonic-gate		return((unsigned char *)alloc(asize));
587c478bdstevel@tonic-gate	else
597c478bdstevel@tonic-gate		return(getstak(asize));
607c478bdstevel@tonic-gate}
617c478bdstevel@tonic-gate
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate/* ========	command line decoding	========*/
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gatestruct trenod *
697c478bdstevel@tonic-gatemakefork(flgs, i)
707c478bdstevel@tonic-gate	int	flgs;
717c478bdstevel@tonic-gate	struct trenod *i;
727c478bdstevel@tonic-gate{
73965005cchin	struct forknod *t;
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate	t = (struct forknod *)getstor(sizeof(struct forknod));
767c478bdstevel@tonic-gate	t->forktyp = flgs|TFORK;
777c478bdstevel@tonic-gate	t->forktre = i;
787c478bdstevel@tonic-gate	t->forkio = 0;
797c478bdstevel@tonic-gate	return((struct trenod *)t);
807c478bdstevel@tonic-gate}
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gatestatic struct trenod *
837c478bdstevel@tonic-gatemakelist(type, i, r)
847c478bdstevel@tonic-gate	int	type;
857c478bdstevel@tonic-gate	struct trenod *i, *r;
867c478bdstevel@tonic-gate{
87965005cchin	struct lstnod *t;
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate	if (i == 0 || r == 0)
907c478bdstevel@tonic-gate		synbad();
917c478bdstevel@tonic-gate	else
927c478bdstevel@tonic-gate	{
937c478bdstevel@tonic-gate		t = (struct lstnod *)getstor(sizeof(struct lstnod));
947c478bdstevel@tonic-gate		t->lsttyp = type;
957c478bdstevel@tonic-gate		t->lstlef = i;
967c478bdstevel@tonic-gate		t->lstrit = r;
977c478bdstevel@tonic-gate	}
987c478bdstevel@tonic-gate	return((struct trenod *)t);
997c478bdstevel@tonic-gate}
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate/*
1027c478bdstevel@tonic-gate * cmd
1037c478bdstevel@tonic-gate *	empty
1047c478bdstevel@tonic-gate *	list
1057c478bdstevel@tonic-gate *	list & [ cmd ]
1067c478bdstevel@tonic-gate *	list [ ; cmd ]
1077c478bdstevel@tonic-gate */
1087c478bdstevel@tonic-gatestruct trenod *
1097c478bdstevel@tonic-gatecmd(sym, flg)
110965005cchin	int	sym;
1117c478bdstevel@tonic-gate	int		flg;
1127c478bdstevel@tonic-gate{
113965005cchin	struct trenod *i, *e;
1147c478bdstevel@tonic-gate	i = list(flg);
1157c478bdstevel@tonic-gate	if (wdval == NL)
1167c478bdstevel@tonic-gate	{
1177c478bdstevel@tonic-gate		if (flg & NLFLG)
1187c478bdstevel@tonic-gate		{
1197c478bdstevel@tonic-gate			wdval = ';';
1207c478bdstevel@tonic-gate			chkpr();
1217c478bdstevel@tonic-gate		}
1227c478bdstevel@tonic-gate	}
1237c478bdstevel@tonic-gate	else if (i == 0 && (flg & MTFLG) == 0)
1247c478bdstevel@tonic-gate		synbad();
1257c478bdstevel@tonic-gate
1267c478bdstevel@tonic-gate	switch (wdval)
1277c478bdstevel@tonic-gate	{
1287c478bdstevel@tonic-gate	case '&':
1297c478bdstevel@tonic-gate		if (i)
1307c478bdstevel@tonic-gate			i = makefork(FAMP, i);
1317c478bdstevel@tonic-gate		else
1327c478bdstevel@tonic-gate			synbad();
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gate	case ';':
1357c478bdstevel@tonic-gate		if (e = cmd(sym, flg | MTFLG))
1367c478bdstevel@tonic-gate			i = makelist(TLST, i, e);
1377c478bdstevel@tonic-gate		else if (i == 0)
1387c478bdstevel@tonic-gate			synbad();
1397c478bdstevel@tonic-gate		break;
1407c478bdstevel@tonic-gate
1417c478bdstevel@tonic-gate	case EOFSYM:
1427c478bdstevel@tonic-gate		if (sym == NL)
1437c478bdstevel@tonic-gate			break;
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate	default:
1467c478bdstevel@tonic-gate		if (sym)
1477c478bdstevel@tonic-gate			chksym(sym);
1487c478bdstevel@tonic-gate	}
1497c478bdstevel@tonic-gate	return(i);
1507c478bdstevel@tonic-gate}
1517c478bdstevel@tonic-gate
1527c478bdstevel@tonic-gate/*
1537c478bdstevel@tonic-gate * list
1547c478bdstevel@tonic-gate *	term
1557c478bdstevel@tonic-gate *	list && term
1567c478bdstevel@tonic-gate *	list || term
1577c478bdstevel@tonic-gate */
1587c478bdstevel@tonic-gatestatic struct trenod *
1597c478bdstevel@tonic-gatelist(flg)
1607c478bdstevel@tonic-gate{
161965005cchin	struct trenod *r;
162965005cchin	int		b;
1637c478bdstevel@tonic-gate	r = term(flg);
1647c478bdstevel@tonic-gate	while (r && ((b = (wdval == ANDFSYM)) || wdval == ORFSYM))
1657c478bdstevel@tonic-gate		r = makelist((b ? TAND : TORF), r, term(NLFLG));
1667c478bdstevel@tonic-gate	return(r);
1677c478bdstevel@tonic-gate}
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gate/*
1707c478bdstevel@tonic-gate * term
1717c478bdstevel@tonic-gate *	item
1727c478bdstevel@tonic-gate *	item |^ term
1737c478bdstevel@tonic-gate */
1747c478bdstevel@tonic-gatestatic struct trenod *
1757c478bdstevel@tonic-gateterm(flg)
1767c478bdstevel@tonic-gate{
177965005cchin	struct trenod *t;
1787c478bdstevel@tonic-gate
1797c478bdstevel@tonic-gate	reserv++;
1807c478bdstevel@tonic-gate	if (flg & NLFLG)
1817c478bdstevel@tonic-gate		skipnl();
1827c478bdstevel@tonic-gate	else
1837c478bdstevel@tonic-gate		word();
1847c478bdstevel@tonic-gate	if ((t = item(TRUE)) && (wdval == '^' || wdval == '|'))
1857c478bdstevel@tonic-gate	{
1867c478bdstevel@tonic-gate		struct trenod	*left;
1877c478bdstevel@tonic-gate		struct trenod	*right;
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate		left = makefork(FPOU, t);
1907c478bdstevel@tonic-gate		right = makefork(FPIN, term(NLFLG));
1917c478bdstevel@tonic-gate		return(makefork(0, makelist(TFIL, left, right)));
1927c478bdstevel@tonic-gate	}
1937c478bdstevel@tonic-gate	else
1947c478bdstevel@tonic-gate		return(t);
1957c478bdstevel@tonic-gate}
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gate
1987c478bdstevel@tonic-gatestatic struct regnod *
1997c478bdstevel@tonic-gatesyncase(esym)
200965005cchinint	esym;
2017c478bdstevel@tonic-gate{
2027c478bdstevel@tonic-gate	skipnl();
2037c478bdstevel@tonic-gate	if (wdval == esym)
2047c478bdstevel@tonic-gate		return(0);
2057c478bdstevel@tonic-gate	else
2067c478bdstevel@tonic-gate	{
207965005cchin		struct regnod *r =
208965005cchin		    (struct regnod *)getstor(sizeof (struct regnod));
209965005cchin		struct argnod *argp;
2107c478bdstevel@tonic-gate
2117c478bdstevel@tonic-gate		r->regptr = 0;
2127c478bdstevel@tonic-gate		for (;;)
2137c478bdstevel@tonic-gate		{
2147c478bdstevel@tonic-gate			if (fndef)
2157c478bdstevel@tonic-gate			{
2167c478bdstevel@tonic-gate				argp= wdarg;
2177c478bdstevel@tonic-gate				wdarg = (struct argnod *)alloc(length(argp->argval) + BYTESPERWORD);
2187c478bdstevel@tonic-gate				movstr(argp->argval, wdarg->argval);
2197c478bdstevel@tonic-gate			}
2207c478bdstevel@tonic-gate
2217c478bdstevel@tonic-gate			wdarg->argnxt = r->regptr;
2227c478bdstevel@tonic-gate			r->regptr = wdarg;
2237c478bdstevel@tonic-gate
2247c478bdstevel@tonic-gate			/* 'in' is not a reserved word in this case */
2257c478bdstevel@tonic-gate			if (wdval == INSYM){
2267c478bdstevel@tonic-gate				wdval = 0;
2277c478bdstevel@tonic-gate			}
2287c478bdstevel@tonic-gate			if (wdval || (word() != ')' && wdval != '|'))
2297c478bdstevel@tonic-gate				synbad();
2307c478bdstevel@tonic-gate			if (wdval == '|')
2317c478bdstevel@tonic-gate				word();
2327c478bdstevel@tonic-gate			else
2337c478bdstevel@tonic-gate				break;
2347c478bdstevel@tonic-gate		}
2357c478bdstevel@tonic-gate		r->regcom = cmd(0, NLFLG | MTFLG);
2367c478bdstevel@tonic-gate		if (wdval == ECSYM)
2377c478bdstevel@tonic-gate			r->regnxt = syncase(esym);
2387c478bdstevel@tonic-gate		else
2397c478bdstevel@tonic-gate		{
2407c478bdstevel@tonic-gate			chksym(esym);
2417c478bdstevel@tonic-gate			r->regnxt = 0;
2427c478bdstevel@tonic-gate		}
2437c478bdstevel@tonic-gate		return(r);
2447c478bdstevel@tonic-gate	}
2457c478bdstevel@tonic-gate}
2467c478bdstevel@tonic-gate
2477c478bdstevel@tonic-gate/*
2487c478bdstevel@tonic-gate * item
2497c478bdstevel@tonic-gate *
2507c478bdstevel@tonic-gate *	( cmd ) [ < in  ] [ > out ]
2517c478bdstevel@tonic-gate *	word word* [ < in ] [ > out ]
2527c478bdstevel@tonic-gate *	if ... then ... else ... fi
2537c478bdstevel@tonic-gate *	for ... while ... do ... done
2547c478bdstevel@tonic-gate *	case ... in ... esac
2557c478bdstevel@tonic-gate *	begin ... end
2567c478bdstevel@tonic-gate */
2577c478bdstevel@tonic-gatestatic struct trenod *
2587c478bdstevel@tonic-gateitem(flag)
2597c478bdstevel@tonic-gate	BOOL	flag;
2607c478bdstevel@tonic-gate{
261965005cchin	struct trenod *r;
262965005cchin	struct ionod *io;
2637c478bdstevel@tonic-gate
2647c478bdstevel@tonic-gate	if (flag)
2657c478bdstevel@tonic-gate		io = inout((struct ionod *)0);
2667c478bdstevel@tonic-gate	else
2677c478bdstevel@tonic-gate		io = 0;
2687c478bdstevel@tonic-gate	switch (wdval)
2697c478bdstevel@tonic-gate	{
2707c478bdstevel@tonic-gate	case CASYM:
2717c478bdstevel@tonic-gate		{
272965005cchin			struct swnod *t;
2737c478bdstevel@tonic-gate
2747c478bdstevel@tonic-gate			t = (struct swnod *)getstor(sizeof(struct swnod));
2757c478bdstevel@tonic-gate			r = (struct trenod *)t;
2767c478bdstevel@tonic-gate
2777c478bdstevel@tonic-gate			chkword();
2787c478bdstevel@tonic-gate			if (fndef)
2797c478bdstevel@tonic-gate				t->swarg = make(wdarg->argval);
2807c478bdstevel@tonic-gate			else
2817c478bdstevel@tonic-gate				t->swarg = wdarg->argval;
2827c478bdstevel@tonic-gate			skipnl();
2837c478bdstevel@tonic-gate			chksym(INSYM | BRSYM);
2847c478bdstevel@tonic-gate			t->swlst = syncase(wdval == INSYM ? ESSYM : KTSYM);
2857c478bdstevel@tonic-gate			t->swtyp = TSW;
2867c478bdstevel@tonic-gate			break;
2877c478bdstevel@tonic-gate		}
2887c478bdstevel@tonic-gate
2897c478bdstevel@tonic-gate	case IFSYM:
2907c478bdstevel@tonic-gate		{
291965005cchin			int	w;
292965005cchin			struct ifnod *t;
2937c478bdstevel@tonic-gate
2947c478bdstevel@tonic-gate			t = (struct ifnod *)getstor(sizeof(struct ifnod));
2957c478bdstevel@tonic-gate			r = (struct trenod *)t;
2967c478bdstevel@tonic-gate
2977c478bdstevel@tonic-gate			t->iftyp = TIF;
2987c478bdstevel@tonic-gate			t->iftre = cmd(THSYM, NLFLG);
2997c478bdstevel@tonic-gate			t->thtre = cmd(ELSYM | FISYM | EFSYM, NLFLG);
3007c478bdstevel@tonic-gate			t->eltre = ((w = wdval) == ELSYM ? cmd(FISYM, NLFLG) : (w == EFSYM ? (wdval = IFSYM, item(0)) : 0));
3017c478bdstevel@tonic-gate			if (w == EFSYM)
3027c478bdstevel@tonic-gate				return(r);
3037c478bdstevel@tonic-gate			break;
3047c478bdstevel@tonic-gate		}
3057c478bdstevel@tonic-gate
3067c478bdstevel@tonic-gate	case FORSYM:
3077c478bdstevel@tonic-gate		{
308965005cchin			struct fornod *t;
3097c478bdstevel@tonic-gate
3107c478bdstevel@tonic-gate			t = (struct fornod *)getstor(sizeof(struct fornod));
3117c478bdstevel@tonic-gate			r = (struct trenod *)t;
3127c478bdstevel@tonic-gate
3137c478bdstevel@tonic-gate			t->fortyp = TFOR;
3147c478bdstevel@tonic-gate			t->forlst = 0;
3157c478bdstevel@tonic-gate			chkword();
3167c478bdstevel@tonic-gate			if (fndef)
3177c478bdstevel@tonic-gate				t->fornam = make(wdarg->argval);
3187c478bdstevel@tonic-gate			else
3197c478bdstevel@tonic-gate				t->fornam = wdarg->argval;
3207c478bdstevel@tonic-gate			if (skipnl() == INSYM)
3217c478bdstevel@tonic-gate			{
3227c478bdstevel@tonic-gate				chkword();
3237c478bdstevel@tonic-gate
3247c478bdstevel@tonic-gate				nohash++;
3257c478bdstevel@tonic-gate				t->forlst = (struct comnod *)item(0);
3267c478bdstevel@tonic-gate				nohash--;
3277c478bdstevel@tonic-gate
3287c478bdstevel@tonic-gate				if (wdval != NL && wdval != ';')
3297c478bdstevel@tonic-gate					synbad();
3307c478bdstevel@tonic-gate				if (wdval == NL)
3317c478bdstevel@tonic-gate					chkpr();
3327c478bdstevel@tonic-gate				skipnl();
3337c478bdstevel@tonic-gate			}
3347c478bdstevel@tonic-gate			chksym(DOSYM | BRSYM);
3357c478bdstevel@tonic-gate			t->fortre = cmd(wdval == DOSYM ? ODSYM : KTSYM, NLFLG);
3367c478bdstevel@tonic-gate			break;
3377c478bdstevel@tonic-gate		}
3387c478bdstevel@tonic-gate
3397c478bdstevel@tonic-gate	case WHSYM:
3407c478bdstevel@tonic-gate	case UNSYM:
3417c478bdstevel@tonic-gate		{
342965005cchin			struct whnod *t;
3437c478bdstevel@tonic-gate
3447c478bdstevel@tonic-gate			t = (struct whnod *)getstor(sizeof(struct whnod));
3457c478bdstevel@tonic-gate			r = (struct trenod *)t;
3467c478bdstevel@tonic-gate
3477c478bdstevel@tonic-gate			t->whtyp = (wdval == WHSYM ? TWH : TUN);
3487c478bdstevel@tonic-gate			t->whtre = cmd(DOSYM, NLFLG);
3497c478bdstevel@tonic-gate			t->dotre = cmd(ODSYM, NLFLG);
3507c478bdstevel@tonic-gate			break;
3517c478bdstevel@tonic-gate		}
3527c478bdstevel@tonic-gate
3537c478bdstevel@tonic-gate	case BRSYM:
3547c478bdstevel@tonic-gate		r = cmd(KTSYM, NLFLG);
3557c478bdstevel@tonic-gate		break;
3567c478bdstevel@tonic-gate
3577c478bdstevel@tonic-gate	case '(':
3587c478bdstevel@tonic-gate		{
359965005cchin			struct parnod *p;
3607c478bdstevel@tonic-gate
3617c478bdstevel@tonic-gate			p = (struct parnod *)getstor(sizeof(struct parnod));
3627c478bdstevel@tonic-gate			p->partre = cmd(')', NLFLG);
3637c478bdstevel@tonic-gate			p->partyp = TPAR;
3647c478bdstevel@tonic-gate			r = makefork(0, p);
3657c478bdstevel@tonic-gate			break;
3667c478bdstevel@tonic-gate		}
3677c478bdstevel@tonic-gate
3687c478bdstevel@tonic-gate	default:
3697c478bdstevel@tonic-gate		if (io == 0)
3707c478bdstevel@tonic-gate			return(0);
3717c478bdstevel@tonic-gate
3727c478bdstevel@tonic-gate	case 0:
3737c478bdstevel@tonic-gate		{
374965005cchin			struct comnod *t;
375965005cchin			struct argnod *argp;
376965005cchin			struct argnod **argtail;
377965005cchin			struct argnod **argset = 0;
3787c478bdstevel@tonic-gate			int	keywd = 1;
3797c478bdstevel@tonic-gate			unsigned char	*com;
3807c478bdstevel@tonic-gate
3817c478bdstevel@tonic-gate			if ((wdval != NL) && ((peekn = skipwc()) == '('))
3827c478bdstevel@tonic-gate			{
3837c478bdstevel@tonic-gate				struct fndnod *f;
3847c478bdstevel@tonic-gate				struct ionod  *saveio;
3857c478bdstevel@tonic-gate
3867c478bdstevel@tonic-gate				saveio = iotemp;
3877c478bdstevel@tonic-gate				peekn = 0;
3887c478bdstevel@tonic-gate				if (skipwc() != ')')
3897c478bdstevel@tonic-gate					synbad();
3907c478bdstevel@tonic-gate
391d6b882aNobutomo Nakano				/*
392d6b882aNobutomo Nakano				 * We increase fndef before calling getstor(),
393d6b882aNobutomo Nakano				 * so that getstor() uses malloc to allocate
394d6b882aNobutomo Nakano				 * memory instead of stack. This is necessary
395d6b882aNobutomo Nakano				 * since fndnod will be hung on np->namenv,
396d6b882aNobutomo Nakano				 * which persists over command executions.
397d6b882aNobutomo Nakano				 */
398d6b882aNobutomo Nakano				fndef++;
3997c478bdstevel@tonic-gate				f = (struct fndnod *)getstor(sizeof(struct fndnod));
4007c478bdstevel@tonic-gate				r = (struct trenod *)f;
4017c478bdstevel@tonic-gate
4027c478bdstevel@tonic-gate				f->fndtyp = TFND;
403d6b882aNobutomo Nakano				f->fndnam = make(wdarg->argval);
404d6b882aNobutomo Nakano				f->fndref = 0;
4057c478bdstevel@tonic-gate				reserv++;
4067c478bdstevel@tonic-gate				skipnl();
4077c478bdstevel@tonic-gate				f->fndval = (struct trenod *)item(0);
4087c478bdstevel@tonic-gate				fndef--;
4097c478bdstevel@tonic-gate
4107c478bdstevel@tonic-gate				if (iotemp != saveio)
4117c478bdstevel@tonic-gate				{
4127c478bdstevel@tonic-gate					struct ionod 	*ioptr = iotemp;
4137c478bdstevel@tonic-gate
4147c478bdstevel@tonic-gate					while (ioptr->iolst != saveio)
4157c478bdstevel@tonic-gate						ioptr = ioptr->iolst;
4167c478bdstevel@tonic-gate
4177c478bdstevel@tonic-gate					ioptr->iolst = fiotemp;
4187c478bdstevel@tonic-gate					fiotemp = iotemp;
4197c478bdstevel@tonic-gate					iotemp = saveio;
4207c478bdstevel@tonic-gate				}
4217c478bdstevel@tonic-gate				return(r);
4227c478bdstevel@tonic-gate			}
4237c478bdstevel@tonic-gate			else
4247c478bdstevel@tonic-gate			{
4257c478bdstevel@tonic-gate				t = (struct comnod *)getstor(sizeof(struct comnod));
4267c478bdstevel@tonic-gate				r = (struct trenod *)t;
4277c478bdstevel@tonic-gate
4287c478bdstevel@tonic-gate				t->comio = io; /*initial io chain*/
4297c478bdstevel@tonic-gate				argtail = &(t->comarg);
4307c478bdstevel@tonic-gate
4317c478bdstevel@tonic-gate				while (wdval == 0)
4327c478bdstevel@tonic-gate				{
4337c478bdstevel@tonic-gate					if (fndef)
4347c478bdstevel@tonic-gate					{
4357c478bdstevel@tonic-gate						argp = wdarg;
4367c478bdstevel@tonic-gate						wdarg = (struct argnod *)alloc(length(argp->argval) + BYTESPERWORD);
4377c478bdstevel@tonic-gate						movstr(argp->argval, wdarg->argval);
4387c478bdstevel@tonic-gate					}
4397c478bdstevel@tonic-gate
4407c478bdstevel@tonic-gate					argp = wdarg;
4417c478bdstevel@tonic-gate					if (wdset && keywd)
4427c478bdstevel@tonic-gate					{
4437c478bdstevel@tonic-gate						argp->argnxt = (struct argnod *)argset;
4447c478bdstevel@tonic-gate						argset = (struct argnod **)argp;
4457c478bdstevel@tonic-gate					}
4467c478bdstevel@tonic-gate					else
4477c478bdstevel@tonic-gate					{
4487c478bdstevel@tonic-gate						*argtail = argp;
4497c478bdstevel@tonic-gate						argtail = &(argp->argnxt);
4507c478bdstevel@tonic-gate						keywd = flags & keyflg;
4517c478bdstevel@tonic-gate					}
4527c478bdstevel@tonic-gate					word();
4537c478bdstevel@tonic-gate					if (flag)
4547c478bdstevel@tonic-gate					{
4557c478bdstevel@tonic-gate						if (io)
4567c478bdstevel@tonic-gate						{
4577c478bdstevel@tonic-gate							while(io->ionxt)
4587c478bdstevel@tonic-gate								io = io->ionxt;
4597c478bdstevel@tonic-gate							io->ionxt = inout((struct ionod *)0);
4607c478bdstevel@tonic-gate						}
4617c478bdstevel@tonic-gate						else
4627c478bdstevel@tonic-gate							t->comio = io = inout((struct ionod *)0);
4637c478bdstevel@tonic-gate 					}
4647c478bdstevel@tonic-gate				}
4657c478bdstevel@tonic-gate
4667c478bdstevel@tonic-gate				t->comtyp = TCOM;
4677c478bdstevel@tonic-gate				t->comset = (struct argnod *)argset;
4687c478bdstevel@tonic-gate				*argtail = 0;
4697c478bdstevel@tonic-gate
4707c478bdstevel@tonic-gate				if (nohash == 0 && (fndef == 0 || (flags & hashflg)))
4717c478bdstevel@tonic-gate				{
4727c478bdstevel@tonic-gate					if (t->comarg)
4737c478bdstevel@tonic-gate					{
4747c478bdstevel@tonic-gate						com = t->comarg->argval;
4757c478bdstevel@tonic-gate						if (*com && *com != DOLLAR)
4767c478bdstevel@tonic-gate							pathlook(com, 0, t->comset);
4777c478bdstevel@tonic-gate					}
4787c478bdstevel@tonic-gate				}
4797c478bdstevel@tonic-gate
4807c478bdstevel@tonic-gate				return(r);
4817c478bdstevel@tonic-gate			}
4827c478bdstevel@tonic-gate		}
4837c478bdstevel@tonic-gate
4847c478bdstevel@tonic-gate	}
4857c478bdstevel@tonic-gate	reserv++;
4867c478bdstevel@tonic-gate	word();
4877c478bdstevel@tonic-gate	if (io = inout(io))
4887c478bdstevel@tonic-gate	{
4897c478bdstevel@tonic-gate		r = makefork(0,r);
4907c478bdstevel@tonic-gate		r->treio = io;
4917c478bdstevel@tonic-gate	}
4927c478bdstevel@tonic-gate	return(r);
4937c478bdstevel@tonic-gate}
4947c478bdstevel@tonic-gate
4957c478bdstevel@tonic-gate
4967c478bdstevel@tonic-gatestatic int
4977c478bdstevel@tonic-gateskipnl()
4987c478bdstevel@tonic-gate{
4997c478bdstevel@tonic-gate	while ((reserv++, word() == NL))
5007c478bdstevel@tonic-gate		chkpr();
5017c478bdstevel@tonic-gate	return(wdval);
5027c478bdstevel@tonic-gate}
5037c478bdstevel@tonic-gate
5047c478bdstevel@tonic-gatestatic struct ionod *
5057c478bdstevel@tonic-gateinout(lastio)
5067c478bdstevel@tonic-gate	struct ionod *lastio;
5077c478bdstevel@tonic-gate{
508965005cchin	int	iof;
509965005cchin	struct ionod *iop;
510965005cchin	unsigned int	c;
5117c478bdstevel@tonic-gate
5127c478bdstevel@tonic-gate	iof = wdnum;
5137c478bdstevel@tonic-gate	switch (wdval)
5147c478bdstevel@tonic-gate	{
5157c478bdstevel@tonic-gate	case DOCSYM:	/*	<<	*/
5161573d36nakanon		iof |= IODOC|IODOC_SUBST;
5177c478bdstevel@tonic-gate		break;
5187c478bdstevel@tonic-gate
5197c478bdstevel@tonic-gate	case APPSYM:	/*	>>	*/
5207c478bdstevel@tonic-gate	case '>':
5217c478bdstevel@tonic-gate		if (wdnum == 0)
5227c478bdstevel@tonic-gate			iof |= 1;
5237c478bdstevel@tonic-gate		iof |= IOPUT;
5247c478bdstevel@tonic-gate		if (wdval == APPSYM)
5257c478bdstevel@tonic-gate		{
5267c478bdstevel@tonic-gate			iof |= IOAPP;
5277c478bdstevel@tonic-gate			break;
5287c478bdstevel@tonic-gate		}
5297c478bdstevel@tonic-gate
5307c478bdstevel@tonic-gate	case '<':
5317c478bdstevel@tonic-gate		if ((c = nextwc()) == '&')
5327c478bdstevel@tonic-gate			iof |= IOMOV;
5337c478bdstevel@tonic-gate		else if (c == '>')
5347c478bdstevel@tonic-gate			iof |= IORDW;
5357c478bdstevel@tonic-gate		else
5367c478bdstevel@tonic-gate			peekn = c | MARK;
5377c478bdstevel@tonic-gate		break;
5387c478bdstevel@tonic-gate
5397c478bdstevel@tonic-gate	default:
5407c478bdstevel@tonic-gate		return(lastio);
5417c478bdstevel@tonic-gate	}
5427c478bdstevel@tonic-gate
5437c478bdstevel@tonic-gate	chkword();
5447c478bdstevel@tonic-gate	iop = (struct ionod *)getstor(sizeof(struct ionod));
5457c478bdstevel@tonic-gate
5467c478bdstevel@tonic-gate	if (fndef)
5477c478bdstevel@tonic-gate		iop->ioname = (char *) make(wdarg->argval);
5487c478bdstevel@tonic-gate	else
5497c478bdstevel@tonic-gate		iop->ioname = (char *) (wdarg->argval);
5507c478bdstevel@tonic-gate
5517c478bdstevel@tonic-gate	iop->iolink = 0;
5527c478bdstevel@tonic-gate	iop->iofile = iof;
5537c478bdstevel@tonic-gate	if (iof & IODOC)
5547c478bdstevel@tonic-gate	{
5557c478bdstevel@tonic-gate		iop->iolst = iopend;
5567c478bdstevel@tonic-gate		iopend = iop;
5577c478bdstevel@tonic-gate	}
5587c478bdstevel@tonic-gate	word();
5597c478bdstevel@tonic-gate	iop->ionxt = inout(lastio);
5607c478bdstevel@tonic-gate	return(iop);
5617c478bdstevel@tonic-gate}
5627c478bdstevel@tonic-gate
563965005cchinstatic void
564965005cchinchkword(void)
5657c478bdstevel@tonic-gate{
5667c478bdstevel@tonic-gate	if (word())
5677c478bdstevel@tonic-gate		synbad();
5687c478bdstevel@tonic-gate}
5697c478bdstevel@tonic-gate
570965005cchinstatic void
571965005cchinchksym(int sym)
5727c478bdstevel@tonic-gate{
573965005cchin	int	x = sym & wdval;
5747c478bdstevel@tonic-gate
5757c478bdstevel@tonic-gate	if (((x & SYMFLG) ? x : sym) != wdval)
5767c478bdstevel@tonic-gate		synbad();
5777c478bdstevel@tonic-gate}
5787c478bdstevel@tonic-gate
579965005cchinstatic void
580965005cchinprsym(int sym)
5817c478bdstevel@tonic-gate{
5827c478bdstevel@tonic-gate	if (sym & SYMFLG)
5837c478bdstevel@tonic-gate	{
584965005cchin		const struct sysnod *sp = reserved;
5857c478bdstevel@tonic-gate
5867c478bdstevel@tonic-gate		while (sp->sysval && sp->sysval != sym)
5877c478bdstevel@tonic-gate			sp++;
5887c478bdstevel@tonic-gate		prs(sp->sysnam);
5897c478bdstevel@tonic-gate	}
5907c478bdstevel@tonic-gate	else if (sym == EOFSYM)
59139e7390na		prs(_gettext(endoffile));
5927c478bdstevel@tonic-gate	else
5937c478bdstevel@tonic-gate	{
5947c478bdstevel@tonic-gate		if (sym & SYMREP)
5957c478bdstevel@tonic-gate			prc(sym);
5967c478bdstevel@tonic-gate		if (sym == NL)
59739e7390na			prs(_gettext(nlorsemi));
5987c478bdstevel@tonic-gate		else
5997c478bdstevel@tonic-gate			prc(sym);
6007c478bdstevel@tonic-gate	}
6017c478bdstevel@tonic-gate}
6027c478bdstevel@tonic-gate
603965005cchinstatic void
604965005cchinsynbad(void)
6057c478bdstevel@tonic-gate{
6067c478bdstevel@tonic-gate	prp();
60739e7390na	prs(_gettext(synmsg));
6087c478bdstevel@tonic-gate	if ((flags & ttyflg) == 0)
6097c478bdstevel@tonic-gate	{
61039e7390na		prs(_gettext(atline));
6117c478bdstevel@tonic-gate		prn(standin->flin);
6127c478bdstevel@tonic-gate	}
6137c478bdstevel@tonic-gate	prs(colon);
6147c478bdstevel@tonic-gate	prc(LQ);
6157c478bdstevel@tonic-gate	if (wdval)
6167c478bdstevel@tonic-gate		prsym(wdval);
6177c478bdstevel@tonic-gate	else
6187c478bdstevel@tonic-gate		prs_cntl(wdarg->argval);
6197c478bdstevel@tonic-gate	prc(RQ);
62039e7390na	prs(_gettext(unexpected));
6217c478bdstevel@tonic-gate	newline();
6227c478bdstevel@tonic-gate	exitsh(SYNBAD);
6237c478bdstevel@tonic-gate}
624