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