xref: /freebsd/bin/sh/parser.c (revision 70ed590b393173d4ea697be2a27054ed171f0c1a)
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
35 static char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
36 #endif
37 #endif /* not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
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 
75 struct heredoc {
76 	struct heredoc *next;	/* next here document in list */
77 	union node *here;		/* redirection node */
78 	char *eofmark;		/* string indicating end of input */
79 	int striptabs;		/* if set, strip leading tabs */
80 };
81 
82 struct parser_temp {
83 	struct parser_temp *next;
84 	void *data;
85 };
86 
87 
88 STATIC struct heredoc *heredoclist;	/* list of here documents to read */
89 STATIC int doprompt;		/* if set, prompt the user */
90 STATIC int needprompt;		/* true if interactive and at start of line */
91 STATIC int lasttoken;		/* last token read */
92 MKINIT int tokpushback;		/* last token pushed back */
93 STATIC char *wordtext;		/* text of last word returned by readtoken */
94 MKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
95 STATIC struct nodelist *backquotelist;
96 STATIC union node *redirnode;
97 STATIC struct heredoc *heredoc;
98 STATIC int quoteflag;		/* set if (part of) last token was quoted */
99 STATIC int startlinno;		/* line # where last token started */
100 STATIC int funclinno;		/* line # where the current function started */
101 STATIC struct parser_temp *parser_temp;
102 
103 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
104 static int noaliases = 0;
105 
106 
107 STATIC union node *list(int);
108 STATIC union node *andor(void);
109 STATIC union node *pipeline(void);
110 STATIC union node *command(void);
111 STATIC union node *simplecmd(union node **, union node *);
112 STATIC union node *makename(void);
113 STATIC void parsefname(void);
114 STATIC void parseheredoc(void);
115 STATIC int peektoken(void);
116 STATIC int readtoken(void);
117 STATIC int xxreadtoken(void);
118 STATIC int readtoken1(int, char const *, char *, int);
119 STATIC int noexpand(char *);
120 STATIC void synexpect(int);
121 STATIC void synerror(const char *);
122 STATIC void setprompt(int);
123 
124 
125 STATIC void *
126 parser_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 
141 STATIC void *
142 parser_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 
156 STATIC void
157 parser_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 
176 STATIC void
177 parser_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 
197 union node *
198 parsecmd(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 
224 STATIC union node *
225 list(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 
294 STATIC union node *
295 andor(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 
321 STATIC union node *
322 pipeline(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 
363 STATIC union node *
364 command(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) {
429 TRACE(("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 
593 checkneg:
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 
605 STATIC union node *
606 simplecmd(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 
672 checkneg:
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 
683 STATIC union node *
684 makename(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 
696 void 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 
716 STATIC void
717 parsefname(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 
758 STATIC void
759 parseheredoc(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 
782 STATIC int
783 peektoken(void)
784 {
785 	int t;
786 
787 	t = readtoken();
788 	tokpushback++;
789 	return (t);
790 }
791 
792 STATIC int
793 readtoken(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 		}
839 out:
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 
872 STATIC int
873 xxreadtoken(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 	}
937 breakloop:
938 	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
939 #undef RETURN
940 }
941 
942 
943 #define MAXNEST_STATIC 8
944 struct 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 
962 STATIC char *
963 parsebackq(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                 }
1053 done:
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 
1134 STATIC int
1135 readtoken1(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 					state[level].syntax = BASESYNTAX;
1227 					quotef++;
1228 				}
1229 				break;
1230 			case CVAR:	/* '$' */
1231 				PARSESUB();		/* parse substitution */
1232 				break;
1233 			case CENDVAR:	/* '}' */
1234 				if (level > 0 &&
1235 				    (state[level].category == TSTATE_VAR_OLD ||
1236 				    state[level].category == TSTATE_VAR_NEW)) {
1237 					if (state[level].category == TSTATE_VAR_OLD)
1238 						state[level - 1].syntax = state[level].syntax;
1239 					else
1240 						newvarnest--;
1241 					level--;
1242 					USTPUTC(CTLENDVAR, out);
1243 				} else {
1244 					USTPUTC(c, out);
1245 				}
1246 				break;
1247 			case CLP:	/* '(' in arithmetic */
1248 				state[level].parenlevel++;
1249 				USTPUTC(c, out);
1250 				break;
1251 			case CRP:	/* ')' in arithmetic */
1252 				if (state[level].parenlevel > 0) {
1253 					USTPUTC(c, out);
1254 					--state[level].parenlevel;
1255 				} else {
1256 					if (pgetc() == ')') {
1257 						if (level > 0 &&
1258 						    state[level].category == TSTATE_ARITH) {
1259 							level--;
1260 							USTPUTC(CTLENDARI, out);
1261 						} else
1262 							USTPUTC(')', out);
1263 					} else {
1264 						/*
1265 						 * unbalanced parens
1266 						 *  (don't 2nd guess - no error)
1267 						 */
1268 						pungetc();
1269 						USTPUTC(')', out);
1270 					}
1271 				}
1272 				break;
1273 			case CBQUOTE:	/* '`' */
1274 				out = parsebackq(out, &bqlist, 1,
1275 				    state[level].syntax == DQSYNTAX &&
1276 				    (eofmark == NULL || newvarnest > 0),
1277 				    state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
1278 				break;
1279 			case CEOF:
1280 				goto endword;		/* exit outer loop */
1281 			default:
1282 				if (level == 0)
1283 					goto endword;	/* exit outer loop */
1284 				USTPUTC(c, out);
1285 			}
1286 			c = pgetc_macro();
1287 		}
1288 	}
1289 endword:
1290 	if (state[level].syntax == ARISYNTAX)
1291 		synerror("Missing '))'");
1292 	if (state[level].syntax != BASESYNTAX && eofmark == NULL)
1293 		synerror("Unterminated quoted string");
1294 	if (state[level].category == TSTATE_VAR_OLD ||
1295 	    state[level].category == TSTATE_VAR_NEW) {
1296 		startlinno = plinno;
1297 		synerror("Missing '}'");
1298 	}
1299 	if (state != state_static)
1300 		parser_temp_free_upto(state);
1301 	USTPUTC('\0', out);
1302 	len = out - stackblock();
1303 	out = stackblock();
1304 	if (eofmark == NULL) {
1305 		if ((c == '>' || c == '<')
1306 		 && quotef == 0
1307 		 && len <= 2
1308 		 && (*out == '\0' || is_digit(*out))) {
1309 			PARSEREDIR();
1310 			return lasttoken = TREDIR;
1311 		} else {
1312 			pungetc();
1313 		}
1314 	}
1315 	quoteflag = quotef;
1316 	backquotelist = bqlist;
1317 	grabstackblock(len);
1318 	wordtext = out;
1319 	return lasttoken = TWORD;
1320 /* end of readtoken routine */
1321 
1322 
1323 /*
1324  * Check to see whether we are at the end of the here document.  When this
1325  * is called, c is set to the first character of the next input line.  If
1326  * we are at the end of the here document, this routine sets the c to PEOF.
1327  */
1328 
1329 checkend: {
1330 	if (eofmark) {
1331 		if (striptabs) {
1332 			while (c == '\t')
1333 				c = pgetc();
1334 		}
1335 		if (c == *eofmark) {
1336 			if (pfgets(line, sizeof line) != NULL) {
1337 				char *p, *q;
1338 
1339 				p = line;
1340 				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1341 				if (*p == '\n' && *q == '\0') {
1342 					c = PEOF;
1343 					plinno++;
1344 					needprompt = doprompt;
1345 				} else {
1346 					pushstring(line, strlen(line), NULL);
1347 				}
1348 			}
1349 		}
1350 	}
1351 	goto checkend_return;
1352 }
1353 
1354 
1355 /*
1356  * Parse a redirection operator.  The variable "out" points to a string
1357  * specifying the fd to be redirected.  The variable "c" contains the
1358  * first character of the redirection operator.
1359  */
1360 
1361 parseredir: {
1362 	char fd = *out;
1363 	union node *np;
1364 
1365 	np = (union node *)stalloc(sizeof (struct nfile));
1366 	if (c == '>') {
1367 		np->nfile.fd = 1;
1368 		c = pgetc();
1369 		if (c == '>')
1370 			np->type = NAPPEND;
1371 		else if (c == '&')
1372 			np->type = NTOFD;
1373 		else if (c == '|')
1374 			np->type = NCLOBBER;
1375 		else {
1376 			np->type = NTO;
1377 			pungetc();
1378 		}
1379 	} else {	/* c == '<' */
1380 		np->nfile.fd = 0;
1381 		c = pgetc();
1382 		if (c == '<') {
1383 			if (sizeof (struct nfile) != sizeof (struct nhere)) {
1384 				np = (union node *)stalloc(sizeof (struct nhere));
1385 				np->nfile.fd = 0;
1386 			}
1387 			np->type = NHERE;
1388 			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1389 			heredoc->here = np;
1390 			if ((c = pgetc()) == '-') {
1391 				heredoc->striptabs = 1;
1392 			} else {
1393 				heredoc->striptabs = 0;
1394 				pungetc();
1395 			}
1396 		} else if (c == '&')
1397 			np->type = NFROMFD;
1398 		else if (c == '>')
1399 			np->type = NFROMTO;
1400 		else {
1401 			np->type = NFROM;
1402 			pungetc();
1403 		}
1404 	}
1405 	if (fd != '\0')
1406 		np->nfile.fd = digit_val(fd);
1407 	redirnode = np;
1408 	goto parseredir_return;
1409 }
1410 
1411 
1412 /*
1413  * Parse a substitution.  At this point, we have read the dollar sign
1414  * and nothing else.
1415  */
1416 
1417 parsesub: {
1418 	char buf[10];
1419 	int subtype;
1420 	int typeloc;
1421 	int flags;
1422 	char *p;
1423 	static const char types[] = "}-+?=";
1424 	int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1425 	int i;
1426 	int linno;
1427 	int length;
1428 
1429 	c = pgetc();
1430 	if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1431 	    !is_special(c)) {
1432 		USTPUTC('$', out);
1433 		pungetc();
1434 	} else if (c == '(') {	/* $(command) or $((arith)) */
1435 		if (pgetc() == '(') {
1436 			PARSEARITH();
1437 		} else {
1438 			pungetc();
1439 			out = parsebackq(out, &bqlist, 0,
1440 			    state[level].syntax == DQSYNTAX &&
1441 			    (eofmark == NULL || newvarnest > 0),
1442 			    state[level].syntax == DQSYNTAX ||
1443 			    state[level].syntax == ARISYNTAX);
1444 		}
1445 	} else {
1446 		USTPUTC(CTLVAR, out);
1447 		typeloc = out - stackblock();
1448 		USTPUTC(VSNORMAL, out);
1449 		subtype = VSNORMAL;
1450 		flags = 0;
1451 		if (c == '{') {
1452 			bracketed_name = 1;
1453 			c = pgetc();
1454 			if (c == '#') {
1455 				if ((c = pgetc()) == '}')
1456 					c = '#';
1457 				else
1458 					subtype = VSLENGTH;
1459 			}
1460 			else
1461 				subtype = 0;
1462 		}
1463 		if (!is_eof(c) && is_name(c)) {
1464 			length = 0;
1465 			do {
1466 				STPUTC(c, out);
1467 				c = pgetc();
1468 				length++;
1469 			} while (!is_eof(c) && is_in_name(c));
1470 			if (length == 6 &&
1471 			    strncmp(out - length, "LINENO", length) == 0) {
1472 				/* Replace the variable name with the
1473 				 * current line number. */
1474 				linno = plinno;
1475 				if (funclinno != 0)
1476 					linno -= funclinno - 1;
1477 				snprintf(buf, sizeof(buf), "%d", linno);
1478 				STADJUST(-6, out);
1479 				for (i = 0; buf[i] != '\0'; i++)
1480 					STPUTC(buf[i], out);
1481 				flags |= VSLINENO;
1482 			}
1483 		} else if (is_digit(c)) {
1484 			if (bracketed_name) {
1485 				do {
1486 					STPUTC(c, out);
1487 					c = pgetc();
1488 				} while (is_digit(c));
1489 			} else {
1490 				STPUTC(c, out);
1491 				c = pgetc();
1492 			}
1493 		} else {
1494 			if (! is_special(c)) {
1495 				subtype = VSERROR;
1496 				if (c == '}')
1497 					pungetc();
1498 				else if (c == '\n' || c == PEOF)
1499 					synerror("Unexpected end of line in substitution");
1500 				else
1501 					USTPUTC(c, out);
1502 			} else {
1503 				USTPUTC(c, out);
1504 				c = pgetc();
1505 			}
1506 		}
1507 		if (subtype == 0) {
1508 			switch (c) {
1509 			case ':':
1510 				flags |= VSNUL;
1511 				c = pgetc();
1512 				/*FALLTHROUGH*/
1513 			default:
1514 				p = strchr(types, c);
1515 				if (p == NULL) {
1516 					if (c == '\n' || c == PEOF)
1517 						synerror("Unexpected end of line in substitution");
1518 					if (flags == VSNUL)
1519 						STPUTC(':', out);
1520 					STPUTC(c, out);
1521 					subtype = VSERROR;
1522 				} else
1523 					subtype = p - types + VSNORMAL;
1524 				break;
1525 			case '%':
1526 			case '#':
1527 				{
1528 					int cc = c;
1529 					subtype = c == '#' ? VSTRIMLEFT :
1530 							     VSTRIMRIGHT;
1531 					c = pgetc();
1532 					if (c == cc)
1533 						subtype++;
1534 					else
1535 						pungetc();
1536 					break;
1537 				}
1538 			}
1539 		} else if (subtype != VSERROR) {
1540 			pungetc();
1541 		}
1542 		STPUTC('=', out);
1543 		if (subtype != VSLENGTH && (state[level].syntax == DQSYNTAX ||
1544 		    state[level].syntax == ARISYNTAX))
1545 			flags |= VSQUOTE;
1546 		*(stackblock() + typeloc) = subtype | flags;
1547 		if (subtype != VSNORMAL) {
1548 			if (level + 1 >= maxnest) {
1549 				maxnest *= 2;
1550 				if (state == state_static) {
1551 					state = parser_temp_alloc(
1552 					    maxnest * sizeof(*state));
1553 					memcpy(state, state_static,
1554 					    MAXNEST_STATIC * sizeof(*state));
1555 				} else
1556 					state = parser_temp_realloc(state,
1557 					    maxnest * sizeof(*state));
1558 			}
1559 			level++;
1560 			state[level].parenlevel = 0;
1561 			if (subtype == VSMINUS || subtype == VSPLUS ||
1562 			    subtype == VSQUESTION || subtype == VSASSIGN) {
1563 				/*
1564 				 * For operators that were in the Bourne shell,
1565 				 * inherit the double-quote state.
1566 				 */
1567 				state[level].syntax = state[level - 1].syntax;
1568 				state[level].category = TSTATE_VAR_OLD;
1569 			} else {
1570 				/*
1571 				 * The other operators take a pattern,
1572 				 * so go to BASESYNTAX.
1573 				 * Also, ' and " are now special, even
1574 				 * in here documents.
1575 				 */
1576 				state[level].syntax = BASESYNTAX;
1577 				state[level].category = TSTATE_VAR_NEW;
1578 				newvarnest++;
1579 			}
1580 		}
1581 	}
1582 	goto parsesub_return;
1583 }
1584 
1585 
1586 /*
1587  * Parse an arithmetic expansion (indicate start of one and set state)
1588  */
1589 parsearith: {
1590 
1591 	if (level + 1 >= maxnest) {
1592 		maxnest *= 2;
1593 		if (state == state_static) {
1594 			state = parser_temp_alloc(
1595 			    maxnest * sizeof(*state));
1596 			memcpy(state, state_static,
1597 			    MAXNEST_STATIC * sizeof(*state));
1598 		} else
1599 			state = parser_temp_realloc(state,
1600 			    maxnest * sizeof(*state));
1601 	}
1602 	level++;
1603 	state[level].syntax = ARISYNTAX;
1604 	state[level].parenlevel = 0;
1605 	state[level].category = TSTATE_ARITH;
1606 	USTPUTC(CTLARI, out);
1607 	if (state[level - 1].syntax == DQSYNTAX)
1608 		USTPUTC('"',out);
1609 	else
1610 		USTPUTC(' ',out);
1611 	goto parsearith_return;
1612 }
1613 
1614 } /* end of readtoken */
1615 
1616 
1617 
1618 #ifdef mkinit
1619 RESET {
1620 	tokpushback = 0;
1621 	checkkwd = 0;
1622 }
1623 #endif
1624 
1625 /*
1626  * Returns true if the text contains nothing to expand (no dollar signs
1627  * or backquotes).
1628  */
1629 
1630 STATIC int
1631 noexpand(char *text)
1632 {
1633 	char *p;
1634 	char c;
1635 
1636 	p = text;
1637 	while ((c = *p++) != '\0') {
1638 		if ( c == CTLQUOTEMARK)
1639 			continue;
1640 		if (c == CTLESC)
1641 			p++;
1642 		else if (BASESYNTAX[(int)c] == CCTL)
1643 			return 0;
1644 	}
1645 	return 1;
1646 }
1647 
1648 
1649 /*
1650  * Return true if the argument is a legal variable name (a letter or
1651  * underscore followed by zero or more letters, underscores, and digits).
1652  */
1653 
1654 int
1655 goodname(const char *name)
1656 {
1657 	const char *p;
1658 
1659 	p = name;
1660 	if (! is_name(*p))
1661 		return 0;
1662 	while (*++p) {
1663 		if (! is_in_name(*p))
1664 			return 0;
1665 	}
1666 	return 1;
1667 }
1668 
1669 
1670 /*
1671  * Called when an unexpected token is read during the parse.  The argument
1672  * is the token that is expected, or -1 if more than one type of token can
1673  * occur at this point.
1674  */
1675 
1676 STATIC void
1677 synexpect(int token)
1678 {
1679 	char msg[64];
1680 
1681 	if (token >= 0) {
1682 		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1683 			tokname[lasttoken], tokname[token]);
1684 	} else {
1685 		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1686 	}
1687 	synerror(msg);
1688 }
1689 
1690 
1691 STATIC void
1692 synerror(const char *msg)
1693 {
1694 	if (commandname)
1695 		outfmt(out2, "%s: %d: ", commandname, startlinno);
1696 	outfmt(out2, "Syntax error: %s\n", msg);
1697 	error((char *)NULL);
1698 }
1699 
1700 STATIC void
1701 setprompt(int which)
1702 {
1703 	whichprompt = which;
1704 
1705 #ifndef NO_HISTORY
1706 	if (!el)
1707 #endif
1708 	{
1709 		out2str(getprompt(NULL));
1710 		flushout(out2);
1711 	}
1712 }
1713 
1714 /*
1715  * called by editline -- any expansions to the prompt
1716  *    should be added here.
1717  */
1718 char *
1719 getprompt(void *unused __unused)
1720 {
1721 	static char ps[PROMPTLEN];
1722 	char *fmt;
1723 	int i, j, trim;
1724 	static char internal_error[] = "<internal prompt error>";
1725 
1726 	/*
1727 	 * Select prompt format.
1728 	 */
1729 	switch (whichprompt) {
1730 	case 0:
1731 		fmt = nullstr;
1732 		break;
1733 	case 1:
1734 		fmt = ps1val();
1735 		break;
1736 	case 2:
1737 		fmt = ps2val();
1738 		break;
1739 	default:
1740 		return internal_error;
1741 	}
1742 
1743 	/*
1744 	 * Format prompt string.
1745 	 */
1746 	for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1747 		if (*fmt == '\\')
1748 			switch (*++fmt) {
1749 
1750 				/*
1751 				 * Hostname.
1752 				 *
1753 				 * \h specifies just the local hostname,
1754 				 * \H specifies fully-qualified hostname.
1755 				 */
1756 			case 'h':
1757 			case 'H':
1758 				ps[i] = '\0';
1759 				gethostname(&ps[i], PROMPTLEN - i);
1760 				/* Skip to end of hostname. */
1761 				trim = (*fmt == 'h') ? '.' : '\0';
1762 				while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1763 					i++;
1764 				break;
1765 
1766 				/*
1767 				 * Working directory.
1768 				 *
1769 				 * \W specifies just the final component,
1770 				 * \w specifies the entire path.
1771 				 */
1772 			case 'W':
1773 			case 'w':
1774 				ps[i] = '\0';
1775 				getcwd(&ps[i], PROMPTLEN - i);
1776 				if (*fmt == 'W' && ps[i + 1] != '\0') {
1777 					/* Final path component only. */
1778 					trim = 1;
1779 					for (j = i; ps[j] != '\0'; j++)
1780 					  if (ps[j] == '/')
1781 						trim = j + 1;
1782 					memmove(&ps[i], &ps[trim],
1783 					    j - trim + 1);
1784 				}
1785 				/* Skip to end of path. */
1786 				while (ps[i + 1] != '\0')
1787 					i++;
1788 				break;
1789 
1790 				/*
1791 				 * Superuser status.
1792 				 *
1793 				 * '$' for normal users, '#' for root.
1794 				 */
1795 			case '$':
1796 				ps[i] = (geteuid() != 0) ? '$' : '#';
1797 				break;
1798 
1799 				/*
1800 				 * A literal \.
1801 				 */
1802 			case '\\':
1803 				ps[i] = '\\';
1804 				break;
1805 
1806 				/*
1807 				 * Emit unrecognized formats verbatim.
1808 				 */
1809 			default:
1810 				ps[i++] = '\\';
1811 				ps[i] = *fmt;
1812 				break;
1813 			}
1814 		else
1815 			ps[i] = *fmt;
1816 	ps[i] = '\0';
1817 	return (ps);
1818 }
1819