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