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