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