parser.c revision 221513
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 221513 2011-05-05 20:55:55Z 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) { 24317987Speter if (n2->type == NCMD || n2->type == NPIPE) { 24417987Speter n2->ncmd.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) { 5452760Ssef if (t != TENDCASE) 546214709Sjilles synexpect(TENDCASE); 5472760Ssef else 548214709Sjilles checkkwd = CHKNL | CHKKWD, readtoken(); 5492760Ssef } 5501556Srgrimes cpp = &cp->nclist.next; 551104202Stjr } 5521556Srgrimes *cpp = NULL; 553214709Sjilles checkkwd = CHKKWD | CHKALIAS; 5541556Srgrimes break; 5551556Srgrimes case TLP: 5561556Srgrimes n1 = (union node *)stalloc(sizeof (struct nredir)); 5571556Srgrimes n1->type = NSUBSHELL; 558214525Sjilles n1->nredir.n = list(0, 0); 5591556Srgrimes n1->nredir.redirect = NULL; 5601556Srgrimes if (readtoken() != TRP) 5611556Srgrimes synexpect(TRP); 562214709Sjilles checkkwd = CHKKWD | CHKALIAS; 563218325Sjilles is_subshell = 1; 5641556Srgrimes break; 5651556Srgrimes case TBEGIN: 566214525Sjilles n1 = list(0, 0); 5671556Srgrimes if (readtoken() != TEND) 5681556Srgrimes synexpect(TEND); 569214709Sjilles checkkwd = CHKKWD | CHKALIAS; 5701556Srgrimes break; 5711556Srgrimes /* Handle an empty command like other simple commands. */ 572210221Sjilles case TBACKGND: 57317987Speter case TSEMI: 574101662Stjr case TAND: 575101662Stjr case TOR: 57617987Speter /* 57717987Speter * An empty command before a ; doesn't make much sense, and 57817987Speter * should certainly be disallowed in the case of `if ;'. 57917987Speter */ 58017987Speter if (!redir) 58117987Speter synexpect(-1); 5821556Srgrimes case TNL: 58310399Sjoerg case TEOF: 5841556Srgrimes case TWORD: 58517987Speter case TRP: 5861556Srgrimes tokpushback++; 58775160Sbrian n1 = simplecmd(rpp, redir); 588214281Sjilles return n1; 5891556Srgrimes default: 5901556Srgrimes synexpect(-1); 5911556Srgrimes } 5921556Srgrimes 5931556Srgrimes /* Now check for redirection which may follow command */ 5941556Srgrimes while (readtoken() == TREDIR) { 5951556Srgrimes *rpp = n2 = redirnode; 5961556Srgrimes rpp = &n2->nfile.next; 5971556Srgrimes parsefname(); 5981556Srgrimes } 5991556Srgrimes tokpushback++; 6001556Srgrimes *rpp = NULL; 6011556Srgrimes if (redir) { 602218325Sjilles if (!is_subshell) { 6031556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 6041556Srgrimes n2->type = NREDIR; 6051556Srgrimes n2->nredir.n = n1; 6061556Srgrimes n1 = n2; 6071556Srgrimes } 6081556Srgrimes n1->nredir.redirect = redir; 6091556Srgrimes } 61075160Sbrian 611214281Sjilles return n1; 6121556Srgrimes} 6131556Srgrimes 6141556Srgrimes 615213811Sobrienstatic union node * 61690111Simpsimplecmd(union node **rpp, union node *redir) 61790111Simp{ 6181556Srgrimes union node *args, **app; 6191556Srgrimes union node **orig_rpp = rpp; 620210087Sjilles union node *n = NULL; 621214304Sjilles int special; 6221556Srgrimes 6231556Srgrimes /* If we don't have any redirections already, then we must reset */ 6241556Srgrimes /* rpp to be the address of the local redir variable. */ 6251556Srgrimes if (redir == 0) 6261556Srgrimes rpp = &redir; 6271556Srgrimes 6281556Srgrimes args = NULL; 6291556Srgrimes app = &args; 6308855Srgrimes /* 6311556Srgrimes * We save the incoming value, because we need this for shell 6321556Srgrimes * functions. There can not be a redirect or an argument between 6338855Srgrimes * the function name and the open parenthesis. 6341556Srgrimes */ 6351556Srgrimes orig_rpp = rpp; 6361556Srgrimes 6371556Srgrimes for (;;) { 6381556Srgrimes if (readtoken() == TWORD) { 6391556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 6401556Srgrimes n->type = NARG; 6411556Srgrimes n->narg.text = wordtext; 6421556Srgrimes n->narg.backquote = backquotelist; 6431556Srgrimes *app = n; 6441556Srgrimes app = &n->narg.next; 6451556Srgrimes } else if (lasttoken == TREDIR) { 6461556Srgrimes *rpp = n = redirnode; 6471556Srgrimes rpp = &n->nfile.next; 6481556Srgrimes parsefname(); /* read name of redirection file */ 6491556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 6501556Srgrimes && rpp == orig_rpp) { 6511556Srgrimes /* We have a function */ 6521556Srgrimes if (readtoken() != TRP) 6531556Srgrimes synexpect(TRP); 654179022Sstefanf funclinno = plinno; 655214291Sjilles /* 656214291Sjilles * - Require plain text. 657214291Sjilles * - Functions with '/' cannot be called. 658214534Sjilles * - Reject name=(). 659214534Sjilles * - Reject ksh extended glob patterns. 660214291Sjilles */ 661214291Sjilles if (!noexpand(n->narg.text) || quoteflag || 662214534Sjilles strchr(n->narg.text, '/') || 663214534Sjilles strchr("!%*+-=?@}~", 664214534Sjilles n->narg.text[strlen(n->narg.text) - 1])) 6651556Srgrimes synerror("Bad function name"); 666214291Sjilles rmescapes(n->narg.text); 667214304Sjilles if (find_builtin(n->narg.text, &special) >= 0 && 668214304Sjilles special) 669214304Sjilles synerror("Cannot override a special builtin with a function"); 6701556Srgrimes n->type = NDEFUN; 6711556Srgrimes n->narg.next = command(); 672179022Sstefanf funclinno = 0; 673210087Sjilles return n; 6741556Srgrimes } else { 6751556Srgrimes tokpushback++; 6761556Srgrimes break; 6771556Srgrimes } 6781556Srgrimes } 6791556Srgrimes *app = NULL; 6801556Srgrimes *rpp = NULL; 6811556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 6821556Srgrimes n->type = NCMD; 6831556Srgrimes n->ncmd.backgnd = 0; 6841556Srgrimes n->ncmd.args = args; 6851556Srgrimes n->ncmd.redirect = redir; 686210087Sjilles return n; 6871556Srgrimes} 6881556Srgrimes 689213811Sobrienstatic union node * 69090111Simpmakename(void) 69190111Simp{ 69217987Speter union node *n; 6931556Srgrimes 69417987Speter n = (union node *)stalloc(sizeof (struct narg)); 69517987Speter n->type = NARG; 69617987Speter n->narg.next = NULL; 69717987Speter n->narg.text = wordtext; 69817987Speter n->narg.backquote = backquotelist; 69917987Speter return n; 70017987Speter} 70117987Speter 702213760Sobrienvoid 703213760Sobrienfixredir(union node *n, const char *text, int err) 70490111Simp{ 70517987Speter TRACE(("Fix redir %s %d\n", text, err)); 70617987Speter if (!err) 70717987Speter n->ndup.vname = NULL; 70817987Speter 70917987Speter if (is_digit(text[0]) && text[1] == '\0') 71017987Speter n->ndup.dupfd = digit_val(text[0]); 71117987Speter else if (text[0] == '-' && text[1] == '\0') 71217987Speter n->ndup.dupfd = -1; 71317987Speter else { 71420425Ssteve 71517987Speter if (err) 71617987Speter synerror("Bad fd number"); 71717987Speter else 71817987Speter n->ndup.vname = makename(); 71917987Speter } 72017987Speter} 72117987Speter 72217987Speter 723213811Sobrienstatic void 72490111Simpparsefname(void) 72590111Simp{ 7261556Srgrimes union node *n = redirnode; 7271556Srgrimes 7281556Srgrimes if (readtoken() != TWORD) 7291556Srgrimes synexpect(-1); 7301556Srgrimes if (n->type == NHERE) { 7311556Srgrimes struct heredoc *here = heredoc; 7321556Srgrimes struct heredoc *p; 7331556Srgrimes int i; 7341556Srgrimes 7351556Srgrimes if (quoteflag == 0) 7361556Srgrimes n->type = NXHERE; 7371556Srgrimes TRACE(("Here document %d\n", n->type)); 7381556Srgrimes if (here->striptabs) { 7391556Srgrimes while (*wordtext == '\t') 7401556Srgrimes wordtext++; 7411556Srgrimes } 7421556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 7431556Srgrimes synerror("Illegal eof marker for << redirection"); 7441556Srgrimes rmescapes(wordtext); 7451556Srgrimes here->eofmark = wordtext; 7461556Srgrimes here->next = NULL; 7471556Srgrimes if (heredoclist == NULL) 7481556Srgrimes heredoclist = here; 7491556Srgrimes else { 7501556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 7511556Srgrimes p->next = here; 7521556Srgrimes } 7531556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 75417987Speter fixredir(n, wordtext, 0); 7551556Srgrimes } else { 75617987Speter n->nfile.fname = makename(); 7571556Srgrimes } 7581556Srgrimes} 7591556Srgrimes 7601556Srgrimes 7611556Srgrimes/* 7621556Srgrimes * Input any here documents. 7631556Srgrimes */ 7641556Srgrimes 765213811Sobrienstatic void 76690111Simpparseheredoc(void) 76790111Simp{ 7681556Srgrimes struct heredoc *here; 7691556Srgrimes union node *n; 7701556Srgrimes 7711556Srgrimes while (heredoclist) { 7721556Srgrimes here = heredoclist; 7731556Srgrimes heredoclist = here->next; 7741556Srgrimes if (needprompt) { 7751556Srgrimes setprompt(2); 7761556Srgrimes needprompt = 0; 7771556Srgrimes } 7781556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 7791556Srgrimes here->eofmark, here->striptabs); 7801556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 7811556Srgrimes n->narg.type = NARG; 7821556Srgrimes n->narg.next = NULL; 7831556Srgrimes n->narg.text = wordtext; 7841556Srgrimes n->narg.backquote = backquotelist; 7851556Srgrimes here->here->nhere.doc = n; 7861556Srgrimes } 7871556Srgrimes} 7881556Srgrimes 789213811Sobrienstatic int 79090111Simppeektoken(void) 79190111Simp{ 7921556Srgrimes int t; 7931556Srgrimes 7941556Srgrimes t = readtoken(); 7951556Srgrimes tokpushback++; 7961556Srgrimes return (t); 7971556Srgrimes} 7981556Srgrimes 799213811Sobrienstatic int 80090111Simpreadtoken(void) 80190111Simp{ 8021556Srgrimes int t; 8031556Srgrimes struct alias *ap; 8041556Srgrimes#ifdef DEBUG 8051556Srgrimes int alreadyseen = tokpushback; 8061556Srgrimes#endif 8078855Srgrimes 8081556Srgrimes top: 8091556Srgrimes t = xxreadtoken(); 8101556Srgrimes 811214709Sjilles /* 812214709Sjilles * eat newlines 813214709Sjilles */ 814214709Sjilles if (checkkwd & CHKNL) { 815214709Sjilles while (t == TNL) { 816214709Sjilles parseheredoc(); 817214709Sjilles t = xxreadtoken(); 818214709Sjilles } 819214709Sjilles } 8201556Srgrimes 821214709Sjilles /* 822214709Sjilles * check for keywords and aliases 823214709Sjilles */ 824214709Sjilles if (t == TWORD && !quoteflag) 825214709Sjilles { 826214709Sjilles const char * const *pp; 827214709Sjilles 828214709Sjilles if (checkkwd & CHKKWD) 82998463Sjmallett for (pp = parsekwd; *pp; pp++) { 83020425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 83117987Speter { 8321556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 8331556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 8341556Srgrimes goto out; 8351556Srgrimes } 8361556Srgrimes } 837214709Sjilles if (checkkwd & CHKALIAS && 838214709Sjilles (ap = lookupalias(wordtext, 1)) != NULL) { 839214709Sjilles pushstring(ap->val, strlen(ap->val), ap); 840214709Sjilles goto top; 8411556Srgrimes } 842214709Sjilles } 8431556Srgrimesout: 844214709Sjilles if (t != TNOT) 845214709Sjilles checkkwd = 0; 846214709Sjilles 8471556Srgrimes#ifdef DEBUG 8481556Srgrimes if (!alreadyseen) 8491556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8501556Srgrimes else 8511556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8521556Srgrimes#endif 8531556Srgrimes return (t); 8541556Srgrimes} 8551556Srgrimes 8561556Srgrimes 8571556Srgrimes/* 8581556Srgrimes * Read the next input token. 8591556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 8601556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 8611556Srgrimes * quoted. 8621556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 8631556Srgrimes * the redirection. 8641556Srgrimes * In all cases, the variable startlinno is set to the number of the line 8651556Srgrimes * on which the token starts. 8661556Srgrimes * 8671556Srgrimes * [Change comment: here documents and internal procedures] 8681556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 8691556Srgrimes * word parsing code into a separate routine. In this case, readtoken 8701556Srgrimes * doesn't need to have any internal procedures, but parseword does. 8711556Srgrimes * We could also make parseoperator in essence the main routine, and 8721556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 8731556Srgrimes */ 8741556Srgrimes 8751556Srgrimes#define RETURN(token) return lasttoken = token 8761556Srgrimes 877213811Sobrienstatic int 87890111Simpxxreadtoken(void) 87990111Simp{ 88025230Ssteve int c; 8811556Srgrimes 8821556Srgrimes if (tokpushback) { 8831556Srgrimes tokpushback = 0; 8841556Srgrimes return lasttoken; 8851556Srgrimes } 8861556Srgrimes if (needprompt) { 8871556Srgrimes setprompt(2); 8881556Srgrimes needprompt = 0; 8891556Srgrimes } 8901556Srgrimes startlinno = plinno; 8911556Srgrimes for (;;) { /* until token or start of word found */ 8921556Srgrimes c = pgetc_macro(); 8931556Srgrimes switch (c) { 8941556Srgrimes case ' ': case '\t': 8951556Srgrimes continue; 8961556Srgrimes case '#': 8971556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 8981556Srgrimes pungetc(); 8991556Srgrimes continue; 9001556Srgrimes case '\\': 9011556Srgrimes if (pgetc() == '\n') { 9021556Srgrimes startlinno = ++plinno; 9031556Srgrimes if (doprompt) 9041556Srgrimes setprompt(2); 9051556Srgrimes else 9061556Srgrimes setprompt(0); 9071556Srgrimes continue; 9081556Srgrimes } 9091556Srgrimes pungetc(); 9101556Srgrimes goto breakloop; 9111556Srgrimes case '\n': 9121556Srgrimes plinno++; 9131556Srgrimes needprompt = doprompt; 9141556Srgrimes RETURN(TNL); 9151556Srgrimes case PEOF: 9161556Srgrimes RETURN(TEOF); 9171556Srgrimes case '&': 9181556Srgrimes if (pgetc() == '&') 9191556Srgrimes RETURN(TAND); 9201556Srgrimes pungetc(); 9211556Srgrimes RETURN(TBACKGND); 9221556Srgrimes case '|': 9231556Srgrimes if (pgetc() == '|') 9241556Srgrimes RETURN(TOR); 9251556Srgrimes pungetc(); 9261556Srgrimes RETURN(TPIPE); 9271556Srgrimes case ';': 9281556Srgrimes if (pgetc() == ';') 9291556Srgrimes RETURN(TENDCASE); 9301556Srgrimes pungetc(); 9311556Srgrimes RETURN(TSEMI); 9321556Srgrimes case '(': 9331556Srgrimes RETURN(TLP); 9341556Srgrimes case ')': 9351556Srgrimes RETURN(TRP); 9361556Srgrimes default: 9371556Srgrimes goto breakloop; 9381556Srgrimes } 9391556Srgrimes } 9401556Srgrimesbreakloop: 9411556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 9421556Srgrimes#undef RETURN 9431556Srgrimes} 9441556Srgrimes 9451556Srgrimes 946213811Sobrien#define MAXNEST_static 8 947206145Sjillesstruct tokenstate 948206145Sjilles{ 949206145Sjilles const char *syntax; /* *SYNTAX */ 950206145Sjilles int parenlevel; /* levels of parentheses in arithmetic */ 951206145Sjilles enum tokenstate_category 952206145Sjilles { 953206145Sjilles TSTATE_TOP, 954206145Sjilles TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */ 955206145Sjilles TSTATE_VAR_NEW, /* other ${var...}, own dquote state */ 956206145Sjilles TSTATE_ARITH 957206145Sjilles } category; 958206145Sjilles}; 959206145Sjilles 960206145Sjilles 961205130Sjilles/* 962205130Sjilles * Called to parse command substitutions. 963205130Sjilles */ 9641556Srgrimes 965213811Sobrienstatic char * 966205130Sjillesparsebackq(char *out, struct nodelist **pbqlist, 967205130Sjilles int oldstyle, int dblquote, int quoted) 968205130Sjilles{ 969205130Sjilles struct nodelist **nlpp; 970205130Sjilles union node *n; 971205130Sjilles char *volatile str; 972205130Sjilles struct jmploc jmploc; 973205130Sjilles struct jmploc *const savehandler = handler; 974205130Sjilles int savelen; 975205130Sjilles int saveprompt; 976205130Sjilles const int bq_startlinno = plinno; 977205130Sjilles char *volatile ostr = NULL; 978205130Sjilles struct parsefile *const savetopfile = getcurrentfile(); 979208655Sjilles struct heredoc *const saveheredoclist = heredoclist; 980208655Sjilles struct heredoc *here; 981205130Sjilles 982205130Sjilles str = NULL; 983205130Sjilles if (setjmp(jmploc.loc)) { 984205130Sjilles popfilesupto(savetopfile); 985205130Sjilles if (str) 986205130Sjilles ckfree(str); 987205130Sjilles if (ostr) 988205130Sjilles ckfree(ostr); 989208655Sjilles heredoclist = saveheredoclist; 990205130Sjilles handler = savehandler; 991205130Sjilles if (exception == EXERROR) { 992205130Sjilles startlinno = bq_startlinno; 993205130Sjilles synerror("Error in command substitution"); 994205130Sjilles } 995205130Sjilles longjmp(handler->loc, 1); 996205130Sjilles } 997205130Sjilles INTOFF; 998205130Sjilles savelen = out - stackblock(); 999205130Sjilles if (savelen > 0) { 1000205130Sjilles str = ckmalloc(savelen); 1001205130Sjilles memcpy(str, stackblock(), savelen); 1002205130Sjilles } 1003205130Sjilles handler = &jmploc; 1004208655Sjilles heredoclist = NULL; 1005205130Sjilles INTON; 1006205130Sjilles if (oldstyle) { 1007205130Sjilles /* We must read until the closing backquote, giving special 1008205130Sjilles treatment to some slashes, and then push the string and 1009205130Sjilles reread it as input, interpreting it normally. */ 1010205130Sjilles char *oout; 1011205130Sjilles int c; 1012205130Sjilles int olen; 1013205130Sjilles 1014205130Sjilles 1015205130Sjilles STARTSTACKSTR(oout); 1016205130Sjilles for (;;) { 1017205130Sjilles if (needprompt) { 1018205130Sjilles setprompt(2); 1019205130Sjilles needprompt = 0; 1020205130Sjilles } 1021215783Sjilles CHECKSTRSPACE(2, oout); 1022205130Sjilles switch (c = pgetc()) { 1023205130Sjilles case '`': 1024205130Sjilles goto done; 1025205130Sjilles 1026205130Sjilles case '\\': 1027205130Sjilles if ((c = pgetc()) == '\n') { 1028205130Sjilles plinno++; 1029205130Sjilles if (doprompt) 1030205130Sjilles setprompt(2); 1031205130Sjilles else 1032205130Sjilles setprompt(0); 1033205130Sjilles /* 1034205130Sjilles * If eating a newline, avoid putting 1035205130Sjilles * the newline into the new character 1036215783Sjilles * stream (via the USTPUTC after the 1037205130Sjilles * switch). 1038205130Sjilles */ 1039205130Sjilles continue; 1040205130Sjilles } 1041205130Sjilles if (c != '\\' && c != '`' && c != '$' 1042205130Sjilles && (!dblquote || c != '"')) 1043215783Sjilles USTPUTC('\\', oout); 1044205130Sjilles break; 1045205130Sjilles 1046205130Sjilles case '\n': 1047205130Sjilles plinno++; 1048205130Sjilles needprompt = doprompt; 1049205130Sjilles break; 1050205130Sjilles 1051205130Sjilles case PEOF: 1052205130Sjilles startlinno = plinno; 1053205130Sjilles synerror("EOF in backquote substitution"); 1054205130Sjilles break; 1055205130Sjilles 1056205130Sjilles default: 1057205130Sjilles break; 1058205130Sjilles } 1059215783Sjilles USTPUTC(c, oout); 1060205130Sjilles } 1061205130Sjillesdone: 1062215783Sjilles USTPUTC('\0', oout); 1063205130Sjilles olen = oout - stackblock(); 1064205130Sjilles INTOFF; 1065205130Sjilles ostr = ckmalloc(olen); 1066205130Sjilles memcpy(ostr, stackblock(), olen); 1067205130Sjilles setinputstring(ostr, 1); 1068205130Sjilles INTON; 1069205130Sjilles } 1070205130Sjilles nlpp = pbqlist; 1071205130Sjilles while (*nlpp) 1072205130Sjilles nlpp = &(*nlpp)->next; 1073205130Sjilles *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 1074205130Sjilles (*nlpp)->next = NULL; 1075205130Sjilles 1076205130Sjilles if (oldstyle) { 1077205130Sjilles saveprompt = doprompt; 1078205130Sjilles doprompt = 0; 1079205130Sjilles } 1080205130Sjilles 1081214525Sjilles n = list(0, oldstyle); 1082205130Sjilles 1083205130Sjilles if (oldstyle) 1084205130Sjilles doprompt = saveprompt; 1085205130Sjilles else { 1086205130Sjilles if (readtoken() != TRP) 1087205130Sjilles synexpect(TRP); 1088205130Sjilles } 1089205130Sjilles 1090205130Sjilles (*nlpp)->n = n; 1091205130Sjilles if (oldstyle) { 1092205130Sjilles /* 1093205130Sjilles * Start reading from old file again, ignoring any pushed back 1094205130Sjilles * tokens left from the backquote parsing 1095205130Sjilles */ 1096205130Sjilles popfile(); 1097205130Sjilles tokpushback = 0; 1098205130Sjilles } 1099205130Sjilles STARTSTACKSTR(out); 1100216706Sjilles CHECKSTRSPACE(savelen + 1, out); 1101208655Sjilles INTOFF; 1102205130Sjilles if (str) { 1103205130Sjilles memcpy(out, str, savelen); 1104205130Sjilles STADJUST(savelen, out); 1105205130Sjilles ckfree(str); 1106205130Sjilles str = NULL; 1107205130Sjilles } 1108205130Sjilles if (ostr) { 1109205130Sjilles ckfree(ostr); 1110205130Sjilles ostr = NULL; 1111205130Sjilles } 1112208655Sjilles here = saveheredoclist; 1113208655Sjilles if (here != NULL) { 1114208655Sjilles while (here->next != NULL) 1115208655Sjilles here = here->next; 1116208655Sjilles here->next = heredoclist; 1117208655Sjilles heredoclist = saveheredoclist; 1118208655Sjilles } 1119205130Sjilles handler = savehandler; 1120208655Sjilles INTON; 1121205130Sjilles if (quoted) 1122205130Sjilles USTPUTC(CTLBACKQ | CTLQUOTE, out); 1123205130Sjilles else 1124205130Sjilles USTPUTC(CTLBACKQ, out); 1125205130Sjilles return out; 1126205130Sjilles} 1127205130Sjilles 1128205130Sjilles 11291556Srgrimes/* 1130221513Sjilles * Called to parse a backslash escape sequence inside $'...'. 1131221513Sjilles * The backslash has already been read. 1132221513Sjilles */ 1133221513Sjillesstatic char * 1134221513Sjillesreadcstyleesc(char *out) 1135221513Sjilles{ 1136221513Sjilles int c, v, i, n; 1137221513Sjilles 1138221513Sjilles c = pgetc(); 1139221513Sjilles switch (c) { 1140221513Sjilles case '\0': 1141221513Sjilles synerror("Unterminated quoted string"); 1142221513Sjilles case '\n': 1143221513Sjilles plinno++; 1144221513Sjilles if (doprompt) 1145221513Sjilles setprompt(2); 1146221513Sjilles else 1147221513Sjilles setprompt(0); 1148221513Sjilles return out; 1149221513Sjilles case '\\': 1150221513Sjilles case '\'': 1151221513Sjilles case '"': 1152221513Sjilles v = c; 1153221513Sjilles break; 1154221513Sjilles case 'a': v = '\a'; break; 1155221513Sjilles case 'b': v = '\b'; break; 1156221513Sjilles case 'e': v = '\033'; break; 1157221513Sjilles case 'f': v = '\f'; break; 1158221513Sjilles case 'n': v = '\n'; break; 1159221513Sjilles case 'r': v = '\r'; break; 1160221513Sjilles case 't': v = '\t'; break; 1161221513Sjilles case 'v': v = '\v'; break; 1162221513Sjilles case 'x': 1163221513Sjilles v = 0; 1164221513Sjilles for (;;) { 1165221513Sjilles c = pgetc(); 1166221513Sjilles if (c >= '0' && c <= '9') 1167221513Sjilles v = (v << 4) + c - '0'; 1168221513Sjilles else if (c >= 'A' && c <= 'F') 1169221513Sjilles v = (v << 4) + c - 'A' + 10; 1170221513Sjilles else if (c >= 'a' && c <= 'f') 1171221513Sjilles v = (v << 4) + c - 'a' + 10; 1172221513Sjilles else 1173221513Sjilles break; 1174221513Sjilles } 1175221513Sjilles pungetc(); 1176221513Sjilles break; 1177221513Sjilles case '0': case '1': case '2': case '3': 1178221513Sjilles case '4': case '5': case '6': case '7': 1179221513Sjilles v = c - '0'; 1180221513Sjilles c = pgetc(); 1181221513Sjilles if (c >= '0' && c <= '7') { 1182221513Sjilles v <<= 3; 1183221513Sjilles v += c - '0'; 1184221513Sjilles c = pgetc(); 1185221513Sjilles if (c >= '0' && c <= '7') { 1186221513Sjilles v <<= 3; 1187221513Sjilles v += c - '0'; 1188221513Sjilles } else 1189221513Sjilles pungetc(); 1190221513Sjilles } else 1191221513Sjilles pungetc(); 1192221513Sjilles break; 1193221513Sjilles case 'c': 1194221513Sjilles c = pgetc(); 1195221513Sjilles if (c < 0x3f || c > 0x7a || c == 0x60) 1196221513Sjilles synerror("Bad escape sequence"); 1197221513Sjilles if (c == '\\' && pgetc() != '\\') 1198221513Sjilles synerror("Bad escape sequence"); 1199221513Sjilles if (c == '?') 1200221513Sjilles v = 127; 1201221513Sjilles else 1202221513Sjilles v = c & 0x1f; 1203221513Sjilles break; 1204221513Sjilles case 'u': 1205221513Sjilles case 'U': 1206221513Sjilles n = c == 'U' ? 8 : 4; 1207221513Sjilles v = 0; 1208221513Sjilles for (i = 0; i < n; i++) { 1209221513Sjilles c = pgetc(); 1210221513Sjilles if (c >= '0' && c <= '9') 1211221513Sjilles v = (v << 4) + c - '0'; 1212221513Sjilles else if (c >= 'A' && c <= 'F') 1213221513Sjilles v = (v << 4) + c - 'A' + 10; 1214221513Sjilles else if (c >= 'a' && c <= 'f') 1215221513Sjilles v = (v << 4) + c - 'a' + 10; 1216221513Sjilles else 1217221513Sjilles synerror("Bad escape sequence"); 1218221513Sjilles } 1219221513Sjilles if (v == 0 || (v >= 0xd800 && v <= 0xdfff)) 1220221513Sjilles synerror("Bad escape sequence"); 1221221513Sjilles /* We really need iconv here. */ 1222221513Sjilles if (v > 127) 1223221513Sjilles v = '?'; 1224221513Sjilles break; 1225221513Sjilles default: 1226221513Sjilles synerror("Bad escape sequence"); 1227221513Sjilles } 1228221513Sjilles v = (char)v; 1229221513Sjilles /* 1230221513Sjilles * We can't handle NUL bytes. 1231221513Sjilles * POSIX says we should skip till the closing quote. 1232221513Sjilles */ 1233221513Sjilles if (v == '\0') { 1234221513Sjilles while ((c = pgetc()) != '\'') { 1235221513Sjilles if (c == '\\') 1236221513Sjilles c = pgetc(); 1237221513Sjilles if (c == PEOF) 1238221513Sjilles synerror("Unterminated quoted string"); 1239221513Sjilles } 1240221513Sjilles pungetc(); 1241221513Sjilles return out; 1242221513Sjilles } 1243221513Sjilles if (SQSYNTAX[v] == CCTL) 1244221513Sjilles USTPUTC(CTLESC, out); 1245221513Sjilles USTPUTC(v, out); 1246221513Sjilles return out; 1247221513Sjilles} 1248221513Sjilles 1249221513Sjilles 1250221513Sjilles/* 12511556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 12521556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 12531556Srgrimes * word which marks the end of the document and striptabs is true if 12541556Srgrimes * leading tabs should be stripped from the document. The argument firstc 12551556Srgrimes * is the first character of the input token or document. 12561556Srgrimes * 12571556Srgrimes * Because C does not have internal subroutines, I have simulated them 12581556Srgrimes * using goto's to implement the subroutine linkage. The following macros 12591556Srgrimes * will run code that appears at the end of readtoken1. 12601556Srgrimes */ 12611556Srgrimes 12621556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 12631556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 12641556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 12651556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 12661556Srgrimes 1267213811Sobrienstatic int 1268206145Sjillesreadtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs) 126990111Simp{ 127017987Speter int c = firstc; 127117987Speter char *out; 12721556Srgrimes int len; 12731556Srgrimes char line[EOFMARKLEN + 1]; 12741556Srgrimes struct nodelist *bqlist; 12751556Srgrimes int quotef; 1276206145Sjilles int newvarnest; 1277206145Sjilles int level; 127854679Scracauer int synentry; 1279213811Sobrien struct tokenstate state_static[MAXNEST_static]; 1280213811Sobrien int maxnest = MAXNEST_static; 1281206145Sjilles struct tokenstate *state = state_static; 1282221513Sjilles int sqiscstyle = 0; 12831556Srgrimes 12841556Srgrimes startlinno = plinno; 12851556Srgrimes quotef = 0; 12861556Srgrimes bqlist = NULL; 1287206145Sjilles newvarnest = 0; 1288206145Sjilles level = 0; 1289206145Sjilles state[level].syntax = initialsyntax; 1290206145Sjilles state[level].parenlevel = 0; 1291206145Sjilles state[level].category = TSTATE_TOP; 12921556Srgrimes 12931556Srgrimes STARTSTACKSTR(out); 12941556Srgrimes loop: { /* for each line, until end of word */ 12951556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 12961556Srgrimes for (;;) { /* until end of line or end of word */ 1297214512Sjilles CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ 129854679Scracauer 1299206145Sjilles synentry = state[level].syntax[c]; 130054679Scracauer 130154679Scracauer switch(synentry) { 13021556Srgrimes case CNL: /* '\n' */ 1303206145Sjilles if (state[level].syntax == BASESYNTAX) 13041556Srgrimes goto endword; /* exit outer loop */ 13051556Srgrimes USTPUTC(c, out); 13061556Srgrimes plinno++; 13071556Srgrimes if (doprompt) 13081556Srgrimes setprompt(2); 13091556Srgrimes else 13101556Srgrimes setprompt(0); 13111556Srgrimes c = pgetc(); 13121556Srgrimes goto loop; /* continue outer loop */ 1313221513Sjilles case CSBACK: 1314221513Sjilles if (sqiscstyle) { 1315221513Sjilles out = readcstyleesc(out); 1316221513Sjilles break; 1317221513Sjilles } 1318221513Sjilles /* FALLTHROUGH */ 13191556Srgrimes case CWORD: 13201556Srgrimes USTPUTC(c, out); 13211556Srgrimes break; 13221556Srgrimes case CCTL: 1323206145Sjilles if (eofmark == NULL || initialsyntax != SQSYNTAX) 13241556Srgrimes USTPUTC(CTLESC, out); 13251556Srgrimes USTPUTC(c, out); 13261556Srgrimes break; 13271556Srgrimes case CBACK: /* backslash */ 13281556Srgrimes c = pgetc(); 13291556Srgrimes if (c == PEOF) { 13301556Srgrimes USTPUTC('\\', out); 13311556Srgrimes pungetc(); 13321556Srgrimes } else if (c == '\n') { 1333160849Syar plinno++; 13341556Srgrimes if (doprompt) 13351556Srgrimes setprompt(2); 13361556Srgrimes else 13371556Srgrimes setprompt(0); 13381556Srgrimes } else { 1339206145Sjilles if (state[level].syntax == DQSYNTAX && 1340206145Sjilles c != '\\' && c != '`' && c != '$' && 1341206145Sjilles (c != '"' || (eofmark != NULL && 1342206145Sjilles newvarnest == 0)) && 1343206145Sjilles (c != '}' || state[level].category != TSTATE_VAR_OLD)) 13441556Srgrimes USTPUTC('\\', out); 1345214512Sjilles if ((eofmark == NULL || 1346214512Sjilles newvarnest > 0) && 1347214512Sjilles state[level].syntax == BASESYNTAX) 1348214512Sjilles USTPUTC(CTLQUOTEMARK, out); 134983675Stegge if (SQSYNTAX[c] == CCTL) 13501556Srgrimes USTPUTC(CTLESC, out); 13511556Srgrimes USTPUTC(c, out); 1352214512Sjilles if ((eofmark == NULL || 1353214512Sjilles newvarnest > 0) && 1354214512Sjilles state[level].syntax == BASESYNTAX && 1355214512Sjilles state[level].category == TSTATE_VAR_OLD) 1356214512Sjilles USTPUTC(CTLQUOTEEND, out); 13571556Srgrimes quotef++; 13581556Srgrimes } 13591556Srgrimes break; 13601556Srgrimes case CSQUOTE: 1361206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1362206145Sjilles state[level].syntax = SQSYNTAX; 1363221513Sjilles sqiscstyle = 0; 13641556Srgrimes break; 13651556Srgrimes case CDQUOTE: 1366206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1367206145Sjilles state[level].syntax = DQSYNTAX; 13681556Srgrimes break; 13691556Srgrimes case CENDQUOTE: 1370206145Sjilles if (eofmark != NULL && newvarnest == 0) 13711556Srgrimes USTPUTC(c, out); 1372206145Sjilles else { 1373214512Sjilles if (state[level].category == TSTATE_VAR_OLD) 1374214512Sjilles USTPUTC(CTLQUOTEEND, out); 1375214305Sjilles state[level].syntax = BASESYNTAX; 13761556Srgrimes quotef++; 13771556Srgrimes } 13781556Srgrimes break; 13791556Srgrimes case CVAR: /* '$' */ 13801556Srgrimes PARSESUB(); /* parse substitution */ 13811556Srgrimes break; 13821556Srgrimes case CENDVAR: /* '}' */ 1383206145Sjilles if (level > 0 && 1384214492Sjilles ((state[level].category == TSTATE_VAR_OLD && 1385214492Sjilles state[level].syntax == 1386214492Sjilles state[level - 1].syntax) || 1387214490Sjilles (state[level].category == TSTATE_VAR_NEW && 1388214490Sjilles state[level].syntax == BASESYNTAX))) { 1389214492Sjilles if (state[level].category == TSTATE_VAR_NEW) 1390206145Sjilles newvarnest--; 1391206145Sjilles level--; 13921556Srgrimes USTPUTC(CTLENDVAR, out); 13931556Srgrimes } else { 13941556Srgrimes USTPUTC(c, out); 13951556Srgrimes } 13961556Srgrimes break; 13971556Srgrimes case CLP: /* '(' in arithmetic */ 1398206145Sjilles state[level].parenlevel++; 13991556Srgrimes USTPUTC(c, out); 14001556Srgrimes break; 14011556Srgrimes case CRP: /* ')' in arithmetic */ 1402206145Sjilles if (state[level].parenlevel > 0) { 14031556Srgrimes USTPUTC(c, out); 1404206145Sjilles --state[level].parenlevel; 14051556Srgrimes } else { 14061556Srgrimes if (pgetc() == ')') { 1407206145Sjilles if (level > 0 && 1408206145Sjilles state[level].category == TSTATE_ARITH) { 1409206145Sjilles level--; 14101556Srgrimes USTPUTC(CTLENDARI, out); 14111556Srgrimes } else 14121556Srgrimes USTPUTC(')', out); 14131556Srgrimes } else { 14148855Srgrimes /* 14151556Srgrimes * unbalanced parens 14161556Srgrimes * (don't 2nd guess - no error) 14171556Srgrimes */ 14181556Srgrimes pungetc(); 14191556Srgrimes USTPUTC(')', out); 14201556Srgrimes } 14211556Srgrimes } 14221556Srgrimes break; 14231556Srgrimes case CBQUOTE: /* '`' */ 1424206145Sjilles out = parsebackq(out, &bqlist, 1, 1425206145Sjilles state[level].syntax == DQSYNTAX && 1426206145Sjilles (eofmark == NULL || newvarnest > 0), 1427206145Sjilles state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX); 14281556Srgrimes break; 14291556Srgrimes case CEOF: 14301556Srgrimes goto endword; /* exit outer loop */ 1431214305Sjilles case CIGN: 1432214305Sjilles break; 14331556Srgrimes default: 1434206145Sjilles if (level == 0) 14351556Srgrimes goto endword; /* exit outer loop */ 14361556Srgrimes USTPUTC(c, out); 14371556Srgrimes } 14381556Srgrimes c = pgetc_macro(); 14391556Srgrimes } 14401556Srgrimes } 14411556Srgrimesendword: 1442206145Sjilles if (state[level].syntax == ARISYNTAX) 14431556Srgrimes synerror("Missing '))'"); 1444206145Sjilles if (state[level].syntax != BASESYNTAX && eofmark == NULL) 14451556Srgrimes synerror("Unterminated quoted string"); 1446206145Sjilles if (state[level].category == TSTATE_VAR_OLD || 1447206145Sjilles state[level].category == TSTATE_VAR_NEW) { 14481556Srgrimes startlinno = plinno; 14491556Srgrimes synerror("Missing '}'"); 14501556Srgrimes } 1451206145Sjilles if (state != state_static) 1452206145Sjilles parser_temp_free_upto(state); 14531556Srgrimes USTPUTC('\0', out); 14541556Srgrimes len = out - stackblock(); 14551556Srgrimes out = stackblock(); 14561556Srgrimes if (eofmark == NULL) { 14571556Srgrimes if ((c == '>' || c == '<') 14581556Srgrimes && quotef == 0 14591556Srgrimes && len <= 2 14601556Srgrimes && (*out == '\0' || is_digit(*out))) { 14611556Srgrimes PARSEREDIR(); 14621556Srgrimes return lasttoken = TREDIR; 14631556Srgrimes } else { 14641556Srgrimes pungetc(); 14651556Srgrimes } 14661556Srgrimes } 14671556Srgrimes quoteflag = quotef; 14681556Srgrimes backquotelist = bqlist; 14691556Srgrimes grabstackblock(len); 14701556Srgrimes wordtext = out; 14711556Srgrimes return lasttoken = TWORD; 14721556Srgrimes/* end of readtoken routine */ 14731556Srgrimes 14741556Srgrimes 14751556Srgrimes/* 14761556Srgrimes * Check to see whether we are at the end of the here document. When this 14771556Srgrimes * is called, c is set to the first character of the next input line. If 14781556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 14791556Srgrimes */ 14801556Srgrimes 14811556Srgrimescheckend: { 14821556Srgrimes if (eofmark) { 14831556Srgrimes if (striptabs) { 14841556Srgrimes while (c == '\t') 14851556Srgrimes c = pgetc(); 14861556Srgrimes } 14871556Srgrimes if (c == *eofmark) { 14881556Srgrimes if (pfgets(line, sizeof line) != NULL) { 148925230Ssteve char *p, *q; 14901556Srgrimes 14911556Srgrimes p = line; 14921556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 14931556Srgrimes if (*p == '\n' && *q == '\0') { 14941556Srgrimes c = PEOF; 14951556Srgrimes plinno++; 14961556Srgrimes needprompt = doprompt; 14971556Srgrimes } else { 14981556Srgrimes pushstring(line, strlen(line), NULL); 14991556Srgrimes } 15001556Srgrimes } 15011556Srgrimes } 15021556Srgrimes } 15031556Srgrimes goto checkend_return; 15041556Srgrimes} 15051556Srgrimes 15061556Srgrimes 15071556Srgrimes/* 15081556Srgrimes * Parse a redirection operator. The variable "out" points to a string 15091556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 15101556Srgrimes * first character of the redirection operator. 15111556Srgrimes */ 15121556Srgrimes 15131556Srgrimesparseredir: { 15141556Srgrimes char fd = *out; 15151556Srgrimes union node *np; 15161556Srgrimes 15171556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 15181556Srgrimes if (c == '>') { 15191556Srgrimes np->nfile.fd = 1; 15201556Srgrimes c = pgetc(); 15211556Srgrimes if (c == '>') 15221556Srgrimes np->type = NAPPEND; 15231556Srgrimes else if (c == '&') 15241556Srgrimes np->type = NTOFD; 152596922Stjr else if (c == '|') 152696922Stjr np->type = NCLOBBER; 15271556Srgrimes else { 15281556Srgrimes np->type = NTO; 15291556Srgrimes pungetc(); 15301556Srgrimes } 15311556Srgrimes } else { /* c == '<' */ 15321556Srgrimes np->nfile.fd = 0; 15331556Srgrimes c = pgetc(); 15341556Srgrimes if (c == '<') { 15351556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 15361556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 15371556Srgrimes np->nfile.fd = 0; 15381556Srgrimes } 15391556Srgrimes np->type = NHERE; 15401556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 15411556Srgrimes heredoc->here = np; 15421556Srgrimes if ((c = pgetc()) == '-') { 15431556Srgrimes heredoc->striptabs = 1; 15441556Srgrimes } else { 15451556Srgrimes heredoc->striptabs = 0; 15461556Srgrimes pungetc(); 15471556Srgrimes } 15481556Srgrimes } else if (c == '&') 15491556Srgrimes np->type = NFROMFD; 155066612Sbrian else if (c == '>') 155166612Sbrian np->type = NFROMTO; 15521556Srgrimes else { 15531556Srgrimes np->type = NFROM; 15541556Srgrimes pungetc(); 15551556Srgrimes } 15561556Srgrimes } 15571556Srgrimes if (fd != '\0') 15581556Srgrimes np->nfile.fd = digit_val(fd); 15591556Srgrimes redirnode = np; 15601556Srgrimes goto parseredir_return; 15611556Srgrimes} 15621556Srgrimes 15631556Srgrimes 15641556Srgrimes/* 15651556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 15661556Srgrimes * and nothing else. 15671556Srgrimes */ 15681556Srgrimes 15691556Srgrimesparsesub: { 1570179022Sstefanf char buf[10]; 15711556Srgrimes int subtype; 15721556Srgrimes int typeloc; 15731556Srgrimes int flags; 15741556Srgrimes char *p; 15751556Srgrimes static const char types[] = "}-+?="; 1576179022Sstefanf int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 1577179022Sstefanf int linno; 1578179387Sstefanf int length; 1579219623Sjilles int c1; 15801556Srgrimes 15811556Srgrimes c = pgetc(); 1582221513Sjilles if (c == '(') { /* $(command) or $((arith)) */ 15831556Srgrimes if (pgetc() == '(') { 15841556Srgrimes PARSEARITH(); 15851556Srgrimes } else { 15861556Srgrimes pungetc(); 1587206145Sjilles out = parsebackq(out, &bqlist, 0, 1588206145Sjilles state[level].syntax == DQSYNTAX && 1589206145Sjilles (eofmark == NULL || newvarnest > 0), 1590206145Sjilles state[level].syntax == DQSYNTAX || 1591206145Sjilles state[level].syntax == ARISYNTAX); 15921556Srgrimes } 1593221513Sjilles } else if (c == '{' || is_name(c) || is_special(c)) { 15941556Srgrimes USTPUTC(CTLVAR, out); 15951556Srgrimes typeloc = out - stackblock(); 15961556Srgrimes USTPUTC(VSNORMAL, out); 15971556Srgrimes subtype = VSNORMAL; 1598179022Sstefanf flags = 0; 15991556Srgrimes if (c == '{') { 160018202Speter bracketed_name = 1; 16011556Srgrimes c = pgetc(); 1602219623Sjilles subtype = 0; 16031556Srgrimes } 1604219623Sjillesvarname: 1605149026Sstefanf if (!is_eof(c) && is_name(c)) { 1606179387Sstefanf length = 0; 16071556Srgrimes do { 16081556Srgrimes STPUTC(c, out); 16091556Srgrimes c = pgetc(); 1610179387Sstefanf length++; 1611149026Sstefanf } while (!is_eof(c) && is_in_name(c)); 1612179387Sstefanf if (length == 6 && 1613179387Sstefanf strncmp(out - length, "LINENO", length) == 0) { 1614179022Sstefanf /* Replace the variable name with the 1615179022Sstefanf * current line number. */ 1616179022Sstefanf linno = plinno; 1617179022Sstefanf if (funclinno != 0) 1618179022Sstefanf linno -= funclinno - 1; 1619179022Sstefanf snprintf(buf, sizeof(buf), "%d", linno); 1620179022Sstefanf STADJUST(-6, out); 1621215783Sjilles STPUTS(buf, out); 1622179022Sstefanf flags |= VSLINENO; 1623179022Sstefanf } 162418202Speter } else if (is_digit(c)) { 162518202Speter if (bracketed_name) { 162618202Speter do { 162718202Speter STPUTC(c, out); 162818202Speter c = pgetc(); 162918202Speter } while (is_digit(c)); 163018202Speter } else { 163118202Speter STPUTC(c, out); 163218202Speter c = pgetc(); 163318202Speter } 1634219623Sjilles } else if (is_special(c)) { 1635219623Sjilles c1 = c; 1636219623Sjilles c = pgetc(); 1637219623Sjilles if (subtype == 0 && c1 == '#') { 1638219623Sjilles subtype = VSLENGTH; 1639219623Sjilles if (strchr(types, c) == NULL && c != ':' && 1640219623Sjilles c != '#' && c != '%') 1641219623Sjilles goto varname; 1642219623Sjilles c1 = c; 1643219623Sjilles c = pgetc(); 1644219623Sjilles if (c1 != '}' && c == '}') { 1645219623Sjilles pungetc(); 1646219623Sjilles c = c1; 1647219623Sjilles goto varname; 1648219623Sjilles } 1649219623Sjilles pungetc(); 1650219623Sjilles c = c1; 1651219623Sjilles c1 = '#'; 1652219623Sjilles subtype = 0; 1653219623Sjilles } 1654219623Sjilles USTPUTC(c1, out); 16551556Srgrimes } else { 1656219623Sjilles subtype = VSERROR; 1657219623Sjilles if (c == '}') 1658219623Sjilles pungetc(); 1659219623Sjilles else if (c == '\n' || c == PEOF) 1660219623Sjilles synerror("Unexpected end of line in substitution"); 1661219623Sjilles else 1662164003Sstefanf USTPUTC(c, out); 16631556Srgrimes } 16641556Srgrimes if (subtype == 0) { 166517987Speter switch (c) { 166617987Speter case ':': 1667179022Sstefanf flags |= VSNUL; 16681556Srgrimes c = pgetc(); 166917987Speter /*FALLTHROUGH*/ 167017987Speter default: 167117987Speter p = strchr(types, c); 1672164003Sstefanf if (p == NULL) { 1673206144Sjilles if (c == '\n' || c == PEOF) 1674206144Sjilles synerror("Unexpected end of line in substitution"); 1675164003Sstefanf if (flags == VSNUL) 1676164003Sstefanf STPUTC(':', out); 1677164003Sstefanf STPUTC(c, out); 1678164003Sstefanf subtype = VSERROR; 1679164003Sstefanf } else 1680164003Sstefanf subtype = p - types + VSNORMAL; 168117987Speter break; 168217987Speter case '%': 168320425Ssteve case '#': 168417987Speter { 168517987Speter int cc = c; 168617987Speter subtype = c == '#' ? VSTRIMLEFT : 168717987Speter VSTRIMRIGHT; 168817987Speter c = pgetc(); 168917987Speter if (c == cc) 169017987Speter subtype++; 169117987Speter else 169217987Speter pungetc(); 169317987Speter break; 169417987Speter } 16951556Srgrimes } 1696164003Sstefanf } else if (subtype != VSERROR) { 1697221461Sjilles if (subtype == VSLENGTH && c != '}') 1698221461Sjilles subtype = VSERROR; 16991556Srgrimes pungetc(); 17001556Srgrimes } 1701164003Sstefanf STPUTC('=', out); 1702220903Sjilles if (state[level].syntax == DQSYNTAX || 1703220903Sjilles state[level].syntax == ARISYNTAX) 17041556Srgrimes flags |= VSQUOTE; 17051556Srgrimes *(stackblock() + typeloc) = subtype | flags; 1706206145Sjilles if (subtype != VSNORMAL) { 1707206145Sjilles if (level + 1 >= maxnest) { 1708206145Sjilles maxnest *= 2; 1709206145Sjilles if (state == state_static) { 1710206145Sjilles state = parser_temp_alloc( 1711206145Sjilles maxnest * sizeof(*state)); 1712206145Sjilles memcpy(state, state_static, 1713213811Sobrien MAXNEST_static * sizeof(*state)); 1714206145Sjilles } else 1715206145Sjilles state = parser_temp_realloc(state, 1716206145Sjilles maxnest * sizeof(*state)); 1717206145Sjilles } 1718206145Sjilles level++; 1719206145Sjilles state[level].parenlevel = 0; 1720206145Sjilles if (subtype == VSMINUS || subtype == VSPLUS || 1721206145Sjilles subtype == VSQUESTION || subtype == VSASSIGN) { 1722206145Sjilles /* 1723206145Sjilles * For operators that were in the Bourne shell, 1724206145Sjilles * inherit the double-quote state. 1725206145Sjilles */ 1726206145Sjilles state[level].syntax = state[level - 1].syntax; 1727206145Sjilles state[level].category = TSTATE_VAR_OLD; 1728206145Sjilles } else { 1729206145Sjilles /* 1730206145Sjilles * The other operators take a pattern, 1731206145Sjilles * so go to BASESYNTAX. 1732206145Sjilles * Also, ' and " are now special, even 1733206145Sjilles * in here documents. 1734206145Sjilles */ 1735206145Sjilles state[level].syntax = BASESYNTAX; 1736206145Sjilles state[level].category = TSTATE_VAR_NEW; 1737206145Sjilles newvarnest++; 1738206145Sjilles } 1739206145Sjilles } 1740221513Sjilles } else if (c == '\'' && state[level].syntax == BASESYNTAX) { 1741221513Sjilles /* $'cstylequotes' */ 1742221513Sjilles USTPUTC(CTLQUOTEMARK, out); 1743221513Sjilles state[level].syntax = SQSYNTAX; 1744221513Sjilles sqiscstyle = 1; 1745221513Sjilles } else { 1746221513Sjilles USTPUTC('$', out); 1747221513Sjilles pungetc(); 17481556Srgrimes } 17491556Srgrimes goto parsesub_return; 17501556Srgrimes} 17511556Srgrimes 17521556Srgrimes 17531556Srgrimes/* 17541556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 17551556Srgrimes */ 17561556Srgrimesparsearith: { 17571556Srgrimes 1758206145Sjilles if (level + 1 >= maxnest) { 1759206145Sjilles maxnest *= 2; 1760206145Sjilles if (state == state_static) { 1761206145Sjilles state = parser_temp_alloc( 1762206145Sjilles maxnest * sizeof(*state)); 1763206145Sjilles memcpy(state, state_static, 1764213811Sobrien MAXNEST_static * sizeof(*state)); 1765206145Sjilles } else 1766206145Sjilles state = parser_temp_realloc(state, 1767206145Sjilles maxnest * sizeof(*state)); 17681556Srgrimes } 1769206145Sjilles level++; 1770206145Sjilles state[level].syntax = ARISYNTAX; 1771206145Sjilles state[level].parenlevel = 0; 1772206145Sjilles state[level].category = TSTATE_ARITH; 1773206145Sjilles USTPUTC(CTLARI, out); 1774206145Sjilles if (state[level - 1].syntax == DQSYNTAX) 1775206145Sjilles USTPUTC('"',out); 1776206145Sjilles else 1777206145Sjilles USTPUTC(' ',out); 17781556Srgrimes goto parsearith_return; 17791556Srgrimes} 17801556Srgrimes 17811556Srgrimes} /* end of readtoken */ 17821556Srgrimes 17831556Srgrimes 17841556Srgrimes 17851556Srgrimes#ifdef mkinit 17861556SrgrimesRESET { 17871556Srgrimes tokpushback = 0; 17881556Srgrimes checkkwd = 0; 17891556Srgrimes} 17901556Srgrimes#endif 17911556Srgrimes 17921556Srgrimes/* 17931556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 17941556Srgrimes * or backquotes). 17951556Srgrimes */ 17961556Srgrimes 1797213811Sobrienstatic int 179890111Simpnoexpand(char *text) 179990111Simp{ 180025230Ssteve char *p; 180125230Ssteve char c; 18021556Srgrimes 18031556Srgrimes p = text; 18041556Srgrimes while ((c = *p++) != '\0') { 180539137Stegge if ( c == CTLQUOTEMARK) 180639137Stegge continue; 18071556Srgrimes if (c == CTLESC) 18081556Srgrimes p++; 180983675Stegge else if (BASESYNTAX[(int)c] == CCTL) 18101556Srgrimes return 0; 18111556Srgrimes } 18121556Srgrimes return 1; 18131556Srgrimes} 18141556Srgrimes 18151556Srgrimes 18161556Srgrimes/* 18171556Srgrimes * Return true if the argument is a legal variable name (a letter or 18181556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 18191556Srgrimes */ 18201556Srgrimes 18211556Srgrimesint 1822200956Sjillesgoodname(const char *name) 182390111Simp{ 1824200956Sjilles const char *p; 18251556Srgrimes 18261556Srgrimes p = name; 18271556Srgrimes if (! is_name(*p)) 18281556Srgrimes return 0; 18291556Srgrimes while (*++p) { 18301556Srgrimes if (! is_in_name(*p)) 18311556Srgrimes return 0; 18321556Srgrimes } 18331556Srgrimes return 1; 18341556Srgrimes} 18351556Srgrimes 18361556Srgrimes 18371556Srgrimes/* 18381556Srgrimes * Called when an unexpected token is read during the parse. The argument 18391556Srgrimes * is the token that is expected, or -1 if more than one type of token can 18401556Srgrimes * occur at this point. 18411556Srgrimes */ 18421556Srgrimes 1843213811Sobrienstatic void 184490111Simpsynexpect(int token) 184517987Speter{ 18461556Srgrimes char msg[64]; 18471556Srgrimes 18481556Srgrimes if (token >= 0) { 18491556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 18501556Srgrimes tokname[lasttoken], tokname[token]); 18511556Srgrimes } else { 18521556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 18531556Srgrimes } 18541556Srgrimes synerror(msg); 18551556Srgrimes} 18561556Srgrimes 18571556Srgrimes 1858213811Sobrienstatic void 1859201053Sjillessynerror(const char *msg) 186090111Simp{ 18611556Srgrimes if (commandname) 1862201366Sjilles outfmt(out2, "%s: %d: ", commandname, startlinno); 1863201366Sjilles outfmt(out2, "Syntax error: %s\n", msg); 18641556Srgrimes error((char *)NULL); 18651556Srgrimes} 18661556Srgrimes 1867213811Sobrienstatic void 186890111Simpsetprompt(int which) 186990111Simp{ 18701556Srgrimes whichprompt = which; 18711556Srgrimes 187217987Speter#ifndef NO_HISTORY 18731556Srgrimes if (!el) 187417987Speter#endif 1875199629Sjilles { 18761556Srgrimes out2str(getprompt(NULL)); 1877199629Sjilles flushout(out2); 1878199629Sjilles } 18791556Srgrimes} 18801556Srgrimes 18811556Srgrimes/* 18821556Srgrimes * called by editline -- any expansions to the prompt 18831556Srgrimes * should be added here. 18841556Srgrimes */ 18851556Srgrimeschar * 188690111Simpgetprompt(void *unused __unused) 188725905Ssteve{ 1888142845Sobrien static char ps[PROMPTLEN]; 1889142845Sobrien char *fmt; 1890209653Sjilles const char *pwd; 1891209653Sjilles int i, trim; 1892214538Sjilles static char internal_error[] = "??"; 1893142845Sobrien 1894142845Sobrien /* 1895142845Sobrien * Select prompt format. 1896142845Sobrien */ 18971556Srgrimes switch (whichprompt) { 18981556Srgrimes case 0: 1899201053Sjilles fmt = nullstr; 1900142845Sobrien break; 19011556Srgrimes case 1: 1902142845Sobrien fmt = ps1val(); 1903142845Sobrien break; 19041556Srgrimes case 2: 1905142845Sobrien fmt = ps2val(); 1906142845Sobrien break; 19071556Srgrimes default: 1908201053Sjilles return internal_error; 19091556Srgrimes } 1910142845Sobrien 1911142845Sobrien /* 1912142845Sobrien * Format prompt string. 1913142845Sobrien */ 1914142845Sobrien for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1915142845Sobrien if (*fmt == '\\') 1916142845Sobrien switch (*++fmt) { 1917142845Sobrien 1918142845Sobrien /* 1919142845Sobrien * Hostname. 1920142845Sobrien * 1921142845Sobrien * \h specifies just the local hostname, 1922142845Sobrien * \H specifies fully-qualified hostname. 1923142845Sobrien */ 1924142845Sobrien case 'h': 1925142845Sobrien case 'H': 1926149024Sstefanf ps[i] = '\0'; 1927142845Sobrien gethostname(&ps[i], PROMPTLEN - i); 1928142845Sobrien /* Skip to end of hostname. */ 1929142845Sobrien trim = (*fmt == 'h') ? '.' : '\0'; 1930142845Sobrien while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1931142845Sobrien i++; 1932142845Sobrien break; 1933142845Sobrien 1934142845Sobrien /* 1935142845Sobrien * Working directory. 1936142845Sobrien * 1937142845Sobrien * \W specifies just the final component, 1938142845Sobrien * \w specifies the entire path. 1939142845Sobrien */ 1940142845Sobrien case 'W': 1941142845Sobrien case 'w': 1942209653Sjilles pwd = lookupvar("PWD"); 1943209653Sjilles if (pwd == NULL) 1944209653Sjilles pwd = "?"; 1945209653Sjilles if (*fmt == 'W' && 1946209653Sjilles *pwd == '/' && pwd[1] != '\0') 1947209653Sjilles strlcpy(&ps[i], strrchr(pwd, '/') + 1, 1948209653Sjilles PROMPTLEN - i); 1949209653Sjilles else 1950209653Sjilles strlcpy(&ps[i], pwd, PROMPTLEN - i); 1951142845Sobrien /* Skip to end of path. */ 1952142845Sobrien while (ps[i + 1] != '\0') 1953142845Sobrien i++; 1954142845Sobrien break; 1955142845Sobrien 1956142845Sobrien /* 1957142845Sobrien * Superuser status. 1958142845Sobrien * 1959142845Sobrien * '$' for normal users, '#' for root. 1960142845Sobrien */ 1961142845Sobrien case '$': 1962142845Sobrien ps[i] = (geteuid() != 0) ? '$' : '#'; 1963142845Sobrien break; 1964142845Sobrien 1965142845Sobrien /* 1966142845Sobrien * A literal \. 1967142845Sobrien */ 1968142845Sobrien case '\\': 1969142845Sobrien ps[i] = '\\'; 1970142845Sobrien break; 1971142845Sobrien 1972142845Sobrien /* 1973142845Sobrien * Emit unrecognized formats verbatim. 1974142845Sobrien */ 1975142845Sobrien default: 1976142845Sobrien ps[i++] = '\\'; 1977142845Sobrien ps[i] = *fmt; 1978142845Sobrien break; 1979142845Sobrien } 1980142845Sobrien else 1981142845Sobrien ps[i] = *fmt; 1982142845Sobrien ps[i] = '\0'; 1983142845Sobrien return (ps); 19841556Srgrimes} 1985