parser.c revision 1.103 1 /* $NetBSD: parser.c,v 1.103 2016/03/08 14:10:04 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #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.103 2016/03/08 14:10:04 christos Exp $");
41 #endif
42 #endif /* not lint */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <limits.h>
47
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
67
68 /*
69 * Shell command parser.
70 */
71
72 #define EOFMARKLEN 79
73
74 /* values returned by readtoken */
75 #include "token.h"
76
77 #define OPENBRACE '{'
78 #define CLOSEBRACE '}'
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 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 */
105
106
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);
123
124
125 static const char EOFhere[] = "EOF reading here (<<) document";
126
127 /*
128 * Read and parse a command. Returns NEOF on end of file. (NULL is a
129 * valid parse tree indicating a blank line.)
130 */
131
132 union node *
133 parsecmd(int interact)
134 {
135 int t;
136
137 tokpushback = 0;
138 doprompt = interact;
139 if (doprompt)
140 setprompt(1);
141 else
142 setprompt(0);
143 needprompt = 0;
144 t = readtoken();
145 if (t == TEOF)
146 return NEOF;
147 if (t == TNL)
148 return NULL;
149 tokpushback++;
150 return list(1, 0);
151 }
152
153
154 STATIC union node *
155 list(int nlflag, int erflag)
156 {
157 union node *n1, *n2, *n3;
158 int tok;
159 TRACE(("list: entered\n"));
160
161 checkkwd = 2;
162 if (nlflag == 0 && tokendlist[peektoken()])
163 return NULL;
164 n1 = NULL;
165 for (;;) {
166 n2 = andor();
167 tok = readtoken();
168 if (tok == TBACKGND) {
169 if (n2->type == NCMD || n2->type == NPIPE) {
170 n2->ncmd.backgnd = 1;
171 } else if (n2->type == NREDIR) {
172 n2->type = NBACKGND;
173 } else {
174 n3 = stalloc(sizeof(struct nredir));
175 n3->type = NBACKGND;
176 n3->nredir.n = n2;
177 n3->nredir.redirect = NULL;
178 n2 = n3;
179 }
180 }
181 if (n1 == NULL) {
182 n1 = n2;
183 }
184 else {
185 n3 = stalloc(sizeof(struct nbinary));
186 n3->type = NSEMI;
187 n3->nbinary.ch1 = n1;
188 n3->nbinary.ch2 = n2;
189 n1 = n3;
190 }
191 switch (tok) {
192 case TBACKGND:
193 case TSEMI:
194 tok = readtoken();
195 /* fall through */
196 case TNL:
197 if (tok == TNL) {
198 parseheredoc();
199 if (nlflag)
200 return n1;
201 } else {
202 tokpushback++;
203 }
204 checkkwd = 2;
205 if (tokendlist[peektoken()])
206 return n1;
207 break;
208 case TEOF:
209 if (heredoclist)
210 parseheredoc();
211 else
212 pungetc(); /* push back EOF on input */
213 return n1;
214 default:
215 if (nlflag || erflag)
216 synexpect(-1);
217 tokpushback++;
218 return n1;
219 }
220 }
221 }
222
223
224
225 STATIC union node *
226 andor(void)
227 {
228 union node *n1, *n2, *n3;
229 int t;
230
231 TRACE(("andor: entered\n"));
232 n1 = pipeline();
233 for (;;) {
234 if ((t = readtoken()) == TAND) {
235 t = NAND;
236 } else if (t == TOR) {
237 t = NOR;
238 } else {
239 tokpushback++;
240 return n1;
241 }
242 n2 = pipeline();
243 n3 = stalloc(sizeof(struct nbinary));
244 n3->type = t;
245 n3->nbinary.ch1 = n1;
246 n3->nbinary.ch2 = n2;
247 n1 = n3;
248 }
249 }
250
251
252
253 STATIC union node *
254 pipeline(void)
255 {
256 union node *n1, *n2, *pipenode;
257 struct nodelist *lp, *prev;
258 int negate;
259
260 TRACE(("pipeline: entered\n"));
261
262 negate = 0;
263 checkkwd = 2;
264 while (readtoken() == TNOT) {
265 TRACE(("pipeline: TNOT recognized\n"));
266 negate = !negate;
267 }
268 tokpushback++;
269 n1 = command();
270 if (readtoken() == TPIPE) {
271 pipenode = stalloc(sizeof(struct npipe));
272 pipenode->type = NPIPE;
273 pipenode->npipe.backgnd = 0;
274 lp = stalloc(sizeof(struct nodelist));
275 pipenode->npipe.cmdlist = lp;
276 lp->n = n1;
277 do {
278 prev = lp;
279 lp = stalloc(sizeof(struct nodelist));
280 lp->n = command();
281 prev->next = lp;
282 } while (readtoken() == TPIPE);
283 lp->next = NULL;
284 n1 = pipenode;
285 }
286 tokpushback++;
287 if (negate) {
288 TRACE(("negate pipeline\n"));
289 n2 = stalloc(sizeof(struct nnot));
290 n2->type = NNOT;
291 n2->nnot.com = n1;
292 return n2;
293 } else
294 return n1;
295 }
296
297
298
299 STATIC union node *
300 command(void)
301 {
302 union node *n1, *n2;
303 union node *ap, **app;
304 union node *cp, **cpp;
305 union node *redir, **rpp;
306 int t, negate = 0;
307
308 TRACE(("command: entered\n"));
309
310 checkkwd = 2;
311 redir = NULL;
312 n1 = NULL;
313 rpp = &redir;
314
315 /* Check for redirection which may precede command */
316 while (readtoken() == TREDIR) {
317 *rpp = n2 = redirnode;
318 rpp = &n2->nfile.next;
319 parsefname();
320 }
321 tokpushback++;
322
323 while (readtoken() == TNOT) {
324 TRACE(("command: TNOT recognized\n"));
325 negate = !negate;
326 }
327 tokpushback++;
328
329 switch (readtoken()) {
330 case TIF:
331 n1 = stalloc(sizeof(struct nif));
332 n1->type = NIF;
333 n1->nif.test = list(0, 0);
334 if (readtoken() != TTHEN)
335 synexpect(TTHEN);
336 n1->nif.ifpart = list(0, 0);
337 n2 = n1;
338 while (readtoken() == TELIF) {
339 n2->nif.elsepart = stalloc(sizeof(struct nif));
340 n2 = n2->nif.elsepart;
341 n2->type = NIF;
342 n2->nif.test = list(0, 0);
343 if (readtoken() != TTHEN)
344 synexpect(TTHEN);
345 n2->nif.ifpart = list(0, 0);
346 }
347 if (lasttoken == TELSE)
348 n2->nif.elsepart = list(0, 0);
349 else {
350 n2->nif.elsepart = NULL;
351 tokpushback++;
352 }
353 if (readtoken() != TFI)
354 synexpect(TFI);
355 checkkwd = 1;
356 break;
357 case TWHILE:
358 case TUNTIL: {
359 int got;
360 n1 = stalloc(sizeof(struct nbinary));
361 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
362 n1->nbinary.ch1 = list(0, 0);
363 if ((got=readtoken()) != TDO) {
364 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
365 synexpect(TDO);
366 }
367 n1->nbinary.ch2 = list(0, 0);
368 if (readtoken() != TDONE)
369 synexpect(TDONE);
370 checkkwd = 1;
371 break;
372 }
373 case TFOR:
374 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
375 synerror("Bad for loop variable");
376 n1 = stalloc(sizeof(struct nfor));
377 n1->type = NFOR;
378 n1->nfor.var = wordtext;
379 if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
380 app = ≈
381 while (readtoken() == TWORD) {
382 n2 = stalloc(sizeof(struct narg));
383 n2->type = NARG;
384 n2->narg.text = wordtext;
385 n2->narg.backquote = backquotelist;
386 *app = n2;
387 app = &n2->narg.next;
388 }
389 *app = NULL;
390 n1->nfor.args = ap;
391 if (lasttoken != TNL && lasttoken != TSEMI)
392 synexpect(-1);
393 } else {
394 static char argvars[5] = {
395 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
396 };
397 n2 = stalloc(sizeof(struct narg));
398 n2->type = NARG;
399 n2->narg.text = argvars;
400 n2->narg.backquote = NULL;
401 n2->narg.next = NULL;
402 n1->nfor.args = n2;
403 /*
404 * Newline or semicolon here is optional (but note
405 * that the original Bourne shell only allowed NL).
406 */
407 if (lasttoken != TNL && lasttoken != TSEMI)
408 tokpushback++;
409 }
410 checkkwd = 2;
411 if ((t = readtoken()) == TDO)
412 t = TDONE;
413 else if (t == TBEGIN)
414 t = TEND;
415 else
416 synexpect(-1);
417 n1->nfor.body = list(0, 0);
418 if (readtoken() != t)
419 synexpect(t);
420 checkkwd = 1;
421 break;
422 case TCASE:
423 n1 = stalloc(sizeof(struct ncase));
424 n1->type = NCASE;
425 if (readtoken() != TWORD)
426 synexpect(TWORD);
427 n1->ncase.expr = n2 = stalloc(sizeof(struct narg));
428 n2->type = NARG;
429 n2->narg.text = wordtext;
430 n2->narg.backquote = backquotelist;
431 n2->narg.next = NULL;
432 while (readtoken() == TNL);
433 if (lasttoken != TWORD || ! equal(wordtext, "in"))
434 synerror("expecting \"in\"");
435 cpp = &n1->ncase.cases;
436 noalias = 1;
437 checkkwd = 2, readtoken();
438 /*
439 * Both ksh and bash accept 'case x in esac'
440 * so configure scripts started taking advantage of this.
441 * The page: http://pubs.opengroup.org/onlinepubs/\
442 * 009695399/utilities/xcu_chap02.html contradicts itself,
443 * as to if this is legal; the "Case Conditional Format"
444 * paragraph shows one case is required, but the "Grammar"
445 * section shows a grammar that explicitly allows the no
446 * case option.
447 */
448 while (lasttoken != TESAC) {
449 *cpp = cp = stalloc(sizeof(struct nclist));
450 if (lasttoken == TLP)
451 readtoken();
452 cp->type = NCLIST;
453 app = &cp->nclist.pattern;
454 for (;;) {
455 *app = ap = stalloc(sizeof(struct narg));
456 ap->type = NARG;
457 ap->narg.text = wordtext;
458 ap->narg.backquote = backquotelist;
459 if (checkkwd = 2, readtoken() != TPIPE)
460 break;
461 app = &ap->narg.next;
462 readtoken();
463 }
464 ap->narg.next = NULL;
465 noalias = 0;
466 if (lasttoken != TRP) {
467 synexpect(TRP);
468 }
469 cp->nclist.body = list(0, 0);
470
471 checkkwd = 2;
472 if ((t = readtoken()) != TESAC) {
473 if (t != TENDCASE) {
474 noalias = 0;
475 synexpect(TENDCASE);
476 } else {
477 noalias = 1;
478 checkkwd = 2;
479 readtoken();
480 }
481 }
482 cpp = &cp->nclist.next;
483 }
484 noalias = 0;
485 *cpp = NULL;
486 checkkwd = 1;
487 break;
488 case TLP:
489 n1 = stalloc(sizeof(struct nredir));
490 n1->type = NSUBSHELL;
491 n1->nredir.n = list(0, 0);
492 n1->nredir.redirect = NULL;
493 if (readtoken() != TRP)
494 synexpect(TRP);
495 checkkwd = 1;
496 break;
497 case TBEGIN:
498 n1 = list(0, 0);
499 if (readtoken() != TEND)
500 synexpect(TEND);
501 checkkwd = 1;
502 break;
503 /* Handle an empty command like other simple commands. */
504 case TSEMI:
505 /*
506 * An empty command before a ; doesn't make much sense, and
507 * should certainly be disallowed in the case of `if ;'.
508 */
509 if (!redir)
510 synexpect(-1);
511 case TAND:
512 case TOR:
513 case TNL:
514 case TEOF:
515 case TWORD:
516 case TRP:
517 tokpushback++;
518 n1 = simplecmd(rpp, redir);
519 goto checkneg;
520 case TENDCASE:
521 if (redir) {
522 tokpushback++;
523 goto checkneg;
524 }
525 /* FALLTHROUGH */
526 default:
527 synexpect(-1);
528 /* NOTREACHED */
529 }
530
531 /* Now check for redirection which may follow command */
532 while (readtoken() == TREDIR) {
533 *rpp = n2 = redirnode;
534 rpp = &n2->nfile.next;
535 parsefname();
536 }
537 tokpushback++;
538 *rpp = NULL;
539 if (redir) {
540 if (n1->type != NSUBSHELL) {
541 n2 = stalloc(sizeof(struct nredir));
542 n2->type = NREDIR;
543 n2->nredir.n = n1;
544 n1 = n2;
545 }
546 n1->nredir.redirect = redir;
547 }
548
549 checkneg:
550 if (negate) {
551 TRACE(("negate command\n"));
552 n2 = stalloc(sizeof(struct nnot));
553 n2->type = NNOT;
554 n2->nnot.com = n1;
555 return n2;
556 }
557 else
558 return n1;
559 }
560
561
562 STATIC union node *
563 simplecmd(union node **rpp, union node *redir)
564 {
565 union node *args, **app;
566 union node **orig_rpp = rpp;
567 union node *n = NULL, *n2;
568 int negate = 0;
569
570 /* If we don't have any redirections already, then we must reset */
571 /* rpp to be the address of the local redir variable. */
572 if (redir == 0)
573 rpp = &redir;
574
575 args = NULL;
576 app = &args;
577 /*
578 * We save the incoming value, because we need this for shell
579 * functions. There can not be a redirect or an argument between
580 * the function name and the open parenthesis.
581 */
582 orig_rpp = rpp;
583
584 while (readtoken() == TNOT) {
585 TRACE(("simplcmd: TNOT recognized\n"));
586 negate = !negate;
587 }
588 tokpushback++;
589
590 for (;;) {
591 if (readtoken() == TWORD) {
592 n = stalloc(sizeof(struct narg));
593 n->type = NARG;
594 n->narg.text = wordtext;
595 n->narg.backquote = backquotelist;
596 *app = n;
597 app = &n->narg.next;
598 } else if (lasttoken == TREDIR) {
599 *rpp = n = redirnode;
600 rpp = &n->nfile.next;
601 parsefname(); /* read name of redirection file */
602 } else if (lasttoken == TLP && app == &args->narg.next
603 && rpp == orig_rpp) {
604 /* We have a function */
605 if (readtoken() != TRP)
606 synexpect(TRP);
607 funclinno = plinno;
608 rmescapes(n->narg.text);
609 if (!goodname(n->narg.text))
610 synerror("Bad function name");
611 n->type = NDEFUN;
612 n->narg.next = command();
613 funclinno = 0;
614 goto checkneg;
615 } else {
616 tokpushback++;
617 break;
618 }
619 }
620 *app = NULL;
621 *rpp = NULL;
622 n = stalloc(sizeof(struct ncmd));
623 n->type = NCMD;
624 n->ncmd.backgnd = 0;
625 n->ncmd.args = args;
626 n->ncmd.redirect = redir;
627
628 checkneg:
629 if (negate) {
630 TRACE(("negate simplecmd\n"));
631 n2 = stalloc(sizeof(struct nnot));
632 n2->type = NNOT;
633 n2->nnot.com = n;
634 return n2;
635 }
636 else
637 return n;
638 }
639
640 STATIC union node *
641 makename(void)
642 {
643 union node *n;
644
645 n = stalloc(sizeof(struct narg));
646 n->type = NARG;
647 n->narg.next = NULL;
648 n->narg.text = wordtext;
649 n->narg.backquote = backquotelist;
650 return n;
651 }
652
653 void
654 fixredir(union node *n, const char *text, int err)
655 {
656 TRACE(("Fix redir %s %d\n", text, err));
657 if (!err)
658 n->ndup.vname = NULL;
659
660 if (is_number(text))
661 n->ndup.dupfd = number(text);
662 else if (text[0] == '-' && text[1] == '\0')
663 n->ndup.dupfd = -1;
664 else {
665
666 if (err)
667 synerror("Bad fd number");
668 else
669 n->ndup.vname = makename();
670 }
671 }
672
673
674 STATIC void
675 parsefname(void)
676 {
677 union node *n = redirnode;
678
679 if (readtoken() != TWORD)
680 synexpect(-1);
681 if (n->type == NHERE) {
682 struct heredoc *here = heredoc;
683 struct heredoc *p;
684 int i;
685
686 if (quoteflag == 0)
687 n->type = NXHERE;
688 TRACE(("Here document %d\n", n->type));
689 if (here->striptabs) {
690 while (*wordtext == '\t')
691 wordtext++;
692 }
693 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
694 synerror("Illegal eof marker for << redirection");
695 rmescapes(wordtext);
696 here->eofmark = wordtext;
697 here->next = NULL;
698 if (heredoclist == NULL)
699 heredoclist = here;
700 else {
701 for (p = heredoclist ; p->next ; p = p->next)
702 continue;
703 p->next = here;
704 }
705 } else if (n->type == NTOFD || n->type == NFROMFD) {
706 fixredir(n, wordtext, 0);
707 } else {
708 n->nfile.fname = makename();
709 }
710 }
711
712
713 /*
714 * Input any here documents.
715 */
716
717 STATIC void
718 parseheredoc(void)
719 {
720 struct heredoc *here;
721 union node *n;
722
723 while (heredoclist) {
724 int c;
725
726 here = heredoclist;
727 heredoclist = here->next;
728 if (needprompt) {
729 setprompt(2);
730 needprompt = 0;
731 }
732 if ((c = pgetc()) == PEOF) {
733 synerror(EOFhere);
734 /* NOTREACHED */
735 }
736 readtoken1(c, here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
737 here->eofmark, here->striptabs);
738 n = stalloc(sizeof(struct narg));
739 n->narg.type = NARG;
740 n->narg.next = NULL;
741 n->narg.text = wordtext;
742 n->narg.backquote = backquotelist;
743 here->here->nhere.doc = n;
744 }
745 }
746
747 STATIC int
748 peektoken(void)
749 {
750 int t;
751
752 t = readtoken();
753 tokpushback++;
754 return (t);
755 }
756
757 STATIC int
758 readtoken(void)
759 {
760 int t;
761 int savecheckkwd = checkkwd;
762 #ifdef DEBUG
763 int alreadyseen = tokpushback;
764 #endif
765 struct alias *ap;
766
767 top:
768 t = xxreadtoken();
769
770 if (checkkwd) {
771 /*
772 * eat newlines
773 */
774 if (checkkwd == 2) {
775 checkkwd = 0;
776 while (t == TNL) {
777 parseheredoc();
778 t = xxreadtoken();
779 }
780 } else
781 checkkwd = 0;
782 /*
783 * check for keywords and aliases
784 */
785 if (t == TWORD && !quoteflag) {
786 const char *const *pp;
787
788 for (pp = parsekwd; *pp; pp++) {
789 if (**pp == *wordtext && equal(*pp, wordtext)) {
790 lasttoken = t = pp -
791 parsekwd + KWDOFFSET;
792 TRACE(("keyword %s recognized\n", tokname[t]));
793 goto out;
794 }
795 }
796 if (!noalias &&
797 (ap = lookupalias(wordtext, 1)) != NULL) {
798 pushstring(ap->val, strlen(ap->val), ap);
799 checkkwd = savecheckkwd;
800 goto top;
801 }
802 }
803 out:
804 checkkwd = (t == TNOT) ? savecheckkwd : 0;
805 }
806 TRACE(("%stoken %s %s\n", alreadyseen ? "reread " : "", tokname[t], t == TWORD ? wordtext : ""));
807 return (t);
808 }
809
810
811 /*
812 * Read the next input token.
813 * If the token is a word, we set backquotelist to the list of cmds in
814 * backquotes. We set quoteflag to true if any part of the word was
815 * quoted.
816 * If the token is TREDIR, then we set redirnode to a structure containing
817 * the redirection.
818 * In all cases, the variable startlinno is set to the number of the line
819 * on which the token starts.
820 *
821 * [Change comment: here documents and internal procedures]
822 * [Readtoken shouldn't have any arguments. Perhaps we should make the
823 * word parsing code into a separate routine. In this case, readtoken
824 * doesn't need to have any internal procedures, but parseword does.
825 * We could also make parseoperator in essence the main routine, and
826 * have parseword (readtoken1?) handle both words and redirection.]
827 */
828
829 #define RETURN(token) return lasttoken = token
830
831 STATIC int
832 xxreadtoken(void)
833 {
834 int c;
835
836 if (tokpushback) {
837 tokpushback = 0;
838 return lasttoken;
839 }
840 if (needprompt) {
841 setprompt(2);
842 needprompt = 0;
843 }
844 startlinno = plinno;
845 for (;;) { /* until token or start of word found */
846 c = pgetc_macro();
847 switch (c) {
848 case ' ': case '\t':
849 continue;
850 case '#':
851 while ((c = pgetc()) != '\n' && c != PEOF)
852 continue;
853 pungetc();
854 continue;
855 case '\\':
856 switch (pgetc()) {
857 case '\n':
858 startlinno = ++plinno;
859 if (doprompt)
860 setprompt(2);
861 else
862 setprompt(0);
863 continue;
864 case PEOF:
865 RETURN(TEOF);
866 default:
867 pungetc();
868 break;
869 }
870 goto breakloop;
871 case '\n':
872 plinno++;
873 needprompt = doprompt;
874 RETURN(TNL);
875 case PEOF:
876 RETURN(TEOF);
877 case '&':
878 if (pgetc() == '&')
879 RETURN(TAND);
880 pungetc();
881 RETURN(TBACKGND);
882 case '|':
883 if (pgetc() == '|')
884 RETURN(TOR);
885 pungetc();
886 RETURN(TPIPE);
887 case ';':
888 if (pgetc() == ';')
889 RETURN(TENDCASE);
890 pungetc();
891 RETURN(TSEMI);
892 case '(':
893 RETURN(TLP);
894 case ')':
895 RETURN(TRP);
896 default:
897 goto breakloop;
898 }
899 }
900 breakloop:
901 return readtoken1(c, BASESYNTAX, NULL, 0);
902 #undef RETURN
903 }
904
905
906
907 /*
908 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
909 * is not NULL, read a here document. In the latter case, eofmark is the
910 * word which marks the end of the document and striptabs is true if
911 * leading tabs should be stripped from the document. The argument firstc
912 * is the first character of the input token or document.
913 *
914 * Because C does not have internal subroutines, I have simulated them
915 * using goto's to implement the subroutine linkage. The following macros
916 * will run code that appears at the end of readtoken1.
917 */
918
919 /*
920 * We used to remember only the current syntax, variable nesting level,
921 * double quote state for each var nesting level, and arith nesting
922 * level (unrelated to var nesting) and one prev syntax when in arith
923 * syntax. This worked for simple cases, but can't handle arith inside
924 * var expansion inside arith inside var with some quoted and some not.
925 *
926 * Inspired by FreeBSD's implementation (though it was the obvious way)
927 * though implemented differently, we now have a stack that keeps track
928 * of what we are doing now, and what we were doing previously.
929 * Every time something changes, which will eventually end and should
930 * revert to the previous state, we push this stack, and then pop it
931 * again later (that is every ${} with an operator (to parse the word
932 * or pattern that follows) ${x} and $x are too simple to need it)
933 * $(( )) $( ) and "...". Always. Really, always!
934 *
935 * The stack is implemented as one static (on the C stack) base block
936 * containing LEVELS_PER_BLOCK (8) stack entries, which should be
937 * enough for the vast majority of cases. For torture tests, we
938 * malloc more blocks as needed. All accesses through the inline
939 * functions below.
940 */
941
942 /*
943 * varnest & arinest will typically be 0 or 1
944 * (varnest can increment in usages like ${x=${y}} but probably
945 * does not really need to)
946 * parenlevel allows balancing parens inside a $(( )), it is reset
947 * at each new nesting level ( $(( ( x + 3 ${unset-)} )) does not work.
948 * quoted is special - we need to know 2 things ... are we inside "..."
949 * (even if inherited from some previous nesting level) and was there
950 * an opening '"' at this level (so the next will be closing).
951 * "..." can span nexting levels, but cannot be opened in one and
952 * closed in a different one.
953 * To handle this, "quoted" has two fields, the bottom 4 (really 2)
954 * bits are 0, 1, or 2, for un, single, and double quoted (single quoted
955 * is really so special that this setting is not very important)
956 * and 0x10 that indicates that an opening quote has been seen.
957 * The bottom 4 bits are inherited, the 0x10 bit is not.
958 */
959 struct tokenstate {
960 const char *ts_syntax;
961 unsigned short ts_parenlevel; /* counters */
962 unsigned short ts_varnest; /* 64000 levels should be enough! */
963 unsigned short ts_arinest;
964 unsigned short ts_quoted; /* 1 -> single, 2 -> double */
965 };
966
967 #define NQ 0x00
968 #define SQ 0x01
969 #define DQ 0x02
970 #define QF 0x0F
971 #define QS 0x10
972
973 #define LEVELS_PER_BLOCK 8
974 #define VSS struct statestack
975 #define VVSS volatile VSS
976
977 struct statestack {
978 VSS *prev; /* previous block in list */
979 int cur; /* which of our tokenstates is current */
980 struct tokenstate tokenstate[LEVELS_PER_BLOCK];
981 };
982
983 static inline struct tokenstate *
984 currentstate(VSS *stack)
985 {
986 return &stack->tokenstate[stack->cur];
987 }
988
989 static inline struct tokenstate *
990 prevstate(VSS *stack)
991 {
992 if (stack->cur != 0)
993 return &stack->tokenstate[stack->cur - 1];
994 if (stack->prev == NULL) /* cannot drop below base */
995 return &stack->tokenstate[0];
996 return &stack->prev->tokenstate[LEVELS_PER_BLOCK - 1];
997 }
998
999 static inline VSS *
1000 bump_state_level(VSS *stack)
1001 {
1002 struct tokenstate *os, *ts;
1003
1004 os = currentstate(stack);
1005
1006 if (++stack->cur >= LEVELS_PER_BLOCK) {
1007 VSS *ss;
1008
1009 ss = (VSS *)ckmalloc(sizeof (struct statestack));
1010 ss->cur = 0;
1011 ss->prev = stack;
1012 stack = ss;
1013 }
1014
1015 ts = currentstate(stack);
1016
1017 ts->ts_parenlevel = 0; /* parens inside never match outside */
1018
1019 ts->ts_quoted = os->ts_quoted & QF; /* these are default settings */
1020 ts->ts_varnest = os->ts_varnest;
1021 ts->ts_arinest = os->ts_arinest; /* when appropriate */
1022 ts->ts_syntax = os->ts_syntax; /* they will be altered */
1023
1024 return stack;
1025 }
1026
1027 static inline VSS *
1028 drop_state_level(VSS *stack)
1029 {
1030 if (stack->cur == 0) {
1031 VSS *ss;
1032
1033 ss = stack;
1034 stack = ss->prev;
1035 if (stack == NULL)
1036 return ss;
1037 ckfree(__UNVOLATILE(ss));
1038 }
1039 --stack->cur;
1040 return stack;
1041 }
1042
1043 static inline void
1044 cleanup_state_stack(VSS *stack)
1045 {
1046 while (stack->prev != NULL) {
1047 stack->cur = 0;
1048 stack = drop_state_level(stack);
1049 }
1050 }
1051
1052 #define CHECKEND() {goto checkend; checkend_return:;}
1053 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
1054 #define PARSESUB() {goto parsesub; parsesub_return:;}
1055 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
1056 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
1057 #define PARSEARITH() {goto parsearith; parsearith_return:;}
1058
1059 /*
1060 * The following macros all assume the existance of a local var "stack"
1061 * which contains a pointer to the current struct stackstate
1062 */
1063
1064 /*
1065 * These are macros rather than inline funcs to avoid code churn as much
1066 * as possible - they replace macros of the same name used previously.
1067 */
1068 #define ISDBLQUOTE() (currentstate(stack)->ts_quoted & QS)
1069 #define SETDBLQUOTE() (currentstate(stack)->ts_quoted = QS | DQ)
1070 #define CLRDBLQUOTE() (currentstate(stack)->ts_quoted = \
1071 stack->cur != 0 || stack->prev ? \
1072 prevstate(stack)->ts_quoted & QF : 0)
1073
1074 /*
1075 * This set are just to avoid excess typing and line lengths...
1076 * The ones that "look like" var names must be implemented to be lvalues
1077 */
1078 #define syntax (currentstate(stack)->ts_syntax)
1079 #define parenlevel (currentstate(stack)->ts_parenlevel)
1080 #define varnest (currentstate(stack)->ts_varnest)
1081 #define arinest (currentstate(stack)->ts_arinest)
1082 #define quoted (currentstate(stack)->ts_quoted)
1083 #define TS_PUSH() (vstack = stack = bump_state_level(stack))
1084 #define TS_POP() (vstack = stack = drop_state_level(stack))
1085
1086 STATIC int
1087 readtoken1(int firstc, char const *syn, char *eofmark, int striptabs)
1088 {
1089 int c = firstc;
1090 char * out;
1091 int len;
1092 char line[EOFMARKLEN + 1];
1093 struct nodelist *bqlist;
1094 int quotef;
1095 int oldstyle;
1096 VSS static_stack;
1097 VSS *stack = &static_stack;
1098 VVSS *vstack = stack;
1099
1100 stack->prev = NULL;
1101 stack->cur = 0;
1102
1103 syntax = syn;
1104
1105 startlinno = plinno;
1106 varnest = 0;
1107 quoted = 0;
1108 if (syntax == DQSYNTAX) {
1109 SETDBLQUOTE();
1110 }
1111 quotef = 0;
1112 bqlist = NULL;
1113 arinest = 0;
1114 parenlevel = 0;
1115
1116 STARTSTACKSTR(out);
1117 loop: { /* for each line, until end of word */
1118 #if ATTY
1119 if (c == '\034' && doprompt
1120 && attyset() && ! equal(termval(), "emacs")) {
1121 attyline();
1122 if (syntax == BASESYNTAX)
1123 return readtoken();
1124 c = pgetc();
1125 goto loop;
1126 }
1127 #endif
1128 CHECKEND(); /* set c to PEOF if at end of here document */
1129 for (;;) { /* until end of line or end of word */
1130 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
1131 switch(syntax[c]) {
1132 case CNL: /* '\n' */
1133 if (syntax == BASESYNTAX)
1134 goto endword; /* exit outer loop */
1135 USTPUTC(c, out);
1136 plinno++;
1137 if (doprompt)
1138 setprompt(2);
1139 else
1140 setprompt(0);
1141 c = pgetc();
1142 goto loop; /* continue outer loop */
1143 case CWORD:
1144 USTPUTC(c, out);
1145 break;
1146 case CCTL:
1147 if (eofmark == NULL || ISDBLQUOTE())
1148 USTPUTC(CTLESC, out);
1149 USTPUTC(c, out);
1150 break;
1151 case CBACK: /* backslash */
1152 c = pgetc();
1153 if (c == PEOF) {
1154 USTPUTC('\\', out);
1155 pungetc();
1156 break;
1157 }
1158 if (c == '\n') {
1159 plinno++;
1160 if (doprompt)
1161 setprompt(2);
1162 else
1163 setprompt(0);
1164 break;
1165 }
1166 quotef = 1;
1167 if (ISDBLQUOTE() && c != '\\' &&
1168 c != '`' && c != '$' &&
1169 (c != '"' || eofmark != NULL))
1170 USTPUTC('\\', out);
1171 if (SQSYNTAX[c] == CCTL)
1172 USTPUTC(CTLESC, out);
1173 else if (eofmark == NULL) {
1174 USTPUTC(CTLQUOTEMARK, out);
1175 USTPUTC(c, out);
1176 if (varnest != 0)
1177 USTPUTC(CTLQUOTEEND, out);
1178 break;
1179 }
1180 USTPUTC(c, out);
1181 break;
1182 case CSQUOTE:
1183 if (syntax != SQSYNTAX) {
1184 if (eofmark == NULL)
1185 USTPUTC(CTLQUOTEMARK, out);
1186 quotef = 1;
1187 TS_PUSH();
1188 syntax = SQSYNTAX;
1189 quoted = SQ;
1190 break;
1191 }
1192 if (eofmark != NULL && arinest == 0 &&
1193 varnest == 0) {
1194 /* Ignore inside quoted here document */
1195 USTPUTC(c, out);
1196 break;
1197 }
1198 /* End of single quotes... */
1199 TS_POP();
1200 if (syntax == BASESYNTAX && varnest != 0)
1201 USTPUTC(CTLQUOTEEND, out);
1202 break;
1203 case CDQUOTE:
1204 if (eofmark != NULL && arinest == 0 &&
1205 varnest == 0) {
1206 /* Ignore inside here document */
1207 USTPUTC(c, out);
1208 break;
1209 }
1210 quotef = 1;
1211 if (arinest) {
1212 if (ISDBLQUOTE()) {
1213 TS_POP();
1214 } else {
1215 TS_PUSH();
1216 syntax = DQSYNTAX;
1217 SETDBLQUOTE();
1218 USTPUTC(CTLQUOTEMARK, out);
1219 }
1220 break;
1221 }
1222 if (eofmark != NULL)
1223 break;
1224 if (ISDBLQUOTE()) {
1225 TS_POP();
1226 if (varnest != 0)
1227 USTPUTC(CTLQUOTEEND, out);
1228 } else {
1229 TS_PUSH();
1230 syntax = DQSYNTAX;
1231 SETDBLQUOTE();
1232 USTPUTC(CTLQUOTEMARK, out);
1233 }
1234 break;
1235 case CVAR: /* '$' */
1236 PARSESUB(); /* parse substitution */
1237 break;
1238 case CENDVAR: /* CLOSEBRACE */
1239 if (varnest > 0 && !ISDBLQUOTE()) {
1240 TS_POP();
1241 USTPUTC(CTLENDVAR, out);
1242 } else {
1243 USTPUTC(c, out);
1244 }
1245 break;
1246 case CLP: /* '(' in arithmetic */
1247 parenlevel++;
1248 USTPUTC(c, out);
1249 break;
1250 case CRP: /* ')' in arithmetic */
1251 if (parenlevel > 0) {
1252 USTPUTC(c, out);
1253 --parenlevel;
1254 } else {
1255 if (pgetc() == ')') {
1256 if (--arinest == 0) {
1257 TS_POP();
1258 USTPUTC(CTLENDARI, out);
1259 } else
1260 USTPUTC(')', out);
1261 } else {
1262 /*
1263 * unbalanced parens
1264 * (don't 2nd guess - no error)
1265 */
1266 pungetc();
1267 USTPUTC(')', out);
1268 }
1269 }
1270 break;
1271 case CBQUOTE: /* '`' */
1272 PARSEBACKQOLD();
1273 break;
1274 case CEOF:
1275 goto endword; /* exit outer loop */
1276 default:
1277 if (varnest == 0 && !ISDBLQUOTE())
1278 goto endword; /* exit outer loop */
1279 USTPUTC(c, out);
1280 }
1281 c = pgetc_macro();
1282 }
1283 }
1284 endword:
1285 if (syntax == ARISYNTAX) {
1286 cleanup_state_stack(stack);
1287 synerror("Missing '))'");
1288 }
1289 if (syntax != BASESYNTAX && /* ! parsebackquote && */ eofmark == NULL) {
1290 cleanup_state_stack(stack);
1291 synerror("Unterminated quoted string");
1292 }
1293 if (varnest != 0) {
1294 cleanup_state_stack(stack);
1295 startlinno = plinno;
1296 /* { */
1297 synerror("Missing '}'");
1298 }
1299 USTPUTC('\0', out);
1300 len = out - stackblock();
1301 out = stackblock();
1302 if (eofmark == NULL) {
1303 if ((c == '>' || c == '<')
1304 && quotef == 0
1305 && (*out == '\0' || is_number(out))) {
1306 PARSEREDIR();
1307 cleanup_state_stack(stack);
1308 return lasttoken = TREDIR;
1309 } else {
1310 pungetc();
1311 }
1312 }
1313 quoteflag = quotef;
1314 backquotelist = bqlist;
1315 grabstackblock(len);
1316 wordtext = out;
1317 cleanup_state_stack(stack);
1318 return lasttoken = TWORD;
1319 /* end of readtoken routine */
1320
1321
1322
1323 /*
1324 * Check to see whether we are at the end of the here document. When this
1325 * is called, c is set to the first character of the next input line. If
1326 * we are at the end of the here document, this routine sets the c to PEOF.
1327 */
1328
1329 checkend: {
1330 if (eofmark) {
1331 if (c == PEOF)
1332 synerror(EOFhere);
1333 if (striptabs) {
1334 while (c == '\t')
1335 c = pgetc();
1336 }
1337 if (c == *eofmark) {
1338 if (pfgets(line, sizeof line) != NULL) {
1339 char *p, *q;
1340
1341 p = line;
1342 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++)
1343 continue;
1344 if ((*p == '\0' || *p == '\n') && *q == '\0') {
1345 c = PEOF;
1346 plinno++;
1347 needprompt = doprompt;
1348 } else {
1349 pushstring(line, strlen(line), NULL);
1350 }
1351 } else
1352 synerror(EOFhere);
1353 }
1354 }
1355 goto checkend_return;
1356 }
1357
1358
1359 /*
1360 * Parse a redirection operator. The variable "out" points to a string
1361 * specifying the fd to be redirected. The variable "c" contains the
1362 * first character of the redirection operator.
1363 */
1364
1365 parseredir: {
1366 char fd[64];
1367 union node *np;
1368 strlcpy(fd, out, sizeof(fd));
1369
1370 np = stalloc(sizeof(struct nfile));
1371 if (c == '>') {
1372 np->nfile.fd = 1;
1373 c = pgetc();
1374 if (c == '>')
1375 np->type = NAPPEND;
1376 else if (c == '|')
1377 np->type = NCLOBBER;
1378 else if (c == '&')
1379 np->type = NTOFD;
1380 else {
1381 np->type = NTO;
1382 pungetc();
1383 }
1384 } else { /* c == '<' */
1385 np->nfile.fd = 0;
1386 switch (c = pgetc()) {
1387 case '<':
1388 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1389 np = stalloc(sizeof(struct nhere));
1390 np->nfile.fd = 0;
1391 }
1392 np->type = NHERE;
1393 heredoc = stalloc(sizeof(struct heredoc));
1394 heredoc->here = np;
1395 if ((c = pgetc()) == '-') {
1396 heredoc->striptabs = 1;
1397 } else {
1398 heredoc->striptabs = 0;
1399 pungetc();
1400 }
1401 break;
1402
1403 case '&':
1404 np->type = NFROMFD;
1405 break;
1406
1407 case '>':
1408 np->type = NFROMTO;
1409 break;
1410
1411 default:
1412 np->type = NFROM;
1413 pungetc();
1414 break;
1415 }
1416 }
1417 if (*fd != '\0')
1418 np->nfile.fd = number(fd);
1419 redirnode = np;
1420 goto parseredir_return;
1421 }
1422
1423
1424 /*
1425 * Parse a substitution. At this point, we have read the dollar sign
1426 * and nothing else.
1427 */
1428
1429 parsesub: {
1430 char buf[10];
1431 int subtype;
1432 int typeloc;
1433 int flags;
1434 char *p;
1435 static const char types[] = "}-+?=";
1436 int i;
1437 int linno;
1438
1439 c = pgetc();
1440 if (c != '(' && c != OPENBRACE && !is_name(c) && !is_special(c)) {
1441 USTPUTC('$', out);
1442 pungetc();
1443 } else if (c == '(') { /* $(command) or $((arith)) */
1444 if (pgetc() == '(') {
1445 PARSEARITH();
1446 } else {
1447 pungetc();
1448 PARSEBACKQNEW();
1449 }
1450 } else {
1451 USTPUTC(CTLVAR, out);
1452 typeloc = out - stackblock();
1453 USTPUTC(VSNORMAL, out);
1454 subtype = VSNORMAL;
1455 flags = 0;
1456 if (c == OPENBRACE) {
1457 c = pgetc();
1458 if (c == '#') {
1459 if ((c = pgetc()) == CLOSEBRACE)
1460 c = '#';
1461 else
1462 subtype = VSLENGTH;
1463 }
1464 else
1465 subtype = 0;
1466 }
1467 if (is_name(c)) {
1468 p = out;
1469 do {
1470 STPUTC(c, out);
1471 c = pgetc();
1472 } while (is_in_name(c));
1473 if (out - p == 6 && strncmp(p, "LINENO", 6) == 0) {
1474 /* Replace the variable name with the
1475 * current line number. */
1476 linno = plinno;
1477 if (funclinno != 0)
1478 linno -= funclinno - 1;
1479 snprintf(buf, sizeof(buf), "%d", linno);
1480 STADJUST(-6, out);
1481 for (i = 0; buf[i] != '\0'; i++)
1482 STPUTC(buf[i], out);
1483 flags |= VSLINENO;
1484 }
1485 } else if (is_digit(c)) {
1486 do {
1487 USTPUTC(c, out);
1488 c = pgetc();
1489 } while (is_digit(c));
1490 }
1491 else if (is_special(c)) {
1492 USTPUTC(c, out);
1493 c = pgetc();
1494 }
1495 else {
1496 badsub:
1497 cleanup_state_stack(stack);
1498 synerror("Bad substitution");
1499 }
1500
1501 STPUTC('=', out);
1502 if (subtype == 0) {
1503 switch (c) {
1504 case ':':
1505 flags |= VSNUL;
1506 c = pgetc();
1507 /*FALLTHROUGH*/
1508 default:
1509 p = strchr(types, c);
1510 if (p == NULL)
1511 goto badsub;
1512 subtype = p - types + VSNORMAL;
1513 break;
1514 case '%':
1515 case '#':
1516 {
1517 int cc = c;
1518 subtype = c == '#' ? VSTRIMLEFT :
1519 VSTRIMRIGHT;
1520 c = pgetc();
1521 if (c == cc)
1522 subtype++;
1523 else
1524 pungetc();
1525 break;
1526 }
1527 }
1528 } else {
1529 pungetc();
1530 }
1531 if (ISDBLQUOTE() || arinest)
1532 flags |= VSQUOTE;
1533 if (subtype >= VSTRIMLEFT && subtype <= VSTRIMRIGHTMAX)
1534 flags |= VSPATQ;
1535 *(stackblock() + typeloc) = subtype | flags;
1536 if (subtype != VSNORMAL) {
1537 TS_PUSH();
1538 varnest++;
1539 arinest = 0;
1540 if (subtype > VSASSIGN) { /* # ## % %% */
1541 syntax = BASESYNTAX;
1542 CLRDBLQUOTE();
1543 }
1544 }
1545 }
1546 goto parsesub_return;
1547 }
1548
1549
1550 /*
1551 * Called to parse command substitutions. Newstyle is set if the command
1552 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1553 * list of commands (passed by reference), and savelen is the number of
1554 * characters on the top of the stack which must be preserved.
1555 */
1556
1557 parsebackq: {
1558 struct nodelist **nlpp;
1559 int savepbq;
1560 union node *n;
1561 char *str = NULL;
1562 struct jmploc jmploc;
1563 struct jmploc *volatile savehandler = NULL;
1564 int savelen;
1565 int saveprompt;
1566
1567 savepbq = parsebackquote;
1568 if (setjmp(jmploc.loc)) {
1569 if (str)
1570 ckfree(str);
1571 cleanup_state_stack(__UNVOLATILE(vstack));
1572 parsebackquote = 0;
1573 handler = savehandler;
1574 longjmp(handler->loc, 1);
1575 }
1576 INTOFF;
1577 str = NULL;
1578 savelen = out - stackblock();
1579 if (savelen > 0) {
1580 str = ckmalloc(savelen);
1581 memcpy(str, stackblock(), savelen);
1582 }
1583 savehandler = handler;
1584 handler = &jmploc;
1585 INTON;
1586 if (oldstyle) {
1587 /* We must read until the closing backquote, giving special
1588 treatment to some slashes, and then push the string and
1589 reread it as input, interpreting it normally. */
1590 char *pout;
1591 int pc;
1592 int psavelen;
1593 char *pstr;
1594
1595 /*
1596 * Because the entire `...` is read here, we don't
1597 * need to bother the state stack. That will be used
1598 * (as appropriate) when the processed string is re-read.
1599 */
1600 STARTSTACKSTR(pout);
1601 for (;;) {
1602 if (needprompt) {
1603 setprompt(2);
1604 needprompt = 0;
1605 }
1606 switch (pc = pgetc()) {
1607 case '`':
1608 goto done;
1609
1610 case '\\':
1611 if ((pc = pgetc()) == '\n') {
1612 plinno++;
1613 if (doprompt)
1614 setprompt(2);
1615 else
1616 setprompt(0);
1617 /*
1618 * If eating a newline, avoid putting
1619 * the newline into the new character
1620 * stream (via the STPUTC after the
1621 * switch).
1622 */
1623 continue;
1624 }
1625 if (pc != '\\' && pc != '`' && pc != '$'
1626 && (!ISDBLQUOTE() || pc != '"'))
1627 STPUTC('\\', pout);
1628 break;
1629
1630 case '\n':
1631 plinno++;
1632 needprompt = doprompt;
1633 break;
1634
1635 case PEOF:
1636 startlinno = plinno;
1637 synerror("EOF in backquote substitution");
1638 break;
1639
1640 default:
1641 break;
1642 }
1643 STPUTC(pc, pout);
1644 }
1645 done:
1646 STPUTC('\0', pout);
1647 psavelen = pout - stackblock();
1648 if (psavelen > 0) {
1649 pstr = grabstackstr(pout);
1650 setinputstring(pstr, 1);
1651 }
1652 }
1653 nlpp = &bqlist;
1654 while (*nlpp)
1655 nlpp = &(*nlpp)->next;
1656 *nlpp = stalloc(sizeof(struct nodelist));
1657 (*nlpp)->next = NULL;
1658 parsebackquote = oldstyle;
1659
1660 if (oldstyle) {
1661 saveprompt = doprompt;
1662 doprompt = 0;
1663 } else
1664 saveprompt = 0;
1665
1666 n = list(0, oldstyle);
1667
1668 if (oldstyle)
1669 doprompt = saveprompt;
1670 else {
1671 if (readtoken() != TRP) {
1672 cleanup_state_stack(stack);
1673 synexpect(TRP);
1674 }
1675 }
1676
1677 (*nlpp)->n = n;
1678 if (oldstyle) {
1679 /*
1680 * Start reading from old file again, ignoring any pushed back
1681 * tokens left from the backquote parsing
1682 */
1683 popfile();
1684 tokpushback = 0;
1685 }
1686 while (stackblocksize() <= savelen)
1687 growstackblock();
1688 STARTSTACKSTR(out);
1689 if (str) {
1690 memcpy(out, str, savelen);
1691 STADJUST(savelen, out);
1692 INTOFF;
1693 ckfree(str);
1694 str = NULL;
1695 INTON;
1696 }
1697 parsebackquote = savepbq;
1698 handler = savehandler;
1699 if (arinest || ISDBLQUOTE())
1700 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1701 else
1702 USTPUTC(CTLBACKQ, out);
1703 if (oldstyle)
1704 goto parsebackq_oldreturn;
1705 else
1706 goto parsebackq_newreturn;
1707 }
1708
1709 /*
1710 * Parse an arithmetic expansion (indicate start of one and set state)
1711 */
1712 parsearith: {
1713
1714 if (syntax == ARISYNTAX) {
1715 /*
1716 * we collapse embedded arithmetic expansion to
1717 * parentheses, which should be equivalent
1718 */
1719 USTPUTC('(', out);
1720 USTPUTC('(', out);
1721 /*
1722 * Need 2 of them because there will (should be)
1723 * two closing ))'s to follow later.
1724 */
1725 parenlevel += 2;
1726 } else {
1727 TS_PUSH();
1728 syntax = ARISYNTAX;
1729 ++arinest;
1730 varnest = 0;
1731
1732 USTPUTC(CTLARI, out);
1733 if (ISDBLQUOTE())
1734 USTPUTC('"',out);
1735 else
1736 USTPUTC(' ',out);
1737 }
1738 goto parsearith_return;
1739 }
1740
1741 } /* end of readtoken */
1742
1743
1744
1745 #ifdef mkinit
1746 RESET {
1747 tokpushback = 0;
1748 checkkwd = 0;
1749 }
1750 #endif
1751
1752 /*
1753 * Returns true if the text contains nothing to expand (no dollar signs
1754 * or backquotes).
1755 */
1756
1757 STATIC int
1758 noexpand(char *text)
1759 {
1760 char *p;
1761 char c;
1762
1763 p = text;
1764 while ((c = *p++) != '\0') {
1765 if (c == CTLQUOTEMARK)
1766 continue;
1767 if (c == CTLESC)
1768 p++;
1769 else if (BASESYNTAX[(int)c] == CCTL)
1770 return 0;
1771 }
1772 return 1;
1773 }
1774
1775
1776 /*
1777 * Return true if the argument is a legal variable name (a letter or
1778 * underscore followed by zero or more letters, underscores, and digits).
1779 */
1780
1781 int
1782 goodname(char *name)
1783 {
1784 char *p;
1785
1786 p = name;
1787 if (! is_name(*p))
1788 return 0;
1789 while (*++p) {
1790 if (! is_in_name(*p))
1791 return 0;
1792 }
1793 return 1;
1794 }
1795
1796
1797 /*
1798 * Called when an unexpected token is read during the parse. The argument
1799 * is the token that is expected, or -1 if more than one type of token can
1800 * occur at this point.
1801 */
1802
1803 STATIC void
1804 synexpect(int token)
1805 {
1806 char msg[64];
1807
1808 if (token >= 0) {
1809 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1810 tokname[lasttoken], tokname[token]);
1811 } else {
1812 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1813 }
1814 synerror(msg);
1815 /* NOTREACHED */
1816 }
1817
1818
1819 STATIC void
1820 synerror(const char *msg)
1821 {
1822 if (commandname)
1823 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1824 else
1825 outfmt(&errout, "%s: ", getprogname());
1826 outfmt(&errout, "Syntax error: %s\n", msg);
1827 error(NULL);
1828 /* NOTREACHED */
1829 }
1830
1831 STATIC void
1832 setprompt(int which)
1833 {
1834 whichprompt = which;
1835
1836 #ifndef SMALL
1837 if (!el)
1838 #endif
1839 out2str(getprompt(NULL));
1840 }
1841
1842 /*
1843 * called by editline -- any expansions to the prompt
1844 * should be added here.
1845 */
1846 const char *
1847 getprompt(void *unused)
1848 {
1849 switch (whichprompt) {
1850 case 0:
1851 return "";
1852 case 1:
1853 return ps1val();
1854 case 2:
1855 return ps2val();
1856 default:
1857 return "<internal prompt error>";
1858 }
1859 }
1860