xref: /freebsd/bin/sh/parser.c (revision 68e7a217f8019b955f87547f218e95ab237597af)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
40 #endif
41 static const char rcsid[] =
42   "$FreeBSD$";
43 #endif /* not lint */
44 
45 #include <stdlib.h>
46 
47 #include "shell.h"
48 #include "parser.h"
49 #include "nodes.h"
50 #include "expand.h"	/* defines rmescapes() */
51 #include "redir.h"	/* defines copyfd() */
52 #include "syntax.h"
53 #include "options.h"
54 #include "input.h"
55 #include "output.h"
56 #include "var.h"
57 #include "error.h"
58 #include "memalloc.h"
59 #include "mystring.h"
60 #include "alias.h"
61 #include "show.h"
62 #include "eval.h"
63 #ifndef NO_HISTORY
64 #include "myhistedit.h"
65 #endif
66 
67 /*
68  * Shell command parser.
69  */
70 
71 #define EOFMARKLEN 79
72 
73 /* values returned by readtoken */
74 #include "token.h"
75 
76 
77 
78 struct heredoc {
79 	struct heredoc *next;	/* next here document in list */
80 	union node *here;		/* redirection node */
81 	char *eofmark;		/* string indicating end of input */
82 	int striptabs;		/* if set, strip leading tabs */
83 };
84 
85 
86 
87 struct heredoc *heredoclist;	/* list of here documents to read */
88 int parsebackquote;		/* nonzero if we are inside backquotes */
89 int doprompt;			/* if set, prompt the user */
90 int needprompt;			/* true if interactive and at start of line */
91 int lasttoken;			/* last token read */
92 MKINIT int tokpushback;		/* last token pushed back */
93 char *wordtext;			/* text of last word returned by readtoken */
94 MKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
95 struct nodelist *backquotelist;
96 union node *redirnode;
97 struct heredoc *heredoc;
98 int quoteflag;			/* set if (part of) last token was quoted */
99 int startlinno;			/* line # where last token started */
100 
101 /* XXX When 'noaliases' is set to one, no alias expansion takes place. */
102 static int noaliases = 0;
103 
104 #define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
105 #ifdef GDB_HACK
106 static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'};
107 static const char types[] = "}-+?=";
108 #endif
109 
110 
111 STATIC union node *list(int);
112 STATIC union node *andor(void);
113 STATIC union node *pipeline(void);
114 STATIC union node *command(void);
115 STATIC union node *simplecmd(union node **, union node *);
116 STATIC union node *makename(void);
117 STATIC void parsefname(void);
118 STATIC void parseheredoc(void);
119 STATIC int peektoken(void);
120 STATIC int readtoken(void);
121 STATIC int xxreadtoken(void);
122 STATIC int readtoken1(int, char const *, char *, int);
123 STATIC int noexpand(char *);
124 STATIC void synexpect(int);
125 STATIC void synerror(char *);
126 STATIC void setprompt(int);
127 
128 
129 /*
130  * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
131  * valid parse tree indicating a blank line.)
132  */
133 
134 union node *
135 parsecmd(int interact)
136 {
137 	int t;
138 
139 	tokpushback = 0;
140 	doprompt = interact;
141 	if (doprompt)
142 		setprompt(1);
143 	else
144 		setprompt(0);
145 	needprompt = 0;
146 	t = readtoken();
147 	if (t == TEOF)
148 		return NEOF;
149 	if (t == TNL)
150 		return NULL;
151 	tokpushback++;
152 	return list(1);
153 }
154 
155 
156 STATIC union node *
157 list(int nlflag)
158 {
159 	union node *n1, *n2, *n3;
160 	int tok;
161 
162 	checkkwd = 2;
163 	if (nlflag == 0 && tokendlist[peektoken()])
164 		return NULL;
165 	n1 = NULL;
166 	for (;;) {
167 		n2 = andor();
168 		tok = readtoken();
169 		if (tok == TBACKGND) {
170 			if (n2->type == NCMD || n2->type == NPIPE) {
171 				n2->ncmd.backgnd = 1;
172 			} else if (n2->type == NREDIR) {
173 				n2->type = NBACKGND;
174 			} else {
175 				n3 = (union node *)stalloc(sizeof (struct nredir));
176 				n3->type = NBACKGND;
177 				n3->nredir.n = n2;
178 				n3->nredir.redirect = NULL;
179 				n2 = n3;
180 			}
181 		}
182 		if (n1 == NULL) {
183 			n1 = n2;
184 		}
185 		else {
186 			n3 = (union node *)stalloc(sizeof (struct nbinary));
187 			n3->type = NSEMI;
188 			n3->nbinary.ch1 = n1;
189 			n3->nbinary.ch2 = n2;
190 			n1 = n3;
191 		}
192 		switch (tok) {
193 		case TBACKGND:
194 		case TSEMI:
195 			tok = readtoken();
196 			/* fall through */
197 		case TNL:
198 			if (tok == TNL) {
199 				parseheredoc();
200 				if (nlflag)
201 					return n1;
202 			} else {
203 				tokpushback++;
204 			}
205 			checkkwd = 2;
206 			if (tokendlist[peektoken()])
207 				return n1;
208 			break;
209 		case TEOF:
210 			if (heredoclist)
211 				parseheredoc();
212 			else
213 				pungetc();		/* push back EOF on input */
214 			return n1;
215 		default:
216 			if (nlflag)
217 				synexpect(-1);
218 			tokpushback++;
219 			return n1;
220 		}
221 	}
222 }
223 
224 
225 
226 STATIC union node *
227 andor(void)
228 {
229 	union node *n1, *n2, *n3;
230 	int t;
231 
232 	n1 = pipeline();
233 	for (;;) {
234 		if ((t = readtoken()) == TAND) {
235 			t = NAND;
236 		} else if (t == TOR) {
237 			t = NOR;
238 		} else {
239 			tokpushback++;
240 			return n1;
241 		}
242 		n2 = pipeline();
243 		n3 = (union node *)stalloc(sizeof (struct nbinary));
244 		n3->type = t;
245 		n3->nbinary.ch1 = n1;
246 		n3->nbinary.ch2 = n2;
247 		n1 = n3;
248 	}
249 }
250 
251 
252 
253 STATIC union node *
254 pipeline(void)
255 {
256 	union node *n1, *n2, *pipenode;
257 	struct nodelist *lp, *prev;
258 	int negate;
259 
260 	negate = 0;
261 	TRACE(("pipeline: entered\n"));
262 	while (readtoken() == TNOT)
263 		negate = !negate;
264 	tokpushback++;
265 	n1 = command();
266 	if (readtoken() == TPIPE) {
267 		pipenode = (union node *)stalloc(sizeof (struct npipe));
268 		pipenode->type = NPIPE;
269 		pipenode->npipe.backgnd = 0;
270 		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
271 		pipenode->npipe.cmdlist = lp;
272 		lp->n = n1;
273 		do {
274 			prev = lp;
275 			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
276 			lp->n = command();
277 			prev->next = lp;
278 		} while (readtoken() == TPIPE);
279 		lp->next = NULL;
280 		n1 = pipenode;
281 	}
282 	tokpushback++;
283 	if (negate) {
284 		n2 = (union node *)stalloc(sizeof (struct nnot));
285 		n2->type = NNOT;
286 		n2->nnot.com = n1;
287 		return n2;
288 	} else
289 		return n1;
290 }
291 
292 
293 
294 STATIC union node *
295 command(void)
296 {
297 	union node *n1, *n2;
298 	union node *ap, **app;
299 	union node *cp, **cpp;
300 	union node *redir, **rpp;
301 	int t, negate = 0;
302 
303 	checkkwd = 2;
304 	redir = NULL;
305 	n1 = NULL;
306 	rpp = &redir;
307 
308 	/* Check for redirection which may precede command */
309 	while (readtoken() == TREDIR) {
310 		*rpp = n2 = redirnode;
311 		rpp = &n2->nfile.next;
312 		parsefname();
313 	}
314 	tokpushback++;
315 
316 	while (readtoken() == TNOT) {
317 		TRACE(("command: TNOT recognized\n"));
318 		negate = !negate;
319 	}
320 	tokpushback++;
321 
322 	switch (readtoken()) {
323 	case TIF:
324 		n1 = (union node *)stalloc(sizeof (struct nif));
325 		n1->type = NIF;
326 		n1->nif.test = list(0);
327 		if (readtoken() != TTHEN)
328 			synexpect(TTHEN);
329 		n1->nif.ifpart = list(0);
330 		n2 = n1;
331 		while (readtoken() == TELIF) {
332 			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
333 			n2 = n2->nif.elsepart;
334 			n2->type = NIF;
335 			n2->nif.test = list(0);
336 			if (readtoken() != TTHEN)
337 				synexpect(TTHEN);
338 			n2->nif.ifpart = list(0);
339 		}
340 		if (lasttoken == TELSE)
341 			n2->nif.elsepart = list(0);
342 		else {
343 			n2->nif.elsepart = NULL;
344 			tokpushback++;
345 		}
346 		if (readtoken() != TFI)
347 			synexpect(TFI);
348 		checkkwd = 1;
349 		break;
350 	case TWHILE:
351 	case TUNTIL: {
352 		int got;
353 		n1 = (union node *)stalloc(sizeof (struct nbinary));
354 		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
355 		n1->nbinary.ch1 = list(0);
356 		if ((got=readtoken()) != TDO) {
357 TRACE(("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 		do {
433 			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
434 			cp->type = NCLIST;
435 			app = &cp->nclist.pattern;
436 			for (;;) {
437 				*app = ap = (union node *)stalloc(sizeof (struct narg));
438 				ap->type = NARG;
439 				ap->narg.text = wordtext;
440 				ap->narg.backquote = backquotelist;
441 				if (checkkwd = 2, readtoken() != TPIPE)
442 					break;
443 				app = &ap->narg.next;
444 				readtoken();
445 			}
446 			ap->narg.next = NULL;
447 			if (lasttoken != TRP)
448 				noaliases = 0, synexpect(TRP);
449 			cp->nclist.body = list(0);
450 
451 			checkkwd = 2;
452 			if ((t = readtoken()) != TESAC) {
453 				if (t != TENDCASE)
454 					noaliases = 0, synexpect(TENDCASE);
455 				else
456 					checkkwd = 2, readtoken();
457 			}
458 			cpp = &cp->nclist.next;
459 		} while(lasttoken != TESAC);
460 		noaliases = 0;	/* reset alias expansion */
461 		*cpp = NULL;
462 		checkkwd = 1;
463 		break;
464 	case TLP:
465 		n1 = (union node *)stalloc(sizeof (struct nredir));
466 		n1->type = NSUBSHELL;
467 		n1->nredir.n = list(0);
468 		n1->nredir.redirect = NULL;
469 		if (readtoken() != TRP)
470 			synexpect(TRP);
471 		checkkwd = 1;
472 		break;
473 	case TBEGIN:
474 		n1 = list(0);
475 		if (readtoken() != TEND)
476 			synexpect(TEND);
477 		checkkwd = 1;
478 		break;
479 	/* Handle an empty command like other simple commands.  */
480 	case TSEMI:
481 		/*
482 		 * An empty command before a ; doesn't make much sense, and
483 		 * should certainly be disallowed in the case of `if ;'.
484 		 */
485 		if (!redir)
486 			synexpect(-1);
487 	case TAND:
488 	case TOR:
489 	case TNL:
490 	case TEOF:
491 	case TWORD:
492 	case TRP:
493 		tokpushback++;
494 		n1 = simplecmd(rpp, redir);
495 		goto checkneg;
496 	default:
497 		synexpect(-1);
498 	}
499 
500 	/* Now check for redirection which may follow command */
501 	while (readtoken() == TREDIR) {
502 		*rpp = n2 = redirnode;
503 		rpp = &n2->nfile.next;
504 		parsefname();
505 	}
506 	tokpushback++;
507 	*rpp = NULL;
508 	if (redir) {
509 		if (n1->type != NSUBSHELL) {
510 			n2 = (union node *)stalloc(sizeof (struct nredir));
511 			n2->type = NREDIR;
512 			n2->nredir.n = n1;
513 			n1 = n2;
514 		}
515 		n1->nredir.redirect = redir;
516 	}
517 
518 checkneg:
519 	if (negate) {
520 		n2 = (union node *)stalloc(sizeof (struct nnot));
521 		n2->type = NNOT;
522 		n2->nnot.com = n1;
523 		return n2;
524 	}
525 	else
526 		return n1;
527 }
528 
529 
530 STATIC union node *
531 simplecmd(union node **rpp, union node *redir)
532 {
533 	union node *args, **app;
534 	union node **orig_rpp = rpp;
535 	union node *n = NULL, *n2;
536 	int negate = 0;
537 
538 	/* If we don't have any redirections already, then we must reset */
539 	/* rpp to be the address of the local redir variable.  */
540 	if (redir == 0)
541 		rpp = &redir;
542 
543 	args = NULL;
544 	app = &args;
545 	/*
546 	 * We save the incoming value, because we need this for shell
547 	 * functions.  There can not be a redirect or an argument between
548 	 * the function name and the open parenthesis.
549 	 */
550 	orig_rpp = rpp;
551 
552 	while (readtoken() == TNOT) {
553 		TRACE(("command: TNOT recognized\n"));
554 		negate = !negate;
555 	}
556 	tokpushback++;
557 
558 	for (;;) {
559 		if (readtoken() == TWORD) {
560 			n = (union node *)stalloc(sizeof (struct narg));
561 			n->type = NARG;
562 			n->narg.text = wordtext;
563 			n->narg.backquote = backquotelist;
564 			*app = n;
565 			app = &n->narg.next;
566 		} else if (lasttoken == TREDIR) {
567 			*rpp = n = redirnode;
568 			rpp = &n->nfile.next;
569 			parsefname();	/* read name of redirection file */
570 		} else if (lasttoken == TLP && app == &args->narg.next
571 					    && rpp == orig_rpp) {
572 			/* We have a function */
573 			if (readtoken() != TRP)
574 				synexpect(TRP);
575 #ifdef notdef
576 			if (! goodname(n->narg.text))
577 				synerror("Bad function name");
578 #endif
579 			n->type = NDEFUN;
580 			n->narg.next = command();
581 			goto checkneg;
582 		} else {
583 			tokpushback++;
584 			break;
585 		}
586 	}
587 	*app = NULL;
588 	*rpp = NULL;
589 	n = (union node *)stalloc(sizeof (struct ncmd));
590 	n->type = NCMD;
591 	n->ncmd.backgnd = 0;
592 	n->ncmd.args = args;
593 	n->ncmd.redirect = redir;
594 
595 checkneg:
596 	if (negate) {
597 		n2 = (union node *)stalloc(sizeof (struct nnot));
598 		n2->type = NNOT;
599 		n2->nnot.com = n;
600 		return n2;
601 	}
602 	else
603 		return n;
604 }
605 
606 STATIC union node *
607 makename(void)
608 {
609 	union node *n;
610 
611 	n = (union node *)stalloc(sizeof (struct narg));
612 	n->type = NARG;
613 	n->narg.next = NULL;
614 	n->narg.text = wordtext;
615 	n->narg.backquote = backquotelist;
616 	return n;
617 }
618 
619 void fixredir(union node *n, const char *text, int err)
620 {
621 	TRACE(("Fix redir %s %d\n", text, err));
622 	if (!err)
623 		n->ndup.vname = NULL;
624 
625 	if (is_digit(text[0]) && text[1] == '\0')
626 		n->ndup.dupfd = digit_val(text[0]);
627 	else if (text[0] == '-' && text[1] == '\0')
628 		n->ndup.dupfd = -1;
629 	else {
630 
631 		if (err)
632 			synerror("Bad fd number");
633 		else
634 			n->ndup.vname = makename();
635 	}
636 }
637 
638 
639 STATIC void
640 parsefname(void)
641 {
642 	union node *n = redirnode;
643 
644 	if (readtoken() != TWORD)
645 		synexpect(-1);
646 	if (n->type == NHERE) {
647 		struct heredoc *here = heredoc;
648 		struct heredoc *p;
649 		int i;
650 
651 		if (quoteflag == 0)
652 			n->type = NXHERE;
653 		TRACE(("Here document %d\n", n->type));
654 		if (here->striptabs) {
655 			while (*wordtext == '\t')
656 				wordtext++;
657 		}
658 		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
659 			synerror("Illegal eof marker for << redirection");
660 		rmescapes(wordtext);
661 		here->eofmark = wordtext;
662 		here->next = NULL;
663 		if (heredoclist == NULL)
664 			heredoclist = here;
665 		else {
666 			for (p = heredoclist ; p->next ; p = p->next);
667 			p->next = here;
668 		}
669 	} else if (n->type == NTOFD || n->type == NFROMFD) {
670 		fixredir(n, wordtext, 0);
671 	} else {
672 		n->nfile.fname = makename();
673 	}
674 }
675 
676 
677 /*
678  * Input any here documents.
679  */
680 
681 STATIC void
682 parseheredoc(void)
683 {
684 	struct heredoc *here;
685 	union node *n;
686 
687 	while (heredoclist) {
688 		here = heredoclist;
689 		heredoclist = here->next;
690 		if (needprompt) {
691 			setprompt(2);
692 			needprompt = 0;
693 		}
694 		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
695 				here->eofmark, here->striptabs);
696 		n = (union node *)stalloc(sizeof (struct narg));
697 		n->narg.type = NARG;
698 		n->narg.next = NULL;
699 		n->narg.text = wordtext;
700 		n->narg.backquote = backquotelist;
701 		here->here->nhere.doc = n;
702 	}
703 }
704 
705 STATIC int
706 peektoken(void)
707 {
708 	int t;
709 
710 	t = readtoken();
711 	tokpushback++;
712 	return (t);
713 }
714 
715 STATIC int
716 readtoken(void)
717 {
718 	int t;
719 	int savecheckkwd = checkkwd;
720 	struct alias *ap;
721 #ifdef DEBUG
722 	int alreadyseen = tokpushback;
723 #endif
724 
725 	top:
726 	t = xxreadtoken();
727 
728 	if (checkkwd) {
729 		/*
730 		 * eat newlines
731 		 */
732 		if (checkkwd == 2) {
733 			checkkwd = 0;
734 			while (t == TNL) {
735 				parseheredoc();
736 				t = xxreadtoken();
737 			}
738 		} else
739 			checkkwd = 0;
740 		/*
741 		 * check for keywords and aliases
742 		 */
743 		if (t == TWORD && !quoteflag)
744 		{
745 			char * const *pp;
746 
747 			for (pp = (char **)parsekwd; *pp; pp++) {
748 				if (**pp == *wordtext && equal(*pp, wordtext))
749 				{
750 					lasttoken = t = pp - parsekwd + KWDOFFSET;
751 					TRACE(("keyword %s recognized\n", tokname[t]));
752 					goto out;
753 				}
754 			}
755 			if (noaliases == 0 &&
756 			    (ap = lookupalias(wordtext, 1)) != NULL) {
757 				pushstring(ap->val, strlen(ap->val), ap);
758 				checkkwd = savecheckkwd;
759 				goto top;
760 			}
761 		}
762 out:
763 		checkkwd = (t == TNOT) ? savecheckkwd : 0;
764 	}
765 #ifdef DEBUG
766 	if (!alreadyseen)
767 	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
768 	else
769 	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
770 #endif
771 	return (t);
772 }
773 
774 
775 /*
776  * Read the next input token.
777  * If the token is a word, we set backquotelist to the list of cmds in
778  *	backquotes.  We set quoteflag to true if any part of the word was
779  *	quoted.
780  * If the token is TREDIR, then we set redirnode to a structure containing
781  *	the redirection.
782  * In all cases, the variable startlinno is set to the number of the line
783  *	on which the token starts.
784  *
785  * [Change comment:  here documents and internal procedures]
786  * [Readtoken shouldn't have any arguments.  Perhaps we should make the
787  *  word parsing code into a separate routine.  In this case, readtoken
788  *  doesn't need to have any internal procedures, but parseword does.
789  *  We could also make parseoperator in essence the main routine, and
790  *  have parseword (readtoken1?) handle both words and redirection.]
791  */
792 
793 #define RETURN(token)	return lasttoken = token
794 
795 STATIC int
796 xxreadtoken(void)
797 {
798 	int c;
799 
800 	if (tokpushback) {
801 		tokpushback = 0;
802 		return lasttoken;
803 	}
804 	if (needprompt) {
805 		setprompt(2);
806 		needprompt = 0;
807 	}
808 	startlinno = plinno;
809 	for (;;) {	/* until token or start of word found */
810 		c = pgetc_macro();
811 		if (c == ' ' || c == '\t')
812 			continue;		/* quick check for white space first */
813 		switch (c) {
814 		case ' ': case '\t':
815 			continue;
816 		case '#':
817 			while ((c = pgetc()) != '\n' && c != PEOF);
818 			pungetc();
819 			continue;
820 		case '\\':
821 			if (pgetc() == '\n') {
822 				startlinno = ++plinno;
823 				if (doprompt)
824 					setprompt(2);
825 				else
826 					setprompt(0);
827 				continue;
828 			}
829 			pungetc();
830 			goto breakloop;
831 		case '\n':
832 			plinno++;
833 			needprompt = doprompt;
834 			RETURN(TNL);
835 		case PEOF:
836 			RETURN(TEOF);
837 		case '&':
838 			if (pgetc() == '&')
839 				RETURN(TAND);
840 			pungetc();
841 			RETURN(TBACKGND);
842 		case '|':
843 			if (pgetc() == '|')
844 				RETURN(TOR);
845 			pungetc();
846 			RETURN(TPIPE);
847 		case ';':
848 			if (pgetc() == ';')
849 				RETURN(TENDCASE);
850 			pungetc();
851 			RETURN(TSEMI);
852 		case '(':
853 			RETURN(TLP);
854 		case ')':
855 			RETURN(TRP);
856 		default:
857 			goto breakloop;
858 		}
859 	}
860 breakloop:
861 	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
862 #undef RETURN
863 }
864 
865 
866 
867 /*
868  * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
869  * is not NULL, read a here document.  In the latter case, eofmark is the
870  * word which marks the end of the document and striptabs is true if
871  * leading tabs should be stripped from the document.  The argument firstc
872  * is the first character of the input token or document.
873  *
874  * Because C does not have internal subroutines, I have simulated them
875  * using goto's to implement the subroutine linkage.  The following macros
876  * will run code that appears at the end of readtoken1.
877  */
878 
879 #define CHECKEND()	{goto checkend; checkend_return:;}
880 #define PARSEREDIR()	{goto parseredir; parseredir_return:;}
881 #define PARSESUB()	{goto parsesub; parsesub_return:;}
882 #define PARSEBACKQOLD()	{oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
883 #define PARSEBACKQNEW()	{oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
884 #define	PARSEARITH()	{goto parsearith; parsearith_return:;}
885 
886 STATIC int
887 readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
888 {
889 	int c = firstc;
890 	char *out;
891 	int len;
892 	char line[EOFMARKLEN + 1];
893 	struct nodelist *bqlist;
894 	int quotef;
895 	int dblquote;
896 	int varnest;	/* levels of variables expansion */
897 	int arinest;	/* levels of arithmetic expansion */
898 	int parenlevel;	/* levels of parens in arithmetic */
899 	int oldstyle;
900 	char const *prevsyntax;	/* syntax before arithmetic */
901 	int synentry;
902 #if __GNUC__
903 	/* Avoid longjmp clobbering */
904 	(void) &out;
905 	(void) &quotef;
906 	(void) &dblquote;
907 	(void) &varnest;
908 	(void) &arinest;
909 	(void) &parenlevel;
910 	(void) &oldstyle;
911 	(void) &prevsyntax;
912 	(void) &syntax;
913 	(void) &synentry;
914 #endif
915 
916 	startlinno = plinno;
917 	dblquote = 0;
918 	if (syntax == DQSYNTAX)
919 		dblquote = 1;
920 	quotef = 0;
921 	bqlist = NULL;
922 	varnest = 0;
923 	arinest = 0;
924 	parenlevel = 0;
925 
926 	STARTSTACKSTR(out);
927 	loop: {	/* for each line, until end of word */
928 #if ATTY
929 		if (c == '\034' && doprompt
930 		 && attyset() && ! equal(termval(), "emacs")) {
931 			attyline();
932 			if (syntax == BASESYNTAX)
933 				return readtoken();
934 			c = pgetc();
935 			goto loop;
936 		}
937 #endif
938 		CHECKEND();	/* set c to PEOF if at end of here document */
939 		for (;;) {	/* until end of line or end of word */
940 			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
941 
942 			synentry = syntax[c];
943 
944 			switch(synentry) {
945 			case CNL:	/* '\n' */
946 				if (syntax == BASESYNTAX)
947 					goto endword;	/* exit outer loop */
948 				USTPUTC(c, out);
949 				plinno++;
950 				if (doprompt)
951 					setprompt(2);
952 				else
953 					setprompt(0);
954 				c = pgetc();
955 				goto loop;		/* continue outer loop */
956 			case CWORD:
957 				USTPUTC(c, out);
958 				break;
959 			case CCTL:
960 				if (eofmark == NULL || dblquote)
961 					USTPUTC(CTLESC, out);
962 				USTPUTC(c, out);
963 				break;
964 			case CBACK:	/* backslash */
965 				c = pgetc();
966 				if (c == PEOF) {
967 					USTPUTC('\\', out);
968 					pungetc();
969 				} else if (c == '\n') {
970 					if (doprompt)
971 						setprompt(2);
972 					else
973 						setprompt(0);
974 				} else {
975 					if (dblquote && c != '\\' &&
976 					    c != '`' && c != '$' &&
977 					    (c != '"' || eofmark != NULL))
978 						USTPUTC('\\', out);
979 					if (SQSYNTAX[c] == CCTL)
980 						USTPUTC(CTLESC, out);
981 					else if (eofmark == NULL)
982 						USTPUTC(CTLQUOTEMARK, out);
983 					USTPUTC(c, out);
984 					quotef++;
985 				}
986 				break;
987 			case CSQUOTE:
988 				if (eofmark == NULL)
989 					USTPUTC(CTLQUOTEMARK, out);
990 				syntax = SQSYNTAX;
991 				break;
992 			case CDQUOTE:
993 				if (eofmark == NULL)
994 					USTPUTC(CTLQUOTEMARK, out);
995 				syntax = DQSYNTAX;
996 				dblquote = 1;
997 				break;
998 			case CENDQUOTE:
999 				if (eofmark != NULL && arinest == 0 &&
1000 				    varnest == 0) {
1001 					USTPUTC(c, out);
1002 				} else {
1003 					if (arinest) {
1004 						syntax = ARISYNTAX;
1005 						dblquote = 0;
1006 					} else if (eofmark == NULL) {
1007 						syntax = BASESYNTAX;
1008 						dblquote = 0;
1009 					}
1010 					quotef++;
1011 				}
1012 				break;
1013 			case CVAR:	/* '$' */
1014 				PARSESUB();		/* parse substitution */
1015 				break;
1016 			case CENDVAR:	/* '}' */
1017 				if (varnest > 0) {
1018 					varnest--;
1019 					USTPUTC(CTLENDVAR, out);
1020 				} else {
1021 					USTPUTC(c, out);
1022 				}
1023 				break;
1024 			case CLP:	/* '(' in arithmetic */
1025 				parenlevel++;
1026 				USTPUTC(c, out);
1027 				break;
1028 			case CRP:	/* ')' in arithmetic */
1029 				if (parenlevel > 0) {
1030 					USTPUTC(c, out);
1031 					--parenlevel;
1032 				} else {
1033 					if (pgetc() == ')') {
1034 						if (--arinest == 0) {
1035 							USTPUTC(CTLENDARI, out);
1036 							syntax = prevsyntax;
1037 							if (syntax == DQSYNTAX)
1038 								dblquote = 1;
1039 							else
1040 								dblquote = 0;
1041 						} else
1042 							USTPUTC(')', out);
1043 					} else {
1044 						/*
1045 						 * unbalanced parens
1046 						 *  (don't 2nd guess - no error)
1047 						 */
1048 						pungetc();
1049 						USTPUTC(')', out);
1050 					}
1051 				}
1052 				break;
1053 			case CBQUOTE:	/* '`' */
1054 				PARSEBACKQOLD();
1055 				break;
1056 			case CEOF:
1057 				goto endword;		/* exit outer loop */
1058 			default:
1059 				if (varnest == 0)
1060 					goto endword;	/* exit outer loop */
1061 				USTPUTC(c, out);
1062 			}
1063 			c = pgetc_macro();
1064 		}
1065 	}
1066 endword:
1067 	if (syntax == ARISYNTAX)
1068 		synerror("Missing '))'");
1069 	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1070 		synerror("Unterminated quoted string");
1071 	if (varnest != 0) {
1072 		startlinno = plinno;
1073 		synerror("Missing '}'");
1074 	}
1075 	USTPUTC('\0', out);
1076 	len = out - stackblock();
1077 	out = stackblock();
1078 	if (eofmark == NULL) {
1079 		if ((c == '>' || c == '<')
1080 		 && quotef == 0
1081 		 && len <= 2
1082 		 && (*out == '\0' || is_digit(*out))) {
1083 			PARSEREDIR();
1084 			return lasttoken = TREDIR;
1085 		} else {
1086 			pungetc();
1087 		}
1088 	}
1089 	quoteflag = quotef;
1090 	backquotelist = bqlist;
1091 	grabstackblock(len);
1092 	wordtext = out;
1093 	return lasttoken = TWORD;
1094 /* end of readtoken routine */
1095 
1096 
1097 
1098 /*
1099  * Check to see whether we are at the end of the here document.  When this
1100  * is called, c is set to the first character of the next input line.  If
1101  * we are at the end of the here document, this routine sets the c to PEOF.
1102  */
1103 
1104 checkend: {
1105 	if (eofmark) {
1106 		if (striptabs) {
1107 			while (c == '\t')
1108 				c = pgetc();
1109 		}
1110 		if (c == *eofmark) {
1111 			if (pfgets(line, sizeof line) != NULL) {
1112 				char *p, *q;
1113 
1114 				p = line;
1115 				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1116 				if (*p == '\n' && *q == '\0') {
1117 					c = PEOF;
1118 					plinno++;
1119 					needprompt = doprompt;
1120 				} else {
1121 					pushstring(line, strlen(line), NULL);
1122 				}
1123 			}
1124 		}
1125 	}
1126 	goto checkend_return;
1127 }
1128 
1129 
1130 /*
1131  * Parse a redirection operator.  The variable "out" points to a string
1132  * specifying the fd to be redirected.  The variable "c" contains the
1133  * first character of the redirection operator.
1134  */
1135 
1136 parseredir: {
1137 	char fd = *out;
1138 	union node *np;
1139 
1140 	np = (union node *)stalloc(sizeof (struct nfile));
1141 	if (c == '>') {
1142 		np->nfile.fd = 1;
1143 		c = pgetc();
1144 		if (c == '>')
1145 			np->type = NAPPEND;
1146 		else if (c == '&')
1147 			np->type = NTOFD;
1148 		else {
1149 			np->type = NTO;
1150 			pungetc();
1151 		}
1152 	} else {	/* c == '<' */
1153 		np->nfile.fd = 0;
1154 		c = pgetc();
1155 		if (c == '<') {
1156 			if (sizeof (struct nfile) != sizeof (struct nhere)) {
1157 				np = (union node *)stalloc(sizeof (struct nhere));
1158 				np->nfile.fd = 0;
1159 			}
1160 			np->type = NHERE;
1161 			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1162 			heredoc->here = np;
1163 			if ((c = pgetc()) == '-') {
1164 				heredoc->striptabs = 1;
1165 			} else {
1166 				heredoc->striptabs = 0;
1167 				pungetc();
1168 			}
1169 		} else if (c == '&')
1170 			np->type = NFROMFD;
1171 		else if (c == '>')
1172 			np->type = NFROMTO;
1173 		else {
1174 			np->type = NFROM;
1175 			pungetc();
1176 		}
1177 	}
1178 	if (fd != '\0')
1179 		np->nfile.fd = digit_val(fd);
1180 	redirnode = np;
1181 	goto parseredir_return;
1182 }
1183 
1184 
1185 /*
1186  * Parse a substitution.  At this point, we have read the dollar sign
1187  * and nothing else.
1188  */
1189 
1190 parsesub: {
1191 	int subtype;
1192 	int typeloc;
1193 	int flags;
1194 	char *p;
1195 #ifndef GDB_HACK
1196 	static const char types[] = "}-+?=";
1197 #endif
1198        int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1199 
1200 	c = pgetc();
1201 	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
1202 		USTPUTC('$', out);
1203 		pungetc();
1204 	} else if (c == '(') {	/* $(command) or $((arith)) */
1205 		if (pgetc() == '(') {
1206 			PARSEARITH();
1207 		} else {
1208 			pungetc();
1209 			PARSEBACKQNEW();
1210 		}
1211 	} else {
1212 		USTPUTC(CTLVAR, out);
1213 		typeloc = out - stackblock();
1214 		USTPUTC(VSNORMAL, out);
1215 		subtype = VSNORMAL;
1216 		if (c == '{') {
1217 			bracketed_name = 1;
1218 			c = pgetc();
1219 			if (c == '#') {
1220 				if ((c = pgetc()) == '}')
1221 					c = '#';
1222 				else
1223 					subtype = VSLENGTH;
1224 			}
1225 			else
1226 				subtype = 0;
1227 		}
1228 		if (is_name(c)) {
1229 			do {
1230 				STPUTC(c, out);
1231 				c = pgetc();
1232 			} while (is_in_name(c));
1233 		} else if (is_digit(c)) {
1234 			if (bracketed_name) {
1235 				do {
1236 					STPUTC(c, out);
1237 					c = pgetc();
1238 				} while (is_digit(c));
1239 			} else {
1240 				STPUTC(c, out);
1241 				c = pgetc();
1242 			}
1243 		} else {
1244 			if (! is_special(c))
1245 badsub:				synerror("Bad substitution");
1246 			USTPUTC(c, out);
1247 			c = pgetc();
1248 		}
1249 		STPUTC('=', out);
1250 		flags = 0;
1251 		if (subtype == 0) {
1252 			switch (c) {
1253 			case ':':
1254 				flags = VSNUL;
1255 				c = pgetc();
1256 				/*FALLTHROUGH*/
1257 			default:
1258 				p = strchr(types, c);
1259 				if (p == NULL)
1260 					goto badsub;
1261 				subtype = p - types + VSNORMAL;
1262 				break;
1263 			case '%':
1264 			case '#':
1265 				{
1266 					int cc = c;
1267 					subtype = c == '#' ? VSTRIMLEFT :
1268 							     VSTRIMRIGHT;
1269 					c = pgetc();
1270 					if (c == cc)
1271 						subtype++;
1272 					else
1273 						pungetc();
1274 					break;
1275 				}
1276 			}
1277 		} else {
1278 			pungetc();
1279 		}
1280 		if (subtype != VSLENGTH && (dblquote || arinest))
1281 			flags |= VSQUOTE;
1282 		*(stackblock() + typeloc) = subtype | flags;
1283 		if (subtype != VSNORMAL)
1284 			varnest++;
1285 	}
1286 	goto parsesub_return;
1287 }
1288 
1289 
1290 /*
1291  * Called to parse command substitutions.  Newstyle is set if the command
1292  * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1293  * list of commands (passed by reference), and savelen is the number of
1294  * characters on the top of the stack which must be preserved.
1295  */
1296 
1297 parsebackq: {
1298 	struct nodelist **nlpp;
1299 	int savepbq;
1300 	union node *n;
1301 	char *volatile str;
1302 	struct jmploc jmploc;
1303 	struct jmploc *volatile savehandler;
1304 	int savelen;
1305 	int saveprompt;
1306 #if __GNUC__
1307 	/* Avoid longjmp clobbering */
1308 	(void) &saveprompt;
1309 #endif
1310 
1311 	savepbq = parsebackquote;
1312 	if (setjmp(jmploc.loc)) {
1313 		if (str)
1314 			ckfree(str);
1315 		parsebackquote = 0;
1316 		handler = savehandler;
1317 		longjmp(handler->loc, 1);
1318 	}
1319 	INTOFF;
1320 	str = NULL;
1321 	savelen = out - stackblock();
1322 	if (savelen > 0) {
1323 		str = ckmalloc(savelen);
1324 		memcpy(str, stackblock(), savelen);
1325 	}
1326 	savehandler = handler;
1327 	handler = &jmploc;
1328 	INTON;
1329         if (oldstyle) {
1330                 /* We must read until the closing backquote, giving special
1331                    treatment to some slashes, and then push the string and
1332                    reread it as input, interpreting it normally.  */
1333                 char *out;
1334                 int c;
1335                 int savelen;
1336                 char *str;
1337 
1338 
1339                 STARTSTACKSTR(out);
1340 		for (;;) {
1341 			if (needprompt) {
1342 				setprompt(2);
1343 				needprompt = 0;
1344 			}
1345 			switch (c = pgetc()) {
1346 			case '`':
1347 				goto done;
1348 
1349 			case '\\':
1350                                 if ((c = pgetc()) == '\n') {
1351 					plinno++;
1352 					if (doprompt)
1353 						setprompt(2);
1354 					else
1355 						setprompt(0);
1356 					/*
1357 					 * If eating a newline, avoid putting
1358 					 * the newline into the new character
1359 					 * stream (via the STPUTC after the
1360 					 * switch).
1361 					 */
1362 					continue;
1363 				}
1364                                 if (c != '\\' && c != '`' && c != '$'
1365                                     && (!dblquote || c != '"'))
1366                                         STPUTC('\\', out);
1367 				break;
1368 
1369 			case '\n':
1370 				plinno++;
1371 				needprompt = doprompt;
1372 				break;
1373 
1374 			case PEOF:
1375 			        startlinno = plinno;
1376 				synerror("EOF in backquote substitution");
1377  				break;
1378 
1379 			default:
1380 				break;
1381 			}
1382 			STPUTC(c, out);
1383                 }
1384 done:
1385                 STPUTC('\0', out);
1386                 savelen = out - stackblock();
1387                 if (savelen > 0) {
1388                         str = ckmalloc(savelen);
1389                         memcpy(str, stackblock(), savelen);
1390 			setinputstring(str, 1);
1391                 }
1392         }
1393 	nlpp = &bqlist;
1394 	while (*nlpp)
1395 		nlpp = &(*nlpp)->next;
1396 	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1397 	(*nlpp)->next = NULL;
1398 	parsebackquote = oldstyle;
1399 
1400 	if (oldstyle) {
1401 		saveprompt = doprompt;
1402 		doprompt = 0;
1403 	}
1404 
1405 	n = list(0);
1406 
1407 	if (oldstyle)
1408 		doprompt = saveprompt;
1409 	else {
1410 		if (readtoken() != TRP)
1411 			synexpect(TRP);
1412 	}
1413 
1414 	(*nlpp)->n = n;
1415         if (oldstyle) {
1416 		/*
1417 		 * Start reading from old file again, ignoring any pushed back
1418 		 * tokens left from the backquote parsing
1419 		 */
1420                 popfile();
1421 		tokpushback = 0;
1422 	}
1423 	while (stackblocksize() <= savelen)
1424 		growstackblock();
1425 	STARTSTACKSTR(out);
1426 	if (str) {
1427 		memcpy(out, str, savelen);
1428 		STADJUST(savelen, out);
1429 		INTOFF;
1430 		ckfree(str);
1431 		str = NULL;
1432 		INTON;
1433 	}
1434 	parsebackquote = savepbq;
1435 	handler = savehandler;
1436 	if (arinest || dblquote)
1437 		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1438 	else
1439 		USTPUTC(CTLBACKQ, out);
1440 	if (oldstyle)
1441 		goto parsebackq_oldreturn;
1442 	else
1443 		goto parsebackq_newreturn;
1444 }
1445 
1446 /*
1447  * Parse an arithmetic expansion (indicate start of one and set state)
1448  */
1449 parsearith: {
1450 
1451 	if (++arinest == 1) {
1452 		prevsyntax = syntax;
1453 		syntax = ARISYNTAX;
1454 		USTPUTC(CTLARI, out);
1455 		if (dblquote)
1456 			USTPUTC('"',out);
1457 		else
1458 			USTPUTC(' ',out);
1459 	} else {
1460 		/*
1461 		 * we collapse embedded arithmetic expansion to
1462 		 * parenthesis, which should be equivalent
1463 		 */
1464 		USTPUTC('(', out);
1465 	}
1466 	goto parsearith_return;
1467 }
1468 
1469 } /* end of readtoken */
1470 
1471 
1472 
1473 #ifdef mkinit
1474 RESET {
1475 	tokpushback = 0;
1476 	checkkwd = 0;
1477 }
1478 #endif
1479 
1480 /*
1481  * Returns true if the text contains nothing to expand (no dollar signs
1482  * or backquotes).
1483  */
1484 
1485 STATIC int
1486 noexpand(char *text)
1487 {
1488 	char *p;
1489 	char c;
1490 
1491 	p = text;
1492 	while ((c = *p++) != '\0') {
1493 		if ( c == CTLQUOTEMARK)
1494 			continue;
1495 		if (c == CTLESC)
1496 			p++;
1497 		else if (BASESYNTAX[(int)c] == CCTL)
1498 			return 0;
1499 	}
1500 	return 1;
1501 }
1502 
1503 
1504 /*
1505  * Return true if the argument is a legal variable name (a letter or
1506  * underscore followed by zero or more letters, underscores, and digits).
1507  */
1508 
1509 int
1510 goodname(char *name)
1511 {
1512 	char *p;
1513 
1514 	p = name;
1515 	if (! is_name(*p))
1516 		return 0;
1517 	while (*++p) {
1518 		if (! is_in_name(*p))
1519 			return 0;
1520 	}
1521 	return 1;
1522 }
1523 
1524 
1525 /*
1526  * Called when an unexpected token is read during the parse.  The argument
1527  * is the token that is expected, or -1 if more than one type of token can
1528  * occur at this point.
1529  */
1530 
1531 STATIC void
1532 synexpect(int token)
1533 {
1534 	char msg[64];
1535 
1536 	if (token >= 0) {
1537 		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1538 			tokname[lasttoken], tokname[token]);
1539 	} else {
1540 		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1541 	}
1542 	synerror(msg);
1543 }
1544 
1545 
1546 STATIC void
1547 synerror(char *msg)
1548 {
1549 	if (commandname)
1550 		outfmt(&errout, "%s: %d: ", commandname, startlinno);
1551 	outfmt(&errout, "Syntax error: %s\n", msg);
1552 	error((char *)NULL);
1553 }
1554 
1555 STATIC void
1556 setprompt(int which)
1557 {
1558 	whichprompt = which;
1559 
1560 #ifndef NO_HISTORY
1561 	if (!el)
1562 #endif
1563 		out2str(getprompt(NULL));
1564 }
1565 
1566 /*
1567  * called by editline -- any expansions to the prompt
1568  *    should be added here.
1569  */
1570 char *
1571 getprompt(void *unused __unused)
1572 {
1573 	switch (whichprompt) {
1574 	case 0:
1575 		return "";
1576 	case 1:
1577 		return ps1val();
1578 	case 2:
1579 		return ps2val();
1580 	default:
1581 		return "<internal prompt error>";
1582 	}
1583 }
1584