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