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