parser.c revision 75155
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 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
40#endif
41static const char rcsid[] =
42  "$FreeBSD: head/bin/sh/parser.c 75155 2001-04-04 09:30:50Z brian $";
43#endif /* not lint */
44
45#include <stdlib.h>
46
47#include "shell.h"
48#include "parser.h"
49#include "nodes.h"
50#include "expand.h"	/* defines rmescapes() */
51#include "redir.h"	/* defines copyfd() */
52#include "syntax.h"
53#include "options.h"
54#include "input.h"
55#include "output.h"
56#include "var.h"
57#include "error.h"
58#include "memalloc.h"
59#include "mystring.h"
60#include "alias.h"
61#include "show.h"
62#include "eval.h"
63#ifndef NO_HISTORY
64#include "myhistedit.h"
65#endif
66
67/*
68 * Shell command parser.
69 */
70
71#define EOFMARKLEN 79
72
73/* values returned by readtoken */
74#include "token.h"
75
76
77
78struct heredoc {
79	struct heredoc *next;	/* next here document in list */
80	union node *here;		/* redirection node */
81	char *eofmark;		/* string indicating end of input */
82	int striptabs;		/* if set, strip leading tabs */
83};
84
85
86
87struct heredoc *heredoclist;	/* list of here documents to read */
88int parsebackquote;		/* nonzero if we are inside backquotes */
89int doprompt;			/* if set, prompt the user */
90int needprompt;			/* true if interactive and at start of line */
91int lasttoken;			/* last token read */
92MKINIT int tokpushback;		/* last token pushed back */
93char *wordtext;			/* text of last word returned by readtoken */
94MKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
95struct nodelist *backquotelist;
96union node *redirnode;
97struct heredoc *heredoc;
98int quoteflag;			/* set if (part of) last token was quoted */
99int startlinno;			/* line # where last token started */
100
101/* XXX When 'noaliases' is set to one, no alias expansion takes place. */
102static int noaliases = 0;
103
104#define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
105#ifdef GDB_HACK
106static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'};
107static const char types[] = "}-+?=";
108#endif
109
110
111STATIC union node *list __P((int));
112STATIC union node *andor __P((void));
113STATIC union node *pipeline __P((void));
114STATIC union node *command __P((void));
115STATIC union node *simplecmd __P((union node **, union node *));
116STATIC union node *makename __P((void));
117STATIC void parsefname __P((void));
118STATIC void parseheredoc __P((void));
119STATIC int peektoken __P((void));
120STATIC int readtoken __P((void));
121STATIC int xxreadtoken __P((void));
122STATIC int readtoken1 __P((int, char const *, char *, int));
123STATIC int noexpand __P((char *));
124STATIC void synexpect __P((int));
125STATIC void synerror __P((char *));
126STATIC void setprompt __P((int));
127
128
129/*
130 * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
131 * valid parse tree indicating a blank line.)
132 */
133
134union node *
135parsecmd(interact)
136	int interact;
137{
138	int t;
139
140	tokpushback = 0;
141	doprompt = interact;
142	if (doprompt)
143		setprompt(1);
144	else
145		setprompt(0);
146	needprompt = 0;
147	t = readtoken();
148	if (t == TEOF)
149		return NEOF;
150	if (t == TNL)
151		return NULL;
152	tokpushback++;
153	return list(1);
154}
155
156
157STATIC union node *
158list(nlflag)
159	int nlflag;
160{
161	union node *n1, *n2, *n3;
162	int tok;
163
164	checkkwd = 2;
165	if (nlflag == 0 && tokendlist[peektoken()])
166		return NULL;
167	n1 = NULL;
168	for (;;) {
169		n2 = andor();
170		tok = readtoken();
171		if (tok == TBACKGND) {
172			if (n2->type == NCMD || n2->type == NPIPE) {
173				n2->ncmd.backgnd = 1;
174			} else if (n2->type == NREDIR) {
175				n2->type = NBACKGND;
176			} else {
177				n3 = (union node *)stalloc(sizeof (struct nredir));
178				n3->type = NBACKGND;
179				n3->nredir.n = n2;
180				n3->nredir.redirect = NULL;
181				n2 = n3;
182			}
183		}
184		if (n1 == NULL) {
185			n1 = n2;
186		}
187		else {
188			n3 = (union node *)stalloc(sizeof (struct nbinary));
189			n3->type = NSEMI;
190			n3->nbinary.ch1 = n1;
191			n3->nbinary.ch2 = n2;
192			n1 = n3;
193		}
194		switch (tok) {
195		case TBACKGND:
196		case TSEMI:
197			tok = readtoken();
198			/* fall through */
199		case TNL:
200			if (tok == TNL) {
201				parseheredoc();
202				if (nlflag)
203					return n1;
204			} else {
205				tokpushback++;
206			}
207			checkkwd = 2;
208			if (tokendlist[peektoken()])
209				return n1;
210			break;
211		case TEOF:
212			if (heredoclist)
213				parseheredoc();
214			else
215				pungetc();		/* push back EOF on input */
216			return n1;
217		default:
218			if (nlflag)
219				synexpect(-1);
220			tokpushback++;
221			return n1;
222		}
223	}
224}
225
226
227
228STATIC union node *
229andor() {
230	union node *n1, *n2, *n3;
231	int t;
232
233	n1 = pipeline();
234	for (;;) {
235		if ((t = readtoken()) == TAND) {
236			t = NAND;
237		} else if (t == TOR) {
238			t = NOR;
239		} else {
240			tokpushback++;
241			return n1;
242		}
243		n2 = pipeline();
244		n3 = (union node *)stalloc(sizeof (struct nbinary));
245		n3->type = t;
246		n3->nbinary.ch1 = n1;
247		n3->nbinary.ch2 = n2;
248		n1 = n3;
249	}
250}
251
252
253
254STATIC union node *
255pipeline() {
256	union node *n1, *pipenode, *notnode;
257	struct nodelist *lp, *prev;
258	int negate = 0;
259	int savecheckkwd = checkkwd;
260
261	TRACE(("pipeline: entered\n"));
262	checkkwd = 2;
263	while (readtoken() == TNOT) {
264		TRACE(("pipeline: TNOT recognized\n"));
265		negate = !negate;
266	}
267	tokpushback++;
268	checkkwd = savecheckkwd;
269	n1 = command();
270	if (readtoken() == TPIPE) {
271		pipenode = (union node *)stalloc(sizeof (struct npipe));
272		pipenode->type = NPIPE;
273		pipenode->npipe.backgnd = 0;
274		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
275		pipenode->npipe.cmdlist = lp;
276		lp->n = n1;
277		do {
278			prev = lp;
279			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
280			lp->n = command();
281			prev->next = lp;
282		} while (readtoken() == TPIPE);
283		lp->next = NULL;
284		n1 = pipenode;
285	}
286	tokpushback++;
287	if (negate) {
288		notnode = (union node *)stalloc(sizeof(struct nnot));
289		notnode->type = NNOT;
290		notnode->nnot.com = n1;
291		n1 = notnode;
292	}
293	return n1;
294}
295
296
297
298STATIC union node *
299command() {
300	union node *n1, *n2;
301	union node *ap, **app;
302	union node *cp, **cpp;
303	union node *redir, **rpp;
304	int t;
305
306	checkkwd = 2;
307	redir = NULL;
308	n1 = NULL;
309	rpp = &redir;
310
311	/* Check for redirection which may precede command */
312	while (readtoken() == TREDIR) {
313		*rpp = n2 = redirnode;
314		rpp = &n2->nfile.next;
315		parsefname();
316	}
317	tokpushback++;
318
319	switch (readtoken()) {
320	case TIF:
321		n1 = (union node *)stalloc(sizeof (struct nif));
322		n1->type = NIF;
323		n1->nif.test = list(0);
324		if (readtoken() != TTHEN)
325			synexpect(TTHEN);
326		n1->nif.ifpart = list(0);
327		n2 = n1;
328		while (readtoken() == TELIF) {
329			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
330			n2 = n2->nif.elsepart;
331			n2->type = NIF;
332			n2->nif.test = list(0);
333			if (readtoken() != TTHEN)
334				synexpect(TTHEN);
335			n2->nif.ifpart = list(0);
336		}
337		if (lasttoken == TELSE)
338			n2->nif.elsepart = list(0);
339		else {
340			n2->nif.elsepart = NULL;
341			tokpushback++;
342		}
343		if (readtoken() != TFI)
344			synexpect(TFI);
345		checkkwd = 1;
346		break;
347	case TWHILE:
348	case TUNTIL: {
349		int got;
350		n1 = (union node *)stalloc(sizeof (struct nbinary));
351		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
352		n1->nbinary.ch1 = list(0);
353		if ((got=readtoken()) != TDO) {
354TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
355			synexpect(TDO);
356		}
357		n1->nbinary.ch2 = list(0);
358		if (readtoken() != TDONE)
359			synexpect(TDONE);
360		checkkwd = 1;
361		break;
362	}
363	case TFOR:
364		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
365			synerror("Bad for loop variable");
366		n1 = (union node *)stalloc(sizeof (struct nfor));
367		n1->type = NFOR;
368		n1->nfor.var = wordtext;
369		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
370			app = &ap;
371			while (readtoken() == TWORD) {
372				n2 = (union node *)stalloc(sizeof (struct narg));
373				n2->type = NARG;
374				n2->narg.text = wordtext;
375				n2->narg.backquote = backquotelist;
376				*app = n2;
377				app = &n2->narg.next;
378			}
379			*app = NULL;
380			n1->nfor.args = ap;
381			if (lasttoken != TNL && lasttoken != TSEMI)
382				synexpect(-1);
383		} else {
384#ifndef GDB_HACK
385			static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
386								   '@', '=', '\0'};
387#endif
388			n2 = (union node *)stalloc(sizeof (struct narg));
389			n2->type = NARG;
390			n2->narg.text = (char *)argvars;
391			n2->narg.backquote = NULL;
392			n2->narg.next = NULL;
393			n1->nfor.args = n2;
394			/*
395			 * Newline or semicolon here is optional (but note
396			 * that the original Bourne shell only allowed NL).
397			 */
398			if (lasttoken != TNL && lasttoken != TSEMI)
399				tokpushback++;
400		}
401		checkkwd = 2;
402		if ((t = readtoken()) == TDO)
403			t = TDONE;
404		else if (t == TBEGIN)
405			t = TEND;
406		else
407			synexpect(-1);
408		n1->nfor.body = list(0);
409		if (readtoken() != t)
410			synexpect(t);
411		checkkwd = 1;
412		break;
413	case TCASE:
414		n1 = (union node *)stalloc(sizeof (struct ncase));
415		n1->type = NCASE;
416		if (readtoken() != TWORD)
417			synexpect(TWORD);
418		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
419		n2->type = NARG;
420		n2->narg.text = wordtext;
421		n2->narg.backquote = backquotelist;
422		n2->narg.next = NULL;
423		while (readtoken() == TNL);
424		if (lasttoken != TWORD || ! equal(wordtext, "in"))
425			synerror("expecting \"in\"");
426		cpp = &n1->ncase.cases;
427		noaliases = 1;	/* turn off alias expansion */
428		checkkwd = 2, readtoken();
429		do {
430			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
431			cp->type = NCLIST;
432			app = &cp->nclist.pattern;
433			for (;;) {
434				*app = ap = (union node *)stalloc(sizeof (struct narg));
435				ap->type = NARG;
436				ap->narg.text = wordtext;
437				ap->narg.backquote = backquotelist;
438				if (checkkwd = 2, readtoken() != TPIPE)
439					break;
440				app = &ap->narg.next;
441				readtoken();
442			}
443			ap->narg.next = NULL;
444			if (lasttoken != TRP)
445				noaliases = 0, synexpect(TRP);
446			cp->nclist.body = list(0);
447
448			checkkwd = 2;
449			if ((t = readtoken()) != TESAC) {
450				if (t != TENDCASE)
451					noaliases = 0, synexpect(TENDCASE);
452				else
453					checkkwd = 2, readtoken();
454			}
455			cpp = &cp->nclist.next;
456		} while(lasttoken != TESAC);
457		noaliases = 0;	/* reset alias expansion */
458		*cpp = NULL;
459		checkkwd = 1;
460		break;
461	case TLP:
462		n1 = (union node *)stalloc(sizeof (struct nredir));
463		n1->type = NSUBSHELL;
464		n1->nredir.n = list(0);
465		n1->nredir.redirect = NULL;
466		if (readtoken() != TRP)
467			synexpect(TRP);
468		checkkwd = 1;
469		break;
470	case TBEGIN:
471		n1 = list(0);
472		if (readtoken() != TEND)
473			synexpect(TEND);
474		checkkwd = 1;
475		break;
476	/* Handle an empty command like other simple commands.  */
477	case TSEMI:
478		/*
479		 * An empty command before a ; doesn't make much sense, and
480		 * should certainly be disallowed in the case of `if ;'.
481		 */
482		if (!redir)
483			synexpect(-1);
484	case TAND:
485	case TOR:
486	case TNL:
487	case TEOF:
488	case TWORD:
489	case TRP:
490		tokpushback++;
491		return simplecmd(rpp, redir);
492	default:
493		synexpect(-1);
494	}
495
496	/* Now check for redirection which may follow command */
497	while (readtoken() == TREDIR) {
498		*rpp = n2 = redirnode;
499		rpp = &n2->nfile.next;
500		parsefname();
501	}
502	tokpushback++;
503	*rpp = NULL;
504	if (redir) {
505		if (n1->type != NSUBSHELL) {
506			n2 = (union node *)stalloc(sizeof (struct nredir));
507			n2->type = NREDIR;
508			n2->nredir.n = n1;
509			n1 = n2;
510		}
511		n1->nredir.redirect = redir;
512	}
513	return n1;
514}
515
516
517STATIC union node *
518simplecmd(rpp, redir)
519	union node **rpp, *redir;
520	{
521	union node *args, **app;
522	union node **orig_rpp = rpp;
523	union node *n = NULL;
524
525	/* If we don't have any redirections already, then we must reset */
526	/* rpp to be the address of the local redir variable.  */
527	if (redir == 0)
528		rpp = &redir;
529
530	args = NULL;
531	app = &args;
532	/*
533	 * We save the incoming value, because we need this for shell
534	 * functions.  There can not be a redirect or an argument between
535	 * the function name and the open parenthesis.
536	 */
537	orig_rpp = rpp;
538
539	for (;;) {
540		if (readtoken() == TWORD) {
541			n = (union node *)stalloc(sizeof (struct narg));
542			n->type = NARG;
543			n->narg.text = wordtext;
544			n->narg.backquote = backquotelist;
545			*app = n;
546			app = &n->narg.next;
547		} else if (lasttoken == TREDIR) {
548			*rpp = n = redirnode;
549			rpp = &n->nfile.next;
550			parsefname();	/* read name of redirection file */
551		} else if (lasttoken == TLP && app == &args->narg.next
552					    && rpp == orig_rpp) {
553			/* We have a function */
554			if (readtoken() != TRP)
555				synexpect(TRP);
556#ifdef notdef
557			if (! goodname(n->narg.text))
558				synerror("Bad function name");
559#endif
560			n->type = NDEFUN;
561			n->narg.next = command();
562			return n;
563		} else {
564			tokpushback++;
565			break;
566		}
567	}
568	*app = NULL;
569	*rpp = NULL;
570	n = (union node *)stalloc(sizeof (struct ncmd));
571	n->type = NCMD;
572	n->ncmd.backgnd = 0;
573	n->ncmd.args = args;
574	n->ncmd.redirect = redir;
575	return n;
576}
577
578STATIC union node *
579makename() {
580	union node *n;
581
582	n = (union node *)stalloc(sizeof (struct narg));
583	n->type = NARG;
584	n->narg.next = NULL;
585	n->narg.text = wordtext;
586	n->narg.backquote = backquotelist;
587	return n;
588}
589
590void fixredir(n, text, err)
591	union node *n;
592	const char *text;
593	int err;
594	{
595	TRACE(("Fix redir %s %d\n", text, err));
596	if (!err)
597		n->ndup.vname = NULL;
598
599	if (is_digit(text[0]) && text[1] == '\0')
600		n->ndup.dupfd = digit_val(text[0]);
601	else if (text[0] == '-' && text[1] == '\0')
602		n->ndup.dupfd = -1;
603	else {
604
605		if (err)
606			synerror("Bad fd number");
607		else
608			n->ndup.vname = makename();
609	}
610}
611
612
613STATIC void
614parsefname() {
615	union node *n = redirnode;
616
617	if (readtoken() != TWORD)
618		synexpect(-1);
619	if (n->type == NHERE) {
620		struct heredoc *here = heredoc;
621		struct heredoc *p;
622		int i;
623
624		if (quoteflag == 0)
625			n->type = NXHERE;
626		TRACE(("Here document %d\n", n->type));
627		if (here->striptabs) {
628			while (*wordtext == '\t')
629				wordtext++;
630		}
631		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
632			synerror("Illegal eof marker for << redirection");
633		rmescapes(wordtext);
634		here->eofmark = wordtext;
635		here->next = NULL;
636		if (heredoclist == NULL)
637			heredoclist = here;
638		else {
639			for (p = heredoclist ; p->next ; p = p->next);
640			p->next = here;
641		}
642	} else if (n->type == NTOFD || n->type == NFROMFD) {
643		fixredir(n, wordtext, 0);
644	} else {
645		n->nfile.fname = makename();
646	}
647}
648
649
650/*
651 * Input any here documents.
652 */
653
654STATIC void
655parseheredoc() {
656	struct heredoc *here;
657	union node *n;
658
659	while (heredoclist) {
660		here = heredoclist;
661		heredoclist = here->next;
662		if (needprompt) {
663			setprompt(2);
664			needprompt = 0;
665		}
666		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
667				here->eofmark, here->striptabs);
668		n = (union node *)stalloc(sizeof (struct narg));
669		n->narg.type = NARG;
670		n->narg.next = NULL;
671		n->narg.text = wordtext;
672		n->narg.backquote = backquotelist;
673		here->here->nhere.doc = n;
674	}
675}
676
677STATIC int
678peektoken() {
679	int t;
680
681	t = readtoken();
682	tokpushback++;
683	return (t);
684}
685
686STATIC int
687readtoken() {
688	int t;
689	int savecheckkwd = checkkwd;
690	struct alias *ap;
691#ifdef DEBUG
692	int alreadyseen = tokpushback;
693#endif
694
695	top:
696	t = xxreadtoken();
697
698	if (checkkwd) {
699		/*
700		 * eat newlines
701		 */
702		if (checkkwd == 2) {
703			checkkwd = 0;
704			while (t == TNL) {
705				parseheredoc();
706				t = xxreadtoken();
707			}
708		} else
709			checkkwd = 0;
710		/*
711		 * check for keywords and aliases
712		 */
713		if (t == TWORD && !quoteflag)
714		{
715			char * const *pp;
716
717			for (pp = (char **)parsekwd; *pp; pp++) {
718				if (**pp == *wordtext && equal(*pp, wordtext))
719				{
720					lasttoken = t = pp - parsekwd + KWDOFFSET;
721					TRACE(("keyword %s recognized\n", tokname[t]));
722					goto out;
723				}
724			}
725			if (noaliases == 0 &&
726			    (ap = lookupalias(wordtext, 1)) != NULL) {
727				pushstring(ap->val, strlen(ap->val), ap);
728				checkkwd = savecheckkwd;
729				goto top;
730			}
731		}
732out:
733		checkkwd = 0;
734	}
735#ifdef DEBUG
736	if (!alreadyseen)
737	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
738	else
739	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
740#endif
741	return (t);
742}
743
744
745/*
746 * Read the next input token.
747 * If the token is a word, we set backquotelist to the list of cmds in
748 *	backquotes.  We set quoteflag to true if any part of the word was
749 *	quoted.
750 * If the token is TREDIR, then we set redirnode to a structure containing
751 *	the redirection.
752 * In all cases, the variable startlinno is set to the number of the line
753 *	on which the token starts.
754 *
755 * [Change comment:  here documents and internal procedures]
756 * [Readtoken shouldn't have any arguments.  Perhaps we should make the
757 *  word parsing code into a separate routine.  In this case, readtoken
758 *  doesn't need to have any internal procedures, but parseword does.
759 *  We could also make parseoperator in essence the main routine, and
760 *  have parseword (readtoken1?) handle both words and redirection.]
761 */
762
763#define RETURN(token)	return lasttoken = token
764
765STATIC int
766xxreadtoken() {
767	int c;
768
769	if (tokpushback) {
770		tokpushback = 0;
771		return lasttoken;
772	}
773	if (needprompt) {
774		setprompt(2);
775		needprompt = 0;
776	}
777	startlinno = plinno;
778	for (;;) {	/* until token or start of word found */
779		c = pgetc_macro();
780		if (c == ' ' || c == '\t')
781			continue;		/* quick check for white space first */
782		switch (c) {
783		case ' ': case '\t':
784			continue;
785		case '#':
786			while ((c = pgetc()) != '\n' && c != PEOF);
787			pungetc();
788			continue;
789		case '\\':
790			if (pgetc() == '\n') {
791				startlinno = ++plinno;
792				if (doprompt)
793					setprompt(2);
794				else
795					setprompt(0);
796				continue;
797			}
798			pungetc();
799			goto breakloop;
800		case '\n':
801			plinno++;
802			needprompt = doprompt;
803			RETURN(TNL);
804		case PEOF:
805			RETURN(TEOF);
806		case '&':
807			if (pgetc() == '&')
808				RETURN(TAND);
809			pungetc();
810			RETURN(TBACKGND);
811		case '|':
812			if (pgetc() == '|')
813				RETURN(TOR);
814			pungetc();
815			RETURN(TPIPE);
816		case ';':
817			if (pgetc() == ';')
818				RETURN(TENDCASE);
819			pungetc();
820			RETURN(TSEMI);
821		case '(':
822			RETURN(TLP);
823		case ')':
824			RETURN(TRP);
825		default:
826			goto breakloop;
827		}
828	}
829breakloop:
830	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
831#undef RETURN
832}
833
834
835
836/*
837 * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
838 * is not NULL, read a here document.  In the latter case, eofmark is the
839 * word which marks the end of the document and striptabs is true if
840 * leading tabs should be stripped from the document.  The argument firstc
841 * is the first character of the input token or document.
842 *
843 * Because C does not have internal subroutines, I have simulated them
844 * using goto's to implement the subroutine linkage.  The following macros
845 * will run code that appears at the end of readtoken1.
846 */
847
848#define CHECKEND()	{goto checkend; checkend_return:;}
849#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
850#define PARSESUB()	{goto parsesub; parsesub_return:;}
851#define PARSEBACKQOLD()	{oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
852#define PARSEBACKQNEW()	{oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
853#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
854
855STATIC int
856readtoken1(firstc, syntax, eofmark, striptabs)
857	int firstc;
858	char const *syntax;
859	char *eofmark;
860	int striptabs;
861	{
862	int c = firstc;
863	char *out;
864	int len;
865	char line[EOFMARKLEN + 1];
866	struct nodelist *bqlist;
867	int quotef;
868	int dblquote;
869	int varnest;	/* levels of variables expansion */
870	int arinest;	/* levels of arithmetic expansion */
871	int parenlevel;	/* levels of parens in arithmetic */
872	int oldstyle;
873	char const *prevsyntax;	/* syntax before arithmetic */
874	int synentry;
875#if __GNUC__
876	/* Avoid longjmp clobbering */
877	(void) &out;
878	(void) &quotef;
879	(void) &dblquote;
880	(void) &varnest;
881	(void) &arinest;
882	(void) &parenlevel;
883	(void) &oldstyle;
884	(void) &prevsyntax;
885	(void) &syntax;
886	(void) &synentry;
887#endif
888
889	startlinno = plinno;
890	dblquote = 0;
891	if (syntax == DQSYNTAX)
892		dblquote = 1;
893	quotef = 0;
894	bqlist = NULL;
895	varnest = 0;
896	arinest = 0;
897	parenlevel = 0;
898
899	STARTSTACKSTR(out);
900	loop: {	/* for each line, until end of word */
901#if ATTY
902		if (c == '\034' && doprompt
903		 && attyset() && ! equal(termval(), "emacs")) {
904			attyline();
905			if (syntax == BASESYNTAX)
906				return readtoken();
907			c = pgetc();
908			goto loop;
909		}
910#endif
911		CHECKEND();	/* set c to PEOF if at end of here document */
912		for (;;) {	/* until end of line or end of word */
913			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
914
915			synentry = syntax[c];
916
917			switch(synentry) {
918			case CNL:	/* '\n' */
919				if (syntax == BASESYNTAX)
920					goto endword;	/* exit outer loop */
921				USTPUTC(c, out);
922				plinno++;
923				if (doprompt)
924					setprompt(2);
925				else
926					setprompt(0);
927				c = pgetc();
928				goto loop;		/* continue outer loop */
929			case CWORD:
930				USTPUTC(c, out);
931				break;
932			case CCTL:
933				if (eofmark == NULL || dblquote)
934					USTPUTC(CTLESC, out);
935				USTPUTC(c, out);
936				break;
937			case CBACK:	/* backslash */
938				c = pgetc();
939				if (c == PEOF) {
940					USTPUTC('\\', out);
941					pungetc();
942				} else if (c == '\n') {
943					if (doprompt)
944						setprompt(2);
945					else
946						setprompt(0);
947				} else {
948					if (dblquote && c != '\\' &&
949					    c != '`' && c != '$' &&
950					    (c != '"' || eofmark != NULL))
951						USTPUTC('\\', out);
952					if (c >= 0 && SQSYNTAX[c] == CCTL)
953						USTPUTC(CTLESC, out);
954					else if (eofmark == NULL)
955						USTPUTC(CTLQUOTEMARK, out);
956					USTPUTC(c, out);
957					quotef++;
958				}
959				break;
960			case CSQUOTE:
961				if (eofmark == NULL)
962					USTPUTC(CTLQUOTEMARK, out);
963				syntax = SQSYNTAX;
964				break;
965			case CDQUOTE:
966				if (eofmark == NULL)
967					USTPUTC(CTLQUOTEMARK, out);
968				syntax = DQSYNTAX;
969				dblquote = 1;
970				break;
971			case CENDQUOTE:
972				if (eofmark != NULL && arinest == 0 &&
973				    varnest == 0) {
974					USTPUTC(c, out);
975				} else {
976					if (arinest) {
977						syntax = ARISYNTAX;
978						dblquote = 0;
979					} else if (eofmark == NULL) {
980						syntax = BASESYNTAX;
981						dblquote = 0;
982					}
983					quotef++;
984				}
985				break;
986			case CVAR:	/* '$' */
987				PARSESUB();		/* parse substitution */
988				break;
989			case CENDVAR:	/* '}' */
990				if (varnest > 0) {
991					varnest--;
992					USTPUTC(CTLENDVAR, out);
993				} else {
994					USTPUTC(c, out);
995				}
996				break;
997			case CLP:	/* '(' in arithmetic */
998				parenlevel++;
999				USTPUTC(c, out);
1000				break;
1001			case CRP:	/* ')' in arithmetic */
1002				if (parenlevel > 0) {
1003					USTPUTC(c, out);
1004					--parenlevel;
1005				} else {
1006					if (pgetc() == ')') {
1007						if (--arinest == 0) {
1008							USTPUTC(CTLENDARI, out);
1009							syntax = prevsyntax;
1010							if (syntax == DQSYNTAX)
1011								dblquote = 1;
1012							else
1013								dblquote = 0;
1014						} else
1015							USTPUTC(')', out);
1016					} else {
1017						/*
1018						 * unbalanced parens
1019						 *  (don't 2nd guess - no error)
1020						 */
1021						pungetc();
1022						USTPUTC(')', out);
1023					}
1024				}
1025				break;
1026			case CBQUOTE:	/* '`' */
1027				PARSEBACKQOLD();
1028				break;
1029			case CEOF:
1030				goto endword;		/* exit outer loop */
1031			default:
1032				if (varnest == 0)
1033					goto endword;	/* exit outer loop */
1034				USTPUTC(c, out);
1035			}
1036			c = pgetc_macro();
1037		}
1038	}
1039endword:
1040	if (syntax == ARISYNTAX)
1041		synerror("Missing '))'");
1042	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1043		synerror("Unterminated quoted string");
1044	if (varnest != 0) {
1045		startlinno = plinno;
1046		synerror("Missing '}'");
1047	}
1048	USTPUTC('\0', out);
1049	len = out - stackblock();
1050	out = stackblock();
1051	if (eofmark == NULL) {
1052		if ((c == '>' || c == '<')
1053		 && quotef == 0
1054		 && len <= 2
1055		 && (*out == '\0' || is_digit(*out))) {
1056			PARSEREDIR();
1057			return lasttoken = TREDIR;
1058		} else {
1059			pungetc();
1060		}
1061	}
1062	quoteflag = quotef;
1063	backquotelist = bqlist;
1064	grabstackblock(len);
1065	wordtext = out;
1066	return lasttoken = TWORD;
1067/* end of readtoken routine */
1068
1069
1070
1071/*
1072 * Check to see whether we are at the end of the here document.  When this
1073 * is called, c is set to the first character of the next input line.  If
1074 * we are at the end of the here document, this routine sets the c to PEOF.
1075 */
1076
1077checkend: {
1078	if (eofmark) {
1079		if (striptabs) {
1080			while (c == '\t')
1081				c = pgetc();
1082		}
1083		if (c == *eofmark) {
1084			if (pfgets(line, sizeof line) != NULL) {
1085				char *p, *q;
1086
1087				p = line;
1088				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1089				if (*p == '\n' && *q == '\0') {
1090					c = PEOF;
1091					plinno++;
1092					needprompt = doprompt;
1093				} else {
1094					pushstring(line, strlen(line), NULL);
1095				}
1096			}
1097		}
1098	}
1099	goto checkend_return;
1100}
1101
1102
1103/*
1104 * Parse a redirection operator.  The variable "out" points to a string
1105 * specifying the fd to be redirected.  The variable "c" contains the
1106 * first character of the redirection operator.
1107 */
1108
1109parseredir: {
1110	char fd = *out;
1111	union node *np;
1112
1113	np = (union node *)stalloc(sizeof (struct nfile));
1114	if (c == '>') {
1115		np->nfile.fd = 1;
1116		c = pgetc();
1117		if (c == '>')
1118			np->type = NAPPEND;
1119		else if (c == '&')
1120			np->type = NTOFD;
1121		else {
1122			np->type = NTO;
1123			pungetc();
1124		}
1125	} else {	/* c == '<' */
1126		np->nfile.fd = 0;
1127		c = pgetc();
1128		if (c == '<') {
1129			if (sizeof (struct nfile) != sizeof (struct nhere)) {
1130				np = (union node *)stalloc(sizeof (struct nhere));
1131				np->nfile.fd = 0;
1132			}
1133			np->type = NHERE;
1134			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1135			heredoc->here = np;
1136			if ((c = pgetc()) == '-') {
1137				heredoc->striptabs = 1;
1138			} else {
1139				heredoc->striptabs = 0;
1140				pungetc();
1141			}
1142		} else if (c == '&')
1143			np->type = NFROMFD;
1144		else if (c == '>')
1145			np->type = NFROMTO;
1146		else {
1147			np->type = NFROM;
1148			pungetc();
1149		}
1150	}
1151	if (fd != '\0')
1152		np->nfile.fd = digit_val(fd);
1153	redirnode = np;
1154	goto parseredir_return;
1155}
1156
1157
1158/*
1159 * Parse a substitution.  At this point, we have read the dollar sign
1160 * and nothing else.
1161 */
1162
1163parsesub: {
1164	int subtype;
1165	int typeloc;
1166	int flags;
1167	char *p;
1168#ifndef GDB_HACK
1169	static const char types[] = "}-+?=";
1170#endif
1171       int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1172
1173	c = pgetc();
1174	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
1175		USTPUTC('$', out);
1176		pungetc();
1177	} else if (c == '(') {	/* $(command) or $((arith)) */
1178		if (pgetc() == '(') {
1179			PARSEARITH();
1180		} else {
1181			pungetc();
1182			PARSEBACKQNEW();
1183		}
1184	} else {
1185		USTPUTC(CTLVAR, out);
1186		typeloc = out - stackblock();
1187		USTPUTC(VSNORMAL, out);
1188		subtype = VSNORMAL;
1189		if (c == '{') {
1190			bracketed_name = 1;
1191			c = pgetc();
1192			if (c == '#') {
1193				if ((c = pgetc()) == '}')
1194					c = '#';
1195				else
1196					subtype = VSLENGTH;
1197			}
1198			else
1199				subtype = 0;
1200		}
1201		if (is_name(c)) {
1202			do {
1203				STPUTC(c, out);
1204				c = pgetc();
1205			} while (is_in_name(c));
1206		} else if (is_digit(c)) {
1207			if (bracketed_name) {
1208				do {
1209					STPUTC(c, out);
1210					c = pgetc();
1211				} while (is_digit(c));
1212			} else {
1213				STPUTC(c, out);
1214				c = pgetc();
1215			}
1216		} else {
1217			if (! is_special(c))
1218badsub:				synerror("Bad substitution");
1219			USTPUTC(c, out);
1220			c = pgetc();
1221		}
1222		STPUTC('=', out);
1223		flags = 0;
1224		if (subtype == 0) {
1225			switch (c) {
1226			case ':':
1227				flags = VSNUL;
1228				c = pgetc();
1229				/*FALLTHROUGH*/
1230			default:
1231				p = strchr(types, c);
1232				if (p == NULL)
1233					goto badsub;
1234				subtype = p - types + VSNORMAL;
1235				break;
1236			case '%':
1237			case '#':
1238				{
1239					int cc = c;
1240					subtype = c == '#' ? VSTRIMLEFT :
1241							     VSTRIMRIGHT;
1242					c = pgetc();
1243					if (c == cc)
1244						subtype++;
1245					else
1246						pungetc();
1247					break;
1248				}
1249			}
1250		} else {
1251			pungetc();
1252		}
1253		if (subtype != VSLENGTH && (dblquote || arinest))
1254			flags |= VSQUOTE;
1255		*(stackblock() + typeloc) = subtype | flags;
1256		if (subtype != VSNORMAL)
1257			varnest++;
1258	}
1259	goto parsesub_return;
1260}
1261
1262
1263/*
1264 * Called to parse command substitutions.  Newstyle is set if the command
1265 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1266 * list of commands (passed by reference), and savelen is the number of
1267 * characters on the top of the stack which must be preserved.
1268 */
1269
1270parsebackq: {
1271	struct nodelist **nlpp;
1272	int savepbq;
1273	union node *n;
1274	char *volatile str;
1275	struct jmploc jmploc;
1276	struct jmploc *volatile savehandler;
1277	int savelen;
1278	int saveprompt;
1279#if __GNUC__
1280	/* Avoid longjmp clobbering */
1281	(void) &saveprompt;
1282#endif
1283
1284	savepbq = parsebackquote;
1285	if (setjmp(jmploc.loc)) {
1286		if (str)
1287			ckfree(str);
1288		parsebackquote = 0;
1289		handler = savehandler;
1290		longjmp(handler->loc, 1);
1291	}
1292	INTOFF;
1293	str = NULL;
1294	savelen = out - stackblock();
1295	if (savelen > 0) {
1296		str = ckmalloc(savelen);
1297		memcpy(str, stackblock(), savelen);
1298	}
1299	savehandler = handler;
1300	handler = &jmploc;
1301	INTON;
1302        if (oldstyle) {
1303                /* We must read until the closing backquote, giving special
1304                   treatment to some slashes, and then push the string and
1305                   reread it as input, interpreting it normally.  */
1306                char *out;
1307                int c;
1308                int savelen;
1309                char *str;
1310
1311
1312                STARTSTACKSTR(out);
1313		for (;;) {
1314			if (needprompt) {
1315				setprompt(2);
1316				needprompt = 0;
1317			}
1318			switch (c = pgetc()) {
1319			case '`':
1320				goto done;
1321
1322			case '\\':
1323                                if ((c = pgetc()) == '\n') {
1324					plinno++;
1325					if (doprompt)
1326						setprompt(2);
1327					else
1328						setprompt(0);
1329					/*
1330					 * If eating a newline, avoid putting
1331					 * the newline into the new character
1332					 * stream (via the STPUTC after the
1333					 * switch).
1334					 */
1335					continue;
1336				}
1337                                if (c != '\\' && c != '`' && c != '$'
1338                                    && (!dblquote || c != '"'))
1339                                        STPUTC('\\', out);
1340				break;
1341
1342			case '\n':
1343				plinno++;
1344				needprompt = doprompt;
1345				break;
1346
1347			case PEOF:
1348			        startlinno = plinno;
1349				synerror("EOF in backquote substitution");
1350 				break;
1351
1352			default:
1353				break;
1354			}
1355			STPUTC(c, out);
1356                }
1357done:
1358                STPUTC('\0', out);
1359                savelen = out - stackblock();
1360                if (savelen > 0) {
1361                        str = ckmalloc(savelen);
1362                        memcpy(str, stackblock(), savelen);
1363			setinputstring(str, 1);
1364                }
1365        }
1366	nlpp = &bqlist;
1367	while (*nlpp)
1368		nlpp = &(*nlpp)->next;
1369	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1370	(*nlpp)->next = NULL;
1371	parsebackquote = oldstyle;
1372
1373	if (oldstyle) {
1374		saveprompt = doprompt;
1375		doprompt = 0;
1376	}
1377
1378	n = list(0);
1379
1380	if (oldstyle)
1381		doprompt = saveprompt;
1382	else {
1383		if (readtoken() != TRP)
1384			synexpect(TRP);
1385	}
1386
1387	(*nlpp)->n = n;
1388        if (oldstyle) {
1389		/*
1390		 * Start reading from old file again, ignoring any pushed back
1391		 * tokens left from the backquote parsing
1392		 */
1393                popfile();
1394		tokpushback = 0;
1395	}
1396	while (stackblocksize() <= savelen)
1397		growstackblock();
1398	STARTSTACKSTR(out);
1399	if (str) {
1400		memcpy(out, str, savelen);
1401		STADJUST(savelen, out);
1402		INTOFF;
1403		ckfree(str);
1404		str = NULL;
1405		INTON;
1406	}
1407	parsebackquote = savepbq;
1408	handler = savehandler;
1409	if (arinest || dblquote)
1410		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1411	else
1412		USTPUTC(CTLBACKQ, out);
1413	if (oldstyle)
1414		goto parsebackq_oldreturn;
1415	else
1416		goto parsebackq_newreturn;
1417}
1418
1419/*
1420 * Parse an arithmetic expansion (indicate start of one and set state)
1421 */
1422parsearith: {
1423
1424	if (++arinest == 1) {
1425		prevsyntax = syntax;
1426		syntax = ARISYNTAX;
1427		USTPUTC(CTLARI, out);
1428		if (dblquote)
1429			USTPUTC('"',out);
1430		else
1431			USTPUTC(' ',out);
1432	} else {
1433		/*
1434		 * we collapse embedded arithmetic expansion to
1435		 * parenthesis, which should be equivalent
1436		 */
1437		USTPUTC('(', out);
1438	}
1439	goto parsearith_return;
1440}
1441
1442} /* end of readtoken */
1443
1444
1445
1446#ifdef mkinit
1447RESET {
1448	tokpushback = 0;
1449	checkkwd = 0;
1450}
1451#endif
1452
1453/*
1454 * Returns true if the text contains nothing to expand (no dollar signs
1455 * or backquotes).
1456 */
1457
1458STATIC int
1459noexpand(text)
1460	char *text;
1461	{
1462	char *p;
1463	char c;
1464
1465	p = text;
1466	while ((c = *p++) != '\0') {
1467		if ( c == CTLQUOTEMARK)
1468			continue;
1469		if (c == CTLESC)
1470			p++;
1471		else if (c >= 0 && BASESYNTAX[(int)c] == CCTL)
1472			return 0;
1473	}
1474	return 1;
1475}
1476
1477
1478/*
1479 * Return true if the argument is a legal variable name (a letter or
1480 * underscore followed by zero or more letters, underscores, and digits).
1481 */
1482
1483int
1484goodname(name)
1485	char *name;
1486	{
1487	char *p;
1488
1489	p = name;
1490	if (! is_name(*p))
1491		return 0;
1492	while (*++p) {
1493		if (! is_in_name(*p))
1494			return 0;
1495	}
1496	return 1;
1497}
1498
1499
1500/*
1501 * Called when an unexpected token is read during the parse.  The argument
1502 * is the token that is expected, or -1 if more than one type of token can
1503 * occur at this point.
1504 */
1505
1506STATIC void
1507synexpect(token)
1508	int token;
1509{
1510	char msg[64];
1511
1512	if (token >= 0) {
1513		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1514			tokname[lasttoken], tokname[token]);
1515	} else {
1516		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1517	}
1518	synerror(msg);
1519}
1520
1521
1522STATIC void
1523synerror(msg)
1524	char *msg;
1525	{
1526	if (commandname)
1527		outfmt(&errout, "%s: %d: ", commandname, startlinno);
1528	outfmt(&errout, "Syntax error: %s\n", msg);
1529	error((char *)NULL);
1530}
1531
1532STATIC void
1533setprompt(which)
1534	int which;
1535	{
1536	whichprompt = which;
1537
1538#ifndef NO_HISTORY
1539	if (!el)
1540#endif
1541		out2str(getprompt(NULL));
1542}
1543
1544/*
1545 * called by editline -- any expansions to the prompt
1546 *    should be added here.
1547 */
1548char *
1549getprompt(unused)
1550	void *unused __unused;
1551{
1552	switch (whichprompt) {
1553	case 0:
1554		return "";
1555	case 1:
1556		return ps1val();
1557	case 2:
1558		return ps2val();
1559	default:
1560		return "<internal prompt error>";
1561	}
1562}
1563