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