]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - bin/sh/parser.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.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  * 4. 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 EOFMARKLEN      79
70 #define PROMPTLEN       128
71
72 /* values of checkkwd variable */
73 #define CHKALIAS        0x1
74 #define CHKKWD          0x2
75 #define CHKNL           0x4
76
77 /* values returned by readtoken */
78 #include "token.h"
79
80
81
82 struct heredoc {
83         struct heredoc *next;   /* next here document in list */
84         union node *here;               /* redirection node */
85         char *eofmark;          /* string indicating end of input */
86         int striptabs;          /* if set, strip leading tabs */
87 };
88
89 struct parser_temp {
90         struct parser_temp *next;
91         void *data;
92 };
93
94
95 static struct heredoc *heredoclist;     /* list of here documents to read */
96 static int doprompt;            /* if set, prompt the user */
97 static int needprompt;          /* true if interactive and at start of line */
98 static int lasttoken;           /* last token read */
99 static int tokpushback;         /* last token pushed back */
100 static char *wordtext;          /* text of last word returned by readtoken */
101 static int checkkwd;
102 static struct nodelist *backquotelist;
103 static union node *redirnode;
104 static struct heredoc *heredoc;
105 static int quoteflag;           /* set if (part of) last token was quoted */
106 static int startlinno;          /* line # where last token started */
107 static int funclinno;           /* line # where the current function started */
108 static struct parser_temp *parser_temp;
109
110
111 static union node *list(int);
112 static union node *andor(void);
113 static union node *pipeline(void);
114 static union node *command(void);
115 static union node *simplecmd(union node **, union node *);
116 static union node *makename(void);
117 static union node *makebinary(int type, union node *n1, union node *n2);
118 static void parsefname(void);
119 static void parseheredoc(void);
120 static int peektoken(void);
121 static int readtoken(void);
122 static int xxreadtoken(void);
123 static int readtoken1(int, const char *, const char *, int);
124 static int noexpand(char *);
125 static void consumetoken(int);
126 static void synexpect(int) __dead2;
127 static void synerror(const char *) __dead2;
128 static void setprompt(int);
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 fixredir(union node *n, const char *text, int err)
687 {
688         TRACE(("Fix redir %s %d\n", text, err));
689         if (!err)
690                 n->ndup.vname = NULL;
691
692         if (is_digit(text[0]) && text[1] == '\0')
693                 n->ndup.dupfd = digit_val(text[0]);
694         else if (text[0] == '-' && text[1] == '\0')
695                 n->ndup.dupfd = -1;
696         else {
697
698                 if (err)
699                         synerror("Bad fd number");
700                 else
701                         n->ndup.vname = makename();
702         }
703 }
704
705
706 static void
707 parsefname(void)
708 {
709         union node *n = redirnode;
710
711         consumetoken(TWORD);
712         if (n->type == NHERE) {
713                 struct heredoc *here = heredoc;
714                 struct heredoc *p;
715                 int i;
716
717                 if (quoteflag == 0)
718                         n->type = NXHERE;
719                 TRACE(("Here document %d\n", n->type));
720                 if (here->striptabs) {
721                         while (*wordtext == '\t')
722                                 wordtext++;
723                 }
724                 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
725                         synerror("Illegal eof marker for << redirection");
726                 rmescapes(wordtext);
727                 here->eofmark = wordtext;
728                 here->next = NULL;
729                 if (heredoclist == NULL)
730                         heredoclist = here;
731                 else {
732                         for (p = heredoclist ; p->next ; p = p->next);
733                         p->next = here;
734                 }
735         } else if (n->type == NTOFD || n->type == NFROMFD) {
736                 fixredir(n, wordtext, 0);
737         } else {
738                 n->nfile.fname = makename();
739         }
740 }
741
742
743 /*
744  * Input any here documents.
745  */
746
747 static void
748 parseheredoc(void)
749 {
750         struct heredoc *here;
751         union node *n;
752
753         while (heredoclist) {
754                 here = heredoclist;
755                 heredoclist = here->next;
756                 if (needprompt) {
757                         setprompt(2);
758                         needprompt = 0;
759                 }
760                 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
761                                 here->eofmark, here->striptabs);
762                 n = makename();
763                 here->here->nhere.doc = n;
764         }
765 }
766
767 static int
768 peektoken(void)
769 {
770         int t;
771
772         t = readtoken();
773         tokpushback++;
774         return (t);
775 }
776
777 static int
778 readtoken(void)
779 {
780         int t;
781         struct alias *ap;
782 #ifdef DEBUG
783         int alreadyseen = tokpushback;
784 #endif
785
786         top:
787         t = xxreadtoken();
788
789         /*
790          * eat newlines
791          */
792         if (checkkwd & CHKNL) {
793                 while (t == TNL) {
794                         parseheredoc();
795                         t = xxreadtoken();
796                 }
797         }
798
799         /*
800          * check for keywords and aliases
801          */
802         if (t == TWORD && !quoteflag)
803         {
804                 const char * const *pp;
805
806                 if (checkkwd & CHKKWD)
807                         for (pp = parsekwd; *pp; pp++) {
808                                 if (**pp == *wordtext && equal(*pp, wordtext))
809                                 {
810                                         lasttoken = t = pp - parsekwd + KWDOFFSET;
811                                         TRACE(("keyword %s recognized\n", tokname[t]));
812                                         goto out;
813                                 }
814                         }
815                 if (checkkwd & CHKALIAS &&
816                     (ap = lookupalias(wordtext, 1)) != NULL) {
817                         pushstring(ap->val, strlen(ap->val), ap);
818                         goto top;
819                 }
820         }
821 out:
822         if (t != TNOT)
823                 checkkwd = 0;
824
825 #ifdef DEBUG
826         if (!alreadyseen)
827             TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
828         else
829             TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
830 #endif
831         return (t);
832 }
833
834
835 /*
836  * Read the next input token.
837  * If the token is a word, we set backquotelist to the list of cmds in
838  *      backquotes.  We set quoteflag to true if any part of the word was
839  *      quoted.
840  * If the token is TREDIR, then we set redirnode to a structure containing
841  *      the redirection.
842  * In all cases, the variable startlinno is set to the number of the line
843  *      on which the token starts.
844  *
845  * [Change comment:  here documents and internal procedures]
846  * [Readtoken shouldn't have any arguments.  Perhaps we should make the
847  *  word parsing code into a separate routine.  In this case, readtoken
848  *  doesn't need to have any internal procedures, but parseword does.
849  *  We could also make parseoperator in essence the main routine, and
850  *  have parseword (readtoken1?) handle both words and redirection.]
851  */
852
853 #define RETURN(token)   return lasttoken = token
854
855 static int
856 xxreadtoken(void)
857 {
858         int c;
859
860         if (tokpushback) {
861                 tokpushback = 0;
862                 return lasttoken;
863         }
864         if (needprompt) {
865                 setprompt(2);
866                 needprompt = 0;
867         }
868         startlinno = plinno;
869         for (;;) {      /* until token or start of word found */
870                 c = pgetc_macro();
871                 switch (c) {
872                 case ' ': case '\t':
873                         continue;
874                 case '#':
875                         while ((c = pgetc()) != '\n' && c != PEOF);
876                         pungetc();
877                         continue;
878                 case '\\':
879                         if (pgetc() == '\n') {
880                                 startlinno = ++plinno;
881                                 if (doprompt)
882                                         setprompt(2);
883                                 else
884                                         setprompt(0);
885                                 continue;
886                         }
887                         pungetc();
888                         goto breakloop;
889                 case '\n':
890                         plinno++;
891                         needprompt = doprompt;
892                         RETURN(TNL);
893                 case PEOF:
894                         RETURN(TEOF);
895                 case '&':
896                         if (pgetc() == '&')
897                                 RETURN(TAND);
898                         pungetc();
899                         RETURN(TBACKGND);
900                 case '|':
901                         if (pgetc() == '|')
902                                 RETURN(TOR);
903                         pungetc();
904                         RETURN(TPIPE);
905                 case ';':
906                         c = pgetc();
907                         if (c == ';')
908                                 RETURN(TENDCASE);
909                         else if (c == '&')
910                                 RETURN(TFALLTHRU);
911                         pungetc();
912                         RETURN(TSEMI);
913                 case '(':
914                         RETURN(TLP);
915                 case ')':
916                         RETURN(TRP);
917                 default:
918                         goto breakloop;
919                 }
920         }
921 breakloop:
922         return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
923 #undef RETURN
924 }
925
926
927 #define MAXNEST_static 8
928 struct tokenstate
929 {
930         const char *syntax; /* *SYNTAX */
931         int parenlevel; /* levels of parentheses in arithmetic */
932         enum tokenstate_category
933         {
934                 TSTATE_TOP,
935                 TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
936                 TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
937                 TSTATE_ARITH
938         } category;
939 };
940
941
942 /*
943  * Called to parse command substitutions.
944  */
945
946 static char *
947 parsebackq(char *out, struct nodelist **pbqlist,
948                 int oldstyle, int dblquote, int quoted)
949 {
950         struct nodelist **nlpp;
951         union node *n;
952         char *volatile str;
953         struct jmploc jmploc;
954         struct jmploc *const savehandler = handler;
955         size_t savelen;
956         int saveprompt;
957         const int bq_startlinno = plinno;
958         char *volatile ostr = NULL;
959         struct parsefile *const savetopfile = getcurrentfile();
960         struct heredoc *const saveheredoclist = heredoclist;
961         struct heredoc *here;
962
963         str = NULL;
964         if (setjmp(jmploc.loc)) {
965                 popfilesupto(savetopfile);
966                 if (str)
967                         ckfree(str);
968                 if (ostr)
969                         ckfree(ostr);
970                 heredoclist = saveheredoclist;
971                 handler = savehandler;
972                 if (exception == EXERROR) {
973                         startlinno = bq_startlinno;
974                         synerror("Error in command substitution");
975                 }
976                 longjmp(handler->loc, 1);
977         }
978         INTOFF;
979         savelen = out - stackblock();
980         if (savelen > 0) {
981                 str = ckmalloc(savelen);
982                 memcpy(str, stackblock(), savelen);
983         }
984         handler = &jmploc;
985         heredoclist = NULL;
986         INTON;
987         if (oldstyle) {
988                 /* We must read until the closing backquote, giving special
989                    treatment to some slashes, and then push the string and
990                    reread it as input, interpreting it normally.  */
991                 char *oout;
992                 int c;
993                 int olen;
994
995
996                 STARTSTACKSTR(oout);
997                 for (;;) {
998                         if (needprompt) {
999                                 setprompt(2);
1000                                 needprompt = 0;
1001                         }
1002                         CHECKSTRSPACE(2, oout);
1003                         switch (c = pgetc()) {
1004                         case '`':
1005                                 goto done;
1006
1007                         case '\\':
1008                                 if ((c = pgetc()) == '\n') {
1009                                         plinno++;
1010                                         if (doprompt)
1011                                                 setprompt(2);
1012                                         else
1013                                                 setprompt(0);
1014                                         /*
1015                                          * If eating a newline, avoid putting
1016                                          * the newline into the new character
1017                                          * stream (via the USTPUTC after the
1018                                          * switch).
1019                                          */
1020                                         continue;
1021                                 }
1022                                 if (c != '\\' && c != '`' && c != '$'
1023                                     && (!dblquote || c != '"'))
1024                                         USTPUTC('\\', oout);
1025                                 break;
1026
1027                         case '\n':
1028                                 plinno++;
1029                                 needprompt = doprompt;
1030                                 break;
1031
1032                         case PEOF:
1033                                 startlinno = plinno;
1034                                 synerror("EOF in backquote substitution");
1035                                 break;
1036
1037                         default:
1038                                 break;
1039                         }
1040                         USTPUTC(c, oout);
1041                 }
1042 done:
1043                 USTPUTC('\0', oout);
1044                 olen = oout - stackblock();
1045                 INTOFF;
1046                 ostr = ckmalloc(olen);
1047                 memcpy(ostr, stackblock(), olen);
1048                 setinputstring(ostr, 1);
1049                 INTON;
1050         }
1051         nlpp = pbqlist;
1052         while (*nlpp)
1053                 nlpp = &(*nlpp)->next;
1054         *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1055         (*nlpp)->next = NULL;
1056
1057         if (oldstyle) {
1058                 saveprompt = doprompt;
1059                 doprompt = 0;
1060         }
1061
1062         n = list(0);
1063
1064         if (oldstyle) {
1065                 if (peektoken() != TEOF)
1066                         synexpect(-1);
1067                 doprompt = saveprompt;
1068         } else
1069                 consumetoken(TRP);
1070
1071         (*nlpp)->n = n;
1072         if (oldstyle) {
1073                 /*
1074                  * Start reading from old file again, ignoring any pushed back
1075                  * tokens left from the backquote parsing
1076                  */
1077                 popfile();
1078                 tokpushback = 0;
1079         }
1080         STARTSTACKSTR(out);
1081         CHECKSTRSPACE(savelen + 1, out);
1082         INTOFF;
1083         if (str) {
1084                 memcpy(out, str, savelen);
1085                 STADJUST(savelen, out);
1086                 ckfree(str);
1087                 str = NULL;
1088         }
1089         if (ostr) {
1090                 ckfree(ostr);
1091                 ostr = NULL;
1092         }
1093         here = saveheredoclist;
1094         if (here != NULL) {
1095                 while (here->next != NULL)
1096                         here = here->next;
1097                 here->next = heredoclist;
1098                 heredoclist = saveheredoclist;
1099         }
1100         handler = savehandler;
1101         INTON;
1102         if (quoted)
1103                 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1104         else
1105                 USTPUTC(CTLBACKQ, out);
1106         return out;
1107 }
1108
1109
1110 /*
1111  * Called to parse a backslash escape sequence inside $'...'.
1112  * The backslash has already been read.
1113  */
1114 static char *
1115 readcstyleesc(char *out)
1116 {
1117         int c, v, i, n;
1118
1119         c = pgetc();
1120         switch (c) {
1121         case '\0':
1122                 synerror("Unterminated quoted string");
1123         case '\n':
1124                 plinno++;
1125                 if (doprompt)
1126                         setprompt(2);
1127                 else
1128                         setprompt(0);
1129                 return out;
1130         case '\\':
1131         case '\'':
1132         case '"':
1133                 v = c;
1134                 break;
1135         case 'a': v = '\a'; break;
1136         case 'b': v = '\b'; break;
1137         case 'e': v = '\033'; break;
1138         case 'f': v = '\f'; break;
1139         case 'n': v = '\n'; break;
1140         case 'r': v = '\r'; break;
1141         case 't': v = '\t'; break;
1142         case 'v': v = '\v'; break;
1143         case 'x':
1144                   v = 0;
1145                   for (;;) {
1146                           c = pgetc();
1147                           if (c >= '0' && c <= '9')
1148                                   v = (v << 4) + c - '0';
1149                           else if (c >= 'A' && c <= 'F')
1150                                   v = (v << 4) + c - 'A' + 10;
1151                           else if (c >= 'a' && c <= 'f')
1152                                   v = (v << 4) + c - 'a' + 10;
1153                           else
1154                                   break;
1155                   }
1156                   pungetc();
1157                   break;
1158         case '0': case '1': case '2': case '3':
1159         case '4': case '5': case '6': case '7':
1160                   v = c - '0';
1161                   c = pgetc();
1162                   if (c >= '0' && c <= '7') {
1163                           v <<= 3;
1164                           v += c - '0';
1165                           c = pgetc();
1166                           if (c >= '0' && c <= '7') {
1167                                   v <<= 3;
1168                                   v += c - '0';
1169                           } else
1170                                   pungetc();
1171                   } else
1172                           pungetc();
1173                   break;
1174         case 'c':
1175                   c = pgetc();
1176                   if (c < 0x3f || c > 0x7a || c == 0x60)
1177                           synerror("Bad escape sequence");
1178                   if (c == '\\' && pgetc() != '\\')
1179                           synerror("Bad escape sequence");
1180                   if (c == '?')
1181                           v = 127;
1182                   else
1183                           v = c & 0x1f;
1184                   break;
1185         case 'u':
1186         case 'U':
1187                   n = c == 'U' ? 8 : 4;
1188                   v = 0;
1189                   for (i = 0; i < n; i++) {
1190                           c = pgetc();
1191                           if (c >= '0' && c <= '9')
1192                                   v = (v << 4) + c - '0';
1193                           else if (c >= 'A' && c <= 'F')
1194                                   v = (v << 4) + c - 'A' + 10;
1195                           else if (c >= 'a' && c <= 'f')
1196                                   v = (v << 4) + c - 'a' + 10;
1197                           else
1198                                   synerror("Bad escape sequence");
1199                   }
1200                   if (v == 0 || (v >= 0xd800 && v <= 0xdfff))
1201                           synerror("Bad escape sequence");
1202                   /* We really need iconv here. */
1203                   if (initial_localeisutf8 && v > 127) {
1204                           CHECKSTRSPACE(4, out);
1205                           /*
1206                            * We cannot use wctomb() as the locale may have
1207                            * changed.
1208                            */
1209                           if (v <= 0x7ff) {
1210                                   USTPUTC(0xc0 | v >> 6, out);
1211                                   USTPUTC(0x80 | (v & 0x3f), out);
1212                                   return out;
1213                           } else if (v <= 0xffff) {
1214                                   USTPUTC(0xe0 | v >> 12, out);
1215                                   USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1216                                   USTPUTC(0x80 | (v & 0x3f), out);
1217                                   return out;
1218                           } else if (v <= 0x10ffff) {
1219                                   USTPUTC(0xf0 | v >> 18, out);
1220                                   USTPUTC(0x80 | ((v >> 12) & 0x3f), out);
1221                                   USTPUTC(0x80 | ((v >> 6) & 0x3f), out);
1222                                   USTPUTC(0x80 | (v & 0x3f), out);
1223                                   return out;
1224                           }
1225                   }
1226                   if (v > 127)
1227                           v = '?';
1228                   break;
1229         default:
1230                   synerror("Bad escape sequence");
1231         }
1232         v = (char)v;
1233         /*
1234          * We can't handle NUL bytes.
1235          * POSIX says we should skip till the closing quote.
1236          */
1237         if (v == '\0') {
1238                 while ((c = pgetc()) != '\'') {
1239                         if (c == '\\')
1240                                 c = pgetc();
1241                         if (c == PEOF)
1242                                 synerror("Unterminated quoted string");
1243                 }
1244                 pungetc();
1245                 return out;
1246         }
1247         if (SQSYNTAX[v] == CCTL)
1248                 USTPUTC(CTLESC, out);
1249         USTPUTC(v, out);
1250         return out;
1251 }
1252
1253
1254 /*
1255  * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
1256  * is not NULL, read a here document.  In the latter case, eofmark is the
1257  * word which marks the end of the document and striptabs is true if
1258  * leading tabs should be stripped from the document.  The argument firstc
1259  * is the first character of the input token or document.
1260  *
1261  * Because C does not have internal subroutines, I have simulated them
1262  * using goto's to implement the subroutine linkage.  The following macros
1263  * will run code that appears at the end of readtoken1.
1264  */
1265
1266 #define CHECKEND()      {goto checkend; checkend_return:;}
1267 #define PARSEREDIR()    {goto parseredir; parseredir_return:;}
1268 #define PARSESUB()      {goto parsesub; parsesub_return:;}
1269 #define PARSEARITH()    {goto parsearith; parsearith_return:;}
1270
1271 static int
1272 readtoken1(int firstc, char const *initialsyntax, const char *eofmark,
1273     int striptabs)
1274 {
1275         int c = firstc;
1276         char *out;
1277         int len;
1278         char line[EOFMARKLEN + 1];
1279         struct nodelist *bqlist;
1280         int quotef;
1281         int newvarnest;
1282         int level;
1283         int synentry;
1284         struct tokenstate state_static[MAXNEST_static];
1285         int maxnest = MAXNEST_static;
1286         struct tokenstate *state = state_static;
1287         int sqiscstyle = 0;
1288
1289         startlinno = plinno;
1290         quotef = 0;
1291         bqlist = NULL;
1292         newvarnest = 0;
1293         level = 0;
1294         state[level].syntax = initialsyntax;
1295         state[level].parenlevel = 0;
1296         state[level].category = TSTATE_TOP;
1297
1298         STARTSTACKSTR(out);
1299         loop: { /* for each line, until end of word */
1300                 CHECKEND();     /* set c to PEOF if at end of here document */
1301                 for (;;) {      /* until end of line or end of word */
1302                         CHECKSTRSPACE(4, out);  /* permit 4 calls to USTPUTC */
1303
1304                         synentry = state[level].syntax[c];
1305
1306                         switch(synentry) {
1307                         case CNL:       /* '\n' */
1308                                 if (state[level].syntax == BASESYNTAX)
1309                                         goto endword;   /* exit outer loop */
1310                                 USTPUTC(c, out);
1311                                 plinno++;
1312                                 if (doprompt)
1313                                         setprompt(2);
1314                                 else
1315                                         setprompt(0);
1316                                 c = pgetc();
1317                                 goto loop;              /* continue outer loop */
1318                         case CSBACK:
1319                                 if (sqiscstyle) {
1320                                         out = readcstyleesc(out);
1321                                         break;
1322                                 }
1323                                 /* FALLTHROUGH */
1324                         case CWORD:
1325                                 USTPUTC(c, out);
1326                                 break;
1327                         case CCTL:
1328                                 if (eofmark == NULL || initialsyntax != SQSYNTAX)
1329                                         USTPUTC(CTLESC, out);
1330                                 USTPUTC(c, out);
1331                                 break;
1332                         case CBACK:     /* backslash */
1333                                 c = pgetc();
1334                                 if (c == PEOF) {
1335                                         USTPUTC('\\', out);
1336                                         pungetc();
1337                                 } else if (c == '\n') {
1338                                         plinno++;
1339                                         if (doprompt)
1340                                                 setprompt(2);
1341                                         else
1342                                                 setprompt(0);
1343                                 } else {
1344                                         if (state[level].syntax == DQSYNTAX &&
1345                                             c != '\\' && c != '`' && c != '$' &&
1346                                             (c != '"' || (eofmark != NULL &&
1347                                                 newvarnest == 0)) &&
1348                                             (c != '}' || state[level].category != TSTATE_VAR_OLD))
1349                                                 USTPUTC('\\', out);
1350                                         if ((eofmark == NULL ||
1351                                             newvarnest > 0) &&
1352                                             state[level].syntax == BASESYNTAX)
1353                                                 USTPUTC(CTLQUOTEMARK, out);
1354                                         if (SQSYNTAX[c] == CCTL)
1355                                                 USTPUTC(CTLESC, out);
1356                                         USTPUTC(c, out);
1357                                         if ((eofmark == NULL ||
1358                                             newvarnest > 0) &&
1359                                             state[level].syntax == BASESYNTAX &&
1360                                             state[level].category == TSTATE_VAR_OLD)
1361                                                 USTPUTC(CTLQUOTEEND, out);
1362                                         quotef++;
1363                                 }
1364                                 break;
1365                         case CSQUOTE:
1366                                 USTPUTC(CTLQUOTEMARK, out);
1367                                 state[level].syntax = SQSYNTAX;
1368                                 sqiscstyle = 0;
1369                                 break;
1370                         case CDQUOTE:
1371                                 USTPUTC(CTLQUOTEMARK, out);
1372                                 state[level].syntax = DQSYNTAX;
1373                                 break;
1374                         case CENDQUOTE:
1375                                 if (eofmark != NULL && newvarnest == 0)
1376                                         USTPUTC(c, out);
1377                                 else {
1378                                         if (state[level].category == TSTATE_VAR_OLD)
1379                                                 USTPUTC(CTLQUOTEEND, out);
1380                                         state[level].syntax = BASESYNTAX;
1381                                         quotef++;
1382                                 }
1383                                 break;
1384                         case CVAR:      /* '$' */
1385                                 PARSESUB();             /* parse substitution */
1386                                 break;
1387                         case CENDVAR:   /* '}' */
1388                                 if (level > 0 &&
1389                                     ((state[level].category == TSTATE_VAR_OLD &&
1390                                       state[level].syntax ==
1391                                       state[level - 1].syntax) ||
1392                                     (state[level].category == TSTATE_VAR_NEW &&
1393                                      state[level].syntax == BASESYNTAX))) {
1394                                         if (state[level].category == TSTATE_VAR_NEW)
1395                                                 newvarnest--;
1396                                         level--;
1397                                         USTPUTC(CTLENDVAR, out);
1398                                 } else {
1399                                         USTPUTC(c, out);
1400                                 }
1401                                 break;
1402                         case CLP:       /* '(' in arithmetic */
1403                                 state[level].parenlevel++;
1404                                 USTPUTC(c, out);
1405                                 break;
1406                         case CRP:       /* ')' in arithmetic */
1407                                 if (state[level].parenlevel > 0) {
1408                                         USTPUTC(c, out);
1409                                         --state[level].parenlevel;
1410                                 } else {
1411                                         if (pgetc() == ')') {
1412                                                 if (level > 0 &&
1413                                                     state[level].category == TSTATE_ARITH) {
1414                                                         level--;
1415                                                         USTPUTC(CTLENDARI, out);
1416                                                 } else
1417                                                         USTPUTC(')', out);
1418                                         } else {
1419                                                 /*
1420                                                  * unbalanced parens
1421                                                  *  (don't 2nd guess - no error)
1422                                                  */
1423                                                 pungetc();
1424                                                 USTPUTC(')', out);
1425                                         }
1426                                 }
1427                                 break;
1428                         case CBQUOTE:   /* '`' */
1429                                 out = parsebackq(out, &bqlist, 1,
1430                                     state[level].syntax == DQSYNTAX &&
1431                                     (eofmark == NULL || newvarnest > 0),
1432                                     state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
1433                                 break;
1434                         case CEOF:
1435                                 goto endword;           /* exit outer loop */
1436                         case CIGN:
1437                                 break;
1438                         default:
1439                                 if (level == 0)
1440                                         goto endword;   /* exit outer loop */
1441                                 USTPUTC(c, out);
1442                         }
1443                         c = pgetc_macro();
1444                 }
1445         }
1446 endword:
1447         if (state[level].syntax == ARISYNTAX)
1448                 synerror("Missing '))'");
1449         if (state[level].syntax != BASESYNTAX && eofmark == NULL)
1450                 synerror("Unterminated quoted string");
1451         if (state[level].category == TSTATE_VAR_OLD ||
1452             state[level].category == TSTATE_VAR_NEW) {
1453                 startlinno = plinno;
1454                 synerror("Missing '}'");
1455         }
1456         if (state != state_static)
1457                 parser_temp_free_upto(state);
1458         USTPUTC('\0', out);
1459         len = out - stackblock();
1460         out = stackblock();
1461         if (eofmark == NULL) {
1462                 if ((c == '>' || c == '<')
1463                  && quotef == 0
1464                  && len <= 2
1465                  && (*out == '\0' || is_digit(*out))) {
1466                         PARSEREDIR();
1467                         return lasttoken = TREDIR;
1468                 } else {
1469                         pungetc();
1470                 }
1471         }
1472         quoteflag = quotef;
1473         backquotelist = bqlist;
1474         grabstackblock(len);
1475         wordtext = out;
1476         return lasttoken = TWORD;
1477 /* end of readtoken routine */
1478
1479
1480 /*
1481  * Check to see whether we are at the end of the here document.  When this
1482  * is called, c is set to the first character of the next input line.  If
1483  * we are at the end of the here document, this routine sets the c to PEOF.
1484  */
1485
1486 checkend: {
1487         if (eofmark) {
1488                 if (striptabs) {
1489                         while (c == '\t')
1490                                 c = pgetc();
1491                 }
1492                 if (c == *eofmark) {
1493                         if (pfgets(line, sizeof line) != NULL) {
1494                                 const char *p, *q;
1495
1496                                 p = line;
1497                                 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1498                                 if ((*p == '\0' || *p == '\n') && *q == '\0') {
1499                                         c = PEOF;
1500                                         if (*p == '\n') {
1501                                                 plinno++;
1502                                                 needprompt = doprompt;
1503                                         }
1504                                 } else {
1505                                         pushstring(line, strlen(line), NULL);
1506                                 }
1507                         }
1508                 }
1509         }
1510         goto checkend_return;
1511 }
1512
1513
1514 /*
1515  * Parse a redirection operator.  The variable "out" points to a string
1516  * specifying the fd to be redirected.  The variable "c" contains the
1517  * first character of the redirection operator.
1518  */
1519
1520 parseredir: {
1521         char fd = *out;
1522         union node *np;
1523
1524         np = (union node *)stalloc(sizeof (struct nfile));
1525         if (c == '>') {
1526                 np->nfile.fd = 1;
1527                 c = pgetc();
1528                 if (c == '>')
1529                         np->type = NAPPEND;
1530                 else if (c == '&')
1531                         np->type = NTOFD;
1532                 else if (c == '|')
1533                         np->type = NCLOBBER;
1534                 else {
1535                         np->type = NTO;
1536                         pungetc();
1537                 }
1538         } else {        /* c == '<' */
1539                 np->nfile.fd = 0;
1540                 c = pgetc();
1541                 if (c == '<') {
1542                         if (sizeof (struct nfile) != sizeof (struct nhere)) {
1543                                 np = (union node *)stalloc(sizeof (struct nhere));
1544                                 np->nfile.fd = 0;
1545                         }
1546                         np->type = NHERE;
1547                         heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1548                         heredoc->here = np;
1549                         if ((c = pgetc()) == '-') {
1550                                 heredoc->striptabs = 1;
1551                         } else {
1552                                 heredoc->striptabs = 0;
1553                                 pungetc();
1554                         }
1555                 } else if (c == '&')
1556                         np->type = NFROMFD;
1557                 else if (c == '>')
1558                         np->type = NFROMTO;
1559                 else {
1560                         np->type = NFROM;
1561                         pungetc();
1562                 }
1563         }
1564         if (fd != '\0')
1565                 np->nfile.fd = digit_val(fd);
1566         redirnode = np;
1567         goto parseredir_return;
1568 }
1569
1570
1571 /*
1572  * Parse a substitution.  At this point, we have read the dollar sign
1573  * and nothing else.
1574  */
1575
1576 parsesub: {
1577         char buf[10];
1578         int subtype;
1579         int typeloc;
1580         int flags;
1581         char *p;
1582         static const char types[] = "}-+?=";
1583         int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1584         int linno;
1585         int length;
1586         int c1;
1587
1588         c = pgetc();
1589         if (c == '(') { /* $(command) or $((arith)) */
1590                 if (pgetc() == '(') {
1591                         PARSEARITH();
1592                 } else {
1593                         pungetc();
1594                         out = parsebackq(out, &bqlist, 0,
1595                             state[level].syntax == DQSYNTAX &&
1596                             (eofmark == NULL || newvarnest > 0),
1597                             state[level].syntax == DQSYNTAX ||
1598                             state[level].syntax == ARISYNTAX);
1599                 }
1600         } else if (c == '{' || is_name(c) || is_special(c)) {
1601                 USTPUTC(CTLVAR, out);
1602                 typeloc = out - stackblock();
1603                 USTPUTC(VSNORMAL, out);
1604                 subtype = VSNORMAL;
1605                 flags = 0;
1606                 if (c == '{') {
1607                         bracketed_name = 1;
1608                         c = pgetc();
1609                         subtype = 0;
1610                 }
1611 varname:
1612                 if (!is_eof(c) && is_name(c)) {
1613                         length = 0;
1614                         do {
1615                                 STPUTC(c, out);
1616                                 c = pgetc();
1617                                 length++;
1618                         } while (!is_eof(c) && is_in_name(c));
1619                         if (length == 6 &&
1620                             strncmp(out - length, "LINENO", length) == 0) {
1621                                 /* Replace the variable name with the
1622                                  * current line number. */
1623                                 linno = plinno;
1624                                 if (funclinno != 0)
1625                                         linno -= funclinno - 1;
1626                                 snprintf(buf, sizeof(buf), "%d", linno);
1627                                 STADJUST(-6, out);
1628                                 STPUTS(buf, out);
1629                                 flags |= VSLINENO;
1630                         }
1631                 } else if (is_digit(c)) {
1632                         if (bracketed_name) {
1633                                 do {
1634                                         STPUTC(c, out);
1635                                         c = pgetc();
1636                                 } while (is_digit(c));
1637                         } else {
1638                                 STPUTC(c, out);
1639                                 c = pgetc();
1640                         }
1641                 } else if (is_special(c)) {
1642                         c1 = c;
1643                         c = pgetc();
1644                         if (subtype == 0 && c1 == '#') {
1645                                 subtype = VSLENGTH;
1646                                 if (strchr(types, c) == NULL && c != ':' &&
1647                                     c != '#' && c != '%')
1648                                         goto varname;
1649                                 c1 = c;
1650                                 c = pgetc();
1651                                 if (c1 != '}' && c == '}') {
1652                                         pungetc();
1653                                         c = c1;
1654                                         goto varname;
1655                                 }
1656                                 pungetc();
1657                                 c = c1;
1658                                 c1 = '#';
1659                                 subtype = 0;
1660                         }
1661                         USTPUTC(c1, out);
1662                 } else {
1663                         subtype = VSERROR;
1664                         if (c == '}')
1665                                 pungetc();
1666                         else if (c == '\n' || c == PEOF)
1667                                 synerror("Unexpected end of line in substitution");
1668                         else
1669                                 USTPUTC(c, out);
1670                 }
1671                 if (subtype == 0) {
1672                         switch (c) {
1673                         case ':':
1674                                 flags |= VSNUL;
1675                                 c = pgetc();
1676                                 /*FALLTHROUGH*/
1677                         default:
1678                                 p = strchr(types, c);
1679                                 if (p == NULL) {
1680                                         if (c == '\n' || c == PEOF)
1681                                                 synerror("Unexpected end of line in substitution");
1682                                         if (flags == VSNUL)
1683                                                 STPUTC(':', out);
1684                                         STPUTC(c, out);
1685                                         subtype = VSERROR;
1686                                 } else
1687                                         subtype = p - types + VSNORMAL;
1688                                 break;
1689                         case '%':
1690                         case '#':
1691                                 {
1692                                         int cc = c;
1693                                         subtype = c == '#' ? VSTRIMLEFT :
1694                                                              VSTRIMRIGHT;
1695                                         c = pgetc();
1696                                         if (c == cc)
1697                                                 subtype++;
1698                                         else
1699                                                 pungetc();
1700                                         break;
1701                                 }
1702                         }
1703                 } else if (subtype != VSERROR) {
1704                         if (subtype == VSLENGTH && c != '}')
1705                                 subtype = VSERROR;
1706                         pungetc();
1707                 }
1708                 STPUTC('=', out);
1709                 if (state[level].syntax == DQSYNTAX ||
1710                     state[level].syntax == ARISYNTAX)
1711                         flags |= VSQUOTE;
1712                 *(stackblock() + typeloc) = subtype | flags;
1713                 if (subtype != VSNORMAL) {
1714                         if (level + 1 >= maxnest) {
1715                                 maxnest *= 2;
1716                                 if (state == state_static) {
1717                                         state = parser_temp_alloc(
1718                                             maxnest * sizeof(*state));
1719                                         memcpy(state, state_static,
1720                                             MAXNEST_static * sizeof(*state));
1721                                 } else
1722                                         state = parser_temp_realloc(state,
1723                                             maxnest * sizeof(*state));
1724                         }
1725                         level++;
1726                         state[level].parenlevel = 0;
1727                         if (subtype == VSMINUS || subtype == VSPLUS ||
1728                             subtype == VSQUESTION || subtype == VSASSIGN) {
1729                                 /*
1730                                  * For operators that were in the Bourne shell,
1731                                  * inherit the double-quote state.
1732                                  */
1733                                 state[level].syntax = state[level - 1].syntax;
1734                                 state[level].category = TSTATE_VAR_OLD;
1735                         } else {
1736                                 /*
1737                                  * The other operators take a pattern,
1738                                  * so go to BASESYNTAX.
1739                                  * Also, ' and " are now special, even
1740                                  * in here documents.
1741                                  */
1742                                 state[level].syntax = BASESYNTAX;
1743                                 state[level].category = TSTATE_VAR_NEW;
1744                                 newvarnest++;
1745                         }
1746                 }
1747         } else if (c == '\'' && state[level].syntax == BASESYNTAX) {
1748                 /* $'cstylequotes' */
1749                 USTPUTC(CTLQUOTEMARK, out);
1750                 state[level].syntax = SQSYNTAX;
1751                 sqiscstyle = 1;
1752         } else {
1753                 USTPUTC('$', out);
1754                 pungetc();
1755         }
1756         goto parsesub_return;
1757 }
1758
1759
1760 /*
1761  * Parse an arithmetic expansion (indicate start of one and set state)
1762  */
1763 parsearith: {
1764
1765         if (level + 1 >= maxnest) {
1766                 maxnest *= 2;
1767                 if (state == state_static) {
1768                         state = parser_temp_alloc(
1769                             maxnest * sizeof(*state));
1770                         memcpy(state, state_static,
1771                             MAXNEST_static * sizeof(*state));
1772                 } else
1773                         state = parser_temp_realloc(state,
1774                             maxnest * sizeof(*state));
1775         }
1776         level++;
1777         state[level].syntax = ARISYNTAX;
1778         state[level].parenlevel = 0;
1779         state[level].category = TSTATE_ARITH;
1780         USTPUTC(CTLARI, out);
1781         if (state[level - 1].syntax == DQSYNTAX)
1782                 USTPUTC('"',out);
1783         else
1784                 USTPUTC(' ',out);
1785         goto parsearith_return;
1786 }
1787
1788 } /* end of readtoken */
1789
1790
1791 /*
1792  * Returns true if the text contains nothing to expand (no dollar signs
1793  * or backquotes).
1794  */
1795
1796 static int
1797 noexpand(char *text)
1798 {
1799         char *p;
1800         char c;
1801
1802         p = text;
1803         while ((c = *p++) != '\0') {
1804                 if ( c == CTLQUOTEMARK)
1805                         continue;
1806                 if (c == CTLESC)
1807                         p++;
1808                 else if (BASESYNTAX[(int)c] == CCTL)
1809                         return 0;
1810         }
1811         return 1;
1812 }
1813
1814
1815 /*
1816  * Return true if the argument is a legal variable name (a letter or
1817  * underscore followed by zero or more letters, underscores, and digits).
1818  */
1819
1820 int
1821 goodname(const char *name)
1822 {
1823         const char *p;
1824
1825         p = name;
1826         if (! is_name(*p))
1827                 return 0;
1828         while (*++p) {
1829                 if (! is_in_name(*p))
1830                         return 0;
1831         }
1832         return 1;
1833 }
1834
1835
1836 int
1837 isassignment(const char *p)
1838 {
1839         if (!is_name(*p))
1840                 return 0;
1841         p++;
1842         for (;;) {
1843                 if (*p == '=')
1844                         return 1;
1845                 else if (!is_in_name(*p))
1846                         return 0;
1847                 p++;
1848         }
1849 }
1850
1851
1852 static void
1853 consumetoken(int token)
1854 {
1855         if (readtoken() != token)
1856                 synexpect(token);
1857 }
1858
1859
1860 /*
1861  * Called when an unexpected token is read during the parse.  The argument
1862  * is the token that is expected, or -1 if more than one type of token can
1863  * occur at this point.
1864  */
1865
1866 static void
1867 synexpect(int token)
1868 {
1869         char msg[64];
1870
1871         if (token >= 0) {
1872                 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1873                         tokname[lasttoken], tokname[token]);
1874         } else {
1875                 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1876         }
1877         synerror(msg);
1878 }
1879
1880
1881 static void
1882 synerror(const char *msg)
1883 {
1884         if (commandname)
1885                 outfmt(out2, "%s: %d: ", commandname, startlinno);
1886         outfmt(out2, "Syntax error: %s\n", msg);
1887         error((char *)NULL);
1888 }
1889
1890 static void
1891 setprompt(int which)
1892 {
1893         whichprompt = which;
1894
1895 #ifndef NO_HISTORY
1896         if (!el)
1897 #endif
1898         {
1899                 out2str(getprompt(NULL));
1900                 flushout(out2);
1901         }
1902 }
1903
1904 /*
1905  * called by editline -- any expansions to the prompt
1906  *    should be added here.
1907  */
1908 char *
1909 getprompt(void *unused __unused)
1910 {
1911         static char ps[PROMPTLEN];
1912         char *fmt;
1913         const char *pwd;
1914         int i, trim;
1915         static char internal_error[] = "??";
1916
1917         /*
1918          * Select prompt format.
1919          */
1920         switch (whichprompt) {
1921         case 0:
1922                 fmt = nullstr;
1923                 break;
1924         case 1:
1925                 fmt = ps1val();
1926                 break;
1927         case 2:
1928                 fmt = ps2val();
1929                 break;
1930         default:
1931                 return internal_error;
1932         }
1933
1934         /*
1935          * Format prompt string.
1936          */
1937         for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1938                 if (*fmt == '\\')
1939                         switch (*++fmt) {
1940
1941                                 /*
1942                                  * Hostname.
1943                                  *
1944                                  * \h specifies just the local hostname,
1945                                  * \H specifies fully-qualified hostname.
1946                                  */
1947                         case 'h':
1948                         case 'H':
1949                                 ps[i] = '\0';
1950                                 gethostname(&ps[i], PROMPTLEN - i);
1951                                 /* Skip to end of hostname. */
1952                                 trim = (*fmt == 'h') ? '.' : '\0';
1953                                 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1954                                         i++;
1955                                 break;
1956
1957                                 /*
1958                                  * Working directory.
1959                                  *
1960                                  * \W specifies just the final component,
1961                                  * \w specifies the entire path.
1962                                  */
1963                         case 'W':
1964                         case 'w':
1965                                 pwd = lookupvar("PWD");
1966                                 if (pwd == NULL)
1967                                         pwd = "?";
1968                                 if (*fmt == 'W' &&
1969                                     *pwd == '/' && pwd[1] != '\0')
1970                                         strlcpy(&ps[i], strrchr(pwd, '/') + 1,
1971                                             PROMPTLEN - i);
1972                                 else
1973                                         strlcpy(&ps[i], pwd, PROMPTLEN - i);
1974                                 /* Skip to end of path. */
1975                                 while (ps[i + 1] != '\0')
1976                                         i++;
1977                                 break;
1978
1979                                 /*
1980                                  * Superuser status.
1981                                  *
1982                                  * '$' for normal users, '#' for root.
1983                                  */
1984                         case '$':
1985                                 ps[i] = (geteuid() != 0) ? '$' : '#';
1986                                 break;
1987
1988                                 /*
1989                                  * A literal \.
1990                                  */
1991                         case '\\':
1992                                 ps[i] = '\\';
1993                                 break;
1994
1995                                 /*
1996                                  * Emit unrecognized formats verbatim.
1997                                  */
1998                         default:
1999                                 ps[i++] = '\\';
2000                                 ps[i] = *fmt;
2001                                 break;
2002                         }
2003                 else
2004                         ps[i] = *fmt;
2005         ps[i] = '\0';
2006         return (ps);
2007 }
2008
2009
2010 const char *
2011 expandstr(const char *ps)
2012 {
2013         union node n;
2014         struct jmploc jmploc;
2015         struct jmploc *const savehandler = handler;
2016         const int saveprompt = doprompt;
2017         struct parsefile *const savetopfile = getcurrentfile();
2018         struct parser_temp *const saveparser_temp = parser_temp;
2019         const char *result = NULL;
2020
2021         if (!setjmp(jmploc.loc)) {
2022                 handler = &jmploc;
2023                 parser_temp = NULL;
2024                 setinputstring(ps, 1);
2025                 doprompt = 0;
2026                 readtoken1(pgetc(), DQSYNTAX, "\n\n", 0);
2027                 if (backquotelist != NULL)
2028                         error("Command substitution not allowed here");
2029
2030                 n.narg.type = NARG;
2031                 n.narg.next = NULL;
2032                 n.narg.text = wordtext;
2033                 n.narg.backquote = backquotelist;
2034
2035                 expandarg(&n, NULL, 0);
2036                 result = stackblock();
2037                 INTOFF;
2038         }
2039         handler = savehandler;
2040         doprompt = saveprompt;
2041         popfilesupto(savetopfile);
2042         if (parser_temp != saveparser_temp) {
2043                 parser_temp_free_all();
2044                 parser_temp = saveparser_temp;
2045         }
2046         if (result != NULL) {
2047                 INTON;
2048         } else if (exception == EXINT)
2049                 raise(SIGINT);
2050         return result;
2051 }