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