parser.c revision 1.94 1 /* $NetBSD: parser.c,v 1.94 2016/02/19 13:50:37 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.94 2016/02/19 13:50:37 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 = (union node *)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 = (union node *)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 = (union node *)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 = (union node *)stalloc(sizeof (struct npipe));
270 pipenode->type = NPIPE;
271 pipenode->npipe.backgnd = 0;
272 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
273 pipenode->npipe.cmdlist = lp;
274 lp->n = n1;
275 do {
276 prev = lp;
277 lp = (struct nodelist *)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 = (union node *)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 = (union node *)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 = (union node *)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 = (union node *)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 = (union node *)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 = (union node *)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] = {CTLVAR, VSNORMAL|VSQUOTE,
393 '@', '=', '\0'};
394 n2 = (union node *)stalloc(sizeof (struct narg));
395 n2->type = NARG;
396 n2->narg.text = argvars;
397 n2->narg.backquote = NULL;
398 n2->narg.next = NULL;
399 n1->nfor.args = n2;
400 /*
401 * Newline or semicolon here is optional (but note
402 * that the original Bourne shell only allowed NL).
403 */
404 if (lasttoken != TNL && lasttoken != TSEMI)
405 tokpushback++;
406 }
407 checkkwd = 2;
408 if ((t = readtoken()) == TDO)
409 t = TDONE;
410 else if (t == TBEGIN)
411 t = TEND;
412 else
413 synexpect(-1);
414 n1->nfor.body = list(0, 0);
415 if (readtoken() != t)
416 synexpect(t);
417 checkkwd = 1;
418 break;
419 case TCASE:
420 n1 = (union node *)stalloc(sizeof (struct ncase));
421 n1->type = NCASE;
422 if (readtoken() != TWORD)
423 synexpect(TWORD);
424 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
425 n2->type = NARG;
426 n2->narg.text = wordtext;
427 n2->narg.backquote = backquotelist;
428 n2->narg.next = NULL;
429 while (readtoken() == TNL);
430 if (lasttoken != TWORD || ! equal(wordtext, "in"))
431 synerror("expecting \"in\"");
432 cpp = &n1->ncase.cases;
433 noalias = 1;
434 checkkwd = 2, readtoken();
435 /*
436 * Both ksh and bash accept 'case x in esac'
437 * so configure scripts started taking advantage of this.
438 * The page: http://pubs.opengroup.org/onlinepubs/\
439 * 009695399/utilities/xcu_chap02.html contradicts itself,
440 * as to if this is legal; the "Case Conditional Format"
441 * paragraph shows one case is required, but the "Grammar"
442 * section shows a grammar that explicitly allows the no
443 * case option.
444 */
445 while (lasttoken != TESAC) {
446 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
447 if (lasttoken == TLP)
448 readtoken();
449 cp->type = NCLIST;
450 app = &cp->nclist.pattern;
451 for (;;) {
452 *app = ap = (union node *)stalloc(sizeof (struct narg));
453 ap->type = NARG;
454 ap->narg.text = wordtext;
455 ap->narg.backquote = backquotelist;
456 if (checkkwd = 2, readtoken() != TPIPE)
457 break;
458 app = &ap->narg.next;
459 readtoken();
460 }
461 ap->narg.next = NULL;
462 noalias = 0;
463 if (lasttoken != TRP) {
464 synexpect(TRP);
465 }
466 cp->nclist.body = list(0, 0);
467
468 checkkwd = 2;
469 if ((t = readtoken()) != TESAC) {
470 if (t != TENDCASE) {
471 noalias = 0;
472 synexpect(TENDCASE);
473 } else {
474 noalias = 1;
475 checkkwd = 2;
476 readtoken();
477 }
478 }
479 cpp = &cp->nclist.next;
480 }
481 noalias = 0;
482 *cpp = NULL;
483 checkkwd = 1;
484 break;
485 case TLP:
486 n1 = (union node *)stalloc(sizeof (struct nredir));
487 n1->type = NSUBSHELL;
488 n1->nredir.n = list(0, 0);
489 n1->nredir.redirect = NULL;
490 if (readtoken() != TRP)
491 synexpect(TRP);
492 checkkwd = 1;
493 break;
494 case TBEGIN:
495 n1 = list(0, 0);
496 if (readtoken() != TEND)
497 synexpect(TEND);
498 checkkwd = 1;
499 break;
500 /* Handle an empty command like other simple commands. */
501 case TSEMI:
502 /*
503 * An empty command before a ; doesn't make much sense, and
504 * should certainly be disallowed in the case of `if ;'.
505 */
506 if (!redir)
507 synexpect(-1);
508 case TAND:
509 case TOR:
510 case TNL:
511 case TEOF:
512 case TWORD:
513 case TRP:
514 tokpushback++;
515 n1 = simplecmd(rpp, redir);
516 goto checkneg;
517 default:
518 synexpect(-1);
519 /* NOTREACHED */
520 }
521
522 /* Now check for redirection which may follow command */
523 while (readtoken() == TREDIR) {
524 *rpp = n2 = redirnode;
525 rpp = &n2->nfile.next;
526 parsefname();
527 }
528 tokpushback++;
529 *rpp = NULL;
530 if (redir) {
531 if (n1->type != NSUBSHELL) {
532 n2 = (union node *)stalloc(sizeof (struct nredir));
533 n2->type = NREDIR;
534 n2->nredir.n = n1;
535 n1 = n2;
536 }
537 n1->nredir.redirect = redir;
538 }
539
540 checkneg:
541 if (negate) {
542 TRACE(("negate command\n"));
543 n2 = (union node *)stalloc(sizeof (struct nnot));
544 n2->type = NNOT;
545 n2->nnot.com = n1;
546 return n2;
547 }
548 else
549 return n1;
550 }
551
552
553 STATIC union node *
554 simplecmd(union node **rpp, union node *redir)
555 {
556 union node *args, **app;
557 union node **orig_rpp = rpp;
558 union node *n = NULL, *n2;
559 int negate = 0;
560
561 /* If we don't have any redirections already, then we must reset */
562 /* rpp to be the address of the local redir variable. */
563 if (redir == 0)
564 rpp = &redir;
565
566 args = NULL;
567 app = &args;
568 /*
569 * We save the incoming value, because we need this for shell
570 * functions. There can not be a redirect or an argument between
571 * the function name and the open parenthesis.
572 */
573 orig_rpp = rpp;
574
575 while (readtoken() == TNOT) {
576 TRACE(("simplcmd: TNOT recognized\n"));
577 negate = !negate;
578 }
579 tokpushback++;
580
581 for (;;) {
582 if (readtoken() == TWORD) {
583 n = (union node *)stalloc(sizeof (struct narg));
584 n->type = NARG;
585 n->narg.text = wordtext;
586 n->narg.backquote = backquotelist;
587 *app = n;
588 app = &n->narg.next;
589 } else if (lasttoken == TREDIR) {
590 *rpp = n = redirnode;
591 rpp = &n->nfile.next;
592 parsefname(); /* read name of redirection file */
593 } else if (lasttoken == TLP && app == &args->narg.next
594 && rpp == orig_rpp) {
595 /* We have a function */
596 if (readtoken() != TRP)
597 synexpect(TRP);
598 funclinno = plinno;
599 rmescapes(n->narg.text);
600 if (!goodname(n->narg.text))
601 synerror("Bad function name");
602 n->type = NDEFUN;
603 n->narg.next = command();
604 funclinno = 0;
605 goto checkneg;
606 } else {
607 tokpushback++;
608 break;
609 }
610 }
611 *app = NULL;
612 *rpp = NULL;
613 n = (union node *)stalloc(sizeof (struct ncmd));
614 n->type = NCMD;
615 n->ncmd.backgnd = 0;
616 n->ncmd.args = args;
617 n->ncmd.redirect = redir;
618
619 checkneg:
620 if (negate) {
621 TRACE(("negate simplecmd\n"));
622 n2 = (union node *)stalloc(sizeof (struct nnot));
623 n2->type = NNOT;
624 n2->nnot.com = n;
625 return n2;
626 }
627 else
628 return n;
629 }
630
631 STATIC union node *
632 makename(void)
633 {
634 union node *n;
635
636 n = (union node *)stalloc(sizeof (struct narg));
637 n->type = NARG;
638 n->narg.next = NULL;
639 n->narg.text = wordtext;
640 n->narg.backquote = backquotelist;
641 return n;
642 }
643
644 void fixredir(union node *n, const char *text, int err)
645 {
646 TRACE(("Fix redir %s %d\n", text, err));
647 if (!err)
648 n->ndup.vname = NULL;
649
650 if (is_number(text))
651 n->ndup.dupfd = number(text);
652 else if (text[0] == '-' && text[1] == '\0')
653 n->ndup.dupfd = -1;
654 else {
655
656 if (err)
657 synerror("Bad fd number");
658 else
659 n->ndup.vname = makename();
660 }
661 }
662
663
664 STATIC void
665 parsefname(void)
666 {
667 union node *n = redirnode;
668
669 if (readtoken() != TWORD)
670 synexpect(-1);
671 if (n->type == NHERE) {
672 struct heredoc *here = heredoc;
673 struct heredoc *p;
674 int i;
675
676 if (quoteflag == 0)
677 n->type = NXHERE;
678 TRACE(("Here document %d\n", n->type));
679 if (here->striptabs) {
680 while (*wordtext == '\t')
681 wordtext++;
682 }
683 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
684 synerror("Illegal eof marker for << redirection");
685 rmescapes(wordtext);
686 here->eofmark = wordtext;
687 here->next = NULL;
688 if (heredoclist == NULL)
689 heredoclist = here;
690 else {
691 for (p = heredoclist ; p->next ; p = p->next)
692 continue;
693 p->next = here;
694 }
695 } else if (n->type == NTOFD || n->type == NFROMFD) {
696 fixredir(n, wordtext, 0);
697 } else {
698 n->nfile.fname = makename();
699 }
700 }
701
702
703 /*
704 * Input any here documents.
705 */
706
707 STATIC void
708 parseheredoc(void)
709 {
710 struct heredoc *here;
711 union node *n;
712
713 while (heredoclist) {
714 here = heredoclist;
715 heredoclist = here->next;
716 if (needprompt) {
717 setprompt(2);
718 needprompt = 0;
719 }
720 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
721 here->eofmark, here->striptabs);
722 n = (union node *)stalloc(sizeof (struct narg));
723 n->narg.type = NARG;
724 n->narg.next = NULL;
725 n->narg.text = wordtext;
726 n->narg.backquote = backquotelist;
727 here->here->nhere.doc = n;
728 }
729 }
730
731 STATIC int
732 peektoken(void)
733 {
734 int t;
735
736 t = readtoken();
737 tokpushback++;
738 return (t);
739 }
740
741 STATIC int
742 readtoken(void)
743 {
744 int t;
745 int savecheckkwd = checkkwd;
746 #ifdef DEBUG
747 int alreadyseen = tokpushback;
748 #endif
749 struct alias *ap;
750
751 top:
752 t = xxreadtoken();
753
754 if (checkkwd) {
755 /*
756 * eat newlines
757 */
758 if (checkkwd == 2) {
759 checkkwd = 0;
760 while (t == TNL) {
761 parseheredoc();
762 t = xxreadtoken();
763 }
764 } else
765 checkkwd = 0;
766 /*
767 * check for keywords and aliases
768 */
769 if (t == TWORD && !quoteflag)
770 {
771 const char *const *pp;
772
773 for (pp = parsekwd; *pp; pp++) {
774 if (**pp == *wordtext && equal(*pp, wordtext))
775 {
776 lasttoken = t = pp -
777 parsekwd + KWDOFFSET;
778 TRACE(("keyword %s recognized\n", tokname[t]));
779 goto out;
780 }
781 }
782 if (!noalias &&
783 (ap = lookupalias(wordtext, 1)) != NULL) {
784 pushstring(ap->val, strlen(ap->val), ap);
785 checkkwd = savecheckkwd;
786 goto top;
787 }
788 }
789 out:
790 checkkwd = (t == TNOT) ? savecheckkwd : 0;
791 }
792 TRACE(("%stoken %s %s\n", alreadyseen ? "reread " : "", tokname[t], t == TWORD ? wordtext : ""));
793 return (t);
794 }
795
796
797 /*
798 * Read the next input token.
799 * If the token is a word, we set backquotelist to the list of cmds in
800 * backquotes. We set quoteflag to true if any part of the word was
801 * quoted.
802 * If the token is TREDIR, then we set redirnode to a structure containing
803 * the redirection.
804 * In all cases, the variable startlinno is set to the number of the line
805 * on which the token starts.
806 *
807 * [Change comment: here documents and internal procedures]
808 * [Readtoken shouldn't have any arguments. Perhaps we should make the
809 * word parsing code into a separate routine. In this case, readtoken
810 * doesn't need to have any internal procedures, but parseword does.
811 * We could also make parseoperator in essence the main routine, and
812 * have parseword (readtoken1?) handle both words and redirection.]
813 */
814
815 #define RETURN(token) return lasttoken = token
816
817 STATIC int
818 xxreadtoken(void)
819 {
820 int c;
821
822 if (tokpushback) {
823 tokpushback = 0;
824 return lasttoken;
825 }
826 if (needprompt) {
827 setprompt(2);
828 needprompt = 0;
829 }
830 startlinno = plinno;
831 for (;;) { /* until token or start of word found */
832 c = pgetc_macro();
833 switch (c) {
834 case ' ': case '\t':
835 continue;
836 case '#':
837 while ((c = pgetc()) != '\n' && c != PEOF)
838 continue;
839 pungetc();
840 continue;
841 case '\\':
842 switch (pgetc()) {
843 case '\n':
844 startlinno = ++plinno;
845 if (doprompt)
846 setprompt(2);
847 else
848 setprompt(0);
849 continue;
850 case PEOF:
851 RETURN(TEOF);
852 default:
853 pungetc();
854 break;
855 }
856 goto breakloop;
857 case '\n':
858 plinno++;
859 needprompt = doprompt;
860 RETURN(TNL);
861 case PEOF:
862 RETURN(TEOF);
863 case '&':
864 if (pgetc() == '&')
865 RETURN(TAND);
866 pungetc();
867 RETURN(TBACKGND);
868 case '|':
869 if (pgetc() == '|')
870 RETURN(TOR);
871 pungetc();
872 RETURN(TPIPE);
873 case ';':
874 if (pgetc() == ';')
875 RETURN(TENDCASE);
876 pungetc();
877 RETURN(TSEMI);
878 case '(':
879 RETURN(TLP);
880 case ')':
881 RETURN(TRP);
882 default:
883 goto breakloop;
884 }
885 }
886 breakloop:
887 return readtoken1(c, BASESYNTAX, NULL, 0);
888 #undef RETURN
889 }
890
891
892
893 /*
894 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
895 * is not NULL, read a here document. In the latter case, eofmark is the
896 * word which marks the end of the document and striptabs is true if
897 * leading tabs should be stripped from the document. The argument firstc
898 * is the first character of the input token or document.
899 *
900 * Because C does not have internal subroutines, I have simulated them
901 * using goto's to implement the subroutine linkage. The following macros
902 * will run code that appears at the end of readtoken1.
903 */
904
905 #define CHECKEND() {goto checkend; checkend_return:;}
906 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
907 #define PARSESUB() {goto parsesub; parsesub_return:;}
908 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
909 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
910 #define PARSEARITH() {goto parsearith; parsearith_return:;}
911
912 /*
913 * Keep track of nested doublequotes in dblquote and doublequotep.
914 * We use dblquote for the first 32 levels, and we expand to a malloc'ed
915 * region for levels above that. Usually we never need to malloc.
916 * This code assumes that an int is 32 bits. We don't use uint32_t,
917 * because the rest of the code does not.
918 */
919 #define ISDBLQUOTE() ((varnest < 32) ? (dblquote & (1 << varnest)) : \
920 (dblquotep[(varnest / 32) - 1] & (1 << (varnest % 32))))
921
922 #define SETDBLQUOTE() \
923 if (varnest < 32) \
924 dblquote |= (1 << varnest); \
925 else \
926 dblquotep[(varnest / 32) - 1] |= (1 << (varnest % 32))
927
928 #define CLRDBLQUOTE() \
929 if (varnest < 32) \
930 dblquote &= ~(1 << varnest); \
931 else \
932 dblquotep[(varnest / 32) - 1] &= ~(1 << (varnest % 32))
933
934 #define INCREASENEST() \
935 do { \
936 if (varnest++ >= maxnest) { \
937 dblquotep = ckrealloc(dblquotep, maxnest / 8); \
938 dblquotep[(maxnest / 32) - 1] = 0; \
939 maxnest += 32; \
940 } \
941 } while (/*CONSTCOND*/0)
942
943 STATIC int
944 readtoken1(int firstc, char const *syn, char *eofmark, int striptabs)
945 {
946 char const * volatile syntax = syn;
947 int c = firstc;
948 char * volatile out;
949 int len;
950 char line[EOFMARKLEN + 1];
951 struct nodelist *bqlist;
952 volatile int quotef;
953 int * volatile dblquotep = NULL;
954 volatile size_t maxnest = 32;
955 volatile int dblquote;
956 volatile size_t varnest; /* levels of variables expansion */
957 volatile int arinest; /* levels of arithmetic expansion */
958 volatile int parenlevel; /* levels of parens in arithmetic */
959 volatile int oldstyle;
960 char const * volatile prevsyntax; /* syntax before arithmetic */
961 #ifdef __GNUC__
962 prevsyntax = NULL; /* XXX gcc4 */
963 #endif
964
965 startlinno = plinno;
966 dblquote = 0;
967 varnest = 0;
968 if (syntax == DQSYNTAX) {
969 SETDBLQUOTE();
970 }
971 quotef = 0;
972 bqlist = NULL;
973 arinest = 0;
974 parenlevel = 0;
975
976 STARTSTACKSTR(out);
977 loop: { /* for each line, until end of word */
978 #if ATTY
979 if (c == '\034' && doprompt
980 && attyset() && ! equal(termval(), "emacs")) {
981 attyline();
982 if (syntax == BASESYNTAX)
983 return readtoken();
984 c = pgetc();
985 goto loop;
986 }
987 #endif
988 CHECKEND(); /* set c to PEOF if at end of here document */
989 for (;;) { /* until end of line or end of word */
990 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
991 switch(syntax[c]) {
992 case CNL: /* '\n' */
993 if (syntax == BASESYNTAX)
994 goto endword; /* exit outer loop */
995 USTPUTC(c, out);
996 plinno++;
997 if (doprompt)
998 setprompt(2);
999 else
1000 setprompt(0);
1001 c = pgetc();
1002 goto loop; /* continue outer loop */
1003 case CWORD:
1004 USTPUTC(c, out);
1005 break;
1006 case CCTL:
1007 if (eofmark == NULL || ISDBLQUOTE())
1008 USTPUTC(CTLESC, out);
1009 USTPUTC(c, out);
1010 break;
1011 case CBACK: /* backslash */
1012 c = pgetc();
1013 if (c == PEOF) {
1014 USTPUTC('\\', out);
1015 pungetc();
1016 break;
1017 }
1018 if (c == '\n') {
1019 plinno++;
1020 if (doprompt)
1021 setprompt(2);
1022 else
1023 setprompt(0);
1024 break;
1025 }
1026 quotef = 1;
1027 if (ISDBLQUOTE() && c != '\\' &&
1028 c != '`' && c != '$' &&
1029 (c != '"' || eofmark != NULL))
1030 USTPUTC('\\', out);
1031 if (SQSYNTAX[c] == CCTL)
1032 USTPUTC(CTLESC, out);
1033 else if (eofmark == NULL) {
1034 USTPUTC(CTLQUOTEMARK, out);
1035 USTPUTC(c, out);
1036 if (varnest != 0)
1037 USTPUTC(CTLQUOTEEND, out);
1038 break;
1039 }
1040 USTPUTC(c, out);
1041 break;
1042 case CSQUOTE:
1043 if (syntax != SQSYNTAX) {
1044 if (eofmark == NULL)
1045 USTPUTC(CTLQUOTEMARK, out);
1046 quotef = 1;
1047 syntax = SQSYNTAX;
1048 break;
1049 }
1050 if (eofmark != NULL && arinest == 0 &&
1051 varnest == 0) {
1052 /* Ignore inside quoted here document */
1053 USTPUTC(c, out);
1054 break;
1055 }
1056 /* End of single quotes... */
1057 if (arinest)
1058 syntax = ARISYNTAX;
1059 else {
1060 syntax = BASESYNTAX;
1061 if (varnest != 0)
1062 USTPUTC(CTLQUOTEEND, out);
1063 }
1064 break;
1065 case CDQUOTE:
1066 if (eofmark != NULL && arinest == 0 &&
1067 varnest == 0) {
1068 /* Ignore inside here document */
1069 USTPUTC(c, out);
1070 break;
1071 }
1072 quotef = 1;
1073 if (arinest) {
1074 if (ISDBLQUOTE()) {
1075 syntax = ARISYNTAX;
1076 CLRDBLQUOTE();
1077 } else {
1078 syntax = DQSYNTAX;
1079 SETDBLQUOTE();
1080 USTPUTC(CTLQUOTEMARK, out);
1081 }
1082 break;
1083 }
1084 if (eofmark != NULL)
1085 break;
1086 if (ISDBLQUOTE()) {
1087 if (varnest != 0)
1088 USTPUTC(CTLQUOTEEND, out);
1089 syntax = BASESYNTAX;
1090 CLRDBLQUOTE();
1091 } else {
1092 syntax = DQSYNTAX;
1093 SETDBLQUOTE();
1094 USTPUTC(CTLQUOTEMARK, out);
1095 }
1096 break;
1097 case CVAR: /* '$' */
1098 PARSESUB(); /* parse substitution */
1099 break;
1100 case CENDVAR: /* CLOSEBRACE */
1101 if (varnest > 0 && !ISDBLQUOTE()) {
1102 varnest--;
1103 USTPUTC(CTLENDVAR, out);
1104 } else {
1105 USTPUTC(c, out);
1106 }
1107 break;
1108 case CLP: /* '(' in arithmetic */
1109 parenlevel++;
1110 USTPUTC(c, out);
1111 break;
1112 case CRP: /* ')' in arithmetic */
1113 if (parenlevel > 0) {
1114 USTPUTC(c, out);
1115 --parenlevel;
1116 } else {
1117 if (pgetc() == ')') {
1118 if (varnest > 0) /* always */
1119 varnest--;
1120 if (--arinest == 0) {
1121 USTPUTC(CTLENDARI, out);
1122 syntax = prevsyntax;
1123 } else
1124 USTPUTC(')', out);
1125 } else {
1126 /*
1127 * unbalanced parens
1128 * (don't 2nd guess - no error)
1129 */
1130 pungetc();
1131 USTPUTC(')', out);
1132 }
1133 }
1134 break;
1135 case CBQUOTE: /* '`' */
1136 PARSEBACKQOLD();
1137 break;
1138 case CEOF:
1139 goto endword; /* exit outer loop */
1140 default:
1141 if (varnest == 0 && !ISDBLQUOTE())
1142 goto endword; /* exit outer loop */
1143 USTPUTC(c, out);
1144 }
1145 c = pgetc_macro();
1146 }
1147 }
1148 endword:
1149 if (syntax == ARISYNTAX)
1150 synerror("Missing '))'");
1151 if (syntax != BASESYNTAX && /* ! parsebackquote && */ eofmark == NULL)
1152 synerror("Unterminated quoted string");
1153 if (varnest != 0) {
1154 startlinno = plinno;
1155 /* { */
1156 synerror("Missing '}'");
1157 }
1158 USTPUTC('\0', out);
1159 len = out - stackblock();
1160 out = stackblock();
1161 if (eofmark == NULL) {
1162 if ((c == '>' || c == '<')
1163 && quotef == 0
1164 && (*out == '\0' || is_number(out))) {
1165 PARSEREDIR();
1166 return lasttoken = TREDIR;
1167 } else {
1168 pungetc();
1169 }
1170 }
1171 quoteflag = quotef;
1172 backquotelist = bqlist;
1173 grabstackblock(len);
1174 wordtext = out;
1175 if (dblquotep != NULL)
1176 ckfree(dblquotep);
1177 return lasttoken = TWORD;
1178 /* end of readtoken routine */
1179
1180
1181
1182 /*
1183 * Check to see whether we are at the end of the here document. When this
1184 * is called, c is set to the first character of the next input line. If
1185 * we are at the end of the here document, this routine sets the c to PEOF.
1186 */
1187
1188 checkend: {
1189 if (eofmark) {
1190 if (striptabs) {
1191 while (c == '\t')
1192 c = pgetc();
1193 }
1194 if (c == *eofmark) {
1195 if (pfgets(line, sizeof line) != NULL) {
1196 char *p, *q;
1197
1198 p = line;
1199 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++)
1200 continue;
1201 if ((*p == '\0' || *p == '\n') && *q == '\0') {
1202 c = PEOF;
1203 plinno++;
1204 needprompt = doprompt;
1205 } else {
1206 pushstring(line, strlen(line), NULL);
1207 }
1208 }
1209 }
1210 }
1211 goto checkend_return;
1212 }
1213
1214
1215 /*
1216 * Parse a redirection operator. The variable "out" points to a string
1217 * specifying the fd to be redirected. The variable "c" contains the
1218 * first character of the redirection operator.
1219 */
1220
1221 parseredir: {
1222 char fd[64];
1223 union node *np;
1224 strlcpy(fd, out, sizeof(fd));
1225
1226 np = (union node *)stalloc(sizeof (struct nfile));
1227 if (c == '>') {
1228 np->nfile.fd = 1;
1229 c = pgetc();
1230 if (c == '>')
1231 np->type = NAPPEND;
1232 else if (c == '|')
1233 np->type = NCLOBBER;
1234 else if (c == '&')
1235 np->type = NTOFD;
1236 else {
1237 np->type = NTO;
1238 pungetc();
1239 }
1240 } else { /* c == '<' */
1241 np->nfile.fd = 0;
1242 switch (c = pgetc()) {
1243 case '<':
1244 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1245 np = (union node *)stalloc(sizeof (struct nhere));
1246 np->nfile.fd = 0;
1247 }
1248 np->type = NHERE;
1249 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1250 heredoc->here = np;
1251 if ((c = pgetc()) == '-') {
1252 heredoc->striptabs = 1;
1253 } else {
1254 heredoc->striptabs = 0;
1255 pungetc();
1256 }
1257 break;
1258
1259 case '&':
1260 np->type = NFROMFD;
1261 break;
1262
1263 case '>':
1264 np->type = NFROMTO;
1265 break;
1266
1267 default:
1268 np->type = NFROM;
1269 pungetc();
1270 break;
1271 }
1272 }
1273 if (*fd != '\0')
1274 np->nfile.fd = number(fd);
1275 redirnode = np;
1276 goto parseredir_return;
1277 }
1278
1279
1280 /*
1281 * Parse a substitution. At this point, we have read the dollar sign
1282 * and nothing else.
1283 */
1284
1285 parsesub: {
1286 char buf[10];
1287 int subtype;
1288 int typeloc;
1289 int flags;
1290 char *p;
1291 static const char types[] = "}-+?=";
1292 int i;
1293 int linno;
1294
1295 c = pgetc();
1296 if (c != '(' && c != OPENBRACE && !is_name(c) && !is_special(c)) {
1297 USTPUTC('$', out);
1298 pungetc();
1299 } else if (c == '(') { /* $(command) or $((arith)) */
1300 if (pgetc() == '(') {
1301 PARSEARITH();
1302 } else {
1303 pungetc();
1304 PARSEBACKQNEW();
1305 }
1306 } else {
1307 USTPUTC(CTLVAR, out);
1308 typeloc = out - stackblock();
1309 USTPUTC(VSNORMAL, out);
1310 subtype = VSNORMAL;
1311 flags = 0;
1312 if (c == OPENBRACE) {
1313 c = pgetc();
1314 if (c == '#') {
1315 if ((c = pgetc()) == CLOSEBRACE)
1316 c = '#';
1317 else
1318 subtype = VSLENGTH;
1319 }
1320 else
1321 subtype = 0;
1322 }
1323 if (is_name(c)) {
1324 p = out;
1325 do {
1326 STPUTC(c, out);
1327 c = pgetc();
1328 } while (is_in_name(c));
1329 if (out - p == 6 && strncmp(p, "LINENO", 6) == 0) {
1330 /* Replace the variable name with the
1331 * current line number. */
1332 linno = plinno;
1333 if (funclinno != 0)
1334 linno -= funclinno - 1;
1335 snprintf(buf, sizeof(buf), "%d", linno);
1336 STADJUST(-6, out);
1337 for (i = 0; buf[i] != '\0'; i++)
1338 STPUTC(buf[i], out);
1339 flags |= VSLINENO;
1340 }
1341 } else if (is_digit(c)) {
1342 do {
1343 USTPUTC(c, out);
1344 c = pgetc();
1345 } while (is_digit(c));
1346 }
1347 else if (is_special(c)) {
1348 USTPUTC(c, out);
1349 c = pgetc();
1350 }
1351 else
1352 badsub: synerror("Bad substitution");
1353
1354 STPUTC('=', out);
1355 if (subtype == 0) {
1356 switch (c) {
1357 case ':':
1358 flags |= VSNUL;
1359 c = pgetc();
1360 /*FALLTHROUGH*/
1361 default:
1362 p = strchr(types, c);
1363 if (p == NULL)
1364 goto badsub;
1365 subtype = p - types + VSNORMAL;
1366 break;
1367 case '%':
1368 case '#':
1369 {
1370 int cc = c;
1371 subtype = c == '#' ? VSTRIMLEFT :
1372 VSTRIMRIGHT;
1373 c = pgetc();
1374 if (c == cc)
1375 subtype++;
1376 else
1377 pungetc();
1378 break;
1379 }
1380 }
1381 } else {
1382 pungetc();
1383 }
1384 if (ISDBLQUOTE() || arinest)
1385 flags |= VSQUOTE;
1386 *(stackblock() + typeloc) = subtype | flags;
1387 if (subtype != VSNORMAL)
1388 INCREASENEST();
1389 }
1390 goto parsesub_return;
1391 }
1392
1393
1394 /*
1395 * Called to parse command substitutions. Newstyle is set if the command
1396 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1397 * list of commands (passed by reference), and savelen is the number of
1398 * characters on the top of the stack which must be preserved.
1399 */
1400
1401 parsebackq: {
1402 struct nodelist **nlpp;
1403 int savepbq;
1404 union node *n;
1405 char *volatile str = NULL;
1406 struct jmploc jmploc;
1407 struct jmploc *volatile savehandler = NULL;
1408 int savelen;
1409 int saveprompt;
1410
1411 savepbq = parsebackquote;
1412 if (setjmp(jmploc.loc)) {
1413 if (str)
1414 ckfree(str);
1415 parsebackquote = 0;
1416 handler = savehandler;
1417 longjmp(handler->loc, 1);
1418 }
1419 INTOFF;
1420 str = NULL;
1421 savelen = out - stackblock();
1422 if (savelen > 0) {
1423 str = ckmalloc(savelen);
1424 memcpy(str, stackblock(), savelen);
1425 }
1426 savehandler = handler;
1427 handler = &jmploc;
1428 INTON;
1429 if (oldstyle) {
1430 /* We must read until the closing backquote, giving special
1431 treatment to some slashes, and then push the string and
1432 reread it as input, interpreting it normally. */
1433 char *pout;
1434 int pc;
1435 int psavelen;
1436 char *pstr;
1437
1438
1439 STARTSTACKSTR(pout);
1440 for (;;) {
1441 if (needprompt) {
1442 setprompt(2);
1443 needprompt = 0;
1444 }
1445 switch (pc = pgetc()) {
1446 case '`':
1447 goto done;
1448
1449 case '\\':
1450 if ((pc = pgetc()) == '\n') {
1451 plinno++;
1452 if (doprompt)
1453 setprompt(2);
1454 else
1455 setprompt(0);
1456 /*
1457 * If eating a newline, avoid putting
1458 * the newline into the new character
1459 * stream (via the STPUTC after the
1460 * switch).
1461 */
1462 continue;
1463 }
1464 if (pc != '\\' && pc != '`' && pc != '$'
1465 && (!ISDBLQUOTE() || pc != '"'))
1466 STPUTC('\\', pout);
1467 break;
1468
1469 case '\n':
1470 plinno++;
1471 needprompt = doprompt;
1472 break;
1473
1474 case PEOF:
1475 startlinno = plinno;
1476 synerror("EOF in backquote substitution");
1477 break;
1478
1479 default:
1480 break;
1481 }
1482 STPUTC(pc, pout);
1483 }
1484 done:
1485 STPUTC('\0', pout);
1486 psavelen = pout - stackblock();
1487 if (psavelen > 0) {
1488 pstr = grabstackstr(pout);
1489 setinputstring(pstr, 1);
1490 }
1491 }
1492 nlpp = &bqlist;
1493 while (*nlpp)
1494 nlpp = &(*nlpp)->next;
1495 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1496 (*nlpp)->next = NULL;
1497 parsebackquote = oldstyle;
1498
1499 if (oldstyle) {
1500 saveprompt = doprompt;
1501 doprompt = 0;
1502 } else
1503 saveprompt = 0;
1504
1505 n = list(0, oldstyle);
1506
1507 if (oldstyle)
1508 doprompt = saveprompt;
1509 else {
1510 if (readtoken() != TRP)
1511 synexpect(TRP);
1512 }
1513
1514 (*nlpp)->n = n;
1515 if (oldstyle) {
1516 /*
1517 * Start reading from old file again, ignoring any pushed back
1518 * tokens left from the backquote parsing
1519 */
1520 popfile();
1521 tokpushback = 0;
1522 }
1523 while (stackblocksize() <= savelen)
1524 growstackblock();
1525 STARTSTACKSTR(out);
1526 if (str) {
1527 memcpy(out, str, savelen);
1528 STADJUST(savelen, out);
1529 INTOFF;
1530 ckfree(str);
1531 str = NULL;
1532 INTON;
1533 }
1534 parsebackquote = savepbq;
1535 handler = savehandler;
1536 if (arinest || ISDBLQUOTE())
1537 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1538 else
1539 USTPUTC(CTLBACKQ, out);
1540 if (oldstyle)
1541 goto parsebackq_oldreturn;
1542 else
1543 goto parsebackq_newreturn;
1544 }
1545
1546 /*
1547 * Parse an arithmetic expansion (indicate start of one and set state)
1548 */
1549 parsearith: {
1550
1551 if (++arinest == 1) {
1552 prevsyntax = syntax;
1553 syntax = ARISYNTAX;
1554 USTPUTC(CTLARI, out);
1555 if (ISDBLQUOTE())
1556 USTPUTC('"',out);
1557 else
1558 USTPUTC(' ',out);
1559 } else {
1560 /*
1561 * we collapse embedded arithmetic expansion to
1562 * parenthesis, which should be equivalent
1563 */
1564 USTPUTC('(', out);
1565 }
1566 INCREASENEST();
1567 goto parsearith_return;
1568 }
1569
1570 } /* end of readtoken */
1571
1572
1573
1574 #ifdef mkinit
1575 RESET {
1576 tokpushback = 0;
1577 checkkwd = 0;
1578 }
1579 #endif
1580
1581 /*
1582 * Returns true if the text contains nothing to expand (no dollar signs
1583 * or backquotes).
1584 */
1585
1586 STATIC int
1587 noexpand(char *text)
1588 {
1589 char *p;
1590 char c;
1591
1592 p = text;
1593 while ((c = *p++) != '\0') {
1594 if (c == CTLQUOTEMARK)
1595 continue;
1596 if (c == CTLESC)
1597 p++;
1598 else if (BASESYNTAX[(int)c] == CCTL)
1599 return 0;
1600 }
1601 return 1;
1602 }
1603
1604
1605 /*
1606 * Return true if the argument is a legal variable name (a letter or
1607 * underscore followed by zero or more letters, underscores, and digits).
1608 */
1609
1610 int
1611 goodname(char *name)
1612 {
1613 char *p;
1614
1615 p = name;
1616 if (! is_name(*p))
1617 return 0;
1618 while (*++p) {
1619 if (! is_in_name(*p))
1620 return 0;
1621 }
1622 return 1;
1623 }
1624
1625
1626 /*
1627 * Called when an unexpected token is read during the parse. The argument
1628 * is the token that is expected, or -1 if more than one type of token can
1629 * occur at this point.
1630 */
1631
1632 STATIC void
1633 synexpect(int token)
1634 {
1635 char msg[64];
1636
1637 if (token >= 0) {
1638 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1639 tokname[lasttoken], tokname[token]);
1640 } else {
1641 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1642 }
1643 synerror(msg);
1644 /* NOTREACHED */
1645 }
1646
1647
1648 STATIC void
1649 synerror(const char *msg)
1650 {
1651 if (commandname)
1652 outfmt(&errout, "%s: %d: ", commandname, startlinno);
1653 else
1654 outfmt(&errout, "%s: ", getprogname());
1655 outfmt(&errout, "Syntax error: %s\n", msg);
1656 error(NULL);
1657 /* NOTREACHED */
1658 }
1659
1660 STATIC void
1661 setprompt(int which)
1662 {
1663 whichprompt = which;
1664
1665 #ifndef SMALL
1666 if (!el)
1667 #endif
1668 out2str(getprompt(NULL));
1669 }
1670
1671 /*
1672 * called by editline -- any expansions to the prompt
1673 * should be added here.
1674 */
1675 const char *
1676 getprompt(void *unused)
1677 {
1678 switch (whichprompt) {
1679 case 0:
1680 return "";
1681 case 1:
1682 return ps1val();
1683 case 2:
1684 return ps2val();
1685 default:
1686 return "<internal prompt error>";
1687 }
1688 }
1689