11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD$");
401556Srgrimes
4117987Speter#include <stdlib.h>
42149017Sstefanf#include <unistd.h>
43209337Sjilles#include <stdio.h>
4417987Speter
451556Srgrimes#include "shell.h"
461556Srgrimes#include "parser.h"
471556Srgrimes#include "nodes.h"
481556Srgrimes#include "expand.h"	/* defines rmescapes() */
491556Srgrimes#include "syntax.h"
501556Srgrimes#include "options.h"
511556Srgrimes#include "input.h"
521556Srgrimes#include "output.h"
531556Srgrimes#include "var.h"
541556Srgrimes#include "error.h"
551556Srgrimes#include "memalloc.h"
561556Srgrimes#include "mystring.h"
571556Srgrimes#include "alias.h"
5817987Speter#include "show.h"
5959436Scracauer#include "eval.h"
60214304Sjilles#include "exec.h"	/* to check for special builtins */
6117987Speter#ifndef NO_HISTORY
621556Srgrimes#include "myhistedit.h"
6317987Speter#endif
641556Srgrimes
651556Srgrimes/*
661556Srgrimes * Shell command parser.
671556Srgrimes */
681556Srgrimes
69142845Sobrien#define	EOFMARKLEN	79
70142845Sobrien#define	PROMPTLEN	128
711556Srgrimes
72214709Sjilles/* values of checkkwd variable */
73214709Sjilles#define CHKALIAS	0x1
74214709Sjilles#define CHKKWD		0x2
75214709Sjilles#define CHKNL		0x4
76214709Sjilles
771556Srgrimes/* values returned by readtoken */
7817987Speter#include "token.h"
791556Srgrimes
801556Srgrimes
811556Srgrimes
821556Srgrimesstruct heredoc {
831556Srgrimes	struct heredoc *next;	/* next here document in list */
841556Srgrimes	union node *here;		/* redirection node */
851556Srgrimes	char *eofmark;		/* string indicating end of input */
861556Srgrimes	int striptabs;		/* if set, strip leading tabs */
871556Srgrimes};
881556Srgrimes
89206145Sjillesstruct parser_temp {
90206145Sjilles	struct parser_temp *next;
91206145Sjilles	void *data;
92206145Sjilles};
931556Srgrimes
941556Srgrimes
95213760Sobrienstatic struct heredoc *heredoclist;	/* list of here documents to read */
96213760Sobrienstatic int doprompt;		/* if set, prompt the user */
97213760Sobrienstatic int needprompt;		/* true if interactive and at start of line */
98213760Sobrienstatic int lasttoken;		/* last token read */
99255068Sjillesstatic int tokpushback;		/* last token pushed back */
100213760Sobrienstatic char *wordtext;		/* text of last word returned by readtoken */
101253659Sjillesstatic int checkkwd;
102213760Sobrienstatic struct nodelist *backquotelist;
103213760Sobrienstatic union node *redirnode;
104213760Sobrienstatic struct heredoc *heredoc;
105213760Sobrienstatic int quoteflag;		/* set if (part of) last token was quoted */
106213760Sobrienstatic int startlinno;		/* line # where last token started */
107213760Sobrienstatic int funclinno;		/* line # where the current function started */
108213760Sobrienstatic struct parser_temp *parser_temp;
1091556Srgrimes
1101556Srgrimes
111255087Sjillesstatic union node *list(int);
112213811Sobrienstatic union node *andor(void);
113213811Sobrienstatic union node *pipeline(void);
114213811Sobrienstatic union node *command(void);
115213811Sobrienstatic union node *simplecmd(union node **, union node *);
116213811Sobrienstatic union node *makename(void);
117255085Sjillesstatic union node *makebinary(int type, union node *n1, union node *n2);
118213811Sobrienstatic void parsefname(void);
119213811Sobrienstatic void parseheredoc(void);
120213811Sobrienstatic int peektoken(void);
121213811Sobrienstatic int readtoken(void);
122213811Sobrienstatic int xxreadtoken(void);
123248980Sjillesstatic int readtoken1(int, const char *, const char *, int);
124213811Sobrienstatic int noexpand(char *);
125255073Sjillesstatic void consumetoken(int);
126213811Sobrienstatic void synexpect(int) __dead2;
127213811Sobrienstatic void synerror(const char *) __dead2;
128213811Sobrienstatic void setprompt(int);
1291556Srgrimes
13017987Speter
131213811Sobrienstatic void *
132206145Sjillesparser_temp_alloc(size_t len)
133206145Sjilles{
134206145Sjilles	struct parser_temp *t;
135206145Sjilles
136206145Sjilles	INTOFF;
137206145Sjilles	t = ckmalloc(sizeof(*t));
138206145Sjilles	t->data = NULL;
139206145Sjilles	t->next = parser_temp;
140206145Sjilles	parser_temp = t;
141206145Sjilles	t->data = ckmalloc(len);
142206145Sjilles	INTON;
143206145Sjilles	return t->data;
144206145Sjilles}
145206145Sjilles
146206145Sjilles
147213811Sobrienstatic void *
148206145Sjillesparser_temp_realloc(void *ptr, size_t len)
149206145Sjilles{
150206145Sjilles	struct parser_temp *t;
151206145Sjilles
152206145Sjilles	INTOFF;
153206145Sjilles	t = parser_temp;
154206145Sjilles	if (ptr != t->data)
155206145Sjilles		error("bug: parser_temp_realloc misused");
156206145Sjilles	t->data = ckrealloc(t->data, len);
157206145Sjilles	INTON;
158206145Sjilles	return t->data;
159206145Sjilles}
160206145Sjilles
161206145Sjilles
162213811Sobrienstatic void
163206145Sjillesparser_temp_free_upto(void *ptr)
164206145Sjilles{
165206145Sjilles	struct parser_temp *t;
166206145Sjilles	int done = 0;
167206145Sjilles
168206145Sjilles	INTOFF;
169206145Sjilles	while (parser_temp != NULL && !done) {
170206145Sjilles		t = parser_temp;
171206145Sjilles		parser_temp = t->next;
172206145Sjilles		done = t->data == ptr;
173206145Sjilles		ckfree(t->data);
174206145Sjilles		ckfree(t);
175206145Sjilles	}
176206145Sjilles	INTON;
177206145Sjilles	if (!done)
178206145Sjilles		error("bug: parser_temp_free_upto misused");
179206145Sjilles}
180206145Sjilles
181206145Sjilles
182213811Sobrienstatic void
183206145Sjillesparser_temp_free_all(void)
184206145Sjilles{
185206145Sjilles	struct parser_temp *t;
186206145Sjilles
187206145Sjilles	INTOFF;
188206145Sjilles	while (parser_temp != NULL) {
189206145Sjilles		t = parser_temp;
190206145Sjilles		parser_temp = t->next;
191206145Sjilles		ckfree(t->data);
192206145Sjilles		ckfree(t);
193206145Sjilles	}
194206145Sjilles	INTON;
195206145Sjilles}
196206145Sjilles
197206145Sjilles
1981556Srgrimes/*
1991556Srgrimes * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
2001556Srgrimes * valid parse tree indicating a blank line.)
2011556Srgrimes */
2021556Srgrimes
2031556Srgrimesunion node *
20490111Simpparsecmd(int interact)
20517987Speter{
2061556Srgrimes	int t;
2071556Srgrimes
208206145Sjilles	/* This assumes the parser is not re-entered,
209206145Sjilles	 * which could happen if we add command substitution on PS1/PS2.
210206145Sjilles	 */
211206145Sjilles	parser_temp_free_all();
212208656Sjilles	heredoclist = NULL;
213206145Sjilles
21460593Scracauer	tokpushback = 0;
215254426Sjilles	checkkwd = 0;
2161556Srgrimes	doprompt = interact;
2171556Srgrimes	if (doprompt)
2181556Srgrimes		setprompt(1);
2191556Srgrimes	else
2201556Srgrimes		setprompt(0);
2211556Srgrimes	needprompt = 0;
2221556Srgrimes	t = readtoken();
2231556Srgrimes	if (t == TEOF)
2241556Srgrimes		return NEOF;
2251556Srgrimes	if (t == TNL)
2261556Srgrimes		return NULL;
2271556Srgrimes	tokpushback++;
228255087Sjilles	return list(1);
2291556Srgrimes}
2301556Srgrimes
2311556Srgrimes
232289938Sjilles/*
233289938Sjilles * Read and parse words for wordexp.
234289938Sjilles * Returns a list of NARG nodes; NULL if there are no words.
235289938Sjilles */
236289938Sjillesunion node *
237289938Sjillesparsewordexp(void)
238289938Sjilles{
239289938Sjilles	union node *n, *first = NULL, **pnext;
240289938Sjilles	int t;
241289938Sjilles
242289938Sjilles	/* This assumes the parser is not re-entered,
243289938Sjilles	 * which could happen if we add command substitution on PS1/PS2.
244289938Sjilles	 */
245289938Sjilles	parser_temp_free_all();
246289938Sjilles	heredoclist = NULL;
247289938Sjilles
248289938Sjilles	tokpushback = 0;
249289938Sjilles	checkkwd = 0;
250289938Sjilles	doprompt = 0;
251289938Sjilles	setprompt(0);
252289938Sjilles	needprompt = 0;
253289938Sjilles	pnext = &first;
254289938Sjilles	while ((t = readtoken()) != TEOF) {
255289938Sjilles		if (t != TWORD)
256289938Sjilles			synexpect(TWORD);
257289938Sjilles		n = makename();
258289938Sjilles		*pnext = n;
259289938Sjilles		pnext = &n->narg.next;
260289938Sjilles	}
261289938Sjilles	return first;
262289938Sjilles}
263289938Sjilles
264289938Sjilles
265213811Sobrienstatic union node *
266255087Sjilleslist(int nlflag)
26717987Speter{
268214599Sjilles	union node *ntop, *n1, *n2, *n3;
26917987Speter	int tok;
2701556Srgrimes
271214709Sjilles	checkkwd = CHKNL | CHKKWD | CHKALIAS;
272255087Sjilles	if (!nlflag && tokendlist[peektoken()])
2731556Srgrimes		return NULL;
274214599Sjilles	ntop = n1 = NULL;
2751556Srgrimes	for (;;) {
27617987Speter		n2 = andor();
27717987Speter		tok = readtoken();
27817987Speter		if (tok == TBACKGND) {
279245382Sjilles			if (n2 != NULL && n2->type == NPIPE) {
280223282Sjilles				n2->npipe.backgnd = 1;
281245382Sjilles			} else if (n2 != NULL && n2->type == NREDIR) {
28217987Speter				n2->type = NBACKGND;
28317987Speter			} else {
28417987Speter				n3 = (union node *)stalloc(sizeof (struct nredir));
28517987Speter				n3->type = NBACKGND;
28617987Speter				n3->nredir.n = n2;
28717987Speter				n3->nredir.redirect = NULL;
28817987Speter				n2 = n3;
28917987Speter			}
29017987Speter		}
291214599Sjilles		if (ntop == NULL)
292214599Sjilles			ntop = n2;
293214599Sjilles		else if (n1 == NULL) {
294255085Sjilles			n1 = makebinary(NSEMI, ntop, n2);
295214599Sjilles			ntop = n1;
29617987Speter		}
29717987Speter		else {
298255085Sjilles			n3 = makebinary(NSEMI, n1->nbinary.ch2, n2);
299214599Sjilles			n1->nbinary.ch2 = n3;
30017987Speter			n1 = n3;
30117987Speter		}
30217987Speter		switch (tok) {
30313882Sjoerg		case TBACKGND:
30417987Speter		case TSEMI:
30517987Speter			tok = readtoken();
306102410Scharnier			/* FALLTHROUGH */
3071556Srgrimes		case TNL:
30817987Speter			if (tok == TNL) {
30917987Speter				parseheredoc();
31017987Speter				if (nlflag)
311214599Sjilles					return ntop;
312210488Sjilles			} else if (tok == TEOF && nlflag) {
313210488Sjilles				parseheredoc();
314214599Sjilles				return ntop;
31517987Speter			} else {
31617987Speter				tokpushback++;
31717987Speter			}
318214709Sjilles			checkkwd = CHKNL | CHKKWD | CHKALIAS;
319255087Sjilles			if (!nlflag && tokendlist[peektoken()])
320214599Sjilles				return ntop;
3211556Srgrimes			break;
3221556Srgrimes		case TEOF:
3231556Srgrimes			if (heredoclist)
3241556Srgrimes				parseheredoc();
3251556Srgrimes			else
3261556Srgrimes				pungetc();		/* push back EOF on input */
327214599Sjilles			return ntop;
3281556Srgrimes		default:
329255087Sjilles			if (nlflag)
3301556Srgrimes				synexpect(-1);
3311556Srgrimes			tokpushback++;
332214599Sjilles			return ntop;
3331556Srgrimes		}
3341556Srgrimes	}
3351556Srgrimes}
3361556Srgrimes
3371556Srgrimes
3381556Srgrimes
339213811Sobrienstatic union node *
34090111Simpandor(void)
34190111Simp{
342255085Sjilles	union node *n;
3431556Srgrimes	int t;
3441556Srgrimes
345255085Sjilles	n = pipeline();
3461556Srgrimes	for (;;) {
3471556Srgrimes		if ((t = readtoken()) == TAND) {
3481556Srgrimes			t = NAND;
3491556Srgrimes		} else if (t == TOR) {
3501556Srgrimes			t = NOR;
3511556Srgrimes		} else {
3521556Srgrimes			tokpushback++;
353255085Sjilles			return n;
3541556Srgrimes		}
355255085Sjilles		n = makebinary(t, n, pipeline());
3561556Srgrimes	}
3571556Srgrimes}
3581556Srgrimes
3591556Srgrimes
3601556Srgrimes
361213811Sobrienstatic union node *
36290111Simppipeline(void)
36390111Simp{
36475336Sbrian	union node *n1, *n2, *pipenode;
3651556Srgrimes	struct nodelist *lp, *prev;
366214281Sjilles	int negate, t;
3671556Srgrimes
36875336Sbrian	negate = 0;
369214709Sjilles	checkkwd = CHKNL | CHKKWD | CHKALIAS;
3701556Srgrimes	TRACE(("pipeline: entered\n"));
37175336Sbrian	while (readtoken() == TNOT)
37275336Sbrian		negate = !negate;
37375336Sbrian	tokpushback++;
3741556Srgrimes	n1 = command();
3751556Srgrimes	if (readtoken() == TPIPE) {
3761556Srgrimes		pipenode = (union node *)stalloc(sizeof (struct npipe));
3771556Srgrimes		pipenode->type = NPIPE;
3781556Srgrimes		pipenode->npipe.backgnd = 0;
3791556Srgrimes		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
3801556Srgrimes		pipenode->npipe.cmdlist = lp;
3811556Srgrimes		lp->n = n1;
3821556Srgrimes		do {
3831556Srgrimes			prev = lp;
3841556Srgrimes			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
385214709Sjilles			checkkwd = CHKNL | CHKKWD | CHKALIAS;
386214281Sjilles			t = readtoken();
387214281Sjilles			tokpushback++;
388214281Sjilles			if (t == TNOT)
389214281Sjilles				lp->n = pipeline();
390214281Sjilles			else
391214281Sjilles				lp->n = command();
3921556Srgrimes			prev->next = lp;
3931556Srgrimes		} while (readtoken() == TPIPE);
3941556Srgrimes		lp->next = NULL;
3951556Srgrimes		n1 = pipenode;
3961556Srgrimes	}
3971556Srgrimes	tokpushback++;
39875336Sbrian	if (negate) {
39975336Sbrian		n2 = (union node *)stalloc(sizeof (struct nnot));
40075336Sbrian		n2->type = NNOT;
40175336Sbrian		n2->nnot.com = n1;
40275336Sbrian		return n2;
40375336Sbrian	} else
40475336Sbrian		return n1;
4051556Srgrimes}
4061556Srgrimes
4071556Srgrimes
4081556Srgrimes
409213811Sobrienstatic union node *
41090111Simpcommand(void)
41190111Simp{
4121556Srgrimes	union node *n1, *n2;
4131556Srgrimes	union node *ap, **app;
4141556Srgrimes	union node *cp, **cpp;
4151556Srgrimes	union node *redir, **rpp;
416214281Sjilles	int t;
417218325Sjilles	int is_subshell;
4181556Srgrimes
419214709Sjilles	checkkwd = CHKNL | CHKKWD | CHKALIAS;
420218325Sjilles	is_subshell = 0;
42117987Speter	redir = NULL;
42217987Speter	n1 = NULL;
4231556Srgrimes	rpp = &redir;
42420425Ssteve
4251556Srgrimes	/* Check for redirection which may precede command */
4261556Srgrimes	while (readtoken() == TREDIR) {
4271556Srgrimes		*rpp = n2 = redirnode;
4281556Srgrimes		rpp = &n2->nfile.next;
4291556Srgrimes		parsefname();
4301556Srgrimes	}
4311556Srgrimes	tokpushback++;
4321556Srgrimes
4331556Srgrimes	switch (readtoken()) {
4341556Srgrimes	case TIF:
4351556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nif));
4361556Srgrimes		n1->type = NIF;
437255087Sjilles		if ((n1->nif.test = list(0)) == NULL)
438104554Stjr			synexpect(-1);
439255073Sjilles		consumetoken(TTHEN);
440255087Sjilles		n1->nif.ifpart = list(0);
4411556Srgrimes		n2 = n1;
4421556Srgrimes		while (readtoken() == TELIF) {
4431556Srgrimes			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
4441556Srgrimes			n2 = n2->nif.elsepart;
4451556Srgrimes			n2->type = NIF;
446255087Sjilles			if ((n2->nif.test = list(0)) == NULL)
447104554Stjr				synexpect(-1);
448255073Sjilles			consumetoken(TTHEN);
449255087Sjilles			n2->nif.ifpart = list(0);
4501556Srgrimes		}
4511556Srgrimes		if (lasttoken == TELSE)
452255087Sjilles			n2->nif.elsepart = list(0);
4531556Srgrimes		else {
4541556Srgrimes			n2->nif.elsepart = NULL;
4551556Srgrimes			tokpushback++;
4561556Srgrimes		}
457255073Sjilles		consumetoken(TFI);
458214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
4591556Srgrimes		break;
4601556Srgrimes	case TWHILE:
461255073Sjilles	case TUNTIL:
462255085Sjilles		t = lasttoken;
463255087Sjilles		if ((n1 = list(0)) == NULL)
464104554Stjr			synexpect(-1);
465255073Sjilles		consumetoken(TDO);
466255087Sjilles		n1 = makebinary((t == TWHILE)? NWHILE : NUNTIL, n1, list(0));
467255073Sjilles		consumetoken(TDONE);
468214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
4691556Srgrimes		break;
4701556Srgrimes	case TFOR:
4711556Srgrimes		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
4721556Srgrimes			synerror("Bad for loop variable");
4731556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nfor));
4741556Srgrimes		n1->type = NFOR;
4751556Srgrimes		n1->nfor.var = wordtext;
476199282Sjilles		while (readtoken() == TNL)
477199282Sjilles			;
478199282Sjilles		if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
4791556Srgrimes			app = &ap;
4801556Srgrimes			while (readtoken() == TWORD) {
481255081Sjilles				n2 = makename();
4821556Srgrimes				*app = n2;
4831556Srgrimes				app = &n2->narg.next;
4841556Srgrimes			}
4851556Srgrimes			*app = NULL;
4861556Srgrimes			n1->nfor.args = ap;
4871556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4881556Srgrimes				synexpect(-1);
4891556Srgrimes		} else {
490149096Sstefanf			static char argvars[5] = {
491149096Sstefanf				CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
492149096Sstefanf			};
4931556Srgrimes			n2 = (union node *)stalloc(sizeof (struct narg));
4941556Srgrimes			n2->type = NARG;
495149096Sstefanf			n2->narg.text = argvars;
4961556Srgrimes			n2->narg.backquote = NULL;
4971556Srgrimes			n2->narg.next = NULL;
4981556Srgrimes			n1->nfor.args = n2;
4991556Srgrimes			/*
5001556Srgrimes			 * Newline or semicolon here is optional (but note
5011556Srgrimes			 * that the original Bourne shell only allowed NL).
5021556Srgrimes			 */
5031556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
5041556Srgrimes				tokpushback++;
5051556Srgrimes		}
506214709Sjilles		checkkwd = CHKNL | CHKKWD | CHKALIAS;
5071556Srgrimes		if ((t = readtoken()) == TDO)
5081556Srgrimes			t = TDONE;
5091556Srgrimes		else if (t == TBEGIN)
5101556Srgrimes			t = TEND;
5111556Srgrimes		else
5121556Srgrimes			synexpect(-1);
513255087Sjilles		n1->nfor.body = list(0);
514255073Sjilles		consumetoken(t);
515214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
5161556Srgrimes		break;
5171556Srgrimes	case TCASE:
5181556Srgrimes		n1 = (union node *)stalloc(sizeof (struct ncase));
5191556Srgrimes		n1->type = NCASE;
520255073Sjilles		consumetoken(TWORD);
521255081Sjilles		n1->ncase.expr = makename();
5221556Srgrimes		while (readtoken() == TNL);
5231556Srgrimes		if (lasttoken != TWORD || ! equal(wordtext, "in"))
5241556Srgrimes			synerror("expecting \"in\"");
5251556Srgrimes		cpp = &n1->ncase.cases;
526214709Sjilles		checkkwd = CHKNL | CHKKWD, readtoken();
527104202Stjr		while (lasttoken != TESAC) {
5281556Srgrimes			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
5291556Srgrimes			cp->type = NCLIST;
5301556Srgrimes			app = &cp->nclist.pattern;
531104207Stjr			if (lasttoken == TLP)
532104207Stjr				readtoken();
5331556Srgrimes			for (;;) {
534255081Sjilles				*app = ap = makename();
535214709Sjilles				checkkwd = CHKNL | CHKKWD;
536214709Sjilles				if (readtoken() != TPIPE)
5371556Srgrimes					break;
5381556Srgrimes				app = &ap->narg.next;
5392760Ssef				readtoken();
5401556Srgrimes			}
5411556Srgrimes			ap->narg.next = NULL;
5421556Srgrimes			if (lasttoken != TRP)
543214709Sjilles				synexpect(TRP);
544255087Sjilles			cp->nclist.body = list(0);
5452760Ssef
546214709Sjilles			checkkwd = CHKNL | CHKKWD | CHKALIAS;
5472760Ssef			if ((t = readtoken()) != TESAC) {
548223186Sjilles				if (t == TENDCASE)
549223186Sjilles					;
550223186Sjilles				else if (t == TFALLTHRU)
551223186Sjilles					cp->type = NCLISTFALLTHRU;
552223186Sjilles				else
553214709Sjilles					synexpect(TENDCASE);
554223186Sjilles				checkkwd = CHKNL | CHKKWD, readtoken();
5552760Ssef			}
5561556Srgrimes			cpp = &cp->nclist.next;
557104202Stjr		}
5581556Srgrimes		*cpp = NULL;
559214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
5601556Srgrimes		break;
5611556Srgrimes	case TLP:
5621556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nredir));
5631556Srgrimes		n1->type = NSUBSHELL;
564255087Sjilles		n1->nredir.n = list(0);
5651556Srgrimes		n1->nredir.redirect = NULL;
566255073Sjilles		consumetoken(TRP);
567214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
568218325Sjilles		is_subshell = 1;
5691556Srgrimes		break;
5701556Srgrimes	case TBEGIN:
571255087Sjilles		n1 = list(0);
572255073Sjilles		consumetoken(TEND);
573214709Sjilles		checkkwd = CHKKWD | CHKALIAS;
5741556Srgrimes		break;
575254843Sjilles	/* A simple command must have at least one redirection or word. */
576210221Sjilles	case TBACKGND:
57717987Speter	case TSEMI:
578101662Stjr	case TAND:
579101662Stjr	case TOR:
580254335Sjilles	case TPIPE:
581254335Sjilles	case TENDCASE:
582254335Sjilles	case TFALLTHRU:
583254843Sjilles	case TEOF:
584254843Sjilles	case TNL:
585254843Sjilles	case TRP:
58617987Speter		if (!redir)
58717987Speter			synexpect(-1);
5881556Srgrimes	case TWORD:
5891556Srgrimes		tokpushback++;
59075160Sbrian		n1 = simplecmd(rpp, redir);
591214281Sjilles		return n1;
5921556Srgrimes	default:
5931556Srgrimes		synexpect(-1);
5941556Srgrimes	}
5951556Srgrimes
5961556Srgrimes	/* Now check for redirection which may follow command */
5971556Srgrimes	while (readtoken() == TREDIR) {
5981556Srgrimes		*rpp = n2 = redirnode;
5991556Srgrimes		rpp = &n2->nfile.next;
6001556Srgrimes		parsefname();
6011556Srgrimes	}
6021556Srgrimes	tokpushback++;
6031556Srgrimes	*rpp = NULL;
6041556Srgrimes	if (redir) {
605218325Sjilles		if (!is_subshell) {
6061556Srgrimes			n2 = (union node *)stalloc(sizeof (struct nredir));
6071556Srgrimes			n2->type = NREDIR;
6081556Srgrimes			n2->nredir.n = n1;
6091556Srgrimes			n1 = n2;
6101556Srgrimes		}
6111556Srgrimes		n1->nredir.redirect = redir;
6121556Srgrimes	}
61375160Sbrian
614214281Sjilles	return n1;
6151556Srgrimes}
6161556Srgrimes
6171556Srgrimes
618213811Sobrienstatic union node *
61990111Simpsimplecmd(union node **rpp, union node *redir)
62090111Simp{
6211556Srgrimes	union node *args, **app;
6221556Srgrimes	union node **orig_rpp = rpp;
623210087Sjilles	union node *n = NULL;
624214304Sjilles	int special;
625222165Sjilles	int savecheckkwd;
6261556Srgrimes
6271556Srgrimes	/* If we don't have any redirections already, then we must reset */
6281556Srgrimes	/* rpp to be the address of the local redir variable.  */
6291556Srgrimes	if (redir == 0)
6301556Srgrimes		rpp = &redir;
6311556Srgrimes
6321556Srgrimes	args = NULL;
6331556Srgrimes	app = &args;
6348855Srgrimes	/*
6351556Srgrimes	 * We save the incoming value, because we need this for shell
6361556Srgrimes	 * functions.  There can not be a redirect or an argument between
6378855Srgrimes	 * the function name and the open parenthesis.
6381556Srgrimes	 */
6391556Srgrimes	orig_rpp = rpp;
6401556Srgrimes
641222165Sjilles	savecheckkwd = CHKALIAS;
642222165Sjilles
6431556Srgrimes	for (;;) {
644222165Sjilles		checkkwd = savecheckkwd;
6451556Srgrimes		if (readtoken() == TWORD) {
646255081Sjilles			n = makename();
6471556Srgrimes			*app = n;
6481556Srgrimes			app = &n->narg.next;
649222165Sjilles			if (savecheckkwd != 0 && !isassignment(wordtext))
650222165Sjilles				savecheckkwd = 0;
6511556Srgrimes		} else if (lasttoken == TREDIR) {
6521556Srgrimes			*rpp = n = redirnode;
6531556Srgrimes			rpp = &n->nfile.next;
6541556Srgrimes			parsefname();	/* read name of redirection file */
6551556Srgrimes		} else if (lasttoken == TLP && app == &args->narg.next
6561556Srgrimes					    && rpp == orig_rpp) {
6571556Srgrimes			/* We have a function */
658255073Sjilles			consumetoken(TRP);
659179022Sstefanf			funclinno = plinno;
660214291Sjilles			/*
661214291Sjilles			 * - Require plain text.
662214291Sjilles			 * - Functions with '/' cannot be called.
663214534Sjilles			 * - Reject name=().
664214534Sjilles			 * - Reject ksh extended glob patterns.
665214291Sjilles			 */
666214291Sjilles			if (!noexpand(n->narg.text) || quoteflag ||
667214534Sjilles			    strchr(n->narg.text, '/') ||
668214534Sjilles			    strchr("!%*+-=?@}~",
669214534Sjilles				n->narg.text[strlen(n->narg.text) - 1]))
6701556Srgrimes				synerror("Bad function name");
671214291Sjilles			rmescapes(n->narg.text);
672214304Sjilles			if (find_builtin(n->narg.text, &special) >= 0 &&
673214304Sjilles			    special)
674214304Sjilles				synerror("Cannot override a special builtin with a function");
6751556Srgrimes			n->type = NDEFUN;
6761556Srgrimes			n->narg.next = command();
677179022Sstefanf			funclinno = 0;
678210087Sjilles			return n;
6791556Srgrimes		} else {
6801556Srgrimes			tokpushback++;
6811556Srgrimes			break;
6821556Srgrimes		}
6831556Srgrimes	}
6841556Srgrimes	*app = NULL;
6851556Srgrimes	*rpp = NULL;
6861556Srgrimes	n = (union node *)stalloc(sizeof (struct ncmd));
6871556Srgrimes	n->type = NCMD;
6881556Srgrimes	n->ncmd.args = args;
6891556Srgrimes	n->ncmd.redirect = redir;
690210087Sjilles	return n;
6911556Srgrimes}
6921556Srgrimes
693213811Sobrienstatic union node *
69490111Simpmakename(void)
69590111Simp{
69617987Speter	union node *n;
6971556Srgrimes
69817987Speter	n = (union node *)stalloc(sizeof (struct narg));
69917987Speter	n->type = NARG;
70017987Speter	n->narg.next = NULL;
70117987Speter	n->narg.text = wordtext;
70217987Speter	n->narg.backquote = backquotelist;
70317987Speter	return n;
70417987Speter}
70517987Speter
706255085Sjillesstatic union node *
707255085Sjillesmakebinary(int type, union node *n1, union node *n2)
708255085Sjilles{
709255085Sjilles	union node *n;
710255085Sjilles
711255085Sjilles	n = (union node *)stalloc(sizeof (struct nbinary));
712255085Sjilles	n->type = type;
713255085Sjilles	n->nbinary.ch1 = n1;
714255085Sjilles	n->nbinary.ch2 = n2;
715255085Sjilles	return (n);
716255085Sjilles}
717255085Sjilles
718213760Sobrienvoid
719262951Sjmmvforcealias(void)
720262951Sjmmv{
721262951Sjmmv	checkkwd |= CHKALIAS;
722262951Sjmmv}
723262951Sjmmv
724262951Sjmmvvoid
725213760Sobrienfixredir(union node *n, const char *text, int err)
72690111Simp{
72717987Speter	TRACE(("Fix redir %s %d\n", text, err));
72817987Speter	if (!err)
72917987Speter		n->ndup.vname = NULL;
73017987Speter
73117987Speter	if (is_digit(text[0]) && text[1] == '\0')
73217987Speter		n->ndup.dupfd = digit_val(text[0]);
73317987Speter	else if (text[0] == '-' && text[1] == '\0')
73417987Speter		n->ndup.dupfd = -1;
73517987Speter	else {
73620425Ssteve
73717987Speter		if (err)
73817987Speter			synerror("Bad fd number");
73917987Speter		else
74017987Speter			n->ndup.vname = makename();
74117987Speter	}
74217987Speter}
74317987Speter
74417987Speter
745213811Sobrienstatic void
74690111Simpparsefname(void)
74790111Simp{
7481556Srgrimes	union node *n = redirnode;
7491556Srgrimes
750255073Sjilles	consumetoken(TWORD);
7511556Srgrimes	if (n->type == NHERE) {
7521556Srgrimes		struct heredoc *here = heredoc;
7531556Srgrimes		struct heredoc *p;
7541556Srgrimes		int i;
7551556Srgrimes
7561556Srgrimes		if (quoteflag == 0)
7571556Srgrimes			n->type = NXHERE;
7581556Srgrimes		TRACE(("Here document %d\n", n->type));
7591556Srgrimes		if (here->striptabs) {
7601556Srgrimes			while (*wordtext == '\t')
7611556Srgrimes				wordtext++;
7621556Srgrimes		}
7631556Srgrimes		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
7641556Srgrimes			synerror("Illegal eof marker for << redirection");
7651556Srgrimes		rmescapes(wordtext);
7661556Srgrimes		here->eofmark = wordtext;
7671556Srgrimes		here->next = NULL;
7681556Srgrimes		if (heredoclist == NULL)
7691556Srgrimes			heredoclist = here;
7701556Srgrimes		else {
7711556Srgrimes			for (p = heredoclist ; p->next ; p = p->next);
7721556Srgrimes			p->next = here;
7731556Srgrimes		}
7741556Srgrimes	} else if (n->type == NTOFD || n->type == NFROMFD) {
77517987Speter		fixredir(n, wordtext, 0);
7761556Srgrimes	} else {
77717987Speter		n->nfile.fname = makename();
7781556Srgrimes	}
7791556Srgrimes}
7801556Srgrimes
7811556Srgrimes
7821556Srgrimes/*
7831556Srgrimes * Input any here documents.
7841556Srgrimes */
7851556Srgrimes
786213811Sobrienstatic void
78790111Simpparseheredoc(void)
78890111Simp{
7891556Srgrimes	struct heredoc *here;
7901556Srgrimes	union node *n;
7911556Srgrimes
7921556Srgrimes	while (heredoclist) {
7931556Srgrimes		here = heredoclist;
7941556Srgrimes		heredoclist = here->next;
7951556Srgrimes		if (needprompt) {
7961556Srgrimes			setprompt(2);
7971556Srgrimes			needprompt = 0;
7981556Srgrimes		}
7991556Srgrimes		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
8001556Srgrimes				here->eofmark, here->striptabs);
801255081Sjilles		n = makename();
8021556Srgrimes		here->here->nhere.doc = n;
8031556Srgrimes	}
8041556Srgrimes}
8051556Srgrimes
806213811Sobrienstatic int
80790111Simppeektoken(void)
80890111Simp{
8091556Srgrimes	int t;
8101556Srgrimes
8111556Srgrimes	t = readtoken();
8121556Srgrimes	tokpushback++;
8131556Srgrimes	return (t);
8141556Srgrimes}
8151556Srgrimes
816213811Sobrienstatic int
81790111Simpreadtoken(void)
81890111Simp{
8191556Srgrimes	int t;
8201556Srgrimes	struct alias *ap;
8211556Srgrimes#ifdef DEBUG
8221556Srgrimes	int alreadyseen = tokpushback;
8231556Srgrimes#endif
8248855Srgrimes
8251556Srgrimes	top:
8261556Srgrimes	t = xxreadtoken();
8271556Srgrimes
828214709Sjilles	/*
829214709Sjilles	 * eat newlines
830214709Sjilles	 */
831214709Sjilles	if (checkkwd & CHKNL) {
832214709Sjilles		while (t == TNL) {
833214709Sjilles			parseheredoc();
834214709Sjilles			t = xxreadtoken();
835214709Sjilles		}
836214709Sjilles	}
8371556Srgrimes
838214709Sjilles	/*
839214709Sjilles	 * check for keywords and aliases
840214709Sjilles	 */
841214709Sjilles	if (t == TWORD && !quoteflag)
842214709Sjilles	{
843214709Sjilles		const char * const *pp;
844214709Sjilles
845214709Sjilles		if (checkkwd & CHKKWD)
84698463Sjmallett			for (pp = parsekwd; *pp; pp++) {
84720425Ssteve				if (**pp == *wordtext && equal(*pp, wordtext))
84817987Speter				{
8491556Srgrimes					lasttoken = t = pp - parsekwd + KWDOFFSET;
8501556Srgrimes					TRACE(("keyword %s recognized\n", tokname[t]));
8511556Srgrimes					goto out;
8521556Srgrimes				}
8531556Srgrimes			}
854214709Sjilles		if (checkkwd & CHKALIAS &&
855214709Sjilles		    (ap = lookupalias(wordtext, 1)) != NULL) {
856214709Sjilles			pushstring(ap->val, strlen(ap->val), ap);
857214709Sjilles			goto top;
8581556Srgrimes		}
859214709Sjilles	}
8601556Srgrimesout:
861214709Sjilles	if (t != TNOT)
862214709Sjilles		checkkwd = 0;
863214709Sjilles
8641556Srgrimes#ifdef DEBUG
8651556Srgrimes	if (!alreadyseen)
8661556Srgrimes	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8671556Srgrimes	else
8681556Srgrimes	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8691556Srgrimes#endif
8701556Srgrimes	return (t);
8711556Srgrimes}
8721556Srgrimes
8731556Srgrimes
8741556Srgrimes/*
8751556Srgrimes * Read the next input token.
8761556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in
8771556Srgrimes *	backquotes.  We set quoteflag to true if any part of the word was
8781556Srgrimes *	quoted.
8791556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing
8801556Srgrimes *	the redirection.
8811556Srgrimes * In all cases, the variable startlinno is set to the number of the line
8821556Srgrimes *	on which the token starts.
8831556Srgrimes *
8841556Srgrimes * [Change comment:  here documents and internal procedures]
8851556Srgrimes * [Readtoken shouldn't have any arguments.  Perhaps we should make the
8861556Srgrimes *  word parsing code into a separate routine.  In this case, readtoken
8871556Srgrimes *  doesn't need to have any internal procedures, but parseword does.
8881556Srgrimes *  We could also make parseoperator in essence the main routine, and
8891556Srgrimes *  have parseword (readtoken1?) handle both words and redirection.]
8901556Srgrimes */
8911556Srgrimes
8921556Srgrimes#define RETURN(token)	return lasttoken = token
8931556Srgrimes
894213811Sobrienstatic int
89590111Simpxxreadtoken(void)
89690111Simp{
89725230Ssteve	int c;
8981556Srgrimes
8991556Srgrimes	if (tokpushback) {
9001556Srgrimes		tokpushback = 0;
9011556Srgrimes		return lasttoken;
9021556Srgrimes	}
9031556Srgrimes	if (needprompt) {
9041556Srgrimes		setprompt(2);
9051556Srgrimes		needprompt = 0;
9061556Srgrimes	}
9071556Srgrimes	startlinno = plinno;
9081556Srgrimes	for (;;) {	/* until token or start of word found */
9091556Srgrimes		c = pgetc_macro();
9101556Srgrimes		switch (c) {
9111556Srgrimes		case ' ': case '\t':
9121556Srgrimes			continue;
9131556Srgrimes		case '#':
9141556Srgrimes			while ((c = pgetc()) != '\n' && c != PEOF);
9151556Srgrimes			pungetc();
9161556Srgrimes			continue;
9171556Srgrimes		case '\\':
9181556Srgrimes			if (pgetc() == '\n') {
9191556Srgrimes				startlinno = ++plinno;
9201556Srgrimes				if (doprompt)
9211556Srgrimes					setprompt(2);
9221556Srgrimes				else
9231556Srgrimes					setprompt(0);
9241556Srgrimes				continue;
9251556Srgrimes			}
9261556Srgrimes			pungetc();
9271556Srgrimes			goto breakloop;
9281556Srgrimes		case '\n':
9291556Srgrimes			plinno++;
9301556Srgrimes			needprompt = doprompt;
9311556Srgrimes			RETURN(TNL);
9321556Srgrimes		case PEOF:
9331556Srgrimes			RETURN(TEOF);
9341556Srgrimes		case '&':
9351556Srgrimes			if (pgetc() == '&')
9361556Srgrimes				RETURN(TAND);
9371556Srgrimes			pungetc();
9381556Srgrimes			RETURN(TBACKGND);
9391556Srgrimes		case '|':
9401556Srgrimes			if (pgetc() == '|')
9411556Srgrimes				RETURN(TOR);
9421556Srgrimes			pungetc();
9431556Srgrimes			RETURN(TPIPE);
9441556Srgrimes		case ';':
945223186Sjilles			c = pgetc();
946223186Sjilles			if (c == ';')
9471556Srgrimes				RETURN(TENDCASE);
948223186Sjilles			else if (c == '&')
949223186Sjilles				RETURN(TFALLTHRU);
9501556Srgrimes			pungetc();
9511556Srgrimes			RETURN(TSEMI);
9521556Srgrimes		case '(':
9531556Srgrimes			RETURN(TLP);
9541556Srgrimes		case ')':
9551556Srgrimes			RETURN(TRP);
9561556Srgrimes		default:
9571556Srgrimes			goto breakloop;
9581556Srgrimes		}
9591556Srgrimes	}
9601556Srgrimesbreakloop:
9611556Srgrimes	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
9621556Srgrimes#undef RETURN
9631556Srgrimes}
9641556Srgrimes
9651556Srgrimes
966213811Sobrien#define MAXNEST_static 8
967206145Sjillesstruct tokenstate
968206145Sjilles{
969206145Sjilles	const char *syntax; /* *SYNTAX */
970206145Sjilles	int parenlevel; /* levels of parentheses in arithmetic */
971206145Sjilles	enum tokenstate_category
972206145Sjilles	{
973206145Sjilles		TSTATE_TOP,
974206145Sjilles		TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
975206145Sjilles		TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
976206145Sjilles		TSTATE_ARITH
977206145Sjilles	} category;
978206145Sjilles};
979206145Sjilles
980206145Sjilles
981205130Sjilles/*
982205130Sjilles * Called to parse command substitutions.
983205130Sjilles */
9841556Srgrimes
985213811Sobrienstatic char *
986205130Sjillesparsebackq(char *out, struct nodelist **pbqlist,
987205130Sjilles		int oldstyle, int dblquote, int quoted)
988205130Sjilles{
989205130Sjilles	struct nodelist **nlpp;
990205130Sjilles	union node *n;
991205130Sjilles	char *volatile str;
992205130Sjilles	struct jmploc jmploc;
993205130Sjilles	struct jmploc *const savehandler = handler;
994248980Sjilles	size_t savelen;
995205130Sjilles	int saveprompt;
996205130Sjilles	const int bq_startlinno = plinno;
997205130Sjilles	char *volatile ostr = NULL;
998205130Sjilles	struct parsefile *const savetopfile = getcurrentfile();
999208655Sjilles	struct heredoc *const saveheredoclist = heredoclist;
1000208655Sjilles	struct heredoc *here;
1001205130Sjilles
1002205130Sjilles	str = NULL;
1003205130Sjilles	if (setjmp(jmploc.loc)) {
1004205130Sjilles		popfilesupto(savetopfile);
1005205130Sjilles		if (str)
1006205130Sjilles			ckfree(str);
1007205130Sjilles		if (ostr)
1008205130Sjilles			ckfree(ostr);
1009208655Sjilles		heredoclist = saveheredoclist;
1010205130Sjilles		handler = savehandler;
1011205130Sjilles		if (exception == EXERROR) {
1012205130Sjilles			startlinno = bq_startlinno;
1013205130Sjilles			synerror("Error in command substitution");
1014205130Sjilles		}
1015205130Sjilles		longjmp(handler->loc, 1);
1016205130Sjilles	}
1017205130Sjilles	INTOFF;
1018205130Sjilles	savelen = out - stackblock();
1019205130Sjilles	if (savelen > 0) {
1020205130Sjilles		str = ckmalloc(savelen);
1021205130Sjilles		memcpy(str, stackblock(), savelen);
1022205130Sjilles	}
1023205130Sjilles	handler = &jmploc;
1024208655Sjilles	heredoclist = NULL;
1025205130Sjilles	INTON;
1026205130Sjilles        if (oldstyle) {
1027205130Sjilles                /* We must read until the closing backquote, giving special
1028205130Sjilles                   treatment to some slashes, and then push the string and
1029205130Sjilles                   reread it as input, interpreting it normally.  */
1030205130Sjilles                char *oout;
1031205130Sjilles                int c;
1032205130Sjilles                int olen;
1033205130Sjilles
1034205130Sjilles
1035205130Sjilles                STARTSTACKSTR(oout);
1036205130Sjilles		for (;;) {
1037205130Sjilles			if (needprompt) {
1038205130Sjilles				setprompt(2);
1039205130Sjilles				needprompt = 0;
1040205130Sjilles			}
1041215783Sjilles			CHECKSTRSPACE(2, oout);
1042205130Sjilles			switch (c = pgetc()) {
1043205130Sjilles			case '`':
1044205130Sjilles				goto done;
1045205130Sjilles
1046205130Sjilles			case '\\':
1047205130Sjilles                                if ((c = pgetc()) == '\n') {
1048205130Sjilles					plinno++;
1049205130Sjilles					if (doprompt)
1050205130Sjilles						setprompt(2);
1051205130Sjilles					else
1052205130Sjilles						setprompt(0);
1053205130Sjilles					/*
1054205130Sjilles					 * If eating a newline, avoid putting
1055205130Sjilles					 * the newline into the new character
1056215783Sjilles					 * stream (via the USTPUTC after the
1057205130Sjilles					 * switch).
1058205130Sjilles					 */
1059205130Sjilles					continue;
1060205130Sjilles				}
1061205130Sjilles                                if (c != '\\' && c != '`' && c != '$'
1062205130Sjilles                                    && (!dblquote || c != '"'))
1063215783Sjilles                                        USTPUTC('\\', oout);
1064205130Sjilles				break;
1065205130Sjilles
1066205130Sjilles			case '\n':
1067205130Sjilles				plinno++;
1068205130Sjilles				needprompt = doprompt;
1069205130Sjilles				break;
1070205130Sjilles
1071205130Sjilles			case PEOF:
1072205130Sjilles			        startlinno = plinno;
1073205130Sjilles				synerror("EOF in backquote substitution");
1074205130Sjilles 				break;
1075205130Sjilles
1076205130Sjilles			default:
1077205130Sjilles				break;
1078205130Sjilles			}
1079215783Sjilles			USTPUTC(c, oout);
1080205130Sjilles                }
1081205130Sjillesdone:
1082215783Sjilles                USTPUTC('\0', oout);
1083205130Sjilles                olen = oout - stackblock();
1084205130Sjilles		INTOFF;
1085205130Sjilles		ostr = ckmalloc(olen);
1086205130Sjilles		memcpy(ostr, stackblock(), olen);
1087205130Sjilles		setinputstring(ostr, 1);
1088205130Sjilles		INTON;
1089205130Sjilles        }
1090205130Sjilles	nlpp = pbqlist;
1091205130Sjilles	while (*nlpp)
1092205130Sjilles		nlpp = &(*nlpp)->next;
1093205130Sjilles	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1094205130Sjilles	(*nlpp)->next = NULL;
1095205130Sjilles
1096205130Sjilles	if (oldstyle) {
1097205130Sjilles		saveprompt = doprompt;
1098205130Sjilles		doprompt = 0;
1099205130Sjilles	}
1100205130Sjilles
1101255087Sjilles	n = list(0);
1102205130Sjilles
1103255087Sjilles	if (oldstyle) {
1104255087Sjilles		if (peektoken() != TEOF)
1105255087Sjilles			synexpect(-1);
1106205130Sjilles		doprompt = saveprompt;
1107255087Sjilles	} else
1108255073Sjilles		consumetoken(TRP);
1109205130Sjilles
1110205130Sjilles	(*nlpp)->n = n;
1111205130Sjilles        if (oldstyle) {
1112205130Sjilles		/*
1113205130Sjilles		 * Start reading from old file again, ignoring any pushed back
1114205130Sjilles		 * tokens left from the backquote parsing
1115205130Sjilles		 */
1116205130Sjilles                popfile();
1117205130Sjilles		tokpushback = 0;
1118205130Sjilles	}
1119205130Sjilles	STARTSTACKSTR(out);
1120216706Sjilles	CHECKSTRSPACE(savelen + 1, out);
1121208655Sjilles	INTOFF;
1122205130Sjilles	if (str) {
1123205130Sjilles		memcpy(out, str, savelen);
1124205130Sjilles		STADJUST(savelen, out);
1125205130Sjilles		ckfree(str);
1126205130Sjilles		str = NULL;
1127205130Sjilles	}
1128205130Sjilles	if (ostr) {
1129205130Sjilles		ckfree(ostr);
1130205130Sjilles		ostr = NULL;
1131205130Sjilles	}
1132208655Sjilles	here = saveheredoclist;
1133208655Sjilles	if (here != NULL) {
1134208655Sjilles		while (here->next != NULL)
1135208655Sjilles			here = here->next;
1136208655Sjilles		here->next = heredoclist;
1137208655Sjilles		heredoclist = saveheredoclist;
1138208655Sjilles	}
1139205130Sjilles	handler = savehandler;
1140208655Sjilles	INTON;
1141205130Sjilles	if (quoted)
1142205130Sjilles		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1143205130Sjilles	else
1144205130Sjilles		USTPUTC(CTLBACKQ, out);
1145205130Sjilles	return out;
1146205130Sjilles}
1147205130Sjilles
1148205130Sjilles
11491556Srgrimes/*
1150221513Sjilles * Called to parse a backslash escape sequence inside $'...'.
1151221513Sjilles * The backslash has already been read.
1152221513Sjilles */
1153221513Sjillesstatic char *
1154221513Sjillesreadcstyleesc(char *out)
1155221513Sjilles{
1156221513Sjilles	int c, v, i, n;
1157221513Sjilles
1158221513Sjilles	c = pgetc();
1159221513Sjilles	switch (c) {
1160221513Sjilles	case '\0':
1161221513Sjilles		synerror("Unterminated quoted string");
1162221513Sjilles	case '\n':
1163221513Sjilles		plinno++;
1164221513Sjilles		if (doprompt)
1165221513Sjilles			setprompt(2);
1166221513Sjilles		else
1167221513Sjilles			setprompt(0);
1168221513Sjilles		return out;
1169221513Sjilles	case '\\':
1170221513Sjilles	case '\'':
1171221513Sjilles	case '"':
1172221513Sjilles		v = c;
1173221513Sjilles		break;
1174221513Sjilles	case 'a': v = '\a'; break;
1175221513Sjilles	case 'b': v = '\b'; break;
1176221513Sjilles	case 'e': v = '\033'; break;
1177221513Sjilles	case 'f': v = '\f'; break;
1178221513Sjilles	case 'n': v = '\n'; break;
1179221513Sjilles	case 'r': v = '\r'; break;
1180221513Sjilles	case 't': v = '\t'; break;
1181221513Sjilles	case 'v': v = '\v'; break;
1182221513Sjilles	case 'x':
1183221513Sjilles		  v = 0;
1184221513Sjilles		  for (;;) {
1185221513Sjilles			  c = pgetc();
1186221513Sjilles			  if (c >= '0' && c <= '9')
1187221513Sjilles				  v = (v << 4) + c - '0';
1188221513Sjilles			  else if (c >= 'A' && c <= 'F')
1189221513Sjilles				  v = (v << 4) + c - 'A' + 10;
1190221513Sjilles			  else if (c >= 'a' && c <= 'f')
1191221513Sjilles				  v = (v << 4) + c - 'a' + 10;
1192221513Sjilles			  else
1193221513Sjilles				  break;
1194221513Sjilles		  }
1195221513Sjilles		  pungetc();
1196221513Sjilles		  break;
1197221513Sjilles	case '0': case '1': case '2': case '3':
1198221513Sjilles	case '4': case '5': case '6': case '7':
1199221513Sjilles		  v = c - '0';
1200221513Sjilles		  c = pgetc();
1201221513Sjilles		  if (c >= '0' && c <= '7') {
1202221513Sjilles			  v <<= 3;
1203221513Sjilles			  v += c - '0';
1204221513Sjilles			  c = pgetc();
1205221513Sjilles			  if (c >= '0' && c <= '7') {
1206221513Sjilles				  v <<= 3;
1207221513Sjilles				  v += c - '0';
1208221513Sjilles			  } else
1209221513Sjilles				  pungetc();
1210221513Sjilles		  } else
1211221513Sjilles			  pungetc();
1212221513Sjilles		  break;
1213221513Sjilles	case 'c':
1214221513Sjilles		  c = pgetc();
1215221513Sjilles		  if (c < 0x3f || c > 0x7a || c == 0x60)
1216221513Sjilles			  synerror("Bad escape sequence");
1217221513Sjilles		  if (c == '\\' && pgetc() != '\\')
1218221513Sjilles			  synerror("Bad escape sequence");
1219221513Sjilles		  if (c == '?')
1220221513Sjilles			  v = 127;
1221221513Sjilles		  else
1222221513Sjilles			  v = c & 0x1f;
1223221513Sjilles		  break;
1224221513Sjilles	case 'u':
1225221513Sjilles	case 'U':
1226221513Sjilles		  n = c == 'U' ? 8 : 4;
1227221513Sjilles		  v = 0;
1228221513Sjilles		  for (i = 0; i < n; i++) {
1229221513Sjilles			  c = pgetc();
1230221513Sjilles			  if (c >= '0' && c <= '9')
1231221513Sjilles				  v = (v << 4) + c - '0';
1232221513Sjilles			  else if (c >= 'A' && c <= 'F')
1233221513Sjilles				  v = (v << 4) + c - 'A' + 10;
1234221513Sjilles			  else if (c >= 'a' && c <= 'f')
1235221513Sjilles				  v = (v << 4) + c - 'a' + 10;
1236221513Sjilles			  else
1237221513Sjilles				  synerror("Bad escape sequence");
1238221513Sjilles		  }
1239221513Sjilles		  if (v == 0 || (v >= 0xd800 && v <= 0xdfff))
1240221513Sjilles			  synerror("Bad escape sequence");
1241221513Sjilles		  /* We really need iconv here. */
1242221669Sjilles		  if (initial_localeisutf8 && v > 127) {
1243221669Sjilles			  CHECKSTRSPACE(4, out);
1244221669Sjilles			  /*
1245221669Sjilles			   * We cannot use wctomb() as the locale may have
1246221669Sjilles			   * changed.
1247221669Sjilles			   */
1248221669Sjilles			  if (v <= 0x7ff) {
1249221669Sjilles				  USTPUTC(0xc0 | v >> 6, out);
1250221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1251221669Sjilles				  return out;
1252221669Sjilles			  } else if (v <= 0xffff) {
1253221669Sjilles				  USTPUTC(0xe0 | v >> 12, out);
1254221669Sjilles				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1255221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1256221669Sjilles				  return out;
1257221669Sjilles			  } else if (v <= 0x10ffff) {
1258221669Sjilles				  USTPUTC(0xf0 | v >> 18, out);
1259221669Sjilles				  USTPUTC(0x80 | ((v >> 12) & 0x3f), out);
1260221669Sjilles				  USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1261221669Sjilles				  USTPUTC(0x80 | (v & 0x3f), out);
1262221669Sjilles				  return out;
1263221669Sjilles			  }
1264221669Sjilles		  }
1265221513Sjilles		  if (v > 127)
1266221513Sjilles			  v = '?';
1267221513Sjilles		  break;
1268221513Sjilles	default:
1269221513Sjilles		  synerror("Bad escape sequence");
1270221513Sjilles	}
1271221513Sjilles	v = (char)v;
1272221513Sjilles	/*
1273221513Sjilles	 * We can't handle NUL bytes.
1274221513Sjilles	 * POSIX says we should skip till the closing quote.
1275221513Sjilles	 */
1276221513Sjilles	if (v == '\0') {
1277221513Sjilles		while ((c = pgetc()) != '\'') {
1278221513Sjilles			if (c == '\\')
1279221513Sjilles				c = pgetc();
1280221513Sjilles			if (c == PEOF)
1281221513Sjilles				synerror("Unterminated quoted string");
1282221513Sjilles		}
1283221513Sjilles		pungetc();
1284221513Sjilles		return out;
1285221513Sjilles	}
1286221513Sjilles	if (SQSYNTAX[v] == CCTL)
1287221513Sjilles		USTPUTC(CTLESC, out);
1288221513Sjilles	USTPUTC(v, out);
1289221513Sjilles	return out;
1290221513Sjilles}
1291221513Sjilles
1292221513Sjilles
1293221513Sjilles/*
12941556Srgrimes * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
12951556Srgrimes * is not NULL, read a here document.  In the latter case, eofmark is the
12961556Srgrimes * word which marks the end of the document and striptabs is true if
12971556Srgrimes * leading tabs should be stripped from the document.  The argument firstc
12981556Srgrimes * is the first character of the input token or document.
12991556Srgrimes *
13001556Srgrimes * Because C does not have internal subroutines, I have simulated them
13011556Srgrimes * using goto's to implement the subroutine linkage.  The following macros
13021556Srgrimes * will run code that appears at the end of readtoken1.
13031556Srgrimes */
13041556Srgrimes
13051556Srgrimes#define CHECKEND()	{goto checkend; checkend_return:;}
13061556Srgrimes#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
13071556Srgrimes#define PARSESUB()	{goto parsesub; parsesub_return:;}
13081556Srgrimes#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
13091556Srgrimes
1310213811Sobrienstatic int
1311248980Sjillesreadtoken1(int firstc, char const *initialsyntax, const char *eofmark,
1312248980Sjilles    int striptabs)
131390111Simp{
131417987Speter	int c = firstc;
131517987Speter	char *out;
13161556Srgrimes	int len;
13171556Srgrimes	char line[EOFMARKLEN + 1];
13181556Srgrimes	struct nodelist *bqlist;
13191556Srgrimes	int quotef;
1320206145Sjilles	int newvarnest;
1321206145Sjilles	int level;
132254679Scracauer	int synentry;
1323213811Sobrien	struct tokenstate state_static[MAXNEST_static];
1324213811Sobrien	int maxnest = MAXNEST_static;
1325206145Sjilles	struct tokenstate *state = state_static;
1326221513Sjilles	int sqiscstyle = 0;
13271556Srgrimes
13281556Srgrimes	startlinno = plinno;
13291556Srgrimes	quotef = 0;
13301556Srgrimes	bqlist = NULL;
1331206145Sjilles	newvarnest = 0;
1332206145Sjilles	level = 0;
1333206145Sjilles	state[level].syntax = initialsyntax;
1334206145Sjilles	state[level].parenlevel = 0;
1335206145Sjilles	state[level].category = TSTATE_TOP;
13361556Srgrimes
13371556Srgrimes	STARTSTACKSTR(out);
13381556Srgrimes	loop: {	/* for each line, until end of word */
13391556Srgrimes		CHECKEND();	/* set c to PEOF if at end of here document */
13401556Srgrimes		for (;;) {	/* until end of line or end of word */
1341214512Sjilles			CHECKSTRSPACE(4, out);	/* permit 4 calls to USTPUTC */
134254679Scracauer
1343206145Sjilles			synentry = state[level].syntax[c];
134454679Scracauer
134554679Scracauer			switch(synentry) {
13461556Srgrimes			case CNL:	/* '\n' */
1347206145Sjilles				if (state[level].syntax == BASESYNTAX)
13481556Srgrimes					goto endword;	/* exit outer loop */
13491556Srgrimes				USTPUTC(c, out);
13501556Srgrimes				plinno++;
13511556Srgrimes				if (doprompt)
13521556Srgrimes					setprompt(2);
13531556Srgrimes				else
13541556Srgrimes					setprompt(0);
13551556Srgrimes				c = pgetc();
13561556Srgrimes				goto loop;		/* continue outer loop */
1357221513Sjilles			case CSBACK:
1358221513Sjilles				if (sqiscstyle) {
1359221513Sjilles					out = readcstyleesc(out);
1360221513Sjilles					break;
1361221513Sjilles				}
1362221513Sjilles				/* FALLTHROUGH */
13631556Srgrimes			case CWORD:
13641556Srgrimes				USTPUTC(c, out);
13651556Srgrimes				break;
13661556Srgrimes			case CCTL:
1367206145Sjilles				if (eofmark == NULL || initialsyntax != SQSYNTAX)
13681556Srgrimes					USTPUTC(CTLESC, out);
13691556Srgrimes				USTPUTC(c, out);
13701556Srgrimes				break;
13711556Srgrimes			case CBACK:	/* backslash */
13721556Srgrimes				c = pgetc();
13731556Srgrimes				if (c == PEOF) {
13741556Srgrimes					USTPUTC('\\', out);
13751556Srgrimes					pungetc();
13761556Srgrimes				} else if (c == '\n') {
1377160849Syar					plinno++;
13781556Srgrimes					if (doprompt)
13791556Srgrimes						setprompt(2);
13801556Srgrimes					else
13811556Srgrimes						setprompt(0);
13821556Srgrimes				} else {
1383206145Sjilles					if (state[level].syntax == DQSYNTAX &&
1384206145Sjilles					    c != '\\' && c != '`' && c != '$' &&
1385206145Sjilles					    (c != '"' || (eofmark != NULL &&
1386206145Sjilles						newvarnest == 0)) &&
1387206145Sjilles					    (c != '}' || state[level].category != TSTATE_VAR_OLD))
13881556Srgrimes						USTPUTC('\\', out);
1389214512Sjilles					if ((eofmark == NULL ||
1390214512Sjilles					    newvarnest > 0) &&
1391214512Sjilles					    state[level].syntax == BASESYNTAX)
1392214512Sjilles						USTPUTC(CTLQUOTEMARK, out);
139383675Stegge					if (SQSYNTAX[c] == CCTL)
13941556Srgrimes						USTPUTC(CTLESC, out);
13951556Srgrimes					USTPUTC(c, out);
1396214512Sjilles					if ((eofmark == NULL ||
1397214512Sjilles					    newvarnest > 0) &&
1398214512Sjilles					    state[level].syntax == BASESYNTAX &&
1399214512Sjilles					    state[level].category == TSTATE_VAR_OLD)
1400214512Sjilles						USTPUTC(CTLQUOTEEND, out);
14011556Srgrimes					quotef++;
14021556Srgrimes				}
14031556Srgrimes				break;
14041556Srgrimes			case CSQUOTE:
1405206145Sjilles				USTPUTC(CTLQUOTEMARK, out);
1406206145Sjilles				state[level].syntax = SQSYNTAX;
1407221513Sjilles				sqiscstyle = 0;
14081556Srgrimes				break;
14091556Srgrimes			case CDQUOTE:
1410206145Sjilles				USTPUTC(CTLQUOTEMARK, out);
1411206145Sjilles				state[level].syntax = DQSYNTAX;
14121556Srgrimes				break;
14131556Srgrimes			case CENDQUOTE:
1414206145Sjilles				if (eofmark != NULL && newvarnest == 0)
14151556Srgrimes					USTPUTC(c, out);
1416206145Sjilles				else {
1417214512Sjilles					if (state[level].category == TSTATE_VAR_OLD)
1418214512Sjilles						USTPUTC(CTLQUOTEEND, out);
1419214305Sjilles					state[level].syntax = BASESYNTAX;
14201556Srgrimes					quotef++;
14211556Srgrimes				}
14221556Srgrimes				break;
14231556Srgrimes			case CVAR:	/* '$' */
14241556Srgrimes				PARSESUB();		/* parse substitution */
14251556Srgrimes				break;
14261556Srgrimes			case CENDVAR:	/* '}' */
1427206145Sjilles				if (level > 0 &&
1428214492Sjilles				    ((state[level].category == TSTATE_VAR_OLD &&
1429214492Sjilles				      state[level].syntax ==
1430214492Sjilles				      state[level - 1].syntax) ||
1431214490Sjilles				    (state[level].category == TSTATE_VAR_NEW &&
1432214490Sjilles				     state[level].syntax == BASESYNTAX))) {
1433214492Sjilles					if (state[level].category == TSTATE_VAR_NEW)
1434206145Sjilles						newvarnest--;
1435206145Sjilles					level--;
14361556Srgrimes					USTPUTC(CTLENDVAR, out);
14371556Srgrimes				} else {
14381556Srgrimes					USTPUTC(c, out);
14391556Srgrimes				}
14401556Srgrimes				break;
14411556Srgrimes			case CLP:	/* '(' in arithmetic */
1442206145Sjilles				state[level].parenlevel++;
14431556Srgrimes				USTPUTC(c, out);
14441556Srgrimes				break;
14451556Srgrimes			case CRP:	/* ')' in arithmetic */
1446206145Sjilles				if (state[level].parenlevel > 0) {
14471556Srgrimes					USTPUTC(c, out);
1448206145Sjilles					--state[level].parenlevel;
14491556Srgrimes				} else {
14501556Srgrimes					if (pgetc() == ')') {
1451206145Sjilles						if (level > 0 &&
1452206145Sjilles						    state[level].category == TSTATE_ARITH) {
1453206145Sjilles							level--;
14541556Srgrimes							USTPUTC(CTLENDARI, out);
14551556Srgrimes						} else
14561556Srgrimes							USTPUTC(')', out);
14571556Srgrimes					} else {
14588855Srgrimes						/*
14591556Srgrimes						 * unbalanced parens
14601556Srgrimes						 *  (don't 2nd guess - no error)
14611556Srgrimes						 */
14621556Srgrimes						pungetc();
14631556Srgrimes						USTPUTC(')', out);
14641556Srgrimes					}
14651556Srgrimes				}
14661556Srgrimes				break;
14671556Srgrimes			case CBQUOTE:	/* '`' */
1468206145Sjilles				out = parsebackq(out, &bqlist, 1,
1469206145Sjilles				    state[level].syntax == DQSYNTAX &&
1470206145Sjilles				    (eofmark == NULL || newvarnest > 0),
1471206145Sjilles				    state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
14721556Srgrimes				break;
14731556Srgrimes			case CEOF:
14741556Srgrimes				goto endword;		/* exit outer loop */
1475214305Sjilles			case CIGN:
1476214305Sjilles				break;
14771556Srgrimes			default:
1478206145Sjilles				if (level == 0)
14791556Srgrimes					goto endword;	/* exit outer loop */
14801556Srgrimes				USTPUTC(c, out);
14811556Srgrimes			}
14821556Srgrimes			c = pgetc_macro();
14831556Srgrimes		}
14841556Srgrimes	}
14851556Srgrimesendword:
1486206145Sjilles	if (state[level].syntax == ARISYNTAX)
14871556Srgrimes		synerror("Missing '))'");
1488206145Sjilles	if (state[level].syntax != BASESYNTAX && eofmark == NULL)
14891556Srgrimes		synerror("Unterminated quoted string");
1490206145Sjilles	if (state[level].category == TSTATE_VAR_OLD ||
1491206145Sjilles	    state[level].category == TSTATE_VAR_NEW) {
14921556Srgrimes		startlinno = plinno;
14931556Srgrimes		synerror("Missing '}'");
14941556Srgrimes	}
1495206145Sjilles	if (state != state_static)
1496206145Sjilles		parser_temp_free_upto(state);
14971556Srgrimes	USTPUTC('\0', out);
14981556Srgrimes	len = out - stackblock();
14991556Srgrimes	out = stackblock();
15001556Srgrimes	if (eofmark == NULL) {
15011556Srgrimes		if ((c == '>' || c == '<')
15021556Srgrimes		 && quotef == 0
15031556Srgrimes		 && len <= 2
15041556Srgrimes		 && (*out == '\0' || is_digit(*out))) {
15051556Srgrimes			PARSEREDIR();
15061556Srgrimes			return lasttoken = TREDIR;
15071556Srgrimes		} else {
15081556Srgrimes			pungetc();
15091556Srgrimes		}
15101556Srgrimes	}
15111556Srgrimes	quoteflag = quotef;
15121556Srgrimes	backquotelist = bqlist;
15131556Srgrimes	grabstackblock(len);
15141556Srgrimes	wordtext = out;
15151556Srgrimes	return lasttoken = TWORD;
15161556Srgrimes/* end of readtoken routine */
15171556Srgrimes
15181556Srgrimes
15191556Srgrimes/*
15201556Srgrimes * Check to see whether we are at the end of the here document.  When this
15211556Srgrimes * is called, c is set to the first character of the next input line.  If
15221556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF.
15231556Srgrimes */
15241556Srgrimes
15251556Srgrimescheckend: {
15261556Srgrimes	if (eofmark) {
15271556Srgrimes		if (striptabs) {
15281556Srgrimes			while (c == '\t')
15291556Srgrimes				c = pgetc();
15301556Srgrimes		}
15311556Srgrimes		if (c == *eofmark) {
15321556Srgrimes			if (pfgets(line, sizeof line) != NULL) {
1533248980Sjilles				const char *p, *q;
15341556Srgrimes
15351556Srgrimes				p = line;
15361556Srgrimes				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1537222134Sjilles				if ((*p == '\0' || *p == '\n') && *q == '\0') {
15381556Srgrimes					c = PEOF;
1539222134Sjilles					if (*p == '\n') {
1540222134Sjilles						plinno++;
1541222134Sjilles						needprompt = doprompt;
1542222134Sjilles					}
15431556Srgrimes				} else {
15441556Srgrimes					pushstring(line, strlen(line), NULL);
15451556Srgrimes				}
15461556Srgrimes			}
15471556Srgrimes		}
15481556Srgrimes	}
15491556Srgrimes	goto checkend_return;
15501556Srgrimes}
15511556Srgrimes
15521556Srgrimes
15531556Srgrimes/*
15541556Srgrimes * Parse a redirection operator.  The variable "out" points to a string
15551556Srgrimes * specifying the fd to be redirected.  The variable "c" contains the
15561556Srgrimes * first character of the redirection operator.
15571556Srgrimes */
15581556Srgrimes
15591556Srgrimesparseredir: {
15601556Srgrimes	char fd = *out;
15611556Srgrimes	union node *np;
15621556Srgrimes
15631556Srgrimes	np = (union node *)stalloc(sizeof (struct nfile));
15641556Srgrimes	if (c == '>') {
15651556Srgrimes		np->nfile.fd = 1;
15661556Srgrimes		c = pgetc();
15671556Srgrimes		if (c == '>')
15681556Srgrimes			np->type = NAPPEND;
15691556Srgrimes		else if (c == '&')
15701556Srgrimes			np->type = NTOFD;
157196922Stjr		else if (c == '|')
157296922Stjr			np->type = NCLOBBER;
15731556Srgrimes		else {
15741556Srgrimes			np->type = NTO;
15751556Srgrimes			pungetc();
15761556Srgrimes		}
15771556Srgrimes	} else {	/* c == '<' */
15781556Srgrimes		np->nfile.fd = 0;
15791556Srgrimes		c = pgetc();
15801556Srgrimes		if (c == '<') {
15811556Srgrimes			if (sizeof (struct nfile) != sizeof (struct nhere)) {
15821556Srgrimes				np = (union node *)stalloc(sizeof (struct nhere));
15831556Srgrimes				np->nfile.fd = 0;
15841556Srgrimes			}
15851556Srgrimes			np->type = NHERE;
15861556Srgrimes			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
15871556Srgrimes			heredoc->here = np;
15881556Srgrimes			if ((c = pgetc()) == '-') {
15891556Srgrimes				heredoc->striptabs = 1;
15901556Srgrimes			} else {
15911556Srgrimes				heredoc->striptabs = 0;
15921556Srgrimes				pungetc();
15931556Srgrimes			}
15941556Srgrimes		} else if (c == '&')
15951556Srgrimes			np->type = NFROMFD;
159666612Sbrian		else if (c == '>')
159766612Sbrian			np->type = NFROMTO;
15981556Srgrimes		else {
15991556Srgrimes			np->type = NFROM;
16001556Srgrimes			pungetc();
16011556Srgrimes		}
16021556Srgrimes	}
16031556Srgrimes	if (fd != '\0')
16041556Srgrimes		np->nfile.fd = digit_val(fd);
16051556Srgrimes	redirnode = np;
16061556Srgrimes	goto parseredir_return;
16071556Srgrimes}
16081556Srgrimes
16091556Srgrimes
16101556Srgrimes/*
16111556Srgrimes * Parse a substitution.  At this point, we have read the dollar sign
16121556Srgrimes * and nothing else.
16131556Srgrimes */
16141556Srgrimes
16151556Srgrimesparsesub: {
1616179022Sstefanf	char buf[10];
16171556Srgrimes	int subtype;
16181556Srgrimes	int typeloc;
16191556Srgrimes	int flags;
16201556Srgrimes	char *p;
16211556Srgrimes	static const char types[] = "}-+?=";
1622179022Sstefanf	int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1623179022Sstefanf	int linno;
1624179387Sstefanf	int length;
1625219623Sjilles	int c1;
16261556Srgrimes
16271556Srgrimes	c = pgetc();
1628221513Sjilles	if (c == '(') {	/* $(command) or $((arith)) */
16291556Srgrimes		if (pgetc() == '(') {
16301556Srgrimes			PARSEARITH();
16311556Srgrimes		} else {
16321556Srgrimes			pungetc();
1633206145Sjilles			out = parsebackq(out, &bqlist, 0,
1634206145Sjilles			    state[level].syntax == DQSYNTAX &&
1635206145Sjilles			    (eofmark == NULL || newvarnest > 0),
1636206145Sjilles			    state[level].syntax == DQSYNTAX ||
1637206145Sjilles			    state[level].syntax == ARISYNTAX);
16381556Srgrimes		}
1639221513Sjilles	} else if (c == '{' || is_name(c) || is_special(c)) {
16401556Srgrimes		USTPUTC(CTLVAR, out);
16411556Srgrimes		typeloc = out - stackblock();
16421556Srgrimes		USTPUTC(VSNORMAL, out);
16431556Srgrimes		subtype = VSNORMAL;
1644179022Sstefanf		flags = 0;
16451556Srgrimes		if (c == '{') {
164618202Speter			bracketed_name = 1;
16471556Srgrimes			c = pgetc();
1648219623Sjilles			subtype = 0;
16491556Srgrimes		}
1650219623Sjillesvarname:
1651149026Sstefanf		if (!is_eof(c) && is_name(c)) {
1652179387Sstefanf			length = 0;
16531556Srgrimes			do {
16541556Srgrimes				STPUTC(c, out);
16551556Srgrimes				c = pgetc();
1656179387Sstefanf				length++;
1657149026Sstefanf			} while (!is_eof(c) && is_in_name(c));
1658179387Sstefanf			if (length == 6 &&
1659179387Sstefanf			    strncmp(out - length, "LINENO", length) == 0) {
1660179022Sstefanf				/* Replace the variable name with the
1661179022Sstefanf				 * current line number. */
1662179022Sstefanf				linno = plinno;
1663179022Sstefanf				if (funclinno != 0)
1664179022Sstefanf					linno -= funclinno - 1;
1665179022Sstefanf				snprintf(buf, sizeof(buf), "%d", linno);
1666179022Sstefanf				STADJUST(-6, out);
1667215783Sjilles				STPUTS(buf, out);
1668179022Sstefanf				flags |= VSLINENO;
1669179022Sstefanf			}
167018202Speter		} else if (is_digit(c)) {
167118202Speter			if (bracketed_name) {
167218202Speter				do {
167318202Speter					STPUTC(c, out);
167418202Speter					c = pgetc();
167518202Speter				} while (is_digit(c));
167618202Speter			} else {
167718202Speter				STPUTC(c, out);
167818202Speter				c = pgetc();
167918202Speter			}
1680219623Sjilles		} else if (is_special(c)) {
1681219623Sjilles			c1 = c;
1682219623Sjilles			c = pgetc();
1683219623Sjilles			if (subtype == 0 && c1 == '#') {
1684219623Sjilles				subtype = VSLENGTH;
1685219623Sjilles				if (strchr(types, c) == NULL && c != ':' &&
1686219623Sjilles				    c != '#' && c != '%')
1687219623Sjilles					goto varname;
1688219623Sjilles				c1 = c;
1689219623Sjilles				c = pgetc();
1690219623Sjilles				if (c1 != '}' && c == '}') {
1691219623Sjilles					pungetc();
1692219623Sjilles					c = c1;
1693219623Sjilles					goto varname;
1694219623Sjilles				}
1695219623Sjilles				pungetc();
1696219623Sjilles				c = c1;
1697219623Sjilles				c1 = '#';
1698219623Sjilles				subtype = 0;
1699219623Sjilles			}
1700219623Sjilles			USTPUTC(c1, out);
17011556Srgrimes		} else {
1702219623Sjilles			subtype = VSERROR;
1703219623Sjilles			if (c == '}')
1704219623Sjilles				pungetc();
1705219623Sjilles			else if (c == '\n' || c == PEOF)
1706219623Sjilles				synerror("Unexpected end of line in substitution");
1707287749Sjilles			else if (BASESYNTAX[c] != CCTL)
1708164003Sstefanf				USTPUTC(c, out);
17091556Srgrimes		}
17101556Srgrimes		if (subtype == 0) {
171117987Speter			switch (c) {
171217987Speter			case ':':
1713179022Sstefanf				flags |= VSNUL;
17141556Srgrimes				c = pgetc();
171517987Speter				/*FALLTHROUGH*/
171617987Speter			default:
171717987Speter				p = strchr(types, c);
1718164003Sstefanf				if (p == NULL) {
1719206144Sjilles					if (c == '\n' || c == PEOF)
1720206144Sjilles						synerror("Unexpected end of line in substitution");
1721164003Sstefanf					if (flags == VSNUL)
1722164003Sstefanf						STPUTC(':', out);
1723287749Sjilles					if (BASESYNTAX[c] != CCTL)
1724287749Sjilles						STPUTC(c, out);
1725164003Sstefanf					subtype = VSERROR;
1726164003Sstefanf				} else
1727164003Sstefanf					subtype = p - types + VSNORMAL;
172817987Speter				break;
172917987Speter			case '%':
173020425Ssteve			case '#':
173117987Speter				{
173217987Speter					int cc = c;
173317987Speter					subtype = c == '#' ? VSTRIMLEFT :
173417987Speter							     VSTRIMRIGHT;
173517987Speter					c = pgetc();
173617987Speter					if (c == cc)
173717987Speter						subtype++;
173817987Speter					else
173917987Speter						pungetc();
174017987Speter					break;
174117987Speter				}
17421556Srgrimes			}
1743164003Sstefanf		} else if (subtype != VSERROR) {
1744221461Sjilles			if (subtype == VSLENGTH && c != '}')
1745221461Sjilles				subtype = VSERROR;
17461556Srgrimes			pungetc();
17471556Srgrimes		}
1748164003Sstefanf		STPUTC('=', out);
1749220903Sjilles		if (state[level].syntax == DQSYNTAX ||
1750220903Sjilles		    state[level].syntax == ARISYNTAX)
17511556Srgrimes			flags |= VSQUOTE;
17521556Srgrimes		*(stackblock() + typeloc) = subtype | flags;
1753206145Sjilles		if (subtype != VSNORMAL) {
1754206145Sjilles			if (level + 1 >= maxnest) {
1755206145Sjilles				maxnest *= 2;
1756206145Sjilles				if (state == state_static) {
1757206145Sjilles					state = parser_temp_alloc(
1758206145Sjilles					    maxnest * sizeof(*state));
1759206145Sjilles					memcpy(state, state_static,
1760213811Sobrien					    MAXNEST_static * sizeof(*state));
1761206145Sjilles				} else
1762206145Sjilles					state = parser_temp_realloc(state,
1763206145Sjilles					    maxnest * sizeof(*state));
1764206145Sjilles			}
1765206145Sjilles			level++;
1766206145Sjilles			state[level].parenlevel = 0;
1767206145Sjilles			if (subtype == VSMINUS || subtype == VSPLUS ||
1768206145Sjilles			    subtype == VSQUESTION || subtype == VSASSIGN) {
1769206145Sjilles				/*
1770206145Sjilles				 * For operators that were in the Bourne shell,
1771206145Sjilles				 * inherit the double-quote state.
1772206145Sjilles				 */
1773206145Sjilles				state[level].syntax = state[level - 1].syntax;
1774206145Sjilles				state[level].category = TSTATE_VAR_OLD;
1775206145Sjilles			} else {
1776206145Sjilles				/*
1777206145Sjilles				 * The other operators take a pattern,
1778206145Sjilles				 * so go to BASESYNTAX.
1779206145Sjilles				 * Also, ' and " are now special, even
1780206145Sjilles				 * in here documents.
1781206145Sjilles				 */
1782206145Sjilles				state[level].syntax = BASESYNTAX;
1783206145Sjilles				state[level].category = TSTATE_VAR_NEW;
1784206145Sjilles				newvarnest++;
1785206145Sjilles			}
1786206145Sjilles		}
1787221513Sjilles	} else if (c == '\'' && state[level].syntax == BASESYNTAX) {
1788221513Sjilles		/* $'cstylequotes' */
1789221513Sjilles		USTPUTC(CTLQUOTEMARK, out);
1790221513Sjilles		state[level].syntax = SQSYNTAX;
1791221513Sjilles		sqiscstyle = 1;
1792221513Sjilles	} else {
1793221513Sjilles		USTPUTC('$', out);
1794221513Sjilles		pungetc();
17951556Srgrimes	}
17961556Srgrimes	goto parsesub_return;
17971556Srgrimes}
17981556Srgrimes
17991556Srgrimes
18001556Srgrimes/*
18011556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state)
18021556Srgrimes */
18031556Srgrimesparsearith: {
18041556Srgrimes
1805206145Sjilles	if (level + 1 >= maxnest) {
1806206145Sjilles		maxnest *= 2;
1807206145Sjilles		if (state == state_static) {
1808206145Sjilles			state = parser_temp_alloc(
1809206145Sjilles			    maxnest * sizeof(*state));
1810206145Sjilles			memcpy(state, state_static,
1811213811Sobrien			    MAXNEST_static * sizeof(*state));
1812206145Sjilles		} else
1813206145Sjilles			state = parser_temp_realloc(state,
1814206145Sjilles			    maxnest * sizeof(*state));
18151556Srgrimes	}
1816206145Sjilles	level++;
1817206145Sjilles	state[level].syntax = ARISYNTAX;
1818206145Sjilles	state[level].parenlevel = 0;
1819206145Sjilles	state[level].category = TSTATE_ARITH;
1820206145Sjilles	USTPUTC(CTLARI, out);
1821206145Sjilles	if (state[level - 1].syntax == DQSYNTAX)
1822206145Sjilles		USTPUTC('"',out);
1823206145Sjilles	else
1824206145Sjilles		USTPUTC(' ',out);
18251556Srgrimes	goto parsearith_return;
18261556Srgrimes}
18271556Srgrimes
18281556Srgrimes} /* end of readtoken */
18291556Srgrimes
18301556Srgrimes
18311556Srgrimes/*
18321556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs
18331556Srgrimes * or backquotes).
18341556Srgrimes */
18351556Srgrimes
1836213811Sobrienstatic int
183790111Simpnoexpand(char *text)
183890111Simp{
183925230Ssteve	char *p;
184025230Ssteve	char c;
18411556Srgrimes
18421556Srgrimes	p = text;
18431556Srgrimes	while ((c = *p++) != '\0') {
184439137Stegge		if ( c == CTLQUOTEMARK)
184539137Stegge			continue;
18461556Srgrimes		if (c == CTLESC)
18471556Srgrimes			p++;
184883675Stegge		else if (BASESYNTAX[(int)c] == CCTL)
18491556Srgrimes			return 0;
18501556Srgrimes	}
18511556Srgrimes	return 1;
18521556Srgrimes}
18531556Srgrimes
18541556Srgrimes
18551556Srgrimes/*
18561556Srgrimes * Return true if the argument is a legal variable name (a letter or
18571556Srgrimes * underscore followed by zero or more letters, underscores, and digits).
18581556Srgrimes */
18591556Srgrimes
18601556Srgrimesint
1861200956Sjillesgoodname(const char *name)
186290111Simp{
1863200956Sjilles	const char *p;
18641556Srgrimes
18651556Srgrimes	p = name;
18661556Srgrimes	if (! is_name(*p))
18671556Srgrimes		return 0;
18681556Srgrimes	while (*++p) {
18691556Srgrimes		if (! is_in_name(*p))
18701556Srgrimes			return 0;
18711556Srgrimes	}
18721556Srgrimes	return 1;
18731556Srgrimes}
18741556Srgrimes
18751556Srgrimes
1876222165Sjillesint
1877222165Sjillesisassignment(const char *p)
1878222165Sjilles{
1879222165Sjilles	if (!is_name(*p))
1880222165Sjilles		return 0;
1881222165Sjilles	p++;
1882222165Sjilles	for (;;) {
1883222165Sjilles		if (*p == '=')
1884222165Sjilles			return 1;
1885222165Sjilles		else if (!is_in_name(*p))
1886222165Sjilles			return 0;
1887222165Sjilles		p++;
1888222165Sjilles	}
1889222165Sjilles}
1890222165Sjilles
1891222165Sjilles
1892255073Sjillesstatic void
1893255073Sjillesconsumetoken(int token)
1894255073Sjilles{
1895255073Sjilles	if (readtoken() != token)
1896255073Sjilles		synexpect(token);
1897255073Sjilles}
1898255073Sjilles
1899255073Sjilles
19001556Srgrimes/*
19011556Srgrimes * Called when an unexpected token is read during the parse.  The argument
19021556Srgrimes * is the token that is expected, or -1 if more than one type of token can
19031556Srgrimes * occur at this point.
19041556Srgrimes */
19051556Srgrimes
1906213811Sobrienstatic void
190790111Simpsynexpect(int token)
190817987Speter{
19091556Srgrimes	char msg[64];
19101556Srgrimes
19111556Srgrimes	if (token >= 0) {
19121556Srgrimes		fmtstr(msg, 64, "%s unexpected (expecting %s)",
19131556Srgrimes			tokname[lasttoken], tokname[token]);
19141556Srgrimes	} else {
19151556Srgrimes		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
19161556Srgrimes	}
19171556Srgrimes	synerror(msg);
19181556Srgrimes}
19191556Srgrimes
19201556Srgrimes
1921213811Sobrienstatic void
1922201053Sjillessynerror(const char *msg)
192390111Simp{
19241556Srgrimes	if (commandname)
1925201366Sjilles		outfmt(out2, "%s: %d: ", commandname, startlinno);
1926201366Sjilles	outfmt(out2, "Syntax error: %s\n", msg);
19271556Srgrimes	error((char *)NULL);
19281556Srgrimes}
19291556Srgrimes
1930213811Sobrienstatic void
193190111Simpsetprompt(int which)
193290111Simp{
19331556Srgrimes	whichprompt = which;
19341556Srgrimes
193517987Speter#ifndef NO_HISTORY
19361556Srgrimes	if (!el)
193717987Speter#endif
1938199629Sjilles	{
19391556Srgrimes		out2str(getprompt(NULL));
1940199629Sjilles		flushout(out2);
1941199629Sjilles	}
19421556Srgrimes}
19431556Srgrimes
19441556Srgrimes/*
19451556Srgrimes * called by editline -- any expansions to the prompt
19461556Srgrimes *    should be added here.
19471556Srgrimes */
19481556Srgrimeschar *
194990111Simpgetprompt(void *unused __unused)
195025905Ssteve{
1951142845Sobrien	static char ps[PROMPTLEN];
1952142845Sobrien	char *fmt;
1953209653Sjilles	const char *pwd;
1954209653Sjilles	int i, trim;
1955214538Sjilles	static char internal_error[] = "??";
1956142845Sobrien
1957142845Sobrien	/*
1958142845Sobrien	 * Select prompt format.
1959142845Sobrien	 */
19601556Srgrimes	switch (whichprompt) {
19611556Srgrimes	case 0:
1962201053Sjilles		fmt = nullstr;
1963142845Sobrien		break;
19641556Srgrimes	case 1:
1965142845Sobrien		fmt = ps1val();
1966142845Sobrien		break;
19671556Srgrimes	case 2:
1968142845Sobrien		fmt = ps2val();
1969142845Sobrien		break;
19701556Srgrimes	default:
1971201053Sjilles		return internal_error;
19721556Srgrimes	}
1973142845Sobrien
1974142845Sobrien	/*
1975142845Sobrien	 * Format prompt string.
1976142845Sobrien	 */
1977301140Struckman	for (i = 0; (i < PROMPTLEN - 1) && (*fmt != '\0'); i++, fmt++)
1978142845Sobrien		if (*fmt == '\\')
1979142845Sobrien			switch (*++fmt) {
1980142845Sobrien
1981142845Sobrien				/*
1982142845Sobrien				 * Hostname.
1983142845Sobrien				 *
1984142845Sobrien				 * \h specifies just the local hostname,
1985142845Sobrien				 * \H specifies fully-qualified hostname.
1986142845Sobrien				 */
1987142845Sobrien			case 'h':
1988142845Sobrien			case 'H':
1989149024Sstefanf				ps[i] = '\0';
1990301140Struckman				gethostname(&ps[i], PROMPTLEN - i - 1);
1991301140Struckman				ps[PROMPTLEN - 1] = '\0';
1992142845Sobrien				/* Skip to end of hostname. */
1993142845Sobrien				trim = (*fmt == 'h') ? '.' : '\0';
1994299487Svangyzen				while ((ps[i] != '\0') && (ps[i] != trim))
1995142845Sobrien					i++;
1996299487Svangyzen				--i;
1997142845Sobrien				break;
1998142845Sobrien
1999142845Sobrien				/*
2000142845Sobrien				 * Working directory.
2001142845Sobrien				 *
2002142845Sobrien				 * \W specifies just the final component,
2003142845Sobrien				 * \w specifies the entire path.
2004142845Sobrien				 */
2005142845Sobrien			case 'W':
2006142845Sobrien			case 'w':
2007209653Sjilles				pwd = lookupvar("PWD");
2008299487Svangyzen				if (pwd == NULL || *pwd == '\0')
2009209653Sjilles					pwd = "?";
2010209653Sjilles				if (*fmt == 'W' &&
2011209653Sjilles				    *pwd == '/' && pwd[1] != '\0')
2012209653Sjilles					strlcpy(&ps[i], strrchr(pwd, '/') + 1,
2013209653Sjilles					    PROMPTLEN - i);
2014209653Sjilles				else
2015209653Sjilles					strlcpy(&ps[i], pwd, PROMPTLEN - i);
2016142845Sobrien				/* Skip to end of path. */
2017142845Sobrien				while (ps[i + 1] != '\0')
2018142845Sobrien					i++;
2019142845Sobrien				break;
2020142845Sobrien
2021142845Sobrien				/*
2022142845Sobrien				 * Superuser status.
2023142845Sobrien				 *
2024142845Sobrien				 * '$' for normal users, '#' for root.
2025142845Sobrien				 */
2026142845Sobrien			case '$':
2027142845Sobrien				ps[i] = (geteuid() != 0) ? '$' : '#';
2028142845Sobrien				break;
2029142845Sobrien
2030142845Sobrien				/*
2031142845Sobrien				 * A literal \.
2032142845Sobrien				 */
2033142845Sobrien			case '\\':
2034142845Sobrien				ps[i] = '\\';
2035142845Sobrien				break;
2036142845Sobrien
2037142845Sobrien				/*
2038142845Sobrien				 * Emit unrecognized formats verbatim.
2039142845Sobrien				 */
2040142845Sobrien			default:
2041301140Struckman				ps[i] = '\\';
2042301571Struckman				if (i < PROMPTLEN - 2)
2043301140Struckman					ps[++i] = *fmt;
2044142845Sobrien				break;
2045142845Sobrien			}
2046142845Sobrien		else
2047142845Sobrien			ps[i] = *fmt;
2048142845Sobrien	ps[i] = '\0';
2049142845Sobrien	return (ps);
20501556Srgrimes}
2051222907Sjilles
2052222907Sjilles
2053222907Sjillesconst char *
2054248980Sjillesexpandstr(const char *ps)
2055222907Sjilles{
2056222907Sjilles	union node n;
2057222907Sjilles	struct jmploc jmploc;
2058222907Sjilles	struct jmploc *const savehandler = handler;
2059222907Sjilles	const int saveprompt = doprompt;
2060222907Sjilles	struct parsefile *const savetopfile = getcurrentfile();
2061222907Sjilles	struct parser_temp *const saveparser_temp = parser_temp;
2062222907Sjilles	const char *result = NULL;
2063222907Sjilles
2064222907Sjilles	if (!setjmp(jmploc.loc)) {
2065222907Sjilles		handler = &jmploc;
2066222907Sjilles		parser_temp = NULL;
2067222907Sjilles		setinputstring(ps, 1);
2068222907Sjilles		doprompt = 0;
2069222907Sjilles		readtoken1(pgetc(), DQSYNTAX, "\n\n", 0);
2070222907Sjilles		if (backquotelist != NULL)
2071222907Sjilles			error("Command substitution not allowed here");
2072222907Sjilles
2073222907Sjilles		n.narg.type = NARG;
2074222907Sjilles		n.narg.next = NULL;
2075222907Sjilles		n.narg.text = wordtext;
2076222907Sjilles		n.narg.backquote = backquotelist;
2077222907Sjilles
2078222907Sjilles		expandarg(&n, NULL, 0);
2079222907Sjilles		result = stackblock();
2080222907Sjilles		INTOFF;
2081222907Sjilles	}
2082222907Sjilles	handler = savehandler;
2083222907Sjilles	doprompt = saveprompt;
2084222907Sjilles	popfilesupto(savetopfile);
2085222907Sjilles	if (parser_temp != saveparser_temp) {
2086222907Sjilles		parser_temp_free_all();
2087222907Sjilles		parser_temp = saveparser_temp;
2088222907Sjilles	}
2089222907Sjilles	if (result != NULL) {
2090222907Sjilles		INTON;
2091222907Sjilles	} else if (exception == EXINT)
2092222907Sjilles		raise(SIGINT);
2093222907Sjilles	return result;
2094222907Sjilles}
2095