Home | History | Annotate | Line # | Download | only in sh
parser.c revision 1.41
      1 /*	$NetBSD: parser.c,v 1.41 1999/01/25 14:20:56 mycroft 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.41 1999/01/25 14:20:56 mycroft 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 					else if (eofmark == NULL)
    964 						USTPUTC(CTLQUOTEMARK, out);
    965 					USTPUTC(c, out);
    966 					quotef++;
    967 				}
    968 				break;
    969 			case CSQUOTE:
    970 				if (eofmark == NULL)
    971 					USTPUTC(CTLQUOTEMARK, out);
    972 				syntax = SQSYNTAX;
    973 				break;
    974 			case CDQUOTE:
    975 				if (eofmark == NULL)
    976 					USTPUTC(CTLQUOTEMARK, out);
    977 				syntax = DQSYNTAX;
    978 				dblquote = 1;
    979 				break;
    980 			case CENDQUOTE:
    981 				if (eofmark != NULL && arinest == 0 &&
    982 				    varnest == 0) {
    983 					USTPUTC(c, out);
    984 				} else {
    985 					if (arinest) {
    986 						syntax = ARISYNTAX;
    987 						dblquote = 0;
    988 					} else if (eofmark == NULL) {
    989 						syntax = BASESYNTAX;
    990 						dblquote = 0;
    991 					}
    992 					quotef++;
    993 				}
    994 				break;
    995 			case CVAR:	/* '$' */
    996 				PARSESUB();		/* parse substitution */
    997 				break;
    998 			case CENDVAR:	/* '}' */
    999 				if (varnest > 0) {
   1000 					varnest--;
   1001 					USTPUTC(CTLENDVAR, out);
   1002 				} else {
   1003 					USTPUTC(c, out);
   1004 				}
   1005 				break;
   1006 			case CLP:	/* '(' in arithmetic */
   1007 				parenlevel++;
   1008 				USTPUTC(c, out);
   1009 				break;
   1010 			case CRP:	/* ')' in arithmetic */
   1011 				if (parenlevel > 0) {
   1012 					USTPUTC(c, out);
   1013 					--parenlevel;
   1014 				} else {
   1015 					if (pgetc() == ')') {
   1016 						if (--arinest == 0) {
   1017 							USTPUTC(CTLENDARI, out);
   1018 							syntax = prevsyntax;
   1019 							if (syntax == DQSYNTAX)
   1020 								dblquote = 1;
   1021 							else
   1022 								dblquote = 0;
   1023 						} else
   1024 							USTPUTC(')', out);
   1025 					} else {
   1026 						/*
   1027 						 * unbalanced parens
   1028 						 *  (don't 2nd guess - no error)
   1029 						 */
   1030 						pungetc();
   1031 						USTPUTC(')', out);
   1032 					}
   1033 				}
   1034 				break;
   1035 			case CBQUOTE:	/* '`' */
   1036 				PARSEBACKQOLD();
   1037 				break;
   1038 			case CEOF:
   1039 				goto endword;		/* exit outer loop */
   1040 			default:
   1041 				if (varnest == 0)
   1042 					goto endword;	/* exit outer loop */
   1043 				USTPUTC(c, out);
   1044 			}
   1045 			c = pgetc_macro();
   1046 		}
   1047 	}
   1048 endword:
   1049 	if (syntax == ARISYNTAX)
   1050 		synerror("Missing '))'");
   1051 	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
   1052 		synerror("Unterminated quoted string");
   1053 	if (varnest != 0) {
   1054 		startlinno = plinno;
   1055 		synerror("Missing '}'");
   1056 	}
   1057 	USTPUTC('\0', out);
   1058 	len = out - stackblock();
   1059 	out = stackblock();
   1060 	if (eofmark == NULL) {
   1061 		if ((c == '>' || c == '<')
   1062 		 && quotef == 0
   1063 		 && len <= 2
   1064 		 && (*out == '\0' || is_digit(*out))) {
   1065 			PARSEREDIR();
   1066 			return lasttoken = TREDIR;
   1067 		} else {
   1068 			pungetc();
   1069 		}
   1070 	}
   1071 	quoteflag = quotef;
   1072 	backquotelist = bqlist;
   1073 	grabstackblock(len);
   1074 	wordtext = out;
   1075 	return lasttoken = TWORD;
   1076 /* end of readtoken routine */
   1077 
   1078 
   1079 
   1080 /*
   1081  * Check to see whether we are at the end of the here document.  When this
   1082  * is called, c is set to the first character of the next input line.  If
   1083  * we are at the end of the here document, this routine sets the c to PEOF.
   1084  */
   1085 
   1086 checkend: {
   1087 	if (eofmark) {
   1088 		if (striptabs) {
   1089 			while (c == '\t')
   1090 				c = pgetc();
   1091 		}
   1092 		if (c == *eofmark) {
   1093 			if (pfgets(line, sizeof line) != NULL) {
   1094 				char *p, *q;
   1095 
   1096 				p = line;
   1097 				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
   1098 				if (*p == '\n' && *q == '\0') {
   1099 					c = PEOF;
   1100 					plinno++;
   1101 					needprompt = doprompt;
   1102 				} else {
   1103 					pushstring(line, strlen(line), NULL);
   1104 				}
   1105 			}
   1106 		}
   1107 	}
   1108 	goto checkend_return;
   1109 }
   1110 
   1111 
   1112 /*
   1113  * Parse a redirection operator.  The variable "out" points to a string
   1114  * specifying the fd to be redirected.  The variable "c" contains the
   1115  * first character of the redirection operator.
   1116  */
   1117 
   1118 parseredir: {
   1119 	char fd = *out;
   1120 	union node *np;
   1121 
   1122 	np = (union node *)stalloc(sizeof (struct nfile));
   1123 	if (c == '>') {
   1124 		np->nfile.fd = 1;
   1125 		c = pgetc();
   1126 		if (c == '>')
   1127 			np->type = NAPPEND;
   1128 		else if (c == '&')
   1129 			np->type = NTOFD;
   1130 		else {
   1131 			np->type = NTO;
   1132 			pungetc();
   1133 		}
   1134 	} else {	/* c == '<' */
   1135 		np->nfile.fd = 0;
   1136 		c = pgetc();
   1137 		if (c == '<') {
   1138 			if (sizeof (struct nfile) != sizeof (struct nhere)) {
   1139 				np = (union node *)stalloc(sizeof (struct nhere));
   1140 				np->nfile.fd = 0;
   1141 			}
   1142 			np->type = NHERE;
   1143 			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
   1144 			heredoc->here = np;
   1145 			if ((c = pgetc()) == '-') {
   1146 				heredoc->striptabs = 1;
   1147 			} else {
   1148 				heredoc->striptabs = 0;
   1149 				pungetc();
   1150 			}
   1151 		} else if (c == '&')
   1152 			np->type = NFROMFD;
   1153 		else {
   1154 			np->type = NFROM;
   1155 			pungetc();
   1156 		}
   1157 	}
   1158 	if (fd != '\0')
   1159 		np->nfile.fd = digit_val(fd);
   1160 	redirnode = np;
   1161 	goto parseredir_return;
   1162 }
   1163 
   1164 
   1165 /*
   1166  * Parse a substitution.  At this point, we have read the dollar sign
   1167  * and nothing else.
   1168  */
   1169 
   1170 parsesub: {
   1171 	int subtype;
   1172 	int typeloc;
   1173 	int flags;
   1174 	char *p;
   1175 #ifndef GDB_HACK
   1176 	static const char types[] = "}-+?=";
   1177 #endif
   1178 
   1179 	c = pgetc();
   1180 	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
   1181 		USTPUTC('$', out);
   1182 		pungetc();
   1183 	} else if (c == '(') {	/* $(command) or $((arith)) */
   1184 		if (pgetc() == '(') {
   1185 			PARSEARITH();
   1186 		} else {
   1187 			pungetc();
   1188 			PARSEBACKQNEW();
   1189 		}
   1190 	} else {
   1191 		USTPUTC(CTLVAR, out);
   1192 		typeloc = out - stackblock();
   1193 		USTPUTC(VSNORMAL, out);
   1194 		subtype = VSNORMAL;
   1195 		if (c == '{') {
   1196 			c = pgetc();
   1197 			if (c == '#') {
   1198 				if ((c = pgetc()) == '}')
   1199 					c = '#';
   1200 				else
   1201 					subtype = VSLENGTH;
   1202 			}
   1203 			else
   1204 				subtype = 0;
   1205 		}
   1206 		if (is_name(c)) {
   1207 			do {
   1208 				STPUTC(c, out);
   1209 				c = pgetc();
   1210 			} while (is_in_name(c));
   1211 		} else if (is_digit(c)) {
   1212 			do {
   1213 				USTPUTC(c, out);
   1214 				c = pgetc();
   1215 			} while (is_digit(c));
   1216 		}
   1217 		else if (is_special(c)) {
   1218 			USTPUTC(c, out);
   1219 			c = pgetc();
   1220 		}
   1221 		else
   1222 badsub:			synerror("Bad substitution");
   1223 
   1224 		STPUTC('=', out);
   1225 		flags = 0;
   1226 		if (subtype == 0) {
   1227 			switch (c) {
   1228 			case ':':
   1229 				flags = VSNUL;
   1230 				c = pgetc();
   1231 				/*FALLTHROUGH*/
   1232 			default:
   1233 				p = strchr(types, c);
   1234 				if (p == NULL)
   1235 					goto badsub;
   1236 				subtype = p - types + VSNORMAL;
   1237 				break;
   1238 			case '%':
   1239 			case '#':
   1240 				{
   1241 					int cc = c;
   1242 					subtype = c == '#' ? VSTRIMLEFT :
   1243 							     VSTRIMRIGHT;
   1244 					c = pgetc();
   1245 					if (c == cc)
   1246 						subtype++;
   1247 					else
   1248 						pungetc();
   1249 					break;
   1250 				}
   1251 			}
   1252 		} else {
   1253 			pungetc();
   1254 		}
   1255 		if (dblquote || arinest)
   1256 			flags |= VSQUOTE;
   1257 		*(stackblock() + typeloc) = subtype | flags;
   1258 		if (subtype != VSNORMAL)
   1259 			varnest++;
   1260 	}
   1261 	goto parsesub_return;
   1262 }
   1263 
   1264 
   1265 /*
   1266  * Called to parse command substitutions.  Newstyle is set if the command
   1267  * is enclosed inside $(...); nlpp is a pointer to the head of the linked
   1268  * list of commands (passed by reference), and savelen is the number of
   1269  * characters on the top of the stack which must be preserved.
   1270  */
   1271 
   1272 parsebackq: {
   1273 	struct nodelist **nlpp;
   1274 	int savepbq;
   1275 	union node *n;
   1276 	char *volatile str;
   1277 	struct jmploc jmploc;
   1278 	struct jmploc *volatile savehandler;
   1279 	int savelen;
   1280 	int saveprompt;
   1281 #ifdef __GNUC__
   1282 	(void) &saveprompt;
   1283 #endif
   1284 
   1285 	savepbq = parsebackquote;
   1286 	if (setjmp(jmploc.loc)) {
   1287 		if (str)
   1288 			ckfree(str);
   1289 		parsebackquote = 0;
   1290 		handler = savehandler;
   1291 		longjmp(handler->loc, 1);
   1292 	}
   1293 	INTOFF;
   1294 	str = NULL;
   1295 	savelen = out - stackblock();
   1296 	if (savelen > 0) {
   1297 		str = ckmalloc(savelen);
   1298 		memcpy(str, stackblock(), savelen);
   1299 	}
   1300 	savehandler = handler;
   1301 	handler = &jmploc;
   1302 	INTON;
   1303         if (oldstyle) {
   1304                 /* We must read until the closing backquote, giving special
   1305                    treatment to some slashes, and then push the string and
   1306                    reread it as input, interpreting it normally.  */
   1307                 char *out;
   1308                 int c;
   1309                 int savelen;
   1310                 char *str;
   1311 
   1312 
   1313                 STARTSTACKSTR(out);
   1314 		for (;;) {
   1315 			if (needprompt) {
   1316 				setprompt(2);
   1317 				needprompt = 0;
   1318 			}
   1319 			switch (c = pgetc()) {
   1320 			case '`':
   1321 				goto done;
   1322 
   1323 			case '\\':
   1324                                 if ((c = pgetc()) == '\n') {
   1325 					plinno++;
   1326 					if (doprompt)
   1327 						setprompt(2);
   1328 					else
   1329 						setprompt(0);
   1330 					/*
   1331 					 * If eating a newline, avoid putting
   1332 					 * the newline into the new character
   1333 					 * stream (via the STPUTC after the
   1334 					 * switch).
   1335 					 */
   1336 					continue;
   1337 				}
   1338                                 if (c != '\\' && c != '`' && c != '$'
   1339                                     && (!dblquote || c != '"'))
   1340                                         STPUTC('\\', out);
   1341 				break;
   1342 
   1343 			case '\n':
   1344 				plinno++;
   1345 				needprompt = doprompt;
   1346 				break;
   1347 
   1348 			case PEOF:
   1349 			        startlinno = plinno;
   1350 				synerror("EOF in backquote substitution");
   1351  				break;
   1352 
   1353 			default:
   1354 				break;
   1355 			}
   1356 			STPUTC(c, out);
   1357                 }
   1358 done:
   1359                 STPUTC('\0', out);
   1360                 savelen = out - stackblock();
   1361                 if (savelen > 0) {
   1362 			str = grabstackstr(out);
   1363 			setinputstring(str, 1);
   1364                 }
   1365         }
   1366 	nlpp = &bqlist;
   1367 	while (*nlpp)
   1368 		nlpp = &(*nlpp)->next;
   1369 	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
   1370 	(*nlpp)->next = NULL;
   1371 	parsebackquote = oldstyle;
   1372 
   1373 	if (oldstyle) {
   1374 		saveprompt = doprompt;
   1375 		doprompt = 0;
   1376 	}
   1377 
   1378 	n = list(0);
   1379 
   1380 	if (oldstyle)
   1381 		doprompt = saveprompt;
   1382 	else {
   1383 		if (readtoken() != TRP)
   1384 			synexpect(TRP);
   1385 	}
   1386 
   1387 	(*nlpp)->n = n;
   1388         if (oldstyle) {
   1389 		/*
   1390 		 * Start reading from old file again, ignoring any pushed back
   1391 		 * tokens left from the backquote parsing
   1392 		 */
   1393                 popfile();
   1394 		tokpushback = 0;
   1395 	}
   1396 	while (stackblocksize() <= savelen)
   1397 		growstackblock();
   1398 	STARTSTACKSTR(out);
   1399 	if (str) {
   1400 		memcpy(out, str, savelen);
   1401 		STADJUST(savelen, out);
   1402 		INTOFF;
   1403 		ckfree(str);
   1404 		str = NULL;
   1405 		INTON;
   1406 	}
   1407 	parsebackquote = savepbq;
   1408 	handler = savehandler;
   1409 	if (arinest || dblquote)
   1410 		USTPUTC(CTLBACKQ | CTLQUOTE, out);
   1411 	else
   1412 		USTPUTC(CTLBACKQ, out);
   1413 	if (oldstyle)
   1414 		goto parsebackq_oldreturn;
   1415 	else
   1416 		goto parsebackq_newreturn;
   1417 }
   1418 
   1419 /*
   1420  * Parse an arithmetic expansion (indicate start of one and set state)
   1421  */
   1422 parsearith: {
   1423 
   1424 	if (++arinest == 1) {
   1425 		prevsyntax = syntax;
   1426 		syntax = ARISYNTAX;
   1427 		USTPUTC(CTLARI, out);
   1428 		if (dblquote)
   1429 			USTPUTC('"',out);
   1430 		else
   1431 			USTPUTC(' ',out);
   1432 	} else {
   1433 		/*
   1434 		 * we collapse embedded arithmetic expansion to
   1435 		 * parenthesis, which should be equivalent
   1436 		 */
   1437 		USTPUTC('(', out);
   1438 	}
   1439 	goto parsearith_return;
   1440 }
   1441 
   1442 } /* end of readtoken */
   1443 
   1444 
   1445 
   1446 #ifdef mkinit
   1447 RESET {
   1448 	tokpushback = 0;
   1449 	checkkwd = 0;
   1450 }
   1451 #endif
   1452 
   1453 /*
   1454  * Returns true if the text contains nothing to expand (no dollar signs
   1455  * or backquotes).
   1456  */
   1457 
   1458 STATIC int
   1459 noexpand(text)
   1460 	char *text;
   1461 	{
   1462 	char *p;
   1463 	char c;
   1464 
   1465 	p = text;
   1466 	while ((c = *p++) != '\0') {
   1467 		if (c == CTLQUOTEMARK)
   1468 			continue;
   1469 		if (c == CTLESC)
   1470 			p++;
   1471 		else if (BASESYNTAX[(int)c] == CCTL)
   1472 			return 0;
   1473 	}
   1474 	return 1;
   1475 }
   1476 
   1477 
   1478 /*
   1479  * Return true if the argument is a legal variable name (a letter or
   1480  * underscore followed by zero or more letters, underscores, and digits).
   1481  */
   1482 
   1483 int
   1484 goodname(name)
   1485 	char *name;
   1486 	{
   1487 	char *p;
   1488 
   1489 	p = name;
   1490 	if (! is_name(*p))
   1491 		return 0;
   1492 	while (*++p) {
   1493 		if (! is_in_name(*p))
   1494 			return 0;
   1495 	}
   1496 	return 1;
   1497 }
   1498 
   1499 
   1500 /*
   1501  * Called when an unexpected token is read during the parse.  The argument
   1502  * is the token that is expected, or -1 if more than one type of token can
   1503  * occur at this point.
   1504  */
   1505 
   1506 STATIC void
   1507 synexpect(token)
   1508 	int token;
   1509 {
   1510 	char msg[64];
   1511 
   1512 	if (token >= 0) {
   1513 		fmtstr(msg, 64, "%s unexpected (expecting %s)",
   1514 			tokname[lasttoken], tokname[token]);
   1515 	} else {
   1516 		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
   1517 	}
   1518 	synerror(msg);
   1519 	/* NOTREACHED */
   1520 }
   1521 
   1522 
   1523 STATIC void
   1524 synerror(msg)
   1525 	char *msg;
   1526 	{
   1527 	if (commandname)
   1528 		outfmt(&errout, "%s: %d: ", commandname, startlinno);
   1529 	outfmt(&errout, "Syntax error: %s\n", msg);
   1530 	error((char *)NULL);
   1531 	/* NOTREACHED */
   1532 }
   1533 
   1534 STATIC void
   1535 setprompt(which)
   1536 	int which;
   1537 	{
   1538 	whichprompt = which;
   1539 
   1540 #ifndef SMALL
   1541 	if (!el)
   1542 #endif
   1543 		out2str(getprompt(NULL));
   1544 }
   1545 
   1546 /*
   1547  * called by editline -- any expansions to the prompt
   1548  *    should be added here.
   1549  */
   1550 char *
   1551 getprompt(unused)
   1552 	void *unused;
   1553 	{
   1554 	switch (whichprompt) {
   1555 	case 0:
   1556 		return "";
   1557 	case 1:
   1558 		return ps1val();
   1559 	case 2:
   1560 		return ps2val();
   1561 	default:
   1562 		return "<internal prompt error>";
   1563 	}
   1564 }
   1565