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 */ 99255068Sjillesstatic int tokpushback; /* last token pushed back */ 100213760Sobrienstatic char *wordtext; /* text of last word returned by readtoken */ 101253659Sjillesstatic int checkkwd; 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 111255087Sjillesstatic union node *list(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); 117255085Sjillesstatic union node *makebinary(int type, union node *n1, union node *n2); 118213811Sobrienstatic void parsefname(void); 119213811Sobrienstatic void parseheredoc(void); 120213811Sobrienstatic int peektoken(void); 121213811Sobrienstatic int readtoken(void); 122213811Sobrienstatic int xxreadtoken(void); 123248980Sjillesstatic int readtoken1(int, const char *, const char *, int); 124213811Sobrienstatic int noexpand(char *); 125255073Sjillesstatic void consumetoken(int); 126213811Sobrienstatic void synexpect(int) __dead2; 127213811Sobrienstatic void synerror(const char *) __dead2; 128213811Sobrienstatic void setprompt(int); 1291556Srgrimes 13017987Speter 131213811Sobrienstatic void * 132206145Sjillesparser_temp_alloc(size_t len) 133206145Sjilles{ 134206145Sjilles struct parser_temp *t; 135206145Sjilles 136206145Sjilles INTOFF; 137206145Sjilles t = ckmalloc(sizeof(*t)); 138206145Sjilles t->data = NULL; 139206145Sjilles t->next = parser_temp; 140206145Sjilles parser_temp = t; 141206145Sjilles t->data = ckmalloc(len); 142206145Sjilles INTON; 143206145Sjilles return t->data; 144206145Sjilles} 145206145Sjilles 146206145Sjilles 147213811Sobrienstatic void * 148206145Sjillesparser_temp_realloc(void *ptr, size_t len) 149206145Sjilles{ 150206145Sjilles struct parser_temp *t; 151206145Sjilles 152206145Sjilles INTOFF; 153206145Sjilles t = parser_temp; 154206145Sjilles if (ptr != t->data) 155206145Sjilles error("bug: parser_temp_realloc misused"); 156206145Sjilles t->data = ckrealloc(t->data, len); 157206145Sjilles INTON; 158206145Sjilles return t->data; 159206145Sjilles} 160206145Sjilles 161206145Sjilles 162213811Sobrienstatic void 163206145Sjillesparser_temp_free_upto(void *ptr) 164206145Sjilles{ 165206145Sjilles struct parser_temp *t; 166206145Sjilles int done = 0; 167206145Sjilles 168206145Sjilles INTOFF; 169206145Sjilles while (parser_temp != NULL && !done) { 170206145Sjilles t = parser_temp; 171206145Sjilles parser_temp = t->next; 172206145Sjilles done = t->data == ptr; 173206145Sjilles ckfree(t->data); 174206145Sjilles ckfree(t); 175206145Sjilles } 176206145Sjilles INTON; 177206145Sjilles if (!done) 178206145Sjilles error("bug: parser_temp_free_upto misused"); 179206145Sjilles} 180206145Sjilles 181206145Sjilles 182213811Sobrienstatic void 183206145Sjillesparser_temp_free_all(void) 184206145Sjilles{ 185206145Sjilles struct parser_temp *t; 186206145Sjilles 187206145Sjilles INTOFF; 188206145Sjilles while (parser_temp != NULL) { 189206145Sjilles t = parser_temp; 190206145Sjilles parser_temp = t->next; 191206145Sjilles ckfree(t->data); 192206145Sjilles ckfree(t); 193206145Sjilles } 194206145Sjilles INTON; 195206145Sjilles} 196206145Sjilles 197206145Sjilles 1981556Srgrimes/* 1991556Srgrimes * Read and parse a command. Returns NEOF on end of file. (NULL is a 2001556Srgrimes * valid parse tree indicating a blank line.) 2011556Srgrimes */ 2021556Srgrimes 2031556Srgrimesunion node * 20490111Simpparsecmd(int interact) 20517987Speter{ 2061556Srgrimes int t; 2071556Srgrimes 208206145Sjilles /* This assumes the parser is not re-entered, 209206145Sjilles * which could happen if we add command substitution on PS1/PS2. 210206145Sjilles */ 211206145Sjilles parser_temp_free_all(); 212208656Sjilles heredoclist = NULL; 213206145Sjilles 21460593Scracauer tokpushback = 0; 215254426Sjilles checkkwd = 0; 2161556Srgrimes doprompt = interact; 2171556Srgrimes if (doprompt) 2181556Srgrimes setprompt(1); 2191556Srgrimes else 2201556Srgrimes setprompt(0); 2211556Srgrimes needprompt = 0; 2221556Srgrimes t = readtoken(); 2231556Srgrimes if (t == TEOF) 2241556Srgrimes return NEOF; 2251556Srgrimes if (t == TNL) 2261556Srgrimes return NULL; 2271556Srgrimes tokpushback++; 228255087Sjilles return list(1); 2291556Srgrimes} 2301556Srgrimes 2311556Srgrimes 232213811Sobrienstatic union node * 233255087Sjilleslist(int nlflag) 23417987Speter{ 235214599Sjilles union node *ntop, *n1, *n2, *n3; 23617987Speter int tok; 2371556Srgrimes 238214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 239255087Sjilles if (!nlflag && tokendlist[peektoken()]) 2401556Srgrimes return NULL; 241214599Sjilles ntop = n1 = NULL; 2421556Srgrimes for (;;) { 24317987Speter n2 = andor(); 24417987Speter tok = readtoken(); 24517987Speter if (tok == TBACKGND) { 246245382Sjilles if (n2 != NULL && n2->type == NPIPE) { 247223282Sjilles n2->npipe.backgnd = 1; 248245382Sjilles } else if (n2 != NULL && n2->type == NREDIR) { 24917987Speter n2->type = NBACKGND; 25017987Speter } else { 25117987Speter n3 = (union node *)stalloc(sizeof (struct nredir)); 25217987Speter n3->type = NBACKGND; 25317987Speter n3->nredir.n = n2; 25417987Speter n3->nredir.redirect = NULL; 25517987Speter n2 = n3; 25617987Speter } 25717987Speter } 258214599Sjilles if (ntop == NULL) 259214599Sjilles ntop = n2; 260214599Sjilles else if (n1 == NULL) { 261255085Sjilles n1 = makebinary(NSEMI, ntop, n2); 262214599Sjilles ntop = n1; 26317987Speter } 26417987Speter else { 265255085Sjilles n3 = makebinary(NSEMI, n1->nbinary.ch2, n2); 266214599Sjilles n1->nbinary.ch2 = n3; 26717987Speter n1 = n3; 26817987Speter } 26917987Speter switch (tok) { 27013882Sjoerg case TBACKGND: 27117987Speter case TSEMI: 27217987Speter tok = readtoken(); 273102410Scharnier /* FALLTHROUGH */ 2741556Srgrimes case TNL: 27517987Speter if (tok == TNL) { 27617987Speter parseheredoc(); 27717987Speter if (nlflag) 278214599Sjilles return ntop; 279210488Sjilles } else if (tok == TEOF && nlflag) { 280210488Sjilles parseheredoc(); 281214599Sjilles return ntop; 28217987Speter } else { 28317987Speter tokpushback++; 28417987Speter } 285214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 286255087Sjilles if (!nlflag && tokendlist[peektoken()]) 287214599Sjilles return ntop; 2881556Srgrimes break; 2891556Srgrimes case TEOF: 2901556Srgrimes if (heredoclist) 2911556Srgrimes parseheredoc(); 2921556Srgrimes else 2931556Srgrimes pungetc(); /* push back EOF on input */ 294214599Sjilles return ntop; 2951556Srgrimes default: 296255087Sjilles if (nlflag) 2971556Srgrimes synexpect(-1); 2981556Srgrimes tokpushback++; 299214599Sjilles return ntop; 3001556Srgrimes } 3011556Srgrimes } 3021556Srgrimes} 3031556Srgrimes 3041556Srgrimes 3051556Srgrimes 306213811Sobrienstatic union node * 30790111Simpandor(void) 30890111Simp{ 309255085Sjilles union node *n; 3101556Srgrimes int t; 3111556Srgrimes 312255085Sjilles n = pipeline(); 3131556Srgrimes for (;;) { 3141556Srgrimes if ((t = readtoken()) == TAND) { 3151556Srgrimes t = NAND; 3161556Srgrimes } else if (t == TOR) { 3171556Srgrimes t = NOR; 3181556Srgrimes } else { 3191556Srgrimes tokpushback++; 320255085Sjilles return n; 3211556Srgrimes } 322255085Sjilles n = makebinary(t, n, pipeline()); 3231556Srgrimes } 3241556Srgrimes} 3251556Srgrimes 3261556Srgrimes 3271556Srgrimes 328213811Sobrienstatic union node * 32990111Simppipeline(void) 33090111Simp{ 33175336Sbrian union node *n1, *n2, *pipenode; 3321556Srgrimes struct nodelist *lp, *prev; 333214281Sjilles int negate, t; 3341556Srgrimes 33575336Sbrian negate = 0; 336214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 3371556Srgrimes TRACE(("pipeline: entered\n")); 33875336Sbrian while (readtoken() == TNOT) 33975336Sbrian negate = !negate; 34075336Sbrian tokpushback++; 3411556Srgrimes n1 = command(); 3421556Srgrimes if (readtoken() == TPIPE) { 3431556Srgrimes pipenode = (union node *)stalloc(sizeof (struct npipe)); 3441556Srgrimes pipenode->type = NPIPE; 3451556Srgrimes pipenode->npipe.backgnd = 0; 3461556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 3471556Srgrimes pipenode->npipe.cmdlist = lp; 3481556Srgrimes lp->n = n1; 3491556Srgrimes do { 3501556Srgrimes prev = lp; 3511556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 352214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 353214281Sjilles t = readtoken(); 354214281Sjilles tokpushback++; 355214281Sjilles if (t == TNOT) 356214281Sjilles lp->n = pipeline(); 357214281Sjilles else 358214281Sjilles lp->n = command(); 3591556Srgrimes prev->next = lp; 3601556Srgrimes } while (readtoken() == TPIPE); 3611556Srgrimes lp->next = NULL; 3621556Srgrimes n1 = pipenode; 3631556Srgrimes } 3641556Srgrimes tokpushback++; 36575336Sbrian if (negate) { 36675336Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 36775336Sbrian n2->type = NNOT; 36875336Sbrian n2->nnot.com = n1; 36975336Sbrian return n2; 37075336Sbrian } else 37175336Sbrian return n1; 3721556Srgrimes} 3731556Srgrimes 3741556Srgrimes 3751556Srgrimes 376213811Sobrienstatic union node * 37790111Simpcommand(void) 37890111Simp{ 3791556Srgrimes union node *n1, *n2; 3801556Srgrimes union node *ap, **app; 3811556Srgrimes union node *cp, **cpp; 3821556Srgrimes union node *redir, **rpp; 383214281Sjilles int t; 384218325Sjilles int is_subshell; 3851556Srgrimes 386214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 387218325Sjilles is_subshell = 0; 38817987Speter redir = NULL; 38917987Speter n1 = NULL; 3901556Srgrimes rpp = &redir; 39120425Ssteve 3921556Srgrimes /* Check for redirection which may precede command */ 3931556Srgrimes while (readtoken() == TREDIR) { 3941556Srgrimes *rpp = n2 = redirnode; 3951556Srgrimes rpp = &n2->nfile.next; 3961556Srgrimes parsefname(); 3971556Srgrimes } 3981556Srgrimes tokpushback++; 3991556Srgrimes 4001556Srgrimes switch (readtoken()) { 4011556Srgrimes case TIF: 4021556Srgrimes n1 = (union node *)stalloc(sizeof (struct nif)); 4031556Srgrimes n1->type = NIF; 404255087Sjilles if ((n1->nif.test = list(0)) == NULL) 405104554Stjr synexpect(-1); 406255073Sjilles consumetoken(TTHEN); 407255087Sjilles n1->nif.ifpart = list(0); 4081556Srgrimes n2 = n1; 4091556Srgrimes while (readtoken() == TELIF) { 4101556Srgrimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 4111556Srgrimes n2 = n2->nif.elsepart; 4121556Srgrimes n2->type = NIF; 413255087Sjilles if ((n2->nif.test = list(0)) == NULL) 414104554Stjr synexpect(-1); 415255073Sjilles consumetoken(TTHEN); 416255087Sjilles n2->nif.ifpart = list(0); 4171556Srgrimes } 4181556Srgrimes if (lasttoken == TELSE) 419255087Sjilles n2->nif.elsepart = list(0); 4201556Srgrimes else { 4211556Srgrimes n2->nif.elsepart = NULL; 4221556Srgrimes tokpushback++; 4231556Srgrimes } 424255073Sjilles consumetoken(TFI); 425214709Sjilles checkkwd = CHKKWD | CHKALIAS; 4261556Srgrimes break; 4271556Srgrimes case TWHILE: 428255073Sjilles case TUNTIL: 429255085Sjilles t = lasttoken; 430255087Sjilles if ((n1 = list(0)) == NULL) 431104554Stjr synexpect(-1); 432255073Sjilles consumetoken(TDO); 433255087Sjilles n1 = makebinary((t == TWHILE)? NWHILE : NUNTIL, n1, list(0)); 434255073Sjilles consumetoken(TDONE); 435214709Sjilles checkkwd = CHKKWD | CHKALIAS; 4361556Srgrimes break; 4371556Srgrimes case TFOR: 4381556Srgrimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 4391556Srgrimes synerror("Bad for loop variable"); 4401556Srgrimes n1 = (union node *)stalloc(sizeof (struct nfor)); 4411556Srgrimes n1->type = NFOR; 4421556Srgrimes n1->nfor.var = wordtext; 443199282Sjilles while (readtoken() == TNL) 444199282Sjilles ; 445199282Sjilles if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) { 4461556Srgrimes app = ≈ 4471556Srgrimes while (readtoken() == TWORD) { 448255081Sjilles n2 = makename(); 4491556Srgrimes *app = n2; 4501556Srgrimes app = &n2->narg.next; 4511556Srgrimes } 4521556Srgrimes *app = NULL; 4531556Srgrimes n1->nfor.args = ap; 4541556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 4551556Srgrimes synexpect(-1); 4561556Srgrimes } else { 457149096Sstefanf static char argvars[5] = { 458149096Sstefanf CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' 459149096Sstefanf }; 4601556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 4611556Srgrimes n2->type = NARG; 462149096Sstefanf n2->narg.text = argvars; 4631556Srgrimes n2->narg.backquote = NULL; 4641556Srgrimes n2->narg.next = NULL; 4651556Srgrimes n1->nfor.args = n2; 4661556Srgrimes /* 4671556Srgrimes * Newline or semicolon here is optional (but note 4681556Srgrimes * that the original Bourne shell only allowed NL). 4691556Srgrimes */ 4701556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 4711556Srgrimes tokpushback++; 4721556Srgrimes } 473214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 4741556Srgrimes if ((t = readtoken()) == TDO) 4751556Srgrimes t = TDONE; 4761556Srgrimes else if (t == TBEGIN) 4771556Srgrimes t = TEND; 4781556Srgrimes else 4791556Srgrimes synexpect(-1); 480255087Sjilles n1->nfor.body = list(0); 481255073Sjilles consumetoken(t); 482214709Sjilles checkkwd = CHKKWD | CHKALIAS; 4831556Srgrimes break; 4841556Srgrimes case TCASE: 4851556Srgrimes n1 = (union node *)stalloc(sizeof (struct ncase)); 4861556Srgrimes n1->type = NCASE; 487255073Sjilles consumetoken(TWORD); 488255081Sjilles n1->ncase.expr = makename(); 4891556Srgrimes while (readtoken() == TNL); 4901556Srgrimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 4911556Srgrimes synerror("expecting \"in\""); 4921556Srgrimes cpp = &n1->ncase.cases; 493214709Sjilles checkkwd = CHKNL | CHKKWD, readtoken(); 494104202Stjr while (lasttoken != TESAC) { 4951556Srgrimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 4961556Srgrimes cp->type = NCLIST; 4971556Srgrimes app = &cp->nclist.pattern; 498104207Stjr if (lasttoken == TLP) 499104207Stjr readtoken(); 5001556Srgrimes for (;;) { 501255081Sjilles *app = ap = makename(); 502214709Sjilles checkkwd = CHKNL | CHKKWD; 503214709Sjilles if (readtoken() != TPIPE) 5041556Srgrimes break; 5051556Srgrimes app = &ap->narg.next; 5062760Ssef readtoken(); 5071556Srgrimes } 5081556Srgrimes ap->narg.next = NULL; 5091556Srgrimes if (lasttoken != TRP) 510214709Sjilles synexpect(TRP); 511255087Sjilles cp->nclist.body = list(0); 5122760Ssef 513214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 5142760Ssef if ((t = readtoken()) != TESAC) { 515223186Sjilles if (t == TENDCASE) 516223186Sjilles ; 517223186Sjilles else if (t == TFALLTHRU) 518223186Sjilles cp->type = NCLISTFALLTHRU; 519223186Sjilles else 520214709Sjilles synexpect(TENDCASE); 521223186Sjilles checkkwd = CHKNL | CHKKWD, readtoken(); 5222760Ssef } 5231556Srgrimes cpp = &cp->nclist.next; 524104202Stjr } 5251556Srgrimes *cpp = NULL; 526214709Sjilles checkkwd = CHKKWD | CHKALIAS; 5271556Srgrimes break; 5281556Srgrimes case TLP: 5291556Srgrimes n1 = (union node *)stalloc(sizeof (struct nredir)); 5301556Srgrimes n1->type = NSUBSHELL; 531255087Sjilles n1->nredir.n = list(0); 5321556Srgrimes n1->nredir.redirect = NULL; 533255073Sjilles consumetoken(TRP); 534214709Sjilles checkkwd = CHKKWD | CHKALIAS; 535218325Sjilles is_subshell = 1; 5361556Srgrimes break; 5371556Srgrimes case TBEGIN: 538255087Sjilles n1 = list(0); 539255073Sjilles consumetoken(TEND); 540214709Sjilles checkkwd = CHKKWD | CHKALIAS; 5411556Srgrimes break; 542254843Sjilles /* A simple command must have at least one redirection or word. */ 543210221Sjilles case TBACKGND: 54417987Speter case TSEMI: 545101662Stjr case TAND: 546101662Stjr case TOR: 547254335Sjilles case TPIPE: 548254335Sjilles case TENDCASE: 549254335Sjilles case TFALLTHRU: 550254843Sjilles case TEOF: 551254843Sjilles case TNL: 552254843Sjilles case TRP: 55317987Speter if (!redir) 55417987Speter synexpect(-1); 5551556Srgrimes case TWORD: 5561556Srgrimes tokpushback++; 55775160Sbrian n1 = simplecmd(rpp, redir); 558214281Sjilles return n1; 5591556Srgrimes default: 5601556Srgrimes synexpect(-1); 5611556Srgrimes } 5621556Srgrimes 5631556Srgrimes /* Now check for redirection which may follow command */ 5641556Srgrimes while (readtoken() == TREDIR) { 5651556Srgrimes *rpp = n2 = redirnode; 5661556Srgrimes rpp = &n2->nfile.next; 5671556Srgrimes parsefname(); 5681556Srgrimes } 5691556Srgrimes tokpushback++; 5701556Srgrimes *rpp = NULL; 5711556Srgrimes if (redir) { 572218325Sjilles if (!is_subshell) { 5731556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 5741556Srgrimes n2->type = NREDIR; 5751556Srgrimes n2->nredir.n = n1; 5761556Srgrimes n1 = n2; 5771556Srgrimes } 5781556Srgrimes n1->nredir.redirect = redir; 5791556Srgrimes } 58075160Sbrian 581214281Sjilles return n1; 5821556Srgrimes} 5831556Srgrimes 5841556Srgrimes 585213811Sobrienstatic union node * 58690111Simpsimplecmd(union node **rpp, union node *redir) 58790111Simp{ 5881556Srgrimes union node *args, **app; 5891556Srgrimes union node **orig_rpp = rpp; 590210087Sjilles union node *n = NULL; 591214304Sjilles int special; 592222165Sjilles int savecheckkwd; 5931556Srgrimes 5941556Srgrimes /* If we don't have any redirections already, then we must reset */ 5951556Srgrimes /* rpp to be the address of the local redir variable. */ 5961556Srgrimes if (redir == 0) 5971556Srgrimes rpp = &redir; 5981556Srgrimes 5991556Srgrimes args = NULL; 6001556Srgrimes app = &args; 6018855Srgrimes /* 6021556Srgrimes * We save the incoming value, because we need this for shell 6031556Srgrimes * functions. There can not be a redirect or an argument between 6048855Srgrimes * the function name and the open parenthesis. 6051556Srgrimes */ 6061556Srgrimes orig_rpp = rpp; 6071556Srgrimes 608222165Sjilles savecheckkwd = CHKALIAS; 609222165Sjilles 6101556Srgrimes for (;;) { 611222165Sjilles checkkwd = savecheckkwd; 6121556Srgrimes if (readtoken() == TWORD) { 613255081Sjilles n = makename(); 6141556Srgrimes *app = n; 6151556Srgrimes app = &n->narg.next; 616222165Sjilles if (savecheckkwd != 0 && !isassignment(wordtext)) 617222165Sjilles savecheckkwd = 0; 6181556Srgrimes } else if (lasttoken == TREDIR) { 6191556Srgrimes *rpp = n = redirnode; 6201556Srgrimes rpp = &n->nfile.next; 6211556Srgrimes parsefname(); /* read name of redirection file */ 6221556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 6231556Srgrimes && rpp == orig_rpp) { 6241556Srgrimes /* We have a function */ 625255073Sjilles consumetoken(TRP); 626179022Sstefanf funclinno = plinno; 627214291Sjilles /* 628214291Sjilles * - Require plain text. 629214291Sjilles * - Functions with '/' cannot be called. 630214534Sjilles * - Reject name=(). 631214534Sjilles * - Reject ksh extended glob patterns. 632214291Sjilles */ 633214291Sjilles if (!noexpand(n->narg.text) || quoteflag || 634214534Sjilles strchr(n->narg.text, '/') || 635214534Sjilles strchr("!%*+-=?@}~", 636214534Sjilles n->narg.text[strlen(n->narg.text) - 1])) 6371556Srgrimes synerror("Bad function name"); 638214291Sjilles rmescapes(n->narg.text); 639214304Sjilles if (find_builtin(n->narg.text, &special) >= 0 && 640214304Sjilles special) 641214304Sjilles synerror("Cannot override a special builtin with a function"); 6421556Srgrimes n->type = NDEFUN; 6431556Srgrimes n->narg.next = command(); 644179022Sstefanf funclinno = 0; 645210087Sjilles return n; 6461556Srgrimes } else { 6471556Srgrimes tokpushback++; 6481556Srgrimes break; 6491556Srgrimes } 6501556Srgrimes } 6511556Srgrimes *app = NULL; 6521556Srgrimes *rpp = NULL; 6531556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 6541556Srgrimes n->type = NCMD; 6551556Srgrimes n->ncmd.args = args; 6561556Srgrimes n->ncmd.redirect = redir; 657210087Sjilles return n; 6581556Srgrimes} 6591556Srgrimes 660213811Sobrienstatic union node * 66190111Simpmakename(void) 66290111Simp{ 66317987Speter union node *n; 6641556Srgrimes 66517987Speter n = (union node *)stalloc(sizeof (struct narg)); 66617987Speter n->type = NARG; 66717987Speter n->narg.next = NULL; 66817987Speter n->narg.text = wordtext; 66917987Speter n->narg.backquote = backquotelist; 67017987Speter return n; 67117987Speter} 67217987Speter 673255085Sjillesstatic union node * 674255085Sjillesmakebinary(int type, union node *n1, union node *n2) 675255085Sjilles{ 676255085Sjilles union node *n; 677255085Sjilles 678255085Sjilles n = (union node *)stalloc(sizeof (struct nbinary)); 679255085Sjilles n->type = type; 680255085Sjilles n->nbinary.ch1 = n1; 681255085Sjilles n->nbinary.ch2 = n2; 682255085Sjilles return (n); 683255085Sjilles} 684255085Sjilles 685213760Sobrienvoid 686213760Sobrienfixredir(union node *n, const char *text, int err) 68790111Simp{ 68817987Speter TRACE(("Fix redir %s %d\n", text, err)); 68917987Speter if (!err) 69017987Speter n->ndup.vname = NULL; 69117987Speter 69217987Speter if (is_digit(text[0]) && text[1] == '\0') 69317987Speter n->ndup.dupfd = digit_val(text[0]); 69417987Speter else if (text[0] == '-' && text[1] == '\0') 69517987Speter n->ndup.dupfd = -1; 69617987Speter else { 69720425Ssteve 69817987Speter if (err) 69917987Speter synerror("Bad fd number"); 70017987Speter else 70117987Speter n->ndup.vname = makename(); 70217987Speter } 70317987Speter} 70417987Speter 70517987Speter 706213811Sobrienstatic void 70790111Simpparsefname(void) 70890111Simp{ 7091556Srgrimes union node *n = redirnode; 7101556Srgrimes 711255073Sjilles consumetoken(TWORD); 7121556Srgrimes if (n->type == NHERE) { 7131556Srgrimes struct heredoc *here = heredoc; 7141556Srgrimes struct heredoc *p; 7151556Srgrimes int i; 7161556Srgrimes 7171556Srgrimes if (quoteflag == 0) 7181556Srgrimes n->type = NXHERE; 7191556Srgrimes TRACE(("Here document %d\n", n->type)); 7201556Srgrimes if (here->striptabs) { 7211556Srgrimes while (*wordtext == '\t') 7221556Srgrimes wordtext++; 7231556Srgrimes } 7241556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 7251556Srgrimes synerror("Illegal eof marker for << redirection"); 7261556Srgrimes rmescapes(wordtext); 7271556Srgrimes here->eofmark = wordtext; 7281556Srgrimes here->next = NULL; 7291556Srgrimes if (heredoclist == NULL) 7301556Srgrimes heredoclist = here; 7311556Srgrimes else { 7321556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 7331556Srgrimes p->next = here; 7341556Srgrimes } 7351556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 73617987Speter fixredir(n, wordtext, 0); 7371556Srgrimes } else { 73817987Speter n->nfile.fname = makename(); 7391556Srgrimes } 7401556Srgrimes} 7411556Srgrimes 7421556Srgrimes 7431556Srgrimes/* 7441556Srgrimes * Input any here documents. 7451556Srgrimes */ 7461556Srgrimes 747213811Sobrienstatic void 74890111Simpparseheredoc(void) 74990111Simp{ 7501556Srgrimes struct heredoc *here; 7511556Srgrimes union node *n; 7521556Srgrimes 7531556Srgrimes while (heredoclist) { 7541556Srgrimes here = heredoclist; 7551556Srgrimes heredoclist = here->next; 7561556Srgrimes if (needprompt) { 7571556Srgrimes setprompt(2); 7581556Srgrimes needprompt = 0; 7591556Srgrimes } 7601556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 7611556Srgrimes here->eofmark, here->striptabs); 762255081Sjilles n = makename(); 7631556Srgrimes here->here->nhere.doc = n; 7641556Srgrimes } 7651556Srgrimes} 7661556Srgrimes 767213811Sobrienstatic int 76890111Simppeektoken(void) 76990111Simp{ 7701556Srgrimes int t; 7711556Srgrimes 7721556Srgrimes t = readtoken(); 7731556Srgrimes tokpushback++; 7741556Srgrimes return (t); 7751556Srgrimes} 7761556Srgrimes 777213811Sobrienstatic int 77890111Simpreadtoken(void) 77990111Simp{ 7801556Srgrimes int t; 7811556Srgrimes struct alias *ap; 7821556Srgrimes#ifdef DEBUG 7831556Srgrimes int alreadyseen = tokpushback; 7841556Srgrimes#endif 7858855Srgrimes 7861556Srgrimes top: 7871556Srgrimes t = xxreadtoken(); 7881556Srgrimes 789214709Sjilles /* 790214709Sjilles * eat newlines 791214709Sjilles */ 792214709Sjilles if (checkkwd & CHKNL) { 793214709Sjilles while (t == TNL) { 794214709Sjilles parseheredoc(); 795214709Sjilles t = xxreadtoken(); 796214709Sjilles } 797214709Sjilles } 7981556Srgrimes 799214709Sjilles /* 800214709Sjilles * check for keywords and aliases 801214709Sjilles */ 802214709Sjilles if (t == TWORD && !quoteflag) 803214709Sjilles { 804214709Sjilles const char * const *pp; 805214709Sjilles 806214709Sjilles if (checkkwd & CHKKWD) 80798463Sjmallett for (pp = parsekwd; *pp; pp++) { 80820425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 80917987Speter { 8101556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 8111556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 8121556Srgrimes goto out; 8131556Srgrimes } 8141556Srgrimes } 815214709Sjilles if (checkkwd & CHKALIAS && 816214709Sjilles (ap = lookupalias(wordtext, 1)) != NULL) { 817214709Sjilles pushstring(ap->val, strlen(ap->val), ap); 818214709Sjilles goto top; 8191556Srgrimes } 820214709Sjilles } 8211556Srgrimesout: 822214709Sjilles if (t != TNOT) 823214709Sjilles checkkwd = 0; 824214709Sjilles 8251556Srgrimes#ifdef DEBUG 8261556Srgrimes if (!alreadyseen) 8271556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8281556Srgrimes else 8291556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8301556Srgrimes#endif 8311556Srgrimes return (t); 8321556Srgrimes} 8331556Srgrimes 8341556Srgrimes 8351556Srgrimes/* 8361556Srgrimes * Read the next input token. 8371556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 8381556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 8391556Srgrimes * quoted. 8401556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 8411556Srgrimes * the redirection. 8421556Srgrimes * In all cases, the variable startlinno is set to the number of the line 8431556Srgrimes * on which the token starts. 8441556Srgrimes * 8451556Srgrimes * [Change comment: here documents and internal procedures] 8461556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 8471556Srgrimes * word parsing code into a separate routine. In this case, readtoken 8481556Srgrimes * doesn't need to have any internal procedures, but parseword does. 8491556Srgrimes * We could also make parseoperator in essence the main routine, and 8501556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 8511556Srgrimes */ 8521556Srgrimes 8531556Srgrimes#define RETURN(token) return lasttoken = token 8541556Srgrimes 855213811Sobrienstatic int 85690111Simpxxreadtoken(void) 85790111Simp{ 85825230Ssteve int c; 8591556Srgrimes 8601556Srgrimes if (tokpushback) { 8611556Srgrimes tokpushback = 0; 8621556Srgrimes return lasttoken; 8631556Srgrimes } 8641556Srgrimes if (needprompt) { 8651556Srgrimes setprompt(2); 8661556Srgrimes needprompt = 0; 8671556Srgrimes } 8681556Srgrimes startlinno = plinno; 8691556Srgrimes for (;;) { /* until token or start of word found */ 8701556Srgrimes c = pgetc_macro(); 8711556Srgrimes switch (c) { 8721556Srgrimes case ' ': case '\t': 8731556Srgrimes continue; 8741556Srgrimes case '#': 8751556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 8761556Srgrimes pungetc(); 8771556Srgrimes continue; 8781556Srgrimes case '\\': 8791556Srgrimes if (pgetc() == '\n') { 8801556Srgrimes startlinno = ++plinno; 8811556Srgrimes if (doprompt) 8821556Srgrimes setprompt(2); 8831556Srgrimes else 8841556Srgrimes setprompt(0); 8851556Srgrimes continue; 8861556Srgrimes } 8871556Srgrimes pungetc(); 8881556Srgrimes goto breakloop; 8891556Srgrimes case '\n': 8901556Srgrimes plinno++; 8911556Srgrimes needprompt = doprompt; 8921556Srgrimes RETURN(TNL); 8931556Srgrimes case PEOF: 8941556Srgrimes RETURN(TEOF); 8951556Srgrimes case '&': 8961556Srgrimes if (pgetc() == '&') 8971556Srgrimes RETURN(TAND); 8981556Srgrimes pungetc(); 8991556Srgrimes RETURN(TBACKGND); 9001556Srgrimes case '|': 9011556Srgrimes if (pgetc() == '|') 9021556Srgrimes RETURN(TOR); 9031556Srgrimes pungetc(); 9041556Srgrimes RETURN(TPIPE); 9051556Srgrimes case ';': 906223186Sjilles c = pgetc(); 907223186Sjilles if (c == ';') 9081556Srgrimes RETURN(TENDCASE); 909223186Sjilles else if (c == '&') 910223186Sjilles RETURN(TFALLTHRU); 9111556Srgrimes pungetc(); 9121556Srgrimes RETURN(TSEMI); 9131556Srgrimes case '(': 9141556Srgrimes RETURN(TLP); 9151556Srgrimes case ')': 9161556Srgrimes RETURN(TRP); 9171556Srgrimes default: 9181556Srgrimes goto breakloop; 9191556Srgrimes } 9201556Srgrimes } 9211556Srgrimesbreakloop: 9221556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 9231556Srgrimes#undef RETURN 9241556Srgrimes} 9251556Srgrimes 9261556Srgrimes 927213811Sobrien#define MAXNEST_static 8 928206145Sjillesstruct tokenstate 929206145Sjilles{ 930206145Sjilles const char *syntax; /* *SYNTAX */ 931206145Sjilles int parenlevel; /* levels of parentheses in arithmetic */ 932206145Sjilles enum tokenstate_category 933206145Sjilles { 934206145Sjilles TSTATE_TOP, 935206145Sjilles TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */ 936206145Sjilles TSTATE_VAR_NEW, /* other ${var...}, own dquote state */ 937206145Sjilles TSTATE_ARITH 938206145Sjilles } category; 939206145Sjilles}; 940206145Sjilles 941206145Sjilles 942205130Sjilles/* 943205130Sjilles * Called to parse command substitutions. 944205130Sjilles */ 9451556Srgrimes 946213811Sobrienstatic char * 947205130Sjillesparsebackq(char *out, struct nodelist **pbqlist, 948205130Sjilles int oldstyle, int dblquote, int quoted) 949205130Sjilles{ 950205130Sjilles struct nodelist **nlpp; 951205130Sjilles union node *n; 952205130Sjilles char *volatile str; 953205130Sjilles struct jmploc jmploc; 954205130Sjilles struct jmploc *const savehandler = handler; 955248980Sjilles size_t savelen; 956205130Sjilles int saveprompt; 957205130Sjilles const int bq_startlinno = plinno; 958205130Sjilles char *volatile ostr = NULL; 959205130Sjilles struct parsefile *const savetopfile = getcurrentfile(); 960208655Sjilles struct heredoc *const saveheredoclist = heredoclist; 961208655Sjilles struct heredoc *here; 962205130Sjilles 963205130Sjilles str = NULL; 964205130Sjilles if (setjmp(jmploc.loc)) { 965205130Sjilles popfilesupto(savetopfile); 966205130Sjilles if (str) 967205130Sjilles ckfree(str); 968205130Sjilles if (ostr) 969205130Sjilles ckfree(ostr); 970208655Sjilles heredoclist = saveheredoclist; 971205130Sjilles handler = savehandler; 972205130Sjilles if (exception == EXERROR) { 973205130Sjilles startlinno = bq_startlinno; 974205130Sjilles synerror("Error in command substitution"); 975205130Sjilles } 976205130Sjilles longjmp(handler->loc, 1); 977205130Sjilles } 978205130Sjilles INTOFF; 979205130Sjilles savelen = out - stackblock(); 980205130Sjilles if (savelen > 0) { 981205130Sjilles str = ckmalloc(savelen); 982205130Sjilles memcpy(str, stackblock(), savelen); 983205130Sjilles } 984205130Sjilles handler = &jmploc; 985208655Sjilles heredoclist = NULL; 986205130Sjilles INTON; 987205130Sjilles if (oldstyle) { 988205130Sjilles /* We must read until the closing backquote, giving special 989205130Sjilles treatment to some slashes, and then push the string and 990205130Sjilles reread it as input, interpreting it normally. */ 991205130Sjilles char *oout; 992205130Sjilles int c; 993205130Sjilles int olen; 994205130Sjilles 995205130Sjilles 996205130Sjilles STARTSTACKSTR(oout); 997205130Sjilles for (;;) { 998205130Sjilles if (needprompt) { 999205130Sjilles setprompt(2); 1000205130Sjilles needprompt = 0; 1001205130Sjilles } 1002215783Sjilles CHECKSTRSPACE(2, oout); 1003205130Sjilles switch (c = pgetc()) { 1004205130Sjilles case '`': 1005205130Sjilles goto done; 1006205130Sjilles 1007205130Sjilles case '\\': 1008205130Sjilles if ((c = pgetc()) == '\n') { 1009205130Sjilles plinno++; 1010205130Sjilles if (doprompt) 1011205130Sjilles setprompt(2); 1012205130Sjilles else 1013205130Sjilles setprompt(0); 1014205130Sjilles /* 1015205130Sjilles * If eating a newline, avoid putting 1016205130Sjilles * the newline into the new character 1017215783Sjilles * stream (via the USTPUTC after the 1018205130Sjilles * switch). 1019205130Sjilles */ 1020205130Sjilles continue; 1021205130Sjilles } 1022205130Sjilles if (c != '\\' && c != '`' && c != '$' 1023205130Sjilles && (!dblquote || c != '"')) 1024215783Sjilles USTPUTC('\\', oout); 1025205130Sjilles break; 1026205130Sjilles 1027205130Sjilles case '\n': 1028205130Sjilles plinno++; 1029205130Sjilles needprompt = doprompt; 1030205130Sjilles break; 1031205130Sjilles 1032205130Sjilles case PEOF: 1033205130Sjilles startlinno = plinno; 1034205130Sjilles synerror("EOF in backquote substitution"); 1035205130Sjilles break; 1036205130Sjilles 1037205130Sjilles default: 1038205130Sjilles break; 1039205130Sjilles } 1040215783Sjilles USTPUTC(c, oout); 1041205130Sjilles } 1042205130Sjillesdone: 1043215783Sjilles USTPUTC('\0', oout); 1044205130Sjilles olen = oout - stackblock(); 1045205130Sjilles INTOFF; 1046205130Sjilles ostr = ckmalloc(olen); 1047205130Sjilles memcpy(ostr, stackblock(), olen); 1048205130Sjilles setinputstring(ostr, 1); 1049205130Sjilles INTON; 1050205130Sjilles } 1051205130Sjilles nlpp = pbqlist; 1052205130Sjilles while (*nlpp) 1053205130Sjilles nlpp = &(*nlpp)->next; 1054205130Sjilles *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 1055205130Sjilles (*nlpp)->next = NULL; 1056205130Sjilles 1057205130Sjilles if (oldstyle) { 1058205130Sjilles saveprompt = doprompt; 1059205130Sjilles doprompt = 0; 1060205130Sjilles } 1061205130Sjilles 1062255087Sjilles n = list(0); 1063205130Sjilles 1064255087Sjilles if (oldstyle) { 1065255087Sjilles if (peektoken() != TEOF) 1066255087Sjilles synexpect(-1); 1067205130Sjilles doprompt = saveprompt; 1068255087Sjilles } else 1069255073Sjilles consumetoken(TRP); 1070205130Sjilles 1071205130Sjilles (*nlpp)->n = n; 1072205130Sjilles if (oldstyle) { 1073205130Sjilles /* 1074205130Sjilles * Start reading from old file again, ignoring any pushed back 1075205130Sjilles * tokens left from the backquote parsing 1076205130Sjilles */ 1077205130Sjilles popfile(); 1078205130Sjilles tokpushback = 0; 1079205130Sjilles } 1080205130Sjilles STARTSTACKSTR(out); 1081216706Sjilles CHECKSTRSPACE(savelen + 1, out); 1082208655Sjilles INTOFF; 1083205130Sjilles if (str) { 1084205130Sjilles memcpy(out, str, savelen); 1085205130Sjilles STADJUST(savelen, out); 1086205130Sjilles ckfree(str); 1087205130Sjilles str = NULL; 1088205130Sjilles } 1089205130Sjilles if (ostr) { 1090205130Sjilles ckfree(ostr); 1091205130Sjilles ostr = NULL; 1092205130Sjilles } 1093208655Sjilles here = saveheredoclist; 1094208655Sjilles if (here != NULL) { 1095208655Sjilles while (here->next != NULL) 1096208655Sjilles here = here->next; 1097208655Sjilles here->next = heredoclist; 1098208655Sjilles heredoclist = saveheredoclist; 1099208655Sjilles } 1100205130Sjilles handler = savehandler; 1101208655Sjilles INTON; 1102205130Sjilles if (quoted) 1103205130Sjilles USTPUTC(CTLBACKQ | CTLQUOTE, out); 1104205130Sjilles else 1105205130Sjilles USTPUTC(CTLBACKQ, out); 1106205130Sjilles return out; 1107205130Sjilles} 1108205130Sjilles 1109205130Sjilles 11101556Srgrimes/* 1111221513Sjilles * Called to parse a backslash escape sequence inside $'...'. 1112221513Sjilles * The backslash has already been read. 1113221513Sjilles */ 1114221513Sjillesstatic char * 1115221513Sjillesreadcstyleesc(char *out) 1116221513Sjilles{ 1117221513Sjilles int c, v, i, n; 1118221513Sjilles 1119221513Sjilles c = pgetc(); 1120221513Sjilles switch (c) { 1121221513Sjilles case '\0': 1122221513Sjilles synerror("Unterminated quoted string"); 1123221513Sjilles case '\n': 1124221513Sjilles plinno++; 1125221513Sjilles if (doprompt) 1126221513Sjilles setprompt(2); 1127221513Sjilles else 1128221513Sjilles setprompt(0); 1129221513Sjilles return out; 1130221513Sjilles case '\\': 1131221513Sjilles case '\'': 1132221513Sjilles case '"': 1133221513Sjilles v = c; 1134221513Sjilles break; 1135221513Sjilles case 'a': v = '\a'; break; 1136221513Sjilles case 'b': v = '\b'; break; 1137221513Sjilles case 'e': v = '\033'; break; 1138221513Sjilles case 'f': v = '\f'; break; 1139221513Sjilles case 'n': v = '\n'; break; 1140221513Sjilles case 'r': v = '\r'; break; 1141221513Sjilles case 't': v = '\t'; break; 1142221513Sjilles case 'v': v = '\v'; break; 1143221513Sjilles case 'x': 1144221513Sjilles v = 0; 1145221513Sjilles for (;;) { 1146221513Sjilles c = pgetc(); 1147221513Sjilles if (c >= '0' && c <= '9') 1148221513Sjilles v = (v << 4) + c - '0'; 1149221513Sjilles else if (c >= 'A' && c <= 'F') 1150221513Sjilles v = (v << 4) + c - 'A' + 10; 1151221513Sjilles else if (c >= 'a' && c <= 'f') 1152221513Sjilles v = (v << 4) + c - 'a' + 10; 1153221513Sjilles else 1154221513Sjilles break; 1155221513Sjilles } 1156221513Sjilles pungetc(); 1157221513Sjilles break; 1158221513Sjilles case '0': case '1': case '2': case '3': 1159221513Sjilles case '4': case '5': case '6': case '7': 1160221513Sjilles v = c - '0'; 1161221513Sjilles c = pgetc(); 1162221513Sjilles if (c >= '0' && c <= '7') { 1163221513Sjilles v <<= 3; 1164221513Sjilles v += c - '0'; 1165221513Sjilles c = pgetc(); 1166221513Sjilles if (c >= '0' && c <= '7') { 1167221513Sjilles v <<= 3; 1168221513Sjilles v += c - '0'; 1169221513Sjilles } else 1170221513Sjilles pungetc(); 1171221513Sjilles } else 1172221513Sjilles pungetc(); 1173221513Sjilles break; 1174221513Sjilles case 'c': 1175221513Sjilles c = pgetc(); 1176221513Sjilles if (c < 0x3f || c > 0x7a || c == 0x60) 1177221513Sjilles synerror("Bad escape sequence"); 1178221513Sjilles if (c == '\\' && pgetc() != '\\') 1179221513Sjilles synerror("Bad escape sequence"); 1180221513Sjilles if (c == '?') 1181221513Sjilles v = 127; 1182221513Sjilles else 1183221513Sjilles v = c & 0x1f; 1184221513Sjilles break; 1185221513Sjilles case 'u': 1186221513Sjilles case 'U': 1187221513Sjilles n = c == 'U' ? 8 : 4; 1188221513Sjilles v = 0; 1189221513Sjilles for (i = 0; i < n; i++) { 1190221513Sjilles c = pgetc(); 1191221513Sjilles if (c >= '0' && c <= '9') 1192221513Sjilles v = (v << 4) + c - '0'; 1193221513Sjilles else if (c >= 'A' && c <= 'F') 1194221513Sjilles v = (v << 4) + c - 'A' + 10; 1195221513Sjilles else if (c >= 'a' && c <= 'f') 1196221513Sjilles v = (v << 4) + c - 'a' + 10; 1197221513Sjilles else 1198221513Sjilles synerror("Bad escape sequence"); 1199221513Sjilles } 1200221513Sjilles if (v == 0 || (v >= 0xd800 && v <= 0xdfff)) 1201221513Sjilles synerror("Bad escape sequence"); 1202221513Sjilles /* We really need iconv here. */ 1203221669Sjilles if (initial_localeisutf8 && v > 127) { 1204221669Sjilles CHECKSTRSPACE(4, out); 1205221669Sjilles /* 1206221669Sjilles * We cannot use wctomb() as the locale may have 1207221669Sjilles * changed. 1208221669Sjilles */ 1209221669Sjilles if (v <= 0x7ff) { 1210221669Sjilles USTPUTC(0xc0 | v >> 6, out); 1211221669Sjilles USTPUTC(0x80 | (v & 0x3f), out); 1212221669Sjilles return out; 1213221669Sjilles } else if (v <= 0xffff) { 1214221669Sjilles USTPUTC(0xe0 | v >> 12, out); 1215221669Sjilles USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1216221669Sjilles USTPUTC(0x80 | (v & 0x3f), out); 1217221669Sjilles return out; 1218221669Sjilles } else if (v <= 0x10ffff) { 1219221669Sjilles USTPUTC(0xf0 | v >> 18, out); 1220221669Sjilles USTPUTC(0x80 | ((v >> 12) & 0x3f), out); 1221221669Sjilles USTPUTC(0x80 | ((v >> 6) & 0x3f), out); 1222221669Sjilles USTPUTC(0x80 | (v & 0x3f), out); 1223221669Sjilles return out; 1224221669Sjilles } 1225221669Sjilles } 1226221513Sjilles if (v > 127) 1227221513Sjilles v = '?'; 1228221513Sjilles break; 1229221513Sjilles default: 1230221513Sjilles synerror("Bad escape sequence"); 1231221513Sjilles } 1232221513Sjilles v = (char)v; 1233221513Sjilles /* 1234221513Sjilles * We can't handle NUL bytes. 1235221513Sjilles * POSIX says we should skip till the closing quote. 1236221513Sjilles */ 1237221513Sjilles if (v == '\0') { 1238221513Sjilles while ((c = pgetc()) != '\'') { 1239221513Sjilles if (c == '\\') 1240221513Sjilles c = pgetc(); 1241221513Sjilles if (c == PEOF) 1242221513Sjilles synerror("Unterminated quoted string"); 1243221513Sjilles } 1244221513Sjilles pungetc(); 1245221513Sjilles return out; 1246221513Sjilles } 1247221513Sjilles if (SQSYNTAX[v] == CCTL) 1248221513Sjilles USTPUTC(CTLESC, out); 1249221513Sjilles USTPUTC(v, out); 1250221513Sjilles return out; 1251221513Sjilles} 1252221513Sjilles 1253221513Sjilles 1254221513Sjilles/* 12551556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 12561556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 12571556Srgrimes * word which marks the end of the document and striptabs is true if 12581556Srgrimes * leading tabs should be stripped from the document. The argument firstc 12591556Srgrimes * is the first character of the input token or document. 12601556Srgrimes * 12611556Srgrimes * Because C does not have internal subroutines, I have simulated them 12621556Srgrimes * using goto's to implement the subroutine linkage. The following macros 12631556Srgrimes * will run code that appears at the end of readtoken1. 12641556Srgrimes */ 12651556Srgrimes 12661556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 12671556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 12681556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 12691556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 12701556Srgrimes 1271213811Sobrienstatic int 1272248980Sjillesreadtoken1(int firstc, char const *initialsyntax, const char *eofmark, 1273248980Sjilles int striptabs) 127490111Simp{ 127517987Speter int c = firstc; 127617987Speter char *out; 12771556Srgrimes int len; 12781556Srgrimes char line[EOFMARKLEN + 1]; 12791556Srgrimes struct nodelist *bqlist; 12801556Srgrimes int quotef; 1281206145Sjilles int newvarnest; 1282206145Sjilles int level; 128354679Scracauer int synentry; 1284213811Sobrien struct tokenstate state_static[MAXNEST_static]; 1285213811Sobrien int maxnest = MAXNEST_static; 1286206145Sjilles struct tokenstate *state = state_static; 1287221513Sjilles int sqiscstyle = 0; 12881556Srgrimes 12891556Srgrimes startlinno = plinno; 12901556Srgrimes quotef = 0; 12911556Srgrimes bqlist = NULL; 1292206145Sjilles newvarnest = 0; 1293206145Sjilles level = 0; 1294206145Sjilles state[level].syntax = initialsyntax; 1295206145Sjilles state[level].parenlevel = 0; 1296206145Sjilles state[level].category = TSTATE_TOP; 12971556Srgrimes 12981556Srgrimes STARTSTACKSTR(out); 12991556Srgrimes loop: { /* for each line, until end of word */ 13001556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 13011556Srgrimes for (;;) { /* until end of line or end of word */ 1302214512Sjilles CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ 130354679Scracauer 1304206145Sjilles synentry = state[level].syntax[c]; 130554679Scracauer 130654679Scracauer switch(synentry) { 13071556Srgrimes case CNL: /* '\n' */ 1308206145Sjilles if (state[level].syntax == BASESYNTAX) 13091556Srgrimes goto endword; /* exit outer loop */ 13101556Srgrimes USTPUTC(c, out); 13111556Srgrimes plinno++; 13121556Srgrimes if (doprompt) 13131556Srgrimes setprompt(2); 13141556Srgrimes else 13151556Srgrimes setprompt(0); 13161556Srgrimes c = pgetc(); 13171556Srgrimes goto loop; /* continue outer loop */ 1318221513Sjilles case CSBACK: 1319221513Sjilles if (sqiscstyle) { 1320221513Sjilles out = readcstyleesc(out); 1321221513Sjilles break; 1322221513Sjilles } 1323221513Sjilles /* FALLTHROUGH */ 13241556Srgrimes case CWORD: 13251556Srgrimes USTPUTC(c, out); 13261556Srgrimes break; 13271556Srgrimes case CCTL: 1328206145Sjilles if (eofmark == NULL || initialsyntax != SQSYNTAX) 13291556Srgrimes USTPUTC(CTLESC, out); 13301556Srgrimes USTPUTC(c, out); 13311556Srgrimes break; 13321556Srgrimes case CBACK: /* backslash */ 13331556Srgrimes c = pgetc(); 13341556Srgrimes if (c == PEOF) { 13351556Srgrimes USTPUTC('\\', out); 13361556Srgrimes pungetc(); 13371556Srgrimes } else if (c == '\n') { 1338160849Syar plinno++; 13391556Srgrimes if (doprompt) 13401556Srgrimes setprompt(2); 13411556Srgrimes else 13421556Srgrimes setprompt(0); 13431556Srgrimes } else { 1344206145Sjilles if (state[level].syntax == DQSYNTAX && 1345206145Sjilles c != '\\' && c != '`' && c != '$' && 1346206145Sjilles (c != '"' || (eofmark != NULL && 1347206145Sjilles newvarnest == 0)) && 1348206145Sjilles (c != '}' || state[level].category != TSTATE_VAR_OLD)) 13491556Srgrimes USTPUTC('\\', out); 1350214512Sjilles if ((eofmark == NULL || 1351214512Sjilles newvarnest > 0) && 1352214512Sjilles state[level].syntax == BASESYNTAX) 1353214512Sjilles USTPUTC(CTLQUOTEMARK, out); 135483675Stegge if (SQSYNTAX[c] == CCTL) 13551556Srgrimes USTPUTC(CTLESC, out); 13561556Srgrimes USTPUTC(c, out); 1357214512Sjilles if ((eofmark == NULL || 1358214512Sjilles newvarnest > 0) && 1359214512Sjilles state[level].syntax == BASESYNTAX && 1360214512Sjilles state[level].category == TSTATE_VAR_OLD) 1361214512Sjilles USTPUTC(CTLQUOTEEND, out); 13621556Srgrimes quotef++; 13631556Srgrimes } 13641556Srgrimes break; 13651556Srgrimes case CSQUOTE: 1366206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1367206145Sjilles state[level].syntax = SQSYNTAX; 1368221513Sjilles sqiscstyle = 0; 13691556Srgrimes break; 13701556Srgrimes case CDQUOTE: 1371206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1372206145Sjilles state[level].syntax = DQSYNTAX; 13731556Srgrimes break; 13741556Srgrimes case CENDQUOTE: 1375206145Sjilles if (eofmark != NULL && newvarnest == 0) 13761556Srgrimes USTPUTC(c, out); 1377206145Sjilles else { 1378214512Sjilles if (state[level].category == TSTATE_VAR_OLD) 1379214512Sjilles USTPUTC(CTLQUOTEEND, out); 1380214305Sjilles state[level].syntax = BASESYNTAX; 13811556Srgrimes quotef++; 13821556Srgrimes } 13831556Srgrimes break; 13841556Srgrimes case CVAR: /* '$' */ 13851556Srgrimes PARSESUB(); /* parse substitution */ 13861556Srgrimes break; 13871556Srgrimes case CENDVAR: /* '}' */ 1388206145Sjilles if (level > 0 && 1389214492Sjilles ((state[level].category == TSTATE_VAR_OLD && 1390214492Sjilles state[level].syntax == 1391214492Sjilles state[level - 1].syntax) || 1392214490Sjilles (state[level].category == TSTATE_VAR_NEW && 1393214490Sjilles state[level].syntax == BASESYNTAX))) { 1394214492Sjilles if (state[level].category == TSTATE_VAR_NEW) 1395206145Sjilles newvarnest--; 1396206145Sjilles level--; 13971556Srgrimes USTPUTC(CTLENDVAR, out); 13981556Srgrimes } else { 13991556Srgrimes USTPUTC(c, out); 14001556Srgrimes } 14011556Srgrimes break; 14021556Srgrimes case CLP: /* '(' in arithmetic */ 1403206145Sjilles state[level].parenlevel++; 14041556Srgrimes USTPUTC(c, out); 14051556Srgrimes break; 14061556Srgrimes case CRP: /* ')' in arithmetic */ 1407206145Sjilles if (state[level].parenlevel > 0) { 14081556Srgrimes USTPUTC(c, out); 1409206145Sjilles --state[level].parenlevel; 14101556Srgrimes } else { 14111556Srgrimes if (pgetc() == ')') { 1412206145Sjilles if (level > 0 && 1413206145Sjilles state[level].category == TSTATE_ARITH) { 1414206145Sjilles level--; 14151556Srgrimes USTPUTC(CTLENDARI, out); 14161556Srgrimes } else 14171556Srgrimes USTPUTC(')', out); 14181556Srgrimes } else { 14198855Srgrimes /* 14201556Srgrimes * unbalanced parens 14211556Srgrimes * (don't 2nd guess - no error) 14221556Srgrimes */ 14231556Srgrimes pungetc(); 14241556Srgrimes USTPUTC(')', out); 14251556Srgrimes } 14261556Srgrimes } 14271556Srgrimes break; 14281556Srgrimes case CBQUOTE: /* '`' */ 1429206145Sjilles out = parsebackq(out, &bqlist, 1, 1430206145Sjilles state[level].syntax == DQSYNTAX && 1431206145Sjilles (eofmark == NULL || newvarnest > 0), 1432206145Sjilles state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX); 14331556Srgrimes break; 14341556Srgrimes case CEOF: 14351556Srgrimes goto endword; /* exit outer loop */ 1436214305Sjilles case CIGN: 1437214305Sjilles break; 14381556Srgrimes default: 1439206145Sjilles if (level == 0) 14401556Srgrimes goto endword; /* exit outer loop */ 14411556Srgrimes USTPUTC(c, out); 14421556Srgrimes } 14431556Srgrimes c = pgetc_macro(); 14441556Srgrimes } 14451556Srgrimes } 14461556Srgrimesendword: 1447206145Sjilles if (state[level].syntax == ARISYNTAX) 14481556Srgrimes synerror("Missing '))'"); 1449206145Sjilles if (state[level].syntax != BASESYNTAX && eofmark == NULL) 14501556Srgrimes synerror("Unterminated quoted string"); 1451206145Sjilles if (state[level].category == TSTATE_VAR_OLD || 1452206145Sjilles state[level].category == TSTATE_VAR_NEW) { 14531556Srgrimes startlinno = plinno; 14541556Srgrimes synerror("Missing '}'"); 14551556Srgrimes } 1456206145Sjilles if (state != state_static) 1457206145Sjilles parser_temp_free_upto(state); 14581556Srgrimes USTPUTC('\0', out); 14591556Srgrimes len = out - stackblock(); 14601556Srgrimes out = stackblock(); 14611556Srgrimes if (eofmark == NULL) { 14621556Srgrimes if ((c == '>' || c == '<') 14631556Srgrimes && quotef == 0 14641556Srgrimes && len <= 2 14651556Srgrimes && (*out == '\0' || is_digit(*out))) { 14661556Srgrimes PARSEREDIR(); 14671556Srgrimes return lasttoken = TREDIR; 14681556Srgrimes } else { 14691556Srgrimes pungetc(); 14701556Srgrimes } 14711556Srgrimes } 14721556Srgrimes quoteflag = quotef; 14731556Srgrimes backquotelist = bqlist; 14741556Srgrimes grabstackblock(len); 14751556Srgrimes wordtext = out; 14761556Srgrimes return lasttoken = TWORD; 14771556Srgrimes/* end of readtoken routine */ 14781556Srgrimes 14791556Srgrimes 14801556Srgrimes/* 14811556Srgrimes * Check to see whether we are at the end of the here document. When this 14821556Srgrimes * is called, c is set to the first character of the next input line. If 14831556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 14841556Srgrimes */ 14851556Srgrimes 14861556Srgrimescheckend: { 14871556Srgrimes if (eofmark) { 14881556Srgrimes if (striptabs) { 14891556Srgrimes while (c == '\t') 14901556Srgrimes c = pgetc(); 14911556Srgrimes } 14921556Srgrimes if (c == *eofmark) { 14931556Srgrimes if (pfgets(line, sizeof line) != NULL) { 1494248980Sjilles const char *p, *q; 14951556Srgrimes 14961556Srgrimes p = line; 14971556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 1498222134Sjilles if ((*p == '\0' || *p == '\n') && *q == '\0') { 14991556Srgrimes c = PEOF; 1500222134Sjilles if (*p == '\n') { 1501222134Sjilles plinno++; 1502222134Sjilles needprompt = doprompt; 1503222134Sjilles } 15041556Srgrimes } else { 15051556Srgrimes pushstring(line, strlen(line), NULL); 15061556Srgrimes } 15071556Srgrimes } 15081556Srgrimes } 15091556Srgrimes } 15101556Srgrimes goto checkend_return; 15111556Srgrimes} 15121556Srgrimes 15131556Srgrimes 15141556Srgrimes/* 15151556Srgrimes * Parse a redirection operator. The variable "out" points to a string 15161556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 15171556Srgrimes * first character of the redirection operator. 15181556Srgrimes */ 15191556Srgrimes 15201556Srgrimesparseredir: { 15211556Srgrimes char fd = *out; 15221556Srgrimes union node *np; 15231556Srgrimes 15241556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 15251556Srgrimes if (c == '>') { 15261556Srgrimes np->nfile.fd = 1; 15271556Srgrimes c = pgetc(); 15281556Srgrimes if (c == '>') 15291556Srgrimes np->type = NAPPEND; 15301556Srgrimes else if (c == '&') 15311556Srgrimes np->type = NTOFD; 153296922Stjr else if (c == '|') 153396922Stjr np->type = NCLOBBER; 15341556Srgrimes else { 15351556Srgrimes np->type = NTO; 15361556Srgrimes pungetc(); 15371556Srgrimes } 15381556Srgrimes } else { /* c == '<' */ 15391556Srgrimes np->nfile.fd = 0; 15401556Srgrimes c = pgetc(); 15411556Srgrimes if (c == '<') { 15421556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 15431556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 15441556Srgrimes np->nfile.fd = 0; 15451556Srgrimes } 15461556Srgrimes np->type = NHERE; 15471556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 15481556Srgrimes heredoc->here = np; 15491556Srgrimes if ((c = pgetc()) == '-') { 15501556Srgrimes heredoc->striptabs = 1; 15511556Srgrimes } else { 15521556Srgrimes heredoc->striptabs = 0; 15531556Srgrimes pungetc(); 15541556Srgrimes } 15551556Srgrimes } else if (c == '&') 15561556Srgrimes np->type = NFROMFD; 155766612Sbrian else if (c == '>') 155866612Sbrian np->type = NFROMTO; 15591556Srgrimes else { 15601556Srgrimes np->type = NFROM; 15611556Srgrimes pungetc(); 15621556Srgrimes } 15631556Srgrimes } 15641556Srgrimes if (fd != '\0') 15651556Srgrimes np->nfile.fd = digit_val(fd); 15661556Srgrimes redirnode = np; 15671556Srgrimes goto parseredir_return; 15681556Srgrimes} 15691556Srgrimes 15701556Srgrimes 15711556Srgrimes/* 15721556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 15731556Srgrimes * and nothing else. 15741556Srgrimes */ 15751556Srgrimes 15761556Srgrimesparsesub: { 1577179022Sstefanf char buf[10]; 15781556Srgrimes int subtype; 15791556Srgrimes int typeloc; 15801556Srgrimes int flags; 15811556Srgrimes char *p; 15821556Srgrimes static const char types[] = "}-+?="; 1583179022Sstefanf int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 1584179022Sstefanf int linno; 1585179387Sstefanf int length; 1586219623Sjilles int c1; 15871556Srgrimes 15881556Srgrimes c = pgetc(); 1589221513Sjilles if (c == '(') { /* $(command) or $((arith)) */ 15901556Srgrimes if (pgetc() == '(') { 15911556Srgrimes PARSEARITH(); 15921556Srgrimes } else { 15931556Srgrimes pungetc(); 1594206145Sjilles out = parsebackq(out, &bqlist, 0, 1595206145Sjilles state[level].syntax == DQSYNTAX && 1596206145Sjilles (eofmark == NULL || newvarnest > 0), 1597206145Sjilles state[level].syntax == DQSYNTAX || 1598206145Sjilles state[level].syntax == ARISYNTAX); 15991556Srgrimes } 1600221513Sjilles } else if (c == '{' || is_name(c) || is_special(c)) { 16011556Srgrimes USTPUTC(CTLVAR, out); 16021556Srgrimes typeloc = out - stackblock(); 16031556Srgrimes USTPUTC(VSNORMAL, out); 16041556Srgrimes subtype = VSNORMAL; 1605179022Sstefanf flags = 0; 16061556Srgrimes if (c == '{') { 160718202Speter bracketed_name = 1; 16081556Srgrimes c = pgetc(); 1609219623Sjilles subtype = 0; 16101556Srgrimes } 1611219623Sjillesvarname: 1612149026Sstefanf if (!is_eof(c) && is_name(c)) { 1613179387Sstefanf length = 0; 16141556Srgrimes do { 16151556Srgrimes STPUTC(c, out); 16161556Srgrimes c = pgetc(); 1617179387Sstefanf length++; 1618149026Sstefanf } while (!is_eof(c) && is_in_name(c)); 1619179387Sstefanf if (length == 6 && 1620179387Sstefanf strncmp(out - length, "LINENO", length) == 0) { 1621179022Sstefanf /* Replace the variable name with the 1622179022Sstefanf * current line number. */ 1623179022Sstefanf linno = plinno; 1624179022Sstefanf if (funclinno != 0) 1625179022Sstefanf linno -= funclinno - 1; 1626179022Sstefanf snprintf(buf, sizeof(buf), "%d", linno); 1627179022Sstefanf STADJUST(-6, out); 1628215783Sjilles STPUTS(buf, out); 1629179022Sstefanf flags |= VSLINENO; 1630179022Sstefanf } 163118202Speter } else if (is_digit(c)) { 163218202Speter if (bracketed_name) { 163318202Speter do { 163418202Speter STPUTC(c, out); 163518202Speter c = pgetc(); 163618202Speter } while (is_digit(c)); 163718202Speter } else { 163818202Speter STPUTC(c, out); 163918202Speter c = pgetc(); 164018202Speter } 1641219623Sjilles } else if (is_special(c)) { 1642219623Sjilles c1 = c; 1643219623Sjilles c = pgetc(); 1644219623Sjilles if (subtype == 0 && c1 == '#') { 1645219623Sjilles subtype = VSLENGTH; 1646219623Sjilles if (strchr(types, c) == NULL && c != ':' && 1647219623Sjilles c != '#' && c != '%') 1648219623Sjilles goto varname; 1649219623Sjilles c1 = c; 1650219623Sjilles c = pgetc(); 1651219623Sjilles if (c1 != '}' && c == '}') { 1652219623Sjilles pungetc(); 1653219623Sjilles c = c1; 1654219623Sjilles goto varname; 1655219623Sjilles } 1656219623Sjilles pungetc(); 1657219623Sjilles c = c1; 1658219623Sjilles c1 = '#'; 1659219623Sjilles subtype = 0; 1660219623Sjilles } 1661219623Sjilles USTPUTC(c1, out); 16621556Srgrimes } else { 1663219623Sjilles subtype = VSERROR; 1664219623Sjilles if (c == '}') 1665219623Sjilles pungetc(); 1666219623Sjilles else if (c == '\n' || c == PEOF) 1667219623Sjilles synerror("Unexpected end of line in substitution"); 1668219623Sjilles else 1669164003Sstefanf USTPUTC(c, out); 16701556Srgrimes } 16711556Srgrimes if (subtype == 0) { 167217987Speter switch (c) { 167317987Speter case ':': 1674179022Sstefanf flags |= VSNUL; 16751556Srgrimes c = pgetc(); 167617987Speter /*FALLTHROUGH*/ 167717987Speter default: 167817987Speter p = strchr(types, c); 1679164003Sstefanf if (p == NULL) { 1680206144Sjilles if (c == '\n' || c == PEOF) 1681206144Sjilles synerror("Unexpected end of line in substitution"); 1682164003Sstefanf if (flags == VSNUL) 1683164003Sstefanf STPUTC(':', out); 1684164003Sstefanf STPUTC(c, out); 1685164003Sstefanf subtype = VSERROR; 1686164003Sstefanf } else 1687164003Sstefanf subtype = p - types + VSNORMAL; 168817987Speter break; 168917987Speter case '%': 169020425Ssteve case '#': 169117987Speter { 169217987Speter int cc = c; 169317987Speter subtype = c == '#' ? VSTRIMLEFT : 169417987Speter VSTRIMRIGHT; 169517987Speter c = pgetc(); 169617987Speter if (c == cc) 169717987Speter subtype++; 169817987Speter else 169917987Speter pungetc(); 170017987Speter break; 170117987Speter } 17021556Srgrimes } 1703164003Sstefanf } else if (subtype != VSERROR) { 1704221461Sjilles if (subtype == VSLENGTH && c != '}') 1705221461Sjilles subtype = VSERROR; 17061556Srgrimes pungetc(); 17071556Srgrimes } 1708164003Sstefanf STPUTC('=', out); 1709220903Sjilles if (state[level].syntax == DQSYNTAX || 1710220903Sjilles state[level].syntax == ARISYNTAX) 17111556Srgrimes flags |= VSQUOTE; 17121556Srgrimes *(stackblock() + typeloc) = subtype | flags; 1713206145Sjilles if (subtype != VSNORMAL) { 1714206145Sjilles if (level + 1 >= maxnest) { 1715206145Sjilles maxnest *= 2; 1716206145Sjilles if (state == state_static) { 1717206145Sjilles state = parser_temp_alloc( 1718206145Sjilles maxnest * sizeof(*state)); 1719206145Sjilles memcpy(state, state_static, 1720213811Sobrien MAXNEST_static * sizeof(*state)); 1721206145Sjilles } else 1722206145Sjilles state = parser_temp_realloc(state, 1723206145Sjilles maxnest * sizeof(*state)); 1724206145Sjilles } 1725206145Sjilles level++; 1726206145Sjilles state[level].parenlevel = 0; 1727206145Sjilles if (subtype == VSMINUS || subtype == VSPLUS || 1728206145Sjilles subtype == VSQUESTION || subtype == VSASSIGN) { 1729206145Sjilles /* 1730206145Sjilles * For operators that were in the Bourne shell, 1731206145Sjilles * inherit the double-quote state. 1732206145Sjilles */ 1733206145Sjilles state[level].syntax = state[level - 1].syntax; 1734206145Sjilles state[level].category = TSTATE_VAR_OLD; 1735206145Sjilles } else { 1736206145Sjilles /* 1737206145Sjilles * The other operators take a pattern, 1738206145Sjilles * so go to BASESYNTAX. 1739206145Sjilles * Also, ' and " are now special, even 1740206145Sjilles * in here documents. 1741206145Sjilles */ 1742206145Sjilles state[level].syntax = BASESYNTAX; 1743206145Sjilles state[level].category = TSTATE_VAR_NEW; 1744206145Sjilles newvarnest++; 1745206145Sjilles } 1746206145Sjilles } 1747221513Sjilles } else if (c == '\'' && state[level].syntax == BASESYNTAX) { 1748221513Sjilles /* $'cstylequotes' */ 1749221513Sjilles USTPUTC(CTLQUOTEMARK, out); 1750221513Sjilles state[level].syntax = SQSYNTAX; 1751221513Sjilles sqiscstyle = 1; 1752221513Sjilles } else { 1753221513Sjilles USTPUTC('$', out); 1754221513Sjilles pungetc(); 17551556Srgrimes } 17561556Srgrimes goto parsesub_return; 17571556Srgrimes} 17581556Srgrimes 17591556Srgrimes 17601556Srgrimes/* 17611556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 17621556Srgrimes */ 17631556Srgrimesparsearith: { 17641556Srgrimes 1765206145Sjilles if (level + 1 >= maxnest) { 1766206145Sjilles maxnest *= 2; 1767206145Sjilles if (state == state_static) { 1768206145Sjilles state = parser_temp_alloc( 1769206145Sjilles maxnest * sizeof(*state)); 1770206145Sjilles memcpy(state, state_static, 1771213811Sobrien MAXNEST_static * sizeof(*state)); 1772206145Sjilles } else 1773206145Sjilles state = parser_temp_realloc(state, 1774206145Sjilles maxnest * sizeof(*state)); 17751556Srgrimes } 1776206145Sjilles level++; 1777206145Sjilles state[level].syntax = ARISYNTAX; 1778206145Sjilles state[level].parenlevel = 0; 1779206145Sjilles state[level].category = TSTATE_ARITH; 1780206145Sjilles USTPUTC(CTLARI, out); 1781206145Sjilles if (state[level - 1].syntax == DQSYNTAX) 1782206145Sjilles USTPUTC('"',out); 1783206145Sjilles else 1784206145Sjilles USTPUTC(' ',out); 17851556Srgrimes goto parsearith_return; 17861556Srgrimes} 17871556Srgrimes 17881556Srgrimes} /* end of readtoken */ 17891556Srgrimes 17901556Srgrimes 17911556Srgrimes/* 17921556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 17931556Srgrimes * or backquotes). 17941556Srgrimes */ 17951556Srgrimes 1796213811Sobrienstatic int 179790111Simpnoexpand(char *text) 179890111Simp{ 179925230Ssteve char *p; 180025230Ssteve char c; 18011556Srgrimes 18021556Srgrimes p = text; 18031556Srgrimes while ((c = *p++) != '\0') { 180439137Stegge if ( c == CTLQUOTEMARK) 180539137Stegge continue; 18061556Srgrimes if (c == CTLESC) 18071556Srgrimes p++; 180883675Stegge else if (BASESYNTAX[(int)c] == CCTL) 18091556Srgrimes return 0; 18101556Srgrimes } 18111556Srgrimes return 1; 18121556Srgrimes} 18131556Srgrimes 18141556Srgrimes 18151556Srgrimes/* 18161556Srgrimes * Return true if the argument is a legal variable name (a letter or 18171556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 18181556Srgrimes */ 18191556Srgrimes 18201556Srgrimesint 1821200956Sjillesgoodname(const char *name) 182290111Simp{ 1823200956Sjilles const char *p; 18241556Srgrimes 18251556Srgrimes p = name; 18261556Srgrimes if (! is_name(*p)) 18271556Srgrimes return 0; 18281556Srgrimes while (*++p) { 18291556Srgrimes if (! is_in_name(*p)) 18301556Srgrimes return 0; 18311556Srgrimes } 18321556Srgrimes return 1; 18331556Srgrimes} 18341556Srgrimes 18351556Srgrimes 1836222165Sjillesint 1837222165Sjillesisassignment(const char *p) 1838222165Sjilles{ 1839222165Sjilles if (!is_name(*p)) 1840222165Sjilles return 0; 1841222165Sjilles p++; 1842222165Sjilles for (;;) { 1843222165Sjilles if (*p == '=') 1844222165Sjilles return 1; 1845222165Sjilles else if (!is_in_name(*p)) 1846222165Sjilles return 0; 1847222165Sjilles p++; 1848222165Sjilles } 1849222165Sjilles} 1850222165Sjilles 1851222165Sjilles 1852255073Sjillesstatic void 1853255073Sjillesconsumetoken(int token) 1854255073Sjilles{ 1855255073Sjilles if (readtoken() != token) 1856255073Sjilles synexpect(token); 1857255073Sjilles} 1858255073Sjilles 1859255073Sjilles 18601556Srgrimes/* 18611556Srgrimes * Called when an unexpected token is read during the parse. The argument 18621556Srgrimes * is the token that is expected, or -1 if more than one type of token can 18631556Srgrimes * occur at this point. 18641556Srgrimes */ 18651556Srgrimes 1866213811Sobrienstatic void 186790111Simpsynexpect(int token) 186817987Speter{ 18691556Srgrimes char msg[64]; 18701556Srgrimes 18711556Srgrimes if (token >= 0) { 18721556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 18731556Srgrimes tokname[lasttoken], tokname[token]); 18741556Srgrimes } else { 18751556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 18761556Srgrimes } 18771556Srgrimes synerror(msg); 18781556Srgrimes} 18791556Srgrimes 18801556Srgrimes 1881213811Sobrienstatic void 1882201053Sjillessynerror(const char *msg) 188390111Simp{ 18841556Srgrimes if (commandname) 1885201366Sjilles outfmt(out2, "%s: %d: ", commandname, startlinno); 1886201366Sjilles outfmt(out2, "Syntax error: %s\n", msg); 18871556Srgrimes error((char *)NULL); 18881556Srgrimes} 18891556Srgrimes 1890213811Sobrienstatic void 189190111Simpsetprompt(int which) 189290111Simp{ 18931556Srgrimes whichprompt = which; 18941556Srgrimes 189517987Speter#ifndef NO_HISTORY 18961556Srgrimes if (!el) 189717987Speter#endif 1898199629Sjilles { 18991556Srgrimes out2str(getprompt(NULL)); 1900199629Sjilles flushout(out2); 1901199629Sjilles } 19021556Srgrimes} 19031556Srgrimes 19041556Srgrimes/* 19051556Srgrimes * called by editline -- any expansions to the prompt 19061556Srgrimes * should be added here. 19071556Srgrimes */ 19081556Srgrimeschar * 190990111Simpgetprompt(void *unused __unused) 191025905Ssteve{ 1911142845Sobrien static char ps[PROMPTLEN]; 1912142845Sobrien char *fmt; 1913209653Sjilles const char *pwd; 1914209653Sjilles int i, trim; 1915214538Sjilles static char internal_error[] = "??"; 1916142845Sobrien 1917142845Sobrien /* 1918142845Sobrien * Select prompt format. 1919142845Sobrien */ 19201556Srgrimes switch (whichprompt) { 19211556Srgrimes case 0: 1922201053Sjilles fmt = nullstr; 1923142845Sobrien break; 19241556Srgrimes case 1: 1925142845Sobrien fmt = ps1val(); 1926142845Sobrien break; 19271556Srgrimes case 2: 1928142845Sobrien fmt = ps2val(); 1929142845Sobrien break; 19301556Srgrimes default: 1931201053Sjilles return internal_error; 19321556Srgrimes } 1933142845Sobrien 1934142845Sobrien /* 1935142845Sobrien * Format prompt string. 1936142845Sobrien */ 1937142845Sobrien for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1938142845Sobrien if (*fmt == '\\') 1939142845Sobrien switch (*++fmt) { 1940142845Sobrien 1941142845Sobrien /* 1942142845Sobrien * Hostname. 1943142845Sobrien * 1944142845Sobrien * \h specifies just the local hostname, 1945142845Sobrien * \H specifies fully-qualified hostname. 1946142845Sobrien */ 1947142845Sobrien case 'h': 1948142845Sobrien case 'H': 1949149024Sstefanf ps[i] = '\0'; 1950142845Sobrien gethostname(&ps[i], PROMPTLEN - i); 1951142845Sobrien /* Skip to end of hostname. */ 1952142845Sobrien trim = (*fmt == 'h') ? '.' : '\0'; 1953142845Sobrien while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1954142845Sobrien i++; 1955142845Sobrien break; 1956142845Sobrien 1957142845Sobrien /* 1958142845Sobrien * Working directory. 1959142845Sobrien * 1960142845Sobrien * \W specifies just the final component, 1961142845Sobrien * \w specifies the entire path. 1962142845Sobrien */ 1963142845Sobrien case 'W': 1964142845Sobrien case 'w': 1965209653Sjilles pwd = lookupvar("PWD"); 1966209653Sjilles if (pwd == NULL) 1967209653Sjilles pwd = "?"; 1968209653Sjilles if (*fmt == 'W' && 1969209653Sjilles *pwd == '/' && pwd[1] != '\0') 1970209653Sjilles strlcpy(&ps[i], strrchr(pwd, '/') + 1, 1971209653Sjilles PROMPTLEN - i); 1972209653Sjilles else 1973209653Sjilles strlcpy(&ps[i], pwd, PROMPTLEN - i); 1974142845Sobrien /* Skip to end of path. */ 1975142845Sobrien while (ps[i + 1] != '\0') 1976142845Sobrien i++; 1977142845Sobrien break; 1978142845Sobrien 1979142845Sobrien /* 1980142845Sobrien * Superuser status. 1981142845Sobrien * 1982142845Sobrien * '$' for normal users, '#' for root. 1983142845Sobrien */ 1984142845Sobrien case '$': 1985142845Sobrien ps[i] = (geteuid() != 0) ? '$' : '#'; 1986142845Sobrien break; 1987142845Sobrien 1988142845Sobrien /* 1989142845Sobrien * A literal \. 1990142845Sobrien */ 1991142845Sobrien case '\\': 1992142845Sobrien ps[i] = '\\'; 1993142845Sobrien break; 1994142845Sobrien 1995142845Sobrien /* 1996142845Sobrien * Emit unrecognized formats verbatim. 1997142845Sobrien */ 1998142845Sobrien default: 1999142845Sobrien ps[i++] = '\\'; 2000142845Sobrien ps[i] = *fmt; 2001142845Sobrien break; 2002142845Sobrien } 2003142845Sobrien else 2004142845Sobrien ps[i] = *fmt; 2005142845Sobrien ps[i] = '\0'; 2006142845Sobrien return (ps); 20071556Srgrimes} 2008222907Sjilles 2009222907Sjilles 2010222907Sjillesconst char * 2011248980Sjillesexpandstr(const char *ps) 2012222907Sjilles{ 2013222907Sjilles union node n; 2014222907Sjilles struct jmploc jmploc; 2015222907Sjilles struct jmploc *const savehandler = handler; 2016222907Sjilles const int saveprompt = doprompt; 2017222907Sjilles struct parsefile *const savetopfile = getcurrentfile(); 2018222907Sjilles struct parser_temp *const saveparser_temp = parser_temp; 2019222907Sjilles const char *result = NULL; 2020222907Sjilles 2021222907Sjilles if (!setjmp(jmploc.loc)) { 2022222907Sjilles handler = &jmploc; 2023222907Sjilles parser_temp = NULL; 2024222907Sjilles setinputstring(ps, 1); 2025222907Sjilles doprompt = 0; 2026222907Sjilles readtoken1(pgetc(), DQSYNTAX, "\n\n", 0); 2027222907Sjilles if (backquotelist != NULL) 2028222907Sjilles error("Command substitution not allowed here"); 2029222907Sjilles 2030222907Sjilles n.narg.type = NARG; 2031222907Sjilles n.narg.next = NULL; 2032222907Sjilles n.narg.text = wordtext; 2033222907Sjilles n.narg.backquote = backquotelist; 2034222907Sjilles 2035222907Sjilles expandarg(&n, NULL, 0); 2036222907Sjilles result = stackblock(); 2037222907Sjilles INTOFF; 2038222907Sjilles } 2039222907Sjilles handler = savehandler; 2040222907Sjilles doprompt = saveprompt; 2041222907Sjilles popfilesupto(savetopfile); 2042222907Sjilles if (parser_temp != saveparser_temp) { 2043222907Sjilles parser_temp_free_all(); 2044222907Sjilles parser_temp = saveparser_temp; 2045222907Sjilles } 2046222907Sjilles if (result != NULL) { 2047222907Sjilles INTON; 2048222907Sjilles } else if (exception == EXINT) 2049222907Sjilles raise(SIGINT); 2050222907Sjilles return result; 2051222907Sjilles} 2052