parser.c revision 149017
155682Smarkm/*-
2233294Sstas * Copyright (c) 1991, 1993
3233294Sstas *	The Regents of the University of California.  All rights reserved.
4233294Sstas *
555682Smarkm * This code is derived from software contributed to Berkeley by
6233294Sstas * Kenneth Almquist.
7233294Sstas *
8233294Sstas * Redistribution and use in source and binary forms, with or without
955682Smarkm * modification, are permitted provided that the following conditions
10233294Sstas * are met:
11233294Sstas * 1. Redistributions of source code must retain the above copyright
1255682Smarkm *    notice, this list of conditions and the following disclaimer.
13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
14233294Sstas *    notice, this list of conditions and the following disclaimer in the
15233294Sstas *    documentation and/or other materials provided with the distribution.
1655682Smarkm * 4. Neither the name of the University nor the names of its contributors
17233294Sstas *    may be used to endorse or promote products derived from this software
18233294Sstas *    without specific prior written permission.
19233294Sstas *
2055682Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30233294Sstas * SUCH DAMAGE.
31233294Sstas */
3255682Smarkm
3355682Smarkm#ifndef lint
3455682Smarkm#if 0
3555682Smarkmstatic char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
36233294Sstas#endif
3755682Smarkm#endif /* not lint */
38178825Sdfr#include <sys/cdefs.h>
39178825Sdfr__FBSDID("$FreeBSD: head/bin/sh/parser.c 149017 2005-08-13 08:26:58Z stefanf $");
40120945Snectar
4155682Smarkm#include <stdlib.h>
4255682Smarkm#include <unistd.h>
4355682Smarkm
4455682Smarkm#include "shell.h"
4555682Smarkm#include "parser.h"
4655682Smarkm#include "nodes.h"
4755682Smarkm#include "expand.h"	/* defines rmescapes() */
4855682Smarkm#include "syntax.h"
4955682Smarkm#include "options.h"
5055682Smarkm#include "input.h"
51120945Snectar#include "output.h"
5255682Smarkm#include "var.h"
5378527Sassar#include "error.h"
54178825Sdfr#include "memalloc.h"
5555682Smarkm#include "mystring.h"
5655682Smarkm#include "alias.h"
5755682Smarkm#include "show.h"
5855682Smarkm#include "eval.h"
59178825Sdfr#ifndef NO_HISTORY
60178825Sdfr#include "myhistedit.h"
61178825Sdfr#endif
62178825Sdfr
63178825Sdfr/*
64233294Sstas * Shell command parser.
65178825Sdfr */
66178825Sdfr
6755682Smarkm#define	EOFMARKLEN	79
68233294Sstas#define	PROMPTLEN	128
6955682Smarkm
70178825Sdfr/* values returned by readtoken */
71178825Sdfr#include "token.h"
72178825Sdfr
73178825Sdfr
74233294Sstas
75178825Sdfrstruct heredoc {
76233294Sstas	struct heredoc *next;	/* next here document in list */
7755682Smarkm	union node *here;		/* redirection node */
7855682Smarkm	char *eofmark;		/* string indicating end of input */
7955682Smarkm	int striptabs;		/* if set, strip leading tabs */
8055682Smarkm};
8155682Smarkm
8255682Smarkm
8355682Smarkm
8455682SmarkmSTATIC struct heredoc *heredoclist;	/* list of here documents to read */
8555682SmarkmSTATIC int parsebackquote;	/* nonzero if we are inside backquotes */
8655682SmarkmSTATIC int doprompt;		/* if set, prompt the user */
8755682SmarkmSTATIC int needprompt;		/* true if interactive and at start of line */
8855682SmarkmSTATIC int lasttoken;		/* last token read */
8955682SmarkmMKINIT int tokpushback;		/* last token pushed back */
9055682SmarkmSTATIC char *wordtext;		/* text of last word returned by readtoken */
91178825SdfrMKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
92178825SdfrSTATIC struct nodelist *backquotelist;
9355682SmarkmSTATIC union node *redirnode;
94178825SdfrSTATIC struct heredoc *heredoc;
95178825SdfrSTATIC int quoteflag;		/* set if (part of) last token was quoted */
96178825SdfrSTATIC int startlinno;		/* line # where last token started */
97178825Sdfr
9855682Smarkm/* XXX When 'noaliases' is set to one, no alias expansion takes place. */
99120945Snectarstatic int noaliases = 0;
10055682Smarkm
10155682Smarkm#define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
102178825Sdfr#ifdef GDB_HACK
103178825Sdfrstatic const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'};
104178825Sdfrstatic const char types[] = "}-+?=";
10555682Smarkm#endif
106178825Sdfr
10755682Smarkm
10855682SmarkmSTATIC union node *list(int);
10955682SmarkmSTATIC union node *andor(void);
110120945SnectarSTATIC union node *pipeline(void);
11155682SmarkmSTATIC union node *command(void);
112120945SnectarSTATIC union node *simplecmd(union node **, union node *);
11355682SmarkmSTATIC union node *makename(void);
11455682SmarkmSTATIC void parsefname(void);
11555682SmarkmSTATIC void parseheredoc(void);
11655682SmarkmSTATIC int peektoken(void);
11755682SmarkmSTATIC int readtoken(void);
11855682SmarkmSTATIC int xxreadtoken(void);
11955682SmarkmSTATIC int readtoken1(int, char const *, char *, int);
120178825SdfrSTATIC int noexpand(char *);
12155682SmarkmSTATIC void synexpect(int);
12255682SmarkmSTATIC void synerror(char *);
12355682SmarkmSTATIC void setprompt(int);
12455682Smarkm
12555682Smarkm
12655682Smarkm/*
12755682Smarkm * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
128120945Snectar * valid parse tree indicating a blank line.)
129120945Snectar */
130120945Snectar
131120945Snectarunion node *
132120945Snectarparsecmd(int interact)
13355682Smarkm{
134178825Sdfr	int t;
13555682Smarkm
13655682Smarkm	tokpushback = 0;
13778527Sassar	doprompt = interact;
13855682Smarkm	if (doprompt)
13955682Smarkm		setprompt(1);
140120945Snectar	else
141120945Snectar		setprompt(0);
142178825Sdfr	needprompt = 0;
143233294Sstas	t = readtoken();
14490926Snectar	if (t == TEOF)
14578527Sassar		return NEOF;
14678527Sassar	if (t == TNL)
14755682Smarkm		return NULL;
148120945Snectar	tokpushback++;
149120945Snectar	return list(1);
15055682Smarkm}
15155682Smarkm
15255682Smarkm
153178825SdfrSTATIC union node *
15478527Sassarlist(int nlflag)
15555682Smarkm{
15655682Smarkm	union node *n1, *n2, *n3;
15755682Smarkm	int tok;
158120945Snectar
15955682Smarkm	checkkwd = 2;
160120945Snectar	if (nlflag == 0 && tokendlist[peektoken()])
161120945Snectar		return NULL;
162120945Snectar	n1 = NULL;
163120945Snectar	for (;;) {
164120945Snectar		n2 = andor();
16555682Smarkm		tok = readtoken();
166120945Snectar		if (tok == TBACKGND) {
167120945Snectar			if (n2->type == NCMD || n2->type == NPIPE) {
168178825Sdfr				n2->ncmd.backgnd = 1;
169178825Sdfr			} else if (n2->type == NREDIR) {
17055682Smarkm				n2->type = NBACKGND;
171178825Sdfr			} else {
17255682Smarkm				n3 = (union node *)stalloc(sizeof (struct nredir));
173178825Sdfr				n3->type = NBACKGND;
174120945Snectar				n3->nredir.n = n2;
17555682Smarkm				n3->nredir.redirect = NULL;
176178825Sdfr				n2 = n3;
17755682Smarkm			}
17855682Smarkm		}
17955682Smarkm		if (n1 == NULL) {
18055682Smarkm			n1 = n2;
181178825Sdfr		}
18255682Smarkm		else {
18355682Smarkm			n3 = (union node *)stalloc(sizeof (struct nbinary));
184120945Snectar			n3->type = NSEMI;
185120945Snectar			n3->nbinary.ch1 = n1;
186120945Snectar			n3->nbinary.ch2 = n2;
18755682Smarkm			n1 = n3;
18855682Smarkm		}
18955682Smarkm		switch (tok) {
190120945Snectar		case TBACKGND:
191120945Snectar		case TSEMI:
19255682Smarkm			tok = readtoken();
19355682Smarkm			/* FALLTHROUGH */
19455682Smarkm		case TNL:
195120945Snectar			if (tok == TNL) {
196120945Snectar				parseheredoc();
19755682Smarkm				if (nlflag)
19855682Smarkm					return n1;
19955682Smarkm			} else {
200120945Snectar				tokpushback++;
20155682Smarkm			}
20255682Smarkm			checkkwd = 2;
203120945Snectar			if (tokendlist[peektoken()])
20455682Smarkm				return n1;
205120945Snectar			break;
206120945Snectar		case TEOF:
20755682Smarkm			if (heredoclist)
20855682Smarkm				parseheredoc();
20955682Smarkm			else
21055682Smarkm				pungetc();		/* push back EOF on input */
21155682Smarkm			return n1;
212120945Snectar		default:
21355682Smarkm			if (nlflag)
21455682Smarkm				synexpect(-1);
21555682Smarkm			tokpushback++;
21655682Smarkm			return n1;
217178825Sdfr		}
218120945Snectar	}
219120945Snectar}
220178825Sdfr
221120945Snectar
222120945Snectar
223120945SnectarSTATIC union node *
224120945Snectarandor(void)
225233294Sstas{
226120945Snectar	union node *n1, *n2, *n3;
227120945Snectar	int t;
228120945Snectar
229120945Snectar	n1 = pipeline();
230233294Sstas	for (;;) {
231120945Snectar		if ((t = readtoken()) == TAND) {
232120945Snectar			t = NAND;
233120945Snectar		} else if (t == TOR) {
234120945Snectar			t = NOR;
235233294Sstas		} else {
236178825Sdfr			tokpushback++;
237233294Sstas			return n1;
238233294Sstas		}
239178825Sdfr		n2 = pipeline();
240178825Sdfr		n3 = (union node *)stalloc(sizeof (struct nbinary));
241178825Sdfr		n3->type = t;
242120945Snectar		n3->nbinary.ch1 = n1;
243178825Sdfr		n3->nbinary.ch2 = n2;
244178825Sdfr		n1 = n3;
245120945Snectar	}
246120945Snectar}
247120945Snectar
248120945Snectar
24978527Sassar
25078527SassarSTATIC union node *
251178825Sdfrpipeline(void)
25255682Smarkm{
253	union node *n1, *n2, *pipenode;
254	struct nodelist *lp, *prev;
255	int negate;
256
257	negate = 0;
258	TRACE(("pipeline: entered\n"));
259	while (readtoken() == TNOT)
260		negate = !negate;
261	tokpushback++;
262	n1 = command();
263	if (readtoken() == TPIPE) {
264		pipenode = (union node *)stalloc(sizeof (struct npipe));
265		pipenode->type = NPIPE;
266		pipenode->npipe.backgnd = 0;
267		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
268		pipenode->npipe.cmdlist = lp;
269		lp->n = n1;
270		do {
271			prev = lp;
272			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
273			lp->n = command();
274			prev->next = lp;
275		} while (readtoken() == TPIPE);
276		lp->next = NULL;
277		n1 = pipenode;
278	}
279	tokpushback++;
280	if (negate) {
281		n2 = (union node *)stalloc(sizeof (struct nnot));
282		n2->type = NNOT;
283		n2->nnot.com = n1;
284		return n2;
285	} else
286		return n1;
287}
288
289
290
291STATIC union node *
292command(void)
293{
294	union node *n1, *n2;
295	union node *ap, **app;
296	union node *cp, **cpp;
297	union node *redir, **rpp;
298	int t, negate = 0;
299
300	checkkwd = 2;
301	redir = NULL;
302	n1 = NULL;
303	rpp = &redir;
304
305	/* Check for redirection which may precede command */
306	while (readtoken() == TREDIR) {
307		*rpp = n2 = redirnode;
308		rpp = &n2->nfile.next;
309		parsefname();
310	}
311	tokpushback++;
312
313	while (readtoken() == TNOT) {
314		TRACE(("command: TNOT recognized\n"));
315		negate = !negate;
316	}
317	tokpushback++;
318
319	switch (readtoken()) {
320	case TIF:
321		n1 = (union node *)stalloc(sizeof (struct nif));
322		n1->type = NIF;
323		if ((n1->nif.test = list(0)) == NULL)
324			synexpect(-1);
325		if (readtoken() != TTHEN)
326			synexpect(TTHEN);
327		n1->nif.ifpart = list(0);
328		n2 = n1;
329		while (readtoken() == TELIF) {
330			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
331			n2 = n2->nif.elsepart;
332			n2->type = NIF;
333			if ((n2->nif.test = list(0)) == NULL)
334				synexpect(-1);
335			if (readtoken() != TTHEN)
336				synexpect(TTHEN);
337			n2->nif.ifpart = list(0);
338		}
339		if (lasttoken == TELSE)
340			n2->nif.elsepart = list(0);
341		else {
342			n2->nif.elsepart = NULL;
343			tokpushback++;
344		}
345		if (readtoken() != TFI)
346			synexpect(TFI);
347		checkkwd = 1;
348		break;
349	case TWHILE:
350	case TUNTIL: {
351		int got;
352		n1 = (union node *)stalloc(sizeof (struct nbinary));
353		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
354		if ((n1->nbinary.ch1 = list(0)) == NULL)
355			synexpect(-1);
356		if ((got=readtoken()) != TDO) {
357TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
358			synexpect(TDO);
359		}
360		n1->nbinary.ch2 = list(0);
361		if (readtoken() != TDONE)
362			synexpect(TDONE);
363		checkkwd = 1;
364		break;
365	}
366	case TFOR:
367		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
368			synerror("Bad for loop variable");
369		n1 = (union node *)stalloc(sizeof (struct nfor));
370		n1->type = NFOR;
371		n1->nfor.var = wordtext;
372		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
373			app = &ap;
374			while (readtoken() == TWORD) {
375				n2 = (union node *)stalloc(sizeof (struct narg));
376				n2->type = NARG;
377				n2->narg.text = wordtext;
378				n2->narg.backquote = backquotelist;
379				*app = n2;
380				app = &n2->narg.next;
381			}
382			*app = NULL;
383			n1->nfor.args = ap;
384			if (lasttoken != TNL && lasttoken != TSEMI)
385				synexpect(-1);
386		} else {
387#ifndef GDB_HACK
388			static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
389								   '@', '=', '\0'};
390#endif
391			n2 = (union node *)stalloc(sizeof (struct narg));
392			n2->type = NARG;
393			n2->narg.text = (char *)argvars;
394			n2->narg.backquote = NULL;
395			n2->narg.next = NULL;
396			n1->nfor.args = n2;
397			/*
398			 * Newline or semicolon here is optional (but note
399			 * that the original Bourne shell only allowed NL).
400			 */
401			if (lasttoken != TNL && lasttoken != TSEMI)
402				tokpushback++;
403		}
404		checkkwd = 2;
405		if ((t = readtoken()) == TDO)
406			t = TDONE;
407		else if (t == TBEGIN)
408			t = TEND;
409		else
410			synexpect(-1);
411		n1->nfor.body = list(0);
412		if (readtoken() != t)
413			synexpect(t);
414		checkkwd = 1;
415		break;
416	case TCASE:
417		n1 = (union node *)stalloc(sizeof (struct ncase));
418		n1->type = NCASE;
419		if (readtoken() != TWORD)
420			synexpect(TWORD);
421		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
422		n2->type = NARG;
423		n2->narg.text = wordtext;
424		n2->narg.backquote = backquotelist;
425		n2->narg.next = NULL;
426		while (readtoken() == TNL);
427		if (lasttoken != TWORD || ! equal(wordtext, "in"))
428			synerror("expecting \"in\"");
429		cpp = &n1->ncase.cases;
430		noaliases = 1;	/* turn off alias expansion */
431		checkkwd = 2, readtoken();
432		while (lasttoken != TESAC) {
433			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
434			cp->type = NCLIST;
435			app = &cp->nclist.pattern;
436			if (lasttoken == TLP)
437				readtoken();
438			for (;;) {
439				*app = ap = (union node *)stalloc(sizeof (struct narg));
440				ap->type = NARG;
441				ap->narg.text = wordtext;
442				ap->narg.backquote = backquotelist;
443				if (checkkwd = 2, readtoken() != TPIPE)
444					break;
445				app = &ap->narg.next;
446				readtoken();
447			}
448			ap->narg.next = NULL;
449			if (lasttoken != TRP)
450				noaliases = 0, synexpect(TRP);
451			cp->nclist.body = list(0);
452
453			checkkwd = 2;
454			if ((t = readtoken()) != TESAC) {
455				if (t != TENDCASE)
456					noaliases = 0, synexpect(TENDCASE);
457				else
458					checkkwd = 2, readtoken();
459			}
460			cpp = &cp->nclist.next;
461		}
462		noaliases = 0;	/* reset alias expansion */
463		*cpp = NULL;
464		checkkwd = 1;
465		break;
466	case TLP:
467		n1 = (union node *)stalloc(sizeof (struct nredir));
468		n1->type = NSUBSHELL;
469		n1->nredir.n = list(0);
470		n1->nredir.redirect = NULL;
471		if (readtoken() != TRP)
472			synexpect(TRP);
473		checkkwd = 1;
474		break;
475	case TBEGIN:
476		n1 = list(0);
477		if (readtoken() != TEND)
478			synexpect(TEND);
479		checkkwd = 1;
480		break;
481	/* Handle an empty command like other simple commands.  */
482	case TSEMI:
483	case TAND:
484	case TOR:
485		/*
486		 * An empty command before a ; doesn't make much sense, and
487		 * should certainly be disallowed in the case of `if ;'.
488		 */
489		if (!redir)
490			synexpect(-1);
491	case TNL:
492	case TEOF:
493	case TWORD:
494	case TRP:
495		tokpushback++;
496		n1 = simplecmd(rpp, redir);
497		goto checkneg;
498	default:
499		synexpect(-1);
500	}
501
502	/* Now check for redirection which may follow command */
503	while (readtoken() == TREDIR) {
504		*rpp = n2 = redirnode;
505		rpp = &n2->nfile.next;
506		parsefname();
507	}
508	tokpushback++;
509	*rpp = NULL;
510	if (redir) {
511		if (n1->type != NSUBSHELL) {
512			n2 = (union node *)stalloc(sizeof (struct nredir));
513			n2->type = NREDIR;
514			n2->nredir.n = n1;
515			n1 = n2;
516		}
517		n1->nredir.redirect = redir;
518	}
519
520checkneg:
521	if (negate) {
522		n2 = (union node *)stalloc(sizeof (struct nnot));
523		n2->type = NNOT;
524		n2->nnot.com = n1;
525		return n2;
526	}
527	else
528		return n1;
529}
530
531
532STATIC union node *
533simplecmd(union node **rpp, union node *redir)
534{
535	union node *args, **app;
536	union node **orig_rpp = rpp;
537	union node *n = NULL, *n2;
538	int negate = 0;
539
540	/* If we don't have any redirections already, then we must reset */
541	/* rpp to be the address of the local redir variable.  */
542	if (redir == 0)
543		rpp = &redir;
544
545	args = NULL;
546	app = &args;
547	/*
548	 * We save the incoming value, because we need this for shell
549	 * functions.  There can not be a redirect or an argument between
550	 * the function name and the open parenthesis.
551	 */
552	orig_rpp = rpp;
553
554	while (readtoken() == TNOT) {
555		TRACE(("command: TNOT recognized\n"));
556		negate = !negate;
557	}
558	tokpushback++;
559
560	for (;;) {
561		if (readtoken() == TWORD) {
562			n = (union node *)stalloc(sizeof (struct narg));
563			n->type = NARG;
564			n->narg.text = wordtext;
565			n->narg.backquote = backquotelist;
566			*app = n;
567			app = &n->narg.next;
568		} else if (lasttoken == TREDIR) {
569			*rpp = n = redirnode;
570			rpp = &n->nfile.next;
571			parsefname();	/* read name of redirection file */
572		} else if (lasttoken == TLP && app == &args->narg.next
573					    && rpp == orig_rpp) {
574			/* We have a function */
575			if (readtoken() != TRP)
576				synexpect(TRP);
577#ifdef notdef
578			if (! goodname(n->narg.text))
579				synerror("Bad function name");
580#endif
581			n->type = NDEFUN;
582			n->narg.next = command();
583			goto checkneg;
584		} else {
585			tokpushback++;
586			break;
587		}
588	}
589	*app = NULL;
590	*rpp = NULL;
591	n = (union node *)stalloc(sizeof (struct ncmd));
592	n->type = NCMD;
593	n->ncmd.backgnd = 0;
594	n->ncmd.args = args;
595	n->ncmd.redirect = redir;
596
597checkneg:
598	if (negate) {
599		n2 = (union node *)stalloc(sizeof (struct nnot));
600		n2->type = NNOT;
601		n2->nnot.com = n;
602		return n2;
603	}
604	else
605		return n;
606}
607
608STATIC union node *
609makename(void)
610{
611	union node *n;
612
613	n = (union node *)stalloc(sizeof (struct narg));
614	n->type = NARG;
615	n->narg.next = NULL;
616	n->narg.text = wordtext;
617	n->narg.backquote = backquotelist;
618	return n;
619}
620
621void fixredir(union node *n, const char *text, int err)
622{
623	TRACE(("Fix redir %s %d\n", text, err));
624	if (!err)
625		n->ndup.vname = NULL;
626
627	if (is_digit(text[0]) && text[1] == '\0')
628		n->ndup.dupfd = digit_val(text[0]);
629	else if (text[0] == '-' && text[1] == '\0')
630		n->ndup.dupfd = -1;
631	else {
632
633		if (err)
634			synerror("Bad fd number");
635		else
636			n->ndup.vname = makename();
637	}
638}
639
640
641STATIC void
642parsefname(void)
643{
644	union node *n = redirnode;
645
646	if (readtoken() != TWORD)
647		synexpect(-1);
648	if (n->type == NHERE) {
649		struct heredoc *here = heredoc;
650		struct heredoc *p;
651		int i;
652
653		if (quoteflag == 0)
654			n->type = NXHERE;
655		TRACE(("Here document %d\n", n->type));
656		if (here->striptabs) {
657			while (*wordtext == '\t')
658				wordtext++;
659		}
660		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
661			synerror("Illegal eof marker for << redirection");
662		rmescapes(wordtext);
663		here->eofmark = wordtext;
664		here->next = NULL;
665		if (heredoclist == NULL)
666			heredoclist = here;
667		else {
668			for (p = heredoclist ; p->next ; p = p->next);
669			p->next = here;
670		}
671	} else if (n->type == NTOFD || n->type == NFROMFD) {
672		fixredir(n, wordtext, 0);
673	} else {
674		n->nfile.fname = makename();
675	}
676}
677
678
679/*
680 * Input any here documents.
681 */
682
683STATIC void
684parseheredoc(void)
685{
686	struct heredoc *here;
687	union node *n;
688
689	while (heredoclist) {
690		here = heredoclist;
691		heredoclist = here->next;
692		if (needprompt) {
693			setprompt(2);
694			needprompt = 0;
695		}
696		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
697				here->eofmark, here->striptabs);
698		n = (union node *)stalloc(sizeof (struct narg));
699		n->narg.type = NARG;
700		n->narg.next = NULL;
701		n->narg.text = wordtext;
702		n->narg.backquote = backquotelist;
703		here->here->nhere.doc = n;
704	}
705}
706
707STATIC int
708peektoken(void)
709{
710	int t;
711
712	t = readtoken();
713	tokpushback++;
714	return (t);
715}
716
717STATIC int
718readtoken(void)
719{
720	int t;
721	int savecheckkwd = checkkwd;
722	struct alias *ap;
723#ifdef DEBUG
724	int alreadyseen = tokpushback;
725#endif
726
727	top:
728	t = xxreadtoken();
729
730	if (checkkwd) {
731		/*
732		 * eat newlines
733		 */
734		if (checkkwd == 2) {
735			checkkwd = 0;
736			while (t == TNL) {
737				parseheredoc();
738				t = xxreadtoken();
739			}
740		} else
741			checkkwd = 0;
742		/*
743		 * check for keywords and aliases
744		 */
745		if (t == TWORD && !quoteflag)
746		{
747			const char * const *pp;
748
749			for (pp = parsekwd; *pp; pp++) {
750				if (**pp == *wordtext && equal(*pp, wordtext))
751				{
752					lasttoken = t = pp - parsekwd + KWDOFFSET;
753					TRACE(("keyword %s recognized\n", tokname[t]));
754					goto out;
755				}
756			}
757			if (noaliases == 0 &&
758			    (ap = lookupalias(wordtext, 1)) != NULL) {
759				pushstring(ap->val, strlen(ap->val), ap);
760				checkkwd = savecheckkwd;
761				goto top;
762			}
763		}
764out:
765		checkkwd = (t == TNOT) ? savecheckkwd : 0;
766	}
767#ifdef DEBUG
768	if (!alreadyseen)
769	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
770	else
771	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
772#endif
773	return (t);
774}
775
776
777/*
778 * Read the next input token.
779 * If the token is a word, we set backquotelist to the list of cmds in
780 *	backquotes.  We set quoteflag to true if any part of the word was
781 *	quoted.
782 * If the token is TREDIR, then we set redirnode to a structure containing
783 *	the redirection.
784 * In all cases, the variable startlinno is set to the number of the line
785 *	on which the token starts.
786 *
787 * [Change comment:  here documents and internal procedures]
788 * [Readtoken shouldn't have any arguments.  Perhaps we should make the
789 *  word parsing code into a separate routine.  In this case, readtoken
790 *  doesn't need to have any internal procedures, but parseword does.
791 *  We could also make parseoperator in essence the main routine, and
792 *  have parseword (readtoken1?) handle both words and redirection.]
793 */
794
795#define RETURN(token)	return lasttoken = token
796
797STATIC int
798xxreadtoken(void)
799{
800	int c;
801
802	if (tokpushback) {
803		tokpushback = 0;
804		return lasttoken;
805	}
806	if (needprompt) {
807		setprompt(2);
808		needprompt = 0;
809	}
810	startlinno = plinno;
811	for (;;) {	/* until token or start of word found */
812		c = pgetc_macro();
813		if (c == ' ' || c == '\t')
814			continue;		/* quick check for white space first */
815		switch (c) {
816		case ' ': case '\t':
817			continue;
818		case '#':
819			while ((c = pgetc()) != '\n' && c != PEOF);
820			pungetc();
821			continue;
822		case '\\':
823			if (pgetc() == '\n') {
824				startlinno = ++plinno;
825				if (doprompt)
826					setprompt(2);
827				else
828					setprompt(0);
829				continue;
830			}
831			pungetc();
832			goto breakloop;
833		case '\n':
834			plinno++;
835			needprompt = doprompt;
836			RETURN(TNL);
837		case PEOF:
838			RETURN(TEOF);
839		case '&':
840			if (pgetc() == '&')
841				RETURN(TAND);
842			pungetc();
843			RETURN(TBACKGND);
844		case '|':
845			if (pgetc() == '|')
846				RETURN(TOR);
847			pungetc();
848			RETURN(TPIPE);
849		case ';':
850			if (pgetc() == ';')
851				RETURN(TENDCASE);
852			pungetc();
853			RETURN(TSEMI);
854		case '(':
855			RETURN(TLP);
856		case ')':
857			RETURN(TRP);
858		default:
859			goto breakloop;
860		}
861	}
862breakloop:
863	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
864#undef RETURN
865}
866
867
868
869/*
870 * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
871 * is not NULL, read a here document.  In the latter case, eofmark is the
872 * word which marks the end of the document and striptabs is true if
873 * leading tabs should be stripped from the document.  The argument firstc
874 * is the first character of the input token or document.
875 *
876 * Because C does not have internal subroutines, I have simulated them
877 * using goto's to implement the subroutine linkage.  The following macros
878 * will run code that appears at the end of readtoken1.
879 */
880
881#define CHECKEND()	{goto checkend; checkend_return:;}
882#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
883#define PARSESUB()	{goto parsesub; parsesub_return:;}
884#define PARSEBACKQOLD()	{oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
885#define PARSEBACKQNEW()	{oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
886#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
887
888STATIC int
889readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
890{
891	int c = firstc;
892	char *out;
893	int len;
894	char line[EOFMARKLEN + 1];
895	struct nodelist *bqlist;
896	int quotef;
897	int dblquote;
898	int varnest;	/* levels of variables expansion */
899	int arinest;	/* levels of arithmetic expansion */
900	int parenlevel;	/* levels of parens in arithmetic */
901	int oldstyle;
902	char const *prevsyntax;	/* syntax before arithmetic */
903	int synentry;
904#if __GNUC__
905	/* Avoid longjmp clobbering */
906	(void) &out;
907	(void) &quotef;
908	(void) &dblquote;
909	(void) &varnest;
910	(void) &arinest;
911	(void) &parenlevel;
912	(void) &oldstyle;
913	(void) &prevsyntax;
914	(void) &syntax;
915	(void) &synentry;
916#endif
917
918	startlinno = plinno;
919	dblquote = 0;
920	if (syntax == DQSYNTAX)
921		dblquote = 1;
922	quotef = 0;
923	bqlist = NULL;
924	varnest = 0;
925	arinest = 0;
926	parenlevel = 0;
927
928	STARTSTACKSTR(out);
929	loop: {	/* for each line, until end of word */
930		CHECKEND();	/* set c to PEOF if at end of here document */
931		for (;;) {	/* until end of line or end of word */
932			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
933
934			synentry = syntax[c];
935
936			switch(synentry) {
937			case CNL:	/* '\n' */
938				if (syntax == BASESYNTAX)
939					goto endword;	/* exit outer loop */
940				USTPUTC(c, out);
941				plinno++;
942				if (doprompt)
943					setprompt(2);
944				else
945					setprompt(0);
946				c = pgetc();
947				goto loop;		/* continue outer loop */
948			case CWORD:
949				USTPUTC(c, out);
950				break;
951			case CCTL:
952				if (eofmark == NULL || dblquote)
953					USTPUTC(CTLESC, out);
954				USTPUTC(c, out);
955				break;
956			case CBACK:	/* backslash */
957				c = pgetc();
958				if (c == PEOF) {
959					USTPUTC('\\', out);
960					pungetc();
961				} else if (c == '\n') {
962					if (doprompt)
963						setprompt(2);
964					else
965						setprompt(0);
966				} else {
967					if (dblquote && c != '\\' &&
968					    c != '`' && c != '$' &&
969					    (c != '"' || eofmark != NULL))
970						USTPUTC('\\', out);
971					if (SQSYNTAX[c] == CCTL)
972						USTPUTC(CTLESC, out);
973					else if (eofmark == NULL)
974						USTPUTC(CTLQUOTEMARK, out);
975					USTPUTC(c, out);
976					quotef++;
977				}
978				break;
979			case CSQUOTE:
980				if (eofmark == NULL)
981					USTPUTC(CTLQUOTEMARK, out);
982				syntax = SQSYNTAX;
983				break;
984			case CDQUOTE:
985				if (eofmark == NULL)
986					USTPUTC(CTLQUOTEMARK, out);
987				syntax = DQSYNTAX;
988				dblquote = 1;
989				break;
990			case CENDQUOTE:
991				if (eofmark != NULL && arinest == 0 &&
992				    varnest == 0) {
993					USTPUTC(c, out);
994				} else {
995					if (arinest) {
996						syntax = ARISYNTAX;
997						dblquote = 0;
998					} else if (eofmark == NULL) {
999						syntax = BASESYNTAX;
1000						dblquote = 0;
1001					}
1002					quotef++;
1003				}
1004				break;
1005			case CVAR:	/* '$' */
1006				PARSESUB();		/* parse substitution */
1007				break;
1008			case CENDVAR:	/* '}' */
1009				if (varnest > 0) {
1010					varnest--;
1011					USTPUTC(CTLENDVAR, out);
1012				} else {
1013					USTPUTC(c, out);
1014				}
1015				break;
1016			case CLP:	/* '(' in arithmetic */
1017				parenlevel++;
1018				USTPUTC(c, out);
1019				break;
1020			case CRP:	/* ')' in arithmetic */
1021				if (parenlevel > 0) {
1022					USTPUTC(c, out);
1023					--parenlevel;
1024				} else {
1025					if (pgetc() == ')') {
1026						if (--arinest == 0) {
1027							USTPUTC(CTLENDARI, out);
1028							syntax = prevsyntax;
1029							if (syntax == DQSYNTAX)
1030								dblquote = 1;
1031							else
1032								dblquote = 0;
1033						} else
1034							USTPUTC(')', out);
1035					} else {
1036						/*
1037						 * unbalanced parens
1038						 *  (don't 2nd guess - no error)
1039						 */
1040						pungetc();
1041						USTPUTC(')', out);
1042					}
1043				}
1044				break;
1045			case CBQUOTE:	/* '`' */
1046				PARSEBACKQOLD();
1047				break;
1048			case CEOF:
1049				goto endword;		/* exit outer loop */
1050			default:
1051				if (varnest == 0)
1052					goto endword;	/* exit outer loop */
1053				USTPUTC(c, out);
1054			}
1055			c = pgetc_macro();
1056		}
1057	}
1058endword:
1059	if (syntax == ARISYNTAX)
1060		synerror("Missing '))'");
1061	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1062		synerror("Unterminated quoted string");
1063	if (varnest != 0) {
1064		startlinno = plinno;
1065		synerror("Missing '}'");
1066	}
1067	USTPUTC('\0', out);
1068	len = out - stackblock();
1069	out = stackblock();
1070	if (eofmark == NULL) {
1071		if ((c == '>' || c == '<')
1072		 && quotef == 0
1073		 && len <= 2
1074		 && (*out == '\0' || is_digit(*out))) {
1075			PARSEREDIR();
1076			return lasttoken = TREDIR;
1077		} else {
1078			pungetc();
1079		}
1080	}
1081	quoteflag = quotef;
1082	backquotelist = bqlist;
1083	grabstackblock(len);
1084	wordtext = out;
1085	return lasttoken = TWORD;
1086/* end of readtoken routine */
1087
1088
1089
1090/*
1091 * Check to see whether we are at the end of the here document.  When this
1092 * is called, c is set to the first character of the next input line.  If
1093 * we are at the end of the here document, this routine sets the c to PEOF.
1094 */
1095
1096checkend: {
1097	if (eofmark) {
1098		if (striptabs) {
1099			while (c == '\t')
1100				c = pgetc();
1101		}
1102		if (c == *eofmark) {
1103			if (pfgets(line, sizeof line) != NULL) {
1104				char *p, *q;
1105
1106				p = line;
1107				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1108				if (*p == '\n' && *q == '\0') {
1109					c = PEOF;
1110					plinno++;
1111					needprompt = doprompt;
1112				} else {
1113					pushstring(line, strlen(line), NULL);
1114				}
1115			}
1116		}
1117	}
1118	goto checkend_return;
1119}
1120
1121
1122/*
1123 * Parse a redirection operator.  The variable "out" points to a string
1124 * specifying the fd to be redirected.  The variable "c" contains the
1125 * first character of the redirection operator.
1126 */
1127
1128parseredir: {
1129	char fd = *out;
1130	union node *np;
1131
1132	np = (union node *)stalloc(sizeof (struct nfile));
1133	if (c == '>') {
1134		np->nfile.fd = 1;
1135		c = pgetc();
1136		if (c == '>')
1137			np->type = NAPPEND;
1138		else if (c == '&')
1139			np->type = NTOFD;
1140		else if (c == '|')
1141			np->type = NCLOBBER;
1142		else {
1143			np->type = NTO;
1144			pungetc();
1145		}
1146	} else {	/* c == '<' */
1147		np->nfile.fd = 0;
1148		c = pgetc();
1149		if (c == '<') {
1150			if (sizeof (struct nfile) != sizeof (struct nhere)) {
1151				np = (union node *)stalloc(sizeof (struct nhere));
1152				np->nfile.fd = 0;
1153			}
1154			np->type = NHERE;
1155			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1156			heredoc->here = np;
1157			if ((c = pgetc()) == '-') {
1158				heredoc->striptabs = 1;
1159			} else {
1160				heredoc->striptabs = 0;
1161				pungetc();
1162			}
1163		} else if (c == '&')
1164			np->type = NFROMFD;
1165		else if (c == '>')
1166			np->type = NFROMTO;
1167		else {
1168			np->type = NFROM;
1169			pungetc();
1170		}
1171	}
1172	if (fd != '\0')
1173		np->nfile.fd = digit_val(fd);
1174	redirnode = np;
1175	goto parseredir_return;
1176}
1177
1178
1179/*
1180 * Parse a substitution.  At this point, we have read the dollar sign
1181 * and nothing else.
1182 */
1183
1184parsesub: {
1185	int subtype;
1186	int typeloc;
1187	int flags;
1188	char *p;
1189#ifndef GDB_HACK
1190	static const char types[] = "}-+?=";
1191#endif
1192       int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1193
1194	c = pgetc();
1195	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
1196		USTPUTC('$', out);
1197		pungetc();
1198	} else if (c == '(') {	/* $(command) or $((arith)) */
1199		if (pgetc() == '(') {
1200			PARSEARITH();
1201		} else {
1202			pungetc();
1203			PARSEBACKQNEW();
1204		}
1205	} else {
1206		USTPUTC(CTLVAR, out);
1207		typeloc = out - stackblock();
1208		USTPUTC(VSNORMAL, out);
1209		subtype = VSNORMAL;
1210		if (c == '{') {
1211			bracketed_name = 1;
1212			c = pgetc();
1213			if (c == '#') {
1214				if ((c = pgetc()) == '}')
1215					c = '#';
1216				else
1217					subtype = VSLENGTH;
1218			}
1219			else
1220				subtype = 0;
1221		}
1222		if (is_name(c)) {
1223			do {
1224				STPUTC(c, out);
1225				c = pgetc();
1226			} while (is_in_name(c));
1227		} else if (is_digit(c)) {
1228			if (bracketed_name) {
1229				do {
1230					STPUTC(c, out);
1231					c = pgetc();
1232				} while (is_digit(c));
1233			} else {
1234				STPUTC(c, out);
1235				c = pgetc();
1236			}
1237		} else {
1238			if (! is_special(c))
1239badsub:				synerror("Bad substitution");
1240			USTPUTC(c, out);
1241			c = pgetc();
1242		}
1243		STPUTC('=', out);
1244		flags = 0;
1245		if (subtype == 0) {
1246			switch (c) {
1247			case ':':
1248				flags = VSNUL;
1249				c = pgetc();
1250				/*FALLTHROUGH*/
1251			default:
1252				p = strchr(types, c);
1253				if (p == NULL)
1254					goto badsub;
1255				subtype = p - types + VSNORMAL;
1256				break;
1257			case '%':
1258			case '#':
1259				{
1260					int cc = c;
1261					subtype = c == '#' ? VSTRIMLEFT :
1262							     VSTRIMRIGHT;
1263					c = pgetc();
1264					if (c == cc)
1265						subtype++;
1266					else
1267						pungetc();
1268					break;
1269				}
1270			}
1271		} else {
1272			pungetc();
1273		}
1274		if (subtype != VSLENGTH && (dblquote || arinest))
1275			flags |= VSQUOTE;
1276		*(stackblock() + typeloc) = subtype | flags;
1277		if (subtype != VSNORMAL)
1278			varnest++;
1279	}
1280	goto parsesub_return;
1281}
1282
1283
1284/*
1285 * Called to parse command substitutions.  Newstyle is set if the command
1286 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1287 * list of commands (passed by reference), and savelen is the number of
1288 * characters on the top of the stack which must be preserved.
1289 */
1290
1291parsebackq: {
1292	struct nodelist **nlpp;
1293	int savepbq;
1294	union node *n;
1295	char *volatile str;
1296	struct jmploc jmploc;
1297	struct jmploc *volatile savehandler;
1298	int savelen;
1299	int saveprompt;
1300#if __GNUC__
1301	/* Avoid longjmp clobbering */
1302	(void) &saveprompt;
1303#endif
1304
1305	savepbq = parsebackquote;
1306	if (setjmp(jmploc.loc)) {
1307		if (str)
1308			ckfree(str);
1309		parsebackquote = 0;
1310		handler = savehandler;
1311		longjmp(handler->loc, 1);
1312	}
1313	INTOFF;
1314	str = NULL;
1315	savelen = out - stackblock();
1316	if (savelen > 0) {
1317		str = ckmalloc(savelen);
1318		memcpy(str, stackblock(), savelen);
1319	}
1320	savehandler = handler;
1321	handler = &jmploc;
1322	INTON;
1323        if (oldstyle) {
1324                /* We must read until the closing backquote, giving special
1325                   treatment to some slashes, and then push the string and
1326                   reread it as input, interpreting it normally.  */
1327                char *out;
1328                int c;
1329                int savelen;
1330                char *str;
1331
1332
1333                STARTSTACKSTR(out);
1334		for (;;) {
1335			if (needprompt) {
1336				setprompt(2);
1337				needprompt = 0;
1338			}
1339			switch (c = pgetc()) {
1340			case '`':
1341				goto done;
1342
1343			case '\\':
1344                                if ((c = pgetc()) == '\n') {
1345					plinno++;
1346					if (doprompt)
1347						setprompt(2);
1348					else
1349						setprompt(0);
1350					/*
1351					 * If eating a newline, avoid putting
1352					 * the newline into the new character
1353					 * stream (via the STPUTC after the
1354					 * switch).
1355					 */
1356					continue;
1357				}
1358                                if (c != '\\' && c != '`' && c != '$'
1359                                    && (!dblquote || c != '"'))
1360                                        STPUTC('\\', out);
1361				break;
1362
1363			case '\n':
1364				plinno++;
1365				needprompt = doprompt;
1366				break;
1367
1368			case PEOF:
1369			        startlinno = plinno;
1370				synerror("EOF in backquote substitution");
1371 				break;
1372
1373			default:
1374				break;
1375			}
1376			STPUTC(c, out);
1377                }
1378done:
1379                STPUTC('\0', out);
1380                savelen = out - stackblock();
1381                if (savelen > 0) {
1382                        str = ckmalloc(savelen);
1383                        memcpy(str, stackblock(), savelen);
1384			setinputstring(str, 1);
1385                }
1386        }
1387	nlpp = &bqlist;
1388	while (*nlpp)
1389		nlpp = &(*nlpp)->next;
1390	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1391	(*nlpp)->next = NULL;
1392	parsebackquote = oldstyle;
1393
1394	if (oldstyle) {
1395		saveprompt = doprompt;
1396		doprompt = 0;
1397	}
1398
1399	n = list(0);
1400
1401	if (oldstyle)
1402		doprompt = saveprompt;
1403	else {
1404		if (readtoken() != TRP)
1405			synexpect(TRP);
1406	}
1407
1408	(*nlpp)->n = n;
1409        if (oldstyle) {
1410		/*
1411		 * Start reading from old file again, ignoring any pushed back
1412		 * tokens left from the backquote parsing
1413		 */
1414                popfile();
1415		tokpushback = 0;
1416	}
1417	while (stackblocksize() <= savelen)
1418		growstackblock();
1419	STARTSTACKSTR(out);
1420	if (str) {
1421		memcpy(out, str, savelen);
1422		STADJUST(savelen, out);
1423		INTOFF;
1424		ckfree(str);
1425		str = NULL;
1426		INTON;
1427	}
1428	parsebackquote = savepbq;
1429	handler = savehandler;
1430	if (arinest || dblquote)
1431		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1432	else
1433		USTPUTC(CTLBACKQ, out);
1434	if (oldstyle)
1435		goto parsebackq_oldreturn;
1436	else
1437		goto parsebackq_newreturn;
1438}
1439
1440/*
1441 * Parse an arithmetic expansion (indicate start of one and set state)
1442 */
1443parsearith: {
1444
1445	if (++arinest == 1) {
1446		prevsyntax = syntax;
1447		syntax = ARISYNTAX;
1448		USTPUTC(CTLARI, out);
1449		if (dblquote)
1450			USTPUTC('"',out);
1451		else
1452			USTPUTC(' ',out);
1453	} else {
1454		/*
1455		 * we collapse embedded arithmetic expansion to
1456		 * parenthesis, which should be equivalent
1457		 */
1458		USTPUTC('(', out);
1459	}
1460	goto parsearith_return;
1461}
1462
1463} /* end of readtoken */
1464
1465
1466
1467#ifdef mkinit
1468RESET {
1469	tokpushback = 0;
1470	checkkwd = 0;
1471}
1472#endif
1473
1474/*
1475 * Returns true if the text contains nothing to expand (no dollar signs
1476 * or backquotes).
1477 */
1478
1479STATIC int
1480noexpand(char *text)
1481{
1482	char *p;
1483	char c;
1484
1485	p = text;
1486	while ((c = *p++) != '\0') {
1487		if ( c == CTLQUOTEMARK)
1488			continue;
1489		if (c == CTLESC)
1490			p++;
1491		else if (BASESYNTAX[(int)c] == CCTL)
1492			return 0;
1493	}
1494	return 1;
1495}
1496
1497
1498/*
1499 * Return true if the argument is a legal variable name (a letter or
1500 * underscore followed by zero or more letters, underscores, and digits).
1501 */
1502
1503int
1504goodname(char *name)
1505{
1506	char *p;
1507
1508	p = name;
1509	if (! is_name(*p))
1510		return 0;
1511	while (*++p) {
1512		if (! is_in_name(*p))
1513			return 0;
1514	}
1515	return 1;
1516}
1517
1518
1519/*
1520 * Called when an unexpected token is read during the parse.  The argument
1521 * is the token that is expected, or -1 if more than one type of token can
1522 * occur at this point.
1523 */
1524
1525STATIC void
1526synexpect(int token)
1527{
1528	char msg[64];
1529
1530	if (token >= 0) {
1531		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1532			tokname[lasttoken], tokname[token]);
1533	} else {
1534		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1535	}
1536	synerror(msg);
1537}
1538
1539
1540STATIC void
1541synerror(char *msg)
1542{
1543	if (commandname)
1544		outfmt(&errout, "%s: %d: ", commandname, startlinno);
1545	outfmt(&errout, "Syntax error: %s\n", msg);
1546	error((char *)NULL);
1547}
1548
1549STATIC void
1550setprompt(int which)
1551{
1552	whichprompt = which;
1553
1554#ifndef NO_HISTORY
1555	if (!el)
1556#endif
1557		out2str(getprompt(NULL));
1558}
1559
1560/*
1561 * called by editline -- any expansions to the prompt
1562 *    should be added here.
1563 */
1564char *
1565getprompt(void *unused __unused)
1566{
1567	static char ps[PROMPTLEN];
1568	char *fmt;
1569	int i, j, trim;
1570
1571	/*
1572	 * Select prompt format.
1573	 */
1574	switch (whichprompt) {
1575	case 0:
1576		fmt = "";
1577		break;
1578	case 1:
1579		fmt = ps1val();
1580		break;
1581	case 2:
1582		fmt = ps2val();
1583		break;
1584	default:
1585		return "<internal prompt error>";
1586	}
1587
1588	/*
1589	 * Format prompt string.
1590	 */
1591	for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1592		if (*fmt == '\\')
1593			switch (*++fmt) {
1594
1595				/*
1596				 * Hostname.
1597				 *
1598				 * \h specifies just the local hostname,
1599				 * \H specifies fully-qualified hostname.
1600				 */
1601			case 'h':
1602			case 'H':
1603				ps[i] == '\0';
1604				gethostname(&ps[i], PROMPTLEN - i);
1605				/* Skip to end of hostname. */
1606				trim = (*fmt == 'h') ? '.' : '\0';
1607				while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1608					i++;
1609				break;
1610
1611				/*
1612				 * Working directory.
1613				 *
1614				 * \W specifies just the final component,
1615				 * \w specifies the entire path.
1616				 */
1617			case 'W':
1618			case 'w':
1619				ps[i] == '\0';
1620				getcwd(&ps[i], PROMPTLEN - i);
1621				if (*fmt == 'W') {
1622					/* Final path component only. */
1623					trim = 1;
1624					for (j = i; ps[j] != '\0'; j++)
1625					  if (ps[j] == '/')
1626						trim = j + 1;
1627					memmove(&ps[i], &ps[trim],
1628					    j - trim + 1);
1629				}
1630				/* Skip to end of path. */
1631				while (ps[i + 1] != '\0')
1632					i++;
1633				break;
1634
1635				/*
1636				 * Superuser status.
1637				 *
1638				 * '$' for normal users, '#' for root.
1639				 */
1640			case '$':
1641				ps[i] = (geteuid() != 0) ? '$' : '#';
1642				break;
1643
1644				/*
1645				 * A literal \.
1646				 */
1647			case '\\':
1648				ps[i] = '\\';
1649				break;
1650
1651				/*
1652				 * Emit unrecognized formats verbatim.
1653				 */
1654			default:
1655				ps[i++] = '\\';
1656				ps[i] = *fmt;
1657				break;
1658			}
1659		else
1660			ps[i] = *fmt;
1661	ps[i] = '\0';
1662	return (ps);
1663}
1664