parser.c revision 222907
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/sh/parser.c 222907 2011-06-09 23:12:23Z jilles $");
40
41#include <stdlib.h>
42#include <unistd.h>
43#include <stdio.h>
44
45#include "shell.h"
46#include "parser.h"
47#include "nodes.h"
48#include "expand.h"	/* defines rmescapes() */
49#include "syntax.h"
50#include "options.h"
51#include "input.h"
52#include "output.h"
53#include "var.h"
54#include "error.h"
55#include "memalloc.h"
56#include "mystring.h"
57#include "alias.h"
58#include "show.h"
59#include "eval.h"
60#include "exec.h"	/* to check for special builtins */
61#ifndef NO_HISTORY
62#include "myhistedit.h"
63#endif
64
65/*
66 * Shell command parser.
67 */
68
69#define	EOFMARKLEN	79
70#define	PROMPTLEN	128
71
72/* values of checkkwd variable */
73#define CHKALIAS	0x1
74#define CHKKWD		0x2
75#define CHKNL		0x4
76
77/* values returned by readtoken */
78#include "token.h"
79
80
81
82struct heredoc {
83	struct heredoc *next;	/* next here document in list */
84	union node *here;		/* redirection node */
85	char *eofmark;		/* string indicating end of input */
86	int striptabs;		/* if set, strip leading tabs */
87};
88
89struct parser_temp {
90	struct parser_temp *next;
91	void *data;
92};
93
94
95static struct heredoc *heredoclist;	/* list of here documents to read */
96static int doprompt;		/* if set, prompt the user */
97static int needprompt;		/* true if interactive and at start of line */
98static int lasttoken;		/* last token read */
99MKINIT int tokpushback;		/* last token pushed back */
100static char *wordtext;		/* text of last word returned by readtoken */
101MKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
102static struct nodelist *backquotelist;
103static union node *redirnode;
104static struct heredoc *heredoc;
105static int quoteflag;		/* set if (part of) last token was quoted */
106static int startlinno;		/* line # where last token started */
107static int funclinno;		/* line # where the current function started */
108static struct parser_temp *parser_temp;
109
110
111static union node *list(int, int);
112static union node *andor(void);
113static union node *pipeline(void);
114static union node *command(void);
115static union node *simplecmd(union node **, union node *);
116static union node *makename(void);
117static void parsefname(void);
118static void parseheredoc(void);
119static int peektoken(void);
120static int readtoken(void);
121static int xxreadtoken(void);
122static int readtoken1(int, char const *, char *, int);
123static int noexpand(char *);
124static void synexpect(int) __dead2;
125static void synerror(const char *) __dead2;
126static void setprompt(int);
127
128
129static void *
130parser_temp_alloc(size_t len)
131{
132	struct parser_temp *t;
133
134	INTOFF;
135	t = ckmalloc(sizeof(*t));
136	t->data = NULL;
137	t->next = parser_temp;
138	parser_temp = t;
139	t->data = ckmalloc(len);
140	INTON;
141	return t->data;
142}
143
144
145static void *
146parser_temp_realloc(void *ptr, size_t len)
147{
148	struct parser_temp *t;
149
150	INTOFF;
151	t = parser_temp;
152	if (ptr != t->data)
153		error("bug: parser_temp_realloc misused");
154	t->data = ckrealloc(t->data, len);
155	INTON;
156	return t->data;
157}
158
159
160static void
161parser_temp_free_upto(void *ptr)
162{
163	struct parser_temp *t;
164	int done = 0;
165
166	INTOFF;
167	while (parser_temp != NULL && !done) {
168		t = parser_temp;
169		parser_temp = t->next;
170		done = t->data == ptr;
171		ckfree(t->data);
172		ckfree(t);
173	}
174	INTON;
175	if (!done)
176		error("bug: parser_temp_free_upto misused");
177}
178
179
180static void
181parser_temp_free_all(void)
182{
183	struct parser_temp *t;
184
185	INTOFF;
186	while (parser_temp != NULL) {
187		t = parser_temp;
188		parser_temp = t->next;
189		ckfree(t->data);
190		ckfree(t);
191	}
192	INTON;
193}
194
195
196/*
197 * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
198 * valid parse tree indicating a blank line.)
199 */
200
201union node *
202parsecmd(int interact)
203{
204	int t;
205
206	/* This assumes the parser is not re-entered,
207	 * which could happen if we add command substitution on PS1/PS2.
208	 */
209	parser_temp_free_all();
210	heredoclist = NULL;
211
212	tokpushback = 0;
213	doprompt = interact;
214	if (doprompt)
215		setprompt(1);
216	else
217		setprompt(0);
218	needprompt = 0;
219	t = readtoken();
220	if (t == TEOF)
221		return NEOF;
222	if (t == TNL)
223		return NULL;
224	tokpushback++;
225	return list(1, 1);
226}
227
228
229static union node *
230list(int nlflag, int erflag)
231{
232	union node *ntop, *n1, *n2, *n3;
233	int tok;
234
235	checkkwd = CHKNL | CHKKWD | CHKALIAS;
236	if (!nlflag && !erflag && tokendlist[peektoken()])
237		return NULL;
238	ntop = n1 = NULL;
239	for (;;) {
240		n2 = andor();
241		tok = readtoken();
242		if (tok == TBACKGND) {
243			if (n2->type == NCMD || n2->type == NPIPE) {
244				n2->ncmd.backgnd = 1;
245			} else if (n2->type == NREDIR) {
246				n2->type = NBACKGND;
247			} else {
248				n3 = (union node *)stalloc(sizeof (struct nredir));
249				n3->type = NBACKGND;
250				n3->nredir.n = n2;
251				n3->nredir.redirect = NULL;
252				n2 = n3;
253			}
254		}
255		if (ntop == NULL)
256			ntop = n2;
257		else if (n1 == NULL) {
258			n1 = (union node *)stalloc(sizeof (struct nbinary));
259			n1->type = NSEMI;
260			n1->nbinary.ch1 = ntop;
261			n1->nbinary.ch2 = n2;
262			ntop = n1;
263		}
264		else {
265			n3 = (union node *)stalloc(sizeof (struct nbinary));
266			n3->type = NSEMI;
267			n3->nbinary.ch1 = n1->nbinary.ch2;
268			n3->nbinary.ch2 = n2;
269			n1->nbinary.ch2 = n3;
270			n1 = n3;
271		}
272		switch (tok) {
273		case TBACKGND:
274		case TSEMI:
275			tok = readtoken();
276			/* FALLTHROUGH */
277		case TNL:
278			if (tok == TNL) {
279				parseheredoc();
280				if (nlflag)
281					return ntop;
282			} else if (tok == TEOF && nlflag) {
283				parseheredoc();
284				return ntop;
285			} else {
286				tokpushback++;
287			}
288			checkkwd = CHKNL | CHKKWD | CHKALIAS;
289			if (!nlflag && !erflag && tokendlist[peektoken()])
290				return ntop;
291			break;
292		case TEOF:
293			if (heredoclist)
294				parseheredoc();
295			else
296				pungetc();		/* push back EOF on input */
297			return ntop;
298		default:
299			if (nlflag || erflag)
300				synexpect(-1);
301			tokpushback++;
302			return ntop;
303		}
304	}
305}
306
307
308
309static union node *
310andor(void)
311{
312	union node *n1, *n2, *n3;
313	int t;
314
315	n1 = pipeline();
316	for (;;) {
317		if ((t = readtoken()) == TAND) {
318			t = NAND;
319		} else if (t == TOR) {
320			t = NOR;
321		} else {
322			tokpushback++;
323			return n1;
324		}
325		n2 = pipeline();
326		n3 = (union node *)stalloc(sizeof (struct nbinary));
327		n3->type = t;
328		n3->nbinary.ch1 = n1;
329		n3->nbinary.ch2 = n2;
330		n1 = n3;
331	}
332}
333
334
335
336static union node *
337pipeline(void)
338{
339	union node *n1, *n2, *pipenode;
340	struct nodelist *lp, *prev;
341	int negate, t;
342
343	negate = 0;
344	checkkwd = CHKNL | CHKKWD | CHKALIAS;
345	TRACE(("pipeline: entered\n"));
346	while (readtoken() == TNOT)
347		negate = !negate;
348	tokpushback++;
349	n1 = command();
350	if (readtoken() == TPIPE) {
351		pipenode = (union node *)stalloc(sizeof (struct npipe));
352		pipenode->type = NPIPE;
353		pipenode->npipe.backgnd = 0;
354		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
355		pipenode->npipe.cmdlist = lp;
356		lp->n = n1;
357		do {
358			prev = lp;
359			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
360			checkkwd = CHKNL | CHKKWD | CHKALIAS;
361			t = readtoken();
362			tokpushback++;
363			if (t == TNOT)
364				lp->n = pipeline();
365			else
366				lp->n = command();
367			prev->next = lp;
368		} while (readtoken() == TPIPE);
369		lp->next = NULL;
370		n1 = pipenode;
371	}
372	tokpushback++;
373	if (negate) {
374		n2 = (union node *)stalloc(sizeof (struct nnot));
375		n2->type = NNOT;
376		n2->nnot.com = n1;
377		return n2;
378	} else
379		return n1;
380}
381
382
383
384static union node *
385command(void)
386{
387	union node *n1, *n2;
388	union node *ap, **app;
389	union node *cp, **cpp;
390	union node *redir, **rpp;
391	int t;
392	int is_subshell;
393
394	checkkwd = CHKNL | CHKKWD | CHKALIAS;
395	is_subshell = 0;
396	redir = NULL;
397	n1 = NULL;
398	rpp = &redir;
399
400	/* Check for redirection which may precede command */
401	while (readtoken() == TREDIR) {
402		*rpp = n2 = redirnode;
403		rpp = &n2->nfile.next;
404		parsefname();
405	}
406	tokpushback++;
407
408	switch (readtoken()) {
409	case TIF:
410		n1 = (union node *)stalloc(sizeof (struct nif));
411		n1->type = NIF;
412		if ((n1->nif.test = list(0, 0)) == NULL)
413			synexpect(-1);
414		if (readtoken() != TTHEN)
415			synexpect(TTHEN);
416		n1->nif.ifpart = list(0, 0);
417		n2 = n1;
418		while (readtoken() == TELIF) {
419			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
420			n2 = n2->nif.elsepart;
421			n2->type = NIF;
422			if ((n2->nif.test = list(0, 0)) == NULL)
423				synexpect(-1);
424			if (readtoken() != TTHEN)
425				synexpect(TTHEN);
426			n2->nif.ifpart = list(0, 0);
427		}
428		if (lasttoken == TELSE)
429			n2->nif.elsepart = list(0, 0);
430		else {
431			n2->nif.elsepart = NULL;
432			tokpushback++;
433		}
434		if (readtoken() != TFI)
435			synexpect(TFI);
436		checkkwd = CHKKWD | CHKALIAS;
437		break;
438	case TWHILE:
439	case TUNTIL: {
440		int got;
441		n1 = (union node *)stalloc(sizeof (struct nbinary));
442		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
443		if ((n1->nbinary.ch1 = list(0, 0)) == NULL)
444			synexpect(-1);
445		if ((got=readtoken()) != TDO) {
446TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
447			synexpect(TDO);
448		}
449		n1->nbinary.ch2 = list(0, 0);
450		if (readtoken() != TDONE)
451			synexpect(TDONE);
452		checkkwd = CHKKWD | CHKALIAS;
453		break;
454	}
455	case TFOR:
456		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
457			synerror("Bad for loop variable");
458		n1 = (union node *)stalloc(sizeof (struct nfor));
459		n1->type = NFOR;
460		n1->nfor.var = wordtext;
461		while (readtoken() == TNL)
462			;
463		if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
464			app = &ap;
465			while (readtoken() == TWORD) {
466				n2 = (union node *)stalloc(sizeof (struct narg));
467				n2->type = NARG;
468				n2->narg.text = wordtext;
469				n2->narg.backquote = backquotelist;
470				*app = n2;
471				app = &n2->narg.next;
472			}
473			*app = NULL;
474			n1->nfor.args = ap;
475			if (lasttoken != TNL && lasttoken != TSEMI)
476				synexpect(-1);
477		} else {
478			static char argvars[5] = {
479				CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
480			};
481			n2 = (union node *)stalloc(sizeof (struct narg));
482			n2->type = NARG;
483			n2->narg.text = argvars;
484			n2->narg.backquote = NULL;
485			n2->narg.next = NULL;
486			n1->nfor.args = n2;
487			/*
488			 * Newline or semicolon here is optional (but note
489			 * that the original Bourne shell only allowed NL).
490			 */
491			if (lasttoken != TNL && lasttoken != TSEMI)
492				tokpushback++;
493		}
494		checkkwd = CHKNL | CHKKWD | CHKALIAS;
495		if ((t = readtoken()) == TDO)
496			t = TDONE;
497		else if (t == TBEGIN)
498			t = TEND;
499		else
500			synexpect(-1);
501		n1->nfor.body = list(0, 0);
502		if (readtoken() != t)
503			synexpect(t);
504		checkkwd = CHKKWD | CHKALIAS;
505		break;
506	case TCASE:
507		n1 = (union node *)stalloc(sizeof (struct ncase));
508		n1->type = NCASE;
509		if (readtoken() != TWORD)
510			synexpect(TWORD);
511		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
512		n2->type = NARG;
513		n2->narg.text = wordtext;
514		n2->narg.backquote = backquotelist;
515		n2->narg.next = NULL;
516		while (readtoken() == TNL);
517		if (lasttoken != TWORD || ! equal(wordtext, "in"))
518			synerror("expecting \"in\"");
519		cpp = &n1->ncase.cases;
520		checkkwd = CHKNL | CHKKWD, readtoken();
521		while (lasttoken != TESAC) {
522			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
523			cp->type = NCLIST;
524			app = &cp->nclist.pattern;
525			if (lasttoken == TLP)
526				readtoken();
527			for (;;) {
528				*app = ap = (union node *)stalloc(sizeof (struct narg));
529				ap->type = NARG;
530				ap->narg.text = wordtext;
531				ap->narg.backquote = backquotelist;
532				checkkwd = CHKNL | CHKKWD;
533				if (readtoken() != TPIPE)
534					break;
535				app = &ap->narg.next;
536				readtoken();
537			}
538			ap->narg.next = NULL;
539			if (lasttoken != TRP)
540				synexpect(TRP);
541			cp->nclist.body = list(0, 0);
542
543			checkkwd = CHKNL | CHKKWD | CHKALIAS;
544			if ((t = readtoken()) != TESAC) {
545				if (t != TENDCASE)
546					synexpect(TENDCASE);
547				else
548					checkkwd = CHKNL | CHKKWD, readtoken();
549			}
550			cpp = &cp->nclist.next;
551		}
552		*cpp = NULL;
553		checkkwd = CHKKWD | CHKALIAS;
554		break;
555	case TLP:
556		n1 = (union node *)stalloc(sizeof (struct nredir));
557		n1->type = NSUBSHELL;
558		n1->nredir.n = list(0, 0);
559		n1->nredir.redirect = NULL;
560		if (readtoken() != TRP)
561			synexpect(TRP);
562		checkkwd = CHKKWD | CHKALIAS;
563		is_subshell = 1;
564		break;
565	case TBEGIN:
566		n1 = list(0, 0);
567		if (readtoken() != TEND)
568			synexpect(TEND);
569		checkkwd = CHKKWD | CHKALIAS;
570		break;
571	/* Handle an empty command like other simple commands.  */
572	case TBACKGND:
573	case TSEMI:
574	case TAND:
575	case TOR:
576		/*
577		 * An empty command before a ; doesn't make much sense, and
578		 * should certainly be disallowed in the case of `if ;'.
579		 */
580		if (!redir)
581			synexpect(-1);
582	case TNL:
583	case TEOF:
584	case TWORD:
585	case TRP:
586		tokpushback++;
587		n1 = simplecmd(rpp, redir);
588		return n1;
589	default:
590		synexpect(-1);
591	}
592
593	/* Now check for redirection which may follow command */
594	while (readtoken() == TREDIR) {
595		*rpp = n2 = redirnode;
596		rpp = &n2->nfile.next;
597		parsefname();
598	}
599	tokpushback++;
600	*rpp = NULL;
601	if (redir) {
602		if (!is_subshell) {
603			n2 = (union node *)stalloc(sizeof (struct nredir));
604			n2->type = NREDIR;
605			n2->nredir.n = n1;
606			n1 = n2;
607		}
608		n1->nredir.redirect = redir;
609	}
610
611	return n1;
612}
613
614
615static union node *
616simplecmd(union node **rpp, union node *redir)
617{
618	union node *args, **app;
619	union node **orig_rpp = rpp;
620	union node *n = NULL;
621	int special;
622	int savecheckkwd;
623
624	/* If we don't have any redirections already, then we must reset */
625	/* rpp to be the address of the local redir variable.  */
626	if (redir == 0)
627		rpp = &redir;
628
629	args = NULL;
630	app = &args;
631	/*
632	 * We save the incoming value, because we need this for shell
633	 * functions.  There can not be a redirect or an argument between
634	 * the function name and the open parenthesis.
635	 */
636	orig_rpp = rpp;
637
638	savecheckkwd = CHKALIAS;
639
640	for (;;) {
641		checkkwd = savecheckkwd;
642		if (readtoken() == TWORD) {
643			n = (union node *)stalloc(sizeof (struct narg));
644			n->type = NARG;
645			n->narg.text = wordtext;
646			n->narg.backquote = backquotelist;
647			*app = n;
648			app = &n->narg.next;
649			if (savecheckkwd != 0 && !isassignment(wordtext))
650				savecheckkwd = 0;
651		} else if (lasttoken == TREDIR) {
652			*rpp = n = redirnode;
653			rpp = &n->nfile.next;
654			parsefname();	/* read name of redirection file */
655		} else if (lasttoken == TLP && app == &args->narg.next
656					    && rpp == orig_rpp) {
657			/* We have a function */
658			if (readtoken() != TRP)
659				synexpect(TRP);
660			funclinno = plinno;
661			/*
662			 * - Require plain text.
663			 * - Functions with '/' cannot be called.
664			 * - Reject name=().
665			 * - Reject ksh extended glob patterns.
666			 */
667			if (!noexpand(n->narg.text) || quoteflag ||
668			    strchr(n->narg.text, '/') ||
669			    strchr("!%*+-=?@}~",
670				n->narg.text[strlen(n->narg.text) - 1]))
671				synerror("Bad function name");
672			rmescapes(n->narg.text);
673			if (find_builtin(n->narg.text, &special) >= 0 &&
674			    special)
675				synerror("Cannot override a special builtin with a function");
676			n->type = NDEFUN;
677			n->narg.next = command();
678			funclinno = 0;
679			return n;
680		} else {
681			tokpushback++;
682			break;
683		}
684	}
685	*app = NULL;
686	*rpp = NULL;
687	n = (union node *)stalloc(sizeof (struct ncmd));
688	n->type = NCMD;
689	n->ncmd.backgnd = 0;
690	n->ncmd.args = args;
691	n->ncmd.redirect = redir;
692	return n;
693}
694
695static union node *
696makename(void)
697{
698	union node *n;
699
700	n = (union node *)stalloc(sizeof (struct narg));
701	n->type = NARG;
702	n->narg.next = NULL;
703	n->narg.text = wordtext;
704	n->narg.backquote = backquotelist;
705	return n;
706}
707
708void
709fixredir(union node *n, const char *text, int err)
710{
711	TRACE(("Fix redir %s %d\n", text, err));
712	if (!err)
713		n->ndup.vname = NULL;
714
715	if (is_digit(text[0]) && text[1] == '\0')
716		n->ndup.dupfd = digit_val(text[0]);
717	else if (text[0] == '-' && text[1] == '\0')
718		n->ndup.dupfd = -1;
719	else {
720
721		if (err)
722			synerror("Bad fd number");
723		else
724			n->ndup.vname = makename();
725	}
726}
727
728
729static void
730parsefname(void)
731{
732	union node *n = redirnode;
733
734	if (readtoken() != TWORD)
735		synexpect(-1);
736	if (n->type == NHERE) {
737		struct heredoc *here = heredoc;
738		struct heredoc *p;
739		int i;
740
741		if (quoteflag == 0)
742			n->type = NXHERE;
743		TRACE(("Here document %d\n", n->type));
744		if (here->striptabs) {
745			while (*wordtext == '\t')
746				wordtext++;
747		}
748		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
749			synerror("Illegal eof marker for << redirection");
750		rmescapes(wordtext);
751		here->eofmark = wordtext;
752		here->next = NULL;
753		if (heredoclist == NULL)
754			heredoclist = here;
755		else {
756			for (p = heredoclist ; p->next ; p = p->next);
757			p->next = here;
758		}
759	} else if (n->type == NTOFD || n->type == NFROMFD) {
760		fixredir(n, wordtext, 0);
761	} else {
762		n->nfile.fname = makename();
763	}
764}
765
766
767/*
768 * Input any here documents.
769 */
770
771static void
772parseheredoc(void)
773{
774	struct heredoc *here;
775	union node *n;
776
777	while (heredoclist) {
778		here = heredoclist;
779		heredoclist = here->next;
780		if (needprompt) {
781			setprompt(2);
782			needprompt = 0;
783		}
784		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
785				here->eofmark, here->striptabs);
786		n = (union node *)stalloc(sizeof (struct narg));
787		n->narg.type = NARG;
788		n->narg.next = NULL;
789		n->narg.text = wordtext;
790		n->narg.backquote = backquotelist;
791		here->here->nhere.doc = n;
792	}
793}
794
795static int
796peektoken(void)
797{
798	int t;
799
800	t = readtoken();
801	tokpushback++;
802	return (t);
803}
804
805static int
806readtoken(void)
807{
808	int t;
809	struct alias *ap;
810#ifdef DEBUG
811	int alreadyseen = tokpushback;
812#endif
813
814	top:
815	t = xxreadtoken();
816
817	/*
818	 * eat newlines
819	 */
820	if (checkkwd & CHKNL) {
821		while (t == TNL) {
822			parseheredoc();
823			t = xxreadtoken();
824		}
825	}
826
827	/*
828	 * check for keywords and aliases
829	 */
830	if (t == TWORD && !quoteflag)
831	{
832		const char * const *pp;
833
834		if (checkkwd & CHKKWD)
835			for (pp = parsekwd; *pp; pp++) {
836				if (**pp == *wordtext && equal(*pp, wordtext))
837				{
838					lasttoken = t = pp - parsekwd + KWDOFFSET;
839					TRACE(("keyword %s recognized\n", tokname[t]));
840					goto out;
841				}
842			}
843		if (checkkwd & CHKALIAS &&
844		    (ap = lookupalias(wordtext, 1)) != NULL) {
845			pushstring(ap->val, strlen(ap->val), ap);
846			goto top;
847		}
848	}
849out:
850	if (t != TNOT)
851		checkkwd = 0;
852
853#ifdef DEBUG
854	if (!alreadyseen)
855	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
856	else
857	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
858#endif
859	return (t);
860}
861
862
863/*
864 * Read the next input token.
865 * If the token is a word, we set backquotelist to the list of cmds in
866 *	backquotes.  We set quoteflag to true if any part of the word was
867 *	quoted.
868 * If the token is TREDIR, then we set redirnode to a structure containing
869 *	the redirection.
870 * In all cases, the variable startlinno is set to the number of the line
871 *	on which the token starts.
872 *
873 * [Change comment:  here documents and internal procedures]
874 * [Readtoken shouldn't have any arguments.  Perhaps we should make the
875 *  word parsing code into a separate routine.  In this case, readtoken
876 *  doesn't need to have any internal procedures, but parseword does.
877 *  We could also make parseoperator in essence the main routine, and
878 *  have parseword (readtoken1?) handle both words and redirection.]
879 */
880
881#define RETURN(token)	return lasttoken = token
882
883static int
884xxreadtoken(void)
885{
886	int c;
887
888	if (tokpushback) {
889		tokpushback = 0;
890		return lasttoken;
891	}
892	if (needprompt) {
893		setprompt(2);
894		needprompt = 0;
895	}
896	startlinno = plinno;
897	for (;;) {	/* until token or start of word found */
898		c = pgetc_macro();
899		switch (c) {
900		case ' ': case '\t':
901			continue;
902		case '#':
903			while ((c = pgetc()) != '\n' && c != PEOF);
904			pungetc();
905			continue;
906		case '\\':
907			if (pgetc() == '\n') {
908				startlinno = ++plinno;
909				if (doprompt)
910					setprompt(2);
911				else
912					setprompt(0);
913				continue;
914			}
915			pungetc();
916			goto breakloop;
917		case '\n':
918			plinno++;
919			needprompt = doprompt;
920			RETURN(TNL);
921		case PEOF:
922			RETURN(TEOF);
923		case '&':
924			if (pgetc() == '&')
925				RETURN(TAND);
926			pungetc();
927			RETURN(TBACKGND);
928		case '|':
929			if (pgetc() == '|')
930				RETURN(TOR);
931			pungetc();
932			RETURN(TPIPE);
933		case ';':
934			if (pgetc() == ';')
935				RETURN(TENDCASE);
936			pungetc();
937			RETURN(TSEMI);
938		case '(':
939			RETURN(TLP);
940		case ')':
941			RETURN(TRP);
942		default:
943			goto breakloop;
944		}
945	}
946breakloop:
947	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
948#undef RETURN
949}
950
951
952#define MAXNEST_static 8
953struct tokenstate
954{
955	const char *syntax; /* *SYNTAX */
956	int parenlevel; /* levels of parentheses in arithmetic */
957	enum tokenstate_category
958	{
959		TSTATE_TOP,
960		TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
961		TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
962		TSTATE_ARITH
963	} category;
964};
965
966
967/*
968 * Called to parse command substitutions.
969 */
970
971static char *
972parsebackq(char *out, struct nodelist **pbqlist,
973		int oldstyle, int dblquote, int quoted)
974{
975	struct nodelist **nlpp;
976	union node *n;
977	char *volatile str;
978	struct jmploc jmploc;
979	struct jmploc *const savehandler = handler;
980	int savelen;
981	int saveprompt;
982	const int bq_startlinno = plinno;
983	char *volatile ostr = NULL;
984	struct parsefile *const savetopfile = getcurrentfile();
985	struct heredoc *const saveheredoclist = heredoclist;
986	struct heredoc *here;
987
988	str = NULL;
989	if (setjmp(jmploc.loc)) {
990		popfilesupto(savetopfile);
991		if (str)
992			ckfree(str);
993		if (ostr)
994			ckfree(ostr);
995		heredoclist = saveheredoclist;
996		handler = savehandler;
997		if (exception == EXERROR) {
998			startlinno = bq_startlinno;
999			synerror("Error in command substitution");
1000		}
1001		longjmp(handler->loc, 1);
1002	}
1003	INTOFF;
1004	savelen = out - stackblock();
1005	if (savelen > 0) {
1006		str = ckmalloc(savelen);
1007		memcpy(str, stackblock(), savelen);
1008	}
1009	handler = &jmploc;
1010	heredoclist = NULL;
1011	INTON;
1012        if (oldstyle) {
1013                /* We must read until the closing backquote, giving special
1014                   treatment to some slashes, and then push the string and
1015                   reread it as input, interpreting it normally.  */
1016                char *oout;
1017                int c;
1018                int olen;
1019
1020
1021                STARTSTACKSTR(oout);
1022		for (;;) {
1023			if (needprompt) {
1024				setprompt(2);
1025				needprompt = 0;
1026			}
1027			CHECKSTRSPACE(2, oout);
1028			switch (c = pgetc()) {
1029			case '`':
1030				goto done;
1031
1032			case '\\':
1033                                if ((c = pgetc()) == '\n') {
1034					plinno++;
1035					if (doprompt)
1036						setprompt(2);
1037					else
1038						setprompt(0);
1039					/*
1040					 * If eating a newline, avoid putting
1041					 * the newline into the new character
1042					 * stream (via the USTPUTC after the
1043					 * switch).
1044					 */
1045					continue;
1046				}
1047                                if (c != '\\' && c != '`' && c != '$'
1048                                    && (!dblquote || c != '"'))
1049                                        USTPUTC('\\', oout);
1050				break;
1051
1052			case '\n':
1053				plinno++;
1054				needprompt = doprompt;
1055				break;
1056
1057			case PEOF:
1058			        startlinno = plinno;
1059				synerror("EOF in backquote substitution");
1060 				break;
1061
1062			default:
1063				break;
1064			}
1065			USTPUTC(c, oout);
1066                }
1067done:
1068                USTPUTC('\0', oout);
1069                olen = oout - stackblock();
1070		INTOFF;
1071		ostr = ckmalloc(olen);
1072		memcpy(ostr, stackblock(), olen);
1073		setinputstring(ostr, 1);
1074		INTON;
1075        }
1076	nlpp = pbqlist;
1077	while (*nlpp)
1078		nlpp = &(*nlpp)->next;
1079	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1080	(*nlpp)->next = NULL;
1081
1082	if (oldstyle) {
1083		saveprompt = doprompt;
1084		doprompt = 0;
1085	}
1086
1087	n = list(0, oldstyle);
1088
1089	if (oldstyle)
1090		doprompt = saveprompt;
1091	else {
1092		if (readtoken() != TRP)
1093			synexpect(TRP);
1094	}
1095
1096	(*nlpp)->n = n;
1097        if (oldstyle) {
1098		/*
1099		 * Start reading from old file again, ignoring any pushed back
1100		 * tokens left from the backquote parsing
1101		 */
1102                popfile();
1103		tokpushback = 0;
1104	}
1105	STARTSTACKSTR(out);
1106	CHECKSTRSPACE(savelen + 1, out);
1107	INTOFF;
1108	if (str) {
1109		memcpy(out, str, savelen);
1110		STADJUST(savelen, out);
1111		ckfree(str);
1112		str = NULL;
1113	}
1114	if (ostr) {
1115		ckfree(ostr);
1116		ostr = NULL;
1117	}
1118	here = saveheredoclist;
1119	if (here != NULL) {
1120		while (here->next != NULL)
1121			here = here->next;
1122		here->next = heredoclist;
1123		heredoclist = saveheredoclist;
1124	}
1125	handler = savehandler;
1126	INTON;
1127	if (quoted)
1128		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1129	else
1130		USTPUTC(CTLBACKQ, out);
1131	return out;
1132}
1133
1134
1135/*
1136 * Called to parse a backslash escape sequence inside $'...'.
1137 * The backslash has already been read.
1138 */
1139static char *
1140readcstyleesc(char *out)
1141{
1142	int c, v, i, n;
1143
1144	c = pgetc();
1145	switch (c) {
1146	case '\0':
1147		synerror("Unterminated quoted string");
1148	case '\n':
1149		plinno++;
1150		if (doprompt)
1151			setprompt(2);
1152		else
1153			setprompt(0);
1154		return out;
1155	case '\\':
1156	case '\'':
1157	case '"':
1158		v = c;
1159		break;
1160	case 'a': v = '\a'; break;
1161	case 'b': v = '\b'; break;
1162	case 'e': v = '\033'; break;
1163	case 'f': v = '\f'; break;
1164	case 'n': v = '\n'; break;
1165	case 'r': v = '\r'; break;
1166	case 't': v = '\t'; break;
1167	case 'v': v = '\v'; break;
1168	case 'x':
1169		  v = 0;
1170		  for (;;) {
1171			  c = pgetc();
1172			  if (c >= '0' && c <= '9')
1173				  v = (v << 4) + c - '0';
1174			  else if (c >= 'A' && c <= 'F')
1175				  v = (v << 4) + c - 'A' + 10;
1176			  else if (c >= 'a' && c <= 'f')
1177				  v = (v << 4) + c - 'a' + 10;
1178			  else
1179				  break;
1180		  }
1181		  pungetc();
1182		  break;
1183	case '0': case '1': case '2': case '3':
1184	case '4': case '5': case '6': case '7':
1185		  v = c - '0';
1186		  c = pgetc();
1187		  if (c >= '0' && c <= '7') {
1188			  v <<= 3;
1189			  v += c - '0';
1190			  c = pgetc();
1191			  if (c >= '0' && c <= '7') {
1192				  v <<= 3;
1193				  v += c - '0';
1194			  } else
1195				  pungetc();
1196		  } else
1197			  pungetc();
1198		  break;
1199	case 'c':
1200		  c = pgetc();
1201		  if (c < 0x3f || c > 0x7a || c == 0x60)
1202			  synerror("Bad escape sequence");
1203		  if (c == '\\' && pgetc() != '\\')
1204			  synerror("Bad escape sequence");
1205		  if (c == '?')
1206			  v = 127;
1207		  else
1208			  v = c & 0x1f;
1209		  break;
1210	case 'u':
1211	case 'U':
1212		  n = c == 'U' ? 8 : 4;
1213		  v = 0;
1214		  for (i = 0; i < n; i++) {
1215			  c = pgetc();
1216			  if (c >= '0' && c <= '9')
1217				  v = (v << 4) + c - '0';
1218			  else if (c >= 'A' && c <= 'F')
1219				  v = (v << 4) + c - 'A' + 10;
1220			  else if (c >= 'a' && c <= 'f')
1221				  v = (v << 4) + c - 'a' + 10;
1222			  else
1223				  synerror("Bad escape sequence");
1224		  }
1225		  if (v == 0 || (v >= 0xd800 && v <= 0xdfff))
1226			  synerror("Bad escape sequence");
1227		  /* We really need iconv here. */
1228		  if (initial_localeisutf8 && v > 127) {
1229			  CHECKSTRSPACE(4, out);
1230			  /*
1231			   * We cannot use wctomb() as the locale may have
1232			   * changed.
1233			   */
1234			  if (v <= 0x7ff) {
1235				  USTPUTC(0xc0 | v >> 6, out);
1236				  USTPUTC(0x80 | (v & 0x3f), out);
1237				  return out;
1238			  } else if (v <= 0xffff) {
1239				  USTPUTC(0xe0 | v >> 12, out);
1240				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1241				  USTPUTC(0x80 | (v & 0x3f), out);
1242				  return out;
1243			  } else if (v <= 0x10ffff) {
1244				  USTPUTC(0xf0 | v >> 18, out);
1245				  USTPUTC(0x80 | ((v >> 12) & 0x3f), out);
1246				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1247				  USTPUTC(0x80 | (v & 0x3f), out);
1248				  return out;
1249			  }
1250		  }
1251		  if (v > 127)
1252			  v = '?';
1253		  break;
1254	default:
1255		  synerror("Bad escape sequence");
1256	}
1257	v = (char)v;
1258	/*
1259	 * We can't handle NUL bytes.
1260	 * POSIX says we should skip till the closing quote.
1261	 */
1262	if (v == '\0') {
1263		while ((c = pgetc()) != '\'') {
1264			if (c == '\\')
1265				c = pgetc();
1266			if (c == PEOF)
1267				synerror("Unterminated quoted string");
1268		}
1269		pungetc();
1270		return out;
1271	}
1272	if (SQSYNTAX[v] == CCTL)
1273		USTPUTC(CTLESC, out);
1274	USTPUTC(v, out);
1275	return out;
1276}
1277
1278
1279/*
1280 * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
1281 * is not NULL, read a here document.  In the latter case, eofmark is the
1282 * word which marks the end of the document and striptabs is true if
1283 * leading tabs should be stripped from the document.  The argument firstc
1284 * is the first character of the input token or document.
1285 *
1286 * Because C does not have internal subroutines, I have simulated them
1287 * using goto's to implement the subroutine linkage.  The following macros
1288 * will run code that appears at the end of readtoken1.
1289 */
1290
1291#define CHECKEND()	{goto checkend; checkend_return:;}
1292#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
1293#define PARSESUB()	{goto parsesub; parsesub_return:;}
1294#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
1295
1296static int
1297readtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs)
1298{
1299	int c = firstc;
1300	char *out;
1301	int len;
1302	char line[EOFMARKLEN + 1];
1303	struct nodelist *bqlist;
1304	int quotef;
1305	int newvarnest;
1306	int level;
1307	int synentry;
1308	struct tokenstate state_static[MAXNEST_static];
1309	int maxnest = MAXNEST_static;
1310	struct tokenstate *state = state_static;
1311	int sqiscstyle = 0;
1312
1313	startlinno = plinno;
1314	quotef = 0;
1315	bqlist = NULL;
1316	newvarnest = 0;
1317	level = 0;
1318	state[level].syntax = initialsyntax;
1319	state[level].parenlevel = 0;
1320	state[level].category = TSTATE_TOP;
1321
1322	STARTSTACKSTR(out);
1323	loop: {	/* for each line, until end of word */
1324		CHECKEND();	/* set c to PEOF if at end of here document */
1325		for (;;) {	/* until end of line or end of word */
1326			CHECKSTRSPACE(4, out);	/* permit 4 calls to USTPUTC */
1327
1328			synentry = state[level].syntax[c];
1329
1330			switch(synentry) {
1331			case CNL:	/* '\n' */
1332				if (state[level].syntax == BASESYNTAX)
1333					goto endword;	/* exit outer loop */
1334				USTPUTC(c, out);
1335				plinno++;
1336				if (doprompt)
1337					setprompt(2);
1338				else
1339					setprompt(0);
1340				c = pgetc();
1341				goto loop;		/* continue outer loop */
1342			case CSBACK:
1343				if (sqiscstyle) {
1344					out = readcstyleesc(out);
1345					break;
1346				}
1347				/* FALLTHROUGH */
1348			case CWORD:
1349				USTPUTC(c, out);
1350				break;
1351			case CCTL:
1352				if (eofmark == NULL || initialsyntax != SQSYNTAX)
1353					USTPUTC(CTLESC, out);
1354				USTPUTC(c, out);
1355				break;
1356			case CBACK:	/* backslash */
1357				c = pgetc();
1358				if (c == PEOF) {
1359					USTPUTC('\\', out);
1360					pungetc();
1361				} else if (c == '\n') {
1362					plinno++;
1363					if (doprompt)
1364						setprompt(2);
1365					else
1366						setprompt(0);
1367				} else {
1368					if (state[level].syntax == DQSYNTAX &&
1369					    c != '\\' && c != '`' && c != '$' &&
1370					    (c != '"' || (eofmark != NULL &&
1371						newvarnest == 0)) &&
1372					    (c != '}' || state[level].category != TSTATE_VAR_OLD))
1373						USTPUTC('\\', out);
1374					if ((eofmark == NULL ||
1375					    newvarnest > 0) &&
1376					    state[level].syntax == BASESYNTAX)
1377						USTPUTC(CTLQUOTEMARK, out);
1378					if (SQSYNTAX[c] == CCTL)
1379						USTPUTC(CTLESC, out);
1380					USTPUTC(c, out);
1381					if ((eofmark == NULL ||
1382					    newvarnest > 0) &&
1383					    state[level].syntax == BASESYNTAX &&
1384					    state[level].category == TSTATE_VAR_OLD)
1385						USTPUTC(CTLQUOTEEND, out);
1386					quotef++;
1387				}
1388				break;
1389			case CSQUOTE:
1390				USTPUTC(CTLQUOTEMARK, out);
1391				state[level].syntax = SQSYNTAX;
1392				sqiscstyle = 0;
1393				break;
1394			case CDQUOTE:
1395				USTPUTC(CTLQUOTEMARK, out);
1396				state[level].syntax = DQSYNTAX;
1397				break;
1398			case CENDQUOTE:
1399				if (eofmark != NULL && newvarnest == 0)
1400					USTPUTC(c, out);
1401				else {
1402					if (state[level].category == TSTATE_VAR_OLD)
1403						USTPUTC(CTLQUOTEEND, out);
1404					state[level].syntax = BASESYNTAX;
1405					quotef++;
1406				}
1407				break;
1408			case CVAR:	/* '$' */
1409				PARSESUB();		/* parse substitution */
1410				break;
1411			case CENDVAR:	/* '}' */
1412				if (level > 0 &&
1413				    ((state[level].category == TSTATE_VAR_OLD &&
1414				      state[level].syntax ==
1415				      state[level - 1].syntax) ||
1416				    (state[level].category == TSTATE_VAR_NEW &&
1417				     state[level].syntax == BASESYNTAX))) {
1418					if (state[level].category == TSTATE_VAR_NEW)
1419						newvarnest--;
1420					level--;
1421					USTPUTC(CTLENDVAR, out);
1422				} else {
1423					USTPUTC(c, out);
1424				}
1425				break;
1426			case CLP:	/* '(' in arithmetic */
1427				state[level].parenlevel++;
1428				USTPUTC(c, out);
1429				break;
1430			case CRP:	/* ')' in arithmetic */
1431				if (state[level].parenlevel > 0) {
1432					USTPUTC(c, out);
1433					--state[level].parenlevel;
1434				} else {
1435					if (pgetc() == ')') {
1436						if (level > 0 &&
1437						    state[level].category == TSTATE_ARITH) {
1438							level--;
1439							USTPUTC(CTLENDARI, out);
1440						} else
1441							USTPUTC(')', out);
1442					} else {
1443						/*
1444						 * unbalanced parens
1445						 *  (don't 2nd guess - no error)
1446						 */
1447						pungetc();
1448						USTPUTC(')', out);
1449					}
1450				}
1451				break;
1452			case CBQUOTE:	/* '`' */
1453				out = parsebackq(out, &bqlist, 1,
1454				    state[level].syntax == DQSYNTAX &&
1455				    (eofmark == NULL || newvarnest > 0),
1456				    state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
1457				break;
1458			case CEOF:
1459				goto endword;		/* exit outer loop */
1460			case CIGN:
1461				break;
1462			default:
1463				if (level == 0)
1464					goto endword;	/* exit outer loop */
1465				USTPUTC(c, out);
1466			}
1467			c = pgetc_macro();
1468		}
1469	}
1470endword:
1471	if (state[level].syntax == ARISYNTAX)
1472		synerror("Missing '))'");
1473	if (state[level].syntax != BASESYNTAX && eofmark == NULL)
1474		synerror("Unterminated quoted string");
1475	if (state[level].category == TSTATE_VAR_OLD ||
1476	    state[level].category == TSTATE_VAR_NEW) {
1477		startlinno = plinno;
1478		synerror("Missing '}'");
1479	}
1480	if (state != state_static)
1481		parser_temp_free_upto(state);
1482	USTPUTC('\0', out);
1483	len = out - stackblock();
1484	out = stackblock();
1485	if (eofmark == NULL) {
1486		if ((c == '>' || c == '<')
1487		 && quotef == 0
1488		 && len <= 2
1489		 && (*out == '\0' || is_digit(*out))) {
1490			PARSEREDIR();
1491			return lasttoken = TREDIR;
1492		} else {
1493			pungetc();
1494		}
1495	}
1496	quoteflag = quotef;
1497	backquotelist = bqlist;
1498	grabstackblock(len);
1499	wordtext = out;
1500	return lasttoken = TWORD;
1501/* end of readtoken routine */
1502
1503
1504/*
1505 * Check to see whether we are at the end of the here document.  When this
1506 * is called, c is set to the first character of the next input line.  If
1507 * we are at the end of the here document, this routine sets the c to PEOF.
1508 */
1509
1510checkend: {
1511	if (eofmark) {
1512		if (striptabs) {
1513			while (c == '\t')
1514				c = pgetc();
1515		}
1516		if (c == *eofmark) {
1517			if (pfgets(line, sizeof line) != NULL) {
1518				char *p, *q;
1519
1520				p = line;
1521				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1522				if ((*p == '\0' || *p == '\n') && *q == '\0') {
1523					c = PEOF;
1524					if (*p == '\n') {
1525						plinno++;
1526						needprompt = doprompt;
1527					}
1528				} else {
1529					pushstring(line, strlen(line), NULL);
1530				}
1531			}
1532		}
1533	}
1534	goto checkend_return;
1535}
1536
1537
1538/*
1539 * Parse a redirection operator.  The variable "out" points to a string
1540 * specifying the fd to be redirected.  The variable "c" contains the
1541 * first character of the redirection operator.
1542 */
1543
1544parseredir: {
1545	char fd = *out;
1546	union node *np;
1547
1548	np = (union node *)stalloc(sizeof (struct nfile));
1549	if (c == '>') {
1550		np->nfile.fd = 1;
1551		c = pgetc();
1552		if (c == '>')
1553			np->type = NAPPEND;
1554		else if (c == '&')
1555			np->type = NTOFD;
1556		else if (c == '|')
1557			np->type = NCLOBBER;
1558		else {
1559			np->type = NTO;
1560			pungetc();
1561		}
1562	} else {	/* c == '<' */
1563		np->nfile.fd = 0;
1564		c = pgetc();
1565		if (c == '<') {
1566			if (sizeof (struct nfile) != sizeof (struct nhere)) {
1567				np = (union node *)stalloc(sizeof (struct nhere));
1568				np->nfile.fd = 0;
1569			}
1570			np->type = NHERE;
1571			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1572			heredoc->here = np;
1573			if ((c = pgetc()) == '-') {
1574				heredoc->striptabs = 1;
1575			} else {
1576				heredoc->striptabs = 0;
1577				pungetc();
1578			}
1579		} else if (c == '&')
1580			np->type = NFROMFD;
1581		else if (c == '>')
1582			np->type = NFROMTO;
1583		else {
1584			np->type = NFROM;
1585			pungetc();
1586		}
1587	}
1588	if (fd != '\0')
1589		np->nfile.fd = digit_val(fd);
1590	redirnode = np;
1591	goto parseredir_return;
1592}
1593
1594
1595/*
1596 * Parse a substitution.  At this point, we have read the dollar sign
1597 * and nothing else.
1598 */
1599
1600parsesub: {
1601	char buf[10];
1602	int subtype;
1603	int typeloc;
1604	int flags;
1605	char *p;
1606	static const char types[] = "}-+?=";
1607	int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1608	int linno;
1609	int length;
1610	int c1;
1611
1612	c = pgetc();
1613	if (c == '(') {	/* $(command) or $((arith)) */
1614		if (pgetc() == '(') {
1615			PARSEARITH();
1616		} else {
1617			pungetc();
1618			out = parsebackq(out, &bqlist, 0,
1619			    state[level].syntax == DQSYNTAX &&
1620			    (eofmark == NULL || newvarnest > 0),
1621			    state[level].syntax == DQSYNTAX ||
1622			    state[level].syntax == ARISYNTAX);
1623		}
1624	} else if (c == '{' || is_name(c) || is_special(c)) {
1625		USTPUTC(CTLVAR, out);
1626		typeloc = out - stackblock();
1627		USTPUTC(VSNORMAL, out);
1628		subtype = VSNORMAL;
1629		flags = 0;
1630		if (c == '{') {
1631			bracketed_name = 1;
1632			c = pgetc();
1633			subtype = 0;
1634		}
1635varname:
1636		if (!is_eof(c) && is_name(c)) {
1637			length = 0;
1638			do {
1639				STPUTC(c, out);
1640				c = pgetc();
1641				length++;
1642			} while (!is_eof(c) && is_in_name(c));
1643			if (length == 6 &&
1644			    strncmp(out - length, "LINENO", length) == 0) {
1645				/* Replace the variable name with the
1646				 * current line number. */
1647				linno = plinno;
1648				if (funclinno != 0)
1649					linno -= funclinno - 1;
1650				snprintf(buf, sizeof(buf), "%d", linno);
1651				STADJUST(-6, out);
1652				STPUTS(buf, out);
1653				flags |= VSLINENO;
1654			}
1655		} else if (is_digit(c)) {
1656			if (bracketed_name) {
1657				do {
1658					STPUTC(c, out);
1659					c = pgetc();
1660				} while (is_digit(c));
1661			} else {
1662				STPUTC(c, out);
1663				c = pgetc();
1664			}
1665		} else if (is_special(c)) {
1666			c1 = c;
1667			c = pgetc();
1668			if (subtype == 0 && c1 == '#') {
1669				subtype = VSLENGTH;
1670				if (strchr(types, c) == NULL && c != ':' &&
1671				    c != '#' && c != '%')
1672					goto varname;
1673				c1 = c;
1674				c = pgetc();
1675				if (c1 != '}' && c == '}') {
1676					pungetc();
1677					c = c1;
1678					goto varname;
1679				}
1680				pungetc();
1681				c = c1;
1682				c1 = '#';
1683				subtype = 0;
1684			}
1685			USTPUTC(c1, out);
1686		} else {
1687			subtype = VSERROR;
1688			if (c == '}')
1689				pungetc();
1690			else if (c == '\n' || c == PEOF)
1691				synerror("Unexpected end of line in substitution");
1692			else
1693				USTPUTC(c, out);
1694		}
1695		if (subtype == 0) {
1696			switch (c) {
1697			case ':':
1698				flags |= VSNUL;
1699				c = pgetc();
1700				/*FALLTHROUGH*/
1701			default:
1702				p = strchr(types, c);
1703				if (p == NULL) {
1704					if (c == '\n' || c == PEOF)
1705						synerror("Unexpected end of line in substitution");
1706					if (flags == VSNUL)
1707						STPUTC(':', out);
1708					STPUTC(c, out);
1709					subtype = VSERROR;
1710				} else
1711					subtype = p - types + VSNORMAL;
1712				break;
1713			case '%':
1714			case '#':
1715				{
1716					int cc = c;
1717					subtype = c == '#' ? VSTRIMLEFT :
1718							     VSTRIMRIGHT;
1719					c = pgetc();
1720					if (c == cc)
1721						subtype++;
1722					else
1723						pungetc();
1724					break;
1725				}
1726			}
1727		} else if (subtype != VSERROR) {
1728			if (subtype == VSLENGTH && c != '}')
1729				subtype = VSERROR;
1730			pungetc();
1731		}
1732		STPUTC('=', out);
1733		if (state[level].syntax == DQSYNTAX ||
1734		    state[level].syntax == ARISYNTAX)
1735			flags |= VSQUOTE;
1736		*(stackblock() + typeloc) = subtype | flags;
1737		if (subtype != VSNORMAL) {
1738			if (level + 1 >= maxnest) {
1739				maxnest *= 2;
1740				if (state == state_static) {
1741					state = parser_temp_alloc(
1742					    maxnest * sizeof(*state));
1743					memcpy(state, state_static,
1744					    MAXNEST_static * sizeof(*state));
1745				} else
1746					state = parser_temp_realloc(state,
1747					    maxnest * sizeof(*state));
1748			}
1749			level++;
1750			state[level].parenlevel = 0;
1751			if (subtype == VSMINUS || subtype == VSPLUS ||
1752			    subtype == VSQUESTION || subtype == VSASSIGN) {
1753				/*
1754				 * For operators that were in the Bourne shell,
1755				 * inherit the double-quote state.
1756				 */
1757				state[level].syntax = state[level - 1].syntax;
1758				state[level].category = TSTATE_VAR_OLD;
1759			} else {
1760				/*
1761				 * The other operators take a pattern,
1762				 * so go to BASESYNTAX.
1763				 * Also, ' and " are now special, even
1764				 * in here documents.
1765				 */
1766				state[level].syntax = BASESYNTAX;
1767				state[level].category = TSTATE_VAR_NEW;
1768				newvarnest++;
1769			}
1770		}
1771	} else if (c == '\'' && state[level].syntax == BASESYNTAX) {
1772		/* $'cstylequotes' */
1773		USTPUTC(CTLQUOTEMARK, out);
1774		state[level].syntax = SQSYNTAX;
1775		sqiscstyle = 1;
1776	} else {
1777		USTPUTC('$', out);
1778		pungetc();
1779	}
1780	goto parsesub_return;
1781}
1782
1783
1784/*
1785 * Parse an arithmetic expansion (indicate start of one and set state)
1786 */
1787parsearith: {
1788
1789	if (level + 1 >= maxnest) {
1790		maxnest *= 2;
1791		if (state == state_static) {
1792			state = parser_temp_alloc(
1793			    maxnest * sizeof(*state));
1794			memcpy(state, state_static,
1795			    MAXNEST_static * sizeof(*state));
1796		} else
1797			state = parser_temp_realloc(state,
1798			    maxnest * sizeof(*state));
1799	}
1800	level++;
1801	state[level].syntax = ARISYNTAX;
1802	state[level].parenlevel = 0;
1803	state[level].category = TSTATE_ARITH;
1804	USTPUTC(CTLARI, out);
1805	if (state[level - 1].syntax == DQSYNTAX)
1806		USTPUTC('"',out);
1807	else
1808		USTPUTC(' ',out);
1809	goto parsearith_return;
1810}
1811
1812} /* end of readtoken */
1813
1814
1815
1816#ifdef mkinit
1817RESET {
1818	tokpushback = 0;
1819	checkkwd = 0;
1820}
1821#endif
1822
1823/*
1824 * Returns true if the text contains nothing to expand (no dollar signs
1825 * or backquotes).
1826 */
1827
1828static int
1829noexpand(char *text)
1830{
1831	char *p;
1832	char c;
1833
1834	p = text;
1835	while ((c = *p++) != '\0') {
1836		if ( c == CTLQUOTEMARK)
1837			continue;
1838		if (c == CTLESC)
1839			p++;
1840		else if (BASESYNTAX[(int)c] == CCTL)
1841			return 0;
1842	}
1843	return 1;
1844}
1845
1846
1847/*
1848 * Return true if the argument is a legal variable name (a letter or
1849 * underscore followed by zero or more letters, underscores, and digits).
1850 */
1851
1852int
1853goodname(const char *name)
1854{
1855	const char *p;
1856
1857	p = name;
1858	if (! is_name(*p))
1859		return 0;
1860	while (*++p) {
1861		if (! is_in_name(*p))
1862			return 0;
1863	}
1864	return 1;
1865}
1866
1867
1868int
1869isassignment(const char *p)
1870{
1871	if (!is_name(*p))
1872		return 0;
1873	p++;
1874	for (;;) {
1875		if (*p == '=')
1876			return 1;
1877		else if (!is_in_name(*p))
1878			return 0;
1879		p++;
1880	}
1881}
1882
1883
1884/*
1885 * Called when an unexpected token is read during the parse.  The argument
1886 * is the token that is expected, or -1 if more than one type of token can
1887 * occur at this point.
1888 */
1889
1890static void
1891synexpect(int token)
1892{
1893	char msg[64];
1894
1895	if (token >= 0) {
1896		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1897			tokname[lasttoken], tokname[token]);
1898	} else {
1899		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1900	}
1901	synerror(msg);
1902}
1903
1904
1905static void
1906synerror(const char *msg)
1907{
1908	if (commandname)
1909		outfmt(out2, "%s: %d: ", commandname, startlinno);
1910	outfmt(out2, "Syntax error: %s\n", msg);
1911	error((char *)NULL);
1912}
1913
1914static void
1915setprompt(int which)
1916{
1917	whichprompt = which;
1918
1919#ifndef NO_HISTORY
1920	if (!el)
1921#endif
1922	{
1923		out2str(getprompt(NULL));
1924		flushout(out2);
1925	}
1926}
1927
1928/*
1929 * called by editline -- any expansions to the prompt
1930 *    should be added here.
1931 */
1932char *
1933getprompt(void *unused __unused)
1934{
1935	static char ps[PROMPTLEN];
1936	char *fmt;
1937	const char *pwd;
1938	int i, trim;
1939	static char internal_error[] = "??";
1940
1941	/*
1942	 * Select prompt format.
1943	 */
1944	switch (whichprompt) {
1945	case 0:
1946		fmt = nullstr;
1947		break;
1948	case 1:
1949		fmt = ps1val();
1950		break;
1951	case 2:
1952		fmt = ps2val();
1953		break;
1954	default:
1955		return internal_error;
1956	}
1957
1958	/*
1959	 * Format prompt string.
1960	 */
1961	for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1962		if (*fmt == '\\')
1963			switch (*++fmt) {
1964
1965				/*
1966				 * Hostname.
1967				 *
1968				 * \h specifies just the local hostname,
1969				 * \H specifies fully-qualified hostname.
1970				 */
1971			case 'h':
1972			case 'H':
1973				ps[i] = '\0';
1974				gethostname(&ps[i], PROMPTLEN - i);
1975				/* Skip to end of hostname. */
1976				trim = (*fmt == 'h') ? '.' : '\0';
1977				while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1978					i++;
1979				break;
1980
1981				/*
1982				 * Working directory.
1983				 *
1984				 * \W specifies just the final component,
1985				 * \w specifies the entire path.
1986				 */
1987			case 'W':
1988			case 'w':
1989				pwd = lookupvar("PWD");
1990				if (pwd == NULL)
1991					pwd = "?";
1992				if (*fmt == 'W' &&
1993				    *pwd == '/' && pwd[1] != '\0')
1994					strlcpy(&ps[i], strrchr(pwd, '/') + 1,
1995					    PROMPTLEN - i);
1996				else
1997					strlcpy(&ps[i], pwd, PROMPTLEN - i);
1998				/* Skip to end of path. */
1999				while (ps[i + 1] != '\0')
2000					i++;
2001				break;
2002
2003				/*
2004				 * Superuser status.
2005				 *
2006				 * '$' for normal users, '#' for root.
2007				 */
2008			case '$':
2009				ps[i] = (geteuid() != 0) ? '$' : '#';
2010				break;
2011
2012				/*
2013				 * A literal \.
2014				 */
2015			case '\\':
2016				ps[i] = '\\';
2017				break;
2018
2019				/*
2020				 * Emit unrecognized formats verbatim.
2021				 */
2022			default:
2023				ps[i++] = '\\';
2024				ps[i] = *fmt;
2025				break;
2026			}
2027		else
2028			ps[i] = *fmt;
2029	ps[i] = '\0';
2030	return (ps);
2031}
2032
2033
2034const char *
2035expandstr(char *ps)
2036{
2037	union node n;
2038	struct jmploc jmploc;
2039	struct jmploc *const savehandler = handler;
2040	const int saveprompt = doprompt;
2041	struct parsefile *const savetopfile = getcurrentfile();
2042	struct parser_temp *const saveparser_temp = parser_temp;
2043	const char *result = NULL;
2044
2045	if (!setjmp(jmploc.loc)) {
2046		handler = &jmploc;
2047		parser_temp = NULL;
2048		setinputstring(ps, 1);
2049		doprompt = 0;
2050		readtoken1(pgetc(), DQSYNTAX, "\n\n", 0);
2051		if (backquotelist != NULL)
2052			error("Command substitution not allowed here");
2053
2054		n.narg.type = NARG;
2055		n.narg.next = NULL;
2056		n.narg.text = wordtext;
2057		n.narg.backquote = backquotelist;
2058
2059		expandarg(&n, NULL, 0);
2060		result = stackblock();
2061		INTOFF;
2062	}
2063	handler = savehandler;
2064	doprompt = saveprompt;
2065	popfilesupto(savetopfile);
2066	if (parser_temp != saveparser_temp) {
2067		parser_temp_free_all();
2068		parser_temp = saveparser_temp;
2069	}
2070	if (result != NULL) {
2071		INTON;
2072	} else if (exception == EXINT)
2073		raise(SIGINT);
2074	return result;
2075}
2076