parser.c revision 222907
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/parser.c 222907 2011-06-09 23:12:23Z jilles $");
401556Srgrimes
4117987Speter#include <stdlib.h>
42149017Sstefanf#include <unistd.h>
43209337Sjilles#include <stdio.h>
4417987Speter
451556Srgrimes#include "shell.h"
461556Srgrimes#include "parser.h"
471556Srgrimes#include "nodes.h"
481556Srgrimes#include "expand.h"	/* defines rmescapes() */
491556Srgrimes#include "syntax.h"
501556Srgrimes#include "options.h"
511556Srgrimes#include "input.h"
521556Srgrimes#include "output.h"
531556Srgrimes#include "var.h"
541556Srgrimes#include "error.h"
551556Srgrimes#include "memalloc.h"
561556Srgrimes#include "mystring.h"
571556Srgrimes#include "alias.h"
5817987Speter#include "show.h"
5959436Scracauer#include "eval.h"
60214304Sjilles#include "exec.h"	/* to check for special builtins */
6117987Speter#ifndef NO_HISTORY
621556Srgrimes#include "myhistedit.h"
6317987Speter#endif
641556Srgrimes
651556Srgrimes/*
661556Srgrimes * Shell command parser.
671556Srgrimes */
681556Srgrimes
69142845Sobrien#define	EOFMARKLEN	79
70142845Sobrien#define	PROMPTLEN	128
711556Srgrimes
72214709Sjilles/* values of checkkwd variable */
73214709Sjilles#define CHKALIAS	0x1
74214709Sjilles#define CHKKWD		0x2
75214709Sjilles#define CHKNL		0x4
76214709Sjilles
771556Srgrimes/* values returned by readtoken */
7817987Speter#include "token.h"
791556Srgrimes
801556Srgrimes
811556Srgrimes
821556Srgrimesstruct heredoc {
831556Srgrimes	struct heredoc *next;	/* next here document in list */
841556Srgrimes	union node *here;		/* redirection node */
851556Srgrimes	char *eofmark;		/* string indicating end of input */
861556Srgrimes	int striptabs;		/* if set, strip leading tabs */
871556Srgrimes};
881556Srgrimes
89206145Sjillesstruct parser_temp {
90206145Sjilles	struct parser_temp *next;
91206145Sjilles	void *data;
92206145Sjilles};
931556Srgrimes
941556Srgrimes
95213760Sobrienstatic struct heredoc *heredoclist;	/* list of here documents to read */
96213760Sobrienstatic int doprompt;		/* if set, prompt the user */
97213760Sobrienstatic int needprompt;		/* true if interactive and at start of line */
98213760Sobrienstatic int lasttoken;		/* last token read */
991556SrgrimesMKINIT int tokpushback;		/* last token pushed back */
100213760Sobrienstatic char *wordtext;		/* text of last word returned by readtoken */
1011556SrgrimesMKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
102213760Sobrienstatic struct nodelist *backquotelist;
103213760Sobrienstatic union node *redirnode;
104213760Sobrienstatic struct heredoc *heredoc;
105213760Sobrienstatic int quoteflag;		/* set if (part of) last token was quoted */
106213760Sobrienstatic int startlinno;		/* line # where last token started */
107213760Sobrienstatic int funclinno;		/* line # where the current function started */
108213760Sobrienstatic struct parser_temp *parser_temp;
1091556Srgrimes
1101556Srgrimes
111214525Sjillesstatic union node *list(int, int);
112213811Sobrienstatic union node *andor(void);
113213811Sobrienstatic union node *pipeline(void);
114213811Sobrienstatic union node *command(void);
115213811Sobrienstatic union node *simplecmd(union node **, union node *);
116213811Sobrienstatic union node *makename(void);
117213811Sobrienstatic void parsefname(void);
118213811Sobrienstatic void parseheredoc(void);
119213811Sobrienstatic int peektoken(void);
120213811Sobrienstatic int readtoken(void);
121213811Sobrienstatic int xxreadtoken(void);
122213811Sobrienstatic int readtoken1(int, char const *, char *, int);
123213811Sobrienstatic int noexpand(char *);
124213811Sobrienstatic void synexpect(int) __dead2;
125213811Sobrienstatic void synerror(const char *) __dead2;
126213811Sobrienstatic void setprompt(int);
1271556Srgrimes
12817987Speter
129213811Sobrienstatic void *
130206145Sjillesparser_temp_alloc(size_t len)
131206145Sjilles{
132206145Sjilles	struct parser_temp *t;
133206145Sjilles
134206145Sjilles	INTOFF;
135206145Sjilles	t = ckmalloc(sizeof(*t));
136206145Sjilles	t->data = NULL;
137206145Sjilles	t->next = parser_temp;
138206145Sjilles	parser_temp = t;
139206145Sjilles	t->data = ckmalloc(len);
140206145Sjilles	INTON;
141206145Sjilles	return t->data;
142206145Sjilles}
143206145Sjilles
144206145Sjilles
145213811Sobrienstatic void *
146206145Sjillesparser_temp_realloc(void *ptr, size_t len)
147206145Sjilles{
148206145Sjilles	struct parser_temp *t;
149206145Sjilles
150206145Sjilles	INTOFF;
151206145Sjilles	t = parser_temp;
152206145Sjilles	if (ptr != t->data)
153206145Sjilles		error("bug: parser_temp_realloc misused");
154206145Sjilles	t->data = ckrealloc(t->data, len);
155206145Sjilles	INTON;
156206145Sjilles	return t->data;
157206145Sjilles}
158206145Sjilles
159206145Sjilles
160213811Sobrienstatic void
161206145Sjillesparser_temp_free_upto(void *ptr)
162206145Sjilles{
163206145Sjilles	struct parser_temp *t;
164206145Sjilles	int done = 0;
165206145Sjilles
166206145Sjilles	INTOFF;
167206145Sjilles	while (parser_temp != NULL && !done) {
168206145Sjilles		t = parser_temp;
169206145Sjilles		parser_temp = t->next;
170206145Sjilles		done = t->data == ptr;
171206145Sjilles		ckfree(t->data);
172206145Sjilles		ckfree(t);
173206145Sjilles	}
174206145Sjilles	INTON;
175206145Sjilles	if (!done)
176206145Sjilles		error("bug: parser_temp_free_upto misused");
177206145Sjilles}
178206145Sjilles
179206145Sjilles
180213811Sobrienstatic void
181206145Sjillesparser_temp_free_all(void)
182206145Sjilles{
183206145Sjilles	struct parser_temp *t;
184206145Sjilles
185206145Sjilles	INTOFF;
186206145Sjilles	while (parser_temp != NULL) {
187206145Sjilles		t = parser_temp;
188206145Sjilles		parser_temp = t->next;
189206145Sjilles		ckfree(t->data);
190206145Sjilles		ckfree(t);
191206145Sjilles	}
192206145Sjilles	INTON;
193206145Sjilles}
194206145Sjilles
195206145Sjilles
1961556Srgrimes/*
1971556Srgrimes * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
1981556Srgrimes * valid parse tree indicating a blank line.)
1991556Srgrimes */
2001556Srgrimes
2011556Srgrimesunion node *
20290111Simpparsecmd(int interact)
20317987Speter{
2041556Srgrimes	int t;
2051556Srgrimes
206206145Sjilles	/* This assumes the parser is not re-entered,
207206145Sjilles	 * which could happen if we add command substitution on PS1/PS2.
208206145Sjilles	 */
209206145Sjilles	parser_temp_free_all();
210208656Sjilles	heredoclist = NULL;
211206145Sjilles
21260593Scracauer	tokpushback = 0;
2131556Srgrimes	doprompt = interact;
2141556Srgrimes	if (doprompt)
2151556Srgrimes		setprompt(1);
2161556Srgrimes	else
2171556Srgrimes		setprompt(0);
2181556Srgrimes	needprompt = 0;
2191556Srgrimes	t = readtoken();
2201556Srgrimes	if (t == TEOF)
2211556Srgrimes		return NEOF;
2221556Srgrimes	if (t == TNL)
2231556Srgrimes		return NULL;
2241556Srgrimes	tokpushback++;
225214531Sjilles	return list(1, 1);
2261556Srgrimes}
2271556Srgrimes
2281556Srgrimes
229213811Sobrienstatic union node *
230214525Sjilleslist(int nlflag, int erflag)
23117987Speter{
232214599Sjilles	union node *ntop, *n1, *n2, *n3;
23317987Speter	int tok;
2341556Srgrimes
235214709Sjilles	checkkwd = CHKNL | CHKKWD | CHKALIAS;
236214531Sjilles	if (!nlflag && !erflag && tokendlist[peektoken()])
2371556Srgrimes		return NULL;
238214599Sjilles	ntop = n1 = NULL;
2391556Srgrimes	for (;;) {
24017987Speter		n2 = andor();
24117987Speter		tok = readtoken();
24217987Speter		if (tok == TBACKGND) {
24317987Speter			if (n2->type == NCMD || n2->type == NPIPE) {
24417987Speter				n2->ncmd.backgnd = 1;
24517987Speter			} else if (n2->type == NREDIR) {
24617987Speter				n2->type = NBACKGND;
24717987Speter			} else {
24817987Speter				n3 = (union node *)stalloc(sizeof (struct nredir));
24917987Speter				n3->type = NBACKGND;
25017987Speter				n3->nredir.n = n2;
25117987Speter				n3->nredir.redirect = NULL;
25217987Speter				n2 = n3;
25317987Speter			}
25417987Speter		}
255214599Sjilles		if (ntop == NULL)
256214599Sjilles			ntop = n2;
257214599Sjilles		else if (n1 == NULL) {
258214599Sjilles			n1 = (union node *)stalloc(sizeof (struct nbinary));
259214599Sjilles			n1->type = NSEMI;
260214599Sjilles			n1->nbinary.ch1 = ntop;
261214599Sjilles			n1->nbinary.ch2 = n2;
262214599Sjilles			ntop = n1;
26317987Speter		}
26417987Speter		else {
26517987Speter			n3 = (union node *)stalloc(sizeof (struct nbinary));
26617987Speter			n3->type = NSEMI;
267214599Sjilles			n3->nbinary.ch1 = n1->nbinary.ch2;
26817987Speter			n3->nbinary.ch2 = n2;
269214599Sjilles			n1->nbinary.ch2 = n3;
27017987Speter			n1 = n3;
27117987Speter		}
27217987Speter		switch (tok) {
27313882Sjoerg		case TBACKGND:
27417987Speter		case TSEMI:
27517987Speter			tok = readtoken();
276102410Scharnier			/* FALLTHROUGH */
2771556Srgrimes		case TNL:
27817987Speter			if (tok == TNL) {
27917987Speter				parseheredoc();
28017987Speter				if (nlflag)
281214599Sjilles					return ntop;
282210488Sjilles			} else if (tok == TEOF && nlflag) {
283210488Sjilles				parseheredoc();
284214599Sjilles				return ntop;
28517987Speter			} else {
28617987Speter				tokpushback++;
28717987Speter			}
288214709Sjilles			checkkwd = CHKNL | CHKKWD | CHKALIAS;
289214531Sjilles			if (!nlflag && !erflag && tokendlist[peektoken()])
290214599Sjilles				return ntop;
2911556Srgrimes			break;
2921556Srgrimes		case TEOF:
2931556Srgrimes			if (heredoclist)
2941556Srgrimes				parseheredoc();
2951556Srgrimes			else
2961556Srgrimes				pungetc();		/* push back EOF on input */
297214599Sjilles			return ntop;
2981556Srgrimes		default:
299214525Sjilles			if (nlflag || erflag)
3001556Srgrimes				synexpect(-1);
3011556Srgrimes			tokpushback++;
302214599Sjilles			return ntop;
3031556Srgrimes		}
3041556Srgrimes	}
3051556Srgrimes}
3061556Srgrimes
3071556Srgrimes
3081556Srgrimes
309213811Sobrienstatic union node *
31090111Simpandor(void)
31190111Simp{
3121556Srgrimes	union node *n1, *n2, *n3;
3131556Srgrimes	int t;
3141556Srgrimes
3151556Srgrimes	n1 = pipeline();
3161556Srgrimes	for (;;) {
3171556Srgrimes		if ((t = readtoken()) == TAND) {
3181556Srgrimes			t = NAND;
3191556Srgrimes		} else if (t == TOR) {
3201556Srgrimes			t = NOR;
3211556Srgrimes		} else {
3221556Srgrimes			tokpushback++;
3231556Srgrimes			return n1;
3241556Srgrimes		}
3251556Srgrimes		n2 = pipeline();
3261556Srgrimes		n3 = (union node *)stalloc(sizeof (struct nbinary));
3271556Srgrimes		n3->type = t;
3281556Srgrimes		n3->nbinary.ch1 = n1;
3291556Srgrimes		n3->nbinary.ch2 = n2;
3301556Srgrimes		n1 = n3;
3311556Srgrimes	}
3321556Srgrimes}
3331556Srgrimes
3341556Srgrimes
3351556Srgrimes
336213811Sobrienstatic union node *
33790111Simppipeline(void)
33890111Simp{
33975336Sbrian	union node *n1, *n2, *pipenode;
3401556Srgrimes	struct nodelist *lp, *prev;
341214281Sjilles	int negate, t;
3421556Srgrimes
34375336Sbrian	negate = 0;
344214709Sjilles	checkkwd = CHKNL | CHKKWD | CHKALIAS;
3451556Srgrimes	TRACE(("pipeline: entered\n"));
34675336Sbrian	while (readtoken() == TNOT)
34775336Sbrian		negate = !negate;
34875336Sbrian	tokpushback++;
3491556Srgrimes	n1 = command();
3501556Srgrimes	if (readtoken() == TPIPE) {
3511556Srgrimes		pipenode = (union node *)stalloc(sizeof (struct npipe));
3521556Srgrimes		pipenode->type = NPIPE;
3531556Srgrimes		pipenode->npipe.backgnd = 0;
3541556Srgrimes		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
3551556Srgrimes		pipenode->npipe.cmdlist = lp;
3561556Srgrimes		lp->n = n1;
3571556Srgrimes		do {
3581556Srgrimes			prev = lp;
3591556Srgrimes			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
360214709Sjilles			checkkwd = CHKNL | CHKKWD | CHKALIAS;
361214281Sjilles			t = readtoken();
362214281Sjilles			tokpushback++;
363214281Sjilles			if (t == TNOT)
364214281Sjilles				lp->n = pipeline();
365214281Sjilles			else
366214281Sjilles				lp->n = command();
3671556Srgrimes			prev->next = lp;
3681556Srgrimes		} while (readtoken() == TPIPE);
3691556Srgrimes		lp->next = NULL;
3701556Srgrimes		n1 = pipenode;
3711556Srgrimes	}
3721556Srgrimes	tokpushback++;
37375336Sbrian	if (negate) {
37475336Sbrian		n2 = (union node *)stalloc(sizeof (struct nnot));
37575336Sbrian		n2->type = NNOT;
37675336Sbrian		n2->nnot.com = n1;
37775336Sbrian		return n2;
37875336Sbrian	} else
37975336Sbrian		return n1;
3801556Srgrimes}
3811556Srgrimes
3821556Srgrimes
3831556Srgrimes
384213811Sobrienstatic union node *
38590111Simpcommand(void)
38690111Simp{
3871556Srgrimes	union node *n1, *n2;
3881556Srgrimes	union node *ap, **app;
3891556Srgrimes	union node *cp, **cpp;
3901556Srgrimes	union node *redir, **rpp;
391214281Sjilles	int t;
392218325Sjilles	int is_subshell;
3931556Srgrimes
394214709Sjilles	checkkwd = CHKNL | CHKKWD | CHKALIAS;
395218325Sjilles	is_subshell = 0;
39617987Speter	redir = NULL;
39717987Speter	n1 = NULL;
3981556Srgrimes	rpp = &redir;
39920425Ssteve
4001556Srgrimes	/* Check for redirection which may precede command */
4011556Srgrimes	while (readtoken() == TREDIR) {
4021556Srgrimes		*rpp = n2 = redirnode;
4031556Srgrimes		rpp = &n2->nfile.next;
4041556Srgrimes		parsefname();
4051556Srgrimes	}
4061556Srgrimes	tokpushback++;
4071556Srgrimes
4081556Srgrimes	switch (readtoken()) {
4091556Srgrimes	case TIF:
4101556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nif));
4111556Srgrimes		n1->type = NIF;
412214525Sjilles		if ((n1->nif.test = list(0, 0)) == NULL)
413104554Stjr			synexpect(-1);
4141556Srgrimes		if (readtoken() != TTHEN)
4151556Srgrimes			synexpect(TTHEN);
416214525Sjilles		n1->nif.ifpart = list(0, 0);
4171556Srgrimes		n2 = n1;
4181556Srgrimes		while (readtoken() == TELIF) {
4191556Srgrimes			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
4201556Srgrimes			n2 = n2->nif.elsepart;
4211556Srgrimes			n2->type = NIF;
422214525Sjilles			if ((n2->nif.test = list(0, 0)) == NULL)
423104554Stjr				synexpect(-1);
4241556Srgrimes			if (readtoken() != TTHEN)
4251556Srgrimes				synexpect(TTHEN);
426214525Sjilles			n2->nif.ifpart = list(0, 0);
4271556Srgrimes		}
4281556Srgrimes		if (lasttoken == TELSE)
429214525Sjilles			n2->nif.elsepart = list(0, 0);
4301556Srgrimes		else {
4311556Srgrimes			n2->nif.elsepart = NULL;
4321556Srgrimes			tokpushback++;
4331556Srgrimes		}
4341556Srgrimes		if (readtoken() != TFI)
4351556Srgrimes			synexpect(TFI);
436214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
4371556Srgrimes		break;
4381556Srgrimes	case TWHILE:
4391556Srgrimes	case TUNTIL: {
4401556Srgrimes		int got;
4411556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nbinary));
4421556Srgrimes		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
443214525Sjilles		if ((n1->nbinary.ch1 = list(0, 0)) == NULL)
444104554Stjr			synexpect(-1);
4451556Srgrimes		if ((got=readtoken()) != TDO) {
4461556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
4471556Srgrimes			synexpect(TDO);
4481556Srgrimes		}
449214525Sjilles		n1->nbinary.ch2 = list(0, 0);
4501556Srgrimes		if (readtoken() != TDONE)
4511556Srgrimes			synexpect(TDONE);
452214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
4531556Srgrimes		break;
4541556Srgrimes	}
4551556Srgrimes	case TFOR:
4561556Srgrimes		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
4571556Srgrimes			synerror("Bad for loop variable");
4581556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nfor));
4591556Srgrimes		n1->type = NFOR;
4601556Srgrimes		n1->nfor.var = wordtext;
461199282Sjilles		while (readtoken() == TNL)
462199282Sjilles			;
463199282Sjilles		if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
4641556Srgrimes			app = &ap;
4651556Srgrimes			while (readtoken() == TWORD) {
4661556Srgrimes				n2 = (union node *)stalloc(sizeof (struct narg));
4671556Srgrimes				n2->type = NARG;
4681556Srgrimes				n2->narg.text = wordtext;
4691556Srgrimes				n2->narg.backquote = backquotelist;
4701556Srgrimes				*app = n2;
4711556Srgrimes				app = &n2->narg.next;
4721556Srgrimes			}
4731556Srgrimes			*app = NULL;
4741556Srgrimes			n1->nfor.args = ap;
4751556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4761556Srgrimes				synexpect(-1);
4771556Srgrimes		} else {
478149096Sstefanf			static char argvars[5] = {
479149096Sstefanf				CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
480149096Sstefanf			};
4811556Srgrimes			n2 = (union node *)stalloc(sizeof (struct narg));
4821556Srgrimes			n2->type = NARG;
483149096Sstefanf			n2->narg.text = argvars;
4841556Srgrimes			n2->narg.backquote = NULL;
4851556Srgrimes			n2->narg.next = NULL;
4861556Srgrimes			n1->nfor.args = n2;
4871556Srgrimes			/*
4881556Srgrimes			 * Newline or semicolon here is optional (but note
4891556Srgrimes			 * that the original Bourne shell only allowed NL).
4901556Srgrimes			 */
4911556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4921556Srgrimes				tokpushback++;
4931556Srgrimes		}
494214709Sjilles		checkkwd = CHKNL | CHKKWD | CHKALIAS;
4951556Srgrimes		if ((t = readtoken()) == TDO)
4961556Srgrimes			t = TDONE;
4971556Srgrimes		else if (t == TBEGIN)
4981556Srgrimes			t = TEND;
4991556Srgrimes		else
5001556Srgrimes			synexpect(-1);
501214525Sjilles		n1->nfor.body = list(0, 0);
5021556Srgrimes		if (readtoken() != t)
5031556Srgrimes			synexpect(t);
504214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
5051556Srgrimes		break;
5061556Srgrimes	case TCASE:
5071556Srgrimes		n1 = (union node *)stalloc(sizeof (struct ncase));
5081556Srgrimes		n1->type = NCASE;
5091556Srgrimes		if (readtoken() != TWORD)
5101556Srgrimes			synexpect(TWORD);
5111556Srgrimes		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
5121556Srgrimes		n2->type = NARG;
5131556Srgrimes		n2->narg.text = wordtext;
5141556Srgrimes		n2->narg.backquote = backquotelist;
5151556Srgrimes		n2->narg.next = NULL;
5161556Srgrimes		while (readtoken() == TNL);
5171556Srgrimes		if (lasttoken != TWORD || ! equal(wordtext, "in"))
5181556Srgrimes			synerror("expecting \"in\"");
5191556Srgrimes		cpp = &n1->ncase.cases;
520214709Sjilles		checkkwd = CHKNL | CHKKWD, readtoken();
521104202Stjr		while (lasttoken != TESAC) {
5221556Srgrimes			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
5231556Srgrimes			cp->type = NCLIST;
5241556Srgrimes			app = &cp->nclist.pattern;
525104207Stjr			if (lasttoken == TLP)
526104207Stjr				readtoken();
5271556Srgrimes			for (;;) {
5281556Srgrimes				*app = ap = (union node *)stalloc(sizeof (struct narg));
5291556Srgrimes				ap->type = NARG;
5301556Srgrimes				ap->narg.text = wordtext;
5311556Srgrimes				ap->narg.backquote = backquotelist;
532214709Sjilles				checkkwd = CHKNL | CHKKWD;
533214709Sjilles				if (readtoken() != TPIPE)
5341556Srgrimes					break;
5351556Srgrimes				app = &ap->narg.next;
5362760Ssef				readtoken();
5371556Srgrimes			}
5381556Srgrimes			ap->narg.next = NULL;
5391556Srgrimes			if (lasttoken != TRP)
540214709Sjilles				synexpect(TRP);
541214525Sjilles			cp->nclist.body = list(0, 0);
5422760Ssef
543214709Sjilles			checkkwd = CHKNL | CHKKWD | CHKALIAS;
5442760Ssef			if ((t = readtoken()) != TESAC) {
5452760Ssef				if (t != TENDCASE)
546214709Sjilles					synexpect(TENDCASE);
5472760Ssef				else
548214709Sjilles					checkkwd = CHKNL | CHKKWD, readtoken();
5492760Ssef			}
5501556Srgrimes			cpp = &cp->nclist.next;
551104202Stjr		}
5521556Srgrimes		*cpp = NULL;
553214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
5541556Srgrimes		break;
5551556Srgrimes	case TLP:
5561556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nredir));
5571556Srgrimes		n1->type = NSUBSHELL;
558214525Sjilles		n1->nredir.n = list(0, 0);
5591556Srgrimes		n1->nredir.redirect = NULL;
5601556Srgrimes		if (readtoken() != TRP)
5611556Srgrimes			synexpect(TRP);
562214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
563218325Sjilles		is_subshell = 1;
5641556Srgrimes		break;
5651556Srgrimes	case TBEGIN:
566214525Sjilles		n1 = list(0, 0);
5671556Srgrimes		if (readtoken() != TEND)
5681556Srgrimes			synexpect(TEND);
569214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
5701556Srgrimes		break;
5711556Srgrimes	/* Handle an empty command like other simple commands.  */
572210221Sjilles	case TBACKGND:
57317987Speter	case TSEMI:
574101662Stjr	case TAND:
575101662Stjr	case TOR:
57617987Speter		/*
57717987Speter		 * An empty command before a ; doesn't make much sense, and
57817987Speter		 * should certainly be disallowed in the case of `if ;'.
57917987Speter		 */
58017987Speter		if (!redir)
58117987Speter			synexpect(-1);
5821556Srgrimes	case TNL:
58310399Sjoerg	case TEOF:
5841556Srgrimes	case TWORD:
58517987Speter	case TRP:
5861556Srgrimes		tokpushback++;
58775160Sbrian		n1 = simplecmd(rpp, redir);
588214281Sjilles		return n1;
5891556Srgrimes	default:
5901556Srgrimes		synexpect(-1);
5911556Srgrimes	}
5921556Srgrimes
5931556Srgrimes	/* Now check for redirection which may follow command */
5941556Srgrimes	while (readtoken() == TREDIR) {
5951556Srgrimes		*rpp = n2 = redirnode;
5961556Srgrimes		rpp = &n2->nfile.next;
5971556Srgrimes		parsefname();
5981556Srgrimes	}
5991556Srgrimes	tokpushback++;
6001556Srgrimes	*rpp = NULL;
6011556Srgrimes	if (redir) {
602218325Sjilles		if (!is_subshell) {
6031556Srgrimes			n2 = (union node *)stalloc(sizeof (struct nredir));
6041556Srgrimes			n2->type = NREDIR;
6051556Srgrimes			n2->nredir.n = n1;
6061556Srgrimes			n1 = n2;
6071556Srgrimes		}
6081556Srgrimes		n1->nredir.redirect = redir;
6091556Srgrimes	}
61075160Sbrian
611214281Sjilles	return n1;
6121556Srgrimes}
6131556Srgrimes
6141556Srgrimes
615213811Sobrienstatic union node *
61690111Simpsimplecmd(union node **rpp, union node *redir)
61790111Simp{
6181556Srgrimes	union node *args, **app;
6191556Srgrimes	union node **orig_rpp = rpp;
620210087Sjilles	union node *n = NULL;
621214304Sjilles	int special;
622222165Sjilles	int savecheckkwd;
6231556Srgrimes
6241556Srgrimes	/* If we don't have any redirections already, then we must reset */
6251556Srgrimes	/* rpp to be the address of the local redir variable.  */
6261556Srgrimes	if (redir == 0)
6271556Srgrimes		rpp = &redir;
6281556Srgrimes
6291556Srgrimes	args = NULL;
6301556Srgrimes	app = &args;
6318855Srgrimes	/*
6321556Srgrimes	 * We save the incoming value, because we need this for shell
6331556Srgrimes	 * functions.  There can not be a redirect or an argument between
6348855Srgrimes	 * the function name and the open parenthesis.
6351556Srgrimes	 */
6361556Srgrimes	orig_rpp = rpp;
6371556Srgrimes
638222165Sjilles	savecheckkwd = CHKALIAS;
639222165Sjilles
6401556Srgrimes	for (;;) {
641222165Sjilles		checkkwd = savecheckkwd;
6421556Srgrimes		if (readtoken() == TWORD) {
6431556Srgrimes			n = (union node *)stalloc(sizeof (struct narg));
6441556Srgrimes			n->type = NARG;
6451556Srgrimes			n->narg.text = wordtext;
6461556Srgrimes			n->narg.backquote = backquotelist;
6471556Srgrimes			*app = n;
6481556Srgrimes			app = &n->narg.next;
649222165Sjilles			if (savecheckkwd != 0 && !isassignment(wordtext))
650222165Sjilles				savecheckkwd = 0;
6511556Srgrimes		} else if (lasttoken == TREDIR) {
6521556Srgrimes			*rpp = n = redirnode;
6531556Srgrimes			rpp = &n->nfile.next;
6541556Srgrimes			parsefname();	/* read name of redirection file */
6551556Srgrimes		} else if (lasttoken == TLP && app == &args->narg.next
6561556Srgrimes					    && rpp == orig_rpp) {
6571556Srgrimes			/* We have a function */
6581556Srgrimes			if (readtoken() != TRP)
6591556Srgrimes				synexpect(TRP);
660179022Sstefanf			funclinno = plinno;
661214291Sjilles			/*
662214291Sjilles			 * - Require plain text.
663214291Sjilles			 * - Functions with '/' cannot be called.
664214534Sjilles			 * - Reject name=().
665214534Sjilles			 * - Reject ksh extended glob patterns.
666214291Sjilles			 */
667214291Sjilles			if (!noexpand(n->narg.text) || quoteflag ||
668214534Sjilles			    strchr(n->narg.text, '/') ||
669214534Sjilles			    strchr("!%*+-=?@}~",
670214534Sjilles				n->narg.text[strlen(n->narg.text) - 1]))
6711556Srgrimes				synerror("Bad function name");
672214291Sjilles			rmescapes(n->narg.text);
673214304Sjilles			if (find_builtin(n->narg.text, &special) >= 0 &&
674214304Sjilles			    special)
675214304Sjilles				synerror("Cannot override a special builtin with a function");
6761556Srgrimes			n->type = NDEFUN;
6771556Srgrimes			n->narg.next = command();
678179022Sstefanf			funclinno = 0;
679210087Sjilles			return n;
6801556Srgrimes		} else {
6811556Srgrimes			tokpushback++;
6821556Srgrimes			break;
6831556Srgrimes		}
6841556Srgrimes	}
6851556Srgrimes	*app = NULL;
6861556Srgrimes	*rpp = NULL;
6871556Srgrimes	n = (union node *)stalloc(sizeof (struct ncmd));
6881556Srgrimes	n->type = NCMD;
6891556Srgrimes	n->ncmd.backgnd = 0;
6901556Srgrimes	n->ncmd.args = args;
6911556Srgrimes	n->ncmd.redirect = redir;
692210087Sjilles	return n;
6931556Srgrimes}
6941556Srgrimes
695213811Sobrienstatic union node *
69690111Simpmakename(void)
69790111Simp{
69817987Speter	union node *n;
6991556Srgrimes
70017987Speter	n = (union node *)stalloc(sizeof (struct narg));
70117987Speter	n->type = NARG;
70217987Speter	n->narg.next = NULL;
70317987Speter	n->narg.text = wordtext;
70417987Speter	n->narg.backquote = backquotelist;
70517987Speter	return n;
70617987Speter}
70717987Speter
708213760Sobrienvoid
709213760Sobrienfixredir(union node *n, const char *text, int err)
71090111Simp{
71117987Speter	TRACE(("Fix redir %s %d\n", text, err));
71217987Speter	if (!err)
71317987Speter		n->ndup.vname = NULL;
71417987Speter
71517987Speter	if (is_digit(text[0]) && text[1] == '\0')
71617987Speter		n->ndup.dupfd = digit_val(text[0]);
71717987Speter	else if (text[0] == '-' && text[1] == '\0')
71817987Speter		n->ndup.dupfd = -1;
71917987Speter	else {
72020425Ssteve
72117987Speter		if (err)
72217987Speter			synerror("Bad fd number");
72317987Speter		else
72417987Speter			n->ndup.vname = makename();
72517987Speter	}
72617987Speter}
72717987Speter
72817987Speter
729213811Sobrienstatic void
73090111Simpparsefname(void)
73190111Simp{
7321556Srgrimes	union node *n = redirnode;
7331556Srgrimes
7341556Srgrimes	if (readtoken() != TWORD)
7351556Srgrimes		synexpect(-1);
7361556Srgrimes	if (n->type == NHERE) {
7371556Srgrimes		struct heredoc *here = heredoc;
7381556Srgrimes		struct heredoc *p;
7391556Srgrimes		int i;
7401556Srgrimes
7411556Srgrimes		if (quoteflag == 0)
7421556Srgrimes			n->type = NXHERE;
7431556Srgrimes		TRACE(("Here document %d\n", n->type));
7441556Srgrimes		if (here->striptabs) {
7451556Srgrimes			while (*wordtext == '\t')
7461556Srgrimes				wordtext++;
7471556Srgrimes		}
7481556Srgrimes		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
7491556Srgrimes			synerror("Illegal eof marker for << redirection");
7501556Srgrimes		rmescapes(wordtext);
7511556Srgrimes		here->eofmark = wordtext;
7521556Srgrimes		here->next = NULL;
7531556Srgrimes		if (heredoclist == NULL)
7541556Srgrimes			heredoclist = here;
7551556Srgrimes		else {
7561556Srgrimes			for (p = heredoclist ; p->next ; p = p->next);
7571556Srgrimes			p->next = here;
7581556Srgrimes		}
7591556Srgrimes	} else if (n->type == NTOFD || n->type == NFROMFD) {
76017987Speter		fixredir(n, wordtext, 0);
7611556Srgrimes	} else {
76217987Speter		n->nfile.fname = makename();
7631556Srgrimes	}
7641556Srgrimes}
7651556Srgrimes
7661556Srgrimes
7671556Srgrimes/*
7681556Srgrimes * Input any here documents.
7691556Srgrimes */
7701556Srgrimes
771213811Sobrienstatic void
77290111Simpparseheredoc(void)
77390111Simp{
7741556Srgrimes	struct heredoc *here;
7751556Srgrimes	union node *n;
7761556Srgrimes
7771556Srgrimes	while (heredoclist) {
7781556Srgrimes		here = heredoclist;
7791556Srgrimes		heredoclist = here->next;
7801556Srgrimes		if (needprompt) {
7811556Srgrimes			setprompt(2);
7821556Srgrimes			needprompt = 0;
7831556Srgrimes		}
7841556Srgrimes		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
7851556Srgrimes				here->eofmark, here->striptabs);
7861556Srgrimes		n = (union node *)stalloc(sizeof (struct narg));
7871556Srgrimes		n->narg.type = NARG;
7881556Srgrimes		n->narg.next = NULL;
7891556Srgrimes		n->narg.text = wordtext;
7901556Srgrimes		n->narg.backquote = backquotelist;
7911556Srgrimes		here->here->nhere.doc = n;
7921556Srgrimes	}
7931556Srgrimes}
7941556Srgrimes
795213811Sobrienstatic int
79690111Simppeektoken(void)
79790111Simp{
7981556Srgrimes	int t;
7991556Srgrimes
8001556Srgrimes	t = readtoken();
8011556Srgrimes	tokpushback++;
8021556Srgrimes	return (t);
8031556Srgrimes}
8041556Srgrimes
805213811Sobrienstatic int
80690111Simpreadtoken(void)
80790111Simp{
8081556Srgrimes	int t;
8091556Srgrimes	struct alias *ap;
8101556Srgrimes#ifdef DEBUG
8111556Srgrimes	int alreadyseen = tokpushback;
8121556Srgrimes#endif
8138855Srgrimes
8141556Srgrimes	top:
8151556Srgrimes	t = xxreadtoken();
8161556Srgrimes
817214709Sjilles	/*
818214709Sjilles	 * eat newlines
819214709Sjilles	 */
820214709Sjilles	if (checkkwd & CHKNL) {
821214709Sjilles		while (t == TNL) {
822214709Sjilles			parseheredoc();
823214709Sjilles			t = xxreadtoken();
824214709Sjilles		}
825214709Sjilles	}
8261556Srgrimes
827214709Sjilles	/*
828214709Sjilles	 * check for keywords and aliases
829214709Sjilles	 */
830214709Sjilles	if (t == TWORD && !quoteflag)
831214709Sjilles	{
832214709Sjilles		const char * const *pp;
833214709Sjilles
834214709Sjilles		if (checkkwd & CHKKWD)
83598463Sjmallett			for (pp = parsekwd; *pp; pp++) {
83620425Ssteve				if (**pp == *wordtext && equal(*pp, wordtext))
83717987Speter				{
8381556Srgrimes					lasttoken = t = pp - parsekwd + KWDOFFSET;
8391556Srgrimes					TRACE(("keyword %s recognized\n", tokname[t]));
8401556Srgrimes					goto out;
8411556Srgrimes				}
8421556Srgrimes			}
843214709Sjilles		if (checkkwd & CHKALIAS &&
844214709Sjilles		    (ap = lookupalias(wordtext, 1)) != NULL) {
845214709Sjilles			pushstring(ap->val, strlen(ap->val), ap);
846214709Sjilles			goto top;
8471556Srgrimes		}
848214709Sjilles	}
8491556Srgrimesout:
850214709Sjilles	if (t != TNOT)
851214709Sjilles		checkkwd = 0;
852214709Sjilles
8531556Srgrimes#ifdef DEBUG
8541556Srgrimes	if (!alreadyseen)
8551556Srgrimes	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8561556Srgrimes	else
8571556Srgrimes	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8581556Srgrimes#endif
8591556Srgrimes	return (t);
8601556Srgrimes}
8611556Srgrimes
8621556Srgrimes
8631556Srgrimes/*
8641556Srgrimes * Read the next input token.
8651556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in
8661556Srgrimes *	backquotes.  We set quoteflag to true if any part of the word was
8671556Srgrimes *	quoted.
8681556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing
8691556Srgrimes *	the redirection.
8701556Srgrimes * In all cases, the variable startlinno is set to the number of the line
8711556Srgrimes *	on which the token starts.
8721556Srgrimes *
8731556Srgrimes * [Change comment:  here documents and internal procedures]
8741556Srgrimes * [Readtoken shouldn't have any arguments.  Perhaps we should make the
8751556Srgrimes *  word parsing code into a separate routine.  In this case, readtoken
8761556Srgrimes *  doesn't need to have any internal procedures, but parseword does.
8771556Srgrimes *  We could also make parseoperator in essence the main routine, and
8781556Srgrimes *  have parseword (readtoken1?) handle both words and redirection.]
8791556Srgrimes */
8801556Srgrimes
8811556Srgrimes#define RETURN(token)	return lasttoken = token
8821556Srgrimes
883213811Sobrienstatic int
88490111Simpxxreadtoken(void)
88590111Simp{
88625230Ssteve	int c;
8871556Srgrimes
8881556Srgrimes	if (tokpushback) {
8891556Srgrimes		tokpushback = 0;
8901556Srgrimes		return lasttoken;
8911556Srgrimes	}
8921556Srgrimes	if (needprompt) {
8931556Srgrimes		setprompt(2);
8941556Srgrimes		needprompt = 0;
8951556Srgrimes	}
8961556Srgrimes	startlinno = plinno;
8971556Srgrimes	for (;;) {	/* until token or start of word found */
8981556Srgrimes		c = pgetc_macro();
8991556Srgrimes		switch (c) {
9001556Srgrimes		case ' ': case '\t':
9011556Srgrimes			continue;
9021556Srgrimes		case '#':
9031556Srgrimes			while ((c = pgetc()) != '\n' && c != PEOF);
9041556Srgrimes			pungetc();
9051556Srgrimes			continue;
9061556Srgrimes		case '\\':
9071556Srgrimes			if (pgetc() == '\n') {
9081556Srgrimes				startlinno = ++plinno;
9091556Srgrimes				if (doprompt)
9101556Srgrimes					setprompt(2);
9111556Srgrimes				else
9121556Srgrimes					setprompt(0);
9131556Srgrimes				continue;
9141556Srgrimes			}
9151556Srgrimes			pungetc();
9161556Srgrimes			goto breakloop;
9171556Srgrimes		case '\n':
9181556Srgrimes			plinno++;
9191556Srgrimes			needprompt = doprompt;
9201556Srgrimes			RETURN(TNL);
9211556Srgrimes		case PEOF:
9221556Srgrimes			RETURN(TEOF);
9231556Srgrimes		case '&':
9241556Srgrimes			if (pgetc() == '&')
9251556Srgrimes				RETURN(TAND);
9261556Srgrimes			pungetc();
9271556Srgrimes			RETURN(TBACKGND);
9281556Srgrimes		case '|':
9291556Srgrimes			if (pgetc() == '|')
9301556Srgrimes				RETURN(TOR);
9311556Srgrimes			pungetc();
9321556Srgrimes			RETURN(TPIPE);
9331556Srgrimes		case ';':
9341556Srgrimes			if (pgetc() == ';')
9351556Srgrimes				RETURN(TENDCASE);
9361556Srgrimes			pungetc();
9371556Srgrimes			RETURN(TSEMI);
9381556Srgrimes		case '(':
9391556Srgrimes			RETURN(TLP);
9401556Srgrimes		case ')':
9411556Srgrimes			RETURN(TRP);
9421556Srgrimes		default:
9431556Srgrimes			goto breakloop;
9441556Srgrimes		}
9451556Srgrimes	}
9461556Srgrimesbreakloop:
9471556Srgrimes	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
9481556Srgrimes#undef RETURN
9491556Srgrimes}
9501556Srgrimes
9511556Srgrimes
952213811Sobrien#define MAXNEST_static 8
953206145Sjillesstruct tokenstate
954206145Sjilles{
955206145Sjilles	const char *syntax; /* *SYNTAX */
956206145Sjilles	int parenlevel; /* levels of parentheses in arithmetic */
957206145Sjilles	enum tokenstate_category
958206145Sjilles	{
959206145Sjilles		TSTATE_TOP,
960206145Sjilles		TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
961206145Sjilles		TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
962206145Sjilles		TSTATE_ARITH
963206145Sjilles	} category;
964206145Sjilles};
965206145Sjilles
966206145Sjilles
967205130Sjilles/*
968205130Sjilles * Called to parse command substitutions.
969205130Sjilles */
9701556Srgrimes
971213811Sobrienstatic char *
972205130Sjillesparsebackq(char *out, struct nodelist **pbqlist,
973205130Sjilles		int oldstyle, int dblquote, int quoted)
974205130Sjilles{
975205130Sjilles	struct nodelist **nlpp;
976205130Sjilles	union node *n;
977205130Sjilles	char *volatile str;
978205130Sjilles	struct jmploc jmploc;
979205130Sjilles	struct jmploc *const savehandler = handler;
980205130Sjilles	int savelen;
981205130Sjilles	int saveprompt;
982205130Sjilles	const int bq_startlinno = plinno;
983205130Sjilles	char *volatile ostr = NULL;
984205130Sjilles	struct parsefile *const savetopfile = getcurrentfile();
985208655Sjilles	struct heredoc *const saveheredoclist = heredoclist;
986208655Sjilles	struct heredoc *here;
987205130Sjilles
988205130Sjilles	str = NULL;
989205130Sjilles	if (setjmp(jmploc.loc)) {
990205130Sjilles		popfilesupto(savetopfile);
991205130Sjilles		if (str)
992205130Sjilles			ckfree(str);
993205130Sjilles		if (ostr)
994205130Sjilles			ckfree(ostr);
995208655Sjilles		heredoclist = saveheredoclist;
996205130Sjilles		handler = savehandler;
997205130Sjilles		if (exception == EXERROR) {
998205130Sjilles			startlinno = bq_startlinno;
999205130Sjilles			synerror("Error in command substitution");
1000205130Sjilles		}
1001205130Sjilles		longjmp(handler->loc, 1);
1002205130Sjilles	}
1003205130Sjilles	INTOFF;
1004205130Sjilles	savelen = out - stackblock();
1005205130Sjilles	if (savelen > 0) {
1006205130Sjilles		str = ckmalloc(savelen);
1007205130Sjilles		memcpy(str, stackblock(), savelen);
1008205130Sjilles	}
1009205130Sjilles	handler = &jmploc;
1010208655Sjilles	heredoclist = NULL;
1011205130Sjilles	INTON;
1012205130Sjilles        if (oldstyle) {
1013205130Sjilles                /* We must read until the closing backquote, giving special
1014205130Sjilles                   treatment to some slashes, and then push the string and
1015205130Sjilles                   reread it as input, interpreting it normally.  */
1016205130Sjilles                char *oout;
1017205130Sjilles                int c;
1018205130Sjilles                int olen;
1019205130Sjilles
1020205130Sjilles
1021205130Sjilles                STARTSTACKSTR(oout);
1022205130Sjilles		for (;;) {
1023205130Sjilles			if (needprompt) {
1024205130Sjilles				setprompt(2);
1025205130Sjilles				needprompt = 0;
1026205130Sjilles			}
1027215783Sjilles			CHECKSTRSPACE(2, oout);
1028205130Sjilles			switch (c = pgetc()) {
1029205130Sjilles			case '`':
1030205130Sjilles				goto done;
1031205130Sjilles
1032205130Sjilles			case '\\':
1033205130Sjilles                                if ((c = pgetc()) == '\n') {
1034205130Sjilles					plinno++;
1035205130Sjilles					if (doprompt)
1036205130Sjilles						setprompt(2);
1037205130Sjilles					else
1038205130Sjilles						setprompt(0);
1039205130Sjilles					/*
1040205130Sjilles					 * If eating a newline, avoid putting
1041205130Sjilles					 * the newline into the new character
1042215783Sjilles					 * stream (via the USTPUTC after the
1043205130Sjilles					 * switch).
1044205130Sjilles					 */
1045205130Sjilles					continue;
1046205130Sjilles				}
1047205130Sjilles                                if (c != '\\' && c != '`' && c != '$'
1048205130Sjilles                                    && (!dblquote || c != '"'))
1049215783Sjilles                                        USTPUTC('\\', oout);
1050205130Sjilles				break;
1051205130Sjilles
1052205130Sjilles			case '\n':
1053205130Sjilles				plinno++;
1054205130Sjilles				needprompt = doprompt;
1055205130Sjilles				break;
1056205130Sjilles
1057205130Sjilles			case PEOF:
1058205130Sjilles			        startlinno = plinno;
1059205130Sjilles				synerror("EOF in backquote substitution");
1060205130Sjilles 				break;
1061205130Sjilles
1062205130Sjilles			default:
1063205130Sjilles				break;
1064205130Sjilles			}
1065215783Sjilles			USTPUTC(c, oout);
1066205130Sjilles                }
1067205130Sjillesdone:
1068215783Sjilles                USTPUTC('\0', oout);
1069205130Sjilles                olen = oout - stackblock();
1070205130Sjilles		INTOFF;
1071205130Sjilles		ostr = ckmalloc(olen);
1072205130Sjilles		memcpy(ostr, stackblock(), olen);
1073205130Sjilles		setinputstring(ostr, 1);
1074205130Sjilles		INTON;
1075205130Sjilles        }
1076205130Sjilles	nlpp = pbqlist;
1077205130Sjilles	while (*nlpp)
1078205130Sjilles		nlpp = &(*nlpp)->next;
1079205130Sjilles	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1080205130Sjilles	(*nlpp)->next = NULL;
1081205130Sjilles
1082205130Sjilles	if (oldstyle) {
1083205130Sjilles		saveprompt = doprompt;
1084205130Sjilles		doprompt = 0;
1085205130Sjilles	}
1086205130Sjilles
1087214525Sjilles	n = list(0, oldstyle);
1088205130Sjilles
1089205130Sjilles	if (oldstyle)
1090205130Sjilles		doprompt = saveprompt;
1091205130Sjilles	else {
1092205130Sjilles		if (readtoken() != TRP)
1093205130Sjilles			synexpect(TRP);
1094205130Sjilles	}
1095205130Sjilles
1096205130Sjilles	(*nlpp)->n = n;
1097205130Sjilles        if (oldstyle) {
1098205130Sjilles		/*
1099205130Sjilles		 * Start reading from old file again, ignoring any pushed back
1100205130Sjilles		 * tokens left from the backquote parsing
1101205130Sjilles		 */
1102205130Sjilles                popfile();
1103205130Sjilles		tokpushback = 0;
1104205130Sjilles	}
1105205130Sjilles	STARTSTACKSTR(out);
1106216706Sjilles	CHECKSTRSPACE(savelen + 1, out);
1107208655Sjilles	INTOFF;
1108205130Sjilles	if (str) {
1109205130Sjilles		memcpy(out, str, savelen);
1110205130Sjilles		STADJUST(savelen, out);
1111205130Sjilles		ckfree(str);
1112205130Sjilles		str = NULL;
1113205130Sjilles	}
1114205130Sjilles	if (ostr) {
1115205130Sjilles		ckfree(ostr);
1116205130Sjilles		ostr = NULL;
1117205130Sjilles	}
1118208655Sjilles	here = saveheredoclist;
1119208655Sjilles	if (here != NULL) {
1120208655Sjilles		while (here->next != NULL)
1121208655Sjilles			here = here->next;
1122208655Sjilles		here->next = heredoclist;
1123208655Sjilles		heredoclist = saveheredoclist;
1124208655Sjilles	}
1125205130Sjilles	handler = savehandler;
1126208655Sjilles	INTON;
1127205130Sjilles	if (quoted)
1128205130Sjilles		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1129205130Sjilles	else
1130205130Sjilles		USTPUTC(CTLBACKQ, out);
1131205130Sjilles	return out;
1132205130Sjilles}
1133205130Sjilles
1134205130Sjilles
11351556Srgrimes/*
1136221513Sjilles * Called to parse a backslash escape sequence inside $'...'.
1137221513Sjilles * The backslash has already been read.
1138221513Sjilles */
1139221513Sjillesstatic char *
1140221513Sjillesreadcstyleesc(char *out)
1141221513Sjilles{
1142221513Sjilles	int c, v, i, n;
1143221513Sjilles
1144221513Sjilles	c = pgetc();
1145221513Sjilles	switch (c) {
1146221513Sjilles	case '\0':
1147221513Sjilles		synerror("Unterminated quoted string");
1148221513Sjilles	case '\n':
1149221513Sjilles		plinno++;
1150221513Sjilles		if (doprompt)
1151221513Sjilles			setprompt(2);
1152221513Sjilles		else
1153221513Sjilles			setprompt(0);
1154221513Sjilles		return out;
1155221513Sjilles	case '\\':
1156221513Sjilles	case '\'':
1157221513Sjilles	case '"':
1158221513Sjilles		v = c;
1159221513Sjilles		break;
1160221513Sjilles	case 'a': v = '\a'; break;
1161221513Sjilles	case 'b': v = '\b'; break;
1162221513Sjilles	case 'e': v = '\033'; break;
1163221513Sjilles	case 'f': v = '\f'; break;
1164221513Sjilles	case 'n': v = '\n'; break;
1165221513Sjilles	case 'r': v = '\r'; break;
1166221513Sjilles	case 't': v = '\t'; break;
1167221513Sjilles	case 'v': v = '\v'; break;
1168221513Sjilles	case 'x':
1169221513Sjilles		  v = 0;
1170221513Sjilles		  for (;;) {
1171221513Sjilles			  c = pgetc();
1172221513Sjilles			  if (c >= '0' && c <= '9')
1173221513Sjilles				  v = (v << 4) + c - '0';
1174221513Sjilles			  else if (c >= 'A' && c <= 'F')
1175221513Sjilles				  v = (v << 4) + c - 'A' + 10;
1176221513Sjilles			  else if (c >= 'a' && c <= 'f')
1177221513Sjilles				  v = (v << 4) + c - 'a' + 10;
1178221513Sjilles			  else
1179221513Sjilles				  break;
1180221513Sjilles		  }
1181221513Sjilles		  pungetc();
1182221513Sjilles		  break;
1183221513Sjilles	case '0': case '1': case '2': case '3':
1184221513Sjilles	case '4': case '5': case '6': case '7':
1185221513Sjilles		  v = c - '0';
1186221513Sjilles		  c = pgetc();
1187221513Sjilles		  if (c >= '0' && c <= '7') {
1188221513Sjilles			  v <<= 3;
1189221513Sjilles			  v += c - '0';
1190221513Sjilles			  c = pgetc();
1191221513Sjilles			  if (c >= '0' && c <= '7') {
1192221513Sjilles				  v <<= 3;
1193221513Sjilles				  v += c - '0';
1194221513Sjilles			  } else
1195221513Sjilles				  pungetc();
1196221513Sjilles		  } else
1197221513Sjilles			  pungetc();
1198221513Sjilles		  break;
1199221513Sjilles	case 'c':
1200221513Sjilles		  c = pgetc();
1201221513Sjilles		  if (c < 0x3f || c > 0x7a || c == 0x60)
1202221513Sjilles			  synerror("Bad escape sequence");
1203221513Sjilles		  if (c == '\\' && pgetc() != '\\')
1204221513Sjilles			  synerror("Bad escape sequence");
1205221513Sjilles		  if (c == '?')
1206221513Sjilles			  v = 127;
1207221513Sjilles		  else
1208221513Sjilles			  v = c & 0x1f;
1209221513Sjilles		  break;
1210221513Sjilles	case 'u':
1211221513Sjilles	case 'U':
1212221513Sjilles		  n = c == 'U' ? 8 : 4;
1213221513Sjilles		  v = 0;
1214221513Sjilles		  for (i = 0; i < n; i++) {
1215221513Sjilles			  c = pgetc();
1216221513Sjilles			  if (c >= '0' && c <= '9')
1217221513Sjilles				  v = (v << 4) + c - '0';
1218221513Sjilles			  else if (c >= 'A' && c <= 'F')
1219221513Sjilles				  v = (v << 4) + c - 'A' + 10;
1220221513Sjilles			  else if (c >= 'a' && c <= 'f')
1221221513Sjilles				  v = (v << 4) + c - 'a' + 10;
1222221513Sjilles			  else
1223221513Sjilles				  synerror("Bad escape sequence");
1224221513Sjilles		  }
1225221513Sjilles		  if (v == 0 || (v >= 0xd800 && v <= 0xdfff))
1226221513Sjilles			  synerror("Bad escape sequence");
1227221513Sjilles		  /* We really need iconv here. */
1228221669Sjilles		  if (initial_localeisutf8 && v > 127) {
1229221669Sjilles			  CHECKSTRSPACE(4, out);
1230221669Sjilles			  /*
1231221669Sjilles			   * We cannot use wctomb() as the locale may have
1232221669Sjilles			   * changed.
1233221669Sjilles			   */
1234221669Sjilles			  if (v <= 0x7ff) {
1235221669Sjilles				  USTPUTC(0xc0 | v >> 6, out);
1236221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1237221669Sjilles				  return out;
1238221669Sjilles			  } else if (v <= 0xffff) {
1239221669Sjilles				  USTPUTC(0xe0 | v >> 12, out);
1240221669Sjilles				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1241221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1242221669Sjilles				  return out;
1243221669Sjilles			  } else if (v <= 0x10ffff) {
1244221669Sjilles				  USTPUTC(0xf0 | v >> 18, out);
1245221669Sjilles				  USTPUTC(0x80 | ((v >> 12) & 0x3f), out);
1246221669Sjilles				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1247221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1248221669Sjilles				  return out;
1249221669Sjilles			  }
1250221669Sjilles		  }
1251221513Sjilles		  if (v > 127)
1252221513Sjilles			  v = '?';
1253221513Sjilles		  break;
1254221513Sjilles	default:
1255221513Sjilles		  synerror("Bad escape sequence");
1256221513Sjilles	}
1257221513Sjilles	v = (char)v;
1258221513Sjilles	/*
1259221513Sjilles	 * We can't handle NUL bytes.
1260221513Sjilles	 * POSIX says we should skip till the closing quote.
1261221513Sjilles	 */
1262221513Sjilles	if (v == '\0') {
1263221513Sjilles		while ((c = pgetc()) != '\'') {
1264221513Sjilles			if (c == '\\')
1265221513Sjilles				c = pgetc();
1266221513Sjilles			if (c == PEOF)
1267221513Sjilles				synerror("Unterminated quoted string");
1268221513Sjilles		}
1269221513Sjilles		pungetc();
1270221513Sjilles		return out;
1271221513Sjilles	}
1272221513Sjilles	if (SQSYNTAX[v] == CCTL)
1273221513Sjilles		USTPUTC(CTLESC, out);
1274221513Sjilles	USTPUTC(v, out);
1275221513Sjilles	return out;
1276221513Sjilles}
1277221513Sjilles
1278221513Sjilles
1279221513Sjilles/*
12801556Srgrimes * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
12811556Srgrimes * is not NULL, read a here document.  In the latter case, eofmark is the
12821556Srgrimes * word which marks the end of the document and striptabs is true if
12831556Srgrimes * leading tabs should be stripped from the document.  The argument firstc
12841556Srgrimes * is the first character of the input token or document.
12851556Srgrimes *
12861556Srgrimes * Because C does not have internal subroutines, I have simulated them
12871556Srgrimes * using goto's to implement the subroutine linkage.  The following macros
12881556Srgrimes * will run code that appears at the end of readtoken1.
12891556Srgrimes */
12901556Srgrimes
12911556Srgrimes#define CHECKEND()	{goto checkend; checkend_return:;}
12921556Srgrimes#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
12931556Srgrimes#define PARSESUB()	{goto parsesub; parsesub_return:;}
12941556Srgrimes#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
12951556Srgrimes
1296213811Sobrienstatic int
1297206145Sjillesreadtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs)
129890111Simp{
129917987Speter	int c = firstc;
130017987Speter	char *out;
13011556Srgrimes	int len;
13021556Srgrimes	char line[EOFMARKLEN + 1];
13031556Srgrimes	struct nodelist *bqlist;
13041556Srgrimes	int quotef;
1305206145Sjilles	int newvarnest;
1306206145Sjilles	int level;
130754679Scracauer	int synentry;
1308213811Sobrien	struct tokenstate state_static[MAXNEST_static];
1309213811Sobrien	int maxnest = MAXNEST_static;
1310206145Sjilles	struct tokenstate *state = state_static;
1311221513Sjilles	int sqiscstyle = 0;
13121556Srgrimes
13131556Srgrimes	startlinno = plinno;
13141556Srgrimes	quotef = 0;
13151556Srgrimes	bqlist = NULL;
1316206145Sjilles	newvarnest = 0;
1317206145Sjilles	level = 0;
1318206145Sjilles	state[level].syntax = initialsyntax;
1319206145Sjilles	state[level].parenlevel = 0;
1320206145Sjilles	state[level].category = TSTATE_TOP;
13211556Srgrimes
13221556Srgrimes	STARTSTACKSTR(out);
13231556Srgrimes	loop: {	/* for each line, until end of word */
13241556Srgrimes		CHECKEND();	/* set c to PEOF if at end of here document */
13251556Srgrimes		for (;;) {	/* until end of line or end of word */
1326214512Sjilles			CHECKSTRSPACE(4, out);	/* permit 4 calls to USTPUTC */
132754679Scracauer
1328206145Sjilles			synentry = state[level].syntax[c];
132954679Scracauer
133054679Scracauer			switch(synentry) {
13311556Srgrimes			case CNL:	/* '\n' */
1332206145Sjilles				if (state[level].syntax == BASESYNTAX)
13331556Srgrimes					goto endword;	/* exit outer loop */
13341556Srgrimes				USTPUTC(c, out);
13351556Srgrimes				plinno++;
13361556Srgrimes				if (doprompt)
13371556Srgrimes					setprompt(2);
13381556Srgrimes				else
13391556Srgrimes					setprompt(0);
13401556Srgrimes				c = pgetc();
13411556Srgrimes				goto loop;		/* continue outer loop */
1342221513Sjilles			case CSBACK:
1343221513Sjilles				if (sqiscstyle) {
1344221513Sjilles					out = readcstyleesc(out);
1345221513Sjilles					break;
1346221513Sjilles				}
1347221513Sjilles				/* FALLTHROUGH */
13481556Srgrimes			case CWORD:
13491556Srgrimes				USTPUTC(c, out);
13501556Srgrimes				break;
13511556Srgrimes			case CCTL:
1352206145Sjilles				if (eofmark == NULL || initialsyntax != SQSYNTAX)
13531556Srgrimes					USTPUTC(CTLESC, out);
13541556Srgrimes				USTPUTC(c, out);
13551556Srgrimes				break;
13561556Srgrimes			case CBACK:	/* backslash */
13571556Srgrimes				c = pgetc();
13581556Srgrimes				if (c == PEOF) {
13591556Srgrimes					USTPUTC('\\', out);
13601556Srgrimes					pungetc();
13611556Srgrimes				} else if (c == '\n') {
1362160849Syar					plinno++;
13631556Srgrimes					if (doprompt)
13641556Srgrimes						setprompt(2);
13651556Srgrimes					else
13661556Srgrimes						setprompt(0);
13671556Srgrimes				} else {
1368206145Sjilles					if (state[level].syntax == DQSYNTAX &&
1369206145Sjilles					    c != '\\' && c != '`' && c != '$' &&
1370206145Sjilles					    (c != '"' || (eofmark != NULL &&
1371206145Sjilles						newvarnest == 0)) &&
1372206145Sjilles					    (c != '}' || state[level].category != TSTATE_VAR_OLD))
13731556Srgrimes						USTPUTC('\\', out);
1374214512Sjilles					if ((eofmark == NULL ||
1375214512Sjilles					    newvarnest > 0) &&
1376214512Sjilles					    state[level].syntax == BASESYNTAX)
1377214512Sjilles						USTPUTC(CTLQUOTEMARK, out);
137883675Stegge					if (SQSYNTAX[c] == CCTL)
13791556Srgrimes						USTPUTC(CTLESC, out);
13801556Srgrimes					USTPUTC(c, out);
1381214512Sjilles					if ((eofmark == NULL ||
1382214512Sjilles					    newvarnest > 0) &&
1383214512Sjilles					    state[level].syntax == BASESYNTAX &&
1384214512Sjilles					    state[level].category == TSTATE_VAR_OLD)
1385214512Sjilles						USTPUTC(CTLQUOTEEND, out);
13861556Srgrimes					quotef++;
13871556Srgrimes				}
13881556Srgrimes				break;
13891556Srgrimes			case CSQUOTE:
1390206145Sjilles				USTPUTC(CTLQUOTEMARK, out);
1391206145Sjilles				state[level].syntax = SQSYNTAX;
1392221513Sjilles				sqiscstyle = 0;
13931556Srgrimes				break;
13941556Srgrimes			case CDQUOTE:
1395206145Sjilles				USTPUTC(CTLQUOTEMARK, out);
1396206145Sjilles				state[level].syntax = DQSYNTAX;
13971556Srgrimes				break;
13981556Srgrimes			case CENDQUOTE:
1399206145Sjilles				if (eofmark != NULL && newvarnest == 0)
14001556Srgrimes					USTPUTC(c, out);
1401206145Sjilles				else {
1402214512Sjilles					if (state[level].category == TSTATE_VAR_OLD)
1403214512Sjilles						USTPUTC(CTLQUOTEEND, out);
1404214305Sjilles					state[level].syntax = BASESYNTAX;
14051556Srgrimes					quotef++;
14061556Srgrimes				}
14071556Srgrimes				break;
14081556Srgrimes			case CVAR:	/* '$' */
14091556Srgrimes				PARSESUB();		/* parse substitution */
14101556Srgrimes				break;
14111556Srgrimes			case CENDVAR:	/* '}' */
1412206145Sjilles				if (level > 0 &&
1413214492Sjilles				    ((state[level].category == TSTATE_VAR_OLD &&
1414214492Sjilles				      state[level].syntax ==
1415214492Sjilles				      state[level - 1].syntax) ||
1416214490Sjilles				    (state[level].category == TSTATE_VAR_NEW &&
1417214490Sjilles				     state[level].syntax == BASESYNTAX))) {
1418214492Sjilles					if (state[level].category == TSTATE_VAR_NEW)
1419206145Sjilles						newvarnest--;
1420206145Sjilles					level--;
14211556Srgrimes					USTPUTC(CTLENDVAR, out);
14221556Srgrimes				} else {
14231556Srgrimes					USTPUTC(c, out);
14241556Srgrimes				}
14251556Srgrimes				break;
14261556Srgrimes			case CLP:	/* '(' in arithmetic */
1427206145Sjilles				state[level].parenlevel++;
14281556Srgrimes				USTPUTC(c, out);
14291556Srgrimes				break;
14301556Srgrimes			case CRP:	/* ')' in arithmetic */
1431206145Sjilles				if (state[level].parenlevel > 0) {
14321556Srgrimes					USTPUTC(c, out);
1433206145Sjilles					--state[level].parenlevel;
14341556Srgrimes				} else {
14351556Srgrimes					if (pgetc() == ')') {
1436206145Sjilles						if (level > 0 &&
1437206145Sjilles						    state[level].category == TSTATE_ARITH) {
1438206145Sjilles							level--;
14391556Srgrimes							USTPUTC(CTLENDARI, out);
14401556Srgrimes						} else
14411556Srgrimes							USTPUTC(')', out);
14421556Srgrimes					} else {
14438855Srgrimes						/*
14441556Srgrimes						 * unbalanced parens
14451556Srgrimes						 *  (don't 2nd guess - no error)
14461556Srgrimes						 */
14471556Srgrimes						pungetc();
14481556Srgrimes						USTPUTC(')', out);
14491556Srgrimes					}
14501556Srgrimes				}
14511556Srgrimes				break;
14521556Srgrimes			case CBQUOTE:	/* '`' */
1453206145Sjilles				out = parsebackq(out, &bqlist, 1,
1454206145Sjilles				    state[level].syntax == DQSYNTAX &&
1455206145Sjilles				    (eofmark == NULL || newvarnest > 0),
1456206145Sjilles				    state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
14571556Srgrimes				break;
14581556Srgrimes			case CEOF:
14591556Srgrimes				goto endword;		/* exit outer loop */
1460214305Sjilles			case CIGN:
1461214305Sjilles				break;
14621556Srgrimes			default:
1463206145Sjilles				if (level == 0)
14641556Srgrimes					goto endword;	/* exit outer loop */
14651556Srgrimes				USTPUTC(c, out);
14661556Srgrimes			}
14671556Srgrimes			c = pgetc_macro();
14681556Srgrimes		}
14691556Srgrimes	}
14701556Srgrimesendword:
1471206145Sjilles	if (state[level].syntax == ARISYNTAX)
14721556Srgrimes		synerror("Missing '))'");
1473206145Sjilles	if (state[level].syntax != BASESYNTAX && eofmark == NULL)
14741556Srgrimes		synerror("Unterminated quoted string");
1475206145Sjilles	if (state[level].category == TSTATE_VAR_OLD ||
1476206145Sjilles	    state[level].category == TSTATE_VAR_NEW) {
14771556Srgrimes		startlinno = plinno;
14781556Srgrimes		synerror("Missing '}'");
14791556Srgrimes	}
1480206145Sjilles	if (state != state_static)
1481206145Sjilles		parser_temp_free_upto(state);
14821556Srgrimes	USTPUTC('\0', out);
14831556Srgrimes	len = out - stackblock();
14841556Srgrimes	out = stackblock();
14851556Srgrimes	if (eofmark == NULL) {
14861556Srgrimes		if ((c == '>' || c == '<')
14871556Srgrimes		 && quotef == 0
14881556Srgrimes		 && len <= 2
14891556Srgrimes		 && (*out == '\0' || is_digit(*out))) {
14901556Srgrimes			PARSEREDIR();
14911556Srgrimes			return lasttoken = TREDIR;
14921556Srgrimes		} else {
14931556Srgrimes			pungetc();
14941556Srgrimes		}
14951556Srgrimes	}
14961556Srgrimes	quoteflag = quotef;
14971556Srgrimes	backquotelist = bqlist;
14981556Srgrimes	grabstackblock(len);
14991556Srgrimes	wordtext = out;
15001556Srgrimes	return lasttoken = TWORD;
15011556Srgrimes/* end of readtoken routine */
15021556Srgrimes
15031556Srgrimes
15041556Srgrimes/*
15051556Srgrimes * Check to see whether we are at the end of the here document.  When this
15061556Srgrimes * is called, c is set to the first character of the next input line.  If
15071556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF.
15081556Srgrimes */
15091556Srgrimes
15101556Srgrimescheckend: {
15111556Srgrimes	if (eofmark) {
15121556Srgrimes		if (striptabs) {
15131556Srgrimes			while (c == '\t')
15141556Srgrimes				c = pgetc();
15151556Srgrimes		}
15161556Srgrimes		if (c == *eofmark) {
15171556Srgrimes			if (pfgets(line, sizeof line) != NULL) {
151825230Ssteve				char *p, *q;
15191556Srgrimes
15201556Srgrimes				p = line;
15211556Srgrimes				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1522222134Sjilles				if ((*p == '\0' || *p == '\n') && *q == '\0') {
15231556Srgrimes					c = PEOF;
1524222134Sjilles					if (*p == '\n') {
1525222134Sjilles						plinno++;
1526222134Sjilles						needprompt = doprompt;
1527222134Sjilles					}
15281556Srgrimes				} else {
15291556Srgrimes					pushstring(line, strlen(line), NULL);
15301556Srgrimes				}
15311556Srgrimes			}
15321556Srgrimes		}
15331556Srgrimes	}
15341556Srgrimes	goto checkend_return;
15351556Srgrimes}
15361556Srgrimes
15371556Srgrimes
15381556Srgrimes/*
15391556Srgrimes * Parse a redirection operator.  The variable "out" points to a string
15401556Srgrimes * specifying the fd to be redirected.  The variable "c" contains the
15411556Srgrimes * first character of the redirection operator.
15421556Srgrimes */
15431556Srgrimes
15441556Srgrimesparseredir: {
15451556Srgrimes	char fd = *out;
15461556Srgrimes	union node *np;
15471556Srgrimes
15481556Srgrimes	np = (union node *)stalloc(sizeof (struct nfile));
15491556Srgrimes	if (c == '>') {
15501556Srgrimes		np->nfile.fd = 1;
15511556Srgrimes		c = pgetc();
15521556Srgrimes		if (c == '>')
15531556Srgrimes			np->type = NAPPEND;
15541556Srgrimes		else if (c == '&')
15551556Srgrimes			np->type = NTOFD;
155696922Stjr		else if (c == '|')
155796922Stjr			np->type = NCLOBBER;
15581556Srgrimes		else {
15591556Srgrimes			np->type = NTO;
15601556Srgrimes			pungetc();
15611556Srgrimes		}
15621556Srgrimes	} else {	/* c == '<' */
15631556Srgrimes		np->nfile.fd = 0;
15641556Srgrimes		c = pgetc();
15651556Srgrimes		if (c == '<') {
15661556Srgrimes			if (sizeof (struct nfile) != sizeof (struct nhere)) {
15671556Srgrimes				np = (union node *)stalloc(sizeof (struct nhere));
15681556Srgrimes				np->nfile.fd = 0;
15691556Srgrimes			}
15701556Srgrimes			np->type = NHERE;
15711556Srgrimes			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
15721556Srgrimes			heredoc->here = np;
15731556Srgrimes			if ((c = pgetc()) == '-') {
15741556Srgrimes				heredoc->striptabs = 1;
15751556Srgrimes			} else {
15761556Srgrimes				heredoc->striptabs = 0;
15771556Srgrimes				pungetc();
15781556Srgrimes			}
15791556Srgrimes		} else if (c == '&')
15801556Srgrimes			np->type = NFROMFD;
158166612Sbrian		else if (c == '>')
158266612Sbrian			np->type = NFROMTO;
15831556Srgrimes		else {
15841556Srgrimes			np->type = NFROM;
15851556Srgrimes			pungetc();
15861556Srgrimes		}
15871556Srgrimes	}
15881556Srgrimes	if (fd != '\0')
15891556Srgrimes		np->nfile.fd = digit_val(fd);
15901556Srgrimes	redirnode = np;
15911556Srgrimes	goto parseredir_return;
15921556Srgrimes}
15931556Srgrimes
15941556Srgrimes
15951556Srgrimes/*
15961556Srgrimes * Parse a substitution.  At this point, we have read the dollar sign
15971556Srgrimes * and nothing else.
15981556Srgrimes */
15991556Srgrimes
16001556Srgrimesparsesub: {
1601179022Sstefanf	char buf[10];
16021556Srgrimes	int subtype;
16031556Srgrimes	int typeloc;
16041556Srgrimes	int flags;
16051556Srgrimes	char *p;
16061556Srgrimes	static const char types[] = "}-+?=";
1607179022Sstefanf	int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1608179022Sstefanf	int linno;
1609179387Sstefanf	int length;
1610219623Sjilles	int c1;
16111556Srgrimes
16121556Srgrimes	c = pgetc();
1613221513Sjilles	if (c == '(') {	/* $(command) or $((arith)) */
16141556Srgrimes		if (pgetc() == '(') {
16151556Srgrimes			PARSEARITH();
16161556Srgrimes		} else {
16171556Srgrimes			pungetc();
1618206145Sjilles			out = parsebackq(out, &bqlist, 0,
1619206145Sjilles			    state[level].syntax == DQSYNTAX &&
1620206145Sjilles			    (eofmark == NULL || newvarnest > 0),
1621206145Sjilles			    state[level].syntax == DQSYNTAX ||
1622206145Sjilles			    state[level].syntax == ARISYNTAX);
16231556Srgrimes		}
1624221513Sjilles	} else if (c == '{' || is_name(c) || is_special(c)) {
16251556Srgrimes		USTPUTC(CTLVAR, out);
16261556Srgrimes		typeloc = out - stackblock();
16271556Srgrimes		USTPUTC(VSNORMAL, out);
16281556Srgrimes		subtype = VSNORMAL;
1629179022Sstefanf		flags = 0;
16301556Srgrimes		if (c == '{') {
163118202Speter			bracketed_name = 1;
16321556Srgrimes			c = pgetc();
1633219623Sjilles			subtype = 0;
16341556Srgrimes		}
1635219623Sjillesvarname:
1636149026Sstefanf		if (!is_eof(c) && is_name(c)) {
1637179387Sstefanf			length = 0;
16381556Srgrimes			do {
16391556Srgrimes				STPUTC(c, out);
16401556Srgrimes				c = pgetc();
1641179387Sstefanf				length++;
1642149026Sstefanf			} while (!is_eof(c) && is_in_name(c));
1643179387Sstefanf			if (length == 6 &&
1644179387Sstefanf			    strncmp(out - length, "LINENO", length) == 0) {
1645179022Sstefanf				/* Replace the variable name with the
1646179022Sstefanf				 * current line number. */
1647179022Sstefanf				linno = plinno;
1648179022Sstefanf				if (funclinno != 0)
1649179022Sstefanf					linno -= funclinno - 1;
1650179022Sstefanf				snprintf(buf, sizeof(buf), "%d", linno);
1651179022Sstefanf				STADJUST(-6, out);
1652215783Sjilles				STPUTS(buf, out);
1653179022Sstefanf				flags |= VSLINENO;
1654179022Sstefanf			}
165518202Speter		} else if (is_digit(c)) {
165618202Speter			if (bracketed_name) {
165718202Speter				do {
165818202Speter					STPUTC(c, out);
165918202Speter					c = pgetc();
166018202Speter				} while (is_digit(c));
166118202Speter			} else {
166218202Speter				STPUTC(c, out);
166318202Speter				c = pgetc();
166418202Speter			}
1665219623Sjilles		} else if (is_special(c)) {
1666219623Sjilles			c1 = c;
1667219623Sjilles			c = pgetc();
1668219623Sjilles			if (subtype == 0 && c1 == '#') {
1669219623Sjilles				subtype = VSLENGTH;
1670219623Sjilles				if (strchr(types, c) == NULL && c != ':' &&
1671219623Sjilles				    c != '#' && c != '%')
1672219623Sjilles					goto varname;
1673219623Sjilles				c1 = c;
1674219623Sjilles				c = pgetc();
1675219623Sjilles				if (c1 != '}' && c == '}') {
1676219623Sjilles					pungetc();
1677219623Sjilles					c = c1;
1678219623Sjilles					goto varname;
1679219623Sjilles				}
1680219623Sjilles				pungetc();
1681219623Sjilles				c = c1;
1682219623Sjilles				c1 = '#';
1683219623Sjilles				subtype = 0;
1684219623Sjilles			}
1685219623Sjilles			USTPUTC(c1, out);
16861556Srgrimes		} else {
1687219623Sjilles			subtype = VSERROR;
1688219623Sjilles			if (c == '}')
1689219623Sjilles				pungetc();
1690219623Sjilles			else if (c == '\n' || c == PEOF)
1691219623Sjilles				synerror("Unexpected end of line in substitution");
1692219623Sjilles			else
1693164003Sstefanf				USTPUTC(c, out);
16941556Srgrimes		}
16951556Srgrimes		if (subtype == 0) {
169617987Speter			switch (c) {
169717987Speter			case ':':
1698179022Sstefanf				flags |= VSNUL;
16991556Srgrimes				c = pgetc();
170017987Speter				/*FALLTHROUGH*/
170117987Speter			default:
170217987Speter				p = strchr(types, c);
1703164003Sstefanf				if (p == NULL) {
1704206144Sjilles					if (c == '\n' || c == PEOF)
1705206144Sjilles						synerror("Unexpected end of line in substitution");
1706164003Sstefanf					if (flags == VSNUL)
1707164003Sstefanf						STPUTC(':', out);
1708164003Sstefanf					STPUTC(c, out);
1709164003Sstefanf					subtype = VSERROR;
1710164003Sstefanf				} else
1711164003Sstefanf					subtype = p - types + VSNORMAL;
171217987Speter				break;
171317987Speter			case '%':
171420425Ssteve			case '#':
171517987Speter				{
171617987Speter					int cc = c;
171717987Speter					subtype = c == '#' ? VSTRIMLEFT :
171817987Speter							     VSTRIMRIGHT;
171917987Speter					c = pgetc();
172017987Speter					if (c == cc)
172117987Speter						subtype++;
172217987Speter					else
172317987Speter						pungetc();
172417987Speter					break;
172517987Speter				}
17261556Srgrimes			}
1727164003Sstefanf		} else if (subtype != VSERROR) {
1728221461Sjilles			if (subtype == VSLENGTH && c != '}')
1729221461Sjilles				subtype = VSERROR;
17301556Srgrimes			pungetc();
17311556Srgrimes		}
1732164003Sstefanf		STPUTC('=', out);
1733220903Sjilles		if (state[level].syntax == DQSYNTAX ||
1734220903Sjilles		    state[level].syntax == ARISYNTAX)
17351556Srgrimes			flags |= VSQUOTE;
17361556Srgrimes		*(stackblock() + typeloc) = subtype | flags;
1737206145Sjilles		if (subtype != VSNORMAL) {
1738206145Sjilles			if (level + 1 >= maxnest) {
1739206145Sjilles				maxnest *= 2;
1740206145Sjilles				if (state == state_static) {
1741206145Sjilles					state = parser_temp_alloc(
1742206145Sjilles					    maxnest * sizeof(*state));
1743206145Sjilles					memcpy(state, state_static,
1744213811Sobrien					    MAXNEST_static * sizeof(*state));
1745206145Sjilles				} else
1746206145Sjilles					state = parser_temp_realloc(state,
1747206145Sjilles					    maxnest * sizeof(*state));
1748206145Sjilles			}
1749206145Sjilles			level++;
1750206145Sjilles			state[level].parenlevel = 0;
1751206145Sjilles			if (subtype == VSMINUS || subtype == VSPLUS ||
1752206145Sjilles			    subtype == VSQUESTION || subtype == VSASSIGN) {
1753206145Sjilles				/*
1754206145Sjilles				 * For operators that were in the Bourne shell,
1755206145Sjilles				 * inherit the double-quote state.
1756206145Sjilles				 */
1757206145Sjilles				state[level].syntax = state[level - 1].syntax;
1758206145Sjilles				state[level].category = TSTATE_VAR_OLD;
1759206145Sjilles			} else {
1760206145Sjilles				/*
1761206145Sjilles				 * The other operators take a pattern,
1762206145Sjilles				 * so go to BASESYNTAX.
1763206145Sjilles				 * Also, ' and " are now special, even
1764206145Sjilles				 * in here documents.
1765206145Sjilles				 */
1766206145Sjilles				state[level].syntax = BASESYNTAX;
1767206145Sjilles				state[level].category = TSTATE_VAR_NEW;
1768206145Sjilles				newvarnest++;
1769206145Sjilles			}
1770206145Sjilles		}
1771221513Sjilles	} else if (c == '\'' && state[level].syntax == BASESYNTAX) {
1772221513Sjilles		/* $'cstylequotes' */
1773221513Sjilles		USTPUTC(CTLQUOTEMARK, out);
1774221513Sjilles		state[level].syntax = SQSYNTAX;
1775221513Sjilles		sqiscstyle = 1;
1776221513Sjilles	} else {
1777221513Sjilles		USTPUTC('$', out);
1778221513Sjilles		pungetc();
17791556Srgrimes	}
17801556Srgrimes	goto parsesub_return;
17811556Srgrimes}
17821556Srgrimes
17831556Srgrimes
17841556Srgrimes/*
17851556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state)
17861556Srgrimes */
17871556Srgrimesparsearith: {
17881556Srgrimes
1789206145Sjilles	if (level + 1 >= maxnest) {
1790206145Sjilles		maxnest *= 2;
1791206145Sjilles		if (state == state_static) {
1792206145Sjilles			state = parser_temp_alloc(
1793206145Sjilles			    maxnest * sizeof(*state));
1794206145Sjilles			memcpy(state, state_static,
1795213811Sobrien			    MAXNEST_static * sizeof(*state));
1796206145Sjilles		} else
1797206145Sjilles			state = parser_temp_realloc(state,
1798206145Sjilles			    maxnest * sizeof(*state));
17991556Srgrimes	}
1800206145Sjilles	level++;
1801206145Sjilles	state[level].syntax = ARISYNTAX;
1802206145Sjilles	state[level].parenlevel = 0;
1803206145Sjilles	state[level].category = TSTATE_ARITH;
1804206145Sjilles	USTPUTC(CTLARI, out);
1805206145Sjilles	if (state[level - 1].syntax == DQSYNTAX)
1806206145Sjilles		USTPUTC('"',out);
1807206145Sjilles	else
1808206145Sjilles		USTPUTC(' ',out);
18091556Srgrimes	goto parsearith_return;
18101556Srgrimes}
18111556Srgrimes
18121556Srgrimes} /* end of readtoken */
18131556Srgrimes
18141556Srgrimes
18151556Srgrimes
18161556Srgrimes#ifdef mkinit
18171556SrgrimesRESET {
18181556Srgrimes	tokpushback = 0;
18191556Srgrimes	checkkwd = 0;
18201556Srgrimes}
18211556Srgrimes#endif
18221556Srgrimes
18231556Srgrimes/*
18241556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs
18251556Srgrimes * or backquotes).
18261556Srgrimes */
18271556Srgrimes
1828213811Sobrienstatic int
182990111Simpnoexpand(char *text)
183090111Simp{
183125230Ssteve	char *p;
183225230Ssteve	char c;
18331556Srgrimes
18341556Srgrimes	p = text;
18351556Srgrimes	while ((c = *p++) != '\0') {
183639137Stegge		if ( c == CTLQUOTEMARK)
183739137Stegge			continue;
18381556Srgrimes		if (c == CTLESC)
18391556Srgrimes			p++;
184083675Stegge		else if (BASESYNTAX[(int)c] == CCTL)
18411556Srgrimes			return 0;
18421556Srgrimes	}
18431556Srgrimes	return 1;
18441556Srgrimes}
18451556Srgrimes
18461556Srgrimes
18471556Srgrimes/*
18481556Srgrimes * Return true if the argument is a legal variable name (a letter or
18491556Srgrimes * underscore followed by zero or more letters, underscores, and digits).
18501556Srgrimes */
18511556Srgrimes
18521556Srgrimesint
1853200956Sjillesgoodname(const char *name)
185490111Simp{
1855200956Sjilles	const char *p;
18561556Srgrimes
18571556Srgrimes	p = name;
18581556Srgrimes	if (! is_name(*p))
18591556Srgrimes		return 0;
18601556Srgrimes	while (*++p) {
18611556Srgrimes		if (! is_in_name(*p))
18621556Srgrimes			return 0;
18631556Srgrimes	}
18641556Srgrimes	return 1;
18651556Srgrimes}
18661556Srgrimes
18671556Srgrimes
1868222165Sjillesint
1869222165Sjillesisassignment(const char *p)
1870222165Sjilles{
1871222165Sjilles	if (!is_name(*p))
1872222165Sjilles		return 0;
1873222165Sjilles	p++;
1874222165Sjilles	for (;;) {
1875222165Sjilles		if (*p == '=')
1876222165Sjilles			return 1;
1877222165Sjilles		else if (!is_in_name(*p))
1878222165Sjilles			return 0;
1879222165Sjilles		p++;
1880222165Sjilles	}
1881222165Sjilles}
1882222165Sjilles
1883222165Sjilles
18841556Srgrimes/*
18851556Srgrimes * Called when an unexpected token is read during the parse.  The argument
18861556Srgrimes * is the token that is expected, or -1 if more than one type of token can
18871556Srgrimes * occur at this point.
18881556Srgrimes */
18891556Srgrimes
1890213811Sobrienstatic void
189190111Simpsynexpect(int token)
189217987Speter{
18931556Srgrimes	char msg[64];
18941556Srgrimes
18951556Srgrimes	if (token >= 0) {
18961556Srgrimes		fmtstr(msg, 64, "%s unexpected (expecting %s)",
18971556Srgrimes			tokname[lasttoken], tokname[token]);
18981556Srgrimes	} else {
18991556Srgrimes		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
19001556Srgrimes	}
19011556Srgrimes	synerror(msg);
19021556Srgrimes}
19031556Srgrimes
19041556Srgrimes
1905213811Sobrienstatic void
1906201053Sjillessynerror(const char *msg)
190790111Simp{
19081556Srgrimes	if (commandname)
1909201366Sjilles		outfmt(out2, "%s: %d: ", commandname, startlinno);
1910201366Sjilles	outfmt(out2, "Syntax error: %s\n", msg);
19111556Srgrimes	error((char *)NULL);
19121556Srgrimes}
19131556Srgrimes
1914213811Sobrienstatic void
191590111Simpsetprompt(int which)
191690111Simp{
19171556Srgrimes	whichprompt = which;
19181556Srgrimes
191917987Speter#ifndef NO_HISTORY
19201556Srgrimes	if (!el)
192117987Speter#endif
1922199629Sjilles	{
19231556Srgrimes		out2str(getprompt(NULL));
1924199629Sjilles		flushout(out2);
1925199629Sjilles	}
19261556Srgrimes}
19271556Srgrimes
19281556Srgrimes/*
19291556Srgrimes * called by editline -- any expansions to the prompt
19301556Srgrimes *    should be added here.
19311556Srgrimes */
19321556Srgrimeschar *
193390111Simpgetprompt(void *unused __unused)
193425905Ssteve{
1935142845Sobrien	static char ps[PROMPTLEN];
1936142845Sobrien	char *fmt;
1937209653Sjilles	const char *pwd;
1938209653Sjilles	int i, trim;
1939214538Sjilles	static char internal_error[] = "??";
1940142845Sobrien
1941142845Sobrien	/*
1942142845Sobrien	 * Select prompt format.
1943142845Sobrien	 */
19441556Srgrimes	switch (whichprompt) {
19451556Srgrimes	case 0:
1946201053Sjilles		fmt = nullstr;
1947142845Sobrien		break;
19481556Srgrimes	case 1:
1949142845Sobrien		fmt = ps1val();
1950142845Sobrien		break;
19511556Srgrimes	case 2:
1952142845Sobrien		fmt = ps2val();
1953142845Sobrien		break;
19541556Srgrimes	default:
1955201053Sjilles		return internal_error;
19561556Srgrimes	}
1957142845Sobrien
1958142845Sobrien	/*
1959142845Sobrien	 * Format prompt string.
1960142845Sobrien	 */
1961142845Sobrien	for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1962142845Sobrien		if (*fmt == '\\')
1963142845Sobrien			switch (*++fmt) {
1964142845Sobrien
1965142845Sobrien				/*
1966142845Sobrien				 * Hostname.
1967142845Sobrien				 *
1968142845Sobrien				 * \h specifies just the local hostname,
1969142845Sobrien				 * \H specifies fully-qualified hostname.
1970142845Sobrien				 */
1971142845Sobrien			case 'h':
1972142845Sobrien			case 'H':
1973149024Sstefanf				ps[i] = '\0';
1974142845Sobrien				gethostname(&ps[i], PROMPTLEN - i);
1975142845Sobrien				/* Skip to end of hostname. */
1976142845Sobrien				trim = (*fmt == 'h') ? '.' : '\0';
1977142845Sobrien				while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1978142845Sobrien					i++;
1979142845Sobrien				break;
1980142845Sobrien
1981142845Sobrien				/*
1982142845Sobrien				 * Working directory.
1983142845Sobrien				 *
1984142845Sobrien				 * \W specifies just the final component,
1985142845Sobrien				 * \w specifies the entire path.
1986142845Sobrien				 */
1987142845Sobrien			case 'W':
1988142845Sobrien			case 'w':
1989209653Sjilles				pwd = lookupvar("PWD");
1990209653Sjilles				if (pwd == NULL)
1991209653Sjilles					pwd = "?";
1992209653Sjilles				if (*fmt == 'W' &&
1993209653Sjilles				    *pwd == '/' && pwd[1] != '\0')
1994209653Sjilles					strlcpy(&ps[i], strrchr(pwd, '/') + 1,
1995209653Sjilles					    PROMPTLEN - i);
1996209653Sjilles				else
1997209653Sjilles					strlcpy(&ps[i], pwd, PROMPTLEN - i);
1998142845Sobrien				/* Skip to end of path. */
1999142845Sobrien				while (ps[i + 1] != '\0')
2000142845Sobrien					i++;
2001142845Sobrien				break;
2002142845Sobrien
2003142845Sobrien				/*
2004142845Sobrien				 * Superuser status.
2005142845Sobrien				 *
2006142845Sobrien				 * '$' for normal users, '#' for root.
2007142845Sobrien				 */
2008142845Sobrien			case '$':
2009142845Sobrien				ps[i] = (geteuid() != 0) ? '$' : '#';
2010142845Sobrien				break;
2011142845Sobrien
2012142845Sobrien				/*
2013142845Sobrien				 * A literal \.
2014142845Sobrien				 */
2015142845Sobrien			case '\\':
2016142845Sobrien				ps[i] = '\\';
2017142845Sobrien				break;
2018142845Sobrien
2019142845Sobrien				/*
2020142845Sobrien				 * Emit unrecognized formats verbatim.
2021142845Sobrien				 */
2022142845Sobrien			default:
2023142845Sobrien				ps[i++] = '\\';
2024142845Sobrien				ps[i] = *fmt;
2025142845Sobrien				break;
2026142845Sobrien			}
2027142845Sobrien		else
2028142845Sobrien			ps[i] = *fmt;
2029142845Sobrien	ps[i] = '\0';
2030142845Sobrien	return (ps);
20311556Srgrimes}
2032222907Sjilles
2033222907Sjilles
2034222907Sjillesconst char *
2035222907Sjillesexpandstr(char *ps)
2036222907Sjilles{
2037222907Sjilles	union node n;
2038222907Sjilles	struct jmploc jmploc;
2039222907Sjilles	struct jmploc *const savehandler = handler;
2040222907Sjilles	const int saveprompt = doprompt;
2041222907Sjilles	struct parsefile *const savetopfile = getcurrentfile();
2042222907Sjilles	struct parser_temp *const saveparser_temp = parser_temp;
2043222907Sjilles	const char *result = NULL;
2044222907Sjilles
2045222907Sjilles	if (!setjmp(jmploc.loc)) {
2046222907Sjilles		handler = &jmploc;
2047222907Sjilles		parser_temp = NULL;
2048222907Sjilles		setinputstring(ps, 1);
2049222907Sjilles		doprompt = 0;
2050222907Sjilles		readtoken1(pgetc(), DQSYNTAX, "\n\n", 0);
2051222907Sjilles		if (backquotelist != NULL)
2052222907Sjilles			error("Command substitution not allowed here");
2053222907Sjilles
2054222907Sjilles		n.narg.type = NARG;
2055222907Sjilles		n.narg.next = NULL;
2056222907Sjilles		n.narg.text = wordtext;
2057222907Sjilles		n.narg.backquote = backquotelist;
2058222907Sjilles
2059222907Sjilles		expandarg(&n, NULL, 0);
2060222907Sjilles		result = stackblock();
2061222907Sjilles		INTOFF;
2062222907Sjilles	}
2063222907Sjilles	handler = savehandler;
2064222907Sjilles	doprompt = saveprompt;
2065222907Sjilles	popfilesupto(savetopfile);
2066222907Sjilles	if (parser_temp != saveparser_temp) {
2067222907Sjilles		parser_temp_free_all();
2068222907Sjilles		parser_temp = saveparser_temp;
2069222907Sjilles	}
2070222907Sjilles	if (result != NULL) {
2071222907Sjilles		INTON;
2072222907Sjilles	} else if (exception == EXINT)
2073222907Sjilles		raise(SIGINT);
2074222907Sjilles	return result;
2075222907Sjilles}
2076