parser.c revision 223282
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: head/bin/sh/parser.c 223282 2011-06-18 23:58:59Z jilles $"); 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) { 243223282Sjilles if (n2->type == NPIPE) { 244223282Sjilles n2->npipe.backgnd = 1; 24517987Speter } else if (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: 57917987Speter /* 58017987Speter * An empty command before a ; doesn't make much sense, and 58117987Speter * should certainly be disallowed in the case of `if ;'. 58217987Speter */ 58317987Speter if (!redir) 58417987Speter synexpect(-1); 5851556Srgrimes case TNL: 58610399Sjoerg case TEOF: 5871556Srgrimes case TWORD: 58817987Speter case TRP: 5891556Srgrimes tokpushback++; 59075160Sbrian n1 = simplecmd(rpp, redir); 591214281Sjilles return n1; 5921556Srgrimes default: 5931556Srgrimes synexpect(-1); 5941556Srgrimes } 5951556Srgrimes 5961556Srgrimes /* Now check for redirection which may follow command */ 5971556Srgrimes while (readtoken() == TREDIR) { 5981556Srgrimes *rpp = n2 = redirnode; 5991556Srgrimes rpp = &n2->nfile.next; 6001556Srgrimes parsefname(); 6011556Srgrimes } 6021556Srgrimes tokpushback++; 6031556Srgrimes *rpp = NULL; 6041556Srgrimes if (redir) { 605218325Sjilles if (!is_subshell) { 6061556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 6071556Srgrimes n2->type = NREDIR; 6081556Srgrimes n2->nredir.n = n1; 6091556Srgrimes n1 = n2; 6101556Srgrimes } 6111556Srgrimes n1->nredir.redirect = redir; 6121556Srgrimes } 61375160Sbrian 614214281Sjilles return n1; 6151556Srgrimes} 6161556Srgrimes 6171556Srgrimes 618213811Sobrienstatic union node * 61990111Simpsimplecmd(union node **rpp, union node *redir) 62090111Simp{ 6211556Srgrimes union node *args, **app; 6221556Srgrimes union node **orig_rpp = rpp; 623210087Sjilles union node *n = NULL; 624214304Sjilles int special; 625222165Sjilles int savecheckkwd; 6261556Srgrimes 6271556Srgrimes /* If we don't have any redirections already, then we must reset */ 6281556Srgrimes /* rpp to be the address of the local redir variable. */ 6291556Srgrimes if (redir == 0) 6301556Srgrimes rpp = &redir; 6311556Srgrimes 6321556Srgrimes args = NULL; 6331556Srgrimes app = &args; 6348855Srgrimes /* 6351556Srgrimes * We save the incoming value, because we need this for shell 6361556Srgrimes * functions. There can not be a redirect or an argument between 6378855Srgrimes * the function name and the open parenthesis. 6381556Srgrimes */ 6391556Srgrimes orig_rpp = rpp; 6401556Srgrimes 641222165Sjilles savecheckkwd = CHKALIAS; 642222165Sjilles 6431556Srgrimes for (;;) { 644222165Sjilles checkkwd = savecheckkwd; 6451556Srgrimes if (readtoken() == TWORD) { 6461556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 6471556Srgrimes n->type = NARG; 6481556Srgrimes n->narg.text = wordtext; 6491556Srgrimes n->narg.backquote = backquotelist; 6501556Srgrimes *app = n; 6511556Srgrimes app = &n->narg.next; 652222165Sjilles if (savecheckkwd != 0 && !isassignment(wordtext)) 653222165Sjilles savecheckkwd = 0; 6541556Srgrimes } else if (lasttoken == TREDIR) { 6551556Srgrimes *rpp = n = redirnode; 6561556Srgrimes rpp = &n->nfile.next; 6571556Srgrimes parsefname(); /* read name of redirection file */ 6581556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 6591556Srgrimes && rpp == orig_rpp) { 6601556Srgrimes /* We have a function */ 6611556Srgrimes if (readtoken() != TRP) 6621556Srgrimes synexpect(TRP); 663179022Sstefanf funclinno = plinno; 664214291Sjilles /* 665214291Sjilles * - Require plain text. 666214291Sjilles * - Functions with '/' cannot be called. 667214534Sjilles * - Reject name=(). 668214534Sjilles * - Reject ksh extended glob patterns. 669214291Sjilles */ 670214291Sjilles if (!noexpand(n->narg.text) || quoteflag || 671214534Sjilles strchr(n->narg.text, '/') || 672214534Sjilles strchr("!%*+-=?@}~", 673214534Sjilles n->narg.text[strlen(n->narg.text) - 1])) 6741556Srgrimes synerror("Bad function name"); 675214291Sjilles rmescapes(n->narg.text); 676214304Sjilles if (find_builtin(n->narg.text, &special) >= 0 && 677214304Sjilles special) 678214304Sjilles synerror("Cannot override a special builtin with a function"); 6791556Srgrimes n->type = NDEFUN; 6801556Srgrimes n->narg.next = command(); 681179022Sstefanf funclinno = 0; 682210087Sjilles return n; 6831556Srgrimes } else { 6841556Srgrimes tokpushback++; 6851556Srgrimes break; 6861556Srgrimes } 6871556Srgrimes } 6881556Srgrimes *app = NULL; 6891556Srgrimes *rpp = NULL; 6901556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 6911556Srgrimes n->type = NCMD; 6921556Srgrimes n->ncmd.args = args; 6931556Srgrimes n->ncmd.redirect = redir; 694210087Sjilles return n; 6951556Srgrimes} 6961556Srgrimes 697213811Sobrienstatic union node * 69890111Simpmakename(void) 69990111Simp{ 70017987Speter union node *n; 7011556Srgrimes 70217987Speter n = (union node *)stalloc(sizeof (struct narg)); 70317987Speter n->type = NARG; 70417987Speter n->narg.next = NULL; 70517987Speter n->narg.text = wordtext; 70617987Speter n->narg.backquote = backquotelist; 70717987Speter return n; 70817987Speter} 70917987Speter 710213760Sobrienvoid 711213760Sobrienfixredir(union node *n, const char *text, int err) 71290111Simp{ 71317987Speter TRACE(("Fix redir %s %d\n", text, err)); 71417987Speter if (!err) 71517987Speter n->ndup.vname = NULL; 71617987Speter 71717987Speter if (is_digit(text[0]) && text[1] == '\0') 71817987Speter n->ndup.dupfd = digit_val(text[0]); 71917987Speter else if (text[0] == '-' && text[1] == '\0') 72017987Speter n->ndup.dupfd = -1; 72117987Speter else { 72220425Ssteve 72317987Speter if (err) 72417987Speter synerror("Bad fd number"); 72517987Speter else 72617987Speter n->ndup.vname = makename(); 72717987Speter } 72817987Speter} 72917987Speter 73017987Speter 731213811Sobrienstatic void 73290111Simpparsefname(void) 73390111Simp{ 7341556Srgrimes union node *n = redirnode; 7351556Srgrimes 7361556Srgrimes if (readtoken() != TWORD) 7371556Srgrimes synexpect(-1); 7381556Srgrimes if (n->type == NHERE) { 7391556Srgrimes struct heredoc *here = heredoc; 7401556Srgrimes struct heredoc *p; 7411556Srgrimes int i; 7421556Srgrimes 7431556Srgrimes if (quoteflag == 0) 7441556Srgrimes n->type = NXHERE; 7451556Srgrimes TRACE(("Here document %d\n", n->type)); 7461556Srgrimes if (here->striptabs) { 7471556Srgrimes while (*wordtext == '\t') 7481556Srgrimes wordtext++; 7491556Srgrimes } 7501556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 7511556Srgrimes synerror("Illegal eof marker for << redirection"); 7521556Srgrimes rmescapes(wordtext); 7531556Srgrimes here->eofmark = wordtext; 7541556Srgrimes here->next = NULL; 7551556Srgrimes if (heredoclist == NULL) 7561556Srgrimes heredoclist = here; 7571556Srgrimes else { 7581556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 7591556Srgrimes p->next = here; 7601556Srgrimes } 7611556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 76217987Speter fixredir(n, wordtext, 0); 7631556Srgrimes } else { 76417987Speter n->nfile.fname = makename(); 7651556Srgrimes } 7661556Srgrimes} 7671556Srgrimes 7681556Srgrimes 7691556Srgrimes/* 7701556Srgrimes * Input any here documents. 7711556Srgrimes */ 7721556Srgrimes 773213811Sobrienstatic void 77490111Simpparseheredoc(void) 77590111Simp{ 7761556Srgrimes struct heredoc *here; 7771556Srgrimes union node *n; 7781556Srgrimes 7791556Srgrimes while (heredoclist) { 7801556Srgrimes here = heredoclist; 7811556Srgrimes heredoclist = here->next; 7821556Srgrimes if (needprompt) { 7831556Srgrimes setprompt(2); 7841556Srgrimes needprompt = 0; 7851556Srgrimes } 7861556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 7871556Srgrimes here->eofmark, here->striptabs); 7881556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 7891556Srgrimes n->narg.type = NARG; 7901556Srgrimes n->narg.next = NULL; 7911556Srgrimes n->narg.text = wordtext; 7921556Srgrimes n->narg.backquote = backquotelist; 7931556Srgrimes here->here->nhere.doc = n; 7941556Srgrimes } 7951556Srgrimes} 7961556Srgrimes 797213811Sobrienstatic int 79890111Simppeektoken(void) 79990111Simp{ 8001556Srgrimes int t; 8011556Srgrimes 8021556Srgrimes t = readtoken(); 8031556Srgrimes tokpushback++; 8041556Srgrimes return (t); 8051556Srgrimes} 8061556Srgrimes 807213811Sobrienstatic int 80890111Simpreadtoken(void) 80990111Simp{ 8101556Srgrimes int t; 8111556Srgrimes struct alias *ap; 8121556Srgrimes#ifdef DEBUG 8131556Srgrimes int alreadyseen = tokpushback; 8141556Srgrimes#endif 8158855Srgrimes 8161556Srgrimes top: 8171556Srgrimes t = xxreadtoken(); 8181556Srgrimes 819214709Sjilles /* 820214709Sjilles * eat newlines 821214709Sjilles */ 822214709Sjilles if (checkkwd & CHKNL) { 823214709Sjilles while (t == TNL) { 824214709Sjilles parseheredoc(); 825214709Sjilles t = xxreadtoken(); 826214709Sjilles } 827214709Sjilles } 8281556Srgrimes 829214709Sjilles /* 830214709Sjilles * check for keywords and aliases 831214709Sjilles */ 832214709Sjilles if (t == TWORD && !quoteflag) 833214709Sjilles { 834214709Sjilles const char * const *pp; 835214709Sjilles 836214709Sjilles if (checkkwd & CHKKWD) 83798463Sjmallett for (pp = parsekwd; *pp; pp++) { 83820425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 83917987Speter { 8401556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 8411556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 8421556Srgrimes goto out; 8431556Srgrimes } 8441556Srgrimes } 845214709Sjilles if (checkkwd & CHKALIAS && 846214709Sjilles (ap = lookupalias(wordtext, 1)) != NULL) { 847214709Sjilles pushstring(ap->val, strlen(ap->val), ap); 848214709Sjilles goto top; 8491556Srgrimes } 850214709Sjilles } 8511556Srgrimesout: 852214709Sjilles if (t != TNOT) 853214709Sjilles checkkwd = 0; 854214709Sjilles 8551556Srgrimes#ifdef DEBUG 8561556Srgrimes if (!alreadyseen) 8571556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8581556Srgrimes else 8591556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8601556Srgrimes#endif 8611556Srgrimes return (t); 8621556Srgrimes} 8631556Srgrimes 8641556Srgrimes 8651556Srgrimes/* 8661556Srgrimes * Read the next input token. 8671556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 8681556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 8691556Srgrimes * quoted. 8701556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 8711556Srgrimes * the redirection. 8721556Srgrimes * In all cases, the variable startlinno is set to the number of the line 8731556Srgrimes * on which the token starts. 8741556Srgrimes * 8751556Srgrimes * [Change comment: here documents and internal procedures] 8761556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 8771556Srgrimes * word parsing code into a separate routine. In this case, readtoken 8781556Srgrimes * doesn't need to have any internal procedures, but parseword does. 8791556Srgrimes * We could also make parseoperator in essence the main routine, and 8801556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 8811556Srgrimes */ 8821556Srgrimes 8831556Srgrimes#define RETURN(token) return lasttoken = token 8841556Srgrimes 885213811Sobrienstatic int 88690111Simpxxreadtoken(void) 88790111Simp{ 88825230Ssteve int c; 8891556Srgrimes 8901556Srgrimes if (tokpushback) { 8911556Srgrimes tokpushback = 0; 8921556Srgrimes return lasttoken; 8931556Srgrimes } 8941556Srgrimes if (needprompt) { 8951556Srgrimes setprompt(2); 8961556Srgrimes needprompt = 0; 8971556Srgrimes } 8981556Srgrimes startlinno = plinno; 8991556Srgrimes for (;;) { /* until token or start of word found */ 9001556Srgrimes c = pgetc_macro(); 9011556Srgrimes switch (c) { 9021556Srgrimes case ' ': case '\t': 9031556Srgrimes continue; 9041556Srgrimes case '#': 9051556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 9061556Srgrimes pungetc(); 9071556Srgrimes continue; 9081556Srgrimes case '\\': 9091556Srgrimes if (pgetc() == '\n') { 9101556Srgrimes startlinno = ++plinno; 9111556Srgrimes if (doprompt) 9121556Srgrimes setprompt(2); 9131556Srgrimes else 9141556Srgrimes setprompt(0); 9151556Srgrimes continue; 9161556Srgrimes } 9171556Srgrimes pungetc(); 9181556Srgrimes goto breakloop; 9191556Srgrimes case '\n': 9201556Srgrimes plinno++; 9211556Srgrimes needprompt = doprompt; 9221556Srgrimes RETURN(TNL); 9231556Srgrimes case PEOF: 9241556Srgrimes RETURN(TEOF); 9251556Srgrimes case '&': 9261556Srgrimes if (pgetc() == '&') 9271556Srgrimes RETURN(TAND); 9281556Srgrimes pungetc(); 9291556Srgrimes RETURN(TBACKGND); 9301556Srgrimes case '|': 9311556Srgrimes if (pgetc() == '|') 9321556Srgrimes RETURN(TOR); 9331556Srgrimes pungetc(); 9341556Srgrimes RETURN(TPIPE); 9351556Srgrimes case ';': 936223186Sjilles c = pgetc(); 937223186Sjilles if (c == ';') 9381556Srgrimes RETURN(TENDCASE); 939223186Sjilles else if (c == '&') 940223186Sjilles RETURN(TFALLTHRU); 9411556Srgrimes pungetc(); 9421556Srgrimes RETURN(TSEMI); 9431556Srgrimes case '(': 9441556Srgrimes RETURN(TLP); 9451556Srgrimes case ')': 9461556Srgrimes RETURN(TRP); 9471556Srgrimes default: 9481556Srgrimes goto breakloop; 9491556Srgrimes } 9501556Srgrimes } 9511556Srgrimesbreakloop: 9521556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 9531556Srgrimes#undef RETURN 9541556Srgrimes} 9551556Srgrimes 9561556Srgrimes 957213811Sobrien#define MAXNEST_static 8 958206145Sjillesstruct tokenstate 959206145Sjilles{ 960206145Sjilles const char *syntax; /* *SYNTAX */ 961206145Sjilles int parenlevel; /* levels of parentheses in arithmetic */ 962206145Sjilles enum tokenstate_category 963206145Sjilles { 964206145Sjilles TSTATE_TOP, 965206145Sjilles TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */ 966206145Sjilles TSTATE_VAR_NEW, /* other ${var...}, own dquote state */ 967206145Sjilles TSTATE_ARITH 968206145Sjilles } category; 969206145Sjilles}; 970206145Sjilles 971206145Sjilles 972205130Sjilles/* 973205130Sjilles * Called to parse command substitutions. 974205130Sjilles */ 9751556Srgrimes 976213811Sobrienstatic char * 977205130Sjillesparsebackq(char *out, struct nodelist **pbqlist, 978205130Sjilles int oldstyle, int dblquote, int quoted) 979205130Sjilles{ 980205130Sjilles struct nodelist **nlpp; 981205130Sjilles union node *n; 982205130Sjilles char *volatile str; 983205130Sjilles struct jmploc jmploc; 984205130Sjilles struct jmploc *const savehandler = handler; 985205130Sjilles int savelen; 986205130Sjilles int saveprompt; 987205130Sjilles const int bq_startlinno = plinno; 988205130Sjilles char *volatile ostr = NULL; 989205130Sjilles struct parsefile *const savetopfile = getcurrentfile(); 990208655Sjilles struct heredoc *const saveheredoclist = heredoclist; 991208655Sjilles struct heredoc *here; 992205130Sjilles 993205130Sjilles str = NULL; 994205130Sjilles if (setjmp(jmploc.loc)) { 995205130Sjilles popfilesupto(savetopfile); 996205130Sjilles if (str) 997205130Sjilles ckfree(str); 998205130Sjilles if (ostr) 999205130Sjilles ckfree(ostr); 1000208655Sjilles heredoclist = saveheredoclist; 1001205130Sjilles handler = savehandler; 1002205130Sjilles if (exception == EXERROR) { 1003205130Sjilles startlinno = bq_startlinno; 1004205130Sjilles synerror("Error in command substitution"); 1005205130Sjilles } 1006205130Sjilles longjmp(handler->loc, 1); 1007205130Sjilles } 1008205130Sjilles INTOFF; 1009205130Sjilles savelen = out - stackblock(); 1010205130Sjilles if (savelen > 0) { 1011205130Sjilles str = ckmalloc(savelen); 1012205130Sjilles memcpy(str, stackblock(), savelen); 1013205130Sjilles } 1014205130Sjilles handler = &jmploc; 1015208655Sjilles heredoclist = NULL; 1016205130Sjilles INTON; 1017205130Sjilles if (oldstyle) { 1018205130Sjilles /* We must read until the closing backquote, giving special 1019205130Sjilles treatment to some slashes, and then push the string and 1020205130Sjilles reread it as input, interpreting it normally. */ 1021205130Sjilles char *oout; 1022205130Sjilles int c; 1023205130Sjilles int olen; 1024205130Sjilles 1025205130Sjilles 1026205130Sjilles STARTSTACKSTR(oout); 1027205130Sjilles for (;;) { 1028205130Sjilles if (needprompt) { 1029205130Sjilles setprompt(2); 1030205130Sjilles needprompt = 0; 1031205130Sjilles } 1032215783Sjilles CHECKSTRSPACE(2, oout); 1033205130Sjilles switch (c = pgetc()) { 1034205130Sjilles case '`': 1035205130Sjilles goto done; 1036205130Sjilles 1037205130Sjilles case '\\': 1038205130Sjilles if ((c = pgetc()) == '\n') { 1039205130Sjilles plinno++; 1040205130Sjilles if (doprompt) 1041205130Sjilles setprompt(2); 1042205130Sjilles else 1043205130Sjilles setprompt(0); 1044205130Sjilles /* 1045205130Sjilles * If eating a newline, avoid putting 1046205130Sjilles * the newline into the new character 1047215783Sjilles * stream (via the USTPUTC after the 1048205130Sjilles * switch). 1049205130Sjilles */ 1050205130Sjilles continue; 1051205130Sjilles } 1052205130Sjilles if (c != '\\' && c != '`' && c != '$' 1053205130Sjilles && (!dblquote || c != '"')) 1054215783Sjilles USTPUTC('\\', oout); 1055205130Sjilles break; 1056205130Sjilles 1057205130Sjilles case '\n': 1058205130Sjilles plinno++; 1059205130Sjilles needprompt = doprompt; 1060205130Sjilles break; 1061205130Sjilles 1062205130Sjilles case PEOF: 1063205130Sjilles startlinno = plinno; 1064205130Sjilles synerror("EOF in backquote substitution"); 1065205130Sjilles break; 1066205130Sjilles 1067205130Sjilles default: 1068205130Sjilles break; 1069205130Sjilles } 1070215783Sjilles USTPUTC(c, oout); 1071205130Sjilles } 1072205130Sjillesdone: 1073215783Sjilles USTPUTC('\0', oout); 1074205130Sjilles olen = oout - stackblock(); 1075205130Sjilles INTOFF; 1076205130Sjilles ostr = ckmalloc(olen); 1077205130Sjilles memcpy(ostr, stackblock(), olen); 1078205130Sjilles setinputstring(ostr, 1); 1079205130Sjilles INTON; 1080205130Sjilles } 1081205130Sjilles nlpp = pbqlist; 1082205130Sjilles while (*nlpp) 1083205130Sjilles nlpp = &(*nlpp)->next; 1084205130Sjilles *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 1085205130Sjilles (*nlpp)->next = NULL; 1086205130Sjilles 1087205130Sjilles if (oldstyle) { 1088205130Sjilles saveprompt = doprompt; 1089205130Sjilles doprompt = 0; 1090205130Sjilles } 1091205130Sjilles 1092214525Sjilles n = list(0, oldstyle); 1093205130Sjilles 1094205130Sjilles if (oldstyle) 1095205130Sjilles doprompt = saveprompt; 1096205130Sjilles else { 1097205130Sjilles if (readtoken() != TRP) 1098205130Sjilles synexpect(TRP); 1099205130Sjilles } 1100205130Sjilles 1101205130Sjilles (*nlpp)->n = n; 1102205130Sjilles if (oldstyle) { 1103205130Sjilles /* 1104205130Sjilles * Start reading from old file again, ignoring any pushed back 1105205130Sjilles * tokens left from the backquote parsing 1106205130Sjilles */ 1107205130Sjilles popfile(); 1108205130Sjilles tokpushback = 0; 1109205130Sjilles } 1110205130Sjilles STARTSTACKSTR(out); 1111216706Sjilles CHECKSTRSPACE(savelen + 1, out); 1112208655Sjilles INTOFF; 1113205130Sjilles if (str) { 1114205130Sjilles memcpy(out, str, savelen); 1115205130Sjilles STADJUST(savelen, out); 1116205130Sjilles ckfree(str); 1117205130Sjilles str = NULL; 1118205130Sjilles } 1119205130Sjilles if (ostr) { 1120205130Sjilles ckfree(ostr); 1121205130Sjilles ostr = NULL; 1122205130Sjilles } 1123208655Sjilles here = saveheredoclist; 1124208655Sjilles if (here != NULL) { 1125208655Sjilles while (here->next != NULL) 1126208655Sjilles here = here->next; 1127208655Sjilles here->next = heredoclist; 1128208655Sjilles heredoclist = saveheredoclist; 1129208655Sjilles } 1130205130Sjilles handler = savehandler; 1131208655Sjilles INTON; 1132205130Sjilles if (quoted) 1133205130Sjilles USTPUTC(CTLBACKQ | CTLQUOTE, out); 1134205130Sjilles else 1135205130Sjilles USTPUTC(CTLBACKQ, out); 1136205130Sjilles return out; 1137205130Sjilles} 1138205130Sjilles 1139205130Sjilles 11401556Srgrimes/* 1141221513Sjilles * Called to parse a backslash escape sequence inside $'...'. 1142221513Sjilles * The backslash has already been read. 1143221513Sjilles */ 1144221513Sjillesstatic char * 1145221513Sjillesreadcstyleesc(char *out) 1146221513Sjilles{ 1147221513Sjilles int c, v, i, n; 1148221513Sjilles 1149221513Sjilles c = pgetc(); 1150221513Sjilles switch (c) { 1151221513Sjilles case '\0': 1152221513Sjilles synerror("Unterminated quoted string"); 1153221513Sjilles case '\n': 1154221513Sjilles plinno++; 1155221513Sjilles if (doprompt) 1156221513Sjilles setprompt(2); 1157221513Sjilles else 1158221513Sjilles setprompt(0); 1159221513Sjilles return out; 1160221513Sjilles case '\\': 1161221513Sjilles case '\'': 1162221513Sjilles case '"': 1163221513Sjilles v = c; 1164221513Sjilles break; 1165221513Sjilles case 'a': v = '\a'; break; 1166221513Sjilles case 'b': v = '\b'; break; 1167221513Sjilles case 'e': v = '\033'; break; 1168221513Sjilles case 'f': v = '\f'; break; 1169221513Sjilles case 'n': v = '\n'; break; 1170221513Sjilles case 'r': v = '\r'; break; 1171221513Sjilles case 't': v = '\t'; break; 1172221513Sjilles case 'v': v = '\v'; break; 1173221513Sjilles case 'x': 1174221513Sjilles v = 0; 1175221513Sjilles for (;;) { 1176221513Sjilles c = pgetc(); 1177221513Sjilles if (c >= '0' && c <= '9') 1178221513Sjilles v = (v << 4) + c - '0'; 1179221513Sjilles else if (c >= 'A' && c <= 'F') 1180221513Sjilles v = (v << 4) + c - 'A' + 10; 1181221513Sjilles else if (c >= 'a' && c <= 'f') 1182221513Sjilles v = (v << 4) + c - 'a' + 10; 1183221513Sjilles else 1184221513Sjilles break; 1185221513Sjilles } 1186221513Sjilles pungetc(); 1187221513Sjilles break; 1188221513Sjilles case '0': case '1': case '2': case '3': 1189221513Sjilles case '4': case '5': case '6': case '7': 1190221513Sjilles v = c - '0'; 1191221513Sjilles c = pgetc(); 1192221513Sjilles if (c >= '0' && c <= '7') { 1193221513Sjilles v <<= 3; 1194221513Sjilles v += c - '0'; 1195221513Sjilles c = pgetc(); 1196221513Sjilles if (c >= '0' && c <= '7') { 1197221513Sjilles v <<= 3; 1198221513Sjilles v += c - '0'; 1199221513Sjilles } else 1200221513Sjilles pungetc(); 1201221513Sjilles } else 1202221513Sjilles pungetc(); 1203221513Sjilles break; 1204221513Sjilles case 'c': 1205221513Sjilles c = pgetc(); 1206221513Sjilles if (c < 0x3f || c > 0x7a || c == 0x60) 1207221513Sjilles synerror("Bad escape sequence"); 1208221513Sjilles if (c == '\\' && pgetc() != '\\') 1209221513Sjilles synerror("Bad escape sequence"); 1210221513Sjilles if (c == '?') 1211221513Sjilles v = 127; 1212221513Sjilles else 1213221513Sjilles v = c & 0x1f; 1214221513Sjilles break; 1215221513Sjilles case 'u': 1216221513Sjilles case 'U': 1217221513Sjilles n = c == 'U' ? 8 : 4; 1218221513Sjilles v = 0; 1219221513Sjilles for (i = 0; i < n; i++) { 1220221513Sjilles c = pgetc(); 1221221513Sjilles if (c >= '0' && c <= '9') 1222221513Sjilles v = (v << 4) + c - '0'; 1223221513Sjilles else if (c >= 'A' && c <= 'F') 1224221513Sjilles v = (v << 4) + c - 'A' + 10; 1225221513Sjilles else if (c >= 'a' && c <= 'f') 1226221513Sjilles v = (v << 4) + c - 'a' + 10; 1227221513Sjilles else 1228221513Sjilles synerror("Bad escape sequence"); 1229221513Sjilles } 1230221513Sjilles if (v == 0 || (v >= 0xd800 && v <= 0xdfff)) 1231221513Sjilles synerror("Bad escape sequence"); 1232221513Sjilles /* We really need iconv here. */ 1233221669Sjilles if (initial_localeisutf8 && v > 127) { 1234221669Sjilles CHECKSTRSPACE(4, out); 1235221669Sjilles /* 1236221669Sjilles * We cannot use wctomb() as the locale may have 1237221669Sjilles * changed. 1238221669Sjilles */ 1239221669Sjilles if (v <= 0x7ff) { 1240221669Sjilles USTPUTC(0xc0 | v >> 6, out); 1241221669Sjilles USTPUTC(0x80 | (v & 0x3f), out); 1242221669Sjilles return out; 1243221669Sjilles } else if (v <= 0xffff) { 1244221669Sjilles USTPUTC(0xe0 | v >> 12, out); 1245221669Sjilles USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1246221669Sjilles USTPUTC(0x80 | (v & 0x3f), out); 1247221669Sjilles return out; 1248221669Sjilles } else if (v <= 0x10ffff) { 1249221669Sjilles USTPUTC(0xf0 | v >> 18, out); 1250221669Sjilles USTPUTC(0x80 | ((v >> 12) & 0x3f), out); 1251221669Sjilles USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1252221669Sjilles USTPUTC(0x80 | (v & 0x3f), out); 1253221669Sjilles return out; 1254221669Sjilles } 1255221669Sjilles } 1256221513Sjilles if (v > 127) 1257221513Sjilles v = '?'; 1258221513Sjilles break; 1259221513Sjilles default: 1260221513Sjilles synerror("Bad escape sequence"); 1261221513Sjilles } 1262221513Sjilles v = (char)v; 1263221513Sjilles /* 1264221513Sjilles * We can't handle NUL bytes. 1265221513Sjilles * POSIX says we should skip till the closing quote. 1266221513Sjilles */ 1267221513Sjilles if (v == '\0') { 1268221513Sjilles while ((c = pgetc()) != '\'') { 1269221513Sjilles if (c == '\\') 1270221513Sjilles c = pgetc(); 1271221513Sjilles if (c == PEOF) 1272221513Sjilles synerror("Unterminated quoted string"); 1273221513Sjilles } 1274221513Sjilles pungetc(); 1275221513Sjilles return out; 1276221513Sjilles } 1277221513Sjilles if (SQSYNTAX[v] == CCTL) 1278221513Sjilles USTPUTC(CTLESC, out); 1279221513Sjilles USTPUTC(v, out); 1280221513Sjilles return out; 1281221513Sjilles} 1282221513Sjilles 1283221513Sjilles 1284221513Sjilles/* 12851556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 12861556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 12871556Srgrimes * word which marks the end of the document and striptabs is true if 12881556Srgrimes * leading tabs should be stripped from the document. The argument firstc 12891556Srgrimes * is the first character of the input token or document. 12901556Srgrimes * 12911556Srgrimes * Because C does not have internal subroutines, I have simulated them 12921556Srgrimes * using goto's to implement the subroutine linkage. The following macros 12931556Srgrimes * will run code that appears at the end of readtoken1. 12941556Srgrimes */ 12951556Srgrimes 12961556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 12971556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 12981556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 12991556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 13001556Srgrimes 1301213811Sobrienstatic int 1302206145Sjillesreadtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs) 130390111Simp{ 130417987Speter int c = firstc; 130517987Speter char *out; 13061556Srgrimes int len; 13071556Srgrimes char line[EOFMARKLEN + 1]; 13081556Srgrimes struct nodelist *bqlist; 13091556Srgrimes int quotef; 1310206145Sjilles int newvarnest; 1311206145Sjilles int level; 131254679Scracauer int synentry; 1313213811Sobrien struct tokenstate state_static[MAXNEST_static]; 1314213811Sobrien int maxnest = MAXNEST_static; 1315206145Sjilles struct tokenstate *state = state_static; 1316221513Sjilles int sqiscstyle = 0; 13171556Srgrimes 13181556Srgrimes startlinno = plinno; 13191556Srgrimes quotef = 0; 13201556Srgrimes bqlist = NULL; 1321206145Sjilles newvarnest = 0; 1322206145Sjilles level = 0; 1323206145Sjilles state[level].syntax = initialsyntax; 1324206145Sjilles state[level].parenlevel = 0; 1325206145Sjilles state[level].category = TSTATE_TOP; 13261556Srgrimes 13271556Srgrimes STARTSTACKSTR(out); 13281556Srgrimes loop: { /* for each line, until end of word */ 13291556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 13301556Srgrimes for (;;) { /* until end of line or end of word */ 1331214512Sjilles CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ 133254679Scracauer 1333206145Sjilles synentry = state[level].syntax[c]; 133454679Scracauer 133554679Scracauer switch(synentry) { 13361556Srgrimes case CNL: /* '\n' */ 1337206145Sjilles if (state[level].syntax == BASESYNTAX) 13381556Srgrimes goto endword; /* exit outer loop */ 13391556Srgrimes USTPUTC(c, out); 13401556Srgrimes plinno++; 13411556Srgrimes if (doprompt) 13421556Srgrimes setprompt(2); 13431556Srgrimes else 13441556Srgrimes setprompt(0); 13451556Srgrimes c = pgetc(); 13461556Srgrimes goto loop; /* continue outer loop */ 1347221513Sjilles case CSBACK: 1348221513Sjilles if (sqiscstyle) { 1349221513Sjilles out = readcstyleesc(out); 1350221513Sjilles break; 1351221513Sjilles } 1352221513Sjilles /* FALLTHROUGH */ 13531556Srgrimes case CWORD: 13541556Srgrimes USTPUTC(c, out); 13551556Srgrimes break; 13561556Srgrimes case CCTL: 1357206145Sjilles if (eofmark == NULL || initialsyntax != SQSYNTAX) 13581556Srgrimes USTPUTC(CTLESC, out); 13591556Srgrimes USTPUTC(c, out); 13601556Srgrimes break; 13611556Srgrimes case CBACK: /* backslash */ 13621556Srgrimes c = pgetc(); 13631556Srgrimes if (c == PEOF) { 13641556Srgrimes USTPUTC('\\', out); 13651556Srgrimes pungetc(); 13661556Srgrimes } else if (c == '\n') { 1367160849Syar plinno++; 13681556Srgrimes if (doprompt) 13691556Srgrimes setprompt(2); 13701556Srgrimes else 13711556Srgrimes setprompt(0); 13721556Srgrimes } else { 1373206145Sjilles if (state[level].syntax == DQSYNTAX && 1374206145Sjilles c != '\\' && c != '`' && c != '$' && 1375206145Sjilles (c != '"' || (eofmark != NULL && 1376206145Sjilles newvarnest == 0)) && 1377206145Sjilles (c != '}' || state[level].category != TSTATE_VAR_OLD)) 13781556Srgrimes USTPUTC('\\', out); 1379214512Sjilles if ((eofmark == NULL || 1380214512Sjilles newvarnest > 0) && 1381214512Sjilles state[level].syntax == BASESYNTAX) 1382214512Sjilles USTPUTC(CTLQUOTEMARK, out); 138383675Stegge if (SQSYNTAX[c] == CCTL) 13841556Srgrimes USTPUTC(CTLESC, out); 13851556Srgrimes USTPUTC(c, out); 1386214512Sjilles if ((eofmark == NULL || 1387214512Sjilles newvarnest > 0) && 1388214512Sjilles state[level].syntax == BASESYNTAX && 1389214512Sjilles state[level].category == TSTATE_VAR_OLD) 1390214512Sjilles USTPUTC(CTLQUOTEEND, out); 13911556Srgrimes quotef++; 13921556Srgrimes } 13931556Srgrimes break; 13941556Srgrimes case CSQUOTE: 1395206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1396206145Sjilles state[level].syntax = SQSYNTAX; 1397221513Sjilles sqiscstyle = 0; 13981556Srgrimes break; 13991556Srgrimes case CDQUOTE: 1400206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1401206145Sjilles state[level].syntax = DQSYNTAX; 14021556Srgrimes break; 14031556Srgrimes case CENDQUOTE: 1404206145Sjilles if (eofmark != NULL && newvarnest == 0) 14051556Srgrimes USTPUTC(c, out); 1406206145Sjilles else { 1407214512Sjilles if (state[level].category == TSTATE_VAR_OLD) 1408214512Sjilles USTPUTC(CTLQUOTEEND, out); 1409214305Sjilles state[level].syntax = BASESYNTAX; 14101556Srgrimes quotef++; 14111556Srgrimes } 14121556Srgrimes break; 14131556Srgrimes case CVAR: /* '$' */ 14141556Srgrimes PARSESUB(); /* parse substitution */ 14151556Srgrimes break; 14161556Srgrimes case CENDVAR: /* '}' */ 1417206145Sjilles if (level > 0 && 1418214492Sjilles ((state[level].category == TSTATE_VAR_OLD && 1419214492Sjilles state[level].syntax == 1420214492Sjilles state[level - 1].syntax) || 1421214490Sjilles (state[level].category == TSTATE_VAR_NEW && 1422214490Sjilles state[level].syntax == BASESYNTAX))) { 1423214492Sjilles if (state[level].category == TSTATE_VAR_NEW) 1424206145Sjilles newvarnest--; 1425206145Sjilles level--; 14261556Srgrimes USTPUTC(CTLENDVAR, out); 14271556Srgrimes } else { 14281556Srgrimes USTPUTC(c, out); 14291556Srgrimes } 14301556Srgrimes break; 14311556Srgrimes case CLP: /* '(' in arithmetic */ 1432206145Sjilles state[level].parenlevel++; 14331556Srgrimes USTPUTC(c, out); 14341556Srgrimes break; 14351556Srgrimes case CRP: /* ')' in arithmetic */ 1436206145Sjilles if (state[level].parenlevel > 0) { 14371556Srgrimes USTPUTC(c, out); 1438206145Sjilles --state[level].parenlevel; 14391556Srgrimes } else { 14401556Srgrimes if (pgetc() == ')') { 1441206145Sjilles if (level > 0 && 1442206145Sjilles state[level].category == TSTATE_ARITH) { 1443206145Sjilles level--; 14441556Srgrimes USTPUTC(CTLENDARI, out); 14451556Srgrimes } else 14461556Srgrimes USTPUTC(')', out); 14471556Srgrimes } else { 14488855Srgrimes /* 14491556Srgrimes * unbalanced parens 14501556Srgrimes * (don't 2nd guess - no error) 14511556Srgrimes */ 14521556Srgrimes pungetc(); 14531556Srgrimes USTPUTC(')', out); 14541556Srgrimes } 14551556Srgrimes } 14561556Srgrimes break; 14571556Srgrimes case CBQUOTE: /* '`' */ 1458206145Sjilles out = parsebackq(out, &bqlist, 1, 1459206145Sjilles state[level].syntax == DQSYNTAX && 1460206145Sjilles (eofmark == NULL || newvarnest > 0), 1461206145Sjilles state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX); 14621556Srgrimes break; 14631556Srgrimes case CEOF: 14641556Srgrimes goto endword; /* exit outer loop */ 1465214305Sjilles case CIGN: 1466214305Sjilles break; 14671556Srgrimes default: 1468206145Sjilles if (level == 0) 14691556Srgrimes goto endword; /* exit outer loop */ 14701556Srgrimes USTPUTC(c, out); 14711556Srgrimes } 14721556Srgrimes c = pgetc_macro(); 14731556Srgrimes } 14741556Srgrimes } 14751556Srgrimesendword: 1476206145Sjilles if (state[level].syntax == ARISYNTAX) 14771556Srgrimes synerror("Missing '))'"); 1478206145Sjilles if (state[level].syntax != BASESYNTAX && eofmark == NULL) 14791556Srgrimes synerror("Unterminated quoted string"); 1480206145Sjilles if (state[level].category == TSTATE_VAR_OLD || 1481206145Sjilles state[level].category == TSTATE_VAR_NEW) { 14821556Srgrimes startlinno = plinno; 14831556Srgrimes synerror("Missing '}'"); 14841556Srgrimes } 1485206145Sjilles if (state != state_static) 1486206145Sjilles parser_temp_free_upto(state); 14871556Srgrimes USTPUTC('\0', out); 14881556Srgrimes len = out - stackblock(); 14891556Srgrimes out = stackblock(); 14901556Srgrimes if (eofmark == NULL) { 14911556Srgrimes if ((c == '>' || c == '<') 14921556Srgrimes && quotef == 0 14931556Srgrimes && len <= 2 14941556Srgrimes && (*out == '\0' || is_digit(*out))) { 14951556Srgrimes PARSEREDIR(); 14961556Srgrimes return lasttoken = TREDIR; 14971556Srgrimes } else { 14981556Srgrimes pungetc(); 14991556Srgrimes } 15001556Srgrimes } 15011556Srgrimes quoteflag = quotef; 15021556Srgrimes backquotelist = bqlist; 15031556Srgrimes grabstackblock(len); 15041556Srgrimes wordtext = out; 15051556Srgrimes return lasttoken = TWORD; 15061556Srgrimes/* end of readtoken routine */ 15071556Srgrimes 15081556Srgrimes 15091556Srgrimes/* 15101556Srgrimes * Check to see whether we are at the end of the here document. When this 15111556Srgrimes * is called, c is set to the first character of the next input line. If 15121556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 15131556Srgrimes */ 15141556Srgrimes 15151556Srgrimescheckend: { 15161556Srgrimes if (eofmark) { 15171556Srgrimes if (striptabs) { 15181556Srgrimes while (c == '\t') 15191556Srgrimes c = pgetc(); 15201556Srgrimes } 15211556Srgrimes if (c == *eofmark) { 15221556Srgrimes if (pfgets(line, sizeof line) != NULL) { 152325230Ssteve char *p, *q; 15241556Srgrimes 15251556Srgrimes p = line; 15261556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 1527222134Sjilles if ((*p == '\0' || *p == '\n') && *q == '\0') { 15281556Srgrimes c = PEOF; 1529222134Sjilles if (*p == '\n') { 1530222134Sjilles plinno++; 1531222134Sjilles needprompt = doprompt; 1532222134Sjilles } 15331556Srgrimes } else { 15341556Srgrimes pushstring(line, strlen(line), NULL); 15351556Srgrimes } 15361556Srgrimes } 15371556Srgrimes } 15381556Srgrimes } 15391556Srgrimes goto checkend_return; 15401556Srgrimes} 15411556Srgrimes 15421556Srgrimes 15431556Srgrimes/* 15441556Srgrimes * Parse a redirection operator. The variable "out" points to a string 15451556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 15461556Srgrimes * first character of the redirection operator. 15471556Srgrimes */ 15481556Srgrimes 15491556Srgrimesparseredir: { 15501556Srgrimes char fd = *out; 15511556Srgrimes union node *np; 15521556Srgrimes 15531556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 15541556Srgrimes if (c == '>') { 15551556Srgrimes np->nfile.fd = 1; 15561556Srgrimes c = pgetc(); 15571556Srgrimes if (c == '>') 15581556Srgrimes np->type = NAPPEND; 15591556Srgrimes else if (c == '&') 15601556Srgrimes np->type = NTOFD; 156196922Stjr else if (c == '|') 156296922Stjr np->type = NCLOBBER; 15631556Srgrimes else { 15641556Srgrimes np->type = NTO; 15651556Srgrimes pungetc(); 15661556Srgrimes } 15671556Srgrimes } else { /* c == '<' */ 15681556Srgrimes np->nfile.fd = 0; 15691556Srgrimes c = pgetc(); 15701556Srgrimes if (c == '<') { 15711556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 15721556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 15731556Srgrimes np->nfile.fd = 0; 15741556Srgrimes } 15751556Srgrimes np->type = NHERE; 15761556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 15771556Srgrimes heredoc->here = np; 15781556Srgrimes if ((c = pgetc()) == '-') { 15791556Srgrimes heredoc->striptabs = 1; 15801556Srgrimes } else { 15811556Srgrimes heredoc->striptabs = 0; 15821556Srgrimes pungetc(); 15831556Srgrimes } 15841556Srgrimes } else if (c == '&') 15851556Srgrimes np->type = NFROMFD; 158666612Sbrian else if (c == '>') 158766612Sbrian np->type = NFROMTO; 15881556Srgrimes else { 15891556Srgrimes np->type = NFROM; 15901556Srgrimes pungetc(); 15911556Srgrimes } 15921556Srgrimes } 15931556Srgrimes if (fd != '\0') 15941556Srgrimes np->nfile.fd = digit_val(fd); 15951556Srgrimes redirnode = np; 15961556Srgrimes goto parseredir_return; 15971556Srgrimes} 15981556Srgrimes 15991556Srgrimes 16001556Srgrimes/* 16011556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 16021556Srgrimes * and nothing else. 16031556Srgrimes */ 16041556Srgrimes 16051556Srgrimesparsesub: { 1606179022Sstefanf char buf[10]; 16071556Srgrimes int subtype; 16081556Srgrimes int typeloc; 16091556Srgrimes int flags; 16101556Srgrimes char *p; 16111556Srgrimes static const char types[] = "}-+?="; 1612179022Sstefanf int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 1613179022Sstefanf int linno; 1614179387Sstefanf int length; 1615219623Sjilles int c1; 16161556Srgrimes 16171556Srgrimes c = pgetc(); 1618221513Sjilles if (c == '(') { /* $(command) or $((arith)) */ 16191556Srgrimes if (pgetc() == '(') { 16201556Srgrimes PARSEARITH(); 16211556Srgrimes } else { 16221556Srgrimes pungetc(); 1623206145Sjilles out = parsebackq(out, &bqlist, 0, 1624206145Sjilles state[level].syntax == DQSYNTAX && 1625206145Sjilles (eofmark == NULL || newvarnest > 0), 1626206145Sjilles state[level].syntax == DQSYNTAX || 1627206145Sjilles state[level].syntax == ARISYNTAX); 16281556Srgrimes } 1629221513Sjilles } else if (c == '{' || is_name(c) || is_special(c)) { 16301556Srgrimes USTPUTC(CTLVAR, out); 16311556Srgrimes typeloc = out - stackblock(); 16321556Srgrimes USTPUTC(VSNORMAL, out); 16331556Srgrimes subtype = VSNORMAL; 1634179022Sstefanf flags = 0; 16351556Srgrimes if (c == '{') { 163618202Speter bracketed_name = 1; 16371556Srgrimes c = pgetc(); 1638219623Sjilles subtype = 0; 16391556Srgrimes } 1640219623Sjillesvarname: 1641149026Sstefanf if (!is_eof(c) && is_name(c)) { 1642179387Sstefanf length = 0; 16431556Srgrimes do { 16441556Srgrimes STPUTC(c, out); 16451556Srgrimes c = pgetc(); 1646179387Sstefanf length++; 1647149026Sstefanf } while (!is_eof(c) && is_in_name(c)); 1648179387Sstefanf if (length == 6 && 1649179387Sstefanf strncmp(out - length, "LINENO", length) == 0) { 1650179022Sstefanf /* Replace the variable name with the 1651179022Sstefanf * current line number. */ 1652179022Sstefanf linno = plinno; 1653179022Sstefanf if (funclinno != 0) 1654179022Sstefanf linno -= funclinno - 1; 1655179022Sstefanf snprintf(buf, sizeof(buf), "%d", linno); 1656179022Sstefanf STADJUST(-6, out); 1657215783Sjilles STPUTS(buf, out); 1658179022Sstefanf flags |= VSLINENO; 1659179022Sstefanf } 166018202Speter } else if (is_digit(c)) { 166118202Speter if (bracketed_name) { 166218202Speter do { 166318202Speter STPUTC(c, out); 166418202Speter c = pgetc(); 166518202Speter } while (is_digit(c)); 166618202Speter } else { 166718202Speter STPUTC(c, out); 166818202Speter c = pgetc(); 166918202Speter } 1670219623Sjilles } else if (is_special(c)) { 1671219623Sjilles c1 = c; 1672219623Sjilles c = pgetc(); 1673219623Sjilles if (subtype == 0 && c1 == '#') { 1674219623Sjilles subtype = VSLENGTH; 1675219623Sjilles if (strchr(types, c) == NULL && c != ':' && 1676219623Sjilles c != '#' && c != '%') 1677219623Sjilles goto varname; 1678219623Sjilles c1 = c; 1679219623Sjilles c = pgetc(); 1680219623Sjilles if (c1 != '}' && c == '}') { 1681219623Sjilles pungetc(); 1682219623Sjilles c = c1; 1683219623Sjilles goto varname; 1684219623Sjilles } 1685219623Sjilles pungetc(); 1686219623Sjilles c = c1; 1687219623Sjilles c1 = '#'; 1688219623Sjilles subtype = 0; 1689219623Sjilles } 1690219623Sjilles USTPUTC(c1, out); 16911556Srgrimes } else { 1692219623Sjilles subtype = VSERROR; 1693219623Sjilles if (c == '}') 1694219623Sjilles pungetc(); 1695219623Sjilles else if (c == '\n' || c == PEOF) 1696219623Sjilles synerror("Unexpected end of line in substitution"); 1697219623Sjilles else 1698164003Sstefanf USTPUTC(c, out); 16991556Srgrimes } 17001556Srgrimes if (subtype == 0) { 170117987Speter switch (c) { 170217987Speter case ':': 1703179022Sstefanf flags |= VSNUL; 17041556Srgrimes c = pgetc(); 170517987Speter /*FALLTHROUGH*/ 170617987Speter default: 170717987Speter p = strchr(types, c); 1708164003Sstefanf if (p == NULL) { 1709206144Sjilles if (c == '\n' || c == PEOF) 1710206144Sjilles synerror("Unexpected end of line in substitution"); 1711164003Sstefanf if (flags == VSNUL) 1712164003Sstefanf STPUTC(':', out); 1713164003Sstefanf STPUTC(c, out); 1714164003Sstefanf subtype = VSERROR; 1715164003Sstefanf } else 1716164003Sstefanf subtype = p - types + VSNORMAL; 171717987Speter break; 171817987Speter case '%': 171920425Ssteve case '#': 172017987Speter { 172117987Speter int cc = c; 172217987Speter subtype = c == '#' ? VSTRIMLEFT : 172317987Speter VSTRIMRIGHT; 172417987Speter c = pgetc(); 172517987Speter if (c == cc) 172617987Speter subtype++; 172717987Speter else 172817987Speter pungetc(); 172917987Speter break; 173017987Speter } 17311556Srgrimes } 1732164003Sstefanf } else if (subtype != VSERROR) { 1733221461Sjilles if (subtype == VSLENGTH && c != '}') 1734221461Sjilles subtype = VSERROR; 17351556Srgrimes pungetc(); 17361556Srgrimes } 1737164003Sstefanf STPUTC('=', out); 1738220903Sjilles if (state[level].syntax == DQSYNTAX || 1739220903Sjilles state[level].syntax == ARISYNTAX) 17401556Srgrimes flags |= VSQUOTE; 17411556Srgrimes *(stackblock() + typeloc) = subtype | flags; 1742206145Sjilles if (subtype != VSNORMAL) { 1743206145Sjilles if (level + 1 >= maxnest) { 1744206145Sjilles maxnest *= 2; 1745206145Sjilles if (state == state_static) { 1746206145Sjilles state = parser_temp_alloc( 1747206145Sjilles maxnest * sizeof(*state)); 1748206145Sjilles memcpy(state, state_static, 1749213811Sobrien MAXNEST_static * sizeof(*state)); 1750206145Sjilles } else 1751206145Sjilles state = parser_temp_realloc(state, 1752206145Sjilles maxnest * sizeof(*state)); 1753206145Sjilles } 1754206145Sjilles level++; 1755206145Sjilles state[level].parenlevel = 0; 1756206145Sjilles if (subtype == VSMINUS || subtype == VSPLUS || 1757206145Sjilles subtype == VSQUESTION || subtype == VSASSIGN) { 1758206145Sjilles /* 1759206145Sjilles * For operators that were in the Bourne shell, 1760206145Sjilles * inherit the double-quote state. 1761206145Sjilles */ 1762206145Sjilles state[level].syntax = state[level - 1].syntax; 1763206145Sjilles state[level].category = TSTATE_VAR_OLD; 1764206145Sjilles } else { 1765206145Sjilles /* 1766206145Sjilles * The other operators take a pattern, 1767206145Sjilles * so go to BASESYNTAX. 1768206145Sjilles * Also, ' and " are now special, even 1769206145Sjilles * in here documents. 1770206145Sjilles */ 1771206145Sjilles state[level].syntax = BASESYNTAX; 1772206145Sjilles state[level].category = TSTATE_VAR_NEW; 1773206145Sjilles newvarnest++; 1774206145Sjilles } 1775206145Sjilles } 1776221513Sjilles } else if (c == '\'' && state[level].syntax == BASESYNTAX) { 1777221513Sjilles /* $'cstylequotes' */ 1778221513Sjilles USTPUTC(CTLQUOTEMARK, out); 1779221513Sjilles state[level].syntax = SQSYNTAX; 1780221513Sjilles sqiscstyle = 1; 1781221513Sjilles } else { 1782221513Sjilles USTPUTC('$', out); 1783221513Sjilles pungetc(); 17841556Srgrimes } 17851556Srgrimes goto parsesub_return; 17861556Srgrimes} 17871556Srgrimes 17881556Srgrimes 17891556Srgrimes/* 17901556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 17911556Srgrimes */ 17921556Srgrimesparsearith: { 17931556Srgrimes 1794206145Sjilles if (level + 1 >= maxnest) { 1795206145Sjilles maxnest *= 2; 1796206145Sjilles if (state == state_static) { 1797206145Sjilles state = parser_temp_alloc( 1798206145Sjilles maxnest * sizeof(*state)); 1799206145Sjilles memcpy(state, state_static, 1800213811Sobrien MAXNEST_static * sizeof(*state)); 1801206145Sjilles } else 1802206145Sjilles state = parser_temp_realloc(state, 1803206145Sjilles maxnest * sizeof(*state)); 18041556Srgrimes } 1805206145Sjilles level++; 1806206145Sjilles state[level].syntax = ARISYNTAX; 1807206145Sjilles state[level].parenlevel = 0; 1808206145Sjilles state[level].category = TSTATE_ARITH; 1809206145Sjilles USTPUTC(CTLARI, out); 1810206145Sjilles if (state[level - 1].syntax == DQSYNTAX) 1811206145Sjilles USTPUTC('"',out); 1812206145Sjilles else 1813206145Sjilles USTPUTC(' ',out); 18141556Srgrimes goto parsearith_return; 18151556Srgrimes} 18161556Srgrimes 18171556Srgrimes} /* end of readtoken */ 18181556Srgrimes 18191556Srgrimes 18201556Srgrimes 18211556Srgrimes#ifdef mkinit 18221556SrgrimesRESET { 18231556Srgrimes tokpushback = 0; 18241556Srgrimes checkkwd = 0; 18251556Srgrimes} 18261556Srgrimes#endif 18271556Srgrimes 18281556Srgrimes/* 18291556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 18301556Srgrimes * or backquotes). 18311556Srgrimes */ 18321556Srgrimes 1833213811Sobrienstatic int 183490111Simpnoexpand(char *text) 183590111Simp{ 183625230Ssteve char *p; 183725230Ssteve char c; 18381556Srgrimes 18391556Srgrimes p = text; 18401556Srgrimes while ((c = *p++) != '\0') { 184139137Stegge if ( c == CTLQUOTEMARK) 184239137Stegge continue; 18431556Srgrimes if (c == CTLESC) 18441556Srgrimes p++; 184583675Stegge else if (BASESYNTAX[(int)c] == CCTL) 18461556Srgrimes return 0; 18471556Srgrimes } 18481556Srgrimes return 1; 18491556Srgrimes} 18501556Srgrimes 18511556Srgrimes 18521556Srgrimes/* 18531556Srgrimes * Return true if the argument is a legal variable name (a letter or 18541556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 18551556Srgrimes */ 18561556Srgrimes 18571556Srgrimesint 1858200956Sjillesgoodname(const char *name) 185990111Simp{ 1860200956Sjilles const char *p; 18611556Srgrimes 18621556Srgrimes p = name; 18631556Srgrimes if (! is_name(*p)) 18641556Srgrimes return 0; 18651556Srgrimes while (*++p) { 18661556Srgrimes if (! is_in_name(*p)) 18671556Srgrimes return 0; 18681556Srgrimes } 18691556Srgrimes return 1; 18701556Srgrimes} 18711556Srgrimes 18721556Srgrimes 1873222165Sjillesint 1874222165Sjillesisassignment(const char *p) 1875222165Sjilles{ 1876222165Sjilles if (!is_name(*p)) 1877222165Sjilles return 0; 1878222165Sjilles p++; 1879222165Sjilles for (;;) { 1880222165Sjilles if (*p == '=') 1881222165Sjilles return 1; 1882222165Sjilles else if (!is_in_name(*p)) 1883222165Sjilles return 0; 1884222165Sjilles p++; 1885222165Sjilles } 1886222165Sjilles} 1887222165Sjilles 1888222165Sjilles 18891556Srgrimes/* 18901556Srgrimes * Called when an unexpected token is read during the parse. The argument 18911556Srgrimes * is the token that is expected, or -1 if more than one type of token can 18921556Srgrimes * occur at this point. 18931556Srgrimes */ 18941556Srgrimes 1895213811Sobrienstatic void 189690111Simpsynexpect(int token) 189717987Speter{ 18981556Srgrimes char msg[64]; 18991556Srgrimes 19001556Srgrimes if (token >= 0) { 19011556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 19021556Srgrimes tokname[lasttoken], tokname[token]); 19031556Srgrimes } else { 19041556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 19051556Srgrimes } 19061556Srgrimes synerror(msg); 19071556Srgrimes} 19081556Srgrimes 19091556Srgrimes 1910213811Sobrienstatic void 1911201053Sjillessynerror(const char *msg) 191290111Simp{ 19131556Srgrimes if (commandname) 1914201366Sjilles outfmt(out2, "%s: %d: ", commandname, startlinno); 1915201366Sjilles outfmt(out2, "Syntax error: %s\n", msg); 19161556Srgrimes error((char *)NULL); 19171556Srgrimes} 19181556Srgrimes 1919213811Sobrienstatic void 192090111Simpsetprompt(int which) 192190111Simp{ 19221556Srgrimes whichprompt = which; 19231556Srgrimes 192417987Speter#ifndef NO_HISTORY 19251556Srgrimes if (!el) 192617987Speter#endif 1927199629Sjilles { 19281556Srgrimes out2str(getprompt(NULL)); 1929199629Sjilles flushout(out2); 1930199629Sjilles } 19311556Srgrimes} 19321556Srgrimes 19331556Srgrimes/* 19341556Srgrimes * called by editline -- any expansions to the prompt 19351556Srgrimes * should be added here. 19361556Srgrimes */ 19371556Srgrimeschar * 193890111Simpgetprompt(void *unused __unused) 193925905Ssteve{ 1940142845Sobrien static char ps[PROMPTLEN]; 1941142845Sobrien char *fmt; 1942209653Sjilles const char *pwd; 1943209653Sjilles int i, trim; 1944214538Sjilles static char internal_error[] = "??"; 1945142845Sobrien 1946142845Sobrien /* 1947142845Sobrien * Select prompt format. 1948142845Sobrien */ 19491556Srgrimes switch (whichprompt) { 19501556Srgrimes case 0: 1951201053Sjilles fmt = nullstr; 1952142845Sobrien break; 19531556Srgrimes case 1: 1954142845Sobrien fmt = ps1val(); 1955142845Sobrien break; 19561556Srgrimes case 2: 1957142845Sobrien fmt = ps2val(); 1958142845Sobrien break; 19591556Srgrimes default: 1960201053Sjilles return internal_error; 19611556Srgrimes } 1962142845Sobrien 1963142845Sobrien /* 1964142845Sobrien * Format prompt string. 1965142845Sobrien */ 1966142845Sobrien for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1967142845Sobrien if (*fmt == '\\') 1968142845Sobrien switch (*++fmt) { 1969142845Sobrien 1970142845Sobrien /* 1971142845Sobrien * Hostname. 1972142845Sobrien * 1973142845Sobrien * \h specifies just the local hostname, 1974142845Sobrien * \H specifies fully-qualified hostname. 1975142845Sobrien */ 1976142845Sobrien case 'h': 1977142845Sobrien case 'H': 1978149024Sstefanf ps[i] = '\0'; 1979142845Sobrien gethostname(&ps[i], PROMPTLEN - i); 1980142845Sobrien /* Skip to end of hostname. */ 1981142845Sobrien trim = (*fmt == 'h') ? '.' : '\0'; 1982142845Sobrien while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1983142845Sobrien i++; 1984142845Sobrien break; 1985142845Sobrien 1986142845Sobrien /* 1987142845Sobrien * Working directory. 1988142845Sobrien * 1989142845Sobrien * \W specifies just the final component, 1990142845Sobrien * \w specifies the entire path. 1991142845Sobrien */ 1992142845Sobrien case 'W': 1993142845Sobrien case 'w': 1994209653Sjilles pwd = lookupvar("PWD"); 1995209653Sjilles if (pwd == NULL) 1996209653Sjilles pwd = "?"; 1997209653Sjilles if (*fmt == 'W' && 1998209653Sjilles *pwd == '/' && pwd[1] != '\0') 1999209653Sjilles strlcpy(&ps[i], strrchr(pwd, '/') + 1, 2000209653Sjilles PROMPTLEN - i); 2001209653Sjilles else 2002209653Sjilles strlcpy(&ps[i], pwd, PROMPTLEN - i); 2003142845Sobrien /* Skip to end of path. */ 2004142845Sobrien while (ps[i + 1] != '\0') 2005142845Sobrien i++; 2006142845Sobrien break; 2007142845Sobrien 2008142845Sobrien /* 2009142845Sobrien * Superuser status. 2010142845Sobrien * 2011142845Sobrien * '$' for normal users, '#' for root. 2012142845Sobrien */ 2013142845Sobrien case '$': 2014142845Sobrien ps[i] = (geteuid() != 0) ? '$' : '#'; 2015142845Sobrien break; 2016142845Sobrien 2017142845Sobrien /* 2018142845Sobrien * A literal \. 2019142845Sobrien */ 2020142845Sobrien case '\\': 2021142845Sobrien ps[i] = '\\'; 2022142845Sobrien break; 2023142845Sobrien 2024142845Sobrien /* 2025142845Sobrien * Emit unrecognized formats verbatim. 2026142845Sobrien */ 2027142845Sobrien default: 2028142845Sobrien ps[i++] = '\\'; 2029142845Sobrien ps[i] = *fmt; 2030142845Sobrien break; 2031142845Sobrien } 2032142845Sobrien else 2033142845Sobrien ps[i] = *fmt; 2034142845Sobrien ps[i] = '\0'; 2035142845Sobrien return (ps); 20361556Srgrimes} 2037222907Sjilles 2038222907Sjilles 2039222907Sjillesconst char * 2040222907Sjillesexpandstr(char *ps) 2041222907Sjilles{ 2042222907Sjilles union node n; 2043222907Sjilles struct jmploc jmploc; 2044222907Sjilles struct jmploc *const savehandler = handler; 2045222907Sjilles const int saveprompt = doprompt; 2046222907Sjilles struct parsefile *const savetopfile = getcurrentfile(); 2047222907Sjilles struct parser_temp *const saveparser_temp = parser_temp; 2048222907Sjilles const char *result = NULL; 2049222907Sjilles 2050222907Sjilles if (!setjmp(jmploc.loc)) { 2051222907Sjilles handler = &jmploc; 2052222907Sjilles parser_temp = NULL; 2053222907Sjilles setinputstring(ps, 1); 2054222907Sjilles doprompt = 0; 2055222907Sjilles readtoken1(pgetc(), DQSYNTAX, "\n\n", 0); 2056222907Sjilles if (backquotelist != NULL) 2057222907Sjilles error("Command substitution not allowed here"); 2058222907Sjilles 2059222907Sjilles n.narg.type = NARG; 2060222907Sjilles n.narg.next = NULL; 2061222907Sjilles n.narg.text = wordtext; 2062222907Sjilles n.narg.backquote = backquotelist; 2063222907Sjilles 2064222907Sjilles expandarg(&n, NULL, 0); 2065222907Sjilles result = stackblock(); 2066222907Sjilles INTOFF; 2067222907Sjilles } 2068222907Sjilles handler = savehandler; 2069222907Sjilles doprompt = saveprompt; 2070222907Sjilles popfilesupto(savetopfile); 2071222907Sjilles if (parser_temp != saveparser_temp) { 2072222907Sjilles parser_temp_free_all(); 2073222907Sjilles parser_temp = saveparser_temp; 2074222907Sjilles } 2075222907Sjilles if (result != NULL) { 2076222907Sjilles INTON; 2077222907Sjilles } else if (exception == EXINT) 2078222907Sjilles raise(SIGINT); 2079222907Sjilles return result; 2080222907Sjilles} 2081