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