11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD$"); 401556Srgrimes 4117987Speter#include <stdlib.h> 42149017Sstefanf#include <unistd.h> 43209337Sjilles#include <stdio.h> 4417987Speter 451556Srgrimes#include "shell.h" 461556Srgrimes#include "parser.h" 471556Srgrimes#include "nodes.h" 481556Srgrimes#include "expand.h" /* defines rmescapes() */ 491556Srgrimes#include "syntax.h" 501556Srgrimes#include "options.h" 511556Srgrimes#include "input.h" 521556Srgrimes#include "output.h" 531556Srgrimes#include "var.h" 541556Srgrimes#include "error.h" 551556Srgrimes#include "memalloc.h" 561556Srgrimes#include "mystring.h" 571556Srgrimes#include "alias.h" 5817987Speter#include "show.h" 5959436Scracauer#include "eval.h" 60214304Sjilles#include "exec.h" /* to check for special builtins */ 6117987Speter#ifndef NO_HISTORY 621556Srgrimes#include "myhistedit.h" 6317987Speter#endif 641556Srgrimes 651556Srgrimes/* 661556Srgrimes * Shell command parser. 671556Srgrimes */ 681556Srgrimes 69142845Sobrien#define EOFMARKLEN 79 70142845Sobrien#define PROMPTLEN 128 711556Srgrimes 72214709Sjilles/* values of checkkwd variable */ 73214709Sjilles#define CHKALIAS 0x1 74214709Sjilles#define CHKKWD 0x2 75214709Sjilles#define CHKNL 0x4 76214709Sjilles 771556Srgrimes/* values returned by readtoken */ 7817987Speter#include "token.h" 791556Srgrimes 801556Srgrimes 811556Srgrimes 821556Srgrimesstruct heredoc { 831556Srgrimes struct heredoc *next; /* next here document in list */ 841556Srgrimes union node *here; /* redirection node */ 851556Srgrimes char *eofmark; /* string indicating end of input */ 861556Srgrimes int striptabs; /* if set, strip leading tabs */ 871556Srgrimes}; 881556Srgrimes 89206145Sjillesstruct parser_temp { 90206145Sjilles struct parser_temp *next; 91206145Sjilles void *data; 92206145Sjilles}; 931556Srgrimes 941556Srgrimes 95213760Sobrienstatic struct heredoc *heredoclist; /* list of here documents to read */ 96213760Sobrienstatic int doprompt; /* if set, prompt the user */ 97213760Sobrienstatic int needprompt; /* true if interactive and at start of line */ 98213760Sobrienstatic int lasttoken; /* last token read */ 991556SrgrimesMKINIT int tokpushback; /* last token pushed back */ 100213760Sobrienstatic char *wordtext; /* text of last word returned by readtoken */ 1011556SrgrimesMKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ 102213760Sobrienstatic struct nodelist *backquotelist; 103213760Sobrienstatic union node *redirnode; 104213760Sobrienstatic struct heredoc *heredoc; 105213760Sobrienstatic int quoteflag; /* set if (part of) last token was quoted */ 106213760Sobrienstatic int startlinno; /* line # where last token started */ 107213760Sobrienstatic int funclinno; /* line # where the current function started */ 108213760Sobrienstatic struct parser_temp *parser_temp; 1091556Srgrimes 1101556Srgrimes 111214525Sjillesstatic union node *list(int, int); 112213811Sobrienstatic union node *andor(void); 113213811Sobrienstatic union node *pipeline(void); 114213811Sobrienstatic union node *command(void); 115213811Sobrienstatic union node *simplecmd(union node **, union node *); 116213811Sobrienstatic union node *makename(void); 117213811Sobrienstatic void parsefname(void); 118213811Sobrienstatic void parseheredoc(void); 119213811Sobrienstatic int peektoken(void); 120213811Sobrienstatic int readtoken(void); 121213811Sobrienstatic int xxreadtoken(void); 122213811Sobrienstatic int readtoken1(int, char const *, char *, int); 123213811Sobrienstatic int noexpand(char *); 124213811Sobrienstatic void synexpect(int) __dead2; 125213811Sobrienstatic void synerror(const char *) __dead2; 126213811Sobrienstatic void setprompt(int); 1271556Srgrimes 12817987Speter 129213811Sobrienstatic void * 130206145Sjillesparser_temp_alloc(size_t len) 131206145Sjilles{ 132206145Sjilles struct parser_temp *t; 133206145Sjilles 134206145Sjilles INTOFF; 135206145Sjilles t = ckmalloc(sizeof(*t)); 136206145Sjilles t->data = NULL; 137206145Sjilles t->next = parser_temp; 138206145Sjilles parser_temp = t; 139206145Sjilles t->data = ckmalloc(len); 140206145Sjilles INTON; 141206145Sjilles return t->data; 142206145Sjilles} 143206145Sjilles 144206145Sjilles 145213811Sobrienstatic void * 146206145Sjillesparser_temp_realloc(void *ptr, size_t len) 147206145Sjilles{ 148206145Sjilles struct parser_temp *t; 149206145Sjilles 150206145Sjilles INTOFF; 151206145Sjilles t = parser_temp; 152206145Sjilles if (ptr != t->data) 153206145Sjilles error("bug: parser_temp_realloc misused"); 154206145Sjilles t->data = ckrealloc(t->data, len); 155206145Sjilles INTON; 156206145Sjilles return t->data; 157206145Sjilles} 158206145Sjilles 159206145Sjilles 160213811Sobrienstatic void 161206145Sjillesparser_temp_free_upto(void *ptr) 162206145Sjilles{ 163206145Sjilles struct parser_temp *t; 164206145Sjilles int done = 0; 165206145Sjilles 166206145Sjilles INTOFF; 167206145Sjilles while (parser_temp != NULL && !done) { 168206145Sjilles t = parser_temp; 169206145Sjilles parser_temp = t->next; 170206145Sjilles done = t->data == ptr; 171206145Sjilles ckfree(t->data); 172206145Sjilles ckfree(t); 173206145Sjilles } 174206145Sjilles INTON; 175206145Sjilles if (!done) 176206145Sjilles error("bug: parser_temp_free_upto misused"); 177206145Sjilles} 178206145Sjilles 179206145Sjilles 180213811Sobrienstatic void 181206145Sjillesparser_temp_free_all(void) 182206145Sjilles{ 183206145Sjilles struct parser_temp *t; 184206145Sjilles 185206145Sjilles INTOFF; 186206145Sjilles while (parser_temp != NULL) { 187206145Sjilles t = parser_temp; 188206145Sjilles parser_temp = t->next; 189206145Sjilles ckfree(t->data); 190206145Sjilles ckfree(t); 191206145Sjilles } 192206145Sjilles INTON; 193206145Sjilles} 194206145Sjilles 195206145Sjilles 1961556Srgrimes/* 1971556Srgrimes * Read and parse a command. Returns NEOF on end of file. (NULL is a 1981556Srgrimes * valid parse tree indicating a blank line.) 1991556Srgrimes */ 2001556Srgrimes 2011556Srgrimesunion node * 20290111Simpparsecmd(int interact) 20317987Speter{ 2041556Srgrimes int t; 2051556Srgrimes 206206145Sjilles /* This assumes the parser is not re-entered, 207206145Sjilles * which could happen if we add command substitution on PS1/PS2. 208206145Sjilles */ 209206145Sjilles parser_temp_free_all(); 210208656Sjilles heredoclist = NULL; 211206145Sjilles 21260593Scracauer tokpushback = 0; 2131556Srgrimes doprompt = interact; 2141556Srgrimes if (doprompt) 2151556Srgrimes setprompt(1); 2161556Srgrimes else 2171556Srgrimes setprompt(0); 2181556Srgrimes needprompt = 0; 2191556Srgrimes t = readtoken(); 2201556Srgrimes if (t == TEOF) 2211556Srgrimes return NEOF; 2221556Srgrimes if (t == TNL) 2231556Srgrimes return NULL; 2241556Srgrimes tokpushback++; 225214531Sjilles return list(1, 1); 2261556Srgrimes} 2271556Srgrimes 2281556Srgrimes 229213811Sobrienstatic union node * 230214525Sjilleslist(int nlflag, int erflag) 23117987Speter{ 232214599Sjilles union node *ntop, *n1, *n2, *n3; 23317987Speter int tok; 2341556Srgrimes 235214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 236214531Sjilles if (!nlflag && !erflag && tokendlist[peektoken()]) 2371556Srgrimes return NULL; 238214599Sjilles ntop = n1 = NULL; 2391556Srgrimes for (;;) { 24017987Speter n2 = andor(); 24117987Speter tok = readtoken(); 24217987Speter if (tok == TBACKGND) { 243245690Sjilles if (n2 != NULL && n2->type == NPIPE) { 244223282Sjilles n2->npipe.backgnd = 1; 245245690Sjilles } else if (n2 != NULL && n2->type == NREDIR) { 24617987Speter n2->type = NBACKGND; 24717987Speter } else { 24817987Speter n3 = (union node *)stalloc(sizeof (struct nredir)); 24917987Speter n3->type = NBACKGND; 25017987Speter n3->nredir.n = n2; 25117987Speter n3->nredir.redirect = NULL; 25217987Speter n2 = n3; 25317987Speter } 25417987Speter } 255214599Sjilles if (ntop == NULL) 256214599Sjilles ntop = n2; 257214599Sjilles else if (n1 == NULL) { 258214599Sjilles n1 = (union node *)stalloc(sizeof (struct nbinary)); 259214599Sjilles n1->type = NSEMI; 260214599Sjilles n1->nbinary.ch1 = ntop; 261214599Sjilles n1->nbinary.ch2 = n2; 262214599Sjilles ntop = n1; 26317987Speter } 26417987Speter else { 26517987Speter n3 = (union node *)stalloc(sizeof (struct nbinary)); 26617987Speter n3->type = NSEMI; 267214599Sjilles n3->nbinary.ch1 = n1->nbinary.ch2; 26817987Speter n3->nbinary.ch2 = n2; 269214599Sjilles n1->nbinary.ch2 = n3; 27017987Speter n1 = n3; 27117987Speter } 27217987Speter switch (tok) { 27313882Sjoerg case TBACKGND: 27417987Speter case TSEMI: 27517987Speter tok = readtoken(); 276102410Scharnier /* FALLTHROUGH */ 2771556Srgrimes case TNL: 27817987Speter if (tok == TNL) { 27917987Speter parseheredoc(); 28017987Speter if (nlflag) 281214599Sjilles return ntop; 282210488Sjilles } else if (tok == TEOF && nlflag) { 283210488Sjilles parseheredoc(); 284214599Sjilles return ntop; 28517987Speter } else { 28617987Speter tokpushback++; 28717987Speter } 288214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 289214531Sjilles if (!nlflag && !erflag && tokendlist[peektoken()]) 290214599Sjilles return ntop; 2911556Srgrimes break; 2921556Srgrimes case TEOF: 2931556Srgrimes if (heredoclist) 2941556Srgrimes parseheredoc(); 2951556Srgrimes else 2961556Srgrimes pungetc(); /* push back EOF on input */ 297214599Sjilles return ntop; 2981556Srgrimes default: 299214525Sjilles if (nlflag || erflag) 3001556Srgrimes synexpect(-1); 3011556Srgrimes tokpushback++; 302214599Sjilles return ntop; 3031556Srgrimes } 3041556Srgrimes } 3051556Srgrimes} 3061556Srgrimes 3071556Srgrimes 3081556Srgrimes 309213811Sobrienstatic union node * 31090111Simpandor(void) 31190111Simp{ 3121556Srgrimes union node *n1, *n2, *n3; 3131556Srgrimes int t; 3141556Srgrimes 3151556Srgrimes n1 = pipeline(); 3161556Srgrimes for (;;) { 3171556Srgrimes if ((t = readtoken()) == TAND) { 3181556Srgrimes t = NAND; 3191556Srgrimes } else if (t == TOR) { 3201556Srgrimes t = NOR; 3211556Srgrimes } else { 3221556Srgrimes tokpushback++; 3231556Srgrimes return n1; 3241556Srgrimes } 3251556Srgrimes n2 = pipeline(); 3261556Srgrimes n3 = (union node *)stalloc(sizeof (struct nbinary)); 3271556Srgrimes n3->type = t; 3281556Srgrimes n3->nbinary.ch1 = n1; 3291556Srgrimes n3->nbinary.ch2 = n2; 3301556Srgrimes n1 = n3; 3311556Srgrimes } 3321556Srgrimes} 3331556Srgrimes 3341556Srgrimes 3351556Srgrimes 336213811Sobrienstatic union node * 33790111Simppipeline(void) 33890111Simp{ 33975336Sbrian union node *n1, *n2, *pipenode; 3401556Srgrimes struct nodelist *lp, *prev; 341214281Sjilles int negate, t; 3421556Srgrimes 34375336Sbrian negate = 0; 344214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 3451556Srgrimes TRACE(("pipeline: entered\n")); 34675336Sbrian while (readtoken() == TNOT) 34775336Sbrian negate = !negate; 34875336Sbrian tokpushback++; 3491556Srgrimes n1 = command(); 3501556Srgrimes if (readtoken() == TPIPE) { 3511556Srgrimes pipenode = (union node *)stalloc(sizeof (struct npipe)); 3521556Srgrimes pipenode->type = NPIPE; 3531556Srgrimes pipenode->npipe.backgnd = 0; 3541556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 3551556Srgrimes pipenode->npipe.cmdlist = lp; 3561556Srgrimes lp->n = n1; 3571556Srgrimes do { 3581556Srgrimes prev = lp; 3591556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 360214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 361214281Sjilles t = readtoken(); 362214281Sjilles tokpushback++; 363214281Sjilles if (t == TNOT) 364214281Sjilles lp->n = pipeline(); 365214281Sjilles else 366214281Sjilles lp->n = command(); 3671556Srgrimes prev->next = lp; 3681556Srgrimes } while (readtoken() == TPIPE); 3691556Srgrimes lp->next = NULL; 3701556Srgrimes n1 = pipenode; 3711556Srgrimes } 3721556Srgrimes tokpushback++; 37375336Sbrian if (negate) { 37475336Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 37575336Sbrian n2->type = NNOT; 37675336Sbrian n2->nnot.com = n1; 37775336Sbrian return n2; 37875336Sbrian } else 37975336Sbrian return n1; 3801556Srgrimes} 3811556Srgrimes 3821556Srgrimes 3831556Srgrimes 384213811Sobrienstatic union node * 38590111Simpcommand(void) 38690111Simp{ 3871556Srgrimes union node *n1, *n2; 3881556Srgrimes union node *ap, **app; 3891556Srgrimes union node *cp, **cpp; 3901556Srgrimes union node *redir, **rpp; 391214281Sjilles int t; 392218325Sjilles int is_subshell; 3931556Srgrimes 394214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 395218325Sjilles is_subshell = 0; 39617987Speter redir = NULL; 39717987Speter n1 = NULL; 3981556Srgrimes rpp = &redir; 39920425Ssteve 4001556Srgrimes /* Check for redirection which may precede command */ 4011556Srgrimes while (readtoken() == TREDIR) { 4021556Srgrimes *rpp = n2 = redirnode; 4031556Srgrimes rpp = &n2->nfile.next; 4041556Srgrimes parsefname(); 4051556Srgrimes } 4061556Srgrimes tokpushback++; 4071556Srgrimes 4081556Srgrimes switch (readtoken()) { 4091556Srgrimes case TIF: 4101556Srgrimes n1 = (union node *)stalloc(sizeof (struct nif)); 4111556Srgrimes n1->type = NIF; 412214525Sjilles if ((n1->nif.test = list(0, 0)) == NULL) 413104554Stjr synexpect(-1); 4141556Srgrimes if (readtoken() != TTHEN) 4151556Srgrimes synexpect(TTHEN); 416214525Sjilles n1->nif.ifpart = list(0, 0); 4171556Srgrimes n2 = n1; 4181556Srgrimes while (readtoken() == TELIF) { 4191556Srgrimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 4201556Srgrimes n2 = n2->nif.elsepart; 4211556Srgrimes n2->type = NIF; 422214525Sjilles if ((n2->nif.test = list(0, 0)) == NULL) 423104554Stjr synexpect(-1); 4241556Srgrimes if (readtoken() != TTHEN) 4251556Srgrimes synexpect(TTHEN); 426214525Sjilles n2->nif.ifpart = list(0, 0); 4271556Srgrimes } 4281556Srgrimes if (lasttoken == TELSE) 429214525Sjilles n2->nif.elsepart = list(0, 0); 4301556Srgrimes else { 4311556Srgrimes n2->nif.elsepart = NULL; 4321556Srgrimes tokpushback++; 4331556Srgrimes } 4341556Srgrimes if (readtoken() != TFI) 4351556Srgrimes synexpect(TFI); 436214709Sjilles checkkwd = CHKKWD | CHKALIAS; 4371556Srgrimes break; 4381556Srgrimes case TWHILE: 4391556Srgrimes case TUNTIL: { 4401556Srgrimes int got; 4411556Srgrimes n1 = (union node *)stalloc(sizeof (struct nbinary)); 4421556Srgrimes n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 443214525Sjilles if ((n1->nbinary.ch1 = list(0, 0)) == NULL) 444104554Stjr synexpect(-1); 4451556Srgrimes if ((got=readtoken()) != TDO) { 4461556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 4471556Srgrimes synexpect(TDO); 4481556Srgrimes } 449214525Sjilles n1->nbinary.ch2 = list(0, 0); 4501556Srgrimes if (readtoken() != TDONE) 4511556Srgrimes synexpect(TDONE); 452214709Sjilles checkkwd = CHKKWD | CHKALIAS; 4531556Srgrimes break; 4541556Srgrimes } 4551556Srgrimes case TFOR: 4561556Srgrimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 4571556Srgrimes synerror("Bad for loop variable"); 4581556Srgrimes n1 = (union node *)stalloc(sizeof (struct nfor)); 4591556Srgrimes n1->type = NFOR; 4601556Srgrimes n1->nfor.var = wordtext; 461199282Sjilles while (readtoken() == TNL) 462199282Sjilles ; 463199282Sjilles if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) { 4641556Srgrimes app = ≈ 4651556Srgrimes while (readtoken() == TWORD) { 4661556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 4671556Srgrimes n2->type = NARG; 4681556Srgrimes n2->narg.text = wordtext; 4691556Srgrimes n2->narg.backquote = backquotelist; 4701556Srgrimes *app = n2; 4711556Srgrimes app = &n2->narg.next; 4721556Srgrimes } 4731556Srgrimes *app = NULL; 4741556Srgrimes n1->nfor.args = ap; 4751556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 4761556Srgrimes synexpect(-1); 4771556Srgrimes } else { 478149096Sstefanf static char argvars[5] = { 479149096Sstefanf CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' 480149096Sstefanf }; 4811556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 4821556Srgrimes n2->type = NARG; 483149096Sstefanf n2->narg.text = argvars; 4841556Srgrimes n2->narg.backquote = NULL; 4851556Srgrimes n2->narg.next = NULL; 4861556Srgrimes n1->nfor.args = n2; 4871556Srgrimes /* 4881556Srgrimes * Newline or semicolon here is optional (but note 4891556Srgrimes * that the original Bourne shell only allowed NL). 4901556Srgrimes */ 4911556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 4921556Srgrimes tokpushback++; 4931556Srgrimes } 494214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 4951556Srgrimes if ((t = readtoken()) == TDO) 4961556Srgrimes t = TDONE; 4971556Srgrimes else if (t == TBEGIN) 4981556Srgrimes t = TEND; 4991556Srgrimes else 5001556Srgrimes synexpect(-1); 501214525Sjilles n1->nfor.body = list(0, 0); 5021556Srgrimes if (readtoken() != t) 5031556Srgrimes synexpect(t); 504214709Sjilles checkkwd = CHKKWD | CHKALIAS; 5051556Srgrimes break; 5061556Srgrimes case TCASE: 5071556Srgrimes n1 = (union node *)stalloc(sizeof (struct ncase)); 5081556Srgrimes n1->type = NCASE; 5091556Srgrimes if (readtoken() != TWORD) 5101556Srgrimes synexpect(TWORD); 5111556Srgrimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 5121556Srgrimes n2->type = NARG; 5131556Srgrimes n2->narg.text = wordtext; 5141556Srgrimes n2->narg.backquote = backquotelist; 5151556Srgrimes n2->narg.next = NULL; 5161556Srgrimes while (readtoken() == TNL); 5171556Srgrimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 5181556Srgrimes synerror("expecting \"in\""); 5191556Srgrimes cpp = &n1->ncase.cases; 520214709Sjilles checkkwd = CHKNL | CHKKWD, readtoken(); 521104202Stjr while (lasttoken != TESAC) { 5221556Srgrimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 5231556Srgrimes cp->type = NCLIST; 5241556Srgrimes app = &cp->nclist.pattern; 525104207Stjr if (lasttoken == TLP) 526104207Stjr readtoken(); 5271556Srgrimes for (;;) { 5281556Srgrimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 5291556Srgrimes ap->type = NARG; 5301556Srgrimes ap->narg.text = wordtext; 5311556Srgrimes ap->narg.backquote = backquotelist; 532214709Sjilles checkkwd = CHKNL | CHKKWD; 533214709Sjilles if (readtoken() != TPIPE) 5341556Srgrimes break; 5351556Srgrimes app = &ap->narg.next; 5362760Ssef readtoken(); 5371556Srgrimes } 5381556Srgrimes ap->narg.next = NULL; 5391556Srgrimes if (lasttoken != TRP) 540214709Sjilles synexpect(TRP); 541214525Sjilles cp->nclist.body = list(0, 0); 5422760Ssef 543214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 5442760Ssef if ((t = readtoken()) != TESAC) { 545223186Sjilles if (t == TENDCASE) 546223186Sjilles ; 547223186Sjilles else if (t == TFALLTHRU) 548223186Sjilles cp->type = NCLISTFALLTHRU; 549223186Sjilles else 550214709Sjilles synexpect(TENDCASE); 551223186Sjilles checkkwd = CHKNL | CHKKWD, readtoken(); 5522760Ssef } 5531556Srgrimes cpp = &cp->nclist.next; 554104202Stjr } 5551556Srgrimes *cpp = NULL; 556214709Sjilles checkkwd = CHKKWD | CHKALIAS; 5571556Srgrimes break; 5581556Srgrimes case TLP: 5591556Srgrimes n1 = (union node *)stalloc(sizeof (struct nredir)); 5601556Srgrimes n1->type = NSUBSHELL; 561214525Sjilles n1->nredir.n = list(0, 0); 5621556Srgrimes n1->nredir.redirect = NULL; 5631556Srgrimes if (readtoken() != TRP) 5641556Srgrimes synexpect(TRP); 565214709Sjilles checkkwd = CHKKWD | CHKALIAS; 566218325Sjilles is_subshell = 1; 5671556Srgrimes break; 5681556Srgrimes case TBEGIN: 569214525Sjilles n1 = list(0, 0); 5701556Srgrimes if (readtoken() != TEND) 5711556Srgrimes synexpect(TEND); 572214709Sjilles checkkwd = CHKKWD | CHKALIAS; 5731556Srgrimes break; 5741556Srgrimes /* Handle an empty command like other simple commands. */ 575210221Sjilles case TBACKGND: 57617987Speter case TSEMI: 577101662Stjr case TAND: 578101662Stjr case TOR: 579255065Sjilles case TPIPE: 580255065Sjilles case TENDCASE: 581255065Sjilles case TFALLTHRU: 58217987Speter /* 58317987Speter * An empty command before a ; doesn't make much sense, and 58417987Speter * should certainly be disallowed in the case of `if ;'. 58517987Speter */ 58617987Speter if (!redir) 58717987Speter synexpect(-1); 5881556Srgrimes case TNL: 58910399Sjoerg case TEOF: 5901556Srgrimes case TWORD: 59117987Speter case TRP: 5921556Srgrimes tokpushback++; 59375160Sbrian n1 = simplecmd(rpp, redir); 594214281Sjilles return n1; 5951556Srgrimes default: 5961556Srgrimes synexpect(-1); 5971556Srgrimes } 5981556Srgrimes 5991556Srgrimes /* Now check for redirection which may follow command */ 6001556Srgrimes while (readtoken() == TREDIR) { 6011556Srgrimes *rpp = n2 = redirnode; 6021556Srgrimes rpp = &n2->nfile.next; 6031556Srgrimes parsefname(); 6041556Srgrimes } 6051556Srgrimes tokpushback++; 6061556Srgrimes *rpp = NULL; 6071556Srgrimes if (redir) { 608218325Sjilles if (!is_subshell) { 6091556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 6101556Srgrimes n2->type = NREDIR; 6111556Srgrimes n2->nredir.n = n1; 6121556Srgrimes n1 = n2; 6131556Srgrimes } 6141556Srgrimes n1->nredir.redirect = redir; 6151556Srgrimes } 61675160Sbrian 617214281Sjilles return n1; 6181556Srgrimes} 6191556Srgrimes 6201556Srgrimes 621213811Sobrienstatic union node * 62290111Simpsimplecmd(union node **rpp, union node *redir) 62390111Simp{ 6241556Srgrimes union node *args, **app; 6251556Srgrimes union node **orig_rpp = rpp; 626210087Sjilles union node *n = NULL; 627214304Sjilles int special; 628222165Sjilles int savecheckkwd; 6291556Srgrimes 6301556Srgrimes /* If we don't have any redirections already, then we must reset */ 6311556Srgrimes /* rpp to be the address of the local redir variable. */ 6321556Srgrimes if (redir == 0) 6331556Srgrimes rpp = &redir; 6341556Srgrimes 6351556Srgrimes args = NULL; 6361556Srgrimes app = &args; 6378855Srgrimes /* 6381556Srgrimes * We save the incoming value, because we need this for shell 6391556Srgrimes * functions. There can not be a redirect or an argument between 6408855Srgrimes * the function name and the open parenthesis. 6411556Srgrimes */ 6421556Srgrimes orig_rpp = rpp; 6431556Srgrimes 644222165Sjilles savecheckkwd = CHKALIAS; 645222165Sjilles 6461556Srgrimes for (;;) { 647222165Sjilles checkkwd = savecheckkwd; 6481556Srgrimes if (readtoken() == TWORD) { 6491556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 6501556Srgrimes n->type = NARG; 6511556Srgrimes n->narg.text = wordtext; 6521556Srgrimes n->narg.backquote = backquotelist; 6531556Srgrimes *app = n; 6541556Srgrimes app = &n->narg.next; 655222165Sjilles if (savecheckkwd != 0 && !isassignment(wordtext)) 656222165Sjilles savecheckkwd = 0; 6571556Srgrimes } else if (lasttoken == TREDIR) { 6581556Srgrimes *rpp = n = redirnode; 6591556Srgrimes rpp = &n->nfile.next; 6601556Srgrimes parsefname(); /* read name of redirection file */ 6611556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 6621556Srgrimes && rpp == orig_rpp) { 6631556Srgrimes /* We have a function */ 6641556Srgrimes if (readtoken() != TRP) 6651556Srgrimes synexpect(TRP); 666179022Sstefanf funclinno = plinno; 667214291Sjilles /* 668214291Sjilles * - Require plain text. 669214291Sjilles * - Functions with '/' cannot be called. 670214534Sjilles * - Reject name=(). 671214534Sjilles * - Reject ksh extended glob patterns. 672214291Sjilles */ 673214291Sjilles if (!noexpand(n->narg.text) || quoteflag || 674214534Sjilles strchr(n->narg.text, '/') || 675214534Sjilles strchr("!%*+-=?@}~", 676214534Sjilles n->narg.text[strlen(n->narg.text) - 1])) 6771556Srgrimes synerror("Bad function name"); 678214291Sjilles rmescapes(n->narg.text); 679214304Sjilles if (find_builtin(n->narg.text, &special) >= 0 && 680214304Sjilles special) 681214304Sjilles synerror("Cannot override a special builtin with a function"); 6821556Srgrimes n->type = NDEFUN; 6831556Srgrimes n->narg.next = command(); 684179022Sstefanf funclinno = 0; 685210087Sjilles return n; 6861556Srgrimes } else { 6871556Srgrimes tokpushback++; 6881556Srgrimes break; 6891556Srgrimes } 6901556Srgrimes } 6911556Srgrimes *app = NULL; 6921556Srgrimes *rpp = NULL; 6931556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 6941556Srgrimes n->type = NCMD; 6951556Srgrimes n->ncmd.args = args; 6961556Srgrimes n->ncmd.redirect = redir; 697210087Sjilles return n; 6981556Srgrimes} 6991556Srgrimes 700213811Sobrienstatic union node * 70190111Simpmakename(void) 70290111Simp{ 70317987Speter union node *n; 7041556Srgrimes 70517987Speter n = (union node *)stalloc(sizeof (struct narg)); 70617987Speter n->type = NARG; 70717987Speter n->narg.next = NULL; 70817987Speter n->narg.text = wordtext; 70917987Speter n->narg.backquote = backquotelist; 71017987Speter return n; 71117987Speter} 71217987Speter 713213760Sobrienvoid 714213760Sobrienfixredir(union node *n, const char *text, int err) 71590111Simp{ 71617987Speter TRACE(("Fix redir %s %d\n", text, err)); 71717987Speter if (!err) 71817987Speter n->ndup.vname = NULL; 71917987Speter 72017987Speter if (is_digit(text[0]) && text[1] == '\0') 72117987Speter n->ndup.dupfd = digit_val(text[0]); 72217987Speter else if (text[0] == '-' && text[1] == '\0') 72317987Speter n->ndup.dupfd = -1; 72417987Speter else { 72520425Ssteve 72617987Speter if (err) 72717987Speter synerror("Bad fd number"); 72817987Speter else 72917987Speter n->ndup.vname = makename(); 73017987Speter } 73117987Speter} 73217987Speter 73317987Speter 734213811Sobrienstatic void 73590111Simpparsefname(void) 73690111Simp{ 7371556Srgrimes union node *n = redirnode; 7381556Srgrimes 7391556Srgrimes if (readtoken() != TWORD) 7401556Srgrimes synexpect(-1); 7411556Srgrimes if (n->type == NHERE) { 7421556Srgrimes struct heredoc *here = heredoc; 7431556Srgrimes struct heredoc *p; 7441556Srgrimes int i; 7451556Srgrimes 7461556Srgrimes if (quoteflag == 0) 7471556Srgrimes n->type = NXHERE; 7481556Srgrimes TRACE(("Here document %d\n", n->type)); 7491556Srgrimes if (here->striptabs) { 7501556Srgrimes while (*wordtext == '\t') 7511556Srgrimes wordtext++; 7521556Srgrimes } 7531556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 7541556Srgrimes synerror("Illegal eof marker for << redirection"); 7551556Srgrimes rmescapes(wordtext); 7561556Srgrimes here->eofmark = wordtext; 7571556Srgrimes here->next = NULL; 7581556Srgrimes if (heredoclist == NULL) 7591556Srgrimes heredoclist = here; 7601556Srgrimes else { 7611556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 7621556Srgrimes p->next = here; 7631556Srgrimes } 7641556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 76517987Speter fixredir(n, wordtext, 0); 7661556Srgrimes } else { 76717987Speter n->nfile.fname = makename(); 7681556Srgrimes } 7691556Srgrimes} 7701556Srgrimes 7711556Srgrimes 7721556Srgrimes/* 7731556Srgrimes * Input any here documents. 7741556Srgrimes */ 7751556Srgrimes 776213811Sobrienstatic void 77790111Simpparseheredoc(void) 77890111Simp{ 7791556Srgrimes struct heredoc *here; 7801556Srgrimes union node *n; 7811556Srgrimes 7821556Srgrimes while (heredoclist) { 7831556Srgrimes here = heredoclist; 7841556Srgrimes heredoclist = here->next; 7851556Srgrimes if (needprompt) { 7861556Srgrimes setprompt(2); 7871556Srgrimes needprompt = 0; 7881556Srgrimes } 7891556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 7901556Srgrimes here->eofmark, here->striptabs); 7911556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 7921556Srgrimes n->narg.type = NARG; 7931556Srgrimes n->narg.next = NULL; 7941556Srgrimes n->narg.text = wordtext; 7951556Srgrimes n->narg.backquote = backquotelist; 7961556Srgrimes here->here->nhere.doc = n; 7971556Srgrimes } 7981556Srgrimes} 7991556Srgrimes 800213811Sobrienstatic int 80190111Simppeektoken(void) 80290111Simp{ 8031556Srgrimes int t; 8041556Srgrimes 8051556Srgrimes t = readtoken(); 8061556Srgrimes tokpushback++; 8071556Srgrimes return (t); 8081556Srgrimes} 8091556Srgrimes 810213811Sobrienstatic int 81190111Simpreadtoken(void) 81290111Simp{ 8131556Srgrimes int t; 8141556Srgrimes struct alias *ap; 8151556Srgrimes#ifdef DEBUG 8161556Srgrimes int alreadyseen = tokpushback; 8171556Srgrimes#endif 8188855Srgrimes 8191556Srgrimes top: 8201556Srgrimes t = xxreadtoken(); 8211556Srgrimes 822214709Sjilles /* 823214709Sjilles * eat newlines 824214709Sjilles */ 825214709Sjilles if (checkkwd & CHKNL) { 826214709Sjilles while (t == TNL) { 827214709Sjilles parseheredoc(); 828214709Sjilles t = xxreadtoken(); 829214709Sjilles } 830214709Sjilles } 8311556Srgrimes 832214709Sjilles /* 833214709Sjilles * check for keywords and aliases 834214709Sjilles */ 835214709Sjilles if (t == TWORD && !quoteflag) 836214709Sjilles { 837214709Sjilles const char * const *pp; 838214709Sjilles 839214709Sjilles if (checkkwd & CHKKWD) 84098463Sjmallett for (pp = parsekwd; *pp; pp++) { 84120425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 84217987Speter { 8431556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 8441556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 8451556Srgrimes goto out; 8461556Srgrimes } 8471556Srgrimes } 848214709Sjilles if (checkkwd & CHKALIAS && 849214709Sjilles (ap = lookupalias(wordtext, 1)) != NULL) { 850214709Sjilles pushstring(ap->val, strlen(ap->val), ap); 851214709Sjilles goto top; 8521556Srgrimes } 853214709Sjilles } 8541556Srgrimesout: 855214709Sjilles if (t != TNOT) 856214709Sjilles checkkwd = 0; 857214709Sjilles 8581556Srgrimes#ifdef DEBUG 8591556Srgrimes if (!alreadyseen) 8601556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8611556Srgrimes else 8621556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8631556Srgrimes#endif 8641556Srgrimes return (t); 8651556Srgrimes} 8661556Srgrimes 8671556Srgrimes 8681556Srgrimes/* 8691556Srgrimes * Read the next input token. 8701556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 8711556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 8721556Srgrimes * quoted. 8731556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 8741556Srgrimes * the redirection. 8751556Srgrimes * In all cases, the variable startlinno is set to the number of the line 8761556Srgrimes * on which the token starts. 8771556Srgrimes * 8781556Srgrimes * [Change comment: here documents and internal procedures] 8791556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 8801556Srgrimes * word parsing code into a separate routine. In this case, readtoken 8811556Srgrimes * doesn't need to have any internal procedures, but parseword does. 8821556Srgrimes * We could also make parseoperator in essence the main routine, and 8831556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 8841556Srgrimes */ 8851556Srgrimes 8861556Srgrimes#define RETURN(token) return lasttoken = token 8871556Srgrimes 888213811Sobrienstatic int 88990111Simpxxreadtoken(void) 89090111Simp{ 89125230Ssteve int c; 8921556Srgrimes 8931556Srgrimes if (tokpushback) { 8941556Srgrimes tokpushback = 0; 8951556Srgrimes return lasttoken; 8961556Srgrimes } 8971556Srgrimes if (needprompt) { 8981556Srgrimes setprompt(2); 8991556Srgrimes needprompt = 0; 9001556Srgrimes } 9011556Srgrimes startlinno = plinno; 9021556Srgrimes for (;;) { /* until token or start of word found */ 9031556Srgrimes c = pgetc_macro(); 9041556Srgrimes switch (c) { 9051556Srgrimes case ' ': case '\t': 9061556Srgrimes continue; 9071556Srgrimes case '#': 9081556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 9091556Srgrimes pungetc(); 9101556Srgrimes continue; 9111556Srgrimes case '\\': 9121556Srgrimes if (pgetc() == '\n') { 9131556Srgrimes startlinno = ++plinno; 9141556Srgrimes if (doprompt) 9151556Srgrimes setprompt(2); 9161556Srgrimes else 9171556Srgrimes setprompt(0); 9181556Srgrimes continue; 9191556Srgrimes } 9201556Srgrimes pungetc(); 9211556Srgrimes goto breakloop; 9221556Srgrimes case '\n': 9231556Srgrimes plinno++; 9241556Srgrimes needprompt = doprompt; 9251556Srgrimes RETURN(TNL); 9261556Srgrimes case PEOF: 9271556Srgrimes RETURN(TEOF); 9281556Srgrimes case '&': 9291556Srgrimes if (pgetc() == '&') 9301556Srgrimes RETURN(TAND); 9311556Srgrimes pungetc(); 9321556Srgrimes RETURN(TBACKGND); 9331556Srgrimes case '|': 9341556Srgrimes if (pgetc() == '|') 9351556Srgrimes RETURN(TOR); 9361556Srgrimes pungetc(); 9371556Srgrimes RETURN(TPIPE); 9381556Srgrimes case ';': 939223186Sjilles c = pgetc(); 940223186Sjilles if (c == ';') 9411556Srgrimes RETURN(TENDCASE); 942223186Sjilles else if (c == '&') 943223186Sjilles RETURN(TFALLTHRU); 9441556Srgrimes pungetc(); 9451556Srgrimes RETURN(TSEMI); 9461556Srgrimes case '(': 9471556Srgrimes RETURN(TLP); 9481556Srgrimes case ')': 9491556Srgrimes RETURN(TRP); 9501556Srgrimes default: 9511556Srgrimes goto breakloop; 9521556Srgrimes } 9531556Srgrimes } 9541556Srgrimesbreakloop: 9551556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 9561556Srgrimes#undef RETURN 9571556Srgrimes} 9581556Srgrimes 9591556Srgrimes 960213811Sobrien#define MAXNEST_static 8 961206145Sjillesstruct tokenstate 962206145Sjilles{ 963206145Sjilles const char *syntax; /* *SYNTAX */ 964206145Sjilles int parenlevel; /* levels of parentheses in arithmetic */ 965206145Sjilles enum tokenstate_category 966206145Sjilles { 967206145Sjilles TSTATE_TOP, 968206145Sjilles TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */ 969206145Sjilles TSTATE_VAR_NEW, /* other ${var...}, own dquote state */ 970206145Sjilles TSTATE_ARITH 971206145Sjilles } category; 972206145Sjilles}; 973206145Sjilles 974206145Sjilles 975205130Sjilles/* 976205130Sjilles * Called to parse command substitutions. 977205130Sjilles */ 9781556Srgrimes 979213811Sobrienstatic char * 980205130Sjillesparsebackq(char *out, struct nodelist **pbqlist, 981205130Sjilles int oldstyle, int dblquote, int quoted) 982205130Sjilles{ 983205130Sjilles struct nodelist **nlpp; 984205130Sjilles union node *n; 985205130Sjilles char *volatile str; 986205130Sjilles struct jmploc jmploc; 987205130Sjilles struct jmploc *const savehandler = handler; 988205130Sjilles int savelen; 989205130Sjilles int saveprompt; 990205130Sjilles const int bq_startlinno = plinno; 991205130Sjilles char *volatile ostr = NULL; 992205130Sjilles struct parsefile *const savetopfile = getcurrentfile(); 993208655Sjilles struct heredoc *const saveheredoclist = heredoclist; 994208655Sjilles struct heredoc *here; 995205130Sjilles 996205130Sjilles str = NULL; 997205130Sjilles if (setjmp(jmploc.loc)) { 998205130Sjilles popfilesupto(savetopfile); 999205130Sjilles if (str) 1000205130Sjilles ckfree(str); 1001205130Sjilles if (ostr) 1002205130Sjilles ckfree(ostr); 1003208655Sjilles heredoclist = saveheredoclist; 1004205130Sjilles handler = savehandler; 1005205130Sjilles if (exception == EXERROR) { 1006205130Sjilles startlinno = bq_startlinno; 1007205130Sjilles synerror("Error in command substitution"); 1008205130Sjilles } 1009205130Sjilles longjmp(handler->loc, 1); 1010205130Sjilles } 1011205130Sjilles INTOFF; 1012205130Sjilles savelen = out - stackblock(); 1013205130Sjilles if (savelen > 0) { 1014205130Sjilles str = ckmalloc(savelen); 1015205130Sjilles memcpy(str, stackblock(), savelen); 1016205130Sjilles } 1017205130Sjilles handler = &jmploc; 1018208655Sjilles heredoclist = NULL; 1019205130Sjilles INTON; 1020205130Sjilles if (oldstyle) { 1021205130Sjilles /* We must read until the closing backquote, giving special 1022205130Sjilles treatment to some slashes, and then push the string and 1023205130Sjilles reread it as input, interpreting it normally. */ 1024205130Sjilles char *oout; 1025205130Sjilles int c; 1026205130Sjilles int olen; 1027205130Sjilles 1028205130Sjilles 1029205130Sjilles STARTSTACKSTR(oout); 1030205130Sjilles for (;;) { 1031205130Sjilles if (needprompt) { 1032205130Sjilles setprompt(2); 1033205130Sjilles needprompt = 0; 1034205130Sjilles } 1035215783Sjilles CHECKSTRSPACE(2, oout); 1036205130Sjilles switch (c = pgetc()) { 1037205130Sjilles case '`': 1038205130Sjilles goto done; 1039205130Sjilles 1040205130Sjilles case '\\': 1041205130Sjilles if ((c = pgetc()) == '\n') { 1042205130Sjilles plinno++; 1043205130Sjilles if (doprompt) 1044205130Sjilles setprompt(2); 1045205130Sjilles else 1046205130Sjilles setprompt(0); 1047205130Sjilles /* 1048205130Sjilles * If eating a newline, avoid putting 1049205130Sjilles * the newline into the new character 1050215783Sjilles * stream (via the USTPUTC after the 1051205130Sjilles * switch). 1052205130Sjilles */ 1053205130Sjilles continue; 1054205130Sjilles } 1055205130Sjilles if (c != '\\' && c != '`' && c != '$' 1056205130Sjilles && (!dblquote || c != '"')) 1057215783Sjilles USTPUTC('\\', oout); 1058205130Sjilles break; 1059205130Sjilles 1060205130Sjilles case '\n': 1061205130Sjilles plinno++; 1062205130Sjilles needprompt = doprompt; 1063205130Sjilles break; 1064205130Sjilles 1065205130Sjilles case PEOF: 1066205130Sjilles startlinno = plinno; 1067205130Sjilles synerror("EOF in backquote substitution"); 1068205130Sjilles break; 1069205130Sjilles 1070205130Sjilles default: 1071205130Sjilles break; 1072205130Sjilles } 1073215783Sjilles USTPUTC(c, oout); 1074205130Sjilles } 1075205130Sjillesdone: 1076215783Sjilles USTPUTC('\0', oout); 1077205130Sjilles olen = oout - stackblock(); 1078205130Sjilles INTOFF; 1079205130Sjilles ostr = ckmalloc(olen); 1080205130Sjilles memcpy(ostr, stackblock(), olen); 1081205130Sjilles setinputstring(ostr, 1); 1082205130Sjilles INTON; 1083205130Sjilles } 1084205130Sjilles nlpp = pbqlist; 1085205130Sjilles while (*nlpp) 1086205130Sjilles nlpp = &(*nlpp)->next; 1087205130Sjilles *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 1088205130Sjilles (*nlpp)->next = NULL; 1089205130Sjilles 1090205130Sjilles if (oldstyle) { 1091205130Sjilles saveprompt = doprompt; 1092205130Sjilles doprompt = 0; 1093205130Sjilles } 1094205130Sjilles 1095214525Sjilles n = list(0, oldstyle); 1096205130Sjilles 1097205130Sjilles if (oldstyle) 1098205130Sjilles doprompt = saveprompt; 1099205130Sjilles else { 1100205130Sjilles if (readtoken() != TRP) 1101205130Sjilles synexpect(TRP); 1102205130Sjilles } 1103205130Sjilles 1104205130Sjilles (*nlpp)->n = n; 1105205130Sjilles if (oldstyle) { 1106205130Sjilles /* 1107205130Sjilles * Start reading from old file again, ignoring any pushed back 1108205130Sjilles * tokens left from the backquote parsing 1109205130Sjilles */ 1110205130Sjilles popfile(); 1111205130Sjilles tokpushback = 0; 1112205130Sjilles } 1113205130Sjilles STARTSTACKSTR(out); 1114216706Sjilles CHECKSTRSPACE(savelen + 1, out); 1115208655Sjilles INTOFF; 1116205130Sjilles if (str) { 1117205130Sjilles memcpy(out, str, savelen); 1118205130Sjilles STADJUST(savelen, out); 1119205130Sjilles ckfree(str); 1120205130Sjilles str = NULL; 1121205130Sjilles } 1122205130Sjilles if (ostr) { 1123205130Sjilles ckfree(ostr); 1124205130Sjilles ostr = NULL; 1125205130Sjilles } 1126208655Sjilles here = saveheredoclist; 1127208655Sjilles if (here != NULL) { 1128208655Sjilles while (here->next != NULL) 1129208655Sjilles here = here->next; 1130208655Sjilles here->next = heredoclist; 1131208655Sjilles heredoclist = saveheredoclist; 1132208655Sjilles } 1133205130Sjilles handler = savehandler; 1134208655Sjilles INTON; 1135205130Sjilles if (quoted) 1136205130Sjilles USTPUTC(CTLBACKQ | CTLQUOTE, out); 1137205130Sjilles else 1138205130Sjilles USTPUTC(CTLBACKQ, out); 1139205130Sjilles return out; 1140205130Sjilles} 1141205130Sjilles 1142205130Sjilles 11431556Srgrimes/* 1144221513Sjilles * Called to parse a backslash escape sequence inside $'...'. 1145221513Sjilles * The backslash has already been read. 1146221513Sjilles */ 1147221513Sjillesstatic char * 1148221513Sjillesreadcstyleesc(char *out) 1149221513Sjilles{ 1150221513Sjilles int c, v, i, n; 1151221513Sjilles 1152221513Sjilles c = pgetc(); 1153221513Sjilles switch (c) { 1154221513Sjilles case '\0': 1155221513Sjilles synerror("Unterminated quoted string"); 1156221513Sjilles case '\n': 1157221513Sjilles plinno++; 1158221513Sjilles if (doprompt) 1159221513Sjilles setprompt(2); 1160221513Sjilles else 1161221513Sjilles setprompt(0); 1162221513Sjilles return out; 1163221513Sjilles case '\\': 1164221513Sjilles case '\'': 1165221513Sjilles case '"': 1166221513Sjilles v = c; 1167221513Sjilles break; 1168221513Sjilles case 'a': v = '\a'; break; 1169221513Sjilles case 'b': v = '\b'; break; 1170221513Sjilles case 'e': v = '\033'; break; 1171221513Sjilles case 'f': v = '\f'; break; 1172221513Sjilles case 'n': v = '\n'; break; 1173221513Sjilles case 'r': v = '\r'; break; 1174221513Sjilles case 't': v = '\t'; break; 1175221513Sjilles case 'v': v = '\v'; break; 1176221513Sjilles case 'x': 1177221513Sjilles v = 0; 1178221513Sjilles for (;;) { 1179221513Sjilles c = pgetc(); 1180221513Sjilles if (c >= '0' && c <= '9') 1181221513Sjilles v = (v << 4) + c - '0'; 1182221513Sjilles else if (c >= 'A' && c <= 'F') 1183221513Sjilles v = (v << 4) + c - 'A' + 10; 1184221513Sjilles else if (c >= 'a' && c <= 'f') 1185221513Sjilles v = (v << 4) + c - 'a' + 10; 1186221513Sjilles else 1187221513Sjilles break; 1188221513Sjilles } 1189221513Sjilles pungetc(); 1190221513Sjilles break; 1191221513Sjilles case '0': case '1': case '2': case '3': 1192221513Sjilles case '4': case '5': case '6': case '7': 1193221513Sjilles v = c - '0'; 1194221513Sjilles c = pgetc(); 1195221513Sjilles if (c >= '0' && c <= '7') { 1196221513Sjilles v <<= 3; 1197221513Sjilles v += c - '0'; 1198221513Sjilles c = pgetc(); 1199221513Sjilles if (c >= '0' && c <= '7') { 1200221513Sjilles v <<= 3; 1201221513Sjilles v += c - '0'; 1202221513Sjilles } else 1203221513Sjilles pungetc(); 1204221513Sjilles } else 1205221513Sjilles pungetc(); 1206221513Sjilles break; 1207221513Sjilles case 'c': 1208221513Sjilles c = pgetc(); 1209221513Sjilles if (c < 0x3f || c > 0x7a || c == 0x60) 1210221513Sjilles synerror("Bad escape sequence"); 1211221513Sjilles if (c == '\\' && pgetc() != '\\') 1212221513Sjilles synerror("Bad escape sequence"); 1213221513Sjilles if (c == '?') 1214221513Sjilles v = 127; 1215221513Sjilles else 1216221513Sjilles v = c & 0x1f; 1217221513Sjilles break; 1218221513Sjilles case 'u': 1219221513Sjilles case 'U': 1220221513Sjilles n = c == 'U' ? 8 : 4; 1221221513Sjilles v = 0; 1222221513Sjilles for (i = 0; i < n; i++) { 1223221513Sjilles c = pgetc(); 1224221513Sjilles if (c >= '0' && c <= '9') 1225221513Sjilles v = (v << 4) + c - '0'; 1226221513Sjilles else if (c >= 'A' && c <= 'F') 1227221513Sjilles v = (v << 4) + c - 'A' + 10; 1228221513Sjilles else if (c >= 'a' && c <= 'f') 1229221513Sjilles v = (v << 4) + c - 'a' + 10; 1230221513Sjilles else 1231221513Sjilles synerror("Bad escape sequence"); 1232221513Sjilles } 1233221513Sjilles if (v == 0 || (v >= 0xd800 && v <= 0xdfff)) 1234221513Sjilles synerror("Bad escape sequence"); 1235221513Sjilles /* We really need iconv here. */ 1236221669Sjilles if (initial_localeisutf8 && v > 127) { 1237221669Sjilles CHECKSTRSPACE(4, out); 1238221669Sjilles /* 1239221669Sjilles * We cannot use wctomb() as the locale may have 1240221669Sjilles * changed. 1241221669Sjilles */ 1242221669Sjilles if (v <= 0x7ff) { 1243221669Sjilles USTPUTC(0xc0 | v >> 6, out); 1244221669Sjilles USTPUTC(0x80 | (v & 0x3f), out); 1245221669Sjilles return out; 1246221669Sjilles } else if (v <= 0xffff) { 1247221669Sjilles USTPUTC(0xe0 | v >> 12, out); 1248221669Sjilles USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1249221669Sjilles USTPUTC(0x80 | (v & 0x3f), out); 1250221669Sjilles return out; 1251221669Sjilles } else if (v <= 0x10ffff) { 1252221669Sjilles USTPUTC(0xf0 | v >> 18, out); 1253221669Sjilles USTPUTC(0x80 | ((v >> 12) & 0x3f), out); 1254221669Sjilles USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1255221669Sjilles USTPUTC(0x80 | (v & 0x3f), out); 1256221669Sjilles return out; 1257221669Sjilles } 1258221669Sjilles } 1259221513Sjilles if (v > 127) 1260221513Sjilles v = '?'; 1261221513Sjilles break; 1262221513Sjilles default: 1263221513Sjilles synerror("Bad escape sequence"); 1264221513Sjilles } 1265221513Sjilles v = (char)v; 1266221513Sjilles /* 1267221513Sjilles * We can't handle NUL bytes. 1268221513Sjilles * POSIX says we should skip till the closing quote. 1269221513Sjilles */ 1270221513Sjilles if (v == '\0') { 1271221513Sjilles while ((c = pgetc()) != '\'') { 1272221513Sjilles if (c == '\\') 1273221513Sjilles c = pgetc(); 1274221513Sjilles if (c == PEOF) 1275221513Sjilles synerror("Unterminated quoted string"); 1276221513Sjilles } 1277221513Sjilles pungetc(); 1278221513Sjilles return out; 1279221513Sjilles } 1280221513Sjilles if (SQSYNTAX[v] == CCTL) 1281221513Sjilles USTPUTC(CTLESC, out); 1282221513Sjilles USTPUTC(v, out); 1283221513Sjilles return out; 1284221513Sjilles} 1285221513Sjilles 1286221513Sjilles 1287221513Sjilles/* 12881556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 12891556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 12901556Srgrimes * word which marks the end of the document and striptabs is true if 12911556Srgrimes * leading tabs should be stripped from the document. The argument firstc 12921556Srgrimes * is the first character of the input token or document. 12931556Srgrimes * 12941556Srgrimes * Because C does not have internal subroutines, I have simulated them 12951556Srgrimes * using goto's to implement the subroutine linkage. The following macros 12961556Srgrimes * will run code that appears at the end of readtoken1. 12971556Srgrimes */ 12981556Srgrimes 12991556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 13001556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 13011556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 13021556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 13031556Srgrimes 1304213811Sobrienstatic int 1305206145Sjillesreadtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs) 130690111Simp{ 130717987Speter int c = firstc; 130817987Speter char *out; 13091556Srgrimes int len; 13101556Srgrimes char line[EOFMARKLEN + 1]; 13111556Srgrimes struct nodelist *bqlist; 13121556Srgrimes int quotef; 1313206145Sjilles int newvarnest; 1314206145Sjilles int level; 131554679Scracauer int synentry; 1316213811Sobrien struct tokenstate state_static[MAXNEST_static]; 1317213811Sobrien int maxnest = MAXNEST_static; 1318206145Sjilles struct tokenstate *state = state_static; 1319221513Sjilles int sqiscstyle = 0; 13201556Srgrimes 13211556Srgrimes startlinno = plinno; 13221556Srgrimes quotef = 0; 13231556Srgrimes bqlist = NULL; 1324206145Sjilles newvarnest = 0; 1325206145Sjilles level = 0; 1326206145Sjilles state[level].syntax = initialsyntax; 1327206145Sjilles state[level].parenlevel = 0; 1328206145Sjilles state[level].category = TSTATE_TOP; 13291556Srgrimes 13301556Srgrimes STARTSTACKSTR(out); 13311556Srgrimes loop: { /* for each line, until end of word */ 13321556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 13331556Srgrimes for (;;) { /* until end of line or end of word */ 1334214512Sjilles CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ 133554679Scracauer 1336206145Sjilles synentry = state[level].syntax[c]; 133754679Scracauer 133854679Scracauer switch(synentry) { 13391556Srgrimes case CNL: /* '\n' */ 1340206145Sjilles if (state[level].syntax == BASESYNTAX) 13411556Srgrimes goto endword; /* exit outer loop */ 13421556Srgrimes USTPUTC(c, out); 13431556Srgrimes plinno++; 13441556Srgrimes if (doprompt) 13451556Srgrimes setprompt(2); 13461556Srgrimes else 13471556Srgrimes setprompt(0); 13481556Srgrimes c = pgetc(); 13491556Srgrimes goto loop; /* continue outer loop */ 1350221513Sjilles case CSBACK: 1351221513Sjilles if (sqiscstyle) { 1352221513Sjilles out = readcstyleesc(out); 1353221513Sjilles break; 1354221513Sjilles } 1355221513Sjilles /* FALLTHROUGH */ 13561556Srgrimes case CWORD: 13571556Srgrimes USTPUTC(c, out); 13581556Srgrimes break; 13591556Srgrimes case CCTL: 1360206145Sjilles if (eofmark == NULL || initialsyntax != SQSYNTAX) 13611556Srgrimes USTPUTC(CTLESC, out); 13621556Srgrimes USTPUTC(c, out); 13631556Srgrimes break; 13641556Srgrimes case CBACK: /* backslash */ 13651556Srgrimes c = pgetc(); 13661556Srgrimes if (c == PEOF) { 13671556Srgrimes USTPUTC('\\', out); 13681556Srgrimes pungetc(); 13691556Srgrimes } else if (c == '\n') { 1370160849Syar plinno++; 13711556Srgrimes if (doprompt) 13721556Srgrimes setprompt(2); 13731556Srgrimes else 13741556Srgrimes setprompt(0); 13751556Srgrimes } else { 1376206145Sjilles if (state[level].syntax == DQSYNTAX && 1377206145Sjilles c != '\\' && c != '`' && c != '$' && 1378206145Sjilles (c != '"' || (eofmark != NULL && 1379206145Sjilles newvarnest == 0)) && 1380206145Sjilles (c != '}' || state[level].category != TSTATE_VAR_OLD)) 13811556Srgrimes USTPUTC('\\', out); 1382214512Sjilles if ((eofmark == NULL || 1383214512Sjilles newvarnest > 0) && 1384214512Sjilles state[level].syntax == BASESYNTAX) 1385214512Sjilles USTPUTC(CTLQUOTEMARK, out); 138683675Stegge if (SQSYNTAX[c] == CCTL) 13871556Srgrimes USTPUTC(CTLESC, out); 13881556Srgrimes USTPUTC(c, out); 1389214512Sjilles if ((eofmark == NULL || 1390214512Sjilles newvarnest > 0) && 1391214512Sjilles state[level].syntax == BASESYNTAX && 1392214512Sjilles state[level].category == TSTATE_VAR_OLD) 1393214512Sjilles USTPUTC(CTLQUOTEEND, out); 13941556Srgrimes quotef++; 13951556Srgrimes } 13961556Srgrimes break; 13971556Srgrimes case CSQUOTE: 1398206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1399206145Sjilles state[level].syntax = SQSYNTAX; 1400221513Sjilles sqiscstyle = 0; 14011556Srgrimes break; 14021556Srgrimes case CDQUOTE: 1403206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1404206145Sjilles state[level].syntax = DQSYNTAX; 14051556Srgrimes break; 14061556Srgrimes case CENDQUOTE: 1407206145Sjilles if (eofmark != NULL && newvarnest == 0) 14081556Srgrimes USTPUTC(c, out); 1409206145Sjilles else { 1410214512Sjilles if (state[level].category == TSTATE_VAR_OLD) 1411214512Sjilles USTPUTC(CTLQUOTEEND, out); 1412214305Sjilles state[level].syntax = BASESYNTAX; 14131556Srgrimes quotef++; 14141556Srgrimes } 14151556Srgrimes break; 14161556Srgrimes case CVAR: /* '$' */ 14171556Srgrimes PARSESUB(); /* parse substitution */ 14181556Srgrimes break; 14191556Srgrimes case CENDVAR: /* '}' */ 1420206145Sjilles if (level > 0 && 1421214492Sjilles ((state[level].category == TSTATE_VAR_OLD && 1422214492Sjilles state[level].syntax == 1423214492Sjilles state[level - 1].syntax) || 1424214490Sjilles (state[level].category == TSTATE_VAR_NEW && 1425214490Sjilles state[level].syntax == BASESYNTAX))) { 1426214492Sjilles if (state[level].category == TSTATE_VAR_NEW) 1427206145Sjilles newvarnest--; 1428206145Sjilles level--; 14291556Srgrimes USTPUTC(CTLENDVAR, out); 14301556Srgrimes } else { 14311556Srgrimes USTPUTC(c, out); 14321556Srgrimes } 14331556Srgrimes break; 14341556Srgrimes case CLP: /* '(' in arithmetic */ 1435206145Sjilles state[level].parenlevel++; 14361556Srgrimes USTPUTC(c, out); 14371556Srgrimes break; 14381556Srgrimes case CRP: /* ')' in arithmetic */ 1439206145Sjilles if (state[level].parenlevel > 0) { 14401556Srgrimes USTPUTC(c, out); 1441206145Sjilles --state[level].parenlevel; 14421556Srgrimes } else { 14431556Srgrimes if (pgetc() == ')') { 1444206145Sjilles if (level > 0 && 1445206145Sjilles state[level].category == TSTATE_ARITH) { 1446206145Sjilles level--; 14471556Srgrimes USTPUTC(CTLENDARI, out); 14481556Srgrimes } else 14491556Srgrimes USTPUTC(')', out); 14501556Srgrimes } else { 14518855Srgrimes /* 14521556Srgrimes * unbalanced parens 14531556Srgrimes * (don't 2nd guess - no error) 14541556Srgrimes */ 14551556Srgrimes pungetc(); 14561556Srgrimes USTPUTC(')', out); 14571556Srgrimes } 14581556Srgrimes } 14591556Srgrimes break; 14601556Srgrimes case CBQUOTE: /* '`' */ 1461206145Sjilles out = parsebackq(out, &bqlist, 1, 1462206145Sjilles state[level].syntax == DQSYNTAX && 1463206145Sjilles (eofmark == NULL || newvarnest > 0), 1464206145Sjilles state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX); 14651556Srgrimes break; 14661556Srgrimes case CEOF: 14671556Srgrimes goto endword; /* exit outer loop */ 1468214305Sjilles case CIGN: 1469214305Sjilles break; 14701556Srgrimes default: 1471206145Sjilles if (level == 0) 14721556Srgrimes goto endword; /* exit outer loop */ 14731556Srgrimes USTPUTC(c, out); 14741556Srgrimes } 14751556Srgrimes c = pgetc_macro(); 14761556Srgrimes } 14771556Srgrimes } 14781556Srgrimesendword: 1479206145Sjilles if (state[level].syntax == ARISYNTAX) 14801556Srgrimes synerror("Missing '))'"); 1481206145Sjilles if (state[level].syntax != BASESYNTAX && eofmark == NULL) 14821556Srgrimes synerror("Unterminated quoted string"); 1483206145Sjilles if (state[level].category == TSTATE_VAR_OLD || 1484206145Sjilles state[level].category == TSTATE_VAR_NEW) { 14851556Srgrimes startlinno = plinno; 14861556Srgrimes synerror("Missing '}'"); 14871556Srgrimes } 1488206145Sjilles if (state != state_static) 1489206145Sjilles parser_temp_free_upto(state); 14901556Srgrimes USTPUTC('\0', out); 14911556Srgrimes len = out - stackblock(); 14921556Srgrimes out = stackblock(); 14931556Srgrimes if (eofmark == NULL) { 14941556Srgrimes if ((c == '>' || c == '<') 14951556Srgrimes && quotef == 0 14961556Srgrimes && len <= 2 14971556Srgrimes && (*out == '\0' || is_digit(*out))) { 14981556Srgrimes PARSEREDIR(); 14991556Srgrimes return lasttoken = TREDIR; 15001556Srgrimes } else { 15011556Srgrimes pungetc(); 15021556Srgrimes } 15031556Srgrimes } 15041556Srgrimes quoteflag = quotef; 15051556Srgrimes backquotelist = bqlist; 15061556Srgrimes grabstackblock(len); 15071556Srgrimes wordtext = out; 15081556Srgrimes return lasttoken = TWORD; 15091556Srgrimes/* end of readtoken routine */ 15101556Srgrimes 15111556Srgrimes 15121556Srgrimes/* 15131556Srgrimes * Check to see whether we are at the end of the here document. When this 15141556Srgrimes * is called, c is set to the first character of the next input line. If 15151556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 15161556Srgrimes */ 15171556Srgrimes 15181556Srgrimescheckend: { 15191556Srgrimes if (eofmark) { 15201556Srgrimes if (striptabs) { 15211556Srgrimes while (c == '\t') 15221556Srgrimes c = pgetc(); 15231556Srgrimes } 15241556Srgrimes if (c == *eofmark) { 15251556Srgrimes if (pfgets(line, sizeof line) != NULL) { 152625230Ssteve char *p, *q; 15271556Srgrimes 15281556Srgrimes p = line; 15291556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 1530222134Sjilles if ((*p == '\0' || *p == '\n') && *q == '\0') { 15311556Srgrimes c = PEOF; 1532222134Sjilles if (*p == '\n') { 1533222134Sjilles plinno++; 1534222134Sjilles needprompt = doprompt; 1535222134Sjilles } 15361556Srgrimes } else { 15371556Srgrimes pushstring(line, strlen(line), NULL); 15381556Srgrimes } 15391556Srgrimes } 15401556Srgrimes } 15411556Srgrimes } 15421556Srgrimes goto checkend_return; 15431556Srgrimes} 15441556Srgrimes 15451556Srgrimes 15461556Srgrimes/* 15471556Srgrimes * Parse a redirection operator. The variable "out" points to a string 15481556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 15491556Srgrimes * first character of the redirection operator. 15501556Srgrimes */ 15511556Srgrimes 15521556Srgrimesparseredir: { 15531556Srgrimes char fd = *out; 15541556Srgrimes union node *np; 15551556Srgrimes 15561556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 15571556Srgrimes if (c == '>') { 15581556Srgrimes np->nfile.fd = 1; 15591556Srgrimes c = pgetc(); 15601556Srgrimes if (c == '>') 15611556Srgrimes np->type = NAPPEND; 15621556Srgrimes else if (c == '&') 15631556Srgrimes np->type = NTOFD; 156496922Stjr else if (c == '|') 156596922Stjr np->type = NCLOBBER; 15661556Srgrimes else { 15671556Srgrimes np->type = NTO; 15681556Srgrimes pungetc(); 15691556Srgrimes } 15701556Srgrimes } else { /* c == '<' */ 15711556Srgrimes np->nfile.fd = 0; 15721556Srgrimes c = pgetc(); 15731556Srgrimes if (c == '<') { 15741556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 15751556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 15761556Srgrimes np->nfile.fd = 0; 15771556Srgrimes } 15781556Srgrimes np->type = NHERE; 15791556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 15801556Srgrimes heredoc->here = np; 15811556Srgrimes if ((c = pgetc()) == '-') { 15821556Srgrimes heredoc->striptabs = 1; 15831556Srgrimes } else { 15841556Srgrimes heredoc->striptabs = 0; 15851556Srgrimes pungetc(); 15861556Srgrimes } 15871556Srgrimes } else if (c == '&') 15881556Srgrimes np->type = NFROMFD; 158966612Sbrian else if (c == '>') 159066612Sbrian np->type = NFROMTO; 15911556Srgrimes else { 15921556Srgrimes np->type = NFROM; 15931556Srgrimes pungetc(); 15941556Srgrimes } 15951556Srgrimes } 15961556Srgrimes if (fd != '\0') 15971556Srgrimes np->nfile.fd = digit_val(fd); 15981556Srgrimes redirnode = np; 15991556Srgrimes goto parseredir_return; 16001556Srgrimes} 16011556Srgrimes 16021556Srgrimes 16031556Srgrimes/* 16041556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 16051556Srgrimes * and nothing else. 16061556Srgrimes */ 16071556Srgrimes 16081556Srgrimesparsesub: { 1609179022Sstefanf char buf[10]; 16101556Srgrimes int subtype; 16111556Srgrimes int typeloc; 16121556Srgrimes int flags; 16131556Srgrimes char *p; 16141556Srgrimes static const char types[] = "}-+?="; 1615179022Sstefanf int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 1616179022Sstefanf int linno; 1617179387Sstefanf int length; 1618219623Sjilles int c1; 16191556Srgrimes 16201556Srgrimes c = pgetc(); 1621221513Sjilles if (c == '(') { /* $(command) or $((arith)) */ 16221556Srgrimes if (pgetc() == '(') { 16231556Srgrimes PARSEARITH(); 16241556Srgrimes } else { 16251556Srgrimes pungetc(); 1626206145Sjilles out = parsebackq(out, &bqlist, 0, 1627206145Sjilles state[level].syntax == DQSYNTAX && 1628206145Sjilles (eofmark == NULL || newvarnest > 0), 1629206145Sjilles state[level].syntax == DQSYNTAX || 1630206145Sjilles state[level].syntax == ARISYNTAX); 16311556Srgrimes } 1632221513Sjilles } else if (c == '{' || is_name(c) || is_special(c)) { 16331556Srgrimes USTPUTC(CTLVAR, out); 16341556Srgrimes typeloc = out - stackblock(); 16351556Srgrimes USTPUTC(VSNORMAL, out); 16361556Srgrimes subtype = VSNORMAL; 1637179022Sstefanf flags = 0; 16381556Srgrimes if (c == '{') { 163918202Speter bracketed_name = 1; 16401556Srgrimes c = pgetc(); 1641219623Sjilles subtype = 0; 16421556Srgrimes } 1643219623Sjillesvarname: 1644149026Sstefanf if (!is_eof(c) && is_name(c)) { 1645179387Sstefanf length = 0; 16461556Srgrimes do { 16471556Srgrimes STPUTC(c, out); 16481556Srgrimes c = pgetc(); 1649179387Sstefanf length++; 1650149026Sstefanf } while (!is_eof(c) && is_in_name(c)); 1651179387Sstefanf if (length == 6 && 1652179387Sstefanf strncmp(out - length, "LINENO", length) == 0) { 1653179022Sstefanf /* Replace the variable name with the 1654179022Sstefanf * current line number. */ 1655179022Sstefanf linno = plinno; 1656179022Sstefanf if (funclinno != 0) 1657179022Sstefanf linno -= funclinno - 1; 1658179022Sstefanf snprintf(buf, sizeof(buf), "%d", linno); 1659179022Sstefanf STADJUST(-6, out); 1660215783Sjilles STPUTS(buf, out); 1661179022Sstefanf flags |= VSLINENO; 1662179022Sstefanf } 166318202Speter } else if (is_digit(c)) { 166418202Speter if (bracketed_name) { 166518202Speter do { 166618202Speter STPUTC(c, out); 166718202Speter c = pgetc(); 166818202Speter } while (is_digit(c)); 166918202Speter } else { 167018202Speter STPUTC(c, out); 167118202Speter c = pgetc(); 167218202Speter } 1673219623Sjilles } else if (is_special(c)) { 1674219623Sjilles c1 = c; 1675219623Sjilles c = pgetc(); 1676219623Sjilles if (subtype == 0 && c1 == '#') { 1677219623Sjilles subtype = VSLENGTH; 1678219623Sjilles if (strchr(types, c) == NULL && c != ':' && 1679219623Sjilles c != '#' && c != '%') 1680219623Sjilles goto varname; 1681219623Sjilles c1 = c; 1682219623Sjilles c = pgetc(); 1683219623Sjilles if (c1 != '}' && c == '}') { 1684219623Sjilles pungetc(); 1685219623Sjilles c = c1; 1686219623Sjilles goto varname; 1687219623Sjilles } 1688219623Sjilles pungetc(); 1689219623Sjilles c = c1; 1690219623Sjilles c1 = '#'; 1691219623Sjilles subtype = 0; 1692219623Sjilles } 1693219623Sjilles USTPUTC(c1, out); 16941556Srgrimes } else { 1695219623Sjilles subtype = VSERROR; 1696219623Sjilles if (c == '}') 1697219623Sjilles pungetc(); 1698219623Sjilles else if (c == '\n' || c == PEOF) 1699219623Sjilles synerror("Unexpected end of line in substitution"); 1700219623Sjilles else 1701164003Sstefanf USTPUTC(c, out); 17021556Srgrimes } 17031556Srgrimes if (subtype == 0) { 170417987Speter switch (c) { 170517987Speter case ':': 1706179022Sstefanf flags |= VSNUL; 17071556Srgrimes c = pgetc(); 170817987Speter /*FALLTHROUGH*/ 170917987Speter default: 171017987Speter p = strchr(types, c); 1711164003Sstefanf if (p == NULL) { 1712206144Sjilles if (c == '\n' || c == PEOF) 1713206144Sjilles synerror("Unexpected end of line in substitution"); 1714164003Sstefanf if (flags == VSNUL) 1715164003Sstefanf STPUTC(':', out); 1716164003Sstefanf STPUTC(c, out); 1717164003Sstefanf subtype = VSERROR; 1718164003Sstefanf } else 1719164003Sstefanf subtype = p - types + VSNORMAL; 172017987Speter break; 172117987Speter case '%': 172220425Ssteve case '#': 172317987Speter { 172417987Speter int cc = c; 172517987Speter subtype = c == '#' ? VSTRIMLEFT : 172617987Speter VSTRIMRIGHT; 172717987Speter c = pgetc(); 172817987Speter if (c == cc) 172917987Speter subtype++; 173017987Speter else 173117987Speter pungetc(); 173217987Speter break; 173317987Speter } 17341556Srgrimes } 1735164003Sstefanf } else if (subtype != VSERROR) { 1736221461Sjilles if (subtype == VSLENGTH && c != '}') 1737221461Sjilles subtype = VSERROR; 17381556Srgrimes pungetc(); 17391556Srgrimes } 1740164003Sstefanf STPUTC('=', out); 1741220903Sjilles if (state[level].syntax == DQSYNTAX || 1742220903Sjilles state[level].syntax == ARISYNTAX) 17431556Srgrimes flags |= VSQUOTE; 17441556Srgrimes *(stackblock() + typeloc) = subtype | flags; 1745206145Sjilles if (subtype != VSNORMAL) { 1746206145Sjilles if (level + 1 >= maxnest) { 1747206145Sjilles maxnest *= 2; 1748206145Sjilles if (state == state_static) { 1749206145Sjilles state = parser_temp_alloc( 1750206145Sjilles maxnest * sizeof(*state)); 1751206145Sjilles memcpy(state, state_static, 1752213811Sobrien MAXNEST_static * sizeof(*state)); 1753206145Sjilles } else 1754206145Sjilles state = parser_temp_realloc(state, 1755206145Sjilles maxnest * sizeof(*state)); 1756206145Sjilles } 1757206145Sjilles level++; 1758206145Sjilles state[level].parenlevel = 0; 1759206145Sjilles if (subtype == VSMINUS || subtype == VSPLUS || 1760206145Sjilles subtype == VSQUESTION || subtype == VSASSIGN) { 1761206145Sjilles /* 1762206145Sjilles * For operators that were in the Bourne shell, 1763206145Sjilles * inherit the double-quote state. 1764206145Sjilles */ 1765206145Sjilles state[level].syntax = state[level - 1].syntax; 1766206145Sjilles state[level].category = TSTATE_VAR_OLD; 1767206145Sjilles } else { 1768206145Sjilles /* 1769206145Sjilles * The other operators take a pattern, 1770206145Sjilles * so go to BASESYNTAX. 1771206145Sjilles * Also, ' and " are now special, even 1772206145Sjilles * in here documents. 1773206145Sjilles */ 1774206145Sjilles state[level].syntax = BASESYNTAX; 1775206145Sjilles state[level].category = TSTATE_VAR_NEW; 1776206145Sjilles newvarnest++; 1777206145Sjilles } 1778206145Sjilles } 1779221513Sjilles } else if (c == '\'' && state[level].syntax == BASESYNTAX) { 1780221513Sjilles /* $'cstylequotes' */ 1781221513Sjilles USTPUTC(CTLQUOTEMARK, out); 1782221513Sjilles state[level].syntax = SQSYNTAX; 1783221513Sjilles sqiscstyle = 1; 1784221513Sjilles } else { 1785221513Sjilles USTPUTC('$', out); 1786221513Sjilles pungetc(); 17871556Srgrimes } 17881556Srgrimes goto parsesub_return; 17891556Srgrimes} 17901556Srgrimes 17911556Srgrimes 17921556Srgrimes/* 17931556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 17941556Srgrimes */ 17951556Srgrimesparsearith: { 17961556Srgrimes 1797206145Sjilles if (level + 1 >= maxnest) { 1798206145Sjilles maxnest *= 2; 1799206145Sjilles if (state == state_static) { 1800206145Sjilles state = parser_temp_alloc( 1801206145Sjilles maxnest * sizeof(*state)); 1802206145Sjilles memcpy(state, state_static, 1803213811Sobrien MAXNEST_static * sizeof(*state)); 1804206145Sjilles } else 1805206145Sjilles state = parser_temp_realloc(state, 1806206145Sjilles maxnest * sizeof(*state)); 18071556Srgrimes } 1808206145Sjilles level++; 1809206145Sjilles state[level].syntax = ARISYNTAX; 1810206145Sjilles state[level].parenlevel = 0; 1811206145Sjilles state[level].category = TSTATE_ARITH; 1812206145Sjilles USTPUTC(CTLARI, out); 1813206145Sjilles if (state[level - 1].syntax == DQSYNTAX) 1814206145Sjilles USTPUTC('"',out); 1815206145Sjilles else 1816206145Sjilles USTPUTC(' ',out); 18171556Srgrimes goto parsearith_return; 18181556Srgrimes} 18191556Srgrimes 18201556Srgrimes} /* end of readtoken */ 18211556Srgrimes 18221556Srgrimes 18231556Srgrimes 18241556Srgrimes#ifdef mkinit 18251556SrgrimesRESET { 18261556Srgrimes tokpushback = 0; 18271556Srgrimes checkkwd = 0; 18281556Srgrimes} 18291556Srgrimes#endif 18301556Srgrimes 18311556Srgrimes/* 18321556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 18331556Srgrimes * or backquotes). 18341556Srgrimes */ 18351556Srgrimes 1836213811Sobrienstatic int 183790111Simpnoexpand(char *text) 183890111Simp{ 183925230Ssteve char *p; 184025230Ssteve char c; 18411556Srgrimes 18421556Srgrimes p = text; 18431556Srgrimes while ((c = *p++) != '\0') { 184439137Stegge if ( c == CTLQUOTEMARK) 184539137Stegge continue; 18461556Srgrimes if (c == CTLESC) 18471556Srgrimes p++; 184883675Stegge else if (BASESYNTAX[(int)c] == CCTL) 18491556Srgrimes return 0; 18501556Srgrimes } 18511556Srgrimes return 1; 18521556Srgrimes} 18531556Srgrimes 18541556Srgrimes 18551556Srgrimes/* 18561556Srgrimes * Return true if the argument is a legal variable name (a letter or 18571556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 18581556Srgrimes */ 18591556Srgrimes 18601556Srgrimesint 1861200956Sjillesgoodname(const char *name) 186290111Simp{ 1863200956Sjilles const char *p; 18641556Srgrimes 18651556Srgrimes p = name; 18661556Srgrimes if (! is_name(*p)) 18671556Srgrimes return 0; 18681556Srgrimes while (*++p) { 18691556Srgrimes if (! is_in_name(*p)) 18701556Srgrimes return 0; 18711556Srgrimes } 18721556Srgrimes return 1; 18731556Srgrimes} 18741556Srgrimes 18751556Srgrimes 1876222165Sjillesint 1877222165Sjillesisassignment(const char *p) 1878222165Sjilles{ 1879222165Sjilles if (!is_name(*p)) 1880222165Sjilles return 0; 1881222165Sjilles p++; 1882222165Sjilles for (;;) { 1883222165Sjilles if (*p == '=') 1884222165Sjilles return 1; 1885222165Sjilles else if (!is_in_name(*p)) 1886222165Sjilles return 0; 1887222165Sjilles p++; 1888222165Sjilles } 1889222165Sjilles} 1890222165Sjilles 1891222165Sjilles 18921556Srgrimes/* 18931556Srgrimes * Called when an unexpected token is read during the parse. The argument 18941556Srgrimes * is the token that is expected, or -1 if more than one type of token can 18951556Srgrimes * occur at this point. 18961556Srgrimes */ 18971556Srgrimes 1898213811Sobrienstatic void 189990111Simpsynexpect(int token) 190017987Speter{ 19011556Srgrimes char msg[64]; 19021556Srgrimes 19031556Srgrimes if (token >= 0) { 19041556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 19051556Srgrimes tokname[lasttoken], tokname[token]); 19061556Srgrimes } else { 19071556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 19081556Srgrimes } 19091556Srgrimes synerror(msg); 19101556Srgrimes} 19111556Srgrimes 19121556Srgrimes 1913213811Sobrienstatic void 1914201053Sjillessynerror(const char *msg) 191590111Simp{ 19161556Srgrimes if (commandname) 1917201366Sjilles outfmt(out2, "%s: %d: ", commandname, startlinno); 1918201366Sjilles outfmt(out2, "Syntax error: %s\n", msg); 19191556Srgrimes error((char *)NULL); 19201556Srgrimes} 19211556Srgrimes 1922213811Sobrienstatic void 192390111Simpsetprompt(int which) 192490111Simp{ 19251556Srgrimes whichprompt = which; 19261556Srgrimes 192717987Speter#ifndef NO_HISTORY 19281556Srgrimes if (!el) 192917987Speter#endif 1930199629Sjilles { 19311556Srgrimes out2str(getprompt(NULL)); 1932199629Sjilles flushout(out2); 1933199629Sjilles } 19341556Srgrimes} 19351556Srgrimes 19361556Srgrimes/* 19371556Srgrimes * called by editline -- any expansions to the prompt 19381556Srgrimes * should be added here. 19391556Srgrimes */ 19401556Srgrimeschar * 194190111Simpgetprompt(void *unused __unused) 194225905Ssteve{ 1943142845Sobrien static char ps[PROMPTLEN]; 1944142845Sobrien char *fmt; 1945209653Sjilles const char *pwd; 1946209653Sjilles int i, trim; 1947214538Sjilles static char internal_error[] = "??"; 1948142845Sobrien 1949142845Sobrien /* 1950142845Sobrien * Select prompt format. 1951142845Sobrien */ 19521556Srgrimes switch (whichprompt) { 19531556Srgrimes case 0: 1954201053Sjilles fmt = nullstr; 1955142845Sobrien break; 19561556Srgrimes case 1: 1957142845Sobrien fmt = ps1val(); 1958142845Sobrien break; 19591556Srgrimes case 2: 1960142845Sobrien fmt = ps2val(); 1961142845Sobrien break; 19621556Srgrimes default: 1963201053Sjilles return internal_error; 19641556Srgrimes } 1965142845Sobrien 1966142845Sobrien /* 1967142845Sobrien * Format prompt string. 1968142845Sobrien */ 1969142845Sobrien for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1970142845Sobrien if (*fmt == '\\') 1971142845Sobrien switch (*++fmt) { 1972142845Sobrien 1973142845Sobrien /* 1974142845Sobrien * Hostname. 1975142845Sobrien * 1976142845Sobrien * \h specifies just the local hostname, 1977142845Sobrien * \H specifies fully-qualified hostname. 1978142845Sobrien */ 1979142845Sobrien case 'h': 1980142845Sobrien case 'H': 1981149024Sstefanf ps[i] = '\0'; 1982142845Sobrien gethostname(&ps[i], PROMPTLEN - i); 1983142845Sobrien /* Skip to end of hostname. */ 1984142845Sobrien trim = (*fmt == 'h') ? '.' : '\0'; 1985142845Sobrien while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1986142845Sobrien i++; 1987142845Sobrien break; 1988142845Sobrien 1989142845Sobrien /* 1990142845Sobrien * Working directory. 1991142845Sobrien * 1992142845Sobrien * \W specifies just the final component, 1993142845Sobrien * \w specifies the entire path. 1994142845Sobrien */ 1995142845Sobrien case 'W': 1996142845Sobrien case 'w': 1997209653Sjilles pwd = lookupvar("PWD"); 1998209653Sjilles if (pwd == NULL) 1999209653Sjilles pwd = "?"; 2000209653Sjilles if (*fmt == 'W' && 2001209653Sjilles *pwd == '/' && pwd[1] != '\0') 2002209653Sjilles strlcpy(&ps[i], strrchr(pwd, '/') + 1, 2003209653Sjilles PROMPTLEN - i); 2004209653Sjilles else 2005209653Sjilles strlcpy(&ps[i], pwd, PROMPTLEN - i); 2006142845Sobrien /* Skip to end of path. */ 2007142845Sobrien while (ps[i + 1] != '\0') 2008142845Sobrien i++; 2009142845Sobrien break; 2010142845Sobrien 2011142845Sobrien /* 2012142845Sobrien * Superuser status. 2013142845Sobrien * 2014142845Sobrien * '$' for normal users, '#' for root. 2015142845Sobrien */ 2016142845Sobrien case '$': 2017142845Sobrien ps[i] = (geteuid() != 0) ? '$' : '#'; 2018142845Sobrien break; 2019142845Sobrien 2020142845Sobrien /* 2021142845Sobrien * A literal \. 2022142845Sobrien */ 2023142845Sobrien case '\\': 2024142845Sobrien ps[i] = '\\'; 2025142845Sobrien break; 2026142845Sobrien 2027142845Sobrien /* 2028142845Sobrien * Emit unrecognized formats verbatim. 2029142845Sobrien */ 2030142845Sobrien default: 2031142845Sobrien ps[i++] = '\\'; 2032142845Sobrien ps[i] = *fmt; 2033142845Sobrien break; 2034142845Sobrien } 2035142845Sobrien else 2036142845Sobrien ps[i] = *fmt; 2037142845Sobrien ps[i] = '\0'; 2038142845Sobrien return (ps); 20391556Srgrimes} 2040222907Sjilles 2041222907Sjilles 2042222907Sjillesconst char * 2043222907Sjillesexpandstr(char *ps) 2044222907Sjilles{ 2045222907Sjilles union node n; 2046222907Sjilles struct jmploc jmploc; 2047222907Sjilles struct jmploc *const savehandler = handler; 2048222907Sjilles const int saveprompt = doprompt; 2049222907Sjilles struct parsefile *const savetopfile = getcurrentfile(); 2050222907Sjilles struct parser_temp *const saveparser_temp = parser_temp; 2051222907Sjilles const char *result = NULL; 2052222907Sjilles 2053222907Sjilles if (!setjmp(jmploc.loc)) { 2054222907Sjilles handler = &jmploc; 2055222907Sjilles parser_temp = NULL; 2056222907Sjilles setinputstring(ps, 1); 2057222907Sjilles doprompt = 0; 2058222907Sjilles readtoken1(pgetc(), DQSYNTAX, "\n\n", 0); 2059222907Sjilles if (backquotelist != NULL) 2060222907Sjilles error("Command substitution not allowed here"); 2061222907Sjilles 2062222907Sjilles n.narg.type = NARG; 2063222907Sjilles n.narg.next = NULL; 2064222907Sjilles n.narg.text = wordtext; 2065222907Sjilles n.narg.backquote = backquotelist; 2066222907Sjilles 2067222907Sjilles expandarg(&n, NULL, 0); 2068222907Sjilles result = stackblock(); 2069222907Sjilles INTOFF; 2070222907Sjilles } 2071222907Sjilles handler = savehandler; 2072222907Sjilles doprompt = saveprompt; 2073222907Sjilles popfilesupto(savetopfile); 2074222907Sjilles if (parser_temp != saveparser_temp) { 2075222907Sjilles parser_temp_free_all(); 2076222907Sjilles parser_temp = saveparser_temp; 2077222907Sjilles } 2078222907Sjilles if (result != NULL) { 2079222907Sjilles INTON; 2080222907Sjilles } else if (exception == EXINT) 2081222907Sjilles raise(SIGINT); 2082222907Sjilles return result; 2083222907Sjilles} 2084