xref: /netbsd-src/bin/sh/parser.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /*	$NetBSD: parser.c,v 1.47 2001/04/03 15:00:11 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.47 2001/04/03 15:00:11 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 struct heredoc *heredoclist;	/* list of here documents to read */
91 int parsebackquote;		/* nonzero if we are inside backquotes */
92 int doprompt;			/* if set, prompt the user */
93 int needprompt;			/* true if interactive and at start of line */
94 int lasttoken;			/* last token read */
95 MKINIT int tokpushback;		/* last token pushed back */
96 char *wordtext;			/* text of last word returned by readtoken */
97 MKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
98 struct nodelist *backquotelist;
99 union node *redirnode;
100 struct heredoc *heredoc;
101 int quoteflag;			/* set if (part of) last token was quoted */
102 int startlinno;			/* line # where last token started */
103 
104 
105 STATIC union node *list __P((int));
106 STATIC union node *andor __P((void));
107 STATIC union node *pipeline __P((void));
108 STATIC union node *command __P((void));
109 STATIC union node *simplecmd __P((union node **, union node *));
110 STATIC union node *makename __P((void));
111 STATIC void parsefname __P((void));
112 STATIC void parseheredoc __P((void));
113 STATIC int peektoken __P((void));
114 STATIC int readtoken __P((void));
115 STATIC int xxreadtoken __P((void));
116 STATIC int readtoken1 __P((int, char const *, char *, int));
117 STATIC int noexpand __P((char *));
118 STATIC void synexpect __P((int)) __attribute__((noreturn));
119 STATIC void synerror __P((const char *)) __attribute__((noreturn));
120 STATIC void setprompt __P((int));
121 
122 
123 /*
124  * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
125  * valid parse tree indicating a blank line.)
126  */
127 
128 union node *
129 parsecmd(int interact)
130 {
131 	int t;
132 
133 	tokpushback = 0;
134 	doprompt = interact;
135 	if (doprompt)
136 		setprompt(1);
137 	else
138 		setprompt(0);
139 	needprompt = 0;
140 	t = readtoken();
141 	if (t == TEOF)
142 		return NEOF;
143 	if (t == TNL)
144 		return NULL;
145 	tokpushback++;
146 	return list(1);
147 }
148 
149 
150 STATIC union node *
151 list(nlflag)
152 	int nlflag;
153 {
154 	union node *n1, *n2, *n3;
155 	int tok;
156 
157 	checkkwd = 2;
158 	if (nlflag == 0 && tokendlist[peektoken()])
159 		return NULL;
160 	n1 = NULL;
161 	for (;;) {
162 		n2 = andor();
163 		tok = readtoken();
164 		if (tok == TBACKGND) {
165 			if (n2->type == NCMD || n2->type == NPIPE) {
166 				n2->ncmd.backgnd = 1;
167 			} else if (n2->type == NREDIR) {
168 				n2->type = NBACKGND;
169 			} else {
170 				n3 = (union node *)stalloc(sizeof (struct nredir));
171 				n3->type = NBACKGND;
172 				n3->nredir.n = n2;
173 				n3->nredir.redirect = NULL;
174 				n2 = n3;
175 			}
176 		}
177 		if (n1 == NULL) {
178 			n1 = n2;
179 		}
180 		else {
181 			n3 = (union node *)stalloc(sizeof (struct nbinary));
182 			n3->type = NSEMI;
183 			n3->nbinary.ch1 = n1;
184 			n3->nbinary.ch2 = n2;
185 			n1 = n3;
186 		}
187 		switch (tok) {
188 		case TBACKGND:
189 		case TSEMI:
190 			tok = readtoken();
191 			/* fall through */
192 		case TNL:
193 			if (tok == TNL) {
194 				parseheredoc();
195 				if (nlflag)
196 					return n1;
197 			} else {
198 				tokpushback++;
199 			}
200 			checkkwd = 2;
201 			if (tokendlist[peektoken()])
202 				return n1;
203 			break;
204 		case TEOF:
205 			if (heredoclist)
206 				parseheredoc();
207 			else
208 				pungetc();		/* push back EOF on input */
209 			return n1;
210 		default:
211 			if (nlflag)
212 				synexpect(-1);
213 			tokpushback++;
214 			return n1;
215 		}
216 	}
217 }
218 
219 
220 
221 STATIC union node *
222 andor() {
223 	union node *n1, *n2, *n3;
224 	int t;
225 
226 	n1 = pipeline();
227 	for (;;) {
228 		if ((t = readtoken()) == TAND) {
229 			t = NAND;
230 		} else if (t == TOR) {
231 			t = NOR;
232 		} else {
233 			tokpushback++;
234 			return n1;
235 		}
236 		n2 = pipeline();
237 		n3 = (union node *)stalloc(sizeof (struct nbinary));
238 		n3->type = t;
239 		n3->nbinary.ch1 = n1;
240 		n3->nbinary.ch2 = n2;
241 		n1 = n3;
242 	}
243 }
244 
245 
246 
247 STATIC union node *
248 pipeline() {
249 	union node *n1, *n2, *pipenode;
250 	struct nodelist *lp, *prev;
251 	int negate;
252 
253 	negate = 0;
254 	TRACE(("pipeline: entered\n"));
255 	while (readtoken() == TNOT)
256 		negate = !negate;
257 	tokpushback++;
258 	n1 = command();
259 	if (readtoken() == TPIPE) {
260 		pipenode = (union node *)stalloc(sizeof (struct npipe));
261 		pipenode->type = NPIPE;
262 		pipenode->npipe.backgnd = 0;
263 		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
264 		pipenode->npipe.cmdlist = lp;
265 		lp->n = n1;
266 		do {
267 			prev = lp;
268 			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
269 			lp->n = command();
270 			prev->next = lp;
271 		} while (readtoken() == TPIPE);
272 		lp->next = NULL;
273 		n1 = pipenode;
274 	}
275 	tokpushback++;
276 	if (negate) {
277 		n2 = (union node *)stalloc(sizeof (struct nnot));
278 		n2->type = NNOT;
279 		n2->nnot.com = n1;
280 		return n2;
281 	} else
282 		return n1;
283 }
284 
285 
286 
287 STATIC union node *
288 command() {
289 	union node *n1, *n2;
290 	union node *ap, **app;
291 	union node *cp, **cpp;
292 	union node *redir, **rpp;
293 	int t, negate = 0;
294 
295 	checkkwd = 2;
296 	redir = NULL;
297 	n1 = NULL;
298 	rpp = &redir;
299 
300 	/* Check for redirection which may precede command */
301 	while (readtoken() == TREDIR) {
302 		*rpp = n2 = redirnode;
303 		rpp = &n2->nfile.next;
304 		parsefname();
305 	}
306 	tokpushback++;
307 
308 	while (readtoken() == TNOT) {
309 		TRACE(("command: TNOT recognized\n"));
310 		negate = !negate;
311 	}
312 	tokpushback++;
313 
314 	switch (readtoken()) {
315 	case TIF:
316 		n1 = (union node *)stalloc(sizeof (struct nif));
317 		n1->type = NIF;
318 		n1->nif.test = list(0);
319 		if (readtoken() != TTHEN)
320 			synexpect(TTHEN);
321 		n1->nif.ifpart = list(0);
322 		n2 = n1;
323 		while (readtoken() == TELIF) {
324 			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
325 			n2 = n2->nif.elsepart;
326 			n2->type = NIF;
327 			n2->nif.test = list(0);
328 			if (readtoken() != TTHEN)
329 				synexpect(TTHEN);
330 			n2->nif.ifpart = list(0);
331 		}
332 		if (lasttoken == TELSE)
333 			n2->nif.elsepart = list(0);
334 		else {
335 			n2->nif.elsepart = NULL;
336 			tokpushback++;
337 		}
338 		if (readtoken() != TFI)
339 			synexpect(TFI);
340 		checkkwd = 1;
341 		break;
342 	case TWHILE:
343 	case TUNTIL: {
344 		int got;
345 		n1 = (union node *)stalloc(sizeof (struct nbinary));
346 		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
347 		n1->nbinary.ch1 = list(0);
348 		if ((got=readtoken()) != TDO) {
349 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
350 			synexpect(TDO);
351 		}
352 		n1->nbinary.ch2 = list(0);
353 		if (readtoken() != TDONE)
354 			synexpect(TDONE);
355 		checkkwd = 1;
356 		break;
357 	}
358 	case TFOR:
359 		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
360 			synerror("Bad for loop variable");
361 		n1 = (union node *)stalloc(sizeof (struct nfor));
362 		n1->type = NFOR;
363 		n1->nfor.var = wordtext;
364 		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
365 			app = &ap;
366 			while (readtoken() == TWORD) {
367 				n2 = (union node *)stalloc(sizeof (struct narg));
368 				n2->type = NARG;
369 				n2->narg.text = wordtext;
370 				n2->narg.backquote = backquotelist;
371 				*app = n2;
372 				app = &n2->narg.next;
373 			}
374 			*app = NULL;
375 			n1->nfor.args = ap;
376 			if (lasttoken != TNL && lasttoken != TSEMI)
377 				synexpect(-1);
378 		} else {
379 			static char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
380 								   '@', '=', '\0'};
381 			n2 = (union node *)stalloc(sizeof (struct narg));
382 			n2->type = NARG;
383 			n2->narg.text = argvars;
384 			n2->narg.backquote = NULL;
385 			n2->narg.next = NULL;
386 			n1->nfor.args = n2;
387 			/*
388 			 * Newline or semicolon here is optional (but note
389 			 * that the original Bourne shell only allowed NL).
390 			 */
391 			if (lasttoken != TNL && lasttoken != TSEMI)
392 				tokpushback++;
393 		}
394 		checkkwd = 2;
395 		if ((t = readtoken()) == TDO)
396 			t = TDONE;
397 		else if (t == TBEGIN)
398 			t = TEND;
399 		else
400 			synexpect(-1);
401 		n1->nfor.body = list(0);
402 		if (readtoken() != t)
403 			synexpect(t);
404 		checkkwd = 1;
405 		break;
406 	case TCASE:
407 		n1 = (union node *)stalloc(sizeof (struct ncase));
408 		n1->type = NCASE;
409 		if (readtoken() != TWORD)
410 			synexpect(TWORD);
411 		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
412 		n2->type = NARG;
413 		n2->narg.text = wordtext;
414 		n2->narg.backquote = backquotelist;
415 		n2->narg.next = NULL;
416 		while (readtoken() == TNL);
417 		if (lasttoken != TWORD || ! equal(wordtext, "in"))
418 			synerror("expecting \"in\"");
419 		cpp = &n1->ncase.cases;
420 		checkkwd = 2, readtoken();
421 		do {
422 			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
423 			cp->type = NCLIST;
424 			app = &cp->nclist.pattern;
425 			for (;;) {
426 				*app = ap = (union node *)stalloc(sizeof (struct narg));
427 				ap->type = NARG;
428 				ap->narg.text = wordtext;
429 				ap->narg.backquote = backquotelist;
430 				if (checkkwd = 2, readtoken() != TPIPE)
431 					break;
432 				app = &ap->narg.next;
433 				readtoken();
434 			}
435 			ap->narg.next = NULL;
436 			if (lasttoken != TRP)
437 				synexpect(TRP);
438 			cp->nclist.body = list(0);
439 
440 			checkkwd = 2;
441 			if ((t = readtoken()) != TESAC) {
442 				if (t != TENDCASE)
443 					synexpect(TENDCASE);
444 				else
445 					checkkwd = 2, readtoken();
446 			}
447 			cpp = &cp->nclist.next;
448 		} while(lasttoken != TESAC);
449 		*cpp = NULL;
450 		checkkwd = 1;
451 		break;
452 	case TLP:
453 		n1 = (union node *)stalloc(sizeof (struct nredir));
454 		n1->type = NSUBSHELL;
455 		n1->nredir.n = list(0);
456 		n1->nredir.redirect = NULL;
457 		if (readtoken() != TRP)
458 			synexpect(TRP);
459 		checkkwd = 1;
460 		break;
461 	case TBEGIN:
462 		n1 = list(0);
463 		if (readtoken() != TEND)
464 			synexpect(TEND);
465 		checkkwd = 1;
466 		break;
467 	/* Handle an empty command like other simple commands.  */
468 	case TSEMI:
469 		/*
470 		 * An empty command before a ; doesn't make much sense, and
471 		 * should certainly be disallowed in the case of `if ;'.
472 		 */
473 		if (!redir)
474 			synexpect(-1);
475 	case TAND:
476 	case TOR:
477 	case TNL:
478 	case TEOF:
479 	case TWORD:
480 	case TRP:
481 		tokpushback++;
482 		n1 = simplecmd(rpp, redir);
483 		goto checkneg;
484 	default:
485 		synexpect(-1);
486 		/* NOTREACHED */
487 	}
488 
489 	/* Now check for redirection which may follow command */
490 	while (readtoken() == TREDIR) {
491 		*rpp = n2 = redirnode;
492 		rpp = &n2->nfile.next;
493 		parsefname();
494 	}
495 	tokpushback++;
496 	*rpp = NULL;
497 	if (redir) {
498 		if (n1->type != NSUBSHELL) {
499 			n2 = (union node *)stalloc(sizeof (struct nredir));
500 			n2->type = NREDIR;
501 			n2->nredir.n = n1;
502 			n1 = n2;
503 		}
504 		n1->nredir.redirect = redir;
505 	}
506 
507 checkneg:
508 	if (negate) {
509 		n2 = (union node *)stalloc(sizeof (struct nnot));
510 		n2->type = NNOT;
511 		n2->nnot.com = n1;
512 		return n2;
513 	}
514 	else
515 		return n1;
516 }
517 
518 
519 STATIC union node *
520 simplecmd(rpp, redir)
521 	union node **rpp, *redir;
522 	{
523 	union node *args, **app;
524 	union node **orig_rpp = rpp;
525 	union node *n = NULL, *n2;
526 	int negate = 0;
527 
528 	/* If we don't have any redirections already, then we must reset */
529 	/* rpp to be the address of the local redir variable.  */
530 	if (redir == 0)
531 		rpp = &redir;
532 
533 	args = NULL;
534 	app = &args;
535 	/*
536 	 * We save the incoming value, because we need this for shell
537 	 * functions.  There can not be a redirect or an argument between
538 	 * the function name and the open parenthesis.
539 	 */
540 	orig_rpp = rpp;
541 
542 	while (readtoken() == TNOT) {
543 		TRACE(("command: TNOT recognized\n"));
544 		negate = !negate;
545 	}
546 	tokpushback++;
547 
548 	for (;;) {
549 		if (readtoken() == TWORD) {
550 			n = (union node *)stalloc(sizeof (struct narg));
551 			n->type = NARG;
552 			n->narg.text = wordtext;
553 			n->narg.backquote = backquotelist;
554 			*app = n;
555 			app = &n->narg.next;
556 		} else if (lasttoken == TREDIR) {
557 			*rpp = n = redirnode;
558 			rpp = &n->nfile.next;
559 			parsefname();	/* read name of redirection file */
560 		} else if (lasttoken == TLP && app == &args->narg.next
561 					    && rpp == orig_rpp) {
562 			/* We have a function */
563 			if (readtoken() != TRP)
564 				synexpect(TRP);
565 #ifdef notdef
566 			if (! goodname(n->narg.text))
567 				synerror("Bad function name");
568 #endif
569 			n->type = NDEFUN;
570 			n->narg.next = command();
571 			goto checkneg;
572 		} else {
573 			tokpushback++;
574 			break;
575 		}
576 	}
577 	*app = NULL;
578 	*rpp = NULL;
579 	n = (union node *)stalloc(sizeof (struct ncmd));
580 	n->type = NCMD;
581 	n->ncmd.backgnd = 0;
582 	n->ncmd.args = args;
583 	n->ncmd.redirect = redir;
584 
585 checkneg:
586 	if (negate) {
587 		n2 = (union node *)stalloc(sizeof (struct nnot));
588 		n2->type = NNOT;
589 		n2->nnot.com = n;
590 		return n2;
591 	}
592 	else
593 		return n;
594 }
595 
596 STATIC union node *
597 makename() {
598 	union node *n;
599 
600 	n = (union node *)stalloc(sizeof (struct narg));
601 	n->type = NARG;
602 	n->narg.next = NULL;
603 	n->narg.text = wordtext;
604 	n->narg.backquote = backquotelist;
605 	return n;
606 }
607 
608 void fixredir(union node *n, const char *text, int err)
609 	{
610 	TRACE(("Fix redir %s %d\n", text, err));
611 	if (!err)
612 		n->ndup.vname = NULL;
613 
614 	if (is_digit(text[0]) && text[1] == '\0')
615 		n->ndup.dupfd = digit_val(text[0]);
616 	else if (text[0] == '-' && text[1] == '\0')
617 		n->ndup.dupfd = -1;
618 	else {
619 
620 		if (err)
621 			synerror("Bad fd number");
622 		else
623 			n->ndup.vname = makename();
624 	}
625 }
626 
627 
628 STATIC void
629 parsefname() {
630 	union node *n = redirnode;
631 
632 	if (readtoken() != TWORD)
633 		synexpect(-1);
634 	if (n->type == NHERE) {
635 		struct heredoc *here = heredoc;
636 		struct heredoc *p;
637 		int i;
638 
639 		if (quoteflag == 0)
640 			n->type = NXHERE;
641 		TRACE(("Here document %d\n", n->type));
642 		if (here->striptabs) {
643 			while (*wordtext == '\t')
644 				wordtext++;
645 		}
646 		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
647 			synerror("Illegal eof marker for << redirection");
648 		rmescapes(wordtext);
649 		here->eofmark = wordtext;
650 		here->next = NULL;
651 		if (heredoclist == NULL)
652 			heredoclist = here;
653 		else {
654 			for (p = heredoclist ; p->next ; p = p->next);
655 			p->next = here;
656 		}
657 	} else if (n->type == NTOFD || n->type == NFROMFD) {
658 		fixredir(n, wordtext, 0);
659 	} else {
660 		n->nfile.fname = makename();
661 	}
662 }
663 
664 
665 /*
666  * Input any here documents.
667  */
668 
669 STATIC void
670 parseheredoc() {
671 	struct heredoc *here;
672 	union node *n;
673 
674 	while (heredoclist) {
675 		here = heredoclist;
676 		heredoclist = here->next;
677 		if (needprompt) {
678 			setprompt(2);
679 			needprompt = 0;
680 		}
681 		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
682 				here->eofmark, here->striptabs);
683 		n = (union node *)stalloc(sizeof (struct narg));
684 		n->narg.type = NARG;
685 		n->narg.next = NULL;
686 		n->narg.text = wordtext;
687 		n->narg.backquote = backquotelist;
688 		here->here->nhere.doc = n;
689 	}
690 }
691 
692 STATIC int
693 peektoken() {
694 	int t;
695 
696 	t = readtoken();
697 	tokpushback++;
698 	return (t);
699 }
700 
701 STATIC int
702 readtoken() {
703 	int t;
704 	int savecheckkwd = checkkwd;
705 	struct alias *ap;
706 #ifdef DEBUG
707 	int alreadyseen = tokpushback;
708 #endif
709 
710 	top:
711 	t = xxreadtoken();
712 
713 	if (checkkwd) {
714 		/*
715 		 * eat newlines
716 		 */
717 		if (checkkwd == 2) {
718 			checkkwd = 0;
719 			while (t == TNL) {
720 				parseheredoc();
721 				t = xxreadtoken();
722 			}
723 		} else
724 			checkkwd = 0;
725 		/*
726 		 * check for keywords and aliases
727 		 */
728 		if (t == TWORD && !quoteflag)
729 		{
730 			const char *const *pp;
731 
732 			for (pp = parsekwd; *pp; pp++) {
733 				if (**pp == *wordtext && equal(*pp, wordtext))
734 				{
735 					lasttoken = t = pp -
736 					    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 	static const char types[] = "}-+?=";
1185 
1186 	c = pgetc();
1187 	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
1188 		USTPUTC('$', out);
1189 		pungetc();
1190 	} else if (c == '(') {	/* $(command) or $((arith)) */
1191 		if (pgetc() == '(') {
1192 			PARSEARITH();
1193 		} else {
1194 			pungetc();
1195 			PARSEBACKQNEW();
1196 		}
1197 	} else {
1198 		USTPUTC(CTLVAR, out);
1199 		typeloc = out - stackblock();
1200 		USTPUTC(VSNORMAL, out);
1201 		subtype = VSNORMAL;
1202 		if (c == '{') {
1203 			c = pgetc();
1204 			if (c == '#') {
1205 				if ((c = pgetc()) == '}')
1206 					c = '#';
1207 				else
1208 					subtype = VSLENGTH;
1209 			}
1210 			else
1211 				subtype = 0;
1212 		}
1213 		if (is_name(c)) {
1214 			do {
1215 				STPUTC(c, out);
1216 				c = pgetc();
1217 			} while (is_in_name(c));
1218 		} else if (is_digit(c)) {
1219 			do {
1220 				USTPUTC(c, out);
1221 				c = pgetc();
1222 			} while (is_digit(c));
1223 		}
1224 		else if (is_special(c)) {
1225 			USTPUTC(c, out);
1226 			c = pgetc();
1227 		}
1228 		else
1229 badsub:			synerror("Bad substitution");
1230 
1231 		STPUTC('=', out);
1232 		flags = 0;
1233 		if (subtype == 0) {
1234 			switch (c) {
1235 			case ':':
1236 				flags = VSNUL;
1237 				c = pgetc();
1238 				/*FALLTHROUGH*/
1239 			default:
1240 				p = strchr(types, c);
1241 				if (p == NULL)
1242 					goto badsub;
1243 				subtype = p - types + VSNORMAL;
1244 				break;
1245 			case '%':
1246 			case '#':
1247 				{
1248 					int cc = c;
1249 					subtype = c == '#' ? VSTRIMLEFT :
1250 							     VSTRIMRIGHT;
1251 					c = pgetc();
1252 					if (c == cc)
1253 						subtype++;
1254 					else
1255 						pungetc();
1256 					break;
1257 				}
1258 			}
1259 		} else {
1260 			pungetc();
1261 		}
1262 		if (dblquote || arinest)
1263 			flags |= VSQUOTE;
1264 		*(stackblock() + typeloc) = subtype | flags;
1265 		if (subtype != VSNORMAL)
1266 			varnest++;
1267 	}
1268 	goto parsesub_return;
1269 }
1270 
1271 
1272 /*
1273  * Called to parse command substitutions.  Newstyle is set if the command
1274  * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1275  * list of commands (passed by reference), and savelen is the number of
1276  * characters on the top of the stack which must be preserved.
1277  */
1278 
1279 parsebackq: {
1280 	struct nodelist **nlpp;
1281 	int savepbq;
1282 	union node *n;
1283 	char *volatile str;
1284 	struct jmploc jmploc;
1285 	struct jmploc *volatile savehandler;
1286 	int savelen;
1287 	int saveprompt;
1288 #ifdef __GNUC__
1289 	(void) &saveprompt;
1290 #endif
1291 
1292 	savepbq = parsebackquote;
1293 	if (setjmp(jmploc.loc)) {
1294 		if (str)
1295 			ckfree(str);
1296 		parsebackquote = 0;
1297 		handler = savehandler;
1298 		longjmp(handler->loc, 1);
1299 	}
1300 	INTOFF;
1301 	str = NULL;
1302 	savelen = out - stackblock();
1303 	if (savelen > 0) {
1304 		str = ckmalloc(savelen);
1305 		memcpy(str, stackblock(), savelen);
1306 	}
1307 	savehandler = handler;
1308 	handler = &jmploc;
1309 	INTON;
1310         if (oldstyle) {
1311                 /* We must read until the closing backquote, giving special
1312                    treatment to some slashes, and then push the string and
1313                    reread it as input, interpreting it normally.  */
1314                 char *pout;
1315                 int pc;
1316                 int psavelen;
1317                 char *pstr;
1318 
1319 
1320                 STARTSTACKSTR(pout);
1321 		for (;;) {
1322 			if (needprompt) {
1323 				setprompt(2);
1324 				needprompt = 0;
1325 			}
1326 			switch (pc = pgetc()) {
1327 			case '`':
1328 				goto done;
1329 
1330 			case '\\':
1331                                 if ((pc = pgetc()) == '\n') {
1332 					plinno++;
1333 					if (doprompt)
1334 						setprompt(2);
1335 					else
1336 						setprompt(0);
1337 					/*
1338 					 * If eating a newline, avoid putting
1339 					 * the newline into the new character
1340 					 * stream (via the STPUTC after the
1341 					 * switch).
1342 					 */
1343 					continue;
1344 				}
1345                                 if (pc != '\\' && pc != '`' && pc != '$'
1346                                     && (!dblquote || pc != '"'))
1347                                         STPUTC('\\', pout);
1348 				break;
1349 
1350 			case '\n':
1351 				plinno++;
1352 				needprompt = doprompt;
1353 				break;
1354 
1355 			case PEOF:
1356 			        startlinno = plinno;
1357 				synerror("EOF in backquote substitution");
1358  				break;
1359 
1360 			default:
1361 				break;
1362 			}
1363 			STPUTC(pc, pout);
1364                 }
1365 done:
1366                 STPUTC('\0', pout);
1367                 psavelen = pout - stackblock();
1368                 if (psavelen > 0) {
1369 			pstr = grabstackstr(pout);
1370 			setinputstring(pstr, 1);
1371                 }
1372         }
1373 	nlpp = &bqlist;
1374 	while (*nlpp)
1375 		nlpp = &(*nlpp)->next;
1376 	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1377 	(*nlpp)->next = NULL;
1378 	parsebackquote = oldstyle;
1379 
1380 	if (oldstyle) {
1381 		saveprompt = doprompt;
1382 		doprompt = 0;
1383 	}
1384 
1385 	n = list(0);
1386 
1387 	if (oldstyle)
1388 		doprompt = saveprompt;
1389 	else {
1390 		if (readtoken() != TRP)
1391 			synexpect(TRP);
1392 	}
1393 
1394 	(*nlpp)->n = n;
1395         if (oldstyle) {
1396 		/*
1397 		 * Start reading from old file again, ignoring any pushed back
1398 		 * tokens left from the backquote parsing
1399 		 */
1400                 popfile();
1401 		tokpushback = 0;
1402 	}
1403 	while (stackblocksize() <= savelen)
1404 		growstackblock();
1405 	STARTSTACKSTR(out);
1406 	if (str) {
1407 		memcpy(out, str, savelen);
1408 		STADJUST(savelen, out);
1409 		INTOFF;
1410 		ckfree(str);
1411 		str = NULL;
1412 		INTON;
1413 	}
1414 	parsebackquote = savepbq;
1415 	handler = savehandler;
1416 	if (arinest || dblquote)
1417 		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1418 	else
1419 		USTPUTC(CTLBACKQ, out);
1420 	if (oldstyle)
1421 		goto parsebackq_oldreturn;
1422 	else
1423 		goto parsebackq_newreturn;
1424 }
1425 
1426 /*
1427  * Parse an arithmetic expansion (indicate start of one and set state)
1428  */
1429 parsearith: {
1430 
1431 	if (++arinest == 1) {
1432 		prevsyntax = syntax;
1433 		syntax = ARISYNTAX;
1434 		USTPUTC(CTLARI, out);
1435 		if (dblquote)
1436 			USTPUTC('"',out);
1437 		else
1438 			USTPUTC(' ',out);
1439 	} else {
1440 		/*
1441 		 * we collapse embedded arithmetic expansion to
1442 		 * parenthesis, which should be equivalent
1443 		 */
1444 		USTPUTC('(', out);
1445 	}
1446 	goto parsearith_return;
1447 }
1448 
1449 } /* end of readtoken */
1450 
1451 
1452 
1453 #ifdef mkinit
1454 RESET {
1455 	tokpushback = 0;
1456 	checkkwd = 0;
1457 }
1458 #endif
1459 
1460 /*
1461  * Returns true if the text contains nothing to expand (no dollar signs
1462  * or backquotes).
1463  */
1464 
1465 STATIC int
1466 noexpand(text)
1467 	char *text;
1468 	{
1469 	char *p;
1470 	char c;
1471 
1472 	p = text;
1473 	while ((c = *p++) != '\0') {
1474 		if (c == CTLQUOTEMARK)
1475 			continue;
1476 		if (c == CTLESC)
1477 			p++;
1478 		else if (BASESYNTAX[(int)c] == CCTL)
1479 			return 0;
1480 	}
1481 	return 1;
1482 }
1483 
1484 
1485 /*
1486  * Return true if the argument is a legal variable name (a letter or
1487  * underscore followed by zero or more letters, underscores, and digits).
1488  */
1489 
1490 int
1491 goodname(char *name)
1492 	{
1493 	char *p;
1494 
1495 	p = name;
1496 	if (! is_name(*p))
1497 		return 0;
1498 	while (*++p) {
1499 		if (! is_in_name(*p))
1500 			return 0;
1501 	}
1502 	return 1;
1503 }
1504 
1505 
1506 /*
1507  * Called when an unexpected token is read during the parse.  The argument
1508  * is the token that is expected, or -1 if more than one type of token can
1509  * occur at this point.
1510  */
1511 
1512 STATIC void
1513 synexpect(token)
1514 	int token;
1515 {
1516 	char msg[64];
1517 
1518 	if (token >= 0) {
1519 		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1520 			tokname[lasttoken], tokname[token]);
1521 	} else {
1522 		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1523 	}
1524 	synerror(msg);
1525 	/* NOTREACHED */
1526 }
1527 
1528 
1529 STATIC void
1530 synerror(msg)
1531 	const char *msg;
1532 	{
1533 	if (commandname)
1534 		outfmt(&errout, "%s: %d: ", commandname, startlinno);
1535 	outfmt(&errout, "Syntax error: %s\n", msg);
1536 	error((char *)NULL);
1537 	/* NOTREACHED */
1538 }
1539 
1540 STATIC void
1541 setprompt(which)
1542 	int which;
1543 	{
1544 	whichprompt = which;
1545 
1546 #ifndef SMALL
1547 	if (!el)
1548 #endif
1549 		out2str(getprompt(NULL));
1550 }
1551 
1552 /*
1553  * called by editline -- any expansions to the prompt
1554  *    should be added here.
1555  */
1556 const char *
1557 getprompt(void *unused)
1558 	{
1559 	switch (whichprompt) {
1560 	case 0:
1561 		return "";
1562 	case 1:
1563 		return ps1val();
1564 	case 2:
1565 		return ps2val();
1566 	default:
1567 		return "<internal prompt error>";
1568 	}
1569 }
1570