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