parser.c revision 17987
155505Sshin/*-
255505Sshin * Copyright (c) 1991, 1993
355505Sshin *	The Regents of the University of California.  All rights reserved.
455505Sshin *
555505Sshin * This code is derived from software contributed to Berkeley by
655505Sshin * Kenneth Almquist.
755505Sshin *
855505Sshin * Redistribution and use in source and binary forms, with or without
955505Sshin * modification, are permitted provided that the following conditions
1055505Sshin * are met:
1155505Sshin * 1. Redistributions of source code must retain the above copyright
1255505Sshin *    notice, this list of conditions and the following disclaimer.
1355505Sshin * 2. Redistributions in binary form must reproduce the above copyright
1455505Sshin *    notice, this list of conditions and the following disclaimer in the
1555505Sshin *    documentation and/or other materials provided with the distribution.
1655505Sshin * 3. All advertising materials mentioning features or use of this software
1755505Sshin *    must display the following acknowledgement:
1855505Sshin *	This product includes software developed by the University of
1955505Sshin *	California, Berkeley and its contributors.
2055505Sshin * 4. Neither the name of the University nor the names of its contributors
2155505Sshin *    may be used to endorse or promote products derived from this software
2255505Sshin *    without specific prior written permission.
2355505Sshin *
2455505Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2555505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2655505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2755505Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2855505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2955505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3055505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3155505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3255505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3355505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3455505Sshin * SUCH DAMAGE.
3555505Sshin *
3655505Sshin *	$Id: parser.c,v 1.11 1996/02/03 13:27:55 joerg Exp $
37171135Sgnn */
3855505Sshin
3955505Sshin#ifndef lint
4055505Sshinstatic char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
4155505Sshin#endif /* not lint */
4255505Sshin
4355505Sshin#include <stdlib.h>
4455505Sshin
4555505Sshin#include "shell.h"
4655505Sshin#include "parser.h"
4755505Sshin#include "nodes.h"
4855505Sshin#include "expand.h"	/* defines rmescapes() */
4955505Sshin#include "redir.h"	/* defines copyfd() */
5055505Sshin#include "syntax.h"
5155505Sshin#include "options.h"
5255505Sshin#include "input.h"
5355505Sshin#include "output.h"
5455505Sshin#include "var.h"
5555505Sshin#include "error.h"
5655505Sshin#include "memalloc.h"
5755505Sshin#include "mystring.h"
5855505Sshin#include "alias.h"
5955505Sshin#include "show.h"
6055505Sshin#ifndef NO_HISTORY
6155505Sshin#include "myhistedit.h"
6255505Sshin#endif
6355505Sshin
6455505Sshin/*
6555505Sshin * Shell command parser.
6655505Sshin */
6755505Sshin
6855505Sshin#define EOFMARKLEN 79
6955505Sshin
7055505Sshin/* values returned by readtoken */
7155505Sshin#include "token.h"
7255505Sshin
7355505Sshin
7455505Sshin
7555505Sshinstruct heredoc {
7655505Sshin	struct heredoc *next;	/* next here document in list */
7755505Sshin	union node *here;		/* redirection node */
7855505Sshin	char *eofmark;		/* string indicating end of input */
7955505Sshin	int striptabs;		/* if set, strip leading tabs */
8055505Sshin};
8155505Sshin
8255505Sshin
8355505Sshin
8455505Sshinstruct heredoc *heredoclist;	/* list of here documents to read */
8555505Sshinint parsebackquote;		/* nonzero if we are inside backquotes */
8655505Sshinint doprompt;			/* if set, prompt the user */
8755505Sshinint needprompt;			/* true if interactive and at start of line */
8855505Sshinint lasttoken;			/* last token read */
8955505SshinMKINIT int tokpushback;		/* last token pushed back */
9055505Sshinchar *wordtext;			/* text of last word returned by readtoken */
9155505SshinMKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
9255505Sshinstruct nodelist *backquotelist;
9355505Sshinunion node *redirnode;
9455505Sshinstruct heredoc *heredoc;
9555505Sshinint quoteflag;			/* set if (part of) last token was quoted */
9655505Sshinint startlinno;			/* line # where last token started */
9755505Sshin
9855505Sshin
9955505Sshin#define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
10055505Sshin#ifdef GDB_HACK
10155505Sshinstatic const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'};
10255505Sshinstatic const char types[] = "}-+?=";
10355505Sshin#endif
10455505Sshin
10555505Sshin
10655505SshinSTATIC union node *list __P((int));
10755505SshinSTATIC union node *andor __P((void));
10855505SshinSTATIC union node *pipeline __P((void));
10955505SshinSTATIC union node *command __P((void));
11055505SshinSTATIC union node *simplecmd __P((union node **, union node *));
11155505SshinSTATIC union node *makename __P((void));
11255505SshinSTATIC void parsefname __P((void));
11355505SshinSTATIC void parseheredoc __P((void));
11455505SshinSTATIC int peektoken __P((void));
11555505SshinSTATIC int readtoken __P((void));
11655505SshinSTATIC int xxreadtoken __P((void));
11755505SshinSTATIC int readtoken1 __P((int, char const *, char *, int));
11855505SshinSTATIC int noexpand __P((char *));
11955505SshinSTATIC void synexpect __P((int));
12055505SshinSTATIC void synerror __P((char *));
12155505SshinSTATIC void setprompt __P((int));
12255505Sshin
12355505Sshin
12455505Sshin/*
12555505Sshin * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
12655505Sshin * valid parse tree indicating a blank line.)
12755505Sshin */
12855505Sshin
12955505Sshinunion node *
13055505Sshinparsecmd(interact)
13155505Sshin	int interact;
13255505Sshin{
13355505Sshin	int t;
13455505Sshin
13555505Sshin	doprompt = interact;
13655505Sshin	if (doprompt)
13755505Sshin		setprompt(1);
13855505Sshin	else
13955505Sshin		setprompt(0);
14055505Sshin	needprompt = 0;
14155505Sshin	t = readtoken();
14255505Sshin	if (t == TEOF)
14355505Sshin		return NEOF;
14455505Sshin	if (t == TNL)
14555505Sshin		return NULL;
14655505Sshin	tokpushback++;
14755505Sshin	return list(1);
14855505Sshin}
14955505Sshin
15055505Sshin
15155505SshinSTATIC union node *
15255505Sshinlist(nlflag)
15355505Sshin	int nlflag;
15455505Sshin{
15555505Sshin	union node *n1, *n2, *n3;
15655505Sshin	int tok;
15755505Sshin
15855505Sshin	checkkwd = 2;
15955505Sshin	if (nlflag == 0 && tokendlist[peektoken()])
16055505Sshin		return NULL;
161	n1 = NULL;
162	for (;;) {
163		n2 = andor();
164		tok = readtoken();
165		if (tok == TBACKGND) {
166			if (n2->type == NCMD || n2->type == NPIPE) {
167				n2->ncmd.backgnd = 1;
168			} else if (n2->type == NREDIR) {
169				n2->type = NBACKGND;
170			} else {
171				n3 = (union node *)stalloc(sizeof (struct nredir));
172				n3->type = NBACKGND;
173				n3->nredir.n = n2;
174				n3->nredir.redirect = NULL;
175				n2 = n3;
176			}
177		}
178		if (n1 == NULL) {
179			n1 = n2;
180		}
181		else {
182			n3 = (union node *)stalloc(sizeof (struct nbinary));
183			n3->type = NSEMI;
184			n3->nbinary.ch1 = n1;
185			n3->nbinary.ch2 = n2;
186			n1 = n3;
187		}
188		switch (tok) {
189		case TBACKGND:
190		case TSEMI:
191			tok = readtoken();
192			/* fall through */
193		case TNL:
194			if (tok == TNL) {
195				parseheredoc();
196				if (nlflag)
197					return n1;
198			} else {
199				tokpushback++;
200			}
201			checkkwd = 2;
202			if (tokendlist[peektoken()])
203				return n1;
204			break;
205		case TEOF:
206			if (heredoclist)
207				parseheredoc();
208			else
209				pungetc();		/* push back EOF on input */
210			return n1;
211		default:
212			if (nlflag)
213				synexpect(-1);
214			tokpushback++;
215			return n1;
216		}
217	}
218}
219
220
221
222STATIC union node *
223andor() {
224	union node *n1, *n2, *n3;
225	int t;
226
227	n1 = pipeline();
228	for (;;) {
229		if ((t = readtoken()) == TAND) {
230			t = NAND;
231		} else if (t == TOR) {
232			t = NOR;
233		} else {
234			tokpushback++;
235			return n1;
236		}
237		n2 = pipeline();
238		n3 = (union node *)stalloc(sizeof (struct nbinary));
239		n3->type = t;
240		n3->nbinary.ch1 = n1;
241		n3->nbinary.ch2 = n2;
242		n1 = n3;
243	}
244}
245
246
247
248STATIC union node *
249pipeline() {
250	union node *n1, *pipenode, *notnode;
251	struct nodelist *lp, *prev;
252	int negate = 0;
253
254	TRACE(("pipeline: entered\n"));
255	while (readtoken() == TNOT) {
256		TRACE(("pipeline: TNOT recognized\n"));
257		negate = !negate;
258	}
259	tokpushback++;
260	n1 = command();
261	if (readtoken() == TPIPE) {
262		pipenode = (union node *)stalloc(sizeof (struct npipe));
263		pipenode->type = NPIPE;
264		pipenode->npipe.backgnd = 0;
265		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
266		pipenode->npipe.cmdlist = lp;
267		lp->n = n1;
268		do {
269			prev = lp;
270			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
271			lp->n = command();
272			prev->next = lp;
273		} while (readtoken() == TPIPE);
274		lp->next = NULL;
275		n1 = pipenode;
276	}
277	tokpushback++;
278	if (negate) {
279		notnode = (union node *)stalloc(sizeof (struct nnot));
280		notnode->type = NNOT;
281		notnode->nnot.com = n1;
282		n1 = notnode;
283	}
284	return n1;
285}
286
287
288
289STATIC union node *
290command() {
291	union node *n1, *n2;
292	union node *ap, **app;
293	union node *cp, **cpp;
294	union node *redir, **rpp;
295	int t;
296
297	checkkwd = 2;
298	redir = NULL;
299	n1 = NULL;
300	rpp = &redir;
301	/* Check for redirection which may precede command */
302	while (readtoken() == TREDIR) {
303		*rpp = n2 = redirnode;
304		rpp = &n2->nfile.next;
305		parsefname();
306	}
307	tokpushback++;
308
309	switch (readtoken()) {
310	case TIF:
311		n1 = (union node *)stalloc(sizeof (struct nif));
312		n1->type = NIF;
313		n1->nif.test = list(0);
314		if (readtoken() != TTHEN)
315			synexpect(TTHEN);
316		n1->nif.ifpart = list(0);
317		n2 = n1;
318		while (readtoken() == TELIF) {
319			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
320			n2 = n2->nif.elsepart;
321			n2->type = NIF;
322			n2->nif.test = list(0);
323			if (readtoken() != TTHEN)
324				synexpect(TTHEN);
325			n2->nif.ifpart = list(0);
326		}
327		if (lasttoken == TELSE)
328			n2->nif.elsepart = list(0);
329		else {
330			n2->nif.elsepart = NULL;
331			tokpushback++;
332		}
333		if (readtoken() != TFI)
334			synexpect(TFI);
335		checkkwd = 1;
336		break;
337	case TWHILE:
338	case TUNTIL: {
339		int got;
340		n1 = (union node *)stalloc(sizeof (struct nbinary));
341		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
342		n1->nbinary.ch1 = list(0);
343		if ((got=readtoken()) != TDO) {
344TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
345			synexpect(TDO);
346		}
347		n1->nbinary.ch2 = list(0);
348		if (readtoken() != TDONE)
349			synexpect(TDONE);
350		checkkwd = 1;
351		break;
352	}
353	case TFOR:
354		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
355			synerror("Bad for loop variable");
356		n1 = (union node *)stalloc(sizeof (struct nfor));
357		n1->type = NFOR;
358		n1->nfor.var = wordtext;
359		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
360			app = &ap;
361			while (readtoken() == TWORD) {
362				n2 = (union node *)stalloc(sizeof (struct narg));
363				n2->type = NARG;
364				n2->narg.text = wordtext;
365				n2->narg.backquote = backquotelist;
366				*app = n2;
367				app = &n2->narg.next;
368			}
369			*app = NULL;
370			n1->nfor.args = ap;
371			if (lasttoken != TNL && lasttoken != TSEMI)
372				synexpect(-1);
373		} else {
374#ifndef GDB_HACK
375			static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
376								   '@', '=', '\0'};
377#endif
378			n2 = (union node *)stalloc(sizeof (struct narg));
379			n2->type = NARG;
380			n2->narg.text = (char *)argvars;
381			n2->narg.backquote = NULL;
382			n2->narg.next = NULL;
383			n1->nfor.args = n2;
384			/*
385			 * Newline or semicolon here is optional (but note
386			 * that the original Bourne shell only allowed NL).
387			 */
388			if (lasttoken != TNL && lasttoken != TSEMI)
389				tokpushback++;
390		}
391		checkkwd = 2;
392		if ((t = readtoken()) == TDO)
393			t = TDONE;
394		else if (t == TBEGIN)
395			t = TEND;
396		else
397			synexpect(-1);
398		n1->nfor.body = list(0);
399		if (readtoken() != t)
400			synexpect(t);
401		checkkwd = 1;
402		break;
403	case TCASE:
404		n1 = (union node *)stalloc(sizeof (struct ncase));
405		n1->type = NCASE;
406		if (readtoken() != TWORD)
407			synexpect(TWORD);
408		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
409		n2->type = NARG;
410		n2->narg.text = wordtext;
411		n2->narg.backquote = backquotelist;
412		n2->narg.next = NULL;
413		while (readtoken() == TNL);
414		if (lasttoken != TWORD || ! equal(wordtext, "in"))
415			synerror("expecting \"in\"");
416		cpp = &n1->ncase.cases;
417		checkkwd = 2, readtoken();
418		do {
419			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
420			cp->type = NCLIST;
421			app = &cp->nclist.pattern;
422			for (;;) {
423				*app = ap = (union node *)stalloc(sizeof (struct narg));
424				ap->type = NARG;
425				ap->narg.text = wordtext;
426				ap->narg.backquote = backquotelist;
427				if (checkkwd = 2, readtoken() != TPIPE)
428					break;
429				app = &ap->narg.next;
430				readtoken();
431			}
432			ap->narg.next = NULL;
433			if (lasttoken != TRP)
434				synexpect(TRP);
435			cp->nclist.body = list(0);
436
437			checkkwd = 2;
438			if ((t = readtoken()) != TESAC) {
439				if (t != TENDCASE)
440					synexpect(TENDCASE);
441				else
442					checkkwd = 2, readtoken();
443			}
444			cpp = &cp->nclist.next;
445		} while(lasttoken != TESAC);
446		*cpp = NULL;
447		checkkwd = 1;
448		break;
449	case TLP:
450		n1 = (union node *)stalloc(sizeof (struct nredir));
451		n1->type = NSUBSHELL;
452		n1->nredir.n = list(0);
453		n1->nredir.redirect = NULL;
454		if (readtoken() != TRP)
455			synexpect(TRP);
456		checkkwd = 1;
457		break;
458	case TBEGIN:
459		n1 = list(0);
460		if (readtoken() != TEND)
461			synexpect(TEND);
462		checkkwd = 1;
463		break;
464	/* Handle an empty command like other simple commands.  */
465	case TSEMI:
466		/*
467		 * An empty command before a ; doesn't make much sense, and
468		 * should certainly be disallowed in the case of `if ;'.
469		 */
470		if (!redir)
471			synexpect(-1);
472	case TAND:	/* XXX merge query! */
473	case TOR:	/* XXX merge query! */
474	case TNL:
475	case TEOF:
476	case TWORD:
477	case TRP:
478		tokpushback++;
479		return simplecmd(rpp, redir);
480	default:
481		synexpect(-1);
482	}
483
484	/* Now check for redirection which may follow command */
485	while (readtoken() == TREDIR) {
486		*rpp = n2 = redirnode;
487		rpp = &n2->nfile.next;
488		parsefname();
489	}
490	tokpushback++;
491	*rpp = NULL;
492	if (redir) {
493		if (n1->type != NSUBSHELL) {
494			n2 = (union node *)stalloc(sizeof (struct nredir));
495			n2->type = NREDIR;
496			n2->nredir.n = n1;
497			n1 = n2;
498		}
499		n1->nredir.redirect = redir;
500	}
501	return n1;
502}
503
504
505STATIC union node *
506simplecmd(rpp, redir)
507	union node **rpp, *redir;
508	{
509	union node *args, **app;
510	union node **orig_rpp = rpp;
511	union node *n = NULL;
512
513	/* If we don't have any redirections already, then we must reset */
514	/* rpp to be the address of the local redir variable.  */
515	if (redir == 0)
516		rpp = &redir;
517
518	args = NULL;
519	app = &args;
520	/*
521	 * We save the incoming value, because we need this for shell
522	 * functions.  There can not be a redirect or an argument between
523	 * the function name and the open parenthesis.
524	 */
525	orig_rpp = rpp;
526
527	for (;;) {
528		if (readtoken() == TWORD) {
529			n = (union node *)stalloc(sizeof (struct narg));
530			n->type = NARG;
531			n->narg.text = wordtext;
532			n->narg.backquote = backquotelist;
533			*app = n;
534			app = &n->narg.next;
535		} else if (lasttoken == TREDIR) {
536			*rpp = n = redirnode;
537			rpp = &n->nfile.next;
538			parsefname();	/* read name of redirection file */
539		} else if (lasttoken == TLP && app == &args->narg.next
540					    && rpp == orig_rpp) {
541			/* We have a function */
542			if (readtoken() != TRP)
543				synexpect(TRP);
544#ifdef notdef
545			if (! goodname(n->narg.text))
546				synerror("Bad function name");
547#endif
548			n->type = NDEFUN;
549			n->narg.next = command();
550			return n;
551		} else {
552			tokpushback++;
553			break;
554		}
555	}
556	*app = NULL;
557	*rpp = NULL;
558	n = (union node *)stalloc(sizeof (struct ncmd));
559	n->type = NCMD;
560	n->ncmd.backgnd = 0;
561	n->ncmd.args = args;
562	n->ncmd.redirect = redir;
563	return n;
564}
565
566STATIC union node *
567makename() {
568	union node *n;
569
570	n = (union node *)stalloc(sizeof (struct narg));
571	n->type = NARG;
572	n->narg.next = NULL;
573	n->narg.text = wordtext;
574	n->narg.backquote = backquotelist;
575	return n;
576}
577
578void fixredir(n, text, err)
579	union node *n;
580	const char *text;
581	int err;
582	{
583	TRACE(("Fix redir %s %d\n", text, err));
584	if (!err)
585		n->ndup.vname = NULL;
586
587	if (is_digit(text[0]) && text[1] == '\0')
588		n->ndup.dupfd = digit_val(text[0]);
589	else if (text[0] == '-' && text[1] == '\0')
590		n->ndup.dupfd = -1;
591	else {
592
593		if (err)
594			synerror("Bad fd number");
595		else
596			n->ndup.vname = makename();
597	}
598}
599
600
601STATIC void
602parsefname() {
603	union node *n = redirnode;
604
605	if (readtoken() != TWORD)
606		synexpect(-1);
607	if (n->type == NHERE) {
608		struct heredoc *here = heredoc;
609		struct heredoc *p;
610		int i;
611
612		if (quoteflag == 0)
613			n->type = NXHERE;
614		TRACE(("Here document %d\n", n->type));
615		if (here->striptabs) {
616			while (*wordtext == '\t')
617				wordtext++;
618		}
619		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
620			synerror("Illegal eof marker for << redirection");
621		rmescapes(wordtext);
622		here->eofmark = wordtext;
623		here->next = NULL;
624		if (heredoclist == NULL)
625			heredoclist = here;
626		else {
627			for (p = heredoclist ; p->next ; p = p->next);
628			p->next = here;
629		}
630	} else if (n->type == NTOFD || n->type == NFROMFD) {
631		fixredir(n, wordtext, 0);
632	} else {
633		n->nfile.fname = makename();
634	}
635}
636
637
638/*
639 * Input any here documents.
640 */
641
642STATIC void
643parseheredoc() {
644	struct heredoc *here;
645	union node *n;
646
647	while (heredoclist) {
648		here = heredoclist;
649		heredoclist = here->next;
650		if (needprompt) {
651			setprompt(2);
652			needprompt = 0;
653		}
654		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
655				here->eofmark, here->striptabs);
656		n = (union node *)stalloc(sizeof (struct narg));
657		n->narg.type = NARG;
658		n->narg.next = NULL;
659		n->narg.text = wordtext;
660		n->narg.backquote = backquotelist;
661		here->here->nhere.doc = n;
662	}
663}
664
665STATIC int
666peektoken() {
667	int t;
668
669	t = readtoken();
670	tokpushback++;
671	return (t);
672}
673
674STATIC int xxreadtoken();
675
676STATIC int
677readtoken() {
678	int t;
679	int savecheckkwd = checkkwd;
680	struct alias *ap;
681#ifdef DEBUG
682	int alreadyseen = tokpushback;
683#endif
684
685	top:
686	t = xxreadtoken();
687
688	if (checkkwd) {
689		/*
690		 * eat newlines
691		 */
692		if (checkkwd == 2) {
693			checkkwd = 0;
694			while (t == TNL) {
695				parseheredoc();
696				t = xxreadtoken();
697			}
698		} else
699			checkkwd = 0;
700		/*
701		 * check for keywords and aliases
702		 */
703		if (t == TWORD && !quoteflag)
704		{
705			register char * const *pp;
706
707			for (pp = (char **)parsekwd; *pp; pp++) {
708				if (**pp == *wordtext && equal(*pp, wordtext))
709				{
710					lasttoken = t = pp - parsekwd + KWDOFFSET;
711					TRACE(("keyword %s recognized\n", tokname[t]));
712					goto out;
713				}
714			}
715			if ((ap = lookupalias(wordtext, 1)) != NULL) {
716				pushstring(ap->val, strlen(ap->val), ap);
717				checkkwd = savecheckkwd;
718				goto top;
719			}
720		}
721out:
722		checkkwd = 0;
723	}
724#ifdef DEBUG
725	if (!alreadyseen)
726	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
727	else
728	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
729#endif
730	return (t);
731}
732
733
734/*
735 * Read the next input token.
736 * If the token is a word, we set backquotelist to the list of cmds in
737 *	backquotes.  We set quoteflag to true if any part of the word was
738 *	quoted.
739 * If the token is TREDIR, then we set redirnode to a structure containing
740 *	the redirection.
741 * In all cases, the variable startlinno is set to the number of the line
742 *	on which the token starts.
743 *
744 * [Change comment:  here documents and internal procedures]
745 * [Readtoken shouldn't have any arguments.  Perhaps we should make the
746 *  word parsing code into a separate routine.  In this case, readtoken
747 *  doesn't need to have any internal procedures, but parseword does.
748 *  We could also make parseoperator in essence the main routine, and
749 *  have parseword (readtoken1?) handle both words and redirection.]
750 */
751
752#define RETURN(token)	return lasttoken = token
753
754STATIC int
755xxreadtoken() {
756	register c;
757
758	if (tokpushback) {
759		tokpushback = 0;
760		return lasttoken;
761	}
762	if (needprompt) {
763		setprompt(2);
764		needprompt = 0;
765	}
766	startlinno = plinno;
767	for (;;) {	/* until token or start of word found */
768		c = pgetc_macro();
769		if (c == ' ' || c == '\t')
770			continue;		/* quick check for white space first */
771		switch (c) {
772		case ' ': case '\t':
773			continue;
774		case '#':
775			while ((c = pgetc()) != '\n' && c != PEOF);
776			pungetc();
777			continue;
778		case '\\':
779			if (pgetc() == '\n') {
780				startlinno = ++plinno;
781				if (doprompt)
782					setprompt(2);
783				else
784					setprompt(0);
785				continue;
786			}
787			pungetc();
788			goto breakloop;
789		case '\n':
790			plinno++;
791			needprompt = doprompt;
792			RETURN(TNL);
793		case PEOF:
794			RETURN(TEOF);
795		case '&':
796			if (pgetc() == '&')
797				RETURN(TAND);
798			pungetc();
799			RETURN(TBACKGND);
800		case '|':
801			if (pgetc() == '|')
802				RETURN(TOR);
803			pungetc();
804			RETURN(TPIPE);
805		case ';':
806			if (pgetc() == ';')
807				RETURN(TENDCASE);
808			pungetc();
809			RETURN(TSEMI);
810		case '(':
811			RETURN(TLP);
812		case ')':
813			RETURN(TRP);
814		default:
815			goto breakloop;
816		}
817	}
818breakloop:
819	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
820#undef RETURN
821}
822
823
824
825/*
826 * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
827 * is not NULL, read a here document.  In the latter case, eofmark is the
828 * word which marks the end of the document and striptabs is true if
829 * leading tabs should be stripped from the document.  The argument firstc
830 * is the first character of the input token or document.
831 *
832 * Because C does not have internal subroutines, I have simulated them
833 * using goto's to implement the subroutine linkage.  The following macros
834 * will run code that appears at the end of readtoken1.
835 */
836
837#define CHECKEND()	{goto checkend; checkend_return:;}
838#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
839#define PARSESUB()	{goto parsesub; parsesub_return:;}
840#define PARSEBACKQOLD()	{oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
841#define PARSEBACKQNEW()	{oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
842#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
843
844STATIC int
845readtoken1(firstc, syntax, eofmark, striptabs)
846	int firstc;
847	char const *syntax;
848	char *eofmark;
849	int striptabs;
850	{
851	int c = firstc;
852	char *out;
853	int len;
854	char line[EOFMARKLEN + 1];
855	struct nodelist *bqlist;
856	int quotef;
857	int dblquote;
858	int varnest;	/* levels of variables expansion */
859	int arinest;	/* levels of arithmetic expansion */
860	int parenlevel;	/* levels of parens in arithmetic */
861	int oldstyle;
862	char const *prevsyntax;	/* syntax before arithmetic */
863#if __GNUC__
864	/* Avoid longjmp clobbering */
865	(void) &out;
866	(void) &quotef;
867	(void) &dblquote;
868	(void) &varnest;
869	(void) &arinest;
870	(void) &parenlevel;
871	(void) &oldstyle;
872	(void) &prevsyntax;
873	(void) &syntax;
874#endif
875
876	startlinno = plinno;
877	dblquote = 0;
878	if (syntax == DQSYNTAX)
879		dblquote = 1;
880	quotef = 0;
881	bqlist = NULL;
882	varnest = 0;
883	arinest = 0;
884	parenlevel = 0;
885
886	STARTSTACKSTR(out);
887	loop: {	/* for each line, until end of word */
888#if ATTY
889		if (c == '\034' && doprompt
890		 && attyset() && ! equal(termval(), "emacs")) {
891			attyline();
892			if (syntax == BASESYNTAX)
893				return readtoken();
894			c = pgetc();
895			goto loop;
896		}
897#endif
898		CHECKEND();	/* set c to PEOF if at end of here document */
899		for (;;) {	/* until end of line or end of word */
900			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
901			switch(syntax[c]) {
902			case CNL:	/* '\n' */
903				if (syntax == BASESYNTAX)
904					goto endword;	/* exit outer loop */
905				USTPUTC(c, out);
906				plinno++;
907				if (doprompt)
908					setprompt(2);
909				else
910					setprompt(0);
911				c = pgetc();
912				goto loop;		/* continue outer loop */
913			case CWORD:
914				USTPUTC(c, out);
915				break;
916			case CCTL:
917				if (eofmark == NULL || dblquote)
918					USTPUTC(CTLESC, out);
919				USTPUTC(c, out);
920				break;
921			case CBACK:	/* backslash */
922				c = pgetc();
923				if (c == PEOF) {
924					USTPUTC('\\', out);
925					pungetc();
926				} else if (c == '\n') {
927					if (doprompt)
928						setprompt(2);
929					else
930						setprompt(0);
931				} else {
932					if (dblquote && c != '\\' && c != '`' && c != '$'
933							 && (c != '"' || eofmark != NULL))
934						USTPUTC('\\', out);
935					if (SQSYNTAX[c] == CCTL)
936						USTPUTC(CTLESC, out);
937					USTPUTC(c, out);
938					quotef++;
939				}
940				break;
941			case CSQUOTE:
942				syntax = SQSYNTAX;
943				break;
944			case CDQUOTE:
945				syntax = DQSYNTAX;
946				dblquote = 1;
947				break;
948			case CENDQUOTE:
949				if (eofmark) {
950					USTPUTC(c, out);
951				} else {
952					if (arinest)
953						syntax = ARISYNTAX;
954					else
955						syntax = BASESYNTAX;
956					quotef++;
957					dblquote = 0;
958				}
959				break;
960			case CVAR:	/* '$' */
961				PARSESUB();		/* parse substitution */
962				break;
963			case CENDVAR:	/* '}' */
964				if (varnest > 0) {
965					varnest--;
966					USTPUTC(CTLENDVAR, out);
967				} else {
968					USTPUTC(c, out);
969				}
970				break;
971			case CLP:	/* '(' in arithmetic */
972				parenlevel++;
973				USTPUTC(c, out);
974				break;
975			case CRP:	/* ')' in arithmetic */
976				if (parenlevel > 0) {
977					USTPUTC(c, out);
978					--parenlevel;
979				} else {
980					if (pgetc() == ')') {
981						if (--arinest == 0) {
982							USTPUTC(CTLENDARI, out);
983							syntax = prevsyntax;
984						} else
985							USTPUTC(')', out);
986					} else {
987						/*
988						 * unbalanced parens
989						 *  (don't 2nd guess - no error)
990						 */
991						pungetc();
992						USTPUTC(')', out);
993					}
994				}
995				break;
996			case CBQUOTE:	/* '`' */
997				PARSEBACKQOLD();
998				break;
999			case CEOF:
1000				goto endword;		/* exit outer loop */
1001			default:
1002				if (varnest == 0)
1003					goto endword;	/* exit outer loop */
1004				USTPUTC(c, out);
1005			}
1006			c = pgetc_macro();
1007		}
1008	}
1009endword:
1010	if (syntax == ARISYNTAX)
1011		synerror("Missing '))'");
1012	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1013		synerror("Unterminated quoted string");
1014	if (varnest != 0) {
1015		startlinno = plinno;
1016		synerror("Missing '}'");
1017	}
1018	USTPUTC('\0', out);
1019	len = out - stackblock();
1020	out = stackblock();
1021	if (eofmark == NULL) {
1022		if ((c == '>' || c == '<')
1023		 && quotef == 0
1024		 && len <= 2
1025		 && (*out == '\0' || is_digit(*out))) {
1026			PARSEREDIR();
1027			return lasttoken = TREDIR;
1028		} else {
1029			pungetc();
1030		}
1031	}
1032	quoteflag = quotef;
1033	backquotelist = bqlist;
1034	grabstackblock(len);
1035	wordtext = out;
1036	return lasttoken = TWORD;
1037/* end of readtoken routine */
1038
1039
1040
1041/*
1042 * Check to see whether we are at the end of the here document.  When this
1043 * is called, c is set to the first character of the next input line.  If
1044 * we are at the end of the here document, this routine sets the c to PEOF.
1045 */
1046
1047checkend: {
1048	if (eofmark) {
1049		if (striptabs) {
1050			while (c == '\t')
1051				c = pgetc();
1052		}
1053		if (c == *eofmark) {
1054			if (pfgets(line, sizeof line) != NULL) {
1055				register char *p, *q;
1056
1057				p = line;
1058				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1059				if (*p == '\n' && *q == '\0') {
1060					c = PEOF;
1061					plinno++;
1062					needprompt = doprompt;
1063				} else {
1064					pushstring(line, strlen(line), NULL);
1065				}
1066			}
1067		}
1068	}
1069	goto checkend_return;
1070}
1071
1072
1073/*
1074 * Parse a redirection operator.  The variable "out" points to a string
1075 * specifying the fd to be redirected.  The variable "c" contains the
1076 * first character of the redirection operator.
1077 */
1078
1079parseredir: {
1080	char fd = *out;
1081	union node *np;
1082
1083	np = (union node *)stalloc(sizeof (struct nfile));
1084	if (c == '>') {
1085		np->nfile.fd = 1;
1086		c = pgetc();
1087		if (c == '>')
1088			np->type = NAPPEND;
1089		else if (c == '&')
1090			np->type = NTOFD;
1091		else {
1092			np->type = NTO;
1093			pungetc();
1094		}
1095	} else {	/* c == '<' */
1096		np->nfile.fd = 0;
1097		c = pgetc();
1098		if (c == '<') {
1099			if (sizeof (struct nfile) != sizeof (struct nhere)) {
1100				np = (union node *)stalloc(sizeof (struct nhere));
1101				np->nfile.fd = 0;
1102			}
1103			np->type = NHERE;
1104			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1105			heredoc->here = np;
1106			if ((c = pgetc()) == '-') {
1107				heredoc->striptabs = 1;
1108			} else {
1109				heredoc->striptabs = 0;
1110				pungetc();
1111			}
1112		} else if (c == '&')
1113			np->type = NFROMFD;
1114		else {
1115			np->type = NFROM;
1116			pungetc();
1117		}
1118	}
1119	if (fd != '\0')
1120		np->nfile.fd = digit_val(fd);
1121	redirnode = np;
1122	goto parseredir_return;
1123}
1124
1125
1126/*
1127 * Parse a substitution.  At this point, we have read the dollar sign
1128 * and nothing else.
1129 */
1130
1131parsesub: {
1132	int subtype;
1133	int typeloc;
1134	int flags;
1135	char *p;
1136#ifndef GDB_HACK
1137	static const char types[] = "}-+?=";
1138#endif
1139
1140	c = pgetc();
1141	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
1142		USTPUTC('$', out);
1143		pungetc();
1144	} else if (c == '(') {	/* $(command) or $((arith)) */
1145		if (pgetc() == '(') {
1146			PARSEARITH();
1147		} else {
1148			pungetc();
1149			PARSEBACKQNEW();
1150		}
1151	} else {
1152		USTPUTC(CTLVAR, out);
1153		typeloc = out - stackblock();
1154		USTPUTC(VSNORMAL, out);
1155		subtype = VSNORMAL;
1156		if (c == '{') {
1157			c = pgetc();
1158			if (c == '#') {
1159				if ((c = pgetc()) == '}')
1160					c = '#';
1161				else
1162					subtype = VSLENGTH;
1163			}
1164			else
1165				subtype = 0;
1166		}
1167		if (is_name(c)) {
1168			do {
1169				STPUTC(c, out);
1170				c = pgetc();
1171			} while (is_in_name(c));
1172		} else {
1173			if (! is_special(c))
1174badsub:				synerror("Bad substitution");
1175			USTPUTC(c, out);
1176			c = pgetc();
1177		}
1178		STPUTC('=', out);
1179		flags = 0;
1180		if (subtype == 0) {
1181			switch (c) {
1182			case ':':
1183				flags = VSNUL;
1184				c = pgetc();
1185				/*FALLTHROUGH*/
1186			default:
1187				p = strchr(types, c);
1188				if (p == NULL)
1189					goto badsub;
1190				subtype = p - types + VSNORMAL;
1191				break;
1192			case '%':
1193			case '#':
1194				{
1195					int cc = c;
1196					subtype = c == '#' ? VSTRIMLEFT :
1197							     VSTRIMRIGHT;
1198					c = pgetc();
1199					if (c == cc)
1200						subtype++;
1201					else
1202						pungetc();
1203					break;
1204				}
1205			}
1206		} else {
1207			pungetc();
1208		}
1209		if (dblquote || arinest)
1210			flags |= VSQUOTE;
1211		*(stackblock() + typeloc) = subtype | flags;
1212		if (subtype != VSNORMAL)
1213			varnest++;
1214	}
1215	goto parsesub_return;
1216}
1217
1218
1219/*
1220 * Called to parse command substitutions.  Newstyle is set if the command
1221 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1222 * list of commands (passed by reference), and savelen is the number of
1223 * characters on the top of the stack which must be preserved.
1224 */
1225
1226parsebackq: {
1227	struct nodelist **nlpp;
1228	int savepbq;
1229	union node *n;
1230	char *volatile str;
1231	struct jmploc jmploc;
1232	struct jmploc *volatile savehandler;
1233	int savelen;
1234
1235	savepbq = parsebackquote;
1236	if (setjmp(jmploc.loc)) {
1237		if (str)
1238			ckfree(str);
1239		parsebackquote = 0;
1240		handler = savehandler;
1241		longjmp(handler->loc, 1);
1242	}
1243	INTOFF;
1244	str = NULL;
1245	savelen = out - stackblock();
1246	if (savelen > 0) {
1247		str = ckmalloc(savelen);
1248		memcpy(str, stackblock(), savelen);
1249	}
1250	savehandler = handler;
1251	handler = &jmploc;
1252	INTON;
1253        if (oldstyle) {
1254                /* We must read until the closing backquote, giving special
1255                   treatment to some slashes, and then push the string and
1256                   reread it as input, interpreting it normally.  */
1257                register char *out;
1258                register c;
1259                int savelen;
1260                char *str;
1261
1262                STARTSTACKSTR(out);
1263                while ((c = pgetc ()) != '`') {
1264                       if (c == PEOF) {
1265                                startlinno = plinno;
1266                                synerror("EOF in backquote substitution");
1267                       }
1268                       if (c == '\\') {
1269                                c = pgetc ();
1270                                if (c != '\\' && c != '`' && c != '$'
1271                                    && (!dblquote || c != '"'))
1272                                        STPUTC('\\', out);
1273                       }
1274                       STPUTC(c, out);
1275                }
1276                STPUTC('\0', out);
1277                savelen = out - stackblock();
1278                if (savelen > 0) {
1279                        str = ckmalloc(savelen);
1280                        memcpy(str, stackblock(), savelen);
1281			setinputstring(str, 1);
1282                }
1283        }
1284	nlpp = &bqlist;
1285	while (*nlpp)
1286		nlpp = &(*nlpp)->next;
1287	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1288	(*nlpp)->next = NULL;
1289	parsebackquote = oldstyle;
1290	n = list(0);
1291        if (!oldstyle && (readtoken() != TRP))
1292                synexpect(TRP);
1293	(*nlpp)->n = n;
1294        /* Start reading from old file again.  */
1295        if (oldstyle)
1296                popfile();
1297	while (stackblocksize() <= savelen)
1298		growstackblock();
1299	STARTSTACKSTR(out);
1300	if (str) {
1301		memcpy(out, str, savelen);
1302		STADJUST(savelen, out);
1303		INTOFF;
1304		ckfree(str);
1305		str = NULL;
1306		INTON;
1307	}
1308	parsebackquote = savepbq;
1309	handler = savehandler;
1310	if (arinest || dblquote)
1311		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1312	else
1313		USTPUTC(CTLBACKQ, out);
1314	if (oldstyle)
1315		goto parsebackq_oldreturn;
1316	else
1317		goto parsebackq_newreturn;
1318}
1319
1320/*
1321 * Parse an arithmetic expansion (indicate start of one and set state)
1322 */
1323parsearith: {
1324
1325	if (++arinest == 1) {
1326		prevsyntax = syntax;
1327		syntax = ARISYNTAX;
1328		USTPUTC(CTLARI, out);
1329	} else {
1330		/*
1331		 * we collapse embedded arithmetic expansion to
1332		 * parenthesis, which should be equivalent
1333		 */
1334		USTPUTC('(', out);
1335	}
1336	goto parsearith_return;
1337}
1338
1339} /* end of readtoken */
1340
1341
1342
1343#ifdef mkinit
1344RESET {
1345	tokpushback = 0;
1346	checkkwd = 0;
1347}
1348#endif
1349
1350/*
1351 * Returns true if the text contains nothing to expand (no dollar signs
1352 * or backquotes).
1353 */
1354
1355STATIC int
1356noexpand(text)
1357	char *text;
1358	{
1359	register char *p;
1360	register char c;
1361
1362	p = text;
1363	while ((c = *p++) != '\0') {
1364		if (c == CTLESC)
1365			p++;
1366		else if (BASESYNTAX[c] == CCTL)
1367			return 0;
1368	}
1369	return 1;
1370}
1371
1372
1373/*
1374 * Return true if the argument is a legal variable name (a letter or
1375 * underscore followed by zero or more letters, underscores, and digits).
1376 */
1377
1378int
1379goodname(name)
1380	char *name;
1381	{
1382	register char *p;
1383
1384	p = name;
1385	if (! is_name(*p))
1386		return 0;
1387	while (*++p) {
1388		if (! is_in_name(*p))
1389			return 0;
1390	}
1391	return 1;
1392}
1393
1394
1395/*
1396 * Called when an unexpected token is read during the parse.  The argument
1397 * is the token that is expected, or -1 if more than one type of token can
1398 * occur at this point.
1399 */
1400
1401STATIC void
1402synexpect(token)
1403	int token;
1404{
1405	char msg[64];
1406
1407	if (token >= 0) {
1408		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1409			tokname[lasttoken], tokname[token]);
1410	} else {
1411		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1412	}
1413	synerror(msg);
1414}
1415
1416
1417STATIC void
1418synerror(msg)
1419	char *msg;
1420	{
1421	if (commandname)
1422		outfmt(&errout, "%s: %d: ", commandname, startlinno);
1423	outfmt(&errout, "Syntax error: %s\n", msg);
1424	error((char *)NULL);
1425}
1426
1427STATIC void
1428setprompt(which)
1429	int which;
1430	{
1431	whichprompt = which;
1432
1433#ifndef NO_HISTORY
1434	if (!el)
1435#endif
1436		out2str(getprompt(NULL));
1437}
1438
1439/*
1440 * called by editline -- any expansions to the prompt
1441 *    should be added here.
1442 */
1443char *
1444getprompt(unused)
1445	void *unused;
1446	{
1447	switch (whichprompt) {
1448	case 0:
1449		return "";
1450	case 1:
1451		return ps1val();
1452	case 2:
1453		return ps2val();
1454	default:
1455		return "<internal prompt error>";
1456	}
1457}
1458