VirtualBox

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

Last change on this file since 2629 was 2628, checked in by bird, 12 years ago

kash/parser.c: Fixed incorrect handling of \r\n -> \n conversion when there is a lone \r at the end of the input buffer.

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