VirtualBox

source: kBuild/trunk/src/kash/parser.c@ 1231

Last change on this file since 1231 was 1218, checked in by bird, 17 years ago

changed eol-style to LF.

  • Property svn:eol-style set to LF
File size: 37.6 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 "shinstance.h"
65
66/*
67 * Shell command parser.
68 */
69
70#define EOFMARKLEN 79
71
72/* values returned by readtoken */
73#include "token.h"
74
75#define OPENBRACE '{'
76#define CLOSEBRACE '}'
77
78
79struct heredoc {
80 struct heredoc *next; /* next here document in list */
81 union node *here; /* redirection node */
82 char *eofmark; /* string indicating end of input */
83 int striptabs; /* if set, strip leading tabs */
84};
85
86
87
88//static int noalias = 0; /* when set, don't handle aliases */
89//struct heredoc *heredoclist; /* list of here documents to read */
90//int parsebackquote; /* nonzero if we are inside backquotes */
91//int doprompt; /* if set, prompt the user */
92//int needprompt; /* true if interactive and at start of line */
93//int lasttoken; /* last token read */
94//MKINIT int tokpushback; /* last token pushed back */
95//char *wordtext; /* text of last word returned by readtoken */
96//MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */
97//struct nodelist *backquotelist;
98//union node *redirnode;
99//struct heredoc *heredoc;
100//int quoteflag; /* set if (part of) last token was quoted */
101//int startlinno; /* line # where last token started */
102
103
104STATIC union node *list(shinstance *, int);
105STATIC union node *andor(shinstance *);
106STATIC union node *pipeline(shinstance *);
107STATIC union node *command(shinstance *);
108STATIC union node *simplecmd(shinstance *, union node **, union node *);
109STATIC union node *makename(shinstance *);
110STATIC void parsefname(shinstance *);
111STATIC void parseheredoc(shinstance *);
112STATIC int peektoken(shinstance *);
113STATIC int readtoken(shinstance *);
114STATIC int xxreadtoken(shinstance *);
115STATIC int readtoken1(shinstance *, int, char const *, char *, int);
116STATIC int noexpand(shinstance *, char *);
117STATIC void synexpect(shinstance *, int) __attribute__((__noreturn__));
118STATIC void synerror(shinstance *, const char *) __attribute__((__noreturn__));
119STATIC void setprompt(shinstance *, int);
120
121
122/*
123 * Read and parse a command. Returns NEOF on end of file. (NULL is a
124 * valid parse tree indicating a blank line.)
125 */
126
127union node *
128parsecmd(shinstance *psh, int interact)
129{
130 int t;
131
132 psh->tokpushback = 0;
133 psh->doprompt = interact;
134 if (psh->doprompt)
135 setprompt(psh, 1);
136 else
137 setprompt(psh, 0);
138 psh->needprompt = 0;
139 t = readtoken(psh);
140 if (t == TEOF)
141 return NEOF;
142 if (t == TNL)
143 return NULL;
144 psh->tokpushback++;
145 return list(psh, 1);
146}
147
148
149STATIC union node *
150list(shinstance *psh, int nlflag)
151{
152 union node *n1, *n2, *n3;
153 int tok;
154
155 psh->checkkwd = 2;
156 if (nlflag == 0 && tokendlist[peektoken(psh)])
157 return NULL;
158 n1 = NULL;
159 for (;;) {
160 n2 = andor(psh);
161 tok = readtoken(psh);
162 if (tok == TBACKGND) {
163 if (n2->type == NCMD || n2->type == NPIPE) {
164 n2->ncmd.backgnd = 1;
165 } else if (n2->type == NREDIR) {
166 n2->type = NBACKGND;
167 } else {
168 n3 = (union node *)stalloc(psh, sizeof (struct nredir));
169 n3->type = NBACKGND;
170 n3->nredir.n = n2;
171 n3->nredir.redirect = NULL;
172 n2 = n3;
173 }
174 }
175 if (n1 == NULL) {
176 n1 = n2;
177 }
178 else {
179 n3 = (union node *)stalloc(psh, sizeof (struct nbinary));
180 n3->type = NSEMI;
181 n3->nbinary.ch1 = n1;
182 n3->nbinary.ch2 = n2;
183 n1 = n3;
184 }
185 switch (tok) {
186 case TBACKGND:
187 case TSEMI:
188 tok = readtoken(psh);
189 /* fall through */
190 case TNL:
191 if (tok == TNL) {
192 parseheredoc(psh);
193 if (nlflag)
194 return n1;
195 } else {
196 psh->tokpushback++;
197 }
198 psh->checkkwd = 2;
199 if (tokendlist[peektoken(psh)])
200 return n1;
201 break;
202 case TEOF:
203 if (psh->heredoclist)
204 parseheredoc(psh);
205 else
206 pungetc(psh); /* push back EOF on input */
207 return n1;
208 default:
209 if (nlflag)
210 synexpect(psh, -1);
211 psh->tokpushback++;
212 return n1;
213 }
214 }
215}
216
217
218
219STATIC union node *
220andor(shinstance *psh)
221{
222 union node *n1, *n2, *n3;
223 int t;
224
225 n1 = pipeline(psh);
226 for (;;) {
227 if ((t = readtoken(psh)) == TAND) {
228 t = NAND;
229 } else if (t == TOR) {
230 t = NOR;
231 } else {
232 psh->tokpushback++;
233 return n1;
234 }
235 n2 = pipeline(psh);
236 n3 = (union node *)stalloc(psh, sizeof (struct nbinary));
237 n3->type = t;
238 n3->nbinary.ch1 = n1;
239 n3->nbinary.ch2 = n2;
240 n1 = n3;
241 }
242}
243
244
245
246STATIC union node *
247pipeline(shinstance *psh)
248{
249 union node *n1, *n2, *pipenode;
250 struct nodelist *lp, *prev;
251 int negate;
252
253 negate = 0;
254 TRACE((psh, "pipeline: entered\n"));
255 while (readtoken(psh) == TNOT)
256 negate = !negate;
257 psh->tokpushback++;
258 n1 = command(psh);
259 if (readtoken(psh) == TPIPE) {
260 pipenode = (union node *)stalloc(psh, sizeof (struct npipe));
261 pipenode->type = NPIPE;
262 pipenode->npipe.backgnd = 0;
263 lp = (struct nodelist *)stalloc(psh, sizeof (struct nodelist));
264 pipenode->npipe.cmdlist = lp;
265 lp->n = n1;
266 do {
267 prev = lp;
268 lp = (struct nodelist *)stalloc(psh, sizeof (struct nodelist));
269 lp->n = command(psh);
270 prev->next = lp;
271 } while (readtoken(psh) == TPIPE);
272 lp->next = NULL;
273 n1 = pipenode;
274 }
275 psh->tokpushback++;
276 if (negate) {
277 n2 = (union node *)stalloc(psh, sizeof (struct nnot));
278 n2->type = NNOT;
279 n2->nnot.com = n1;
280 return n2;
281 } else
282 return n1;
283}
284
285
286
287STATIC union node *
288command(shinstance *psh)
289{
290 union node *n1, *n2;
291 union node *ap, **app;
292 union node *cp, **cpp;
293 union node *redir, **rpp;
294 int t, negate = 0;
295
296 psh->checkkwd = 2;
297 redir = NULL;
298 n1 = NULL;
299 rpp = &redir;
300
301 /* Check for redirection which may precede command */
302 while (readtoken(psh) == TREDIR) {
303 *rpp = n2 = psh->redirnode;
304 rpp = &n2->nfile.next;
305 parsefname(psh);
306 }
307 psh->tokpushback++;
308
309 while (readtoken(psh) == TNOT) {
310 TRACE((psh, "command: TNOT recognized\n"));
311 negate = !negate;
312 }
313 psh->tokpushback++;
314
315 switch (readtoken(psh)) {
316 case TIF:
317 n1 = (union node *)stalloc(psh, sizeof (struct nif));
318 n1->type = NIF;
319 n1->nif.test = list(psh, 0);
320 if (readtoken(psh) != TTHEN)
321 synexpect(psh, TTHEN);
322 n1->nif.ifpart = list(psh, 0);
323 n2 = n1;
324 while (readtoken(psh) == TELIF) {
325 n2->nif.elsepart = (union node *)stalloc(psh, sizeof (struct nif));
326 n2 = n2->nif.elsepart;
327 n2->type = NIF;
328 n2->nif.test = list(psh, 0);
329 if (readtoken(psh) != TTHEN)
330 synexpect(psh, TTHEN);
331 n2->nif.ifpart = list(psh, 0);
332 }
333 if (psh->lasttoken == TELSE)
334 n2->nif.elsepart = list(psh, 0);
335 else {
336 n2->nif.elsepart = NULL;
337 psh->tokpushback++;
338 }
339 if (readtoken(psh) != TFI)
340 synexpect(psh, TFI);
341 psh->checkkwd = 1;
342 break;
343 case TWHILE:
344 case TUNTIL: {
345 int got;
346 n1 = (union node *)stalloc(psh, sizeof (struct nbinary));
347 n1->type = (psh->lasttoken == TWHILE)? NWHILE : NUNTIL;
348 n1->nbinary.ch1 = list(psh, 0);
349 if ((got=readtoken(psh)) != TDO) {
350TRACE((psh, "expecting DO got %s %s\n", tokname[got], got == TWORD ? psh->wordtext : ""));
351 synexpect(psh, TDO);
352 }
353 n1->nbinary.ch2 = list(psh, 0);
354 if (readtoken(psh) != TDONE)
355 synexpect(psh, TDONE);
356 psh->checkkwd = 1;
357 break;
358 }
359 case TFOR:
360 if (readtoken(psh) != TWORD || psh->quoteflag || ! goodname(psh->wordtext))
361 synerror(psh, "Bad for loop variable");
362 n1 = (union node *)stalloc(psh, sizeof (struct nfor));
363 n1->type = NFOR;
364 n1->nfor.var = psh->wordtext;
365 if (readtoken(psh) == TWORD && ! psh->quoteflag && equal(psh->wordtext, "in")) {
366 app = &ap;
367 while (readtoken(psh) == TWORD) {
368 n2 = (union node *)stalloc(psh, sizeof (struct narg));
369 n2->type = NARG;
370 n2->narg.text = psh->wordtext;
371 n2->narg.backquote = psh->backquotelist;
372 *app = n2;
373 app = &n2->narg.next;
374 }
375 *app = NULL;
376 n1->nfor.args = ap;
377 if (psh->lasttoken != TNL && psh->lasttoken != TSEMI)
378 synexpect(psh, -1);
379 } else {
380 static char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
381 '@', '=', '\0'};
382 n2 = (union node *)stalloc(psh, sizeof (struct narg));
383 n2->type = NARG;
384 n2->narg.text = argvars;
385 n2->narg.backquote = NULL;
386 n2->narg.next = NULL;
387 n1->nfor.args = n2;
388 /*
389 * Newline or semicolon here is optional (but note
390 * that the original Bourne shell only allowed NL).
391 */
392 if (psh->lasttoken != TNL && psh->lasttoken != TSEMI)
393 psh->tokpushback++;
394 }
395 psh->checkkwd = 2;
396 if ((t = readtoken(psh)) == TDO)
397 t = TDONE;
398 else if (t == TBEGIN)
399 t = TEND;
400 else
401 synexpect(psh, -1);
402 n1->nfor.body = list(psh, 0);
403 if (readtoken(psh) != t)
404 synexpect(psh, t);
405 psh->checkkwd = 1;
406 break;
407 case TCASE:
408 n1 = (union node *)stalloc(psh, sizeof (struct ncase));
409 n1->type = NCASE;
410 if (readtoken(psh) != TWORD)
411 synexpect(psh, TWORD);
412 n1->ncase.expr = n2 = (union node *)stalloc(psh, sizeof (struct narg));
413 n2->type = NARG;
414 n2->narg.text = psh->wordtext;
415 n2->narg.backquote = psh->backquotelist;
416 n2->narg.next = NULL;
417 while (readtoken(psh) == TNL);
418 if (psh->lasttoken != TWORD || ! equal(psh->wordtext, "in"))
419 synerror(psh, "expecting \"in\"");
420 cpp = &n1->ncase.cases;
421 psh->noalias = 1;
422 psh->checkkwd = 2, readtoken(psh);
423 do {
424 *cpp = cp = (union node *)stalloc(psh, sizeof (struct nclist));
425 cp->type = NCLIST;
426 app = &cp->nclist.pattern;
427 for (;;) {
428 *app = ap = (union node *)stalloc(psh, sizeof (struct narg));
429 ap->type = NARG;
430 ap->narg.text = psh->wordtext;
431 ap->narg.backquote = psh->backquotelist;
432 if (psh->checkkwd = 2, readtoken(psh) != TPIPE)
433 break;
434 app = &ap->narg.next;
435 readtoken(psh);
436 }
437 ap->narg.next = NULL;
438 psh->noalias = 0;
439 if (psh->lasttoken != TRP) {
440 synexpect(psh, TRP);
441 }
442 cp->nclist.body = list(psh, 0);
443
444 psh->checkkwd = 2;
445 if ((t = readtoken(psh)) != TESAC) {
446 if (t != TENDCASE) {
447 psh->noalias = 0;
448 synexpect(psh, TENDCASE);
449 } else {
450 psh->noalias = 1;
451 psh->checkkwd = 2;
452 readtoken(psh);
453 }
454 }
455 cpp = &cp->nclist.next;
456 } while(psh->lasttoken != TESAC);
457 psh->noalias = 0;
458 *cpp = NULL;
459 psh->checkkwd = 1;
460 break;
461 case TLP:
462 n1 = (union node *)stalloc(psh, sizeof (struct nredir));
463 n1->type = NSUBSHELL;
464 n1->nredir.n = list(psh, 0);
465 n1->nredir.redirect = NULL;
466 if (readtoken(psh) != TRP)
467 synexpect(psh, TRP);
468 psh->checkkwd = 1;
469 break;
470 case TBEGIN:
471 n1 = list(psh, 0);
472 if (readtoken(psh) != TEND)
473 synexpect(psh, TEND);
474 psh->checkkwd = 1;
475 break;
476 /* Handle an empty command like other simple commands. */
477 case TSEMI:
478 /*
479 * An empty command before a ; doesn't make much sense, and
480 * should certainly be disallowed in the case of `if ;'.
481 */
482 if (!redir)
483 synexpect(psh, -1);
484 case TAND:
485 case TOR:
486 case TNL:
487 case TEOF:
488 case TWORD:
489 case TRP:
490 psh->tokpushback++;
491 n1 = simplecmd(psh, rpp, redir);
492 goto checkneg;
493 default:
494 synexpect(psh, -1);
495 /* NOTREACHED */
496 }
497
498 /* Now check for redirection which may follow command */
499 while (readtoken(psh) == TREDIR) {
500 *rpp = n2 = psh->redirnode;
501 rpp = &n2->nfile.next;
502 parsefname(psh);
503 }
504 psh->tokpushback++;
505 *rpp = NULL;
506 if (redir) {
507 if (n1->type != NSUBSHELL) {
508 n2 = (union node *)stalloc(psh, sizeof (struct nredir));
509 n2->type = NREDIR;
510 n2->nredir.n = n1;
511 n1 = n2;
512 }
513 n1->nredir.redirect = redir;
514 }
515
516checkneg:
517 if (negate) {
518 n2 = (union node *)stalloc(psh, sizeof (struct nnot));
519 n2->type = NNOT;
520 n2->nnot.com = n1;
521 return n2;
522 }
523 else
524 return n1;
525}
526
527
528STATIC union node *
529simplecmd(shinstance *psh, union node **rpp, union node *redir)
530{
531 union node *args, **app;
532 union node **orig_rpp = rpp;
533 union node *n = NULL, *n2;
534 int negate = 0;
535
536 /* If we don't have any redirections already, then we must reset */
537 /* rpp to be the address of the local redir variable. */
538 if (redir == 0)
539 rpp = &redir;
540
541 args = NULL;
542 app = &args;
543 /*
544 * We save the incoming value, because we need this for shell
545 * functions. There can not be a redirect or an argument between
546 * the function name and the open parenthesis.
547 */
548 orig_rpp = rpp;
549
550 while (readtoken(psh) == TNOT) {
551 TRACE((psh, "command: TNOT recognized\n"));
552 negate = !negate;
553 }
554 psh->tokpushback++;
555
556 for (;;) {
557 if (readtoken(psh) == TWORD) {
558 n = (union node *)stalloc(psh, sizeof (struct narg));
559 n->type = NARG;
560 n->narg.text = psh->wordtext;
561 n->narg.backquote = psh->backquotelist;
562 *app = n;
563 app = &n->narg.next;
564 } else if (psh->lasttoken == TREDIR) {
565 *rpp = n = psh->redirnode;
566 rpp = &n->nfile.next;
567 parsefname(psh); /* read name of redirection file */
568 } else if (psh->lasttoken == TLP && app == &args->narg.next
569 && rpp == orig_rpp) {
570 /* We have a function */
571 if (readtoken(psh) != TRP)
572 synexpect(psh, TRP);
573#ifdef notdef
574 if (! goodname(n->narg.text))
575 synerror(psh, "Bad function name");
576#endif
577 n->type = NDEFUN;
578 n->narg.next = command(psh);
579 goto checkneg;
580 } else {
581 psh->tokpushback++;
582 break;
583 }
584 }
585 *app = NULL;
586 *rpp = NULL;
587 n = (union node *)stalloc(psh, sizeof (struct ncmd));
588 n->type = NCMD;
589 n->ncmd.backgnd = 0;
590 n->ncmd.args = args;
591 n->ncmd.redirect = redir;
592
593checkneg:
594 if (negate) {
595 n2 = (union node *)stalloc(psh, sizeof (struct nnot));
596 n2->type = NNOT;
597 n2->nnot.com = n;
598 return n2;
599 }
600 else
601 return n;
602}
603
604STATIC union node *
605makename(shinstance *psh)
606{
607 union node *n;
608
609 n = (union node *)stalloc(psh, sizeof (struct narg));
610 n->type = NARG;
611 n->narg.next = NULL;
612 n->narg.text = psh->wordtext;
613 n->narg.backquote = psh->backquotelist;
614 return n;
615}
616
617void fixredir(shinstance *psh, union node *n, const char *text, int err)
618 {
619 TRACE((psh, "Fix redir %s %d\n", text, err));
620 if (!err)
621 n->ndup.vname = NULL;
622
623 if (is_digit(text[0]) && text[1] == '\0')
624 n->ndup.dupfd = digit_val(text[0]);
625 else if (text[0] == '-' && text[1] == '\0')
626 n->ndup.dupfd = -1;
627 else {
628
629 if (err)
630 synerror(psh, "Bad fd number");
631 else
632 n->ndup.vname = makename(psh);
633 }
634}
635
636
637STATIC void
638parsefname(shinstance *psh)
639{
640 union node *n = psh->redirnode;
641
642 if (readtoken(psh) != TWORD)
643 synexpect(psh, -1);
644 if (n->type == NHERE) {
645 struct heredoc *here = psh->heredoc;
646 struct heredoc *p;
647 size_t i;
648
649 if (psh->quoteflag == 0)
650 n->type = NXHERE;
651 TRACE((psh, "Here document %d\n", n->type));
652 if (here->striptabs) {
653 while (*psh->wordtext == '\t')
654 psh->wordtext++;
655 }
656 if (! noexpand(psh, psh->wordtext) || (i = strlen(psh->wordtext)) == 0 || i > EOFMARKLEN)
657 synerror(psh, "Illegal eof marker for << redirection");
658 rmescapes(psh, psh->wordtext);
659 here->eofmark = psh->wordtext;
660 here->next = NULL;
661 if (psh->heredoclist == NULL)
662 psh->heredoclist = here;
663 else {
664 for (p = psh->heredoclist ; p->next ; p = p->next);
665 p->next = here;
666 }
667 } else if (n->type == NTOFD || n->type == NFROMFD) {
668 fixredir(psh, n, psh->wordtext, 0);
669 } else {
670 n->nfile.fname = makename(psh);
671 }
672}
673
674
675/*
676 * Input any here documents.
677 */
678
679STATIC void
680parseheredoc(shinstance *psh)
681{
682 struct heredoc *here;
683 union node *n;
684
685 while (psh->heredoclist) {
686 here = psh->heredoclist;
687 psh->heredoclist = here->next;
688 if (psh->needprompt) {
689 setprompt(psh, 2);
690 psh->needprompt = 0;
691 }
692 readtoken1(psh, pgetc(psh), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
693 here->eofmark, here->striptabs);
694 n = (union node *)stalloc(psh, sizeof (struct narg));
695 n->narg.type = NARG;
696 n->narg.next = NULL;
697 n->narg.text = psh->wordtext;
698 n->narg.backquote = psh->backquotelist;
699 here->here->nhere.doc = n;
700 }
701}
702
703STATIC int
704peektoken(shinstance *psh)
705{
706 int t;
707
708 t = readtoken(psh);
709 psh->tokpushback++;
710 return (t);
711}
712
713STATIC int
714readtoken(shinstance *psh)
715{
716 int t;
717 int savecheckkwd = psh->checkkwd;
718#ifdef DEBUG
719 int alreadyseen = psh->tokpushback;
720#endif
721 struct alias *ap;
722
723 top:
724 t = xxreadtoken(psh);
725
726 if (psh->checkkwd) {
727 /*
728 * eat newlines
729 */
730 if (psh->checkkwd == 2) {
731 psh->checkkwd = 0;
732 while (t == TNL) {
733 parseheredoc(psh);
734 t = xxreadtoken(psh);
735 }
736 } else
737 psh->checkkwd = 0;
738 /*
739 * check for keywords and aliases
740 */
741 if (t == TWORD && !psh->quoteflag)
742 {
743 const char *const *pp;
744
745 for (pp = parsekwd; *pp; pp++) {
746 if (**pp == *psh->wordtext && equal(*pp, psh->wordtext))
747 {
748 psh->lasttoken = t = (int)(pp -
749 parsekwd + KWDOFFSET);
750 TRACE((psh, "keyword %s recognized\n", tokname[t]));
751 goto out;
752 }
753 }
754 if(!psh->noalias &&
755 (ap = lookupalias(psh, psh->wordtext, 1)) != NULL) {
756 pushstring(psh, ap->val, strlen(ap->val), ap);
757 psh->checkkwd = savecheckkwd;
758 goto top;
759 }
760 }
761out:
762 psh->checkkwd = (t == TNOT) ? savecheckkwd : 0;
763 }
764#ifdef DEBUG
765 if (!alreadyseen)
766 TRACE((psh, "token %s %s\n", tokname[t], t == TWORD ? psh->wordtext : ""));
767 else
768 TRACE((psh, "reread token %s %s\n", tokname[t], t == TWORD ? psh->wordtext : ""));
769#endif
770 return (t);
771}
772
773
774/*
775 * Read the next input token.
776 * If the token is a word, we set psh->backquotelist to the list of cmds in
777 * backquotes. We set psh->quoteflag to true if any part of the word was
778 * quoted.
779 * If the token is TREDIR, then we set psh->redirnode to a structure containing
780 * the redirection.
781 * In all cases, the variable psh->startlinno is set to the number of the line
782 * on which the token starts.
783 *
784 * [Change comment: here documents and internal procedures]
785 * [Readtoken shouldn't have any arguments. Perhaps we should make the
786 * word parsing code into a separate routine. In this case, readtoken
787 * doesn't need to have any internal procedures, but parseword does.
788 * We could also make parseoperator in essence the main routine, and
789 * have parseword (readtoken1?) handle both words and redirection.]
790 */
791
792#define RETURN(token) return psh->lasttoken = token
793
794STATIC int
795xxreadtoken(shinstance *psh)
796{
797 int c;
798
799 if (psh->tokpushback) {
800 psh->tokpushback = 0;
801 return psh->lasttoken;
802 }
803 if (psh->needprompt) {
804 setprompt(psh, 2);
805 psh->needprompt = 0;
806 }
807 psh->startlinno = psh->plinno;
808 for (;;) { /* until token or start of word found */
809 c = pgetc_macro(psh);
810 if (c == ' ' || c == '\t')
811 continue; /* quick check for white space first */
812 switch (c) {
813 case ' ': case '\t':
814 continue;
815 case '#':
816 while ((c = pgetc(psh)) != '\n' && c != PEOF);
817 pungetc(psh);
818 continue;
819 case '\\':
820 if (pgetc(psh) == '\n') {
821 psh->startlinno = ++psh->plinno;
822 if (psh->doprompt)
823 setprompt(psh, 2);
824 else
825 setprompt(psh, 0);
826 continue;
827 }
828 pungetc(psh);
829 goto breakloop;
830 case '\n':
831 psh->plinno++;
832 psh->needprompt = psh->doprompt;
833 RETURN(TNL);
834 case PEOF:
835 RETURN(TEOF);
836 case '&':
837 if (pgetc(psh) == '&')
838 RETURN(TAND);
839 pungetc(psh);
840 RETURN(TBACKGND);
841 case '|':
842 if (pgetc(psh) == '|')
843 RETURN(TOR);
844 pungetc(psh);
845 RETURN(TPIPE);
846 case ';':
847 if (pgetc(psh) == ';')
848 RETURN(TENDCASE);
849 pungetc(psh);
850 RETURN(TSEMI);
851 case '(':
852 RETURN(TLP);
853 case ')':
854 RETURN(TRP);
855 default:
856 goto breakloop;
857 }
858 }
859breakloop:
860 return readtoken1(psh, c, BASESYNTAX, (char *)NULL, 0);
861#undef RETURN
862}
863
864
865
866/*
867 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
868 * is not NULL, read a here document. In the latter case, eofmark is the
869 * word which marks the end of the document and striptabs is true if
870 * leading tabs should be stripped from the document. The argument firstc
871 * is the first character of the input token or document.
872 *
873 * Because C does not have internal subroutines, I have simulated them
874 * using goto's to implement the subroutine linkage. The following macros
875 * will run code that appears at the end of readtoken1.
876 */
877
878#define CHECKEND() {goto checkend; checkend_return:;}
879#define PARSEREDIR() {goto parseredir; parseredir_return:;}
880#define PARSESUB() {goto parsesub; parsesub_return:;}
881#define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
882#define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
883#define PARSEARITH() {goto parsearith; parsearith_return:;}
884
885/*
886 * Keep track of nested doublequotes in dblquote and doublequotep.
887 * We use dblquote for the first 32 levels, and we expand to a malloc'ed
888 * region for levels above that. Usually we never need to malloc.
889 * This code assumes that an int is 32 bits. We don't use uint32_t,
890 * because the rest of the code does not.
891 */
892#define ISDBLQUOTE() ((varnest < 32) ? (dblquote & (1 << varnest)) : \
893 (dblquotep[(varnest / 32) - 1] & (1 << (varnest % 32))))
894
895#define SETDBLQUOTE() \
896 if (varnest < 32) \
897 dblquote |= (1 << varnest); \
898 else \
899 dblquotep[(varnest / 32) - 1] |= (1 << (varnest % 32))
900
901#define CLRDBLQUOTE() \
902 if (varnest < 32) \
903 dblquote &= ~(1 << varnest); \
904 else \
905 dblquotep[(varnest / 32) - 1] &= ~(1 << (varnest % 32))
906
907STATIC int
908readtoken1(shinstance *psh, int firstc, char const *syntax, char *eofmark, int striptabs)
909{
910 int c = firstc;
911 char *out;
912 int len;
913 char line[EOFMARKLEN + 1];
914 struct nodelist *bqlist;
915 int quotef;
916 int *dblquotep = NULL;
917 size_t maxnest = 32;
918 int dblquote;
919 int varnest; /* levels of variables expansion */
920 int arinest; /* levels of arithmetic expansion */
921 int parenlevel; /* levels of parens in arithmetic */
922 int oldstyle;
923 char const *prevsyntax; /* syntax before arithmetic */
924#if __GNUC__
925 /* Avoid longjmp clobbering */
926 (void) &maxnest;
927 (void) &dblquotep;
928 (void) &out;
929 (void) &quotef;
930 (void) &dblquote;
931 (void) &varnest;
932 (void) &arinest;
933 (void) &parenlevel;
934 (void) &oldstyle;
935 (void) &prevsyntax;
936 (void) &syntax;
937#endif
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 STARTSTACKSTR(psh, out);
951 loop: { /* for each line, until end of word */
952#if ATTY
953 if (c == '\034' && psh->doprompt
954 && attyset() && ! equal(termval(), "emacs")) {
955 attyline();
956 if (syntax == BASESYNTAX)
957 return readtoken(psh);
958 c = pgetc(psh);
959 goto loop;
960 }
961#endif
962 CHECKEND(); /* set c to PEOF if at end of here document */
963 for (;;) { /* until end of line or end of word */
964 CHECKSTRSPACE(psh, 4, out); /* permit 4 calls to USTPUTC */
965 switch(syntax[c]) {
966 case CNL: /* '\n' */
967 if (syntax == BASESYNTAX)
968 goto endword; /* exit outer loop */
969 USTPUTC(psh, c, out);
970 psh->plinno++;
971 if (psh->doprompt)
972 setprompt(psh, 2);
973 else
974 setprompt(psh, 0);
975 c = pgetc(psh);
976 goto loop; /* continue outer loop */
977 case CWORD:
978 USTPUTC(psh, c, out);
979 break;
980 case CCTL:
981 if (eofmark == NULL || ISDBLQUOTE())
982 USTPUTC(psh, CTLESC, out);
983 USTPUTC(psh, c, out);
984 break;
985 case CBACK: /* backslash */
986 c = pgetc(psh);
987 if (c == PEOF) {
988 USTPUTC(psh, '\\', out);
989 pungetc(psh);
990 break;
991 }
992 if (c == '\n') {
993 if (psh->doprompt)
994 setprompt(psh, 2);
995 else
996 setprompt(psh, 0);
997 break;
998 }
999 quotef = 1;
1000 if (ISDBLQUOTE() && c != '\\' &&
1001 c != '`' && c != '$' &&
1002 (c != '"' || eofmark != NULL))
1003 USTPUTC(psh, '\\', out);
1004 if (SQSYNTAX[c] == CCTL)
1005 USTPUTC(psh, CTLESC, out);
1006 else if (eofmark == NULL) {
1007 USTPUTC(psh, CTLQUOTEMARK, out);
1008 USTPUTC(psh, c, out);
1009 if (varnest != 0)
1010 USTPUTC(psh, CTLQUOTEEND, out);
1011 break;
1012 }
1013 USTPUTC(psh, c, out);
1014 break;
1015 case CSQUOTE:
1016 if (syntax != SQSYNTAX) {
1017 if (eofmark == NULL)
1018 USTPUTC(psh, CTLQUOTEMARK, out);
1019 quotef = 1;
1020 syntax = SQSYNTAX;
1021 break;
1022 }
1023 if (eofmark != NULL && arinest == 0 &&
1024 varnest == 0) {
1025 /* Ignore inside quoted here document */
1026 USTPUTC(psh, c, out);
1027 break;
1028 }
1029 /* End of single quotes... */
1030 if (arinest)
1031 syntax = ARISYNTAX;
1032 else {
1033 syntax = BASESYNTAX;
1034 if (varnest != 0)
1035 USTPUTC(psh, CTLQUOTEEND, out);
1036 }
1037 break;
1038 case CDQUOTE:
1039 if (eofmark != NULL && arinest == 0 &&
1040 varnest == 0) {
1041 /* Ignore inside here document */
1042 USTPUTC(psh, c, out);
1043 break;
1044 }
1045 quotef = 1;
1046 if (arinest) {
1047 if (ISDBLQUOTE()) {
1048 syntax = ARISYNTAX;
1049 CLRDBLQUOTE();
1050 } else {
1051 syntax = DQSYNTAX;
1052 SETDBLQUOTE();
1053 USTPUTC(psh, CTLQUOTEMARK, out);
1054 }
1055 break;
1056 }
1057 if (eofmark != NULL)
1058 break;
1059 if (ISDBLQUOTE()) {
1060 if (varnest != 0)
1061 USTPUTC(psh, CTLQUOTEEND, out);
1062 syntax = BASESYNTAX;
1063 CLRDBLQUOTE();
1064 } else {
1065 syntax = DQSYNTAX;
1066 SETDBLQUOTE();
1067 USTPUTC(psh, CTLQUOTEMARK, out);
1068 }
1069 break;
1070 case CVAR: /* '$' */
1071 PARSESUB(); /* parse substitution */
1072 break;
1073 case CENDVAR: /* CLOSEBRACE */
1074 if (varnest > 0 && !ISDBLQUOTE()) {
1075 varnest--;
1076 USTPUTC(psh, CTLENDVAR, out);
1077 } else {
1078 USTPUTC(psh, c, out);
1079 }
1080 break;
1081 case CLP: /* '(' in arithmetic */
1082 parenlevel++;
1083 USTPUTC(psh, c, out);
1084 break;
1085 case CRP: /* ')' in arithmetic */
1086 if (parenlevel > 0) {
1087 USTPUTC(psh, c, out);
1088 --parenlevel;
1089 } else {
1090 if (pgetc(psh) == ')') {
1091 if (--arinest == 0) {
1092 USTPUTC(psh, CTLENDARI, out);
1093 syntax = prevsyntax;
1094 if (syntax == DQSYNTAX)
1095 SETDBLQUOTE();
1096 else
1097 CLRDBLQUOTE();
1098 } else
1099 USTPUTC(psh, ')', out);
1100 } else {
1101 /*
1102 * unbalanced parens
1103 * (don't 2nd guess - no error)
1104 */
1105 pungetc(psh);
1106 USTPUTC(psh, ')', out);
1107 }
1108 }
1109 break;
1110 case CBQUOTE: /* '`' */
1111 PARSEBACKQOLD();
1112 break;
1113 case CSHEOF:
1114 goto endword; /* exit outer loop */
1115 default:
1116 if (varnest == 0)
1117 goto endword; /* exit outer loop */
1118 USTPUTC(psh, c, out);
1119 }
1120 c = pgetc_macro(psh);
1121 }
1122 }
1123endword:
1124 if (syntax == ARISYNTAX)
1125 synerror(psh, "Missing '))'");
1126 if (syntax != BASESYNTAX && ! psh->parsebackquote && eofmark == NULL)
1127 synerror(psh, "Unterminated quoted string");
1128 if (varnest != 0) {
1129 psh->startlinno = psh->plinno;
1130 /* { */
1131 synerror(psh, "Missing '}'");
1132 }
1133 USTPUTC(psh, '\0', out);
1134 len = (int)(out - stackblock(psh));
1135 out = stackblock(psh);
1136 if (eofmark == NULL) {
1137 if ((c == '>' || c == '<')
1138 && quotef == 0
1139 && len <= 2
1140 && (*out == '\0' || is_digit(*out))) {
1141 PARSEREDIR();
1142 return psh->lasttoken = TREDIR;
1143 } else {
1144 pungetc(psh);
1145 }
1146 }
1147 psh->quoteflag = quotef;
1148 psh->backquotelist = bqlist;
1149 grabstackblock(psh, len);
1150 psh->wordtext = out;
1151 if (dblquotep != NULL)
1152 ckfree(dblquotep);
1153 return psh->lasttoken = TWORD;
1154/* end of readtoken routine */
1155
1156
1157
1158/*
1159 * Check to see whether we are at the end of the here document. When this
1160 * is called, c is set to the first character of the next input line. If
1161 * we are at the end of the here document, this routine sets the c to PEOF.
1162 */
1163
1164checkend: {
1165 if (eofmark) {
1166 if (striptabs) {
1167 while (c == '\t')
1168 c = pgetc(psh);
1169 }
1170 if (c == *eofmark) {
1171 if (pfgets(psh, line, sizeof line) != NULL) {
1172 char *p, *q;
1173
1174 p = line;
1175 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1176 if (*p == '\n' && *q == '\0') {
1177 c = PEOF;
1178 psh->plinno++;
1179 psh->needprompt = psh->doprompt;
1180 } else {
1181 pushstring(psh, line, strlen(line), NULL);
1182 }
1183 }
1184 }
1185 }
1186 goto checkend_return;
1187}
1188
1189
1190/*
1191 * Parse a redirection operator. The variable "out" points to a string
1192 * specifying the fd to be redirected. The variable "c" contains the
1193 * first character of the redirection operator.
1194 */
1195
1196parseredir: {
1197 char fd = *out;
1198 union node *np;
1199
1200 np = (union node *)stalloc(psh, sizeof (struct nfile));
1201 if (c == '>') {
1202 np->nfile.fd = 1;
1203 c = pgetc(psh);
1204 if (c == '>')
1205 np->type = NAPPEND;
1206 else if (c == '|')
1207 np->type = NCLOBBER;
1208 else if (c == '&')
1209 np->type = NTOFD;
1210 else {
1211 np->type = NTO;
1212 pungetc(psh);
1213 }
1214 } else { /* c == '<' */
1215 np->nfile.fd = 0;
1216 switch (c = pgetc(psh)) {
1217 case '<':
1218 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1219 np = (union node *)stalloc(psh, sizeof (struct nhere));
1220 np->nfile.fd = 0;
1221 }
1222 np->type = NHERE;
1223 psh->heredoc = (struct heredoc *)stalloc(psh, sizeof (struct heredoc));
1224 psh->heredoc->here = np;
1225 if ((c = pgetc(psh)) == '-') {
1226 psh->heredoc->striptabs = 1;
1227 } else {
1228 psh->heredoc->striptabs = 0;
1229 pungetc(psh);
1230 }
1231 break;
1232
1233 case '&':
1234 np->type = NFROMFD;
1235 break;
1236
1237 case '>':
1238 np->type = NFROMTO;
1239 break;
1240
1241 default:
1242 np->type = NFROM;
1243 pungetc(psh);
1244 break;
1245 }
1246 }
1247 if (fd != '\0')
1248 np->nfile.fd = digit_val(fd);
1249 psh->redirnode = np;
1250 goto parseredir_return;
1251}
1252
1253
1254/*
1255 * Parse a substitution. At this point, we have read the dollar sign
1256 * and nothing else.
1257 */
1258
1259parsesub: {
1260 int subtype;
1261 int typeloc;
1262 int flags;
1263 char *p;
1264 static const char types[] = "}-+?=";
1265
1266 c = pgetc(psh);
1267 if (c != '(' && c != OPENBRACE && !is_name(c) && !is_special(c)) {
1268 USTPUTC(psh, '$', out);
1269 pungetc(psh);
1270 } else if (c == '(') { /* $(command) or $((arith)) */
1271 if (pgetc(psh) == '(') {
1272 PARSEARITH();
1273 } else {
1274 pungetc(psh);
1275 PARSEBACKQNEW();
1276 }
1277 } else {
1278 USTPUTC(psh, CTLVAR, out);
1279 typeloc = (int)(out - stackblock(psh));
1280 USTPUTC(psh, VSNORMAL, out);
1281 subtype = VSNORMAL;
1282 if (c == OPENBRACE) {
1283 c = pgetc(psh);
1284 if (c == '#') {
1285 if ((c = pgetc(psh)) == CLOSEBRACE)
1286 c = '#';
1287 else
1288 subtype = VSLENGTH;
1289 }
1290 else
1291 subtype = 0;
1292 }
1293 if (is_name(c)) {
1294 do {
1295 STPUTC(psh, c, out);
1296 c = pgetc(psh);
1297 } while (is_in_name(c));
1298 } else if (is_digit(c)) {
1299 do {
1300 USTPUTC(psh, c, out);
1301 c = pgetc(psh);
1302 } while (is_digit(c));
1303 }
1304 else if (is_special(c)) {
1305 USTPUTC(psh, c, out);
1306 c = pgetc(psh);
1307 }
1308 else
1309badsub: synerror(psh, "Bad substitution");
1310
1311 STPUTC(psh, '=', out);
1312 flags = 0;
1313 if (subtype == 0) {
1314 switch (c) {
1315 case ':':
1316 flags = VSNUL;
1317 c = pgetc(psh);
1318 /*FALLTHROUGH*/
1319 default:
1320 p = strchr(types, c);
1321 if (p == NULL)
1322 goto badsub;
1323 subtype = (int)(p - types + VSNORMAL);
1324 break;
1325 case '%':
1326 case '#':
1327 {
1328 int cc = c;
1329 subtype = c == '#' ? VSTRIMLEFT :
1330 VSTRIMRIGHT;
1331 c = pgetc(psh);
1332 if (c == cc)
1333 subtype++;
1334 else
1335 pungetc(psh);
1336 break;
1337 }
1338 }
1339 } else {
1340 pungetc(psh);
1341 }
1342 if (ISDBLQUOTE() || arinest)
1343 flags |= VSQUOTE;
1344 *(stackblock(psh) + typeloc) = subtype | flags;
1345 if (subtype != VSNORMAL) {
1346 varnest++;
1347 if (varnest >= maxnest) {
1348 dblquotep = ckrealloc(dblquotep, maxnest / 8);
1349 dblquotep[(maxnest / 32) - 1] = 0;
1350 maxnest += 32;
1351 }
1352 }
1353 }
1354 goto parsesub_return;
1355}
1356
1357
1358/*
1359 * Called to parse command substitutions. Newstyle is set if the command
1360 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1361 * list of commands (passed by reference), and savelen is the number of
1362 * characters on the top of the stack which must be preserved.
1363 */
1364
1365parsebackq: {
1366 struct nodelist **nlpp;
1367 int savepbq;
1368 union node *n;
1369 char *volatile str;
1370 struct jmploc jmploc;
1371 struct jmploc *volatile savehandler;
1372 int savelen;
1373 int saveprompt;
1374#ifdef __GNUC__
1375 (void) &saveprompt;
1376#endif
1377
1378 savepbq = psh->parsebackquote;
1379 if (setjmp(jmploc.loc)) {
1380 if (str)
1381 ckfree(str);
1382 psh->parsebackquote = 0;
1383 psh->handler = savehandler;
1384 longjmp(psh->handler->loc, 1);
1385 }
1386 INTOFF;
1387 str = NULL;
1388 savelen = (int)(out - stackblock(psh));
1389 if (savelen > 0) {
1390 str = ckmalloc(savelen);
1391 memcpy(str, stackblock(psh), savelen);
1392 }
1393 savehandler = psh->handler;
1394 psh->handler = &jmploc;
1395 INTON;
1396 if (oldstyle) {
1397 /* We must read until the closing backquote, giving special
1398 treatment to some slashes, and then push the string and
1399 reread it as input, interpreting it normally. */
1400 char *pout;
1401 int pc;
1402 int psavelen;
1403 char *pstr;
1404
1405
1406 STARTSTACKSTR(psh, pout);
1407 for (;;) {
1408 if (psh->needprompt) {
1409 setprompt(psh, 2);
1410 psh->needprompt = 0;
1411 }
1412 switch (pc = pgetc(psh)) {
1413 case '`':
1414 goto done;
1415
1416 case '\\':
1417 if ((pc = pgetc(psh)) == '\n') {
1418 psh->plinno++;
1419 if (psh->doprompt)
1420 setprompt(psh, 2);
1421 else
1422 setprompt(psh, 0);
1423 /*
1424 * If eating a newline, avoid putting
1425 * the newline into the new character
1426 * stream (via the STPUTC after the
1427 * switch).
1428 */
1429 continue;
1430 }
1431 if (pc != '\\' && pc != '`' && pc != '$'
1432 && (!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(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
1597STATIC 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
1613STATIC void
1614synerror(shinstance *psh, const char *msg)
1615{
1616 if (psh->commandname)
1617 outfmt(&psh->errout, "%s: %d: ", psh->commandname, psh->startlinno);
1618 outfmt(&psh->errout, "Syntax error: %s\n", msg);
1619 error(psh, (char *)NULL);
1620 /* NOTREACHED */
1621}
1622
1623STATIC void
1624setprompt(shinstance *psh, int which)
1625{
1626 psh->whichprompt = which;
1627
1628#ifndef SMALL
1629 if (!el)
1630#endif
1631 out2str(psh, getprompt(psh, NULL));
1632}
1633
1634/*
1635 * called by editline -- any expansions to the prompt
1636 * should be added here.
1637 */
1638const char *
1639getprompt(shinstance *psh, void *unused)
1640{
1641 switch (psh->whichprompt) {
1642 case 0:
1643 return "";
1644 case 1:
1645 return ps1val(psh);
1646 case 2:
1647 return ps2val(psh);
1648 default:
1649 return "<internal prompt error>";
1650 }
1651}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette