Home | History | Annotate | Line # | Download | only in sh
parser.c revision 1.40
      1 /*	$NetBSD: parser.c,v 1.40 1998/09/26 20:56:33 itohy 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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
     43 #else
     44 __RCSID("$NetBSD: parser.c,v 1.40 1998/09/26 20:56:33 itohy Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 #include <stdlib.h>
     49 
     50 #include "shell.h"
     51 #include "parser.h"
     52 #include "nodes.h"
     53 #include "expand.h"	/* defines rmescapes() */
     54 #include "redir.h"	/* defines copyfd() */
     55 #include "syntax.h"
     56 #include "options.h"
     57 #include "input.h"
     58 #include "output.h"
     59 #include "var.h"
     60 #include "error.h"
     61 #include "memalloc.h"
     62 #include "mystring.h"
     63 #include "alias.h"
     64 #include "show.h"
     65 #ifndef SMALL
     66 #include "myhistedit.h"
     67 #endif
     68 
     69 /*
     70  * Shell command parser.
     71  */
     72 
     73 #define EOFMARKLEN 79
     74 
     75 /* values returned by readtoken */
     76 #include "token.h"
     77 
     78 
     79 
     80 struct heredoc {
     81 	struct heredoc *next;	/* next here document in list */
     82 	union node *here;		/* redirection node */
     83 	char *eofmark;		/* string indicating end of input */
     84 	int striptabs;		/* if set, strip leading tabs */
     85 };
     86 
     87 
     88 
     89 struct heredoc *heredoclist;	/* list of here documents to read */
     90 int parsebackquote;		/* nonzero if we are inside backquotes */
     91 int doprompt;			/* if set, prompt the user */
     92 int needprompt;			/* true if interactive and at start of line */
     93 int lasttoken;			/* last token read */
     94 MKINIT int tokpushback;		/* last token pushed back */
     95 char *wordtext;			/* text of last word returned by readtoken */
     96 MKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
     97 struct nodelist *backquotelist;
     98 union node *redirnode;
     99 struct heredoc *heredoc;
    100 int quoteflag;			/* set if (part of) last token was quoted */
    101 int startlinno;			/* line # where last token started */
    102 
    103 
    104 #define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
    105 #ifdef GDB_HACK
    106 static const char argvars[5] = {(char)CTLVAR, (char)(VSNORMAL|VSQUOTE),
    107     '@', '=', '\0'};
    108 static const char types[] = "}-+?=";
    109 #endif
    110 
    111 
    112 STATIC union node *list __P((int));
    113 STATIC union node *andor __P((void));
    114 STATIC union node *pipeline __P((void));
    115 STATIC union node *command __P((void));
    116 STATIC union node *simplecmd __P((union node **, union node *));
    117 STATIC union node *makename __P((void));
    118 STATIC void parsefname __P((void));
    119 STATIC void parseheredoc __P((void));
    120 STATIC int peektoken __P((void));
    121 STATIC int readtoken __P((void));
    122 STATIC int xxreadtoken __P((void));
    123 STATIC int readtoken1 __P((int, char const *, char *, int));
    124 STATIC int noexpand __P((char *));
    125 STATIC void synexpect __P((int)) __attribute__((noreturn));
    126 STATIC void synerror __P((char *)) __attribute__((noreturn));
    127 STATIC void setprompt __P((int));
    128 
    129 
    130 /*
    131  * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
    132  * valid parse tree indicating a blank line.)
    133  */
    134 
    135 union node *
    136 parsecmd(interact)
    137 	int interact;
    138 {
    139 	int t;
    140 
    141 	doprompt = interact;
    142 	if (doprompt)
    143 		setprompt(1);
    144 	else
    145 		setprompt(0);
    146 	needprompt = 0;
    147 	t = readtoken();
    148 	if (t == TEOF)
    149 		return NEOF;
    150 	if (t == TNL)
    151 		return NULL;
    152 	tokpushback++;
    153 	return list(1);
    154 }
    155 
    156 
    157 STATIC union node *
    158 list(nlflag)
    159 	int nlflag;
    160 {
    161 	union node *n1, *n2, *n3;
    162 	int tok;
    163 
    164 	checkkwd = 2;
    165 	if (nlflag == 0 && tokendlist[peektoken()])
    166 		return NULL;
    167 	n1 = NULL;
    168 	for (;;) {
    169 		n2 = andor();
    170 		tok = readtoken();
    171 		if (tok == TBACKGND) {
    172 			if (n2->type == NCMD || n2->type == NPIPE) {
    173 				n2->ncmd.backgnd = 1;
    174 			} else if (n2->type == NREDIR) {
    175 				n2->type = NBACKGND;
    176 			} else {
    177 				n3 = (union node *)stalloc(sizeof (struct nredir));
    178 				n3->type = NBACKGND;
    179 				n3->nredir.n = n2;
    180 				n3->nredir.redirect = NULL;
    181 				n2 = n3;
    182 			}
    183 		}
    184 		if (n1 == NULL) {
    185 			n1 = n2;
    186 		}
    187 		else {
    188 			n3 = (union node *)stalloc(sizeof (struct nbinary));
    189 			n3->type = NSEMI;
    190 			n3->nbinary.ch1 = n1;
    191 			n3->nbinary.ch2 = n2;
    192 			n1 = n3;
    193 		}
    194 		switch (tok) {
    195 		case TBACKGND:
    196 		case TSEMI:
    197 			tok = readtoken();
    198 			/* fall through */
    199 		case TNL:
    200 			if (tok == TNL) {
    201 				parseheredoc();
    202 				if (nlflag)
    203 					return n1;
    204 			} else {
    205 				tokpushback++;
    206 			}
    207 			checkkwd = 2;
    208 			if (tokendlist[peektoken()])
    209 				return n1;
    210 			break;
    211 		case TEOF:
    212 			if (heredoclist)
    213 				parseheredoc();
    214 			else
    215 				pungetc();		/* push back EOF on input */
    216 			return n1;
    217 		default:
    218 			if (nlflag)
    219 				synexpect(-1);
    220 			tokpushback++;
    221 			return n1;
    222 		}
    223 	}
    224 }
    225 
    226 
    227 
    228 STATIC union node *
    229 andor() {
    230 	union node *n1, *n2, *n3;
    231 	int t;
    232 
    233 	n1 = pipeline();
    234 	for (;;) {
    235 		if ((t = readtoken()) == TAND) {
    236 			t = NAND;
    237 		} else if (t == TOR) {
    238 			t = NOR;
    239 		} else {
    240 			tokpushback++;
    241 			return n1;
    242 		}
    243 		n2 = pipeline();
    244 		n3 = (union node *)stalloc(sizeof (struct nbinary));
    245 		n3->type = t;
    246 		n3->nbinary.ch1 = n1;
    247 		n3->nbinary.ch2 = n2;
    248 		n1 = n3;
    249 	}
    250 }
    251 
    252 
    253 
    254 STATIC union node *
    255 pipeline() {
    256 	union node *n1, *pipenode;
    257 	struct nodelist *lp, *prev;
    258 
    259 	TRACE(("pipeline: entered\n"));
    260 	n1 = command();
    261 	if (readtoken() == TPIPE) {
    262 		pipenode = (union node *)stalloc(sizeof (struct npipe));
    263 		pipenode->type = NPIPE;
    264 		pipenode->npipe.backgnd = 0;
    265 		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
    266 		pipenode->npipe.cmdlist = lp;
    267 		lp->n = n1;
    268 		do {
    269 			prev = lp;
    270 			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
    271 			lp->n = command();
    272 			prev->next = lp;
    273 		} while (readtoken() == TPIPE);
    274 		lp->next = NULL;
    275 		n1 = pipenode;
    276 	}
    277 	tokpushback++;
    278 	return n1;
    279 }
    280 
    281 
    282 
    283 STATIC union node *
    284 command() {
    285 	union node *n1, *n2;
    286 	union node *ap, **app;
    287 	union node *cp, **cpp;
    288 	union node *redir, **rpp;
    289 	int t, negate = 0;
    290 
    291 	checkkwd = 2;
    292 	redir = NULL;
    293 	n1 = NULL;
    294 	rpp = &redir;
    295 
    296 	/* Check for redirection which may precede command */
    297 	while (readtoken() == TREDIR) {
    298 		*rpp = n2 = redirnode;
    299 		rpp = &n2->nfile.next;
    300 		parsefname();
    301 	}
    302 	tokpushback++;
    303 
    304 	while (readtoken() == TNOT) {
    305 		TRACE(("command: TNOT recognized\n"));
    306 		negate = !negate;
    307 	}
    308 	tokpushback++;
    309 
    310 	switch (readtoken()) {
    311 	case TIF:
    312 		n1 = (union node *)stalloc(sizeof (struct nif));
    313 		n1->type = NIF;
    314 		n1->nif.test = list(0);
    315 		if (readtoken() != TTHEN)
    316 			synexpect(TTHEN);
    317 		n1->nif.ifpart = list(0);
    318 		n2 = n1;
    319 		while (readtoken() == TELIF) {
    320 			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
    321 			n2 = n2->nif.elsepart;
    322 			n2->type = NIF;
    323 			n2->nif.test = list(0);
    324 			if (readtoken() != TTHEN)
    325 				synexpect(TTHEN);
    326 			n2->nif.ifpart = list(0);
    327 		}
    328 		if (lasttoken == TELSE)
    329 			n2->nif.elsepart = list(0);
    330 		else {
    331 			n2->nif.elsepart = NULL;
    332 			tokpushback++;
    333 		}
    334 		if (readtoken() != TFI)
    335 			synexpect(TFI);
    336 		checkkwd = 1;
    337 		break;
    338 	case TWHILE:
    339 	case TUNTIL: {
    340 		int got;
    341 		n1 = (union node *)stalloc(sizeof (struct nbinary));
    342 		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
    343 		n1->nbinary.ch1 = list(0);
    344 		if ((got=readtoken()) != TDO) {
    345 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
    346 			synexpect(TDO);
    347 		}
    348 		n1->nbinary.ch2 = list(0);
    349 		if (readtoken() != TDONE)
    350 			synexpect(TDONE);
    351 		checkkwd = 1;
    352 		break;
    353 	}
    354 	case TFOR:
    355 		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
    356 			synerror("Bad for loop variable");
    357 		n1 = (union node *)stalloc(sizeof (struct nfor));
    358 		n1->type = NFOR;
    359 		n1->nfor.var = wordtext;
    360 		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
    361 			app = &ap;
    362 			while (readtoken() == TWORD) {
    363 				n2 = (union node *)stalloc(sizeof (struct narg));
    364 				n2->type = NARG;
    365 				n2->narg.text = wordtext;
    366 				n2->narg.backquote = backquotelist;
    367 				*app = n2;
    368 				app = &n2->narg.next;
    369 			}
    370 			*app = NULL;
    371 			n1->nfor.args = ap;
    372 			if (lasttoken != TNL && lasttoken != TSEMI)
    373 				synexpect(-1);
    374 		} else {
    375 #ifndef GDB_HACK
    376 			static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
    377 								   '@', '=', '\0'};
    378 #endif
    379 			n2 = (union node *)stalloc(sizeof (struct narg));
    380 			n2->type = NARG;
    381 			n2->narg.text = (char *)argvars;
    382 			n2->narg.backquote = NULL;
    383 			n2->narg.next = NULL;
    384 			n1->nfor.args = n2;
    385 			/*
    386 			 * Newline or semicolon here is optional (but note
    387 			 * that the original Bourne shell only allowed NL).
    388 			 */
    389 			if (lasttoken != TNL && lasttoken != TSEMI)
    390 				tokpushback++;
    391 		}
    392 		checkkwd = 2;
    393 		if ((t = readtoken()) == TDO)
    394 			t = TDONE;
    395 		else if (t == TBEGIN)
    396 			t = TEND;
    397 		else
    398 			synexpect(-1);
    399 		n1->nfor.body = list(0);
    400 		if (readtoken() != t)
    401 			synexpect(t);
    402 		checkkwd = 1;
    403 		break;
    404 	case TCASE:
    405 		n1 = (union node *)stalloc(sizeof (struct ncase));
    406 		n1->type = NCASE;
    407 		if (readtoken() != TWORD)
    408 			synexpect(TWORD);
    409 		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
    410 		n2->type = NARG;
    411 		n2->narg.text = wordtext;
    412 		n2->narg.backquote = backquotelist;
    413 		n2->narg.next = NULL;
    414 		while (readtoken() == TNL);
    415 		if (lasttoken != TWORD || ! equal(wordtext, "in"))
    416 			synerror("expecting \"in\"");
    417 		cpp = &n1->ncase.cases;
    418 		checkkwd = 2, readtoken();
    419 		do {
    420 			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
    421 			cp->type = NCLIST;
    422 			app = &cp->nclist.pattern;
    423 			for (;;) {
    424 				*app = ap = (union node *)stalloc(sizeof (struct narg));
    425 				ap->type = NARG;
    426 				ap->narg.text = wordtext;
    427 				ap->narg.backquote = backquotelist;
    428 				if (checkkwd = 2, readtoken() != TPIPE)
    429 					break;
    430 				app = &ap->narg.next;
    431 				readtoken();
    432 			}
    433 			ap->narg.next = NULL;
    434 			if (lasttoken != TRP)
    435 				synexpect(TRP);
    436 			cp->nclist.body = list(0);
    437 
    438 			checkkwd = 2;
    439 			if ((t = readtoken()) != TESAC) {
    440 				if (t != TENDCASE)
    441 					synexpect(TENDCASE);
    442 				else
    443 					checkkwd = 2, readtoken();
    444 			}
    445 			cpp = &cp->nclist.next;
    446 		} while(lasttoken != TESAC);
    447 		*cpp = NULL;
    448 		checkkwd = 1;
    449 		break;
    450 	case TLP:
    451 		n1 = (union node *)stalloc(sizeof (struct nredir));
    452 		n1->type = NSUBSHELL;
    453 		n1->nredir.n = list(0);
    454 		n1->nredir.redirect = NULL;
    455 		if (readtoken() != TRP)
    456 			synexpect(TRP);
    457 		checkkwd = 1;
    458 		break;
    459 	case TBEGIN:
    460 		n1 = list(0);
    461 		if (readtoken() != TEND)
    462 			synexpect(TEND);
    463 		checkkwd = 1;
    464 		break;
    465 	/* Handle an empty command like other simple commands.  */
    466 	case TSEMI:
    467 		/*
    468 		 * An empty command before a ; doesn't make much sense, and
    469 		 * should certainly be disallowed in the case of `if ;'.
    470 		 */
    471 		if (!redir)
    472 			synexpect(-1);
    473 	case TAND:
    474 	case TOR:
    475 	case TNL:
    476 	case TEOF:
    477 	case TWORD:
    478 	case TRP:
    479 		tokpushback++;
    480 		n1 = simplecmd(rpp, redir);
    481 		goto checkneg;
    482 	default:
    483 		synexpect(-1);
    484 		/* NOTREACHED */
    485 	}
    486 
    487 	/* Now check for redirection which may follow command */
    488 	while (readtoken() == TREDIR) {
    489 		*rpp = n2 = redirnode;
    490 		rpp = &n2->nfile.next;
    491 		parsefname();
    492 	}
    493 	tokpushback++;
    494 	*rpp = NULL;
    495 	if (redir) {
    496 		if (n1->type != NSUBSHELL) {
    497 			n2 = (union node *)stalloc(sizeof (struct nredir));
    498 			n2->type = NREDIR;
    499 			n2->nredir.n = n1;
    500 			n1 = n2;
    501 		}
    502 		n1->nredir.redirect = redir;
    503 	}
    504 
    505 checkneg:
    506 	if (negate) {
    507 		n2 = (union node *)stalloc(sizeof (struct nnot));
    508 		n2->type = NNOT;
    509 		n2->nnot.com = n1;
    510 		return n2;
    511 	}
    512 	else
    513 		return n1;
    514 }
    515 
    516 
    517 STATIC union node *
    518 simplecmd(rpp, redir)
    519 	union node **rpp, *redir;
    520 	{
    521 	union node *args, **app;
    522 	union node **orig_rpp = rpp;
    523 	union node *n = NULL, *n2;
    524 	int negate = 0;
    525 
    526 	/* If we don't have any redirections already, then we must reset */
    527 	/* rpp to be the address of the local redir variable.  */
    528 	if (redir == 0)
    529 		rpp = &redir;
    530 
    531 	args = NULL;
    532 	app = &args;
    533 	/*
    534 	 * We save the incoming value, because we need this for shell
    535 	 * functions.  There can not be a redirect or an argument between
    536 	 * the function name and the open parenthesis.
    537 	 */
    538 	orig_rpp = rpp;
    539 
    540 	while (readtoken() == TNOT) {
    541 		TRACE(("command: TNOT recognized\n"));
    542 		negate = !negate;
    543 	}
    544 	tokpushback++;
    545 
    546 	for (;;) {
    547 		if (readtoken() == TWORD) {
    548 			n = (union node *)stalloc(sizeof (struct narg));
    549 			n->type = NARG;
    550 			n->narg.text = wordtext;
    551 			n->narg.backquote = backquotelist;
    552 			*app = n;
    553 			app = &n->narg.next;
    554 		} else if (lasttoken == TREDIR) {
    555 			*rpp = n = redirnode;
    556 			rpp = &n->nfile.next;
    557 			parsefname();	/* read name of redirection file */
    558 		} else if (lasttoken == TLP && app == &args->narg.next
    559 					    && rpp == orig_rpp) {
    560 			/* We have a function */
    561 			if (readtoken() != TRP)
    562 				synexpect(TRP);
    563 #ifdef notdef
    564 			if (! goodname(n->narg.text))
    565 				synerror("Bad function name");
    566 #endif
    567 			n->type = NDEFUN;
    568 			n->narg.next = command();
    569 			goto checkneg;
    570 		} else {
    571 			tokpushback++;
    572 			break;
    573 		}
    574 	}
    575 	*app = NULL;
    576 	*rpp = NULL;
    577 	n = (union node *)stalloc(sizeof (struct ncmd));
    578 	n->type = NCMD;
    579 	n->ncmd.backgnd = 0;
    580 	n->ncmd.args = args;
    581 	n->ncmd.redirect = redir;
    582 
    583 checkneg:
    584 	if (negate) {
    585 		n2 = (union node *)stalloc(sizeof (struct nnot));
    586 		n2->type = NNOT;
    587 		n2->nnot.com = n;
    588 		return n2;
    589 	}
    590 	else
    591 		return n;
    592 }
    593 
    594 STATIC union node *
    595 makename() {
    596 	union node *n;
    597 
    598 	n = (union node *)stalloc(sizeof (struct narg));
    599 	n->type = NARG;
    600 	n->narg.next = NULL;
    601 	n->narg.text = wordtext;
    602 	n->narg.backquote = backquotelist;
    603 	return n;
    604 }
    605 
    606 void fixredir(n, text, err)
    607 	union node *n;
    608 	const char *text;
    609 	int err;
    610 	{
    611 	TRACE(("Fix redir %s %d\n", text, err));
    612 	if (!err)
    613 		n->ndup.vname = NULL;
    614 
    615 	if (is_digit(text[0]) && text[1] == '\0')
    616 		n->ndup.dupfd = digit_val(text[0]);
    617 	else if (text[0] == '-' && text[1] == '\0')
    618 		n->ndup.dupfd = -1;
    619 	else {
    620 
    621 		if (err)
    622 			synerror("Bad fd number");
    623 		else
    624 			n->ndup.vname = makename();
    625 	}
    626 }
    627 
    628 
    629 STATIC void
    630 parsefname() {
    631 	union node *n = redirnode;
    632 
    633 	if (readtoken() != TWORD)
    634 		synexpect(-1);
    635 	if (n->type == NHERE) {
    636 		struct heredoc *here = heredoc;
    637 		struct heredoc *p;
    638 		int i;
    639 
    640 		if (quoteflag == 0)
    641 			n->type = NXHERE;
    642 		TRACE(("Here document %d\n", n->type));
    643 		if (here->striptabs) {
    644 			while (*wordtext == '\t')
    645 				wordtext++;
    646 		}
    647 		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
    648 			synerror("Illegal eof marker for << redirection");
    649 		rmescapes(wordtext);
    650 		here->eofmark = wordtext;
    651 		here->next = NULL;
    652 		if (heredoclist == NULL)
    653 			heredoclist = here;
    654 		else {
    655 			for (p = heredoclist ; p->next ; p = p->next);
    656 			p->next = here;
    657 		}
    658 	} else if (n->type == NTOFD || n->type == NFROMFD) {
    659 		fixredir(n, wordtext, 0);
    660 	} else {
    661 		n->nfile.fname = makename();
    662 	}
    663 }
    664 
    665 
    666 /*
    667  * Input any here documents.
    668  */
    669 
    670 STATIC void
    671 parseheredoc() {
    672 	struct heredoc *here;
    673 	union node *n;
    674 
    675 	while (heredoclist) {
    676 		here = heredoclist;
    677 		heredoclist = here->next;
    678 		if (needprompt) {
    679 			setprompt(2);
    680 			needprompt = 0;
    681 		}
    682 		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
    683 				here->eofmark, here->striptabs);
    684 		n = (union node *)stalloc(sizeof (struct narg));
    685 		n->narg.type = NARG;
    686 		n->narg.next = NULL;
    687 		n->narg.text = wordtext;
    688 		n->narg.backquote = backquotelist;
    689 		here->here->nhere.doc = n;
    690 	}
    691 }
    692 
    693 STATIC int
    694 peektoken() {
    695 	int t;
    696 
    697 	t = readtoken();
    698 	tokpushback++;
    699 	return (t);
    700 }
    701 
    702 STATIC int
    703 readtoken() {
    704 	int t;
    705 	int savecheckkwd = checkkwd;
    706 	struct alias *ap;
    707 #ifdef DEBUG
    708 	int alreadyseen = tokpushback;
    709 #endif
    710 
    711 	top:
    712 	t = xxreadtoken();
    713 
    714 	if (checkkwd) {
    715 		/*
    716 		 * eat newlines
    717 		 */
    718 		if (checkkwd == 2) {
    719 			checkkwd = 0;
    720 			while (t == TNL) {
    721 				parseheredoc();
    722 				t = xxreadtoken();
    723 			}
    724 		} else
    725 			checkkwd = 0;
    726 		/*
    727 		 * check for keywords and aliases
    728 		 */
    729 		if (t == TWORD && !quoteflag)
    730 		{
    731 			char * const *pp;
    732 
    733 			for (pp = (char **)parsekwd; *pp; pp++) {
    734 				if (**pp == *wordtext && equal(*pp, wordtext))
    735 				{
    736 					lasttoken = t = pp - parsekwd + KWDOFFSET;
    737 					TRACE(("keyword %s recognized\n", tokname[t]));
    738 					goto out;
    739 				}
    740 			}
    741 			if ((ap = lookupalias(wordtext, 1)) != NULL) {
    742 				pushstring(ap->val, strlen(ap->val), ap);
    743 				checkkwd = savecheckkwd;
    744 				goto top;
    745 			}
    746 		}
    747 out:
    748 		checkkwd = (t == TNOT) ? savecheckkwd : 0;
    749 	}
    750 #ifdef DEBUG
    751 	if (!alreadyseen)
    752 	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
    753 	else
    754 	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
    755 #endif
    756 	return (t);
    757 }
    758 
    759 
    760 /*
    761  * Read the next input token.
    762  * If the token is a word, we set backquotelist to the list of cmds in
    763  *	backquotes.  We set quoteflag to true if any part of the word was
    764  *	quoted.
    765  * If the token is TREDIR, then we set redirnode to a structure containing
    766  *	the redirection.
    767  * In all cases, the variable startlinno is set to the number of the line
    768  *	on which the token starts.
    769  *
    770  * [Change comment:  here documents and internal procedures]
    771  * [Readtoken shouldn't have any arguments.  Perhaps we should make the
    772  *  word parsing code into a separate routine.  In this case, readtoken
    773  *  doesn't need to have any internal procedures, but parseword does.
    774  *  We could also make parseoperator in essence the main routine, and
    775  *  have parseword (readtoken1?) handle both words and redirection.]
    776  */
    777 
    778 #define RETURN(token)	return lasttoken = token
    779 
    780 STATIC int
    781 xxreadtoken() {
    782 	int c;
    783 
    784 	if (tokpushback) {
    785 		tokpushback = 0;
    786 		return lasttoken;
    787 	}
    788 	if (needprompt) {
    789 		setprompt(2);
    790 		needprompt = 0;
    791 	}
    792 	startlinno = plinno;
    793 	for (;;) {	/* until token or start of word found */
    794 		c = pgetc_macro();
    795 		if (c == ' ' || c == '\t')
    796 			continue;		/* quick check for white space first */
    797 		switch (c) {
    798 		case ' ': case '\t':
    799 			continue;
    800 		case '#':
    801 			while ((c = pgetc()) != '\n' && c != PEOF);
    802 			pungetc();
    803 			continue;
    804 		case '\\':
    805 			if (pgetc() == '\n') {
    806 				startlinno = ++plinno;
    807 				if (doprompt)
    808 					setprompt(2);
    809 				else
    810 					setprompt(0);
    811 				continue;
    812 			}
    813 			pungetc();
    814 			goto breakloop;
    815 		case '\n':
    816 			plinno++;
    817 			needprompt = doprompt;
    818 			RETURN(TNL);
    819 		case PEOF:
    820 			RETURN(TEOF);
    821 		case '&':
    822 			if (pgetc() == '&')
    823 				RETURN(TAND);
    824 			pungetc();
    825 			RETURN(TBACKGND);
    826 		case '|':
    827 			if (pgetc() == '|')
    828 				RETURN(TOR);
    829 			pungetc();
    830 			RETURN(TPIPE);
    831 		case ';':
    832 			if (pgetc() == ';')
    833 				RETURN(TENDCASE);
    834 			pungetc();
    835 			RETURN(TSEMI);
    836 		case '(':
    837 			RETURN(TLP);
    838 		case ')':
    839 			RETURN(TRP);
    840 		default:
    841 			goto breakloop;
    842 		}
    843 	}
    844 breakloop:
    845 	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
    846 #undef RETURN
    847 }
    848 
    849 
    850 
    851 /*
    852  * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
    853  * is not NULL, read a here document.  In the latter case, eofmark is the
    854  * word which marks the end of the document and striptabs is true if
    855  * leading tabs should be stripped from the document.  The argument firstc
    856  * is the first character of the input token or document.
    857  *
    858  * Because C does not have internal subroutines, I have simulated them
    859  * using goto's to implement the subroutine linkage.  The following macros
    860  * will run code that appears at the end of readtoken1.
    861  */
    862 
    863 #define CHECKEND()	{goto checkend; checkend_return:;}
    864 #define PARSEREDIR()	{goto parseredir; parseredir_return:;}
    865 #define PARSESUB()	{goto parsesub; parsesub_return:;}
    866 #define PARSEBACKQOLD()	{oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
    867 #define PARSEBACKQNEW()	{oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
    868 #define	PARSEARITH()	{goto parsearith; parsearith_return:;}
    869 
    870 STATIC int
    871 readtoken1(firstc, syntax, eofmark, striptabs)
    872 	int firstc;
    873 	char const *syntax;
    874 	char *eofmark;
    875 	int striptabs;
    876 	{
    877 	int c = firstc;
    878 	char *out;
    879 	int len;
    880 	char line[EOFMARKLEN + 1];
    881 	struct nodelist *bqlist;
    882 	int quotef;
    883 	int dblquote;
    884 	int varnest;	/* levels of variables expansion */
    885 	int arinest;	/* levels of arithmetic expansion */
    886 	int parenlevel;	/* levels of parens in arithmetic */
    887 	int oldstyle;
    888 	char const *prevsyntax;	/* syntax before arithmetic */
    889 #if __GNUC__
    890 	/* Avoid longjmp clobbering */
    891 	(void) &out;
    892 	(void) &quotef;
    893 	(void) &dblquote;
    894 	(void) &varnest;
    895 	(void) &arinest;
    896 	(void) &parenlevel;
    897 	(void) &oldstyle;
    898 	(void) &prevsyntax;
    899 	(void) &syntax;
    900 #endif
    901 
    902 	startlinno = plinno;
    903 	dblquote = 0;
    904 	if (syntax == DQSYNTAX)
    905 		dblquote = 1;
    906 	quotef = 0;
    907 	bqlist = NULL;
    908 	varnest = 0;
    909 	arinest = 0;
    910 	parenlevel = 0;
    911 
    912 	STARTSTACKSTR(out);
    913 	loop: {	/* for each line, until end of word */
    914 #if ATTY
    915 		if (c == '\034' && doprompt
    916 		 && attyset() && ! equal(termval(), "emacs")) {
    917 			attyline();
    918 			if (syntax == BASESYNTAX)
    919 				return readtoken();
    920 			c = pgetc();
    921 			goto loop;
    922 		}
    923 #endif
    924 		CHECKEND();	/* set c to PEOF if at end of here document */
    925 		for (;;) {	/* until end of line or end of word */
    926 			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
    927 			switch(syntax[c]) {
    928 			case CNL:	/* '\n' */
    929 				if (syntax == BASESYNTAX)
    930 					goto endword;	/* exit outer loop */
    931 				USTPUTC(c, out);
    932 				plinno++;
    933 				if (doprompt)
    934 					setprompt(2);
    935 				else
    936 					setprompt(0);
    937 				c = pgetc();
    938 				goto loop;		/* continue outer loop */
    939 			case CWORD:
    940 				USTPUTC(c, out);
    941 				break;
    942 			case CCTL:
    943 				if (eofmark == NULL || dblquote)
    944 					USTPUTC(CTLESC, out);
    945 				USTPUTC(c, out);
    946 				break;
    947 			case CBACK:	/* backslash */
    948 				c = pgetc();
    949 				if (c == PEOF) {
    950 					USTPUTC('\\', out);
    951 					pungetc();
    952 				} else if (c == '\n') {
    953 					if (doprompt)
    954 						setprompt(2);
    955 					else
    956 						setprompt(0);
    957 				} else {
    958 					if (dblquote && c != '\\' && c != '`' && c != '$'
    959 							 && (c != '"' || eofmark != NULL))
    960 						USTPUTC('\\', out);
    961 					if (SQSYNTAX[c] == CCTL)
    962 						USTPUTC(CTLESC, out);
    963 					USTPUTC(c, out);
    964 					quotef++;
    965 				}
    966 				break;
    967 			case CSQUOTE:
    968 				syntax = SQSYNTAX;
    969 				break;
    970 			case CDQUOTE:
    971 				syntax = DQSYNTAX;
    972 				dblquote = 1;
    973 				break;
    974 			case CENDQUOTE:
    975 				if (eofmark) {
    976 					USTPUTC(c, out);
    977 				} else {
    978 					if (arinest)
    979 						syntax = ARISYNTAX;
    980 					else
    981 						syntax = BASESYNTAX;
    982 					quotef++;
    983 					dblquote = 0;
    984 				}
    985 				break;
    986 			case CVAR:	/* '$' */
    987 				PARSESUB();		/* parse substitution */
    988 				break;
    989 			case CENDVAR:	/* '}' */
    990 				if (varnest > 0) {
    991 					varnest--;
    992 					USTPUTC(CTLENDVAR, out);
    993 				} else {
    994 					USTPUTC(c, out);
    995 				}
    996 				break;
    997 			case CLP:	/* '(' in arithmetic */
    998 				parenlevel++;
    999 				USTPUTC(c, out);
   1000 				break;
   1001 			case CRP:	/* ')' in arithmetic */
   1002 				if (parenlevel > 0) {
   1003 					USTPUTC(c, out);
   1004 					--parenlevel;
   1005 				} else {
   1006 					if (pgetc() == ')') {
   1007 						if (--arinest == 0) {
   1008 							USTPUTC(CTLENDARI, out);
   1009 							syntax = prevsyntax;
   1010 						} else
   1011 							USTPUTC(')', out);
   1012 					} else {
   1013 						/*
   1014 						 * unbalanced parens
   1015 						 *  (don't 2nd guess - no error)
   1016 						 */
   1017 						pungetc();
   1018 						USTPUTC(')', out);
   1019 					}
   1020 				}
   1021 				break;
   1022 			case CBQUOTE:	/* '`' */
   1023 				PARSEBACKQOLD();
   1024 				break;
   1025 			case CEOF:
   1026 				goto endword;		/* exit outer loop */
   1027 			default:
   1028 				if (varnest == 0)
   1029 					goto endword;	/* exit outer loop */
   1030 				USTPUTC(c, out);
   1031 			}
   1032 			c = pgetc_macro();
   1033 		}
   1034 	}
   1035 endword:
   1036 	if (syntax == ARISYNTAX)
   1037 		synerror("Missing '))'");
   1038 	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
   1039 		synerror("Unterminated quoted string");
   1040 	if (varnest != 0) {
   1041 		startlinno = plinno;
   1042 		synerror("Missing '}'");
   1043 	}
   1044 	USTPUTC('\0', out);
   1045 	len = out - stackblock();
   1046 	out = stackblock();
   1047 	if (eofmark == NULL) {
   1048 		if ((c == '>' || c == '<')
   1049 		 && quotef == 0
   1050 		 && len <= 2
   1051 		 && (*out == '\0' || is_digit(*out))) {
   1052 			PARSEREDIR();
   1053 			return lasttoken = TREDIR;
   1054 		} else {
   1055 			pungetc();
   1056 		}
   1057 	}
   1058 	quoteflag = quotef;
   1059 	backquotelist = bqlist;
   1060 	grabstackblock(len);
   1061 	wordtext = out;
   1062 	return lasttoken = TWORD;
   1063 /* end of readtoken routine */
   1064 
   1065 
   1066 
   1067 /*
   1068  * Check to see whether we are at the end of the here document.  When this
   1069  * is called, c is set to the first character of the next input line.  If
   1070  * we are at the end of the here document, this routine sets the c to PEOF.
   1071  */
   1072 
   1073 checkend: {
   1074 	if (eofmark) {
   1075 		if (striptabs) {
   1076 			while (c == '\t')
   1077 				c = pgetc();
   1078 		}
   1079 		if (c == *eofmark) {
   1080 			if (pfgets(line, sizeof line) != NULL) {
   1081 				char *p, *q;
   1082 
   1083 				p = line;
   1084 				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
   1085 				if (*p == '\n' && *q == '\0') {
   1086 					c = PEOF;
   1087 					plinno++;
   1088 					needprompt = doprompt;
   1089 				} else {
   1090 					pushstring(line, strlen(line), NULL);
   1091 				}
   1092 			}
   1093 		}
   1094 	}
   1095 	goto checkend_return;
   1096 }
   1097 
   1098 
   1099 /*
   1100  * Parse a redirection operator.  The variable "out" points to a string
   1101  * specifying the fd to be redirected.  The variable "c" contains the
   1102  * first character of the redirection operator.
   1103  */
   1104 
   1105 parseredir: {
   1106 	char fd = *out;
   1107 	union node *np;
   1108 
   1109 	np = (union node *)stalloc(sizeof (struct nfile));
   1110 	if (c == '>') {
   1111 		np->nfile.fd = 1;
   1112 		c = pgetc();
   1113 		if (c == '>')
   1114 			np->type = NAPPEND;
   1115 		else if (c == '&')
   1116 			np->type = NTOFD;
   1117 		else {
   1118 			np->type = NTO;
   1119 			pungetc();
   1120 		}
   1121 	} else {	/* c == '<' */
   1122 		np->nfile.fd = 0;
   1123 		c = pgetc();
   1124 		if (c == '<') {
   1125 			if (sizeof (struct nfile) != sizeof (struct nhere)) {
   1126 				np = (union node *)stalloc(sizeof (struct nhere));
   1127 				np->nfile.fd = 0;
   1128 			}
   1129 			np->type = NHERE;
   1130 			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
   1131 			heredoc->here = np;
   1132 			if ((c = pgetc()) == '-') {
   1133 				heredoc->striptabs = 1;
   1134 			} else {
   1135 				heredoc->striptabs = 0;
   1136 				pungetc();
   1137 			}
   1138 		} else if (c == '&')
   1139 			np->type = NFROMFD;
   1140 		else {
   1141 			np->type = NFROM;
   1142 			pungetc();
   1143 		}
   1144 	}
   1145 	if (fd != '\0')
   1146 		np->nfile.fd = digit_val(fd);
   1147 	redirnode = np;
   1148 	goto parseredir_return;
   1149 }
   1150 
   1151 
   1152 /*
   1153  * Parse a substitution.  At this point, we have read the dollar sign
   1154  * and nothing else.
   1155  */
   1156 
   1157 parsesub: {
   1158 	int subtype;
   1159 	int typeloc;
   1160 	int flags;
   1161 	char *p;
   1162 #ifndef GDB_HACK
   1163 	static const char types[] = "}-+?=";
   1164 #endif
   1165 
   1166 	c = pgetc();
   1167 	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
   1168 		USTPUTC('$', out);
   1169 		pungetc();
   1170 	} else if (c == '(') {	/* $(command) or $((arith)) */
   1171 		if (pgetc() == '(') {
   1172 			PARSEARITH();
   1173 		} else {
   1174 			pungetc();
   1175 			PARSEBACKQNEW();
   1176 		}
   1177 	} else {
   1178 		USTPUTC(CTLVAR, out);
   1179 		typeloc = out - stackblock();
   1180 		USTPUTC(VSNORMAL, out);
   1181 		subtype = VSNORMAL;
   1182 		if (c == '{') {
   1183 			c = pgetc();
   1184 			if (c == '#') {
   1185 				if ((c = pgetc()) == '}')
   1186 					c = '#';
   1187 				else
   1188 					subtype = VSLENGTH;
   1189 			}
   1190 			else
   1191 				subtype = 0;
   1192 		}
   1193 		if (is_name(c)) {
   1194 			do {
   1195 				STPUTC(c, out);
   1196 				c = pgetc();
   1197 			} while (is_in_name(c));
   1198 		} else if (is_digit(c)) {
   1199 			do {
   1200 				USTPUTC(c, out);
   1201 				c = pgetc();
   1202 			} while (is_digit(c));
   1203 		}
   1204 		else if (is_special(c)) {
   1205 			USTPUTC(c, out);
   1206 			c = pgetc();
   1207 		}
   1208 		else
   1209 badsub:			synerror("Bad substitution");
   1210 
   1211 		STPUTC('=', out);
   1212 		flags = 0;
   1213 		if (subtype == 0) {
   1214 			switch (c) {
   1215 			case ':':
   1216 				flags = VSNUL;
   1217 				c = pgetc();
   1218 				/*FALLTHROUGH*/
   1219 			default:
   1220 				p = strchr(types, c);
   1221 				if (p == NULL)
   1222 					goto badsub;
   1223 				subtype = p - types + VSNORMAL;
   1224 				break;
   1225 			case '%':
   1226 			case '#':
   1227 				{
   1228 					int cc = c;
   1229 					subtype = c == '#' ? VSTRIMLEFT :
   1230 							     VSTRIMRIGHT;
   1231 					c = pgetc();
   1232 					if (c == cc)
   1233 						subtype++;
   1234 					else
   1235 						pungetc();
   1236 					break;
   1237 				}
   1238 			}
   1239 		} else {
   1240 			pungetc();
   1241 		}
   1242 		if (dblquote || arinest)
   1243 			flags |= VSQUOTE;
   1244 		*(stackblock() + typeloc) = subtype | flags;
   1245 		if (subtype != VSNORMAL)
   1246 			varnest++;
   1247 	}
   1248 	goto parsesub_return;
   1249 }
   1250 
   1251 
   1252 /*
   1253  * Called to parse command substitutions.  Newstyle is set if the command
   1254  * is enclosed inside $(...); nlpp is a pointer to the head of the linked
   1255  * list of commands (passed by reference), and savelen is the number of
   1256  * characters on the top of the stack which must be preserved.
   1257  */
   1258 
   1259 parsebackq: {
   1260 	struct nodelist **nlpp;
   1261 	int savepbq;
   1262 	union node *n;
   1263 	char *volatile str;
   1264 	struct jmploc jmploc;
   1265 	struct jmploc *volatile savehandler;
   1266 	int savelen;
   1267 	int saveprompt;
   1268 #ifdef __GNUC__
   1269 	(void) &saveprompt;
   1270 #endif
   1271 
   1272 	savepbq = parsebackquote;
   1273 	if (setjmp(jmploc.loc)) {
   1274 		if (str)
   1275 			ckfree(str);
   1276 		parsebackquote = 0;
   1277 		handler = savehandler;
   1278 		longjmp(handler->loc, 1);
   1279 	}
   1280 	INTOFF;
   1281 	str = NULL;
   1282 	savelen = out - stackblock();
   1283 	if (savelen > 0) {
   1284 		str = ckmalloc(savelen);
   1285 		memcpy(str, stackblock(), savelen);
   1286 	}
   1287 	savehandler = handler;
   1288 	handler = &jmploc;
   1289 	INTON;
   1290         if (oldstyle) {
   1291                 /* We must read until the closing backquote, giving special
   1292                    treatment to some slashes, and then push the string and
   1293                    reread it as input, interpreting it normally.  */
   1294                 char *out;
   1295                 int c;
   1296                 int savelen;
   1297                 char *str;
   1298 
   1299 
   1300                 STARTSTACKSTR(out);
   1301 		for (;;) {
   1302 			if (needprompt) {
   1303 				setprompt(2);
   1304 				needprompt = 0;
   1305 			}
   1306 			switch (c = pgetc()) {
   1307 			case '`':
   1308 				goto done;
   1309 
   1310 			case '\\':
   1311                                 if ((c = pgetc()) == '\n') {
   1312 					plinno++;
   1313 					if (doprompt)
   1314 						setprompt(2);
   1315 					else
   1316 						setprompt(0);
   1317 					/*
   1318 					 * If eating a newline, avoid putting
   1319 					 * the newline into the new character
   1320 					 * stream (via the STPUTC after the
   1321 					 * switch).
   1322 					 */
   1323 					continue;
   1324 				}
   1325                                 if (c != '\\' && c != '`' && c != '$'
   1326                                     && (!dblquote || c != '"'))
   1327                                         STPUTC('\\', out);
   1328 				break;
   1329 
   1330 			case '\n':
   1331 				plinno++;
   1332 				needprompt = doprompt;
   1333 				break;
   1334 
   1335 			case PEOF:
   1336 			        startlinno = plinno;
   1337 				synerror("EOF in backquote substitution");
   1338  				break;
   1339 
   1340 			default:
   1341 				break;
   1342 			}
   1343 			STPUTC(c, out);
   1344                 }
   1345 done:
   1346                 STPUTC('\0', out);
   1347                 savelen = out - stackblock();
   1348                 if (savelen > 0) {
   1349 			str = grabstackstr(out);
   1350 			setinputstring(str, 1);
   1351                 }
   1352         }
   1353 	nlpp = &bqlist;
   1354 	while (*nlpp)
   1355 		nlpp = &(*nlpp)->next;
   1356 	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
   1357 	(*nlpp)->next = NULL;
   1358 	parsebackquote = oldstyle;
   1359 
   1360 	if (oldstyle) {
   1361 		saveprompt = doprompt;
   1362 		doprompt = 0;
   1363 	}
   1364 
   1365 	n = list(0);
   1366 
   1367 	if (oldstyle)
   1368 		doprompt = saveprompt;
   1369 	else {
   1370 		if (readtoken() != TRP)
   1371 			synexpect(TRP);
   1372 	}
   1373 
   1374 	(*nlpp)->n = n;
   1375         if (oldstyle) {
   1376 		/*
   1377 		 * Start reading from old file again, ignoring any pushed back
   1378 		 * tokens left from the backquote parsing
   1379 		 */
   1380                 popfile();
   1381 		tokpushback = 0;
   1382 	}
   1383 	while (stackblocksize() <= savelen)
   1384 		growstackblock();
   1385 	STARTSTACKSTR(out);
   1386 	if (str) {
   1387 		memcpy(out, str, savelen);
   1388 		STADJUST(savelen, out);
   1389 		INTOFF;
   1390 		ckfree(str);
   1391 		str = NULL;
   1392 		INTON;
   1393 	}
   1394 	parsebackquote = savepbq;
   1395 	handler = savehandler;
   1396 	if (arinest || dblquote)
   1397 		USTPUTC(CTLBACKQ | CTLQUOTE, out);
   1398 	else
   1399 		USTPUTC(CTLBACKQ, out);
   1400 	if (oldstyle)
   1401 		goto parsebackq_oldreturn;
   1402 	else
   1403 		goto parsebackq_newreturn;
   1404 }
   1405 
   1406 /*
   1407  * Parse an arithmetic expansion (indicate start of one and set state)
   1408  */
   1409 parsearith: {
   1410 
   1411 	if (++arinest == 1) {
   1412 		prevsyntax = syntax;
   1413 		syntax = ARISYNTAX;
   1414 		USTPUTC(CTLARI, out);
   1415 	} else {
   1416 		/*
   1417 		 * we collapse embedded arithmetic expansion to
   1418 		 * parenthesis, which should be equivalent
   1419 		 */
   1420 		USTPUTC('(', out);
   1421 	}
   1422 	goto parsearith_return;
   1423 }
   1424 
   1425 } /* end of readtoken */
   1426 
   1427 
   1428 
   1429 #ifdef mkinit
   1430 RESET {
   1431 	tokpushback = 0;
   1432 	checkkwd = 0;
   1433 }
   1434 #endif
   1435 
   1436 /*
   1437  * Returns true if the text contains nothing to expand (no dollar signs
   1438  * or backquotes).
   1439  */
   1440 
   1441 STATIC int
   1442 noexpand(text)
   1443 	char *text;
   1444 	{
   1445 	char *p;
   1446 	char c;
   1447 
   1448 	p = text;
   1449 	while ((c = *p++) != '\0') {
   1450 		if (c == CTLESC)
   1451 			p++;
   1452 		else if (BASESYNTAX[(int)c] == CCTL)
   1453 			return 0;
   1454 	}
   1455 	return 1;
   1456 }
   1457 
   1458 
   1459 /*
   1460  * Return true if the argument is a legal variable name (a letter or
   1461  * underscore followed by zero or more letters, underscores, and digits).
   1462  */
   1463 
   1464 int
   1465 goodname(name)
   1466 	char *name;
   1467 	{
   1468 	char *p;
   1469 
   1470 	p = name;
   1471 	if (! is_name(*p))
   1472 		return 0;
   1473 	while (*++p) {
   1474 		if (! is_in_name(*p))
   1475 			return 0;
   1476 	}
   1477 	return 1;
   1478 }
   1479 
   1480 
   1481 /*
   1482  * Called when an unexpected token is read during the parse.  The argument
   1483  * is the token that is expected, or -1 if more than one type of token can
   1484  * occur at this point.
   1485  */
   1486 
   1487 STATIC void
   1488 synexpect(token)
   1489 	int token;
   1490 {
   1491 	char msg[64];
   1492 
   1493 	if (token >= 0) {
   1494 		fmtstr(msg, 64, "%s unexpected (expecting %s)",
   1495 			tokname[lasttoken], tokname[token]);
   1496 	} else {
   1497 		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
   1498 	}
   1499 	synerror(msg);
   1500 	/* NOTREACHED */
   1501 }
   1502 
   1503 
   1504 STATIC void
   1505 synerror(msg)
   1506 	char *msg;
   1507 	{
   1508 	if (commandname)
   1509 		outfmt(&errout, "%s: %d: ", commandname, startlinno);
   1510 	outfmt(&errout, "Syntax error: %s\n", msg);
   1511 	error((char *)NULL);
   1512 	/* NOTREACHED */
   1513 }
   1514 
   1515 STATIC void
   1516 setprompt(which)
   1517 	int which;
   1518 	{
   1519 	whichprompt = which;
   1520 
   1521 #ifndef SMALL
   1522 	if (!el)
   1523 #endif
   1524 		out2str(getprompt(NULL));
   1525 }
   1526 
   1527 /*
   1528  * called by editline -- any expansions to the prompt
   1529  *    should be added here.
   1530  */
   1531 char *
   1532 getprompt(unused)
   1533 	void *unused;
   1534 	{
   1535 	switch (whichprompt) {
   1536 	case 0:
   1537 		return "";
   1538 	case 1:
   1539 		return ps1val();
   1540 	case 2:
   1541 		return ps2val();
   1542 	default:
   1543 		return "<internal prompt error>";
   1544 	}
   1545 }
   1546