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