parser.c revision 222165
133965Sjdp/*- 278828Sobrien * Copyright (c) 1991, 1993 3218822Sdim * The Regents of the University of California. All rights reserved. 433965Sjdp * 533965Sjdp * This code is derived from software contributed to Berkeley by 633965Sjdp * Kenneth Almquist. 733965Sjdp * 833965Sjdp * Redistribution and use in source and binary forms, with or without 933965Sjdp * modification, are permitted provided that the following conditions 1033965Sjdp * are met: 1133965Sjdp * 1. Redistributions of source code must retain the above copyright 1233965Sjdp * notice, this list of conditions and the following disclaimer. 1333965Sjdp * 2. Redistributions in binary form must reproduce the above copyright 1433965Sjdp * notice, this list of conditions and the following disclaimer in the 1533965Sjdp * documentation and/or other materials provided with the distribution. 1633965Sjdp * 4. Neither the name of the University nor the names of its contributors 1733965Sjdp * may be used to endorse or promote products derived from this software 1833965Sjdp * without specific prior written permission. 1933965Sjdp * 20218822Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21218822Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2233965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2333965Sjdp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2433965Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2533965Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2660484Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2733965Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2833965Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2933965Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3033965Sjdp * SUCH DAMAGE. 3133965Sjdp */ 3233965Sjdp 3333965Sjdp#ifndef lint 3433965Sjdp#if 0 35130561Sobrienstatic char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; 3633965Sjdp#endif 3733965Sjdp#endif /* not lint */ 3833965Sjdp#include <sys/cdefs.h> 3933965Sjdp__FBSDID("$FreeBSD: head/bin/sh/parser.c 222165 2011-05-21 22:03:06Z jilles $"); 4033965Sjdp 4133965Sjdp#include <stdlib.h> 4233965Sjdp#include <unistd.h> 4333965Sjdp#include <stdio.h> 4433965Sjdp 4533965Sjdp#include "shell.h" 4633965Sjdp#include "parser.h" 4733965Sjdp#include "nodes.h" 4833965Sjdp#include "expand.h" /* defines rmescapes() */ 4933965Sjdp#include "syntax.h" 5033965Sjdp#include "options.h" 5133965Sjdp#include "input.h" 5233965Sjdp#include "output.h" 5333965Sjdp#include "var.h" 5433965Sjdp#include "error.h" 5577298Sobrien#include "memalloc.h" 5633965Sjdp#include "mystring.h" 5733965Sjdp#include "alias.h" 5833965Sjdp#include "show.h" 59218822Sdim#include "eval.h" 60218822Sdim#include "exec.h" /* to check for special builtins */ 61218822Sdim#ifndef NO_HISTORY 62218822Sdim#include "myhistedit.h" 6377298Sobrien#endif 6477298Sobrien 6577298Sobrien/* 6677298Sobrien * Shell command parser. 6777298Sobrien */ 6877298Sobrien 6977298Sobrien#define EOFMARKLEN 79 7033965Sjdp#define PROMPTLEN 128 7133965Sjdp 7277298Sobrien/* values of checkkwd variable */ 7377298Sobrien#define CHKALIAS 0x1 7477298Sobrien#define CHKKWD 0x2 7577298Sobrien#define CHKNL 0x4 7633965Sjdp 7733965Sjdp/* values returned by readtoken */ 7833965Sjdp#include "token.h" 7933965Sjdp 8033965Sjdp 8133965Sjdp 8260484Sobrienstruct heredoc { 8360484Sobrien struct heredoc *next; /* next here document in list */ 8460484Sobrien union node *here; /* redirection node */ 8533965Sjdp char *eofmark; /* string indicating end of input */ 8633965Sjdp int striptabs; /* if set, strip leading tabs */ 8733965Sjdp}; 8833965Sjdp 8933965Sjdpstruct parser_temp { 9033965Sjdp struct parser_temp *next; 9133965Sjdp void *data; 9277298Sobrien}; 9333965Sjdp 9433965Sjdp 9533965Sjdpstatic struct heredoc *heredoclist; /* list of here documents to read */ 9633965Sjdpstatic int doprompt; /* if set, prompt the user */ 9777298Sobrienstatic int needprompt; /* true if interactive and at start of line */ 9833965Sjdpstatic int lasttoken; /* last token read */ 9933965SjdpMKINIT int tokpushback; /* last token pushed back */ 10033965Sjdpstatic char *wordtext; /* text of last word returned by readtoken */ 10133965SjdpMKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ 10277298Sobrienstatic struct nodelist *backquotelist; 10377298Sobrienstatic union node *redirnode; 10433965Sjdpstatic struct heredoc *heredoc; 10533965Sjdpstatic int quoteflag; /* set if (part of) last token was quoted */ 10633965Sjdpstatic int startlinno; /* line # where last token started */ 10733965Sjdpstatic int funclinno; /* line # where the current function started */ 10833965Sjdpstatic struct parser_temp *parser_temp; 10977298Sobrien 11077298Sobrien 11177298Sobrienstatic union node *list(int, int); 11277298Sobrienstatic union node *andor(void); 11377298Sobrienstatic union node *pipeline(void); 11477298Sobrienstatic union node *command(void); 11577298Sobrienstatic union node *simplecmd(union node **, union node *); 11677298Sobrienstatic union node *makename(void); 11777298Sobrienstatic void parsefname(void); 11877298Sobrienstatic void parseheredoc(void); 11977298Sobrienstatic int peektoken(void); 12077298Sobrienstatic int readtoken(void); 12177298Sobrienstatic int xxreadtoken(void); 12277298Sobrienstatic int readtoken1(int, char const *, char *, int); 12377298Sobrienstatic int noexpand(char *); 12477298Sobrienstatic void synexpect(int) __dead2; 12577298Sobrienstatic void synerror(const char *) __dead2; 12633965Sjdpstatic void setprompt(int); 127130561Sobrien 128130561Sobrien 12933965Sjdpstatic void * 13033965Sjdpparser_temp_alloc(size_t len) 13177298Sobrien{ 13233965Sjdp struct parser_temp *t; 13333965Sjdp 13433965Sjdp INTOFF; 13533965Sjdp t = ckmalloc(sizeof(*t)); 13633965Sjdp t->data = NULL; 13777298Sobrien t->next = parser_temp; 13877298Sobrien parser_temp = t; 13933965Sjdp t->data = ckmalloc(len); 140130561Sobrien INTON; 14133965Sjdp return t->data; 14233965Sjdp} 14333965Sjdp 14433965Sjdp 14533965Sjdpstatic void * 14633965Sjdpparser_temp_realloc(void *ptr, size_t len) 14733965Sjdp{ 14833965Sjdp struct parser_temp *t; 14933965Sjdp 15033965Sjdp INTOFF; 15133965Sjdp t = parser_temp; 15233965Sjdp if (ptr != t->data) 15333965Sjdp error("bug: parser_temp_realloc misused"); 15433965Sjdp t->data = ckrealloc(t->data, len); 15533965Sjdp INTON; 15633965Sjdp return t->data; 15760484Sobrien} 15833965Sjdp 15933965Sjdp 16033965Sjdpstatic void 16133965Sjdpparser_temp_free_upto(void *ptr) 16233965Sjdp{ 16333965Sjdp struct parser_temp *t; 16433965Sjdp int done = 0; 16533965Sjdp 16633965Sjdp INTOFF; 16733965Sjdp while (parser_temp != NULL && !done) { 16833965Sjdp t = parser_temp; 16933965Sjdp parser_temp = t->next; 17033965Sjdp done = t->data == ptr; 17133965Sjdp ckfree(t->data); 17277298Sobrien ckfree(t); 17333965Sjdp } 17433965Sjdp INTON; 175130561Sobrien if (!done) 17633965Sjdp error("bug: parser_temp_free_upto misused"); 17733965Sjdp} 17833965Sjdp 17933965Sjdp 18033965Sjdpstatic void 18133965Sjdpparser_temp_free_all(void) 18233965Sjdp{ 18333965Sjdp struct parser_temp *t; 18433965Sjdp 18533965Sjdp INTOFF; 18633965Sjdp while (parser_temp != NULL) { 18733965Sjdp t = parser_temp; 18833965Sjdp parser_temp = t->next; 18933965Sjdp ckfree(t->data); 19033965Sjdp ckfree(t); 19160484Sobrien } 19233965Sjdp INTON; 19333965Sjdp} 19433965Sjdp 19533965Sjdp 19633965Sjdp/* 19733965Sjdp * Read and parse a command. Returns NEOF on end of file. (NULL is a 19833965Sjdp * valid parse tree indicating a blank line.) 19933965Sjdp */ 20033965Sjdp 20133965Sjdpunion node * 202130561Sobrienparsecmd(int interact) 20333965Sjdp{ 20433965Sjdp int t; 20577298Sobrien 20677298Sobrien /* This assumes the parser is not re-entered, 20733965Sjdp * which could happen if we add command substitution on PS1/PS2. 20833965Sjdp */ 20933965Sjdp parser_temp_free_all(); 21033965Sjdp heredoclist = NULL; 21133965Sjdp 21233965Sjdp tokpushback = 0; 21333965Sjdp doprompt = interact; 21433965Sjdp if (doprompt) 21577298Sobrien setprompt(1); 21633965Sjdp else 21733965Sjdp setprompt(0); 21877298Sobrien needprompt = 0; 21933965Sjdp t = readtoken(); 22033965Sjdp if (t == TEOF) 22133965Sjdp return NEOF; 22233965Sjdp if (t == TNL) 22333965Sjdp return NULL; 224130561Sobrien tokpushback++; 22533965Sjdp return list(1, 1); 22633965Sjdp} 22733965Sjdp 22833965Sjdp 22933965Sjdpstatic union node * 23033965Sjdplist(int nlflag, int erflag) 23133965Sjdp{ 23233965Sjdp union node *ntop, *n1, *n2, *n3; 23333965Sjdp int tok; 23477298Sobrien 23577298Sobrien checkkwd = CHKNL | CHKKWD | CHKALIAS; 23633965Sjdp if (!nlflag && !erflag && tokendlist[peektoken()]) 23777298Sobrien return NULL; 238130561Sobrien ntop = n1 = NULL; 23933965Sjdp for (;;) { 24033965Sjdp n2 = andor(); 24160484Sobrien tok = readtoken(); 24233965Sjdp if (tok == TBACKGND) { 24333965Sjdp if (n2->type == NCMD || n2->type == NPIPE) { 24433965Sjdp n2->ncmd.backgnd = 1; 24533965Sjdp } else if (n2->type == NREDIR) { 24633965Sjdp n2->type = NBACKGND; 24733965Sjdp } else { 24833965Sjdp n3 = (union node *)stalloc(sizeof (struct nredir)); 24933965Sjdp n3->type = NBACKGND; 25077298Sobrien n3->nredir.n = n2; 25133965Sjdp n3->nredir.redirect = NULL; 25233965Sjdp n2 = n3; 253130561Sobrien } 25433965Sjdp } 25533965Sjdp if (ntop == NULL) 25633965Sjdp ntop = n2; 25733965Sjdp else if (n1 == NULL) { 25833965Sjdp n1 = (union node *)stalloc(sizeof (struct nbinary)); 25933965Sjdp n1->type = NSEMI; 26033965Sjdp n1->nbinary.ch1 = ntop; 26133965Sjdp n1->nbinary.ch2 = n2; 26233965Sjdp ntop = n1; 263130561Sobrien } 26433965Sjdp else { 26533965Sjdp n3 = (union node *)stalloc(sizeof (struct nbinary)); 26677298Sobrien n3->type = NSEMI; 26733965Sjdp n3->nbinary.ch1 = n1->nbinary.ch2; 26833965Sjdp n3->nbinary.ch2 = n2; 26960484Sobrien n1->nbinary.ch2 = n3; 27060484Sobrien n1 = n3; 27160484Sobrien } 27260484Sobrien switch (tok) { 27360484Sobrien case TBACKGND: 27460484Sobrien case TSEMI: 27560484Sobrien tok = readtoken(); 27633965Sjdp /* FALLTHROUGH */ 27733965Sjdp case TNL: 27833965Sjdp if (tok == TNL) { 27960484Sobrien parseheredoc(); 28038889Sjdp if (nlflag) 28138889Sjdp return ntop; 28238889Sjdp } else if (tok == TEOF && nlflag) { 28338889Sjdp parseheredoc(); 28438889Sjdp return ntop; 285218822Sdim } else { 28633965Sjdp tokpushback++; 28733965Sjdp } 28833965Sjdp checkkwd = CHKNL | CHKKWD | CHKALIAS; 28933965Sjdp if (!nlflag && !erflag && tokendlist[peektoken()]) 29033965Sjdp return ntop; 29133965Sjdp break; 29233965Sjdp case TEOF: 29333965Sjdp if (heredoclist) 29433965Sjdp parseheredoc(); 295130561Sobrien else 29633965Sjdp pungetc(); /* push back EOF on input */ 29733965Sjdp return ntop; 29833965Sjdp default: 29933965Sjdp if (nlflag || erflag) 30033965Sjdp synexpect(-1); 301130561Sobrien tokpushback++; 30233965Sjdp return ntop; 30377298Sobrien } 30433965Sjdp } 30533965Sjdp} 30633965Sjdp 30733965Sjdp 30833965Sjdp 30933965Sjdpstatic union node * 31077298Sobrienandor(void) 31177298Sobrien{ 31277298Sobrien union node *n1, *n2, *n3; 31377298Sobrien int t; 31460484Sobrien 31577298Sobrien n1 = pipeline(); 31677298Sobrien for (;;) { 31777298Sobrien if ((t = readtoken()) == TAND) { 31860484Sobrien t = NAND; 31977298Sobrien } else if (t == TOR) { 32077298Sobrien t = NOR; 32133965Sjdp } else { 32233965Sjdp tokpushback++; 32333965Sjdp return n1; 32433965Sjdp } 32533965Sjdp n2 = pipeline(); 32633965Sjdp n3 = (union node *)stalloc(sizeof (struct nbinary)); 32733965Sjdp n3->type = t; 32833965Sjdp n3->nbinary.ch1 = n1; 32933965Sjdp n3->nbinary.ch2 = n2; 33033965Sjdp n1 = n3; 33133965Sjdp } 33233965Sjdp} 33333965Sjdp 33433965Sjdp 33533965Sjdp 33633965Sjdpstatic union node * 33733965Sjdppipeline(void) 33833965Sjdp{ 33933965Sjdp union node *n1, *n2, *pipenode; 34033965Sjdp struct nodelist *lp, *prev; 34133965Sjdp int negate, t; 34233965Sjdp 34333965Sjdp negate = 0; 34433965Sjdp checkkwd = CHKNL | CHKKWD | CHKALIAS; 34533965Sjdp TRACE(("pipeline: entered\n")); 34633965Sjdp while (readtoken() == TNOT) 34777298Sobrien negate = !negate; 348218822Sdim tokpushback++; 349218822Sdim n1 = command(); 350218822Sdim if (readtoken() == TPIPE) { 35133965Sjdp pipenode = (union node *)stalloc(sizeof (struct npipe)); 35233965Sjdp pipenode->type = NPIPE; 35333965Sjdp pipenode->npipe.backgnd = 0; 35433965Sjdp lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 35533965Sjdp pipenode->npipe.cmdlist = lp; 35633965Sjdp lp->n = n1; 35733965Sjdp do { 35833965Sjdp prev = lp; 35933965Sjdp lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 36033965Sjdp checkkwd = CHKNL | CHKKWD | CHKALIAS; 36133965Sjdp t = readtoken(); 36233965Sjdp tokpushback++; 36333965Sjdp if (t == TNOT) 36433965Sjdp lp->n = pipeline(); 36533965Sjdp else 36633965Sjdp lp->n = command(); 36733965Sjdp prev->next = lp; 36833965Sjdp } while (readtoken() == TPIPE); 36960484Sobrien lp->next = NULL; 37033965Sjdp n1 = pipenode; 37133965Sjdp } 37233965Sjdp tokpushback++; 37333965Sjdp if (negate) { 37433965Sjdp n2 = (union node *)stalloc(sizeof (struct nnot)); 37533965Sjdp n2->type = NNOT; 376218822Sdim n2->nnot.com = n1; 377218822Sdim return n2; 378218822Sdim } else 37933965Sjdp return n1; 38033965Sjdp} 38133965Sjdp 38233965Sjdp 38333965Sjdp 38433965Sjdpstatic union node * 38533965Sjdpcommand(void) 38633965Sjdp{ 38733965Sjdp union node *n1, *n2; 38833965Sjdp union node *ap, **app; 38933965Sjdp union node *cp, **cpp; 39033965Sjdp union node *redir, **rpp; 39133965Sjdp int t; 39233965Sjdp int is_subshell; 39389857Sobrien 39433965Sjdp checkkwd = CHKNL | CHKKWD | CHKALIAS; 39560484Sobrien is_subshell = 0; 39660484Sobrien redir = NULL; 39760484Sobrien n1 = NULL; 39860484Sobrien rpp = &redir; 39977298Sobrien 40033965Sjdp /* Check for redirection which may precede command */ 40133965Sjdp while (readtoken() == TREDIR) { 40233965Sjdp *rpp = n2 = redirnode; 40377298Sobrien rpp = &n2->nfile.next; 40433965Sjdp parsefname(); 40533965Sjdp } 40633965Sjdp tokpushback++; 40777298Sobrien 40833965Sjdp switch (readtoken()) { 40977298Sobrien case TIF: 41077298Sobrien n1 = (union node *)stalloc(sizeof (struct nif)); 41133965Sjdp n1->type = NIF; 41233965Sjdp if ((n1->nif.test = list(0, 0)) == NULL) 413130561Sobrien synexpect(-1); 41433965Sjdp if (readtoken() != TTHEN) 41533965Sjdp synexpect(TTHEN); 41633965Sjdp n1->nif.ifpart = list(0, 0); 41733965Sjdp n2 = n1; 41833965Sjdp while (readtoken() == TELIF) { 419130561Sobrien n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 42033965Sjdp n2 = n2->nif.elsepart; 42133965Sjdp n2->type = NIF; 42233965Sjdp if ((n2->nif.test = list(0, 0)) == NULL) 42333965Sjdp synexpect(-1); 42433965Sjdp if (readtoken() != TTHEN) 42533965Sjdp synexpect(TTHEN); 42633965Sjdp n2->nif.ifpart = list(0, 0); 42733965Sjdp } 42833965Sjdp if (lasttoken == TELSE) 42977298Sobrien n2->nif.elsepart = list(0, 0); 43077298Sobrien else { 43177298Sobrien n2->nif.elsepart = NULL; 43277298Sobrien tokpushback++; 43377298Sobrien } 43477298Sobrien if (readtoken() != TFI) 43577298Sobrien synexpect(TFI); 43677298Sobrien checkkwd = CHKKWD | CHKALIAS; 43738889Sjdp break; 438218822Sdim case TWHILE: 439218822Sdim case TUNTIL: { 440218822Sdim int got; 44133965Sjdp n1 = (union node *)stalloc(sizeof (struct nbinary)); 442218822Sdim n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 443218822Sdim if ((n1->nbinary.ch1 = list(0, 0)) == NULL) 444218822Sdim synexpect(-1); 445218822Sdim if ((got=readtoken()) != TDO) { 446218822SdimTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 447218822Sdim synexpect(TDO); 448218822Sdim } 449218822Sdim n1->nbinary.ch2 = list(0, 0); 450218822Sdim if (readtoken() != TDONE) 451218822Sdim synexpect(TDONE); 452218822Sdim checkkwd = CHKKWD | CHKALIAS; 453218822Sdim break; 454218822Sdim } 455218822Sdim case TFOR: 456218822Sdim if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 457218822Sdim synerror("Bad for loop variable"); 45833965Sjdp n1 = (union node *)stalloc(sizeof (struct nfor)); 45933965Sjdp n1->type = NFOR; 460218822Sdim n1->nfor.var = wordtext; 461218822Sdim while (readtoken() == TNL) 462218822Sdim ; 463218822Sdim if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) { 464218822Sdim app = ≈ 465218822Sdim while (readtoken() == TWORD) { 46638889Sjdp n2 = (union node *)stalloc(sizeof (struct narg)); 46738889Sjdp n2->type = NARG; 46838889Sjdp n2->narg.text = wordtext; 46938889Sjdp n2->narg.backquote = backquotelist; 47038889Sjdp *app = n2; 47138889Sjdp app = &n2->narg.next; 47238889Sjdp } 47338889Sjdp *app = NULL; 47438889Sjdp n1->nfor.args = ap; 47538889Sjdp if (lasttoken != TNL && lasttoken != TSEMI) 47677298Sobrien synexpect(-1); 477218822Sdim } else { 478218822Sdim static char argvars[5] = { 479218822Sdim CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' 480218822Sdim }; 481218822Sdim n2 = (union node *)stalloc(sizeof (struct narg)); 482218822Sdim n2->type = NARG; 483218822Sdim n2->narg.text = argvars; 48433965Sjdp n2->narg.backquote = NULL; 48577298Sobrien n2->narg.next = NULL; 48677298Sobrien n1->nfor.args = n2; 48777298Sobrien /* 48877298Sobrien * Newline or semicolon here is optional (but note 48977298Sobrien * that the original Bourne shell only allowed NL). 490130561Sobrien */ 49133965Sjdp if (lasttoken != TNL && lasttoken != TSEMI) 49233965Sjdp tokpushback++; 49333965Sjdp } 49433965Sjdp checkkwd = CHKNL | CHKKWD | CHKALIAS; 49533965Sjdp if ((t = readtoken()) == TDO) 49633965Sjdp t = TDONE; 49733965Sjdp else if (t == TBEGIN) 49833965Sjdp t = TEND; 49933965Sjdp else 50033965Sjdp synexpect(-1); 50133965Sjdp n1->nfor.body = list(0, 0); 50233965Sjdp if (readtoken() != t) 50333965Sjdp synexpect(t); 50433965Sjdp checkkwd = CHKKWD | CHKALIAS; 50533965Sjdp break; 50633965Sjdp case TCASE: 50733965Sjdp n1 = (union node *)stalloc(sizeof (struct ncase)); 50833965Sjdp n1->type = NCASE; 50933965Sjdp if (readtoken() != TWORD) 51033965Sjdp synexpect(TWORD); 51177298Sobrien n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 512 n2->type = NARG; 513 n2->narg.text = wordtext; 514 n2->narg.backquote = backquotelist; 515 n2->narg.next = NULL; 516 while (readtoken() == TNL); 517 if (lasttoken != TWORD || ! equal(wordtext, "in")) 518 synerror("expecting \"in\""); 519 cpp = &n1->ncase.cases; 520 checkkwd = CHKNL | CHKKWD, readtoken(); 521 while (lasttoken != TESAC) { 522 *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 523 cp->type = NCLIST; 524 app = &cp->nclist.pattern; 525 if (lasttoken == TLP) 526 readtoken(); 527 for (;;) { 528 *app = ap = (union node *)stalloc(sizeof (struct narg)); 529 ap->type = NARG; 530 ap->narg.text = wordtext; 531 ap->narg.backquote = backquotelist; 532 checkkwd = CHKNL | CHKKWD; 533 if (readtoken() != TPIPE) 534 break; 535 app = &ap->narg.next; 536 readtoken(); 537 } 538 ap->narg.next = NULL; 539 if (lasttoken != TRP) 540 synexpect(TRP); 541 cp->nclist.body = list(0, 0); 542 543 checkkwd = CHKNL | CHKKWD | CHKALIAS; 544 if ((t = readtoken()) != TESAC) { 545 if (t != TENDCASE) 546 synexpect(TENDCASE); 547 else 548 checkkwd = CHKNL | CHKKWD, readtoken(); 549 } 550 cpp = &cp->nclist.next; 551 } 552 *cpp = NULL; 553 checkkwd = CHKKWD | CHKALIAS; 554 break; 555 case TLP: 556 n1 = (union node *)stalloc(sizeof (struct nredir)); 557 n1->type = NSUBSHELL; 558 n1->nredir.n = list(0, 0); 559 n1->nredir.redirect = NULL; 560 if (readtoken() != TRP) 561 synexpect(TRP); 562 checkkwd = CHKKWD | CHKALIAS; 563 is_subshell = 1; 564 break; 565 case TBEGIN: 566 n1 = list(0, 0); 567 if (readtoken() != TEND) 568 synexpect(TEND); 569 checkkwd = CHKKWD | CHKALIAS; 570 break; 571 /* Handle an empty command like other simple commands. */ 572 case TBACKGND: 573 case TSEMI: 574 case TAND: 575 case TOR: 576 /* 577 * An empty command before a ; doesn't make much sense, and 578 * should certainly be disallowed in the case of `if ;'. 579 */ 580 if (!redir) 581 synexpect(-1); 582 case TNL: 583 case TEOF: 584 case TWORD: 585 case TRP: 586 tokpushback++; 587 n1 = simplecmd(rpp, redir); 588 return n1; 589 default: 590 synexpect(-1); 591 } 592 593 /* Now check for redirection which may follow command */ 594 while (readtoken() == TREDIR) { 595 *rpp = n2 = redirnode; 596 rpp = &n2->nfile.next; 597 parsefname(); 598 } 599 tokpushback++; 600 *rpp = NULL; 601 if (redir) { 602 if (!is_subshell) { 603 n2 = (union node *)stalloc(sizeof (struct nredir)); 604 n2->type = NREDIR; 605 n2->nredir.n = n1; 606 n1 = n2; 607 } 608 n1->nredir.redirect = redir; 609 } 610 611 return n1; 612} 613 614 615static union node * 616simplecmd(union node **rpp, union node *redir) 617{ 618 union node *args, **app; 619 union node **orig_rpp = rpp; 620 union node *n = NULL; 621 int special; 622 int savecheckkwd; 623 624 /* If we don't have any redirections already, then we must reset */ 625 /* rpp to be the address of the local redir variable. */ 626 if (redir == 0) 627 rpp = &redir; 628 629 args = NULL; 630 app = &args; 631 /* 632 * We save the incoming value, because we need this for shell 633 * functions. There can not be a redirect or an argument between 634 * the function name and the open parenthesis. 635 */ 636 orig_rpp = rpp; 637 638 savecheckkwd = CHKALIAS; 639 640 for (;;) { 641 checkkwd = savecheckkwd; 642 if (readtoken() == TWORD) { 643 n = (union node *)stalloc(sizeof (struct narg)); 644 n->type = NARG; 645 n->narg.text = wordtext; 646 n->narg.backquote = backquotelist; 647 *app = n; 648 app = &n->narg.next; 649 if (savecheckkwd != 0 && !isassignment(wordtext)) 650 savecheckkwd = 0; 651 } else if (lasttoken == TREDIR) { 652 *rpp = n = redirnode; 653 rpp = &n->nfile.next; 654 parsefname(); /* read name of redirection file */ 655 } else if (lasttoken == TLP && app == &args->narg.next 656 && rpp == orig_rpp) { 657 /* We have a function */ 658 if (readtoken() != TRP) 659 synexpect(TRP); 660 funclinno = plinno; 661 /* 662 * - Require plain text. 663 * - Functions with '/' cannot be called. 664 * - Reject name=(). 665 * - Reject ksh extended glob patterns. 666 */ 667 if (!noexpand(n->narg.text) || quoteflag || 668 strchr(n->narg.text, '/') || 669 strchr("!%*+-=?@}~", 670 n->narg.text[strlen(n->narg.text) - 1])) 671 synerror("Bad function name"); 672 rmescapes(n->narg.text); 673 if (find_builtin(n->narg.text, &special) >= 0 && 674 special) 675 synerror("Cannot override a special builtin with a function"); 676 n->type = NDEFUN; 677 n->narg.next = command(); 678 funclinno = 0; 679 return n; 680 } else { 681 tokpushback++; 682 break; 683 } 684 } 685 *app = NULL; 686 *rpp = NULL; 687 n = (union node *)stalloc(sizeof (struct ncmd)); 688 n->type = NCMD; 689 n->ncmd.backgnd = 0; 690 n->ncmd.args = args; 691 n->ncmd.redirect = redir; 692 return n; 693} 694 695static union node * 696makename(void) 697{ 698 union node *n; 699 700 n = (union node *)stalloc(sizeof (struct narg)); 701 n->type = NARG; 702 n->narg.next = NULL; 703 n->narg.text = wordtext; 704 n->narg.backquote = backquotelist; 705 return n; 706} 707 708void 709fixredir(union node *n, const char *text, int err) 710{ 711 TRACE(("Fix redir %s %d\n", text, err)); 712 if (!err) 713 n->ndup.vname = NULL; 714 715 if (is_digit(text[0]) && text[1] == '\0') 716 n->ndup.dupfd = digit_val(text[0]); 717 else if (text[0] == '-' && text[1] == '\0') 718 n->ndup.dupfd = -1; 719 else { 720 721 if (err) 722 synerror("Bad fd number"); 723 else 724 n->ndup.vname = makename(); 725 } 726} 727 728 729static void 730parsefname(void) 731{ 732 union node *n = redirnode; 733 734 if (readtoken() != TWORD) 735 synexpect(-1); 736 if (n->type == NHERE) { 737 struct heredoc *here = heredoc; 738 struct heredoc *p; 739 int i; 740 741 if (quoteflag == 0) 742 n->type = NXHERE; 743 TRACE(("Here document %d\n", n->type)); 744 if (here->striptabs) { 745 while (*wordtext == '\t') 746 wordtext++; 747 } 748 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 749 synerror("Illegal eof marker for << redirection"); 750 rmescapes(wordtext); 751 here->eofmark = wordtext; 752 here->next = NULL; 753 if (heredoclist == NULL) 754 heredoclist = here; 755 else { 756 for (p = heredoclist ; p->next ; p = p->next); 757 p->next = here; 758 } 759 } else if (n->type == NTOFD || n->type == NFROMFD) { 760 fixredir(n, wordtext, 0); 761 } else { 762 n->nfile.fname = makename(); 763 } 764} 765 766 767/* 768 * Input any here documents. 769 */ 770 771static void 772parseheredoc(void) 773{ 774 struct heredoc *here; 775 union node *n; 776 777 while (heredoclist) { 778 here = heredoclist; 779 heredoclist = here->next; 780 if (needprompt) { 781 setprompt(2); 782 needprompt = 0; 783 } 784 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 785 here->eofmark, here->striptabs); 786 n = (union node *)stalloc(sizeof (struct narg)); 787 n->narg.type = NARG; 788 n->narg.next = NULL; 789 n->narg.text = wordtext; 790 n->narg.backquote = backquotelist; 791 here->here->nhere.doc = n; 792 } 793} 794 795static int 796peektoken(void) 797{ 798 int t; 799 800 t = readtoken(); 801 tokpushback++; 802 return (t); 803} 804 805static int 806readtoken(void) 807{ 808 int t; 809 struct alias *ap; 810#ifdef DEBUG 811 int alreadyseen = tokpushback; 812#endif 813 814 top: 815 t = xxreadtoken(); 816 817 /* 818 * eat newlines 819 */ 820 if (checkkwd & CHKNL) { 821 while (t == TNL) { 822 parseheredoc(); 823 t = xxreadtoken(); 824 } 825 } 826 827 /* 828 * check for keywords and aliases 829 */ 830 if (t == TWORD && !quoteflag) 831 { 832 const char * const *pp; 833 834 if (checkkwd & CHKKWD) 835 for (pp = parsekwd; *pp; pp++) { 836 if (**pp == *wordtext && equal(*pp, wordtext)) 837 { 838 lasttoken = t = pp - parsekwd + KWDOFFSET; 839 TRACE(("keyword %s recognized\n", tokname[t])); 840 goto out; 841 } 842 } 843 if (checkkwd & CHKALIAS && 844 (ap = lookupalias(wordtext, 1)) != NULL) { 845 pushstring(ap->val, strlen(ap->val), ap); 846 goto top; 847 } 848 } 849out: 850 if (t != TNOT) 851 checkkwd = 0; 852 853#ifdef DEBUG 854 if (!alreadyseen) 855 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 856 else 857 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 858#endif 859 return (t); 860} 861 862 863/* 864 * Read the next input token. 865 * If the token is a word, we set backquotelist to the list of cmds in 866 * backquotes. We set quoteflag to true if any part of the word was 867 * quoted. 868 * If the token is TREDIR, then we set redirnode to a structure containing 869 * the redirection. 870 * In all cases, the variable startlinno is set to the number of the line 871 * on which the token starts. 872 * 873 * [Change comment: here documents and internal procedures] 874 * [Readtoken shouldn't have any arguments. Perhaps we should make the 875 * word parsing code into a separate routine. In this case, readtoken 876 * doesn't need to have any internal procedures, but parseword does. 877 * We could also make parseoperator in essence the main routine, and 878 * have parseword (readtoken1?) handle both words and redirection.] 879 */ 880 881#define RETURN(token) return lasttoken = token 882 883static int 884xxreadtoken(void) 885{ 886 int c; 887 888 if (tokpushback) { 889 tokpushback = 0; 890 return lasttoken; 891 } 892 if (needprompt) { 893 setprompt(2); 894 needprompt = 0; 895 } 896 startlinno = plinno; 897 for (;;) { /* until token or start of word found */ 898 c = pgetc_macro(); 899 switch (c) { 900 case ' ': case '\t': 901 continue; 902 case '#': 903 while ((c = pgetc()) != '\n' && c != PEOF); 904 pungetc(); 905 continue; 906 case '\\': 907 if (pgetc() == '\n') { 908 startlinno = ++plinno; 909 if (doprompt) 910 setprompt(2); 911 else 912 setprompt(0); 913 continue; 914 } 915 pungetc(); 916 goto breakloop; 917 case '\n': 918 plinno++; 919 needprompt = doprompt; 920 RETURN(TNL); 921 case PEOF: 922 RETURN(TEOF); 923 case '&': 924 if (pgetc() == '&') 925 RETURN(TAND); 926 pungetc(); 927 RETURN(TBACKGND); 928 case '|': 929 if (pgetc() == '|') 930 RETURN(TOR); 931 pungetc(); 932 RETURN(TPIPE); 933 case ';': 934 if (pgetc() == ';') 935 RETURN(TENDCASE); 936 pungetc(); 937 RETURN(TSEMI); 938 case '(': 939 RETURN(TLP); 940 case ')': 941 RETURN(TRP); 942 default: 943 goto breakloop; 944 } 945 } 946breakloop: 947 return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 948#undef RETURN 949} 950 951 952#define MAXNEST_static 8 953struct tokenstate 954{ 955 const char *syntax; /* *SYNTAX */ 956 int parenlevel; /* levels of parentheses in arithmetic */ 957 enum tokenstate_category 958 { 959 TSTATE_TOP, 960 TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */ 961 TSTATE_VAR_NEW, /* other ${var...}, own dquote state */ 962 TSTATE_ARITH 963 } category; 964}; 965 966 967/* 968 * Called to parse command substitutions. 969 */ 970 971static char * 972parsebackq(char *out, struct nodelist **pbqlist, 973 int oldstyle, int dblquote, int quoted) 974{ 975 struct nodelist **nlpp; 976 union node *n; 977 char *volatile str; 978 struct jmploc jmploc; 979 struct jmploc *const savehandler = handler; 980 int savelen; 981 int saveprompt; 982 const int bq_startlinno = plinno; 983 char *volatile ostr = NULL; 984 struct parsefile *const savetopfile = getcurrentfile(); 985 struct heredoc *const saveheredoclist = heredoclist; 986 struct heredoc *here; 987 988 str = NULL; 989 if (setjmp(jmploc.loc)) { 990 popfilesupto(savetopfile); 991 if (str) 992 ckfree(str); 993 if (ostr) 994 ckfree(ostr); 995 heredoclist = saveheredoclist; 996 handler = savehandler; 997 if (exception == EXERROR) { 998 startlinno = bq_startlinno; 999 synerror("Error in command substitution"); 1000 } 1001 longjmp(handler->loc, 1); 1002 } 1003 INTOFF; 1004 savelen = out - stackblock(); 1005 if (savelen > 0) { 1006 str = ckmalloc(savelen); 1007 memcpy(str, stackblock(), savelen); 1008 } 1009 handler = &jmploc; 1010 heredoclist = NULL; 1011 INTON; 1012 if (oldstyle) { 1013 /* We must read until the closing backquote, giving special 1014 treatment to some slashes, and then push the string and 1015 reread it as input, interpreting it normally. */ 1016 char *oout; 1017 int c; 1018 int olen; 1019 1020 1021 STARTSTACKSTR(oout); 1022 for (;;) { 1023 if (needprompt) { 1024 setprompt(2); 1025 needprompt = 0; 1026 } 1027 CHECKSTRSPACE(2, oout); 1028 switch (c = pgetc()) { 1029 case '`': 1030 goto done; 1031 1032 case '\\': 1033 if ((c = pgetc()) == '\n') { 1034 plinno++; 1035 if (doprompt) 1036 setprompt(2); 1037 else 1038 setprompt(0); 1039 /* 1040 * If eating a newline, avoid putting 1041 * the newline into the new character 1042 * stream (via the USTPUTC after the 1043 * switch). 1044 */ 1045 continue; 1046 } 1047 if (c != '\\' && c != '`' && c != '$' 1048 && (!dblquote || c != '"')) 1049 USTPUTC('\\', oout); 1050 break; 1051 1052 case '\n': 1053 plinno++; 1054 needprompt = doprompt; 1055 break; 1056 1057 case PEOF: 1058 startlinno = plinno; 1059 synerror("EOF in backquote substitution"); 1060 break; 1061 1062 default: 1063 break; 1064 } 1065 USTPUTC(c, oout); 1066 } 1067done: 1068 USTPUTC('\0', oout); 1069 olen = oout - stackblock(); 1070 INTOFF; 1071 ostr = ckmalloc(olen); 1072 memcpy(ostr, stackblock(), olen); 1073 setinputstring(ostr, 1); 1074 INTON; 1075 } 1076 nlpp = pbqlist; 1077 while (*nlpp) 1078 nlpp = &(*nlpp)->next; 1079 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 1080 (*nlpp)->next = NULL; 1081 1082 if (oldstyle) { 1083 saveprompt = doprompt; 1084 doprompt = 0; 1085 } 1086 1087 n = list(0, oldstyle); 1088 1089 if (oldstyle) 1090 doprompt = saveprompt; 1091 else { 1092 if (readtoken() != TRP) 1093 synexpect(TRP); 1094 } 1095 1096 (*nlpp)->n = n; 1097 if (oldstyle) { 1098 /* 1099 * Start reading from old file again, ignoring any pushed back 1100 * tokens left from the backquote parsing 1101 */ 1102 popfile(); 1103 tokpushback = 0; 1104 } 1105 STARTSTACKSTR(out); 1106 CHECKSTRSPACE(savelen + 1, out); 1107 INTOFF; 1108 if (str) { 1109 memcpy(out, str, savelen); 1110 STADJUST(savelen, out); 1111 ckfree(str); 1112 str = NULL; 1113 } 1114 if (ostr) { 1115 ckfree(ostr); 1116 ostr = NULL; 1117 } 1118 here = saveheredoclist; 1119 if (here != NULL) { 1120 while (here->next != NULL) 1121 here = here->next; 1122 here->next = heredoclist; 1123 heredoclist = saveheredoclist; 1124 } 1125 handler = savehandler; 1126 INTON; 1127 if (quoted) 1128 USTPUTC(CTLBACKQ | CTLQUOTE, out); 1129 else 1130 USTPUTC(CTLBACKQ, out); 1131 return out; 1132} 1133 1134 1135/* 1136 * Called to parse a backslash escape sequence inside $'...'. 1137 * The backslash has already been read. 1138 */ 1139static char * 1140readcstyleesc(char *out) 1141{ 1142 int c, v, i, n; 1143 1144 c = pgetc(); 1145 switch (c) { 1146 case '\0': 1147 synerror("Unterminated quoted string"); 1148 case '\n': 1149 plinno++; 1150 if (doprompt) 1151 setprompt(2); 1152 else 1153 setprompt(0); 1154 return out; 1155 case '\\': 1156 case '\'': 1157 case '"': 1158 v = c; 1159 break; 1160 case 'a': v = '\a'; break; 1161 case 'b': v = '\b'; break; 1162 case 'e': v = '\033'; break; 1163 case 'f': v = '\f'; break; 1164 case 'n': v = '\n'; break; 1165 case 'r': v = '\r'; break; 1166 case 't': v = '\t'; break; 1167 case 'v': v = '\v'; break; 1168 case 'x': 1169 v = 0; 1170 for (;;) { 1171 c = pgetc(); 1172 if (c >= '0' && c <= '9') 1173 v = (v << 4) + c - '0'; 1174 else if (c >= 'A' && c <= 'F') 1175 v = (v << 4) + c - 'A' + 10; 1176 else if (c >= 'a' && c <= 'f') 1177 v = (v << 4) + c - 'a' + 10; 1178 else 1179 break; 1180 } 1181 pungetc(); 1182 break; 1183 case '0': case '1': case '2': case '3': 1184 case '4': case '5': case '6': case '7': 1185 v = c - '0'; 1186 c = pgetc(); 1187 if (c >= '0' && c <= '7') { 1188 v <<= 3; 1189 v += c - '0'; 1190 c = pgetc(); 1191 if (c >= '0' && c <= '7') { 1192 v <<= 3; 1193 v += c - '0'; 1194 } else 1195 pungetc(); 1196 } else 1197 pungetc(); 1198 break; 1199 case 'c': 1200 c = pgetc(); 1201 if (c < 0x3f || c > 0x7a || c == 0x60) 1202 synerror("Bad escape sequence"); 1203 if (c == '\\' && pgetc() != '\\') 1204 synerror("Bad escape sequence"); 1205 if (c == '?') 1206 v = 127; 1207 else 1208 v = c & 0x1f; 1209 break; 1210 case 'u': 1211 case 'U': 1212 n = c == 'U' ? 8 : 4; 1213 v = 0; 1214 for (i = 0; i < n; i++) { 1215 c = pgetc(); 1216 if (c >= '0' && c <= '9') 1217 v = (v << 4) + c - '0'; 1218 else if (c >= 'A' && c <= 'F') 1219 v = (v << 4) + c - 'A' + 10; 1220 else if (c >= 'a' && c <= 'f') 1221 v = (v << 4) + c - 'a' + 10; 1222 else 1223 synerror("Bad escape sequence"); 1224 } 1225 if (v == 0 || (v >= 0xd800 && v <= 0xdfff)) 1226 synerror("Bad escape sequence"); 1227 /* We really need iconv here. */ 1228 if (initial_localeisutf8 && v > 127) { 1229 CHECKSTRSPACE(4, out); 1230 /* 1231 * We cannot use wctomb() as the locale may have 1232 * changed. 1233 */ 1234 if (v <= 0x7ff) { 1235 USTPUTC(0xc0 | v >> 6, out); 1236 USTPUTC(0x80 | (v & 0x3f), out); 1237 return out; 1238 } else if (v <= 0xffff) { 1239 USTPUTC(0xe0 | v >> 12, out); 1240 USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1241 USTPUTC(0x80 | (v & 0x3f), out); 1242 return out; 1243 } else if (v <= 0x10ffff) { 1244 USTPUTC(0xf0 | v >> 18, out); 1245 USTPUTC(0x80 | ((v >> 12) & 0x3f), out); 1246 USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1247 USTPUTC(0x80 | (v & 0x3f), out); 1248 return out; 1249 } 1250 } 1251 if (v > 127) 1252 v = '?'; 1253 break; 1254 default: 1255 synerror("Bad escape sequence"); 1256 } 1257 v = (char)v; 1258 /* 1259 * We can't handle NUL bytes. 1260 * POSIX says we should skip till the closing quote. 1261 */ 1262 if (v == '\0') { 1263 while ((c = pgetc()) != '\'') { 1264 if (c == '\\') 1265 c = pgetc(); 1266 if (c == PEOF) 1267 synerror("Unterminated quoted string"); 1268 } 1269 pungetc(); 1270 return out; 1271 } 1272 if (SQSYNTAX[v] == CCTL) 1273 USTPUTC(CTLESC, out); 1274 USTPUTC(v, out); 1275 return out; 1276} 1277 1278 1279/* 1280 * If eofmark is NULL, read a word or a redirection symbol. If eofmark 1281 * is not NULL, read a here document. In the latter case, eofmark is the 1282 * word which marks the end of the document and striptabs is true if 1283 * leading tabs should be stripped from the document. The argument firstc 1284 * is the first character of the input token or document. 1285 * 1286 * Because C does not have internal subroutines, I have simulated them 1287 * using goto's to implement the subroutine linkage. The following macros 1288 * will run code that appears at the end of readtoken1. 1289 */ 1290 1291#define CHECKEND() {goto checkend; checkend_return:;} 1292#define PARSEREDIR() {goto parseredir; parseredir_return:;} 1293#define PARSESUB() {goto parsesub; parsesub_return:;} 1294#define PARSEARITH() {goto parsearith; parsearith_return:;} 1295 1296static int 1297readtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs) 1298{ 1299 int c = firstc; 1300 char *out; 1301 int len; 1302 char line[EOFMARKLEN + 1]; 1303 struct nodelist *bqlist; 1304 int quotef; 1305 int newvarnest; 1306 int level; 1307 int synentry; 1308 struct tokenstate state_static[MAXNEST_static]; 1309 int maxnest = MAXNEST_static; 1310 struct tokenstate *state = state_static; 1311 int sqiscstyle = 0; 1312 1313 startlinno = plinno; 1314 quotef = 0; 1315 bqlist = NULL; 1316 newvarnest = 0; 1317 level = 0; 1318 state[level].syntax = initialsyntax; 1319 state[level].parenlevel = 0; 1320 state[level].category = TSTATE_TOP; 1321 1322 STARTSTACKSTR(out); 1323 loop: { /* for each line, until end of word */ 1324 CHECKEND(); /* set c to PEOF if at end of here document */ 1325 for (;;) { /* until end of line or end of word */ 1326 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ 1327 1328 synentry = state[level].syntax[c]; 1329 1330 switch(synentry) { 1331 case CNL: /* '\n' */ 1332 if (state[level].syntax == BASESYNTAX) 1333 goto endword; /* exit outer loop */ 1334 USTPUTC(c, out); 1335 plinno++; 1336 if (doprompt) 1337 setprompt(2); 1338 else 1339 setprompt(0); 1340 c = pgetc(); 1341 goto loop; /* continue outer loop */ 1342 case CSBACK: 1343 if (sqiscstyle) { 1344 out = readcstyleesc(out); 1345 break; 1346 } 1347 /* FALLTHROUGH */ 1348 case CWORD: 1349 USTPUTC(c, out); 1350 break; 1351 case CCTL: 1352 if (eofmark == NULL || initialsyntax != SQSYNTAX) 1353 USTPUTC(CTLESC, out); 1354 USTPUTC(c, out); 1355 break; 1356 case CBACK: /* backslash */ 1357 c = pgetc(); 1358 if (c == PEOF) { 1359 USTPUTC('\\', out); 1360 pungetc(); 1361 } else if (c == '\n') { 1362 plinno++; 1363 if (doprompt) 1364 setprompt(2); 1365 else 1366 setprompt(0); 1367 } else { 1368 if (state[level].syntax == DQSYNTAX && 1369 c != '\\' && c != '`' && c != '$' && 1370 (c != '"' || (eofmark != NULL && 1371 newvarnest == 0)) && 1372 (c != '}' || state[level].category != TSTATE_VAR_OLD)) 1373 USTPUTC('\\', out); 1374 if ((eofmark == NULL || 1375 newvarnest > 0) && 1376 state[level].syntax == BASESYNTAX) 1377 USTPUTC(CTLQUOTEMARK, out); 1378 if (SQSYNTAX[c] == CCTL) 1379 USTPUTC(CTLESC, out); 1380 USTPUTC(c, out); 1381 if ((eofmark == NULL || 1382 newvarnest > 0) && 1383 state[level].syntax == BASESYNTAX && 1384 state[level].category == TSTATE_VAR_OLD) 1385 USTPUTC(CTLQUOTEEND, out); 1386 quotef++; 1387 } 1388 break; 1389 case CSQUOTE: 1390 USTPUTC(CTLQUOTEMARK, out); 1391 state[level].syntax = SQSYNTAX; 1392 sqiscstyle = 0; 1393 break; 1394 case CDQUOTE: 1395 USTPUTC(CTLQUOTEMARK, out); 1396 state[level].syntax = DQSYNTAX; 1397 break; 1398 case CENDQUOTE: 1399 if (eofmark != NULL && newvarnest == 0) 1400 USTPUTC(c, out); 1401 else { 1402 if (state[level].category == TSTATE_VAR_OLD) 1403 USTPUTC(CTLQUOTEEND, out); 1404 state[level].syntax = BASESYNTAX; 1405 quotef++; 1406 } 1407 break; 1408 case CVAR: /* '$' */ 1409 PARSESUB(); /* parse substitution */ 1410 break; 1411 case CENDVAR: /* '}' */ 1412 if (level > 0 && 1413 ((state[level].category == TSTATE_VAR_OLD && 1414 state[level].syntax == 1415 state[level - 1].syntax) || 1416 (state[level].category == TSTATE_VAR_NEW && 1417 state[level].syntax == BASESYNTAX))) { 1418 if (state[level].category == TSTATE_VAR_NEW) 1419 newvarnest--; 1420 level--; 1421 USTPUTC(CTLENDVAR, out); 1422 } else { 1423 USTPUTC(c, out); 1424 } 1425 break; 1426 case CLP: /* '(' in arithmetic */ 1427 state[level].parenlevel++; 1428 USTPUTC(c, out); 1429 break; 1430 case CRP: /* ')' in arithmetic */ 1431 if (state[level].parenlevel > 0) { 1432 USTPUTC(c, out); 1433 --state[level].parenlevel; 1434 } else { 1435 if (pgetc() == ')') { 1436 if (level > 0 && 1437 state[level].category == TSTATE_ARITH) { 1438 level--; 1439 USTPUTC(CTLENDARI, out); 1440 } else 1441 USTPUTC(')', out); 1442 } else { 1443 /* 1444 * unbalanced parens 1445 * (don't 2nd guess - no error) 1446 */ 1447 pungetc(); 1448 USTPUTC(')', out); 1449 } 1450 } 1451 break; 1452 case CBQUOTE: /* '`' */ 1453 out = parsebackq(out, &bqlist, 1, 1454 state[level].syntax == DQSYNTAX && 1455 (eofmark == NULL || newvarnest > 0), 1456 state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX); 1457 break; 1458 case CEOF: 1459 goto endword; /* exit outer loop */ 1460 case CIGN: 1461 break; 1462 default: 1463 if (level == 0) 1464 goto endword; /* exit outer loop */ 1465 USTPUTC(c, out); 1466 } 1467 c = pgetc_macro(); 1468 } 1469 } 1470endword: 1471 if (state[level].syntax == ARISYNTAX) 1472 synerror("Missing '))'"); 1473 if (state[level].syntax != BASESYNTAX && eofmark == NULL) 1474 synerror("Unterminated quoted string"); 1475 if (state[level].category == TSTATE_VAR_OLD || 1476 state[level].category == TSTATE_VAR_NEW) { 1477 startlinno = plinno; 1478 synerror("Missing '}'"); 1479 } 1480 if (state != state_static) 1481 parser_temp_free_upto(state); 1482 USTPUTC('\0', out); 1483 len = out - stackblock(); 1484 out = stackblock(); 1485 if (eofmark == NULL) { 1486 if ((c == '>' || c == '<') 1487 && quotef == 0 1488 && len <= 2 1489 && (*out == '\0' || is_digit(*out))) { 1490 PARSEREDIR(); 1491 return lasttoken = TREDIR; 1492 } else { 1493 pungetc(); 1494 } 1495 } 1496 quoteflag = quotef; 1497 backquotelist = bqlist; 1498 grabstackblock(len); 1499 wordtext = out; 1500 return lasttoken = TWORD; 1501/* end of readtoken routine */ 1502 1503 1504/* 1505 * Check to see whether we are at the end of the here document. When this 1506 * is called, c is set to the first character of the next input line. If 1507 * we are at the end of the here document, this routine sets the c to PEOF. 1508 */ 1509 1510checkend: { 1511 if (eofmark) { 1512 if (striptabs) { 1513 while (c == '\t') 1514 c = pgetc(); 1515 } 1516 if (c == *eofmark) { 1517 if (pfgets(line, sizeof line) != NULL) { 1518 char *p, *q; 1519 1520 p = line; 1521 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 1522 if ((*p == '\0' || *p == '\n') && *q == '\0') { 1523 c = PEOF; 1524 if (*p == '\n') { 1525 plinno++; 1526 needprompt = doprompt; 1527 } 1528 } else { 1529 pushstring(line, strlen(line), NULL); 1530 } 1531 } 1532 } 1533 } 1534 goto checkend_return; 1535} 1536 1537 1538/* 1539 * Parse a redirection operator. The variable "out" points to a string 1540 * specifying the fd to be redirected. The variable "c" contains the 1541 * first character of the redirection operator. 1542 */ 1543 1544parseredir: { 1545 char fd = *out; 1546 union node *np; 1547 1548 np = (union node *)stalloc(sizeof (struct nfile)); 1549 if (c == '>') { 1550 np->nfile.fd = 1; 1551 c = pgetc(); 1552 if (c == '>') 1553 np->type = NAPPEND; 1554 else if (c == '&') 1555 np->type = NTOFD; 1556 else if (c == '|') 1557 np->type = NCLOBBER; 1558 else { 1559 np->type = NTO; 1560 pungetc(); 1561 } 1562 } else { /* c == '<' */ 1563 np->nfile.fd = 0; 1564 c = pgetc(); 1565 if (c == '<') { 1566 if (sizeof (struct nfile) != sizeof (struct nhere)) { 1567 np = (union node *)stalloc(sizeof (struct nhere)); 1568 np->nfile.fd = 0; 1569 } 1570 np->type = NHERE; 1571 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 1572 heredoc->here = np; 1573 if ((c = pgetc()) == '-') { 1574 heredoc->striptabs = 1; 1575 } else { 1576 heredoc->striptabs = 0; 1577 pungetc(); 1578 } 1579 } else if (c == '&') 1580 np->type = NFROMFD; 1581 else if (c == '>') 1582 np->type = NFROMTO; 1583 else { 1584 np->type = NFROM; 1585 pungetc(); 1586 } 1587 } 1588 if (fd != '\0') 1589 np->nfile.fd = digit_val(fd); 1590 redirnode = np; 1591 goto parseredir_return; 1592} 1593 1594 1595/* 1596 * Parse a substitution. At this point, we have read the dollar sign 1597 * and nothing else. 1598 */ 1599 1600parsesub: { 1601 char buf[10]; 1602 int subtype; 1603 int typeloc; 1604 int flags; 1605 char *p; 1606 static const char types[] = "}-+?="; 1607 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 1608 int linno; 1609 int length; 1610 int c1; 1611 1612 c = pgetc(); 1613 if (c == '(') { /* $(command) or $((arith)) */ 1614 if (pgetc() == '(') { 1615 PARSEARITH(); 1616 } else { 1617 pungetc(); 1618 out = parsebackq(out, &bqlist, 0, 1619 state[level].syntax == DQSYNTAX && 1620 (eofmark == NULL || newvarnest > 0), 1621 state[level].syntax == DQSYNTAX || 1622 state[level].syntax == ARISYNTAX); 1623 } 1624 } else if (c == '{' || is_name(c) || is_special(c)) { 1625 USTPUTC(CTLVAR, out); 1626 typeloc = out - stackblock(); 1627 USTPUTC(VSNORMAL, out); 1628 subtype = VSNORMAL; 1629 flags = 0; 1630 if (c == '{') { 1631 bracketed_name = 1; 1632 c = pgetc(); 1633 subtype = 0; 1634 } 1635varname: 1636 if (!is_eof(c) && is_name(c)) { 1637 length = 0; 1638 do { 1639 STPUTC(c, out); 1640 c = pgetc(); 1641 length++; 1642 } while (!is_eof(c) && is_in_name(c)); 1643 if (length == 6 && 1644 strncmp(out - length, "LINENO", length) == 0) { 1645 /* Replace the variable name with the 1646 * current line number. */ 1647 linno = plinno; 1648 if (funclinno != 0) 1649 linno -= funclinno - 1; 1650 snprintf(buf, sizeof(buf), "%d", linno); 1651 STADJUST(-6, out); 1652 STPUTS(buf, out); 1653 flags |= VSLINENO; 1654 } 1655 } else if (is_digit(c)) { 1656 if (bracketed_name) { 1657 do { 1658 STPUTC(c, out); 1659 c = pgetc(); 1660 } while (is_digit(c)); 1661 } else { 1662 STPUTC(c, out); 1663 c = pgetc(); 1664 } 1665 } else if (is_special(c)) { 1666 c1 = c; 1667 c = pgetc(); 1668 if (subtype == 0 && c1 == '#') { 1669 subtype = VSLENGTH; 1670 if (strchr(types, c) == NULL && c != ':' && 1671 c != '#' && c != '%') 1672 goto varname; 1673 c1 = c; 1674 c = pgetc(); 1675 if (c1 != '}' && c == '}') { 1676 pungetc(); 1677 c = c1; 1678 goto varname; 1679 } 1680 pungetc(); 1681 c = c1; 1682 c1 = '#'; 1683 subtype = 0; 1684 } 1685 USTPUTC(c1, out); 1686 } else { 1687 subtype = VSERROR; 1688 if (c == '}') 1689 pungetc(); 1690 else if (c == '\n' || c == PEOF) 1691 synerror("Unexpected end of line in substitution"); 1692 else 1693 USTPUTC(c, out); 1694 } 1695 if (subtype == 0) { 1696 switch (c) { 1697 case ':': 1698 flags |= VSNUL; 1699 c = pgetc(); 1700 /*FALLTHROUGH*/ 1701 default: 1702 p = strchr(types, c); 1703 if (p == NULL) { 1704 if (c == '\n' || c == PEOF) 1705 synerror("Unexpected end of line in substitution"); 1706 if (flags == VSNUL) 1707 STPUTC(':', out); 1708 STPUTC(c, out); 1709 subtype = VSERROR; 1710 } else 1711 subtype = p - types + VSNORMAL; 1712 break; 1713 case '%': 1714 case '#': 1715 { 1716 int cc = c; 1717 subtype = c == '#' ? VSTRIMLEFT : 1718 VSTRIMRIGHT; 1719 c = pgetc(); 1720 if (c == cc) 1721 subtype++; 1722 else 1723 pungetc(); 1724 break; 1725 } 1726 } 1727 } else if (subtype != VSERROR) { 1728 if (subtype == VSLENGTH && c != '}') 1729 subtype = VSERROR; 1730 pungetc(); 1731 } 1732 STPUTC('=', out); 1733 if (state[level].syntax == DQSYNTAX || 1734 state[level].syntax == ARISYNTAX) 1735 flags |= VSQUOTE; 1736 *(stackblock() + typeloc) = subtype | flags; 1737 if (subtype != VSNORMAL) { 1738 if (level + 1 >= maxnest) { 1739 maxnest *= 2; 1740 if (state == state_static) { 1741 state = parser_temp_alloc( 1742 maxnest * sizeof(*state)); 1743 memcpy(state, state_static, 1744 MAXNEST_static * sizeof(*state)); 1745 } else 1746 state = parser_temp_realloc(state, 1747 maxnest * sizeof(*state)); 1748 } 1749 level++; 1750 state[level].parenlevel = 0; 1751 if (subtype == VSMINUS || subtype == VSPLUS || 1752 subtype == VSQUESTION || subtype == VSASSIGN) { 1753 /* 1754 * For operators that were in the Bourne shell, 1755 * inherit the double-quote state. 1756 */ 1757 state[level].syntax = state[level - 1].syntax; 1758 state[level].category = TSTATE_VAR_OLD; 1759 } else { 1760 /* 1761 * The other operators take a pattern, 1762 * so go to BASESYNTAX. 1763 * Also, ' and " are now special, even 1764 * in here documents. 1765 */ 1766 state[level].syntax = BASESYNTAX; 1767 state[level].category = TSTATE_VAR_NEW; 1768 newvarnest++; 1769 } 1770 } 1771 } else if (c == '\'' && state[level].syntax == BASESYNTAX) { 1772 /* $'cstylequotes' */ 1773 USTPUTC(CTLQUOTEMARK, out); 1774 state[level].syntax = SQSYNTAX; 1775 sqiscstyle = 1; 1776 } else { 1777 USTPUTC('$', out); 1778 pungetc(); 1779 } 1780 goto parsesub_return; 1781} 1782 1783 1784/* 1785 * Parse an arithmetic expansion (indicate start of one and set state) 1786 */ 1787parsearith: { 1788 1789 if (level + 1 >= maxnest) { 1790 maxnest *= 2; 1791 if (state == state_static) { 1792 state = parser_temp_alloc( 1793 maxnest * sizeof(*state)); 1794 memcpy(state, state_static, 1795 MAXNEST_static * sizeof(*state)); 1796 } else 1797 state = parser_temp_realloc(state, 1798 maxnest * sizeof(*state)); 1799 } 1800 level++; 1801 state[level].syntax = ARISYNTAX; 1802 state[level].parenlevel = 0; 1803 state[level].category = TSTATE_ARITH; 1804 USTPUTC(CTLARI, out); 1805 if (state[level - 1].syntax == DQSYNTAX) 1806 USTPUTC('"',out); 1807 else 1808 USTPUTC(' ',out); 1809 goto parsearith_return; 1810} 1811 1812} /* end of readtoken */ 1813 1814 1815 1816#ifdef mkinit 1817RESET { 1818 tokpushback = 0; 1819 checkkwd = 0; 1820} 1821#endif 1822 1823/* 1824 * Returns true if the text contains nothing to expand (no dollar signs 1825 * or backquotes). 1826 */ 1827 1828static int 1829noexpand(char *text) 1830{ 1831 char *p; 1832 char c; 1833 1834 p = text; 1835 while ((c = *p++) != '\0') { 1836 if ( c == CTLQUOTEMARK) 1837 continue; 1838 if (c == CTLESC) 1839 p++; 1840 else if (BASESYNTAX[(int)c] == CCTL) 1841 return 0; 1842 } 1843 return 1; 1844} 1845 1846 1847/* 1848 * Return true if the argument is a legal variable name (a letter or 1849 * underscore followed by zero or more letters, underscores, and digits). 1850 */ 1851 1852int 1853goodname(const char *name) 1854{ 1855 const char *p; 1856 1857 p = name; 1858 if (! is_name(*p)) 1859 return 0; 1860 while (*++p) { 1861 if (! is_in_name(*p)) 1862 return 0; 1863 } 1864 return 1; 1865} 1866 1867 1868int 1869isassignment(const char *p) 1870{ 1871 if (!is_name(*p)) 1872 return 0; 1873 p++; 1874 for (;;) { 1875 if (*p == '=') 1876 return 1; 1877 else if (!is_in_name(*p)) 1878 return 0; 1879 p++; 1880 } 1881} 1882 1883 1884/* 1885 * Called when an unexpected token is read during the parse. The argument 1886 * is the token that is expected, or -1 if more than one type of token can 1887 * occur at this point. 1888 */ 1889 1890static void 1891synexpect(int token) 1892{ 1893 char msg[64]; 1894 1895 if (token >= 0) { 1896 fmtstr(msg, 64, "%s unexpected (expecting %s)", 1897 tokname[lasttoken], tokname[token]); 1898 } else { 1899 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 1900 } 1901 synerror(msg); 1902} 1903 1904 1905static void 1906synerror(const char *msg) 1907{ 1908 if (commandname) 1909 outfmt(out2, "%s: %d: ", commandname, startlinno); 1910 outfmt(out2, "Syntax error: %s\n", msg); 1911 error((char *)NULL); 1912} 1913 1914static void 1915setprompt(int which) 1916{ 1917 whichprompt = which; 1918 1919#ifndef NO_HISTORY 1920 if (!el) 1921#endif 1922 { 1923 out2str(getprompt(NULL)); 1924 flushout(out2); 1925 } 1926} 1927 1928/* 1929 * called by editline -- any expansions to the prompt 1930 * should be added here. 1931 */ 1932char * 1933getprompt(void *unused __unused) 1934{ 1935 static char ps[PROMPTLEN]; 1936 char *fmt; 1937 const char *pwd; 1938 int i, trim; 1939 static char internal_error[] = "??"; 1940 1941 /* 1942 * Select prompt format. 1943 */ 1944 switch (whichprompt) { 1945 case 0: 1946 fmt = nullstr; 1947 break; 1948 case 1: 1949 fmt = ps1val(); 1950 break; 1951 case 2: 1952 fmt = ps2val(); 1953 break; 1954 default: 1955 return internal_error; 1956 } 1957 1958 /* 1959 * Format prompt string. 1960 */ 1961 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1962 if (*fmt == '\\') 1963 switch (*++fmt) { 1964 1965 /* 1966 * Hostname. 1967 * 1968 * \h specifies just the local hostname, 1969 * \H specifies fully-qualified hostname. 1970 */ 1971 case 'h': 1972 case 'H': 1973 ps[i] = '\0'; 1974 gethostname(&ps[i], PROMPTLEN - i); 1975 /* Skip to end of hostname. */ 1976 trim = (*fmt == 'h') ? '.' : '\0'; 1977 while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1978 i++; 1979 break; 1980 1981 /* 1982 * Working directory. 1983 * 1984 * \W specifies just the final component, 1985 * \w specifies the entire path. 1986 */ 1987 case 'W': 1988 case 'w': 1989 pwd = lookupvar("PWD"); 1990 if (pwd == NULL) 1991 pwd = "?"; 1992 if (*fmt == 'W' && 1993 *pwd == '/' && pwd[1] != '\0') 1994 strlcpy(&ps[i], strrchr(pwd, '/') + 1, 1995 PROMPTLEN - i); 1996 else 1997 strlcpy(&ps[i], pwd, PROMPTLEN - i); 1998 /* Skip to end of path. */ 1999 while (ps[i + 1] != '\0') 2000 i++; 2001 break; 2002 2003 /* 2004 * Superuser status. 2005 * 2006 * '$' for normal users, '#' for root. 2007 */ 2008 case '$': 2009 ps[i] = (geteuid() != 0) ? '$' : '#'; 2010 break; 2011 2012 /* 2013 * A literal \. 2014 */ 2015 case '\\': 2016 ps[i] = '\\'; 2017 break; 2018 2019 /* 2020 * Emit unrecognized formats verbatim. 2021 */ 2022 default: 2023 ps[i++] = '\\'; 2024 ps[i] = *fmt; 2025 break; 2026 } 2027 else 2028 ps[i] = *fmt; 2029 ps[i] = '\0'; 2030 return (ps); 2031} 2032