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