xref: /freebsd/bin/sh/parser.c (revision ce4946daa5ce852d28008dac492029500ab2ee95)
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 __P((int));
112 STATIC union node *andor __P((void));
113 STATIC union node *pipeline __P((void));
114 STATIC union node *command __P((void));
115 STATIC union node *simplecmd __P((union node **, union node *));
116 STATIC union node *makename __P((void));
117 STATIC void parsefname __P((void));
118 STATIC void parseheredoc __P((void));
119 STATIC int peektoken __P((void));
120 STATIC int readtoken __P((void));
121 STATIC int xxreadtoken __P((void));
122 STATIC int readtoken1 __P((int, char const *, char *, int));
123 STATIC int noexpand __P((char *));
124 STATIC void synexpect __P((int));
125 STATIC void synerror __P((char *));
126 STATIC void setprompt __P((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(interact)
136 	int interact;
137 {
138 	int t;
139 
140 	tokpushback = 0;
141 	doprompt = interact;
142 	if (doprompt)
143 		setprompt(1);
144 	else
145 		setprompt(0);
146 	needprompt = 0;
147 	t = readtoken();
148 	if (t == TEOF)
149 		return NEOF;
150 	if (t == TNL)
151 		return NULL;
152 	tokpushback++;
153 	return list(1);
154 }
155 
156 
157 STATIC union node *
158 list(nlflag)
159 	int nlflag;
160 {
161 	union node *n1, *n2, *n3;
162 	int tok;
163 
164 	checkkwd = 2;
165 	if (nlflag == 0 && tokendlist[peektoken()])
166 		return NULL;
167 	n1 = NULL;
168 	for (;;) {
169 		n2 = andor();
170 		tok = readtoken();
171 		if (tok == TBACKGND) {
172 			if (n2->type == NCMD || n2->type == NPIPE) {
173 				n2->ncmd.backgnd = 1;
174 			} else if (n2->type == NREDIR) {
175 				n2->type = NBACKGND;
176 			} else {
177 				n3 = (union node *)stalloc(sizeof (struct nredir));
178 				n3->type = NBACKGND;
179 				n3->nredir.n = n2;
180 				n3->nredir.redirect = NULL;
181 				n2 = n3;
182 			}
183 		}
184 		if (n1 == NULL) {
185 			n1 = n2;
186 		}
187 		else {
188 			n3 = (union node *)stalloc(sizeof (struct nbinary));
189 			n3->type = NSEMI;
190 			n3->nbinary.ch1 = n1;
191 			n3->nbinary.ch2 = n2;
192 			n1 = n3;
193 		}
194 		switch (tok) {
195 		case TBACKGND:
196 		case TSEMI:
197 			tok = readtoken();
198 			/* fall through */
199 		case TNL:
200 			if (tok == TNL) {
201 				parseheredoc();
202 				if (nlflag)
203 					return n1;
204 			} else {
205 				tokpushback++;
206 			}
207 			checkkwd = 2;
208 			if (tokendlist[peektoken()])
209 				return n1;
210 			break;
211 		case TEOF:
212 			if (heredoclist)
213 				parseheredoc();
214 			else
215 				pungetc();		/* push back EOF on input */
216 			return n1;
217 		default:
218 			if (nlflag)
219 				synexpect(-1);
220 			tokpushback++;
221 			return n1;
222 		}
223 	}
224 }
225 
226 
227 
228 STATIC union node *
229 andor() {
230 	union node *n1, *n2, *n3;
231 	int t;
232 
233 	n1 = pipeline();
234 	for (;;) {
235 		if ((t = readtoken()) == TAND) {
236 			t = NAND;
237 		} else if (t == TOR) {
238 			t = NOR;
239 		} else {
240 			tokpushback++;
241 			return n1;
242 		}
243 		n2 = pipeline();
244 		n3 = (union node *)stalloc(sizeof (struct nbinary));
245 		n3->type = t;
246 		n3->nbinary.ch1 = n1;
247 		n3->nbinary.ch2 = n2;
248 		n1 = n3;
249 	}
250 }
251 
252 
253 
254 STATIC union node *
255 pipeline() {
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() {
296 	union node *n1, *n2;
297 	union node *ap, **app;
298 	union node *cp, **cpp;
299 	union node *redir, **rpp;
300 	int t, negate = 0;
301 
302 	checkkwd = 2;
303 	redir = NULL;
304 	n1 = NULL;
305 	rpp = &redir;
306 
307 	/* Check for redirection which may precede command */
308 	while (readtoken() == TREDIR) {
309 		*rpp = n2 = redirnode;
310 		rpp = &n2->nfile.next;
311 		parsefname();
312 	}
313 	tokpushback++;
314 
315 	while (readtoken() == TNOT) {
316 		TRACE(("command: TNOT recognized\n"));
317 		negate = !negate;
318 	}
319 	tokpushback++;
320 
321 	switch (readtoken()) {
322 	case TIF:
323 		n1 = (union node *)stalloc(sizeof (struct nif));
324 		n1->type = NIF;
325 		n1->nif.test = list(0);
326 		if (readtoken() != TTHEN)
327 			synexpect(TTHEN);
328 		n1->nif.ifpart = list(0);
329 		n2 = n1;
330 		while (readtoken() == TELIF) {
331 			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
332 			n2 = n2->nif.elsepart;
333 			n2->type = NIF;
334 			n2->nif.test = list(0);
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 		n1->nbinary.ch1 = list(0);
355 		if ((got=readtoken()) != TDO) {
356 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
357 			synexpect(TDO);
358 		}
359 		n1->nbinary.ch2 = list(0);
360 		if (readtoken() != TDONE)
361 			synexpect(TDONE);
362 		checkkwd = 1;
363 		break;
364 	}
365 	case TFOR:
366 		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
367 			synerror("Bad for loop variable");
368 		n1 = (union node *)stalloc(sizeof (struct nfor));
369 		n1->type = NFOR;
370 		n1->nfor.var = wordtext;
371 		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
372 			app = &ap;
373 			while (readtoken() == TWORD) {
374 				n2 = (union node *)stalloc(sizeof (struct narg));
375 				n2->type = NARG;
376 				n2->narg.text = wordtext;
377 				n2->narg.backquote = backquotelist;
378 				*app = n2;
379 				app = &n2->narg.next;
380 			}
381 			*app = NULL;
382 			n1->nfor.args = ap;
383 			if (lasttoken != TNL && lasttoken != TSEMI)
384 				synexpect(-1);
385 		} else {
386 #ifndef GDB_HACK
387 			static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
388 								   '@', '=', '\0'};
389 #endif
390 			n2 = (union node *)stalloc(sizeof (struct narg));
391 			n2->type = NARG;
392 			n2->narg.text = (char *)argvars;
393 			n2->narg.backquote = NULL;
394 			n2->narg.next = NULL;
395 			n1->nfor.args = n2;
396 			/*
397 			 * Newline or semicolon here is optional (but note
398 			 * that the original Bourne shell only allowed NL).
399 			 */
400 			if (lasttoken != TNL && lasttoken != TSEMI)
401 				tokpushback++;
402 		}
403 		checkkwd = 2;
404 		if ((t = readtoken()) == TDO)
405 			t = TDONE;
406 		else if (t == TBEGIN)
407 			t = TEND;
408 		else
409 			synexpect(-1);
410 		n1->nfor.body = list(0);
411 		if (readtoken() != t)
412 			synexpect(t);
413 		checkkwd = 1;
414 		break;
415 	case TCASE:
416 		n1 = (union node *)stalloc(sizeof (struct ncase));
417 		n1->type = NCASE;
418 		if (readtoken() != TWORD)
419 			synexpect(TWORD);
420 		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
421 		n2->type = NARG;
422 		n2->narg.text = wordtext;
423 		n2->narg.backquote = backquotelist;
424 		n2->narg.next = NULL;
425 		while (readtoken() == TNL);
426 		if (lasttoken != TWORD || ! equal(wordtext, "in"))
427 			synerror("expecting \"in\"");
428 		cpp = &n1->ncase.cases;
429 		noaliases = 1;	/* turn off alias expansion */
430 		checkkwd = 2, readtoken();
431 		do {
432 			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
433 			cp->type = NCLIST;
434 			app = &cp->nclist.pattern;
435 			for (;;) {
436 				*app = ap = (union node *)stalloc(sizeof (struct narg));
437 				ap->type = NARG;
438 				ap->narg.text = wordtext;
439 				ap->narg.backquote = backquotelist;
440 				if (checkkwd = 2, readtoken() != TPIPE)
441 					break;
442 				app = &ap->narg.next;
443 				readtoken();
444 			}
445 			ap->narg.next = NULL;
446 			if (lasttoken != TRP)
447 				noaliases = 0, synexpect(TRP);
448 			cp->nclist.body = list(0);
449 
450 			checkkwd = 2;
451 			if ((t = readtoken()) != TESAC) {
452 				if (t != TENDCASE)
453 					noaliases = 0, synexpect(TENDCASE);
454 				else
455 					checkkwd = 2, readtoken();
456 			}
457 			cpp = &cp->nclist.next;
458 		} while(lasttoken != TESAC);
459 		noaliases = 0;	/* reset alias expansion */
460 		*cpp = NULL;
461 		checkkwd = 1;
462 		break;
463 	case TLP:
464 		n1 = (union node *)stalloc(sizeof (struct nredir));
465 		n1->type = NSUBSHELL;
466 		n1->nredir.n = list(0);
467 		n1->nredir.redirect = NULL;
468 		if (readtoken() != TRP)
469 			synexpect(TRP);
470 		checkkwd = 1;
471 		break;
472 	case TBEGIN:
473 		n1 = list(0);
474 		if (readtoken() != TEND)
475 			synexpect(TEND);
476 		checkkwd = 1;
477 		break;
478 	/* Handle an empty command like other simple commands.  */
479 	case TSEMI:
480 		/*
481 		 * An empty command before a ; doesn't make much sense, and
482 		 * should certainly be disallowed in the case of `if ;'.
483 		 */
484 		if (!redir)
485 			synexpect(-1);
486 	case TAND:
487 	case TOR:
488 	case TNL:
489 	case TEOF:
490 	case TWORD:
491 	case TRP:
492 		tokpushback++;
493 		n1 = simplecmd(rpp, redir);
494 		goto checkneg;
495 	default:
496 		synexpect(-1);
497 	}
498 
499 	/* Now check for redirection which may follow command */
500 	while (readtoken() == TREDIR) {
501 		*rpp = n2 = redirnode;
502 		rpp = &n2->nfile.next;
503 		parsefname();
504 	}
505 	tokpushback++;
506 	*rpp = NULL;
507 	if (redir) {
508 		if (n1->type != NSUBSHELL) {
509 			n2 = (union node *)stalloc(sizeof (struct nredir));
510 			n2->type = NREDIR;
511 			n2->nredir.n = n1;
512 			n1 = n2;
513 		}
514 		n1->nredir.redirect = redir;
515 	}
516 
517 checkneg:
518 	if (negate) {
519 		n2 = (union node *)stalloc(sizeof (struct nnot));
520 		n2->type = NNOT;
521 		n2->nnot.com = n1;
522 		return n2;
523 	}
524 	else
525 		return n1;
526 }
527 
528 
529 STATIC union node *
530 simplecmd(rpp, redir)
531 	union node **rpp, *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() {
608 	union node *n;
609 
610 	n = (union node *)stalloc(sizeof (struct narg));
611 	n->type = NARG;
612 	n->narg.next = NULL;
613 	n->narg.text = wordtext;
614 	n->narg.backquote = backquotelist;
615 	return n;
616 }
617 
618 void fixredir(n, text, err)
619 	union node *n;
620 	const char *text;
621 	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 
641 STATIC void
642 parsefname() {
643 	union node *n = redirnode;
644 
645 	if (readtoken() != TWORD)
646 		synexpect(-1);
647 	if (n->type == NHERE) {
648 		struct heredoc *here = heredoc;
649 		struct heredoc *p;
650 		int i;
651 
652 		if (quoteflag == 0)
653 			n->type = NXHERE;
654 		TRACE(("Here document %d\n", n->type));
655 		if (here->striptabs) {
656 			while (*wordtext == '\t')
657 				wordtext++;
658 		}
659 		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
660 			synerror("Illegal eof marker for << redirection");
661 		rmescapes(wordtext);
662 		here->eofmark = wordtext;
663 		here->next = NULL;
664 		if (heredoclist == NULL)
665 			heredoclist = here;
666 		else {
667 			for (p = heredoclist ; p->next ; p = p->next);
668 			p->next = here;
669 		}
670 	} else if (n->type == NTOFD || n->type == NFROMFD) {
671 		fixredir(n, wordtext, 0);
672 	} else {
673 		n->nfile.fname = makename();
674 	}
675 }
676 
677 
678 /*
679  * Input any here documents.
680  */
681 
682 STATIC void
683 parseheredoc() {
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() {
707 	int t;
708 
709 	t = readtoken();
710 	tokpushback++;
711 	return (t);
712 }
713 
714 STATIC int
715 readtoken() {
716 	int t;
717 	int savecheckkwd = checkkwd;
718 	struct alias *ap;
719 #ifdef DEBUG
720 	int alreadyseen = tokpushback;
721 #endif
722 
723 	top:
724 	t = xxreadtoken();
725 
726 	if (checkkwd) {
727 		/*
728 		 * eat newlines
729 		 */
730 		if (checkkwd == 2) {
731 			checkkwd = 0;
732 			while (t == TNL) {
733 				parseheredoc();
734 				t = xxreadtoken();
735 			}
736 		} else
737 			checkkwd = 0;
738 		/*
739 		 * check for keywords and aliases
740 		 */
741 		if (t == TWORD && !quoteflag)
742 		{
743 			char * const *pp;
744 
745 			for (pp = (char **)parsekwd; *pp; pp++) {
746 				if (**pp == *wordtext && equal(*pp, wordtext))
747 				{
748 					lasttoken = t = pp - parsekwd + KWDOFFSET;
749 					TRACE(("keyword %s recognized\n", tokname[t]));
750 					goto out;
751 				}
752 			}
753 			if (noaliases == 0 &&
754 			    (ap = lookupalias(wordtext, 1)) != NULL) {
755 				pushstring(ap->val, strlen(ap->val), ap);
756 				checkkwd = savecheckkwd;
757 				goto top;
758 			}
759 		}
760 out:
761 		checkkwd = (t == TNOT) ? savecheckkwd : 0;
762 	}
763 #ifdef DEBUG
764 	if (!alreadyseen)
765 	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
766 	else
767 	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
768 #endif
769 	return (t);
770 }
771 
772 
773 /*
774  * Read the next input token.
775  * If the token is a word, we set backquotelist to the list of cmds in
776  *	backquotes.  We set quoteflag to true if any part of the word was
777  *	quoted.
778  * If the token is TREDIR, then we set redirnode to a structure containing
779  *	the redirection.
780  * In all cases, the variable startlinno is set to the number of the line
781  *	on which the token starts.
782  *
783  * [Change comment:  here documents and internal procedures]
784  * [Readtoken shouldn't have any arguments.  Perhaps we should make the
785  *  word parsing code into a separate routine.  In this case, readtoken
786  *  doesn't need to have any internal procedures, but parseword does.
787  *  We could also make parseoperator in essence the main routine, and
788  *  have parseword (readtoken1?) handle both words and redirection.]
789  */
790 
791 #define RETURN(token)	return lasttoken = token
792 
793 STATIC int
794 xxreadtoken() {
795 	int c;
796 
797 	if (tokpushback) {
798 		tokpushback = 0;
799 		return lasttoken;
800 	}
801 	if (needprompt) {
802 		setprompt(2);
803 		needprompt = 0;
804 	}
805 	startlinno = plinno;
806 	for (;;) {	/* until token or start of word found */
807 		c = pgetc_macro();
808 		if (c == ' ' || c == '\t')
809 			continue;		/* quick check for white space first */
810 		switch (c) {
811 		case ' ': case '\t':
812 			continue;
813 		case '#':
814 			while ((c = pgetc()) != '\n' && c != PEOF);
815 			pungetc();
816 			continue;
817 		case '\\':
818 			if (pgetc() == '\n') {
819 				startlinno = ++plinno;
820 				if (doprompt)
821 					setprompt(2);
822 				else
823 					setprompt(0);
824 				continue;
825 			}
826 			pungetc();
827 			goto breakloop;
828 		case '\n':
829 			plinno++;
830 			needprompt = doprompt;
831 			RETURN(TNL);
832 		case PEOF:
833 			RETURN(TEOF);
834 		case '&':
835 			if (pgetc() == '&')
836 				RETURN(TAND);
837 			pungetc();
838 			RETURN(TBACKGND);
839 		case '|':
840 			if (pgetc() == '|')
841 				RETURN(TOR);
842 			pungetc();
843 			RETURN(TPIPE);
844 		case ';':
845 			if (pgetc() == ';')
846 				RETURN(TENDCASE);
847 			pungetc();
848 			RETURN(TSEMI);
849 		case '(':
850 			RETURN(TLP);
851 		case ')':
852 			RETURN(TRP);
853 		default:
854 			goto breakloop;
855 		}
856 	}
857 breakloop:
858 	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
859 #undef RETURN
860 }
861 
862 
863 
864 /*
865  * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
866  * is not NULL, read a here document.  In the latter case, eofmark is the
867  * word which marks the end of the document and striptabs is true if
868  * leading tabs should be stripped from the document.  The argument firstc
869  * is the first character of the input token or document.
870  *
871  * Because C does not have internal subroutines, I have simulated them
872  * using goto's to implement the subroutine linkage.  The following macros
873  * will run code that appears at the end of readtoken1.
874  */
875 
876 #define CHECKEND()	{goto checkend; checkend_return:;}
877 #define PARSEREDIR()	{goto parseredir; parseredir_return:;}
878 #define PARSESUB()	{goto parsesub; parsesub_return:;}
879 #define PARSEBACKQOLD()	{oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
880 #define PARSEBACKQNEW()	{oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
881 #define	PARSEARITH()	{goto parsearith; parsearith_return:;}
882 
883 STATIC int
884 readtoken1(firstc, syntax, eofmark, striptabs)
885 	int firstc;
886 	char const *syntax;
887 	char *eofmark;
888 	int striptabs;
889 	{
890 	int c = firstc;
891 	char *out;
892 	int len;
893 	char line[EOFMARKLEN + 1];
894 	struct nodelist *bqlist;
895 	int quotef;
896 	int dblquote;
897 	int varnest;	/* levels of variables expansion */
898 	int arinest;	/* levels of arithmetic expansion */
899 	int parenlevel;	/* levels of parens in arithmetic */
900 	int oldstyle;
901 	char const *prevsyntax;	/* syntax before arithmetic */
902 	int synentry;
903 #if __GNUC__
904 	/* Avoid longjmp clobbering */
905 	(void) &out;
906 	(void) &quotef;
907 	(void) &dblquote;
908 	(void) &varnest;
909 	(void) &arinest;
910 	(void) &parenlevel;
911 	(void) &oldstyle;
912 	(void) &prevsyntax;
913 	(void) &syntax;
914 	(void) &synentry;
915 #endif
916 
917 	startlinno = plinno;
918 	dblquote = 0;
919 	if (syntax == DQSYNTAX)
920 		dblquote = 1;
921 	quotef = 0;
922 	bqlist = NULL;
923 	varnest = 0;
924 	arinest = 0;
925 	parenlevel = 0;
926 
927 	STARTSTACKSTR(out);
928 	loop: {	/* for each line, until end of word */
929 #if ATTY
930 		if (c == '\034' && doprompt
931 		 && attyset() && ! equal(termval(), "emacs")) {
932 			attyline();
933 			if (syntax == BASESYNTAX)
934 				return readtoken();
935 			c = pgetc();
936 			goto loop;
937 		}
938 #endif
939 		CHECKEND();	/* set c to PEOF if at end of here document */
940 		for (;;) {	/* until end of line or end of word */
941 			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
942 
943 			synentry = syntax[c];
944 
945 			switch(synentry) {
946 			case CNL:	/* '\n' */
947 				if (syntax == BASESYNTAX)
948 					goto endword;	/* exit outer loop */
949 				USTPUTC(c, out);
950 				plinno++;
951 				if (doprompt)
952 					setprompt(2);
953 				else
954 					setprompt(0);
955 				c = pgetc();
956 				goto loop;		/* continue outer loop */
957 			case CWORD:
958 				USTPUTC(c, out);
959 				break;
960 			case CCTL:
961 				if (eofmark == NULL || dblquote)
962 					USTPUTC(CTLESC, out);
963 				USTPUTC(c, out);
964 				break;
965 			case CBACK:	/* backslash */
966 				c = pgetc();
967 				if (c == PEOF) {
968 					USTPUTC('\\', out);
969 					pungetc();
970 				} else if (c == '\n') {
971 					if (doprompt)
972 						setprompt(2);
973 					else
974 						setprompt(0);
975 				} else {
976 					if (dblquote && c != '\\' &&
977 					    c != '`' && c != '$' &&
978 					    (c != '"' || eofmark != NULL))
979 						USTPUTC('\\', out);
980 					if (c >= 0 && SQSYNTAX[c] == CCTL)
981 						USTPUTC(CTLESC, out);
982 					else if (eofmark == NULL)
983 						USTPUTC(CTLQUOTEMARK, out);
984 					USTPUTC(c, out);
985 					quotef++;
986 				}
987 				break;
988 			case CSQUOTE:
989 				if (eofmark == NULL)
990 					USTPUTC(CTLQUOTEMARK, out);
991 				syntax = SQSYNTAX;
992 				break;
993 			case CDQUOTE:
994 				if (eofmark == NULL)
995 					USTPUTC(CTLQUOTEMARK, out);
996 				syntax = DQSYNTAX;
997 				dblquote = 1;
998 				break;
999 			case CENDQUOTE:
1000 				if (eofmark != NULL && arinest == 0 &&
1001 				    varnest == 0) {
1002 					USTPUTC(c, out);
1003 				} else {
1004 					if (arinest) {
1005 						syntax = ARISYNTAX;
1006 						dblquote = 0;
1007 					} else if (eofmark == NULL) {
1008 						syntax = BASESYNTAX;
1009 						dblquote = 0;
1010 					}
1011 					quotef++;
1012 				}
1013 				break;
1014 			case CVAR:	/* '$' */
1015 				PARSESUB();		/* parse substitution */
1016 				break;
1017 			case CENDVAR:	/* '}' */
1018 				if (varnest > 0) {
1019 					varnest--;
1020 					USTPUTC(CTLENDVAR, out);
1021 				} else {
1022 					USTPUTC(c, out);
1023 				}
1024 				break;
1025 			case CLP:	/* '(' in arithmetic */
1026 				parenlevel++;
1027 				USTPUTC(c, out);
1028 				break;
1029 			case CRP:	/* ')' in arithmetic */
1030 				if (parenlevel > 0) {
1031 					USTPUTC(c, out);
1032 					--parenlevel;
1033 				} else {
1034 					if (pgetc() == ')') {
1035 						if (--arinest == 0) {
1036 							USTPUTC(CTLENDARI, out);
1037 							syntax = prevsyntax;
1038 							if (syntax == DQSYNTAX)
1039 								dblquote = 1;
1040 							else
1041 								dblquote = 0;
1042 						} else
1043 							USTPUTC(')', out);
1044 					} else {
1045 						/*
1046 						 * unbalanced parens
1047 						 *  (don't 2nd guess - no error)
1048 						 */
1049 						pungetc();
1050 						USTPUTC(')', out);
1051 					}
1052 				}
1053 				break;
1054 			case CBQUOTE:	/* '`' */
1055 				PARSEBACKQOLD();
1056 				break;
1057 			case CEOF:
1058 				goto endword;		/* exit outer loop */
1059 			default:
1060 				if (varnest == 0)
1061 					goto endword;	/* exit outer loop */
1062 				USTPUTC(c, out);
1063 			}
1064 			c = pgetc_macro();
1065 		}
1066 	}
1067 endword:
1068 	if (syntax == ARISYNTAX)
1069 		synerror("Missing '))'");
1070 	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1071 		synerror("Unterminated quoted string");
1072 	if (varnest != 0) {
1073 		startlinno = plinno;
1074 		synerror("Missing '}'");
1075 	}
1076 	USTPUTC('\0', out);
1077 	len = out - stackblock();
1078 	out = stackblock();
1079 	if (eofmark == NULL) {
1080 		if ((c == '>' || c == '<')
1081 		 && quotef == 0
1082 		 && len <= 2
1083 		 && (*out == '\0' || is_digit(*out))) {
1084 			PARSEREDIR();
1085 			return lasttoken = TREDIR;
1086 		} else {
1087 			pungetc();
1088 		}
1089 	}
1090 	quoteflag = quotef;
1091 	backquotelist = bqlist;
1092 	grabstackblock(len);
1093 	wordtext = out;
1094 	return lasttoken = TWORD;
1095 /* end of readtoken routine */
1096 
1097 
1098 
1099 /*
1100  * Check to see whether we are at the end of the here document.  When this
1101  * is called, c is set to the first character of the next input line.  If
1102  * we are at the end of the here document, this routine sets the c to PEOF.
1103  */
1104 
1105 checkend: {
1106 	if (eofmark) {
1107 		if (striptabs) {
1108 			while (c == '\t')
1109 				c = pgetc();
1110 		}
1111 		if (c == *eofmark) {
1112 			if (pfgets(line, sizeof line) != NULL) {
1113 				char *p, *q;
1114 
1115 				p = line;
1116 				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1117 				if (*p == '\n' && *q == '\0') {
1118 					c = PEOF;
1119 					plinno++;
1120 					needprompt = doprompt;
1121 				} else {
1122 					pushstring(line, strlen(line), NULL);
1123 				}
1124 			}
1125 		}
1126 	}
1127 	goto checkend_return;
1128 }
1129 
1130 
1131 /*
1132  * Parse a redirection operator.  The variable "out" points to a string
1133  * specifying the fd to be redirected.  The variable "c" contains the
1134  * first character of the redirection operator.
1135  */
1136 
1137 parseredir: {
1138 	char fd = *out;
1139 	union node *np;
1140 
1141 	np = (union node *)stalloc(sizeof (struct nfile));
1142 	if (c == '>') {
1143 		np->nfile.fd = 1;
1144 		c = pgetc();
1145 		if (c == '>')
1146 			np->type = NAPPEND;
1147 		else if (c == '&')
1148 			np->type = NTOFD;
1149 		else {
1150 			np->type = NTO;
1151 			pungetc();
1152 		}
1153 	} else {	/* c == '<' */
1154 		np->nfile.fd = 0;
1155 		c = pgetc();
1156 		if (c == '<') {
1157 			if (sizeof (struct nfile) != sizeof (struct nhere)) {
1158 				np = (union node *)stalloc(sizeof (struct nhere));
1159 				np->nfile.fd = 0;
1160 			}
1161 			np->type = NHERE;
1162 			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1163 			heredoc->here = np;
1164 			if ((c = pgetc()) == '-') {
1165 				heredoc->striptabs = 1;
1166 			} else {
1167 				heredoc->striptabs = 0;
1168 				pungetc();
1169 			}
1170 		} else if (c == '&')
1171 			np->type = NFROMFD;
1172 		else if (c == '>')
1173 			np->type = NFROMTO;
1174 		else {
1175 			np->type = NFROM;
1176 			pungetc();
1177 		}
1178 	}
1179 	if (fd != '\0')
1180 		np->nfile.fd = digit_val(fd);
1181 	redirnode = np;
1182 	goto parseredir_return;
1183 }
1184 
1185 
1186 /*
1187  * Parse a substitution.  At this point, we have read the dollar sign
1188  * and nothing else.
1189  */
1190 
1191 parsesub: {
1192 	int subtype;
1193 	int typeloc;
1194 	int flags;
1195 	char *p;
1196 #ifndef GDB_HACK
1197 	static const char types[] = "}-+?=";
1198 #endif
1199        int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1200 
1201 	c = pgetc();
1202 	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
1203 		USTPUTC('$', out);
1204 		pungetc();
1205 	} else if (c == '(') {	/* $(command) or $((arith)) */
1206 		if (pgetc() == '(') {
1207 			PARSEARITH();
1208 		} else {
1209 			pungetc();
1210 			PARSEBACKQNEW();
1211 		}
1212 	} else {
1213 		USTPUTC(CTLVAR, out);
1214 		typeloc = out - stackblock();
1215 		USTPUTC(VSNORMAL, out);
1216 		subtype = VSNORMAL;
1217 		if (c == '{') {
1218 			bracketed_name = 1;
1219 			c = pgetc();
1220 			if (c == '#') {
1221 				if ((c = pgetc()) == '}')
1222 					c = '#';
1223 				else
1224 					subtype = VSLENGTH;
1225 			}
1226 			else
1227 				subtype = 0;
1228 		}
1229 		if (is_name(c)) {
1230 			do {
1231 				STPUTC(c, out);
1232 				c = pgetc();
1233 			} while (is_in_name(c));
1234 		} else if (is_digit(c)) {
1235 			if (bracketed_name) {
1236 				do {
1237 					STPUTC(c, out);
1238 					c = pgetc();
1239 				} while (is_digit(c));
1240 			} else {
1241 				STPUTC(c, out);
1242 				c = pgetc();
1243 			}
1244 		} else {
1245 			if (! is_special(c))
1246 badsub:				synerror("Bad substitution");
1247 			USTPUTC(c, out);
1248 			c = pgetc();
1249 		}
1250 		STPUTC('=', out);
1251 		flags = 0;
1252 		if (subtype == 0) {
1253 			switch (c) {
1254 			case ':':
1255 				flags = VSNUL;
1256 				c = pgetc();
1257 				/*FALLTHROUGH*/
1258 			default:
1259 				p = strchr(types, c);
1260 				if (p == NULL)
1261 					goto badsub;
1262 				subtype = p - types + VSNORMAL;
1263 				break;
1264 			case '%':
1265 			case '#':
1266 				{
1267 					int cc = c;
1268 					subtype = c == '#' ? VSTRIMLEFT :
1269 							     VSTRIMRIGHT;
1270 					c = pgetc();
1271 					if (c == cc)
1272 						subtype++;
1273 					else
1274 						pungetc();
1275 					break;
1276 				}
1277 			}
1278 		} else {
1279 			pungetc();
1280 		}
1281 		if (subtype != VSLENGTH && (dblquote || arinest))
1282 			flags |= VSQUOTE;
1283 		*(stackblock() + typeloc) = subtype | flags;
1284 		if (subtype != VSNORMAL)
1285 			varnest++;
1286 	}
1287 	goto parsesub_return;
1288 }
1289 
1290 
1291 /*
1292  * Called to parse command substitutions.  Newstyle is set if the command
1293  * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1294  * list of commands (passed by reference), and savelen is the number of
1295  * characters on the top of the stack which must be preserved.
1296  */
1297 
1298 parsebackq: {
1299 	struct nodelist **nlpp;
1300 	int savepbq;
1301 	union node *n;
1302 	char *volatile str;
1303 	struct jmploc jmploc;
1304 	struct jmploc *volatile savehandler;
1305 	int savelen;
1306 	int saveprompt;
1307 #if __GNUC__
1308 	/* Avoid longjmp clobbering */
1309 	(void) &saveprompt;
1310 #endif
1311 
1312 	savepbq = parsebackquote;
1313 	if (setjmp(jmploc.loc)) {
1314 		if (str)
1315 			ckfree(str);
1316 		parsebackquote = 0;
1317 		handler = savehandler;
1318 		longjmp(handler->loc, 1);
1319 	}
1320 	INTOFF;
1321 	str = NULL;
1322 	savelen = out - stackblock();
1323 	if (savelen > 0) {
1324 		str = ckmalloc(savelen);
1325 		memcpy(str, stackblock(), savelen);
1326 	}
1327 	savehandler = handler;
1328 	handler = &jmploc;
1329 	INTON;
1330         if (oldstyle) {
1331                 /* We must read until the closing backquote, giving special
1332                    treatment to some slashes, and then push the string and
1333                    reread it as input, interpreting it normally.  */
1334                 char *out;
1335                 int c;
1336                 int savelen;
1337                 char *str;
1338 
1339 
1340                 STARTSTACKSTR(out);
1341 		for (;;) {
1342 			if (needprompt) {
1343 				setprompt(2);
1344 				needprompt = 0;
1345 			}
1346 			switch (c = pgetc()) {
1347 			case '`':
1348 				goto done;
1349 
1350 			case '\\':
1351                                 if ((c = pgetc()) == '\n') {
1352 					plinno++;
1353 					if (doprompt)
1354 						setprompt(2);
1355 					else
1356 						setprompt(0);
1357 					/*
1358 					 * If eating a newline, avoid putting
1359 					 * the newline into the new character
1360 					 * stream (via the STPUTC after the
1361 					 * switch).
1362 					 */
1363 					continue;
1364 				}
1365                                 if (c != '\\' && c != '`' && c != '$'
1366                                     && (!dblquote || c != '"'))
1367                                         STPUTC('\\', out);
1368 				break;
1369 
1370 			case '\n':
1371 				plinno++;
1372 				needprompt = doprompt;
1373 				break;
1374 
1375 			case PEOF:
1376 			        startlinno = plinno;
1377 				synerror("EOF in backquote substitution");
1378  				break;
1379 
1380 			default:
1381 				break;
1382 			}
1383 			STPUTC(c, out);
1384                 }
1385 done:
1386                 STPUTC('\0', out);
1387                 savelen = out - stackblock();
1388                 if (savelen > 0) {
1389                         str = ckmalloc(savelen);
1390                         memcpy(str, stackblock(), savelen);
1391 			setinputstring(str, 1);
1392                 }
1393         }
1394 	nlpp = &bqlist;
1395 	while (*nlpp)
1396 		nlpp = &(*nlpp)->next;
1397 	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1398 	(*nlpp)->next = NULL;
1399 	parsebackquote = oldstyle;
1400 
1401 	if (oldstyle) {
1402 		saveprompt = doprompt;
1403 		doprompt = 0;
1404 	}
1405 
1406 	n = list(0);
1407 
1408 	if (oldstyle)
1409 		doprompt = saveprompt;
1410 	else {
1411 		if (readtoken() != TRP)
1412 			synexpect(TRP);
1413 	}
1414 
1415 	(*nlpp)->n = n;
1416         if (oldstyle) {
1417 		/*
1418 		 * Start reading from old file again, ignoring any pushed back
1419 		 * tokens left from the backquote parsing
1420 		 */
1421                 popfile();
1422 		tokpushback = 0;
1423 	}
1424 	while (stackblocksize() <= savelen)
1425 		growstackblock();
1426 	STARTSTACKSTR(out);
1427 	if (str) {
1428 		memcpy(out, str, savelen);
1429 		STADJUST(savelen, out);
1430 		INTOFF;
1431 		ckfree(str);
1432 		str = NULL;
1433 		INTON;
1434 	}
1435 	parsebackquote = savepbq;
1436 	handler = savehandler;
1437 	if (arinest || dblquote)
1438 		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1439 	else
1440 		USTPUTC(CTLBACKQ, out);
1441 	if (oldstyle)
1442 		goto parsebackq_oldreturn;
1443 	else
1444 		goto parsebackq_newreturn;
1445 }
1446 
1447 /*
1448  * Parse an arithmetic expansion (indicate start of one and set state)
1449  */
1450 parsearith: {
1451 
1452 	if (++arinest == 1) {
1453 		prevsyntax = syntax;
1454 		syntax = ARISYNTAX;
1455 		USTPUTC(CTLARI, out);
1456 		if (dblquote)
1457 			USTPUTC('"',out);
1458 		else
1459 			USTPUTC(' ',out);
1460 	} else {
1461 		/*
1462 		 * we collapse embedded arithmetic expansion to
1463 		 * parenthesis, which should be equivalent
1464 		 */
1465 		USTPUTC('(', out);
1466 	}
1467 	goto parsearith_return;
1468 }
1469 
1470 } /* end of readtoken */
1471 
1472 
1473 
1474 #ifdef mkinit
1475 RESET {
1476 	tokpushback = 0;
1477 	checkkwd = 0;
1478 }
1479 #endif
1480 
1481 /*
1482  * Returns true if the text contains nothing to expand (no dollar signs
1483  * or backquotes).
1484  */
1485 
1486 STATIC int
1487 noexpand(text)
1488 	char *text;
1489 	{
1490 	char *p;
1491 	char c;
1492 
1493 	p = text;
1494 	while ((c = *p++) != '\0') {
1495 		if ( c == CTLQUOTEMARK)
1496 			continue;
1497 		if (c == CTLESC)
1498 			p++;
1499 		else if (c >= 0 && BASESYNTAX[(int)c] == CCTL)
1500 			return 0;
1501 	}
1502 	return 1;
1503 }
1504 
1505 
1506 /*
1507  * Return true if the argument is a legal variable name (a letter or
1508  * underscore followed by zero or more letters, underscores, and digits).
1509  */
1510 
1511 int
1512 goodname(name)
1513 	char *name;
1514 	{
1515 	char *p;
1516 
1517 	p = name;
1518 	if (! is_name(*p))
1519 		return 0;
1520 	while (*++p) {
1521 		if (! is_in_name(*p))
1522 			return 0;
1523 	}
1524 	return 1;
1525 }
1526 
1527 
1528 /*
1529  * Called when an unexpected token is read during the parse.  The argument
1530  * is the token that is expected, or -1 if more than one type of token can
1531  * occur at this point.
1532  */
1533 
1534 STATIC void
1535 synexpect(token)
1536 	int token;
1537 {
1538 	char msg[64];
1539 
1540 	if (token >= 0) {
1541 		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1542 			tokname[lasttoken], tokname[token]);
1543 	} else {
1544 		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1545 	}
1546 	synerror(msg);
1547 }
1548 
1549 
1550 STATIC void
1551 synerror(msg)
1552 	char *msg;
1553 	{
1554 	if (commandname)
1555 		outfmt(&errout, "%s: %d: ", commandname, startlinno);
1556 	outfmt(&errout, "Syntax error: %s\n", msg);
1557 	error((char *)NULL);
1558 }
1559 
1560 STATIC void
1561 setprompt(which)
1562 	int which;
1563 	{
1564 	whichprompt = which;
1565 
1566 #ifndef NO_HISTORY
1567 	if (!el)
1568 #endif
1569 		out2str(getprompt(NULL));
1570 }
1571 
1572 /*
1573  * called by editline -- any expansions to the prompt
1574  *    should be added here.
1575  */
1576 char *
1577 getprompt(unused)
1578 	void *unused __unused;
1579 {
1580 	switch (whichprompt) {
1581 	case 0:
1582 		return "";
1583 	case 1:
1584 		return ps1val();
1585 	case 2:
1586 		return ps2val();
1587 	default:
1588 		return "<internal prompt error>";
1589 	}
1590 }
1591