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