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

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

kash: Use kHlpAssert instead of assert.h (debugger stops on the assertion rather than at exit process code).

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