Merge branch 'vendor/TNFTP'
[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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)parser.c    8.7 (Berkeley) 5/16/95";
36 #endif
37 #endif /* not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <stdio.h>
44
45 #include "shell.h"
46 #include "parser.h"
47 #include "nodes.h"
48 #include "expand.h"     /* defines rmescapes() */
49 #include "syntax.h"
50 #include "options.h"
51 #include "input.h"
52 #include "output.h"
53 #include "var.h"
54 #include "error.h"
55 #include "memalloc.h"
56 #include "mystring.h"
57 #include "alias.h"
58 #include "show.h"
59 #include "eval.h"
60 #include "exec.h"       /* to check for special builtins */
61 #ifndef NO_HISTORY
62 #include "myhistedit.h"
63 #endif
64
65 /*
66  * Shell command parser.
67  */
68
69 #define PROMPTLEN       128
70
71 /* values of checkkwd variable */
72 #define CHKALIAS        0x1
73 #define CHKKWD          0x2
74 #define CHKNL           0x4
75
76 /* values returned by readtoken */
77 #include "token.h"
78
79
80
81 struct heredoc {
82         struct heredoc *next;   /* next here document in list */
83         union node *here;               /* redirection node */
84         char *eofmark;          /* string indicating end of input */
85         int striptabs;          /* if set, strip leading tabs */
86 };
87
88 struct parser_temp {
89         struct parser_temp *next;
90         void *data;
91 };
92
93
94 static struct heredoc *heredoclist;     /* list of here documents to read */
95 static int doprompt;            /* if set, prompt the user */
96 static int needprompt;          /* true if interactive and at start of line */
97 static int lasttoken;           /* last token read */
98 static int tokpushback;         /* last token pushed back */
99 static char *wordtext;          /* text of last word returned by readtoken */
100 static int checkkwd;
101 static struct nodelist *backquotelist;
102 static union node *redirnode;
103 static struct heredoc *heredoc;
104 static int quoteflag;           /* set if (part of) last token was quoted */
105 static int startlinno;          /* line # where last token started */
106 static int funclinno;           /* line # where the current function started */
107 static struct parser_temp *parser_temp;
108
109
110 static union node *list(int);
111 static union node *andor(void);
112 static union node *pipeline(void);
113 static union node *command(void);
114 static union node *simplecmd(union node **, union node *);
115 static union node *makename(void);
116 static union node *makebinary(int type, union node *n1, union node *n2);
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, const char *, const char *, int);
123 static int noexpand(char *);
124 static void consumetoken(int);
125 static void synexpect(int) __dead2;
126 static void synerror(const char *) __dead2;
127 static void setprompt(int);
128 static int pgetc_linecont(void);
129
130
131 static void *
132 parser_temp_alloc(size_t len)
133 {
134         struct parser_temp *t;
135
136         INTOFF;
137         t = ckmalloc(sizeof(*t));
138         t->data = NULL;
139         t->next = parser_temp;
140         parser_temp = t;
141         t->data = ckmalloc(len);
142         INTON;
143         return t->data;
144 }
145
146
147 static void *
148 parser_temp_realloc(void *ptr, size_t len)
149 {
150         struct parser_temp *t;
151
152         INTOFF;
153         t = parser_temp;
154         if (ptr != t->data)
155                 error("bug: parser_temp_realloc misused");
156         t->data = ckrealloc(t->data, len);
157         INTON;
158         return t->data;
159 }
160
161
162 static void
163 parser_temp_free_upto(void *ptr)
164 {
165         struct parser_temp *t;
166         int done = 0;
167
168         INTOFF;
169         while (parser_temp != NULL && !done) {
170                 t = parser_temp;
171                 parser_temp = t->next;
172                 done = t->data == ptr;
173                 ckfree(t->data);
174                 ckfree(t);
175         }
176         INTON;
177         if (!done)
178                 error("bug: parser_temp_free_upto misused");
179 }
180
181
182 static void
183 parser_temp_free_all(void)
184 {
185         struct parser_temp *t;
186
187         INTOFF;
188         while (parser_temp != NULL) {
189                 t = parser_temp;
190                 parser_temp = t->next;
191                 ckfree(t->data);
192                 ckfree(t);
193         }
194         INTON;
195 }
196
197
198 /*
199  * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
200  * valid parse tree indicating a blank line.)
201  */
202
203 union node *
204 parsecmd(int interact)
205 {
206         int t;
207
208         /* This assumes the parser is not re-entered,
209          * which could happen if we add command substitution on PS1/PS2.
210          */
211         parser_temp_free_all();
212         heredoclist = NULL;
213
214         tokpushback = 0;
215         checkkwd = 0;
216         doprompt = interact;
217         if (doprompt)
218                 setprompt(1);
219         else
220                 setprompt(0);
221         needprompt = 0;
222         t = readtoken();
223         if (t == TEOF)
224                 return NEOF;
225         if (t == TNL)
226                 return NULL;
227         tokpushback++;
228         return list(1);
229 }
230
231
232 static union node *
233 list(int nlflag)
234 {
235         union node *ntop, *n1, *n2, *n3;
236         int tok;
237
238         checkkwd = CHKNL | CHKKWD | CHKALIAS;
239         if (!nlflag && tokendlist[peektoken()])
240                 return NULL;
241         ntop = n1 = NULL;
242         for (;;) {
243                 n2 = andor();
244                 tok = readtoken();
245                 if (tok == TBACKGND) {
246                         if (n2 != NULL && n2->type == NPIPE) {
247                                 n2->npipe.backgnd = 1;
248                         } else if (n2 != NULL && n2->type == NREDIR) {
249                                 n2->type = NBACKGND;
250                         } else {
251                                 n3 = (union node *)stalloc(sizeof (struct nredir));
252                                 n3->type = NBACKGND;
253                                 n3->nredir.n = n2;
254                                 n3->nredir.redirect = NULL;
255                                 n2 = n3;
256                         }
257                 }
258                 if (ntop == NULL)
259                         ntop = n2;
260                 else if (n1 == NULL) {
261                         n1 = makebinary(NSEMI, ntop, n2);
262                         ntop = n1;
263                 }
264                 else {
265                         n3 = makebinary(NSEMI, n1->nbinary.ch2, n2);
266                         n1->nbinary.ch2 = n3;
267                         n1 = n3;
268                 }
269                 switch (tok) {
270                 case TBACKGND:
271                 case TSEMI:
272                         tok = readtoken();
273                         /* FALLTHROUGH */
274                 case TNL:
275                         if (tok == TNL) {
276                                 parseheredoc();
277                                 if (nlflag)
278                                         return ntop;
279                         } else if (tok == TEOF && nlflag) {
280                                 parseheredoc();
281                                 return ntop;
282                         } else {
283                                 tokpushback++;
284                         }
285                         checkkwd = CHKNL | CHKKWD | CHKALIAS;
286                         if (!nlflag && tokendlist[peektoken()])
287                                 return ntop;
288                         break;
289                 case TEOF:
290                         if (heredoclist)
291                                 parseheredoc();
292                         else
293                                 pungetc();              /* push back EOF on input */
294                         return ntop;
295                 default:
296                         if (nlflag)
297                                 synexpect(-1);
298                         tokpushback++;
299                         return ntop;
300                 }
301         }
302 }
303
304
305
306 static union node *
307 andor(void)
308 {
309         union node *n;
310         int t;
311
312         n = pipeline();
313         for (;;) {
314                 if ((t = readtoken()) == TAND) {
315                         t = NAND;
316                 } else if (t == TOR) {
317                         t = NOR;
318                 } else {
319                         tokpushback++;
320                         return n;
321                 }
322                 n = makebinary(t, n, pipeline());
323         }
324 }
325
326
327
328 static union node *
329 pipeline(void)
330 {
331         union node *n1, *n2, *pipenode;
332         struct nodelist *lp, *prev;
333         int negate, t;
334
335         negate = 0;
336         checkkwd = CHKNL | CHKKWD | CHKALIAS;
337         TRACE(("pipeline: entered\n"));
338         while (readtoken() == TNOT)
339                 negate = !negate;
340         tokpushback++;
341         n1 = command();
342         if (readtoken() == TPIPE) {
343                 pipenode = (union node *)stalloc(sizeof (struct npipe));
344                 pipenode->type = NPIPE;
345                 pipenode->npipe.backgnd = 0;
346                 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
347                 pipenode->npipe.cmdlist = lp;
348                 lp->n = n1;
349                 do {
350                         prev = lp;
351                         lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
352                         checkkwd = CHKNL | CHKKWD | CHKALIAS;
353                         t = readtoken();
354                         tokpushback++;
355                         if (t == TNOT)
356                                 lp->n = pipeline();
357                         else
358                                 lp->n = command();
359                         prev->next = lp;
360                 } while (readtoken() == TPIPE);
361                 lp->next = NULL;
362                 n1 = pipenode;
363         }
364         tokpushback++;
365         if (negate) {
366                 n2 = (union node *)stalloc(sizeof (struct nnot));
367                 n2->type = NNOT;
368                 n2->nnot.com = n1;
369                 return n2;
370         } else
371                 return n1;
372 }
373
374
375
376 static union node *
377 command(void)
378 {
379         union node *n1, *n2;
380         union node *ap, **app;
381         union node *cp, **cpp;
382         union node *redir, **rpp;
383         int t;
384         int is_subshell;
385
386         checkkwd = CHKNL | CHKKWD | CHKALIAS;
387         is_subshell = 0;
388         redir = NULL;
389         n1 = NULL;
390         rpp = &redir;
391
392         /* Check for redirection which may precede command */
393         while (readtoken() == TREDIR) {
394                 *rpp = n2 = redirnode;
395                 rpp = &n2->nfile.next;
396                 parsefname();
397         }
398         tokpushback++;
399
400         switch (readtoken()) {
401         case TIF:
402                 n1 = (union node *)stalloc(sizeof (struct nif));
403                 n1->type = NIF;
404                 if ((n1->nif.test = list(0)) == NULL)
405                         synexpect(-1);
406                 consumetoken(TTHEN);
407                 n1->nif.ifpart = list(0);
408                 n2 = n1;
409                 while (readtoken() == TELIF) {
410                         n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
411                         n2 = n2->nif.elsepart;
412                         n2->type = NIF;
413                         if ((n2->nif.test = list(0)) == NULL)
414                                 synexpect(-1);
415                         consumetoken(TTHEN);
416                         n2->nif.ifpart = list(0);
417                 }
418                 if (lasttoken == TELSE)
419                         n2->nif.elsepart = list(0);
420                 else {
421                         n2->nif.elsepart = NULL;
422                         tokpushback++;
423                 }
424                 consumetoken(TFI);
425                 checkkwd = CHKKWD | CHKALIAS;
426                 break;
427         case TWHILE:
428         case TUNTIL:
429                 t = lasttoken;
430                 if ((n1 = list(0)) == NULL)
431                         synexpect(-1);
432                 consumetoken(TDO);
433                 n1 = makebinary((t == TWHILE)? NWHILE : NUNTIL, n1, list(0));
434                 consumetoken(TDONE);
435                 checkkwd = CHKKWD | CHKALIAS;
436                 break;
437         case TFOR:
438                 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
439                         synerror("Bad for loop variable");
440                 n1 = (union node *)stalloc(sizeof (struct nfor));
441                 n1->type = NFOR;
442                 n1->nfor.var = wordtext;
443                 while (readtoken() == TNL)
444                         ;
445                 if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
446                         app = &ap;
447                         while (readtoken() == TWORD) {
448                                 n2 = makename();
449                                 *app = n2;
450                                 app = &n2->narg.next;
451                         }
452                         *app = NULL;
453                         n1->nfor.args = ap;
454                         if (lasttoken != TNL && lasttoken != TSEMI)
455                                 synexpect(-1);
456                 } else {
457                         static char argvars[5] = {
458                                 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
459                         };
460                         n2 = (union node *)stalloc(sizeof (struct narg));
461                         n2->type = NARG;
462                         n2->narg.text = argvars;
463                         n2->narg.backquote = NULL;
464                         n2->narg.next = NULL;
465                         n1->nfor.args = n2;
466                         /*
467                          * Newline or semicolon here is optional (but note
468                          * that the original Bourne shell only allowed NL).
469                          */
470                         if (lasttoken != TNL && lasttoken != TSEMI)
471                                 tokpushback++;
472                 }
473                 checkkwd = CHKNL | CHKKWD | CHKALIAS;
474                 if ((t = readtoken()) == TDO)
475                         t = TDONE;
476                 else if (t == TBEGIN)
477                         t = TEND;
478                 else
479                         synexpect(-1);
480                 n1->nfor.body = list(0);
481                 consumetoken(t);
482                 checkkwd = CHKKWD | CHKALIAS;
483                 break;
484         case TCASE:
485                 n1 = (union node *)stalloc(sizeof (struct ncase));
486                 n1->type = NCASE;
487                 consumetoken(TWORD);
488                 n1->ncase.expr = makename();
489                 while (readtoken() == TNL);
490                 if (lasttoken != TWORD || ! equal(wordtext, "in"))
491                         synerror("expecting \"in\"");
492                 cpp = &n1->ncase.cases;
493                 checkkwd = CHKNL | CHKKWD, readtoken();
494                 while (lasttoken != TESAC) {
495                         *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
496                         cp->type = NCLIST;
497                         app = &cp->nclist.pattern;
498                         if (lasttoken == TLP)
499                                 readtoken();
500                         for (;;) {
501                                 *app = ap = makename();
502                                 checkkwd = CHKNL | CHKKWD;
503                                 if (readtoken() != TPIPE)
504                                         break;
505                                 app = &ap->narg.next;
506                                 readtoken();
507                         }
508                         ap->narg.next = NULL;
509                         if (lasttoken != TRP)
510                                 synexpect(TRP);
511                         cp->nclist.body = list(0);
512
513                         checkkwd = CHKNL | CHKKWD | CHKALIAS;
514                         if ((t = readtoken()) != TESAC) {
515                                 if (t == TENDCASE)
516                                         ;
517                                 else if (t == TFALLTHRU)
518                                         cp->type = NCLISTFALLTHRU;
519                                 else
520                                         synexpect(TENDCASE);
521                                 checkkwd = CHKNL | CHKKWD, readtoken();
522                         }
523                         cpp = &cp->nclist.next;
524                 }
525                 *cpp = NULL;
526                 checkkwd = CHKKWD | CHKALIAS;
527                 break;
528         case TLP:
529                 n1 = (union node *)stalloc(sizeof (struct nredir));
530                 n1->type = NSUBSHELL;
531                 n1->nredir.n = list(0);
532                 n1->nredir.redirect = NULL;
533                 consumetoken(TRP);
534                 checkkwd = CHKKWD | CHKALIAS;
535                 is_subshell = 1;
536                 break;
537         case TBEGIN:
538                 n1 = list(0);
539                 consumetoken(TEND);
540                 checkkwd = CHKKWD | CHKALIAS;
541                 break;
542         /* A simple command must have at least one redirection or word. */
543         case TBACKGND:
544         case TSEMI:
545         case TAND:
546         case TOR:
547         case TPIPE:
548         case TENDCASE:
549         case TFALLTHRU:
550         case TEOF:
551         case TNL:
552         case TRP:
553                 if (!redir)
554                         synexpect(-1);
555         case TWORD:
556                 tokpushback++;
557                 n1 = simplecmd(rpp, redir);
558                 return n1;
559         default:
560                 synexpect(-1);
561         }
562
563         /* Now check for redirection which may follow command */
564         while (readtoken() == TREDIR) {
565                 *rpp = n2 = redirnode;
566                 rpp = &n2->nfile.next;
567                 parsefname();
568         }
569         tokpushback++;
570         *rpp = NULL;
571         if (redir) {
572                 if (!is_subshell) {
573                         n2 = (union node *)stalloc(sizeof (struct nredir));
574                         n2->type = NREDIR;
575                         n2->nredir.n = n1;
576                         n1 = n2;
577                 }
578                 n1->nredir.redirect = redir;
579         }
580
581         return n1;
582 }
583
584
585 static union node *
586 simplecmd(union node **rpp, union node *redir)
587 {
588         union node *args, **app;
589         union node **orig_rpp = rpp;
590         union node *n = NULL;
591         int special;
592         int savecheckkwd;
593
594         /* If we don't have any redirections already, then we must reset */
595         /* rpp to be the address of the local redir variable.  */
596         if (redir == 0)
597                 rpp = &redir;
598
599         args = NULL;
600         app = &args;
601         /*
602          * We save the incoming value, because we need this for shell
603          * functions.  There can not be a redirect or an argument between
604          * the function name and the open parenthesis.
605          */
606         orig_rpp = rpp;
607
608         savecheckkwd = CHKALIAS;
609
610         for (;;) {
611                 checkkwd = savecheckkwd;
612                 if (readtoken() == TWORD) {
613                         n = makename();
614                         *app = n;
615                         app = &n->narg.next;
616                         if (savecheckkwd != 0 && !isassignment(wordtext))
617                                 savecheckkwd = 0;
618                 } else if (lasttoken == TREDIR) {
619                         *rpp = n = redirnode;
620                         rpp = &n->nfile.next;
621                         parsefname();   /* read name of redirection file */
622                 } else if (lasttoken == TLP && app == &args->narg.next
623                                             && rpp == orig_rpp) {
624                         /* We have a function */
625                         consumetoken(TRP);
626                         funclinno = plinno;
627                         /*
628                          * - Require plain text.
629                          * - Functions with '/' cannot be called.
630                          * - Reject name=().
631                          * - Reject ksh extended glob patterns.
632                          */
633                         if (!noexpand(n->narg.text) || quoteflag ||
634                             strchr(n->narg.text, '/') ||
635                             strchr("!%*+-=?@}~",
636                                 n->narg.text[strlen(n->narg.text) - 1]))
637                                 synerror("Bad function name");
638                         rmescapes(n->narg.text);
639                         if (find_builtin(n->narg.text, &special) >= 0 &&
640                             special)
641                                 synerror("Cannot override a special builtin with a function");
642                         n->type = NDEFUN;
643                         n->narg.next = command();
644                         funclinno = 0;
645                         return n;
646                 } else {
647                         tokpushback++;
648                         break;
649                 }
650         }
651         *app = NULL;
652         *rpp = NULL;
653         n = (union node *)stalloc(sizeof (struct ncmd));
654         n->type = NCMD;
655         n->ncmd.args = args;
656         n->ncmd.redirect = redir;
657         return n;
658 }
659
660 static union node *
661 makename(void)
662 {
663         union node *n;
664
665         n = (union node *)stalloc(sizeof (struct narg));
666         n->type = NARG;
667         n->narg.next = NULL;
668         n->narg.text = wordtext;
669         n->narg.backquote = backquotelist;
670         return n;
671 }
672
673 static union node *
674 makebinary(int type, union node *n1, union node *n2)
675 {
676         union node *n;
677
678         n = (union node *)stalloc(sizeof (struct nbinary));
679         n->type = type;
680         n->nbinary.ch1 = n1;
681         n->nbinary.ch2 = n2;
682         return (n);
683 }
684
685 void
686 forcealias(void)
687 {
688         checkkwd |= CHKALIAS;
689 }
690
691 void
692 fixredir(union node *n, const char *text, int err)
693 {
694         TRACE(("Fix redir %s %d\n", text, err));
695         if (!err)
696                 n->ndup.vname = NULL;
697
698         if (is_digit(text[0]) && text[1] == '\0')
699                 n->ndup.dupfd = digit_val(text[0]);
700         else if (text[0] == '-' && text[1] == '\0')
701                 n->ndup.dupfd = -1;
702         else {
703
704                 if (err)
705                         synerror("Bad fd number");
706                 else
707                         n->ndup.vname = makename();
708         }
709 }
710
711
712 static void
713 parsefname(void)
714 {
715         union node *n = redirnode;
716
717         consumetoken(TWORD);
718         if (n->type == NHERE) {
719                 struct heredoc *here = heredoc;
720                 struct heredoc *p;
721
722                 if (quoteflag == 0)
723                         n->type = NXHERE;
724                 TRACE(("Here document %d\n", n->type));
725                 if (here->striptabs) {
726                         while (*wordtext == '\t')
727                                 wordtext++;
728                 }
729                 if (! noexpand(wordtext))
730                         synerror("Illegal eof marker for << redirection");
731                 rmescapes(wordtext);
732                 here->eofmark = wordtext;
733                 here->next = NULL;
734                 if (heredoclist == NULL)
735                         heredoclist = here;
736                 else {
737                         for (p = heredoclist ; p->next ; p = p->next);
738                         p->next = here;
739                 }
740         } else if (n->type == NTOFD || n->type == NFROMFD) {
741                 fixredir(n, wordtext, 0);
742         } else {
743                 n->nfile.fname = makename();
744         }
745 }
746
747
748 /*
749  * Input any here documents.
750  */
751
752 static void
753 parseheredoc(void)
754 {
755         struct heredoc *here;
756         union node *n;
757
758         while (heredoclist) {
759                 here = heredoclist;
760                 heredoclist = here->next;
761                 if (needprompt) {
762                         setprompt(2);
763                         needprompt = 0;
764                 }
765                 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
766                                 here->eofmark, here->striptabs);
767                 n = makename();
768                 here->here->nhere.doc = n;
769         }
770 }
771
772 static int
773 peektoken(void)
774 {
775         int t;
776
777         t = readtoken();
778         tokpushback++;
779         return (t);
780 }
781
782 static int
783 readtoken(void)
784 {
785         int t;
786         struct alias *ap;
787 #ifdef DEBUG
788         int alreadyseen = tokpushback;
789 #endif
790
791         top:
792         t = xxreadtoken();
793
794         /*
795          * eat newlines
796          */
797         if (checkkwd & CHKNL) {
798                 while (t == TNL) {
799                         parseheredoc();
800                         t = xxreadtoken();
801                 }
802         }
803
804         /*
805          * check for keywords and aliases
806          */
807         if (t == TWORD && !quoteflag)
808         {
809                 const char * const *pp;
810
811                 if (checkkwd & CHKKWD)
812                         for (pp = parsekwd; *pp; pp++) {
813                                 if (**pp == *wordtext && equal(*pp, wordtext))
814                                 {
815                                         lasttoken = t = pp - parsekwd + KWDOFFSET;
816                                         TRACE(("keyword %s recognized\n", tokname[t]));
817                                         goto out;
818                                 }
819                         }
820                 if (checkkwd & CHKALIAS &&
821                     (ap = lookupalias(wordtext, 1)) != NULL) {
822                         pushstring(ap->val, strlen(ap->val), ap);
823                         goto top;
824                 }
825         }
826 out:
827         if (t != TNOT)
828                 checkkwd = 0;
829
830 #ifdef DEBUG
831         if (!alreadyseen)
832             TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
833         else
834             TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
835 #endif
836         return (t);
837 }
838
839
840 /*
841  * Read the next input token.
842  * If the token is a word, we set backquotelist to the list of cmds in
843  *      backquotes.  We set quoteflag to true if any part of the word was
844  *      quoted.
845  * If the token is TREDIR, then we set redirnode to a structure containing
846  *      the redirection.
847  * In all cases, the variable startlinno is set to the number of the line
848  *      on which the token starts.
849  *
850  * [Change comment:  here documents and internal procedures]
851  * [Readtoken shouldn't have any arguments.  Perhaps we should make the
852  *  word parsing code into a separate routine.  In this case, readtoken
853  *  doesn't need to have any internal procedures, but parseword does.
854  *  We could also make parseoperator in essence the main routine, and
855  *  have parseword (readtoken1?) handle both words and redirection.]
856  */
857
858 #define RETURN(token)   return lasttoken = token
859
860 static int
861 xxreadtoken(void)
862 {
863         int c;
864
865         if (tokpushback) {
866                 tokpushback = 0;
867                 return lasttoken;
868         }
869         if (needprompt) {
870                 setprompt(2);
871                 needprompt = 0;
872         }
873         startlinno = plinno;
874         for (;;) {      /* until token or start of word found */
875                 c = pgetc_macro();
876                 switch (c) {
877                 case ' ': case '\t':
878                         continue;
879                 case '#':
880                         while ((c = pgetc()) != '\n' && c != PEOF);
881                         pungetc();
882                         continue;
883                 case '\\':
884                         if (pgetc() == '\n') {
885                                 startlinno = ++plinno;
886                                 if (doprompt)
887                                         setprompt(2);
888                                 else
889                                         setprompt(0);
890                                 continue;
891                         }
892                         pungetc();
893                         /* FALLTHROUGH */
894                 default:
895                         return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
896                 case '\n':
897                         plinno++;
898                         needprompt = doprompt;
899                         RETURN(TNL);
900                 case PEOF:
901                         RETURN(TEOF);
902                 case '&':
903                         if (pgetc_linecont() == '&')
904                                 RETURN(TAND);
905                         pungetc();
906                         RETURN(TBACKGND);
907                 case '|':
908                         if (pgetc_linecont() == '|')
909                                 RETURN(TOR);
910                         pungetc();
911                         RETURN(TPIPE);
912                 case ';':
913                         c = pgetc_linecont();
914                         if (c == ';')
915                                 RETURN(TENDCASE);
916                         else if (c == '&')
917                                 RETURN(TFALLTHRU);
918                         pungetc();
919                         RETURN(TSEMI);
920                 case '(':
921                         RETURN(TLP);
922                 case ')':
923                         RETURN(TRP);
924                 }
925         }
926 #undef RETURN
927 }
928
929
930 #define MAXNEST_static 8
931 struct tokenstate
932 {
933         const char *syntax; /* *SYNTAX */
934         int parenlevel; /* levels of parentheses in arithmetic */
935         enum tokenstate_category
936         {
937                 TSTATE_TOP,
938                 TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
939                 TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
940                 TSTATE_ARITH
941         } category;
942 };
943
944
945 /*
946  * Check to see whether we are at the end of the here document.  When this
947  * is called, c is set to the first character of the next input line.  If
948  * we are at the end of the here document, this routine sets the c to PEOF.
949  * The new value of c is returned.
950  */
951
952 static int
953 checkend(int c, const char *eofmark, int striptabs)
954 {
955         if (striptabs) {
956                 while (c == '\t')
957                         c = pgetc();
958         }
959         if (c == *eofmark) {
960                 int c2;
961                 const char *q;
962
963                 for (q = eofmark + 1; c2 = pgetc(), *q != '\0' && c2 == *q; q++)
964                         ;
965                 if ((c2 == PEOF || c2 == '\n') && *q == '\0') {
966                         c = PEOF;
967                         if (c2 == '\n') {
968                                 plinno++;
969                                 needprompt = doprompt;
970                         }
971                 } else {
972                         pungetc();
973                         pushstring(eofmark + 1, q - (eofmark + 1), NULL);
974                 }
975         }
976         return (c);
977 }
978
979
980 /*
981  * Parse a redirection operator.  The variable "out" points to a string
982  * specifying the fd to be redirected.  The variable "c" contains the
983  * first character of the redirection operator.
984  */
985
986 static void
987 parseredir(char *out, int c)
988 {
989         char fd = *out;
990         union node *np;
991
992         np = (union node *)stalloc(sizeof (struct nfile));
993         if (c == '>') {
994                 np->nfile.fd = 1;
995                 c = pgetc_linecont();
996                 if (c == '>')
997                         np->type = NAPPEND;
998                 else if (c == '&')
999                         np->type = NTOFD;
1000                 else if (c == '|')
1001                         np->type = NCLOBBER;
1002                 else {
1003                         np->type = NTO;
1004                         pungetc();
1005                 }
1006         } else {        /* c == '<' */
1007                 np->nfile.fd = 0;
1008                 c = pgetc_linecont();
1009                 if (c == '<') {
1010                         if (sizeof (struct nfile) != sizeof (struct nhere)) {
1011                                 np = (union node *)stalloc(sizeof (struct nhere));
1012                                 np->nfile.fd = 0;
1013                         }
1014                         np->type = NHERE;
1015                         heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1016                         heredoc->here = np;
1017                         if ((c = pgetc_linecont()) == '-') {
1018                                 heredoc->striptabs = 1;
1019                         } else {
1020                                 heredoc->striptabs = 0;
1021                                 pungetc();
1022                         }
1023                 } else if (c == '&')
1024                         np->type = NFROMFD;
1025                 else if (c == '>')
1026                         np->type = NFROMTO;
1027                 else {
1028                         np->type = NFROM;
1029                         pungetc();
1030                 }
1031         }
1032         if (fd != '\0')
1033                 np->nfile.fd = digit_val(fd);
1034         redirnode = np;
1035 }
1036
1037 /*
1038  * Called to parse command substitutions.
1039  */
1040
1041 static char *
1042 parsebackq(char *out, struct nodelist **pbqlist,
1043     int oldstyle, int dblquote, int quoted)
1044 {
1045         struct nodelist **nlpp;
1046         union node *n;
1047         char *volatile str;
1048         struct jmploc jmploc;
1049         struct jmploc *const savehandler = handler;
1050         size_t savelen;
1051         int saveprompt;
1052         const int bq_startlinno = plinno;
1053         char *volatile ostr = NULL;
1054         struct parsefile *const savetopfile = getcurrentfile();
1055         struct heredoc *const saveheredoclist = heredoclist;
1056         struct heredoc *here;
1057
1058         str = NULL;
1059         if (setjmp(jmploc.loc)) {
1060                 popfilesupto(savetopfile);
1061                 if (str)
1062                         ckfree(str);
1063                 if (ostr)
1064                         ckfree(ostr);
1065                 heredoclist = saveheredoclist;
1066                 handler = savehandler;
1067                 if (exception == EXERROR) {
1068                         startlinno = bq_startlinno;
1069                         synerror("Error in command substitution");
1070                 }
1071                 longjmp(handler->loc, 1);
1072         }
1073         INTOFF;
1074         savelen = out - stackblock();
1075         if (savelen > 0) {
1076                 str = ckmalloc(savelen);
1077                 memcpy(str, stackblock(), savelen);
1078         }
1079         handler = &jmploc;
1080         heredoclist = NULL;
1081         INTON;
1082         if (oldstyle) {
1083                 /* We must read until the closing backquote, giving special
1084                    treatment to some slashes, and then push the string and
1085                    reread it as input, interpreting it normally.  */
1086                 char *oout;
1087                 int c;
1088                 int olen;
1089
1090
1091                 STARTSTACKSTR(oout);
1092                 for (;;) {
1093                         if (needprompt) {
1094                                 setprompt(2);
1095                                 needprompt = 0;
1096                         }
1097                         CHECKSTRSPACE(2, oout);
1098                         c = pgetc_linecont();
1099                         if (c == '`')
1100                                 break;
1101                         switch (c) {
1102                         case '\\':
1103                                 c = pgetc();
1104                                 if (c != '\\' && c != '`' && c != '$'
1105                                     && (!dblquote || c != '"'))
1106                                         USTPUTC('\\', oout);
1107                                 break;
1108
1109                         case '\n':
1110                                 plinno++;
1111                                 needprompt = doprompt;
1112                                 break;
1113
1114                         case PEOF:
1115                                 startlinno = plinno;
1116                                 synerror("EOF in backquote substitution");
1117                                 break;
1118
1119                         default:
1120                                 break;
1121                         }
1122                         USTPUTC(c, oout);
1123                 }
1124                 USTPUTC('\0', oout);
1125                 olen = oout - stackblock();
1126                 INTOFF;
1127                 ostr = ckmalloc(olen);
1128                 memcpy(ostr, stackblock(), olen);
1129                 setinputstring(ostr, 1);
1130                 INTON;
1131         }
1132         nlpp = pbqlist;
1133         while (*nlpp)
1134                 nlpp = &(*nlpp)->next;
1135         *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1136         (*nlpp)->next = NULL;
1137
1138         if (oldstyle) {
1139                 saveprompt = doprompt;
1140                 doprompt = 0;
1141         }
1142
1143         n = list(0);
1144
1145         if (oldstyle) {
1146                 if (peektoken() != TEOF)
1147                         synexpect(-1);
1148                 doprompt = saveprompt;
1149         } else
1150                 consumetoken(TRP);
1151
1152         (*nlpp)->n = n;
1153         if (oldstyle) {
1154                 /*
1155                  * Start reading from old file again, ignoring any pushed back
1156                  * tokens left from the backquote parsing
1157                  */
1158                 popfile();
1159                 tokpushback = 0;
1160         }
1161         STARTSTACKSTR(out);
1162         CHECKSTRSPACE(savelen + 1, out);
1163         INTOFF;
1164         if (str) {
1165                 memcpy(out, str, savelen);
1166                 STADJUST(savelen, out);
1167                 ckfree(str);
1168                 str = NULL;
1169         }
1170         if (ostr) {
1171                 ckfree(ostr);
1172                 ostr = NULL;
1173         }
1174         here = saveheredoclist;
1175         if (here != NULL) {
1176                 while (here->next != NULL)
1177                         here = here->next;
1178                 here->next = heredoclist;
1179                 heredoclist = saveheredoclist;
1180         }
1181         handler = savehandler;
1182         INTON;
1183         if (quoted)
1184                 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1185         else
1186                 USTPUTC(CTLBACKQ, out);
1187         return out;
1188 }
1189
1190
1191 /*
1192  * Called to parse a backslash escape sequence inside $'...'.
1193  * The backslash has already been read.
1194  */
1195 static char *
1196 readcstyleesc(char *out)
1197 {
1198         int c, v, i, n;
1199
1200         c = pgetc();
1201         switch (c) {
1202         case '\0':
1203                 synerror("Unterminated quoted string");
1204         case '\n':
1205                 plinno++;
1206                 if (doprompt)
1207                         setprompt(2);
1208                 else
1209                         setprompt(0);
1210                 return out;
1211         case '\\':
1212         case '\'':
1213         case '"':
1214                 v = c;
1215                 break;
1216         case 'a': v = '\a'; break;
1217         case 'b': v = '\b'; break;
1218         case 'e': v = '\033'; break;
1219         case 'f': v = '\f'; break;
1220         case 'n': v = '\n'; break;
1221         case 'r': v = '\r'; break;
1222         case 't': v = '\t'; break;
1223         case 'v': v = '\v'; break;
1224         case 'x':
1225                   v = 0;
1226                   for (;;) {
1227                           c = pgetc();
1228                           if (c >= '0' && c <= '9')
1229                                   v = (v << 4) + c - '0';
1230                           else if (c >= 'A' && c <= 'F')
1231                                   v = (v << 4) + c - 'A' + 10;
1232                           else if (c >= 'a' && c <= 'f')
1233                                   v = (v << 4) + c - 'a' + 10;
1234                           else
1235                                   break;
1236                   }
1237                   pungetc();
1238                   break;
1239         case '0': case '1': case '2': case '3':
1240         case '4': case '5': case '6': case '7':
1241                   v = c - '0';
1242                   c = pgetc();
1243                   if (c >= '0' && c <= '7') {
1244                           v <<= 3;
1245                           v += c - '0';
1246                           c = pgetc();
1247                           if (c >= '0' && c <= '7') {
1248                                   v <<= 3;
1249                                   v += c - '0';
1250                           } else
1251                                   pungetc();
1252                   } else
1253                           pungetc();
1254                   break;
1255         case 'c':
1256                   c = pgetc();
1257                   if (c < 0x3f || c > 0x7a || c == 0x60)
1258                           synerror("Bad escape sequence");
1259                   if (c == '\\' && pgetc() != '\\')
1260                           synerror("Bad escape sequence");
1261                   if (c == '?')
1262                           v = 127;
1263                   else
1264                           v = c & 0x1f;
1265                   break;
1266         case 'u':
1267         case 'U':
1268                   n = c == 'U' ? 8 : 4;
1269                   v = 0;
1270                   for (i = 0; i < n; i++) {
1271                           c = pgetc();
1272                           if (c >= '0' && c <= '9')
1273                                   v = (v << 4) + c - '0';
1274                           else if (c >= 'A' && c <= 'F')
1275                                   v = (v << 4) + c - 'A' + 10;
1276                           else if (c >= 'a' && c <= 'f')
1277                                   v = (v << 4) + c - 'a' + 10;
1278                           else
1279                                   synerror("Bad escape sequence");
1280                   }
1281                   if (v == 0 || (v >= 0xd800 && v <= 0xdfff))
1282                           synerror("Bad escape sequence");
1283                   /* We really need iconv here. */
1284                   if (initial_localeisutf8 && v > 127) {
1285                           CHECKSTRSPACE(4, out);
1286                           /*
1287                            * We cannot use wctomb() as the locale may have
1288                            * changed.
1289                            */
1290                           if (v <= 0x7ff) {
1291                                   USTPUTC(0xc0 | v >> 6, out);
1292                                   USTPUTC(0x80 | (v & 0x3f), out);
1293                                   return out;
1294                           } else if (v <= 0xffff) {
1295                                   USTPUTC(0xe0 | v >> 12, out);
1296                                   USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1297                                   USTPUTC(0x80 | (v & 0x3f), out);
1298                                   return out;
1299                           } else if (v <= 0x10ffff) {
1300                                   USTPUTC(0xf0 | v >> 18, out);
1301                                   USTPUTC(0x80 | ((v >> 12) & 0x3f), out);
1302                                   USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1303                                   USTPUTC(0x80 | (v & 0x3f), out);
1304                                   return out;
1305                           }
1306                   }
1307                   if (v > 127)
1308                           v = '?';
1309                   break;
1310         default:
1311                   synerror("Bad escape sequence");
1312         }
1313         v = (char)v;
1314         /*
1315          * We can't handle NUL bytes.
1316          * POSIX says we should skip till the closing quote.
1317          */
1318         if (v == '\0') {
1319                 while ((c = pgetc()) != '\'') {
1320                         if (c == '\\')
1321                                 c = pgetc();
1322                         if (c == PEOF)
1323                                 synerror("Unterminated quoted string");
1324                         if (c == '\n') {
1325                                 plinno++;
1326                                 if (doprompt)
1327                                         setprompt(2);
1328                                 else
1329                                         setprompt(0);
1330                         }
1331                 }
1332                 pungetc();
1333                 return out;
1334         }
1335         if (SQSYNTAX[v] == CCTL)
1336                 USTPUTC(CTLESC, out);
1337         USTPUTC(v, out);
1338         return out;
1339 }
1340
1341
1342 /*
1343  * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
1344  * is not NULL, read a here document.  In the latter case, eofmark is the
1345  * word which marks the end of the document and striptabs is true if
1346  * leading tabs should be stripped from the document.  The argument firstc
1347  * is the first character of the input token or document.
1348  *
1349  * Because C does not have internal subroutines, I have simulated them
1350  * using goto's to implement the subroutine linkage.  The following macros
1351  * will run code that appears at the end of readtoken1.
1352  */
1353
1354 #define PARSESUB()      {goto parsesub; parsesub_return:;}
1355 #define PARSEARITH()    {goto parsearith; parsearith_return:;}
1356
1357 static int
1358 readtoken1(int firstc, char const *initialsyntax, const char *eofmark,
1359     int striptabs)
1360 {
1361         int c = firstc;
1362         char *out;
1363         int len;
1364         struct nodelist *bqlist;
1365         int quotef;
1366         int newvarnest;
1367         int level;
1368         int synentry;
1369         struct tokenstate state_static[MAXNEST_static];
1370         int maxnest = MAXNEST_static;
1371         struct tokenstate *state = state_static;
1372         int sqiscstyle = 0;
1373
1374         startlinno = plinno;
1375         quotef = 0;
1376         bqlist = NULL;
1377         newvarnest = 0;
1378         level = 0;
1379         state[level].syntax = initialsyntax;
1380         state[level].parenlevel = 0;
1381         state[level].category = TSTATE_TOP;
1382
1383         STARTSTACKSTR(out);
1384         loop: { /* for each line, until end of word */
1385                 if (eofmark)
1386                         /* set c to PEOF if at end of here document */
1387                         c = checkend(c, eofmark, striptabs);
1388                 for (;;) {      /* until end of line or end of word */
1389                         CHECKSTRSPACE(4, out);  /* permit 4 calls to USTPUTC */
1390
1391                         synentry = state[level].syntax[c];
1392
1393                         switch(synentry) {
1394                         case CNL:       /* '\n' */
1395                                 if (state[level].syntax == BASESYNTAX)
1396                                         goto endword;   /* exit outer loop */
1397                                 USTPUTC(c, out);
1398                                 plinno++;
1399                                 if (doprompt)
1400                                         setprompt(2);
1401                                 else
1402                                         setprompt(0);
1403                                 c = pgetc();
1404                                 goto loop;              /* continue outer loop */
1405                         case CSBACK:
1406                                 if (sqiscstyle) {
1407                                         out = readcstyleesc(out);
1408                                         break;
1409                                 }
1410                                 /* FALLTHROUGH */
1411                         case CWORD:
1412                                 USTPUTC(c, out);
1413                                 break;
1414                         case CCTL:
1415                                 if (eofmark == NULL || initialsyntax != SQSYNTAX)
1416                                         USTPUTC(CTLESC, out);
1417                                 USTPUTC(c, out);
1418                                 break;
1419                         case CBACK:     /* backslash */
1420                                 c = pgetc();
1421                                 if (c == PEOF) {
1422                                         USTPUTC('\\', out);
1423                                         pungetc();
1424                                 } else if (c == '\n') {
1425                                         plinno++;
1426                                         if (doprompt)
1427                                                 setprompt(2);
1428                                         else
1429                                                 setprompt(0);
1430                                 } else {
1431                                         if (state[level].syntax == DQSYNTAX &&
1432                                             c != '\\' && c != '`' && c != '$' &&
1433                                             (c != '"' || (eofmark != NULL &&
1434                                                 newvarnest == 0)) &&
1435                                             (c != '}' || state[level].category != TSTATE_VAR_OLD))
1436                                                 USTPUTC('\\', out);
1437                                         if ((eofmark == NULL ||
1438                                             newvarnest > 0) &&
1439                                             state[level].syntax == BASESYNTAX)
1440                                                 USTPUTC(CTLQUOTEMARK, out);
1441                                         if (SQSYNTAX[c] == CCTL)
1442                                                 USTPUTC(CTLESC, out);
1443                                         USTPUTC(c, out);
1444                                         if ((eofmark == NULL ||
1445                                             newvarnest > 0) &&
1446                                             state[level].syntax == BASESYNTAX &&
1447                                             state[level].category == TSTATE_VAR_OLD)
1448                                                 USTPUTC(CTLQUOTEEND, out);
1449                                         quotef++;
1450                                 }
1451                                 break;
1452                         case CSQUOTE:
1453                                 USTPUTC(CTLQUOTEMARK, out);
1454                                 state[level].syntax = SQSYNTAX;
1455                                 sqiscstyle = 0;
1456                                 break;
1457                         case CDQUOTE:
1458                                 USTPUTC(CTLQUOTEMARK, out);
1459                                 state[level].syntax = DQSYNTAX;
1460                                 break;
1461                         case CENDQUOTE:
1462                                 if (eofmark != NULL && newvarnest == 0)
1463                                         USTPUTC(c, out);
1464                                 else {
1465                                         if (state[level].category == TSTATE_VAR_OLD)
1466                                                 USTPUTC(CTLQUOTEEND, out);
1467                                         state[level].syntax = BASESYNTAX;
1468                                         quotef++;
1469                                 }
1470                                 break;
1471                         case CVAR:      /* '$' */
1472                                 PARSESUB();             /* parse substitution */
1473                                 break;
1474                         case CENDVAR:   /* '}' */
1475                                 if (level > 0 &&
1476                                     ((state[level].category == TSTATE_VAR_OLD &&
1477                                       state[level].syntax ==
1478                                       state[level - 1].syntax) ||
1479                                     (state[level].category == TSTATE_VAR_NEW &&
1480                                      state[level].syntax == BASESYNTAX))) {
1481                                         if (state[level].category == TSTATE_VAR_NEW)
1482                                                 newvarnest--;
1483                                         level--;
1484                                         USTPUTC(CTLENDVAR, out);
1485                                 } else {
1486                                         USTPUTC(c, out);
1487                                 }
1488                                 break;
1489                         case CLP:       /* '(' in arithmetic */
1490                                 state[level].parenlevel++;
1491                                 USTPUTC(c, out);
1492                                 break;
1493                         case CRP:       /* ')' in arithmetic */
1494                                 if (state[level].parenlevel > 0) {
1495                                         USTPUTC(c, out);
1496                                         --state[level].parenlevel;
1497                                 } else {
1498                                         if (pgetc_linecont() == ')') {
1499                                                 if (level > 0 &&
1500                                                     state[level].category == TSTATE_ARITH) {
1501                                                         level--;
1502                                                         USTPUTC(CTLENDARI, out);
1503                                                 } else
1504                                                         USTPUTC(')', out);
1505                                         } else {
1506                                                 /*
1507                                                  * unbalanced parens
1508                                                  *  (don't 2nd guess - no error)
1509                                                  */
1510                                                 pungetc();
1511                                                 USTPUTC(')', out);
1512                                         }
1513                                 }
1514                                 break;
1515                         case CBQUOTE:   /* '`' */
1516                                 out = parsebackq(out, &bqlist, 1,
1517                                     state[level].syntax == DQSYNTAX &&
1518                                     (eofmark == NULL || newvarnest > 0),
1519                                     state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
1520                                 break;
1521                         case CEOF:
1522                                 goto endword;           /* exit outer loop */
1523                         case CIGN:
1524                                 break;
1525                         default:
1526                                 if (level == 0)
1527                                         goto endword;   /* exit outer loop */
1528                                 USTPUTC(c, out);
1529                         }
1530                         c = pgetc_macro();
1531                 }
1532         }
1533 endword:
1534         if (state[level].syntax == ARISYNTAX)
1535                 synerror("Missing '))'");
1536         if (state[level].syntax != BASESYNTAX && eofmark == NULL)
1537                 synerror("Unterminated quoted string");
1538         if (state[level].category == TSTATE_VAR_OLD ||
1539             state[level].category == TSTATE_VAR_NEW) {
1540                 startlinno = plinno;
1541                 synerror("Missing '}'");
1542         }
1543         if (state != state_static)
1544                 parser_temp_free_upto(state);
1545         USTPUTC('\0', out);
1546         len = out - stackblock();
1547         out = stackblock();
1548         if (eofmark == NULL) {
1549                 if ((c == '>' || c == '<')
1550                  && quotef == 0
1551                  && len <= 2
1552                  && (*out == '\0' || is_digit(*out))) {
1553                         parseredir(out, c);
1554                         return lasttoken = TREDIR;
1555                 } else {
1556                         pungetc();
1557                 }
1558         }
1559         quoteflag = quotef;
1560         backquotelist = bqlist;
1561         grabstackblock(len);
1562         wordtext = out;
1563         return lasttoken = TWORD;
1564 /* end of readtoken routine */
1565
1566
1567 /*
1568  * Parse a substitution.  At this point, we have read the dollar sign
1569  * and nothing else.
1570  */
1571
1572 parsesub: {
1573         char buf[10];
1574         int subtype;
1575         int typeloc;
1576         int flags;
1577         char *p;
1578         static const char types[] = "}-+?=";
1579         int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1580         int linno;
1581         int length;
1582         int c1;
1583
1584         c = pgetc_linecont();
1585         if (c == '(') { /* $(command) or $((arith)) */
1586                 if (pgetc_linecont() == '(') {
1587                         PARSEARITH();
1588                 } else {
1589                         pungetc();
1590                         out = parsebackq(out, &bqlist, 0,
1591                             state[level].syntax == DQSYNTAX &&
1592                             (eofmark == NULL || newvarnest > 0),
1593                             state[level].syntax == DQSYNTAX ||
1594                             state[level].syntax == ARISYNTAX);
1595                 }
1596         } else if (c == '{' || is_name(c) || is_special(c)) {
1597                 USTPUTC(CTLVAR, out);
1598                 typeloc = out - stackblock();
1599                 USTPUTC(VSNORMAL, out);
1600                 subtype = VSNORMAL;
1601                 flags = 0;
1602                 if (c == '{') {
1603                         bracketed_name = 1;
1604                         c = pgetc_linecont();
1605                         subtype = 0;
1606                 }
1607 varname:
1608                 if (!is_eof(c) && is_name(c)) {
1609                         length = 0;
1610                         do {
1611                                 STPUTC(c, out);
1612                                 c = pgetc_linecont();
1613                                 length++;
1614                         } while (!is_eof(c) && is_in_name(c));
1615                         if (length == 6 &&
1616                             strncmp(out - length, "LINENO", length) == 0) {
1617                                 /* Replace the variable name with the
1618                                  * current line number. */
1619                                 linno = plinno;
1620                                 if (funclinno != 0)
1621                                         linno -= funclinno - 1;
1622                                 snprintf(buf, sizeof(buf), "%d", linno);
1623                                 STADJUST(-6, out);
1624                                 STPUTS(buf, out);
1625                                 flags |= VSLINENO;
1626                         }
1627                 } else if (is_digit(c)) {
1628                         if (bracketed_name) {
1629                                 do {
1630                                         STPUTC(c, out);
1631                                         c = pgetc_linecont();
1632                                 } while (is_digit(c));
1633                         } else {
1634                                 STPUTC(c, out);
1635                                 c = pgetc_linecont();
1636                         }
1637                 } else if (is_special(c)) {
1638                         c1 = c;
1639                         c = pgetc_linecont();
1640                         if (subtype == 0 && c1 == '#') {
1641                                 subtype = VSLENGTH;
1642                                 if (strchr(types, c) == NULL && c != ':' &&
1643                                     c != '#' && c != '%')
1644                                         goto varname;
1645                                 c1 = c;
1646                                 c = pgetc_linecont();
1647                                 if (c1 != '}' && c == '}') {
1648                                         pungetc();
1649                                         c = c1;
1650                                         goto varname;
1651                                 }
1652                                 pungetc();
1653                                 c = c1;
1654                                 c1 = '#';
1655                                 subtype = 0;
1656                         }
1657                         USTPUTC(c1, out);
1658                 } else {
1659                         subtype = VSERROR;
1660                         if (c == '}')
1661                                 pungetc();
1662                         else if (c == '\n' || c == PEOF)
1663                                 synerror("Unexpected end of line in substitution");
1664                         else
1665                                 USTPUTC(c, out);
1666                 }
1667                 if (subtype == 0) {
1668                         switch (c) {
1669                         case ':':
1670                                 flags |= VSNUL;
1671                                 c = pgetc_linecont();
1672                                 /*FALLTHROUGH*/
1673                         default:
1674                                 p = strchr(types, c);
1675                                 if (p == NULL) {
1676                                         if (c == '\n' || c == PEOF)
1677                                                 synerror("Unexpected end of line in substitution");
1678                                         if (flags == VSNUL)
1679                                                 STPUTC(':', out);
1680                                         STPUTC(c, out);
1681                                         subtype = VSERROR;
1682                                 } else
1683                                         subtype = p - types + VSNORMAL;
1684                                 break;
1685                         case '%':
1686                         case '#':
1687                                 {
1688                                         int cc = c;
1689                                         subtype = c == '#' ? VSTRIMLEFT :
1690                                                              VSTRIMRIGHT;
1691                                         c = pgetc_linecont();
1692                                         if (c == cc)
1693                                                 subtype++;
1694                                         else
1695                                                 pungetc();
1696                                         break;
1697                                 }
1698                         }
1699                 } else if (subtype != VSERROR) {
1700                         if (subtype == VSLENGTH && c != '}')
1701                                 subtype = VSERROR;
1702                         pungetc();
1703                 }
1704                 STPUTC('=', out);
1705                 if (state[level].syntax == DQSYNTAX ||
1706                     state[level].syntax == ARISYNTAX)
1707                         flags |= VSQUOTE;
1708                 *(stackblock() + typeloc) = subtype | flags;
1709                 if (subtype != VSNORMAL) {
1710                         if (level + 1 >= maxnest) {
1711                                 maxnest *= 2;
1712                                 if (state == state_static) {
1713                                         state = parser_temp_alloc(
1714                                             maxnest * sizeof(*state));
1715                                         memcpy(state, state_static,
1716                                             MAXNEST_static * sizeof(*state));
1717                                 } else
1718                                         state = parser_temp_realloc(state,
1719                                             maxnest * sizeof(*state));
1720                         }
1721                         level++;
1722                         state[level].parenlevel = 0;
1723                         if (subtype == VSMINUS || subtype == VSPLUS ||
1724                             subtype == VSQUESTION || subtype == VSASSIGN) {
1725                                 /*
1726                                  * For operators that were in the Bourne shell,
1727                                  * inherit the double-quote state.
1728                                  */
1729                                 state[level].syntax = state[level - 1].syntax;
1730                                 state[level].category = TSTATE_VAR_OLD;
1731                         } else {
1732                                 /*
1733                                  * The other operators take a pattern,
1734                                  * so go to BASESYNTAX.
1735                                  * Also, ' and " are now special, even
1736                                  * in here documents.
1737                                  */
1738                                 state[level].syntax = BASESYNTAX;
1739                                 state[level].category = TSTATE_VAR_NEW;
1740                                 newvarnest++;
1741                         }
1742                 }
1743         } else if (c == '\'' && state[level].syntax == BASESYNTAX) {
1744                 /* $'cstylequotes' */
1745                 USTPUTC(CTLQUOTEMARK, out);
1746                 state[level].syntax = SQSYNTAX;
1747                 sqiscstyle = 1;
1748         } else {
1749                 USTPUTC('$', out);
1750                 pungetc();
1751         }
1752         goto parsesub_return;
1753 }
1754
1755
1756 /*
1757  * Parse an arithmetic expansion (indicate start of one and set state)
1758  */
1759 parsearith: {
1760
1761         if (level + 1 >= maxnest) {
1762                 maxnest *= 2;
1763                 if (state == state_static) {
1764                         state = parser_temp_alloc(
1765                             maxnest * sizeof(*state));
1766                         memcpy(state, state_static,
1767                             MAXNEST_static * sizeof(*state));
1768                 } else
1769                         state = parser_temp_realloc(state,
1770                             maxnest * sizeof(*state));
1771         }
1772         level++;
1773         state[level].syntax = ARISYNTAX;
1774         state[level].parenlevel = 0;
1775         state[level].category = TSTATE_ARITH;
1776         USTPUTC(CTLARI, out);
1777         if (state[level - 1].syntax == DQSYNTAX)
1778                 USTPUTC('"',out);
1779         else
1780                 USTPUTC(' ',out);
1781         goto parsearith_return;
1782 }
1783
1784 } /* end of readtoken */
1785
1786
1787 /*
1788  * Returns true if the text contains nothing to expand (no dollar signs
1789  * or backquotes).
1790  */
1791
1792 static int
1793 noexpand(char *text)
1794 {
1795         char *p;
1796         char c;
1797
1798         p = text;
1799         while ((c = *p++) != '\0') {
1800                 if ( c == CTLQUOTEMARK)
1801                         continue;
1802                 if (c == CTLESC)
1803                         p++;
1804                 else if (BASESYNTAX[(int)c] == CCTL)
1805                         return 0;
1806         }
1807         return 1;
1808 }
1809
1810
1811 /*
1812  * Return true if the argument is a legal variable name (a letter or
1813  * underscore followed by zero or more letters, underscores, and digits).
1814  */
1815
1816 int
1817 goodname(const char *name)
1818 {
1819         const char *p;
1820
1821         p = name;
1822         if (! is_name(*p))
1823                 return 0;
1824         while (*++p) {
1825                 if (! is_in_name(*p))
1826                         return 0;
1827         }
1828         return 1;
1829 }
1830
1831
1832 int
1833 isassignment(const char *p)
1834 {
1835         if (!is_name(*p))
1836                 return 0;
1837         p++;
1838         for (;;) {
1839                 if (*p == '=')
1840                         return 1;
1841                 else if (!is_in_name(*p))
1842                         return 0;
1843                 p++;
1844         }
1845 }
1846
1847
1848 static void
1849 consumetoken(int token)
1850 {
1851         if (readtoken() != token)
1852                 synexpect(token);
1853 }
1854
1855
1856 /*
1857  * Called when an unexpected token is read during the parse.  The argument
1858  * is the token that is expected, or -1 if more than one type of token can
1859  * occur at this point.
1860  */
1861
1862 static void
1863 synexpect(int token)
1864 {
1865         char msg[64];
1866
1867         if (token >= 0) {
1868                 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1869                         tokname[lasttoken], tokname[token]);
1870         } else {
1871                 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1872         }
1873         synerror(msg);
1874 }
1875
1876
1877 static void
1878 synerror(const char *msg)
1879 {
1880         if (commandname)
1881                 outfmt(out2, "%s: %d: ", commandname, startlinno);
1882         else if (arg0)
1883                 outfmt(out2, "%s: ", arg0);
1884         outfmt(out2, "Syntax error: %s\n", msg);
1885         error((char *)NULL);
1886 }
1887
1888 static void
1889 setprompt(int which)
1890 {
1891         whichprompt = which;
1892
1893 #ifndef NO_HISTORY
1894         if (!el)
1895 #endif
1896         {
1897                 out2str(getprompt(NULL));
1898                 flushout(out2);
1899         }
1900 }
1901
1902 static int
1903 pgetc_linecont(void)
1904 {
1905         int c;
1906
1907         while ((c = pgetc_macro()) == '\\') {
1908                 c = pgetc();
1909                 if (c == '\n') {
1910                         plinno++;
1911                         if (doprompt)
1912                                 setprompt(2);
1913                         else
1914                                 setprompt(0);
1915                 } else {
1916                         pungetc();
1917                         /* Allow the backslash to be pushed back. */
1918                         pushstring("\\", 1, NULL);
1919                         return (pgetc());
1920                 }
1921         }
1922         return (c);
1923 }
1924
1925 /*
1926  * called by editline -- any expansions to the prompt
1927  *    should be added here.
1928  */
1929 char *
1930 getprompt(void *unused __unused)
1931 {
1932         static char ps[PROMPTLEN];
1933         const char *fmt;
1934         const char *pwd;
1935         int i, trim;
1936         static char internal_error[] = "??";
1937
1938         /*
1939          * Select prompt format.
1940          */
1941         switch (whichprompt) {
1942         case 0:
1943                 fmt = "";
1944                 break;
1945         case 1:
1946                 fmt = ps1val();
1947                 break;
1948         case 2:
1949                 fmt = ps2val();
1950                 break;
1951         default:
1952                 return internal_error;
1953         }
1954
1955         /*
1956          * Format prompt string.
1957          */
1958         for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1959                 if (*fmt == '\\')
1960                         switch (*++fmt) {
1961
1962                                 /*
1963                                  * Hostname.
1964                                  *
1965                                  * \h specifies just the local hostname,
1966                                  * \H specifies fully-qualified hostname.
1967                                  */
1968                         case 'h':
1969                         case 'H':
1970                                 ps[i] = '\0';
1971                                 gethostname(&ps[i], PROMPTLEN - i);
1972                                 /* Skip to end of hostname. */
1973                                 trim = (*fmt == 'h') ? '.' : '\0';
1974                                 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1975                                         i++;
1976                                 break;
1977
1978                                 /*
1979                                  * Working directory.
1980                                  *
1981                                  * \W specifies just the final component,
1982                                  * \w specifies the entire path.
1983                                  */
1984                         case 'W':
1985                         case 'w':
1986                                 pwd = lookupvar("PWD");
1987                                 if (pwd == NULL)
1988                                         pwd = "?";
1989                                 if (*fmt == 'W' &&
1990                                     *pwd == '/' && pwd[1] != '\0')
1991                                         strlcpy(&ps[i], strrchr(pwd, '/') + 1,
1992                                             PROMPTLEN - i);
1993                                 else
1994                                         strlcpy(&ps[i], pwd, PROMPTLEN - i);
1995                                 /* Skip to end of path. */
1996                                 while (ps[i + 1] != '\0')
1997                                         i++;
1998                                 break;
1999
2000                                 /*
2001                                  * Superuser status.
2002                                  *
2003                                  * '$' for normal users, '#' for root.
2004                                  */
2005                         case '$':
2006                                 ps[i] = (geteuid() != 0) ? '$' : '#';
2007                                 break;
2008
2009                                 /*
2010                                  * A literal \.
2011                                  */
2012                         case '\\':
2013                                 ps[i] = '\\';
2014                                 break;
2015
2016                                 /*
2017                                  * Emit unrecognized formats verbatim.
2018                                  */
2019                         default:
2020                                 ps[i++] = '\\';
2021                                 ps[i] = *fmt;
2022                                 break;
2023                         }
2024                 else
2025                         ps[i] = *fmt;
2026         ps[i] = '\0';
2027         return (ps);
2028 }
2029
2030
2031 const char *
2032 expandstr(const char *ps)
2033 {
2034         union node n;
2035         struct jmploc jmploc;
2036         struct jmploc *const savehandler = handler;
2037         const int saveprompt = doprompt;
2038         struct parsefile *const savetopfile = getcurrentfile();
2039         struct parser_temp *const saveparser_temp = parser_temp;
2040         const char *result = NULL;
2041
2042         if (!setjmp(jmploc.loc)) {
2043                 handler = &jmploc;
2044                 parser_temp = NULL;
2045                 setinputstring(ps, 1);
2046                 doprompt = 0;
2047                 readtoken1(pgetc(), DQSYNTAX, "", 0);
2048                 if (backquotelist != NULL)
2049                         error("Command substitution not allowed here");
2050
2051                 n.narg.type = NARG;
2052                 n.narg.next = NULL;
2053                 n.narg.text = wordtext;
2054                 n.narg.backquote = backquotelist;
2055
2056                 expandarg(&n, NULL, 0);
2057                 result = stackblock();
2058                 INTOFF;
2059         }
2060         handler = savehandler;
2061         doprompt = saveprompt;
2062         popfilesupto(savetopfile);
2063         if (parser_temp != saveparser_temp) {
2064                 parser_temp_free_all();
2065                 parser_temp = saveparser_temp;
2066         }
2067         if (result != NULL) {
2068                 INTON;
2069         } else if (exception == EXINT)
2070                 raise(SIGINT);
2071         return result;
2072 }