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