etc/protocols - sync with NetBSD-8
[minix.git] / bin / sh / parser.c
blobe758c08e111ae86067c9351378e9745ac5d97854
1 /* $NetBSD: parser.c,v 1.93 2014/08/29 09:35:19 christos Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
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.
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.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
39 #else
40 __RCSID("$NetBSD: parser.c,v 1.93 2014/08/29 09:35:19 christos Exp $");
41 #endif
42 #endif /* not lint */
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <limits.h>
48 #include "shell.h"
49 #include "parser.h"
50 #include "nodes.h"
51 #include "expand.h" /* defines rmescapes() */
52 #include "eval.h" /* defines commandname */
53 #include "redir.h" /* defines copyfd() */
54 #include "syntax.h"
55 #include "options.h"
56 #include "input.h"
57 #include "output.h"
58 #include "var.h"
59 #include "error.h"
60 #include "memalloc.h"
61 #include "mystring.h"
62 #include "alias.h"
63 #include "show.h"
64 #ifndef SMALL
65 #include "myhistedit.h"
66 #endif
69 * Shell command parser.
72 #define EOFMARKLEN 79
74 /* values returned by readtoken */
75 #include "token.h"
77 #define OPENBRACE '{'
78 #define CLOSEBRACE '}'
81 struct heredoc {
82 struct heredoc *next; /* next here document in list */
83 union node *here; /* redirection node */
84 char *eofmark; /* string indicating end of input */
85 int striptabs; /* if set, strip leading tabs */
90 static int noalias = 0; /* when set, don't handle aliases */
91 struct heredoc *heredoclist; /* list of here documents to read */
92 int parsebackquote; /* nonzero if we are inside backquotes */
93 int doprompt; /* if set, prompt the user */
94 int needprompt; /* true if interactive and at start of line */
95 int lasttoken; /* last token read */
96 MKINIT int tokpushback; /* last token pushed back */
97 char *wordtext; /* text of last word returned by readtoken */
98 MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */
99 struct nodelist *backquotelist;
100 union node *redirnode;
101 struct heredoc *heredoc;
102 int quoteflag; /* set if (part of) last token was quoted */
103 int startlinno; /* line # where last token started */
104 int funclinno; /* line # where the current function started */
107 STATIC union node *list(int, int);
108 STATIC union node *andor(void);
109 STATIC union node *pipeline(void);
110 STATIC union node *command(void);
111 STATIC union node *simplecmd(union node **, union node *);
112 STATIC union node *makename(void);
113 STATIC void parsefname(void);
114 STATIC void parseheredoc(void);
115 STATIC int peektoken(void);
116 STATIC int readtoken(void);
117 STATIC int xxreadtoken(void);
118 STATIC int readtoken1(int, char const *, char *, int);
119 STATIC int noexpand(char *);
120 STATIC void synexpect(int) __dead;
121 STATIC void synerror(const char *) __dead;
122 STATIC void setprompt(int);
126 * Read and parse a command. Returns NEOF on end of file. (NULL is a
127 * valid parse tree indicating a blank line.)
130 union node *
131 parsecmd(int interact)
133 int t;
135 tokpushback = 0;
136 doprompt = interact;
137 if (doprompt)
138 setprompt(1);
139 else
140 setprompt(0);
141 needprompt = 0;
142 t = readtoken();
143 if (t == TEOF)
144 return NEOF;
145 if (t == TNL)
146 return NULL;
147 tokpushback++;
148 return list(1, 0);
152 STATIC union node *
153 list(int nlflag, int erflag)
155 union node *n1, *n2, *n3;
156 int tok;
157 TRACE(("list: entered\n"));
159 checkkwd = 2;
160 if (nlflag == 0 && tokendlist[peektoken()])
161 return NULL;
162 n1 = NULL;
163 for (;;) {
164 n2 = andor();
165 tok = readtoken();
166 if (tok == TBACKGND) {
167 if (n2->type == NCMD || n2->type == NPIPE) {
168 n2->ncmd.backgnd = 1;
169 } else if (n2->type == NREDIR) {
170 n2->type = NBACKGND;
171 } else {
172 n3 = (union node *)stalloc(sizeof (struct nredir));
173 n3->type = NBACKGND;
174 n3->nredir.n = n2;
175 n3->nredir.redirect = NULL;
176 n2 = n3;
179 if (n1 == NULL) {
180 n1 = n2;
182 else {
183 n3 = (union node *)stalloc(sizeof (struct nbinary));
184 n3->type = NSEMI;
185 n3->nbinary.ch1 = n1;
186 n3->nbinary.ch2 = n2;
187 n1 = n3;
189 switch (tok) {
190 case TBACKGND:
191 case TSEMI:
192 tok = readtoken();
193 /* fall through */
194 case TNL:
195 if (tok == TNL) {
196 parseheredoc();
197 if (nlflag)
198 return n1;
199 } else {
200 tokpushback++;
202 checkkwd = 2;
203 if (tokendlist[peektoken()])
204 return n1;
205 break;
206 case TEOF:
207 if (heredoclist)
208 parseheredoc();
209 else
210 pungetc(); /* push back EOF on input */
211 return n1;
212 default:
213 if (nlflag || erflag)
214 synexpect(-1);
215 tokpushback++;
216 return n1;
223 STATIC union node *
224 andor(void)
226 union node *n1, *n2, *n3;
227 int t;
229 TRACE(("andor: entered\n"));
230 n1 = pipeline();
231 for (;;) {
232 if ((t = readtoken()) == TAND) {
233 t = NAND;
234 } else if (t == TOR) {
235 t = NOR;
236 } else {
237 tokpushback++;
238 return n1;
240 n2 = pipeline();
241 n3 = (union node *)stalloc(sizeof (struct nbinary));
242 n3->type = t;
243 n3->nbinary.ch1 = n1;
244 n3->nbinary.ch2 = n2;
245 n1 = n3;
251 STATIC union node *
252 pipeline(void)
254 union node *n1, *n2, *pipenode;
255 struct nodelist *lp, *prev;
256 int negate;
258 TRACE(("pipeline: entered\n"));
260 negate = 0;
261 checkkwd = 2;
262 while (readtoken() == TNOT) {
263 TRACE(("pipeline: TNOT recognized\n"));
264 negate = !negate;
266 tokpushback++;
267 n1 = command();
268 if (readtoken() == TPIPE) {
269 pipenode = (union node *)stalloc(sizeof (struct npipe));
270 pipenode->type = NPIPE;
271 pipenode->npipe.backgnd = 0;
272 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
273 pipenode->npipe.cmdlist = lp;
274 lp->n = n1;
275 do {
276 prev = lp;
277 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
278 lp->n = command();
279 prev->next = lp;
280 } while (readtoken() == TPIPE);
281 lp->next = NULL;
282 n1 = pipenode;
284 tokpushback++;
285 if (negate) {
286 TRACE(("negate pipeline\n"));
287 n2 = (union node *)stalloc(sizeof (struct nnot));
288 n2->type = NNOT;
289 n2->nnot.com = n1;
290 return n2;
291 } else
292 return n1;
297 STATIC union node *
298 command(void)
300 union node *n1, *n2;
301 union node *ap, **app;
302 union node *cp, **cpp;
303 union node *redir, **rpp;
304 int t, negate = 0;
306 TRACE(("command: entered\n"));
308 checkkwd = 2;
309 redir = NULL;
310 n1 = NULL;
311 rpp = &redir;
313 /* Check for redirection which may precede command */
314 while (readtoken() == TREDIR) {
315 *rpp = n2 = redirnode;
316 rpp = &n2->nfile.next;
317 parsefname();
319 tokpushback++;
321 while (readtoken() == TNOT) {
322 TRACE(("command: TNOT recognized\n"));
323 negate = !negate;
325 tokpushback++;
327 switch (readtoken()) {
328 case TIF:
329 n1 = (union node *)stalloc(sizeof (struct nif));
330 n1->type = NIF;
331 n1->nif.test = list(0, 0);
332 if (readtoken() != TTHEN)
333 synexpect(TTHEN);
334 n1->nif.ifpart = list(0, 0);
335 n2 = n1;
336 while (readtoken() == TELIF) {
337 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
338 n2 = n2->nif.elsepart;
339 n2->type = NIF;
340 n2->nif.test = list(0, 0);
341 if (readtoken() != TTHEN)
342 synexpect(TTHEN);
343 n2->nif.ifpart = list(0, 0);
345 if (lasttoken == TELSE)
346 n2->nif.elsepart = list(0, 0);
347 else {
348 n2->nif.elsepart = NULL;
349 tokpushback++;
351 if (readtoken() != TFI)
352 synexpect(TFI);
353 checkkwd = 1;
354 break;
355 case TWHILE:
356 case TUNTIL: {
357 int got;
358 n1 = (union node *)stalloc(sizeof (struct nbinary));
359 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
360 n1->nbinary.ch1 = list(0, 0);
361 if ((got=readtoken()) != TDO) {
362 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
363 synexpect(TDO);
365 n1->nbinary.ch2 = list(0, 0);
366 if (readtoken() != TDONE)
367 synexpect(TDONE);
368 checkkwd = 1;
369 break;
371 case TFOR:
372 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
373 synerror("Bad for loop variable");
374 n1 = (union node *)stalloc(sizeof (struct nfor));
375 n1->type = NFOR;
376 n1->nfor.var = wordtext;
377 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
378 app = &ap;
379 while (readtoken() == TWORD) {
380 n2 = (union node *)stalloc(sizeof (struct narg));
381 n2->type = NARG;
382 n2->narg.text = wordtext;
383 n2->narg.backquote = backquotelist;
384 *app = n2;
385 app = &n2->narg.next;
387 *app = NULL;
388 n1->nfor.args = ap;
389 if (lasttoken != TNL && lasttoken != TSEMI)
390 synexpect(-1);
391 } else {
392 static char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
393 '@', '=', '\0'};
394 n2 = (union node *)stalloc(sizeof (struct narg));
395 n2->type = NARG;
396 n2->narg.text = argvars;
397 n2->narg.backquote = NULL;
398 n2->narg.next = NULL;
399 n1->nfor.args = n2;
401 * Newline or semicolon here is optional (but note
402 * that the original Bourne shell only allowed NL).
404 if (lasttoken != TNL && lasttoken != TSEMI)
405 tokpushback++;
407 checkkwd = 2;
408 if ((t = readtoken()) == TDO)
409 t = TDONE;
410 else if (t == TBEGIN)
411 t = TEND;
412 else
413 synexpect(-1);
414 n1->nfor.body = list(0, 0);
415 if (readtoken() != t)
416 synexpect(t);
417 checkkwd = 1;
418 break;
419 case TCASE:
420 n1 = (union node *)stalloc(sizeof (struct ncase));
421 n1->type = NCASE;
422 if (readtoken() != TWORD)
423 synexpect(TWORD);
424 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
425 n2->type = NARG;
426 n2->narg.text = wordtext;
427 n2->narg.backquote = backquotelist;
428 n2->narg.next = NULL;
429 while (readtoken() == TNL);
430 if (lasttoken != TWORD || ! equal(wordtext, "in"))
431 synerror("expecting \"in\"");
432 cpp = &n1->ncase.cases;
433 noalias = 1;
434 checkkwd = 2, readtoken();
436 * Both ksh and bash accept 'case x in esac'
437 * so configure scripts started taking advantage of this.
438 * The page: http://pubs.opengroup.org/onlinepubs/\
439 * 009695399/utilities/xcu_chap02.html contradicts itself,
440 * as to if this is legal; the "Case Conditional Format"
441 * paragraph shows one case is required, but the "Grammar"
442 * section shows a grammar that explicitly allows the no
443 * case option.
445 while (lasttoken != TESAC) {
446 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
447 if (lasttoken == TLP)
448 readtoken();
449 cp->type = NCLIST;
450 app = &cp->nclist.pattern;
451 for (;;) {
452 *app = ap = (union node *)stalloc(sizeof (struct narg));
453 ap->type = NARG;
454 ap->narg.text = wordtext;
455 ap->narg.backquote = backquotelist;
456 if (checkkwd = 2, readtoken() != TPIPE)
457 break;
458 app = &ap->narg.next;
459 readtoken();
461 ap->narg.next = NULL;
462 noalias = 0;
463 if (lasttoken != TRP) {
464 synexpect(TRP);
466 cp->nclist.body = list(0, 0);
468 checkkwd = 2;
469 if ((t = readtoken()) != TESAC) {
470 if (t != TENDCASE) {
471 noalias = 0;
472 synexpect(TENDCASE);
473 } else {
474 noalias = 1;
475 checkkwd = 2;
476 readtoken();
479 cpp = &cp->nclist.next;
481 noalias = 0;
482 *cpp = NULL;
483 checkkwd = 1;
484 break;
485 case TLP:
486 n1 = (union node *)stalloc(sizeof (struct nredir));
487 n1->type = NSUBSHELL;
488 n1->nredir.n = list(0, 0);
489 n1->nredir.redirect = NULL;
490 if (readtoken() != TRP)
491 synexpect(TRP);
492 checkkwd = 1;
493 break;
494 case TBEGIN:
495 n1 = list(0, 0);
496 if (readtoken() != TEND)
497 synexpect(TEND);
498 checkkwd = 1;
499 break;
500 /* Handle an empty command like other simple commands. */
501 case TSEMI:
503 * An empty command before a ; doesn't make much sense, and
504 * should certainly be disallowed in the case of `if ;'.
506 if (!redir)
507 synexpect(-1);
508 case TAND:
509 case TOR:
510 case TNL:
511 case TEOF:
512 case TWORD:
513 case TRP:
514 tokpushback++;
515 n1 = simplecmd(rpp, redir);
516 goto checkneg;
517 default:
518 synexpect(-1);
519 /* NOTREACHED */
522 /* Now check for redirection which may follow command */
523 while (readtoken() == TREDIR) {
524 *rpp = n2 = redirnode;
525 rpp = &n2->nfile.next;
526 parsefname();
528 tokpushback++;
529 *rpp = NULL;
530 if (redir) {
531 if (n1->type != NSUBSHELL) {
532 n2 = (union node *)stalloc(sizeof (struct nredir));
533 n2->type = NREDIR;
534 n2->nredir.n = n1;
535 n1 = n2;
537 n1->nredir.redirect = redir;
540 checkneg:
541 if (negate) {
542 TRACE(("negate command\n"));
543 n2 = (union node *)stalloc(sizeof (struct nnot));
544 n2->type = NNOT;
545 n2->nnot.com = n1;
546 return n2;
548 else
549 return n1;
553 STATIC union node *
554 simplecmd(union node **rpp, union node *redir)
556 union node *args, **app;
557 union node **orig_rpp = rpp;
558 union node *n = NULL, *n2;
559 int negate = 0;
561 /* If we don't have any redirections already, then we must reset */
562 /* rpp to be the address of the local redir variable. */
563 if (redir == 0)
564 rpp = &redir;
566 args = NULL;
567 app = &args;
569 * We save the incoming value, because we need this for shell
570 * functions. There can not be a redirect or an argument between
571 * the function name and the open parenthesis.
573 orig_rpp = rpp;
575 while (readtoken() == TNOT) {
576 TRACE(("simplcmd: TNOT recognized\n"));
577 negate = !negate;
579 tokpushback++;
581 for (;;) {
582 if (readtoken() == TWORD) {
583 n = (union node *)stalloc(sizeof (struct narg));
584 n->type = NARG;
585 n->narg.text = wordtext;
586 n->narg.backquote = backquotelist;
587 *app = n;
588 app = &n->narg.next;
589 } else if (lasttoken == TREDIR) {
590 *rpp = n = redirnode;
591 rpp = &n->nfile.next;
592 parsefname(); /* read name of redirection file */
593 } else if (lasttoken == TLP && app == &args->narg.next
594 && rpp == orig_rpp) {
595 /* We have a function */
596 if (readtoken() != TRP)
597 synexpect(TRP);
598 funclinno = plinno;
599 rmescapes(n->narg.text);
600 if (!goodname(n->narg.text))
601 synerror("Bad function name");
602 n->type = NDEFUN;
603 n->narg.next = command();
604 funclinno = 0;
605 goto checkneg;
606 } else {
607 tokpushback++;
608 break;
611 *app = NULL;
612 *rpp = NULL;
613 n = (union node *)stalloc(sizeof (struct ncmd));
614 n->type = NCMD;
615 n->ncmd.backgnd = 0;
616 n->ncmd.args = args;
617 n->ncmd.redirect = redir;
619 checkneg:
620 if (negate) {
621 TRACE(("negate simplecmd\n"));
622 n2 = (union node *)stalloc(sizeof (struct nnot));
623 n2->type = NNOT;
624 n2->nnot.com = n;
625 return n2;
627 else
628 return n;
631 STATIC union node *
632 makename(void)
634 union node *n;
636 n = (union node *)stalloc(sizeof (struct narg));
637 n->type = NARG;
638 n->narg.next = NULL;
639 n->narg.text = wordtext;
640 n->narg.backquote = backquotelist;
641 return n;
644 void fixredir(union node *n, const char *text, int err)
646 TRACE(("Fix redir %s %d\n", text, err));
647 if (!err)
648 n->ndup.vname = NULL;
650 if (is_number(text))
651 n->ndup.dupfd = number(text);
652 else if (text[0] == '-' && text[1] == '\0')
653 n->ndup.dupfd = -1;
654 else {
656 if (err)
657 synerror("Bad fd number");
658 else
659 n->ndup.vname = makename();
664 STATIC void
665 parsefname(void)
667 union node *n = redirnode;
669 if (readtoken() != TWORD)
670 synexpect(-1);
671 if (n->type == NHERE) {
672 struct heredoc *here = heredoc;
673 struct heredoc *p;
674 int i;
676 if (quoteflag == 0)
677 n->type = NXHERE;
678 TRACE(("Here document %d\n", n->type));
679 if (here->striptabs) {
680 while (*wordtext == '\t')
681 wordtext++;
683 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
684 synerror("Illegal eof marker for << redirection");
685 rmescapes(wordtext);
686 here->eofmark = wordtext;
687 here->next = NULL;
688 if (heredoclist == NULL)
689 heredoclist = here;
690 else {
691 for (p = heredoclist ; p->next ; p = p->next)
692 continue;
693 p->next = here;
695 } else if (n->type == NTOFD || n->type == NFROMFD) {
696 fixredir(n, wordtext, 0);
697 } else {
698 n->nfile.fname = makename();
704 * Input any here documents.
707 STATIC void
708 parseheredoc(void)
710 struct heredoc *here;
711 union node *n;
713 while (heredoclist) {
714 here = heredoclist;
715 heredoclist = here->next;
716 if (needprompt) {
717 setprompt(2);
718 needprompt = 0;
720 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
721 here->eofmark, here->striptabs);
722 n = (union node *)stalloc(sizeof (struct narg));
723 n->narg.type = NARG;
724 n->narg.next = NULL;
725 n->narg.text = wordtext;
726 n->narg.backquote = backquotelist;
727 here->here->nhere.doc = n;
731 STATIC int
732 peektoken(void)
734 int t;
736 t = readtoken();
737 tokpushback++;
738 return (t);
741 STATIC int
742 readtoken(void)
744 int t;
745 int savecheckkwd = checkkwd;
746 #ifdef DEBUG
747 int alreadyseen = tokpushback;
748 #endif
749 struct alias *ap;
751 top:
752 t = xxreadtoken();
754 if (checkkwd) {
756 * eat newlines
758 if (checkkwd == 2) {
759 checkkwd = 0;
760 while (t == TNL) {
761 parseheredoc();
762 t = xxreadtoken();
764 } else
765 checkkwd = 0;
767 * check for keywords and aliases
769 if (t == TWORD && !quoteflag)
771 const char *const *pp;
773 for (pp = parsekwd; *pp; pp++) {
774 if (**pp == *wordtext && equal(*pp, wordtext))
776 lasttoken = t = pp -
777 parsekwd + KWDOFFSET;
778 TRACE(("keyword %s recognized\n", tokname[t]));
779 goto out;
782 if (!noalias &&
783 (ap = lookupalias(wordtext, 1)) != NULL) {
784 pushstring(ap->val, strlen(ap->val), ap);
785 checkkwd = savecheckkwd;
786 goto top;
789 out:
790 checkkwd = (t == TNOT) ? savecheckkwd : 0;
792 TRACE(("%stoken %s %s\n", alreadyseen ? "reread " : "", tokname[t], t == TWORD ? wordtext : ""));
793 return (t);
798 * Read the next input token.
799 * If the token is a word, we set backquotelist to the list of cmds in
800 * backquotes. We set quoteflag to true if any part of the word was
801 * quoted.
802 * If the token is TREDIR, then we set redirnode to a structure containing
803 * the redirection.
804 * In all cases, the variable startlinno is set to the number of the line
805 * on which the token starts.
807 * [Change comment: here documents and internal procedures]
808 * [Readtoken shouldn't have any arguments. Perhaps we should make the
809 * word parsing code into a separate routine. In this case, readtoken
810 * doesn't need to have any internal procedures, but parseword does.
811 * We could also make parseoperator in essence the main routine, and
812 * have parseword (readtoken1?) handle both words and redirection.]
815 #define RETURN(token) return lasttoken = token
817 STATIC int
818 xxreadtoken(void)
820 int c;
822 if (tokpushback) {
823 tokpushback = 0;
824 return lasttoken;
826 if (needprompt) {
827 setprompt(2);
828 needprompt = 0;
830 startlinno = plinno;
831 for (;;) { /* until token or start of word found */
832 c = pgetc_macro();
833 switch (c) {
834 case ' ': case '\t':
835 continue;
836 case '#':
837 while ((c = pgetc()) != '\n' && c != PEOF)
838 continue;
839 pungetc();
840 continue;
841 case '\\':
842 switch (pgetc()) {
843 case '\n':
844 startlinno = ++plinno;
845 if (doprompt)
846 setprompt(2);
847 else
848 setprompt(0);
849 continue;
850 case PEOF:
851 RETURN(TEOF);
852 default:
853 pungetc();
854 break;
856 goto breakloop;
857 case '\n':
858 plinno++;
859 needprompt = doprompt;
860 RETURN(TNL);
861 case PEOF:
862 RETURN(TEOF);
863 case '&':
864 if (pgetc() == '&')
865 RETURN(TAND);
866 pungetc();
867 RETURN(TBACKGND);
868 case '|':
869 if (pgetc() == '|')
870 RETURN(TOR);
871 pungetc();
872 RETURN(TPIPE);
873 case ';':
874 if (pgetc() == ';')
875 RETURN(TENDCASE);
876 pungetc();
877 RETURN(TSEMI);
878 case '(':
879 RETURN(TLP);
880 case ')':
881 RETURN(TRP);
882 default:
883 goto breakloop;
886 breakloop:
887 return readtoken1(c, BASESYNTAX, NULL, 0);
888 #undef RETURN
894 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
895 * is not NULL, read a here document. In the latter case, eofmark is the
896 * word which marks the end of the document and striptabs is true if
897 * leading tabs should be stripped from the document. The argument firstc
898 * is the first character of the input token or document.
900 * Because C does not have internal subroutines, I have simulated them
901 * using goto's to implement the subroutine linkage. The following macros
902 * will run code that appears at the end of readtoken1.
905 #define CHECKEND() {goto checkend; checkend_return:;}
906 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
907 #define PARSESUB() {goto parsesub; parsesub_return:;}
908 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
909 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
910 #define PARSEARITH() {goto parsearith; parsearith_return:;}
913 * Keep track of nested doublequotes in dblquote and doublequotep.
914 * We use dblquote for the first 32 levels, and we expand to a malloc'ed
915 * region for levels above that. Usually we never need to malloc.
916 * This code assumes that an int is 32 bits. We don't use uint32_t,
917 * because the rest of the code does not.
919 #define ISDBLQUOTE() ((varnest < 32) ? (dblquote & (1 << varnest)) : \
920 (dblquotep[(varnest / 32) - 1] & (1 << (varnest % 32))))
922 #define SETDBLQUOTE() \
923 if (varnest < 32) \
924 dblquote |= (1 << varnest); \
925 else \
926 dblquotep[(varnest / 32) - 1] |= (1 << (varnest % 32))
928 #define CLRDBLQUOTE() \
929 if (varnest < 32) \
930 dblquote &= ~(1 << varnest); \
931 else \
932 dblquotep[(varnest / 32) - 1] &= ~(1 << (varnest % 32))
934 STATIC int
935 readtoken1(int firstc, char const *syn, char *eofmark, int striptabs)
937 char const * volatile syntax = syn;
938 int c = firstc;
939 char * volatile out;
940 int len;
941 char line[EOFMARKLEN + 1];
942 struct nodelist *bqlist;
943 volatile int quotef;
944 int * volatile dblquotep = NULL;
945 volatile size_t maxnest = 32;
946 volatile int dblquote;
947 volatile size_t varnest; /* levels of variables expansion */
948 volatile int arinest; /* levels of arithmetic expansion */
949 volatile int parenlevel; /* levels of parens in arithmetic */
950 volatile int oldstyle;
951 char const * volatile prevsyntax; /* syntax before arithmetic */
952 #ifdef __GNUC__
953 prevsyntax = NULL; /* XXX gcc4 */
954 #endif
956 startlinno = plinno;
957 dblquote = 0;
958 varnest = 0;
959 if (syntax == DQSYNTAX) {
960 SETDBLQUOTE();
962 quotef = 0;
963 bqlist = NULL;
964 arinest = 0;
965 parenlevel = 0;
967 STARTSTACKSTR(out);
968 loop: { /* for each line, until end of word */
969 #if ATTY
970 if (c == '\034' && doprompt
971 && attyset() && ! equal(termval(), "emacs")) {
972 attyline();
973 if (syntax == BASESYNTAX)
974 return readtoken();
975 c = pgetc();
976 goto loop;
978 #endif
979 CHECKEND(); /* set c to PEOF if at end of here document */
980 for (;;) { /* until end of line or end of word */
981 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
982 switch(syntax[c]) {
983 case CNL: /* '\n' */
984 if (syntax == BASESYNTAX)
985 goto endword; /* exit outer loop */
986 USTPUTC(c, out);
987 plinno++;
988 if (doprompt)
989 setprompt(2);
990 else
991 setprompt(0);
992 c = pgetc();
993 goto loop; /* continue outer loop */
994 case CWORD:
995 USTPUTC(c, out);
996 break;
997 case CCTL:
998 if (eofmark == NULL || ISDBLQUOTE())
999 USTPUTC(CTLESC, out);
1000 USTPUTC(c, out);
1001 break;
1002 case CBACK: /* backslash */
1003 c = pgetc();
1004 if (c == PEOF) {
1005 USTPUTC('\\', out);
1006 pungetc();
1007 break;
1009 if (c == '\n') {
1010 plinno++;
1011 if (doprompt)
1012 setprompt(2);
1013 else
1014 setprompt(0);
1015 break;
1017 quotef = 1;
1018 if (ISDBLQUOTE() && c != '\\' &&
1019 c != '`' && c != '$' &&
1020 (c != '"' || eofmark != NULL))
1021 USTPUTC('\\', out);
1022 if (SQSYNTAX[c] == CCTL)
1023 USTPUTC(CTLESC, out);
1024 else if (eofmark == NULL) {
1025 USTPUTC(CTLQUOTEMARK, out);
1026 USTPUTC(c, out);
1027 if (varnest != 0)
1028 USTPUTC(CTLQUOTEEND, out);
1029 break;
1031 USTPUTC(c, out);
1032 break;
1033 case CSQUOTE:
1034 if (syntax != SQSYNTAX) {
1035 if (eofmark == NULL)
1036 USTPUTC(CTLQUOTEMARK, out);
1037 quotef = 1;
1038 syntax = SQSYNTAX;
1039 break;
1041 if (eofmark != NULL && arinest == 0 &&
1042 varnest == 0) {
1043 /* Ignore inside quoted here document */
1044 USTPUTC(c, out);
1045 break;
1047 /* End of single quotes... */
1048 if (arinest)
1049 syntax = ARISYNTAX;
1050 else {
1051 syntax = BASESYNTAX;
1052 if (varnest != 0)
1053 USTPUTC(CTLQUOTEEND, out);
1055 break;
1056 case CDQUOTE:
1057 if (eofmark != NULL && arinest == 0 &&
1058 varnest == 0) {
1059 /* Ignore inside here document */
1060 USTPUTC(c, out);
1061 break;
1063 quotef = 1;
1064 if (arinest) {
1065 if (ISDBLQUOTE()) {
1066 syntax = ARISYNTAX;
1067 CLRDBLQUOTE();
1068 } else {
1069 syntax = DQSYNTAX;
1070 SETDBLQUOTE();
1071 USTPUTC(CTLQUOTEMARK, out);
1073 break;
1075 if (eofmark != NULL)
1076 break;
1077 if (ISDBLQUOTE()) {
1078 if (varnest != 0)
1079 USTPUTC(CTLQUOTEEND, out);
1080 syntax = BASESYNTAX;
1081 CLRDBLQUOTE();
1082 } else {
1083 syntax = DQSYNTAX;
1084 SETDBLQUOTE();
1085 USTPUTC(CTLQUOTEMARK, out);
1087 break;
1088 case CVAR: /* '$' */
1089 PARSESUB(); /* parse substitution */
1090 break;
1091 case CENDVAR: /* CLOSEBRACE */
1092 if (varnest > 0 && !ISDBLQUOTE()) {
1093 varnest--;
1094 USTPUTC(CTLENDVAR, out);
1095 } else {
1096 USTPUTC(c, out);
1098 break;
1099 case CLP: /* '(' in arithmetic */
1100 parenlevel++;
1101 USTPUTC(c, out);
1102 break;
1103 case CRP: /* ')' in arithmetic */
1104 if (parenlevel > 0) {
1105 USTPUTC(c, out);
1106 --parenlevel;
1107 } else {
1108 if (pgetc() == ')') {
1109 if (--arinest == 0) {
1110 USTPUTC(CTLENDARI, out);
1111 syntax = prevsyntax;
1112 if (syntax == DQSYNTAX)
1113 SETDBLQUOTE();
1114 else
1115 CLRDBLQUOTE();
1116 } else
1117 USTPUTC(')', out);
1118 } else {
1120 * unbalanced parens
1121 * (don't 2nd guess - no error)
1123 pungetc();
1124 USTPUTC(')', out);
1127 break;
1128 case CBQUOTE: /* '`' */
1129 PARSEBACKQOLD();
1130 break;
1131 case CEOF:
1132 goto endword; /* exit outer loop */
1133 default:
1134 if (varnest == 0 && !ISDBLQUOTE())
1135 goto endword; /* exit outer loop */
1136 USTPUTC(c, out);
1138 c = pgetc_macro();
1141 endword:
1142 if (syntax == ARISYNTAX)
1143 synerror("Missing '))'");
1144 if (syntax != BASESYNTAX && /* ! parsebackquote && */ eofmark == NULL)
1145 synerror("Unterminated quoted string");
1146 if (varnest != 0) {
1147 startlinno = plinno;
1148 /* { */
1149 synerror("Missing '}'");
1151 USTPUTC('\0', out);
1152 len = out - stackblock();
1153 out = stackblock();
1154 if (eofmark == NULL) {
1155 if ((c == '>' || c == '<')
1156 && quotef == 0
1157 && (*out == '\0' || is_number(out))) {
1158 PARSEREDIR();
1159 return lasttoken = TREDIR;
1160 } else {
1161 pungetc();
1164 quoteflag = quotef;
1165 backquotelist = bqlist;
1166 grabstackblock(len);
1167 wordtext = out;
1168 if (dblquotep != NULL)
1169 ckfree(dblquotep);
1170 return lasttoken = TWORD;
1171 /* end of readtoken routine */
1176 * Check to see whether we are at the end of the here document. When this
1177 * is called, c is set to the first character of the next input line. If
1178 * we are at the end of the here document, this routine sets the c to PEOF.
1181 checkend: {
1182 if (eofmark) {
1183 if (striptabs) {
1184 while (c == '\t')
1185 c = pgetc();
1187 if (c == *eofmark) {
1188 if (pfgets(line, sizeof line) != NULL) {
1189 char *p, *q;
1191 p = line;
1192 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++)
1193 continue;
1194 if ((*p == '\0' || *p == '\n') && *q == '\0') {
1195 c = PEOF;
1196 plinno++;
1197 needprompt = doprompt;
1198 } else {
1199 pushstring(line, strlen(line), NULL);
1204 goto checkend_return;
1209 * Parse a redirection operator. The variable "out" points to a string
1210 * specifying the fd to be redirected. The variable "c" contains the
1211 * first character of the redirection operator.
1214 parseredir: {
1215 char fd[64];
1216 union node *np;
1217 strlcpy(fd, out, sizeof(fd));
1219 np = (union node *)stalloc(sizeof (struct nfile));
1220 if (c == '>') {
1221 np->nfile.fd = 1;
1222 c = pgetc();
1223 if (c == '>')
1224 np->type = NAPPEND;
1225 else if (c == '|')
1226 np->type = NCLOBBER;
1227 else if (c == '&')
1228 np->type = NTOFD;
1229 else {
1230 np->type = NTO;
1231 pungetc();
1233 } else { /* c == '<' */
1234 np->nfile.fd = 0;
1235 switch (c = pgetc()) {
1236 case '<':
1237 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1238 np = (union node *)stalloc(sizeof (struct nhere));
1239 np->nfile.fd = 0;
1241 np->type = NHERE;
1242 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1243 heredoc->here = np;
1244 if ((c = pgetc()) == '-') {
1245 heredoc->striptabs = 1;
1246 } else {
1247 heredoc->striptabs = 0;
1248 pungetc();
1250 break;
1252 case '&':
1253 np->type = NFROMFD;
1254 break;
1256 case '>':
1257 np->type = NFROMTO;
1258 break;
1260 default:
1261 np->type = NFROM;
1262 pungetc();
1263 break;
1266 if (*fd != '\0')
1267 np->nfile.fd = number(fd);
1268 redirnode = np;
1269 goto parseredir_return;
1274 * Parse a substitution. At this point, we have read the dollar sign
1275 * and nothing else.
1278 parsesub: {
1279 char buf[10];
1280 int subtype;
1281 int typeloc;
1282 int flags;
1283 char *p;
1284 static const char types[] = "}-+?=";
1285 int i;
1286 int linno;
1288 c = pgetc();
1289 if (c != '(' && c != OPENBRACE && !is_name(c) && !is_special(c)) {
1290 USTPUTC('$', out);
1291 pungetc();
1292 } else if (c == '(') { /* $(command) or $((arith)) */
1293 if (pgetc() == '(') {
1294 PARSEARITH();
1295 } else {
1296 pungetc();
1297 PARSEBACKQNEW();
1299 } else {
1300 USTPUTC(CTLVAR, out);
1301 typeloc = out - stackblock();
1302 USTPUTC(VSNORMAL, out);
1303 subtype = VSNORMAL;
1304 flags = 0;
1305 if (c == OPENBRACE) {
1306 c = pgetc();
1307 if (c == '#') {
1308 if ((c = pgetc()) == CLOSEBRACE)
1309 c = '#';
1310 else
1311 subtype = VSLENGTH;
1313 else
1314 subtype = 0;
1316 if (is_name(c)) {
1317 p = out;
1318 do {
1319 STPUTC(c, out);
1320 c = pgetc();
1321 } while (is_in_name(c));
1322 if (out - p == 6 && strncmp(p, "LINENO", 6) == 0) {
1323 /* Replace the variable name with the
1324 * current line number. */
1325 linno = plinno;
1326 if (funclinno != 0)
1327 linno -= funclinno - 1;
1328 snprintf(buf, sizeof(buf), "%d", linno);
1329 STADJUST(-6, out);
1330 for (i = 0; buf[i] != '\0'; i++)
1331 STPUTC(buf[i], out);
1332 flags |= VSLINENO;
1334 } else if (is_digit(c)) {
1335 do {
1336 USTPUTC(c, out);
1337 c = pgetc();
1338 } while (is_digit(c));
1340 else if (is_special(c)) {
1341 USTPUTC(c, out);
1342 c = pgetc();
1344 else
1345 badsub: synerror("Bad substitution");
1347 STPUTC('=', out);
1348 if (subtype == 0) {
1349 switch (c) {
1350 case ':':
1351 flags |= VSNUL;
1352 c = pgetc();
1353 /*FALLTHROUGH*/
1354 default:
1355 p = strchr(types, c);
1356 if (p == NULL)
1357 goto badsub;
1358 subtype = p - types + VSNORMAL;
1359 break;
1360 case '%':
1361 case '#':
1363 int cc = c;
1364 subtype = c == '#' ? VSTRIMLEFT :
1365 VSTRIMRIGHT;
1366 c = pgetc();
1367 if (c == cc)
1368 subtype++;
1369 else
1370 pungetc();
1371 break;
1374 } else {
1375 pungetc();
1377 if (ISDBLQUOTE() || arinest)
1378 flags |= VSQUOTE;
1379 *(stackblock() + typeloc) = subtype | flags;
1380 if (subtype != VSNORMAL) {
1381 varnest++;
1382 if (varnest >= maxnest) {
1383 dblquotep = ckrealloc(dblquotep, maxnest / 8);
1384 dblquotep[(maxnest / 32) - 1] = 0;
1385 maxnest += 32;
1389 goto parsesub_return;
1394 * Called to parse command substitutions. Newstyle is set if the command
1395 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1396 * list of commands (passed by reference), and savelen is the number of
1397 * characters on the top of the stack which must be preserved.
1400 parsebackq: {
1401 struct nodelist **nlpp;
1402 int savepbq;
1403 union node *n;
1404 char *volatile str = NULL;
1405 struct jmploc jmploc;
1406 struct jmploc *volatile savehandler = NULL;
1407 int savelen;
1408 int saveprompt;
1410 savepbq = parsebackquote;
1411 if (setjmp(jmploc.loc)) {
1412 if (str)
1413 ckfree(str);
1414 parsebackquote = 0;
1415 handler = savehandler;
1416 longjmp(handler->loc, 1);
1418 INTOFF;
1419 str = NULL;
1420 savelen = out - stackblock();
1421 if (savelen > 0) {
1422 str = ckmalloc(savelen);
1423 memcpy(str, stackblock(), savelen);
1425 savehandler = handler;
1426 handler = &jmploc;
1427 INTON;
1428 if (oldstyle) {
1429 /* We must read until the closing backquote, giving special
1430 treatment to some slashes, and then push the string and
1431 reread it as input, interpreting it normally. */
1432 char *pout;
1433 int pc;
1434 int psavelen;
1435 char *pstr;
1438 STARTSTACKSTR(pout);
1439 for (;;) {
1440 if (needprompt) {
1441 setprompt(2);
1442 needprompt = 0;
1444 switch (pc = pgetc()) {
1445 case '`':
1446 goto done;
1448 case '\\':
1449 if ((pc = pgetc()) == '\n') {
1450 plinno++;
1451 if (doprompt)
1452 setprompt(2);
1453 else
1454 setprompt(0);
1456 * If eating a newline, avoid putting
1457 * the newline into the new character
1458 * stream (via the STPUTC after the
1459 * switch).
1461 continue;
1463 if (pc != '\\' && pc != '`' && pc != '$'
1464 && (!ISDBLQUOTE() || pc != '"'))
1465 STPUTC('\\', pout);
1466 break;
1468 case '\n':
1469 plinno++;
1470 needprompt = doprompt;
1471 break;
1473 case PEOF:
1474 startlinno = plinno;
1475 synerror("EOF in backquote substitution");
1476 break;
1478 default:
1479 break;
1481 STPUTC(pc, pout);
1483 done:
1484 STPUTC('\0', pout);
1485 psavelen = pout - stackblock();
1486 if (psavelen > 0) {
1487 pstr = grabstackstr(pout);
1488 setinputstring(pstr, 1);
1491 nlpp = &bqlist;
1492 while (*nlpp)
1493 nlpp = &(*nlpp)->next;
1494 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1495 (*nlpp)->next = NULL;
1496 parsebackquote = oldstyle;
1498 if (oldstyle) {
1499 saveprompt = doprompt;
1500 doprompt = 0;
1501 } else
1502 saveprompt = 0;
1504 n = list(0, oldstyle);
1506 if (oldstyle)
1507 doprompt = saveprompt;
1508 else {
1509 if (readtoken() != TRP)
1510 synexpect(TRP);
1513 (*nlpp)->n = n;
1514 if (oldstyle) {
1516 * Start reading from old file again, ignoring any pushed back
1517 * tokens left from the backquote parsing
1519 popfile();
1520 tokpushback = 0;
1522 while (stackblocksize() <= savelen)
1523 growstackblock();
1524 STARTSTACKSTR(out);
1525 if (str) {
1526 memcpy(out, str, savelen);
1527 STADJUST(savelen, out);
1528 INTOFF;
1529 ckfree(str);
1530 str = NULL;
1531 INTON;
1533 parsebackquote = savepbq;
1534 handler = savehandler;
1535 if (arinest || ISDBLQUOTE())
1536 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1537 else
1538 USTPUTC(CTLBACKQ, out);
1539 if (oldstyle)
1540 goto parsebackq_oldreturn;
1541 else
1542 goto parsebackq_newreturn;
1546 * Parse an arithmetic expansion (indicate start of one and set state)
1548 parsearith: {
1550 if (++arinest == 1) {
1551 prevsyntax = syntax;
1552 syntax = ARISYNTAX;
1553 USTPUTC(CTLARI, out);
1554 if (ISDBLQUOTE())
1555 USTPUTC('"',out);
1556 else
1557 USTPUTC(' ',out);
1558 } else {
1560 * we collapse embedded arithmetic expansion to
1561 * parenthesis, which should be equivalent
1563 USTPUTC('(', out);
1565 goto parsearith_return;
1568 } /* end of readtoken */
1572 #ifdef mkinit
1573 RESET {
1574 tokpushback = 0;
1575 checkkwd = 0;
1577 #endif
1580 * Returns true if the text contains nothing to expand (no dollar signs
1581 * or backquotes).
1584 STATIC int
1585 noexpand(char *text)
1587 char *p;
1588 char c;
1590 p = text;
1591 while ((c = *p++) != '\0') {
1592 if (c == CTLQUOTEMARK)
1593 continue;
1594 if (c == CTLESC)
1595 p++;
1596 else if (BASESYNTAX[(int)c] == CCTL)
1597 return 0;
1599 return 1;
1604 * Return true if the argument is a legal variable name (a letter or
1605 * underscore followed by zero or more letters, underscores, and digits).
1609 goodname(char *name)
1611 char *p;
1613 p = name;
1614 if (! is_name(*p))
1615 return 0;
1616 while (*++p) {
1617 if (! is_in_name(*p))
1618 return 0;
1620 return 1;
1625 * Called when an unexpected token is read during the parse. The argument
1626 * is the token that is expected, or -1 if more than one type of token can
1627 * occur at this point.
1630 STATIC void
1631 synexpect(int token)
1633 char msg[64];
1635 if (token >= 0) {
1636 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1637 tokname[lasttoken], tokname[token]);
1638 } else {
1639 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1641 synerror(msg);
1642 /* NOTREACHED */
1646 STATIC void
1647 synerror(const char *msg)
1649 if (commandname)
1650 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1651 else
1652 outfmt(&errout, "%s: ", getprogname());
1653 outfmt(&errout, "Syntax error: %s\n", msg);
1654 error(NULL);
1655 /* NOTREACHED */
1658 STATIC void
1659 setprompt(int which)
1661 whichprompt = which;
1663 #ifndef SMALL
1664 if (!el)
1665 #endif
1666 out2str(getprompt(NULL));
1670 * called by editline -- any expansions to the prompt
1671 * should be added here.
1673 const char *
1674 getprompt(void *unused)
1676 switch (whichprompt) {
1677 case 0:
1678 return "";
1679 case 1:
1680 return ps1val();
1681 case 2:
1682 return ps2val();
1683 default:
1684 return "<internal prompt error>";