parser.c revision 223282
157429Smarkm/*-
257429Smarkm * Copyright (c) 1991, 1993
357429Smarkm *	The Regents of the University of California.  All rights reserved.
457429Smarkm *
557429Smarkm * This code is derived from software contributed to Berkeley by
657429Smarkm * Kenneth Almquist.
760576Skris *
865674Skris * Redistribution and use in source and binary forms, with or without
965674Skris * modification, are permitted provided that the following conditions
1065674Skris * are met:
1165674Skris * 1. Redistributions of source code must retain the above copyright
1265674Skris *    notice, this list of conditions and the following disclaimer.
1365674Skris * 2. Redistributions in binary form must reproduce the above copyright
1465674Skris *    notice, this list of conditions and the following disclaimer in the
1565674Skris *    documentation and/or other materials provided with the distribution.
1665674Skris * 4. Neither the name of the University nor the names of its contributors
1765674Skris *    may be used to endorse or promote products derived from this software
1865674Skris *    without specific prior written permission.
1965674Skris *
2065674Skris * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2165674Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2265674Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2365674Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2465674Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2565674Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2665674Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2765674Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2865674Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2965674Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3065674Skris * SUCH DAMAGE.
3165674Skris */
3265674Skris
3365674Skris#ifndef lint
3465674Skris#if 0
3565674Skrisstatic char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
3657429Smarkm#endif
3757429Smarkm#endif /* not lint */
3857429Smarkm#include <sys/cdefs.h>
39149753Sdes__FBSDID("$FreeBSD: head/bin/sh/parser.c 223282 2011-06-18 23:58:59Z jilles $");
4057429Smarkm
4176262Sgreen#include <stdlib.h>
4276262Sgreen#include <unistd.h>
4360576Skris#include <stdio.h>
4460576Skris
4576262Sgreen#include "shell.h"
4657429Smarkm#include "parser.h"
4757429Smarkm#include "nodes.h"
4857429Smarkm#include "expand.h"	/* defines rmescapes() */
4976262Sgreen#include "syntax.h"
5057429Smarkm#include "options.h"
5176262Sgreen#include "input.h"
5276262Sgreen#include "output.h"
5392559Sdes#include "var.h"
54147005Sdes#include "error.h"
55149753Sdes#include "memalloc.h"
5657429Smarkm#include "mystring.h"
5776262Sgreen#include "alias.h"
5876262Sgreen#include "show.h"
5976262Sgreen#include "eval.h"
6057429Smarkm#include "exec.h"	/* to check for special builtins */
6157429Smarkm#ifndef NO_HISTORY
6257429Smarkm#include "myhistedit.h"
6357429Smarkm#endif
6457429Smarkm
6557429Smarkm/*
6657429Smarkm * Shell command parser.
6757429Smarkm */
6892559Sdes
6976262Sgreen#define	EOFMARKLEN	79
7076262Sgreen#define	PROMPTLEN	128
7157429Smarkm
7257429Smarkm/* values of checkkwd variable */
7392559Sdes#define CHKALIAS	0x1
7492559Sdes#define CHKKWD		0x2
7569591Sgreen#define CHKNL		0x4
7669591Sgreen
77137019Sdes/* values returned by readtoken */
7857429Smarkm#include "token.h"
7957429Smarkm
8057429Smarkm
8157429Smarkm
8257429Smarkmstruct heredoc {
8392559Sdes	struct heredoc *next;	/* next here document in list */
8492559Sdes	union node *here;		/* redirection node */
8592559Sdes	char *eofmark;		/* string indicating end of input */
8669591Sgreen	int striptabs;		/* if set, strip leading tabs */
8757429Smarkm};
8857429Smarkm
8957429Smarkmstruct parser_temp {
9057429Smarkm	struct parser_temp *next;
9157429Smarkm	void *data;
92137019Sdes};
93137019Sdes
94137019Sdes
9557429Smarkmstatic struct heredoc *heredoclist;	/* list of here documents to read */
9657429Smarkmstatic int doprompt;		/* if set, prompt the user */
9757429Smarkmstatic int needprompt;		/* true if interactive and at start of line */
9857429Smarkmstatic int lasttoken;		/* last token read */
9957429SmarkmMKINIT int tokpushback;		/* last token pushed back */
10057429Smarkmstatic char *wordtext;		/* text of last word returned by readtoken */
10157429SmarkmMKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
10257429Smarkmstatic struct nodelist *backquotelist;
10357429Smarkmstatic union node *redirnode;
10476262Sgreenstatic struct heredoc *heredoc;
10576262Sgreenstatic int quoteflag;		/* set if (part of) last token was quoted */
10676262Sgreenstatic int startlinno;		/* line # where last token started */
10776262Sgreenstatic int funclinno;		/* line # where the current function started */
10857429Smarkmstatic struct parser_temp *parser_temp;
10957429Smarkm
11057429Smarkm
11157429Smarkmstatic union node *list(int, int);
11257429Smarkmstatic union node *andor(void);
11357429Smarkmstatic union node *pipeline(void);
11457429Smarkmstatic union node *command(void);
11557429Smarkmstatic union node *simplecmd(union node **, union node *);
11657429Smarkmstatic union node *makename(void);
11776262Sgreenstatic void parsefname(void);
11876262Sgreenstatic void parseheredoc(void);
11957429Smarkmstatic int peektoken(void);
12057429Smarkmstatic int readtoken(void);
12157429Smarkmstatic int xxreadtoken(void);
12292559Sdesstatic int readtoken1(int, char const *, char *, int);
12357429Smarkmstatic int noexpand(char *);
12457429Smarkmstatic void synexpect(int) __dead2;
12557429Smarkmstatic void synerror(const char *) __dead2;
12676262Sgreenstatic void setprompt(int);
12776262Sgreen
12876262Sgreen
12992559Sdesstatic void *
13057429Smarkmparser_temp_alloc(size_t len)
13157429Smarkm{
13292559Sdes	struct parser_temp *t;
13357429Smarkm
13492559Sdes	INTOFF;
13592559Sdes	t = ckmalloc(sizeof(*t));
13692559Sdes	t->data = NULL;
13792559Sdes	t->next = parser_temp;
13892559Sdes	parser_temp = t;
13969591Sgreen	t->data = ckmalloc(len);
14057429Smarkm	INTON;
14157429Smarkm	return t->data;
14257429Smarkm}
14357429Smarkm
14457429Smarkm
14557429Smarkmstatic void *
14676262Sgreenparser_temp_realloc(void *ptr, size_t len)
14776262Sgreen{
148126277Sdes	struct parser_temp *t;
14957429Smarkm
15076262Sgreen	INTOFF;
151149753Sdes	t = parser_temp;
152149753Sdes	if (ptr != t->data)
15376262Sgreen		error("bug: parser_temp_realloc misused");
15492559Sdes	t->data = ckrealloc(t->data, len);
15557429Smarkm	INTON;
15657429Smarkm	return t->data;
15776262Sgreen}
15857429Smarkm
15957429Smarkm
16057429Smarkmstatic void
16157429Smarkmparser_temp_free_upto(void *ptr)
16257429Smarkm{
16357429Smarkm	struct parser_temp *t;
16457429Smarkm	int done = 0;
16576262Sgreen
16692559Sdes	INTOFF;
16776262Sgreen	while (parser_temp != NULL && !done) {
16876262Sgreen		t = parser_temp;
16960576Skris		parser_temp = t->next;
17060576Skris		done = t->data == ptr;
17160576Skris		ckfree(t->data);
17276262Sgreen		ckfree(t);
17376262Sgreen	}
17492559Sdes	INTON;
17592559Sdes	if (!done)
17660576Skris		error("bug: parser_temp_free_upto misused");
17760576Skris}
17876262Sgreen
17960576Skris
18060576Skrisstatic void
18160576Skrisparser_temp_free_all(void)
18260576Skris{
18376262Sgreen	struct parser_temp *t;
18460576Skris
18560576Skris	INTOFF;
18660576Skris	while (parser_temp != NULL) {
18760576Skris		t = parser_temp;
18876262Sgreen		parser_temp = t->next;
18960576Skris		ckfree(t->data);
19060576Skris		ckfree(t);
19160576Skris	}
19276262Sgreen	INTON;
19376262Sgreen}
19476262Sgreen
19576262Sgreen
19676262Sgreen/*
19776262Sgreen * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
19876262Sgreen * valid parse tree indicating a blank line.)
19976262Sgreen */
20076262Sgreen
20160576Skrisunion node *
20260576Skrisparsecmd(int interact)
20360576Skris{
20460576Skris	int t;
20560576Skris
20660576Skris	/* This assumes the parser is not re-entered,
20776262Sgreen	 * which could happen if we add command substitution on PS1/PS2.
20860576Skris	 */
20960576Skris	parser_temp_free_all();
21060576Skris	heredoclist = NULL;
21176262Sgreen
21276262Sgreen	tokpushback = 0;
21376262Sgreen	doprompt = interact;
21460576Skris	if (doprompt)
21560576Skris		setprompt(1);
21676262Sgreen	else
21776262Sgreen		setprompt(0);
21876262Sgreen	needprompt = 0;
21960576Skris	t = readtoken();
22060576Skris	if (t == TEOF)
22160576Skris		return NEOF;
22260576Skris	if (t == TNL)
22376262Sgreen		return NULL;
22460576Skris	tokpushback++;
22560576Skris	return list(1, 1);
22660576Skris}
22757429Smarkm
22876262Sgreen
22976262Sgreenstatic union node *
23057429Smarkmlist(int nlflag, int erflag)
23157429Smarkm{
23257429Smarkm	union node *ntop, *n1, *n2, *n3;
23392559Sdes	int tok;
23476262Sgreen
23557429Smarkm	checkkwd = CHKNL | CHKKWD | CHKALIAS;
23657429Smarkm	if (!nlflag && !erflag && tokendlist[peektoken()])
23776262Sgreen		return NULL;
238113911Sdes	ntop = n1 = NULL;
23957429Smarkm	for (;;) {
240149753Sdes		n2 = andor();
241137019Sdes		tok = readtoken();
24257429Smarkm		if (tok == TBACKGND) {
243113911Sdes			if (n2->type == NPIPE) {
244113911Sdes				n2->npipe.backgnd = 1;
245113911Sdes			} else if (n2->type == NREDIR) {
246113911Sdes				n2->type = NBACKGND;
247113911Sdes			} else {
248147005Sdes				n3 = (union node *)stalloc(sizeof (struct nredir));
249147005Sdes				n3->type = NBACKGND;
250147005Sdes				n3->nredir.n = n2;
251147005Sdes				n3->nredir.redirect = NULL;
252137019Sdes				n2 = n3;
25357429Smarkm			}
25457429Smarkm		}
25592559Sdes		if (ntop == NULL)
25657429Smarkm			ntop = n2;
257149753Sdes		else if (n1 == NULL) {
25857429Smarkm			n1 = (union node *)stalloc(sizeof (struct nbinary));
25960576Skris			n1->type = NSEMI;
26057429Smarkm			n1->nbinary.ch1 = ntop;
26176262Sgreen			n1->nbinary.ch2 = n2;
26257429Smarkm			ntop = n1;
26357429Smarkm		}
26476262Sgreen		else {
26576262Sgreen			n3 = (union node *)stalloc(sizeof (struct nbinary));
26692559Sdes			n3->type = NSEMI;
26757429Smarkm			n3->nbinary.ch1 = n1->nbinary.ch2;
26876262Sgreen			n3->nbinary.ch2 = n2;
26957429Smarkm			n1->nbinary.ch2 = n3;
27057429Smarkm			n1 = n3;
27157429Smarkm		}
27257429Smarkm		switch (tok) {
27357429Smarkm		case TBACKGND:
27476262Sgreen		case TSEMI:
27576262Sgreen			tok = readtoken();
27692559Sdes			/* FALLTHROUGH */
27757429Smarkm		case TNL:
27876262Sgreen			if (tok == TNL) {
27957429Smarkm				parseheredoc();
28057429Smarkm				if (nlflag)
28157429Smarkm					return ntop;
28257429Smarkm			} else if (tok == TEOF && nlflag) {
28357429Smarkm				parseheredoc();
28457429Smarkm				return ntop;
28599063Sdes			} else {
28676262Sgreen				tokpushback++;
28776262Sgreen			}
28876262Sgreen			checkkwd = CHKNL | CHKKWD | CHKALIAS;
28976262Sgreen			if (!nlflag && !erflag && tokendlist[peektoken()])
29076262Sgreen				return ntop;
29157429Smarkm			break;
29257429Smarkm		case TEOF:
29357429Smarkm			if (heredoclist)
29476262Sgreen				parseheredoc();
29557429Smarkm			else
29657429Smarkm				pungetc();		/* push back EOF on input */
29776262Sgreen			return ntop;
29876262Sgreen		default:
29976262Sgreen			if (nlflag || erflag)
30060576Skris				synexpect(-1);
30176262Sgreen			tokpushback++;
30276262Sgreen			return ntop;
30376262Sgreen		}
30476262Sgreen	}
30576262Sgreen}
30676262Sgreen
30776262Sgreen
30876262Sgreen
30976262Sgreenstatic union node *
31076262Sgreenandor(void)
31160576Skris{
31276262Sgreen	union node *n1, *n2, *n3;
31360576Skris	int t;
31460576Skris
31557429Smarkm	n1 = pipeline();
31657429Smarkm	for (;;) {
31757429Smarkm		if ((t = readtoken()) == TAND) {
31857429Smarkm			t = NAND;
31957429Smarkm		} else if (t == TOR) {
32057429Smarkm			t = NOR;
32157429Smarkm		} else {
32292559Sdes			tokpushback++;
32376262Sgreen			return n1;
32476262Sgreen		}
32557429Smarkm		n2 = pipeline();
326149753Sdes		n3 = (union node *)stalloc(sizeof (struct nbinary));
327149753Sdes		n3->type = t;
328137019Sdes		n3->nbinary.ch1 = n1;
32957429Smarkm		n3->nbinary.ch2 = n2;
33092559Sdes		n1 = n3;
33169591Sgreen	}
33269591Sgreen}
33376262Sgreen
334113911Sdes
33557429Smarkm
336113911Sdesstatic union node *
337113911Sdespipeline(void)
338113911Sdes{
339113911Sdes	union node *n1, *n2, *pipenode;
340113911Sdes	struct nodelist *lp, *prev;
341113911Sdes	int negate, t;
342137019Sdes
343147005Sdes	negate = 0;
344137019Sdes	checkkwd = CHKNL | CHKKWD | CHKALIAS;
345137019Sdes	TRACE(("pipeline: entered\n"));
346137019Sdes	while (readtoken() == TNOT)
347137019Sdes		negate = !negate;
34857429Smarkm	tokpushback++;
34957429Smarkm	n1 = command();
35092559Sdes	if (readtoken() == TPIPE) {
35157429Smarkm		pipenode = (union node *)stalloc(sizeof (struct npipe));
352149753Sdes		pipenode->type = NPIPE;
35357429Smarkm		pipenode->npipe.backgnd = 0;
35476262Sgreen		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
35557429Smarkm		pipenode->npipe.cmdlist = lp;
35657429Smarkm		lp->n = n1;
35776262Sgreen		do {
35857429Smarkm			prev = lp;
35957429Smarkm			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
36076262Sgreen			checkkwd = CHKNL | CHKKWD | CHKALIAS;
36176262Sgreen			t = readtoken();
36292559Sdes			tokpushback++;
36357429Smarkm			if (t == TNOT)
36476262Sgreen				lp->n = pipeline();
36576262Sgreen			else
36657429Smarkm				lp->n = command();
36757429Smarkm			prev->next = lp;
36857429Smarkm		} while (readtoken() == TPIPE);
36957429Smarkm		lp->next = NULL;
37057429Smarkm		n1 = pipenode;
37176262Sgreen	}
37276262Sgreen	tokpushback++;
37392559Sdes	if (negate) {
37457429Smarkm		n2 = (union node *)stalloc(sizeof (struct nnot));
37576262Sgreen		n2->type = NNOT;
37676262Sgreen		n2->nnot.com = n1;
37757429Smarkm		return n2;
37876262Sgreen	} else
37957429Smarkm		return n1;
38057429Smarkm}
38157429Smarkm
38257429Smarkm
38357429Smarkm
38499063Sdesstatic union node *
38576262Sgreencommand(void)
38676262Sgreen{
38776262Sgreen	union node *n1, *n2;
38876262Sgreen	union node *ap, **app;
38976262Sgreen	union node *cp, **cpp;
39076262Sgreen	union node *redir, **rpp;
39157429Smarkm	int t;
39257429Smarkm	int is_subshell;
39357429Smarkm
39457429Smarkm	checkkwd = CHKNL | CHKKWD | CHKALIAS;
39569591Sgreen	is_subshell = 0;
39669591Sgreen	redir = NULL;
39769591Sgreen	n1 = NULL;
39869591Sgreen	rpp = &redir;
39957429Smarkm
40057429Smarkm	/* Check for redirection which may precede command */
40157429Smarkm	while (readtoken() == TREDIR) {
40257429Smarkm		*rpp = n2 = redirnode;
40357429Smarkm		rpp = &n2->nfile.next;
40492559Sdes		parsefname();
40557429Smarkm	}
40657429Smarkm	tokpushback++;
40792559Sdes
40892559Sdes	switch (readtoken()) {
40992559Sdes	case TIF:
41092559Sdes		n1 = (union node *)stalloc(sizeof (struct nif));
41192559Sdes		n1->type = NIF;
41269591Sgreen		if ((n1->nif.test = list(0, 0)) == NULL)
41357429Smarkm			synexpect(-1);
41457429Smarkm		if (readtoken() != TTHEN)
41557429Smarkm			synexpect(TTHEN);
41657429Smarkm		n1->nif.ifpart = list(0, 0);
41757429Smarkm		n2 = n1;
41857429Smarkm		while (readtoken() == TELIF) {
41957429Smarkm			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
42076262Sgreen			n2 = n2->nif.elsepart;
42176262Sgreen			n2->type = NIF;
42257429Smarkm			if ((n2->nif.test = list(0, 0)) == NULL)
42357429Smarkm				synexpect(-1);
42476262Sgreen			if (readtoken() != TTHEN)
42557429Smarkm				synexpect(TTHEN);
42657429Smarkm			n2->nif.ifpart = list(0, 0);
42776262Sgreen		}
42876262Sgreen		if (lasttoken == TELSE)
42976262Sgreen			n2->nif.elsepart = list(0, 0);
43076262Sgreen		else {
43176262Sgreen			n2->nif.elsepart = NULL;
43257429Smarkm			tokpushback++;
43376262Sgreen		}
43492559Sdes		if (readtoken() != TFI)
43557429Smarkm			synexpect(TFI);
43657429Smarkm		checkkwd = CHKKWD | CHKALIAS;
437113911Sdes		break;
438113911Sdes	case TWHILE:
439113911Sdes	case TUNTIL: {
440113911Sdes		int got;
441113911Sdes		n1 = (union node *)stalloc(sizeof (struct nbinary));
442113911Sdes		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
44376262Sgreen		if ((n1->nbinary.ch1 = list(0, 0)) == NULL)
44476262Sgreen			synexpect(-1);
44557429Smarkm		if ((got=readtoken()) != TDO) {
44676262SgreenTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
44776262Sgreen			synexpect(TDO);
44876262Sgreen		}
44976262Sgreen		n1->nbinary.ch2 = list(0, 0);
45076262Sgreen		if (readtoken() != TDONE)
45176262Sgreen			synexpect(TDONE);
45257429Smarkm		checkkwd = CHKKWD | CHKALIAS;
45360576Skris		break;
45498684Sdes	}
45576262Sgreen	case TFOR:
45676262Sgreen		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
45760576Skris			synerror("Bad for loop variable");
45860576Skris		n1 = (union node *)stalloc(sizeof (struct nfor));
45976262Sgreen		n1->type = NFOR;
46076262Sgreen		n1->nfor.var = wordtext;
46176262Sgreen		while (readtoken() == TNL)
46260576Skris			;
46360576Skris		if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
46460576Skris			app = &ap;
46576262Sgreen			while (readtoken() == TWORD) {
46676262Sgreen				n2 = (union node *)stalloc(sizeof (struct narg));
46776262Sgreen				n2->type = NARG;
46860576Skris				n2->narg.text = wordtext;
46976262Sgreen				n2->narg.backquote = backquotelist;
47076262Sgreen				*app = n2;
47176262Sgreen				app = &n2->narg.next;
47276262Sgreen			}
47376262Sgreen			*app = NULL;
47492559Sdes			n1->nfor.args = ap;
47576262Sgreen			if (lasttoken != TNL && lasttoken != TSEMI)
47676262Sgreen				synexpect(-1);
47776262Sgreen		} else {
47876262Sgreen			static char argvars[5] = {
47976262Sgreen				CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
48076262Sgreen			};
48176262Sgreen			n2 = (union node *)stalloc(sizeof (struct narg));
482113911Sdes			n2->type = NARG;
483113911Sdes			n2->narg.text = argvars;
484113911Sdes			n2->narg.backquote = NULL;
485113911Sdes			n2->narg.next = NULL;
486113911Sdes			n1->nfor.args = n2;
48776262Sgreen			/*
48892559Sdes			 * Newline or semicolon here is optional (but note
48976262Sgreen			 * that the original Bourne shell only allowed NL).
49076262Sgreen			 */
49176262Sgreen			if (lasttoken != TNL && lasttoken != TSEMI)
49276262Sgreen				tokpushback++;
49376262Sgreen		}
49476262Sgreen		checkkwd = CHKNL | CHKKWD | CHKALIAS;
49576262Sgreen		if ((t = readtoken()) == TDO)
49660576Skris			t = TDONE;
49776262Sgreen		else if (t == TBEGIN)
49876262Sgreen			t = TEND;
49960576Skris		else
50060576Skris			synexpect(-1);
50176262Sgreen		n1->nfor.body = list(0, 0);
50276262Sgreen		if (readtoken() != t)
50376262Sgreen			synexpect(t);
50476262Sgreen		checkkwd = CHKKWD | CHKALIAS;
50576262Sgreen		break;
50676262Sgreen	case TCASE:
50776262Sgreen		n1 = (union node *)stalloc(sizeof (struct ncase));
50860576Skris		n1->type = NCASE;
50960576Skris		if (readtoken() != TWORD)
51092559Sdes			synexpect(TWORD);
51176262Sgreen		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
51260576Skris		n2->type = NARG;
51360576Skris		n2->narg.text = wordtext;
51460576Skris		n2->narg.backquote = backquotelist;
51592559Sdes		n2->narg.next = NULL;
51692559Sdes		while (readtoken() == TNL);
51792559Sdes		if (lasttoken != TWORD || ! equal(wordtext, "in"))
51892559Sdes			synerror("expecting \"in\"");
51992559Sdes		cpp = &n1->ncase.cases;
52092559Sdes		checkkwd = CHKNL | CHKKWD, readtoken();
52192559Sdes		while (lasttoken != TESAC) {
52298941Sdes			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
52398941Sdes			cp->type = NCLIST;
52498941Sdes			app = &cp->nclist.pattern;
52592559Sdes			if (lasttoken == TLP)
52660576Skris				readtoken();
52760576Skris			for (;;) {
52860576Skris				*app = ap = (union node *)stalloc(sizeof (struct narg));
52992559Sdes				ap->type = NARG;
530124211Sdes				ap->narg.text = wordtext;
53160576Skris				ap->narg.backquote = backquotelist;
53276262Sgreen				checkkwd = CHKNL | CHKKWD;
53360576Skris				if (readtoken() != TPIPE)
53460576Skris					break;
53576262Sgreen				app = &ap->narg.next;
53676262Sgreen				readtoken();
53776262Sgreen			}
53876262Sgreen			ap->narg.next = NULL;
53976262Sgreen			if (lasttoken != TRP)
54076262Sgreen				synexpect(TRP);
54176262Sgreen			cp->nclist.body = list(0, 0);
54276262Sgreen
54376262Sgreen			checkkwd = CHKNL | CHKKWD | CHKALIAS;
54476262Sgreen			if ((t = readtoken()) != TESAC) {
54576262Sgreen				if (t == TENDCASE)
54676262Sgreen					;
54776262Sgreen				else if (t == TFALLTHRU)
54876262Sgreen					cp->type = NCLISTFALLTHRU;
54976262Sgreen				else
55076262Sgreen					synexpect(TENDCASE);
55176262Sgreen				checkkwd = CHKNL | CHKKWD, readtoken();
55276262Sgreen			}
55376262Sgreen			cpp = &cp->nclist.next;
55476262Sgreen		}
55576262Sgreen		*cpp = NULL;
55676262Sgreen		checkkwd = CHKKWD | CHKALIAS;
55776262Sgreen		break;
55876262Sgreen	case TLP:
55960576Skris		n1 = (union node *)stalloc(sizeof (struct nredir));
56076262Sgreen		n1->type = NSUBSHELL;
56176262Sgreen		n1->nredir.n = list(0, 0);
56276262Sgreen		n1->nredir.redirect = NULL;
56360576Skris		if (readtoken() != TRP)
56460576Skris			synexpect(TRP);
56576262Sgreen		checkkwd = CHKKWD | CHKALIAS;
56660576Skris		is_subshell = 1;
56760576Skris		break;
56876262Sgreen	case TBEGIN:
56960576Skris		n1 = list(0, 0);
57065674Skris		if (readtoken() != TEND)
57176262Sgreen			synexpect(TEND);
57276262Sgreen		checkkwd = CHKKWD | CHKALIAS;
57376262Sgreen		break;
57476262Sgreen	/* Handle an empty command like other simple commands.  */
57592559Sdes	case TBACKGND:
57676262Sgreen	case TSEMI:
57776262Sgreen	case TAND:
57876262Sgreen	case TOR:
57976262Sgreen		/*
58076262Sgreen		 * An empty command before a ; doesn't make much sense, and
58176262Sgreen		 * should certainly be disallowed in the case of `if ;'.
58276262Sgreen		 */
58376262Sgreen		if (!redir)
58476262Sgreen			synexpect(-1);
58576262Sgreen	case TNL:
58676262Sgreen	case TEOF:
58776262Sgreen	case TWORD:
58876262Sgreen	case TRP:
58976262Sgreen		tokpushback++;
59092559Sdes		n1 = simplecmd(rpp, redir);
59192559Sdes		return n1;
59292559Sdes	default:
59392559Sdes		synexpect(-1);
59476262Sgreen	}
59576262Sgreen
59676262Sgreen	/* Now check for redirection which may follow command */
59776262Sgreen	while (readtoken() == TREDIR) {
59892559Sdes		*rpp = n2 = redirnode;
59976262Sgreen		rpp = &n2->nfile.next;
60092559Sdes		parsefname();
60176262Sgreen	}
60276262Sgreen	tokpushback++;
60392559Sdes	*rpp = NULL;
60476262Sgreen	if (redir) {
60565674Skris		if (!is_subshell) {
60665674Skris			n2 = (union node *)stalloc(sizeof (struct nredir));
607147005Sdes			n2->type = NREDIR;
60865674Skris			n2->nredir.n = n1;
609147005Sdes			n1 = n2;
61065674Skris		}
61165674Skris		n1->nredir.redirect = redir;
61265674Skris	}
613147005Sdes
614147005Sdes	return n1;
61565674Skris}
61692559Sdes
61765674Skris
61865674Skrisstatic union node *
61965674Skrissimplecmd(union node **rpp, union node *redir)
62065674Skris{
62165674Skris	union node *args, **app;
62265674Skris	union node **orig_rpp = rpp;
62365674Skris	union node *n = NULL;
62465674Skris	int special;
62565674Skris	int savecheckkwd;
62676262Sgreen
62765674Skris	/* If we don't have any redirections already, then we must reset */
62865674Skris	/* rpp to be the address of the local redir variable.  */
62965674Skris	if (redir == 0)
63065674Skris		rpp = &redir;
63165674Skris
63265674Skris	args = NULL;
63365674Skris	app = &args;
63465674Skris	/*
63565674Skris	 * We save the incoming value, because we need this for shell
63665674Skris	 * functions.  There can not be a redirect or an argument between
63765674Skris	 * the function name and the open parenthesis.
63865674Skris	 */
63976262Sgreen	orig_rpp = rpp;
64076262Sgreen
64176262Sgreen	savecheckkwd = CHKALIAS;
64265674Skris
64376262Sgreen	for (;;) {
64476262Sgreen		checkkwd = savecheckkwd;
64565674Skris		if (readtoken() == TWORD) {
646124211Sdes			n = (union node *)stalloc(sizeof (struct narg));
64776262Sgreen			n->type = NARG;
64876262Sgreen			n->narg.text = wordtext;
64976262Sgreen			n->narg.backquote = backquotelist;
650124211Sdes			*app = n;
651124211Sdes			app = &n->narg.next;
652124211Sdes			if (savecheckkwd != 0 && !isassignment(wordtext))
653124211Sdes				savecheckkwd = 0;
654124211Sdes		} else if (lasttoken == TREDIR) {
655124211Sdes			*rpp = n = redirnode;
656124211Sdes			rpp = &n->nfile.next;
657124211Sdes			parsefname();	/* read name of redirection file */
65876262Sgreen		} else if (lasttoken == TLP && app == &args->narg.next
65976262Sgreen					    && rpp == orig_rpp) {
66076262Sgreen			/* We have a function */
66176262Sgreen			if (readtoken() != TRP)
66276262Sgreen				synexpect(TRP);
66376262Sgreen			funclinno = plinno;
66476262Sgreen			/*
66576262Sgreen			 * - Require plain text.
66676262Sgreen			 * - Functions with '/' cannot be called.
66765674Skris			 * - Reject name=().
668			 * - Reject ksh extended glob patterns.
669			 */
670			if (!noexpand(n->narg.text) || quoteflag ||
671			    strchr(n->narg.text, '/') ||
672			    strchr("!%*+-=?@}~",
673				n->narg.text[strlen(n->narg.text) - 1]))
674				synerror("Bad function name");
675			rmescapes(n->narg.text);
676			if (find_builtin(n->narg.text, &special) >= 0 &&
677			    special)
678				synerror("Cannot override a special builtin with a function");
679			n->type = NDEFUN;
680			n->narg.next = command();
681			funclinno = 0;
682			return n;
683		} else {
684			tokpushback++;
685			break;
686		}
687	}
688	*app = NULL;
689	*rpp = NULL;
690	n = (union node *)stalloc(sizeof (struct ncmd));
691	n->type = NCMD;
692	n->ncmd.args = args;
693	n->ncmd.redirect = redir;
694	return n;
695}
696
697static union node *
698makename(void)
699{
700	union node *n;
701
702	n = (union node *)stalloc(sizeof (struct narg));
703	n->type = NARG;
704	n->narg.next = NULL;
705	n->narg.text = wordtext;
706	n->narg.backquote = backquotelist;
707	return n;
708}
709
710void
711fixredir(union node *n, const char *text, int err)
712{
713	TRACE(("Fix redir %s %d\n", text, err));
714	if (!err)
715		n->ndup.vname = NULL;
716
717	if (is_digit(text[0]) && text[1] == '\0')
718		n->ndup.dupfd = digit_val(text[0]);
719	else if (text[0] == '-' && text[1] == '\0')
720		n->ndup.dupfd = -1;
721	else {
722
723		if (err)
724			synerror("Bad fd number");
725		else
726			n->ndup.vname = makename();
727	}
728}
729
730
731static void
732parsefname(void)
733{
734	union node *n = redirnode;
735
736	if (readtoken() != TWORD)
737		synexpect(-1);
738	if (n->type == NHERE) {
739		struct heredoc *here = heredoc;
740		struct heredoc *p;
741		int i;
742
743		if (quoteflag == 0)
744			n->type = NXHERE;
745		TRACE(("Here document %d\n", n->type));
746		if (here->striptabs) {
747			while (*wordtext == '\t')
748				wordtext++;
749		}
750		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
751			synerror("Illegal eof marker for << redirection");
752		rmescapes(wordtext);
753		here->eofmark = wordtext;
754		here->next = NULL;
755		if (heredoclist == NULL)
756			heredoclist = here;
757		else {
758			for (p = heredoclist ; p->next ; p = p->next);
759			p->next = here;
760		}
761	} else if (n->type == NTOFD || n->type == NFROMFD) {
762		fixredir(n, wordtext, 0);
763	} else {
764		n->nfile.fname = makename();
765	}
766}
767
768
769/*
770 * Input any here documents.
771 */
772
773static void
774parseheredoc(void)
775{
776	struct heredoc *here;
777	union node *n;
778
779	while (heredoclist) {
780		here = heredoclist;
781		heredoclist = here->next;
782		if (needprompt) {
783			setprompt(2);
784			needprompt = 0;
785		}
786		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
787				here->eofmark, here->striptabs);
788		n = (union node *)stalloc(sizeof (struct narg));
789		n->narg.type = NARG;
790		n->narg.next = NULL;
791		n->narg.text = wordtext;
792		n->narg.backquote = backquotelist;
793		here->here->nhere.doc = n;
794	}
795}
796
797static int
798peektoken(void)
799{
800	int t;
801
802	t = readtoken();
803	tokpushback++;
804	return (t);
805}
806
807static int
808readtoken(void)
809{
810	int t;
811	struct alias *ap;
812#ifdef DEBUG
813	int alreadyseen = tokpushback;
814#endif
815
816	top:
817	t = xxreadtoken();
818
819	/*
820	 * eat newlines
821	 */
822	if (checkkwd & CHKNL) {
823		while (t == TNL) {
824			parseheredoc();
825			t = xxreadtoken();
826		}
827	}
828
829	/*
830	 * check for keywords and aliases
831	 */
832	if (t == TWORD && !quoteflag)
833	{
834		const char * const *pp;
835
836		if (checkkwd & CHKKWD)
837			for (pp = parsekwd; *pp; pp++) {
838				if (**pp == *wordtext && equal(*pp, wordtext))
839				{
840					lasttoken = t = pp - parsekwd + KWDOFFSET;
841					TRACE(("keyword %s recognized\n", tokname[t]));
842					goto out;
843				}
844			}
845		if (checkkwd & CHKALIAS &&
846		    (ap = lookupalias(wordtext, 1)) != NULL) {
847			pushstring(ap->val, strlen(ap->val), ap);
848			goto top;
849		}
850	}
851out:
852	if (t != TNOT)
853		checkkwd = 0;
854
855#ifdef DEBUG
856	if (!alreadyseen)
857	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
858	else
859	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
860#endif
861	return (t);
862}
863
864
865/*
866 * Read the next input token.
867 * If the token is a word, we set backquotelist to the list of cmds in
868 *	backquotes.  We set quoteflag to true if any part of the word was
869 *	quoted.
870 * If the token is TREDIR, then we set redirnode to a structure containing
871 *	the redirection.
872 * In all cases, the variable startlinno is set to the number of the line
873 *	on which the token starts.
874 *
875 * [Change comment:  here documents and internal procedures]
876 * [Readtoken shouldn't have any arguments.  Perhaps we should make the
877 *  word parsing code into a separate routine.  In this case, readtoken
878 *  doesn't need to have any internal procedures, but parseword does.
879 *  We could also make parseoperator in essence the main routine, and
880 *  have parseword (readtoken1?) handle both words and redirection.]
881 */
882
883#define RETURN(token)	return lasttoken = token
884
885static int
886xxreadtoken(void)
887{
888	int c;
889
890	if (tokpushback) {
891		tokpushback = 0;
892		return lasttoken;
893	}
894	if (needprompt) {
895		setprompt(2);
896		needprompt = 0;
897	}
898	startlinno = plinno;
899	for (;;) {	/* until token or start of word found */
900		c = pgetc_macro();
901		switch (c) {
902		case ' ': case '\t':
903			continue;
904		case '#':
905			while ((c = pgetc()) != '\n' && c != PEOF);
906			pungetc();
907			continue;
908		case '\\':
909			if (pgetc() == '\n') {
910				startlinno = ++plinno;
911				if (doprompt)
912					setprompt(2);
913				else
914					setprompt(0);
915				continue;
916			}
917			pungetc();
918			goto breakloop;
919		case '\n':
920			plinno++;
921			needprompt = doprompt;
922			RETURN(TNL);
923		case PEOF:
924			RETURN(TEOF);
925		case '&':
926			if (pgetc() == '&')
927				RETURN(TAND);
928			pungetc();
929			RETURN(TBACKGND);
930		case '|':
931			if (pgetc() == '|')
932				RETURN(TOR);
933			pungetc();
934			RETURN(TPIPE);
935		case ';':
936			c = pgetc();
937			if (c == ';')
938				RETURN(TENDCASE);
939			else if (c == '&')
940				RETURN(TFALLTHRU);
941			pungetc();
942			RETURN(TSEMI);
943		case '(':
944			RETURN(TLP);
945		case ')':
946			RETURN(TRP);
947		default:
948			goto breakloop;
949		}
950	}
951breakloop:
952	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
953#undef RETURN
954}
955
956
957#define MAXNEST_static 8
958struct tokenstate
959{
960	const char *syntax; /* *SYNTAX */
961	int parenlevel; /* levels of parentheses in arithmetic */
962	enum tokenstate_category
963	{
964		TSTATE_TOP,
965		TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
966		TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
967		TSTATE_ARITH
968	} category;
969};
970
971
972/*
973 * Called to parse command substitutions.
974 */
975
976static char *
977parsebackq(char *out, struct nodelist **pbqlist,
978		int oldstyle, int dblquote, int quoted)
979{
980	struct nodelist **nlpp;
981	union node *n;
982	char *volatile str;
983	struct jmploc jmploc;
984	struct jmploc *const savehandler = handler;
985	int savelen;
986	int saveprompt;
987	const int bq_startlinno = plinno;
988	char *volatile ostr = NULL;
989	struct parsefile *const savetopfile = getcurrentfile();
990	struct heredoc *const saveheredoclist = heredoclist;
991	struct heredoc *here;
992
993	str = NULL;
994	if (setjmp(jmploc.loc)) {
995		popfilesupto(savetopfile);
996		if (str)
997			ckfree(str);
998		if (ostr)
999			ckfree(ostr);
1000		heredoclist = saveheredoclist;
1001		handler = savehandler;
1002		if (exception == EXERROR) {
1003			startlinno = bq_startlinno;
1004			synerror("Error in command substitution");
1005		}
1006		longjmp(handler->loc, 1);
1007	}
1008	INTOFF;
1009	savelen = out - stackblock();
1010	if (savelen > 0) {
1011		str = ckmalloc(savelen);
1012		memcpy(str, stackblock(), savelen);
1013	}
1014	handler = &jmploc;
1015	heredoclist = NULL;
1016	INTON;
1017        if (oldstyle) {
1018                /* We must read until the closing backquote, giving special
1019                   treatment to some slashes, and then push the string and
1020                   reread it as input, interpreting it normally.  */
1021                char *oout;
1022                int c;
1023                int olen;
1024
1025
1026                STARTSTACKSTR(oout);
1027		for (;;) {
1028			if (needprompt) {
1029				setprompt(2);
1030				needprompt = 0;
1031			}
1032			CHECKSTRSPACE(2, oout);
1033			switch (c = pgetc()) {
1034			case '`':
1035				goto done;
1036
1037			case '\\':
1038                                if ((c = pgetc()) == '\n') {
1039					plinno++;
1040					if (doprompt)
1041						setprompt(2);
1042					else
1043						setprompt(0);
1044					/*
1045					 * If eating a newline, avoid putting
1046					 * the newline into the new character
1047					 * stream (via the USTPUTC after the
1048					 * switch).
1049					 */
1050					continue;
1051				}
1052                                if (c != '\\' && c != '`' && c != '$'
1053                                    && (!dblquote || c != '"'))
1054                                        USTPUTC('\\', oout);
1055				break;
1056
1057			case '\n':
1058				plinno++;
1059				needprompt = doprompt;
1060				break;
1061
1062			case PEOF:
1063			        startlinno = plinno;
1064				synerror("EOF in backquote substitution");
1065 				break;
1066
1067			default:
1068				break;
1069			}
1070			USTPUTC(c, oout);
1071                }
1072done:
1073                USTPUTC('\0', oout);
1074                olen = oout - stackblock();
1075		INTOFF;
1076		ostr = ckmalloc(olen);
1077		memcpy(ostr, stackblock(), olen);
1078		setinputstring(ostr, 1);
1079		INTON;
1080        }
1081	nlpp = pbqlist;
1082	while (*nlpp)
1083		nlpp = &(*nlpp)->next;
1084	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1085	(*nlpp)->next = NULL;
1086
1087	if (oldstyle) {
1088		saveprompt = doprompt;
1089		doprompt = 0;
1090	}
1091
1092	n = list(0, oldstyle);
1093
1094	if (oldstyle)
1095		doprompt = saveprompt;
1096	else {
1097		if (readtoken() != TRP)
1098			synexpect(TRP);
1099	}
1100
1101	(*nlpp)->n = n;
1102        if (oldstyle) {
1103		/*
1104		 * Start reading from old file again, ignoring any pushed back
1105		 * tokens left from the backquote parsing
1106		 */
1107                popfile();
1108		tokpushback = 0;
1109	}
1110	STARTSTACKSTR(out);
1111	CHECKSTRSPACE(savelen + 1, out);
1112	INTOFF;
1113	if (str) {
1114		memcpy(out, str, savelen);
1115		STADJUST(savelen, out);
1116		ckfree(str);
1117		str = NULL;
1118	}
1119	if (ostr) {
1120		ckfree(ostr);
1121		ostr = NULL;
1122	}
1123	here = saveheredoclist;
1124	if (here != NULL) {
1125		while (here->next != NULL)
1126			here = here->next;
1127		here->next = heredoclist;
1128		heredoclist = saveheredoclist;
1129	}
1130	handler = savehandler;
1131	INTON;
1132	if (quoted)
1133		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1134	else
1135		USTPUTC(CTLBACKQ, out);
1136	return out;
1137}
1138
1139
1140/*
1141 * Called to parse a backslash escape sequence inside $'...'.
1142 * The backslash has already been read.
1143 */
1144static char *
1145readcstyleesc(char *out)
1146{
1147	int c, v, i, n;
1148
1149	c = pgetc();
1150	switch (c) {
1151	case '\0':
1152		synerror("Unterminated quoted string");
1153	case '\n':
1154		plinno++;
1155		if (doprompt)
1156			setprompt(2);
1157		else
1158			setprompt(0);
1159		return out;
1160	case '\\':
1161	case '\'':
1162	case '"':
1163		v = c;
1164		break;
1165	case 'a': v = '\a'; break;
1166	case 'b': v = '\b'; break;
1167	case 'e': v = '\033'; break;
1168	case 'f': v = '\f'; break;
1169	case 'n': v = '\n'; break;
1170	case 'r': v = '\r'; break;
1171	case 't': v = '\t'; break;
1172	case 'v': v = '\v'; break;
1173	case 'x':
1174		  v = 0;
1175		  for (;;) {
1176			  c = pgetc();
1177			  if (c >= '0' && c <= '9')
1178				  v = (v << 4) + c - '0';
1179			  else if (c >= 'A' && c <= 'F')
1180				  v = (v << 4) + c - 'A' + 10;
1181			  else if (c >= 'a' && c <= 'f')
1182				  v = (v << 4) + c - 'a' + 10;
1183			  else
1184				  break;
1185		  }
1186		  pungetc();
1187		  break;
1188	case '0': case '1': case '2': case '3':
1189	case '4': case '5': case '6': case '7':
1190		  v = c - '0';
1191		  c = pgetc();
1192		  if (c >= '0' && c <= '7') {
1193			  v <<= 3;
1194			  v += c - '0';
1195			  c = pgetc();
1196			  if (c >= '0' && c <= '7') {
1197				  v <<= 3;
1198				  v += c - '0';
1199			  } else
1200				  pungetc();
1201		  } else
1202			  pungetc();
1203		  break;
1204	case 'c':
1205		  c = pgetc();
1206		  if (c < 0x3f || c > 0x7a || c == 0x60)
1207			  synerror("Bad escape sequence");
1208		  if (c == '\\' && pgetc() != '\\')
1209			  synerror("Bad escape sequence");
1210		  if (c == '?')
1211			  v = 127;
1212		  else
1213			  v = c & 0x1f;
1214		  break;
1215	case 'u':
1216	case 'U':
1217		  n = c == 'U' ? 8 : 4;
1218		  v = 0;
1219		  for (i = 0; i < n; i++) {
1220			  c = pgetc();
1221			  if (c >= '0' && c <= '9')
1222				  v = (v << 4) + c - '0';
1223			  else if (c >= 'A' && c <= 'F')
1224				  v = (v << 4) + c - 'A' + 10;
1225			  else if (c >= 'a' && c <= 'f')
1226				  v = (v << 4) + c - 'a' + 10;
1227			  else
1228				  synerror("Bad escape sequence");
1229		  }
1230		  if (v == 0 || (v >= 0xd800 && v <= 0xdfff))
1231			  synerror("Bad escape sequence");
1232		  /* We really need iconv here. */
1233		  if (initial_localeisutf8 && v > 127) {
1234			  CHECKSTRSPACE(4, out);
1235			  /*
1236			   * We cannot use wctomb() as the locale may have
1237			   * changed.
1238			   */
1239			  if (v <= 0x7ff) {
1240				  USTPUTC(0xc0 | v >> 6, out);
1241				  USTPUTC(0x80 | (v & 0x3f), out);
1242				  return out;
1243			  } else if (v <= 0xffff) {
1244				  USTPUTC(0xe0 | v >> 12, out);
1245				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1246				  USTPUTC(0x80 | (v & 0x3f), out);
1247				  return out;
1248			  } else if (v <= 0x10ffff) {
1249				  USTPUTC(0xf0 | v >> 18, out);
1250				  USTPUTC(0x80 | ((v >> 12) & 0x3f), out);
1251				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1252				  USTPUTC(0x80 | (v & 0x3f), out);
1253				  return out;
1254			  }
1255		  }
1256		  if (v > 127)
1257			  v = '?';
1258		  break;
1259	default:
1260		  synerror("Bad escape sequence");
1261	}
1262	v = (char)v;
1263	/*
1264	 * We can't handle NUL bytes.
1265	 * POSIX says we should skip till the closing quote.
1266	 */
1267	if (v == '\0') {
1268		while ((c = pgetc()) != '\'') {
1269			if (c == '\\')
1270				c = pgetc();
1271			if (c == PEOF)
1272				synerror("Unterminated quoted string");
1273		}
1274		pungetc();
1275		return out;
1276	}
1277	if (SQSYNTAX[v] == CCTL)
1278		USTPUTC(CTLESC, out);
1279	USTPUTC(v, out);
1280	return out;
1281}
1282
1283
1284/*
1285 * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
1286 * is not NULL, read a here document.  In the latter case, eofmark is the
1287 * word which marks the end of the document and striptabs is true if
1288 * leading tabs should be stripped from the document.  The argument firstc
1289 * is the first character of the input token or document.
1290 *
1291 * Because C does not have internal subroutines, I have simulated them
1292 * using goto's to implement the subroutine linkage.  The following macros
1293 * will run code that appears at the end of readtoken1.
1294 */
1295
1296#define CHECKEND()	{goto checkend; checkend_return:;}
1297#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
1298#define PARSESUB()	{goto parsesub; parsesub_return:;}
1299#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
1300
1301static int
1302readtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs)
1303{
1304	int c = firstc;
1305	char *out;
1306	int len;
1307	char line[EOFMARKLEN + 1];
1308	struct nodelist *bqlist;
1309	int quotef;
1310	int newvarnest;
1311	int level;
1312	int synentry;
1313	struct tokenstate state_static[MAXNEST_static];
1314	int maxnest = MAXNEST_static;
1315	struct tokenstate *state = state_static;
1316	int sqiscstyle = 0;
1317
1318	startlinno = plinno;
1319	quotef = 0;
1320	bqlist = NULL;
1321	newvarnest = 0;
1322	level = 0;
1323	state[level].syntax = initialsyntax;
1324	state[level].parenlevel = 0;
1325	state[level].category = TSTATE_TOP;
1326
1327	STARTSTACKSTR(out);
1328	loop: {	/* for each line, until end of word */
1329		CHECKEND();	/* set c to PEOF if at end of here document */
1330		for (;;) {	/* until end of line or end of word */
1331			CHECKSTRSPACE(4, out);	/* permit 4 calls to USTPUTC */
1332
1333			synentry = state[level].syntax[c];
1334
1335			switch(synentry) {
1336			case CNL:	/* '\n' */
1337				if (state[level].syntax == BASESYNTAX)
1338					goto endword;	/* exit outer loop */
1339				USTPUTC(c, out);
1340				plinno++;
1341				if (doprompt)
1342					setprompt(2);
1343				else
1344					setprompt(0);
1345				c = pgetc();
1346				goto loop;		/* continue outer loop */
1347			case CSBACK:
1348				if (sqiscstyle) {
1349					out = readcstyleesc(out);
1350					break;
1351				}
1352				/* FALLTHROUGH */
1353			case CWORD:
1354				USTPUTC(c, out);
1355				break;
1356			case CCTL:
1357				if (eofmark == NULL || initialsyntax != SQSYNTAX)
1358					USTPUTC(CTLESC, out);
1359				USTPUTC(c, out);
1360				break;
1361			case CBACK:	/* backslash */
1362				c = pgetc();
1363				if (c == PEOF) {
1364					USTPUTC('\\', out);
1365					pungetc();
1366				} else if (c == '\n') {
1367					plinno++;
1368					if (doprompt)
1369						setprompt(2);
1370					else
1371						setprompt(0);
1372				} else {
1373					if (state[level].syntax == DQSYNTAX &&
1374					    c != '\\' && c != '`' && c != '$' &&
1375					    (c != '"' || (eofmark != NULL &&
1376						newvarnest == 0)) &&
1377					    (c != '}' || state[level].category != TSTATE_VAR_OLD))
1378						USTPUTC('\\', out);
1379					if ((eofmark == NULL ||
1380					    newvarnest > 0) &&
1381					    state[level].syntax == BASESYNTAX)
1382						USTPUTC(CTLQUOTEMARK, out);
1383					if (SQSYNTAX[c] == CCTL)
1384						USTPUTC(CTLESC, out);
1385					USTPUTC(c, out);
1386					if ((eofmark == NULL ||
1387					    newvarnest > 0) &&
1388					    state[level].syntax == BASESYNTAX &&
1389					    state[level].category == TSTATE_VAR_OLD)
1390						USTPUTC(CTLQUOTEEND, out);
1391					quotef++;
1392				}
1393				break;
1394			case CSQUOTE:
1395				USTPUTC(CTLQUOTEMARK, out);
1396				state[level].syntax = SQSYNTAX;
1397				sqiscstyle = 0;
1398				break;
1399			case CDQUOTE:
1400				USTPUTC(CTLQUOTEMARK, out);
1401				state[level].syntax = DQSYNTAX;
1402				break;
1403			case CENDQUOTE:
1404				if (eofmark != NULL && newvarnest == 0)
1405					USTPUTC(c, out);
1406				else {
1407					if (state[level].category == TSTATE_VAR_OLD)
1408						USTPUTC(CTLQUOTEEND, out);
1409					state[level].syntax = BASESYNTAX;
1410					quotef++;
1411				}
1412				break;
1413			case CVAR:	/* '$' */
1414				PARSESUB();		/* parse substitution */
1415				break;
1416			case CENDVAR:	/* '}' */
1417				if (level > 0 &&
1418				    ((state[level].category == TSTATE_VAR_OLD &&
1419				      state[level].syntax ==
1420				      state[level - 1].syntax) ||
1421				    (state[level].category == TSTATE_VAR_NEW &&
1422				     state[level].syntax == BASESYNTAX))) {
1423					if (state[level].category == TSTATE_VAR_NEW)
1424						newvarnest--;
1425					level--;
1426					USTPUTC(CTLENDVAR, out);
1427				} else {
1428					USTPUTC(c, out);
1429				}
1430				break;
1431			case CLP:	/* '(' in arithmetic */
1432				state[level].parenlevel++;
1433				USTPUTC(c, out);
1434				break;
1435			case CRP:	/* ')' in arithmetic */
1436				if (state[level].parenlevel > 0) {
1437					USTPUTC(c, out);
1438					--state[level].parenlevel;
1439				} else {
1440					if (pgetc() == ')') {
1441						if (level > 0 &&
1442						    state[level].category == TSTATE_ARITH) {
1443							level--;
1444							USTPUTC(CTLENDARI, out);
1445						} else
1446							USTPUTC(')', out);
1447					} else {
1448						/*
1449						 * unbalanced parens
1450						 *  (don't 2nd guess - no error)
1451						 */
1452						pungetc();
1453						USTPUTC(')', out);
1454					}
1455				}
1456				break;
1457			case CBQUOTE:	/* '`' */
1458				out = parsebackq(out, &bqlist, 1,
1459				    state[level].syntax == DQSYNTAX &&
1460				    (eofmark == NULL || newvarnest > 0),
1461				    state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
1462				break;
1463			case CEOF:
1464				goto endword;		/* exit outer loop */
1465			case CIGN:
1466				break;
1467			default:
1468				if (level == 0)
1469					goto endword;	/* exit outer loop */
1470				USTPUTC(c, out);
1471			}
1472			c = pgetc_macro();
1473		}
1474	}
1475endword:
1476	if (state[level].syntax == ARISYNTAX)
1477		synerror("Missing '))'");
1478	if (state[level].syntax != BASESYNTAX && eofmark == NULL)
1479		synerror("Unterminated quoted string");
1480	if (state[level].category == TSTATE_VAR_OLD ||
1481	    state[level].category == TSTATE_VAR_NEW) {
1482		startlinno = plinno;
1483		synerror("Missing '}'");
1484	}
1485	if (state != state_static)
1486		parser_temp_free_upto(state);
1487	USTPUTC('\0', out);
1488	len = out - stackblock();
1489	out = stackblock();
1490	if (eofmark == NULL) {
1491		if ((c == '>' || c == '<')
1492		 && quotef == 0
1493		 && len <= 2
1494		 && (*out == '\0' || is_digit(*out))) {
1495			PARSEREDIR();
1496			return lasttoken = TREDIR;
1497		} else {
1498			pungetc();
1499		}
1500	}
1501	quoteflag = quotef;
1502	backquotelist = bqlist;
1503	grabstackblock(len);
1504	wordtext = out;
1505	return lasttoken = TWORD;
1506/* end of readtoken routine */
1507
1508
1509/*
1510 * Check to see whether we are at the end of the here document.  When this
1511 * is called, c is set to the first character of the next input line.  If
1512 * we are at the end of the here document, this routine sets the c to PEOF.
1513 */
1514
1515checkend: {
1516	if (eofmark) {
1517		if (striptabs) {
1518			while (c == '\t')
1519				c = pgetc();
1520		}
1521		if (c == *eofmark) {
1522			if (pfgets(line, sizeof line) != NULL) {
1523				char *p, *q;
1524
1525				p = line;
1526				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1527				if ((*p == '\0' || *p == '\n') && *q == '\0') {
1528					c = PEOF;
1529					if (*p == '\n') {
1530						plinno++;
1531						needprompt = doprompt;
1532					}
1533				} else {
1534					pushstring(line, strlen(line), NULL);
1535				}
1536			}
1537		}
1538	}
1539	goto checkend_return;
1540}
1541
1542
1543/*
1544 * Parse a redirection operator.  The variable "out" points to a string
1545 * specifying the fd to be redirected.  The variable "c" contains the
1546 * first character of the redirection operator.
1547 */
1548
1549parseredir: {
1550	char fd = *out;
1551	union node *np;
1552
1553	np = (union node *)stalloc(sizeof (struct nfile));
1554	if (c == '>') {
1555		np->nfile.fd = 1;
1556		c = pgetc();
1557		if (c == '>')
1558			np->type = NAPPEND;
1559		else if (c == '&')
1560			np->type = NTOFD;
1561		else if (c == '|')
1562			np->type = NCLOBBER;
1563		else {
1564			np->type = NTO;
1565			pungetc();
1566		}
1567	} else {	/* c == '<' */
1568		np->nfile.fd = 0;
1569		c = pgetc();
1570		if (c == '<') {
1571			if (sizeof (struct nfile) != sizeof (struct nhere)) {
1572				np = (union node *)stalloc(sizeof (struct nhere));
1573				np->nfile.fd = 0;
1574			}
1575			np->type = NHERE;
1576			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1577			heredoc->here = np;
1578			if ((c = pgetc()) == '-') {
1579				heredoc->striptabs = 1;
1580			} else {
1581				heredoc->striptabs = 0;
1582				pungetc();
1583			}
1584		} else if (c == '&')
1585			np->type = NFROMFD;
1586		else if (c == '>')
1587			np->type = NFROMTO;
1588		else {
1589			np->type = NFROM;
1590			pungetc();
1591		}
1592	}
1593	if (fd != '\0')
1594		np->nfile.fd = digit_val(fd);
1595	redirnode = np;
1596	goto parseredir_return;
1597}
1598
1599
1600/*
1601 * Parse a substitution.  At this point, we have read the dollar sign
1602 * and nothing else.
1603 */
1604
1605parsesub: {
1606	char buf[10];
1607	int subtype;
1608	int typeloc;
1609	int flags;
1610	char *p;
1611	static const char types[] = "}-+?=";
1612	int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1613	int linno;
1614	int length;
1615	int c1;
1616
1617	c = pgetc();
1618	if (c == '(') {	/* $(command) or $((arith)) */
1619		if (pgetc() == '(') {
1620			PARSEARITH();
1621		} else {
1622			pungetc();
1623			out = parsebackq(out, &bqlist, 0,
1624			    state[level].syntax == DQSYNTAX &&
1625			    (eofmark == NULL || newvarnest > 0),
1626			    state[level].syntax == DQSYNTAX ||
1627			    state[level].syntax == ARISYNTAX);
1628		}
1629	} else if (c == '{' || is_name(c) || is_special(c)) {
1630		USTPUTC(CTLVAR, out);
1631		typeloc = out - stackblock();
1632		USTPUTC(VSNORMAL, out);
1633		subtype = VSNORMAL;
1634		flags = 0;
1635		if (c == '{') {
1636			bracketed_name = 1;
1637			c = pgetc();
1638			subtype = 0;
1639		}
1640varname:
1641		if (!is_eof(c) && is_name(c)) {
1642			length = 0;
1643			do {
1644				STPUTC(c, out);
1645				c = pgetc();
1646				length++;
1647			} while (!is_eof(c) && is_in_name(c));
1648			if (length == 6 &&
1649			    strncmp(out - length, "LINENO", length) == 0) {
1650				/* Replace the variable name with the
1651				 * current line number. */
1652				linno = plinno;
1653				if (funclinno != 0)
1654					linno -= funclinno - 1;
1655				snprintf(buf, sizeof(buf), "%d", linno);
1656				STADJUST(-6, out);
1657				STPUTS(buf, out);
1658				flags |= VSLINENO;
1659			}
1660		} else if (is_digit(c)) {
1661			if (bracketed_name) {
1662				do {
1663					STPUTC(c, out);
1664					c = pgetc();
1665				} while (is_digit(c));
1666			} else {
1667				STPUTC(c, out);
1668				c = pgetc();
1669			}
1670		} else if (is_special(c)) {
1671			c1 = c;
1672			c = pgetc();
1673			if (subtype == 0 && c1 == '#') {
1674				subtype = VSLENGTH;
1675				if (strchr(types, c) == NULL && c != ':' &&
1676				    c != '#' && c != '%')
1677					goto varname;
1678				c1 = c;
1679				c = pgetc();
1680				if (c1 != '}' && c == '}') {
1681					pungetc();
1682					c = c1;
1683					goto varname;
1684				}
1685				pungetc();
1686				c = c1;
1687				c1 = '#';
1688				subtype = 0;
1689			}
1690			USTPUTC(c1, out);
1691		} else {
1692			subtype = VSERROR;
1693			if (c == '}')
1694				pungetc();
1695			else if (c == '\n' || c == PEOF)
1696				synerror("Unexpected end of line in substitution");
1697			else
1698				USTPUTC(c, out);
1699		}
1700		if (subtype == 0) {
1701			switch (c) {
1702			case ':':
1703				flags |= VSNUL;
1704				c = pgetc();
1705				/*FALLTHROUGH*/
1706			default:
1707				p = strchr(types, c);
1708				if (p == NULL) {
1709					if (c == '\n' || c == PEOF)
1710						synerror("Unexpected end of line in substitution");
1711					if (flags == VSNUL)
1712						STPUTC(':', out);
1713					STPUTC(c, out);
1714					subtype = VSERROR;
1715				} else
1716					subtype = p - types + VSNORMAL;
1717				break;
1718			case '%':
1719			case '#':
1720				{
1721					int cc = c;
1722					subtype = c == '#' ? VSTRIMLEFT :
1723							     VSTRIMRIGHT;
1724					c = pgetc();
1725					if (c == cc)
1726						subtype++;
1727					else
1728						pungetc();
1729					break;
1730				}
1731			}
1732		} else if (subtype != VSERROR) {
1733			if (subtype == VSLENGTH && c != '}')
1734				subtype = VSERROR;
1735			pungetc();
1736		}
1737		STPUTC('=', out);
1738		if (state[level].syntax == DQSYNTAX ||
1739		    state[level].syntax == ARISYNTAX)
1740			flags |= VSQUOTE;
1741		*(stackblock() + typeloc) = subtype | flags;
1742		if (subtype != VSNORMAL) {
1743			if (level + 1 >= maxnest) {
1744				maxnest *= 2;
1745				if (state == state_static) {
1746					state = parser_temp_alloc(
1747					    maxnest * sizeof(*state));
1748					memcpy(state, state_static,
1749					    MAXNEST_static * sizeof(*state));
1750				} else
1751					state = parser_temp_realloc(state,
1752					    maxnest * sizeof(*state));
1753			}
1754			level++;
1755			state[level].parenlevel = 0;
1756			if (subtype == VSMINUS || subtype == VSPLUS ||
1757			    subtype == VSQUESTION || subtype == VSASSIGN) {
1758				/*
1759				 * For operators that were in the Bourne shell,
1760				 * inherit the double-quote state.
1761				 */
1762				state[level].syntax = state[level - 1].syntax;
1763				state[level].category = TSTATE_VAR_OLD;
1764			} else {
1765				/*
1766				 * The other operators take a pattern,
1767				 * so go to BASESYNTAX.
1768				 * Also, ' and " are now special, even
1769				 * in here documents.
1770				 */
1771				state[level].syntax = BASESYNTAX;
1772				state[level].category = TSTATE_VAR_NEW;
1773				newvarnest++;
1774			}
1775		}
1776	} else if (c == '\'' && state[level].syntax == BASESYNTAX) {
1777		/* $'cstylequotes' */
1778		USTPUTC(CTLQUOTEMARK, out);
1779		state[level].syntax = SQSYNTAX;
1780		sqiscstyle = 1;
1781	} else {
1782		USTPUTC('$', out);
1783		pungetc();
1784	}
1785	goto parsesub_return;
1786}
1787
1788
1789/*
1790 * Parse an arithmetic expansion (indicate start of one and set state)
1791 */
1792parsearith: {
1793
1794	if (level + 1 >= maxnest) {
1795		maxnest *= 2;
1796		if (state == state_static) {
1797			state = parser_temp_alloc(
1798			    maxnest * sizeof(*state));
1799			memcpy(state, state_static,
1800			    MAXNEST_static * sizeof(*state));
1801		} else
1802			state = parser_temp_realloc(state,
1803			    maxnest * sizeof(*state));
1804	}
1805	level++;
1806	state[level].syntax = ARISYNTAX;
1807	state[level].parenlevel = 0;
1808	state[level].category = TSTATE_ARITH;
1809	USTPUTC(CTLARI, out);
1810	if (state[level - 1].syntax == DQSYNTAX)
1811		USTPUTC('"',out);
1812	else
1813		USTPUTC(' ',out);
1814	goto parsearith_return;
1815}
1816
1817} /* end of readtoken */
1818
1819
1820
1821#ifdef mkinit
1822RESET {
1823	tokpushback = 0;
1824	checkkwd = 0;
1825}
1826#endif
1827
1828/*
1829 * Returns true if the text contains nothing to expand (no dollar signs
1830 * or backquotes).
1831 */
1832
1833static int
1834noexpand(char *text)
1835{
1836	char *p;
1837	char c;
1838
1839	p = text;
1840	while ((c = *p++) != '\0') {
1841		if ( c == CTLQUOTEMARK)
1842			continue;
1843		if (c == CTLESC)
1844			p++;
1845		else if (BASESYNTAX[(int)c] == CCTL)
1846			return 0;
1847	}
1848	return 1;
1849}
1850
1851
1852/*
1853 * Return true if the argument is a legal variable name (a letter or
1854 * underscore followed by zero or more letters, underscores, and digits).
1855 */
1856
1857int
1858goodname(const char *name)
1859{
1860	const char *p;
1861
1862	p = name;
1863	if (! is_name(*p))
1864		return 0;
1865	while (*++p) {
1866		if (! is_in_name(*p))
1867			return 0;
1868	}
1869	return 1;
1870}
1871
1872
1873int
1874isassignment(const char *p)
1875{
1876	if (!is_name(*p))
1877		return 0;
1878	p++;
1879	for (;;) {
1880		if (*p == '=')
1881			return 1;
1882		else if (!is_in_name(*p))
1883			return 0;
1884		p++;
1885	}
1886}
1887
1888
1889/*
1890 * Called when an unexpected token is read during the parse.  The argument
1891 * is the token that is expected, or -1 if more than one type of token can
1892 * occur at this point.
1893 */
1894
1895static void
1896synexpect(int token)
1897{
1898	char msg[64];
1899
1900	if (token >= 0) {
1901		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1902			tokname[lasttoken], tokname[token]);
1903	} else {
1904		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1905	}
1906	synerror(msg);
1907}
1908
1909
1910static void
1911synerror(const char *msg)
1912{
1913	if (commandname)
1914		outfmt(out2, "%s: %d: ", commandname, startlinno);
1915	outfmt(out2, "Syntax error: %s\n", msg);
1916	error((char *)NULL);
1917}
1918
1919static void
1920setprompt(int which)
1921{
1922	whichprompt = which;
1923
1924#ifndef NO_HISTORY
1925	if (!el)
1926#endif
1927	{
1928		out2str(getprompt(NULL));
1929		flushout(out2);
1930	}
1931}
1932
1933/*
1934 * called by editline -- any expansions to the prompt
1935 *    should be added here.
1936 */
1937char *
1938getprompt(void *unused __unused)
1939{
1940	static char ps[PROMPTLEN];
1941	char *fmt;
1942	const char *pwd;
1943	int i, trim;
1944	static char internal_error[] = "??";
1945
1946	/*
1947	 * Select prompt format.
1948	 */
1949	switch (whichprompt) {
1950	case 0:
1951		fmt = nullstr;
1952		break;
1953	case 1:
1954		fmt = ps1val();
1955		break;
1956	case 2:
1957		fmt = ps2val();
1958		break;
1959	default:
1960		return internal_error;
1961	}
1962
1963	/*
1964	 * Format prompt string.
1965	 */
1966	for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1967		if (*fmt == '\\')
1968			switch (*++fmt) {
1969
1970				/*
1971				 * Hostname.
1972				 *
1973				 * \h specifies just the local hostname,
1974				 * \H specifies fully-qualified hostname.
1975				 */
1976			case 'h':
1977			case 'H':
1978				ps[i] = '\0';
1979				gethostname(&ps[i], PROMPTLEN - i);
1980				/* Skip to end of hostname. */
1981				trim = (*fmt == 'h') ? '.' : '\0';
1982				while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1983					i++;
1984				break;
1985
1986				/*
1987				 * Working directory.
1988				 *
1989				 * \W specifies just the final component,
1990				 * \w specifies the entire path.
1991				 */
1992			case 'W':
1993			case 'w':
1994				pwd = lookupvar("PWD");
1995				if (pwd == NULL)
1996					pwd = "?";
1997				if (*fmt == 'W' &&
1998				    *pwd == '/' && pwd[1] != '\0')
1999					strlcpy(&ps[i], strrchr(pwd, '/') + 1,
2000					    PROMPTLEN - i);
2001				else
2002					strlcpy(&ps[i], pwd, PROMPTLEN - i);
2003				/* Skip to end of path. */
2004				while (ps[i + 1] != '\0')
2005					i++;
2006				break;
2007
2008				/*
2009				 * Superuser status.
2010				 *
2011				 * '$' for normal users, '#' for root.
2012				 */
2013			case '$':
2014				ps[i] = (geteuid() != 0) ? '$' : '#';
2015				break;
2016
2017				/*
2018				 * A literal \.
2019				 */
2020			case '\\':
2021				ps[i] = '\\';
2022				break;
2023
2024				/*
2025				 * Emit unrecognized formats verbatim.
2026				 */
2027			default:
2028				ps[i++] = '\\';
2029				ps[i] = *fmt;
2030				break;
2031			}
2032		else
2033			ps[i] = *fmt;
2034	ps[i] = '\0';
2035	return (ps);
2036}
2037
2038
2039const char *
2040expandstr(char *ps)
2041{
2042	union node n;
2043	struct jmploc jmploc;
2044	struct jmploc *const savehandler = handler;
2045	const int saveprompt = doprompt;
2046	struct parsefile *const savetopfile = getcurrentfile();
2047	struct parser_temp *const saveparser_temp = parser_temp;
2048	const char *result = NULL;
2049
2050	if (!setjmp(jmploc.loc)) {
2051		handler = &jmploc;
2052		parser_temp = NULL;
2053		setinputstring(ps, 1);
2054		doprompt = 0;
2055		readtoken1(pgetc(), DQSYNTAX, "\n\n", 0);
2056		if (backquotelist != NULL)
2057			error("Command substitution not allowed here");
2058
2059		n.narg.type = NARG;
2060		n.narg.next = NULL;
2061		n.narg.text = wordtext;
2062		n.narg.backquote = backquotelist;
2063
2064		expandarg(&n, NULL, 0);
2065		result = stackblock();
2066		INTOFF;
2067	}
2068	handler = savehandler;
2069	doprompt = saveprompt;
2070	popfilesupto(savetopfile);
2071	if (parser_temp != saveparser_temp) {
2072		parser_temp_free_all();
2073		parser_temp = saveparser_temp;
2074	}
2075	if (result != NULL) {
2076		INTON;
2077	} else if (exception == EXINT)
2078		raise(SIGINT);
2079	return result;
2080}
2081