parser.c revision 206145
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
341556Srgrimes#if 0
353044Sdgstatic char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
3621352Ssteve#endif
371556Srgrimes#endif /* not lint */
381556Srgrimes#include <sys/cdefs.h>
391556Srgrimes__FBSDID("$FreeBSD: head/bin/sh/parser.c 206145 2010-04-03 20:55:56Z jilles $");
4020425Ssteve
411556Srgrimes#include <stdlib.h>
421556Srgrimes#include <unistd.h>
4317987Speter
4417987Speter#include "shell.h"
4517987Speter#include "parser.h"
4617987Speter#include "nodes.h"
4717987Speter#include "expand.h"	/* defines rmescapes() */
4817987Speter#include "syntax.h"
4917987Speter#include "options.h"
5017987Speter#include "input.h"
5117987Speter#include "output.h"
5217987Speter#include "var.h"
5317987Speter#include "error.h"
5417987Speter#include "memalloc.h"
5518018Speter#include "mystring.h"
5617987Speter#include "alias.h"
571556Srgrimes#include "show.h"
581556Srgrimes#include "eval.h"
5917987Speter#ifndef NO_HISTORY
601556Srgrimes#include "myhistedit.h"
6117987Speter#endif
6217987Speter
6317987Speter/*
641556Srgrimes * Shell command parser.
651556Srgrimes */
6617987Speter
6717987Speter#define	EOFMARKLEN	79
681556Srgrimes#define	PROMPTLEN	128
691556Srgrimes
701556Srgrimes/* values returned by readtoken */
711556Srgrimes#include "token.h"
721556Srgrimes
731556Srgrimes
741556Srgrimes
751556Srgrimesstruct heredoc {
761556Srgrimes	struct heredoc *next;	/* next here document in list */
771556Srgrimes	union node *here;		/* redirection node */
781556Srgrimes	char *eofmark;		/* string indicating end of input */
791556Srgrimes	int striptabs;		/* if set, strip leading tabs */
801556Srgrimes};
811556Srgrimes
821556Srgrimesstruct parser_temp {
831556Srgrimes	struct parser_temp *next;
841556Srgrimes	void *data;
851556Srgrimes};
861556Srgrimes
871556Srgrimes
881556SrgrimesSTATIC struct heredoc *heredoclist;	/* list of here documents to read */
891556SrgrimesSTATIC int doprompt;		/* if set, prompt the user */
9020425SsteveSTATIC int needprompt;		/* true if interactive and at start of line */
9117987SpeterSTATIC int lasttoken;		/* last token read */
9220425SsteveMKINIT int tokpushback;		/* last token pushed back */
9317987SpeterSTATIC char *wordtext;		/* text of last word returned by readtoken */
9417987SpeterMKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
9517987SpeterSTATIC struct nodelist *backquotelist;
9620425SsteveSTATIC union node *redirnode;
9717987SpeterSTATIC struct heredoc *heredoc;
9820425SsteveSTATIC int quoteflag;		/* set if (part of) last token was quoted */
9917987SpeterSTATIC int startlinno;		/* line # where last token started */
10017987SpeterSTATIC int funclinno;		/* line # where the current function started */
10117987SpeterSTATIC struct parser_temp *parser_temp;
1021556Srgrimes
1031556Srgrimes/* XXX When 'noaliases' is set to one, no alias expansion takes place. */
1041556Srgrimesstatic int noaliases = 0;
1051556Srgrimes
1061556Srgrimes
1071556SrgrimesSTATIC union node *list(int);
1081556SrgrimesSTATIC union node *andor(void);
1091556SrgrimesSTATIC union node *pipeline(void);
1101556SrgrimesSTATIC union node *command(void);
1111556SrgrimesSTATIC union node *simplecmd(union node **, union node *);
1121556SrgrimesSTATIC union node *makename(void);
1131556SrgrimesSTATIC void parsefname(void);
11420425SsteveSTATIC void parseheredoc(void);
1151556SrgrimesSTATIC int peektoken(void);
11620425SsteveSTATIC int readtoken(void);
11717987SpeterSTATIC int xxreadtoken(void);
11817987SpeterSTATIC int readtoken1(int, char const *, char *, int);
1191556SrgrimesSTATIC int noexpand(char *);
1201556SrgrimesSTATIC void synexpect(int);
1211556SrgrimesSTATIC void synerror(const char *);
1221556SrgrimesSTATIC void setprompt(int);
1231556Srgrimes
1241556Srgrimes
1251556SrgrimesSTATIC void *
1261556Srgrimesparser_temp_alloc(size_t len)
12720425Ssteve{
1281556Srgrimes	struct parser_temp *t;
12920425Ssteve
13020425Ssteve	INTOFF;
13120425Ssteve	t = ckmalloc(sizeof(*t));
13220425Ssteve	t->data = NULL;
1331556Srgrimes	t->next = parser_temp;
1341556Srgrimes	parser_temp = t;
1351556Srgrimes	t->data = ckmalloc(len);
1361556Srgrimes	INTON;
1371556Srgrimes	return t->data;
13817987Speter}
13917987Speter
1401556Srgrimes
1411556SrgrimesSTATIC void *
1421556Srgrimesparser_temp_realloc(void *ptr, size_t len)
1431556Srgrimes{
1441556Srgrimes	struct parser_temp *t;
1451556Srgrimes
1461556Srgrimes	INTOFF;
1471556Srgrimes	t = parser_temp;
1481556Srgrimes	if (ptr != t->data)
1491556Srgrimes		error("bug: parser_temp_realloc misused");
1501556Srgrimes	t->data = ckrealloc(t->data, len);
1511556Srgrimes	INTON;
1521556Srgrimes	return t->data;
1531556Srgrimes}
15417987Speter
15520425Ssteve
1561556SrgrimesSTATIC void
15720425Ssteveparser_temp_free_upto(void *ptr)
15820425Ssteve{
15920425Ssteve	struct parser_temp *t;
1601556Srgrimes	int done = 0;
16117987Speter
16220425Ssteve	INTOFF;
1631556Srgrimes	while (parser_temp != NULL && !done) {
16420425Ssteve		t = parser_temp;
16520425Ssteve		parser_temp = t->next;
16620425Ssteve		done = t->data == ptr;
1671556Srgrimes		ckfree(t->data);
1681556Srgrimes		ckfree(t);
1691556Srgrimes	}
1701556Srgrimes	INTON;
1711556Srgrimes	if (!done)
1721556Srgrimes		error("bug: parser_temp_free_upto misused");
17320425Ssteve}
1741556Srgrimes
1751556Srgrimes
1761556SrgrimesSTATIC void
17717987Speterparser_temp_free_all(void)
1781556Srgrimes{
1791556Srgrimes	struct parser_temp *t;
1801556Srgrimes
1811556Srgrimes	INTOFF;
1821556Srgrimes	while (parser_temp != NULL) {
1831556Srgrimes		t = parser_temp;
1841556Srgrimes		parser_temp = t->next;
1851556Srgrimes		ckfree(t->data);
1861556Srgrimes		ckfree(t);
1871556Srgrimes	}
1881556Srgrimes	INTON;
1891556Srgrimes}
1901556Srgrimes
19117987Speter
19217987Speter/*
19317987Speter * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
19420425Ssteve * valid parse tree indicating a blank line.)
19517987Speter */
1961556Srgrimes
1971556Srgrimesunion node *
1981556Srgrimesparsecmd(int interact)
1991556Srgrimes{
2001556Srgrimes	int t;
2011556Srgrimes
2021556Srgrimes	/* This assumes the parser is not re-entered,
2031556Srgrimes	 * which could happen if we add command substitution on PS1/PS2.
20420425Ssteve	 */
2051556Srgrimes	parser_temp_free_all();
20620425Ssteve
20720425Ssteve	tokpushback = 0;
20820425Ssteve	doprompt = interact;
2091556Srgrimes	if (doprompt)
2101556Srgrimes		setprompt(1);
2111556Srgrimes	else
2121556Srgrimes		setprompt(0);
2131556Srgrimes	needprompt = 0;
2141556Srgrimes	t = readtoken();
2151556Srgrimes	if (t == TEOF)
2161556Srgrimes		return NEOF;
21717987Speter	if (t == TNL)
21817987Speter		return NULL;
21917987Speter	tokpushback++;
22020425Ssteve	return list(1);
22117987Speter}
2221556Srgrimes
2231556Srgrimes
2241556SrgrimesSTATIC union node *
2251556Srgrimeslist(int nlflag)
2261556Srgrimes{
2271556Srgrimes	union node *n1, *n2, *n3;
2281556Srgrimes	int tok;
2291556Srgrimes
2301556Srgrimes	checkkwd = 2;
2311556Srgrimes	if (nlflag == 0 && tokendlist[peektoken()])
2321556Srgrimes		return NULL;
2331556Srgrimes	n1 = NULL;
2341556Srgrimes	for (;;) {
2351556Srgrimes		n2 = andor();
2361556Srgrimes		tok = readtoken();
23717987Speter		if (tok == TBACKGND) {
2381556Srgrimes			if (n2->type == NCMD || n2->type == NPIPE) {
2391556Srgrimes				n2->ncmd.backgnd = 1;
2401556Srgrimes			} else if (n2->type == NREDIR) {
2411556Srgrimes				n2->type = NBACKGND;
2421556Srgrimes			} else {
2431556Srgrimes				n3 = (union node *)stalloc(sizeof (struct nredir));
2441556Srgrimes				n3->type = NBACKGND;
2451556Srgrimes				n3->nredir.n = n2;
2461556Srgrimes				n3->nredir.redirect = NULL;
2471556Srgrimes				n2 = n3;
2481556Srgrimes			}
2491556Srgrimes		}
2501556Srgrimes		if (n1 == NULL) {
2511556Srgrimes			n1 = n2;
2521556Srgrimes		}
2531556Srgrimes		else {
2541556Srgrimes			n3 = (union node *)stalloc(sizeof (struct nbinary));
2551556Srgrimes			n3->type = NSEMI;
2561556Srgrimes			n3->nbinary.ch1 = n1;
25717987Speter			n3->nbinary.ch2 = n2;
25817987Speter			n1 = n3;
25920425Ssteve		}
26017987Speter		switch (tok) {
2611556Srgrimes		case TBACKGND:
2621556Srgrimes		case TSEMI:
2631556Srgrimes			tok = readtoken();
2641556Srgrimes			/* FALLTHROUGH */
2651556Srgrimes		case TNL:
2661556Srgrimes			if (tok == TNL) {
2671556Srgrimes				parseheredoc();
2681556Srgrimes				if (nlflag)
2691556Srgrimes					return n1;
2701556Srgrimes			} else {
2711556Srgrimes				tokpushback++;
2721556Srgrimes			}
2731556Srgrimes			checkkwd = 2;
2741556Srgrimes			if (tokendlist[peektoken()])
2751556Srgrimes				return n1;
27620425Ssteve			break;
27717987Speter		case TEOF:
27817987Speter			if (heredoclist)
2791556Srgrimes				parseheredoc();
2801556Srgrimes			else
2811556Srgrimes				pungetc();		/* push back EOF on input */
2821556Srgrimes			return n1;
2831556Srgrimes		default:
2841556Srgrimes			if (nlflag)
2851556Srgrimes				synexpect(-1);
2861556Srgrimes			tokpushback++;
2871556Srgrimes			return n1;
2881556Srgrimes		}
2891556Srgrimes	}
2901556Srgrimes}
2911556Srgrimes
2921556Srgrimes
2931556Srgrimes
2941556SrgrimesSTATIC union node *
2951556Srgrimesandor(void)
2961556Srgrimes{
2971556Srgrimes	union node *n1, *n2, *n3;
2981556Srgrimes	int t;
2991556Srgrimes
3001556Srgrimes	n1 = pipeline();
3011556Srgrimes	for (;;) {
3021556Srgrimes		if ((t = readtoken()) == TAND) {
3031556Srgrimes			t = NAND;
3041556Srgrimes		} else if (t == TOR) {
3051556Srgrimes			t = NOR;
3061556Srgrimes		} else {
3071556Srgrimes			tokpushback++;
3081556Srgrimes			return n1;
3091556Srgrimes		}
3101556Srgrimes		n2 = pipeline();
3111556Srgrimes		n3 = (union node *)stalloc(sizeof (struct nbinary));
3121556Srgrimes		n3->type = t;
3131556Srgrimes		n3->nbinary.ch1 = n1;
3141556Srgrimes		n3->nbinary.ch2 = n2;
3151556Srgrimes		n1 = n3;
3161556Srgrimes	}
31717987Speter}
31817987Speter
3191556Srgrimes
3201556Srgrimes
3211556SrgrimesSTATIC union node *
3221556Srgrimespipeline(void)
3231556Srgrimes{
3241556Srgrimes	union node *n1, *n2, *pipenode;
3251556Srgrimes	struct nodelist *lp, *prev;
3261556Srgrimes	int negate;
3271556Srgrimes
3281556Srgrimes	negate = 0;
3291556Srgrimes	checkkwd = 2;
3301556Srgrimes	TRACE(("pipeline: entered\n"));
3311556Srgrimes	while (readtoken() == TNOT)
3321556Srgrimes		negate = !negate;
3331556Srgrimes	tokpushback++;
3341556Srgrimes	n1 = command();
3351556Srgrimes	if (readtoken() == TPIPE) {
3361556Srgrimes		pipenode = (union node *)stalloc(sizeof (struct npipe));
3371556Srgrimes		pipenode->type = NPIPE;
3381556Srgrimes		pipenode->npipe.backgnd = 0;
3391556Srgrimes		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
3401556Srgrimes		pipenode->npipe.cmdlist = lp;
3411556Srgrimes		lp->n = n1;
3421556Srgrimes		do {
3431556Srgrimes			prev = lp;
3441556Srgrimes			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
3451556Srgrimes			lp->n = command();
3461556Srgrimes			prev->next = lp;
3471556Srgrimes		} while (readtoken() == TPIPE);
3481556Srgrimes		lp->next = NULL;
3491556Srgrimes		n1 = pipenode;
3501556Srgrimes	}
3511556Srgrimes	tokpushback++;
3521556Srgrimes	if (negate) {
3531556Srgrimes		n2 = (union node *)stalloc(sizeof (struct nnot));
3541556Srgrimes		n2->type = NNOT;
3551556Srgrimes		n2->nnot.com = n1;
3561556Srgrimes		return n2;
3571556Srgrimes	} else
3581556Srgrimes		return n1;
3591556Srgrimes}
3601556Srgrimes
3611556Srgrimes
3621556Srgrimes
3631556SrgrimesSTATIC union node *
3641556Srgrimescommand(void)
3651556Srgrimes{
3661556Srgrimes	union node *n1, *n2;
3671556Srgrimes	union node *ap, **app;
3681556Srgrimes	union node *cp, **cpp;
3691556Srgrimes	union node *redir, **rpp;
3701556Srgrimes	int t, negate = 0;
3711556Srgrimes
37220425Ssteve	checkkwd = 2;
37317987Speter	redir = NULL;
37420425Ssteve	n1 = NULL;
37517987Speter	rpp = &redir;
3761556Srgrimes
3771556Srgrimes	/* Check for redirection which may precede command */
3781556Srgrimes	while (readtoken() == TREDIR) {
3791556Srgrimes		*rpp = n2 = redirnode;
3801556Srgrimes		rpp = &n2->nfile.next;
3811556Srgrimes		parsefname();
3821556Srgrimes	}
3831556Srgrimes	tokpushback++;
3841556Srgrimes
3851556Srgrimes	while (readtoken() == TNOT) {
3861556Srgrimes		TRACE(("command: TNOT recognized\n"));
3871556Srgrimes		negate = !negate;
3881556Srgrimes	}
3891556Srgrimes	tokpushback++;
3901556Srgrimes
3911556Srgrimes	switch (readtoken()) {
3921556Srgrimes	case TIF:
3931556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nif));
3941556Srgrimes		n1->type = NIF;
3951556Srgrimes		if ((n1->nif.test = list(0)) == NULL)
3961556Srgrimes			synexpect(-1);
3971556Srgrimes		if (readtoken() != TTHEN)
3981556Srgrimes			synexpect(TTHEN);
3991556Srgrimes		n1->nif.ifpart = list(0);
4001556Srgrimes		n2 = n1;
4011556Srgrimes		while (readtoken() == TELIF) {
4021556Srgrimes			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
4031556Srgrimes			n2 = n2->nif.elsepart;
4041556Srgrimes			n2->type = NIF;
4051556Srgrimes			if ((n2->nif.test = list(0)) == NULL)
4061556Srgrimes				synexpect(-1);
4071556Srgrimes			if (readtoken() != TTHEN)
4081556Srgrimes				synexpect(TTHEN);
4091556Srgrimes			n2->nif.ifpart = list(0);
4101556Srgrimes		}
4111556Srgrimes		if (lasttoken == TELSE)
4121556Srgrimes			n2->nif.elsepart = list(0);
4131556Srgrimes		else {
4141556Srgrimes			n2->nif.elsepart = NULL;
4151556Srgrimes			tokpushback++;
41617987Speter		}
41720425Ssteve		if (readtoken() != TFI)
41817987Speter			synexpect(TFI);
41920425Ssteve		checkkwd = 1;
42017987Speter		break;
4211556Srgrimes	case TWHILE:
4221556Srgrimes	case TUNTIL: {
4231556Srgrimes		int got;
4241556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nbinary));
4251556Srgrimes		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
4261556Srgrimes		if ((n1->nbinary.ch1 = list(0)) == NULL)
4271556Srgrimes			synexpect(-1);
4281556Srgrimes		if ((got=readtoken()) != TDO) {
4291556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
4301556Srgrimes			synexpect(TDO);
4311556Srgrimes		}
4321556Srgrimes		n1->nbinary.ch2 = list(0);
4331556Srgrimes		if (readtoken() != TDONE)
4341556Srgrimes			synexpect(TDONE);
4351556Srgrimes		checkkwd = 1;
4361556Srgrimes		break;
4371556Srgrimes	}
4381556Srgrimes	case TFOR:
4391556Srgrimes		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
4401556Srgrimes			synerror("Bad for loop variable");
4411556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nfor));
4421556Srgrimes		n1->type = NFOR;
4431556Srgrimes		n1->nfor.var = wordtext;
4441556Srgrimes		while (readtoken() == TNL)
4451556Srgrimes			;
4461556Srgrimes		if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
4471556Srgrimes			app = &ap;
4481556Srgrimes			while (readtoken() == TWORD) {
4491556Srgrimes				n2 = (union node *)stalloc(sizeof (struct narg));
4501556Srgrimes				n2->type = NARG;
4511556Srgrimes				n2->narg.text = wordtext;
4521556Srgrimes				n2->narg.backquote = backquotelist;
4531556Srgrimes				*app = n2;
4541556Srgrimes				app = &n2->narg.next;
4551556Srgrimes			}
4561556Srgrimes			*app = NULL;
4571556Srgrimes			n1->nfor.args = ap;
4581556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4591556Srgrimes				synexpect(-1);
4601556Srgrimes		} else {
4611556Srgrimes			static char argvars[5] = {
4621556Srgrimes				CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
4631556Srgrimes			};
4641556Srgrimes			n2 = (union node *)stalloc(sizeof (struct narg));
4651556Srgrimes			n2->type = NARG;
4661556Srgrimes			n2->narg.text = argvars;
4671556Srgrimes			n2->narg.backquote = NULL;
4681556Srgrimes			n2->narg.next = NULL;
4691556Srgrimes			n1->nfor.args = n2;
4701556Srgrimes			/*
4711556Srgrimes			 * Newline or semicolon here is optional (but note
4721556Srgrimes			 * that the original Bourne shell only allowed NL).
4731556Srgrimes			 */
4741556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4751556Srgrimes				tokpushback++;
4761556Srgrimes		}
4771556Srgrimes		checkkwd = 2;
4781556Srgrimes		if ((t = readtoken()) == TDO)
4791556Srgrimes			t = TDONE;
4801556Srgrimes		else if (t == TBEGIN)
4811556Srgrimes			t = TEND;
4821556Srgrimes		else
4831556Srgrimes			synexpect(-1);
4841556Srgrimes		n1->nfor.body = list(0);
4851556Srgrimes		if (readtoken() != t)
4861556Srgrimes			synexpect(t);
4871556Srgrimes		checkkwd = 1;
48817987Speter		break;
48917987Speter	case TCASE:
4901556Srgrimes		n1 = (union node *)stalloc(sizeof (struct ncase));
4911556Srgrimes		n1->type = NCASE;
4921556Srgrimes		if (readtoken() != TWORD)
4931556Srgrimes			synexpect(TWORD);
4941556Srgrimes		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
4951556Srgrimes		n2->type = NARG;
4961556Srgrimes		n2->narg.text = wordtext;
4971556Srgrimes		n2->narg.backquote = backquotelist;
4981556Srgrimes		n2->narg.next = NULL;
4991556Srgrimes		while (readtoken() == TNL);
5001556Srgrimes		if (lasttoken != TWORD || ! equal(wordtext, "in"))
50117987Speter			synerror("expecting \"in\"");
50217987Speter		cpp = &n1->ncase.cases;
5031556Srgrimes		noaliases = 1;	/* turn off alias expansion */
5041556Srgrimes		checkkwd = 2, readtoken();
5051556Srgrimes		while (lasttoken != TESAC) {
5061556Srgrimes			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
5071556Srgrimes			cp->type = NCLIST;
5081556Srgrimes			app = &cp->nclist.pattern;
5091556Srgrimes			if (lasttoken == TLP)
5101556Srgrimes				readtoken();
5111556Srgrimes			for (;;) {
5121556Srgrimes				*app = ap = (union node *)stalloc(sizeof (struct narg));
51317987Speter				ap->type = NARG;
51420425Ssteve				ap->narg.text = wordtext;
51520425Ssteve				ap->narg.backquote = backquotelist;
51620425Ssteve				if (checkkwd = 2, readtoken() != TPIPE)
51720425Ssteve					break;
5181556Srgrimes				app = &ap->narg.next;
5191556Srgrimes				readtoken();
5201556Srgrimes			}
5211556Srgrimes			ap->narg.next = NULL;
5221556Srgrimes			if (lasttoken != TRP)
5231556Srgrimes				noaliases = 0, synexpect(TRP);
5241556Srgrimes			cp->nclist.body = list(0);
5251556Srgrimes
5261556Srgrimes			checkkwd = 2;
5271556Srgrimes			if ((t = readtoken()) != TESAC) {
5281556Srgrimes				if (t != TENDCASE)
5291556Srgrimes					noaliases = 0, synexpect(TENDCASE);
5301556Srgrimes				else
5311556Srgrimes					checkkwd = 2, readtoken();
5321556Srgrimes			}
5331556Srgrimes			cpp = &cp->nclist.next;
5341556Srgrimes		}
5351556Srgrimes		noaliases = 0;	/* reset alias expansion */
5361556Srgrimes		*cpp = NULL;
5371556Srgrimes		checkkwd = 1;
5381556Srgrimes		break;
5391556Srgrimes	case TLP:
5401556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nredir));
5411556Srgrimes		n1->type = NSUBSHELL;
5421556Srgrimes		n1->nredir.n = list(0);
54317987Speter		n1->nredir.redirect = NULL;
54417987Speter		if (readtoken() != TRP)
5451556Srgrimes			synexpect(TRP);
5468855Srgrimes		checkkwd = 1;
5471556Srgrimes		break;
5481556Srgrimes	case TBEGIN:
5491556Srgrimes		n1 = list(0);
5501556Srgrimes		if (readtoken() != TEND)
5511556Srgrimes			synexpect(TEND);
5521556Srgrimes		checkkwd = 1;
5531556Srgrimes		break;
5541556Srgrimes	/* Handle an empty command like other simple commands.  */
5551556Srgrimes	case TSEMI:
5561556Srgrimes	case TAND:
5571556Srgrimes	case TOR:
5581556Srgrimes		/*
5591556Srgrimes		 * An empty command before a ; doesn't make much sense, and
5601556Srgrimes		 * should certainly be disallowed in the case of `if ;'.
5611556Srgrimes		 */
5621556Srgrimes		if (!redir)
5631556Srgrimes			synexpect(-1);
5641556Srgrimes	case TNL:
5651556Srgrimes	case TEOF:
5661556Srgrimes	case TWORD:
5671556Srgrimes	case TRP:
56817987Speter		tokpushback++;
56917987Speter		n1 = simplecmd(rpp, redir);
5701556Srgrimes		goto checkneg;
5711556Srgrimes	default:
5721556Srgrimes		synexpect(-1);
57317987Speter	}
57417987Speter
5751556Srgrimes	/* Now check for redirection which may follow command */
5761556Srgrimes	while (readtoken() == TREDIR) {
5771556Srgrimes		*rpp = n2 = redirnode;
5781556Srgrimes		rpp = &n2->nfile.next;
5791556Srgrimes		parsefname();
5801556Srgrimes	}
5811556Srgrimes	tokpushback++;
5821556Srgrimes	*rpp = NULL;
5831556Srgrimes	if (redir) {
5841556Srgrimes		if (n1->type != NSUBSHELL) {
5851556Srgrimes			n2 = (union node *)stalloc(sizeof (struct nredir));
5861556Srgrimes			n2->type = NREDIR;
5871556Srgrimes			n2->nredir.n = n1;
5881556Srgrimes			n1 = n2;
5891556Srgrimes		}
5901556Srgrimes		n1->nredir.redirect = redir;
5911556Srgrimes	}
5921556Srgrimes
5931556Srgrimescheckneg:
5941556Srgrimes	if (negate) {
5951556Srgrimes		n2 = (union node *)stalloc(sizeof (struct nnot));
5961556Srgrimes		n2->type = NNOT;
5971556Srgrimes		n2->nnot.com = n1;
5981556Srgrimes		return n2;
5991556Srgrimes	}
6001556Srgrimes	else
6011556Srgrimes		return n1;
6021556Srgrimes}
60321352Ssteve
6041556Srgrimes
60520425SsteveSTATIC union node *
6061556Srgrimessimplecmd(union node **rpp, union node *redir)
60718016Speter{
60820425Ssteve	union node *args, **app;
60920425Ssteve	union node **orig_rpp = rpp;
61020425Ssteve	union node *n = NULL, *n2;
61120425Ssteve	int negate = 0;
6121556Srgrimes
6131556Srgrimes	/* If we don't have any redirections already, then we must reset */
6141556Srgrimes	/* rpp to be the address of the local redir variable.  */
6151556Srgrimes	if (redir == 0)
6161556Srgrimes		rpp = &redir;
6171556Srgrimes
6181556Srgrimes	args = NULL;
6191556Srgrimes	app = &args;
6201556Srgrimes	/*
6211556Srgrimes	 * We save the incoming value, because we need this for shell
6221556Srgrimes	 * functions.  There can not be a redirect or an argument between
6231556Srgrimes	 * the function name and the open parenthesis.
6241556Srgrimes	 */
6251556Srgrimes	orig_rpp = rpp;
6261556Srgrimes
6271556Srgrimes	while (readtoken() == TNOT) {
6281556Srgrimes		TRACE(("command: TNOT recognized\n"));
6291556Srgrimes		negate = !negate;
6301556Srgrimes	}
6311556Srgrimes	tokpushback++;
6321556Srgrimes
6331556Srgrimes	for (;;) {
6341556Srgrimes		if (readtoken() == TWORD) {
6351556Srgrimes			n = (union node *)stalloc(sizeof (struct narg));
6361556Srgrimes			n->type = NARG;
6371556Srgrimes			n->narg.text = wordtext;
6381556Srgrimes			n->narg.backquote = backquotelist;
6391556Srgrimes			*app = n;
6401556Srgrimes			app = &n->narg.next;
6411556Srgrimes		} else if (lasttoken == TREDIR) {
6421556Srgrimes			*rpp = n = redirnode;
6431556Srgrimes			rpp = &n->nfile.next;
6441556Srgrimes			parsefname();	/* read name of redirection file */
6451556Srgrimes		} else if (lasttoken == TLP && app == &args->narg.next
6461556Srgrimes					    && rpp == orig_rpp) {
6471556Srgrimes			/* We have a function */
6481556Srgrimes			if (readtoken() != TRP)
64917987Speter				synexpect(TRP);
6501556Srgrimes			funclinno = plinno;
6511556Srgrimes#ifdef notdef
6521556Srgrimes			if (! goodname(n->narg.text))
6531556Srgrimes				synerror("Bad function name");
6541556Srgrimes#endif
6551556Srgrimes			n->type = NDEFUN;
6561556Srgrimes			n->narg.next = command();
6571556Srgrimes			funclinno = 0;
6581556Srgrimes			goto checkneg;
6591556Srgrimes		} else {
6601556Srgrimes			tokpushback++;
6611556Srgrimes			break;
6621556Srgrimes		}
6631556Srgrimes	}
6641556Srgrimes	*app = NULL;
6651556Srgrimes	*rpp = NULL;
6661556Srgrimes	n = (union node *)stalloc(sizeof (struct ncmd));
6671556Srgrimes	n->type = NCMD;
6681556Srgrimes	n->ncmd.backgnd = 0;
6691556Srgrimes	n->ncmd.args = args;
6701556Srgrimes	n->ncmd.redirect = redir;
6711556Srgrimes
6721556Srgrimescheckneg:
6731556Srgrimes	if (negate) {
6741556Srgrimes		n2 = (union node *)stalloc(sizeof (struct nnot));
6751556Srgrimes		n2->type = NNOT;
6761556Srgrimes		n2->nnot.com = n;
6771556Srgrimes		return n2;
6781556Srgrimes	}
6791556Srgrimes	else
6801556Srgrimes		return n;
6811556Srgrimes}
6821556Srgrimes
6831556SrgrimesSTATIC union node *
6841556Srgrimesmakename(void)
6851556Srgrimes{
6861556Srgrimes	union node *n;
6871556Srgrimes
6881556Srgrimes	n = (union node *)stalloc(sizeof (struct narg));
6891556Srgrimes	n->type = NARG;
6901556Srgrimes	n->narg.next = NULL;
6911556Srgrimes	n->narg.text = wordtext;
69217987Speter	n->narg.backquote = backquotelist;
6931556Srgrimes	return n;
6941556Srgrimes}
6951556Srgrimes
6961556Srgrimesvoid fixredir(union node *n, const char *text, int err)
6971556Srgrimes{
6981556Srgrimes	TRACE(("Fix redir %s %d\n", text, err));
6991556Srgrimes	if (!err)
7001556Srgrimes		n->ndup.vname = NULL;
7011556Srgrimes
7021556Srgrimes	if (is_digit(text[0]) && text[1] == '\0')
7031556Srgrimes		n->ndup.dupfd = digit_val(text[0]);
70420425Ssteve	else if (text[0] == '-' && text[1] == '\0')
7051556Srgrimes		n->ndup.dupfd = -1;
70620425Ssteve	else {
70720425Ssteve
70820425Ssteve		if (err)
70920425Ssteve			synerror("Bad fd number");
71020425Ssteve		else
7111556Srgrimes			n->ndup.vname = makename();
7121556Srgrimes	}
7131556Srgrimes}
7141556Srgrimes
7151556Srgrimes
7161556SrgrimesSTATIC void
7171556Srgrimesparsefname(void)
7181556Srgrimes{
7191556Srgrimes	union node *n = redirnode;
7201556Srgrimes
7211556Srgrimes	if (readtoken() != TWORD)
7221556Srgrimes		synexpect(-1);
7231556Srgrimes	if (n->type == NHERE) {
7241556Srgrimes		struct heredoc *here = heredoc;
7251556Srgrimes		struct heredoc *p;
7261556Srgrimes		int i;
7271556Srgrimes
7281556Srgrimes		if (quoteflag == 0)
7291556Srgrimes			n->type = NXHERE;
7301556Srgrimes		TRACE(("Here document %d\n", n->type));
7311556Srgrimes		if (here->striptabs) {
7321556Srgrimes			while (*wordtext == '\t')
7331556Srgrimes				wordtext++;
7341556Srgrimes		}
7351556Srgrimes		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
7361556Srgrimes			synerror("Illegal eof marker for << redirection");
7371556Srgrimes		rmescapes(wordtext);
7381556Srgrimes		here->eofmark = wordtext;
7391556Srgrimes		here->next = NULL;
7401556Srgrimes		if (heredoclist == NULL)
7411556Srgrimes			heredoclist = here;
74217987Speter		else {
7431556Srgrimes			for (p = heredoclist ; p->next ; p = p->next);
74417987Speter			p->next = here;
7451556Srgrimes		}
7461556Srgrimes	} else if (n->type == NTOFD || n->type == NFROMFD) {
7471556Srgrimes		fixredir(n, wordtext, 0);
7481556Srgrimes	} else {
7491556Srgrimes		n->nfile.fname = makename();
7501556Srgrimes	}
7511556Srgrimes}
7521556Srgrimes
7531556Srgrimes
7541556Srgrimes/*
7551556Srgrimes * Input any here documents.
7561556Srgrimes */
7571556Srgrimes
7581556SrgrimesSTATIC void
7591556Srgrimesparseheredoc(void)
7601556Srgrimes{
7611556Srgrimes	struct heredoc *here;
7621556Srgrimes	union node *n;
7631556Srgrimes
7641556Srgrimes	while (heredoclist) {
7651556Srgrimes		here = heredoclist;
7661556Srgrimes		heredoclist = here->next;
7671556Srgrimes		if (needprompt) {
7681556Srgrimes			setprompt(2);
7691556Srgrimes			needprompt = 0;
7701556Srgrimes		}
7711556Srgrimes		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
7721556Srgrimes				here->eofmark, here->striptabs);
7731556Srgrimes		n = (union node *)stalloc(sizeof (struct narg));
7741556Srgrimes		n->narg.type = NARG;
7751556Srgrimes		n->narg.next = NULL;
7761556Srgrimes		n->narg.text = wordtext;
7771556Srgrimes		n->narg.backquote = backquotelist;
7781556Srgrimes		here->here->nhere.doc = n;
7791556Srgrimes	}
7801556Srgrimes}
7811556Srgrimes
7821556SrgrimesSTATIC int
7831556Srgrimespeektoken(void)
7841556Srgrimes{
7851556Srgrimes	int t;
7861556Srgrimes
7871556Srgrimes	t = readtoken();
7881556Srgrimes	tokpushback++;
7891556Srgrimes	return (t);
7901556Srgrimes}
7911556Srgrimes
7921556SrgrimesSTATIC int
7931556Srgrimesreadtoken(void)
7941556Srgrimes{
7951556Srgrimes	int t;
7961556Srgrimes	int savecheckkwd = checkkwd;
7971556Srgrimes	struct alias *ap;
7981556Srgrimes#ifdef DEBUG
7991556Srgrimes	int alreadyseen = tokpushback;
8001556Srgrimes#endif
8011556Srgrimes
8021556Srgrimes	top:
8031556Srgrimes	t = xxreadtoken();
8041556Srgrimes
8051556Srgrimes	if (checkkwd) {
8061556Srgrimes		/*
8071556Srgrimes		 * eat newlines
80817987Speter		 */
80917987Speter		if (checkkwd == 2) {
8101556Srgrimes			checkkwd = 0;
8111556Srgrimes			while (t == TNL) {
8121556Srgrimes				parseheredoc();
8131556Srgrimes				t = xxreadtoken();
8141556Srgrimes			}
8151556Srgrimes		} else
8161556Srgrimes			checkkwd = 0;
8171556Srgrimes		/*
8181556Srgrimes		 * check for keywords and aliases
8191556Srgrimes		 */
8201556Srgrimes		if (t == TWORD && !quoteflag)
8211556Srgrimes		{
8221556Srgrimes			const char * const *pp;
8231556Srgrimes
8241556Srgrimes			for (pp = parsekwd; *pp; pp++) {
8251556Srgrimes				if (**pp == *wordtext && equal(*pp, wordtext))
8261556Srgrimes				{
8271556Srgrimes					lasttoken = t = pp - parsekwd + KWDOFFSET;
8281556Srgrimes					TRACE(("keyword %s recognized\n", tokname[t]));
8291556Srgrimes					goto out;
8301556Srgrimes				}
8311556Srgrimes			}
8321556Srgrimes			if (noaliases == 0 &&
8331556Srgrimes			    (ap = lookupalias(wordtext, 1)) != NULL) {
8341556Srgrimes				pushstring(ap->val, strlen(ap->val), ap);
8351556Srgrimes				checkkwd = savecheckkwd;
8361556Srgrimes				goto top;
8371556Srgrimes			}
8381556Srgrimes		}
8391556Srgrimesout:
8401556Srgrimes		checkkwd = (t == TNOT) ? savecheckkwd : 0;
8411556Srgrimes	}
8421556Srgrimes#ifdef DEBUG
8431556Srgrimes	if (!alreadyseen)
8441556Srgrimes	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8451556Srgrimes	else
8461556Srgrimes	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
8471556Srgrimes#endif
8481556Srgrimes	return (t);
8491556Srgrimes}
8501556Srgrimes
8511556Srgrimes
8521556Srgrimes/*
8531556Srgrimes * Read the next input token.
8541556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in
8551556Srgrimes *	backquotes.  We set quoteflag to true if any part of the word was
8561556Srgrimes *	quoted.
8571556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing
8581556Srgrimes *	the redirection.
8591556Srgrimes * In all cases, the variable startlinno is set to the number of the line
8601556Srgrimes *	on which the token starts.
8611556Srgrimes *
8621556Srgrimes * [Change comment:  here documents and internal procedures]
8631556Srgrimes * [Readtoken shouldn't have any arguments.  Perhaps we should make the
8641556Srgrimes *  word parsing code into a separate routine.  In this case, readtoken
8651556Srgrimes *  doesn't need to have any internal procedures, but parseword does.
8661556Srgrimes *  We could also make parseoperator in essence the main routine, and
8671556Srgrimes *  have parseword (readtoken1?) handle both words and redirection.]
8681556Srgrimes */
86917987Speter
8701556Srgrimes#define RETURN(token)	return lasttoken = token
87117987Speter
8721556SrgrimesSTATIC int
8731556Srgrimesxxreadtoken(void)
8741556Srgrimes{
8751556Srgrimes	int c;
8761556Srgrimes
8771556Srgrimes	if (tokpushback) {
8781556Srgrimes		tokpushback = 0;
8791556Srgrimes		return lasttoken;
8801556Srgrimes	}
8811556Srgrimes	if (needprompt) {
8821556Srgrimes		setprompt(2);
8831556Srgrimes		needprompt = 0;
8841556Srgrimes	}
8851556Srgrimes	startlinno = plinno;
8861556Srgrimes	for (;;) {	/* until token or start of word found */
8871556Srgrimes		c = pgetc_macro();
8881556Srgrimes		if (c == ' ' || c == '\t')
8891556Srgrimes			continue;		/* quick check for white space first */
8901556Srgrimes		switch (c) {
8911556Srgrimes		case ' ': case '\t':
8921556Srgrimes			continue;
8931556Srgrimes		case '#':
8941556Srgrimes			while ((c = pgetc()) != '\n' && c != PEOF);
8951556Srgrimes			pungetc();
8961556Srgrimes			continue;
8971556Srgrimes		case '\\':
8981556Srgrimes			if (pgetc() == '\n') {
8991556Srgrimes				startlinno = ++plinno;
9001556Srgrimes				if (doprompt)
9011556Srgrimes					setprompt(2);
9021556Srgrimes				else
9031556Srgrimes					setprompt(0);
9041556Srgrimes				continue;
9051556Srgrimes			}
9061556Srgrimes			pungetc();
9071556Srgrimes			goto breakloop;
9081556Srgrimes		case '\n':
9091556Srgrimes			plinno++;
9101556Srgrimes			needprompt = doprompt;
9111556Srgrimes			RETURN(TNL);
9121556Srgrimes		case PEOF:
9131556Srgrimes			RETURN(TEOF);
9141556Srgrimes		case '&':
9151556Srgrimes			if (pgetc() == '&')
9161556Srgrimes				RETURN(TAND);
9171556Srgrimes			pungetc();
9181556Srgrimes			RETURN(TBACKGND);
9191556Srgrimes		case '|':
9201556Srgrimes			if (pgetc() == '|')
9211556Srgrimes				RETURN(TOR);
9221556Srgrimes			pungetc();
9231556Srgrimes			RETURN(TPIPE);
9241556Srgrimes		case ';':
9251556Srgrimes			if (pgetc() == ';')
9261556Srgrimes				RETURN(TENDCASE);
9271556Srgrimes			pungetc();
9281556Srgrimes			RETURN(TSEMI);
9291556Srgrimes		case '(':
9301556Srgrimes			RETURN(TLP);
9311556Srgrimes		case ')':
9321556Srgrimes			RETURN(TRP);
9331556Srgrimes		default:
9341556Srgrimes			goto breakloop;
9351556Srgrimes		}
9361556Srgrimes	}
9371556Srgrimesbreakloop:
9381556Srgrimes	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
9391556Srgrimes#undef RETURN
9401556Srgrimes}
9411556Srgrimes
9421556Srgrimes
9431556Srgrimes#define MAXNEST_STATIC 8
9441556Srgrimesstruct tokenstate
9451556Srgrimes{
9461556Srgrimes	const char *syntax; /* *SYNTAX */
9471556Srgrimes	int parenlevel; /* levels of parentheses in arithmetic */
9481556Srgrimes	enum tokenstate_category
9491556Srgrimes	{
9501556Srgrimes		TSTATE_TOP,
9511556Srgrimes		TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
9521556Srgrimes		TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
9531556Srgrimes		TSTATE_ARITH
9541556Srgrimes	} category;
9551556Srgrimes};
9561556Srgrimes
9571556Srgrimes
9581556Srgrimes/*
9591556Srgrimes * Called to parse command substitutions.
9601556Srgrimes */
9611556Srgrimes
9621556SrgrimesSTATIC char *
9631556Srgrimesparsebackq(char *out, struct nodelist **pbqlist,
9641556Srgrimes		int oldstyle, int dblquote, int quoted)
9651556Srgrimes{
9661556Srgrimes	struct nodelist **nlpp;
9671556Srgrimes	union node *n;
9681556Srgrimes	char *volatile str;
9691556Srgrimes	struct jmploc jmploc;
9701556Srgrimes	struct jmploc *const savehandler = handler;
9711556Srgrimes	int savelen;
9721556Srgrimes	int saveprompt;
9731556Srgrimes	const int bq_startlinno = plinno;
9741556Srgrimes	char *volatile ostr = NULL;
9751556Srgrimes	struct parsefile *const savetopfile = getcurrentfile();
9761556Srgrimes
9771556Srgrimes	str = NULL;
9781556Srgrimes	if (setjmp(jmploc.loc)) {
9791556Srgrimes		popfilesupto(savetopfile);
9801556Srgrimes		if (str)
9811556Srgrimes			ckfree(str);
9821556Srgrimes		if (ostr)
9831556Srgrimes			ckfree(ostr);
9841556Srgrimes		handler = savehandler;
9851556Srgrimes		if (exception == EXERROR) {
9861556Srgrimes			startlinno = bq_startlinno;
9871556Srgrimes			synerror("Error in command substitution");
9881556Srgrimes		}
9891556Srgrimes		longjmp(handler->loc, 1);
9901556Srgrimes	}
9911556Srgrimes	INTOFF;
9921556Srgrimes	savelen = out - stackblock();
9931556Srgrimes	if (savelen > 0) {
9941556Srgrimes		str = ckmalloc(savelen);
9951556Srgrimes		memcpy(str, stackblock(), savelen);
9961556Srgrimes	}
9971556Srgrimes	handler = &jmploc;
9981556Srgrimes	INTON;
9991556Srgrimes        if (oldstyle) {
10001556Srgrimes                /* We must read until the closing backquote, giving special
10011556Srgrimes                   treatment to some slashes, and then push the string and
10021556Srgrimes                   reread it as input, interpreting it normally.  */
10031556Srgrimes                char *oout;
10041556Srgrimes                int c;
10051556Srgrimes                int olen;
10061556Srgrimes
10071556Srgrimes
10081556Srgrimes                STARTSTACKSTR(oout);
10091556Srgrimes		for (;;) {
10101556Srgrimes			if (needprompt) {
10111556Srgrimes				setprompt(2);
10121556Srgrimes				needprompt = 0;
10131556Srgrimes			}
10141556Srgrimes			switch (c = pgetc()) {
10151556Srgrimes			case '`':
10161556Srgrimes				goto done;
10171556Srgrimes
10181556Srgrimes			case '\\':
10191556Srgrimes                                if ((c = pgetc()) == '\n') {
10201556Srgrimes					plinno++;
10211556Srgrimes					if (doprompt)
10221556Srgrimes						setprompt(2);
10231556Srgrimes					else
10241556Srgrimes						setprompt(0);
10251556Srgrimes					/*
10261556Srgrimes					 * If eating a newline, avoid putting
10271556Srgrimes					 * the newline into the new character
10281556Srgrimes					 * stream (via the STPUTC after the
10291556Srgrimes					 * switch).
10301556Srgrimes					 */
10311556Srgrimes					continue;
10321556Srgrimes				}
10331556Srgrimes                                if (c != '\\' && c != '`' && c != '$'
10341556Srgrimes                                    && (!dblquote || c != '"'))
10351556Srgrimes                                        STPUTC('\\', oout);
10361556Srgrimes				break;
10371556Srgrimes
10381556Srgrimes			case '\n':
10391556Srgrimes				plinno++;
10401556Srgrimes				needprompt = doprompt;
10411556Srgrimes				break;
10421556Srgrimes
10431556Srgrimes			case PEOF:
10441556Srgrimes			        startlinno = plinno;
10451556Srgrimes				synerror("EOF in backquote substitution");
10461556Srgrimes 				break;
10471556Srgrimes
10481556Srgrimes			default:
10491556Srgrimes				break;
10501556Srgrimes			}
10511556Srgrimes			STPUTC(c, oout);
10521556Srgrimes                }
10531556Srgrimesdone:
10541556Srgrimes                STPUTC('\0', oout);
10551556Srgrimes                olen = oout - stackblock();
10561556Srgrimes		INTOFF;
10571556Srgrimes		ostr = ckmalloc(olen);
10581556Srgrimes		memcpy(ostr, stackblock(), olen);
10591556Srgrimes		setinputstring(ostr, 1);
10601556Srgrimes		INTON;
10611556Srgrimes        }
10621556Srgrimes	nlpp = pbqlist;
10631556Srgrimes	while (*nlpp)
10641556Srgrimes		nlpp = &(*nlpp)->next;
10651556Srgrimes	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
10661556Srgrimes	(*nlpp)->next = NULL;
10671556Srgrimes
10681556Srgrimes	if (oldstyle) {
10691556Srgrimes		saveprompt = doprompt;
10701556Srgrimes		doprompt = 0;
10711556Srgrimes	}
10721556Srgrimes
10731556Srgrimes	n = list(0);
10741556Srgrimes
10751556Srgrimes	if (oldstyle)
10761556Srgrimes		doprompt = saveprompt;
10771556Srgrimes	else {
10781556Srgrimes		if (readtoken() != TRP)
10791556Srgrimes			synexpect(TRP);
10801556Srgrimes	}
10811556Srgrimes
10821556Srgrimes	(*nlpp)->n = n;
10831556Srgrimes        if (oldstyle) {
10841556Srgrimes		/*
10851556Srgrimes		 * Start reading from old file again, ignoring any pushed back
10861556Srgrimes		 * tokens left from the backquote parsing
10871556Srgrimes		 */
10881556Srgrimes                popfile();
10891556Srgrimes		tokpushback = 0;
10901556Srgrimes	}
10911556Srgrimes	while (stackblocksize() <= savelen)
10921556Srgrimes		growstackblock();
10931556Srgrimes	STARTSTACKSTR(out);
10941556Srgrimes	if (str) {
10951556Srgrimes		memcpy(out, str, savelen);
10961556Srgrimes		STADJUST(savelen, out);
10971556Srgrimes		INTOFF;
10981556Srgrimes		ckfree(str);
10991556Srgrimes		str = NULL;
11001556Srgrimes		INTON;
11011556Srgrimes	}
11021556Srgrimes	if (ostr) {
11031556Srgrimes		INTOFF;
110418954Ssteve		ckfree(ostr);
11051556Srgrimes		ostr = NULL;
11061556Srgrimes		INTON;
11071556Srgrimes	}
11081556Srgrimes	handler = savehandler;
11091556Srgrimes	if (quoted)
11101556Srgrimes		USTPUTC(CTLBACKQ | CTLQUOTE, out);
11111556Srgrimes	else
11121556Srgrimes		USTPUTC(CTLBACKQ, out);
11131556Srgrimes	return out;
11141556Srgrimes}
11151556Srgrimes
11161556Srgrimes
1117/*
1118 * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
1119 * is not NULL, read a here document.  In the latter case, eofmark is the
1120 * word which marks the end of the document and striptabs is true if
1121 * leading tabs should be stripped from the document.  The argument firstc
1122 * is the first character of the input token or document.
1123 *
1124 * Because C does not have internal subroutines, I have simulated them
1125 * using goto's to implement the subroutine linkage.  The following macros
1126 * will run code that appears at the end of readtoken1.
1127 */
1128
1129#define CHECKEND()	{goto checkend; checkend_return:;}
1130#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
1131#define PARSESUB()	{goto parsesub; parsesub_return:;}
1132#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
1133
1134STATIC int
1135readtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs)
1136{
1137	int c = firstc;
1138	char *out;
1139	int len;
1140	char line[EOFMARKLEN + 1];
1141	struct nodelist *bqlist;
1142	int quotef;
1143	int newvarnest;
1144	int level;
1145	int synentry;
1146	struct tokenstate state_static[MAXNEST_STATIC];
1147	int maxnest = MAXNEST_STATIC;
1148	struct tokenstate *state = state_static;
1149
1150	startlinno = plinno;
1151	quotef = 0;
1152	bqlist = NULL;
1153	newvarnest = 0;
1154	level = 0;
1155	state[level].syntax = initialsyntax;
1156	state[level].parenlevel = 0;
1157	state[level].category = TSTATE_TOP;
1158
1159	STARTSTACKSTR(out);
1160	loop: {	/* for each line, until end of word */
1161		CHECKEND();	/* set c to PEOF if at end of here document */
1162		for (;;) {	/* until end of line or end of word */
1163			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
1164
1165			synentry = state[level].syntax[c];
1166
1167			switch(synentry) {
1168			case CNL:	/* '\n' */
1169				if (state[level].syntax == BASESYNTAX)
1170					goto endword;	/* exit outer loop */
1171				USTPUTC(c, out);
1172				plinno++;
1173				if (doprompt)
1174					setprompt(2);
1175				else
1176					setprompt(0);
1177				c = pgetc();
1178				goto loop;		/* continue outer loop */
1179			case CWORD:
1180				USTPUTC(c, out);
1181				break;
1182			case CCTL:
1183				if (eofmark == NULL || initialsyntax != SQSYNTAX)
1184					USTPUTC(CTLESC, out);
1185				USTPUTC(c, out);
1186				break;
1187			case CBACK:	/* backslash */
1188				c = pgetc();
1189				if (c == PEOF) {
1190					USTPUTC('\\', out);
1191					pungetc();
1192				} else if (c == '\n') {
1193					plinno++;
1194					if (doprompt)
1195						setprompt(2);
1196					else
1197						setprompt(0);
1198				} else {
1199					if (state[level].syntax == DQSYNTAX &&
1200					    c != '\\' && c != '`' && c != '$' &&
1201					    (c != '"' || (eofmark != NULL &&
1202						newvarnest == 0)) &&
1203					    (c != '}' || state[level].category != TSTATE_VAR_OLD))
1204						USTPUTC('\\', out);
1205					if (SQSYNTAX[c] == CCTL)
1206						USTPUTC(CTLESC, out);
1207					else if (eofmark == NULL ||
1208					    newvarnest > 0)
1209						USTPUTC(CTLQUOTEMARK, out);
1210					USTPUTC(c, out);
1211					quotef++;
1212				}
1213				break;
1214			case CSQUOTE:
1215				USTPUTC(CTLQUOTEMARK, out);
1216				state[level].syntax = SQSYNTAX;
1217				break;
1218			case CDQUOTE:
1219				USTPUTC(CTLQUOTEMARK, out);
1220				state[level].syntax = DQSYNTAX;
1221				break;
1222			case CENDQUOTE:
1223				if (eofmark != NULL && newvarnest == 0)
1224					USTPUTC(c, out);
1225				else {
1226					if (state[level].category == TSTATE_ARITH)
1227						state[level].syntax = ARISYNTAX;
1228					else
1229						state[level].syntax = BASESYNTAX;
1230					quotef++;
1231				}
1232				break;
1233			case CVAR:	/* '$' */
1234				PARSESUB();		/* parse substitution */
1235				break;
1236			case CENDVAR:	/* '}' */
1237				if (level > 0 &&
1238				    (state[level].category == TSTATE_VAR_OLD ||
1239				    state[level].category == TSTATE_VAR_NEW)) {
1240					if (state[level].category == TSTATE_VAR_OLD)
1241						state[level - 1].syntax = state[level].syntax;
1242					else
1243						newvarnest--;
1244					level--;
1245					USTPUTC(CTLENDVAR, out);
1246				} else {
1247					USTPUTC(c, out);
1248				}
1249				break;
1250			case CLP:	/* '(' in arithmetic */
1251				state[level].parenlevel++;
1252				USTPUTC(c, out);
1253				break;
1254			case CRP:	/* ')' in arithmetic */
1255				if (state[level].parenlevel > 0) {
1256					USTPUTC(c, out);
1257					--state[level].parenlevel;
1258				} else {
1259					if (pgetc() == ')') {
1260						if (level > 0 &&
1261						    state[level].category == TSTATE_ARITH) {
1262							level--;
1263							USTPUTC(CTLENDARI, out);
1264						} else
1265							USTPUTC(')', out);
1266					} else {
1267						/*
1268						 * unbalanced parens
1269						 *  (don't 2nd guess - no error)
1270						 */
1271						pungetc();
1272						USTPUTC(')', out);
1273					}
1274				}
1275				break;
1276			case CBQUOTE:	/* '`' */
1277				out = parsebackq(out, &bqlist, 1,
1278				    state[level].syntax == DQSYNTAX &&
1279				    (eofmark == NULL || newvarnest > 0),
1280				    state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
1281				break;
1282			case CEOF:
1283				goto endword;		/* exit outer loop */
1284			default:
1285				if (level == 0)
1286					goto endword;	/* exit outer loop */
1287				USTPUTC(c, out);
1288			}
1289			c = pgetc_macro();
1290		}
1291	}
1292endword:
1293	if (state[level].syntax == ARISYNTAX)
1294		synerror("Missing '))'");
1295	if (state[level].syntax != BASESYNTAX && eofmark == NULL)
1296		synerror("Unterminated quoted string");
1297	if (state[level].category == TSTATE_VAR_OLD ||
1298	    state[level].category == TSTATE_VAR_NEW) {
1299		startlinno = plinno;
1300		synerror("Missing '}'");
1301	}
1302	if (state != state_static)
1303		parser_temp_free_upto(state);
1304	USTPUTC('\0', out);
1305	len = out - stackblock();
1306	out = stackblock();
1307	if (eofmark == NULL) {
1308		if ((c == '>' || c == '<')
1309		 && quotef == 0
1310		 && len <= 2
1311		 && (*out == '\0' || is_digit(*out))) {
1312			PARSEREDIR();
1313			return lasttoken = TREDIR;
1314		} else {
1315			pungetc();
1316		}
1317	}
1318	quoteflag = quotef;
1319	backquotelist = bqlist;
1320	grabstackblock(len);
1321	wordtext = out;
1322	return lasttoken = TWORD;
1323/* end of readtoken routine */
1324
1325
1326/*
1327 * Check to see whether we are at the end of the here document.  When this
1328 * is called, c is set to the first character of the next input line.  If
1329 * we are at the end of the here document, this routine sets the c to PEOF.
1330 */
1331
1332checkend: {
1333	if (eofmark) {
1334		if (striptabs) {
1335			while (c == '\t')
1336				c = pgetc();
1337		}
1338		if (c == *eofmark) {
1339			if (pfgets(line, sizeof line) != NULL) {
1340				char *p, *q;
1341
1342				p = line;
1343				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1344				if (*p == '\n' && *q == '\0') {
1345					c = PEOF;
1346					plinno++;
1347					needprompt = doprompt;
1348				} else {
1349					pushstring(line, strlen(line), NULL);
1350				}
1351			}
1352		}
1353	}
1354	goto checkend_return;
1355}
1356
1357
1358/*
1359 * Parse a redirection operator.  The variable "out" points to a string
1360 * specifying the fd to be redirected.  The variable "c" contains the
1361 * first character of the redirection operator.
1362 */
1363
1364parseredir: {
1365	char fd = *out;
1366	union node *np;
1367
1368	np = (union node *)stalloc(sizeof (struct nfile));
1369	if (c == '>') {
1370		np->nfile.fd = 1;
1371		c = pgetc();
1372		if (c == '>')
1373			np->type = NAPPEND;
1374		else if (c == '&')
1375			np->type = NTOFD;
1376		else if (c == '|')
1377			np->type = NCLOBBER;
1378		else {
1379			np->type = NTO;
1380			pungetc();
1381		}
1382	} else {	/* c == '<' */
1383		np->nfile.fd = 0;
1384		c = pgetc();
1385		if (c == '<') {
1386			if (sizeof (struct nfile) != sizeof (struct nhere)) {
1387				np = (union node *)stalloc(sizeof (struct nhere));
1388				np->nfile.fd = 0;
1389			}
1390			np->type = NHERE;
1391			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1392			heredoc->here = np;
1393			if ((c = pgetc()) == '-') {
1394				heredoc->striptabs = 1;
1395			} else {
1396				heredoc->striptabs = 0;
1397				pungetc();
1398			}
1399		} else if (c == '&')
1400			np->type = NFROMFD;
1401		else if (c == '>')
1402			np->type = NFROMTO;
1403		else {
1404			np->type = NFROM;
1405			pungetc();
1406		}
1407	}
1408	if (fd != '\0')
1409		np->nfile.fd = digit_val(fd);
1410	redirnode = np;
1411	goto parseredir_return;
1412}
1413
1414
1415/*
1416 * Parse a substitution.  At this point, we have read the dollar sign
1417 * and nothing else.
1418 */
1419
1420parsesub: {
1421	char buf[10];
1422	int subtype;
1423	int typeloc;
1424	int flags;
1425	char *p;
1426	static const char types[] = "}-+?=";
1427	int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1428	int i;
1429	int linno;
1430	int length;
1431
1432	c = pgetc();
1433	if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1434	    !is_special(c)) {
1435		USTPUTC('$', out);
1436		pungetc();
1437	} else if (c == '(') {	/* $(command) or $((arith)) */
1438		if (pgetc() == '(') {
1439			PARSEARITH();
1440		} else {
1441			pungetc();
1442			out = parsebackq(out, &bqlist, 0,
1443			    state[level].syntax == DQSYNTAX &&
1444			    (eofmark == NULL || newvarnest > 0),
1445			    state[level].syntax == DQSYNTAX ||
1446			    state[level].syntax == ARISYNTAX);
1447		}
1448	} else {
1449		USTPUTC(CTLVAR, out);
1450		typeloc = out - stackblock();
1451		USTPUTC(VSNORMAL, out);
1452		subtype = VSNORMAL;
1453		flags = 0;
1454		if (c == '{') {
1455			bracketed_name = 1;
1456			c = pgetc();
1457			if (c == '#') {
1458				if ((c = pgetc()) == '}')
1459					c = '#';
1460				else
1461					subtype = VSLENGTH;
1462			}
1463			else
1464				subtype = 0;
1465		}
1466		if (!is_eof(c) && is_name(c)) {
1467			length = 0;
1468			do {
1469				STPUTC(c, out);
1470				c = pgetc();
1471				length++;
1472			} while (!is_eof(c) && is_in_name(c));
1473			if (length == 6 &&
1474			    strncmp(out - length, "LINENO", length) == 0) {
1475				/* Replace the variable name with the
1476				 * current line number. */
1477				linno = plinno;
1478				if (funclinno != 0)
1479					linno -= funclinno - 1;
1480				snprintf(buf, sizeof(buf), "%d", linno);
1481				STADJUST(-6, out);
1482				for (i = 0; buf[i] != '\0'; i++)
1483					STPUTC(buf[i], out);
1484				flags |= VSLINENO;
1485			}
1486		} else if (is_digit(c)) {
1487			if (bracketed_name) {
1488				do {
1489					STPUTC(c, out);
1490					c = pgetc();
1491				} while (is_digit(c));
1492			} else {
1493				STPUTC(c, out);
1494				c = pgetc();
1495			}
1496		} else {
1497			if (! is_special(c)) {
1498				subtype = VSERROR;
1499				if (c == '}')
1500					pungetc();
1501				else if (c == '\n' || c == PEOF)
1502					synerror("Unexpected end of line in substitution");
1503				else
1504					USTPUTC(c, out);
1505			} else {
1506				USTPUTC(c, out);
1507				c = pgetc();
1508			}
1509		}
1510		if (subtype == 0) {
1511			switch (c) {
1512			case ':':
1513				flags |= VSNUL;
1514				c = pgetc();
1515				/*FALLTHROUGH*/
1516			default:
1517				p = strchr(types, c);
1518				if (p == NULL) {
1519					if (c == '\n' || c == PEOF)
1520						synerror("Unexpected end of line in substitution");
1521					if (flags == VSNUL)
1522						STPUTC(':', out);
1523					STPUTC(c, out);
1524					subtype = VSERROR;
1525				} else
1526					subtype = p - types + VSNORMAL;
1527				break;
1528			case '%':
1529			case '#':
1530				{
1531					int cc = c;
1532					subtype = c == '#' ? VSTRIMLEFT :
1533							     VSTRIMRIGHT;
1534					c = pgetc();
1535					if (c == cc)
1536						subtype++;
1537					else
1538						pungetc();
1539					break;
1540				}
1541			}
1542		} else if (subtype != VSERROR) {
1543			pungetc();
1544		}
1545		STPUTC('=', out);
1546		if (subtype != VSLENGTH && (state[level].syntax == DQSYNTAX ||
1547		    state[level].syntax == ARISYNTAX))
1548			flags |= VSQUOTE;
1549		*(stackblock() + typeloc) = subtype | flags;
1550		if (subtype != VSNORMAL) {
1551			if (level + 1 >= maxnest) {
1552				maxnest *= 2;
1553				if (state == state_static) {
1554					state = parser_temp_alloc(
1555					    maxnest * sizeof(*state));
1556					memcpy(state, state_static,
1557					    MAXNEST_STATIC * sizeof(*state));
1558				} else
1559					state = parser_temp_realloc(state,
1560					    maxnest * sizeof(*state));
1561			}
1562			level++;
1563			state[level].parenlevel = 0;
1564			if (subtype == VSMINUS || subtype == VSPLUS ||
1565			    subtype == VSQUESTION || subtype == VSASSIGN) {
1566				/*
1567				 * For operators that were in the Bourne shell,
1568				 * inherit the double-quote state.
1569				 */
1570				state[level].syntax = state[level - 1].syntax;
1571				state[level].category = TSTATE_VAR_OLD;
1572			} else {
1573				/*
1574				 * The other operators take a pattern,
1575				 * so go to BASESYNTAX.
1576				 * Also, ' and " are now special, even
1577				 * in here documents.
1578				 */
1579				state[level].syntax = BASESYNTAX;
1580				state[level].category = TSTATE_VAR_NEW;
1581				newvarnest++;
1582			}
1583		}
1584	}
1585	goto parsesub_return;
1586}
1587
1588
1589/*
1590 * Parse an arithmetic expansion (indicate start of one and set state)
1591 */
1592parsearith: {
1593
1594	if (level + 1 >= maxnest) {
1595		maxnest *= 2;
1596		if (state == state_static) {
1597			state = parser_temp_alloc(
1598			    maxnest * sizeof(*state));
1599			memcpy(state, state_static,
1600			    MAXNEST_STATIC * sizeof(*state));
1601		} else
1602			state = parser_temp_realloc(state,
1603			    maxnest * sizeof(*state));
1604	}
1605	level++;
1606	state[level].syntax = ARISYNTAX;
1607	state[level].parenlevel = 0;
1608	state[level].category = TSTATE_ARITH;
1609	USTPUTC(CTLARI, out);
1610	if (state[level - 1].syntax == DQSYNTAX)
1611		USTPUTC('"',out);
1612	else
1613		USTPUTC(' ',out);
1614	goto parsearith_return;
1615}
1616
1617} /* end of readtoken */
1618
1619
1620
1621#ifdef mkinit
1622RESET {
1623	tokpushback = 0;
1624	checkkwd = 0;
1625}
1626#endif
1627
1628/*
1629 * Returns true if the text contains nothing to expand (no dollar signs
1630 * or backquotes).
1631 */
1632
1633STATIC int
1634noexpand(char *text)
1635{
1636	char *p;
1637	char c;
1638
1639	p = text;
1640	while ((c = *p++) != '\0') {
1641		if ( c == CTLQUOTEMARK)
1642			continue;
1643		if (c == CTLESC)
1644			p++;
1645		else if (BASESYNTAX[(int)c] == CCTL)
1646			return 0;
1647	}
1648	return 1;
1649}
1650
1651
1652/*
1653 * Return true if the argument is a legal variable name (a letter or
1654 * underscore followed by zero or more letters, underscores, and digits).
1655 */
1656
1657int
1658goodname(const char *name)
1659{
1660	const char *p;
1661
1662	p = name;
1663	if (! is_name(*p))
1664		return 0;
1665	while (*++p) {
1666		if (! is_in_name(*p))
1667			return 0;
1668	}
1669	return 1;
1670}
1671
1672
1673/*
1674 * Called when an unexpected token is read during the parse.  The argument
1675 * is the token that is expected, or -1 if more than one type of token can
1676 * occur at this point.
1677 */
1678
1679STATIC void
1680synexpect(int token)
1681{
1682	char msg[64];
1683
1684	if (token >= 0) {
1685		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1686			tokname[lasttoken], tokname[token]);
1687	} else {
1688		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1689	}
1690	synerror(msg);
1691}
1692
1693
1694STATIC void
1695synerror(const char *msg)
1696{
1697	if (commandname)
1698		outfmt(out2, "%s: %d: ", commandname, startlinno);
1699	outfmt(out2, "Syntax error: %s\n", msg);
1700	error((char *)NULL);
1701}
1702
1703STATIC void
1704setprompt(int which)
1705{
1706	whichprompt = which;
1707
1708#ifndef NO_HISTORY
1709	if (!el)
1710#endif
1711	{
1712		out2str(getprompt(NULL));
1713		flushout(out2);
1714	}
1715}
1716
1717/*
1718 * called by editline -- any expansions to the prompt
1719 *    should be added here.
1720 */
1721char *
1722getprompt(void *unused __unused)
1723{
1724	static char ps[PROMPTLEN];
1725	char *fmt;
1726	int i, j, trim;
1727	static char internal_error[] = "<internal prompt error>";
1728
1729	/*
1730	 * Select prompt format.
1731	 */
1732	switch (whichprompt) {
1733	case 0:
1734		fmt = nullstr;
1735		break;
1736	case 1:
1737		fmt = ps1val();
1738		break;
1739	case 2:
1740		fmt = ps2val();
1741		break;
1742	default:
1743		return internal_error;
1744	}
1745
1746	/*
1747	 * Format prompt string.
1748	 */
1749	for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1750		if (*fmt == '\\')
1751			switch (*++fmt) {
1752
1753				/*
1754				 * Hostname.
1755				 *
1756				 * \h specifies just the local hostname,
1757				 * \H specifies fully-qualified hostname.
1758				 */
1759			case 'h':
1760			case 'H':
1761				ps[i] = '\0';
1762				gethostname(&ps[i], PROMPTLEN - i);
1763				/* Skip to end of hostname. */
1764				trim = (*fmt == 'h') ? '.' : '\0';
1765				while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1766					i++;
1767				break;
1768
1769				/*
1770				 * Working directory.
1771				 *
1772				 * \W specifies just the final component,
1773				 * \w specifies the entire path.
1774				 */
1775			case 'W':
1776			case 'w':
1777				ps[i] = '\0';
1778				getcwd(&ps[i], PROMPTLEN - i);
1779				if (*fmt == 'W' && ps[i + 1] != '\0') {
1780					/* Final path component only. */
1781					trim = 1;
1782					for (j = i; ps[j] != '\0'; j++)
1783					  if (ps[j] == '/')
1784						trim = j + 1;
1785					memmove(&ps[i], &ps[trim],
1786					    j - trim + 1);
1787				}
1788				/* Skip to end of path. */
1789				while (ps[i + 1] != '\0')
1790					i++;
1791				break;
1792
1793				/*
1794				 * Superuser status.
1795				 *
1796				 * '$' for normal users, '#' for root.
1797				 */
1798			case '$':
1799				ps[i] = (geteuid() != 0) ? '$' : '#';
1800				break;
1801
1802				/*
1803				 * A literal \.
1804				 */
1805			case '\\':
1806				ps[i] = '\\';
1807				break;
1808
1809				/*
1810				 * Emit unrecognized formats verbatim.
1811				 */
1812			default:
1813				ps[i++] = '\\';
1814				ps[i] = *fmt;
1815				break;
1816			}
1817		else
1818			ps[i] = *fmt;
1819	ps[i] = '\0';
1820	return (ps);
1821}
1822