source: trunk/src/kash/parser.c@ 3437

Last change on this file since 3437 was 3437, checked in by bird, 5 years ago

kash: refactoring evalcommand - complicated, part II.

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