parser.c revision 1.102 1 /* $NetBSD: parser.c,v 1.102 2016/02/28 23:12:23 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.102 2016/02/28 23:12:23 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 volatile struct statestack
975
976 struct statestack {
977 VSS *prev; /* previous block in list */
978 int cur; /* which of our tokenstates is current */
979 struct tokenstate tokenstate[LEVELS_PER_BLOCK];
980 };
981
982 static inline volatile struct tokenstate *
983 currentstate(VSS *stack)
984 {
985 return &stack->tokenstate[stack->cur];
986 }
987
988 static inline volatile struct tokenstate *
989 prevstate(VSS *stack)
990 {
991 if (stack->cur != 0)
992 return &stack->tokenstate[stack->cur - 1];
993 if (stack->prev == NULL) /* cannot drop below base */
994 return &stack->tokenstate[0];
995 return &stack->prev->tokenstate[LEVELS_PER_BLOCK - 1];
996 }
997
998 static inline VSS *
999 bump_state_level(VSS *stack)
1000 {
1001 volatile struct tokenstate *os, *ts;
1002
1003 os = currentstate(stack);
1004
1005 if (++stack->cur >= LEVELS_PER_BLOCK) {
1006 VSS *ss;
1007
1008 ss = (VSS *)ckmalloc(sizeof (struct statestack));
1009 ss->cur = 0;
1010 ss->prev = stack;
1011 stack = ss;
1012 }
1013
1014 ts = currentstate(stack);
1015
1016 ts->ts_parenlevel = 0; /* parens inside never match outside */
1017
1018 ts->ts_quoted = os->ts_quoted & QF; /* these are default settings */
1019 ts->ts_varnest = os->ts_varnest;
1020 ts->ts_arinest = os->ts_arinest; /* when appropriate */
1021 ts->ts_syntax = os->ts_syntax; /* they will be altered */
1022
1023 return stack;
1024 }
1025
1026 static inline VSS *
1027 drop_state_level(VSS *stack)
1028 {
1029 if (stack->cur == 0) {
1030 VSS *ss;
1031
1032 ss = stack;
1033 stack = ss->prev;
1034 if (stack == NULL)
1035 return ss;
1036 ckfree(__UNVOLATILE(ss));
1037 }
1038 --stack->cur;
1039 return stack;
1040 }
1041
1042 static inline void
1043 cleanup_state_stack(VSS *stack)
1044 {
1045 while (stack->prev != NULL) {
1046 stack->cur = 0;
1047 stack = drop_state_level(stack);
1048 }
1049 }
1050
1051 #define CHECKEND() {goto checkend; checkend_return:;}
1052 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
1053 #define PARSESUB() {goto parsesub; parsesub_return:;}
1054 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
1055 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
1056 #define PARSEARITH() {goto parsearith; parsearith_return:;}
1057
1058 /*
1059 * The following macros all assume the existance of a local var "stack"
1060 * which contains a pointer to the current struct stackstate
1061 */
1062
1063 /*
1064 * These are macros rather than inline funcs to avoid code churn as much
1065 * as possible - they replace macros of the same name used previously.
1066 */
1067 #define ISDBLQUOTE() (currentstate(stack)->ts_quoted & QS)
1068 #define SETDBLQUOTE() (currentstate(stack)->ts_quoted = QS | DQ)
1069 #define CLRDBLQUOTE() (currentstate(stack)->ts_quoted = \
1070 stack->cur != 0 || stack->prev ? \
1071 prevstate(stack)->ts_quoted & QF : 0)
1072
1073 /*
1074 * This set are just to avoid excess typing and line lengths...
1075 * The ones that "look like" var names must be implemented to be lvalues
1076 */
1077 #define syntax (currentstate(stack)->ts_syntax)
1078 #define parenlevel (currentstate(stack)->ts_parenlevel)
1079 #define varnest (currentstate(stack)->ts_varnest)
1080 #define arinest (currentstate(stack)->ts_arinest)
1081 #define quoted (currentstate(stack)->ts_quoted)
1082 #define TS_PUSH() (stack = bump_state_level(stack))
1083 #define TS_POP() (stack = drop_state_level(stack))
1084
1085 STATIC int
1086 readtoken1(int firstc, char const *syn, char *eofmark, int striptabs)
1087 {
1088 int c = firstc;
1089 char * volatile out;
1090 int len;
1091 char line[EOFMARKLEN + 1];
1092 struct nodelist *bqlist;
1093 volatile int quotef;
1094 volatile int oldstyle;
1095 VSS static_stack;
1096 VSS *stack = &static_stack;
1097
1098 stack->prev = NULL;
1099 stack->cur = 0;
1100
1101 syntax = syn;
1102
1103 startlinno = plinno;
1104 varnest = 0;
1105 quoted = 0;
1106 if (syntax == DQSYNTAX) {
1107 SETDBLQUOTE();
1108 }
1109 quotef = 0;
1110 bqlist = NULL;
1111 arinest = 0;
1112 parenlevel = 0;
1113
1114 STARTSTACKSTR(out);
1115 loop: { /* for each line, until end of word */
1116 #if ATTY
1117 if (c == '\034' && doprompt
1118 && attyset() && ! equal(termval(), "emacs")) {
1119 attyline();
1120 if (syntax == BASESYNTAX)
1121 return readtoken();
1122 c = pgetc();
1123 goto loop;
1124 }
1125 #endif
1126 CHECKEND(); /* set c to PEOF if at end of here document */
1127 for (;;) { /* until end of line or end of word */
1128 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
1129 switch(syntax[c]) {
1130 case CNL: /* '\n' */
1131 if (syntax == BASESYNTAX)
1132 goto endword; /* exit outer loop */
1133 USTPUTC(c, out);
1134 plinno++;
1135 if (doprompt)
1136 setprompt(2);
1137 else
1138 setprompt(0);
1139 c = pgetc();
1140 goto loop; /* continue outer loop */
1141 case CWORD:
1142 USTPUTC(c, out);
1143 break;
1144 case CCTL:
1145 if (eofmark == NULL || ISDBLQUOTE())
1146 USTPUTC(CTLESC, out);
1147 USTPUTC(c, out);
1148 break;
1149 case CBACK: /* backslash */
1150 c = pgetc();
1151 if (c == PEOF) {
1152 USTPUTC('\\', out);
1153 pungetc();
1154 break;
1155 }
1156 if (c == '\n') {
1157 plinno++;
1158 if (doprompt)
1159 setprompt(2);
1160 else
1161 setprompt(0);
1162 break;
1163 }
1164 quotef = 1;
1165 if (ISDBLQUOTE() && c != '\\' &&
1166 c != '`' && c != '$' &&
1167 (c != '"' || eofmark != NULL))
1168 USTPUTC('\\', out);
1169 if (SQSYNTAX[c] == CCTL)
1170 USTPUTC(CTLESC, out);
1171 else if (eofmark == NULL) {
1172 USTPUTC(CTLQUOTEMARK, out);
1173 USTPUTC(c, out);
1174 if (varnest != 0)
1175 USTPUTC(CTLQUOTEEND, out);
1176 break;
1177 }
1178 USTPUTC(c, out);
1179 break;
1180 case CSQUOTE:
1181 if (syntax != SQSYNTAX) {
1182 if (eofmark == NULL)
1183 USTPUTC(CTLQUOTEMARK, out);
1184 quotef = 1;
1185 TS_PUSH();
1186 syntax = SQSYNTAX;
1187 quoted = SQ;
1188 break;
1189 }
1190 if (eofmark != NULL && arinest == 0 &&
1191 varnest == 0) {
1192 /* Ignore inside quoted here document */
1193 USTPUTC(c, out);
1194 break;
1195 }
1196 /* End of single quotes... */
1197 TS_POP();
1198 if (syntax == BASESYNTAX && varnest != 0)
1199 USTPUTC(CTLQUOTEEND, out);
1200 break;
1201 case CDQUOTE:
1202 if (eofmark != NULL && arinest == 0 &&
1203 varnest == 0) {
1204 /* Ignore inside here document */
1205 USTPUTC(c, out);
1206 break;
1207 }
1208 quotef = 1;
1209 if (arinest) {
1210 if (ISDBLQUOTE()) {
1211 TS_POP();
1212 } else {
1213 TS_PUSH();
1214 syntax = DQSYNTAX;
1215 SETDBLQUOTE();
1216 USTPUTC(CTLQUOTEMARK, out);
1217 }
1218 break;
1219 }
1220 if (eofmark != NULL)
1221 break;
1222 if (ISDBLQUOTE()) {
1223 TS_POP();
1224 if (varnest != 0)
1225 USTPUTC(CTLQUOTEEND, out);
1226 } else {
1227 TS_PUSH();
1228 syntax = DQSYNTAX;
1229 SETDBLQUOTE();
1230 USTPUTC(CTLQUOTEMARK, out);
1231 }
1232 break;
1233 case CVAR: /* '$' */
1234 PARSESUB(); /* parse substitution */
1235 break;
1236 case CENDVAR: /* CLOSEBRACE */
1237 if (varnest > 0 && !ISDBLQUOTE()) {
1238 TS_POP();
1239 USTPUTC(CTLENDVAR, out);
1240 } else {
1241 USTPUTC(c, out);
1242 }
1243 break;
1244 case CLP: /* '(' in arithmetic */
1245 parenlevel++;
1246 USTPUTC(c, out);
1247 break;
1248 case CRP: /* ')' in arithmetic */
1249 if (parenlevel > 0) {
1250 USTPUTC(c, out);
1251 --parenlevel;
1252 } else {
1253 if (pgetc() == ')') {
1254 if (--arinest == 0) {
1255 TS_POP();
1256 USTPUTC(CTLENDARI, out);
1257 } else
1258 USTPUTC(')', out);
1259 } else {
1260 /*
1261 * unbalanced parens
1262 * (don't 2nd guess - no error)
1263 */
1264 pungetc();
1265 USTPUTC(')', out);
1266 }
1267 }
1268 break;
1269 case CBQUOTE: /* '`' */
1270 PARSEBACKQOLD();
1271 break;
1272 case CEOF:
1273 goto endword; /* exit outer loop */
1274 default:
1275 if (varnest == 0 && !ISDBLQUOTE())
1276 goto endword; /* exit outer loop */
1277 USTPUTC(c, out);
1278 }
1279 c = pgetc_macro();
1280 }
1281 }
1282 endword:
1283 if (syntax == ARISYNTAX) {
1284 cleanup_state_stack(stack);
1285 synerror("Missing '))'");
1286 }
1287 if (syntax != BASESYNTAX && /* ! parsebackquote && */ eofmark == NULL) {
1288 cleanup_state_stack(stack);
1289 synerror("Unterminated quoted string");
1290 }
1291 if (varnest != 0) {
1292 cleanup_state_stack(stack);
1293 startlinno = plinno;
1294 /* { */
1295 synerror("Missing '}'");
1296 }
1297 USTPUTC('\0', out);
1298 len = out - stackblock();
1299 out = stackblock();
1300 if (eofmark == NULL) {
1301 if ((c == '>' || c == '<')
1302 && quotef == 0
1303 && (*out == '\0' || is_number(out))) {
1304 PARSEREDIR();
1305 cleanup_state_stack(stack);
1306 return lasttoken = TREDIR;
1307 } else {
1308 pungetc();
1309 }
1310 }
1311 quoteflag = quotef;
1312 backquotelist = bqlist;
1313 grabstackblock(len);
1314 wordtext = out;
1315 cleanup_state_stack(stack);
1316 return lasttoken = TWORD;
1317 /* end of readtoken routine */
1318
1319
1320
1321 /*
1322 * Check to see whether we are at the end of the here document. When this
1323 * is called, c is set to the first character of the next input line. If
1324 * we are at the end of the here document, this routine sets the c to PEOF.
1325 */
1326
1327 checkend: {
1328 if (eofmark) {
1329 if (c == PEOF)
1330 synerror(EOFhere);
1331 if (striptabs) {
1332 while (c == '\t')
1333 c = pgetc();
1334 }
1335 if (c == *eofmark) {
1336 if (pfgets(line, sizeof line) != NULL) {
1337 char *p, *q;
1338
1339 p = line;
1340 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++)
1341 continue;
1342 if ((*p == '\0' || *p == '\n') && *q == '\0') {
1343 c = PEOF;
1344 plinno++;
1345 needprompt = doprompt;
1346 } else {
1347 pushstring(line, strlen(line), NULL);
1348 }
1349 } else
1350 synerror(EOFhere);
1351 }
1352 }
1353 goto checkend_return;
1354 }
1355
1356
1357 /*
1358 * Parse a redirection operator. The variable "out" points to a string
1359 * specifying the fd to be redirected. The variable "c" contains the
1360 * first character of the redirection operator.
1361 */
1362
1363 parseredir: {
1364 char fd[64];
1365 union node *np;
1366 strlcpy(fd, out, sizeof(fd));
1367
1368 np = stalloc(sizeof(struct nfile));
1369 if (c == '>') {
1370 np->nfile.fd = 1;
1371 c = pgetc();
1372 if (c == '>')
1373 np->type = NAPPEND;
1374 else if (c == '|')
1375 np->type = NCLOBBER;
1376 else if (c == '&')
1377 np->type = NTOFD;
1378 else {
1379 np->type = NTO;
1380 pungetc();
1381 }
1382 } else { /* c == '<' */
1383 np->nfile.fd = 0;
1384 switch (c = pgetc()) {
1385 case '<':
1386 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1387 np = stalloc(sizeof(struct nhere));
1388 np->nfile.fd = 0;
1389 }
1390 np->type = NHERE;
1391 heredoc = stalloc(sizeof(struct heredoc));
1392 heredoc->here = np;
1393 if ((c = pgetc()) == '-') {
1394 heredoc->striptabs = 1;
1395 } else {
1396 heredoc->striptabs = 0;
1397 pungetc();
1398 }
1399 break;
1400
1401 case '&':
1402 np->type = NFROMFD;
1403 break;
1404
1405 case '>':
1406 np->type = NFROMTO;
1407 break;
1408
1409 default:
1410 np->type = NFROM;
1411 pungetc();
1412 break;
1413 }
1414 }
1415 if (*fd != '\0')
1416 np->nfile.fd = number(fd);
1417 redirnode = np;
1418 goto parseredir_return;
1419 }
1420
1421
1422 /*
1423 * Parse a substitution. At this point, we have read the dollar sign
1424 * and nothing else.
1425 */
1426
1427 parsesub: {
1428 char buf[10];
1429 int subtype;
1430 int typeloc;
1431 int flags;
1432 char *p;
1433 static const char types[] = "}-+?=";
1434 int i;
1435 int linno;
1436
1437 c = pgetc();
1438 if (c != '(' && c != OPENBRACE && !is_name(c) && !is_special(c)) {
1439 USTPUTC('$', out);
1440 pungetc();
1441 } else if (c == '(') { /* $(command) or $((arith)) */
1442 if (pgetc() == '(') {
1443 PARSEARITH();
1444 } else {
1445 pungetc();
1446 PARSEBACKQNEW();
1447 }
1448 } else {
1449 USTPUTC(CTLVAR, out);
1450 typeloc = out - stackblock();
1451 USTPUTC(VSNORMAL, out);
1452 subtype = VSNORMAL;
1453 flags = 0;
1454 if (c == OPENBRACE) {
1455 c = pgetc();
1456 if (c == '#') {
1457 if ((c = pgetc()) == CLOSEBRACE)
1458 c = '#';
1459 else
1460 subtype = VSLENGTH;
1461 }
1462 else
1463 subtype = 0;
1464 }
1465 if (is_name(c)) {
1466 p = out;
1467 do {
1468 STPUTC(c, out);
1469 c = pgetc();
1470 } while (is_in_name(c));
1471 if (out - p == 6 && strncmp(p, "LINENO", 6) == 0) {
1472 /* Replace the variable name with the
1473 * current line number. */
1474 linno = plinno;
1475 if (funclinno != 0)
1476 linno -= funclinno - 1;
1477 snprintf(buf, sizeof(buf), "%d", linno);
1478 STADJUST(-6, out);
1479 for (i = 0; buf[i] != '\0'; i++)
1480 STPUTC(buf[i], out);
1481 flags |= VSLINENO;
1482 }
1483 } else if (is_digit(c)) {
1484 do {
1485 USTPUTC(c, out);
1486 c = pgetc();
1487 } while (is_digit(c));
1488 }
1489 else if (is_special(c)) {
1490 USTPUTC(c, out);
1491 c = pgetc();
1492 }
1493 else {
1494 badsub:
1495 cleanup_state_stack(stack);
1496 synerror("Bad substitution");
1497 }
1498
1499 STPUTC('=', out);
1500 if (subtype == 0) {
1501 switch (c) {
1502 case ':':
1503 flags |= VSNUL;
1504 c = pgetc();
1505 /*FALLTHROUGH*/
1506 default:
1507 p = strchr(types, c);
1508 if (p == NULL)
1509 goto badsub;
1510 subtype = p - types + VSNORMAL;
1511 break;
1512 case '%':
1513 case '#':
1514 {
1515 int cc = c;
1516 subtype = c == '#' ? VSTRIMLEFT :
1517 VSTRIMRIGHT;
1518 c = pgetc();
1519 if (c == cc)
1520 subtype++;
1521 else
1522 pungetc();
1523 break;
1524 }
1525 }
1526 } else {
1527 pungetc();
1528 }
1529 if (ISDBLQUOTE() || arinest)
1530 flags |= VSQUOTE;
1531 if (subtype >= VSTRIMLEFT && subtype <= VSTRIMRIGHTMAX)
1532 flags |= VSPATQ;
1533 *(stackblock() + typeloc) = subtype | flags;
1534 if (subtype != VSNORMAL) {
1535 TS_PUSH();
1536 varnest++;
1537 arinest = 0;
1538 if (subtype > VSASSIGN) { /* # ## % %% */
1539 syntax = BASESYNTAX;
1540 CLRDBLQUOTE();
1541 }
1542 }
1543 }
1544 goto parsesub_return;
1545 }
1546
1547
1548 /*
1549 * Called to parse command substitutions. Newstyle is set if the command
1550 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1551 * list of commands (passed by reference), and savelen is the number of
1552 * characters on the top of the stack which must be preserved.
1553 */
1554
1555 parsebackq: {
1556 struct nodelist **nlpp;
1557 int savepbq;
1558 union node *n;
1559 char *volatile str = NULL;
1560 struct jmploc jmploc;
1561 struct jmploc *volatile savehandler = NULL;
1562 int savelen;
1563 int saveprompt;
1564
1565 savepbq = parsebackquote;
1566 if (setjmp(jmploc.loc)) {
1567 if (str)
1568 ckfree(str);
1569 cleanup_state_stack(stack);
1570 parsebackquote = 0;
1571 handler = savehandler;
1572 longjmp(handler->loc, 1);
1573 }
1574 INTOFF;
1575 str = NULL;
1576 savelen = out - stackblock();
1577 if (savelen > 0) {
1578 str = ckmalloc(savelen);
1579 memcpy(str, stackblock(), savelen);
1580 }
1581 savehandler = handler;
1582 handler = &jmploc;
1583 INTON;
1584 if (oldstyle) {
1585 /* We must read until the closing backquote, giving special
1586 treatment to some slashes, and then push the string and
1587 reread it as input, interpreting it normally. */
1588 char *pout;
1589 int pc;
1590 int psavelen;
1591 char *pstr;
1592
1593 /*
1594 * Because the entire `...` is read here, we don't
1595 * need to bother the state stack. That will be used
1596 * (as appropriate) when the processed string is re-read.
1597 */
1598 STARTSTACKSTR(pout);
1599 for (;;) {
1600 if (needprompt) {
1601 setprompt(2);
1602 needprompt = 0;
1603 }
1604 switch (pc = pgetc()) {
1605 case '`':
1606 goto done;
1607
1608 case '\\':
1609 if ((pc = pgetc()) == '\n') {
1610 plinno++;
1611 if (doprompt)
1612 setprompt(2);
1613 else
1614 setprompt(0);
1615 /*
1616 * If eating a newline, avoid putting
1617 * the newline into the new character
1618 * stream (via the STPUTC after the
1619 * switch).
1620 */
1621 continue;
1622 }
1623 if (pc != '\\' && pc != '`' && pc != '$'
1624 && (!ISDBLQUOTE() || pc != '"'))
1625 STPUTC('\\', pout);
1626 break;
1627
1628 case '\n':
1629 plinno++;
1630 needprompt = doprompt;
1631 break;
1632
1633 case PEOF:
1634 startlinno = plinno;
1635 synerror("EOF in backquote substitution");
1636 break;
1637
1638 default:
1639 break;
1640 }
1641 STPUTC(pc, pout);
1642 }
1643 done:
1644 STPUTC('\0', pout);
1645 psavelen = pout - stackblock();
1646 if (psavelen > 0) {
1647 pstr = grabstackstr(pout);
1648 setinputstring(pstr, 1);
1649 }
1650 }
1651 nlpp = &bqlist;
1652 while (*nlpp)
1653 nlpp = &(*nlpp)->next;
1654 *nlpp = stalloc(sizeof(struct nodelist));
1655 (*nlpp)->next = NULL;
1656 parsebackquote = oldstyle;
1657
1658 if (oldstyle) {
1659 saveprompt = doprompt;
1660 doprompt = 0;
1661 } else
1662 saveprompt = 0;
1663
1664 n = list(0, oldstyle);
1665
1666 if (oldstyle)
1667 doprompt = saveprompt;
1668 else {
1669 if (readtoken() != TRP) {
1670 cleanup_state_stack(stack);
1671 synexpect(TRP);
1672 }
1673 }
1674
1675 (*nlpp)->n = n;
1676 if (oldstyle) {
1677 /*
1678 * Start reading from old file again, ignoring any pushed back
1679 * tokens left from the backquote parsing
1680 */
1681 popfile();
1682 tokpushback = 0;
1683 }
1684 while (stackblocksize() <= savelen)
1685 growstackblock();
1686 STARTSTACKSTR(out);
1687 if (str) {
1688 memcpy(out, str, savelen);
1689 STADJUST(savelen, out);
1690 INTOFF;
1691 ckfree(str);
1692 str = NULL;
1693 INTON;
1694 }
1695 parsebackquote = savepbq;
1696 handler = savehandler;
1697 if (arinest || ISDBLQUOTE())
1698 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1699 else
1700 USTPUTC(CTLBACKQ, out);
1701 if (oldstyle)
1702 goto parsebackq_oldreturn;
1703 else
1704 goto parsebackq_newreturn;
1705 }
1706
1707 /*
1708 * Parse an arithmetic expansion (indicate start of one and set state)
1709 */
1710 parsearith: {
1711
1712 if (syntax == ARISYNTAX) {
1713 /*
1714 * we collapse embedded arithmetic expansion to
1715 * parentheses, which should be equivalent
1716 */
1717 USTPUTC('(', out);
1718 USTPUTC('(', out);
1719 /*
1720 * Need 2 of them because there will (should be)
1721 * two closing ))'s to follow later.
1722 */
1723 parenlevel += 2;
1724 } else {
1725 TS_PUSH();
1726 syntax = ARISYNTAX;
1727 ++arinest;
1728 varnest = 0;
1729
1730 USTPUTC(CTLARI, out);
1731 if (ISDBLQUOTE())
1732 USTPUTC('"',out);
1733 else
1734 USTPUTC(' ',out);
1735 }
1736 goto parsearith_return;
1737 }
1738
1739 } /* end of readtoken */
1740
1741
1742
1743 #ifdef mkinit
1744 RESET {
1745 tokpushback = 0;
1746 checkkwd = 0;
1747 }
1748 #endif
1749
1750 /*
1751 * Returns true if the text contains nothing to expand (no dollar signs
1752 * or backquotes).
1753 */
1754
1755 STATIC int
1756 noexpand(char *text)
1757 {
1758 char *p;
1759 char c;
1760
1761 p = text;
1762 while ((c = *p++) != '\0') {
1763 if (c == CTLQUOTEMARK)
1764 continue;
1765 if (c == CTLESC)
1766 p++;
1767 else if (BASESYNTAX[(int)c] == CCTL)
1768 return 0;
1769 }
1770 return 1;
1771 }
1772
1773
1774 /*
1775 * Return true if the argument is a legal variable name (a letter or
1776 * underscore followed by zero or more letters, underscores, and digits).
1777 */
1778
1779 int
1780 goodname(char *name)
1781 {
1782 char *p;
1783
1784 p = name;
1785 if (! is_name(*p))
1786 return 0;
1787 while (*++p) {
1788 if (! is_in_name(*p))
1789 return 0;
1790 }
1791 return 1;
1792 }
1793
1794
1795 /*
1796 * Called when an unexpected token is read during the parse. The argument
1797 * is the token that is expected, or -1 if more than one type of token can
1798 * occur at this point.
1799 */
1800
1801 STATIC void
1802 synexpect(int token)
1803 {
1804 char msg[64];
1805
1806 if (token >= 0) {
1807 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1808 tokname[lasttoken], tokname[token]);
1809 } else {
1810 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1811 }
1812 synerror(msg);
1813 /* NOTREACHED */
1814 }
1815
1816
1817 STATIC void
1818 synerror(const char *msg)
1819 {
1820 if (commandname)
1821 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1822 else
1823 outfmt(&errout, "%s: ", getprogname());
1824 outfmt(&errout, "Syntax error: %s\n", msg);
1825 error(NULL);
1826 /* NOTREACHED */
1827 }
1828
1829 STATIC void
1830 setprompt(int which)
1831 {
1832 whichprompt = which;
1833
1834 #ifndef SMALL
1835 if (!el)
1836 #endif
1837 out2str(getprompt(NULL));
1838 }
1839
1840 /*
1841 * called by editline -- any expansions to the prompt
1842 * should be added here.
1843 */
1844 const char *
1845 getprompt(void *unused)
1846 {
1847 switch (whichprompt) {
1848 case 0:
1849 return "";
1850 case 1:
1851 return ps1val();
1852 case 2:
1853 return ps2val();
1854 default:
1855 return "<internal prompt error>";
1856 }
1857 }
1858