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