parser.c revision 254335
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 254335 2013-08-14 19:34:13Z 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 */
99253658Sjillesint tokpushback;		/* last token pushed back */
100213760Sobrienstatic char *wordtext;		/* text of last word returned by readtoken */
101253659Sjillesstatic int checkkwd;
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);
122248980Sjillesstatic int readtoken1(int, const 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) {
243245382Sjilles			if (n2 != NULL && n2->type == NPIPE) {
244223282Sjilles				n2->npipe.backgnd = 1;
245245382Sjilles			} else if (n2 != NULL && 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;
289245381Sjilles			if (!nlflag && (erflag ? peektoken() == TEOF :
290245381Sjilles			    tokendlist[peektoken()]))
291214599Sjilles				return ntop;
2921556Srgrimes			break;
2931556Srgrimes		case TEOF:
2941556Srgrimes			if (heredoclist)
2951556Srgrimes				parseheredoc();
2961556Srgrimes			else
2971556Srgrimes				pungetc();		/* push back EOF on input */
298214599Sjilles			return ntop;
2991556Srgrimes		default:
300214525Sjilles			if (nlflag || erflag)
3011556Srgrimes				synexpect(-1);
3021556Srgrimes			tokpushback++;
303214599Sjilles			return ntop;
3041556Srgrimes		}
3051556Srgrimes	}
3061556Srgrimes}
3071556Srgrimes
3081556Srgrimes
3091556Srgrimes
310213811Sobrienstatic union node *
31190111Simpandor(void)
31290111Simp{
3131556Srgrimes	union node *n1, *n2, *n3;
3141556Srgrimes	int t;
3151556Srgrimes
3161556Srgrimes	n1 = pipeline();
3171556Srgrimes	for (;;) {
3181556Srgrimes		if ((t = readtoken()) == TAND) {
3191556Srgrimes			t = NAND;
3201556Srgrimes		} else if (t == TOR) {
3211556Srgrimes			t = NOR;
3221556Srgrimes		} else {
3231556Srgrimes			tokpushback++;
3241556Srgrimes			return n1;
3251556Srgrimes		}
3261556Srgrimes		n2 = pipeline();
3271556Srgrimes		n3 = (union node *)stalloc(sizeof (struct nbinary));
3281556Srgrimes		n3->type = t;
3291556Srgrimes		n3->nbinary.ch1 = n1;
3301556Srgrimes		n3->nbinary.ch2 = n2;
3311556Srgrimes		n1 = n3;
3321556Srgrimes	}
3331556Srgrimes}
3341556Srgrimes
3351556Srgrimes
3361556Srgrimes
337213811Sobrienstatic union node *
33890111Simppipeline(void)
33990111Simp{
34075336Sbrian	union node *n1, *n2, *pipenode;
3411556Srgrimes	struct nodelist *lp, *prev;
342214281Sjilles	int negate, t;
3431556Srgrimes
34475336Sbrian	negate = 0;
345214709Sjilles	checkkwd = CHKNL | CHKKWD | CHKALIAS;
3461556Srgrimes	TRACE(("pipeline: entered\n"));
34775336Sbrian	while (readtoken() == TNOT)
34875336Sbrian		negate = !negate;
34975336Sbrian	tokpushback++;
3501556Srgrimes	n1 = command();
3511556Srgrimes	if (readtoken() == TPIPE) {
3521556Srgrimes		pipenode = (union node *)stalloc(sizeof (struct npipe));
3531556Srgrimes		pipenode->type = NPIPE;
3541556Srgrimes		pipenode->npipe.backgnd = 0;
3551556Srgrimes		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
3561556Srgrimes		pipenode->npipe.cmdlist = lp;
3571556Srgrimes		lp->n = n1;
3581556Srgrimes		do {
3591556Srgrimes			prev = lp;
3601556Srgrimes			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
361214709Sjilles			checkkwd = CHKNL | CHKKWD | CHKALIAS;
362214281Sjilles			t = readtoken();
363214281Sjilles			tokpushback++;
364214281Sjilles			if (t == TNOT)
365214281Sjilles				lp->n = pipeline();
366214281Sjilles			else
367214281Sjilles				lp->n = command();
3681556Srgrimes			prev->next = lp;
3691556Srgrimes		} while (readtoken() == TPIPE);
3701556Srgrimes		lp->next = NULL;
3711556Srgrimes		n1 = pipenode;
3721556Srgrimes	}
3731556Srgrimes	tokpushback++;
37475336Sbrian	if (negate) {
37575336Sbrian		n2 = (union node *)stalloc(sizeof (struct nnot));
37675336Sbrian		n2->type = NNOT;
37775336Sbrian		n2->nnot.com = n1;
37875336Sbrian		return n2;
37975336Sbrian	} else
38075336Sbrian		return n1;
3811556Srgrimes}
3821556Srgrimes
3831556Srgrimes
3841556Srgrimes
385213811Sobrienstatic union node *
38690111Simpcommand(void)
38790111Simp{
3881556Srgrimes	union node *n1, *n2;
3891556Srgrimes	union node *ap, **app;
3901556Srgrimes	union node *cp, **cpp;
3911556Srgrimes	union node *redir, **rpp;
392214281Sjilles	int t;
393218325Sjilles	int is_subshell;
3941556Srgrimes
395214709Sjilles	checkkwd = CHKNL | CHKKWD | CHKALIAS;
396218325Sjilles	is_subshell = 0;
39717987Speter	redir = NULL;
39817987Speter	n1 = NULL;
3991556Srgrimes	rpp = &redir;
40020425Ssteve
4011556Srgrimes	/* Check for redirection which may precede command */
4021556Srgrimes	while (readtoken() == TREDIR) {
4031556Srgrimes		*rpp = n2 = redirnode;
4041556Srgrimes		rpp = &n2->nfile.next;
4051556Srgrimes		parsefname();
4061556Srgrimes	}
4071556Srgrimes	tokpushback++;
4081556Srgrimes
4091556Srgrimes	switch (readtoken()) {
4101556Srgrimes	case TIF:
4111556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nif));
4121556Srgrimes		n1->type = NIF;
413214525Sjilles		if ((n1->nif.test = list(0, 0)) == NULL)
414104554Stjr			synexpect(-1);
4151556Srgrimes		if (readtoken() != TTHEN)
4161556Srgrimes			synexpect(TTHEN);
417214525Sjilles		n1->nif.ifpart = list(0, 0);
4181556Srgrimes		n2 = n1;
4191556Srgrimes		while (readtoken() == TELIF) {
4201556Srgrimes			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
4211556Srgrimes			n2 = n2->nif.elsepart;
4221556Srgrimes			n2->type = NIF;
423214525Sjilles			if ((n2->nif.test = list(0, 0)) == NULL)
424104554Stjr				synexpect(-1);
4251556Srgrimes			if (readtoken() != TTHEN)
4261556Srgrimes				synexpect(TTHEN);
427214525Sjilles			n2->nif.ifpart = list(0, 0);
4281556Srgrimes		}
4291556Srgrimes		if (lasttoken == TELSE)
430214525Sjilles			n2->nif.elsepart = list(0, 0);
4311556Srgrimes		else {
4321556Srgrimes			n2->nif.elsepart = NULL;
4331556Srgrimes			tokpushback++;
4341556Srgrimes		}
4351556Srgrimes		if (readtoken() != TFI)
4361556Srgrimes			synexpect(TFI);
437214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
4381556Srgrimes		break;
4391556Srgrimes	case TWHILE:
4401556Srgrimes	case TUNTIL: {
4411556Srgrimes		int got;
4421556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nbinary));
4431556Srgrimes		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
444214525Sjilles		if ((n1->nbinary.ch1 = list(0, 0)) == NULL)
445104554Stjr			synexpect(-1);
4461556Srgrimes		if ((got=readtoken()) != TDO) {
4471556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
4481556Srgrimes			synexpect(TDO);
4491556Srgrimes		}
450214525Sjilles		n1->nbinary.ch2 = list(0, 0);
4511556Srgrimes		if (readtoken() != TDONE)
4521556Srgrimes			synexpect(TDONE);
453214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
4541556Srgrimes		break;
4551556Srgrimes	}
4561556Srgrimes	case TFOR:
4571556Srgrimes		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
4581556Srgrimes			synerror("Bad for loop variable");
4591556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nfor));
4601556Srgrimes		n1->type = NFOR;
4611556Srgrimes		n1->nfor.var = wordtext;
462199282Sjilles		while (readtoken() == TNL)
463199282Sjilles			;
464199282Sjilles		if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
4651556Srgrimes			app = &ap;
4661556Srgrimes			while (readtoken() == TWORD) {
4671556Srgrimes				n2 = (union node *)stalloc(sizeof (struct narg));
4681556Srgrimes				n2->type = NARG;
4691556Srgrimes				n2->narg.text = wordtext;
4701556Srgrimes				n2->narg.backquote = backquotelist;
4711556Srgrimes				*app = n2;
4721556Srgrimes				app = &n2->narg.next;
4731556Srgrimes			}
4741556Srgrimes			*app = NULL;
4751556Srgrimes			n1->nfor.args = ap;
4761556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4771556Srgrimes				synexpect(-1);
4781556Srgrimes		} else {
479149096Sstefanf			static char argvars[5] = {
480149096Sstefanf				CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
481149096Sstefanf			};
4821556Srgrimes			n2 = (union node *)stalloc(sizeof (struct narg));
4831556Srgrimes			n2->type = NARG;
484149096Sstefanf			n2->narg.text = argvars;
4851556Srgrimes			n2->narg.backquote = NULL;
4861556Srgrimes			n2->narg.next = NULL;
4871556Srgrimes			n1->nfor.args = n2;
4881556Srgrimes			/*
4891556Srgrimes			 * Newline or semicolon here is optional (but note
4901556Srgrimes			 * that the original Bourne shell only allowed NL).
4911556Srgrimes			 */
4921556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4931556Srgrimes				tokpushback++;
4941556Srgrimes		}
495214709Sjilles		checkkwd = CHKNL | CHKKWD | CHKALIAS;
4961556Srgrimes		if ((t = readtoken()) == TDO)
4971556Srgrimes			t = TDONE;
4981556Srgrimes		else if (t == TBEGIN)
4991556Srgrimes			t = TEND;
5001556Srgrimes		else
5011556Srgrimes			synexpect(-1);
502214525Sjilles		n1->nfor.body = list(0, 0);
5031556Srgrimes		if (readtoken() != t)
5041556Srgrimes			synexpect(t);
505214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
5061556Srgrimes		break;
5071556Srgrimes	case TCASE:
5081556Srgrimes		n1 = (union node *)stalloc(sizeof (struct ncase));
5091556Srgrimes		n1->type = NCASE;
5101556Srgrimes		if (readtoken() != TWORD)
5111556Srgrimes			synexpect(TWORD);
5121556Srgrimes		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
5131556Srgrimes		n2->type = NARG;
5141556Srgrimes		n2->narg.text = wordtext;
5151556Srgrimes		n2->narg.backquote = backquotelist;
5161556Srgrimes		n2->narg.next = NULL;
5171556Srgrimes		while (readtoken() == TNL);
5181556Srgrimes		if (lasttoken != TWORD || ! equal(wordtext, "in"))
5191556Srgrimes			synerror("expecting \"in\"");
5201556Srgrimes		cpp = &n1->ncase.cases;
521214709Sjilles		checkkwd = CHKNL | CHKKWD, readtoken();
522104202Stjr		while (lasttoken != TESAC) {
5231556Srgrimes			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
5241556Srgrimes			cp->type = NCLIST;
5251556Srgrimes			app = &cp->nclist.pattern;
526104207Stjr			if (lasttoken == TLP)
527104207Stjr				readtoken();
5281556Srgrimes			for (;;) {
5291556Srgrimes				*app = ap = (union node *)stalloc(sizeof (struct narg));
5301556Srgrimes				ap->type = NARG;
5311556Srgrimes				ap->narg.text = wordtext;
5321556Srgrimes				ap->narg.backquote = backquotelist;
533214709Sjilles				checkkwd = CHKNL | CHKKWD;
534214709Sjilles				if (readtoken() != TPIPE)
5351556Srgrimes					break;
5361556Srgrimes				app = &ap->narg.next;
5372760Ssef				readtoken();
5381556Srgrimes			}
5391556Srgrimes			ap->narg.next = NULL;
5401556Srgrimes			if (lasttoken != TRP)
541214709Sjilles				synexpect(TRP);
542214525Sjilles			cp->nclist.body = list(0, 0);
5432760Ssef
544214709Sjilles			checkkwd = CHKNL | CHKKWD | CHKALIAS;
5452760Ssef			if ((t = readtoken()) != TESAC) {
546223186Sjilles				if (t == TENDCASE)
547223186Sjilles					;
548223186Sjilles				else if (t == TFALLTHRU)
549223186Sjilles					cp->type = NCLISTFALLTHRU;
550223186Sjilles				else
551214709Sjilles					synexpect(TENDCASE);
552223186Sjilles				checkkwd = CHKNL | CHKKWD, readtoken();
5532760Ssef			}
5541556Srgrimes			cpp = &cp->nclist.next;
555104202Stjr		}
5561556Srgrimes		*cpp = NULL;
557214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
5581556Srgrimes		break;
5591556Srgrimes	case TLP:
5601556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nredir));
5611556Srgrimes		n1->type = NSUBSHELL;
562214525Sjilles		n1->nredir.n = list(0, 0);
5631556Srgrimes		n1->nredir.redirect = NULL;
5641556Srgrimes		if (readtoken() != TRP)
5651556Srgrimes			synexpect(TRP);
566214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
567218325Sjilles		is_subshell = 1;
5681556Srgrimes		break;
5691556Srgrimes	case TBEGIN:
570214525Sjilles		n1 = list(0, 0);
5711556Srgrimes		if (readtoken() != TEND)
5721556Srgrimes			synexpect(TEND);
573214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
5741556Srgrimes		break;
5751556Srgrimes	/* Handle an empty command like other simple commands.  */
576210221Sjilles	case TBACKGND:
57717987Speter	case TSEMI:
578101662Stjr	case TAND:
579101662Stjr	case TOR:
580254335Sjilles	case TPIPE:
581254335Sjilles	case TENDCASE:
582254335Sjilles	case TFALLTHRU:
58317987Speter		/*
58417987Speter		 * An empty command before a ; doesn't make much sense, and
58517987Speter		 * should certainly be disallowed in the case of `if ;'.
58617987Speter		 */
58717987Speter		if (!redir)
58817987Speter			synexpect(-1);
5891556Srgrimes	case TNL:
59010399Sjoerg	case TEOF:
5911556Srgrimes	case TWORD:
59217987Speter	case TRP:
5931556Srgrimes		tokpushback++;
59475160Sbrian		n1 = simplecmd(rpp, redir);
595214281Sjilles		return n1;
5961556Srgrimes	default:
5971556Srgrimes		synexpect(-1);
5981556Srgrimes	}
5991556Srgrimes
6001556Srgrimes	/* Now check for redirection which may follow command */
6011556Srgrimes	while (readtoken() == TREDIR) {
6021556Srgrimes		*rpp = n2 = redirnode;
6031556Srgrimes		rpp = &n2->nfile.next;
6041556Srgrimes		parsefname();
6051556Srgrimes	}
6061556Srgrimes	tokpushback++;
6071556Srgrimes	*rpp = NULL;
6081556Srgrimes	if (redir) {
609218325Sjilles		if (!is_subshell) {
6101556Srgrimes			n2 = (union node *)stalloc(sizeof (struct nredir));
6111556Srgrimes			n2->type = NREDIR;
6121556Srgrimes			n2->nredir.n = n1;
6131556Srgrimes			n1 = n2;
6141556Srgrimes		}
6151556Srgrimes		n1->nredir.redirect = redir;
6161556Srgrimes	}
61775160Sbrian
618214281Sjilles	return n1;
6191556Srgrimes}
6201556Srgrimes
6211556Srgrimes
622213811Sobrienstatic union node *
62390111Simpsimplecmd(union node **rpp, union node *redir)
62490111Simp{
6251556Srgrimes	union node *args, **app;
6261556Srgrimes	union node **orig_rpp = rpp;
627210087Sjilles	union node *n = NULL;
628214304Sjilles	int special;
629222165Sjilles	int savecheckkwd;
6301556Srgrimes
6311556Srgrimes	/* If we don't have any redirections already, then we must reset */
6321556Srgrimes	/* rpp to be the address of the local redir variable.  */
6331556Srgrimes	if (redir == 0)
6341556Srgrimes		rpp = &redir;
6351556Srgrimes
6361556Srgrimes	args = NULL;
6371556Srgrimes	app = &args;
6388855Srgrimes	/*
6391556Srgrimes	 * We save the incoming value, because we need this for shell
6401556Srgrimes	 * functions.  There can not be a redirect or an argument between
6418855Srgrimes	 * the function name and the open parenthesis.
6421556Srgrimes	 */
6431556Srgrimes	orig_rpp = rpp;
6441556Srgrimes
645222165Sjilles	savecheckkwd = CHKALIAS;
646222165Sjilles
6471556Srgrimes	for (;;) {
648222165Sjilles		checkkwd = savecheckkwd;
6491556Srgrimes		if (readtoken() == TWORD) {
6501556Srgrimes			n = (union node *)stalloc(sizeof (struct narg));
6511556Srgrimes			n->type = NARG;
6521556Srgrimes			n->narg.text = wordtext;
6531556Srgrimes			n->narg.backquote = backquotelist;
6541556Srgrimes			*app = n;
6551556Srgrimes			app = &n->narg.next;
656222165Sjilles			if (savecheckkwd != 0 && !isassignment(wordtext))
657222165Sjilles				savecheckkwd = 0;
6581556Srgrimes		} else if (lasttoken == TREDIR) {
6591556Srgrimes			*rpp = n = redirnode;
6601556Srgrimes			rpp = &n->nfile.next;
6611556Srgrimes			parsefname();	/* read name of redirection file */
6621556Srgrimes		} else if (lasttoken == TLP && app == &args->narg.next
6631556Srgrimes					    && rpp == orig_rpp) {
6641556Srgrimes			/* We have a function */
6651556Srgrimes			if (readtoken() != TRP)
6661556Srgrimes				synexpect(TRP);
667179022Sstefanf			funclinno = plinno;
668214291Sjilles			/*
669214291Sjilles			 * - Require plain text.
670214291Sjilles			 * - Functions with '/' cannot be called.
671214534Sjilles			 * - Reject name=().
672214534Sjilles			 * - Reject ksh extended glob patterns.
673214291Sjilles			 */
674214291Sjilles			if (!noexpand(n->narg.text) || quoteflag ||
675214534Sjilles			    strchr(n->narg.text, '/') ||
676214534Sjilles			    strchr("!%*+-=?@}~",
677214534Sjilles				n->narg.text[strlen(n->narg.text) - 1]))
6781556Srgrimes				synerror("Bad function name");
679214291Sjilles			rmescapes(n->narg.text);
680214304Sjilles			if (find_builtin(n->narg.text, &special) >= 0 &&
681214304Sjilles			    special)
682214304Sjilles				synerror("Cannot override a special builtin with a function");
6831556Srgrimes			n->type = NDEFUN;
6841556Srgrimes			n->narg.next = command();
685179022Sstefanf			funclinno = 0;
686210087Sjilles			return n;
6871556Srgrimes		} else {
6881556Srgrimes			tokpushback++;
6891556Srgrimes			break;
6901556Srgrimes		}
6911556Srgrimes	}
6921556Srgrimes	*app = NULL;
6931556Srgrimes	*rpp = NULL;
6941556Srgrimes	n = (union node *)stalloc(sizeof (struct ncmd));
6951556Srgrimes	n->type = NCMD;
6961556Srgrimes	n->ncmd.args = args;
6971556Srgrimes	n->ncmd.redirect = redir;
698210087Sjilles	return n;
6991556Srgrimes}
7001556Srgrimes
701213811Sobrienstatic union node *
70290111Simpmakename(void)
70390111Simp{
70417987Speter	union node *n;
7051556Srgrimes
70617987Speter	n = (union node *)stalloc(sizeof (struct narg));
70717987Speter	n->type = NARG;
70817987Speter	n->narg.next = NULL;
70917987Speter	n->narg.text = wordtext;
71017987Speter	n->narg.backquote = backquotelist;
71117987Speter	return n;
71217987Speter}
71317987Speter
714213760Sobrienvoid
715213760Sobrienfixredir(union node *n, const char *text, int err)
71690111Simp{
71717987Speter	TRACE(("Fix redir %s %d\n", text, err));
71817987Speter	if (!err)
71917987Speter		n->ndup.vname = NULL;
72017987Speter
72117987Speter	if (is_digit(text[0]) && text[1] == '\0')
72217987Speter		n->ndup.dupfd = digit_val(text[0]);
72317987Speter	else if (text[0] == '-' && text[1] == '\0')
72417987Speter		n->ndup.dupfd = -1;
72517987Speter	else {
72620425Ssteve
72717987Speter		if (err)
72817987Speter			synerror("Bad fd number");
72917987Speter		else
73017987Speter			n->ndup.vname = makename();
73117987Speter	}
73217987Speter}
73317987Speter
73417987Speter
735213811Sobrienstatic void
73690111Simpparsefname(void)
73790111Simp{
7381556Srgrimes	union node *n = redirnode;
7391556Srgrimes
7401556Srgrimes	if (readtoken() != TWORD)
7411556Srgrimes		synexpect(-1);
7421556Srgrimes	if (n->type == NHERE) {
7431556Srgrimes		struct heredoc *here = heredoc;
7441556Srgrimes		struct heredoc *p;
7451556Srgrimes		int i;
7461556Srgrimes
7471556Srgrimes		if (quoteflag == 0)
7481556Srgrimes			n->type = NXHERE;
7491556Srgrimes		TRACE(("Here document %d\n", n->type));
7501556Srgrimes		if (here->striptabs) {
7511556Srgrimes			while (*wordtext == '\t')
7521556Srgrimes				wordtext++;
7531556Srgrimes		}
7541556Srgrimes		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
7551556Srgrimes			synerror("Illegal eof marker for << redirection");
7561556Srgrimes		rmescapes(wordtext);
7571556Srgrimes		here->eofmark = wordtext;
7581556Srgrimes		here->next = NULL;
7591556Srgrimes		if (heredoclist == NULL)
7601556Srgrimes			heredoclist = here;
7611556Srgrimes		else {
7621556Srgrimes			for (p = heredoclist ; p->next ; p = p->next);
7631556Srgrimes			p->next = here;
7641556Srgrimes		}
7651556Srgrimes	} else if (n->type == NTOFD || n->type == NFROMFD) {
76617987Speter		fixredir(n, wordtext, 0);
7671556Srgrimes	} else {
76817987Speter		n->nfile.fname = makename();
7691556Srgrimes	}
7701556Srgrimes}
7711556Srgrimes
7721556Srgrimes
7731556Srgrimes/*
7741556Srgrimes * Input any here documents.
7751556Srgrimes */
7761556Srgrimes
777213811Sobrienstatic void
77890111Simpparseheredoc(void)
77990111Simp{
7801556Srgrimes	struct heredoc *here;
7811556Srgrimes	union node *n;
7821556Srgrimes
7831556Srgrimes	while (heredoclist) {
7841556Srgrimes		here = heredoclist;
7851556Srgrimes		heredoclist = here->next;
7861556Srgrimes		if (needprompt) {
7871556Srgrimes			setprompt(2);
7881556Srgrimes			needprompt = 0;
7891556Srgrimes		}
7901556Srgrimes		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
7911556Srgrimes				here->eofmark, here->striptabs);
7921556Srgrimes		n = (union node *)stalloc(sizeof (struct narg));
7931556Srgrimes		n->narg.type = NARG;
7941556Srgrimes		n->narg.next = NULL;
7951556Srgrimes		n->narg.text = wordtext;
7961556Srgrimes		n->narg.backquote = backquotelist;
7971556Srgrimes		here->here->nhere.doc = n;
7981556Srgrimes	}
7991556Srgrimes}
8001556Srgrimes
801213811Sobrienstatic int
80290111Simppeektoken(void)
80390111Simp{
8041556Srgrimes	int t;
8051556Srgrimes
8061556Srgrimes	t = readtoken();
8071556Srgrimes	tokpushback++;
8081556Srgrimes	return (t);
8091556Srgrimes}
8101556Srgrimes
811213811Sobrienstatic int
81290111Simpreadtoken(void)
81390111Simp{
8141556Srgrimes	int t;
8151556Srgrimes	struct alias *ap;
8161556Srgrimes#ifdef DEBUG
8171556Srgrimes	int alreadyseen = tokpushback;
8181556Srgrimes#endif
8198855Srgrimes
8201556Srgrimes	top:
8211556Srgrimes	t = xxreadtoken();
8221556Srgrimes
823214709Sjilles	/*
824214709Sjilles	 * eat newlines
825214709Sjilles	 */
826214709Sjilles	if (checkkwd & CHKNL) {
827214709Sjilles		while (t == TNL) {
828214709Sjilles			parseheredoc();
829214709Sjilles			t = xxreadtoken();
830214709Sjilles		}
831214709Sjilles	}
8321556Srgrimes
833214709Sjilles	/*
834214709Sjilles	 * check for keywords and aliases
835214709Sjilles	 */
836214709Sjilles	if (t == TWORD && !quoteflag)
837214709Sjilles	{
838214709Sjilles		const char * const *pp;
839214709Sjilles
840214709Sjilles		if (checkkwd & CHKKWD)
84198463Sjmallett			for (pp = parsekwd; *pp; pp++) {
84220425Ssteve				if (**pp == *wordtext && equal(*pp, wordtext))
84317987Speter				{
8441556Srgrimes					lasttoken = t = pp - parsekwd + KWDOFFSET;
8451556Srgrimes					TRACE(("keyword %s recognized\n", tokname[t]));
8461556Srgrimes					goto out;
8471556Srgrimes				}
8481556Srgrimes			}
849214709Sjilles		if (checkkwd & CHKALIAS &&
850214709Sjilles		    (ap = lookupalias(wordtext, 1)) != NULL) {
851214709Sjilles			pushstring(ap->val, strlen(ap->val), ap);
852214709Sjilles			goto top;
8531556Srgrimes		}
854214709Sjilles	}
8551556Srgrimesout:
856214709Sjilles	if (t != TNOT)
857214709Sjilles		checkkwd = 0;
858214709Sjilles
8591556Srgrimes#ifdef DEBUG
8601556Srgrimes	if (!alreadyseen)
8611556Srgrimes	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8621556Srgrimes	else
8631556Srgrimes	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8641556Srgrimes#endif
8651556Srgrimes	return (t);
8661556Srgrimes}
8671556Srgrimes
8681556Srgrimes
8691556Srgrimes/*
8701556Srgrimes * Read the next input token.
8711556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in
8721556Srgrimes *	backquotes.  We set quoteflag to true if any part of the word was
8731556Srgrimes *	quoted.
8741556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing
8751556Srgrimes *	the redirection.
8761556Srgrimes * In all cases, the variable startlinno is set to the number of the line
8771556Srgrimes *	on which the token starts.
8781556Srgrimes *
8791556Srgrimes * [Change comment:  here documents and internal procedures]
8801556Srgrimes * [Readtoken shouldn't have any arguments.  Perhaps we should make the
8811556Srgrimes *  word parsing code into a separate routine.  In this case, readtoken
8821556Srgrimes *  doesn't need to have any internal procedures, but parseword does.
8831556Srgrimes *  We could also make parseoperator in essence the main routine, and
8841556Srgrimes *  have parseword (readtoken1?) handle both words and redirection.]
8851556Srgrimes */
8861556Srgrimes
8871556Srgrimes#define RETURN(token)	return lasttoken = token
8881556Srgrimes
889213811Sobrienstatic int
89090111Simpxxreadtoken(void)
89190111Simp{
89225230Ssteve	int c;
8931556Srgrimes
8941556Srgrimes	if (tokpushback) {
8951556Srgrimes		tokpushback = 0;
8961556Srgrimes		return lasttoken;
8971556Srgrimes	}
8981556Srgrimes	if (needprompt) {
8991556Srgrimes		setprompt(2);
9001556Srgrimes		needprompt = 0;
9011556Srgrimes	}
9021556Srgrimes	startlinno = plinno;
9031556Srgrimes	for (;;) {	/* until token or start of word found */
9041556Srgrimes		c = pgetc_macro();
9051556Srgrimes		switch (c) {
9061556Srgrimes		case ' ': case '\t':
9071556Srgrimes			continue;
9081556Srgrimes		case '#':
9091556Srgrimes			while ((c = pgetc()) != '\n' && c != PEOF);
9101556Srgrimes			pungetc();
9111556Srgrimes			continue;
9121556Srgrimes		case '\\':
9131556Srgrimes			if (pgetc() == '\n') {
9141556Srgrimes				startlinno = ++plinno;
9151556Srgrimes				if (doprompt)
9161556Srgrimes					setprompt(2);
9171556Srgrimes				else
9181556Srgrimes					setprompt(0);
9191556Srgrimes				continue;
9201556Srgrimes			}
9211556Srgrimes			pungetc();
9221556Srgrimes			goto breakloop;
9231556Srgrimes		case '\n':
9241556Srgrimes			plinno++;
9251556Srgrimes			needprompt = doprompt;
9261556Srgrimes			RETURN(TNL);
9271556Srgrimes		case PEOF:
9281556Srgrimes			RETURN(TEOF);
9291556Srgrimes		case '&':
9301556Srgrimes			if (pgetc() == '&')
9311556Srgrimes				RETURN(TAND);
9321556Srgrimes			pungetc();
9331556Srgrimes			RETURN(TBACKGND);
9341556Srgrimes		case '|':
9351556Srgrimes			if (pgetc() == '|')
9361556Srgrimes				RETURN(TOR);
9371556Srgrimes			pungetc();
9381556Srgrimes			RETURN(TPIPE);
9391556Srgrimes		case ';':
940223186Sjilles			c = pgetc();
941223186Sjilles			if (c == ';')
9421556Srgrimes				RETURN(TENDCASE);
943223186Sjilles			else if (c == '&')
944223186Sjilles				RETURN(TFALLTHRU);
9451556Srgrimes			pungetc();
9461556Srgrimes			RETURN(TSEMI);
9471556Srgrimes		case '(':
9481556Srgrimes			RETURN(TLP);
9491556Srgrimes		case ')':
9501556Srgrimes			RETURN(TRP);
9511556Srgrimes		default:
9521556Srgrimes			goto breakloop;
9531556Srgrimes		}
9541556Srgrimes	}
9551556Srgrimesbreakloop:
9561556Srgrimes	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
9571556Srgrimes#undef RETURN
9581556Srgrimes}
9591556Srgrimes
9601556Srgrimes
961213811Sobrien#define MAXNEST_static 8
962206145Sjillesstruct tokenstate
963206145Sjilles{
964206145Sjilles	const char *syntax; /* *SYNTAX */
965206145Sjilles	int parenlevel; /* levels of parentheses in arithmetic */
966206145Sjilles	enum tokenstate_category
967206145Sjilles	{
968206145Sjilles		TSTATE_TOP,
969206145Sjilles		TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
970206145Sjilles		TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
971206145Sjilles		TSTATE_ARITH
972206145Sjilles	} category;
973206145Sjilles};
974206145Sjilles
975206145Sjilles
976205130Sjilles/*
977205130Sjilles * Called to parse command substitutions.
978205130Sjilles */
9791556Srgrimes
980213811Sobrienstatic char *
981205130Sjillesparsebackq(char *out, struct nodelist **pbqlist,
982205130Sjilles		int oldstyle, int dblquote, int quoted)
983205130Sjilles{
984205130Sjilles	struct nodelist **nlpp;
985205130Sjilles	union node *n;
986205130Sjilles	char *volatile str;
987205130Sjilles	struct jmploc jmploc;
988205130Sjilles	struct jmploc *const savehandler = handler;
989248980Sjilles	size_t savelen;
990205130Sjilles	int saveprompt;
991205130Sjilles	const int bq_startlinno = plinno;
992205130Sjilles	char *volatile ostr = NULL;
993205130Sjilles	struct parsefile *const savetopfile = getcurrentfile();
994208655Sjilles	struct heredoc *const saveheredoclist = heredoclist;
995208655Sjilles	struct heredoc *here;
996205130Sjilles
997205130Sjilles	str = NULL;
998205130Sjilles	if (setjmp(jmploc.loc)) {
999205130Sjilles		popfilesupto(savetopfile);
1000205130Sjilles		if (str)
1001205130Sjilles			ckfree(str);
1002205130Sjilles		if (ostr)
1003205130Sjilles			ckfree(ostr);
1004208655Sjilles		heredoclist = saveheredoclist;
1005205130Sjilles		handler = savehandler;
1006205130Sjilles		if (exception == EXERROR) {
1007205130Sjilles			startlinno = bq_startlinno;
1008205130Sjilles			synerror("Error in command substitution");
1009205130Sjilles		}
1010205130Sjilles		longjmp(handler->loc, 1);
1011205130Sjilles	}
1012205130Sjilles	INTOFF;
1013205130Sjilles	savelen = out - stackblock();
1014205130Sjilles	if (savelen > 0) {
1015205130Sjilles		str = ckmalloc(savelen);
1016205130Sjilles		memcpy(str, stackblock(), savelen);
1017205130Sjilles	}
1018205130Sjilles	handler = &jmploc;
1019208655Sjilles	heredoclist = NULL;
1020205130Sjilles	INTON;
1021205130Sjilles        if (oldstyle) {
1022205130Sjilles                /* We must read until the closing backquote, giving special
1023205130Sjilles                   treatment to some slashes, and then push the string and
1024205130Sjilles                   reread it as input, interpreting it normally.  */
1025205130Sjilles                char *oout;
1026205130Sjilles                int c;
1027205130Sjilles                int olen;
1028205130Sjilles
1029205130Sjilles
1030205130Sjilles                STARTSTACKSTR(oout);
1031205130Sjilles		for (;;) {
1032205130Sjilles			if (needprompt) {
1033205130Sjilles				setprompt(2);
1034205130Sjilles				needprompt = 0;
1035205130Sjilles			}
1036215783Sjilles			CHECKSTRSPACE(2, oout);
1037205130Sjilles			switch (c = pgetc()) {
1038205130Sjilles			case '`':
1039205130Sjilles				goto done;
1040205130Sjilles
1041205130Sjilles			case '\\':
1042205130Sjilles                                if ((c = pgetc()) == '\n') {
1043205130Sjilles					plinno++;
1044205130Sjilles					if (doprompt)
1045205130Sjilles						setprompt(2);
1046205130Sjilles					else
1047205130Sjilles						setprompt(0);
1048205130Sjilles					/*
1049205130Sjilles					 * If eating a newline, avoid putting
1050205130Sjilles					 * the newline into the new character
1051215783Sjilles					 * stream (via the USTPUTC after the
1052205130Sjilles					 * switch).
1053205130Sjilles					 */
1054205130Sjilles					continue;
1055205130Sjilles				}
1056205130Sjilles                                if (c != '\\' && c != '`' && c != '$'
1057205130Sjilles                                    && (!dblquote || c != '"'))
1058215783Sjilles                                        USTPUTC('\\', oout);
1059205130Sjilles				break;
1060205130Sjilles
1061205130Sjilles			case '\n':
1062205130Sjilles				plinno++;
1063205130Sjilles				needprompt = doprompt;
1064205130Sjilles				break;
1065205130Sjilles
1066205130Sjilles			case PEOF:
1067205130Sjilles			        startlinno = plinno;
1068205130Sjilles				synerror("EOF in backquote substitution");
1069205130Sjilles 				break;
1070205130Sjilles
1071205130Sjilles			default:
1072205130Sjilles				break;
1073205130Sjilles			}
1074215783Sjilles			USTPUTC(c, oout);
1075205130Sjilles                }
1076205130Sjillesdone:
1077215783Sjilles                USTPUTC('\0', oout);
1078205130Sjilles                olen = oout - stackblock();
1079205130Sjilles		INTOFF;
1080205130Sjilles		ostr = ckmalloc(olen);
1081205130Sjilles		memcpy(ostr, stackblock(), olen);
1082205130Sjilles		setinputstring(ostr, 1);
1083205130Sjilles		INTON;
1084205130Sjilles        }
1085205130Sjilles	nlpp = pbqlist;
1086205130Sjilles	while (*nlpp)
1087205130Sjilles		nlpp = &(*nlpp)->next;
1088205130Sjilles	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1089205130Sjilles	(*nlpp)->next = NULL;
1090205130Sjilles
1091205130Sjilles	if (oldstyle) {
1092205130Sjilles		saveprompt = doprompt;
1093205130Sjilles		doprompt = 0;
1094205130Sjilles	}
1095205130Sjilles
1096214525Sjilles	n = list(0, oldstyle);
1097205130Sjilles
1098205130Sjilles	if (oldstyle)
1099205130Sjilles		doprompt = saveprompt;
1100205130Sjilles	else {
1101205130Sjilles		if (readtoken() != TRP)
1102205130Sjilles			synexpect(TRP);
1103205130Sjilles	}
1104205130Sjilles
1105205130Sjilles	(*nlpp)->n = n;
1106205130Sjilles        if (oldstyle) {
1107205130Sjilles		/*
1108205130Sjilles		 * Start reading from old file again, ignoring any pushed back
1109205130Sjilles		 * tokens left from the backquote parsing
1110205130Sjilles		 */
1111205130Sjilles                popfile();
1112205130Sjilles		tokpushback = 0;
1113205130Sjilles	}
1114205130Sjilles	STARTSTACKSTR(out);
1115216706Sjilles	CHECKSTRSPACE(savelen + 1, out);
1116208655Sjilles	INTOFF;
1117205130Sjilles	if (str) {
1118205130Sjilles		memcpy(out, str, savelen);
1119205130Sjilles		STADJUST(savelen, out);
1120205130Sjilles		ckfree(str);
1121205130Sjilles		str = NULL;
1122205130Sjilles	}
1123205130Sjilles	if (ostr) {
1124205130Sjilles		ckfree(ostr);
1125205130Sjilles		ostr = NULL;
1126205130Sjilles	}
1127208655Sjilles	here = saveheredoclist;
1128208655Sjilles	if (here != NULL) {
1129208655Sjilles		while (here->next != NULL)
1130208655Sjilles			here = here->next;
1131208655Sjilles		here->next = heredoclist;
1132208655Sjilles		heredoclist = saveheredoclist;
1133208655Sjilles	}
1134205130Sjilles	handler = savehandler;
1135208655Sjilles	INTON;
1136205130Sjilles	if (quoted)
1137205130Sjilles		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1138205130Sjilles	else
1139205130Sjilles		USTPUTC(CTLBACKQ, out);
1140205130Sjilles	return out;
1141205130Sjilles}
1142205130Sjilles
1143205130Sjilles
11441556Srgrimes/*
1145221513Sjilles * Called to parse a backslash escape sequence inside $'...'.
1146221513Sjilles * The backslash has already been read.
1147221513Sjilles */
1148221513Sjillesstatic char *
1149221513Sjillesreadcstyleesc(char *out)
1150221513Sjilles{
1151221513Sjilles	int c, v, i, n;
1152221513Sjilles
1153221513Sjilles	c = pgetc();
1154221513Sjilles	switch (c) {
1155221513Sjilles	case '\0':
1156221513Sjilles		synerror("Unterminated quoted string");
1157221513Sjilles	case '\n':
1158221513Sjilles		plinno++;
1159221513Sjilles		if (doprompt)
1160221513Sjilles			setprompt(2);
1161221513Sjilles		else
1162221513Sjilles			setprompt(0);
1163221513Sjilles		return out;
1164221513Sjilles	case '\\':
1165221513Sjilles	case '\'':
1166221513Sjilles	case '"':
1167221513Sjilles		v = c;
1168221513Sjilles		break;
1169221513Sjilles	case 'a': v = '\a'; break;
1170221513Sjilles	case 'b': v = '\b'; break;
1171221513Sjilles	case 'e': v = '\033'; break;
1172221513Sjilles	case 'f': v = '\f'; break;
1173221513Sjilles	case 'n': v = '\n'; break;
1174221513Sjilles	case 'r': v = '\r'; break;
1175221513Sjilles	case 't': v = '\t'; break;
1176221513Sjilles	case 'v': v = '\v'; break;
1177221513Sjilles	case 'x':
1178221513Sjilles		  v = 0;
1179221513Sjilles		  for (;;) {
1180221513Sjilles			  c = pgetc();
1181221513Sjilles			  if (c >= '0' && c <= '9')
1182221513Sjilles				  v = (v << 4) + c - '0';
1183221513Sjilles			  else if (c >= 'A' && c <= 'F')
1184221513Sjilles				  v = (v << 4) + c - 'A' + 10;
1185221513Sjilles			  else if (c >= 'a' && c <= 'f')
1186221513Sjilles				  v = (v << 4) + c - 'a' + 10;
1187221513Sjilles			  else
1188221513Sjilles				  break;
1189221513Sjilles		  }
1190221513Sjilles		  pungetc();
1191221513Sjilles		  break;
1192221513Sjilles	case '0': case '1': case '2': case '3':
1193221513Sjilles	case '4': case '5': case '6': case '7':
1194221513Sjilles		  v = c - '0';
1195221513Sjilles		  c = pgetc();
1196221513Sjilles		  if (c >= '0' && c <= '7') {
1197221513Sjilles			  v <<= 3;
1198221513Sjilles			  v += c - '0';
1199221513Sjilles			  c = pgetc();
1200221513Sjilles			  if (c >= '0' && c <= '7') {
1201221513Sjilles				  v <<= 3;
1202221513Sjilles				  v += c - '0';
1203221513Sjilles			  } else
1204221513Sjilles				  pungetc();
1205221513Sjilles		  } else
1206221513Sjilles			  pungetc();
1207221513Sjilles		  break;
1208221513Sjilles	case 'c':
1209221513Sjilles		  c = pgetc();
1210221513Sjilles		  if (c < 0x3f || c > 0x7a || c == 0x60)
1211221513Sjilles			  synerror("Bad escape sequence");
1212221513Sjilles		  if (c == '\\' && pgetc() != '\\')
1213221513Sjilles			  synerror("Bad escape sequence");
1214221513Sjilles		  if (c == '?')
1215221513Sjilles			  v = 127;
1216221513Sjilles		  else
1217221513Sjilles			  v = c & 0x1f;
1218221513Sjilles		  break;
1219221513Sjilles	case 'u':
1220221513Sjilles	case 'U':
1221221513Sjilles		  n = c == 'U' ? 8 : 4;
1222221513Sjilles		  v = 0;
1223221513Sjilles		  for (i = 0; i < n; i++) {
1224221513Sjilles			  c = pgetc();
1225221513Sjilles			  if (c >= '0' && c <= '9')
1226221513Sjilles				  v = (v << 4) + c - '0';
1227221513Sjilles			  else if (c >= 'A' && c <= 'F')
1228221513Sjilles				  v = (v << 4) + c - 'A' + 10;
1229221513Sjilles			  else if (c >= 'a' && c <= 'f')
1230221513Sjilles				  v = (v << 4) + c - 'a' + 10;
1231221513Sjilles			  else
1232221513Sjilles				  synerror("Bad escape sequence");
1233221513Sjilles		  }
1234221513Sjilles		  if (v == 0 || (v >= 0xd800 && v <= 0xdfff))
1235221513Sjilles			  synerror("Bad escape sequence");
1236221513Sjilles		  /* We really need iconv here. */
1237221669Sjilles		  if (initial_localeisutf8 && v > 127) {
1238221669Sjilles			  CHECKSTRSPACE(4, out);
1239221669Sjilles			  /*
1240221669Sjilles			   * We cannot use wctomb() as the locale may have
1241221669Sjilles			   * changed.
1242221669Sjilles			   */
1243221669Sjilles			  if (v <= 0x7ff) {
1244221669Sjilles				  USTPUTC(0xc0 | v >> 6, out);
1245221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1246221669Sjilles				  return out;
1247221669Sjilles			  } else if (v <= 0xffff) {
1248221669Sjilles				  USTPUTC(0xe0 | v >> 12, out);
1249221669Sjilles				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1250221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1251221669Sjilles				  return out;
1252221669Sjilles			  } else if (v <= 0x10ffff) {
1253221669Sjilles				  USTPUTC(0xf0 | v >> 18, out);
1254221669Sjilles				  USTPUTC(0x80 | ((v >> 12) & 0x3f), out);
1255221669Sjilles				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1256221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1257221669Sjilles				  return out;
1258221669Sjilles			  }
1259221669Sjilles		  }
1260221513Sjilles		  if (v > 127)
1261221513Sjilles			  v = '?';
1262221513Sjilles		  break;
1263221513Sjilles	default:
1264221513Sjilles		  synerror("Bad escape sequence");
1265221513Sjilles	}
1266221513Sjilles	v = (char)v;
1267221513Sjilles	/*
1268221513Sjilles	 * We can't handle NUL bytes.
1269221513Sjilles	 * POSIX says we should skip till the closing quote.
1270221513Sjilles	 */
1271221513Sjilles	if (v == '\0') {
1272221513Sjilles		while ((c = pgetc()) != '\'') {
1273221513Sjilles			if (c == '\\')
1274221513Sjilles				c = pgetc();
1275221513Sjilles			if (c == PEOF)
1276221513Sjilles				synerror("Unterminated quoted string");
1277221513Sjilles		}
1278221513Sjilles		pungetc();
1279221513Sjilles		return out;
1280221513Sjilles	}
1281221513Sjilles	if (SQSYNTAX[v] == CCTL)
1282221513Sjilles		USTPUTC(CTLESC, out);
1283221513Sjilles	USTPUTC(v, out);
1284221513Sjilles	return out;
1285221513Sjilles}
1286221513Sjilles
1287221513Sjilles
1288221513Sjilles/*
12891556Srgrimes * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
12901556Srgrimes * is not NULL, read a here document.  In the latter case, eofmark is the
12911556Srgrimes * word which marks the end of the document and striptabs is true if
12921556Srgrimes * leading tabs should be stripped from the document.  The argument firstc
12931556Srgrimes * is the first character of the input token or document.
12941556Srgrimes *
12951556Srgrimes * Because C does not have internal subroutines, I have simulated them
12961556Srgrimes * using goto's to implement the subroutine linkage.  The following macros
12971556Srgrimes * will run code that appears at the end of readtoken1.
12981556Srgrimes */
12991556Srgrimes
13001556Srgrimes#define CHECKEND()	{goto checkend; checkend_return:;}
13011556Srgrimes#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
13021556Srgrimes#define PARSESUB()	{goto parsesub; parsesub_return:;}
13031556Srgrimes#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
13041556Srgrimes
1305213811Sobrienstatic int
1306248980Sjillesreadtoken1(int firstc, char const *initialsyntax, const char *eofmark,
1307248980Sjilles    int striptabs)
130890111Simp{
130917987Speter	int c = firstc;
131017987Speter	char *out;
13111556Srgrimes	int len;
13121556Srgrimes	char line[EOFMARKLEN + 1];
13131556Srgrimes	struct nodelist *bqlist;
13141556Srgrimes	int quotef;
1315206145Sjilles	int newvarnest;
1316206145Sjilles	int level;
131754679Scracauer	int synentry;
1318213811Sobrien	struct tokenstate state_static[MAXNEST_static];
1319213811Sobrien	int maxnest = MAXNEST_static;
1320206145Sjilles	struct tokenstate *state = state_static;
1321221513Sjilles	int sqiscstyle = 0;
13221556Srgrimes
13231556Srgrimes	startlinno = plinno;
13241556Srgrimes	quotef = 0;
13251556Srgrimes	bqlist = NULL;
1326206145Sjilles	newvarnest = 0;
1327206145Sjilles	level = 0;
1328206145Sjilles	state[level].syntax = initialsyntax;
1329206145Sjilles	state[level].parenlevel = 0;
1330206145Sjilles	state[level].category = TSTATE_TOP;
13311556Srgrimes
13321556Srgrimes	STARTSTACKSTR(out);
13331556Srgrimes	loop: {	/* for each line, until end of word */
13341556Srgrimes		CHECKEND();	/* set c to PEOF if at end of here document */
13351556Srgrimes		for (;;) {	/* until end of line or end of word */
1336214512Sjilles			CHECKSTRSPACE(4, out);	/* permit 4 calls to USTPUTC */
133754679Scracauer
1338206145Sjilles			synentry = state[level].syntax[c];
133954679Scracauer
134054679Scracauer			switch(synentry) {
13411556Srgrimes			case CNL:	/* '\n' */
1342206145Sjilles				if (state[level].syntax == BASESYNTAX)
13431556Srgrimes					goto endword;	/* exit outer loop */
13441556Srgrimes				USTPUTC(c, out);
13451556Srgrimes				plinno++;
13461556Srgrimes				if (doprompt)
13471556Srgrimes					setprompt(2);
13481556Srgrimes				else
13491556Srgrimes					setprompt(0);
13501556Srgrimes				c = pgetc();
13511556Srgrimes				goto loop;		/* continue outer loop */
1352221513Sjilles			case CSBACK:
1353221513Sjilles				if (sqiscstyle) {
1354221513Sjilles					out = readcstyleesc(out);
1355221513Sjilles					break;
1356221513Sjilles				}
1357221513Sjilles				/* FALLTHROUGH */
13581556Srgrimes			case CWORD:
13591556Srgrimes				USTPUTC(c, out);
13601556Srgrimes				break;
13611556Srgrimes			case CCTL:
1362206145Sjilles				if (eofmark == NULL || initialsyntax != SQSYNTAX)
13631556Srgrimes					USTPUTC(CTLESC, out);
13641556Srgrimes				USTPUTC(c, out);
13651556Srgrimes				break;
13661556Srgrimes			case CBACK:	/* backslash */
13671556Srgrimes				c = pgetc();
13681556Srgrimes				if (c == PEOF) {
13691556Srgrimes					USTPUTC('\\', out);
13701556Srgrimes					pungetc();
13711556Srgrimes				} else if (c == '\n') {
1372160849Syar					plinno++;
13731556Srgrimes					if (doprompt)
13741556Srgrimes						setprompt(2);
13751556Srgrimes					else
13761556Srgrimes						setprompt(0);
13771556Srgrimes				} else {
1378206145Sjilles					if (state[level].syntax == DQSYNTAX &&
1379206145Sjilles					    c != '\\' && c != '`' && c != '$' &&
1380206145Sjilles					    (c != '"' || (eofmark != NULL &&
1381206145Sjilles						newvarnest == 0)) &&
1382206145Sjilles					    (c != '}' || state[level].category != TSTATE_VAR_OLD))
13831556Srgrimes						USTPUTC('\\', out);
1384214512Sjilles					if ((eofmark == NULL ||
1385214512Sjilles					    newvarnest > 0) &&
1386214512Sjilles					    state[level].syntax == BASESYNTAX)
1387214512Sjilles						USTPUTC(CTLQUOTEMARK, out);
138883675Stegge					if (SQSYNTAX[c] == CCTL)
13891556Srgrimes						USTPUTC(CTLESC, out);
13901556Srgrimes					USTPUTC(c, out);
1391214512Sjilles					if ((eofmark == NULL ||
1392214512Sjilles					    newvarnest > 0) &&
1393214512Sjilles					    state[level].syntax == BASESYNTAX &&
1394214512Sjilles					    state[level].category == TSTATE_VAR_OLD)
1395214512Sjilles						USTPUTC(CTLQUOTEEND, out);
13961556Srgrimes					quotef++;
13971556Srgrimes				}
13981556Srgrimes				break;
13991556Srgrimes			case CSQUOTE:
1400206145Sjilles				USTPUTC(CTLQUOTEMARK, out);
1401206145Sjilles				state[level].syntax = SQSYNTAX;
1402221513Sjilles				sqiscstyle = 0;
14031556Srgrimes				break;
14041556Srgrimes			case CDQUOTE:
1405206145Sjilles				USTPUTC(CTLQUOTEMARK, out);
1406206145Sjilles				state[level].syntax = DQSYNTAX;
14071556Srgrimes				break;
14081556Srgrimes			case CENDQUOTE:
1409206145Sjilles				if (eofmark != NULL && newvarnest == 0)
14101556Srgrimes					USTPUTC(c, out);
1411206145Sjilles				else {
1412214512Sjilles					if (state[level].category == TSTATE_VAR_OLD)
1413214512Sjilles						USTPUTC(CTLQUOTEEND, out);
1414214305Sjilles					state[level].syntax = BASESYNTAX;
14151556Srgrimes					quotef++;
14161556Srgrimes				}
14171556Srgrimes				break;
14181556Srgrimes			case CVAR:	/* '$' */
14191556Srgrimes				PARSESUB();		/* parse substitution */
14201556Srgrimes				break;
14211556Srgrimes			case CENDVAR:	/* '}' */
1422206145Sjilles				if (level > 0 &&
1423214492Sjilles				    ((state[level].category == TSTATE_VAR_OLD &&
1424214492Sjilles				      state[level].syntax ==
1425214492Sjilles				      state[level - 1].syntax) ||
1426214490Sjilles				    (state[level].category == TSTATE_VAR_NEW &&
1427214490Sjilles				     state[level].syntax == BASESYNTAX))) {
1428214492Sjilles					if (state[level].category == TSTATE_VAR_NEW)
1429206145Sjilles						newvarnest--;
1430206145Sjilles					level--;
14311556Srgrimes					USTPUTC(CTLENDVAR, out);
14321556Srgrimes				} else {
14331556Srgrimes					USTPUTC(c, out);
14341556Srgrimes				}
14351556Srgrimes				break;
14361556Srgrimes			case CLP:	/* '(' in arithmetic */
1437206145Sjilles				state[level].parenlevel++;
14381556Srgrimes				USTPUTC(c, out);
14391556Srgrimes				break;
14401556Srgrimes			case CRP:	/* ')' in arithmetic */
1441206145Sjilles				if (state[level].parenlevel > 0) {
14421556Srgrimes					USTPUTC(c, out);
1443206145Sjilles					--state[level].parenlevel;
14441556Srgrimes				} else {
14451556Srgrimes					if (pgetc() == ')') {
1446206145Sjilles						if (level > 0 &&
1447206145Sjilles						    state[level].category == TSTATE_ARITH) {
1448206145Sjilles							level--;
14491556Srgrimes							USTPUTC(CTLENDARI, out);
14501556Srgrimes						} else
14511556Srgrimes							USTPUTC(')', out);
14521556Srgrimes					} else {
14538855Srgrimes						/*
14541556Srgrimes						 * unbalanced parens
14551556Srgrimes						 *  (don't 2nd guess - no error)
14561556Srgrimes						 */
14571556Srgrimes						pungetc();
14581556Srgrimes						USTPUTC(')', out);
14591556Srgrimes					}
14601556Srgrimes				}
14611556Srgrimes				break;
14621556Srgrimes			case CBQUOTE:	/* '`' */
1463206145Sjilles				out = parsebackq(out, &bqlist, 1,
1464206145Sjilles				    state[level].syntax == DQSYNTAX &&
1465206145Sjilles				    (eofmark == NULL || newvarnest > 0),
1466206145Sjilles				    state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
14671556Srgrimes				break;
14681556Srgrimes			case CEOF:
14691556Srgrimes				goto endword;		/* exit outer loop */
1470214305Sjilles			case CIGN:
1471214305Sjilles				break;
14721556Srgrimes			default:
1473206145Sjilles				if (level == 0)
14741556Srgrimes					goto endword;	/* exit outer loop */
14751556Srgrimes				USTPUTC(c, out);
14761556Srgrimes			}
14771556Srgrimes			c = pgetc_macro();
14781556Srgrimes		}
14791556Srgrimes	}
14801556Srgrimesendword:
1481206145Sjilles	if (state[level].syntax == ARISYNTAX)
14821556Srgrimes		synerror("Missing '))'");
1483206145Sjilles	if (state[level].syntax != BASESYNTAX && eofmark == NULL)
14841556Srgrimes		synerror("Unterminated quoted string");
1485206145Sjilles	if (state[level].category == TSTATE_VAR_OLD ||
1486206145Sjilles	    state[level].category == TSTATE_VAR_NEW) {
14871556Srgrimes		startlinno = plinno;
14881556Srgrimes		synerror("Missing '}'");
14891556Srgrimes	}
1490206145Sjilles	if (state != state_static)
1491206145Sjilles		parser_temp_free_upto(state);
14921556Srgrimes	USTPUTC('\0', out);
14931556Srgrimes	len = out - stackblock();
14941556Srgrimes	out = stackblock();
14951556Srgrimes	if (eofmark == NULL) {
14961556Srgrimes		if ((c == '>' || c == '<')
14971556Srgrimes		 && quotef == 0
14981556Srgrimes		 && len <= 2
14991556Srgrimes		 && (*out == '\0' || is_digit(*out))) {
15001556Srgrimes			PARSEREDIR();
15011556Srgrimes			return lasttoken = TREDIR;
15021556Srgrimes		} else {
15031556Srgrimes			pungetc();
15041556Srgrimes		}
15051556Srgrimes	}
15061556Srgrimes	quoteflag = quotef;
15071556Srgrimes	backquotelist = bqlist;
15081556Srgrimes	grabstackblock(len);
15091556Srgrimes	wordtext = out;
15101556Srgrimes	return lasttoken = TWORD;
15111556Srgrimes/* end of readtoken routine */
15121556Srgrimes
15131556Srgrimes
15141556Srgrimes/*
15151556Srgrimes * Check to see whether we are at the end of the here document.  When this
15161556Srgrimes * is called, c is set to the first character of the next input line.  If
15171556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF.
15181556Srgrimes */
15191556Srgrimes
15201556Srgrimescheckend: {
15211556Srgrimes	if (eofmark) {
15221556Srgrimes		if (striptabs) {
15231556Srgrimes			while (c == '\t')
15241556Srgrimes				c = pgetc();
15251556Srgrimes		}
15261556Srgrimes		if (c == *eofmark) {
15271556Srgrimes			if (pfgets(line, sizeof line) != NULL) {
1528248980Sjilles				const char *p, *q;
15291556Srgrimes
15301556Srgrimes				p = line;
15311556Srgrimes				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1532222134Sjilles				if ((*p == '\0' || *p == '\n') && *q == '\0') {
15331556Srgrimes					c = PEOF;
1534222134Sjilles					if (*p == '\n') {
1535222134Sjilles						plinno++;
1536222134Sjilles						needprompt = doprompt;
1537222134Sjilles					}
15381556Srgrimes				} else {
15391556Srgrimes					pushstring(line, strlen(line), NULL);
15401556Srgrimes				}
15411556Srgrimes			}
15421556Srgrimes		}
15431556Srgrimes	}
15441556Srgrimes	goto checkend_return;
15451556Srgrimes}
15461556Srgrimes
15471556Srgrimes
15481556Srgrimes/*
15491556Srgrimes * Parse a redirection operator.  The variable "out" points to a string
15501556Srgrimes * specifying the fd to be redirected.  The variable "c" contains the
15511556Srgrimes * first character of the redirection operator.
15521556Srgrimes */
15531556Srgrimes
15541556Srgrimesparseredir: {
15551556Srgrimes	char fd = *out;
15561556Srgrimes	union node *np;
15571556Srgrimes
15581556Srgrimes	np = (union node *)stalloc(sizeof (struct nfile));
15591556Srgrimes	if (c == '>') {
15601556Srgrimes		np->nfile.fd = 1;
15611556Srgrimes		c = pgetc();
15621556Srgrimes		if (c == '>')
15631556Srgrimes			np->type = NAPPEND;
15641556Srgrimes		else if (c == '&')
15651556Srgrimes			np->type = NTOFD;
156696922Stjr		else if (c == '|')
156796922Stjr			np->type = NCLOBBER;
15681556Srgrimes		else {
15691556Srgrimes			np->type = NTO;
15701556Srgrimes			pungetc();
15711556Srgrimes		}
15721556Srgrimes	} else {	/* c == '<' */
15731556Srgrimes		np->nfile.fd = 0;
15741556Srgrimes		c = pgetc();
15751556Srgrimes		if (c == '<') {
15761556Srgrimes			if (sizeof (struct nfile) != sizeof (struct nhere)) {
15771556Srgrimes				np = (union node *)stalloc(sizeof (struct nhere));
15781556Srgrimes				np->nfile.fd = 0;
15791556Srgrimes			}
15801556Srgrimes			np->type = NHERE;
15811556Srgrimes			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
15821556Srgrimes			heredoc->here = np;
15831556Srgrimes			if ((c = pgetc()) == '-') {
15841556Srgrimes				heredoc->striptabs = 1;
15851556Srgrimes			} else {
15861556Srgrimes				heredoc->striptabs = 0;
15871556Srgrimes				pungetc();
15881556Srgrimes			}
15891556Srgrimes		} else if (c == '&')
15901556Srgrimes			np->type = NFROMFD;
159166612Sbrian		else if (c == '>')
159266612Sbrian			np->type = NFROMTO;
15931556Srgrimes		else {
15941556Srgrimes			np->type = NFROM;
15951556Srgrimes			pungetc();
15961556Srgrimes		}
15971556Srgrimes	}
15981556Srgrimes	if (fd != '\0')
15991556Srgrimes		np->nfile.fd = digit_val(fd);
16001556Srgrimes	redirnode = np;
16011556Srgrimes	goto parseredir_return;
16021556Srgrimes}
16031556Srgrimes
16041556Srgrimes
16051556Srgrimes/*
16061556Srgrimes * Parse a substitution.  At this point, we have read the dollar sign
16071556Srgrimes * and nothing else.
16081556Srgrimes */
16091556Srgrimes
16101556Srgrimesparsesub: {
1611179022Sstefanf	char buf[10];
16121556Srgrimes	int subtype;
16131556Srgrimes	int typeloc;
16141556Srgrimes	int flags;
16151556Srgrimes	char *p;
16161556Srgrimes	static const char types[] = "}-+?=";
1617179022Sstefanf	int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1618179022Sstefanf	int linno;
1619179387Sstefanf	int length;
1620219623Sjilles	int c1;
16211556Srgrimes
16221556Srgrimes	c = pgetc();
1623221513Sjilles	if (c == '(') {	/* $(command) or $((arith)) */
16241556Srgrimes		if (pgetc() == '(') {
16251556Srgrimes			PARSEARITH();
16261556Srgrimes		} else {
16271556Srgrimes			pungetc();
1628206145Sjilles			out = parsebackq(out, &bqlist, 0,
1629206145Sjilles			    state[level].syntax == DQSYNTAX &&
1630206145Sjilles			    (eofmark == NULL || newvarnest > 0),
1631206145Sjilles			    state[level].syntax == DQSYNTAX ||
1632206145Sjilles			    state[level].syntax == ARISYNTAX);
16331556Srgrimes		}
1634221513Sjilles	} else if (c == '{' || is_name(c) || is_special(c)) {
16351556Srgrimes		USTPUTC(CTLVAR, out);
16361556Srgrimes		typeloc = out - stackblock();
16371556Srgrimes		USTPUTC(VSNORMAL, out);
16381556Srgrimes		subtype = VSNORMAL;
1639179022Sstefanf		flags = 0;
16401556Srgrimes		if (c == '{') {
164118202Speter			bracketed_name = 1;
16421556Srgrimes			c = pgetc();
1643219623Sjilles			subtype = 0;
16441556Srgrimes		}
1645219623Sjillesvarname:
1646149026Sstefanf		if (!is_eof(c) && is_name(c)) {
1647179387Sstefanf			length = 0;
16481556Srgrimes			do {
16491556Srgrimes				STPUTC(c, out);
16501556Srgrimes				c = pgetc();
1651179387Sstefanf				length++;
1652149026Sstefanf			} while (!is_eof(c) && is_in_name(c));
1653179387Sstefanf			if (length == 6 &&
1654179387Sstefanf			    strncmp(out - length, "LINENO", length) == 0) {
1655179022Sstefanf				/* Replace the variable name with the
1656179022Sstefanf				 * current line number. */
1657179022Sstefanf				linno = plinno;
1658179022Sstefanf				if (funclinno != 0)
1659179022Sstefanf					linno -= funclinno - 1;
1660179022Sstefanf				snprintf(buf, sizeof(buf), "%d", linno);
1661179022Sstefanf				STADJUST(-6, out);
1662215783Sjilles				STPUTS(buf, out);
1663179022Sstefanf				flags |= VSLINENO;
1664179022Sstefanf			}
166518202Speter		} else if (is_digit(c)) {
166618202Speter			if (bracketed_name) {
166718202Speter				do {
166818202Speter					STPUTC(c, out);
166918202Speter					c = pgetc();
167018202Speter				} while (is_digit(c));
167118202Speter			} else {
167218202Speter				STPUTC(c, out);
167318202Speter				c = pgetc();
167418202Speter			}
1675219623Sjilles		} else if (is_special(c)) {
1676219623Sjilles			c1 = c;
1677219623Sjilles			c = pgetc();
1678219623Sjilles			if (subtype == 0 && c1 == '#') {
1679219623Sjilles				subtype = VSLENGTH;
1680219623Sjilles				if (strchr(types, c) == NULL && c != ':' &&
1681219623Sjilles				    c != '#' && c != '%')
1682219623Sjilles					goto varname;
1683219623Sjilles				c1 = c;
1684219623Sjilles				c = pgetc();
1685219623Sjilles				if (c1 != '}' && c == '}') {
1686219623Sjilles					pungetc();
1687219623Sjilles					c = c1;
1688219623Sjilles					goto varname;
1689219623Sjilles				}
1690219623Sjilles				pungetc();
1691219623Sjilles				c = c1;
1692219623Sjilles				c1 = '#';
1693219623Sjilles				subtype = 0;
1694219623Sjilles			}
1695219623Sjilles			USTPUTC(c1, out);
16961556Srgrimes		} else {
1697219623Sjilles			subtype = VSERROR;
1698219623Sjilles			if (c == '}')
1699219623Sjilles				pungetc();
1700219623Sjilles			else if (c == '\n' || c == PEOF)
1701219623Sjilles				synerror("Unexpected end of line in substitution");
1702219623Sjilles			else
1703164003Sstefanf				USTPUTC(c, out);
17041556Srgrimes		}
17051556Srgrimes		if (subtype == 0) {
170617987Speter			switch (c) {
170717987Speter			case ':':
1708179022Sstefanf				flags |= VSNUL;
17091556Srgrimes				c = pgetc();
171017987Speter				/*FALLTHROUGH*/
171117987Speter			default:
171217987Speter				p = strchr(types, c);
1713164003Sstefanf				if (p == NULL) {
1714206144Sjilles					if (c == '\n' || c == PEOF)
1715206144Sjilles						synerror("Unexpected end of line in substitution");
1716164003Sstefanf					if (flags == VSNUL)
1717164003Sstefanf						STPUTC(':', out);
1718164003Sstefanf					STPUTC(c, out);
1719164003Sstefanf					subtype = VSERROR;
1720164003Sstefanf				} else
1721164003Sstefanf					subtype = p - types + VSNORMAL;
172217987Speter				break;
172317987Speter			case '%':
172420425Ssteve			case '#':
172517987Speter				{
172617987Speter					int cc = c;
172717987Speter					subtype = c == '#' ? VSTRIMLEFT :
172817987Speter							     VSTRIMRIGHT;
172917987Speter					c = pgetc();
173017987Speter					if (c == cc)
173117987Speter						subtype++;
173217987Speter					else
173317987Speter						pungetc();
173417987Speter					break;
173517987Speter				}
17361556Srgrimes			}
1737164003Sstefanf		} else if (subtype != VSERROR) {
1738221461Sjilles			if (subtype == VSLENGTH && c != '}')
1739221461Sjilles				subtype = VSERROR;
17401556Srgrimes			pungetc();
17411556Srgrimes		}
1742164003Sstefanf		STPUTC('=', out);
1743220903Sjilles		if (state[level].syntax == DQSYNTAX ||
1744220903Sjilles		    state[level].syntax == ARISYNTAX)
17451556Srgrimes			flags |= VSQUOTE;
17461556Srgrimes		*(stackblock() + typeloc) = subtype | flags;
1747206145Sjilles		if (subtype != VSNORMAL) {
1748206145Sjilles			if (level + 1 >= maxnest) {
1749206145Sjilles				maxnest *= 2;
1750206145Sjilles				if (state == state_static) {
1751206145Sjilles					state = parser_temp_alloc(
1752206145Sjilles					    maxnest * sizeof(*state));
1753206145Sjilles					memcpy(state, state_static,
1754213811Sobrien					    MAXNEST_static * sizeof(*state));
1755206145Sjilles				} else
1756206145Sjilles					state = parser_temp_realloc(state,
1757206145Sjilles					    maxnest * sizeof(*state));
1758206145Sjilles			}
1759206145Sjilles			level++;
1760206145Sjilles			state[level].parenlevel = 0;
1761206145Sjilles			if (subtype == VSMINUS || subtype == VSPLUS ||
1762206145Sjilles			    subtype == VSQUESTION || subtype == VSASSIGN) {
1763206145Sjilles				/*
1764206145Sjilles				 * For operators that were in the Bourne shell,
1765206145Sjilles				 * inherit the double-quote state.
1766206145Sjilles				 */
1767206145Sjilles				state[level].syntax = state[level - 1].syntax;
1768206145Sjilles				state[level].category = TSTATE_VAR_OLD;
1769206145Sjilles			} else {
1770206145Sjilles				/*
1771206145Sjilles				 * The other operators take a pattern,
1772206145Sjilles				 * so go to BASESYNTAX.
1773206145Sjilles				 * Also, ' and " are now special, even
1774206145Sjilles				 * in here documents.
1775206145Sjilles				 */
1776206145Sjilles				state[level].syntax = BASESYNTAX;
1777206145Sjilles				state[level].category = TSTATE_VAR_NEW;
1778206145Sjilles				newvarnest++;
1779206145Sjilles			}
1780206145Sjilles		}
1781221513Sjilles	} else if (c == '\'' && state[level].syntax == BASESYNTAX) {
1782221513Sjilles		/* $'cstylequotes' */
1783221513Sjilles		USTPUTC(CTLQUOTEMARK, out);
1784221513Sjilles		state[level].syntax = SQSYNTAX;
1785221513Sjilles		sqiscstyle = 1;
1786221513Sjilles	} else {
1787221513Sjilles		USTPUTC('$', out);
1788221513Sjilles		pungetc();
17891556Srgrimes	}
17901556Srgrimes	goto parsesub_return;
17911556Srgrimes}
17921556Srgrimes
17931556Srgrimes
17941556Srgrimes/*
17951556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state)
17961556Srgrimes */
17971556Srgrimesparsearith: {
17981556Srgrimes
1799206145Sjilles	if (level + 1 >= maxnest) {
1800206145Sjilles		maxnest *= 2;
1801206145Sjilles		if (state == state_static) {
1802206145Sjilles			state = parser_temp_alloc(
1803206145Sjilles			    maxnest * sizeof(*state));
1804206145Sjilles			memcpy(state, state_static,
1805213811Sobrien			    MAXNEST_static * sizeof(*state));
1806206145Sjilles		} else
1807206145Sjilles			state = parser_temp_realloc(state,
1808206145Sjilles			    maxnest * sizeof(*state));
18091556Srgrimes	}
1810206145Sjilles	level++;
1811206145Sjilles	state[level].syntax = ARISYNTAX;
1812206145Sjilles	state[level].parenlevel = 0;
1813206145Sjilles	state[level].category = TSTATE_ARITH;
1814206145Sjilles	USTPUTC(CTLARI, out);
1815206145Sjilles	if (state[level - 1].syntax == DQSYNTAX)
1816206145Sjilles		USTPUTC('"',out);
1817206145Sjilles	else
1818206145Sjilles		USTPUTC(' ',out);
18191556Srgrimes	goto parsearith_return;
18201556Srgrimes}
18211556Srgrimes
18221556Srgrimes} /* end of readtoken */
18231556Srgrimes
18241556Srgrimes
1825253650Sjillesvoid
1826253650Sjillesresetparser(void)
1827253650Sjilles{
18281556Srgrimes	tokpushback = 0;
18291556Srgrimes	checkkwd = 0;
18301556Srgrimes}
18311556Srgrimes
1832253650Sjilles
18331556Srgrimes/*
18341556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs
18351556Srgrimes * or backquotes).
18361556Srgrimes */
18371556Srgrimes
1838213811Sobrienstatic int
183990111Simpnoexpand(char *text)
184090111Simp{
184125230Ssteve	char *p;
184225230Ssteve	char c;
18431556Srgrimes
18441556Srgrimes	p = text;
18451556Srgrimes	while ((c = *p++) != '\0') {
184639137Stegge		if ( c == CTLQUOTEMARK)
184739137Stegge			continue;
18481556Srgrimes		if (c == CTLESC)
18491556Srgrimes			p++;
185083675Stegge		else if (BASESYNTAX[(int)c] == CCTL)
18511556Srgrimes			return 0;
18521556Srgrimes	}
18531556Srgrimes	return 1;
18541556Srgrimes}
18551556Srgrimes
18561556Srgrimes
18571556Srgrimes/*
18581556Srgrimes * Return true if the argument is a legal variable name (a letter or
18591556Srgrimes * underscore followed by zero or more letters, underscores, and digits).
18601556Srgrimes */
18611556Srgrimes
18621556Srgrimesint
1863200956Sjillesgoodname(const char *name)
186490111Simp{
1865200956Sjilles	const char *p;
18661556Srgrimes
18671556Srgrimes	p = name;
18681556Srgrimes	if (! is_name(*p))
18691556Srgrimes		return 0;
18701556Srgrimes	while (*++p) {
18711556Srgrimes		if (! is_in_name(*p))
18721556Srgrimes			return 0;
18731556Srgrimes	}
18741556Srgrimes	return 1;
18751556Srgrimes}
18761556Srgrimes
18771556Srgrimes
1878222165Sjillesint
1879222165Sjillesisassignment(const char *p)
1880222165Sjilles{
1881222165Sjilles	if (!is_name(*p))
1882222165Sjilles		return 0;
1883222165Sjilles	p++;
1884222165Sjilles	for (;;) {
1885222165Sjilles		if (*p == '=')
1886222165Sjilles			return 1;
1887222165Sjilles		else if (!is_in_name(*p))
1888222165Sjilles			return 0;
1889222165Sjilles		p++;
1890222165Sjilles	}
1891222165Sjilles}
1892222165Sjilles
1893222165Sjilles
18941556Srgrimes/*
18951556Srgrimes * Called when an unexpected token is read during the parse.  The argument
18961556Srgrimes * is the token that is expected, or -1 if more than one type of token can
18971556Srgrimes * occur at this point.
18981556Srgrimes */
18991556Srgrimes
1900213811Sobrienstatic void
190190111Simpsynexpect(int token)
190217987Speter{
19031556Srgrimes	char msg[64];
19041556Srgrimes
19051556Srgrimes	if (token >= 0) {
19061556Srgrimes		fmtstr(msg, 64, "%s unexpected (expecting %s)",
19071556Srgrimes			tokname[lasttoken], tokname[token]);
19081556Srgrimes	} else {
19091556Srgrimes		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
19101556Srgrimes	}
19111556Srgrimes	synerror(msg);
19121556Srgrimes}
19131556Srgrimes
19141556Srgrimes
1915213811Sobrienstatic void
1916201053Sjillessynerror(const char *msg)
191790111Simp{
19181556Srgrimes	if (commandname)
1919201366Sjilles		outfmt(out2, "%s: %d: ", commandname, startlinno);
1920201366Sjilles	outfmt(out2, "Syntax error: %s\n", msg);
19211556Srgrimes	error((char *)NULL);
19221556Srgrimes}
19231556Srgrimes
1924213811Sobrienstatic void
192590111Simpsetprompt(int which)
192690111Simp{
19271556Srgrimes	whichprompt = which;
19281556Srgrimes
192917987Speter#ifndef NO_HISTORY
19301556Srgrimes	if (!el)
193117987Speter#endif
1932199629Sjilles	{
19331556Srgrimes		out2str(getprompt(NULL));
1934199629Sjilles		flushout(out2);
1935199629Sjilles	}
19361556Srgrimes}
19371556Srgrimes
19381556Srgrimes/*
19391556Srgrimes * called by editline -- any expansions to the prompt
19401556Srgrimes *    should be added here.
19411556Srgrimes */
19421556Srgrimeschar *
194390111Simpgetprompt(void *unused __unused)
194425905Ssteve{
1945142845Sobrien	static char ps[PROMPTLEN];
1946142845Sobrien	char *fmt;
1947209653Sjilles	const char *pwd;
1948209653Sjilles	int i, trim;
1949214538Sjilles	static char internal_error[] = "??";
1950142845Sobrien
1951142845Sobrien	/*
1952142845Sobrien	 * Select prompt format.
1953142845Sobrien	 */
19541556Srgrimes	switch (whichprompt) {
19551556Srgrimes	case 0:
1956201053Sjilles		fmt = nullstr;
1957142845Sobrien		break;
19581556Srgrimes	case 1:
1959142845Sobrien		fmt = ps1val();
1960142845Sobrien		break;
19611556Srgrimes	case 2:
1962142845Sobrien		fmt = ps2val();
1963142845Sobrien		break;
19641556Srgrimes	default:
1965201053Sjilles		return internal_error;
19661556Srgrimes	}
1967142845Sobrien
1968142845Sobrien	/*
1969142845Sobrien	 * Format prompt string.
1970142845Sobrien	 */
1971142845Sobrien	for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1972142845Sobrien		if (*fmt == '\\')
1973142845Sobrien			switch (*++fmt) {
1974142845Sobrien
1975142845Sobrien				/*
1976142845Sobrien				 * Hostname.
1977142845Sobrien				 *
1978142845Sobrien				 * \h specifies just the local hostname,
1979142845Sobrien				 * \H specifies fully-qualified hostname.
1980142845Sobrien				 */
1981142845Sobrien			case 'h':
1982142845Sobrien			case 'H':
1983149024Sstefanf				ps[i] = '\0';
1984142845Sobrien				gethostname(&ps[i], PROMPTLEN - i);
1985142845Sobrien				/* Skip to end of hostname. */
1986142845Sobrien				trim = (*fmt == 'h') ? '.' : '\0';
1987142845Sobrien				while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1988142845Sobrien					i++;
1989142845Sobrien				break;
1990142845Sobrien
1991142845Sobrien				/*
1992142845Sobrien				 * Working directory.
1993142845Sobrien				 *
1994142845Sobrien				 * \W specifies just the final component,
1995142845Sobrien				 * \w specifies the entire path.
1996142845Sobrien				 */
1997142845Sobrien			case 'W':
1998142845Sobrien			case 'w':
1999209653Sjilles				pwd = lookupvar("PWD");
2000209653Sjilles				if (pwd == NULL)
2001209653Sjilles					pwd = "?";
2002209653Sjilles				if (*fmt == 'W' &&
2003209653Sjilles				    *pwd == '/' && pwd[1] != '\0')
2004209653Sjilles					strlcpy(&ps[i], strrchr(pwd, '/') + 1,
2005209653Sjilles					    PROMPTLEN - i);
2006209653Sjilles				else
2007209653Sjilles					strlcpy(&ps[i], pwd, PROMPTLEN - i);
2008142845Sobrien				/* Skip to end of path. */
2009142845Sobrien				while (ps[i + 1] != '\0')
2010142845Sobrien					i++;
2011142845Sobrien				break;
2012142845Sobrien
2013142845Sobrien				/*
2014142845Sobrien				 * Superuser status.
2015142845Sobrien				 *
2016142845Sobrien				 * '$' for normal users, '#' for root.
2017142845Sobrien				 */
2018142845Sobrien			case '$':
2019142845Sobrien				ps[i] = (geteuid() != 0) ? '$' : '#';
2020142845Sobrien				break;
2021142845Sobrien
2022142845Sobrien				/*
2023142845Sobrien				 * A literal \.
2024142845Sobrien				 */
2025142845Sobrien			case '\\':
2026142845Sobrien				ps[i] = '\\';
2027142845Sobrien				break;
2028142845Sobrien
2029142845Sobrien				/*
2030142845Sobrien				 * Emit unrecognized formats verbatim.
2031142845Sobrien				 */
2032142845Sobrien			default:
2033142845Sobrien				ps[i++] = '\\';
2034142845Sobrien				ps[i] = *fmt;
2035142845Sobrien				break;
2036142845Sobrien			}
2037142845Sobrien		else
2038142845Sobrien			ps[i] = *fmt;
2039142845Sobrien	ps[i] = '\0';
2040142845Sobrien	return (ps);
20411556Srgrimes}
2042222907Sjilles
2043222907Sjilles
2044222907Sjillesconst char *
2045248980Sjillesexpandstr(const char *ps)
2046222907Sjilles{
2047222907Sjilles	union node n;
2048222907Sjilles	struct jmploc jmploc;
2049222907Sjilles	struct jmploc *const savehandler = handler;
2050222907Sjilles	const int saveprompt = doprompt;
2051222907Sjilles	struct parsefile *const savetopfile = getcurrentfile();
2052222907Sjilles	struct parser_temp *const saveparser_temp = parser_temp;
2053222907Sjilles	const char *result = NULL;
2054222907Sjilles
2055222907Sjilles	if (!setjmp(jmploc.loc)) {
2056222907Sjilles		handler = &jmploc;
2057222907Sjilles		parser_temp = NULL;
2058222907Sjilles		setinputstring(ps, 1);
2059222907Sjilles		doprompt = 0;
2060222907Sjilles		readtoken1(pgetc(), DQSYNTAX, "\n\n", 0);
2061222907Sjilles		if (backquotelist != NULL)
2062222907Sjilles			error("Command substitution not allowed here");
2063222907Sjilles
2064222907Sjilles		n.narg.type = NARG;
2065222907Sjilles		n.narg.next = NULL;
2066222907Sjilles		n.narg.text = wordtext;
2067222907Sjilles		n.narg.backquote = backquotelist;
2068222907Sjilles
2069222907Sjilles		expandarg(&n, NULL, 0);
2070222907Sjilles		result = stackblock();
2071222907Sjilles		INTOFF;
2072222907Sjilles	}
2073222907Sjilles	handler = savehandler;
2074222907Sjilles	doprompt = saveprompt;
2075222907Sjilles	popfilesupto(savetopfile);
2076222907Sjilles	if (parser_temp != saveparser_temp) {
2077222907Sjilles		parser_temp_free_all();
2078222907Sjilles		parser_temp = saveparser_temp;
2079222907Sjilles	}
2080222907Sjilles	if (result != NULL) {
2081222907Sjilles		INTON;
2082222907Sjilles	} else if (exception == EXINT)
2083222907Sjilles		raise(SIGINT);
2084222907Sjilles	return result;
2085222907Sjilles}
2086