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