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