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