parser.c revision 1.46 1 /* $NetBSD: parser.c,v 1.46 2001/02/04 19:52:06 christos Exp $ */
2
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
43 #else
44 __RCSID("$NetBSD: parser.c,v 1.46 2001/02/04 19:52:06 christos Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <stdlib.h>
49
50 #include "shell.h"
51 #include "parser.h"
52 #include "nodes.h"
53 #include "expand.h" /* defines rmescapes() */
54 #include "eval.h" /* defines commandname */
55 #include "redir.h" /* defines copyfd() */
56 #include "syntax.h"
57 #include "options.h"
58 #include "input.h"
59 #include "output.h"
60 #include "var.h"
61 #include "error.h"
62 #include "memalloc.h"
63 #include "mystring.h"
64 #include "alias.h"
65 #include "show.h"
66 #ifndef SMALL
67 #include "myhistedit.h"
68 #endif
69
70 /*
71 * Shell command parser.
72 */
73
74 #define EOFMARKLEN 79
75
76 /* values returned by readtoken */
77 #include "token.h"
78
79
80
81 struct heredoc {
82 struct heredoc *next; /* next here document in list */
83 union node *here; /* redirection node */
84 char *eofmark; /* string indicating end of input */
85 int striptabs; /* if set, strip leading tabs */
86 };
87
88
89
90 struct heredoc *heredoclist; /* list of here documents to read */
91 int parsebackquote; /* nonzero if we are inside backquotes */
92 int doprompt; /* if set, prompt the user */
93 int needprompt; /* true if interactive and at start of line */
94 int lasttoken; /* last token read */
95 MKINIT int tokpushback; /* last token pushed back */
96 char *wordtext; /* text of last word returned by readtoken */
97 MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */
98 struct nodelist *backquotelist;
99 union node *redirnode;
100 struct heredoc *heredoc;
101 int quoteflag; /* set if (part of) last token was quoted */
102 int startlinno; /* line # where last token started */
103
104
105 STATIC union node *list __P((int));
106 STATIC union node *andor __P((void));
107 STATIC union node *pipeline __P((void));
108 STATIC union node *command __P((void));
109 STATIC union node *simplecmd __P((union node **, union node *));
110 STATIC union node *makename __P((void));
111 STATIC void parsefname __P((void));
112 STATIC void parseheredoc __P((void));
113 STATIC int peektoken __P((void));
114 STATIC int readtoken __P((void));
115 STATIC int xxreadtoken __P((void));
116 STATIC int readtoken1 __P((int, char const *, char *, int));
117 STATIC int noexpand __P((char *));
118 STATIC void synexpect __P((int)) __attribute__((noreturn));
119 STATIC void synerror __P((const char *)) __attribute__((noreturn));
120 STATIC void setprompt __P((int));
121
122
123 /*
124 * Read and parse a command. Returns NEOF on end of file. (NULL is a
125 * valid parse tree indicating a blank line.)
126 */
127
128 union node *
129 parsecmd(int interact)
130 {
131 int t;
132
133 doprompt = interact;
134 if (doprompt)
135 setprompt(1);
136 else
137 setprompt(0);
138 needprompt = 0;
139 t = readtoken();
140 if (t == TEOF)
141 return NEOF;
142 if (t == TNL)
143 return NULL;
144 tokpushback++;
145 return list(1);
146 }
147
148
149 STATIC union node *
150 list(nlflag)
151 int nlflag;
152 {
153 union node *n1, *n2, *n3;
154 int tok;
155
156 checkkwd = 2;
157 if (nlflag == 0 && tokendlist[peektoken()])
158 return NULL;
159 n1 = NULL;
160 for (;;) {
161 n2 = andor();
162 tok = readtoken();
163 if (tok == TBACKGND) {
164 if (n2->type == NCMD || n2->type == NPIPE) {
165 n2->ncmd.backgnd = 1;
166 } else if (n2->type == NREDIR) {
167 n2->type = NBACKGND;
168 } else {
169 n3 = (union node *)stalloc(sizeof (struct nredir));
170 n3->type = NBACKGND;
171 n3->nredir.n = n2;
172 n3->nredir.redirect = NULL;
173 n2 = n3;
174 }
175 }
176 if (n1 == NULL) {
177 n1 = n2;
178 }
179 else {
180 n3 = (union node *)stalloc(sizeof (struct nbinary));
181 n3->type = NSEMI;
182 n3->nbinary.ch1 = n1;
183 n3->nbinary.ch2 = n2;
184 n1 = n3;
185 }
186 switch (tok) {
187 case TBACKGND:
188 case TSEMI:
189 tok = readtoken();
190 /* fall through */
191 case TNL:
192 if (tok == TNL) {
193 parseheredoc();
194 if (nlflag)
195 return n1;
196 } else {
197 tokpushback++;
198 }
199 checkkwd = 2;
200 if (tokendlist[peektoken()])
201 return n1;
202 break;
203 case TEOF:
204 if (heredoclist)
205 parseheredoc();
206 else
207 pungetc(); /* push back EOF on input */
208 return n1;
209 default:
210 if (nlflag)
211 synexpect(-1);
212 tokpushback++;
213 return n1;
214 }
215 }
216 }
217
218
219
220 STATIC union node *
221 andor() {
222 union node *n1, *n2, *n3;
223 int t;
224
225 n1 = pipeline();
226 for (;;) {
227 if ((t = readtoken()) == TAND) {
228 t = NAND;
229 } else if (t == TOR) {
230 t = NOR;
231 } else {
232 tokpushback++;
233 return n1;
234 }
235 n2 = pipeline();
236 n3 = (union node *)stalloc(sizeof (struct nbinary));
237 n3->type = t;
238 n3->nbinary.ch1 = n1;
239 n3->nbinary.ch2 = n2;
240 n1 = n3;
241 }
242 }
243
244
245
246 STATIC union node *
247 pipeline() {
248 union node *n1, *n2, *pipenode;
249 struct nodelist *lp, *prev;
250 int negate;
251
252 negate = 0;
253 TRACE(("pipeline: entered\n"));
254 while (readtoken() == TNOT)
255 negate = !negate;
256 tokpushback++;
257 n1 = command();
258 if (readtoken() == TPIPE) {
259 pipenode = (union node *)stalloc(sizeof (struct npipe));
260 pipenode->type = NPIPE;
261 pipenode->npipe.backgnd = 0;
262 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
263 pipenode->npipe.cmdlist = lp;
264 lp->n = n1;
265 do {
266 prev = lp;
267 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
268 lp->n = command();
269 prev->next = lp;
270 } while (readtoken() == TPIPE);
271 lp->next = NULL;
272 n1 = pipenode;
273 }
274 tokpushback++;
275 if (negate) {
276 n2 = (union node *)stalloc(sizeof (struct nnot));
277 n2->type = NNOT;
278 n2->nnot.com = n1;
279 return n2;
280 } else
281 return n1;
282 }
283
284
285
286 STATIC union node *
287 command() {
288 union node *n1, *n2;
289 union node *ap, **app;
290 union node *cp, **cpp;
291 union node *redir, **rpp;
292 int t, negate = 0;
293
294 checkkwd = 2;
295 redir = NULL;
296 n1 = NULL;
297 rpp = &redir;
298
299 /* Check for redirection which may precede command */
300 while (readtoken() == TREDIR) {
301 *rpp = n2 = redirnode;
302 rpp = &n2->nfile.next;
303 parsefname();
304 }
305 tokpushback++;
306
307 while (readtoken() == TNOT) {
308 TRACE(("command: TNOT recognized\n"));
309 negate = !negate;
310 }
311 tokpushback++;
312
313 switch (readtoken()) {
314 case TIF:
315 n1 = (union node *)stalloc(sizeof (struct nif));
316 n1->type = NIF;
317 n1->nif.test = list(0);
318 if (readtoken() != TTHEN)
319 synexpect(TTHEN);
320 n1->nif.ifpart = list(0);
321 n2 = n1;
322 while (readtoken() == TELIF) {
323 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
324 n2 = n2->nif.elsepart;
325 n2->type = NIF;
326 n2->nif.test = list(0);
327 if (readtoken() != TTHEN)
328 synexpect(TTHEN);
329 n2->nif.ifpart = list(0);
330 }
331 if (lasttoken == TELSE)
332 n2->nif.elsepart = list(0);
333 else {
334 n2->nif.elsepart = NULL;
335 tokpushback++;
336 }
337 if (readtoken() != TFI)
338 synexpect(TFI);
339 checkkwd = 1;
340 break;
341 case TWHILE:
342 case TUNTIL: {
343 int got;
344 n1 = (union node *)stalloc(sizeof (struct nbinary));
345 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
346 n1->nbinary.ch1 = list(0);
347 if ((got=readtoken()) != TDO) {
348 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
349 synexpect(TDO);
350 }
351 n1->nbinary.ch2 = list(0);
352 if (readtoken() != TDONE)
353 synexpect(TDONE);
354 checkkwd = 1;
355 break;
356 }
357 case TFOR:
358 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
359 synerror("Bad for loop variable");
360 n1 = (union node *)stalloc(sizeof (struct nfor));
361 n1->type = NFOR;
362 n1->nfor.var = wordtext;
363 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
364 app = ≈
365 while (readtoken() == TWORD) {
366 n2 = (union node *)stalloc(sizeof (struct narg));
367 n2->type = NARG;
368 n2->narg.text = wordtext;
369 n2->narg.backquote = backquotelist;
370 *app = n2;
371 app = &n2->narg.next;
372 }
373 *app = NULL;
374 n1->nfor.args = ap;
375 if (lasttoken != TNL && lasttoken != TSEMI)
376 synexpect(-1);
377 } else {
378 static char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
379 '@', '=', '\0'};
380 n2 = (union node *)stalloc(sizeof (struct narg));
381 n2->type = NARG;
382 n2->narg.text = argvars;
383 n2->narg.backquote = NULL;
384 n2->narg.next = NULL;
385 n1->nfor.args = n2;
386 /*
387 * Newline or semicolon here is optional (but note
388 * that the original Bourne shell only allowed NL).
389 */
390 if (lasttoken != TNL && lasttoken != TSEMI)
391 tokpushback++;
392 }
393 checkkwd = 2;
394 if ((t = readtoken()) == TDO)
395 t = TDONE;
396 else if (t == TBEGIN)
397 t = TEND;
398 else
399 synexpect(-1);
400 n1->nfor.body = list(0);
401 if (readtoken() != t)
402 synexpect(t);
403 checkkwd = 1;
404 break;
405 case TCASE:
406 n1 = (union node *)stalloc(sizeof (struct ncase));
407 n1->type = NCASE;
408 if (readtoken() != TWORD)
409 synexpect(TWORD);
410 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
411 n2->type = NARG;
412 n2->narg.text = wordtext;
413 n2->narg.backquote = backquotelist;
414 n2->narg.next = NULL;
415 while (readtoken() == TNL);
416 if (lasttoken != TWORD || ! equal(wordtext, "in"))
417 synerror("expecting \"in\"");
418 cpp = &n1->ncase.cases;
419 checkkwd = 2, readtoken();
420 do {
421 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
422 cp->type = NCLIST;
423 app = &cp->nclist.pattern;
424 for (;;) {
425 *app = ap = (union node *)stalloc(sizeof (struct narg));
426 ap->type = NARG;
427 ap->narg.text = wordtext;
428 ap->narg.backquote = backquotelist;
429 if (checkkwd = 2, readtoken() != TPIPE)
430 break;
431 app = &ap->narg.next;
432 readtoken();
433 }
434 ap->narg.next = NULL;
435 if (lasttoken != TRP)
436 synexpect(TRP);
437 cp->nclist.body = list(0);
438
439 checkkwd = 2;
440 if ((t = readtoken()) != TESAC) {
441 if (t != TENDCASE)
442 synexpect(TENDCASE);
443 else
444 checkkwd = 2, readtoken();
445 }
446 cpp = &cp->nclist.next;
447 } while(lasttoken != TESAC);
448 *cpp = NULL;
449 checkkwd = 1;
450 break;
451 case TLP:
452 n1 = (union node *)stalloc(sizeof (struct nredir));
453 n1->type = NSUBSHELL;
454 n1->nredir.n = list(0);
455 n1->nredir.redirect = NULL;
456 if (readtoken() != TRP)
457 synexpect(TRP);
458 checkkwd = 1;
459 break;
460 case TBEGIN:
461 n1 = list(0);
462 if (readtoken() != TEND)
463 synexpect(TEND);
464 checkkwd = 1;
465 break;
466 /* Handle an empty command like other simple commands. */
467 case TSEMI:
468 /*
469 * An empty command before a ; doesn't make much sense, and
470 * should certainly be disallowed in the case of `if ;'.
471 */
472 if (!redir)
473 synexpect(-1);
474 case TAND:
475 case TOR:
476 case TNL:
477 case TEOF:
478 case TWORD:
479 case TRP:
480 tokpushback++;
481 n1 = simplecmd(rpp, redir);
482 goto checkneg;
483 default:
484 synexpect(-1);
485 /* NOTREACHED */
486 }
487
488 /* Now check for redirection which may follow command */
489 while (readtoken() == TREDIR) {
490 *rpp = n2 = redirnode;
491 rpp = &n2->nfile.next;
492 parsefname();
493 }
494 tokpushback++;
495 *rpp = NULL;
496 if (redir) {
497 if (n1->type != NSUBSHELL) {
498 n2 = (union node *)stalloc(sizeof (struct nredir));
499 n2->type = NREDIR;
500 n2->nredir.n = n1;
501 n1 = n2;
502 }
503 n1->nredir.redirect = redir;
504 }
505
506 checkneg:
507 if (negate) {
508 n2 = (union node *)stalloc(sizeof (struct nnot));
509 n2->type = NNOT;
510 n2->nnot.com = n1;
511 return n2;
512 }
513 else
514 return n1;
515 }
516
517
518 STATIC union node *
519 simplecmd(rpp, redir)
520 union node **rpp, *redir;
521 {
522 union node *args, **app;
523 union node **orig_rpp = rpp;
524 union node *n = NULL, *n2;
525 int negate = 0;
526
527 /* If we don't have any redirections already, then we must reset */
528 /* rpp to be the address of the local redir variable. */
529 if (redir == 0)
530 rpp = &redir;
531
532 args = NULL;
533 app = &args;
534 /*
535 * We save the incoming value, because we need this for shell
536 * functions. There can not be a redirect or an argument between
537 * the function name and the open parenthesis.
538 */
539 orig_rpp = rpp;
540
541 while (readtoken() == TNOT) {
542 TRACE(("command: TNOT recognized\n"));
543 negate = !negate;
544 }
545 tokpushback++;
546
547 for (;;) {
548 if (readtoken() == TWORD) {
549 n = (union node *)stalloc(sizeof (struct narg));
550 n->type = NARG;
551 n->narg.text = wordtext;
552 n->narg.backquote = backquotelist;
553 *app = n;
554 app = &n->narg.next;
555 } else if (lasttoken == TREDIR) {
556 *rpp = n = redirnode;
557 rpp = &n->nfile.next;
558 parsefname(); /* read name of redirection file */
559 } else if (lasttoken == TLP && app == &args->narg.next
560 && rpp == orig_rpp) {
561 /* We have a function */
562 if (readtoken() != TRP)
563 synexpect(TRP);
564 #ifdef notdef
565 if (! goodname(n->narg.text))
566 synerror("Bad function name");
567 #endif
568 n->type = NDEFUN;
569 n->narg.next = command();
570 goto checkneg;
571 } else {
572 tokpushback++;
573 break;
574 }
575 }
576 *app = NULL;
577 *rpp = NULL;
578 n = (union node *)stalloc(sizeof (struct ncmd));
579 n->type = NCMD;
580 n->ncmd.backgnd = 0;
581 n->ncmd.args = args;
582 n->ncmd.redirect = redir;
583
584 checkneg:
585 if (negate) {
586 n2 = (union node *)stalloc(sizeof (struct nnot));
587 n2->type = NNOT;
588 n2->nnot.com = n;
589 return n2;
590 }
591 else
592 return n;
593 }
594
595 STATIC union node *
596 makename() {
597 union node *n;
598
599 n = (union node *)stalloc(sizeof (struct narg));
600 n->type = NARG;
601 n->narg.next = NULL;
602 n->narg.text = wordtext;
603 n->narg.backquote = backquotelist;
604 return n;
605 }
606
607 void fixredir(union node *n, const char *text, int err)
608 {
609 TRACE(("Fix redir %s %d\n", text, err));
610 if (!err)
611 n->ndup.vname = NULL;
612
613 if (is_digit(text[0]) && text[1] == '\0')
614 n->ndup.dupfd = digit_val(text[0]);
615 else if (text[0] == '-' && text[1] == '\0')
616 n->ndup.dupfd = -1;
617 else {
618
619 if (err)
620 synerror("Bad fd number");
621 else
622 n->ndup.vname = makename();
623 }
624 }
625
626
627 STATIC void
628 parsefname() {
629 union node *n = redirnode;
630
631 if (readtoken() != TWORD)
632 synexpect(-1);
633 if (n->type == NHERE) {
634 struct heredoc *here = heredoc;
635 struct heredoc *p;
636 int i;
637
638 if (quoteflag == 0)
639 n->type = NXHERE;
640 TRACE(("Here document %d\n", n->type));
641 if (here->striptabs) {
642 while (*wordtext == '\t')
643 wordtext++;
644 }
645 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
646 synerror("Illegal eof marker for << redirection");
647 rmescapes(wordtext);
648 here->eofmark = wordtext;
649 here->next = NULL;
650 if (heredoclist == NULL)
651 heredoclist = here;
652 else {
653 for (p = heredoclist ; p->next ; p = p->next);
654 p->next = here;
655 }
656 } else if (n->type == NTOFD || n->type == NFROMFD) {
657 fixredir(n, wordtext, 0);
658 } else {
659 n->nfile.fname = makename();
660 }
661 }
662
663
664 /*
665 * Input any here documents.
666 */
667
668 STATIC void
669 parseheredoc() {
670 struct heredoc *here;
671 union node *n;
672
673 while (heredoclist) {
674 here = heredoclist;
675 heredoclist = here->next;
676 if (needprompt) {
677 setprompt(2);
678 needprompt = 0;
679 }
680 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
681 here->eofmark, here->striptabs);
682 n = (union node *)stalloc(sizeof (struct narg));
683 n->narg.type = NARG;
684 n->narg.next = NULL;
685 n->narg.text = wordtext;
686 n->narg.backquote = backquotelist;
687 here->here->nhere.doc = n;
688 }
689 }
690
691 STATIC int
692 peektoken() {
693 int t;
694
695 t = readtoken();
696 tokpushback++;
697 return (t);
698 }
699
700 STATIC int
701 readtoken() {
702 int t;
703 int savecheckkwd = checkkwd;
704 struct alias *ap;
705 #ifdef DEBUG
706 int alreadyseen = tokpushback;
707 #endif
708
709 top:
710 t = xxreadtoken();
711
712 if (checkkwd) {
713 /*
714 * eat newlines
715 */
716 if (checkkwd == 2) {
717 checkkwd = 0;
718 while (t == TNL) {
719 parseheredoc();
720 t = xxreadtoken();
721 }
722 } else
723 checkkwd = 0;
724 /*
725 * check for keywords and aliases
726 */
727 if (t == TWORD && !quoteflag)
728 {
729 const char *const *pp;
730
731 for (pp = parsekwd; *pp; pp++) {
732 if (**pp == *wordtext && equal(*pp, wordtext))
733 {
734 lasttoken = t = pp -
735 parsekwd + KWDOFFSET;
736 TRACE(("keyword %s recognized\n", tokname[t]));
737 goto out;
738 }
739 }
740 if ((ap = lookupalias(wordtext, 1)) != NULL) {
741 pushstring(ap->val, strlen(ap->val), ap);
742 checkkwd = savecheckkwd;
743 goto top;
744 }
745 }
746 out:
747 checkkwd = (t == TNOT) ? savecheckkwd : 0;
748 }
749 #ifdef DEBUG
750 if (!alreadyseen)
751 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
752 else
753 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
754 #endif
755 return (t);
756 }
757
758
759 /*
760 * Read the next input token.
761 * If the token is a word, we set backquotelist to the list of cmds in
762 * backquotes. We set quoteflag to true if any part of the word was
763 * quoted.
764 * If the token is TREDIR, then we set redirnode to a structure containing
765 * the redirection.
766 * In all cases, the variable startlinno is set to the number of the line
767 * on which the token starts.
768 *
769 * [Change comment: here documents and internal procedures]
770 * [Readtoken shouldn't have any arguments. Perhaps we should make the
771 * word parsing code into a separate routine. In this case, readtoken
772 * doesn't need to have any internal procedures, but parseword does.
773 * We could also make parseoperator in essence the main routine, and
774 * have parseword (readtoken1?) handle both words and redirection.]
775 */
776
777 #define RETURN(token) return lasttoken = token
778
779 STATIC int
780 xxreadtoken() {
781 int c;
782
783 if (tokpushback) {
784 tokpushback = 0;
785 return lasttoken;
786 }
787 if (needprompt) {
788 setprompt(2);
789 needprompt = 0;
790 }
791 startlinno = plinno;
792 for (;;) { /* until token or start of word found */
793 c = pgetc_macro();
794 if (c == ' ' || c == '\t')
795 continue; /* quick check for white space first */
796 switch (c) {
797 case ' ': case '\t':
798 continue;
799 case '#':
800 while ((c = pgetc()) != '\n' && c != PEOF);
801 pungetc();
802 continue;
803 case '\\':
804 if (pgetc() == '\n') {
805 startlinno = ++plinno;
806 if (doprompt)
807 setprompt(2);
808 else
809 setprompt(0);
810 continue;
811 }
812 pungetc();
813 goto breakloop;
814 case '\n':
815 plinno++;
816 needprompt = doprompt;
817 RETURN(TNL);
818 case PEOF:
819 RETURN(TEOF);
820 case '&':
821 if (pgetc() == '&')
822 RETURN(TAND);
823 pungetc();
824 RETURN(TBACKGND);
825 case '|':
826 if (pgetc() == '|')
827 RETURN(TOR);
828 pungetc();
829 RETURN(TPIPE);
830 case ';':
831 if (pgetc() == ';')
832 RETURN(TENDCASE);
833 pungetc();
834 RETURN(TSEMI);
835 case '(':
836 RETURN(TLP);
837 case ')':
838 RETURN(TRP);
839 default:
840 goto breakloop;
841 }
842 }
843 breakloop:
844 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
845 #undef RETURN
846 }
847
848
849
850 /*
851 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
852 * is not NULL, read a here document. In the latter case, eofmark is the
853 * word which marks the end of the document and striptabs is true if
854 * leading tabs should be stripped from the document. The argument firstc
855 * is the first character of the input token or document.
856 *
857 * Because C does not have internal subroutines, I have simulated them
858 * using goto's to implement the subroutine linkage. The following macros
859 * will run code that appears at the end of readtoken1.
860 */
861
862 #define CHECKEND() {goto checkend; checkend_return:;}
863 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
864 #define PARSESUB() {goto parsesub; parsesub_return:;}
865 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
866 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
867 #define PARSEARITH() {goto parsearith; parsearith_return:;}
868
869 STATIC int
870 readtoken1(firstc, syntax, eofmark, striptabs)
871 int firstc;
872 char const *syntax;
873 char *eofmark;
874 int striptabs;
875 {
876 int c = firstc;
877 char *out;
878 int len;
879 char line[EOFMARKLEN + 1];
880 struct nodelist *bqlist;
881 int quotef;
882 int dblquote;
883 int varnest; /* levels of variables expansion */
884 int arinest; /* levels of arithmetic expansion */
885 int parenlevel; /* levels of parens in arithmetic */
886 int oldstyle;
887 char const *prevsyntax; /* syntax before arithmetic */
888 #if __GNUC__
889 /* Avoid longjmp clobbering */
890 (void) &out;
891 (void) "ef;
892 (void) &dblquote;
893 (void) &varnest;
894 (void) &arinest;
895 (void) &parenlevel;
896 (void) &oldstyle;
897 (void) &prevsyntax;
898 (void) &syntax;
899 #endif
900
901 startlinno = plinno;
902 dblquote = 0;
903 if (syntax == DQSYNTAX)
904 dblquote = 1;
905 quotef = 0;
906 bqlist = NULL;
907 varnest = 0;
908 arinest = 0;
909 parenlevel = 0;
910
911 STARTSTACKSTR(out);
912 loop: { /* for each line, until end of word */
913 #if ATTY
914 if (c == '\034' && doprompt
915 && attyset() && ! equal(termval(), "emacs")) {
916 attyline();
917 if (syntax == BASESYNTAX)
918 return readtoken();
919 c = pgetc();
920 goto loop;
921 }
922 #endif
923 CHECKEND(); /* set c to PEOF if at end of here document */
924 for (;;) { /* until end of line or end of word */
925 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
926 switch(syntax[c]) {
927 case CNL: /* '\n' */
928 if (syntax == BASESYNTAX)
929 goto endword; /* exit outer loop */
930 USTPUTC(c, out);
931 plinno++;
932 if (doprompt)
933 setprompt(2);
934 else
935 setprompt(0);
936 c = pgetc();
937 goto loop; /* continue outer loop */
938 case CWORD:
939 USTPUTC(c, out);
940 break;
941 case CCTL:
942 if (eofmark == NULL || dblquote)
943 USTPUTC(CTLESC, out);
944 USTPUTC(c, out);
945 break;
946 case CBACK: /* backslash */
947 c = pgetc();
948 if (c == PEOF) {
949 USTPUTC('\\', out);
950 pungetc();
951 } else if (c == '\n') {
952 if (doprompt)
953 setprompt(2);
954 else
955 setprompt(0);
956 } else {
957 if (dblquote && c != '\\' && c != '`' && c != '$'
958 && (c != '"' || eofmark != NULL))
959 USTPUTC('\\', out);
960 if (SQSYNTAX[c] == CCTL)
961 USTPUTC(CTLESC, out);
962 else if (eofmark == NULL)
963 USTPUTC(CTLQUOTEMARK, out);
964 USTPUTC(c, out);
965 quotef++;
966 }
967 break;
968 case CSQUOTE:
969 if (eofmark == NULL)
970 USTPUTC(CTLQUOTEMARK, out);
971 syntax = SQSYNTAX;
972 break;
973 case CDQUOTE:
974 if (eofmark == NULL)
975 USTPUTC(CTLQUOTEMARK, out);
976 syntax = DQSYNTAX;
977 dblquote = 1;
978 break;
979 case CENDQUOTE:
980 if (eofmark != NULL && arinest == 0 &&
981 varnest == 0) {
982 USTPUTC(c, out);
983 } else {
984 if (arinest) {
985 syntax = ARISYNTAX;
986 dblquote = 0;
987 } else if (eofmark == NULL) {
988 syntax = BASESYNTAX;
989 dblquote = 0;
990 }
991 quotef++;
992 }
993 break;
994 case CVAR: /* '$' */
995 PARSESUB(); /* parse substitution */
996 break;
997 case CENDVAR: /* '}' */
998 if (varnest > 0) {
999 varnest--;
1000 USTPUTC(CTLENDVAR, out);
1001 } else {
1002 USTPUTC(c, out);
1003 }
1004 break;
1005 case CLP: /* '(' in arithmetic */
1006 parenlevel++;
1007 USTPUTC(c, out);
1008 break;
1009 case CRP: /* ')' in arithmetic */
1010 if (parenlevel > 0) {
1011 USTPUTC(c, out);
1012 --parenlevel;
1013 } else {
1014 if (pgetc() == ')') {
1015 if (--arinest == 0) {
1016 USTPUTC(CTLENDARI, out);
1017 syntax = prevsyntax;
1018 if (syntax == DQSYNTAX)
1019 dblquote = 1;
1020 else
1021 dblquote = 0;
1022 } else
1023 USTPUTC(')', out);
1024 } else {
1025 /*
1026 * unbalanced parens
1027 * (don't 2nd guess - no error)
1028 */
1029 pungetc();
1030 USTPUTC(')', out);
1031 }
1032 }
1033 break;
1034 case CBQUOTE: /* '`' */
1035 PARSEBACKQOLD();
1036 break;
1037 case CEOF:
1038 goto endword; /* exit outer loop */
1039 default:
1040 if (varnest == 0)
1041 goto endword; /* exit outer loop */
1042 USTPUTC(c, out);
1043 }
1044 c = pgetc_macro();
1045 }
1046 }
1047 endword:
1048 if (syntax == ARISYNTAX)
1049 synerror("Missing '))'");
1050 if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1051 synerror("Unterminated quoted string");
1052 if (varnest != 0) {
1053 startlinno = plinno;
1054 synerror("Missing '}'");
1055 }
1056 USTPUTC('\0', out);
1057 len = out - stackblock();
1058 out = stackblock();
1059 if (eofmark == NULL) {
1060 if ((c == '>' || c == '<')
1061 && quotef == 0
1062 && len <= 2
1063 && (*out == '\0' || is_digit(*out))) {
1064 PARSEREDIR();
1065 return lasttoken = TREDIR;
1066 } else {
1067 pungetc();
1068 }
1069 }
1070 quoteflag = quotef;
1071 backquotelist = bqlist;
1072 grabstackblock(len);
1073 wordtext = out;
1074 return lasttoken = TWORD;
1075 /* end of readtoken routine */
1076
1077
1078
1079 /*
1080 * Check to see whether we are at the end of the here document. When this
1081 * is called, c is set to the first character of the next input line. If
1082 * we are at the end of the here document, this routine sets the c to PEOF.
1083 */
1084
1085 checkend: {
1086 if (eofmark) {
1087 if (striptabs) {
1088 while (c == '\t')
1089 c = pgetc();
1090 }
1091 if (c == *eofmark) {
1092 if (pfgets(line, sizeof line) != NULL) {
1093 char *p, *q;
1094
1095 p = line;
1096 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1097 if (*p == '\n' && *q == '\0') {
1098 c = PEOF;
1099 plinno++;
1100 needprompt = doprompt;
1101 } else {
1102 pushstring(line, strlen(line), NULL);
1103 }
1104 }
1105 }
1106 }
1107 goto checkend_return;
1108 }
1109
1110
1111 /*
1112 * Parse a redirection operator. The variable "out" points to a string
1113 * specifying the fd to be redirected. The variable "c" contains the
1114 * first character of the redirection operator.
1115 */
1116
1117 parseredir: {
1118 char fd = *out;
1119 union node *np;
1120
1121 np = (union node *)stalloc(sizeof (struct nfile));
1122 if (c == '>') {
1123 np->nfile.fd = 1;
1124 c = pgetc();
1125 if (c == '>')
1126 np->type = NAPPEND;
1127 else if (c == '&')
1128 np->type = NTOFD;
1129 else {
1130 np->type = NTO;
1131 pungetc();
1132 }
1133 } else { /* c == '<' */
1134 np->nfile.fd = 0;
1135 switch (c = pgetc()) {
1136 case '<':
1137 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1138 np = (union node *)stalloc(sizeof (struct nhere));
1139 np->nfile.fd = 0;
1140 }
1141 np->type = NHERE;
1142 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1143 heredoc->here = np;
1144 if ((c = pgetc()) == '-') {
1145 heredoc->striptabs = 1;
1146 } else {
1147 heredoc->striptabs = 0;
1148 pungetc();
1149 }
1150 break;
1151
1152 case '&':
1153 np->type = NFROMFD;
1154 break;
1155
1156 case '>':
1157 np->type = NFROMTO;
1158 break;
1159
1160 default:
1161 np->type = NFROM;
1162 pungetc();
1163 break;
1164 }
1165 }
1166 if (fd != '\0')
1167 np->nfile.fd = digit_val(fd);
1168 redirnode = np;
1169 goto parseredir_return;
1170 }
1171
1172
1173 /*
1174 * Parse a substitution. At this point, we have read the dollar sign
1175 * and nothing else.
1176 */
1177
1178 parsesub: {
1179 int subtype;
1180 int typeloc;
1181 int flags;
1182 char *p;
1183 static const char types[] = "}-+?=";
1184
1185 c = pgetc();
1186 if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
1187 USTPUTC('$', out);
1188 pungetc();
1189 } else if (c == '(') { /* $(command) or $((arith)) */
1190 if (pgetc() == '(') {
1191 PARSEARITH();
1192 } else {
1193 pungetc();
1194 PARSEBACKQNEW();
1195 }
1196 } else {
1197 USTPUTC(CTLVAR, out);
1198 typeloc = out - stackblock();
1199 USTPUTC(VSNORMAL, out);
1200 subtype = VSNORMAL;
1201 if (c == '{') {
1202 c = pgetc();
1203 if (c == '#') {
1204 if ((c = pgetc()) == '}')
1205 c = '#';
1206 else
1207 subtype = VSLENGTH;
1208 }
1209 else
1210 subtype = 0;
1211 }
1212 if (is_name(c)) {
1213 do {
1214 STPUTC(c, out);
1215 c = pgetc();
1216 } while (is_in_name(c));
1217 } else if (is_digit(c)) {
1218 do {
1219 USTPUTC(c, out);
1220 c = pgetc();
1221 } while (is_digit(c));
1222 }
1223 else if (is_special(c)) {
1224 USTPUTC(c, out);
1225 c = pgetc();
1226 }
1227 else
1228 badsub: synerror("Bad substitution");
1229
1230 STPUTC('=', out);
1231 flags = 0;
1232 if (subtype == 0) {
1233 switch (c) {
1234 case ':':
1235 flags = VSNUL;
1236 c = pgetc();
1237 /*FALLTHROUGH*/
1238 default:
1239 p = strchr(types, c);
1240 if (p == NULL)
1241 goto badsub;
1242 subtype = p - types + VSNORMAL;
1243 break;
1244 case '%':
1245 case '#':
1246 {
1247 int cc = c;
1248 subtype = c == '#' ? VSTRIMLEFT :
1249 VSTRIMRIGHT;
1250 c = pgetc();
1251 if (c == cc)
1252 subtype++;
1253 else
1254 pungetc();
1255 break;
1256 }
1257 }
1258 } else {
1259 pungetc();
1260 }
1261 if (dblquote || arinest)
1262 flags |= VSQUOTE;
1263 *(stackblock() + typeloc) = subtype | flags;
1264 if (subtype != VSNORMAL)
1265 varnest++;
1266 }
1267 goto parsesub_return;
1268 }
1269
1270
1271 /*
1272 * Called to parse command substitutions. Newstyle is set if the command
1273 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1274 * list of commands (passed by reference), and savelen is the number of
1275 * characters on the top of the stack which must be preserved.
1276 */
1277
1278 parsebackq: {
1279 struct nodelist **nlpp;
1280 int savepbq;
1281 union node *n;
1282 char *volatile str;
1283 struct jmploc jmploc;
1284 struct jmploc *volatile savehandler;
1285 int savelen;
1286 int saveprompt;
1287 #ifdef __GNUC__
1288 (void) &saveprompt;
1289 #endif
1290
1291 savepbq = parsebackquote;
1292 if (setjmp(jmploc.loc)) {
1293 if (str)
1294 ckfree(str);
1295 parsebackquote = 0;
1296 handler = savehandler;
1297 longjmp(handler->loc, 1);
1298 }
1299 INTOFF;
1300 str = NULL;
1301 savelen = out - stackblock();
1302 if (savelen > 0) {
1303 str = ckmalloc(savelen);
1304 memcpy(str, stackblock(), savelen);
1305 }
1306 savehandler = handler;
1307 handler = &jmploc;
1308 INTON;
1309 if (oldstyle) {
1310 /* We must read until the closing backquote, giving special
1311 treatment to some slashes, and then push the string and
1312 reread it as input, interpreting it normally. */
1313 char *pout;
1314 int pc;
1315 int psavelen;
1316 char *pstr;
1317
1318
1319 STARTSTACKSTR(pout);
1320 for (;;) {
1321 if (needprompt) {
1322 setprompt(2);
1323 needprompt = 0;
1324 }
1325 switch (pc = pgetc()) {
1326 case '`':
1327 goto done;
1328
1329 case '\\':
1330 if ((pc = pgetc()) == '\n') {
1331 plinno++;
1332 if (doprompt)
1333 setprompt(2);
1334 else
1335 setprompt(0);
1336 /*
1337 * If eating a newline, avoid putting
1338 * the newline into the new character
1339 * stream (via the STPUTC after the
1340 * switch).
1341 */
1342 continue;
1343 }
1344 if (pc != '\\' && pc != '`' && pc != '$'
1345 && (!dblquote || pc != '"'))
1346 STPUTC('\\', pout);
1347 break;
1348
1349 case '\n':
1350 plinno++;
1351 needprompt = doprompt;
1352 break;
1353
1354 case PEOF:
1355 startlinno = plinno;
1356 synerror("EOF in backquote substitution");
1357 break;
1358
1359 default:
1360 break;
1361 }
1362 STPUTC(pc, pout);
1363 }
1364 done:
1365 STPUTC('\0', pout);
1366 psavelen = pout - stackblock();
1367 if (psavelen > 0) {
1368 pstr = grabstackstr(pout);
1369 setinputstring(pstr, 1);
1370 }
1371 }
1372 nlpp = &bqlist;
1373 while (*nlpp)
1374 nlpp = &(*nlpp)->next;
1375 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1376 (*nlpp)->next = NULL;
1377 parsebackquote = oldstyle;
1378
1379 if (oldstyle) {
1380 saveprompt = doprompt;
1381 doprompt = 0;
1382 }
1383
1384 n = list(0);
1385
1386 if (oldstyle)
1387 doprompt = saveprompt;
1388 else {
1389 if (readtoken() != TRP)
1390 synexpect(TRP);
1391 }
1392
1393 (*nlpp)->n = n;
1394 if (oldstyle) {
1395 /*
1396 * Start reading from old file again, ignoring any pushed back
1397 * tokens left from the backquote parsing
1398 */
1399 popfile();
1400 tokpushback = 0;
1401 }
1402 while (stackblocksize() <= savelen)
1403 growstackblock();
1404 STARTSTACKSTR(out);
1405 if (str) {
1406 memcpy(out, str, savelen);
1407 STADJUST(savelen, out);
1408 INTOFF;
1409 ckfree(str);
1410 str = NULL;
1411 INTON;
1412 }
1413 parsebackquote = savepbq;
1414 handler = savehandler;
1415 if (arinest || dblquote)
1416 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1417 else
1418 USTPUTC(CTLBACKQ, out);
1419 if (oldstyle)
1420 goto parsebackq_oldreturn;
1421 else
1422 goto parsebackq_newreturn;
1423 }
1424
1425 /*
1426 * Parse an arithmetic expansion (indicate start of one and set state)
1427 */
1428 parsearith: {
1429
1430 if (++arinest == 1) {
1431 prevsyntax = syntax;
1432 syntax = ARISYNTAX;
1433 USTPUTC(CTLARI, out);
1434 if (dblquote)
1435 USTPUTC('"',out);
1436 else
1437 USTPUTC(' ',out);
1438 } else {
1439 /*
1440 * we collapse embedded arithmetic expansion to
1441 * parenthesis, which should be equivalent
1442 */
1443 USTPUTC('(', out);
1444 }
1445 goto parsearith_return;
1446 }
1447
1448 } /* end of readtoken */
1449
1450
1451
1452 #ifdef mkinit
1453 RESET {
1454 tokpushback = 0;
1455 checkkwd = 0;
1456 }
1457 #endif
1458
1459 /*
1460 * Returns true if the text contains nothing to expand (no dollar signs
1461 * or backquotes).
1462 */
1463
1464 STATIC int
1465 noexpand(text)
1466 char *text;
1467 {
1468 char *p;
1469 char c;
1470
1471 p = text;
1472 while ((c = *p++) != '\0') {
1473 if (c == CTLQUOTEMARK)
1474 continue;
1475 if (c == CTLESC)
1476 p++;
1477 else if (BASESYNTAX[(int)c] == CCTL)
1478 return 0;
1479 }
1480 return 1;
1481 }
1482
1483
1484 /*
1485 * Return true if the argument is a legal variable name (a letter or
1486 * underscore followed by zero or more letters, underscores, and digits).
1487 */
1488
1489 int
1490 goodname(char *name)
1491 {
1492 char *p;
1493
1494 p = name;
1495 if (! is_name(*p))
1496 return 0;
1497 while (*++p) {
1498 if (! is_in_name(*p))
1499 return 0;
1500 }
1501 return 1;
1502 }
1503
1504
1505 /*
1506 * Called when an unexpected token is read during the parse. The argument
1507 * is the token that is expected, or -1 if more than one type of token can
1508 * occur at this point.
1509 */
1510
1511 STATIC void
1512 synexpect(token)
1513 int token;
1514 {
1515 char msg[64];
1516
1517 if (token >= 0) {
1518 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1519 tokname[lasttoken], tokname[token]);
1520 } else {
1521 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1522 }
1523 synerror(msg);
1524 /* NOTREACHED */
1525 }
1526
1527
1528 STATIC void
1529 synerror(msg)
1530 const char *msg;
1531 {
1532 if (commandname)
1533 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1534 outfmt(&errout, "Syntax error: %s\n", msg);
1535 error((char *)NULL);
1536 /* NOTREACHED */
1537 }
1538
1539 STATIC void
1540 setprompt(which)
1541 int which;
1542 {
1543 whichprompt = which;
1544
1545 #ifndef SMALL
1546 if (!el)
1547 #endif
1548 out2str(getprompt(NULL));
1549 }
1550
1551 /*
1552 * called by editline -- any expansions to the prompt
1553 * should be added here.
1554 */
1555 const char *
1556 getprompt(void *unused)
1557 {
1558 switch (whichprompt) {
1559 case 0:
1560 return "";
1561 case 1:
1562 return ps1val();
1563 case 2:
1564 return ps2val();
1565 default:
1566 return "<internal prompt error>";
1567 }
1568 }
1569