parser.c revision 255068
1139776Simp/*- 222521Sdyson * Copyright (c) 1991, 1993 322521Sdyson * The Regents of the University of California. All rights reserved. 4234867Sdaichi * 5234867Sdaichi * This code is derived from software contributed to Berkeley by 61541Srgrimes * Kenneth Almquist. 71541Srgrimes * 81541Srgrimes * Redistribution and use in source and binary forms, with or without 91541Srgrimes * modification, are permitted provided that the following conditions 101541Srgrimes * are met: 111541Srgrimes * 1. Redistributions of source code must retain the above copyright 121541Srgrimes * notice, this list of conditions and the following disclaimer. 131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer in the 151541Srgrimes * documentation and/or other materials provided with the distribution. 161541Srgrimes * 4. Neither the name of the University nor the names of its contributors 171541Srgrimes * may be used to endorse or promote products derived from this software 181541Srgrimes * without specific prior written permission. 191541Srgrimes * 201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301541Srgrimes * SUCH DAMAGE. 311541Srgrimes */ 321541Srgrimes 331541Srgrimes#ifndef lint 341541Srgrimes#if 0 3522521Sdysonstatic char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; 3650477Speter#endif 371541Srgrimes#endif /* not lint */ 381541Srgrimes#include <sys/cdefs.h> 391541Srgrimes__FBSDID("$FreeBSD: head/bin/sh/parser.c 255068 2013-08-30 10:45:02Z jilles $"); 401541Srgrimes 41164829Srodrigc#include <stdlib.h> 42177785Skib#include <unistd.h> 432979Swollman#include <stdio.h> 4478906Sjhb 45164829Srodrigc#include "shell.h" 46164829Srodrigc#include "parser.h" 47164829Srodrigc#include "nodes.h" 481541Srgrimes#include "expand.h" /* defines rmescapes() */ 491541Srgrimes#include "syntax.h" 50164829Srodrigc#include "options.h" 51164829Srodrigc#include "input.h" 5277031Sru#include "output.h" 531541Srgrimes#include "var.h" 54164829Srodrigc#include "error.h" 5530354Sphk#include "memalloc.h" 56164829Srodrigc#include "mystring.h" 57164829Srodrigc#include "alias.h" 58164829Srodrigc#include "show.h" 59164829Srodrigc#include "eval.h" 60164829Srodrigc#include "exec.h" /* to check for special builtins */ 61164829Srodrigc#ifndef NO_HISTORY 62164829Srodrigc#include "myhistedit.h" 63164829Srodrigc#endif 64164829Srodrigc 65164829Srodrigc/* 6612595Sbde * Shell command parser. 67164829Srodrigc */ 68164829Srodrigc 691541Srgrimes#define EOFMARKLEN 79 70164829Srodrigc#define PROMPTLEN 128 71164829Srodrigc 7231273Sphk/* values of checkkwd variable */ 73191990Sattilio#define CHKALIAS 0x1 741541Srgrimes#define CHKKWD 0x2 75164829Srodrigc#define CHKNL 0x4 76164829Srodrigc 77164829Srodrigc/* values returned by readtoken */ 78164829Srodrigc#include "token.h" 79191990Sattilio 80164829Srodrigc 81164829Srodrigc 82164829Srodrigcstruct heredoc { 83164829Srodrigc struct heredoc *next; /* next here document in list */ 84164829Srodrigc union node *here; /* redirection node */ 85164829Srodrigc char *eofmark; /* string indicating end of input */ 86164829Srodrigc int striptabs; /* if set, strip leading tabs */ 87164829Srodrigc}; 88164829Srodrigc 89164829Srodrigcstruct parser_temp { 90164829Srodrigc struct parser_temp *next; 91172643Sdaichi void *data; 92164829Srodrigc}; 93164829Srodrigc 941541Srgrimes 95164829Srodrigcstatic struct heredoc *heredoclist; /* list of here documents to read */ 961541Srgrimesstatic int doprompt; /* if set, prompt the user */ 97164829Srodrigcstatic int needprompt; /* true if interactive and at start of line */ 98164829Srodrigcstatic int lasttoken; /* last token read */ 99164829Srodrigcstatic int tokpushback; /* last token pushed back */ 100164829Srodrigcstatic char *wordtext; /* text of last word returned by readtoken */ 101164829Srodrigcstatic int checkkwd; 102164829Srodrigcstatic struct nodelist *backquotelist; 103172642Sdaichistatic union node *redirnode; 104172643Sdaichistatic struct heredoc *heredoc; 105164829Srodrigcstatic int quoteflag; /* set if (part of) last token was quoted */ 106191990Sattiliostatic int startlinno; /* line # where last token started */ 10729888Skatostatic int funclinno; /* line # where the current function started */ 108165036Srodrigcstatic struct parser_temp *parser_temp; 109165036Srodrigc 110137479Sphk 111165036Srodrigcstatic union node *list(int, int); 112164829Srodrigcstatic union node *andor(void); 11329888Skatostatic union node *pipeline(void); 114164829Srodrigcstatic union node *command(void); 1151541Srgrimesstatic union node *simplecmd(union node **, union node *); 116165036Srodrigcstatic union node *makename(void); 117165036Srodrigcstatic void parsefname(void); 11897195Smuxstatic void parseheredoc(void); 119165036Srodrigcstatic int peektoken(void); 1201541Srgrimesstatic int readtoken(void); 1211541Srgrimesstatic int xxreadtoken(void); 122164829Srodrigcstatic int readtoken1(int, const char *, const char *, int); 1231541Srgrimesstatic int noexpand(char *); 124164829Srodrigcstatic void synexpect(int) __dead2; 125164829Srodrigcstatic void synerror(const char *) __dead2; 126164829Srodrigcstatic void setprompt(int); 127164829Srodrigc 128164829Srodrigc 129164829Srodrigcstatic void * 13097195Smuxparser_temp_alloc(size_t len) 131164829Srodrigc{ 132164829Srodrigc struct parser_temp *t; 133164829Srodrigc 134164829Srodrigc INTOFF; 135164829Srodrigc t = ckmalloc(sizeof(*t)); 136164829Srodrigc t->data = NULL; 137164829Srodrigc t->next = parser_temp; 138164829Srodrigc parser_temp = t; 13998266Smux t->data = ckmalloc(len); 140164829Srodrigc INTON; 141185284Sdaichi return t->data; 14298266Smux} 143164829Srodrigc 144164829Srodrigc 145164829Srodrigcstatic void * 146164829Srodrigcparser_temp_realloc(void *ptr, size_t len) 147164829Srodrigc{ 148164829Srodrigc struct parser_temp *t; 149164829Srodrigc 150185284Sdaichi INTOFF; 151164829Srodrigc t = parser_temp; 152164829Srodrigc if (ptr != t->data) 153164829Srodrigc error("bug: parser_temp_realloc misused"); 154164829Srodrigc t->data = ckrealloc(t->data, len); 155164829Srodrigc INTON; 156164829Srodrigc return t->data; 15797195Smux} 158175202Sattilio 159182371Sattilio 160164829Srodrigcstatic void 161164829Srodrigcparser_temp_free_upto(void *ptr) 162164829Srodrigc{ 163164829Srodrigc struct parser_temp *t; 164164829Srodrigc int done = 0; 165164829Srodrigc 166164829Srodrigc INTOFF; 167164829Srodrigc while (parser_temp != NULL && !done) { 168234867Sdaichi t = parser_temp; 1693496Sphk parser_temp = t->next; 170164829Srodrigc done = t->data == ptr; 1711541Srgrimes ckfree(t->data); 172164829Srodrigc ckfree(t); 173164829Srodrigc } 174164829Srodrigc INTON; 175164829Srodrigc if (!done) 176164829Srodrigc error("bug: parser_temp_free_upto misused"); 177164829Srodrigc} 178164829Srodrigc 179164829Srodrigc 180164829Srodrigcstatic void 181164829Srodrigcparser_temp_free_all(void) 182164829Srodrigc{ 183164829Srodrigc struct parser_temp *t; 184164829Srodrigc 185164829Srodrigc INTOFF; 186164829Srodrigc while (parser_temp != NULL) { 187164829Srodrigc t = parser_temp; 188164829Srodrigc parser_temp = t->next; 189164829Srodrigc ckfree(t->data); 190164829Srodrigc ckfree(t); 191164829Srodrigc } 192164829Srodrigc INTON; 193164829Srodrigc} 194164829Srodrigc 195164829Srodrigc 196164829Srodrigc/* 197164829Srodrigc * Read and parse a command. Returns NEOF on end of file. (NULL is a 198164829Srodrigc * valid parse tree indicating a blank line.) 199164829Srodrigc */ 200164829Srodrigc 201164829Srodrigcunion node * 202164829Srodrigcparsecmd(int interact) 203164829Srodrigc{ 204164829Srodrigc int t; 205164829Srodrigc 206164829Srodrigc /* This assumes the parser is not re-entered, 207172643Sdaichi * which could happen if we add command substitution on PS1/PS2. 208172643Sdaichi */ 209172643Sdaichi parser_temp_free_all(); 210172643Sdaichi heredoclist = NULL; 211172643Sdaichi 212172643Sdaichi tokpushback = 0; 213172643Sdaichi checkkwd = 0; 214172643Sdaichi doprompt = interact; 215172643Sdaichi if (doprompt) 216172643Sdaichi setprompt(1); 217172643Sdaichi else 218172643Sdaichi setprompt(0); 219172643Sdaichi needprompt = 0; 220172643Sdaichi t = readtoken(); 22124921Skato if (t == TEOF) 222164829Srodrigc return NEOF; 223164829Srodrigc if (t == TNL) 224164829Srodrigc return NULL; 225164829Srodrigc tokpushback++; 2261541Srgrimes return list(1, 1); 2278876Srgrimes} 228164829Srodrigc 229164829Srodrigc 230164829Srodrigcstatic union node * 231164829Srodrigclist(int nlflag, int erflag) 2321541Srgrimes{ 233164829Srodrigc union node *ntop, *n1, *n2, *n3; 2341541Srgrimes int tok; 235184650Sjhb 236164829Srodrigc checkkwd = CHKNL | CHKKWD | CHKALIAS; 237164829Srodrigc if (!nlflag && !erflag && tokendlist[peektoken()]) 2381541Srgrimes return NULL; 239164829Srodrigc ntop = n1 = NULL; 24051688Sdillon for (;;) { 241164829Srodrigc n2 = andor(); 242164829Srodrigc tok = readtoken(); 243164829Srodrigc if (tok == TBACKGND) { 24451688Sdillon if (n2 != NULL && n2->type == NPIPE) { 245164829Srodrigc n2->npipe.backgnd = 1; 246164829Srodrigc } else if (n2 != NULL && n2->type == NREDIR) { 247164829Srodrigc n2->type = NBACKGND; 248136135Stakawata } else { 249164829Srodrigc n3 = (union node *)stalloc(sizeof (struct nredir)); 250164829Srodrigc n3->type = NBACKGND; 251164829Srodrigc n3->nredir.n = n2; 252164829Srodrigc n3->nredir.redirect = NULL; 253234867Sdaichi n2 = n3; 254175202Sattilio } 255164829Srodrigc } 256164829Srodrigc if (ntop == NULL) 257164829Srodrigc ntop = n2; 258164829Srodrigc else if (n1 == NULL) { 259164829Srodrigc n1 = (union node *)stalloc(sizeof (struct nbinary)); 260164829Srodrigc n1->type = NSEMI; 261164829Srodrigc n1->nbinary.ch1 = ntop; 262164829Srodrigc n1->nbinary.ch2 = n2; 263164829Srodrigc ntop = n1; 264164829Srodrigc } 265164829Srodrigc else { 266164829Srodrigc n3 = (union node *)stalloc(sizeof (struct nbinary)); 267172643Sdaichi n3->type = NSEMI; 2681541Srgrimes n3->nbinary.ch1 = n1->nbinary.ch2; 269172697Salfred n3->nbinary.ch2 = n2; 2701541Srgrimes n1->nbinary.ch2 = n3; 271164829Srodrigc n1 = n3; 272164829Srodrigc } 273164829Srodrigc switch (tok) { 274164829Srodrigc case TBACKGND: 2751541Srgrimes case TSEMI: 27622521Sdyson tok = readtoken(); 277164829Srodrigc /* FALLTHROUGH */ 278164829Srodrigc case TNL: 279234867Sdaichi if (tok == TNL) { 2801541Srgrimes parseheredoc(); 2811541Srgrimes if (nlflag) 282164829Srodrigc return ntop; 2831541Srgrimes } else if (tok == TEOF && nlflag) { 284164829Srodrigc parseheredoc(); 285164829Srodrigc return ntop; 286172641Sdaichi } else { 287164829Srodrigc tokpushback++; 288164829Srodrigc } 289164829Srodrigc checkkwd = CHKNL | CHKKWD | CHKALIAS; 290164829Srodrigc if (!nlflag && (erflag ? peektoken() == TEOF : 2911541Srgrimes tokendlist[peektoken()])) 2921541Srgrimes return ntop; 2931541Srgrimes break; 294164829Srodrigc case TEOF: 2951541Srgrimes if (heredoclist) 296164829Srodrigc parseheredoc(); 297164829Srodrigc else 298164829Srodrigc pungetc(); /* push back EOF on input */ 2991541Srgrimes return ntop; 300164829Srodrigc default: 301164829Srodrigc if (nlflag || erflag) 302164829Srodrigc synexpect(-1); 30322521Sdyson tokpushback++; 3041541Srgrimes return ntop; 305164829Srodrigc } 306164829Srodrigc } 307164829Srodrigc} 308164829Srodrigc 309164829Srodrigc 310164829Srodrigc 3111541Srgrimesstatic union node * 312164829Srodrigcandor(void) 313164829Srodrigc{ 3141541Srgrimes union node *n1, *n2, *n3; 3151541Srgrimes int t; 3161541Srgrimes 3171541Srgrimes n1 = pipeline(); 3181541Srgrimes for (;;) { 319164829Srodrigc if ((t = readtoken()) == TAND) { 3201541Srgrimes t = NAND; 32131273Sphk } else if (t == TOR) { 322191990Sattilio t = NOR; 3231541Srgrimes } else { 324164829Srodrigc tokpushback++; 325164829Srodrigc return n1; 326164829Srodrigc } 327164829Srodrigc n2 = pipeline(); 328164829Srodrigc n3 = (union node *)stalloc(sizeof (struct nbinary)); 3291541Srgrimes n3->type = t; 330164829Srodrigc n3->nbinary.ch1 = n1; 3311541Srgrimes n3->nbinary.ch2 = n2; 332164829Srodrigc n1 = n3; 333164829Srodrigc } 334164829Srodrigc} 33522521Sdyson 33622521Sdyson 33722521Sdyson 338164829Srodrigcstatic union node * 339191990Sattiliopipeline(void) 340164829Srodrigc{ 341164829Srodrigc union node *n1, *n2, *pipenode; 34222521Sdyson struct nodelist *lp, *prev; 343164829Srodrigc int negate, t; 3441541Srgrimes 3451541Srgrimes negate = 0; 34676688Siedowse checkkwd = CHKNL | CHKKWD | CHKALIAS; 34776688Siedowse TRACE(("pipeline: entered\n")); 3481541Srgrimes while (readtoken() == TNOT) 349164829Srodrigc negate = !negate; 350232918Skevlo tokpushback++; 351164829Srodrigc n1 = command(); 3521541Srgrimes if (readtoken() == TPIPE) { 3531541Srgrimes pipenode = (union node *)stalloc(sizeof (struct npipe)); 3541541Srgrimes pipenode->type = NPIPE; 35531273Sphk pipenode->npipe.backgnd = 0; 356191990Sattilio lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 3571541Srgrimes pipenode->npipe.cmdlist = lp; 358164829Srodrigc lp->n = n1; 359164829Srodrigc do { 3601541Srgrimes prev = lp; 361164829Srodrigc lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 362164829Srodrigc checkkwd = CHKNL | CHKKWD | CHKALIAS; 36351688Sdillon t = readtoken(); 364164829Srodrigc tokpushback++; 365176559Sattilio if (t == TNOT) 3661541Srgrimes lp->n = pipeline(); 367164829Srodrigc else 368164829Srodrigc lp->n = command(); 369175202Sattilio prev->next = lp; 3701541Srgrimes } while (readtoken() == TPIPE); 371164829Srodrigc lp->next = NULL; 372164829Srodrigc n1 = pipenode; 373164829Srodrigc } 3741541Srgrimes tokpushback++; 3751541Srgrimes if (negate) { 37631273Sphk n2 = (union node *)stalloc(sizeof (struct nnot)); 377191990Sattilio n2->type = NNOT; 3781541Srgrimes n2->nnot.com = n1; 379164829Srodrigc return n2; 3801541Srgrimes } else 381164829Srodrigc return n1; 3821541Srgrimes} 383164829Srodrigc 384164829Srodrigc 385164829Srodrigc 386191990Sattiliostatic union node * 387164829Srodrigccommand(void) 388164829Srodrigc{ 389164829Srodrigc union node *n1, *n2; 390191990Sattilio union node *ap, **app; 391164829Srodrigc union node *cp, **cpp; 392164829Srodrigc union node *redir, **rpp; 393164829Srodrigc int t; 394164829Srodrigc int is_subshell; 395164829Srodrigc 396164829Srodrigc checkkwd = CHKNL | CHKKWD | CHKALIAS; 397164829Srodrigc is_subshell = 0; 398164829Srodrigc redir = NULL; 399164829Srodrigc n1 = NULL; 400164829Srodrigc rpp = &redir; 401164829Srodrigc 4021541Srgrimes /* Check for redirection which may precede command */ 4031541Srgrimes while (readtoken() == TREDIR) { 404191990Sattilio *rpp = n2 = redirnode; 405164829Srodrigc rpp = &n2->nfile.next; 406164829Srodrigc parsefname(); 4071541Srgrimes } 408164829Srodrigc tokpushback++; 4091541Srgrimes 4101541Srgrimes switch (readtoken()) { 4111541Srgrimes case TIF: 412164829Srodrigc n1 = (union node *)stalloc(sizeof (struct nif)); 413164829Srodrigc n1->type = NIF; 414191990Sattilio if ((n1->nif.test = list(0, 0)) == NULL) 4151541Srgrimes synexpect(-1); 4161541Srgrimes if (readtoken() != TTHEN) 4171541Srgrimes synexpect(TTHEN); 418164829Srodrigc n1->nif.ifpart = list(0, 0); 419164829Srodrigc n2 = n1; 420164829Srodrigc while (readtoken() == TELIF) { 421164829Srodrigc n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 422164829Srodrigc n2 = n2->nif.elsepart; 4231541Srgrimes n2->type = NIF; 4241541Srgrimes if ((n2->nif.test = list(0, 0)) == NULL) 4251541Srgrimes synexpect(-1); 4261541Srgrimes if (readtoken() != TTHEN) 42722521Sdyson synexpect(TTHEN); 428164829Srodrigc n2->nif.ifpart = list(0, 0); 42922521Sdyson } 4301541Srgrimes if (lasttoken == TELSE) 43122521Sdyson n2->nif.elsepart = list(0, 0); 43222521Sdyson else { 4331541Srgrimes n2->nif.elsepart = NULL; 43422521Sdyson tokpushback++; 4351541Srgrimes } 4361541Srgrimes if (readtoken() != TFI) 4371541Srgrimes synexpect(TFI); 438164829Srodrigc checkkwd = CHKKWD | CHKALIAS; 439191990Sattilio break; 440164829Srodrigc case TWHILE: 441164829Srodrigc case TUNTIL: { 442164829Srodrigc int got; 443164829Srodrigc n1 = (union node *)stalloc(sizeof (struct nbinary)); 444164829Srodrigc n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 445164829Srodrigc if ((n1->nbinary.ch1 = list(0, 0)) == NULL) 446164829Srodrigc synexpect(-1); 447164829Srodrigc if ((got=readtoken()) != TDO) { 448164829SrodrigcTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 449164829Srodrigc synexpect(TDO); 450164829Srodrigc } 451164829Srodrigc n1->nbinary.ch2 = list(0, 0); 452222167Srmacklem if (readtoken() != TDONE) 453222167Srmacklem synexpect(TDONE); 454164829Srodrigc checkkwd = CHKKWD | CHKALIAS; 455164829Srodrigc break; 456164829Srodrigc } 457164829Srodrigc case TFOR: 458164829Srodrigc if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 459164829Srodrigc synerror("Bad for loop variable"); 460184588Sdfr n1 = (union node *)stalloc(sizeof (struct nfor)); 461164829Srodrigc n1->type = NFOR; 462164829Srodrigc n1->nfor.var = wordtext; 463164829Srodrigc while (readtoken() == TNL) 464164829Srodrigc ; 465164829Srodrigc if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) { 466164829Srodrigc app = ≈ 467191990Sattilio while (readtoken() == TWORD) { 468164829Srodrigc n2 = (union node *)stalloc(sizeof (struct narg)); 469164829Srodrigc n2->type = NARG; 470164829Srodrigc n2->narg.text = wordtext; 471164829Srodrigc n2->narg.backquote = backquotelist; 472164829Srodrigc *app = n2; 473164829Srodrigc app = &n2->narg.next; 474164829Srodrigc } 475164829Srodrigc *app = NULL; 476164829Srodrigc n1->nfor.args = ap; 477191990Sattilio if (lasttoken != TNL && lasttoken != TSEMI) 478164829Srodrigc synexpect(-1); 479164829Srodrigc } else { 480191990Sattilio static char argvars[5] = { 481164829Srodrigc CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' 482164829Srodrigc }; 483164829Srodrigc n2 = (union node *)stalloc(sizeof (struct narg)); 484164829Srodrigc n2->type = NARG; 485164829Srodrigc n2->narg.text = argvars; 486164829Srodrigc n2->narg.backquote = NULL; 487164829Srodrigc n2->narg.next = NULL; 488164829Srodrigc n1->nfor.args = n2; 489164829Srodrigc /* 490164829Srodrigc * Newline or semicolon here is optional (but note 491164829Srodrigc * that the original Bourne shell only allowed NL). 492164829Srodrigc */ 493164829Srodrigc if (lasttoken != TNL && lasttoken != TSEMI) 494164829Srodrigc tokpushback++; 495164829Srodrigc } 496164829Srodrigc checkkwd = CHKNL | CHKKWD | CHKALIAS; 4971541Srgrimes if ((t = readtoken()) == TDO) 4982946Swollman t = TDONE; 499164829Srodrigc else if (t == TBEGIN) 500 t = TEND; 501 else 502 synexpect(-1); 503 n1->nfor.body = list(0, 0); 504 if (readtoken() != t) 505 synexpect(t); 506 checkkwd = CHKKWD | CHKALIAS; 507 break; 508 case TCASE: 509 n1 = (union node *)stalloc(sizeof (struct ncase)); 510 n1->type = NCASE; 511 if (readtoken() != TWORD) 512 synexpect(TWORD); 513 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 514 n2->type = NARG; 515 n2->narg.text = wordtext; 516 n2->narg.backquote = backquotelist; 517 n2->narg.next = NULL; 518 while (readtoken() == TNL); 519 if (lasttoken != TWORD || ! equal(wordtext, "in")) 520 synerror("expecting \"in\""); 521 cpp = &n1->ncase.cases; 522 checkkwd = CHKNL | CHKKWD, readtoken(); 523 while (lasttoken != TESAC) { 524 *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 525 cp->type = NCLIST; 526 app = &cp->nclist.pattern; 527 if (lasttoken == TLP) 528 readtoken(); 529 for (;;) { 530 *app = ap = (union node *)stalloc(sizeof (struct narg)); 531 ap->type = NARG; 532 ap->narg.text = wordtext; 533 ap->narg.backquote = backquotelist; 534 checkkwd = CHKNL | CHKKWD; 535 if (readtoken() != TPIPE) 536 break; 537 app = &ap->narg.next; 538 readtoken(); 539 } 540 ap->narg.next = NULL; 541 if (lasttoken != TRP) 542 synexpect(TRP); 543 cp->nclist.body = list(0, 0); 544 545 checkkwd = CHKNL | CHKKWD | CHKALIAS; 546 if ((t = readtoken()) != TESAC) { 547 if (t == TENDCASE) 548 ; 549 else if (t == TFALLTHRU) 550 cp->type = NCLISTFALLTHRU; 551 else 552 synexpect(TENDCASE); 553 checkkwd = CHKNL | CHKKWD, readtoken(); 554 } 555 cpp = &cp->nclist.next; 556 } 557 *cpp = NULL; 558 checkkwd = CHKKWD | CHKALIAS; 559 break; 560 case TLP: 561 n1 = (union node *)stalloc(sizeof (struct nredir)); 562 n1->type = NSUBSHELL; 563 n1->nredir.n = list(0, 0); 564 n1->nredir.redirect = NULL; 565 if (readtoken() != TRP) 566 synexpect(TRP); 567 checkkwd = CHKKWD | CHKALIAS; 568 is_subshell = 1; 569 break; 570 case TBEGIN: 571 n1 = list(0, 0); 572 if (readtoken() != TEND) 573 synexpect(TEND); 574 checkkwd = CHKKWD | CHKALIAS; 575 break; 576 /* A simple command must have at least one redirection or word. */ 577 case TBACKGND: 578 case TSEMI: 579 case TAND: 580 case TOR: 581 case TPIPE: 582 case TENDCASE: 583 case TFALLTHRU: 584 case TEOF: 585 case TNL: 586 case TRP: 587 if (!redir) 588 synexpect(-1); 589 case TWORD: 590 tokpushback++; 591 n1 = simplecmd(rpp, redir); 592 return n1; 593 default: 594 synexpect(-1); 595 } 596 597 /* Now check for redirection which may follow command */ 598 while (readtoken() == TREDIR) { 599 *rpp = n2 = redirnode; 600 rpp = &n2->nfile.next; 601 parsefname(); 602 } 603 tokpushback++; 604 *rpp = NULL; 605 if (redir) { 606 if (!is_subshell) { 607 n2 = (union node *)stalloc(sizeof (struct nredir)); 608 n2->type = NREDIR; 609 n2->nredir.n = n1; 610 n1 = n2; 611 } 612 n1->nredir.redirect = redir; 613 } 614 615 return n1; 616} 617 618 619static union node * 620simplecmd(union node **rpp, union node *redir) 621{ 622 union node *args, **app; 623 union node **orig_rpp = rpp; 624 union node *n = NULL; 625 int special; 626 int savecheckkwd; 627 628 /* If we don't have any redirections already, then we must reset */ 629 /* rpp to be the address of the local redir variable. */ 630 if (redir == 0) 631 rpp = &redir; 632 633 args = NULL; 634 app = &args; 635 /* 636 * We save the incoming value, because we need this for shell 637 * functions. There can not be a redirect or an argument between 638 * the function name and the open parenthesis. 639 */ 640 orig_rpp = rpp; 641 642 savecheckkwd = CHKALIAS; 643 644 for (;;) { 645 checkkwd = savecheckkwd; 646 if (readtoken() == TWORD) { 647 n = (union node *)stalloc(sizeof (struct narg)); 648 n->type = NARG; 649 n->narg.text = wordtext; 650 n->narg.backquote = backquotelist; 651 *app = n; 652 app = &n->narg.next; 653 if (savecheckkwd != 0 && !isassignment(wordtext)) 654 savecheckkwd = 0; 655 } else if (lasttoken == TREDIR) { 656 *rpp = n = redirnode; 657 rpp = &n->nfile.next; 658 parsefname(); /* read name of redirection file */ 659 } else if (lasttoken == TLP && app == &args->narg.next 660 && rpp == orig_rpp) { 661 /* We have a function */ 662 if (readtoken() != TRP) 663 synexpect(TRP); 664 funclinno = plinno; 665 /* 666 * - Require plain text. 667 * - Functions with '/' cannot be called. 668 * - Reject name=(). 669 * - Reject ksh extended glob patterns. 670 */ 671 if (!noexpand(n->narg.text) || quoteflag || 672 strchr(n->narg.text, '/') || 673 strchr("!%*+-=?@}~", 674 n->narg.text[strlen(n->narg.text) - 1])) 675 synerror("Bad function name"); 676 rmescapes(n->narg.text); 677 if (find_builtin(n->narg.text, &special) >= 0 && 678 special) 679 synerror("Cannot override a special builtin with a function"); 680 n->type = NDEFUN; 681 n->narg.next = command(); 682 funclinno = 0; 683 return n; 684 } else { 685 tokpushback++; 686 break; 687 } 688 } 689 *app = NULL; 690 *rpp = NULL; 691 n = (union node *)stalloc(sizeof (struct ncmd)); 692 n->type = NCMD; 693 n->ncmd.args = args; 694 n->ncmd.redirect = redir; 695 return n; 696} 697 698static union node * 699makename(void) 700{ 701 union node *n; 702 703 n = (union node *)stalloc(sizeof (struct narg)); 704 n->type = NARG; 705 n->narg.next = NULL; 706 n->narg.text = wordtext; 707 n->narg.backquote = backquotelist; 708 return n; 709} 710 711void 712fixredir(union node *n, const char *text, int err) 713{ 714 TRACE(("Fix redir %s %d\n", text, err)); 715 if (!err) 716 n->ndup.vname = NULL; 717 718 if (is_digit(text[0]) && text[1] == '\0') 719 n->ndup.dupfd = digit_val(text[0]); 720 else if (text[0] == '-' && text[1] == '\0') 721 n->ndup.dupfd = -1; 722 else { 723 724 if (err) 725 synerror("Bad fd number"); 726 else 727 n->ndup.vname = makename(); 728 } 729} 730 731 732static void 733parsefname(void) 734{ 735 union node *n = redirnode; 736 737 if (readtoken() != TWORD) 738 synexpect(-1); 739 if (n->type == NHERE) { 740 struct heredoc *here = heredoc; 741 struct heredoc *p; 742 int i; 743 744 if (quoteflag == 0) 745 n->type = NXHERE; 746 TRACE(("Here document %d\n", n->type)); 747 if (here->striptabs) { 748 while (*wordtext == '\t') 749 wordtext++; 750 } 751 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 752 synerror("Illegal eof marker for << redirection"); 753 rmescapes(wordtext); 754 here->eofmark = wordtext; 755 here->next = NULL; 756 if (heredoclist == NULL) 757 heredoclist = here; 758 else { 759 for (p = heredoclist ; p->next ; p = p->next); 760 p->next = here; 761 } 762 } else if (n->type == NTOFD || n->type == NFROMFD) { 763 fixredir(n, wordtext, 0); 764 } else { 765 n->nfile.fname = makename(); 766 } 767} 768 769 770/* 771 * Input any here documents. 772 */ 773 774static void 775parseheredoc(void) 776{ 777 struct heredoc *here; 778 union node *n; 779 780 while (heredoclist) { 781 here = heredoclist; 782 heredoclist = here->next; 783 if (needprompt) { 784 setprompt(2); 785 needprompt = 0; 786 } 787 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 788 here->eofmark, here->striptabs); 789 n = (union node *)stalloc(sizeof (struct narg)); 790 n->narg.type = NARG; 791 n->narg.next = NULL; 792 n->narg.text = wordtext; 793 n->narg.backquote = backquotelist; 794 here->here->nhere.doc = n; 795 } 796} 797 798static int 799peektoken(void) 800{ 801 int t; 802 803 t = readtoken(); 804 tokpushback++; 805 return (t); 806} 807 808static int 809readtoken(void) 810{ 811 int t; 812 struct alias *ap; 813#ifdef DEBUG 814 int alreadyseen = tokpushback; 815#endif 816 817 top: 818 t = xxreadtoken(); 819 820 /* 821 * eat newlines 822 */ 823 if (checkkwd & CHKNL) { 824 while (t == TNL) { 825 parseheredoc(); 826 t = xxreadtoken(); 827 } 828 } 829 830 /* 831 * check for keywords and aliases 832 */ 833 if (t == TWORD && !quoteflag) 834 { 835 const char * const *pp; 836 837 if (checkkwd & CHKKWD) 838 for (pp = parsekwd; *pp; pp++) { 839 if (**pp == *wordtext && equal(*pp, wordtext)) 840 { 841 lasttoken = t = pp - parsekwd + KWDOFFSET; 842 TRACE(("keyword %s recognized\n", tokname[t])); 843 goto out; 844 } 845 } 846 if (checkkwd & CHKALIAS && 847 (ap = lookupalias(wordtext, 1)) != NULL) { 848 pushstring(ap->val, strlen(ap->val), ap); 849 goto top; 850 } 851 } 852out: 853 if (t != TNOT) 854 checkkwd = 0; 855 856#ifdef DEBUG 857 if (!alreadyseen) 858 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 859 else 860 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 861#endif 862 return (t); 863} 864 865 866/* 867 * Read the next input token. 868 * If the token is a word, we set backquotelist to the list of cmds in 869 * backquotes. We set quoteflag to true if any part of the word was 870 * quoted. 871 * If the token is TREDIR, then we set redirnode to a structure containing 872 * the redirection. 873 * In all cases, the variable startlinno is set to the number of the line 874 * on which the token starts. 875 * 876 * [Change comment: here documents and internal procedures] 877 * [Readtoken shouldn't have any arguments. Perhaps we should make the 878 * word parsing code into a separate routine. In this case, readtoken 879 * doesn't need to have any internal procedures, but parseword does. 880 * We could also make parseoperator in essence the main routine, and 881 * have parseword (readtoken1?) handle both words and redirection.] 882 */ 883 884#define RETURN(token) return lasttoken = token 885 886static int 887xxreadtoken(void) 888{ 889 int c; 890 891 if (tokpushback) { 892 tokpushback = 0; 893 return lasttoken; 894 } 895 if (needprompt) { 896 setprompt(2); 897 needprompt = 0; 898 } 899 startlinno = plinno; 900 for (;;) { /* until token or start of word found */ 901 c = pgetc_macro(); 902 switch (c) { 903 case ' ': case '\t': 904 continue; 905 case '#': 906 while ((c = pgetc()) != '\n' && c != PEOF); 907 pungetc(); 908 continue; 909 case '\\': 910 if (pgetc() == '\n') { 911 startlinno = ++plinno; 912 if (doprompt) 913 setprompt(2); 914 else 915 setprompt(0); 916 continue; 917 } 918 pungetc(); 919 goto breakloop; 920 case '\n': 921 plinno++; 922 needprompt = doprompt; 923 RETURN(TNL); 924 case PEOF: 925 RETURN(TEOF); 926 case '&': 927 if (pgetc() == '&') 928 RETURN(TAND); 929 pungetc(); 930 RETURN(TBACKGND); 931 case '|': 932 if (pgetc() == '|') 933 RETURN(TOR); 934 pungetc(); 935 RETURN(TPIPE); 936 case ';': 937 c = pgetc(); 938 if (c == ';') 939 RETURN(TENDCASE); 940 else if (c == '&') 941 RETURN(TFALLTHRU); 942 pungetc(); 943 RETURN(TSEMI); 944 case '(': 945 RETURN(TLP); 946 case ')': 947 RETURN(TRP); 948 default: 949 goto breakloop; 950 } 951 } 952breakloop: 953 return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 954#undef RETURN 955} 956 957 958#define MAXNEST_static 8 959struct tokenstate 960{ 961 const char *syntax; /* *SYNTAX */ 962 int parenlevel; /* levels of parentheses in arithmetic */ 963 enum tokenstate_category 964 { 965 TSTATE_TOP, 966 TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */ 967 TSTATE_VAR_NEW, /* other ${var...}, own dquote state */ 968 TSTATE_ARITH 969 } category; 970}; 971 972 973/* 974 * Called to parse command substitutions. 975 */ 976 977static char * 978parsebackq(char *out, struct nodelist **pbqlist, 979 int oldstyle, int dblquote, int quoted) 980{ 981 struct nodelist **nlpp; 982 union node *n; 983 char *volatile str; 984 struct jmploc jmploc; 985 struct jmploc *const savehandler = handler; 986 size_t savelen; 987 int saveprompt; 988 const int bq_startlinno = plinno; 989 char *volatile ostr = NULL; 990 struct parsefile *const savetopfile = getcurrentfile(); 991 struct heredoc *const saveheredoclist = heredoclist; 992 struct heredoc *here; 993 994 str = NULL; 995 if (setjmp(jmploc.loc)) { 996 popfilesupto(savetopfile); 997 if (str) 998 ckfree(str); 999 if (ostr) 1000 ckfree(ostr); 1001 heredoclist = saveheredoclist; 1002 handler = savehandler; 1003 if (exception == EXERROR) { 1004 startlinno = bq_startlinno; 1005 synerror("Error in command substitution"); 1006 } 1007 longjmp(handler->loc, 1); 1008 } 1009 INTOFF; 1010 savelen = out - stackblock(); 1011 if (savelen > 0) { 1012 str = ckmalloc(savelen); 1013 memcpy(str, stackblock(), savelen); 1014 } 1015 handler = &jmploc; 1016 heredoclist = NULL; 1017 INTON; 1018 if (oldstyle) { 1019 /* We must read until the closing backquote, giving special 1020 treatment to some slashes, and then push the string and 1021 reread it as input, interpreting it normally. */ 1022 char *oout; 1023 int c; 1024 int olen; 1025 1026 1027 STARTSTACKSTR(oout); 1028 for (;;) { 1029 if (needprompt) { 1030 setprompt(2); 1031 needprompt = 0; 1032 } 1033 CHECKSTRSPACE(2, oout); 1034 switch (c = pgetc()) { 1035 case '`': 1036 goto done; 1037 1038 case '\\': 1039 if ((c = pgetc()) == '\n') { 1040 plinno++; 1041 if (doprompt) 1042 setprompt(2); 1043 else 1044 setprompt(0); 1045 /* 1046 * If eating a newline, avoid putting 1047 * the newline into the new character 1048 * stream (via the USTPUTC after the 1049 * switch). 1050 */ 1051 continue; 1052 } 1053 if (c != '\\' && c != '`' && c != '$' 1054 && (!dblquote || c != '"')) 1055 USTPUTC('\\', oout); 1056 break; 1057 1058 case '\n': 1059 plinno++; 1060 needprompt = doprompt; 1061 break; 1062 1063 case PEOF: 1064 startlinno = plinno; 1065 synerror("EOF in backquote substitution"); 1066 break; 1067 1068 default: 1069 break; 1070 } 1071 USTPUTC(c, oout); 1072 } 1073done: 1074 USTPUTC('\0', oout); 1075 olen = oout - stackblock(); 1076 INTOFF; 1077 ostr = ckmalloc(olen); 1078 memcpy(ostr, stackblock(), olen); 1079 setinputstring(ostr, 1); 1080 INTON; 1081 } 1082 nlpp = pbqlist; 1083 while (*nlpp) 1084 nlpp = &(*nlpp)->next; 1085 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 1086 (*nlpp)->next = NULL; 1087 1088 if (oldstyle) { 1089 saveprompt = doprompt; 1090 doprompt = 0; 1091 } 1092 1093 n = list(0, oldstyle); 1094 1095 if (oldstyle) 1096 doprompt = saveprompt; 1097 else { 1098 if (readtoken() != TRP) 1099 synexpect(TRP); 1100 } 1101 1102 (*nlpp)->n = n; 1103 if (oldstyle) { 1104 /* 1105 * Start reading from old file again, ignoring any pushed back 1106 * tokens left from the backquote parsing 1107 */ 1108 popfile(); 1109 tokpushback = 0; 1110 } 1111 STARTSTACKSTR(out); 1112 CHECKSTRSPACE(savelen + 1, out); 1113 INTOFF; 1114 if (str) { 1115 memcpy(out, str, savelen); 1116 STADJUST(savelen, out); 1117 ckfree(str); 1118 str = NULL; 1119 } 1120 if (ostr) { 1121 ckfree(ostr); 1122 ostr = NULL; 1123 } 1124 here = saveheredoclist; 1125 if (here != NULL) { 1126 while (here->next != NULL) 1127 here = here->next; 1128 here->next = heredoclist; 1129 heredoclist = saveheredoclist; 1130 } 1131 handler = savehandler; 1132 INTON; 1133 if (quoted) 1134 USTPUTC(CTLBACKQ | CTLQUOTE, out); 1135 else 1136 USTPUTC(CTLBACKQ, out); 1137 return out; 1138} 1139 1140 1141/* 1142 * Called to parse a backslash escape sequence inside $'...'. 1143 * The backslash has already been read. 1144 */ 1145static char * 1146readcstyleesc(char *out) 1147{ 1148 int c, v, i, n; 1149 1150 c = pgetc(); 1151 switch (c) { 1152 case '\0': 1153 synerror("Unterminated quoted string"); 1154 case '\n': 1155 plinno++; 1156 if (doprompt) 1157 setprompt(2); 1158 else 1159 setprompt(0); 1160 return out; 1161 case '\\': 1162 case '\'': 1163 case '"': 1164 v = c; 1165 break; 1166 case 'a': v = '\a'; break; 1167 case 'b': v = '\b'; break; 1168 case 'e': v = '\033'; break; 1169 case 'f': v = '\f'; break; 1170 case 'n': v = '\n'; break; 1171 case 'r': v = '\r'; break; 1172 case 't': v = '\t'; break; 1173 case 'v': v = '\v'; break; 1174 case 'x': 1175 v = 0; 1176 for (;;) { 1177 c = pgetc(); 1178 if (c >= '0' && c <= '9') 1179 v = (v << 4) + c - '0'; 1180 else if (c >= 'A' && c <= 'F') 1181 v = (v << 4) + c - 'A' + 10; 1182 else if (c >= 'a' && c <= 'f') 1183 v = (v << 4) + c - 'a' + 10; 1184 else 1185 break; 1186 } 1187 pungetc(); 1188 break; 1189 case '0': case '1': case '2': case '3': 1190 case '4': case '5': case '6': case '7': 1191 v = c - '0'; 1192 c = pgetc(); 1193 if (c >= '0' && c <= '7') { 1194 v <<= 3; 1195 v += c - '0'; 1196 c = pgetc(); 1197 if (c >= '0' && c <= '7') { 1198 v <<= 3; 1199 v += c - '0'; 1200 } else 1201 pungetc(); 1202 } else 1203 pungetc(); 1204 break; 1205 case 'c': 1206 c = pgetc(); 1207 if (c < 0x3f || c > 0x7a || c == 0x60) 1208 synerror("Bad escape sequence"); 1209 if (c == '\\' && pgetc() != '\\') 1210 synerror("Bad escape sequence"); 1211 if (c == '?') 1212 v = 127; 1213 else 1214 v = c & 0x1f; 1215 break; 1216 case 'u': 1217 case 'U': 1218 n = c == 'U' ? 8 : 4; 1219 v = 0; 1220 for (i = 0; i < n; i++) { 1221 c = pgetc(); 1222 if (c >= '0' && c <= '9') 1223 v = (v << 4) + c - '0'; 1224 else if (c >= 'A' && c <= 'F') 1225 v = (v << 4) + c - 'A' + 10; 1226 else if (c >= 'a' && c <= 'f') 1227 v = (v << 4) + c - 'a' + 10; 1228 else 1229 synerror("Bad escape sequence"); 1230 } 1231 if (v == 0 || (v >= 0xd800 && v <= 0xdfff)) 1232 synerror("Bad escape sequence"); 1233 /* We really need iconv here. */ 1234 if (initial_localeisutf8 && v > 127) { 1235 CHECKSTRSPACE(4, out); 1236 /* 1237 * We cannot use wctomb() as the locale may have 1238 * changed. 1239 */ 1240 if (v <= 0x7ff) { 1241 USTPUTC(0xc0 | v >> 6, out); 1242 USTPUTC(0x80 | (v & 0x3f), out); 1243 return out; 1244 } else if (v <= 0xffff) { 1245 USTPUTC(0xe0 | v >> 12, out); 1246 USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1247 USTPUTC(0x80 | (v & 0x3f), out); 1248 return out; 1249 } else if (v <= 0x10ffff) { 1250 USTPUTC(0xf0 | v >> 18, out); 1251 USTPUTC(0x80 | ((v >> 12) & 0x3f), out); 1252 USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1253 USTPUTC(0x80 | (v & 0x3f), out); 1254 return out; 1255 } 1256 } 1257 if (v > 127) 1258 v = '?'; 1259 break; 1260 default: 1261 synerror("Bad escape sequence"); 1262 } 1263 v = (char)v; 1264 /* 1265 * We can't handle NUL bytes. 1266 * POSIX says we should skip till the closing quote. 1267 */ 1268 if (v == '\0') { 1269 while ((c = pgetc()) != '\'') { 1270 if (c == '\\') 1271 c = pgetc(); 1272 if (c == PEOF) 1273 synerror("Unterminated quoted string"); 1274 } 1275 pungetc(); 1276 return out; 1277 } 1278 if (SQSYNTAX[v] == CCTL) 1279 USTPUTC(CTLESC, out); 1280 USTPUTC(v, out); 1281 return out; 1282} 1283 1284 1285/* 1286 * If eofmark is NULL, read a word or a redirection symbol. If eofmark 1287 * is not NULL, read a here document. In the latter case, eofmark is the 1288 * word which marks the end of the document and striptabs is true if 1289 * leading tabs should be stripped from the document. The argument firstc 1290 * is the first character of the input token or document. 1291 * 1292 * Because C does not have internal subroutines, I have simulated them 1293 * using goto's to implement the subroutine linkage. The following macros 1294 * will run code that appears at the end of readtoken1. 1295 */ 1296 1297#define CHECKEND() {goto checkend; checkend_return:;} 1298#define PARSEREDIR() {goto parseredir; parseredir_return:;} 1299#define PARSESUB() {goto parsesub; parsesub_return:;} 1300#define PARSEARITH() {goto parsearith; parsearith_return:;} 1301 1302static int 1303readtoken1(int firstc, char const *initialsyntax, const char *eofmark, 1304 int striptabs) 1305{ 1306 int c = firstc; 1307 char *out; 1308 int len; 1309 char line[EOFMARKLEN + 1]; 1310 struct nodelist *bqlist; 1311 int quotef; 1312 int newvarnest; 1313 int level; 1314 int synentry; 1315 struct tokenstate state_static[MAXNEST_static]; 1316 int maxnest = MAXNEST_static; 1317 struct tokenstate *state = state_static; 1318 int sqiscstyle = 0; 1319 1320 startlinno = plinno; 1321 quotef = 0; 1322 bqlist = NULL; 1323 newvarnest = 0; 1324 level = 0; 1325 state[level].syntax = initialsyntax; 1326 state[level].parenlevel = 0; 1327 state[level].category = TSTATE_TOP; 1328 1329 STARTSTACKSTR(out); 1330 loop: { /* for each line, until end of word */ 1331 CHECKEND(); /* set c to PEOF if at end of here document */ 1332 for (;;) { /* until end of line or end of word */ 1333 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ 1334 1335 synentry = state[level].syntax[c]; 1336 1337 switch(synentry) { 1338 case CNL: /* '\n' */ 1339 if (state[level].syntax == BASESYNTAX) 1340 goto endword; /* exit outer loop */ 1341 USTPUTC(c, out); 1342 plinno++; 1343 if (doprompt) 1344 setprompt(2); 1345 else 1346 setprompt(0); 1347 c = pgetc(); 1348 goto loop; /* continue outer loop */ 1349 case CSBACK: 1350 if (sqiscstyle) { 1351 out = readcstyleesc(out); 1352 break; 1353 } 1354 /* FALLTHROUGH */ 1355 case CWORD: 1356 USTPUTC(c, out); 1357 break; 1358 case CCTL: 1359 if (eofmark == NULL || initialsyntax != SQSYNTAX) 1360 USTPUTC(CTLESC, out); 1361 USTPUTC(c, out); 1362 break; 1363 case CBACK: /* backslash */ 1364 c = pgetc(); 1365 if (c == PEOF) { 1366 USTPUTC('\\', out); 1367 pungetc(); 1368 } else if (c == '\n') { 1369 plinno++; 1370 if (doprompt) 1371 setprompt(2); 1372 else 1373 setprompt(0); 1374 } else { 1375 if (state[level].syntax == DQSYNTAX && 1376 c != '\\' && c != '`' && c != '$' && 1377 (c != '"' || (eofmark != NULL && 1378 newvarnest == 0)) && 1379 (c != '}' || state[level].category != TSTATE_VAR_OLD)) 1380 USTPUTC('\\', out); 1381 if ((eofmark == NULL || 1382 newvarnest > 0) && 1383 state[level].syntax == BASESYNTAX) 1384 USTPUTC(CTLQUOTEMARK, out); 1385 if (SQSYNTAX[c] == CCTL) 1386 USTPUTC(CTLESC, out); 1387 USTPUTC(c, out); 1388 if ((eofmark == NULL || 1389 newvarnest > 0) && 1390 state[level].syntax == BASESYNTAX && 1391 state[level].category == TSTATE_VAR_OLD) 1392 USTPUTC(CTLQUOTEEND, out); 1393 quotef++; 1394 } 1395 break; 1396 case CSQUOTE: 1397 USTPUTC(CTLQUOTEMARK, out); 1398 state[level].syntax = SQSYNTAX; 1399 sqiscstyle = 0; 1400 break; 1401 case CDQUOTE: 1402 USTPUTC(CTLQUOTEMARK, out); 1403 state[level].syntax = DQSYNTAX; 1404 break; 1405 case CENDQUOTE: 1406 if (eofmark != NULL && newvarnest == 0) 1407 USTPUTC(c, out); 1408 else { 1409 if (state[level].category == TSTATE_VAR_OLD) 1410 USTPUTC(CTLQUOTEEND, out); 1411 state[level].syntax = BASESYNTAX; 1412 quotef++; 1413 } 1414 break; 1415 case CVAR: /* '$' */ 1416 PARSESUB(); /* parse substitution */ 1417 break; 1418 case CENDVAR: /* '}' */ 1419 if (level > 0 && 1420 ((state[level].category == TSTATE_VAR_OLD && 1421 state[level].syntax == 1422 state[level - 1].syntax) || 1423 (state[level].category == TSTATE_VAR_NEW && 1424 state[level].syntax == BASESYNTAX))) { 1425 if (state[level].category == TSTATE_VAR_NEW) 1426 newvarnest--; 1427 level--; 1428 USTPUTC(CTLENDVAR, out); 1429 } else { 1430 USTPUTC(c, out); 1431 } 1432 break; 1433 case CLP: /* '(' in arithmetic */ 1434 state[level].parenlevel++; 1435 USTPUTC(c, out); 1436 break; 1437 case CRP: /* ')' in arithmetic */ 1438 if (state[level].parenlevel > 0) { 1439 USTPUTC(c, out); 1440 --state[level].parenlevel; 1441 } else { 1442 if (pgetc() == ')') { 1443 if (level > 0 && 1444 state[level].category == TSTATE_ARITH) { 1445 level--; 1446 USTPUTC(CTLENDARI, out); 1447 } else 1448 USTPUTC(')', out); 1449 } else { 1450 /* 1451 * unbalanced parens 1452 * (don't 2nd guess - no error) 1453 */ 1454 pungetc(); 1455 USTPUTC(')', out); 1456 } 1457 } 1458 break; 1459 case CBQUOTE: /* '`' */ 1460 out = parsebackq(out, &bqlist, 1, 1461 state[level].syntax == DQSYNTAX && 1462 (eofmark == NULL || newvarnest > 0), 1463 state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX); 1464 break; 1465 case CEOF: 1466 goto endword; /* exit outer loop */ 1467 case CIGN: 1468 break; 1469 default: 1470 if (level == 0) 1471 goto endword; /* exit outer loop */ 1472 USTPUTC(c, out); 1473 } 1474 c = pgetc_macro(); 1475 } 1476 } 1477endword: 1478 if (state[level].syntax == ARISYNTAX) 1479 synerror("Missing '))'"); 1480 if (state[level].syntax != BASESYNTAX && eofmark == NULL) 1481 synerror("Unterminated quoted string"); 1482 if (state[level].category == TSTATE_VAR_OLD || 1483 state[level].category == TSTATE_VAR_NEW) { 1484 startlinno = plinno; 1485 synerror("Missing '}'"); 1486 } 1487 if (state != state_static) 1488 parser_temp_free_upto(state); 1489 USTPUTC('\0', out); 1490 len = out - stackblock(); 1491 out = stackblock(); 1492 if (eofmark == NULL) { 1493 if ((c == '>' || c == '<') 1494 && quotef == 0 1495 && len <= 2 1496 && (*out == '\0' || is_digit(*out))) { 1497 PARSEREDIR(); 1498 return lasttoken = TREDIR; 1499 } else { 1500 pungetc(); 1501 } 1502 } 1503 quoteflag = quotef; 1504 backquotelist = bqlist; 1505 grabstackblock(len); 1506 wordtext = out; 1507 return lasttoken = TWORD; 1508/* end of readtoken routine */ 1509 1510 1511/* 1512 * Check to see whether we are at the end of the here document. When this 1513 * is called, c is set to the first character of the next input line. If 1514 * we are at the end of the here document, this routine sets the c to PEOF. 1515 */ 1516 1517checkend: { 1518 if (eofmark) { 1519 if (striptabs) { 1520 while (c == '\t') 1521 c = pgetc(); 1522 } 1523 if (c == *eofmark) { 1524 if (pfgets(line, sizeof line) != NULL) { 1525 const char *p, *q; 1526 1527 p = line; 1528 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 1529 if ((*p == '\0' || *p == '\n') && *q == '\0') { 1530 c = PEOF; 1531 if (*p == '\n') { 1532 plinno++; 1533 needprompt = doprompt; 1534 } 1535 } else { 1536 pushstring(line, strlen(line), NULL); 1537 } 1538 } 1539 } 1540 } 1541 goto checkend_return; 1542} 1543 1544 1545/* 1546 * Parse a redirection operator. The variable "out" points to a string 1547 * specifying the fd to be redirected. The variable "c" contains the 1548 * first character of the redirection operator. 1549 */ 1550 1551parseredir: { 1552 char fd = *out; 1553 union node *np; 1554 1555 np = (union node *)stalloc(sizeof (struct nfile)); 1556 if (c == '>') { 1557 np->nfile.fd = 1; 1558 c = pgetc(); 1559 if (c == '>') 1560 np->type = NAPPEND; 1561 else if (c == '&') 1562 np->type = NTOFD; 1563 else if (c == '|') 1564 np->type = NCLOBBER; 1565 else { 1566 np->type = NTO; 1567 pungetc(); 1568 } 1569 } else { /* c == '<' */ 1570 np->nfile.fd = 0; 1571 c = pgetc(); 1572 if (c == '<') { 1573 if (sizeof (struct nfile) != sizeof (struct nhere)) { 1574 np = (union node *)stalloc(sizeof (struct nhere)); 1575 np->nfile.fd = 0; 1576 } 1577 np->type = NHERE; 1578 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 1579 heredoc->here = np; 1580 if ((c = pgetc()) == '-') { 1581 heredoc->striptabs = 1; 1582 } else { 1583 heredoc->striptabs = 0; 1584 pungetc(); 1585 } 1586 } else if (c == '&') 1587 np->type = NFROMFD; 1588 else if (c == '>') 1589 np->type = NFROMTO; 1590 else { 1591 np->type = NFROM; 1592 pungetc(); 1593 } 1594 } 1595 if (fd != '\0') 1596 np->nfile.fd = digit_val(fd); 1597 redirnode = np; 1598 goto parseredir_return; 1599} 1600 1601 1602/* 1603 * Parse a substitution. At this point, we have read the dollar sign 1604 * and nothing else. 1605 */ 1606 1607parsesub: { 1608 char buf[10]; 1609 int subtype; 1610 int typeloc; 1611 int flags; 1612 char *p; 1613 static const char types[] = "}-+?="; 1614 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 1615 int linno; 1616 int length; 1617 int c1; 1618 1619 c = pgetc(); 1620 if (c == '(') { /* $(command) or $((arith)) */ 1621 if (pgetc() == '(') { 1622 PARSEARITH(); 1623 } else { 1624 pungetc(); 1625 out = parsebackq(out, &bqlist, 0, 1626 state[level].syntax == DQSYNTAX && 1627 (eofmark == NULL || newvarnest > 0), 1628 state[level].syntax == DQSYNTAX || 1629 state[level].syntax == ARISYNTAX); 1630 } 1631 } else if (c == '{' || is_name(c) || is_special(c)) { 1632 USTPUTC(CTLVAR, out); 1633 typeloc = out - stackblock(); 1634 USTPUTC(VSNORMAL, out); 1635 subtype = VSNORMAL; 1636 flags = 0; 1637 if (c == '{') { 1638 bracketed_name = 1; 1639 c = pgetc(); 1640 subtype = 0; 1641 } 1642varname: 1643 if (!is_eof(c) && is_name(c)) { 1644 length = 0; 1645 do { 1646 STPUTC(c, out); 1647 c = pgetc(); 1648 length++; 1649 } while (!is_eof(c) && is_in_name(c)); 1650 if (length == 6 && 1651 strncmp(out - length, "LINENO", length) == 0) { 1652 /* Replace the variable name with the 1653 * current line number. */ 1654 linno = plinno; 1655 if (funclinno != 0) 1656 linno -= funclinno - 1; 1657 snprintf(buf, sizeof(buf), "%d", linno); 1658 STADJUST(-6, out); 1659 STPUTS(buf, out); 1660 flags |= VSLINENO; 1661 } 1662 } else if (is_digit(c)) { 1663 if (bracketed_name) { 1664 do { 1665 STPUTC(c, out); 1666 c = pgetc(); 1667 } while (is_digit(c)); 1668 } else { 1669 STPUTC(c, out); 1670 c = pgetc(); 1671 } 1672 } else if (is_special(c)) { 1673 c1 = c; 1674 c = pgetc(); 1675 if (subtype == 0 && c1 == '#') { 1676 subtype = VSLENGTH; 1677 if (strchr(types, c) == NULL && c != ':' && 1678 c != '#' && c != '%') 1679 goto varname; 1680 c1 = c; 1681 c = pgetc(); 1682 if (c1 != '}' && c == '}') { 1683 pungetc(); 1684 c = c1; 1685 goto varname; 1686 } 1687 pungetc(); 1688 c = c1; 1689 c1 = '#'; 1690 subtype = 0; 1691 } 1692 USTPUTC(c1, out); 1693 } else { 1694 subtype = VSERROR; 1695 if (c == '}') 1696 pungetc(); 1697 else if (c == '\n' || c == PEOF) 1698 synerror("Unexpected end of line in substitution"); 1699 else 1700 USTPUTC(c, out); 1701 } 1702 if (subtype == 0) { 1703 switch (c) { 1704 case ':': 1705 flags |= VSNUL; 1706 c = pgetc(); 1707 /*FALLTHROUGH*/ 1708 default: 1709 p = strchr(types, c); 1710 if (p == NULL) { 1711 if (c == '\n' || c == PEOF) 1712 synerror("Unexpected end of line in substitution"); 1713 if (flags == VSNUL) 1714 STPUTC(':', out); 1715 STPUTC(c, out); 1716 subtype = VSERROR; 1717 } else 1718 subtype = p - types + VSNORMAL; 1719 break; 1720 case '%': 1721 case '#': 1722 { 1723 int cc = c; 1724 subtype = c == '#' ? VSTRIMLEFT : 1725 VSTRIMRIGHT; 1726 c = pgetc(); 1727 if (c == cc) 1728 subtype++; 1729 else 1730 pungetc(); 1731 break; 1732 } 1733 } 1734 } else if (subtype != VSERROR) { 1735 if (subtype == VSLENGTH && c != '}') 1736 subtype = VSERROR; 1737 pungetc(); 1738 } 1739 STPUTC('=', out); 1740 if (state[level].syntax == DQSYNTAX || 1741 state[level].syntax == ARISYNTAX) 1742 flags |= VSQUOTE; 1743 *(stackblock() + typeloc) = subtype | flags; 1744 if (subtype != VSNORMAL) { 1745 if (level + 1 >= maxnest) { 1746 maxnest *= 2; 1747 if (state == state_static) { 1748 state = parser_temp_alloc( 1749 maxnest * sizeof(*state)); 1750 memcpy(state, state_static, 1751 MAXNEST_static * sizeof(*state)); 1752 } else 1753 state = parser_temp_realloc(state, 1754 maxnest * sizeof(*state)); 1755 } 1756 level++; 1757 state[level].parenlevel = 0; 1758 if (subtype == VSMINUS || subtype == VSPLUS || 1759 subtype == VSQUESTION || subtype == VSASSIGN) { 1760 /* 1761 * For operators that were in the Bourne shell, 1762 * inherit the double-quote state. 1763 */ 1764 state[level].syntax = state[level - 1].syntax; 1765 state[level].category = TSTATE_VAR_OLD; 1766 } else { 1767 /* 1768 * The other operators take a pattern, 1769 * so go to BASESYNTAX. 1770 * Also, ' and " are now special, even 1771 * in here documents. 1772 */ 1773 state[level].syntax = BASESYNTAX; 1774 state[level].category = TSTATE_VAR_NEW; 1775 newvarnest++; 1776 } 1777 } 1778 } else if (c == '\'' && state[level].syntax == BASESYNTAX) { 1779 /* $'cstylequotes' */ 1780 USTPUTC(CTLQUOTEMARK, out); 1781 state[level].syntax = SQSYNTAX; 1782 sqiscstyle = 1; 1783 } else { 1784 USTPUTC('$', out); 1785 pungetc(); 1786 } 1787 goto parsesub_return; 1788} 1789 1790 1791/* 1792 * Parse an arithmetic expansion (indicate start of one and set state) 1793 */ 1794parsearith: { 1795 1796 if (level + 1 >= maxnest) { 1797 maxnest *= 2; 1798 if (state == state_static) { 1799 state = parser_temp_alloc( 1800 maxnest * sizeof(*state)); 1801 memcpy(state, state_static, 1802 MAXNEST_static * sizeof(*state)); 1803 } else 1804 state = parser_temp_realloc(state, 1805 maxnest * sizeof(*state)); 1806 } 1807 level++; 1808 state[level].syntax = ARISYNTAX; 1809 state[level].parenlevel = 0; 1810 state[level].category = TSTATE_ARITH; 1811 USTPUTC(CTLARI, out); 1812 if (state[level - 1].syntax == DQSYNTAX) 1813 USTPUTC('"',out); 1814 else 1815 USTPUTC(' ',out); 1816 goto parsearith_return; 1817} 1818 1819} /* end of readtoken */ 1820 1821 1822/* 1823 * Returns true if the text contains nothing to expand (no dollar signs 1824 * or backquotes). 1825 */ 1826 1827static int 1828noexpand(char *text) 1829{ 1830 char *p; 1831 char c; 1832 1833 p = text; 1834 while ((c = *p++) != '\0') { 1835 if ( c == CTLQUOTEMARK) 1836 continue; 1837 if (c == CTLESC) 1838 p++; 1839 else if (BASESYNTAX[(int)c] == CCTL) 1840 return 0; 1841 } 1842 return 1; 1843} 1844 1845 1846/* 1847 * Return true if the argument is a legal variable name (a letter or 1848 * underscore followed by zero or more letters, underscores, and digits). 1849 */ 1850 1851int 1852goodname(const char *name) 1853{ 1854 const char *p; 1855 1856 p = name; 1857 if (! is_name(*p)) 1858 return 0; 1859 while (*++p) { 1860 if (! is_in_name(*p)) 1861 return 0; 1862 } 1863 return 1; 1864} 1865 1866 1867int 1868isassignment(const char *p) 1869{ 1870 if (!is_name(*p)) 1871 return 0; 1872 p++; 1873 for (;;) { 1874 if (*p == '=') 1875 return 1; 1876 else if (!is_in_name(*p)) 1877 return 0; 1878 p++; 1879 } 1880} 1881 1882 1883/* 1884 * Called when an unexpected token is read during the parse. The argument 1885 * is the token that is expected, or -1 if more than one type of token can 1886 * occur at this point. 1887 */ 1888 1889static void 1890synexpect(int token) 1891{ 1892 char msg[64]; 1893 1894 if (token >= 0) { 1895 fmtstr(msg, 64, "%s unexpected (expecting %s)", 1896 tokname[lasttoken], tokname[token]); 1897 } else { 1898 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 1899 } 1900 synerror(msg); 1901} 1902 1903 1904static void 1905synerror(const char *msg) 1906{ 1907 if (commandname) 1908 outfmt(out2, "%s: %d: ", commandname, startlinno); 1909 outfmt(out2, "Syntax error: %s\n", msg); 1910 error((char *)NULL); 1911} 1912 1913static void 1914setprompt(int which) 1915{ 1916 whichprompt = which; 1917 1918#ifndef NO_HISTORY 1919 if (!el) 1920#endif 1921 { 1922 out2str(getprompt(NULL)); 1923 flushout(out2); 1924 } 1925} 1926 1927/* 1928 * called by editline -- any expansions to the prompt 1929 * should be added here. 1930 */ 1931char * 1932getprompt(void *unused __unused) 1933{ 1934 static char ps[PROMPTLEN]; 1935 char *fmt; 1936 const char *pwd; 1937 int i, trim; 1938 static char internal_error[] = "??"; 1939 1940 /* 1941 * Select prompt format. 1942 */ 1943 switch (whichprompt) { 1944 case 0: 1945 fmt = nullstr; 1946 break; 1947 case 1: 1948 fmt = ps1val(); 1949 break; 1950 case 2: 1951 fmt = ps2val(); 1952 break; 1953 default: 1954 return internal_error; 1955 } 1956 1957 /* 1958 * Format prompt string. 1959 */ 1960 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1961 if (*fmt == '\\') 1962 switch (*++fmt) { 1963 1964 /* 1965 * Hostname. 1966 * 1967 * \h specifies just the local hostname, 1968 * \H specifies fully-qualified hostname. 1969 */ 1970 case 'h': 1971 case 'H': 1972 ps[i] = '\0'; 1973 gethostname(&ps[i], PROMPTLEN - i); 1974 /* Skip to end of hostname. */ 1975 trim = (*fmt == 'h') ? '.' : '\0'; 1976 while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1977 i++; 1978 break; 1979 1980 /* 1981 * Working directory. 1982 * 1983 * \W specifies just the final component, 1984 * \w specifies the entire path. 1985 */ 1986 case 'W': 1987 case 'w': 1988 pwd = lookupvar("PWD"); 1989 if (pwd == NULL) 1990 pwd = "?"; 1991 if (*fmt == 'W' && 1992 *pwd == '/' && pwd[1] != '\0') 1993 strlcpy(&ps[i], strrchr(pwd, '/') + 1, 1994 PROMPTLEN - i); 1995 else 1996 strlcpy(&ps[i], pwd, PROMPTLEN - i); 1997 /* Skip to end of path. */ 1998 while (ps[i + 1] != '\0') 1999 i++; 2000 break; 2001 2002 /* 2003 * Superuser status. 2004 * 2005 * '$' for normal users, '#' for root. 2006 */ 2007 case '$': 2008 ps[i] = (geteuid() != 0) ? '$' : '#'; 2009 break; 2010 2011 /* 2012 * A literal \. 2013 */ 2014 case '\\': 2015 ps[i] = '\\'; 2016 break; 2017 2018 /* 2019 * Emit unrecognized formats verbatim. 2020 */ 2021 default: 2022 ps[i++] = '\\'; 2023 ps[i] = *fmt; 2024 break; 2025 } 2026 else 2027 ps[i] = *fmt; 2028 ps[i] = '\0'; 2029 return (ps); 2030} 2031 2032 2033const char * 2034expandstr(const char *ps) 2035{ 2036 union node n; 2037 struct jmploc jmploc; 2038 struct jmploc *const savehandler = handler; 2039 const int saveprompt = doprompt; 2040 struct parsefile *const savetopfile = getcurrentfile(); 2041 struct parser_temp *const saveparser_temp = parser_temp; 2042 const char *result = NULL; 2043 2044 if (!setjmp(jmploc.loc)) { 2045 handler = &jmploc; 2046 parser_temp = NULL; 2047 setinputstring(ps, 1); 2048 doprompt = 0; 2049 readtoken1(pgetc(), DQSYNTAX, "\n\n", 0); 2050 if (backquotelist != NULL) 2051 error("Command substitution not allowed here"); 2052 2053 n.narg.type = NARG; 2054 n.narg.next = NULL; 2055 n.narg.text = wordtext; 2056 n.narg.backquote = backquotelist; 2057 2058 expandarg(&n, NULL, 0); 2059 result = stackblock(); 2060 INTOFF; 2061 } 2062 handler = savehandler; 2063 doprompt = saveprompt; 2064 popfilesupto(savetopfile); 2065 if (parser_temp != saveparser_temp) { 2066 parser_temp_free_all(); 2067 parser_temp = saveparser_temp; 2068 } 2069 if (result != NULL) { 2070 INTON; 2071 } else if (exception == EXINT) 2072 raise(SIGINT); 2073 return result; 2074} 2075