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