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