xref: /freebsd/bin/sh/parser.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes  * Kenneth Almquist.
94b88c807SRodney W. Grimes  *
104b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes  * are met:
134b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes  *    without specific prior written permission.
214b88c807SRodney W. Grimes  *
224b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes  * SUCH DAMAGE.
334b88c807SRodney W. Grimes  */
344b88c807SRodney W. Grimes 
35d81ca439SEdward Tomasz Napierala #include <sys/param.h>
36d81ca439SEdward Tomasz Napierala #include <pwd.h>
37aa9caaf6SPeter Wemm #include <stdlib.h>
38955e9f68SStefan Farfeleder #include <unistd.h>
3949e10f5eSJilles Tjoelker #include <stdio.h>
40*a675eaecSPiotr Pawel Stefaniak #include <time.h>
41aa9caaf6SPeter Wemm 
424b88c807SRodney W. Grimes #include "shell.h"
434b88c807SRodney W. Grimes #include "parser.h"
444b88c807SRodney W. Grimes #include "nodes.h"
454b88c807SRodney W. Grimes #include "expand.h"	/* defines rmescapes() */
464b88c807SRodney W. Grimes #include "syntax.h"
474b88c807SRodney W. Grimes #include "options.h"
484b88c807SRodney W. Grimes #include "input.h"
494b88c807SRodney W. Grimes #include "output.h"
504b88c807SRodney W. Grimes #include "var.h"
514b88c807SRodney W. Grimes #include "error.h"
524b88c807SRodney W. Grimes #include "memalloc.h"
534b88c807SRodney W. Grimes #include "mystring.h"
544b88c807SRodney W. Grimes #include "alias.h"
55aa9caaf6SPeter Wemm #include "show.h"
56f01e3d0cSMartin Cracauer #include "eval.h"
5767e109adSJilles Tjoelker #include "exec.h"	/* to check for special builtins */
58aa9caaf6SPeter Wemm #ifndef NO_HISTORY
594b88c807SRodney W. Grimes #include "myhistedit.h"
60aa9caaf6SPeter Wemm #endif
614b88c807SRodney W. Grimes 
624b88c807SRodney W. Grimes /*
634b88c807SRodney W. Grimes  * Shell command parser.
644b88c807SRodney W. Grimes  */
654b88c807SRodney W. Grimes 
663cf65f8aSJuraj Lutter #define	PROMPTLEN	192
674b88c807SRodney W. Grimes 
68135ff4b5SJilles Tjoelker /* values of checkkwd variable */
69135ff4b5SJilles Tjoelker #define CHKALIAS	0x1
70135ff4b5SJilles Tjoelker #define CHKKWD		0x2
71135ff4b5SJilles Tjoelker #define CHKNL		0x4
72135ff4b5SJilles Tjoelker 
734b88c807SRodney W. Grimes /* values returned by readtoken */
74aa9caaf6SPeter Wemm #include "token.h"
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes 
774b88c807SRodney W. Grimes 
784b88c807SRodney W. Grimes struct heredoc {
794b88c807SRodney W. Grimes 	struct heredoc *next;	/* next here document in list */
804b88c807SRodney W. Grimes 	union node *here;		/* redirection node */
814b88c807SRodney W. Grimes 	char *eofmark;		/* string indicating end of input */
824b88c807SRodney W. Grimes 	int striptabs;		/* if set, strip leading tabs */
834b88c807SRodney W. Grimes };
844b88c807SRodney W. Grimes 
858cf06f5eSJilles Tjoelker struct parser_temp {
868cf06f5eSJilles Tjoelker 	struct parser_temp *next;
878cf06f5eSJilles Tjoelker 	void *data;
888cf06f5eSJilles Tjoelker };
894b88c807SRodney W. Grimes 
904b88c807SRodney W. Grimes 
91aa7b6f82SDavid E. O'Brien static struct heredoc *heredoclist;	/* list of here documents to read */
92aa7b6f82SDavid E. O'Brien static int doprompt;		/* if set, prompt the user */
93aa7b6f82SDavid E. O'Brien static int needprompt;		/* true if interactive and at start of line */
94aa7b6f82SDavid E. O'Brien static int lasttoken;		/* last token read */
95f52924b4SJilles Tjoelker static int tokpushback;		/* last token pushed back */
96aa7b6f82SDavid E. O'Brien static char *wordtext;		/* text of last word returned by readtoken */
97b6dda50aSJilles Tjoelker static int checkkwd;
98aa7b6f82SDavid E. O'Brien static struct nodelist *backquotelist;
99aa7b6f82SDavid E. O'Brien static union node *redirnode;
100aa7b6f82SDavid E. O'Brien static struct heredoc *heredoc;
101aa7b6f82SDavid E. O'Brien static int quoteflag;		/* set if (part of) last token was quoted */
102aa7b6f82SDavid E. O'Brien static int startlinno;		/* line # where last token started */
103aa7b6f82SDavid E. O'Brien static int funclinno;		/* line # where the current function started */
104aa7b6f82SDavid E. O'Brien static struct parser_temp *parser_temp;
1054b88c807SRodney W. Grimes 
10692fe71faSJilles Tjoelker #define NOEOFMARK ((const char *)&heredoclist)
10792fe71faSJilles Tjoelker 
1084b88c807SRodney W. Grimes 
10932187151SJilles Tjoelker static union node *list(int);
11088328642SDavid E. O'Brien static union node *andor(void);
11188328642SDavid E. O'Brien static union node *pipeline(void);
11288328642SDavid E. O'Brien static union node *command(void);
11388328642SDavid E. O'Brien static union node *simplecmd(union node **, union node *);
11488328642SDavid E. O'Brien static union node *makename(void);
115510739ccSJilles Tjoelker static union node *makebinary(int type, union node *n1, union node *n2);
11688328642SDavid E. O'Brien static void parsefname(void);
11788328642SDavid E. O'Brien static void parseheredoc(void);
11888328642SDavid E. O'Brien static int peektoken(void);
11988328642SDavid E. O'Brien static int readtoken(void);
12088328642SDavid E. O'Brien static int xxreadtoken(void);
12146c6b52dSJilles Tjoelker static int readtoken1(int, const char *, const char *, int);
12288328642SDavid E. O'Brien static int noexpand(char *);
1236ab99f87SJilles Tjoelker static void consumetoken(int);
12488328642SDavid E. O'Brien static void synexpect(int) __dead2;
12588328642SDavid E. O'Brien static void synerror(const char *) __dead2;
12688328642SDavid E. O'Brien static void setprompt(int);
1270b4b9c81SJilles Tjoelker static int pgetc_linecont(void);
128d81ca439SEdward Tomasz Napierala static void getusername(char *, size_t);
1294b88c807SRodney W. Grimes 
130aa9caaf6SPeter Wemm 
13188328642SDavid E. O'Brien static void *
parser_temp_alloc(size_t len)1328cf06f5eSJilles Tjoelker parser_temp_alloc(size_t len)
1338cf06f5eSJilles Tjoelker {
1348cf06f5eSJilles Tjoelker 	struct parser_temp *t;
1358cf06f5eSJilles Tjoelker 
1368cf06f5eSJilles Tjoelker 	INTOFF;
1378cf06f5eSJilles Tjoelker 	t = ckmalloc(sizeof(*t));
1388cf06f5eSJilles Tjoelker 	t->data = NULL;
1398cf06f5eSJilles Tjoelker 	t->next = parser_temp;
1408cf06f5eSJilles Tjoelker 	parser_temp = t;
1418cf06f5eSJilles Tjoelker 	t->data = ckmalloc(len);
1428cf06f5eSJilles Tjoelker 	INTON;
1438cf06f5eSJilles Tjoelker 	return t->data;
1448cf06f5eSJilles Tjoelker }
1458cf06f5eSJilles Tjoelker 
1468cf06f5eSJilles Tjoelker 
14788328642SDavid E. O'Brien static void *
parser_temp_realloc(void * ptr,size_t len)1488cf06f5eSJilles Tjoelker parser_temp_realloc(void *ptr, size_t len)
1498cf06f5eSJilles Tjoelker {
1508cf06f5eSJilles Tjoelker 	struct parser_temp *t;
1518cf06f5eSJilles Tjoelker 
1528cf06f5eSJilles Tjoelker 	INTOFF;
1538cf06f5eSJilles Tjoelker 	t = parser_temp;
1548cf06f5eSJilles Tjoelker 	if (ptr != t->data)
1558cf06f5eSJilles Tjoelker 		error("bug: parser_temp_realloc misused");
1568cf06f5eSJilles Tjoelker 	t->data = ckrealloc(t->data, len);
1578cf06f5eSJilles Tjoelker 	INTON;
1588cf06f5eSJilles Tjoelker 	return t->data;
1598cf06f5eSJilles Tjoelker }
1608cf06f5eSJilles Tjoelker 
1618cf06f5eSJilles Tjoelker 
16288328642SDavid E. O'Brien static void
parser_temp_free_upto(void * ptr)1638cf06f5eSJilles Tjoelker parser_temp_free_upto(void *ptr)
1648cf06f5eSJilles Tjoelker {
1658cf06f5eSJilles Tjoelker 	struct parser_temp *t;
1668cf06f5eSJilles Tjoelker 	int done = 0;
1678cf06f5eSJilles Tjoelker 
1688cf06f5eSJilles Tjoelker 	INTOFF;
1698cf06f5eSJilles Tjoelker 	while (parser_temp != NULL && !done) {
1708cf06f5eSJilles Tjoelker 		t = parser_temp;
1718cf06f5eSJilles Tjoelker 		parser_temp = t->next;
1728cf06f5eSJilles Tjoelker 		done = t->data == ptr;
1738cf06f5eSJilles Tjoelker 		ckfree(t->data);
1748cf06f5eSJilles Tjoelker 		ckfree(t);
1758cf06f5eSJilles Tjoelker 	}
1768cf06f5eSJilles Tjoelker 	INTON;
1778cf06f5eSJilles Tjoelker 	if (!done)
1788cf06f5eSJilles Tjoelker 		error("bug: parser_temp_free_upto misused");
1798cf06f5eSJilles Tjoelker }
1808cf06f5eSJilles Tjoelker 
1818cf06f5eSJilles Tjoelker 
18288328642SDavid E. O'Brien static void
parser_temp_free_all(void)1838cf06f5eSJilles Tjoelker parser_temp_free_all(void)
1848cf06f5eSJilles Tjoelker {
1858cf06f5eSJilles Tjoelker 	struct parser_temp *t;
1868cf06f5eSJilles Tjoelker 
1878cf06f5eSJilles Tjoelker 	INTOFF;
1888cf06f5eSJilles Tjoelker 	while (parser_temp != NULL) {
1898cf06f5eSJilles Tjoelker 		t = parser_temp;
1908cf06f5eSJilles Tjoelker 		parser_temp = t->next;
1918cf06f5eSJilles Tjoelker 		ckfree(t->data);
1928cf06f5eSJilles Tjoelker 		ckfree(t);
1938cf06f5eSJilles Tjoelker 	}
1948cf06f5eSJilles Tjoelker 	INTON;
1958cf06f5eSJilles Tjoelker }
1968cf06f5eSJilles Tjoelker 
1978cf06f5eSJilles Tjoelker 
1984b88c807SRodney W. Grimes /*
1994b88c807SRodney W. Grimes  * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
2004b88c807SRodney W. Grimes  * valid parse tree indicating a blank line.)
2014b88c807SRodney W. Grimes  */
2024b88c807SRodney W. Grimes 
2034b88c807SRodney W. Grimes union node *
parsecmd(int interact)2045134c3f7SWarner Losh parsecmd(int interact)
205aa9caaf6SPeter Wemm {
2064b88c807SRodney W. Grimes 	int t;
2074b88c807SRodney W. Grimes 
2088cf06f5eSJilles Tjoelker 	/* This assumes the parser is not re-entered,
2098cf06f5eSJilles Tjoelker 	 * which could happen if we add command substitution on PS1/PS2.
2108cf06f5eSJilles Tjoelker 	 */
2118cf06f5eSJilles Tjoelker 	parser_temp_free_all();
2125d910070SJilles Tjoelker 	heredoclist = NULL;
2138cf06f5eSJilles Tjoelker 
21498e05fd3SMartin Cracauer 	tokpushback = 0;
2159bb8ccd6SJilles Tjoelker 	checkkwd = 0;
2164b88c807SRodney W. Grimes 	doprompt = interact;
2174b88c807SRodney W. Grimes 	if (doprompt)
2184b88c807SRodney W. Grimes 		setprompt(1);
2194b88c807SRodney W. Grimes 	else
2204b88c807SRodney W. Grimes 		setprompt(0);
2214b88c807SRodney W. Grimes 	needprompt = 0;
2224b88c807SRodney W. Grimes 	t = readtoken();
2234b88c807SRodney W. Grimes 	if (t == TEOF)
2244b88c807SRodney W. Grimes 		return NEOF;
2254b88c807SRodney W. Grimes 	if (t == TNL)
2264b88c807SRodney W. Grimes 		return NULL;
2274b88c807SRodney W. Grimes 	tokpushback++;
22832187151SJilles Tjoelker 	return list(1);
2294b88c807SRodney W. Grimes }
2304b88c807SRodney W. Grimes 
2314b88c807SRodney W. Grimes 
232d358fa78SJilles Tjoelker /*
233d358fa78SJilles Tjoelker  * Read and parse words for wordexp.
234d358fa78SJilles Tjoelker  * Returns a list of NARG nodes; NULL if there are no words.
235d358fa78SJilles Tjoelker  */
236d358fa78SJilles Tjoelker union node *
parsewordexp(void)237d358fa78SJilles Tjoelker parsewordexp(void)
238d358fa78SJilles Tjoelker {
239d358fa78SJilles Tjoelker 	union node *n, *first = NULL, **pnext;
240d358fa78SJilles Tjoelker 	int t;
241d358fa78SJilles Tjoelker 
242d358fa78SJilles Tjoelker 	/* This assumes the parser is not re-entered,
243d358fa78SJilles Tjoelker 	 * which could happen if we add command substitution on PS1/PS2.
244d358fa78SJilles Tjoelker 	 */
245d358fa78SJilles Tjoelker 	parser_temp_free_all();
246d358fa78SJilles Tjoelker 	heredoclist = NULL;
247d358fa78SJilles Tjoelker 
248d358fa78SJilles Tjoelker 	tokpushback = 0;
249d358fa78SJilles Tjoelker 	checkkwd = 0;
250d358fa78SJilles Tjoelker 	doprompt = 0;
251d358fa78SJilles Tjoelker 	setprompt(0);
252d358fa78SJilles Tjoelker 	needprompt = 0;
253d358fa78SJilles Tjoelker 	pnext = &first;
254d358fa78SJilles Tjoelker 	while ((t = readtoken()) != TEOF) {
255d358fa78SJilles Tjoelker 		if (t != TWORD)
256d358fa78SJilles Tjoelker 			synexpect(TWORD);
257d358fa78SJilles Tjoelker 		n = makename();
258d358fa78SJilles Tjoelker 		*pnext = n;
259d358fa78SJilles Tjoelker 		pnext = &n->narg.next;
260d358fa78SJilles Tjoelker 	}
261d358fa78SJilles Tjoelker 	return first;
262d358fa78SJilles Tjoelker }
263d358fa78SJilles Tjoelker 
264d358fa78SJilles Tjoelker 
26588328642SDavid E. O'Brien static union node *
list(int nlflag)26632187151SJilles Tjoelker list(int nlflag)
267aa9caaf6SPeter Wemm {
268dca867f1SJilles Tjoelker 	union node *ntop, *n1, *n2, *n3;
269aa9caaf6SPeter Wemm 	int tok;
2704b88c807SRodney W. Grimes 
271135ff4b5SJilles Tjoelker 	checkkwd = CHKNL | CHKKWD | CHKALIAS;
27232187151SJilles Tjoelker 	if (!nlflag && tokendlist[peektoken()])
2734b88c807SRodney W. Grimes 		return NULL;
274dca867f1SJilles Tjoelker 	ntop = n1 = NULL;
2754b88c807SRodney W. Grimes 	for (;;) {
2764b88c807SRodney W. Grimes 		n2 = andor();
277aa9caaf6SPeter Wemm 		tok = readtoken();
278aa9caaf6SPeter Wemm 		if (tok == TBACKGND) {
27931d39658SJilles Tjoelker 			if (n2 != NULL && n2->type == NPIPE) {
28047e5204eSJilles Tjoelker 				n2->npipe.backgnd = 1;
28131d39658SJilles Tjoelker 			} else if (n2 != NULL && n2->type == NREDIR) {
282aa9caaf6SPeter Wemm 				n2->type = NBACKGND;
283aa9caaf6SPeter Wemm 			} else {
284aa9caaf6SPeter Wemm 				n3 = (union node *)stalloc(sizeof (struct nredir));
285aa9caaf6SPeter Wemm 				n3->type = NBACKGND;
286aa9caaf6SPeter Wemm 				n3->nredir.n = n2;
287aa9caaf6SPeter Wemm 				n3->nredir.redirect = NULL;
288aa9caaf6SPeter Wemm 				n2 = n3;
289aa9caaf6SPeter Wemm 			}
290aa9caaf6SPeter Wemm 		}
291dca867f1SJilles Tjoelker 		if (ntop == NULL)
292dca867f1SJilles Tjoelker 			ntop = n2;
293dca867f1SJilles Tjoelker 		else if (n1 == NULL) {
294510739ccSJilles Tjoelker 			n1 = makebinary(NSEMI, ntop, n2);
295dca867f1SJilles Tjoelker 			ntop = n1;
296aa9caaf6SPeter Wemm 		}
297aa9caaf6SPeter Wemm 		else {
298510739ccSJilles Tjoelker 			n3 = makebinary(NSEMI, n1->nbinary.ch2, n2);
299dca867f1SJilles Tjoelker 			n1->nbinary.ch2 = n3;
3004b88c807SRodney W. Grimes 			n1 = n3;
301aa9caaf6SPeter Wemm 		}
302aa9caaf6SPeter Wemm 		switch (tok) {
303aa9caaf6SPeter Wemm 		case TBACKGND:
304aa9caaf6SPeter Wemm 		case TSEMI:
305aa9caaf6SPeter Wemm 			tok = readtoken();
3060d9f1a69SPhilippe Charnier 			/* FALLTHROUGH */
307aa9caaf6SPeter Wemm 		case TNL:
308aa9caaf6SPeter Wemm 			if (tok == TNL) {
309aa9caaf6SPeter Wemm 				parseheredoc();
310aa9caaf6SPeter Wemm 				if (nlflag)
311dca867f1SJilles Tjoelker 					return ntop;
3126c0c2403SJilles Tjoelker 			} else if (tok == TEOF && nlflag) {
3136c0c2403SJilles Tjoelker 				parseheredoc();
314dca867f1SJilles Tjoelker 				return ntop;
315aa9caaf6SPeter Wemm 			} else {
316aa9caaf6SPeter Wemm 				tokpushback++;
317aa9caaf6SPeter Wemm 			}
318135ff4b5SJilles Tjoelker 			checkkwd = CHKNL | CHKKWD | CHKALIAS;
31932187151SJilles Tjoelker 			if (!nlflag && tokendlist[peektoken()])
320dca867f1SJilles Tjoelker 				return ntop;
3214b88c807SRodney W. Grimes 			break;
3224b88c807SRodney W. Grimes 		case TEOF:
3234b88c807SRodney W. Grimes 			if (heredoclist)
3244b88c807SRodney W. Grimes 				parseheredoc();
3254b88c807SRodney W. Grimes 			else
3264b88c807SRodney W. Grimes 				pungetc();		/* push back EOF on input */
327dca867f1SJilles Tjoelker 			return ntop;
3284b88c807SRodney W. Grimes 		default:
32932187151SJilles Tjoelker 			if (nlflag)
3304b88c807SRodney W. Grimes 				synexpect(-1);
3314b88c807SRodney W. Grimes 			tokpushback++;
332dca867f1SJilles Tjoelker 			return ntop;
3334b88c807SRodney W. Grimes 		}
3344b88c807SRodney W. Grimes 	}
3354b88c807SRodney W. Grimes }
3364b88c807SRodney W. Grimes 
3374b88c807SRodney W. Grimes 
3384b88c807SRodney W. Grimes 
33988328642SDavid E. O'Brien static union node *
andor(void)3405134c3f7SWarner Losh andor(void)
3415134c3f7SWarner Losh {
342510739ccSJilles Tjoelker 	union node *n;
3434b88c807SRodney W. Grimes 	int t;
3444b88c807SRodney W. Grimes 
345510739ccSJilles Tjoelker 	n = pipeline();
3464b88c807SRodney W. Grimes 	for (;;) {
3474b88c807SRodney W. Grimes 		if ((t = readtoken()) == TAND) {
3484b88c807SRodney W. Grimes 			t = NAND;
3494b88c807SRodney W. Grimes 		} else if (t == TOR) {
3504b88c807SRodney W. Grimes 			t = NOR;
3514b88c807SRodney W. Grimes 		} else {
3524b88c807SRodney W. Grimes 			tokpushback++;
353510739ccSJilles Tjoelker 			return n;
3544b88c807SRodney W. Grimes 		}
355510739ccSJilles Tjoelker 		n = makebinary(t, n, pipeline());
3564b88c807SRodney W. Grimes 	}
3574b88c807SRodney W. Grimes }
3584b88c807SRodney W. Grimes 
3594b88c807SRodney W. Grimes 
3604b88c807SRodney W. Grimes 
36188328642SDavid E. O'Brien static union node *
pipeline(void)3625134c3f7SWarner Losh pipeline(void)
3635134c3f7SWarner Losh {
364b785bd7dSBrian Somers 	union node *n1, *n2, *pipenode;
3654b88c807SRodney W. Grimes 	struct nodelist *lp, *prev;
366ba08f69bSJilles Tjoelker 	int negate, t;
3674b88c807SRodney W. Grimes 
368b785bd7dSBrian Somers 	negate = 0;
369135ff4b5SJilles Tjoelker 	checkkwd = CHKNL | CHKKWD | CHKALIAS;
3704b88c807SRodney W. Grimes 	TRACE(("pipeline: entered\n"));
371b785bd7dSBrian Somers 	while (readtoken() == TNOT)
372b785bd7dSBrian Somers 		negate = !negate;
373b785bd7dSBrian Somers 	tokpushback++;
3744b88c807SRodney W. Grimes 	n1 = command();
3754b88c807SRodney W. Grimes 	if (readtoken() == TPIPE) {
3764b88c807SRodney W. Grimes 		pipenode = (union node *)stalloc(sizeof (struct npipe));
3774b88c807SRodney W. Grimes 		pipenode->type = NPIPE;
3784b88c807SRodney W. Grimes 		pipenode->npipe.backgnd = 0;
3794b88c807SRodney W. Grimes 		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
3804b88c807SRodney W. Grimes 		pipenode->npipe.cmdlist = lp;
3814b88c807SRodney W. Grimes 		lp->n = n1;
3824b88c807SRodney W. Grimes 		do {
3834b88c807SRodney W. Grimes 			prev = lp;
3844b88c807SRodney W. Grimes 			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
385135ff4b5SJilles Tjoelker 			checkkwd = CHKNL | CHKKWD | CHKALIAS;
386ba08f69bSJilles Tjoelker 			t = readtoken();
387ba08f69bSJilles Tjoelker 			tokpushback++;
388ba08f69bSJilles Tjoelker 			if (t == TNOT)
389ba08f69bSJilles Tjoelker 				lp->n = pipeline();
390ba08f69bSJilles Tjoelker 			else
3914b88c807SRodney W. Grimes 				lp->n = command();
3924b88c807SRodney W. Grimes 			prev->next = lp;
3934b88c807SRodney W. Grimes 		} while (readtoken() == TPIPE);
3944b88c807SRodney W. Grimes 		lp->next = NULL;
3954b88c807SRodney W. Grimes 		n1 = pipenode;
3964b88c807SRodney W. Grimes 	}
3974b88c807SRodney W. Grimes 	tokpushback++;
398b785bd7dSBrian Somers 	if (negate) {
399b785bd7dSBrian Somers 		n2 = (union node *)stalloc(sizeof (struct nnot));
400b785bd7dSBrian Somers 		n2->type = NNOT;
401b785bd7dSBrian Somers 		n2->nnot.com = n1;
402b785bd7dSBrian Somers 		return n2;
403b785bd7dSBrian Somers 	} else
4044b88c807SRodney W. Grimes 		return n1;
4054b88c807SRodney W. Grimes }
4064b88c807SRodney W. Grimes 
4074b88c807SRodney W. Grimes 
4084b88c807SRodney W. Grimes 
40988328642SDavid E. O'Brien static union node *
command(void)4105134c3f7SWarner Losh command(void)
4115134c3f7SWarner Losh {
4124b88c807SRodney W. Grimes 	union node *n1, *n2;
4134b88c807SRodney W. Grimes 	union node *ap, **app;
4144b88c807SRodney W. Grimes 	union node *cp, **cpp;
4154b88c807SRodney W. Grimes 	union node *redir, **rpp;
416ba08f69bSJilles Tjoelker 	int t;
417b15e9aa3SJilles Tjoelker 	int is_subshell;
4184b88c807SRodney W. Grimes 
419135ff4b5SJilles Tjoelker 	checkkwd = CHKNL | CHKKWD | CHKALIAS;
420b15e9aa3SJilles Tjoelker 	is_subshell = 0;
421aa9caaf6SPeter Wemm 	redir = NULL;
422aa9caaf6SPeter Wemm 	n1 = NULL;
4234b88c807SRodney W. Grimes 	rpp = &redir;
424ab0a2172SSteve Price 
4254b88c807SRodney W. Grimes 	/* Check for redirection which may precede command */
4264b88c807SRodney W. Grimes 	while (readtoken() == TREDIR) {
4274b88c807SRodney W. Grimes 		*rpp = n2 = redirnode;
4284b88c807SRodney W. Grimes 		rpp = &n2->nfile.next;
4294b88c807SRodney W. Grimes 		parsefname();
4304b88c807SRodney W. Grimes 	}
4314b88c807SRodney W. Grimes 	tokpushback++;
4324b88c807SRodney W. Grimes 
4334b88c807SRodney W. Grimes 	switch (readtoken()) {
4344b88c807SRodney W. Grimes 	case TIF:
4354b88c807SRodney W. Grimes 		n1 = (union node *)stalloc(sizeof (struct nif));
4364b88c807SRodney W. Grimes 		n1->type = NIF;
43732187151SJilles Tjoelker 		if ((n1->nif.test = list(0)) == NULL)
438427748f7STim J. Robbins 			synexpect(-1);
4396ab99f87SJilles Tjoelker 		consumetoken(TTHEN);
44032187151SJilles Tjoelker 		n1->nif.ifpart = list(0);
4414b88c807SRodney W. Grimes 		n2 = n1;
4424b88c807SRodney W. Grimes 		while (readtoken() == TELIF) {
4434b88c807SRodney W. Grimes 			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
4444b88c807SRodney W. Grimes 			n2 = n2->nif.elsepart;
4454b88c807SRodney W. Grimes 			n2->type = NIF;
44632187151SJilles Tjoelker 			if ((n2->nif.test = list(0)) == NULL)
447427748f7STim J. Robbins 				synexpect(-1);
4486ab99f87SJilles Tjoelker 			consumetoken(TTHEN);
44932187151SJilles Tjoelker 			n2->nif.ifpart = list(0);
4504b88c807SRodney W. Grimes 		}
4514b88c807SRodney W. Grimes 		if (lasttoken == TELSE)
45232187151SJilles Tjoelker 			n2->nif.elsepart = list(0);
4534b88c807SRodney W. Grimes 		else {
4544b88c807SRodney W. Grimes 			n2->nif.elsepart = NULL;
4554b88c807SRodney W. Grimes 			tokpushback++;
4564b88c807SRodney W. Grimes 		}
4576ab99f87SJilles Tjoelker 		consumetoken(TFI);
458135ff4b5SJilles Tjoelker 		checkkwd = CHKKWD | CHKALIAS;
4594b88c807SRodney W. Grimes 		break;
4604b88c807SRodney W. Grimes 	case TWHILE:
4616ab99f87SJilles Tjoelker 	case TUNTIL:
462510739ccSJilles Tjoelker 		t = lasttoken;
46332187151SJilles Tjoelker 		if ((n1 = list(0)) == NULL)
464427748f7STim J. Robbins 			synexpect(-1);
4656ab99f87SJilles Tjoelker 		consumetoken(TDO);
46632187151SJilles Tjoelker 		n1 = makebinary((t == TWHILE)? NWHILE : NUNTIL, n1, list(0));
4676ab99f87SJilles Tjoelker 		consumetoken(TDONE);
468135ff4b5SJilles Tjoelker 		checkkwd = CHKKWD | CHKALIAS;
4694b88c807SRodney W. Grimes 		break;
4704b88c807SRodney W. Grimes 	case TFOR:
4714b88c807SRodney W. Grimes 		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
4724b88c807SRodney W. Grimes 			synerror("Bad for loop variable");
4734b88c807SRodney W. Grimes 		n1 = (union node *)stalloc(sizeof (struct nfor));
4744b88c807SRodney W. Grimes 		n1->type = NFOR;
4754b88c807SRodney W. Grimes 		n1->nfor.var = wordtext;
47672f750dcSJilles Tjoelker 		checkkwd = CHKNL;
47772f750dcSJilles Tjoelker 		if (readtoken() == TWORD && !quoteflag &&
47872f750dcSJilles Tjoelker 		    equal(wordtext, "in")) {
4794b88c807SRodney W. Grimes 			app = &ap;
4804b88c807SRodney W. Grimes 			while (readtoken() == TWORD) {
48147752ed6SJilles Tjoelker 				n2 = makename();
4824b88c807SRodney W. Grimes 				*app = n2;
4834b88c807SRodney W. Grimes 				app = &n2->narg.next;
4844b88c807SRodney W. Grimes 			}
4854b88c807SRodney W. Grimes 			*app = NULL;
4864b88c807SRodney W. Grimes 			n1->nfor.args = ap;
48772f750dcSJilles Tjoelker 			if (lasttoken == TNL)
48872f750dcSJilles Tjoelker 				tokpushback++;
48972f750dcSJilles Tjoelker 			else if (lasttoken != TSEMI)
4904b88c807SRodney W. Grimes 				synexpect(-1);
4914b88c807SRodney W. Grimes 		} else {
492811beb4bSStefan Farfeleder 			static char argvars[5] = {
493811beb4bSStefan Farfeleder 				CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
494811beb4bSStefan Farfeleder 			};
4954b88c807SRodney W. Grimes 			n2 = (union node *)stalloc(sizeof (struct narg));
4964b88c807SRodney W. Grimes 			n2->type = NARG;
497811beb4bSStefan Farfeleder 			n2->narg.text = argvars;
4984b88c807SRodney W. Grimes 			n2->narg.backquote = NULL;
4994b88c807SRodney W. Grimes 			n2->narg.next = NULL;
5004b88c807SRodney W. Grimes 			n1->nfor.args = n2;
5014b88c807SRodney W. Grimes 			/*
5024b88c807SRodney W. Grimes 			 * Newline or semicolon here is optional (but note
5034b88c807SRodney W. Grimes 			 * that the original Bourne shell only allowed NL).
5044b88c807SRodney W. Grimes 			 */
50572f750dcSJilles Tjoelker 			if (lasttoken != TSEMI)
5064b88c807SRodney W. Grimes 				tokpushback++;
5074b88c807SRodney W. Grimes 		}
508135ff4b5SJilles Tjoelker 		checkkwd = CHKNL | CHKKWD | CHKALIAS;
5094b88c807SRodney W. Grimes 		if ((t = readtoken()) == TDO)
5104b88c807SRodney W. Grimes 			t = TDONE;
5114b88c807SRodney W. Grimes 		else if (t == TBEGIN)
5124b88c807SRodney W. Grimes 			t = TEND;
5134b88c807SRodney W. Grimes 		else
5144b88c807SRodney W. Grimes 			synexpect(-1);
51532187151SJilles Tjoelker 		n1->nfor.body = list(0);
5166ab99f87SJilles Tjoelker 		consumetoken(t);
517135ff4b5SJilles Tjoelker 		checkkwd = CHKKWD | CHKALIAS;
5184b88c807SRodney W. Grimes 		break;
5194b88c807SRodney W. Grimes 	case TCASE:
5204b88c807SRodney W. Grimes 		n1 = (union node *)stalloc(sizeof (struct ncase));
5214b88c807SRodney W. Grimes 		n1->type = NCASE;
5226ab99f87SJilles Tjoelker 		consumetoken(TWORD);
52347752ed6SJilles Tjoelker 		n1->ncase.expr = makename();
52472f750dcSJilles Tjoelker 		checkkwd = CHKNL;
52572f750dcSJilles Tjoelker 		if (readtoken() != TWORD || ! equal(wordtext, "in"))
5264b88c807SRodney W. Grimes 			synerror("expecting \"in\"");
5274b88c807SRodney W. Grimes 		cpp = &n1->ncase.cases;
528135ff4b5SJilles Tjoelker 		checkkwd = CHKNL | CHKKWD, readtoken();
529e00e16adSTim J. Robbins 		while (lasttoken != TESAC) {
5304b88c807SRodney W. Grimes 			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
5314b88c807SRodney W. Grimes 			cp->type = NCLIST;
5324b88c807SRodney W. Grimes 			app = &cp->nclist.pattern;
533f7a9b7feSTim J. Robbins 			if (lasttoken == TLP)
534f7a9b7feSTim J. Robbins 				readtoken();
5354b88c807SRodney W. Grimes 			for (;;) {
53647752ed6SJilles Tjoelker 				*app = ap = makename();
537135ff4b5SJilles Tjoelker 				checkkwd = CHKNL | CHKKWD;
538135ff4b5SJilles Tjoelker 				if (readtoken() != TPIPE)
5394b88c807SRodney W. Grimes 					break;
5404b88c807SRodney W. Grimes 				app = &ap->narg.next;
541650488feSSean Eric Fagan 				readtoken();
5424b88c807SRodney W. Grimes 			}
5434b88c807SRodney W. Grimes 			ap->narg.next = NULL;
5444b88c807SRodney W. Grimes 			if (lasttoken != TRP)
545135ff4b5SJilles Tjoelker 				synexpect(TRP);
54632187151SJilles Tjoelker 			cp->nclist.body = list(0);
547650488feSSean Eric Fagan 
548135ff4b5SJilles Tjoelker 			checkkwd = CHKNL | CHKKWD | CHKALIAS;
549650488feSSean Eric Fagan 			if ((t = readtoken()) != TESAC) {
550c9afaa63SJilles Tjoelker 				if (t == TENDCASE)
551c9afaa63SJilles Tjoelker 					;
552c9afaa63SJilles Tjoelker 				else if (t == TFALLTHRU)
553c9afaa63SJilles Tjoelker 					cp->type = NCLISTFALLTHRU;
554650488feSSean Eric Fagan 				else
555c9afaa63SJilles Tjoelker 					synexpect(TENDCASE);
556135ff4b5SJilles Tjoelker 				checkkwd = CHKNL | CHKKWD, readtoken();
5574b88c807SRodney W. Grimes 			}
558650488feSSean Eric Fagan 			cpp = &cp->nclist.next;
559e00e16adSTim J. Robbins 		}
5604b88c807SRodney W. Grimes 		*cpp = NULL;
561135ff4b5SJilles Tjoelker 		checkkwd = CHKKWD | CHKALIAS;
5624b88c807SRodney W. Grimes 		break;
5634b88c807SRodney W. Grimes 	case TLP:
5644b88c807SRodney W. Grimes 		n1 = (union node *)stalloc(sizeof (struct nredir));
5654b88c807SRodney W. Grimes 		n1->type = NSUBSHELL;
56632187151SJilles Tjoelker 		n1->nredir.n = list(0);
5674b88c807SRodney W. Grimes 		n1->nredir.redirect = NULL;
5686ab99f87SJilles Tjoelker 		consumetoken(TRP);
569135ff4b5SJilles Tjoelker 		checkkwd = CHKKWD | CHKALIAS;
570b15e9aa3SJilles Tjoelker 		is_subshell = 1;
5714b88c807SRodney W. Grimes 		break;
5724b88c807SRodney W. Grimes 	case TBEGIN:
57332187151SJilles Tjoelker 		n1 = list(0);
5746ab99f87SJilles Tjoelker 		consumetoken(TEND);
575135ff4b5SJilles Tjoelker 		checkkwd = CHKKWD | CHKALIAS;
5764b88c807SRodney W. Grimes 		break;
5773cfb11c4SJilles Tjoelker 	/* A simple command must have at least one redirection or word. */
578cac4830cSJilles Tjoelker 	case TBACKGND:
579248ffae5SJoerg Wunsch 	case TSEMI:
580d8d737d7STim J. Robbins 	case TAND:
581d8d737d7STim J. Robbins 	case TOR:
582adc2e8dfSJilles Tjoelker 	case TPIPE:
583adc2e8dfSJilles Tjoelker 	case TENDCASE:
584adc2e8dfSJilles Tjoelker 	case TFALLTHRU:
5853cfb11c4SJilles Tjoelker 	case TEOF:
5863cfb11c4SJilles Tjoelker 	case TNL:
5873cfb11c4SJilles Tjoelker 	case TRP:
588aa9caaf6SPeter Wemm 		if (!redir)
589aa9caaf6SPeter Wemm 			synexpect(-1);
5904b88c807SRodney W. Grimes 	case TWORD:
5914b88c807SRodney W. Grimes 		tokpushback++;
5926c0bde79SBrian Somers 		n1 = simplecmd(rpp, redir);
593ba08f69bSJilles Tjoelker 		return n1;
5944b88c807SRodney W. Grimes 	default:
5954b88c807SRodney W. Grimes 		synexpect(-1);
5964b88c807SRodney W. Grimes 	}
5974b88c807SRodney W. Grimes 
5984b88c807SRodney W. Grimes 	/* Now check for redirection which may follow command */
5994b88c807SRodney W. Grimes 	while (readtoken() == TREDIR) {
6004b88c807SRodney W. Grimes 		*rpp = n2 = redirnode;
6014b88c807SRodney W. Grimes 		rpp = &n2->nfile.next;
6024b88c807SRodney W. Grimes 		parsefname();
6034b88c807SRodney W. Grimes 	}
6044b88c807SRodney W. Grimes 	tokpushback++;
6054b88c807SRodney W. Grimes 	*rpp = NULL;
6064b88c807SRodney W. Grimes 	if (redir) {
607b15e9aa3SJilles Tjoelker 		if (!is_subshell) {
6084b88c807SRodney W. Grimes 			n2 = (union node *)stalloc(sizeof (struct nredir));
6094b88c807SRodney W. Grimes 			n2->type = NREDIR;
6104b88c807SRodney W. Grimes 			n2->nredir.n = n1;
6114b88c807SRodney W. Grimes 			n1 = n2;
6124b88c807SRodney W. Grimes 		}
6134b88c807SRodney W. Grimes 		n1->nredir.redirect = redir;
6144b88c807SRodney W. Grimes 	}
6156c0bde79SBrian Somers 
6164b88c807SRodney W. Grimes 	return n1;
6174b88c807SRodney W. Grimes }
6184b88c807SRodney W. Grimes 
6194b88c807SRodney W. Grimes 
62088328642SDavid E. O'Brien static union node *
simplecmd(union node ** rpp,union node * redir)6215134c3f7SWarner Losh simplecmd(union node **rpp, union node *redir)
6224b88c807SRodney W. Grimes {
6234b88c807SRodney W. Grimes 	union node *args, **app;
6244b88c807SRodney W. Grimes 	union node **orig_rpp = rpp;
625d5af15eaSJilles Tjoelker 	union node *n = NULL;
62667e109adSJilles Tjoelker 	int special;
62705a447d0SJilles Tjoelker 	int savecheckkwd;
6284b88c807SRodney W. Grimes 
6294b88c807SRodney W. Grimes 	/* If we don't have any redirections already, then we must reset */
6304b88c807SRodney W. Grimes 	/* rpp to be the address of the local redir variable.  */
63174136dc3SPedro F. Giffuni 	if (redir == NULL)
6324b88c807SRodney W. Grimes 		rpp = &redir;
6334b88c807SRodney W. Grimes 
6344b88c807SRodney W. Grimes 	args = NULL;
6354b88c807SRodney W. Grimes 	app = &args;
6364b88c807SRodney W. Grimes 	/*
6374b88c807SRodney W. Grimes 	 * We save the incoming value, because we need this for shell
6384b88c807SRodney W. Grimes 	 * functions.  There can not be a redirect or an argument between
6394b88c807SRodney W. Grimes 	 * the function name and the open parenthesis.
6404b88c807SRodney W. Grimes 	 */
6414b88c807SRodney W. Grimes 	orig_rpp = rpp;
6424b88c807SRodney W. Grimes 
64305a447d0SJilles Tjoelker 	savecheckkwd = CHKALIAS;
64405a447d0SJilles Tjoelker 
6454b88c807SRodney W. Grimes 	for (;;) {
64605a447d0SJilles Tjoelker 		checkkwd = savecheckkwd;
6474b88c807SRodney W. Grimes 		if (readtoken() == TWORD) {
64847752ed6SJilles Tjoelker 			n = makename();
6494b88c807SRodney W. Grimes 			*app = n;
6504b88c807SRodney W. Grimes 			app = &n->narg.next;
65105a447d0SJilles Tjoelker 			if (savecheckkwd != 0 && !isassignment(wordtext))
65205a447d0SJilles Tjoelker 				savecheckkwd = 0;
6534b88c807SRodney W. Grimes 		} else if (lasttoken == TREDIR) {
6544b88c807SRodney W. Grimes 			*rpp = n = redirnode;
6554b88c807SRodney W. Grimes 			rpp = &n->nfile.next;
6564b88c807SRodney W. Grimes 			parsefname();	/* read name of redirection file */
6574b88c807SRodney W. Grimes 		} else if (lasttoken == TLP && app == &args->narg.next
6584b88c807SRodney W. Grimes 					    && rpp == orig_rpp) {
6594b88c807SRodney W. Grimes 			/* We have a function */
6606ab99f87SJilles Tjoelker 			consumetoken(TRP);
661b71085aaSStefan Farfeleder 			funclinno = plinno;
662074e83b1SJilles Tjoelker 			/*
663074e83b1SJilles Tjoelker 			 * - Require plain text.
664074e83b1SJilles Tjoelker 			 * - Functions with '/' cannot be called.
665a1251487SJilles Tjoelker 			 * - Reject name=().
666a1251487SJilles Tjoelker 			 * - Reject ksh extended glob patterns.
667074e83b1SJilles Tjoelker 			 */
668074e83b1SJilles Tjoelker 			if (!noexpand(n->narg.text) || quoteflag ||
669a1251487SJilles Tjoelker 			    strchr(n->narg.text, '/') ||
670a1251487SJilles Tjoelker 			    strchr("!%*+-=?@}~",
671a1251487SJilles Tjoelker 				n->narg.text[strlen(n->narg.text) - 1]))
6724b88c807SRodney W. Grimes 				synerror("Bad function name");
673074e83b1SJilles Tjoelker 			rmescapes(n->narg.text);
67467e109adSJilles Tjoelker 			if (find_builtin(n->narg.text, &special) >= 0 &&
67567e109adSJilles Tjoelker 			    special)
67667e109adSJilles Tjoelker 				synerror("Cannot override a special builtin with a function");
6774b88c807SRodney W. Grimes 			n->type = NDEFUN;
6784b88c807SRodney W. Grimes 			n->narg.next = command();
679b71085aaSStefan Farfeleder 			funclinno = 0;
680d5af15eaSJilles Tjoelker 			return n;
6814b88c807SRodney W. Grimes 		} else {
6824b88c807SRodney W. Grimes 			tokpushback++;
6834b88c807SRodney W. Grimes 			break;
6844b88c807SRodney W. Grimes 		}
6854b88c807SRodney W. Grimes 	}
6864b88c807SRodney W. Grimes 	*app = NULL;
6874b88c807SRodney W. Grimes 	*rpp = NULL;
6884b88c807SRodney W. Grimes 	n = (union node *)stalloc(sizeof (struct ncmd));
6894b88c807SRodney W. Grimes 	n->type = NCMD;
6904b88c807SRodney W. Grimes 	n->ncmd.args = args;
6914b88c807SRodney W. Grimes 	n->ncmd.redirect = redir;
6924b88c807SRodney W. Grimes 	return n;
6934b88c807SRodney W. Grimes }
6944b88c807SRodney W. Grimes 
69588328642SDavid E. O'Brien static union node *
makename(void)6965134c3f7SWarner Losh makename(void)
6975134c3f7SWarner Losh {
698aa9caaf6SPeter Wemm 	union node *n;
699aa9caaf6SPeter Wemm 
700aa9caaf6SPeter Wemm 	n = (union node *)stalloc(sizeof (struct narg));
701aa9caaf6SPeter Wemm 	n->type = NARG;
702aa9caaf6SPeter Wemm 	n->narg.next = NULL;
703aa9caaf6SPeter Wemm 	n->narg.text = wordtext;
704aa9caaf6SPeter Wemm 	n->narg.backquote = backquotelist;
705aa9caaf6SPeter Wemm 	return n;
706aa9caaf6SPeter Wemm }
707aa9caaf6SPeter Wemm 
708510739ccSJilles Tjoelker static union node *
makebinary(int type,union node * n1,union node * n2)709510739ccSJilles Tjoelker makebinary(int type, union node *n1, union node *n2)
710510739ccSJilles Tjoelker {
711510739ccSJilles Tjoelker 	union node *n;
712510739ccSJilles Tjoelker 
713510739ccSJilles Tjoelker 	n = (union node *)stalloc(sizeof (struct nbinary));
714510739ccSJilles Tjoelker 	n->type = type;
715510739ccSJilles Tjoelker 	n->nbinary.ch1 = n1;
716510739ccSJilles Tjoelker 	n->nbinary.ch2 = n2;
717510739ccSJilles Tjoelker 	return (n);
718510739ccSJilles Tjoelker }
719510739ccSJilles Tjoelker 
720aa7b6f82SDavid E. O'Brien void
forcealias(void)72148f49aacSJilles Tjoelker forcealias(void)
72248f49aacSJilles Tjoelker {
72348f49aacSJilles Tjoelker 	checkkwd |= CHKALIAS;
72448f49aacSJilles Tjoelker }
72548f49aacSJilles Tjoelker 
72648f49aacSJilles Tjoelker void
fixredir(union node * n,const char * text,int err)727aa7b6f82SDavid E. O'Brien fixredir(union node *n, const char *text, int err)
728aa9caaf6SPeter Wemm {
729aa9caaf6SPeter Wemm 	TRACE(("Fix redir %s %d\n", text, err));
730aa9caaf6SPeter Wemm 	if (!err)
731aa9caaf6SPeter Wemm 		n->ndup.vname = NULL;
732aa9caaf6SPeter Wemm 
733aa9caaf6SPeter Wemm 	if (is_digit(text[0]) && text[1] == '\0')
734aa9caaf6SPeter Wemm 		n->ndup.dupfd = digit_val(text[0]);
735aa9caaf6SPeter Wemm 	else if (text[0] == '-' && text[1] == '\0')
736aa9caaf6SPeter Wemm 		n->ndup.dupfd = -1;
737aa9caaf6SPeter Wemm 	else {
738aa9caaf6SPeter Wemm 
739aa9caaf6SPeter Wemm 		if (err)
740aa9caaf6SPeter Wemm 			synerror("Bad fd number");
741aa9caaf6SPeter Wemm 		else
742aa9caaf6SPeter Wemm 			n->ndup.vname = makename();
743aa9caaf6SPeter Wemm 	}
744aa9caaf6SPeter Wemm }
745aa9caaf6SPeter Wemm 
7464b88c807SRodney W. Grimes 
74788328642SDavid E. O'Brien static void
parsefname(void)7485134c3f7SWarner Losh parsefname(void)
7495134c3f7SWarner Losh {
7504b88c807SRodney W. Grimes 	union node *n = redirnode;
7514b88c807SRodney W. Grimes 
7526ab99f87SJilles Tjoelker 	consumetoken(TWORD);
7534b88c807SRodney W. Grimes 	if (n->type == NHERE) {
7544b88c807SRodney W. Grimes 		struct heredoc *here = heredoc;
7554b88c807SRodney W. Grimes 		struct heredoc *p;
7564b88c807SRodney W. Grimes 
7574b88c807SRodney W. Grimes 		if (quoteflag == 0)
7584b88c807SRodney W. Grimes 			n->type = NXHERE;
7594b88c807SRodney W. Grimes 		TRACE(("Here document %d\n", n->type));
7604b88c807SRodney W. Grimes 		if (here->striptabs) {
7614b88c807SRodney W. Grimes 			while (*wordtext == '\t')
7624b88c807SRodney W. Grimes 				wordtext++;
7634b88c807SRodney W. Grimes 		}
76472238faaSJilles Tjoelker 		if (! noexpand(wordtext))
7654b88c807SRodney W. Grimes 			synerror("Illegal eof marker for << redirection");
7664b88c807SRodney W. Grimes 		rmescapes(wordtext);
7674b88c807SRodney W. Grimes 		here->eofmark = wordtext;
7684b88c807SRodney W. Grimes 		here->next = NULL;
7694b88c807SRodney W. Grimes 		if (heredoclist == NULL)
7704b88c807SRodney W. Grimes 			heredoclist = here;
7714b88c807SRodney W. Grimes 		else {
7724b88c807SRodney W. Grimes 			for (p = heredoclist ; p->next ; p = p->next);
7734b88c807SRodney W. Grimes 			p->next = here;
7744b88c807SRodney W. Grimes 		}
7754b88c807SRodney W. Grimes 	} else if (n->type == NTOFD || n->type == NFROMFD) {
776aa9caaf6SPeter Wemm 		fixredir(n, wordtext, 0);
7774b88c807SRodney W. Grimes 	} else {
778aa9caaf6SPeter Wemm 		n->nfile.fname = makename();
7794b88c807SRodney W. Grimes 	}
7804b88c807SRodney W. Grimes }
7814b88c807SRodney W. Grimes 
7824b88c807SRodney W. Grimes 
7834b88c807SRodney W. Grimes /*
7844b88c807SRodney W. Grimes  * Input any here documents.
7854b88c807SRodney W. Grimes  */
7864b88c807SRodney W. Grimes 
78788328642SDavid E. O'Brien static void
parseheredoc(void)7885134c3f7SWarner Losh parseheredoc(void)
7895134c3f7SWarner Losh {
7904b88c807SRodney W. Grimes 	struct heredoc *here;
7914b88c807SRodney W. Grimes 	union node *n;
7924b88c807SRodney W. Grimes 
7934b88c807SRodney W. Grimes 	while (heredoclist) {
7944b88c807SRodney W. Grimes 		here = heredoclist;
7954b88c807SRodney W. Grimes 		heredoclist = here->next;
7964b88c807SRodney W. Grimes 		if (needprompt) {
7974b88c807SRodney W. Grimes 			setprompt(2);
7984b88c807SRodney W. Grimes 			needprompt = 0;
7994b88c807SRodney W. Grimes 		}
8004b88c807SRodney W. Grimes 		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
8014b88c807SRodney W. Grimes 				here->eofmark, here->striptabs);
80247752ed6SJilles Tjoelker 		n = makename();
8034b88c807SRodney W. Grimes 		here->here->nhere.doc = n;
8044b88c807SRodney W. Grimes 	}
8054b88c807SRodney W. Grimes }
8064b88c807SRodney W. Grimes 
80788328642SDavid E. O'Brien static int
peektoken(void)8085134c3f7SWarner Losh peektoken(void)
8095134c3f7SWarner Losh {
8104b88c807SRodney W. Grimes 	int t;
8114b88c807SRodney W. Grimes 
8124b88c807SRodney W. Grimes 	t = readtoken();
8134b88c807SRodney W. Grimes 	tokpushback++;
8144b88c807SRodney W. Grimes 	return (t);
8154b88c807SRodney W. Grimes }
8164b88c807SRodney W. Grimes 
81788328642SDavid E. O'Brien static int
readtoken(void)8185134c3f7SWarner Losh readtoken(void)
8195134c3f7SWarner Losh {
8204b88c807SRodney W. Grimes 	int t;
8214b88c807SRodney W. Grimes 	struct alias *ap;
8224b88c807SRodney W. Grimes #ifdef DEBUG
8234b88c807SRodney W. Grimes 	int alreadyseen = tokpushback;
8244b88c807SRodney W. Grimes #endif
8254b88c807SRodney W. Grimes 
8264b88c807SRodney W. Grimes 	top:
8274b88c807SRodney W. Grimes 	t = xxreadtoken();
8284b88c807SRodney W. Grimes 
8294b88c807SRodney W. Grimes 	/*
8304b88c807SRodney W. Grimes 	 * eat newlines
8314b88c807SRodney W. Grimes 	 */
832135ff4b5SJilles Tjoelker 	if (checkkwd & CHKNL) {
8334b88c807SRodney W. Grimes 		while (t == TNL) {
8344b88c807SRodney W. Grimes 			parseheredoc();
8354b88c807SRodney W. Grimes 			t = xxreadtoken();
8364b88c807SRodney W. Grimes 		}
837135ff4b5SJilles Tjoelker 	}
838135ff4b5SJilles Tjoelker 
8394b88c807SRodney W. Grimes 	/*
8404b88c807SRodney W. Grimes 	 * check for keywords and aliases
8414b88c807SRodney W. Grimes 	 */
842aa9caaf6SPeter Wemm 	if (t == TWORD && !quoteflag)
843aa9caaf6SPeter Wemm 	{
8448b7808bcSJuli Mallett 		const char * const *pp;
8454b88c807SRodney W. Grimes 
846135ff4b5SJilles Tjoelker 		if (checkkwd & CHKKWD)
8478b7808bcSJuli Mallett 			for (pp = parsekwd; *pp; pp++) {
848aa9caaf6SPeter Wemm 				if (**pp == *wordtext && equal(*pp, wordtext))
849aa9caaf6SPeter Wemm 				{
8504b88c807SRodney W. Grimes 					lasttoken = t = pp - parsekwd + KWDOFFSET;
8514b88c807SRodney W. Grimes 					TRACE(("keyword %s recognized\n", tokname[t]));
8524b88c807SRodney W. Grimes 					goto out;
8534b88c807SRodney W. Grimes 				}
8544b88c807SRodney W. Grimes 			}
855135ff4b5SJilles Tjoelker 		if (checkkwd & CHKALIAS &&
8564417f629SPeter Wemm 		    (ap = lookupalias(wordtext, 1)) != NULL) {
8574b88c807SRodney W. Grimes 			pushstring(ap->val, strlen(ap->val), ap);
8584b88c807SRodney W. Grimes 			goto top;
8594b88c807SRodney W. Grimes 		}
8604b88c807SRodney W. Grimes 	}
8614b88c807SRodney W. Grimes out:
862135ff4b5SJilles Tjoelker 	if (t != TNOT)
863135ff4b5SJilles Tjoelker 		checkkwd = 0;
864135ff4b5SJilles Tjoelker 
8654b88c807SRodney W. Grimes #ifdef DEBUG
8664b88c807SRodney W. Grimes 	if (!alreadyseen)
8674b88c807SRodney W. Grimes 	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8684b88c807SRodney W. Grimes 	else
8694b88c807SRodney W. Grimes 	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8704b88c807SRodney W. Grimes #endif
8714b88c807SRodney W. Grimes 	return (t);
8724b88c807SRodney W. Grimes }
8734b88c807SRodney W. Grimes 
8744b88c807SRodney W. Grimes 
8754b88c807SRodney W. Grimes /*
8764b88c807SRodney W. Grimes  * Read the next input token.
8774b88c807SRodney W. Grimes  * If the token is a word, we set backquotelist to the list of cmds in
8784b88c807SRodney W. Grimes  *	backquotes.  We set quoteflag to true if any part of the word was
8794b88c807SRodney W. Grimes  *	quoted.
8804b88c807SRodney W. Grimes  * If the token is TREDIR, then we set redirnode to a structure containing
8814b88c807SRodney W. Grimes  *	the redirection.
8824b88c807SRodney W. Grimes  * In all cases, the variable startlinno is set to the number of the line
8834b88c807SRodney W. Grimes  *	on which the token starts.
8844b88c807SRodney W. Grimes  *
8854b88c807SRodney W. Grimes  * [Change comment:  here documents and internal procedures]
8864b88c807SRodney W. Grimes  * [Readtoken shouldn't have any arguments.  Perhaps we should make the
8874b88c807SRodney W. Grimes  *  word parsing code into a separate routine.  In this case, readtoken
8884b88c807SRodney W. Grimes  *  doesn't need to have any internal procedures, but parseword does.
8894b88c807SRodney W. Grimes  *  We could also make parseoperator in essence the main routine, and
8904b88c807SRodney W. Grimes  *  have parseword (readtoken1?) handle both words and redirection.]
8914b88c807SRodney W. Grimes  */
8924b88c807SRodney W. Grimes 
8934b88c807SRodney W. Grimes #define RETURN(token)	return lasttoken = token
8944b88c807SRodney W. Grimes 
89588328642SDavid E. O'Brien static int
xxreadtoken(void)8965134c3f7SWarner Losh xxreadtoken(void)
8975134c3f7SWarner Losh {
8987920a31dSSteve Price 	int c;
8994b88c807SRodney W. Grimes 
9004b88c807SRodney W. Grimes 	if (tokpushback) {
9014b88c807SRodney W. Grimes 		tokpushback = 0;
9024b88c807SRodney W. Grimes 		return lasttoken;
9034b88c807SRodney W. Grimes 	}
9044b88c807SRodney W. Grimes 	if (needprompt) {
9054b88c807SRodney W. Grimes 		setprompt(2);
9064b88c807SRodney W. Grimes 		needprompt = 0;
9074b88c807SRodney W. Grimes 	}
9084b88c807SRodney W. Grimes 	startlinno = plinno;
9094b88c807SRodney W. Grimes 	for (;;) {	/* until token or start of word found */
9104b88c807SRodney W. Grimes 		c = pgetc_macro();
9114b88c807SRodney W. Grimes 		switch (c) {
9124b88c807SRodney W. Grimes 		case ' ': case '\t':
9134b88c807SRodney W. Grimes 			continue;
9144b88c807SRodney W. Grimes 		case '#':
9154b88c807SRodney W. Grimes 			while ((c = pgetc()) != '\n' && c != PEOF);
9164b88c807SRodney W. Grimes 			pungetc();
9174b88c807SRodney W. Grimes 			continue;
9184b88c807SRodney W. Grimes 		case '\\':
9194b88c807SRodney W. Grimes 			if (pgetc() == '\n') {
9204b88c807SRodney W. Grimes 				startlinno = ++plinno;
9214b88c807SRodney W. Grimes 				if (doprompt)
9224b88c807SRodney W. Grimes 					setprompt(2);
9234b88c807SRodney W. Grimes 				else
9244b88c807SRodney W. Grimes 					setprompt(0);
9254b88c807SRodney W. Grimes 				continue;
9264b88c807SRodney W. Grimes 			}
9274b88c807SRodney W. Grimes 			pungetc();
928622fdf32SJilles Tjoelker 			/* FALLTHROUGH */
929622fdf32SJilles Tjoelker 		default:
930622fdf32SJilles Tjoelker 			return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
9314b88c807SRodney W. Grimes 		case '\n':
9324b88c807SRodney W. Grimes 			plinno++;
9334b88c807SRodney W. Grimes 			needprompt = doprompt;
9344b88c807SRodney W. Grimes 			RETURN(TNL);
9354b88c807SRodney W. Grimes 		case PEOF:
9364b88c807SRodney W. Grimes 			RETURN(TEOF);
9374b88c807SRodney W. Grimes 		case '&':
9380b4b9c81SJilles Tjoelker 			if (pgetc_linecont() == '&')
9394b88c807SRodney W. Grimes 				RETURN(TAND);
9404b88c807SRodney W. Grimes 			pungetc();
9414b88c807SRodney W. Grimes 			RETURN(TBACKGND);
9424b88c807SRodney W. Grimes 		case '|':
9430b4b9c81SJilles Tjoelker 			if (pgetc_linecont() == '|')
9444b88c807SRodney W. Grimes 				RETURN(TOR);
9454b88c807SRodney W. Grimes 			pungetc();
9464b88c807SRodney W. Grimes 			RETURN(TPIPE);
9474b88c807SRodney W. Grimes 		case ';':
9480b4b9c81SJilles Tjoelker 			c = pgetc_linecont();
949c9afaa63SJilles Tjoelker 			if (c == ';')
9504b88c807SRodney W. Grimes 				RETURN(TENDCASE);
951c9afaa63SJilles Tjoelker 			else if (c == '&')
952c9afaa63SJilles Tjoelker 				RETURN(TFALLTHRU);
9534b88c807SRodney W. Grimes 			pungetc();
9544b88c807SRodney W. Grimes 			RETURN(TSEMI);
9554b88c807SRodney W. Grimes 		case '(':
9564b88c807SRodney W. Grimes 			RETURN(TLP);
9574b88c807SRodney W. Grimes 		case ')':
9584b88c807SRodney W. Grimes 			RETURN(TRP);
9594b88c807SRodney W. Grimes 		}
9604b88c807SRodney W. Grimes 	}
9614b88c807SRodney W. Grimes #undef RETURN
9624b88c807SRodney W. Grimes }
9634b88c807SRodney W. Grimes 
9644b88c807SRodney W. Grimes 
96588328642SDavid E. O'Brien #define MAXNEST_static 8
9668cf06f5eSJilles Tjoelker struct tokenstate
9678cf06f5eSJilles Tjoelker {
9688cf06f5eSJilles Tjoelker 	const char *syntax; /* *SYNTAX */
9698cf06f5eSJilles Tjoelker 	int parenlevel; /* levels of parentheses in arithmetic */
9708cf06f5eSJilles Tjoelker 	enum tokenstate_category
9718cf06f5eSJilles Tjoelker 	{
9728cf06f5eSJilles Tjoelker 		TSTATE_TOP,
9738cf06f5eSJilles Tjoelker 		TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
9748cf06f5eSJilles Tjoelker 		TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
9758cf06f5eSJilles Tjoelker 		TSTATE_ARITH
9768cf06f5eSJilles Tjoelker 	} category;
9778cf06f5eSJilles Tjoelker };
9788cf06f5eSJilles Tjoelker 
9798cf06f5eSJilles Tjoelker 
980cab84206SJilles Tjoelker /*
981671a890eSJilles Tjoelker  * Check to see whether we are at the end of the here document.  When this
982671a890eSJilles Tjoelker  * is called, c is set to the first character of the next input line.  If
983671a890eSJilles Tjoelker  * we are at the end of the here document, this routine sets the c to PEOF.
984671a890eSJilles Tjoelker  * The new value of c is returned.
985671a890eSJilles Tjoelker  */
986671a890eSJilles Tjoelker 
987671a890eSJilles Tjoelker static int
checkend(int c,const char * eofmark,int striptabs)98872238faaSJilles Tjoelker checkend(int c, const char *eofmark, int striptabs)
989671a890eSJilles Tjoelker {
990671a890eSJilles Tjoelker 	if (striptabs) {
991671a890eSJilles Tjoelker 		while (c == '\t')
992671a890eSJilles Tjoelker 			c = pgetc();
993671a890eSJilles Tjoelker 	}
994671a890eSJilles Tjoelker 	if (c == *eofmark) {
99572238faaSJilles Tjoelker 		int c2;
99672238faaSJilles Tjoelker 		const char *q;
997671a890eSJilles Tjoelker 
99872238faaSJilles Tjoelker 		for (q = eofmark + 1; c2 = pgetc(), *q != '\0' && c2 == *q; q++)
99972238faaSJilles Tjoelker 			;
100072238faaSJilles Tjoelker 		if ((c2 == PEOF || c2 == '\n') && *q == '\0') {
1001671a890eSJilles Tjoelker 			c = PEOF;
100272238faaSJilles Tjoelker 			if (c2 == '\n') {
1003671a890eSJilles Tjoelker 				plinno++;
1004671a890eSJilles Tjoelker 				needprompt = doprompt;
1005671a890eSJilles Tjoelker 			}
1006671a890eSJilles Tjoelker 		} else {
100772238faaSJilles Tjoelker 			pungetc();
100872238faaSJilles Tjoelker 			pushstring(eofmark + 1, q - (eofmark + 1), NULL);
1009671a890eSJilles Tjoelker 		}
101092fe71faSJilles Tjoelker 	} else if (c == '\n' && *eofmark == '\0') {
101192fe71faSJilles Tjoelker 		c = PEOF;
101292fe71faSJilles Tjoelker 		plinno++;
101392fe71faSJilles Tjoelker 		needprompt = doprompt;
1014671a890eSJilles Tjoelker 	}
1015671a890eSJilles Tjoelker 	return (c);
1016671a890eSJilles Tjoelker }
1017671a890eSJilles Tjoelker 
1018671a890eSJilles Tjoelker 
1019671a890eSJilles Tjoelker /*
10203f9b4e9aSJilles Tjoelker  * Parse a redirection operator.  The variable "out" points to a string
10213f9b4e9aSJilles Tjoelker  * specifying the fd to be redirected.  The variable "c" contains the
10223f9b4e9aSJilles Tjoelker  * first character of the redirection operator.
10233f9b4e9aSJilles Tjoelker  */
10243f9b4e9aSJilles Tjoelker 
10253f9b4e9aSJilles Tjoelker static void
parseredir(char * out,int c)10263f9b4e9aSJilles Tjoelker parseredir(char *out, int c)
10273f9b4e9aSJilles Tjoelker {
10283f9b4e9aSJilles Tjoelker 	char fd = *out;
10293f9b4e9aSJilles Tjoelker 	union node *np;
10303f9b4e9aSJilles Tjoelker 
10313f9b4e9aSJilles Tjoelker 	np = (union node *)stalloc(sizeof (struct nfile));
10323f9b4e9aSJilles Tjoelker 	if (c == '>') {
10333f9b4e9aSJilles Tjoelker 		np->nfile.fd = 1;
10340b4b9c81SJilles Tjoelker 		c = pgetc_linecont();
10353f9b4e9aSJilles Tjoelker 		if (c == '>')
10363f9b4e9aSJilles Tjoelker 			np->type = NAPPEND;
10373f9b4e9aSJilles Tjoelker 		else if (c == '&')
10383f9b4e9aSJilles Tjoelker 			np->type = NTOFD;
10393f9b4e9aSJilles Tjoelker 		else if (c == '|')
10403f9b4e9aSJilles Tjoelker 			np->type = NCLOBBER;
10413f9b4e9aSJilles Tjoelker 		else {
10423f9b4e9aSJilles Tjoelker 			np->type = NTO;
10433f9b4e9aSJilles Tjoelker 			pungetc();
10443f9b4e9aSJilles Tjoelker 		}
10453f9b4e9aSJilles Tjoelker 	} else {	/* c == '<' */
10463f9b4e9aSJilles Tjoelker 		np->nfile.fd = 0;
10470b4b9c81SJilles Tjoelker 		c = pgetc_linecont();
10483f9b4e9aSJilles Tjoelker 		if (c == '<') {
10493f9b4e9aSJilles Tjoelker 			if (sizeof (struct nfile) != sizeof (struct nhere)) {
10503f9b4e9aSJilles Tjoelker 				np = (union node *)stalloc(sizeof (struct nhere));
10513f9b4e9aSJilles Tjoelker 				np->nfile.fd = 0;
10523f9b4e9aSJilles Tjoelker 			}
10533f9b4e9aSJilles Tjoelker 			np->type = NHERE;
10543f9b4e9aSJilles Tjoelker 			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
10553f9b4e9aSJilles Tjoelker 			heredoc->here = np;
10560b4b9c81SJilles Tjoelker 			if ((c = pgetc_linecont()) == '-') {
10573f9b4e9aSJilles Tjoelker 				heredoc->striptabs = 1;
10583f9b4e9aSJilles Tjoelker 			} else {
10593f9b4e9aSJilles Tjoelker 				heredoc->striptabs = 0;
10603f9b4e9aSJilles Tjoelker 				pungetc();
10613f9b4e9aSJilles Tjoelker 			}
10623f9b4e9aSJilles Tjoelker 		} else if (c == '&')
10633f9b4e9aSJilles Tjoelker 			np->type = NFROMFD;
10643f9b4e9aSJilles Tjoelker 		else if (c == '>')
10653f9b4e9aSJilles Tjoelker 			np->type = NFROMTO;
10663f9b4e9aSJilles Tjoelker 		else {
10673f9b4e9aSJilles Tjoelker 			np->type = NFROM;
10683f9b4e9aSJilles Tjoelker 			pungetc();
10693f9b4e9aSJilles Tjoelker 		}
10703f9b4e9aSJilles Tjoelker 	}
10713f9b4e9aSJilles Tjoelker 	if (fd != '\0')
10723f9b4e9aSJilles Tjoelker 		np->nfile.fd = digit_val(fd);
10733f9b4e9aSJilles Tjoelker 	redirnode = np;
10743f9b4e9aSJilles Tjoelker }
10753f9b4e9aSJilles Tjoelker 
10763f9b4e9aSJilles Tjoelker /*
1077cab84206SJilles Tjoelker  * Called to parse command substitutions.
1078cab84206SJilles Tjoelker  */
1079cab84206SJilles Tjoelker 
108088328642SDavid E. O'Brien static char *
parsebackq(char * out,struct nodelist ** pbqlist,int oldstyle,int dblquote,int quoted)1081cab84206SJilles Tjoelker parsebackq(char *out, struct nodelist **pbqlist,
1082cab84206SJilles Tjoelker 		int oldstyle, int dblquote, int quoted)
1083cab84206SJilles Tjoelker {
1084cab84206SJilles Tjoelker 	struct nodelist **nlpp;
1085cab84206SJilles Tjoelker 	union node *n;
1086cab84206SJilles Tjoelker 	char *volatile str;
1087cab84206SJilles Tjoelker 	struct jmploc jmploc;
1088cab84206SJilles Tjoelker 	struct jmploc *const savehandler = handler;
108946c6b52dSJilles Tjoelker 	size_t savelen;
1090cab84206SJilles Tjoelker 	int saveprompt;
1091cab84206SJilles Tjoelker 	const int bq_startlinno = plinno;
1092cab84206SJilles Tjoelker 	char *volatile ostr = NULL;
1093cab84206SJilles Tjoelker 	struct parsefile *const savetopfile = getcurrentfile();
1094ba02a307SJilles Tjoelker 	struct heredoc *const saveheredoclist = heredoclist;
1095ba02a307SJilles Tjoelker 	struct heredoc *here;
1096cab84206SJilles Tjoelker 
1097cab84206SJilles Tjoelker 	str = NULL;
1098cab84206SJilles Tjoelker 	if (setjmp(jmploc.loc)) {
1099cab84206SJilles Tjoelker 		popfilesupto(savetopfile);
1100cab84206SJilles Tjoelker 		if (str)
1101cab84206SJilles Tjoelker 			ckfree(str);
1102cab84206SJilles Tjoelker 		if (ostr)
1103cab84206SJilles Tjoelker 			ckfree(ostr);
1104ba02a307SJilles Tjoelker 		heredoclist = saveheredoclist;
1105cab84206SJilles Tjoelker 		handler = savehandler;
1106cab84206SJilles Tjoelker 		if (exception == EXERROR) {
1107cab84206SJilles Tjoelker 			startlinno = bq_startlinno;
1108cab84206SJilles Tjoelker 			synerror("Error in command substitution");
1109cab84206SJilles Tjoelker 		}
1110cab84206SJilles Tjoelker 		longjmp(handler->loc, 1);
1111cab84206SJilles Tjoelker 	}
1112cab84206SJilles Tjoelker 	INTOFF;
1113cab84206SJilles Tjoelker 	savelen = out - stackblock();
1114cab84206SJilles Tjoelker 	if (savelen > 0) {
1115cab84206SJilles Tjoelker 		str = ckmalloc(savelen);
1116cab84206SJilles Tjoelker 		memcpy(str, stackblock(), savelen);
1117cab84206SJilles Tjoelker 	}
1118cab84206SJilles Tjoelker 	handler = &jmploc;
1119ba02a307SJilles Tjoelker 	heredoclist = NULL;
1120cab84206SJilles Tjoelker 	INTON;
1121cab84206SJilles Tjoelker         if (oldstyle) {
1122cab84206SJilles Tjoelker                 /* We must read until the closing backquote, giving special
1123cab84206SJilles Tjoelker                    treatment to some slashes, and then push the string and
1124cab84206SJilles Tjoelker                    reread it as input, interpreting it normally.  */
1125cab84206SJilles Tjoelker                 char *oout;
1126cab84206SJilles Tjoelker                 int c;
1127cab84206SJilles Tjoelker                 int olen;
1128cab84206SJilles Tjoelker 
1129cab84206SJilles Tjoelker 
1130cab84206SJilles Tjoelker                 STARTSTACKSTR(oout);
1131cab84206SJilles Tjoelker 		for (;;) {
1132cab84206SJilles Tjoelker 			if (needprompt) {
1133cab84206SJilles Tjoelker 				setprompt(2);
1134cab84206SJilles Tjoelker 				needprompt = 0;
1135cab84206SJilles Tjoelker 			}
11369d37e157SJilles Tjoelker 			CHECKSTRSPACE(2, oout);
11370b4b9c81SJilles Tjoelker 			c = pgetc_linecont();
1138622fdf32SJilles Tjoelker 			if (c == '`')
1139622fdf32SJilles Tjoelker 				break;
1140622fdf32SJilles Tjoelker 			switch (c) {
1141cab84206SJilles Tjoelker 			case '\\':
11420b4b9c81SJilles Tjoelker 				c = pgetc();
1143cab84206SJilles Tjoelker                                 if (c != '\\' && c != '`' && c != '$'
1144cab84206SJilles Tjoelker                                     && (!dblquote || c != '"'))
11459d37e157SJilles Tjoelker                                         USTPUTC('\\', oout);
1146cab84206SJilles Tjoelker 				break;
1147cab84206SJilles Tjoelker 
1148cab84206SJilles Tjoelker 			case '\n':
1149cab84206SJilles Tjoelker 				plinno++;
1150cab84206SJilles Tjoelker 				needprompt = doprompt;
1151cab84206SJilles Tjoelker 				break;
1152cab84206SJilles Tjoelker 
1153cab84206SJilles Tjoelker 			case PEOF:
1154cab84206SJilles Tjoelker 			        startlinno = plinno;
1155cab84206SJilles Tjoelker 				synerror("EOF in backquote substitution");
1156cab84206SJilles Tjoelker  				break;
1157cab84206SJilles Tjoelker 
1158cab84206SJilles Tjoelker 			default:
1159cab84206SJilles Tjoelker 				break;
1160cab84206SJilles Tjoelker 			}
11619d37e157SJilles Tjoelker 			USTPUTC(c, oout);
1162cab84206SJilles Tjoelker                 }
11639d37e157SJilles Tjoelker                 USTPUTC('\0', oout);
1164cab84206SJilles Tjoelker                 olen = oout - stackblock();
1165cab84206SJilles Tjoelker 		INTOFF;
1166cab84206SJilles Tjoelker 		ostr = ckmalloc(olen);
1167cab84206SJilles Tjoelker 		memcpy(ostr, stackblock(), olen);
1168cab84206SJilles Tjoelker 		setinputstring(ostr, 1);
1169cab84206SJilles Tjoelker 		INTON;
1170cab84206SJilles Tjoelker         }
1171cab84206SJilles Tjoelker 	nlpp = pbqlist;
1172cab84206SJilles Tjoelker 	while (*nlpp)
1173cab84206SJilles Tjoelker 		nlpp = &(*nlpp)->next;
1174cab84206SJilles Tjoelker 	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1175cab84206SJilles Tjoelker 	(*nlpp)->next = NULL;
1176cab84206SJilles Tjoelker 
1177cab84206SJilles Tjoelker 	if (oldstyle) {
1178cab84206SJilles Tjoelker 		saveprompt = doprompt;
1179cab84206SJilles Tjoelker 		doprompt = 0;
1180cab84206SJilles Tjoelker 	}
1181cab84206SJilles Tjoelker 
118232187151SJilles Tjoelker 	n = list(0);
1183cab84206SJilles Tjoelker 
118432187151SJilles Tjoelker 	if (oldstyle) {
118532187151SJilles Tjoelker 		if (peektoken() != TEOF)
118632187151SJilles Tjoelker 			synexpect(-1);
1187cab84206SJilles Tjoelker 		doprompt = saveprompt;
118832187151SJilles Tjoelker 	} else
11896ab99f87SJilles Tjoelker 		consumetoken(TRP);
1190cab84206SJilles Tjoelker 
1191cab84206SJilles Tjoelker 	(*nlpp)->n = n;
1192cab84206SJilles Tjoelker         if (oldstyle) {
1193cab84206SJilles Tjoelker 		/*
1194cab84206SJilles Tjoelker 		 * Start reading from old file again, ignoring any pushed back
1195cab84206SJilles Tjoelker 		 * tokens left from the backquote parsing
1196cab84206SJilles Tjoelker 		 */
1197cab84206SJilles Tjoelker                 popfile();
1198cab84206SJilles Tjoelker 		tokpushback = 0;
1199cab84206SJilles Tjoelker 	}
1200cab84206SJilles Tjoelker 	STARTSTACKSTR(out);
1201d8f32e72SJilles Tjoelker 	CHECKSTRSPACE(savelen + 1, out);
1202ba02a307SJilles Tjoelker 	INTOFF;
1203cab84206SJilles Tjoelker 	if (str) {
1204cab84206SJilles Tjoelker 		memcpy(out, str, savelen);
1205cab84206SJilles Tjoelker 		STADJUST(savelen, out);
1206cab84206SJilles Tjoelker 		ckfree(str);
1207cab84206SJilles Tjoelker 		str = NULL;
1208cab84206SJilles Tjoelker 	}
1209cab84206SJilles Tjoelker 	if (ostr) {
1210cab84206SJilles Tjoelker 		ckfree(ostr);
1211cab84206SJilles Tjoelker 		ostr = NULL;
1212ba02a307SJilles Tjoelker 	}
1213ba02a307SJilles Tjoelker 	here = saveheredoclist;
1214ba02a307SJilles Tjoelker 	if (here != NULL) {
1215ba02a307SJilles Tjoelker 		while (here->next != NULL)
1216ba02a307SJilles Tjoelker 			here = here->next;
1217ba02a307SJilles Tjoelker 		here->next = heredoclist;
1218ba02a307SJilles Tjoelker 		heredoclist = saveheredoclist;
1219cab84206SJilles Tjoelker 	}
1220cab84206SJilles Tjoelker 	handler = savehandler;
1221ba02a307SJilles Tjoelker 	INTON;
1222cab84206SJilles Tjoelker 	if (quoted)
1223cab84206SJilles Tjoelker 		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1224cab84206SJilles Tjoelker 	else
1225cab84206SJilles Tjoelker 		USTPUTC(CTLBACKQ, out);
1226cab84206SJilles Tjoelker 	return out;
1227cab84206SJilles Tjoelker }
1228cab84206SJilles Tjoelker 
12294b88c807SRodney W. Grimes 
12304b88c807SRodney W. Grimes /*
1231a62ab027SJilles Tjoelker  * Called to parse a backslash escape sequence inside $'...'.
1232a62ab027SJilles Tjoelker  * The backslash has already been read.
1233a62ab027SJilles Tjoelker  */
1234a62ab027SJilles Tjoelker static char *
readcstyleesc(char * out)1235a62ab027SJilles Tjoelker readcstyleesc(char *out)
1236a62ab027SJilles Tjoelker {
12375e03b81fSJilles Tjoelker 	int c, vc, i, n;
12385e03b81fSJilles Tjoelker 	unsigned int v;
1239a62ab027SJilles Tjoelker 
1240a62ab027SJilles Tjoelker 	c = pgetc();
1241a62ab027SJilles Tjoelker 	switch (c) {
1242a62ab027SJilles Tjoelker 	case '\0':
1243a62ab027SJilles Tjoelker 		synerror("Unterminated quoted string");
1244a62ab027SJilles Tjoelker 	case '\n':
1245a62ab027SJilles Tjoelker 		plinno++;
1246a62ab027SJilles Tjoelker 		if (doprompt)
1247a62ab027SJilles Tjoelker 			setprompt(2);
1248a62ab027SJilles Tjoelker 		else
1249a62ab027SJilles Tjoelker 			setprompt(0);
1250a62ab027SJilles Tjoelker 		return out;
1251a62ab027SJilles Tjoelker 	case '\\':
1252a62ab027SJilles Tjoelker 	case '\'':
1253a62ab027SJilles Tjoelker 	case '"':
1254a62ab027SJilles Tjoelker 		v = c;
1255a62ab027SJilles Tjoelker 		break;
1256a62ab027SJilles Tjoelker 	case 'a': v = '\a'; break;
1257a62ab027SJilles Tjoelker 	case 'b': v = '\b'; break;
1258a62ab027SJilles Tjoelker 	case 'e': v = '\033'; break;
1259a62ab027SJilles Tjoelker 	case 'f': v = '\f'; break;
1260a62ab027SJilles Tjoelker 	case 'n': v = '\n'; break;
1261a62ab027SJilles Tjoelker 	case 'r': v = '\r'; break;
1262a62ab027SJilles Tjoelker 	case 't': v = '\t'; break;
1263a62ab027SJilles Tjoelker 	case 'v': v = '\v'; break;
1264a62ab027SJilles Tjoelker 	case 'x':
1265a62ab027SJilles Tjoelker 		  v = 0;
1266a62ab027SJilles Tjoelker 		  for (;;) {
1267a62ab027SJilles Tjoelker 			  c = pgetc();
1268a62ab027SJilles Tjoelker 			  if (c >= '0' && c <= '9')
1269a62ab027SJilles Tjoelker 				  v = (v << 4) + c - '0';
1270a62ab027SJilles Tjoelker 			  else if (c >= 'A' && c <= 'F')
1271a62ab027SJilles Tjoelker 				  v = (v << 4) + c - 'A' + 10;
1272a62ab027SJilles Tjoelker 			  else if (c >= 'a' && c <= 'f')
1273a62ab027SJilles Tjoelker 				  v = (v << 4) + c - 'a' + 10;
1274a62ab027SJilles Tjoelker 			  else
1275a62ab027SJilles Tjoelker 				  break;
1276a62ab027SJilles Tjoelker 		  }
1277a62ab027SJilles Tjoelker 		  pungetc();
1278a62ab027SJilles Tjoelker 		  break;
1279a62ab027SJilles Tjoelker 	case '0': case '1': case '2': case '3':
1280a62ab027SJilles Tjoelker 	case '4': case '5': case '6': case '7':
1281a62ab027SJilles Tjoelker 		  v = c - '0';
1282a62ab027SJilles Tjoelker 		  c = pgetc();
1283a62ab027SJilles Tjoelker 		  if (c >= '0' && c <= '7') {
1284a62ab027SJilles Tjoelker 			  v <<= 3;
1285a62ab027SJilles Tjoelker 			  v += c - '0';
1286a62ab027SJilles Tjoelker 			  c = pgetc();
1287a62ab027SJilles Tjoelker 			  if (c >= '0' && c <= '7') {
1288a62ab027SJilles Tjoelker 				  v <<= 3;
1289a62ab027SJilles Tjoelker 				  v += c - '0';
1290a62ab027SJilles Tjoelker 			  } else
1291a62ab027SJilles Tjoelker 				  pungetc();
1292a62ab027SJilles Tjoelker 		  } else
1293a62ab027SJilles Tjoelker 			  pungetc();
1294a62ab027SJilles Tjoelker 		  break;
1295a62ab027SJilles Tjoelker 	case 'c':
1296a62ab027SJilles Tjoelker 		  c = pgetc();
1297a62ab027SJilles Tjoelker 		  if (c < 0x3f || c > 0x7a || c == 0x60)
1298a62ab027SJilles Tjoelker 			  synerror("Bad escape sequence");
1299a62ab027SJilles Tjoelker 		  if (c == '\\' && pgetc() != '\\')
1300a62ab027SJilles Tjoelker 			  synerror("Bad escape sequence");
1301a62ab027SJilles Tjoelker 		  if (c == '?')
1302a62ab027SJilles Tjoelker 			  v = 127;
1303a62ab027SJilles Tjoelker 		  else
1304a62ab027SJilles Tjoelker 			  v = c & 0x1f;
1305a62ab027SJilles Tjoelker 		  break;
1306a62ab027SJilles Tjoelker 	case 'u':
1307a62ab027SJilles Tjoelker 	case 'U':
1308a62ab027SJilles Tjoelker 		  n = c == 'U' ? 8 : 4;
1309a62ab027SJilles Tjoelker 		  v = 0;
1310a62ab027SJilles Tjoelker 		  for (i = 0; i < n; i++) {
1311a62ab027SJilles Tjoelker 			  c = pgetc();
1312a62ab027SJilles Tjoelker 			  if (c >= '0' && c <= '9')
1313a62ab027SJilles Tjoelker 				  v = (v << 4) + c - '0';
1314a62ab027SJilles Tjoelker 			  else if (c >= 'A' && c <= 'F')
1315a62ab027SJilles Tjoelker 				  v = (v << 4) + c - 'A' + 10;
1316a62ab027SJilles Tjoelker 			  else if (c >= 'a' && c <= 'f')
1317a62ab027SJilles Tjoelker 				  v = (v << 4) + c - 'a' + 10;
1318a62ab027SJilles Tjoelker 			  else
1319a62ab027SJilles Tjoelker 				  synerror("Bad escape sequence");
1320a62ab027SJilles Tjoelker 		  }
1321a62ab027SJilles Tjoelker 		  if (v == 0 || (v >= 0xd800 && v <= 0xdfff))
1322a62ab027SJilles Tjoelker 			  synerror("Bad escape sequence");
1323a62ab027SJilles Tjoelker 		  /* We really need iconv here. */
132407eb7033SJilles Tjoelker 		  if (initial_localeisutf8 && v > 127) {
132507eb7033SJilles Tjoelker 			  CHECKSTRSPACE(4, out);
132607eb7033SJilles Tjoelker 			  /*
132707eb7033SJilles Tjoelker 			   * We cannot use wctomb() as the locale may have
132807eb7033SJilles Tjoelker 			   * changed.
132907eb7033SJilles Tjoelker 			   */
133007eb7033SJilles Tjoelker 			  if (v <= 0x7ff) {
133107eb7033SJilles Tjoelker 				  USTPUTC(0xc0 | v >> 6, out);
133207eb7033SJilles Tjoelker 				  USTPUTC(0x80 | (v & 0x3f), out);
133307eb7033SJilles Tjoelker 				  return out;
133407eb7033SJilles Tjoelker 			  } else if (v <= 0xffff) {
133507eb7033SJilles Tjoelker 				  USTPUTC(0xe0 | v >> 12, out);
133607eb7033SJilles Tjoelker 				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
133707eb7033SJilles Tjoelker 				  USTPUTC(0x80 | (v & 0x3f), out);
133807eb7033SJilles Tjoelker 				  return out;
133907eb7033SJilles Tjoelker 			  } else if (v <= 0x10ffff) {
134007eb7033SJilles Tjoelker 				  USTPUTC(0xf0 | v >> 18, out);
134107eb7033SJilles Tjoelker 				  USTPUTC(0x80 | ((v >> 12) & 0x3f), out);
134207eb7033SJilles Tjoelker 				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
134307eb7033SJilles Tjoelker 				  USTPUTC(0x80 | (v & 0x3f), out);
134407eb7033SJilles Tjoelker 				  return out;
134507eb7033SJilles Tjoelker 			  }
134607eb7033SJilles Tjoelker 		  }
1347a62ab027SJilles Tjoelker 		  if (v > 127)
1348a62ab027SJilles Tjoelker 			  v = '?';
1349a62ab027SJilles Tjoelker 		  break;
1350a62ab027SJilles Tjoelker 	default:
1351a62ab027SJilles Tjoelker 		  synerror("Bad escape sequence");
1352a62ab027SJilles Tjoelker 	}
13535e03b81fSJilles Tjoelker 	vc = (char)v;
1354a62ab027SJilles Tjoelker 	/*
1355a62ab027SJilles Tjoelker 	 * We can't handle NUL bytes.
1356a62ab027SJilles Tjoelker 	 * POSIX says we should skip till the closing quote.
1357a62ab027SJilles Tjoelker 	 */
13585e03b81fSJilles Tjoelker 	if (vc == '\0') {
1359a62ab027SJilles Tjoelker 		while ((c = pgetc()) != '\'') {
1360a62ab027SJilles Tjoelker 			if (c == '\\')
1361a62ab027SJilles Tjoelker 				c = pgetc();
1362a62ab027SJilles Tjoelker 			if (c == PEOF)
1363a62ab027SJilles Tjoelker 				synerror("Unterminated quoted string");
1364068dfa2dSJilles Tjoelker 			if (c == '\n') {
1365068dfa2dSJilles Tjoelker 				plinno++;
1366068dfa2dSJilles Tjoelker 				if (doprompt)
1367068dfa2dSJilles Tjoelker 					setprompt(2);
1368068dfa2dSJilles Tjoelker 				else
1369068dfa2dSJilles Tjoelker 					setprompt(0);
1370068dfa2dSJilles Tjoelker 			}
1371a62ab027SJilles Tjoelker 		}
1372a62ab027SJilles Tjoelker 		pungetc();
1373a62ab027SJilles Tjoelker 		return out;
1374a62ab027SJilles Tjoelker 	}
13755e03b81fSJilles Tjoelker 	if (SQSYNTAX[vc] == CCTL)
1376a62ab027SJilles Tjoelker 		USTPUTC(CTLESC, out);
13775e03b81fSJilles Tjoelker 	USTPUTC(vc, out);
1378a62ab027SJilles Tjoelker 	return out;
1379a62ab027SJilles Tjoelker }
1380a62ab027SJilles Tjoelker 
1381a62ab027SJilles Tjoelker 
1382a62ab027SJilles Tjoelker /*
13834b88c807SRodney W. Grimes  * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
13844b88c807SRodney W. Grimes  * is not NULL, read a here document.  In the latter case, eofmark is the
13854b88c807SRodney W. Grimes  * word which marks the end of the document and striptabs is true if
13864b88c807SRodney W. Grimes  * leading tabs should be stripped from the document.  The argument firstc
13874b88c807SRodney W. Grimes  * is the first character of the input token or document.
13884b88c807SRodney W. Grimes  *
13894b88c807SRodney W. Grimes  * Because C does not have internal subroutines, I have simulated them
13904b88c807SRodney W. Grimes  * using goto's to implement the subroutine linkage.  The following macros
13914b88c807SRodney W. Grimes  * will run code that appears at the end of readtoken1.
13924b88c807SRodney W. Grimes  */
13934b88c807SRodney W. Grimes 
13944b88c807SRodney W. Grimes #define PARSESUB()	{goto parsesub; parsesub_return:;}
13954b88c807SRodney W. Grimes #define	PARSEARITH()	{goto parsearith; parsearith_return:;}
13964b88c807SRodney W. Grimes 
139788328642SDavid E. O'Brien static int
readtoken1(int firstc,char const * initialsyntax,const char * eofmark,int striptabs)139846c6b52dSJilles Tjoelker readtoken1(int firstc, char const *initialsyntax, const char *eofmark,
139946c6b52dSJilles Tjoelker     int striptabs)
14004b88c807SRodney W. Grimes {
1401aa9caaf6SPeter Wemm 	int c = firstc;
1402aa9caaf6SPeter Wemm 	char *out;
14034b88c807SRodney W. Grimes 	int len;
14044b88c807SRodney W. Grimes 	struct nodelist *bqlist;
14054b88c807SRodney W. Grimes 	int quotef;
14068cf06f5eSJilles Tjoelker 	int newvarnest;
14078cf06f5eSJilles Tjoelker 	int level;
14082dde9ce3SMartin Cracauer 	int synentry;
140988328642SDavid E. O'Brien 	struct tokenstate state_static[MAXNEST_static];
141088328642SDavid E. O'Brien 	int maxnest = MAXNEST_static;
14118cf06f5eSJilles Tjoelker 	struct tokenstate *state = state_static;
1412a62ab027SJilles Tjoelker 	int sqiscstyle = 0;
14134b88c807SRodney W. Grimes 
14144b88c807SRodney W. Grimes 	startlinno = plinno;
14154b88c807SRodney W. Grimes 	quotef = 0;
14164b88c807SRodney W. Grimes 	bqlist = NULL;
14178cf06f5eSJilles Tjoelker 	newvarnest = 0;
14188cf06f5eSJilles Tjoelker 	level = 0;
14198cf06f5eSJilles Tjoelker 	state[level].syntax = initialsyntax;
14208cf06f5eSJilles Tjoelker 	state[level].parenlevel = 0;
14218cf06f5eSJilles Tjoelker 	state[level].category = TSTATE_TOP;
14224b88c807SRodney W. Grimes 
14234b88c807SRodney W. Grimes 	STARTSTACKSTR(out);
14244b88c807SRodney W. Grimes 	loop: {	/* for each line, until end of word */
142592fe71faSJilles Tjoelker 		if (eofmark && eofmark != NOEOFMARK)
1426671a890eSJilles Tjoelker 			/* set c to PEOF if at end of here document */
142772238faaSJilles Tjoelker 			c = checkend(c, eofmark, striptabs);
14284b88c807SRodney W. Grimes 		for (;;) {	/* until end of line or end of word */
1429048f2667SJilles Tjoelker 			CHECKSTRSPACE(4, out);	/* permit 4 calls to USTPUTC */
14302dde9ce3SMartin Cracauer 
14318cf06f5eSJilles Tjoelker 			synentry = state[level].syntax[c];
14322dde9ce3SMartin Cracauer 
14332dde9ce3SMartin Cracauer 			switch(synentry) {
14344b88c807SRodney W. Grimes 			case CNL:	/* '\n' */
1435dc0dbd74SJilles Tjoelker 				if (level == 0)
14364b88c807SRodney W. Grimes 					goto endword;	/* exit outer loop */
1437dc0dbd74SJilles Tjoelker 				/* FALLTHROUGH */
1438dc0dbd74SJilles Tjoelker 			case CQNL:
14394b88c807SRodney W. Grimes 				USTPUTC(c, out);
14404b88c807SRodney W. Grimes 				plinno++;
14414b88c807SRodney W. Grimes 				if (doprompt)
14424b88c807SRodney W. Grimes 					setprompt(2);
14434b88c807SRodney W. Grimes 				else
14444b88c807SRodney W. Grimes 					setprompt(0);
14454b88c807SRodney W. Grimes 				c = pgetc();
14464b88c807SRodney W. Grimes 				goto loop;		/* continue outer loop */
1447a62ab027SJilles Tjoelker 			case CSBACK:
1448a62ab027SJilles Tjoelker 				if (sqiscstyle) {
1449a62ab027SJilles Tjoelker 					out = readcstyleesc(out);
1450a62ab027SJilles Tjoelker 					break;
1451a62ab027SJilles Tjoelker 				}
1452a62ab027SJilles Tjoelker 				/* FALLTHROUGH */
14534b88c807SRodney W. Grimes 			case CWORD:
14544b88c807SRodney W. Grimes 				USTPUTC(c, out);
14554b88c807SRodney W. Grimes 				break;
14564b88c807SRodney W. Grimes 			case CCTL:
14578cf06f5eSJilles Tjoelker 				if (eofmark == NULL || initialsyntax != SQSYNTAX)
14584b88c807SRodney W. Grimes 					USTPUTC(CTLESC, out);
14594b88c807SRodney W. Grimes 				USTPUTC(c, out);
14604b88c807SRodney W. Grimes 				break;
14614b88c807SRodney W. Grimes 			case CBACK:	/* backslash */
14624b88c807SRodney W. Grimes 				c = pgetc();
14634b88c807SRodney W. Grimes 				if (c == PEOF) {
14644b88c807SRodney W. Grimes 					USTPUTC('\\', out);
14654b88c807SRodney W. Grimes 					pungetc();
14664b88c807SRodney W. Grimes 				} else if (c == '\n') {
146762f9f953SYaroslav Tykhiy 					plinno++;
14684b88c807SRodney W. Grimes 					if (doprompt)
14694b88c807SRodney W. Grimes 						setprompt(2);
14704b88c807SRodney W. Grimes 					else
14714b88c807SRodney W. Grimes 						setprompt(0);
14724b88c807SRodney W. Grimes 				} else {
14738cf06f5eSJilles Tjoelker 					if (state[level].syntax == DQSYNTAX &&
14748cf06f5eSJilles Tjoelker 					    c != '\\' && c != '`' && c != '$' &&
14758cf06f5eSJilles Tjoelker 					    (c != '"' || (eofmark != NULL &&
14768cf06f5eSJilles Tjoelker 						newvarnest == 0)) &&
14778cf06f5eSJilles Tjoelker 					    (c != '}' || state[level].category != TSTATE_VAR_OLD))
14784b88c807SRodney W. Grimes 						USTPUTC('\\', out);
1479048f2667SJilles Tjoelker 					if ((eofmark == NULL ||
1480048f2667SJilles Tjoelker 					    newvarnest > 0) &&
1481048f2667SJilles Tjoelker 					    state[level].syntax == BASESYNTAX)
1482048f2667SJilles Tjoelker 						USTPUTC(CTLQUOTEMARK, out);
14830c4eeddaSTor Egge 					if (SQSYNTAX[c] == CCTL)
14844b88c807SRodney W. Grimes 						USTPUTC(CTLESC, out);
14854b88c807SRodney W. Grimes 					USTPUTC(c, out);
1486048f2667SJilles Tjoelker 					if ((eofmark == NULL ||
1487048f2667SJilles Tjoelker 					    newvarnest > 0) &&
1488048f2667SJilles Tjoelker 					    state[level].syntax == BASESYNTAX &&
1489048f2667SJilles Tjoelker 					    state[level].category == TSTATE_VAR_OLD)
1490048f2667SJilles Tjoelker 						USTPUTC(CTLQUOTEEND, out);
14914b88c807SRodney W. Grimes 					quotef++;
14924b88c807SRodney W. Grimes 				}
14934b88c807SRodney W. Grimes 				break;
14944b88c807SRodney W. Grimes 			case CSQUOTE:
14956f47734fSTor Egge 				USTPUTC(CTLQUOTEMARK, out);
14968cf06f5eSJilles Tjoelker 				state[level].syntax = SQSYNTAX;
1497a62ab027SJilles Tjoelker 				sqiscstyle = 0;
14984b88c807SRodney W. Grimes 				break;
14994b88c807SRodney W. Grimes 			case CDQUOTE:
15006f47734fSTor Egge 				USTPUTC(CTLQUOTEMARK, out);
15018cf06f5eSJilles Tjoelker 				state[level].syntax = DQSYNTAX;
15024b88c807SRodney W. Grimes 				break;
15034b88c807SRodney W. Grimes 			case CENDQUOTE:
15048cf06f5eSJilles Tjoelker 				if (eofmark != NULL && newvarnest == 0)
15054b88c807SRodney W. Grimes 					USTPUTC(c, out);
15068cf06f5eSJilles Tjoelker 				else {
1507048f2667SJilles Tjoelker 					if (state[level].category == TSTATE_VAR_OLD)
1508048f2667SJilles Tjoelker 						USTPUTC(CTLQUOTEEND, out);
15098cf06f5eSJilles Tjoelker 					state[level].syntax = BASESYNTAX;
15105557a02aSTor Egge 					quotef++;
15114b88c807SRodney W. Grimes 				}
15124b88c807SRodney W. Grimes 				break;
15134b88c807SRodney W. Grimes 			case CVAR:	/* '$' */
15144b88c807SRodney W. Grimes 				PARSESUB();		/* parse substitution */
15154b88c807SRodney W. Grimes 				break;
15164b88c807SRodney W. Grimes 			case CENDVAR:	/* '}' */
15178cf06f5eSJilles Tjoelker 				if (level > 0 &&
15186c380712SJilles Tjoelker 				    ((state[level].category == TSTATE_VAR_OLD &&
15196c380712SJilles Tjoelker 				      state[level].syntax ==
15206c380712SJilles Tjoelker 				      state[level - 1].syntax) ||
15219cec947fSJilles Tjoelker 				    (state[level].category == TSTATE_VAR_NEW &&
15229cec947fSJilles Tjoelker 				     state[level].syntax == BASESYNTAX))) {
15236c380712SJilles Tjoelker 					if (state[level].category == TSTATE_VAR_NEW)
15248cf06f5eSJilles Tjoelker 						newvarnest--;
15258cf06f5eSJilles Tjoelker 					level--;
15264b88c807SRodney W. Grimes 					USTPUTC(CTLENDVAR, out);
15274b88c807SRodney W. Grimes 				} else {
15284b88c807SRodney W. Grimes 					USTPUTC(c, out);
15294b88c807SRodney W. Grimes 				}
15304b88c807SRodney W. Grimes 				break;
15314b88c807SRodney W. Grimes 			case CLP:	/* '(' in arithmetic */
15328cf06f5eSJilles Tjoelker 				state[level].parenlevel++;
15334b88c807SRodney W. Grimes 				USTPUTC(c, out);
15344b88c807SRodney W. Grimes 				break;
15354b88c807SRodney W. Grimes 			case CRP:	/* ')' in arithmetic */
15368cf06f5eSJilles Tjoelker 				if (state[level].parenlevel > 0) {
15374b88c807SRodney W. Grimes 					USTPUTC(c, out);
15388cf06f5eSJilles Tjoelker 					--state[level].parenlevel;
15394b88c807SRodney W. Grimes 				} else {
15400b4b9c81SJilles Tjoelker 					if (pgetc_linecont() == ')') {
15418cf06f5eSJilles Tjoelker 						if (level > 0 &&
15428cf06f5eSJilles Tjoelker 						    state[level].category == TSTATE_ARITH) {
15438cf06f5eSJilles Tjoelker 							level--;
15444b88c807SRodney W. Grimes 							USTPUTC(CTLENDARI, out);
15454b88c807SRodney W. Grimes 						} else
15464b88c807SRodney W. Grimes 							USTPUTC(')', out);
15474b88c807SRodney W. Grimes 					} else {
15484b88c807SRodney W. Grimes 						/*
15494b88c807SRodney W. Grimes 						 * unbalanced parens
15504b88c807SRodney W. Grimes 						 *  (don't 2nd guess - no error)
15514b88c807SRodney W. Grimes 						 */
15524b88c807SRodney W. Grimes 						pungetc();
15534b88c807SRodney W. Grimes 						USTPUTC(')', out);
15544b88c807SRodney W. Grimes 					}
15554b88c807SRodney W. Grimes 				}
15564b88c807SRodney W. Grimes 				break;
15574b88c807SRodney W. Grimes 			case CBQUOTE:	/* '`' */
15588cf06f5eSJilles Tjoelker 				out = parsebackq(out, &bqlist, 1,
15598cf06f5eSJilles Tjoelker 				    state[level].syntax == DQSYNTAX &&
15608cf06f5eSJilles Tjoelker 				    (eofmark == NULL || newvarnest > 0),
15618cf06f5eSJilles Tjoelker 				    state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
15624b88c807SRodney W. Grimes 				break;
15634b88c807SRodney W. Grimes 			case CEOF:
15644b88c807SRodney W. Grimes 				goto endword;		/* exit outer loop */
1565d94c8673SJilles Tjoelker 			case CIGN:
1566d94c8673SJilles Tjoelker 				break;
15674b88c807SRodney W. Grimes 			default:
15688cf06f5eSJilles Tjoelker 				if (level == 0)
15694b88c807SRodney W. Grimes 					goto endword;	/* exit outer loop */
15704b88c807SRodney W. Grimes 				USTPUTC(c, out);
15714b88c807SRodney W. Grimes 			}
15724b88c807SRodney W. Grimes 			c = pgetc_macro();
15734b88c807SRodney W. Grimes 		}
15744b88c807SRodney W. Grimes 	}
15754b88c807SRodney W. Grimes endword:
15768cf06f5eSJilles Tjoelker 	if (state[level].syntax == ARISYNTAX)
15774b88c807SRodney W. Grimes 		synerror("Missing '))'");
15788cf06f5eSJilles Tjoelker 	if (state[level].syntax != BASESYNTAX && eofmark == NULL)
15794b88c807SRodney W. Grimes 		synerror("Unterminated quoted string");
15808cf06f5eSJilles Tjoelker 	if (state[level].category == TSTATE_VAR_OLD ||
15818cf06f5eSJilles Tjoelker 	    state[level].category == TSTATE_VAR_NEW) {
15824b88c807SRodney W. Grimes 		startlinno = plinno;
15834b88c807SRodney W. Grimes 		synerror("Missing '}'");
15844b88c807SRodney W. Grimes 	}
15858cf06f5eSJilles Tjoelker 	if (state != state_static)
15868cf06f5eSJilles Tjoelker 		parser_temp_free_upto(state);
15874b88c807SRodney W. Grimes 	USTPUTC('\0', out);
15884b88c807SRodney W. Grimes 	len = out - stackblock();
15894b88c807SRodney W. Grimes 	out = stackblock();
15904b88c807SRodney W. Grimes 	if (eofmark == NULL) {
15914b88c807SRodney W. Grimes 		if ((c == '>' || c == '<')
15924b88c807SRodney W. Grimes 		 && quotef == 0
15934b88c807SRodney W. Grimes 		 && len <= 2
15944b88c807SRodney W. Grimes 		 && (*out == '\0' || is_digit(*out))) {
15953f9b4e9aSJilles Tjoelker 			parseredir(out, c);
15964b88c807SRodney W. Grimes 			return lasttoken = TREDIR;
15974b88c807SRodney W. Grimes 		} else {
15984b88c807SRodney W. Grimes 			pungetc();
15994b88c807SRodney W. Grimes 		}
16004b88c807SRodney W. Grimes 	}
16014b88c807SRodney W. Grimes 	quoteflag = quotef;
16024b88c807SRodney W. Grimes 	backquotelist = bqlist;
16034b88c807SRodney W. Grimes 	grabstackblock(len);
16044b88c807SRodney W. Grimes 	wordtext = out;
16054b88c807SRodney W. Grimes 	return lasttoken = TWORD;
16064b88c807SRodney W. Grimes /* end of readtoken routine */
16074b88c807SRodney W. Grimes 
16084b88c807SRodney W. Grimes 
16094b88c807SRodney W. Grimes /*
16104b88c807SRodney W. Grimes  * Parse a substitution.  At this point, we have read the dollar sign
16114b88c807SRodney W. Grimes  * and nothing else.
16124b88c807SRodney W. Grimes  */
16134b88c807SRodney W. Grimes 
16144b88c807SRodney W. Grimes parsesub: {
16154b88c807SRodney W. Grimes 	int subtype;
16164b88c807SRodney W. Grimes 	int typeloc;
16174b88c807SRodney W. Grimes 	int flags;
16184b88c807SRodney W. Grimes 	char *p;
16194b88c807SRodney W. Grimes 	static const char types[] = "}-+?=";
1620b71085aaSStefan Farfeleder 	int linno;
16214f30f299SStefan Farfeleder 	int length;
162235c641edSJilles Tjoelker 	int c1;
16234b88c807SRodney W. Grimes 
16240b4b9c81SJilles Tjoelker 	c = pgetc_linecont();
1625a62ab027SJilles Tjoelker 	if (c == '(') {	/* $(command) or $((arith)) */
16260b4b9c81SJilles Tjoelker 		if (pgetc_linecont() == '(') {
16274b88c807SRodney W. Grimes 			PARSEARITH();
16284b88c807SRodney W. Grimes 		} else {
16294b88c807SRodney W. Grimes 			pungetc();
16308cf06f5eSJilles Tjoelker 			out = parsebackq(out, &bqlist, 0,
16318cf06f5eSJilles Tjoelker 			    state[level].syntax == DQSYNTAX &&
16328cf06f5eSJilles Tjoelker 			    (eofmark == NULL || newvarnest > 0),
16338cf06f5eSJilles Tjoelker 			    state[level].syntax == DQSYNTAX ||
16348cf06f5eSJilles Tjoelker 			    state[level].syntax == ARISYNTAX);
16354b88c807SRodney W. Grimes 		}
1636a62ab027SJilles Tjoelker 	} else if (c == '{' || is_name(c) || is_special(c)) {
16374b88c807SRodney W. Grimes 		USTPUTC(CTLVAR, out);
16384b88c807SRodney W. Grimes 		typeloc = out - stackblock();
16394b88c807SRodney W. Grimes 		USTPUTC(VSNORMAL, out);
16404b88c807SRodney W. Grimes 		subtype = VSNORMAL;
1641b71085aaSStefan Farfeleder 		flags = 0;
16424b88c807SRodney W. Grimes 		if (c == '{') {
16430b4b9c81SJilles Tjoelker 			c = pgetc_linecont();
16444b88c807SRodney W. Grimes 			subtype = 0;
16454b88c807SRodney W. Grimes 		}
164635c641edSJilles Tjoelker varname:
1647716b138bSStefan Farfeleder 		if (!is_eof(c) && is_name(c)) {
16484f30f299SStefan Farfeleder 			length = 0;
16494b88c807SRodney W. Grimes 			do {
16504b88c807SRodney W. Grimes 				STPUTC(c, out);
16510b4b9c81SJilles Tjoelker 				c = pgetc_linecont();
16524f30f299SStefan Farfeleder 				length++;
1653716b138bSStefan Farfeleder 			} while (!is_eof(c) && is_in_name(c));
16544f30f299SStefan Farfeleder 			if (length == 6 &&
16554f30f299SStefan Farfeleder 			    strncmp(out - length, "LINENO", length) == 0) {
1656b71085aaSStefan Farfeleder 				/* Replace the variable name with the
1657b71085aaSStefan Farfeleder 				 * current line number. */
165876963686SJilles Tjoelker 				STADJUST(-6, out);
165976963686SJilles Tjoelker 				CHECKSTRSPACE(11, out);
1660b71085aaSStefan Farfeleder 				linno = plinno;
1661b71085aaSStefan Farfeleder 				if (funclinno != 0)
1662b71085aaSStefan Farfeleder 					linno -= funclinno - 1;
166376963686SJilles Tjoelker 				length = snprintf(out, 11, "%d", linno);
166476963686SJilles Tjoelker 				if (length > 10)
166576963686SJilles Tjoelker 					length = 10;
166676963686SJilles Tjoelker 				out += length;
1667b71085aaSStefan Farfeleder 				flags |= VSLINENO;
1668b71085aaSStefan Farfeleder 			}
16695c817731SPeter Wemm 		} else if (is_digit(c)) {
1670d7250589SJilles Tjoelker 			if (subtype != VSNORMAL) {
16715c817731SPeter Wemm 				do {
16725c817731SPeter Wemm 					STPUTC(c, out);
16730b4b9c81SJilles Tjoelker 					c = pgetc_linecont();
16745c817731SPeter Wemm 				} while (is_digit(c));
16755c817731SPeter Wemm 			} else {
16762efd8c0aSJilles Tjoelker 				USTPUTC(c, out);
16770b4b9c81SJilles Tjoelker 				c = pgetc_linecont();
16785c817731SPeter Wemm 			}
167935c641edSJilles Tjoelker 		} else if (is_special(c)) {
168035c641edSJilles Tjoelker 			c1 = c;
16810b4b9c81SJilles Tjoelker 			c = pgetc_linecont();
168235c641edSJilles Tjoelker 			if (subtype == 0 && c1 == '#') {
168335c641edSJilles Tjoelker 				subtype = VSLENGTH;
168435c641edSJilles Tjoelker 				if (strchr(types, c) == NULL && c != ':' &&
168535c641edSJilles Tjoelker 				    c != '#' && c != '%')
168635c641edSJilles Tjoelker 					goto varname;
168735c641edSJilles Tjoelker 				c1 = c;
16880b4b9c81SJilles Tjoelker 				c = pgetc_linecont();
168935c641edSJilles Tjoelker 				if (c1 != '}' && c == '}') {
169035c641edSJilles Tjoelker 					pungetc();
169135c641edSJilles Tjoelker 					c = c1;
169235c641edSJilles Tjoelker 					goto varname;
169335c641edSJilles Tjoelker 				}
169435c641edSJilles Tjoelker 				pungetc();
169535c641edSJilles Tjoelker 				c = c1;
169635c641edSJilles Tjoelker 				c1 = '#';
169735c641edSJilles Tjoelker 				subtype = 0;
169835c641edSJilles Tjoelker 			}
169935c641edSJilles Tjoelker 			USTPUTC(c1, out);
17004b88c807SRodney W. Grimes 		} else {
170162addaefSStefan Farfeleder 			subtype = VSERROR;
170262addaefSStefan Farfeleder 			if (c == '}')
170362addaefSStefan Farfeleder 				pungetc();
1704d323650fSJilles Tjoelker 			else if (c == '\n' || c == PEOF)
1705d323650fSJilles Tjoelker 				synerror("Unexpected end of line in substitution");
1706d0b0ac18SJilles Tjoelker 			else if (BASESYNTAX[c] != CCTL)
170762addaefSStefan Farfeleder 				USTPUTC(c, out);
170862addaefSStefan Farfeleder 		}
17094b88c807SRodney W. Grimes 		if (subtype == 0) {
1710aa9caaf6SPeter Wemm 			switch (c) {
1711aa9caaf6SPeter Wemm 			case ':':
1712b71085aaSStefan Farfeleder 				flags |= VSNUL;
17130b4b9c81SJilles Tjoelker 				c = pgetc_linecont();
1714aa9caaf6SPeter Wemm 				/*FALLTHROUGH*/
1715aa9caaf6SPeter Wemm 			default:
17164b88c807SRodney W. Grimes 				p = strchr(types, c);
171762addaefSStefan Farfeleder 				if (p == NULL) {
1718d323650fSJilles Tjoelker 					if (c == '\n' || c == PEOF)
1719d323650fSJilles Tjoelker 						synerror("Unexpected end of line in substitution");
172062addaefSStefan Farfeleder 					if (flags == VSNUL)
172162addaefSStefan Farfeleder 						STPUTC(':', out);
1722d0b0ac18SJilles Tjoelker 					if (BASESYNTAX[c] != CCTL)
172362addaefSStefan Farfeleder 						STPUTC(c, out);
172462addaefSStefan Farfeleder 					subtype = VSERROR;
172562addaefSStefan Farfeleder 				} else
17264b88c807SRodney W. Grimes 					subtype = p - types + VSNORMAL;
1727aa9caaf6SPeter Wemm 				break;
1728aa9caaf6SPeter Wemm 			case '%':
1729aa9caaf6SPeter Wemm 			case '#':
1730aa9caaf6SPeter Wemm 				{
1731aa9caaf6SPeter Wemm 					int cc = c;
1732aa9caaf6SPeter Wemm 					subtype = c == '#' ? VSTRIMLEFT :
1733aa9caaf6SPeter Wemm 							     VSTRIMRIGHT;
17340b4b9c81SJilles Tjoelker 					c = pgetc_linecont();
1735aa9caaf6SPeter Wemm 					if (c == cc)
1736aa9caaf6SPeter Wemm 						subtype++;
1737aa9caaf6SPeter Wemm 					else
1738aa9caaf6SPeter Wemm 						pungetc();
1739aa9caaf6SPeter Wemm 					break;
1740aa9caaf6SPeter Wemm 				}
1741aa9caaf6SPeter Wemm 			}
174262addaefSStefan Farfeleder 		} else if (subtype != VSERROR) {
1743fc0818feSJilles Tjoelker 			if (subtype == VSLENGTH && c != '}')
1744fc0818feSJilles Tjoelker 				subtype = VSERROR;
17454b88c807SRodney W. Grimes 			pungetc();
17464b88c807SRodney W. Grimes 		}
174762addaefSStefan Farfeleder 		STPUTC('=', out);
1748caa7ccdcSJilles Tjoelker 		if (state[level].syntax == DQSYNTAX ||
1749caa7ccdcSJilles Tjoelker 		    state[level].syntax == ARISYNTAX)
17504b88c807SRodney W. Grimes 			flags |= VSQUOTE;
17514b88c807SRodney W. Grimes 		*(stackblock() + typeloc) = subtype | flags;
17528cf06f5eSJilles Tjoelker 		if (subtype != VSNORMAL) {
17538cf06f5eSJilles Tjoelker 			if (level + 1 >= maxnest) {
17548cf06f5eSJilles Tjoelker 				maxnest *= 2;
17558cf06f5eSJilles Tjoelker 				if (state == state_static) {
17568cf06f5eSJilles Tjoelker 					state = parser_temp_alloc(
17578cf06f5eSJilles Tjoelker 					    maxnest * sizeof(*state));
17588cf06f5eSJilles Tjoelker 					memcpy(state, state_static,
175988328642SDavid E. O'Brien 					    MAXNEST_static * sizeof(*state));
17608cf06f5eSJilles Tjoelker 				} else
17618cf06f5eSJilles Tjoelker 					state = parser_temp_realloc(state,
17628cf06f5eSJilles Tjoelker 					    maxnest * sizeof(*state));
17638cf06f5eSJilles Tjoelker 			}
17648cf06f5eSJilles Tjoelker 			level++;
17658cf06f5eSJilles Tjoelker 			state[level].parenlevel = 0;
17668cf06f5eSJilles Tjoelker 			if (subtype == VSMINUS || subtype == VSPLUS ||
17678cf06f5eSJilles Tjoelker 			    subtype == VSQUESTION || subtype == VSASSIGN) {
17688cf06f5eSJilles Tjoelker 				/*
17698cf06f5eSJilles Tjoelker 				 * For operators that were in the Bourne shell,
17708cf06f5eSJilles Tjoelker 				 * inherit the double-quote state.
17718cf06f5eSJilles Tjoelker 				 */
17728cf06f5eSJilles Tjoelker 				state[level].syntax = state[level - 1].syntax;
17738cf06f5eSJilles Tjoelker 				state[level].category = TSTATE_VAR_OLD;
17748cf06f5eSJilles Tjoelker 			} else {
17758cf06f5eSJilles Tjoelker 				/*
17768cf06f5eSJilles Tjoelker 				 * The other operators take a pattern,
17778cf06f5eSJilles Tjoelker 				 * so go to BASESYNTAX.
17788cf06f5eSJilles Tjoelker 				 * Also, ' and " are now special, even
17798cf06f5eSJilles Tjoelker 				 * in here documents.
17808cf06f5eSJilles Tjoelker 				 */
17818cf06f5eSJilles Tjoelker 				state[level].syntax = BASESYNTAX;
17828cf06f5eSJilles Tjoelker 				state[level].category = TSTATE_VAR_NEW;
17838cf06f5eSJilles Tjoelker 				newvarnest++;
17848cf06f5eSJilles Tjoelker 			}
17858cf06f5eSJilles Tjoelker 		}
1786a62ab027SJilles Tjoelker 	} else if (c == '\'' && state[level].syntax == BASESYNTAX) {
1787a62ab027SJilles Tjoelker 		/* $'cstylequotes' */
1788a62ab027SJilles Tjoelker 		USTPUTC(CTLQUOTEMARK, out);
1789a62ab027SJilles Tjoelker 		state[level].syntax = SQSYNTAX;
1790a62ab027SJilles Tjoelker 		sqiscstyle = 1;
1791a62ab027SJilles Tjoelker 	} else {
1792a62ab027SJilles Tjoelker 		USTPUTC('$', out);
1793a62ab027SJilles Tjoelker 		pungetc();
17944b88c807SRodney W. Grimes 	}
17954b88c807SRodney W. Grimes 	goto parsesub_return;
17964b88c807SRodney W. Grimes }
17974b88c807SRodney W. Grimes 
17984b88c807SRodney W. Grimes 
17994b88c807SRodney W. Grimes /*
18004b88c807SRodney W. Grimes  * Parse an arithmetic expansion (indicate start of one and set state)
18014b88c807SRodney W. Grimes  */
18024b88c807SRodney W. Grimes parsearith: {
18034b88c807SRodney W. Grimes 
18048cf06f5eSJilles Tjoelker 	if (level + 1 >= maxnest) {
18058cf06f5eSJilles Tjoelker 		maxnest *= 2;
18068cf06f5eSJilles Tjoelker 		if (state == state_static) {
18078cf06f5eSJilles Tjoelker 			state = parser_temp_alloc(
18088cf06f5eSJilles Tjoelker 			    maxnest * sizeof(*state));
18098cf06f5eSJilles Tjoelker 			memcpy(state, state_static,
181088328642SDavid E. O'Brien 			    MAXNEST_static * sizeof(*state));
18118cf06f5eSJilles Tjoelker 		} else
18128cf06f5eSJilles Tjoelker 			state = parser_temp_realloc(state,
18138cf06f5eSJilles Tjoelker 			    maxnest * sizeof(*state));
18148cf06f5eSJilles Tjoelker 	}
18158cf06f5eSJilles Tjoelker 	level++;
18168cf06f5eSJilles Tjoelker 	state[level].syntax = ARISYNTAX;
18178cf06f5eSJilles Tjoelker 	state[level].parenlevel = 0;
18188cf06f5eSJilles Tjoelker 	state[level].category = TSTATE_ARITH;
18194b88c807SRodney W. Grimes 	USTPUTC(CTLARI, out);
18208cf06f5eSJilles Tjoelker 	if (state[level - 1].syntax == DQSYNTAX)
18216f47734fSTor Egge 		USTPUTC('"',out);
18226f47734fSTor Egge 	else
18236f47734fSTor Egge 		USTPUTC(' ',out);
18244b88c807SRodney W. Grimes 	goto parsearith_return;
18254b88c807SRodney W. Grimes }
18264b88c807SRodney W. Grimes 
18274b88c807SRodney W. Grimes } /* end of readtoken */
18284b88c807SRodney W. Grimes 
18294b88c807SRodney W. Grimes 
18304b88c807SRodney W. Grimes /*
18314b88c807SRodney W. Grimes  * Returns true if the text contains nothing to expand (no dollar signs
18324b88c807SRodney W. Grimes  * or backquotes).
18334b88c807SRodney W. Grimes  */
18344b88c807SRodney W. Grimes 
183588328642SDavid E. O'Brien static int
noexpand(char * text)18365134c3f7SWarner Losh noexpand(char *text)
18374b88c807SRodney W. Grimes {
18387920a31dSSteve Price 	char *p;
18397920a31dSSteve Price 	char c;
18404b88c807SRodney W. Grimes 
18414b88c807SRodney W. Grimes 	p = text;
18424b88c807SRodney W. Grimes 	while ((c = *p++) != '\0') {
18435557a02aSTor Egge 		if ( c == CTLQUOTEMARK)
18445557a02aSTor Egge 			continue;
18454b88c807SRodney W. Grimes 		if (c == CTLESC)
18464b88c807SRodney W. Grimes 			p++;
18470c4eeddaSTor Egge 		else if (BASESYNTAX[(int)c] == CCTL)
18484b88c807SRodney W. Grimes 			return 0;
18494b88c807SRodney W. Grimes 	}
18504b88c807SRodney W. Grimes 	return 1;
18514b88c807SRodney W. Grimes }
18524b88c807SRodney W. Grimes 
18534b88c807SRodney W. Grimes 
18544b88c807SRodney W. Grimes /*
18554b88c807SRodney W. Grimes  * Return true if the argument is a legal variable name (a letter or
18564b88c807SRodney W. Grimes  * underscore followed by zero or more letters, underscores, and digits).
18574b88c807SRodney W. Grimes  */
18584b88c807SRodney W. Grimes 
18594b88c807SRodney W. Grimes int
goodname(const char * name)18602cac6e36SJilles Tjoelker goodname(const char *name)
18614b88c807SRodney W. Grimes {
18622cac6e36SJilles Tjoelker 	const char *p;
18634b88c807SRodney W. Grimes 
18644b88c807SRodney W. Grimes 	p = name;
18654b88c807SRodney W. Grimes 	if (! is_name(*p))
18664b88c807SRodney W. Grimes 		return 0;
18674b88c807SRodney W. Grimes 	while (*++p) {
18684b88c807SRodney W. Grimes 		if (! is_in_name(*p))
18694b88c807SRodney W. Grimes 			return 0;
18704b88c807SRodney W. Grimes 	}
18714b88c807SRodney W. Grimes 	return 1;
18724b88c807SRodney W. Grimes }
18734b88c807SRodney W. Grimes 
18744b88c807SRodney W. Grimes 
187505a447d0SJilles Tjoelker int
isassignment(const char * p)187605a447d0SJilles Tjoelker isassignment(const char *p)
187705a447d0SJilles Tjoelker {
187805a447d0SJilles Tjoelker 	if (!is_name(*p))
187905a447d0SJilles Tjoelker 		return 0;
188005a447d0SJilles Tjoelker 	p++;
188105a447d0SJilles Tjoelker 	for (;;) {
188205a447d0SJilles Tjoelker 		if (*p == '=')
188305a447d0SJilles Tjoelker 			return 1;
188405a447d0SJilles Tjoelker 		else if (!is_in_name(*p))
188505a447d0SJilles Tjoelker 			return 0;
188605a447d0SJilles Tjoelker 		p++;
188705a447d0SJilles Tjoelker 	}
188805a447d0SJilles Tjoelker }
188905a447d0SJilles Tjoelker 
189005a447d0SJilles Tjoelker 
18916ab99f87SJilles Tjoelker static void
consumetoken(int token)18926ab99f87SJilles Tjoelker consumetoken(int token)
18936ab99f87SJilles Tjoelker {
18946ab99f87SJilles Tjoelker 	if (readtoken() != token)
18956ab99f87SJilles Tjoelker 		synexpect(token);
18966ab99f87SJilles Tjoelker }
18976ab99f87SJilles Tjoelker 
18986ab99f87SJilles Tjoelker 
18994b88c807SRodney W. Grimes /*
19004b88c807SRodney W. Grimes  * Called when an unexpected token is read during the parse.  The argument
19014b88c807SRodney W. Grimes  * is the token that is expected, or -1 if more than one type of token can
19024b88c807SRodney W. Grimes  * occur at this point.
19034b88c807SRodney W. Grimes  */
19044b88c807SRodney W. Grimes 
190588328642SDavid E. O'Brien static void
synexpect(int token)19065134c3f7SWarner Losh synexpect(int token)
1907aa9caaf6SPeter Wemm {
19084b88c807SRodney W. Grimes 	char msg[64];
19094b88c807SRodney W. Grimes 
19104b88c807SRodney W. Grimes 	if (token >= 0) {
19114b88c807SRodney W. Grimes 		fmtstr(msg, 64, "%s unexpected (expecting %s)",
19124b88c807SRodney W. Grimes 			tokname[lasttoken], tokname[token]);
19134b88c807SRodney W. Grimes 	} else {
19144b88c807SRodney W. Grimes 		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
19154b88c807SRodney W. Grimes 	}
19164b88c807SRodney W. Grimes 	synerror(msg);
19174b88c807SRodney W. Grimes }
19184b88c807SRodney W. Grimes 
19194b88c807SRodney W. Grimes 
192088328642SDavid E. O'Brien static void
synerror(const char * msg)1921384aedabSJilles Tjoelker synerror(const char *msg)
19224b88c807SRodney W. Grimes {
19234b88c807SRodney W. Grimes 	if (commandname)
1924f7cc73afSJilles Tjoelker 		outfmt(out2, "%s: %d: ", commandname, startlinno);
1925fafeab43SJilles Tjoelker 	else if (arg0)
1926fafeab43SJilles Tjoelker 		outfmt(out2, "%s: ", arg0);
1927f7cc73afSJilles Tjoelker 	outfmt(out2, "Syntax error: %s\n", msg);
19284b88c807SRodney W. Grimes 	error((char *)NULL);
19294b88c807SRodney W. Grimes }
19304b88c807SRodney W. Grimes 
193188328642SDavid E. O'Brien static void
setprompt(int which)19325134c3f7SWarner Losh setprompt(int which)
19334b88c807SRodney W. Grimes {
19344b88c807SRodney W. Grimes 	whichprompt = which;
1935c39f3bacSJilles Tjoelker 	if (which == 0)
1936c39f3bacSJilles Tjoelker 		return;
19374b88c807SRodney W. Grimes 
1938aa9caaf6SPeter Wemm #ifndef NO_HISTORY
19394b88c807SRodney W. Grimes 	if (!el)
1940aa9caaf6SPeter Wemm #endif
1941c6204d4aSJilles Tjoelker 	{
19424b88c807SRodney W. Grimes 		out2str(getprompt(NULL));
1943c6204d4aSJilles Tjoelker 		flushout(out2);
1944c6204d4aSJilles Tjoelker 	}
19454b88c807SRodney W. Grimes }
19464b88c807SRodney W. Grimes 
19470b4b9c81SJilles Tjoelker static int
pgetc_linecont(void)19480b4b9c81SJilles Tjoelker pgetc_linecont(void)
19490b4b9c81SJilles Tjoelker {
19500b4b9c81SJilles Tjoelker 	int c;
19510b4b9c81SJilles Tjoelker 
19520b4b9c81SJilles Tjoelker 	while ((c = pgetc_macro()) == '\\') {
19530b4b9c81SJilles Tjoelker 		c = pgetc();
19540b4b9c81SJilles Tjoelker 		if (c == '\n') {
19550b4b9c81SJilles Tjoelker 			plinno++;
19560b4b9c81SJilles Tjoelker 			if (doprompt)
19570b4b9c81SJilles Tjoelker 				setprompt(2);
19580b4b9c81SJilles Tjoelker 			else
19590b4b9c81SJilles Tjoelker 				setprompt(0);
19600b4b9c81SJilles Tjoelker 		} else {
19610b4b9c81SJilles Tjoelker 			pungetc();
19620b4b9c81SJilles Tjoelker 			/* Allow the backslash to be pushed back. */
19630b4b9c81SJilles Tjoelker 			pushstring("\\", 1, NULL);
19640b4b9c81SJilles Tjoelker 			return (pgetc());
19650b4b9c81SJilles Tjoelker 		}
19660b4b9c81SJilles Tjoelker 	}
19670b4b9c81SJilles Tjoelker 	return (c);
19680b4b9c81SJilles Tjoelker }
19690b4b9c81SJilles Tjoelker 
1970d81ca439SEdward Tomasz Napierala 
1971d81ca439SEdward Tomasz Napierala static struct passwd *
getpwlogin(void)1972d81ca439SEdward Tomasz Napierala getpwlogin(void)
1973d81ca439SEdward Tomasz Napierala {
1974d81ca439SEdward Tomasz Napierala 	const char *login;
1975d81ca439SEdward Tomasz Napierala 
1976d81ca439SEdward Tomasz Napierala 	login = getlogin();
1977d81ca439SEdward Tomasz Napierala 	if (login == NULL)
1978d81ca439SEdward Tomasz Napierala 		return (NULL);
1979d81ca439SEdward Tomasz Napierala 
1980d81ca439SEdward Tomasz Napierala 	return (getpwnam(login));
1981d81ca439SEdward Tomasz Napierala }
1982d81ca439SEdward Tomasz Napierala 
1983d81ca439SEdward Tomasz Napierala 
1984d81ca439SEdward Tomasz Napierala static void
getusername(char * name,size_t namelen)1985d81ca439SEdward Tomasz Napierala getusername(char *name, size_t namelen)
1986d81ca439SEdward Tomasz Napierala {
1987d81ca439SEdward Tomasz Napierala 	static char cached_name[MAXLOGNAME];
1988d81ca439SEdward Tomasz Napierala 	struct passwd *pw;
1989d81ca439SEdward Tomasz Napierala 	uid_t euid;
1990d81ca439SEdward Tomasz Napierala 
1991d81ca439SEdward Tomasz Napierala 	if (cached_name[0] == '\0') {
1992d81ca439SEdward Tomasz Napierala 		euid = geteuid();
1993d81ca439SEdward Tomasz Napierala 
1994d81ca439SEdward Tomasz Napierala 		/*
1995d81ca439SEdward Tomasz Napierala 		 * Handle the case when there is more than one
1996d81ca439SEdward Tomasz Napierala 		 * login with the same UID, or when the login
1997d81ca439SEdward Tomasz Napierala 		 * returned by getlogin(2) does no longer match
1998d81ca439SEdward Tomasz Napierala 		 * the current UID.
1999d81ca439SEdward Tomasz Napierala 		 */
2000d81ca439SEdward Tomasz Napierala 		pw = getpwlogin();
2001d81ca439SEdward Tomasz Napierala 		if (pw == NULL || pw->pw_uid != euid)
2002d81ca439SEdward Tomasz Napierala 			pw = getpwuid(euid);
2003d81ca439SEdward Tomasz Napierala 
2004d81ca439SEdward Tomasz Napierala 		if (pw != NULL) {
2005d81ca439SEdward Tomasz Napierala 			strlcpy(cached_name, pw->pw_name,
2006d81ca439SEdward Tomasz Napierala 			    sizeof(cached_name));
2007d81ca439SEdward Tomasz Napierala 		} else {
2008d81ca439SEdward Tomasz Napierala 			snprintf(cached_name, sizeof(cached_name),
2009d81ca439SEdward Tomasz Napierala 			    "%u", euid);
2010d81ca439SEdward Tomasz Napierala 		}
2011d81ca439SEdward Tomasz Napierala 	}
2012d81ca439SEdward Tomasz Napierala 
2013d81ca439SEdward Tomasz Napierala 	strlcpy(name, cached_name, namelen);
2014d81ca439SEdward Tomasz Napierala }
2015d81ca439SEdward Tomasz Napierala 
2016d81ca439SEdward Tomasz Napierala 
20174b88c807SRodney W. Grimes /*
20184b88c807SRodney W. Grimes  * called by editline -- any expansions to the prompt
20194b88c807SRodney W. Grimes  *    should be added here.
20204b88c807SRodney W. Grimes  */
20214b88c807SRodney W. Grimes char *
getprompt(void * unused __unused)20225134c3f7SWarner Losh getprompt(void *unused __unused)
20234b88c807SRodney W. Grimes {
2024f0c73601SDavid E. O'Brien 	static char ps[PROMPTLEN];
20255545faddSJilles Tjoelker 	const char *fmt;
202620c9381cSEdward Tomasz Napierala 	const char *home;
2027c9c987cdSJilles Tjoelker 	const char *pwd;
202820c9381cSEdward Tomasz Napierala 	size_t homelen;
2029c9c987cdSJilles Tjoelker 	int i, trim;
2030274110dfSJilles Tjoelker 	static char internal_error[] = "??";
2031f0c73601SDavid E. O'Brien 
2032f0c73601SDavid E. O'Brien 	/*
2033f0c73601SDavid E. O'Brien 	 * Select prompt format.
2034f0c73601SDavid E. O'Brien 	 */
20354b88c807SRodney W. Grimes 	switch (whichprompt) {
20364b88c807SRodney W. Grimes 	case 0:
2037781bfb5aSJilles Tjoelker 		fmt = "";
2038f0c73601SDavid E. O'Brien 		break;
20394b88c807SRodney W. Grimes 	case 1:
2040f0c73601SDavid E. O'Brien 		fmt = ps1val();
2041f0c73601SDavid E. O'Brien 		break;
20424b88c807SRodney W. Grimes 	case 2:
2043f0c73601SDavid E. O'Brien 		fmt = ps2val();
2044f0c73601SDavid E. O'Brien 		break;
20454b88c807SRodney W. Grimes 	default:
2046384aedabSJilles Tjoelker 		return internal_error;
20474b88c807SRodney W. Grimes 	}
2048f0c73601SDavid E. O'Brien 
2049f0c73601SDavid E. O'Brien 	/*
2050f0c73601SDavid E. O'Brien 	 * Format prompt string.
2051f0c73601SDavid E. O'Brien 	 */
205263b6e661SPiotr Pawel Stefaniak 	for (i = 0; (i < PROMPTLEN - 1) && (*fmt != '\0'); i++, fmt++) {
205363b6e661SPiotr Pawel Stefaniak 		if (*fmt != '\\') {
205463b6e661SPiotr Pawel Stefaniak 			ps[i] = *fmt;
205563b6e661SPiotr Pawel Stefaniak 			continue;
205663b6e661SPiotr Pawel Stefaniak 		}
205763b6e661SPiotr Pawel Stefaniak 
2058f0c73601SDavid E. O'Brien 		switch (*++fmt) {
2059f0c73601SDavid E. O'Brien 
2060f0c73601SDavid E. O'Brien 		/*
20613cf65f8aSJuraj Lutter 		 * Non-printing sequence begin and end.
20623cf65f8aSJuraj Lutter 		 */
20633cf65f8aSJuraj Lutter 		case '[':
20643cf65f8aSJuraj Lutter 		case ']':
20653cf65f8aSJuraj Lutter 			ps[i] = '\001';
20663cf65f8aSJuraj Lutter 			break;
20673cf65f8aSJuraj Lutter 
20683cf65f8aSJuraj Lutter 		/*
20693cf65f8aSJuraj Lutter 		 * Literal \ and some ASCII characters:
20703cf65f8aSJuraj Lutter 		 * \a	BEL
20713cf65f8aSJuraj Lutter 		 * \e	ESC
20723cf65f8aSJuraj Lutter 		 * \r	CR
20733cf65f8aSJuraj Lutter 		 */
20743cf65f8aSJuraj Lutter 		case '\\':
20753cf65f8aSJuraj Lutter 		case 'a':
20763cf65f8aSJuraj Lutter 		case 'e':
20773cf65f8aSJuraj Lutter 		case 'r':
20783cf65f8aSJuraj Lutter 			if (*fmt == 'a')
20793cf65f8aSJuraj Lutter 				ps[i] = '\007';
20803cf65f8aSJuraj Lutter 			else if (*fmt == 'e')
20813cf65f8aSJuraj Lutter 				ps[i] = '\033';
20823cf65f8aSJuraj Lutter 			else if (*fmt == 'r')
20833cf65f8aSJuraj Lutter 				ps[i] = '\r';
20843cf65f8aSJuraj Lutter 			else
20853cf65f8aSJuraj Lutter 				ps[i] = '\\';
20863cf65f8aSJuraj Lutter 			break;
20873cf65f8aSJuraj Lutter 
20883cf65f8aSJuraj Lutter 		/*
20893cf65f8aSJuraj Lutter 		 * CRLF sequence
20903cf65f8aSJuraj Lutter 		 */
20913cf65f8aSJuraj Lutter 		case 'n':
20923cf65f8aSJuraj Lutter 			if (i < PROMPTLEN - 3) {
20933cf65f8aSJuraj Lutter 				ps[i++] = '\r';
20943cf65f8aSJuraj Lutter 				ps[i] = '\n';
20953cf65f8aSJuraj Lutter 			}
20963cf65f8aSJuraj Lutter 			break;
20973cf65f8aSJuraj Lutter 
20983cf65f8aSJuraj Lutter 		/*
2099*a675eaecSPiotr Pawel Stefaniak 		 * Print the current time as per provided strftime format.
2100*a675eaecSPiotr Pawel Stefaniak 		 */
2101*a675eaecSPiotr Pawel Stefaniak 		case 'D': {
2102*a675eaecSPiotr Pawel Stefaniak 			char tfmt[128] = "%X"; /* \D{} means %X. */
2103*a675eaecSPiotr Pawel Stefaniak 			struct tm *now;
2104*a675eaecSPiotr Pawel Stefaniak 
2105*a675eaecSPiotr Pawel Stefaniak 			if (fmt[1] != '{') {
2106*a675eaecSPiotr Pawel Stefaniak 				/*
2107*a675eaecSPiotr Pawel Stefaniak 				 * "\D" but not "\D{", so treat the '\'
2108*a675eaecSPiotr Pawel Stefaniak 				 * literally and rewind fmt to treat 'D'
2109*a675eaecSPiotr Pawel Stefaniak 				 * literally next iteration.
2110*a675eaecSPiotr Pawel Stefaniak 				 */
2111*a675eaecSPiotr Pawel Stefaniak 				ps[i] = '\\';
2112*a675eaecSPiotr Pawel Stefaniak 				fmt--;
2113*a675eaecSPiotr Pawel Stefaniak 				break;
2114*a675eaecSPiotr Pawel Stefaniak 			}
2115*a675eaecSPiotr Pawel Stefaniak 			fmt += 2; /* Consume "D{". */
2116*a675eaecSPiotr Pawel Stefaniak 			if (fmt[0] != '}') {
2117*a675eaecSPiotr Pawel Stefaniak 				char *end;
2118*a675eaecSPiotr Pawel Stefaniak 
2119*a675eaecSPiotr Pawel Stefaniak 				end = memccpy(tfmt, fmt, '}', sizeof(tfmt));
2120*a675eaecSPiotr Pawel Stefaniak 				if (end == NULL) {
2121*a675eaecSPiotr Pawel Stefaniak 					/*
2122*a675eaecSPiotr Pawel Stefaniak 					 * Format too long or no '}', so
2123*a675eaecSPiotr Pawel Stefaniak 					 * ignore "\D{" altogether.
2124*a675eaecSPiotr Pawel Stefaniak 					 * The loop will do i++, but nothing
2125*a675eaecSPiotr Pawel Stefaniak 					 * was written to ps, so do i-- here.
2126*a675eaecSPiotr Pawel Stefaniak 					 * Rewind fmt for similar reason.
2127*a675eaecSPiotr Pawel Stefaniak 					 */
2128*a675eaecSPiotr Pawel Stefaniak 					i--;
2129*a675eaecSPiotr Pawel Stefaniak 					fmt--;
2130*a675eaecSPiotr Pawel Stefaniak 					break;
2131*a675eaecSPiotr Pawel Stefaniak 				}
2132*a675eaecSPiotr Pawel Stefaniak 				*--end = '\0'; /* Ignore the copy of '}'. */
2133*a675eaecSPiotr Pawel Stefaniak 				fmt += end - tfmt;
2134*a675eaecSPiotr Pawel Stefaniak 			}
2135*a675eaecSPiotr Pawel Stefaniak 			now = localtime(&(time_t){time(NULL)});
2136*a675eaecSPiotr Pawel Stefaniak 			i += strftime(&ps[i], PROMPTLEN - i - 1, tfmt, now);
2137*a675eaecSPiotr Pawel Stefaniak 			i--; /* The loop will do i++. */
2138*a675eaecSPiotr Pawel Stefaniak 			break;
2139*a675eaecSPiotr Pawel Stefaniak 		}
2140*a675eaecSPiotr Pawel Stefaniak 
2141*a675eaecSPiotr Pawel Stefaniak 		/*
2142f0c73601SDavid E. O'Brien 		 * Hostname.
2143f0c73601SDavid E. O'Brien 		 *
2144f0c73601SDavid E. O'Brien 		 * \h specifies just the local hostname,
2145f0c73601SDavid E. O'Brien 		 * \H specifies fully-qualified hostname.
2146f0c73601SDavid E. O'Brien 		 */
2147f0c73601SDavid E. O'Brien 		case 'h':
2148f0c73601SDavid E. O'Brien 		case 'H':
21498d999570SStefan Farfeleder 			ps[i] = '\0';
215063a4675dSDon Lewis 			gethostname(&ps[i], PROMPTLEN - i - 1);
215163a4675dSDon Lewis 			ps[PROMPTLEN - 1] = '\0';
2152f0c73601SDavid E. O'Brien 			/* Skip to end of hostname. */
2153f0c73601SDavid E. O'Brien 			trim = (*fmt == 'h') ? '.' : '\0';
21548e3543eeSEric van Gyzen 			while ((ps[i] != '\0') && (ps[i] != trim))
2155f0c73601SDavid E. O'Brien 				i++;
21568e3543eeSEric van Gyzen 			--i;
2157f0c73601SDavid E. O'Brien 			break;
2158f0c73601SDavid E. O'Brien 
2159f0c73601SDavid E. O'Brien 		/*
2160d81ca439SEdward Tomasz Napierala 		 * User name.
2161d81ca439SEdward Tomasz Napierala 		 */
2162d81ca439SEdward Tomasz Napierala 		case 'u':
2163d81ca439SEdward Tomasz Napierala 			ps[i] = '\0';
2164d81ca439SEdward Tomasz Napierala 			getusername(&ps[i], PROMPTLEN - i);
2165d81ca439SEdward Tomasz Napierala 			/* Skip to end of username. */
2166d81ca439SEdward Tomasz Napierala 			while (ps[i + 1] != '\0')
2167d81ca439SEdward Tomasz Napierala 				i++;
2168d81ca439SEdward Tomasz Napierala 			break;
2169d81ca439SEdward Tomasz Napierala 
2170d81ca439SEdward Tomasz Napierala 		/*
2171f0c73601SDavid E. O'Brien 		 * Working directory.
2172f0c73601SDavid E. O'Brien 		 *
2173f0c73601SDavid E. O'Brien 		 * \W specifies just the final component,
2174f0c73601SDavid E. O'Brien 		 * \w specifies the entire path.
2175f0c73601SDavid E. O'Brien 		 */
2176f0c73601SDavid E. O'Brien 		case 'W':
2177f0c73601SDavid E. O'Brien 		case 'w':
2178c9c987cdSJilles Tjoelker 			pwd = lookupvar("PWD");
21798e3543eeSEric van Gyzen 			if (pwd == NULL || *pwd == '\0')
2180c9c987cdSJilles Tjoelker 				pwd = "?";
2181c9c987cdSJilles Tjoelker 			if (*fmt == 'W' &&
2182c9c987cdSJilles Tjoelker 			    *pwd == '/' && pwd[1] != '\0')
2183c9c987cdSJilles Tjoelker 				strlcpy(&ps[i], strrchr(pwd, '/') + 1,
2184c9c987cdSJilles Tjoelker 				    PROMPTLEN - i);
218520c9381cSEdward Tomasz Napierala 			else {
218620c9381cSEdward Tomasz Napierala 				home = lookupvar("HOME");
218720c9381cSEdward Tomasz Napierala 				if (home != NULL)
218820c9381cSEdward Tomasz Napierala 					homelen = strlen(home);
218920c9381cSEdward Tomasz Napierala 				if (home != NULL &&
219020c9381cSEdward Tomasz Napierala 				    strcmp(home, "/") != 0 &&
219120c9381cSEdward Tomasz Napierala 				    strncmp(pwd, home, homelen) == 0 &&
219220c9381cSEdward Tomasz Napierala 				    (pwd[homelen] == '/' ||
219320c9381cSEdward Tomasz Napierala 				    pwd[homelen] == '\0')) {
219420c9381cSEdward Tomasz Napierala 					strlcpy(&ps[i], "~",
219520c9381cSEdward Tomasz Napierala 					    PROMPTLEN - i);
219620c9381cSEdward Tomasz Napierala 					strlcpy(&ps[i + 1],
219720c9381cSEdward Tomasz Napierala 					    pwd + homelen,
219820c9381cSEdward Tomasz Napierala 					    PROMPTLEN - i - 1);
219920c9381cSEdward Tomasz Napierala 				} else {
2200c9c987cdSJilles Tjoelker 					strlcpy(&ps[i], pwd, PROMPTLEN - i);
220120c9381cSEdward Tomasz Napierala 				}
220220c9381cSEdward Tomasz Napierala 			}
2203f0c73601SDavid E. O'Brien 			/* Skip to end of path. */
2204f0c73601SDavid E. O'Brien 			while (ps[i + 1] != '\0')
2205f0c73601SDavid E. O'Brien 				i++;
2206f0c73601SDavid E. O'Brien 			break;
2207f0c73601SDavid E. O'Brien 
2208f0c73601SDavid E. O'Brien 		/*
2209f0c73601SDavid E. O'Brien 		 * Superuser status.
2210f0c73601SDavid E. O'Brien 		 *
2211f0c73601SDavid E. O'Brien 		 * '$' for normal users, '#' for root.
2212f0c73601SDavid E. O'Brien 		 */
2213f0c73601SDavid E. O'Brien 		case '$':
2214f0c73601SDavid E. O'Brien 			ps[i] = (geteuid() != 0) ? '$' : '#';
2215f0c73601SDavid E. O'Brien 			break;
2216f0c73601SDavid E. O'Brien 
2217f0c73601SDavid E. O'Brien 		/*
2218f0c73601SDavid E. O'Brien 		 * Emit unrecognized formats verbatim.
2219f0c73601SDavid E. O'Brien 		 */
2220f0c73601SDavid E. O'Brien 		default:
222163a4675dSDon Lewis 			ps[i] = '\\';
2222eaf2e1e6SDon Lewis 			if (i < PROMPTLEN - 2)
222363a4675dSDon Lewis 				ps[++i] = *fmt;
2224f0c73601SDavid E. O'Brien 			break;
2225f0c73601SDavid E. O'Brien 		}
222663b6e661SPiotr Pawel Stefaniak 
222763b6e661SPiotr Pawel Stefaniak 	}
2228f0c73601SDavid E. O'Brien 	ps[i] = '\0';
2229f0c73601SDavid E. O'Brien 	return (ps);
22304b88c807SRodney W. Grimes }
2231292e6676SJilles Tjoelker 
2232292e6676SJilles Tjoelker 
2233292e6676SJilles Tjoelker const char *
expandstr(const char * ps)223446c6b52dSJilles Tjoelker expandstr(const char *ps)
2235292e6676SJilles Tjoelker {
2236292e6676SJilles Tjoelker 	union node n;
2237292e6676SJilles Tjoelker 	struct jmploc jmploc;
2238292e6676SJilles Tjoelker 	struct jmploc *const savehandler = handler;
2239292e6676SJilles Tjoelker 	const int saveprompt = doprompt;
2240292e6676SJilles Tjoelker 	struct parsefile *const savetopfile = getcurrentfile();
2241292e6676SJilles Tjoelker 	struct parser_temp *const saveparser_temp = parser_temp;
2242292e6676SJilles Tjoelker 	const char *result = NULL;
2243292e6676SJilles Tjoelker 
2244292e6676SJilles Tjoelker 	if (!setjmp(jmploc.loc)) {
2245292e6676SJilles Tjoelker 		handler = &jmploc;
2246292e6676SJilles Tjoelker 		parser_temp = NULL;
2247292e6676SJilles Tjoelker 		setinputstring(ps, 1);
2248292e6676SJilles Tjoelker 		doprompt = 0;
224992fe71faSJilles Tjoelker 		readtoken1(pgetc(), DQSYNTAX, NOEOFMARK, 0);
2250292e6676SJilles Tjoelker 		if (backquotelist != NULL)
2251292e6676SJilles Tjoelker 			error("Command substitution not allowed here");
2252292e6676SJilles Tjoelker 
2253292e6676SJilles Tjoelker 		n.narg.type = NARG;
2254292e6676SJilles Tjoelker 		n.narg.next = NULL;
2255292e6676SJilles Tjoelker 		n.narg.text = wordtext;
2256292e6676SJilles Tjoelker 		n.narg.backquote = backquotelist;
2257292e6676SJilles Tjoelker 
2258292e6676SJilles Tjoelker 		expandarg(&n, NULL, 0);
2259292e6676SJilles Tjoelker 		result = stackblock();
2260292e6676SJilles Tjoelker 		INTOFF;
2261292e6676SJilles Tjoelker 	}
2262292e6676SJilles Tjoelker 	handler = savehandler;
2263292e6676SJilles Tjoelker 	doprompt = saveprompt;
2264292e6676SJilles Tjoelker 	popfilesupto(savetopfile);
2265292e6676SJilles Tjoelker 	if (parser_temp != saveparser_temp) {
2266292e6676SJilles Tjoelker 		parser_temp_free_all();
2267292e6676SJilles Tjoelker 		parser_temp = saveparser_temp;
2268292e6676SJilles Tjoelker 	}
2269292e6676SJilles Tjoelker 	if (result != NULL) {
2270292e6676SJilles Tjoelker 		INTON;
2271292e6676SJilles Tjoelker 	} else if (exception == EXINT)
2272292e6676SJilles Tjoelker 		raise(SIGINT);
2273292e6676SJilles Tjoelker 	return result;
2274292e6676SJilles Tjoelker }
2275