parser.c revision 142845
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: head/bin/sh/parser.c 142845 2005-03-01 03:35:58Z obrien $");
401556Srgrimes
4117987Speter#include <stdlib.h>
4217987Speter
431556Srgrimes#include "shell.h"
441556Srgrimes#include "parser.h"
451556Srgrimes#include "nodes.h"
461556Srgrimes#include "expand.h"	/* defines rmescapes() */
471556Srgrimes#include "syntax.h"
481556Srgrimes#include "options.h"
491556Srgrimes#include "input.h"
501556Srgrimes#include "output.h"
511556Srgrimes#include "var.h"
521556Srgrimes#include "error.h"
531556Srgrimes#include "memalloc.h"
541556Srgrimes#include "mystring.h"
551556Srgrimes#include "alias.h"
5617987Speter#include "show.h"
5759436Scracauer#include "eval.h"
5817987Speter#ifndef NO_HISTORY
591556Srgrimes#include "myhistedit.h"
6017987Speter#endif
611556Srgrimes
621556Srgrimes/*
631556Srgrimes * Shell command parser.
641556Srgrimes */
651556Srgrimes
66142845Sobrien#define	EOFMARKLEN	79
67142845Sobrien#define	PROMPTLEN	128
681556Srgrimes
691556Srgrimes/* values returned by readtoken */
7017987Speter#include "token.h"
711556Srgrimes
721556Srgrimes
731556Srgrimes
741556Srgrimesstruct heredoc {
751556Srgrimes	struct heredoc *next;	/* next here document in list */
761556Srgrimes	union node *here;		/* redirection node */
771556Srgrimes	char *eofmark;		/* string indicating end of input */
781556Srgrimes	int striptabs;		/* if set, strip leading tabs */
791556Srgrimes};
801556Srgrimes
811556Srgrimes
821556Srgrimes
83117261SddsSTATIC struct heredoc *heredoclist;	/* list of here documents to read */
84117261SddsSTATIC int parsebackquote;	/* nonzero if we are inside backquotes */
85117261SddsSTATIC int doprompt;		/* if set, prompt the user */
86117261SddsSTATIC int needprompt;		/* true if interactive and at start of line */
87117261SddsSTATIC int lasttoken;		/* last token read */
881556SrgrimesMKINIT int tokpushback;		/* last token pushed back */
89117261SddsSTATIC char *wordtext;		/* text of last word returned by readtoken */
901556SrgrimesMKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
91117261SddsSTATIC struct nodelist *backquotelist;
92117261SddsSTATIC union node *redirnode;
93117261SddsSTATIC struct heredoc *heredoc;
94117261SddsSTATIC int quoteflag;		/* set if (part of) last token was quoted */
95117261SddsSTATIC int startlinno;		/* line # where last token started */
961556Srgrimes
9718018Speter/* XXX When 'noaliases' is set to one, no alias expansion takes place. */
9818018Speterstatic int noaliases = 0;
991556Srgrimes
1001556Srgrimes#define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
1011556Srgrimes#ifdef GDB_HACK
1021556Srgrimesstatic const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'};
1031556Srgrimesstatic const char types[] = "}-+?=";
1041556Srgrimes#endif
1051556Srgrimes
1061556Srgrimes
10790111SimpSTATIC union node *list(int);
10890111SimpSTATIC union node *andor(void);
10990111SimpSTATIC union node *pipeline(void);
11090111SimpSTATIC union node *command(void);
11190111SimpSTATIC union node *simplecmd(union node **, union node *);
11290111SimpSTATIC union node *makename(void);
11390111SimpSTATIC void parsefname(void);
11490111SimpSTATIC void parseheredoc(void);
11590111SimpSTATIC int peektoken(void);
11690111SimpSTATIC int readtoken(void);
11790111SimpSTATIC int xxreadtoken(void);
11890111SimpSTATIC int readtoken1(int, char const *, char *, int);
11990111SimpSTATIC int noexpand(char *);
12090111SimpSTATIC void synexpect(int);
12190111SimpSTATIC void synerror(char *);
12290111SimpSTATIC void setprompt(int);
1231556Srgrimes
12417987Speter
1251556Srgrimes/*
1261556Srgrimes * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
1271556Srgrimes * valid parse tree indicating a blank line.)
1281556Srgrimes */
1291556Srgrimes
1301556Srgrimesunion node *
13190111Simpparsecmd(int interact)
13217987Speter{
1331556Srgrimes	int t;
1341556Srgrimes
13560593Scracauer	tokpushback = 0;
1361556Srgrimes	doprompt = interact;
1371556Srgrimes	if (doprompt)
1381556Srgrimes		setprompt(1);
1391556Srgrimes	else
1401556Srgrimes		setprompt(0);
1411556Srgrimes	needprompt = 0;
1421556Srgrimes	t = readtoken();
1431556Srgrimes	if (t == TEOF)
1441556Srgrimes		return NEOF;
1451556Srgrimes	if (t == TNL)
1461556Srgrimes		return NULL;
1471556Srgrimes	tokpushback++;
1481556Srgrimes	return list(1);
1491556Srgrimes}
1501556Srgrimes
1511556Srgrimes
1521556SrgrimesSTATIC union node *
15390111Simplist(int nlflag)
15417987Speter{
1551556Srgrimes	union node *n1, *n2, *n3;
15617987Speter	int tok;
1571556Srgrimes
1581556Srgrimes	checkkwd = 2;
1591556Srgrimes	if (nlflag == 0 && tokendlist[peektoken()])
1601556Srgrimes		return NULL;
16117987Speter	n1 = NULL;
1621556Srgrimes	for (;;) {
16317987Speter		n2 = andor();
16417987Speter		tok = readtoken();
16517987Speter		if (tok == TBACKGND) {
16617987Speter			if (n2->type == NCMD || n2->type == NPIPE) {
16717987Speter				n2->ncmd.backgnd = 1;
16817987Speter			} else if (n2->type == NREDIR) {
16917987Speter				n2->type = NBACKGND;
17017987Speter			} else {
17117987Speter				n3 = (union node *)stalloc(sizeof (struct nredir));
17217987Speter				n3->type = NBACKGND;
17317987Speter				n3->nredir.n = n2;
17417987Speter				n3->nredir.redirect = NULL;
17517987Speter				n2 = n3;
17617987Speter			}
17717987Speter		}
17817987Speter		if (n1 == NULL) {
17917987Speter			n1 = n2;
18017987Speter		}
18117987Speter		else {
18217987Speter			n3 = (union node *)stalloc(sizeof (struct nbinary));
18317987Speter			n3->type = NSEMI;
18417987Speter			n3->nbinary.ch1 = n1;
18517987Speter			n3->nbinary.ch2 = n2;
18617987Speter			n1 = n3;
18717987Speter		}
18817987Speter		switch (tok) {
18913882Sjoerg		case TBACKGND:
19017987Speter		case TSEMI:
19117987Speter			tok = readtoken();
192102410Scharnier			/* FALLTHROUGH */
1931556Srgrimes		case TNL:
19417987Speter			if (tok == TNL) {
19517987Speter				parseheredoc();
19617987Speter				if (nlflag)
19717987Speter					return n1;
19817987Speter			} else {
19917987Speter				tokpushback++;
20017987Speter			}
2011556Srgrimes			checkkwd = 2;
2021556Srgrimes			if (tokendlist[peektoken()])
2031556Srgrimes				return n1;
2041556Srgrimes			break;
2051556Srgrimes		case TEOF:
2061556Srgrimes			if (heredoclist)
2071556Srgrimes				parseheredoc();
2081556Srgrimes			else
2091556Srgrimes				pungetc();		/* push back EOF on input */
2101556Srgrimes			return n1;
2111556Srgrimes		default:
2121556Srgrimes			if (nlflag)
2131556Srgrimes				synexpect(-1);
2141556Srgrimes			tokpushback++;
2151556Srgrimes			return n1;
2161556Srgrimes		}
2171556Srgrimes	}
2181556Srgrimes}
2191556Srgrimes
2201556Srgrimes
2211556Srgrimes
2221556SrgrimesSTATIC union node *
22390111Simpandor(void)
22490111Simp{
2251556Srgrimes	union node *n1, *n2, *n3;
2261556Srgrimes	int t;
2271556Srgrimes
2281556Srgrimes	n1 = pipeline();
2291556Srgrimes	for (;;) {
2301556Srgrimes		if ((t = readtoken()) == TAND) {
2311556Srgrimes			t = NAND;
2321556Srgrimes		} else if (t == TOR) {
2331556Srgrimes			t = NOR;
2341556Srgrimes		} else {
2351556Srgrimes			tokpushback++;
2361556Srgrimes			return n1;
2371556Srgrimes		}
2381556Srgrimes		n2 = pipeline();
2391556Srgrimes		n3 = (union node *)stalloc(sizeof (struct nbinary));
2401556Srgrimes		n3->type = t;
2411556Srgrimes		n3->nbinary.ch1 = n1;
2421556Srgrimes		n3->nbinary.ch2 = n2;
2431556Srgrimes		n1 = n3;
2441556Srgrimes	}
2451556Srgrimes}
2461556Srgrimes
2471556Srgrimes
2481556Srgrimes
2491556SrgrimesSTATIC union node *
25090111Simppipeline(void)
25190111Simp{
25275336Sbrian	union node *n1, *n2, *pipenode;
2531556Srgrimes	struct nodelist *lp, *prev;
25475336Sbrian	int negate;
2551556Srgrimes
25675336Sbrian	negate = 0;
2571556Srgrimes	TRACE(("pipeline: entered\n"));
25875336Sbrian	while (readtoken() == TNOT)
25975336Sbrian		negate = !negate;
26075336Sbrian	tokpushback++;
2611556Srgrimes	n1 = command();
2621556Srgrimes	if (readtoken() == TPIPE) {
2631556Srgrimes		pipenode = (union node *)stalloc(sizeof (struct npipe));
2641556Srgrimes		pipenode->type = NPIPE;
2651556Srgrimes		pipenode->npipe.backgnd = 0;
2661556Srgrimes		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
2671556Srgrimes		pipenode->npipe.cmdlist = lp;
2681556Srgrimes		lp->n = n1;
2691556Srgrimes		do {
2701556Srgrimes			prev = lp;
2711556Srgrimes			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
2721556Srgrimes			lp->n = command();
2731556Srgrimes			prev->next = lp;
2741556Srgrimes		} while (readtoken() == TPIPE);
2751556Srgrimes		lp->next = NULL;
2761556Srgrimes		n1 = pipenode;
2771556Srgrimes	}
2781556Srgrimes	tokpushback++;
27975336Sbrian	if (negate) {
28075336Sbrian		n2 = (union node *)stalloc(sizeof (struct nnot));
28175336Sbrian		n2->type = NNOT;
28275336Sbrian		n2->nnot.com = n1;
28375336Sbrian		return n2;
28475336Sbrian	} else
28575336Sbrian		return n1;
2861556Srgrimes}
2871556Srgrimes
2881556Srgrimes
2891556Srgrimes
2901556SrgrimesSTATIC union node *
29190111Simpcommand(void)
29290111Simp{
2931556Srgrimes	union node *n1, *n2;
2941556Srgrimes	union node *ap, **app;
2951556Srgrimes	union node *cp, **cpp;
2961556Srgrimes	union node *redir, **rpp;
29775160Sbrian	int t, negate = 0;
2981556Srgrimes
2991556Srgrimes	checkkwd = 2;
30017987Speter	redir = NULL;
30117987Speter	n1 = NULL;
3021556Srgrimes	rpp = &redir;
30320425Ssteve
3041556Srgrimes	/* Check for redirection which may precede command */
3051556Srgrimes	while (readtoken() == TREDIR) {
3061556Srgrimes		*rpp = n2 = redirnode;
3071556Srgrimes		rpp = &n2->nfile.next;
3081556Srgrimes		parsefname();
3091556Srgrimes	}
3101556Srgrimes	tokpushback++;
3111556Srgrimes
31275160Sbrian	while (readtoken() == TNOT) {
31375160Sbrian		TRACE(("command: TNOT recognized\n"));
31475160Sbrian		negate = !negate;
31575160Sbrian	}
31675160Sbrian	tokpushback++;
31775160Sbrian
3181556Srgrimes	switch (readtoken()) {
3191556Srgrimes	case TIF:
3201556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nif));
3211556Srgrimes		n1->type = NIF;
322104554Stjr		if ((n1->nif.test = list(0)) == NULL)
323104554Stjr			synexpect(-1);
3241556Srgrimes		if (readtoken() != TTHEN)
3251556Srgrimes			synexpect(TTHEN);
3261556Srgrimes		n1->nif.ifpart = list(0);
3271556Srgrimes		n2 = n1;
3281556Srgrimes		while (readtoken() == TELIF) {
3291556Srgrimes			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
3301556Srgrimes			n2 = n2->nif.elsepart;
3311556Srgrimes			n2->type = NIF;
332104554Stjr			if ((n2->nif.test = list(0)) == NULL)
333104554Stjr				synexpect(-1);
3341556Srgrimes			if (readtoken() != TTHEN)
3351556Srgrimes				synexpect(TTHEN);
3361556Srgrimes			n2->nif.ifpart = list(0);
3371556Srgrimes		}
3381556Srgrimes		if (lasttoken == TELSE)
3391556Srgrimes			n2->nif.elsepart = list(0);
3401556Srgrimes		else {
3411556Srgrimes			n2->nif.elsepart = NULL;
3421556Srgrimes			tokpushback++;
3431556Srgrimes		}
3441556Srgrimes		if (readtoken() != TFI)
3451556Srgrimes			synexpect(TFI);
3461556Srgrimes		checkkwd = 1;
3471556Srgrimes		break;
3481556Srgrimes	case TWHILE:
3491556Srgrimes	case TUNTIL: {
3501556Srgrimes		int got;
3511556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nbinary));
3521556Srgrimes		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
353104554Stjr		if ((n1->nbinary.ch1 = list(0)) == NULL)
354104554Stjr			synexpect(-1);
3551556Srgrimes		if ((got=readtoken()) != TDO) {
3561556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
3571556Srgrimes			synexpect(TDO);
3581556Srgrimes		}
3591556Srgrimes		n1->nbinary.ch2 = list(0);
3601556Srgrimes		if (readtoken() != TDONE)
3611556Srgrimes			synexpect(TDONE);
3621556Srgrimes		checkkwd = 1;
3631556Srgrimes		break;
3641556Srgrimes	}
3651556Srgrimes	case TFOR:
3661556Srgrimes		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
3671556Srgrimes			synerror("Bad for loop variable");
3681556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nfor));
3691556Srgrimes		n1->type = NFOR;
3701556Srgrimes		n1->nfor.var = wordtext;
3711556Srgrimes		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
3721556Srgrimes			app = &ap;
3731556Srgrimes			while (readtoken() == TWORD) {
3741556Srgrimes				n2 = (union node *)stalloc(sizeof (struct narg));
3751556Srgrimes				n2->type = NARG;
3761556Srgrimes				n2->narg.text = wordtext;
3771556Srgrimes				n2->narg.backquote = backquotelist;
3781556Srgrimes				*app = n2;
3791556Srgrimes				app = &n2->narg.next;
3801556Srgrimes			}
3811556Srgrimes			*app = NULL;
3821556Srgrimes			n1->nfor.args = ap;
3831556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
3841556Srgrimes				synexpect(-1);
3851556Srgrimes		} else {
3861556Srgrimes#ifndef GDB_HACK
3871556Srgrimes			static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
3881556Srgrimes								   '@', '=', '\0'};
3891556Srgrimes#endif
3901556Srgrimes			n2 = (union node *)stalloc(sizeof (struct narg));
3911556Srgrimes			n2->type = NARG;
3921556Srgrimes			n2->narg.text = (char *)argvars;
3931556Srgrimes			n2->narg.backquote = NULL;
3941556Srgrimes			n2->narg.next = NULL;
3951556Srgrimes			n1->nfor.args = n2;
3961556Srgrimes			/*
3971556Srgrimes			 * Newline or semicolon here is optional (but note
3981556Srgrimes			 * that the original Bourne shell only allowed NL).
3991556Srgrimes			 */
4001556Srgrimes			if (lasttoken != TNL && lasttoken != TSEMI)
4011556Srgrimes				tokpushback++;
4021556Srgrimes		}
4031556Srgrimes		checkkwd = 2;
4041556Srgrimes		if ((t = readtoken()) == TDO)
4051556Srgrimes			t = TDONE;
4061556Srgrimes		else if (t == TBEGIN)
4071556Srgrimes			t = TEND;
4081556Srgrimes		else
4091556Srgrimes			synexpect(-1);
4101556Srgrimes		n1->nfor.body = list(0);
4111556Srgrimes		if (readtoken() != t)
4121556Srgrimes			synexpect(t);
4131556Srgrimes		checkkwd = 1;
4141556Srgrimes		break;
4151556Srgrimes	case TCASE:
4161556Srgrimes		n1 = (union node *)stalloc(sizeof (struct ncase));
4171556Srgrimes		n1->type = NCASE;
4181556Srgrimes		if (readtoken() != TWORD)
4191556Srgrimes			synexpect(TWORD);
4201556Srgrimes		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
4211556Srgrimes		n2->type = NARG;
4221556Srgrimes		n2->narg.text = wordtext;
4231556Srgrimes		n2->narg.backquote = backquotelist;
4241556Srgrimes		n2->narg.next = NULL;
4251556Srgrimes		while (readtoken() == TNL);
4261556Srgrimes		if (lasttoken != TWORD || ! equal(wordtext, "in"))
4271556Srgrimes			synerror("expecting \"in\"");
4281556Srgrimes		cpp = &n1->ncase.cases;
42918018Speter		noaliases = 1;	/* turn off alias expansion */
4302760Ssef		checkkwd = 2, readtoken();
431104202Stjr		while (lasttoken != TESAC) {
4321556Srgrimes			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
4331556Srgrimes			cp->type = NCLIST;
4341556Srgrimes			app = &cp->nclist.pattern;
435104207Stjr			if (lasttoken == TLP)
436104207Stjr				readtoken();
4371556Srgrimes			for (;;) {
4381556Srgrimes				*app = ap = (union node *)stalloc(sizeof (struct narg));
4391556Srgrimes				ap->type = NARG;
4401556Srgrimes				ap->narg.text = wordtext;
4411556Srgrimes				ap->narg.backquote = backquotelist;
4422760Ssef				if (checkkwd = 2, readtoken() != TPIPE)
4431556Srgrimes					break;
4441556Srgrimes				app = &ap->narg.next;
4452760Ssef				readtoken();
4461556Srgrimes			}
4471556Srgrimes			ap->narg.next = NULL;
4481556Srgrimes			if (lasttoken != TRP)
44918018Speter				noaliases = 0, synexpect(TRP);
4501556Srgrimes			cp->nclist.body = list(0);
4512760Ssef
4522760Ssef			checkkwd = 2;
4532760Ssef			if ((t = readtoken()) != TESAC) {
4542760Ssef				if (t != TENDCASE)
45518018Speter					noaliases = 0, synexpect(TENDCASE);
4562760Ssef				else
4572760Ssef					checkkwd = 2, readtoken();
4582760Ssef			}
4591556Srgrimes			cpp = &cp->nclist.next;
460104202Stjr		}
46118018Speter		noaliases = 0;	/* reset alias expansion */
4621556Srgrimes		*cpp = NULL;
4631556Srgrimes		checkkwd = 1;
4641556Srgrimes		break;
4651556Srgrimes	case TLP:
4661556Srgrimes		n1 = (union node *)stalloc(sizeof (struct nredir));
4671556Srgrimes		n1->type = NSUBSHELL;
4681556Srgrimes		n1->nredir.n = list(0);
4691556Srgrimes		n1->nredir.redirect = NULL;
4701556Srgrimes		if (readtoken() != TRP)
4711556Srgrimes			synexpect(TRP);
4721556Srgrimes		checkkwd = 1;
4731556Srgrimes		break;
4741556Srgrimes	case TBEGIN:
4751556Srgrimes		n1 = list(0);
4761556Srgrimes		if (readtoken() != TEND)
4771556Srgrimes			synexpect(TEND);
4781556Srgrimes		checkkwd = 1;
4791556Srgrimes		break;
4801556Srgrimes	/* Handle an empty command like other simple commands.  */
48117987Speter	case TSEMI:
482101662Stjr	case TAND:
483101662Stjr	case TOR:
48417987Speter		/*
48517987Speter		 * An empty command before a ; doesn't make much sense, and
48617987Speter		 * should certainly be disallowed in the case of `if ;'.
48717987Speter		 */
48817987Speter		if (!redir)
48917987Speter			synexpect(-1);
4901556Srgrimes	case TNL:
49110399Sjoerg	case TEOF:
4921556Srgrimes	case TWORD:
49317987Speter	case TRP:
4941556Srgrimes		tokpushback++;
49575160Sbrian		n1 = simplecmd(rpp, redir);
49675160Sbrian		goto checkneg;
4971556Srgrimes	default:
4981556Srgrimes		synexpect(-1);
4991556Srgrimes	}
5001556Srgrimes
5011556Srgrimes	/* Now check for redirection which may follow command */
5021556Srgrimes	while (readtoken() == TREDIR) {
5031556Srgrimes		*rpp = n2 = redirnode;
5041556Srgrimes		rpp = &n2->nfile.next;
5051556Srgrimes		parsefname();
5061556Srgrimes	}
5071556Srgrimes	tokpushback++;
5081556Srgrimes	*rpp = NULL;
5091556Srgrimes	if (redir) {
5101556Srgrimes		if (n1->type != NSUBSHELL) {
5111556Srgrimes			n2 = (union node *)stalloc(sizeof (struct nredir));
5121556Srgrimes			n2->type = NREDIR;
5131556Srgrimes			n2->nredir.n = n1;
5141556Srgrimes			n1 = n2;
5151556Srgrimes		}
5161556Srgrimes		n1->nredir.redirect = redir;
5171556Srgrimes	}
51875160Sbrian
51975160Sbriancheckneg:
52075160Sbrian	if (negate) {
52175160Sbrian		n2 = (union node *)stalloc(sizeof (struct nnot));
52275160Sbrian		n2->type = NNOT;
52375160Sbrian		n2->nnot.com = n1;
52475160Sbrian		return n2;
52575160Sbrian	}
52675160Sbrian	else
52775160Sbrian		return n1;
5281556Srgrimes}
5291556Srgrimes
5301556Srgrimes
5311556SrgrimesSTATIC union node *
53290111Simpsimplecmd(union node **rpp, union node *redir)
53390111Simp{
5341556Srgrimes	union node *args, **app;
5351556Srgrimes	union node **orig_rpp = rpp;
53675160Sbrian	union node *n = NULL, *n2;
53775160Sbrian	int negate = 0;
5381556Srgrimes
5391556Srgrimes	/* If we don't have any redirections already, then we must reset */
5401556Srgrimes	/* rpp to be the address of the local redir variable.  */
5411556Srgrimes	if (redir == 0)
5421556Srgrimes		rpp = &redir;
5431556Srgrimes
5441556Srgrimes	args = NULL;
5451556Srgrimes	app = &args;
5468855Srgrimes	/*
5471556Srgrimes	 * We save the incoming value, because we need this for shell
5481556Srgrimes	 * functions.  There can not be a redirect or an argument between
5498855Srgrimes	 * the function name and the open parenthesis.
5501556Srgrimes	 */
5511556Srgrimes	orig_rpp = rpp;
5521556Srgrimes
55375160Sbrian	while (readtoken() == TNOT) {
55475160Sbrian		TRACE(("command: TNOT recognized\n"));
55575160Sbrian		negate = !negate;
55675160Sbrian	}
55775160Sbrian	tokpushback++;
55875160Sbrian
5591556Srgrimes	for (;;) {
5601556Srgrimes		if (readtoken() == TWORD) {
5611556Srgrimes			n = (union node *)stalloc(sizeof (struct narg));
5621556Srgrimes			n->type = NARG;
5631556Srgrimes			n->narg.text = wordtext;
5641556Srgrimes			n->narg.backquote = backquotelist;
5651556Srgrimes			*app = n;
5661556Srgrimes			app = &n->narg.next;
5671556Srgrimes		} else if (lasttoken == TREDIR) {
5681556Srgrimes			*rpp = n = redirnode;
5691556Srgrimes			rpp = &n->nfile.next;
5701556Srgrimes			parsefname();	/* read name of redirection file */
5711556Srgrimes		} else if (lasttoken == TLP && app == &args->narg.next
5721556Srgrimes					    && rpp == orig_rpp) {
5731556Srgrimes			/* We have a function */
5741556Srgrimes			if (readtoken() != TRP)
5751556Srgrimes				synexpect(TRP);
5761556Srgrimes#ifdef notdef
5771556Srgrimes			if (! goodname(n->narg.text))
5781556Srgrimes				synerror("Bad function name");
5791556Srgrimes#endif
5801556Srgrimes			n->type = NDEFUN;
5811556Srgrimes			n->narg.next = command();
58275160Sbrian			goto checkneg;
5831556Srgrimes		} else {
5841556Srgrimes			tokpushback++;
5851556Srgrimes			break;
5861556Srgrimes		}
5871556Srgrimes	}
5881556Srgrimes	*app = NULL;
5891556Srgrimes	*rpp = NULL;
5901556Srgrimes	n = (union node *)stalloc(sizeof (struct ncmd));
5911556Srgrimes	n->type = NCMD;
5921556Srgrimes	n->ncmd.backgnd = 0;
5931556Srgrimes	n->ncmd.args = args;
5941556Srgrimes	n->ncmd.redirect = redir;
59575160Sbrian
59675160Sbriancheckneg:
59775160Sbrian	if (negate) {
59875160Sbrian		n2 = (union node *)stalloc(sizeof (struct nnot));
59975160Sbrian		n2->type = NNOT;
60075160Sbrian		n2->nnot.com = n;
60175160Sbrian		return n2;
60275160Sbrian	}
60375160Sbrian	else
60475160Sbrian		return n;
6051556Srgrimes}
6061556Srgrimes
60717987SpeterSTATIC union node *
60890111Simpmakename(void)
60990111Simp{
61017987Speter	union node *n;
6111556Srgrimes
61217987Speter	n = (union node *)stalloc(sizeof (struct narg));
61317987Speter	n->type = NARG;
61417987Speter	n->narg.next = NULL;
61517987Speter	n->narg.text = wordtext;
61617987Speter	n->narg.backquote = backquotelist;
61717987Speter	return n;
61817987Speter}
61917987Speter
62090111Simpvoid fixredir(union node *n, const char *text, int err)
62190111Simp{
62217987Speter	TRACE(("Fix redir %s %d\n", text, err));
62317987Speter	if (!err)
62417987Speter		n->ndup.vname = NULL;
62517987Speter
62617987Speter	if (is_digit(text[0]) && text[1] == '\0')
62717987Speter		n->ndup.dupfd = digit_val(text[0]);
62817987Speter	else if (text[0] == '-' && text[1] == '\0')
62917987Speter		n->ndup.dupfd = -1;
63017987Speter	else {
63120425Ssteve
63217987Speter		if (err)
63317987Speter			synerror("Bad fd number");
63417987Speter		else
63517987Speter			n->ndup.vname = makename();
63617987Speter	}
63717987Speter}
63817987Speter
63917987Speter
6401556SrgrimesSTATIC void
64190111Simpparsefname(void)
64290111Simp{
6431556Srgrimes	union node *n = redirnode;
6441556Srgrimes
6451556Srgrimes	if (readtoken() != TWORD)
6461556Srgrimes		synexpect(-1);
6471556Srgrimes	if (n->type == NHERE) {
6481556Srgrimes		struct heredoc *here = heredoc;
6491556Srgrimes		struct heredoc *p;
6501556Srgrimes		int i;
6511556Srgrimes
6521556Srgrimes		if (quoteflag == 0)
6531556Srgrimes			n->type = NXHERE;
6541556Srgrimes		TRACE(("Here document %d\n", n->type));
6551556Srgrimes		if (here->striptabs) {
6561556Srgrimes			while (*wordtext == '\t')
6571556Srgrimes				wordtext++;
6581556Srgrimes		}
6591556Srgrimes		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
6601556Srgrimes			synerror("Illegal eof marker for << redirection");
6611556Srgrimes		rmescapes(wordtext);
6621556Srgrimes		here->eofmark = wordtext;
6631556Srgrimes		here->next = NULL;
6641556Srgrimes		if (heredoclist == NULL)
6651556Srgrimes			heredoclist = here;
6661556Srgrimes		else {
6671556Srgrimes			for (p = heredoclist ; p->next ; p = p->next);
6681556Srgrimes			p->next = here;
6691556Srgrimes		}
6701556Srgrimes	} else if (n->type == NTOFD || n->type == NFROMFD) {
67117987Speter		fixredir(n, wordtext, 0);
6721556Srgrimes	} else {
67317987Speter		n->nfile.fname = makename();
6741556Srgrimes	}
6751556Srgrimes}
6761556Srgrimes
6771556Srgrimes
6781556Srgrimes/*
6791556Srgrimes * Input any here documents.
6801556Srgrimes */
6811556Srgrimes
6821556SrgrimesSTATIC void
68390111Simpparseheredoc(void)
68490111Simp{
6851556Srgrimes	struct heredoc *here;
6861556Srgrimes	union node *n;
6871556Srgrimes
6881556Srgrimes	while (heredoclist) {
6891556Srgrimes		here = heredoclist;
6901556Srgrimes		heredoclist = here->next;
6911556Srgrimes		if (needprompt) {
6921556Srgrimes			setprompt(2);
6931556Srgrimes			needprompt = 0;
6941556Srgrimes		}
6951556Srgrimes		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
6961556Srgrimes				here->eofmark, here->striptabs);
6971556Srgrimes		n = (union node *)stalloc(sizeof (struct narg));
6981556Srgrimes		n->narg.type = NARG;
6991556Srgrimes		n->narg.next = NULL;
7001556Srgrimes		n->narg.text = wordtext;
7011556Srgrimes		n->narg.backquote = backquotelist;
7021556Srgrimes		here->here->nhere.doc = n;
7031556Srgrimes	}
7041556Srgrimes}
7051556Srgrimes
7061556SrgrimesSTATIC int
70790111Simppeektoken(void)
70890111Simp{
7091556Srgrimes	int t;
7101556Srgrimes
7111556Srgrimes	t = readtoken();
7121556Srgrimes	tokpushback++;
7131556Srgrimes	return (t);
7141556Srgrimes}
7151556Srgrimes
7161556SrgrimesSTATIC int
71790111Simpreadtoken(void)
71890111Simp{
7191556Srgrimes	int t;
7201556Srgrimes	int savecheckkwd = checkkwd;
7211556Srgrimes	struct alias *ap;
7221556Srgrimes#ifdef DEBUG
7231556Srgrimes	int alreadyseen = tokpushback;
7241556Srgrimes#endif
7258855Srgrimes
7261556Srgrimes	top:
7271556Srgrimes	t = xxreadtoken();
7281556Srgrimes
7291556Srgrimes	if (checkkwd) {
7301556Srgrimes		/*
7311556Srgrimes		 * eat newlines
7321556Srgrimes		 */
7331556Srgrimes		if (checkkwd == 2) {
7341556Srgrimes			checkkwd = 0;
7351556Srgrimes			while (t == TNL) {
7361556Srgrimes				parseheredoc();
7371556Srgrimes				t = xxreadtoken();
7381556Srgrimes			}
7391556Srgrimes		} else
7401556Srgrimes			checkkwd = 0;
7411556Srgrimes		/*
7421556Srgrimes		 * check for keywords and aliases
7431556Srgrimes		 */
74420425Ssteve		if (t == TWORD && !quoteflag)
74517987Speter		{
74698463Sjmallett			const char * const *pp;
7471556Srgrimes
74898463Sjmallett			for (pp = parsekwd; *pp; pp++) {
74920425Ssteve				if (**pp == *wordtext && equal(*pp, wordtext))
75017987Speter				{
7511556Srgrimes					lasttoken = t = pp - parsekwd + KWDOFFSET;
7521556Srgrimes					TRACE(("keyword %s recognized\n", tokname[t]));
7531556Srgrimes					goto out;
7541556Srgrimes				}
7551556Srgrimes			}
75618018Speter			if (noaliases == 0 &&
75718018Speter			    (ap = lookupalias(wordtext, 1)) != NULL) {
7581556Srgrimes				pushstring(ap->val, strlen(ap->val), ap);
7591556Srgrimes				checkkwd = savecheckkwd;
7601556Srgrimes				goto top;
7611556Srgrimes			}
7621556Srgrimes		}
7631556Srgrimesout:
76475160Sbrian		checkkwd = (t == TNOT) ? savecheckkwd : 0;
7651556Srgrimes	}
7661556Srgrimes#ifdef DEBUG
7671556Srgrimes	if (!alreadyseen)
7681556Srgrimes	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
7691556Srgrimes	else
7701556Srgrimes	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
7711556Srgrimes#endif
7721556Srgrimes	return (t);
7731556Srgrimes}
7741556Srgrimes
7751556Srgrimes
7761556Srgrimes/*
7771556Srgrimes * Read the next input token.
7781556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in
7791556Srgrimes *	backquotes.  We set quoteflag to true if any part of the word was
7801556Srgrimes *	quoted.
7811556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing
7821556Srgrimes *	the redirection.
7831556Srgrimes * In all cases, the variable startlinno is set to the number of the line
7841556Srgrimes *	on which the token starts.
7851556Srgrimes *
7861556Srgrimes * [Change comment:  here documents and internal procedures]
7871556Srgrimes * [Readtoken shouldn't have any arguments.  Perhaps we should make the
7881556Srgrimes *  word parsing code into a separate routine.  In this case, readtoken
7891556Srgrimes *  doesn't need to have any internal procedures, but parseword does.
7901556Srgrimes *  We could also make parseoperator in essence the main routine, and
7911556Srgrimes *  have parseword (readtoken1?) handle both words and redirection.]
7921556Srgrimes */
7931556Srgrimes
7941556Srgrimes#define RETURN(token)	return lasttoken = token
7951556Srgrimes
7961556SrgrimesSTATIC int
79790111Simpxxreadtoken(void)
79890111Simp{
79925230Ssteve	int c;
8001556Srgrimes
8011556Srgrimes	if (tokpushback) {
8021556Srgrimes		tokpushback = 0;
8031556Srgrimes		return lasttoken;
8041556Srgrimes	}
8051556Srgrimes	if (needprompt) {
8061556Srgrimes		setprompt(2);
8071556Srgrimes		needprompt = 0;
8081556Srgrimes	}
8091556Srgrimes	startlinno = plinno;
8101556Srgrimes	for (;;) {	/* until token or start of word found */
8111556Srgrimes		c = pgetc_macro();
8121556Srgrimes		if (c == ' ' || c == '\t')
8131556Srgrimes			continue;		/* quick check for white space first */
8141556Srgrimes		switch (c) {
8151556Srgrimes		case ' ': case '\t':
8161556Srgrimes			continue;
8171556Srgrimes		case '#':
8181556Srgrimes			while ((c = pgetc()) != '\n' && c != PEOF);
8191556Srgrimes			pungetc();
8201556Srgrimes			continue;
8211556Srgrimes		case '\\':
8221556Srgrimes			if (pgetc() == '\n') {
8231556Srgrimes				startlinno = ++plinno;
8241556Srgrimes				if (doprompt)
8251556Srgrimes					setprompt(2);
8261556Srgrimes				else
8271556Srgrimes					setprompt(0);
8281556Srgrimes				continue;
8291556Srgrimes			}
8301556Srgrimes			pungetc();
8311556Srgrimes			goto breakloop;
8321556Srgrimes		case '\n':
8331556Srgrimes			plinno++;
8341556Srgrimes			needprompt = doprompt;
8351556Srgrimes			RETURN(TNL);
8361556Srgrimes		case PEOF:
8371556Srgrimes			RETURN(TEOF);
8381556Srgrimes		case '&':
8391556Srgrimes			if (pgetc() == '&')
8401556Srgrimes				RETURN(TAND);
8411556Srgrimes			pungetc();
8421556Srgrimes			RETURN(TBACKGND);
8431556Srgrimes		case '|':
8441556Srgrimes			if (pgetc() == '|')
8451556Srgrimes				RETURN(TOR);
8461556Srgrimes			pungetc();
8471556Srgrimes			RETURN(TPIPE);
8481556Srgrimes		case ';':
8491556Srgrimes			if (pgetc() == ';')
8501556Srgrimes				RETURN(TENDCASE);
8511556Srgrimes			pungetc();
8521556Srgrimes			RETURN(TSEMI);
8531556Srgrimes		case '(':
8541556Srgrimes			RETURN(TLP);
8551556Srgrimes		case ')':
8561556Srgrimes			RETURN(TRP);
8571556Srgrimes		default:
8581556Srgrimes			goto breakloop;
8591556Srgrimes		}
8601556Srgrimes	}
8611556Srgrimesbreakloop:
8621556Srgrimes	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
8631556Srgrimes#undef RETURN
8641556Srgrimes}
8651556Srgrimes
8661556Srgrimes
8671556Srgrimes
8681556Srgrimes/*
8691556Srgrimes * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
8701556Srgrimes * is not NULL, read a here document.  In the latter case, eofmark is the
8711556Srgrimes * word which marks the end of the document and striptabs is true if
8721556Srgrimes * leading tabs should be stripped from the document.  The argument firstc
8731556Srgrimes * is the first character of the input token or document.
8741556Srgrimes *
8751556Srgrimes * Because C does not have internal subroutines, I have simulated them
8761556Srgrimes * using goto's to implement the subroutine linkage.  The following macros
8771556Srgrimes * will run code that appears at the end of readtoken1.
8781556Srgrimes */
8791556Srgrimes
8801556Srgrimes#define CHECKEND()	{goto checkend; checkend_return:;}
8811556Srgrimes#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
8821556Srgrimes#define PARSESUB()	{goto parsesub; parsesub_return:;}
8831556Srgrimes#define PARSEBACKQOLD()	{oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
8841556Srgrimes#define PARSEBACKQNEW()	{oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
8851556Srgrimes#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
8861556Srgrimes
8871556SrgrimesSTATIC int
88890111Simpreadtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
88990111Simp{
89017987Speter	int c = firstc;
89117987Speter	char *out;
8921556Srgrimes	int len;
8931556Srgrimes	char line[EOFMARKLEN + 1];
8941556Srgrimes	struct nodelist *bqlist;
8951556Srgrimes	int quotef;
8961556Srgrimes	int dblquote;
8971556Srgrimes	int varnest;	/* levels of variables expansion */
8981556Srgrimes	int arinest;	/* levels of arithmetic expansion */
8991556Srgrimes	int parenlevel;	/* levels of parens in arithmetic */
9001556Srgrimes	int oldstyle;
9011556Srgrimes	char const *prevsyntax;	/* syntax before arithmetic */
90254679Scracauer	int synentry;
90317987Speter#if __GNUC__
90417987Speter	/* Avoid longjmp clobbering */
90517987Speter	(void) &out;
90617987Speter	(void) &quotef;
90717987Speter	(void) &dblquote;
90817987Speter	(void) &varnest;
90917987Speter	(void) &arinest;
91017987Speter	(void) &parenlevel;
91117987Speter	(void) &oldstyle;
91217987Speter	(void) &prevsyntax;
91317987Speter	(void) &syntax;
91454679Scracauer	(void) &synentry;
91517987Speter#endif
9161556Srgrimes
9171556Srgrimes	startlinno = plinno;
9181556Srgrimes	dblquote = 0;
9191556Srgrimes	if (syntax == DQSYNTAX)
9201556Srgrimes		dblquote = 1;
9211556Srgrimes	quotef = 0;
9221556Srgrimes	bqlist = NULL;
9231556Srgrimes	varnest = 0;
9241556Srgrimes	arinest = 0;
9251556Srgrimes	parenlevel = 0;
9261556Srgrimes
9271556Srgrimes	STARTSTACKSTR(out);
9281556Srgrimes	loop: {	/* for each line, until end of word */
9291556Srgrimes		CHECKEND();	/* set c to PEOF if at end of here document */
9301556Srgrimes		for (;;) {	/* until end of line or end of word */
9311556Srgrimes			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
93254679Scracauer
93364705Scracauer			synentry = syntax[c];
93454679Scracauer
93554679Scracauer			switch(synentry) {
9361556Srgrimes			case CNL:	/* '\n' */
9371556Srgrimes				if (syntax == BASESYNTAX)
9381556Srgrimes					goto endword;	/* exit outer loop */
9391556Srgrimes				USTPUTC(c, out);
9401556Srgrimes				plinno++;
9411556Srgrimes				if (doprompt)
9421556Srgrimes					setprompt(2);
9431556Srgrimes				else
9441556Srgrimes					setprompt(0);
9451556Srgrimes				c = pgetc();
9461556Srgrimes				goto loop;		/* continue outer loop */
9471556Srgrimes			case CWORD:
9481556Srgrimes				USTPUTC(c, out);
9491556Srgrimes				break;
9501556Srgrimes			case CCTL:
9511556Srgrimes				if (eofmark == NULL || dblquote)
9521556Srgrimes					USTPUTC(CTLESC, out);
9531556Srgrimes				USTPUTC(c, out);
9541556Srgrimes				break;
9551556Srgrimes			case CBACK:	/* backslash */
9561556Srgrimes				c = pgetc();
9571556Srgrimes				if (c == PEOF) {
9581556Srgrimes					USTPUTC('\\', out);
9591556Srgrimes					pungetc();
9601556Srgrimes				} else if (c == '\n') {
9611556Srgrimes					if (doprompt)
9621556Srgrimes						setprompt(2);
9631556Srgrimes					else
9641556Srgrimes						setprompt(0);
9651556Srgrimes				} else {
96654631Scracauer					if (dblquote && c != '\\' &&
96754631Scracauer					    c != '`' && c != '$' &&
96854631Scracauer					    (c != '"' || eofmark != NULL))
9691556Srgrimes						USTPUTC('\\', out);
97083675Stegge					if (SQSYNTAX[c] == CCTL)
9711556Srgrimes						USTPUTC(CTLESC, out);
97239137Stegge					else if (eofmark == NULL)
97338887Stegge						USTPUTC(CTLQUOTEMARK, out);
9741556Srgrimes					USTPUTC(c, out);
9751556Srgrimes					quotef++;
9761556Srgrimes				}
9771556Srgrimes				break;
9781556Srgrimes			case CSQUOTE:
97939137Stegge				if (eofmark == NULL)
98039137Stegge					USTPUTC(CTLQUOTEMARK, out);
9811556Srgrimes				syntax = SQSYNTAX;
9821556Srgrimes				break;
9831556Srgrimes			case CDQUOTE:
98439137Stegge				if (eofmark == NULL)
98539137Stegge					USTPUTC(CTLQUOTEMARK, out);
9861556Srgrimes				syntax = DQSYNTAX;
9871556Srgrimes				dblquote = 1;
9881556Srgrimes				break;
9891556Srgrimes			case CENDQUOTE:
99039137Stegge				if (eofmark != NULL && arinest == 0 &&
99139137Stegge				    varnest == 0) {
9921556Srgrimes					USTPUTC(c, out);
9931556Srgrimes				} else {
99439137Stegge					if (arinest) {
9951556Srgrimes						syntax = ARISYNTAX;
99639137Stegge						dblquote = 0;
99739137Stegge					} else if (eofmark == NULL) {
9981556Srgrimes						syntax = BASESYNTAX;
99939137Stegge						dblquote = 0;
100039137Stegge					}
10011556Srgrimes					quotef++;
10021556Srgrimes				}
10031556Srgrimes				break;
10041556Srgrimes			case CVAR:	/* '$' */
10051556Srgrimes				PARSESUB();		/* parse substitution */
10061556Srgrimes				break;
10071556Srgrimes			case CENDVAR:	/* '}' */
10081556Srgrimes				if (varnest > 0) {
10091556Srgrimes					varnest--;
10101556Srgrimes					USTPUTC(CTLENDVAR, out);
10111556Srgrimes				} else {
10121556Srgrimes					USTPUTC(c, out);
10131556Srgrimes				}
10141556Srgrimes				break;
10151556Srgrimes			case CLP:	/* '(' in arithmetic */
10161556Srgrimes				parenlevel++;
10171556Srgrimes				USTPUTC(c, out);
10181556Srgrimes				break;
10191556Srgrimes			case CRP:	/* ')' in arithmetic */
10201556Srgrimes				if (parenlevel > 0) {
10211556Srgrimes					USTPUTC(c, out);
10221556Srgrimes					--parenlevel;
10231556Srgrimes				} else {
10241556Srgrimes					if (pgetc() == ')') {
10251556Srgrimes						if (--arinest == 0) {
10261556Srgrimes							USTPUTC(CTLENDARI, out);
10271556Srgrimes							syntax = prevsyntax;
102839137Stegge							if (syntax == DQSYNTAX)
102939137Stegge								dblquote = 1;
103039137Stegge							else
103139137Stegge								dblquote = 0;
10321556Srgrimes						} else
10331556Srgrimes							USTPUTC(')', out);
10341556Srgrimes					} else {
10358855Srgrimes						/*
10361556Srgrimes						 * unbalanced parens
10371556Srgrimes						 *  (don't 2nd guess - no error)
10381556Srgrimes						 */
10391556Srgrimes						pungetc();
10401556Srgrimes						USTPUTC(')', out);
10411556Srgrimes					}
10421556Srgrimes				}
10431556Srgrimes				break;
10441556Srgrimes			case CBQUOTE:	/* '`' */
10451556Srgrimes				PARSEBACKQOLD();
10461556Srgrimes				break;
10471556Srgrimes			case CEOF:
10481556Srgrimes				goto endword;		/* exit outer loop */
10491556Srgrimes			default:
10501556Srgrimes				if (varnest == 0)
10511556Srgrimes					goto endword;	/* exit outer loop */
10521556Srgrimes				USTPUTC(c, out);
10531556Srgrimes			}
10541556Srgrimes			c = pgetc_macro();
10551556Srgrimes		}
10561556Srgrimes	}
10571556Srgrimesendword:
10581556Srgrimes	if (syntax == ARISYNTAX)
10591556Srgrimes		synerror("Missing '))'");
10601556Srgrimes	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
10611556Srgrimes		synerror("Unterminated quoted string");
10621556Srgrimes	if (varnest != 0) {
10631556Srgrimes		startlinno = plinno;
10641556Srgrimes		synerror("Missing '}'");
10651556Srgrimes	}
10661556Srgrimes	USTPUTC('\0', out);
10671556Srgrimes	len = out - stackblock();
10681556Srgrimes	out = stackblock();
10691556Srgrimes	if (eofmark == NULL) {
10701556Srgrimes		if ((c == '>' || c == '<')
10711556Srgrimes		 && quotef == 0
10721556Srgrimes		 && len <= 2
10731556Srgrimes		 && (*out == '\0' || is_digit(*out))) {
10741556Srgrimes			PARSEREDIR();
10751556Srgrimes			return lasttoken = TREDIR;
10761556Srgrimes		} else {
10771556Srgrimes			pungetc();
10781556Srgrimes		}
10791556Srgrimes	}
10801556Srgrimes	quoteflag = quotef;
10811556Srgrimes	backquotelist = bqlist;
10821556Srgrimes	grabstackblock(len);
10831556Srgrimes	wordtext = out;
10841556Srgrimes	return lasttoken = TWORD;
10851556Srgrimes/* end of readtoken routine */
10861556Srgrimes
10871556Srgrimes
10881556Srgrimes
10891556Srgrimes/*
10901556Srgrimes * Check to see whether we are at the end of the here document.  When this
10911556Srgrimes * is called, c is set to the first character of the next input line.  If
10921556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF.
10931556Srgrimes */
10941556Srgrimes
10951556Srgrimescheckend: {
10961556Srgrimes	if (eofmark) {
10971556Srgrimes		if (striptabs) {
10981556Srgrimes			while (c == '\t')
10991556Srgrimes				c = pgetc();
11001556Srgrimes		}
11011556Srgrimes		if (c == *eofmark) {
11021556Srgrimes			if (pfgets(line, sizeof line) != NULL) {
110325230Ssteve				char *p, *q;
11041556Srgrimes
11051556Srgrimes				p = line;
11061556Srgrimes				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
11071556Srgrimes				if (*p == '\n' && *q == '\0') {
11081556Srgrimes					c = PEOF;
11091556Srgrimes					plinno++;
11101556Srgrimes					needprompt = doprompt;
11111556Srgrimes				} else {
11121556Srgrimes					pushstring(line, strlen(line), NULL);
11131556Srgrimes				}
11141556Srgrimes			}
11151556Srgrimes		}
11161556Srgrimes	}
11171556Srgrimes	goto checkend_return;
11181556Srgrimes}
11191556Srgrimes
11201556Srgrimes
11211556Srgrimes/*
11221556Srgrimes * Parse a redirection operator.  The variable "out" points to a string
11231556Srgrimes * specifying the fd to be redirected.  The variable "c" contains the
11241556Srgrimes * first character of the redirection operator.
11251556Srgrimes */
11261556Srgrimes
11271556Srgrimesparseredir: {
11281556Srgrimes	char fd = *out;
11291556Srgrimes	union node *np;
11301556Srgrimes
11311556Srgrimes	np = (union node *)stalloc(sizeof (struct nfile));
11321556Srgrimes	if (c == '>') {
11331556Srgrimes		np->nfile.fd = 1;
11341556Srgrimes		c = pgetc();
11351556Srgrimes		if (c == '>')
11361556Srgrimes			np->type = NAPPEND;
11371556Srgrimes		else if (c == '&')
11381556Srgrimes			np->type = NTOFD;
113996922Stjr		else if (c == '|')
114096922Stjr			np->type = NCLOBBER;
11411556Srgrimes		else {
11421556Srgrimes			np->type = NTO;
11431556Srgrimes			pungetc();
11441556Srgrimes		}
11451556Srgrimes	} else {	/* c == '<' */
11461556Srgrimes		np->nfile.fd = 0;
11471556Srgrimes		c = pgetc();
11481556Srgrimes		if (c == '<') {
11491556Srgrimes			if (sizeof (struct nfile) != sizeof (struct nhere)) {
11501556Srgrimes				np = (union node *)stalloc(sizeof (struct nhere));
11511556Srgrimes				np->nfile.fd = 0;
11521556Srgrimes			}
11531556Srgrimes			np->type = NHERE;
11541556Srgrimes			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
11551556Srgrimes			heredoc->here = np;
11561556Srgrimes			if ((c = pgetc()) == '-') {
11571556Srgrimes				heredoc->striptabs = 1;
11581556Srgrimes			} else {
11591556Srgrimes				heredoc->striptabs = 0;
11601556Srgrimes				pungetc();
11611556Srgrimes			}
11621556Srgrimes		} else if (c == '&')
11631556Srgrimes			np->type = NFROMFD;
116466612Sbrian		else if (c == '>')
116566612Sbrian			np->type = NFROMTO;
11661556Srgrimes		else {
11671556Srgrimes			np->type = NFROM;
11681556Srgrimes			pungetc();
11691556Srgrimes		}
11701556Srgrimes	}
11711556Srgrimes	if (fd != '\0')
11721556Srgrimes		np->nfile.fd = digit_val(fd);
11731556Srgrimes	redirnode = np;
11741556Srgrimes	goto parseredir_return;
11751556Srgrimes}
11761556Srgrimes
11771556Srgrimes
11781556Srgrimes/*
11791556Srgrimes * Parse a substitution.  At this point, we have read the dollar sign
11801556Srgrimes * and nothing else.
11811556Srgrimes */
11821556Srgrimes
11831556Srgrimesparsesub: {
11841556Srgrimes	int subtype;
11851556Srgrimes	int typeloc;
11861556Srgrimes	int flags;
11871556Srgrimes	char *p;
11881556Srgrimes#ifndef GDB_HACK
11891556Srgrimes	static const char types[] = "}-+?=";
11901556Srgrimes#endif
119118202Speter       int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
11921556Srgrimes
11931556Srgrimes	c = pgetc();
11941556Srgrimes	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
11951556Srgrimes		USTPUTC('$', out);
11961556Srgrimes		pungetc();
11971556Srgrimes	} else if (c == '(') {	/* $(command) or $((arith)) */
11981556Srgrimes		if (pgetc() == '(') {
11991556Srgrimes			PARSEARITH();
12001556Srgrimes		} else {
12011556Srgrimes			pungetc();
12021556Srgrimes			PARSEBACKQNEW();
12031556Srgrimes		}
12041556Srgrimes	} else {
12051556Srgrimes		USTPUTC(CTLVAR, out);
12061556Srgrimes		typeloc = out - stackblock();
12071556Srgrimes		USTPUTC(VSNORMAL, out);
12081556Srgrimes		subtype = VSNORMAL;
12091556Srgrimes		if (c == '{') {
121018202Speter			bracketed_name = 1;
12111556Srgrimes			c = pgetc();
121217987Speter			if (c == '#') {
121317987Speter				if ((c = pgetc()) == '}')
121417987Speter					c = '#';
121517987Speter				else
121617987Speter					subtype = VSLENGTH;
121717987Speter			}
121817987Speter			else
121917987Speter				subtype = 0;
12201556Srgrimes		}
12211556Srgrimes		if (is_name(c)) {
12221556Srgrimes			do {
12231556Srgrimes				STPUTC(c, out);
12241556Srgrimes				c = pgetc();
12251556Srgrimes			} while (is_in_name(c));
122618202Speter		} else if (is_digit(c)) {
122718202Speter			if (bracketed_name) {
122818202Speter				do {
122918202Speter					STPUTC(c, out);
123018202Speter					c = pgetc();
123118202Speter				} while (is_digit(c));
123218202Speter			} else {
123318202Speter				STPUTC(c, out);
123418202Speter				c = pgetc();
123518202Speter			}
12361556Srgrimes		} else {
12371556Srgrimes			if (! is_special(c))
12381556Srgrimesbadsub:				synerror("Bad substitution");
12391556Srgrimes			USTPUTC(c, out);
12401556Srgrimes			c = pgetc();
12411556Srgrimes		}
12421556Srgrimes		STPUTC('=', out);
12431556Srgrimes		flags = 0;
12441556Srgrimes		if (subtype == 0) {
124517987Speter			switch (c) {
124617987Speter			case ':':
12471556Srgrimes				flags = VSNUL;
12481556Srgrimes				c = pgetc();
124917987Speter				/*FALLTHROUGH*/
125017987Speter			default:
125117987Speter				p = strchr(types, c);
125217987Speter				if (p == NULL)
125317987Speter					goto badsub;
125417987Speter				subtype = p - types + VSNORMAL;
125517987Speter				break;
125617987Speter			case '%':
125720425Ssteve			case '#':
125817987Speter				{
125917987Speter					int cc = c;
126017987Speter					subtype = c == '#' ? VSTRIMLEFT :
126117987Speter							     VSTRIMRIGHT;
126217987Speter					c = pgetc();
126317987Speter					if (c == cc)
126417987Speter						subtype++;
126517987Speter					else
126617987Speter						pungetc();
126717987Speter					break;
126817987Speter				}
12691556Srgrimes			}
12701556Srgrimes		} else {
12711556Srgrimes			pungetc();
12721556Srgrimes		}
127357225Scracauer		if (subtype != VSLENGTH && (dblquote || arinest))
12741556Srgrimes			flags |= VSQUOTE;
12751556Srgrimes		*(stackblock() + typeloc) = subtype | flags;
12761556Srgrimes		if (subtype != VSNORMAL)
12771556Srgrimes			varnest++;
12781556Srgrimes	}
12791556Srgrimes	goto parsesub_return;
12801556Srgrimes}
12811556Srgrimes
12821556Srgrimes
12831556Srgrimes/*
12841556Srgrimes * Called to parse command substitutions.  Newstyle is set if the command
12851556Srgrimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked
12861556Srgrimes * list of commands (passed by reference), and savelen is the number of
12871556Srgrimes * characters on the top of the stack which must be preserved.
12881556Srgrimes */
12891556Srgrimes
12901556Srgrimesparsebackq: {
12911556Srgrimes	struct nodelist **nlpp;
12921556Srgrimes	int savepbq;
12931556Srgrimes	union node *n;
12941556Srgrimes	char *volatile str;
12951556Srgrimes	struct jmploc jmploc;
12961556Srgrimes	struct jmploc *volatile savehandler;
12971556Srgrimes	int savelen;
129820425Ssteve	int saveprompt;
129920425Ssteve#if __GNUC__
130020425Ssteve	/* Avoid longjmp clobbering */
130120425Ssteve	(void) &saveprompt;
130220425Ssteve#endif
13031556Srgrimes
13041556Srgrimes	savepbq = parsebackquote;
13051556Srgrimes	if (setjmp(jmploc.loc)) {
13061556Srgrimes		if (str)
13071556Srgrimes			ckfree(str);
13081556Srgrimes		parsebackquote = 0;
13091556Srgrimes		handler = savehandler;
13101556Srgrimes		longjmp(handler->loc, 1);
13111556Srgrimes	}
13121556Srgrimes	INTOFF;
13131556Srgrimes	str = NULL;
13141556Srgrimes	savelen = out - stackblock();
13151556Srgrimes	if (savelen > 0) {
13161556Srgrimes		str = ckmalloc(savelen);
131717987Speter		memcpy(str, stackblock(), savelen);
13181556Srgrimes	}
13191556Srgrimes	savehandler = handler;
13201556Srgrimes	handler = &jmploc;
13211556Srgrimes	INTON;
13221556Srgrimes        if (oldstyle) {
13231556Srgrimes                /* We must read until the closing backquote, giving special
13241556Srgrimes                   treatment to some slashes, and then push the string and
13251556Srgrimes                   reread it as input, interpreting it normally.  */
132625230Ssteve                char *out;
132725230Ssteve                int c;
13281556Srgrimes                int savelen;
13291556Srgrimes                char *str;
13308855Srgrimes
133120425Ssteve
13321556Srgrimes                STARTSTACKSTR(out);
133320425Ssteve		for (;;) {
133420425Ssteve			if (needprompt) {
133520425Ssteve				setprompt(2);
133620425Ssteve				needprompt = 0;
133720425Ssteve			}
133820425Ssteve			switch (c = pgetc()) {
133920425Ssteve			case '`':
134020425Ssteve				goto done;
134120425Ssteve
134220425Ssteve			case '\\':
134320425Ssteve                                if ((c = pgetc()) == '\n') {
134420425Ssteve					plinno++;
134520425Ssteve					if (doprompt)
134620425Ssteve						setprompt(2);
134720425Ssteve					else
134820425Ssteve						setprompt(0);
134920425Ssteve					/*
135020425Ssteve					 * If eating a newline, avoid putting
135120425Ssteve					 * the newline into the new character
135220425Ssteve					 * stream (via the STPUTC after the
135320425Ssteve					 * switch).
135420425Ssteve					 */
135520425Ssteve					continue;
135620425Ssteve				}
135717987Speter                                if (c != '\\' && c != '`' && c != '$'
13581556Srgrimes                                    && (!dblquote || c != '"'))
13591556Srgrimes                                        STPUTC('\\', out);
136020425Ssteve				break;
136120425Ssteve
136220425Ssteve			case '\n':
136320425Ssteve				plinno++;
136420425Ssteve				needprompt = doprompt;
136520425Ssteve				break;
136620425Ssteve
136720425Ssteve			case PEOF:
136820425Ssteve			        startlinno = plinno;
136920425Ssteve				synerror("EOF in backquote substitution");
137020425Ssteve 				break;
137120425Ssteve
137220425Ssteve			default:
137320425Ssteve				break;
137420425Ssteve			}
137520425Ssteve			STPUTC(c, out);
13761556Srgrimes                }
137720425Sstevedone:
13781556Srgrimes                STPUTC('\0', out);
13791556Srgrimes                savelen = out - stackblock();
13801556Srgrimes                if (savelen > 0) {
13811556Srgrimes                        str = ckmalloc(savelen);
138217987Speter                        memcpy(str, stackblock(), savelen);
138317987Speter			setinputstring(str, 1);
13841556Srgrimes                }
13851556Srgrimes        }
13861556Srgrimes	nlpp = &bqlist;
13871556Srgrimes	while (*nlpp)
13881556Srgrimes		nlpp = &(*nlpp)->next;
13891556Srgrimes	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
13901556Srgrimes	(*nlpp)->next = NULL;
13911556Srgrimes	parsebackquote = oldstyle;
139220425Ssteve
139320425Ssteve	if (oldstyle) {
139420425Ssteve		saveprompt = doprompt;
139520425Ssteve		doprompt = 0;
139620425Ssteve	}
139720425Ssteve
13981556Srgrimes	n = list(0);
139920425Ssteve
140020425Ssteve	if (oldstyle)
140120425Ssteve		doprompt = saveprompt;
140220425Ssteve	else {
140320425Ssteve		if (readtoken() != TRP)
140420425Ssteve			synexpect(TRP);
140520425Ssteve	}
140620425Ssteve
14071556Srgrimes	(*nlpp)->n = n;
140820425Ssteve        if (oldstyle) {
140920425Ssteve		/*
141020425Ssteve		 * Start reading from old file again, ignoring any pushed back
141120425Ssteve		 * tokens left from the backquote parsing
141220425Ssteve		 */
14131556Srgrimes                popfile();
141420425Ssteve		tokpushback = 0;
141520425Ssteve	}
14161556Srgrimes	while (stackblocksize() <= savelen)
14171556Srgrimes		growstackblock();
14181556Srgrimes	STARTSTACKSTR(out);
14191556Srgrimes	if (str) {
142017987Speter		memcpy(out, str, savelen);
14211556Srgrimes		STADJUST(savelen, out);
14221556Srgrimes		INTOFF;
14231556Srgrimes		ckfree(str);
14241556Srgrimes		str = NULL;
14251556Srgrimes		INTON;
14261556Srgrimes	}
14271556Srgrimes	parsebackquote = savepbq;
14281556Srgrimes	handler = savehandler;
14291556Srgrimes	if (arinest || dblquote)
14301556Srgrimes		USTPUTC(CTLBACKQ | CTLQUOTE, out);
14311556Srgrimes	else
14321556Srgrimes		USTPUTC(CTLBACKQ, out);
14331556Srgrimes	if (oldstyle)
14341556Srgrimes		goto parsebackq_oldreturn;
14351556Srgrimes	else
14361556Srgrimes		goto parsebackq_newreturn;
14371556Srgrimes}
14381556Srgrimes
14391556Srgrimes/*
14401556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state)
14411556Srgrimes */
14421556Srgrimesparsearith: {
14431556Srgrimes
14441556Srgrimes	if (++arinest == 1) {
14451556Srgrimes		prevsyntax = syntax;
14461556Srgrimes		syntax = ARISYNTAX;
14471556Srgrimes		USTPUTC(CTLARI, out);
144838887Stegge		if (dblquote)
144938887Stegge			USTPUTC('"',out);
145038887Stegge		else
145138887Stegge			USTPUTC(' ',out);
14521556Srgrimes	} else {
14531556Srgrimes		/*
14541556Srgrimes		 * we collapse embedded arithmetic expansion to
14551556Srgrimes		 * parenthesis, which should be equivalent
14561556Srgrimes		 */
14571556Srgrimes		USTPUTC('(', out);
14581556Srgrimes	}
14591556Srgrimes	goto parsearith_return;
14601556Srgrimes}
14611556Srgrimes
14621556Srgrimes} /* end of readtoken */
14631556Srgrimes
14641556Srgrimes
14651556Srgrimes
14661556Srgrimes#ifdef mkinit
14671556SrgrimesRESET {
14681556Srgrimes	tokpushback = 0;
14691556Srgrimes	checkkwd = 0;
14701556Srgrimes}
14711556Srgrimes#endif
14721556Srgrimes
14731556Srgrimes/*
14741556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs
14751556Srgrimes * or backquotes).
14761556Srgrimes */
14771556Srgrimes
14781556SrgrimesSTATIC int
147990111Simpnoexpand(char *text)
148090111Simp{
148125230Ssteve	char *p;
148225230Ssteve	char c;
14831556Srgrimes
14841556Srgrimes	p = text;
14851556Srgrimes	while ((c = *p++) != '\0') {
148639137Stegge		if ( c == CTLQUOTEMARK)
148739137Stegge			continue;
14881556Srgrimes		if (c == CTLESC)
14891556Srgrimes			p++;
149083675Stegge		else if (BASESYNTAX[(int)c] == CCTL)
14911556Srgrimes			return 0;
14921556Srgrimes	}
14931556Srgrimes	return 1;
14941556Srgrimes}
14951556Srgrimes
14961556Srgrimes
14971556Srgrimes/*
14981556Srgrimes * Return true if the argument is a legal variable name (a letter or
14991556Srgrimes * underscore followed by zero or more letters, underscores, and digits).
15001556Srgrimes */
15011556Srgrimes
15021556Srgrimesint
150390111Simpgoodname(char *name)
150490111Simp{
150525230Ssteve	char *p;
15061556Srgrimes
15071556Srgrimes	p = name;
15081556Srgrimes	if (! is_name(*p))
15091556Srgrimes		return 0;
15101556Srgrimes	while (*++p) {
15111556Srgrimes		if (! is_in_name(*p))
15121556Srgrimes			return 0;
15131556Srgrimes	}
15141556Srgrimes	return 1;
15151556Srgrimes}
15161556Srgrimes
15171556Srgrimes
15181556Srgrimes/*
15191556Srgrimes * Called when an unexpected token is read during the parse.  The argument
15201556Srgrimes * is the token that is expected, or -1 if more than one type of token can
15211556Srgrimes * occur at this point.
15221556Srgrimes */
15231556Srgrimes
15241556SrgrimesSTATIC void
152590111Simpsynexpect(int token)
152617987Speter{
15271556Srgrimes	char msg[64];
15281556Srgrimes
15291556Srgrimes	if (token >= 0) {
15301556Srgrimes		fmtstr(msg, 64, "%s unexpected (expecting %s)",
15311556Srgrimes			tokname[lasttoken], tokname[token]);
15321556Srgrimes	} else {
15331556Srgrimes		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
15341556Srgrimes	}
15351556Srgrimes	synerror(msg);
15361556Srgrimes}
15371556Srgrimes
15381556Srgrimes
15391556SrgrimesSTATIC void
154090111Simpsynerror(char *msg)
154190111Simp{
15421556Srgrimes	if (commandname)
15431556Srgrimes		outfmt(&errout, "%s: %d: ", commandname, startlinno);
15441556Srgrimes	outfmt(&errout, "Syntax error: %s\n", msg);
15451556Srgrimes	error((char *)NULL);
15461556Srgrimes}
15471556Srgrimes
15481556SrgrimesSTATIC void
154990111Simpsetprompt(int which)
155090111Simp{
15511556Srgrimes	whichprompt = which;
15521556Srgrimes
155317987Speter#ifndef NO_HISTORY
15541556Srgrimes	if (!el)
155517987Speter#endif
15561556Srgrimes		out2str(getprompt(NULL));
15571556Srgrimes}
15581556Srgrimes
15591556Srgrimes/*
15601556Srgrimes * called by editline -- any expansions to the prompt
15611556Srgrimes *    should be added here.
15621556Srgrimes */
15631556Srgrimeschar *
156490111Simpgetprompt(void *unused __unused)
156525905Ssteve{
1566142845Sobrien	static char ps[PROMPTLEN];
1567142845Sobrien	char *fmt;
1568142845Sobrien	int i, j, trim;
1569142845Sobrien
1570142845Sobrien	/*
1571142845Sobrien	 * Select prompt format.
1572142845Sobrien	 */
15731556Srgrimes	switch (whichprompt) {
15741556Srgrimes	case 0:
1575142845Sobrien		fmt = "";
1576142845Sobrien		break;
15771556Srgrimes	case 1:
1578142845Sobrien		fmt = ps1val();
1579142845Sobrien		break;
15801556Srgrimes	case 2:
1581142845Sobrien		fmt = ps2val();
1582142845Sobrien		break;
15831556Srgrimes	default:
15841556Srgrimes		return "<internal prompt error>";
15851556Srgrimes	}
1586142845Sobrien
1587142845Sobrien	/*
1588142845Sobrien	 * Format prompt string.
1589142845Sobrien	 */
1590142845Sobrien	for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1591142845Sobrien		if (*fmt == '\\')
1592142845Sobrien			switch (*++fmt) {
1593142845Sobrien
1594142845Sobrien				/*
1595142845Sobrien				 * Hostname.
1596142845Sobrien				 *
1597142845Sobrien				 * \h specifies just the local hostname,
1598142845Sobrien				 * \H specifies fully-qualified hostname.
1599142845Sobrien				 */
1600142845Sobrien			case 'h':
1601142845Sobrien			case 'H':
1602142845Sobrien				ps[i] == '\0';
1603142845Sobrien				gethostname(&ps[i], PROMPTLEN - i);
1604142845Sobrien				/* Skip to end of hostname. */
1605142845Sobrien				trim = (*fmt == 'h') ? '.' : '\0';
1606142845Sobrien				while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1607142845Sobrien					i++;
1608142845Sobrien				break;
1609142845Sobrien
1610142845Sobrien				/*
1611142845Sobrien				 * Working directory.
1612142845Sobrien				 *
1613142845Sobrien				 * \W specifies just the final component,
1614142845Sobrien				 * \w specifies the entire path.
1615142845Sobrien				 */
1616142845Sobrien			case 'W':
1617142845Sobrien			case 'w':
1618142845Sobrien				ps[i] == '\0';
1619142845Sobrien				getcwd(&ps[i], PROMPTLEN - i);
1620142845Sobrien				if (*fmt == 'W') {
1621142845Sobrien					/* Final path component only. */
1622142845Sobrien					trim = 1;
1623142845Sobrien					for (j = i; ps[j] != '\0'; j++)
1624142845Sobrien					  if (ps[j] == '/')
1625142845Sobrien						trim = j + 1;
1626142845Sobrien					memmove(&ps[i], &ps[trim],
1627142845Sobrien					    j - trim + 1);
1628142845Sobrien				}
1629142845Sobrien				/* Skip to end of path. */
1630142845Sobrien				while (ps[i + 1] != '\0')
1631142845Sobrien					i++;
1632142845Sobrien				break;
1633142845Sobrien
1634142845Sobrien				/*
1635142845Sobrien				 * Superuser status.
1636142845Sobrien				 *
1637142845Sobrien				 * '$' for normal users, '#' for root.
1638142845Sobrien				 */
1639142845Sobrien			case '$':
1640142845Sobrien				ps[i] = (geteuid() != 0) ? '$' : '#';
1641142845Sobrien				break;
1642142845Sobrien
1643142845Sobrien				/*
1644142845Sobrien				 * A literal \.
1645142845Sobrien				 */
1646142845Sobrien			case '\\':
1647142845Sobrien				ps[i] = '\\';
1648142845Sobrien				break;
1649142845Sobrien
1650142845Sobrien				/*
1651142845Sobrien				 * Emit unrecognized formats verbatim.
1652142845Sobrien				 */
1653142845Sobrien			default:
1654142845Sobrien				ps[i++] = '\\';
1655142845Sobrien				ps[i] = *fmt;
1656142845Sobrien				break;
1657142845Sobrien			}
1658142845Sobrien		else
1659142845Sobrien			ps[i] = *fmt;
1660142845Sobrien	ps[i] = '\0';
1661142845Sobrien	return (ps);
16621556Srgrimes}
1663