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