d263cb1ae6b8a84f48a4b381cac9f2784cb6b690
[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.7 2006/09/28 04:19:40 pavalos 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
618 fixredir(union node *n, const char *text, int err)
619 {
620         TRACE(("Fix redir %s %d\n", text, err));
621         if (!err)
622                 n->ndup.vname = NULL;
623
624         if (is_digit(text[0]) && text[1] == '\0')
625                 n->ndup.dupfd = digit_val(text[0]);
626         else if (text[0] == '-' && text[1] == '\0')
627                 n->ndup.dupfd = -1;
628         else {
629
630                 if (err)
631                         synerror("Bad fd number");
632                 else
633                         n->ndup.vname = makename();
634         }
635 }
636
637
638 STATIC void
639 parsefname(void)
640 {
641         union node *n = redirnode;
642
643         if (readtoken() != TWORD)
644                 synexpect(-1);
645         if (n->type == NHERE) {
646                 struct heredoc *here = heredoc;
647                 struct heredoc *p;
648                 int i;
649
650                 if (quoteflag == 0)
651                         n->type = NXHERE;
652                 TRACE(("Here document %d\n", n->type));
653                 if (here->striptabs) {
654                         while (*wordtext == '\t')
655                                 wordtext++;
656                 }
657                 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
658                         synerror("Illegal eof marker for << redirection");
659                 rmescapes(wordtext);
660                 here->eofmark = wordtext;
661                 here->next = NULL;
662                 if (heredoclist == NULL)
663                         heredoclist = here;
664                 else {
665                         for (p = heredoclist ; p->next ; p = p->next);
666                         p->next = here;
667                 }
668         } else if (n->type == NTOFD || n->type == NFROMFD) {
669                 fixredir(n, wordtext, 0);
670         } else {
671                 n->nfile.fname = makename();
672         }
673 }
674
675
676 /*
677  * Input any here documents.
678  */
679
680 STATIC void
681 parseheredoc(void)
682 {
683         struct heredoc *here;
684         union node *n;
685
686         while (heredoclist) {
687                 here = heredoclist;
688                 heredoclist = here->next;
689                 if (needprompt) {
690                         setprompt(2);
691                         needprompt = 0;
692                 }
693                 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
694                                 here->eofmark, here->striptabs);
695                 n = (union node *)stalloc(sizeof (struct narg));
696                 n->narg.type = NARG;
697                 n->narg.next = NULL;
698                 n->narg.text = wordtext;
699                 n->narg.backquote = backquotelist;
700                 here->here->nhere.doc = n;
701         }
702 }
703
704 STATIC int
705 peektoken(void)
706 {
707         int t;
708
709         t = readtoken();
710         tokpushback++;
711         return (t);
712 }
713
714 STATIC int
715 readtoken(void)
716 {
717         int t;
718         int savecheckkwd = checkkwd;
719         struct alias *ap;
720 #ifdef DEBUG
721         int alreadyseen = tokpushback;
722 #endif
723
724         top:
725         t = xxreadtoken();
726
727         if (checkkwd) {
728                 /*
729                  * eat newlines
730                  */
731                 if (checkkwd == 2) {
732                         checkkwd = 0;
733                         while (t == TNL) {
734                                 parseheredoc();
735                                 t = xxreadtoken();
736                         }
737                 } else
738                         checkkwd = 0;
739                 /*
740                  * check for keywords and aliases
741                  */
742                 if (t == TWORD && !quoteflag)
743                 {
744                         const char * const *pp;
745
746                         for (pp = parsekwd; *pp; pp++) {
747                                 if (**pp == *wordtext && equal(*pp, wordtext))
748                                 {
749                                         lasttoken = t = pp - parsekwd + KWDOFFSET;
750                                         TRACE(("keyword %s recognized\n", tokname[t]));
751                                         goto out;
752                                 }
753                         }
754                         if (noaliases == 0 &&
755                             (ap = lookupalias(wordtext, 1)) != NULL) {
756                                 pushstring(ap->val, strlen(ap->val), ap);
757                                 checkkwd = savecheckkwd;
758                                 goto top;
759                         }
760                 }
761 out:
762                 checkkwd = (t == TNOT) ? savecheckkwd : 0;
763         }
764 #ifdef DEBUG
765         if (!alreadyseen)
766             TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
767         else
768             TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
769 #endif
770         return (t);
771 }
772
773
774 /*
775  * Read the next input token.
776  * If the token is a word, we set backquotelist to the list of cmds in
777  *      backquotes.  We set quoteflag to true if any part of the word was
778  *      quoted.
779  * If the token is TREDIR, then we set redirnode to a structure containing
780  *      the redirection.
781  * In all cases, the variable startlinno is set to the number of the line
782  *      on which the token starts.
783  *
784  * [Change comment:  here documents and internal procedures]
785  * [Readtoken shouldn't have any arguments.  Perhaps we should make the
786  *  word parsing code into a separate routine.  In this case, readtoken
787  *  doesn't need to have any internal procedures, but parseword does.
788  *  We could also make parseoperator in essence the main routine, and
789  *  have parseword (readtoken1?) handle both words and redirection.]
790  */
791
792 #define RETURN(token)   return lasttoken = token
793
794 STATIC int
795 xxreadtoken(void)
796 {
797         int c;
798
799         if (tokpushback) {
800                 tokpushback = 0;
801                 return lasttoken;
802         }
803         if (needprompt) {
804                 setprompt(2);
805                 needprompt = 0;
806         }
807         startlinno = plinno;
808         for (;;) {      /* until token or start of word found */
809                 c = pgetc_macro();
810                 if (c == ' ' || c == '\t')
811                         continue;               /* quick check for white space first */
812                 switch (c) {
813                 case ' ': case '\t':
814                         continue;
815                 case '#':
816                         while ((c = pgetc()) != '\n' && c != PEOF);
817                         pungetc();
818                         continue;
819                 case '\\':
820                         if (pgetc() == '\n') {
821                                 startlinno = ++plinno;
822                                 if (doprompt)
823                                         setprompt(2);
824                                 else
825                                         setprompt(0);
826                                 continue;
827                         }
828                         pungetc();
829                         goto breakloop;
830                 case '\n':
831                         plinno++;
832                         needprompt = doprompt;
833                         RETURN(TNL);
834                 case PEOF:
835                         RETURN(TEOF);
836                 case '&':
837                         if (pgetc() == '&')
838                                 RETURN(TAND);
839                         pungetc();
840                         RETURN(TBACKGND);
841                 case '|':
842                         if (pgetc() == '|')
843                                 RETURN(TOR);
844                         pungetc();
845                         RETURN(TPIPE);
846                 case ';':
847                         if (pgetc() == ';')
848                                 RETURN(TENDCASE);
849                         pungetc();
850                         RETURN(TSEMI);
851                 case '(':
852                         RETURN(TLP);
853                 case ')':
854                         RETURN(TRP);
855                 default:
856                         goto breakloop;
857                 }
858         }
859 breakloop:
860         return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
861 #undef RETURN
862 }
863
864
865
866 /*
867  * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
868  * is not NULL, read a here document.  In the latter case, eofmark is the
869  * word which marks the end of the document and striptabs is true if
870  * leading tabs should be stripped from the document.  The argument firstc
871  * is the first character of the input token or document.
872  *
873  * Because C does not have internal subroutines, I have simulated them
874  * using goto's to implement the subroutine linkage.  The following macros
875  * will run code that appears at the end of readtoken1.
876  */
877
878 #define CHECKEND()      {goto checkend; checkend_return:;}
879 #define PARSEREDIR()    {goto parseredir; parseredir_return:;}
880 #define PARSESUB()      {goto parsesub; parsesub_return:;}
881 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
882 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
883 #define PARSEARITH()    {goto parsearith; parsearith_return:;}
884
885 STATIC int
886 readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
887 {
888         int c = firstc;
889         char *out;
890         int len;
891         char line[EOFMARKLEN + 1];
892         struct nodelist *bqlist;
893         int quotef;
894         int dblquote;
895         int varnest;    /* levels of variables expansion */
896         int arinest;    /* levels of arithmetic expansion */
897         int parenlevel; /* levels of parens in arithmetic */
898         int oldstyle;
899         char const *prevsyntax; /* syntax before arithmetic */
900         int synentry;
901 #if __GNUC__
902         /* Avoid longjmp clobbering */
903         (void) &out;
904         (void) &quotef;
905         (void) &dblquote;
906         (void) &varnest;
907         (void) &arinest;
908         (void) &parenlevel;
909         (void) &oldstyle;
910         (void) &prevsyntax;
911         (void) &syntax;
912         (void) &synentry;
913 #endif
914
915         startlinno = plinno;
916         dblquote = 0;
917         if (syntax == DQSYNTAX)
918                 dblquote = 1;
919         quotef = 0;
920         bqlist = NULL;
921         varnest = 0;
922         arinest = 0;
923         parenlevel = 0;
924
925         STARTSTACKSTR(out);
926         loop: { /* for each line, until end of word */
927                 CHECKEND();     /* set c to PEOF if at end of here document */
928                 for (;;) {      /* until end of line or end of word */
929                         CHECKSTRSPACE(3, out);  /* permit 3 calls to USTPUTC */
930
931                         synentry = syntax[c];
932
933                         switch(synentry) {
934                         case CNL:       /* '\n' */
935                                 if (syntax == BASESYNTAX)
936                                         goto endword;   /* exit outer loop */
937                                 USTPUTC(c, out);
938                                 plinno++;
939                                 if (doprompt)
940                                         setprompt(2);
941                                 else
942                                         setprompt(0);
943                                 c = pgetc();
944                                 goto loop;              /* continue outer loop */
945                         case CWORD:
946                                 USTPUTC(c, out);
947                                 break;
948                         case CCTL:
949                                 if (eofmark == NULL || dblquote)
950                                         USTPUTC(CTLESC, out);
951                                 USTPUTC(c, out);
952                                 break;
953                         case CBACK:     /* backslash */
954                                 c = pgetc();
955                                 if (c == PEOF) {
956                                         USTPUTC('\\', out);
957                                         pungetc();
958                                 } else if (c == '\n') {
959                                         if (doprompt)
960                                                 setprompt(2);
961                                         else
962                                                 setprompt(0);
963                                 } else {
964                                         if (dblquote && c != '\\' &&
965                                             c != '`' && c != '$' &&
966                                             (c != '"' || eofmark != NULL))
967                                                 USTPUTC('\\', out);
968                                         if (SQSYNTAX[c] == CCTL)
969                                                 USTPUTC(CTLESC, out);
970                                         else if (eofmark == NULL)
971                                                 USTPUTC(CTLQUOTEMARK, out);
972                                         USTPUTC(c, out);
973                                         quotef++;
974                                 }
975                                 break;
976                         case CSQUOTE:
977                                 if (eofmark == NULL)
978                                         USTPUTC(CTLQUOTEMARK, out);
979                                 syntax = SQSYNTAX;
980                                 break;
981                         case CDQUOTE:
982                                 if (eofmark == NULL)
983                                         USTPUTC(CTLQUOTEMARK, out);
984                                 syntax = DQSYNTAX;
985                                 dblquote = 1;
986                                 break;
987                         case CENDQUOTE:
988                                 if (eofmark != NULL && arinest == 0 &&
989                                     varnest == 0) {
990                                         USTPUTC(c, out);
991                                 } else {
992                                         if (arinest) {
993                                                 syntax = ARISYNTAX;
994                                                 dblquote = 0;
995                                         } else if (eofmark == NULL) {
996                                                 syntax = BASESYNTAX;
997                                                 dblquote = 0;
998                                         }
999                                         quotef++;
1000                                 }
1001                                 break;
1002                         case CVAR:      /* '$' */
1003                                 PARSESUB();             /* parse substitution */
1004                                 break;
1005                         case CENDVAR:   /* '}' */
1006                                 if (varnest > 0) {
1007                                         varnest--;
1008                                         USTPUTC(CTLENDVAR, out);
1009                                 } else {
1010                                         USTPUTC(c, out);
1011                                 }
1012                                 break;
1013                         case CLP:       /* '(' in arithmetic */
1014                                 parenlevel++;
1015                                 USTPUTC(c, out);
1016                                 break;
1017                         case CRP:       /* ')' in arithmetic */
1018                                 if (parenlevel > 0) {
1019                                         USTPUTC(c, out);
1020                                         --parenlevel;
1021                                 } else {
1022                                         if (pgetc() == ')') {
1023                                                 if (--arinest == 0) {
1024                                                         USTPUTC(CTLENDARI, out);
1025                                                         syntax = prevsyntax;
1026                                                         if (syntax == DQSYNTAX)
1027                                                                 dblquote = 1;
1028                                                         else
1029                                                                 dblquote = 0;
1030                                                 } else
1031                                                         USTPUTC(')', out);
1032                                         } else {
1033                                                 /*
1034                                                  * unbalanced parens
1035                                                  *  (don't 2nd guess - no error)
1036                                                  */
1037                                                 pungetc();
1038                                                 USTPUTC(')', out);
1039                                         }
1040                                 }
1041                                 break;
1042                         case CBQUOTE:   /* '`' */
1043                                 PARSEBACKQOLD();
1044                                 break;
1045                         case CEOF:
1046                                 goto endword;           /* exit outer loop */
1047                         default:
1048                                 if (varnest == 0)
1049                                         goto endword;   /* exit outer loop */
1050                                 USTPUTC(c, out);
1051                         }
1052                         c = pgetc_macro();
1053                 }
1054         }
1055 endword:
1056         if (syntax == ARISYNTAX)
1057                 synerror("Missing '))'");
1058         if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1059                 synerror("Unterminated quoted string");
1060         if (varnest != 0) {
1061                 startlinno = plinno;
1062                 synerror("Missing '}'");
1063         }
1064         USTPUTC('\0', out);
1065         len = out - stackblock();
1066         out = stackblock();
1067         if (eofmark == NULL) {
1068                 if ((c == '>' || c == '<')
1069                  && quotef == 0
1070                  && len <= 2
1071                  && (*out == '\0' || is_digit(*out))) {
1072                         PARSEREDIR();
1073                         return lasttoken = TREDIR;
1074                 } else {
1075                         pungetc();
1076                 }
1077         }
1078         quoteflag = quotef;
1079         backquotelist = bqlist;
1080         grabstackblock(len);
1081         wordtext = out;
1082         return lasttoken = TWORD;
1083 /* end of readtoken routine */
1084
1085
1086
1087 /*
1088  * Check to see whether we are at the end of the here document.  When this
1089  * is called, c is set to the first character of the next input line.  If
1090  * we are at the end of the here document, this routine sets the c to PEOF.
1091  */
1092
1093 checkend: {
1094         if (eofmark) {
1095                 if (striptabs) {
1096                         while (c == '\t')
1097                                 c = pgetc();
1098                 }
1099                 if (c == *eofmark) {
1100                         if (pfgets(line, sizeof line) != NULL) {
1101                                 char *p, *q;
1102
1103                                 p = line;
1104                                 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1105                                 if (*p == '\n' && *q == '\0') {
1106                                         c = PEOF;
1107                                         plinno++;
1108                                         needprompt = doprompt;
1109                                 } else {
1110                                         pushstring(line, strlen(line), NULL);
1111                                 }
1112                         }
1113                 }
1114         }
1115         goto checkend_return;
1116 }
1117
1118
1119 /*
1120  * Parse a redirection operator.  The variable "out" points to a string
1121  * specifying the fd to be redirected.  The variable "c" contains the
1122  * first character of the redirection operator.
1123  */
1124
1125 parseredir: {
1126         char fd = *out;
1127         union node *np;
1128
1129         np = (union node *)stalloc(sizeof (struct nfile));
1130         if (c == '>') {
1131                 np->nfile.fd = 1;
1132                 c = pgetc();
1133                 if (c == '>')
1134                         np->type = NAPPEND;
1135                 else if (c == '&')
1136                         np->type = NTOFD;
1137                 else if (c == '|')
1138                         np->type = NCLOBBER;
1139                 else {
1140                         np->type = NTO;
1141                         pungetc();
1142                 }
1143         } else {        /* c == '<' */
1144                 np->nfile.fd = 0;
1145                 c = pgetc();
1146                 if (c == '<') {
1147                         if (sizeof (struct nfile) != sizeof (struct nhere)) {
1148                                 np = (union node *)stalloc(sizeof (struct nhere));
1149                                 np->nfile.fd = 0;
1150                         }
1151                         np->type = NHERE;
1152                         heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1153                         heredoc->here = np;
1154                         if ((c = pgetc()) == '-') {
1155                                 heredoc->striptabs = 1;
1156                         } else {
1157                                 heredoc->striptabs = 0;
1158                                 pungetc();
1159                         }
1160                 } else if (c == '&')
1161                         np->type = NFROMFD;
1162                 else if (c == '>')
1163                         np->type = NFROMTO;
1164                 else {
1165                         np->type = NFROM;
1166                         pungetc();
1167                 }
1168         }
1169         if (fd != '\0')
1170                 np->nfile.fd = digit_val(fd);
1171         redirnode = np;
1172         goto parseredir_return;
1173 }
1174
1175
1176 /*
1177  * Parse a substitution.  At this point, we have read the dollar sign
1178  * and nothing else.
1179  */
1180
1181 parsesub: {
1182         int subtype;
1183         int typeloc;
1184         int flags;
1185         char *p;
1186 #ifndef GDB_HACK
1187         static const char types[] = "}-+?=";
1188 #endif
1189        int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1190
1191         c = pgetc();
1192         if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1193             !is_special(c)) {
1194                 USTPUTC('$', out);
1195                 pungetc();
1196         } else if (c == '(') {  /* $(command) or $((arith)) */
1197                 if (pgetc() == '(') {
1198                         PARSEARITH();
1199                 } else {
1200                         pungetc();
1201                         PARSEBACKQNEW();
1202                 }
1203         } else {
1204                 USTPUTC(CTLVAR, out);
1205                 typeloc = out - stackblock();
1206                 USTPUTC(VSNORMAL, out);
1207                 subtype = VSNORMAL;
1208                 if (c == '{') {
1209                         bracketed_name = 1;
1210                         c = pgetc();
1211                         if (c == '#') {
1212                                 if ((c = pgetc()) == '}')
1213                                         c = '#';
1214                                 else
1215                                         subtype = VSLENGTH;
1216                         }
1217                         else
1218                                 subtype = 0;
1219                 }
1220                 if (!is_eof(c) && is_name(c)) {
1221                         do {
1222                                 STPUTC(c, out);
1223                                 c = pgetc();
1224                         } while (!is_eof(c) && is_in_name(c));
1225                 } else if (is_digit(c)) {
1226                         if (bracketed_name) {
1227                                 do {
1228                                         STPUTC(c, out);
1229                                         c = pgetc();
1230                                 } while (is_digit(c));
1231                         } else {
1232                                 STPUTC(c, out);
1233                                 c = pgetc();
1234                         }
1235                 } else {
1236                         if (! is_special(c))
1237 badsub:                         synerror("Bad substitution");
1238                         USTPUTC(c, out);
1239                         c = pgetc();
1240                 }
1241                 STPUTC('=', out);
1242                 flags = 0;
1243                 if (subtype == 0) {
1244                         switch (c) {
1245                         case ':':
1246                                 flags = VSNUL;
1247                                 c = pgetc();
1248                                 /*FALLTHROUGH*/
1249                         default:
1250                                 p = strchr(types, c);
1251                                 if (p == NULL)
1252                                         goto badsub;
1253                                 subtype = p - types + VSNORMAL;
1254                                 break;
1255                         case '%':
1256                         case '#':
1257                                 {
1258                                         int cc = c;
1259                                         subtype = c == '#' ? VSTRIMLEFT :
1260                                                              VSTRIMRIGHT;
1261                                         c = pgetc();
1262                                         if (c == cc)
1263                                                 subtype++;
1264                                         else
1265                                                 pungetc();
1266                                         break;
1267                                 }
1268                         }
1269                 } else {
1270                         pungetc();
1271                 }
1272                 if (subtype != VSLENGTH && (dblquote || arinest))
1273                         flags |= VSQUOTE;
1274                 *(stackblock() + typeloc) = subtype | flags;
1275                 if (subtype != VSNORMAL)
1276                         varnest++;
1277         }
1278         goto parsesub_return;
1279 }
1280
1281
1282 /*
1283  * Called to parse command substitutions.  Newstyle is set if the command
1284  * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1285  * list of commands (passed by reference), and savelen is the number of
1286  * characters on the top of the stack which must be preserved.
1287  */
1288
1289 parsebackq: {
1290         struct nodelist **nlpp;
1291         int savepbq;
1292         union node *n;
1293         char *volatile str;
1294         struct jmploc jmploc;
1295         struct jmploc *volatile savehandler;
1296         int savelen;
1297         int saveprompt;
1298 #if __GNUC__
1299         /* Avoid longjmp clobbering */
1300         (void) &saveprompt;
1301 #endif
1302
1303         savepbq = parsebackquote;
1304         if (setjmp(jmploc.loc)) {
1305                 if (str)
1306                         ckfree(str);
1307                 parsebackquote = 0;
1308                 handler = savehandler;
1309                 longjmp(handler->loc, 1);
1310         }
1311         INTOFF;
1312         str = NULL;
1313         savelen = out - stackblock();
1314         if (savelen > 0) {
1315                 str = ckmalloc(savelen);
1316                 memcpy(str, stackblock(), savelen);
1317         }
1318         savehandler = handler;
1319         handler = &jmploc;
1320         INTON;
1321         if (oldstyle) {
1322                 /* We must read until the closing backquote, giving special
1323                    treatment to some slashes, and then push the string and
1324                    reread it as input, interpreting it normally.  */
1325                 char *out;
1326                 int c;
1327                 int savelen;
1328                 char *str;
1329
1330
1331                 STARTSTACKSTR(out);
1332                 for (;;) {
1333                         if (needprompt) {
1334                                 setprompt(2);
1335                                 needprompt = 0;
1336                         }
1337                         switch (c = pgetc()) {
1338                         case '`':
1339                                 goto done;
1340
1341                         case '\\':
1342                                 if ((c = pgetc()) == '\n') {
1343                                         plinno++;
1344                                         if (doprompt)
1345                                                 setprompt(2);
1346                                         else
1347                                                 setprompt(0);
1348                                         /*
1349                                          * If eating a newline, avoid putting
1350                                          * the newline into the new character
1351                                          * stream (via the STPUTC after the
1352                                          * switch).
1353                                          */
1354                                         continue;
1355                                 }
1356                                 if (c != '\\' && c != '`' && c != '$'
1357                                     && (!dblquote || c != '"'))
1358                                         STPUTC('\\', out);
1359                                 break;
1360
1361                         case '\n':
1362                                 plinno++;
1363                                 needprompt = doprompt;
1364                                 break;
1365
1366                         case PEOF:
1367                                 startlinno = plinno;
1368                                 synerror("EOF in backquote substitution");
1369                                 break;
1370
1371                         default:
1372                                 break;
1373                         }
1374                         STPUTC(c, out);
1375                 }
1376 done:
1377                 STPUTC('\0', out);
1378                 savelen = out - stackblock();
1379                 if (savelen > 0) {
1380                         str = ckmalloc(savelen);
1381                         memcpy(str, stackblock(), savelen);
1382                         setinputstring(str, 1);
1383                 }
1384         }
1385         nlpp = &bqlist;
1386         while (*nlpp)
1387                 nlpp = &(*nlpp)->next;
1388         *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1389         (*nlpp)->next = NULL;
1390         parsebackquote = oldstyle;
1391
1392         if (oldstyle) {
1393                 saveprompt = doprompt;
1394                 doprompt = 0;
1395         }
1396
1397         n = list(0);
1398
1399         if (oldstyle)
1400                 doprompt = saveprompt;
1401         else {
1402                 if (readtoken() != TRP)
1403                         synexpect(TRP);
1404         }
1405
1406         (*nlpp)->n = n;
1407         if (oldstyle) {
1408                 /*
1409                  * Start reading from old file again, ignoring any pushed back
1410                  * tokens left from the backquote parsing
1411                  */
1412                 popfile();
1413                 tokpushback = 0;
1414         }
1415         while (stackblocksize() <= savelen)
1416                 growstackblock();
1417         STARTSTACKSTR(out);
1418         if (str) {
1419                 memcpy(out, str, savelen);
1420                 STADJUST(savelen, out);
1421                 INTOFF;
1422                 ckfree(str);
1423                 str = NULL;
1424                 INTON;
1425         }
1426         parsebackquote = savepbq;
1427         handler = savehandler;
1428         if (arinest || dblquote)
1429                 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1430         else
1431                 USTPUTC(CTLBACKQ, out);
1432         if (oldstyle)
1433                 goto parsebackq_oldreturn;
1434         else
1435                 goto parsebackq_newreturn;
1436 }
1437
1438 /*
1439  * Parse an arithmetic expansion (indicate start of one and set state)
1440  */
1441 parsearith: {
1442
1443         if (++arinest == 1) {
1444                 prevsyntax = syntax;
1445                 syntax = ARISYNTAX;
1446                 USTPUTC(CTLARI, out);
1447                 if (dblquote)
1448                         USTPUTC('"',out);
1449                 else
1450                         USTPUTC(' ',out);
1451         } else {
1452                 /*
1453                  * we collapse embedded arithmetic expansion to
1454                  * parenthesis, which should be equivalent
1455                  */
1456                 USTPUTC('(', out);
1457         }
1458         goto parsearith_return;
1459 }
1460
1461 } /* end of readtoken */
1462
1463
1464
1465 #ifdef mkinit
1466 RESET {
1467         tokpushback = 0;
1468         checkkwd = 0;
1469 }
1470 #endif
1471
1472 /*
1473  * Returns true if the text contains nothing to expand (no dollar signs
1474  * or backquotes).
1475  */
1476
1477 STATIC int
1478 noexpand(char *text)
1479 {
1480         char *p;
1481         char c;
1482
1483         p = text;
1484         while ((c = *p++) != '\0') {
1485                 if ( c == CTLQUOTEMARK)
1486                         continue;
1487                 if (c == CTLESC)
1488                         p++;
1489                 else if (BASESYNTAX[(int)c] == CCTL)
1490                         return 0;
1491         }
1492         return 1;
1493 }
1494
1495
1496 /*
1497  * Return true if the argument is a legal variable name (a letter or
1498  * underscore followed by zero or more letters, underscores, and digits).
1499  */
1500
1501 int
1502 goodname(char *name)
1503 {
1504         char *p;
1505
1506         p = name;
1507         if (! is_name(*p))
1508                 return 0;
1509         while (*++p) {
1510                 if (! is_in_name(*p))
1511                         return 0;
1512         }
1513         return 1;
1514 }
1515
1516
1517 /*
1518  * Called when an unexpected token is read during the parse.  The argument
1519  * is the token that is expected, or -1 if more than one type of token can
1520  * occur at this point.
1521  */
1522
1523 STATIC void
1524 synexpect(int token)
1525 {
1526         char msg[64];
1527
1528         if (token >= 0) {
1529                 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1530                         tokname[lasttoken], tokname[token]);
1531         } else {
1532                 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1533         }
1534         synerror(msg);
1535 }
1536
1537
1538 STATIC void
1539 synerror(const char *msg)
1540 {
1541         if (commandname)
1542                 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1543         outfmt(&errout, "Syntax error: %s\n", msg);
1544         error((char *)NULL);
1545 }
1546
1547 STATIC void
1548 setprompt(int which)
1549 {
1550         whichprompt = which;
1551
1552 #ifndef NO_HISTORY
1553         if (!el)
1554 #endif
1555                 out2str(getprompt(NULL));
1556 }
1557
1558 /*
1559  * called by editline -- any expansions to the prompt
1560  *    should be added here.
1561  */
1562 const char *
1563 getprompt(void *unused __unused)
1564 {
1565         switch (whichprompt) {
1566         case 0:
1567                 return "";
1568         case 1:
1569                 return ps1val();
1570         case 2:
1571                 return ps2val();
1572         default:
1573                 return "<internal prompt error>";
1574         }
1575 }