parser.c revision 215783
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 215783 2010-11-23 22:17:39Z 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; 3921556Srgrimes 393214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 39417987Speter redir = NULL; 39517987Speter n1 = NULL; 3961556Srgrimes rpp = &redir; 39720425Ssteve 3981556Srgrimes /* Check for redirection which may precede command */ 3991556Srgrimes while (readtoken() == TREDIR) { 4001556Srgrimes *rpp = n2 = redirnode; 4011556Srgrimes rpp = &n2->nfile.next; 4021556Srgrimes parsefname(); 4031556Srgrimes } 4041556Srgrimes tokpushback++; 4051556Srgrimes 4061556Srgrimes switch (readtoken()) { 4071556Srgrimes case TIF: 4081556Srgrimes n1 = (union node *)stalloc(sizeof (struct nif)); 4091556Srgrimes n1->type = NIF; 410214525Sjilles if ((n1->nif.test = list(0, 0)) == NULL) 411104554Stjr synexpect(-1); 4121556Srgrimes if (readtoken() != TTHEN) 4131556Srgrimes synexpect(TTHEN); 414214525Sjilles n1->nif.ifpart = list(0, 0); 4151556Srgrimes n2 = n1; 4161556Srgrimes while (readtoken() == TELIF) { 4171556Srgrimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 4181556Srgrimes n2 = n2->nif.elsepart; 4191556Srgrimes n2->type = NIF; 420214525Sjilles if ((n2->nif.test = list(0, 0)) == NULL) 421104554Stjr synexpect(-1); 4221556Srgrimes if (readtoken() != TTHEN) 4231556Srgrimes synexpect(TTHEN); 424214525Sjilles n2->nif.ifpart = list(0, 0); 4251556Srgrimes } 4261556Srgrimes if (lasttoken == TELSE) 427214525Sjilles n2->nif.elsepart = list(0, 0); 4281556Srgrimes else { 4291556Srgrimes n2->nif.elsepart = NULL; 4301556Srgrimes tokpushback++; 4311556Srgrimes } 4321556Srgrimes if (readtoken() != TFI) 4331556Srgrimes synexpect(TFI); 434214709Sjilles checkkwd = CHKKWD | CHKALIAS; 4351556Srgrimes break; 4361556Srgrimes case TWHILE: 4371556Srgrimes case TUNTIL: { 4381556Srgrimes int got; 4391556Srgrimes n1 = (union node *)stalloc(sizeof (struct nbinary)); 4401556Srgrimes n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 441214525Sjilles if ((n1->nbinary.ch1 = list(0, 0)) == NULL) 442104554Stjr synexpect(-1); 4431556Srgrimes if ((got=readtoken()) != TDO) { 4441556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 4451556Srgrimes synexpect(TDO); 4461556Srgrimes } 447214525Sjilles n1->nbinary.ch2 = list(0, 0); 4481556Srgrimes if (readtoken() != TDONE) 4491556Srgrimes synexpect(TDONE); 450214709Sjilles checkkwd = CHKKWD | CHKALIAS; 4511556Srgrimes break; 4521556Srgrimes } 4531556Srgrimes case TFOR: 4541556Srgrimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 4551556Srgrimes synerror("Bad for loop variable"); 4561556Srgrimes n1 = (union node *)stalloc(sizeof (struct nfor)); 4571556Srgrimes n1->type = NFOR; 4581556Srgrimes n1->nfor.var = wordtext; 459199282Sjilles while (readtoken() == TNL) 460199282Sjilles ; 461199282Sjilles if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) { 4621556Srgrimes app = ≈ 4631556Srgrimes while (readtoken() == TWORD) { 4641556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 4651556Srgrimes n2->type = NARG; 4661556Srgrimes n2->narg.text = wordtext; 4671556Srgrimes n2->narg.backquote = backquotelist; 4681556Srgrimes *app = n2; 4691556Srgrimes app = &n2->narg.next; 4701556Srgrimes } 4711556Srgrimes *app = NULL; 4721556Srgrimes n1->nfor.args = ap; 4731556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 4741556Srgrimes synexpect(-1); 4751556Srgrimes } else { 476149096Sstefanf static char argvars[5] = { 477149096Sstefanf CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' 478149096Sstefanf }; 4791556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 4801556Srgrimes n2->type = NARG; 481149096Sstefanf n2->narg.text = argvars; 4821556Srgrimes n2->narg.backquote = NULL; 4831556Srgrimes n2->narg.next = NULL; 4841556Srgrimes n1->nfor.args = n2; 4851556Srgrimes /* 4861556Srgrimes * Newline or semicolon here is optional (but note 4871556Srgrimes * that the original Bourne shell only allowed NL). 4881556Srgrimes */ 4891556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 4901556Srgrimes tokpushback++; 4911556Srgrimes } 492214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 4931556Srgrimes if ((t = readtoken()) == TDO) 4941556Srgrimes t = TDONE; 4951556Srgrimes else if (t == TBEGIN) 4961556Srgrimes t = TEND; 4971556Srgrimes else 4981556Srgrimes synexpect(-1); 499214525Sjilles n1->nfor.body = list(0, 0); 5001556Srgrimes if (readtoken() != t) 5011556Srgrimes synexpect(t); 502214709Sjilles checkkwd = CHKKWD | CHKALIAS; 5031556Srgrimes break; 5041556Srgrimes case TCASE: 5051556Srgrimes n1 = (union node *)stalloc(sizeof (struct ncase)); 5061556Srgrimes n1->type = NCASE; 5071556Srgrimes if (readtoken() != TWORD) 5081556Srgrimes synexpect(TWORD); 5091556Srgrimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 5101556Srgrimes n2->type = NARG; 5111556Srgrimes n2->narg.text = wordtext; 5121556Srgrimes n2->narg.backquote = backquotelist; 5131556Srgrimes n2->narg.next = NULL; 5141556Srgrimes while (readtoken() == TNL); 5151556Srgrimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 5161556Srgrimes synerror("expecting \"in\""); 5171556Srgrimes cpp = &n1->ncase.cases; 518214709Sjilles checkkwd = CHKNL | CHKKWD, readtoken(); 519104202Stjr while (lasttoken != TESAC) { 5201556Srgrimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 5211556Srgrimes cp->type = NCLIST; 5221556Srgrimes app = &cp->nclist.pattern; 523104207Stjr if (lasttoken == TLP) 524104207Stjr readtoken(); 5251556Srgrimes for (;;) { 5261556Srgrimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 5271556Srgrimes ap->type = NARG; 5281556Srgrimes ap->narg.text = wordtext; 5291556Srgrimes ap->narg.backquote = backquotelist; 530214709Sjilles checkkwd = CHKNL | CHKKWD; 531214709Sjilles if (readtoken() != TPIPE) 5321556Srgrimes break; 5331556Srgrimes app = &ap->narg.next; 5342760Ssef readtoken(); 5351556Srgrimes } 5361556Srgrimes ap->narg.next = NULL; 5371556Srgrimes if (lasttoken != TRP) 538214709Sjilles synexpect(TRP); 539214525Sjilles cp->nclist.body = list(0, 0); 5402760Ssef 541214709Sjilles checkkwd = CHKNL | CHKKWD | CHKALIAS; 5422760Ssef if ((t = readtoken()) != TESAC) { 5432760Ssef if (t != TENDCASE) 544214709Sjilles synexpect(TENDCASE); 5452760Ssef else 546214709Sjilles checkkwd = CHKNL | CHKKWD, readtoken(); 5472760Ssef } 5481556Srgrimes cpp = &cp->nclist.next; 549104202Stjr } 5501556Srgrimes *cpp = NULL; 551214709Sjilles checkkwd = CHKKWD | CHKALIAS; 5521556Srgrimes break; 5531556Srgrimes case TLP: 5541556Srgrimes n1 = (union node *)stalloc(sizeof (struct nredir)); 5551556Srgrimes n1->type = NSUBSHELL; 556214525Sjilles n1->nredir.n = list(0, 0); 5571556Srgrimes n1->nredir.redirect = NULL; 5581556Srgrimes if (readtoken() != TRP) 5591556Srgrimes synexpect(TRP); 560214709Sjilles checkkwd = CHKKWD | CHKALIAS; 5611556Srgrimes break; 5621556Srgrimes case TBEGIN: 563214525Sjilles n1 = list(0, 0); 5641556Srgrimes if (readtoken() != TEND) 5651556Srgrimes synexpect(TEND); 566214709Sjilles checkkwd = CHKKWD | CHKALIAS; 5671556Srgrimes break; 5681556Srgrimes /* Handle an empty command like other simple commands. */ 569210221Sjilles case TBACKGND: 57017987Speter case TSEMI: 571101662Stjr case TAND: 572101662Stjr case TOR: 57317987Speter /* 57417987Speter * An empty command before a ; doesn't make much sense, and 57517987Speter * should certainly be disallowed in the case of `if ;'. 57617987Speter */ 57717987Speter if (!redir) 57817987Speter synexpect(-1); 5791556Srgrimes case TNL: 58010399Sjoerg case TEOF: 5811556Srgrimes case TWORD: 58217987Speter case TRP: 5831556Srgrimes tokpushback++; 58475160Sbrian n1 = simplecmd(rpp, redir); 585214281Sjilles return n1; 5861556Srgrimes default: 5871556Srgrimes synexpect(-1); 5881556Srgrimes } 5891556Srgrimes 5901556Srgrimes /* Now check for redirection which may follow command */ 5911556Srgrimes while (readtoken() == TREDIR) { 5921556Srgrimes *rpp = n2 = redirnode; 5931556Srgrimes rpp = &n2->nfile.next; 5941556Srgrimes parsefname(); 5951556Srgrimes } 5961556Srgrimes tokpushback++; 5971556Srgrimes *rpp = NULL; 5981556Srgrimes if (redir) { 5991556Srgrimes if (n1->type != NSUBSHELL) { 6001556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 6011556Srgrimes n2->type = NREDIR; 6021556Srgrimes n2->nredir.n = n1; 6031556Srgrimes n1 = n2; 6041556Srgrimes } 6051556Srgrimes n1->nredir.redirect = redir; 6061556Srgrimes } 60775160Sbrian 608214281Sjilles return n1; 6091556Srgrimes} 6101556Srgrimes 6111556Srgrimes 612213811Sobrienstatic union node * 61390111Simpsimplecmd(union node **rpp, union node *redir) 61490111Simp{ 6151556Srgrimes union node *args, **app; 6161556Srgrimes union node **orig_rpp = rpp; 617210087Sjilles union node *n = NULL; 618214304Sjilles int special; 6191556Srgrimes 6201556Srgrimes /* If we don't have any redirections already, then we must reset */ 6211556Srgrimes /* rpp to be the address of the local redir variable. */ 6221556Srgrimes if (redir == 0) 6231556Srgrimes rpp = &redir; 6241556Srgrimes 6251556Srgrimes args = NULL; 6261556Srgrimes app = &args; 6278855Srgrimes /* 6281556Srgrimes * We save the incoming value, because we need this for shell 6291556Srgrimes * functions. There can not be a redirect or an argument between 6308855Srgrimes * the function name and the open parenthesis. 6311556Srgrimes */ 6321556Srgrimes orig_rpp = rpp; 6331556Srgrimes 6341556Srgrimes for (;;) { 6351556Srgrimes if (readtoken() == TWORD) { 6361556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 6371556Srgrimes n->type = NARG; 6381556Srgrimes n->narg.text = wordtext; 6391556Srgrimes n->narg.backquote = backquotelist; 6401556Srgrimes *app = n; 6411556Srgrimes app = &n->narg.next; 6421556Srgrimes } else if (lasttoken == TREDIR) { 6431556Srgrimes *rpp = n = redirnode; 6441556Srgrimes rpp = &n->nfile.next; 6451556Srgrimes parsefname(); /* read name of redirection file */ 6461556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 6471556Srgrimes && rpp == orig_rpp) { 6481556Srgrimes /* We have a function */ 6491556Srgrimes if (readtoken() != TRP) 6501556Srgrimes synexpect(TRP); 651179022Sstefanf funclinno = plinno; 652214291Sjilles /* 653214291Sjilles * - Require plain text. 654214291Sjilles * - Functions with '/' cannot be called. 655214534Sjilles * - Reject name=(). 656214534Sjilles * - Reject ksh extended glob patterns. 657214291Sjilles */ 658214291Sjilles if (!noexpand(n->narg.text) || quoteflag || 659214534Sjilles strchr(n->narg.text, '/') || 660214534Sjilles strchr("!%*+-=?@}~", 661214534Sjilles n->narg.text[strlen(n->narg.text) - 1])) 6621556Srgrimes synerror("Bad function name"); 663214291Sjilles rmescapes(n->narg.text); 664214304Sjilles if (find_builtin(n->narg.text, &special) >= 0 && 665214304Sjilles special) 666214304Sjilles synerror("Cannot override a special builtin with a function"); 6671556Srgrimes n->type = NDEFUN; 6681556Srgrimes n->narg.next = command(); 669179022Sstefanf funclinno = 0; 670210087Sjilles return n; 6711556Srgrimes } else { 6721556Srgrimes tokpushback++; 6731556Srgrimes break; 6741556Srgrimes } 6751556Srgrimes } 6761556Srgrimes *app = NULL; 6771556Srgrimes *rpp = NULL; 6781556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 6791556Srgrimes n->type = NCMD; 6801556Srgrimes n->ncmd.backgnd = 0; 6811556Srgrimes n->ncmd.args = args; 6821556Srgrimes n->ncmd.redirect = redir; 683210087Sjilles return n; 6841556Srgrimes} 6851556Srgrimes 686213811Sobrienstatic union node * 68790111Simpmakename(void) 68890111Simp{ 68917987Speter union node *n; 6901556Srgrimes 69117987Speter n = (union node *)stalloc(sizeof (struct narg)); 69217987Speter n->type = NARG; 69317987Speter n->narg.next = NULL; 69417987Speter n->narg.text = wordtext; 69517987Speter n->narg.backquote = backquotelist; 69617987Speter return n; 69717987Speter} 69817987Speter 699213760Sobrienvoid 700213760Sobrienfixredir(union node *n, const char *text, int err) 70190111Simp{ 70217987Speter TRACE(("Fix redir %s %d\n", text, err)); 70317987Speter if (!err) 70417987Speter n->ndup.vname = NULL; 70517987Speter 70617987Speter if (is_digit(text[0]) && text[1] == '\0') 70717987Speter n->ndup.dupfd = digit_val(text[0]); 70817987Speter else if (text[0] == '-' && text[1] == '\0') 70917987Speter n->ndup.dupfd = -1; 71017987Speter else { 71120425Ssteve 71217987Speter if (err) 71317987Speter synerror("Bad fd number"); 71417987Speter else 71517987Speter n->ndup.vname = makename(); 71617987Speter } 71717987Speter} 71817987Speter 71917987Speter 720213811Sobrienstatic void 72190111Simpparsefname(void) 72290111Simp{ 7231556Srgrimes union node *n = redirnode; 7241556Srgrimes 7251556Srgrimes if (readtoken() != TWORD) 7261556Srgrimes synexpect(-1); 7271556Srgrimes if (n->type == NHERE) { 7281556Srgrimes struct heredoc *here = heredoc; 7291556Srgrimes struct heredoc *p; 7301556Srgrimes int i; 7311556Srgrimes 7321556Srgrimes if (quoteflag == 0) 7331556Srgrimes n->type = NXHERE; 7341556Srgrimes TRACE(("Here document %d\n", n->type)); 7351556Srgrimes if (here->striptabs) { 7361556Srgrimes while (*wordtext == '\t') 7371556Srgrimes wordtext++; 7381556Srgrimes } 7391556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 7401556Srgrimes synerror("Illegal eof marker for << redirection"); 7411556Srgrimes rmescapes(wordtext); 7421556Srgrimes here->eofmark = wordtext; 7431556Srgrimes here->next = NULL; 7441556Srgrimes if (heredoclist == NULL) 7451556Srgrimes heredoclist = here; 7461556Srgrimes else { 7471556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 7481556Srgrimes p->next = here; 7491556Srgrimes } 7501556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 75117987Speter fixredir(n, wordtext, 0); 7521556Srgrimes } else { 75317987Speter n->nfile.fname = makename(); 7541556Srgrimes } 7551556Srgrimes} 7561556Srgrimes 7571556Srgrimes 7581556Srgrimes/* 7591556Srgrimes * Input any here documents. 7601556Srgrimes */ 7611556Srgrimes 762213811Sobrienstatic void 76390111Simpparseheredoc(void) 76490111Simp{ 7651556Srgrimes struct heredoc *here; 7661556Srgrimes union node *n; 7671556Srgrimes 7681556Srgrimes while (heredoclist) { 7691556Srgrimes here = heredoclist; 7701556Srgrimes heredoclist = here->next; 7711556Srgrimes if (needprompt) { 7721556Srgrimes setprompt(2); 7731556Srgrimes needprompt = 0; 7741556Srgrimes } 7751556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 7761556Srgrimes here->eofmark, here->striptabs); 7771556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 7781556Srgrimes n->narg.type = NARG; 7791556Srgrimes n->narg.next = NULL; 7801556Srgrimes n->narg.text = wordtext; 7811556Srgrimes n->narg.backquote = backquotelist; 7821556Srgrimes here->here->nhere.doc = n; 7831556Srgrimes } 7841556Srgrimes} 7851556Srgrimes 786213811Sobrienstatic int 78790111Simppeektoken(void) 78890111Simp{ 7891556Srgrimes int t; 7901556Srgrimes 7911556Srgrimes t = readtoken(); 7921556Srgrimes tokpushback++; 7931556Srgrimes return (t); 7941556Srgrimes} 7951556Srgrimes 796213811Sobrienstatic int 79790111Simpreadtoken(void) 79890111Simp{ 7991556Srgrimes int t; 8001556Srgrimes struct alias *ap; 8011556Srgrimes#ifdef DEBUG 8021556Srgrimes int alreadyseen = tokpushback; 8031556Srgrimes#endif 8048855Srgrimes 8051556Srgrimes top: 8061556Srgrimes t = xxreadtoken(); 8071556Srgrimes 808214709Sjilles /* 809214709Sjilles * eat newlines 810214709Sjilles */ 811214709Sjilles if (checkkwd & CHKNL) { 812214709Sjilles while (t == TNL) { 813214709Sjilles parseheredoc(); 814214709Sjilles t = xxreadtoken(); 815214709Sjilles } 816214709Sjilles } 8171556Srgrimes 818214709Sjilles /* 819214709Sjilles * check for keywords and aliases 820214709Sjilles */ 821214709Sjilles if (t == TWORD && !quoteflag) 822214709Sjilles { 823214709Sjilles const char * const *pp; 824214709Sjilles 825214709Sjilles if (checkkwd & CHKKWD) 82698463Sjmallett for (pp = parsekwd; *pp; pp++) { 82720425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 82817987Speter { 8291556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 8301556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 8311556Srgrimes goto out; 8321556Srgrimes } 8331556Srgrimes } 834214709Sjilles if (checkkwd & CHKALIAS && 835214709Sjilles (ap = lookupalias(wordtext, 1)) != NULL) { 836214709Sjilles pushstring(ap->val, strlen(ap->val), ap); 837214709Sjilles goto top; 8381556Srgrimes } 839214709Sjilles } 8401556Srgrimesout: 841214709Sjilles if (t != TNOT) 842214709Sjilles checkkwd = 0; 843214709Sjilles 8441556Srgrimes#ifdef DEBUG 8451556Srgrimes if (!alreadyseen) 8461556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8471556Srgrimes else 8481556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8491556Srgrimes#endif 8501556Srgrimes return (t); 8511556Srgrimes} 8521556Srgrimes 8531556Srgrimes 8541556Srgrimes/* 8551556Srgrimes * Read the next input token. 8561556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 8571556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 8581556Srgrimes * quoted. 8591556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 8601556Srgrimes * the redirection. 8611556Srgrimes * In all cases, the variable startlinno is set to the number of the line 8621556Srgrimes * on which the token starts. 8631556Srgrimes * 8641556Srgrimes * [Change comment: here documents and internal procedures] 8651556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 8661556Srgrimes * word parsing code into a separate routine. In this case, readtoken 8671556Srgrimes * doesn't need to have any internal procedures, but parseword does. 8681556Srgrimes * We could also make parseoperator in essence the main routine, and 8691556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 8701556Srgrimes */ 8711556Srgrimes 8721556Srgrimes#define RETURN(token) return lasttoken = token 8731556Srgrimes 874213811Sobrienstatic int 87590111Simpxxreadtoken(void) 87690111Simp{ 87725230Ssteve int c; 8781556Srgrimes 8791556Srgrimes if (tokpushback) { 8801556Srgrimes tokpushback = 0; 8811556Srgrimes return lasttoken; 8821556Srgrimes } 8831556Srgrimes if (needprompt) { 8841556Srgrimes setprompt(2); 8851556Srgrimes needprompt = 0; 8861556Srgrimes } 8871556Srgrimes startlinno = plinno; 8881556Srgrimes for (;;) { /* until token or start of word found */ 8891556Srgrimes c = pgetc_macro(); 8901556Srgrimes if (c == ' ' || c == '\t') 8911556Srgrimes continue; /* quick check for white space first */ 8921556Srgrimes switch (c) { 8931556Srgrimes case ' ': case '\t': 8941556Srgrimes continue; 8951556Srgrimes case '#': 8961556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 8971556Srgrimes pungetc(); 8981556Srgrimes continue; 8991556Srgrimes case '\\': 9001556Srgrimes if (pgetc() == '\n') { 9011556Srgrimes startlinno = ++plinno; 9021556Srgrimes if (doprompt) 9031556Srgrimes setprompt(2); 9041556Srgrimes else 9051556Srgrimes setprompt(0); 9061556Srgrimes continue; 9071556Srgrimes } 9081556Srgrimes pungetc(); 9091556Srgrimes goto breakloop; 9101556Srgrimes case '\n': 9111556Srgrimes plinno++; 9121556Srgrimes needprompt = doprompt; 9131556Srgrimes RETURN(TNL); 9141556Srgrimes case PEOF: 9151556Srgrimes RETURN(TEOF); 9161556Srgrimes case '&': 9171556Srgrimes if (pgetc() == '&') 9181556Srgrimes RETURN(TAND); 9191556Srgrimes pungetc(); 9201556Srgrimes RETURN(TBACKGND); 9211556Srgrimes case '|': 9221556Srgrimes if (pgetc() == '|') 9231556Srgrimes RETURN(TOR); 9241556Srgrimes pungetc(); 9251556Srgrimes RETURN(TPIPE); 9261556Srgrimes case ';': 9271556Srgrimes if (pgetc() == ';') 9281556Srgrimes RETURN(TENDCASE); 9291556Srgrimes pungetc(); 9301556Srgrimes RETURN(TSEMI); 9311556Srgrimes case '(': 9321556Srgrimes RETURN(TLP); 9331556Srgrimes case ')': 9341556Srgrimes RETURN(TRP); 9351556Srgrimes default: 9361556Srgrimes goto breakloop; 9371556Srgrimes } 9381556Srgrimes } 9391556Srgrimesbreakloop: 9401556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 9411556Srgrimes#undef RETURN 9421556Srgrimes} 9431556Srgrimes 9441556Srgrimes 945213811Sobrien#define MAXNEST_static 8 946206145Sjillesstruct tokenstate 947206145Sjilles{ 948206145Sjilles const char *syntax; /* *SYNTAX */ 949206145Sjilles int parenlevel; /* levels of parentheses in arithmetic */ 950206145Sjilles enum tokenstate_category 951206145Sjilles { 952206145Sjilles TSTATE_TOP, 953206145Sjilles TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */ 954206145Sjilles TSTATE_VAR_NEW, /* other ${var...}, own dquote state */ 955206145Sjilles TSTATE_ARITH 956206145Sjilles } category; 957206145Sjilles}; 958206145Sjilles 959206145Sjilles 960205130Sjilles/* 961205130Sjilles * Called to parse command substitutions. 962205130Sjilles */ 9631556Srgrimes 964213811Sobrienstatic char * 965205130Sjillesparsebackq(char *out, struct nodelist **pbqlist, 966205130Sjilles int oldstyle, int dblquote, int quoted) 967205130Sjilles{ 968205130Sjilles struct nodelist **nlpp; 969205130Sjilles union node *n; 970205130Sjilles char *volatile str; 971205130Sjilles struct jmploc jmploc; 972205130Sjilles struct jmploc *const savehandler = handler; 973205130Sjilles int savelen; 974205130Sjilles int saveprompt; 975205130Sjilles const int bq_startlinno = plinno; 976205130Sjilles char *volatile ostr = NULL; 977205130Sjilles struct parsefile *const savetopfile = getcurrentfile(); 978208655Sjilles struct heredoc *const saveheredoclist = heredoclist; 979208655Sjilles struct heredoc *here; 980205130Sjilles 981205130Sjilles str = NULL; 982205130Sjilles if (setjmp(jmploc.loc)) { 983205130Sjilles popfilesupto(savetopfile); 984205130Sjilles if (str) 985205130Sjilles ckfree(str); 986205130Sjilles if (ostr) 987205130Sjilles ckfree(ostr); 988208655Sjilles heredoclist = saveheredoclist; 989205130Sjilles handler = savehandler; 990205130Sjilles if (exception == EXERROR) { 991205130Sjilles startlinno = bq_startlinno; 992205130Sjilles synerror("Error in command substitution"); 993205130Sjilles } 994205130Sjilles longjmp(handler->loc, 1); 995205130Sjilles } 996205130Sjilles INTOFF; 997205130Sjilles savelen = out - stackblock(); 998205130Sjilles if (savelen > 0) { 999205130Sjilles str = ckmalloc(savelen); 1000205130Sjilles memcpy(str, stackblock(), savelen); 1001205130Sjilles } 1002205130Sjilles handler = &jmploc; 1003208655Sjilles heredoclist = NULL; 1004205130Sjilles INTON; 1005205130Sjilles if (oldstyle) { 1006205130Sjilles /* We must read until the closing backquote, giving special 1007205130Sjilles treatment to some slashes, and then push the string and 1008205130Sjilles reread it as input, interpreting it normally. */ 1009205130Sjilles char *oout; 1010205130Sjilles int c; 1011205130Sjilles int olen; 1012205130Sjilles 1013205130Sjilles 1014205130Sjilles STARTSTACKSTR(oout); 1015205130Sjilles for (;;) { 1016205130Sjilles if (needprompt) { 1017205130Sjilles setprompt(2); 1018205130Sjilles needprompt = 0; 1019205130Sjilles } 1020215783Sjilles CHECKSTRSPACE(2, oout); 1021205130Sjilles switch (c = pgetc()) { 1022205130Sjilles case '`': 1023205130Sjilles goto done; 1024205130Sjilles 1025205130Sjilles case '\\': 1026205130Sjilles if ((c = pgetc()) == '\n') { 1027205130Sjilles plinno++; 1028205130Sjilles if (doprompt) 1029205130Sjilles setprompt(2); 1030205130Sjilles else 1031205130Sjilles setprompt(0); 1032205130Sjilles /* 1033205130Sjilles * If eating a newline, avoid putting 1034205130Sjilles * the newline into the new character 1035215783Sjilles * stream (via the USTPUTC after the 1036205130Sjilles * switch). 1037205130Sjilles */ 1038205130Sjilles continue; 1039205130Sjilles } 1040205130Sjilles if (c != '\\' && c != '`' && c != '$' 1041205130Sjilles && (!dblquote || c != '"')) 1042215783Sjilles USTPUTC('\\', oout); 1043205130Sjilles break; 1044205130Sjilles 1045205130Sjilles case '\n': 1046205130Sjilles plinno++; 1047205130Sjilles needprompt = doprompt; 1048205130Sjilles break; 1049205130Sjilles 1050205130Sjilles case PEOF: 1051205130Sjilles startlinno = plinno; 1052205130Sjilles synerror("EOF in backquote substitution"); 1053205130Sjilles break; 1054205130Sjilles 1055205130Sjilles default: 1056205130Sjilles break; 1057205130Sjilles } 1058215783Sjilles USTPUTC(c, oout); 1059205130Sjilles } 1060205130Sjillesdone: 1061215783Sjilles USTPUTC('\0', oout); 1062205130Sjilles olen = oout - stackblock(); 1063205130Sjilles INTOFF; 1064205130Sjilles ostr = ckmalloc(olen); 1065205130Sjilles memcpy(ostr, stackblock(), olen); 1066205130Sjilles setinputstring(ostr, 1); 1067205130Sjilles INTON; 1068205130Sjilles } 1069205130Sjilles nlpp = pbqlist; 1070205130Sjilles while (*nlpp) 1071205130Sjilles nlpp = &(*nlpp)->next; 1072205130Sjilles *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 1073205130Sjilles (*nlpp)->next = NULL; 1074205130Sjilles 1075205130Sjilles if (oldstyle) { 1076205130Sjilles saveprompt = doprompt; 1077205130Sjilles doprompt = 0; 1078205130Sjilles } 1079205130Sjilles 1080214525Sjilles n = list(0, oldstyle); 1081205130Sjilles 1082205130Sjilles if (oldstyle) 1083205130Sjilles doprompt = saveprompt; 1084205130Sjilles else { 1085205130Sjilles if (readtoken() != TRP) 1086205130Sjilles synexpect(TRP); 1087205130Sjilles } 1088205130Sjilles 1089205130Sjilles (*nlpp)->n = n; 1090205130Sjilles if (oldstyle) { 1091205130Sjilles /* 1092205130Sjilles * Start reading from old file again, ignoring any pushed back 1093205130Sjilles * tokens left from the backquote parsing 1094205130Sjilles */ 1095205130Sjilles popfile(); 1096205130Sjilles tokpushback = 0; 1097205130Sjilles } 1098205130Sjilles while (stackblocksize() <= savelen) 1099205130Sjilles growstackblock(); 1100205130Sjilles STARTSTACKSTR(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/* 11301556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 11311556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 11321556Srgrimes * word which marks the end of the document and striptabs is true if 11331556Srgrimes * leading tabs should be stripped from the document. The argument firstc 11341556Srgrimes * is the first character of the input token or document. 11351556Srgrimes * 11361556Srgrimes * Because C does not have internal subroutines, I have simulated them 11371556Srgrimes * using goto's to implement the subroutine linkage. The following macros 11381556Srgrimes * will run code that appears at the end of readtoken1. 11391556Srgrimes */ 11401556Srgrimes 11411556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 11421556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 11431556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 11441556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 11451556Srgrimes 1146213811Sobrienstatic int 1147206145Sjillesreadtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs) 114890111Simp{ 114917987Speter int c = firstc; 115017987Speter char *out; 11511556Srgrimes int len; 11521556Srgrimes char line[EOFMARKLEN + 1]; 11531556Srgrimes struct nodelist *bqlist; 11541556Srgrimes int quotef; 1155206145Sjilles int newvarnest; 1156206145Sjilles int level; 115754679Scracauer int synentry; 1158213811Sobrien struct tokenstate state_static[MAXNEST_static]; 1159213811Sobrien int maxnest = MAXNEST_static; 1160206145Sjilles struct tokenstate *state = state_static; 11611556Srgrimes 11621556Srgrimes startlinno = plinno; 11631556Srgrimes quotef = 0; 11641556Srgrimes bqlist = NULL; 1165206145Sjilles newvarnest = 0; 1166206145Sjilles level = 0; 1167206145Sjilles state[level].syntax = initialsyntax; 1168206145Sjilles state[level].parenlevel = 0; 1169206145Sjilles state[level].category = TSTATE_TOP; 11701556Srgrimes 11711556Srgrimes STARTSTACKSTR(out); 11721556Srgrimes loop: { /* for each line, until end of word */ 11731556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 11741556Srgrimes for (;;) { /* until end of line or end of word */ 1175214512Sjilles CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ 117654679Scracauer 1177206145Sjilles synentry = state[level].syntax[c]; 117854679Scracauer 117954679Scracauer switch(synentry) { 11801556Srgrimes case CNL: /* '\n' */ 1181206145Sjilles if (state[level].syntax == BASESYNTAX) 11821556Srgrimes goto endword; /* exit outer loop */ 11831556Srgrimes USTPUTC(c, out); 11841556Srgrimes plinno++; 11851556Srgrimes if (doprompt) 11861556Srgrimes setprompt(2); 11871556Srgrimes else 11881556Srgrimes setprompt(0); 11891556Srgrimes c = pgetc(); 11901556Srgrimes goto loop; /* continue outer loop */ 11911556Srgrimes case CWORD: 11921556Srgrimes USTPUTC(c, out); 11931556Srgrimes break; 11941556Srgrimes case CCTL: 1195206145Sjilles if (eofmark == NULL || initialsyntax != SQSYNTAX) 11961556Srgrimes USTPUTC(CTLESC, out); 11971556Srgrimes USTPUTC(c, out); 11981556Srgrimes break; 11991556Srgrimes case CBACK: /* backslash */ 12001556Srgrimes c = pgetc(); 12011556Srgrimes if (c == PEOF) { 12021556Srgrimes USTPUTC('\\', out); 12031556Srgrimes pungetc(); 12041556Srgrimes } else if (c == '\n') { 1205160849Syar plinno++; 12061556Srgrimes if (doprompt) 12071556Srgrimes setprompt(2); 12081556Srgrimes else 12091556Srgrimes setprompt(0); 12101556Srgrimes } else { 1211206145Sjilles if (state[level].syntax == DQSYNTAX && 1212206145Sjilles c != '\\' && c != '`' && c != '$' && 1213206145Sjilles (c != '"' || (eofmark != NULL && 1214206145Sjilles newvarnest == 0)) && 1215206145Sjilles (c != '}' || state[level].category != TSTATE_VAR_OLD)) 12161556Srgrimes USTPUTC('\\', out); 1217214512Sjilles if ((eofmark == NULL || 1218214512Sjilles newvarnest > 0) && 1219214512Sjilles state[level].syntax == BASESYNTAX) 1220214512Sjilles USTPUTC(CTLQUOTEMARK, out); 122183675Stegge if (SQSYNTAX[c] == CCTL) 12221556Srgrimes USTPUTC(CTLESC, out); 12231556Srgrimes USTPUTC(c, out); 1224214512Sjilles if ((eofmark == NULL || 1225214512Sjilles newvarnest > 0) && 1226214512Sjilles state[level].syntax == BASESYNTAX && 1227214512Sjilles state[level].category == TSTATE_VAR_OLD) 1228214512Sjilles USTPUTC(CTLQUOTEEND, out); 12291556Srgrimes quotef++; 12301556Srgrimes } 12311556Srgrimes break; 12321556Srgrimes case CSQUOTE: 1233206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1234206145Sjilles state[level].syntax = SQSYNTAX; 12351556Srgrimes break; 12361556Srgrimes case CDQUOTE: 1237206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1238206145Sjilles state[level].syntax = DQSYNTAX; 12391556Srgrimes break; 12401556Srgrimes case CENDQUOTE: 1241206145Sjilles if (eofmark != NULL && newvarnest == 0) 12421556Srgrimes USTPUTC(c, out); 1243206145Sjilles else { 1244214512Sjilles if (state[level].category == TSTATE_VAR_OLD) 1245214512Sjilles USTPUTC(CTLQUOTEEND, out); 1246214305Sjilles state[level].syntax = BASESYNTAX; 12471556Srgrimes quotef++; 12481556Srgrimes } 12491556Srgrimes break; 12501556Srgrimes case CVAR: /* '$' */ 12511556Srgrimes PARSESUB(); /* parse substitution */ 12521556Srgrimes break; 12531556Srgrimes case CENDVAR: /* '}' */ 1254206145Sjilles if (level > 0 && 1255214492Sjilles ((state[level].category == TSTATE_VAR_OLD && 1256214492Sjilles state[level].syntax == 1257214492Sjilles state[level - 1].syntax) || 1258214490Sjilles (state[level].category == TSTATE_VAR_NEW && 1259214490Sjilles state[level].syntax == BASESYNTAX))) { 1260214492Sjilles if (state[level].category == TSTATE_VAR_NEW) 1261206145Sjilles newvarnest--; 1262206145Sjilles level--; 12631556Srgrimes USTPUTC(CTLENDVAR, out); 12641556Srgrimes } else { 12651556Srgrimes USTPUTC(c, out); 12661556Srgrimes } 12671556Srgrimes break; 12681556Srgrimes case CLP: /* '(' in arithmetic */ 1269206145Sjilles state[level].parenlevel++; 12701556Srgrimes USTPUTC(c, out); 12711556Srgrimes break; 12721556Srgrimes case CRP: /* ')' in arithmetic */ 1273206145Sjilles if (state[level].parenlevel > 0) { 12741556Srgrimes USTPUTC(c, out); 1275206145Sjilles --state[level].parenlevel; 12761556Srgrimes } else { 12771556Srgrimes if (pgetc() == ')') { 1278206145Sjilles if (level > 0 && 1279206145Sjilles state[level].category == TSTATE_ARITH) { 1280206145Sjilles level--; 12811556Srgrimes USTPUTC(CTLENDARI, out); 12821556Srgrimes } else 12831556Srgrimes USTPUTC(')', out); 12841556Srgrimes } else { 12858855Srgrimes /* 12861556Srgrimes * unbalanced parens 12871556Srgrimes * (don't 2nd guess - no error) 12881556Srgrimes */ 12891556Srgrimes pungetc(); 12901556Srgrimes USTPUTC(')', out); 12911556Srgrimes } 12921556Srgrimes } 12931556Srgrimes break; 12941556Srgrimes case CBQUOTE: /* '`' */ 1295206145Sjilles out = parsebackq(out, &bqlist, 1, 1296206145Sjilles state[level].syntax == DQSYNTAX && 1297206145Sjilles (eofmark == NULL || newvarnest > 0), 1298206145Sjilles state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX); 12991556Srgrimes break; 13001556Srgrimes case CEOF: 13011556Srgrimes goto endword; /* exit outer loop */ 1302214305Sjilles case CIGN: 1303214305Sjilles break; 13041556Srgrimes default: 1305206145Sjilles if (level == 0) 13061556Srgrimes goto endword; /* exit outer loop */ 13071556Srgrimes USTPUTC(c, out); 13081556Srgrimes } 13091556Srgrimes c = pgetc_macro(); 13101556Srgrimes } 13111556Srgrimes } 13121556Srgrimesendword: 1313206145Sjilles if (state[level].syntax == ARISYNTAX) 13141556Srgrimes synerror("Missing '))'"); 1315206145Sjilles if (state[level].syntax != BASESYNTAX && eofmark == NULL) 13161556Srgrimes synerror("Unterminated quoted string"); 1317206145Sjilles if (state[level].category == TSTATE_VAR_OLD || 1318206145Sjilles state[level].category == TSTATE_VAR_NEW) { 13191556Srgrimes startlinno = plinno; 13201556Srgrimes synerror("Missing '}'"); 13211556Srgrimes } 1322206145Sjilles if (state != state_static) 1323206145Sjilles parser_temp_free_upto(state); 13241556Srgrimes USTPUTC('\0', out); 13251556Srgrimes len = out - stackblock(); 13261556Srgrimes out = stackblock(); 13271556Srgrimes if (eofmark == NULL) { 13281556Srgrimes if ((c == '>' || c == '<') 13291556Srgrimes && quotef == 0 13301556Srgrimes && len <= 2 13311556Srgrimes && (*out == '\0' || is_digit(*out))) { 13321556Srgrimes PARSEREDIR(); 13331556Srgrimes return lasttoken = TREDIR; 13341556Srgrimes } else { 13351556Srgrimes pungetc(); 13361556Srgrimes } 13371556Srgrimes } 13381556Srgrimes quoteflag = quotef; 13391556Srgrimes backquotelist = bqlist; 13401556Srgrimes grabstackblock(len); 13411556Srgrimes wordtext = out; 13421556Srgrimes return lasttoken = TWORD; 13431556Srgrimes/* end of readtoken routine */ 13441556Srgrimes 13451556Srgrimes 13461556Srgrimes/* 13471556Srgrimes * Check to see whether we are at the end of the here document. When this 13481556Srgrimes * is called, c is set to the first character of the next input line. If 13491556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 13501556Srgrimes */ 13511556Srgrimes 13521556Srgrimescheckend: { 13531556Srgrimes if (eofmark) { 13541556Srgrimes if (striptabs) { 13551556Srgrimes while (c == '\t') 13561556Srgrimes c = pgetc(); 13571556Srgrimes } 13581556Srgrimes if (c == *eofmark) { 13591556Srgrimes if (pfgets(line, sizeof line) != NULL) { 136025230Ssteve char *p, *q; 13611556Srgrimes 13621556Srgrimes p = line; 13631556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 13641556Srgrimes if (*p == '\n' && *q == '\0') { 13651556Srgrimes c = PEOF; 13661556Srgrimes plinno++; 13671556Srgrimes needprompt = doprompt; 13681556Srgrimes } else { 13691556Srgrimes pushstring(line, strlen(line), NULL); 13701556Srgrimes } 13711556Srgrimes } 13721556Srgrimes } 13731556Srgrimes } 13741556Srgrimes goto checkend_return; 13751556Srgrimes} 13761556Srgrimes 13771556Srgrimes 13781556Srgrimes/* 13791556Srgrimes * Parse a redirection operator. The variable "out" points to a string 13801556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 13811556Srgrimes * first character of the redirection operator. 13821556Srgrimes */ 13831556Srgrimes 13841556Srgrimesparseredir: { 13851556Srgrimes char fd = *out; 13861556Srgrimes union node *np; 13871556Srgrimes 13881556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 13891556Srgrimes if (c == '>') { 13901556Srgrimes np->nfile.fd = 1; 13911556Srgrimes c = pgetc(); 13921556Srgrimes if (c == '>') 13931556Srgrimes np->type = NAPPEND; 13941556Srgrimes else if (c == '&') 13951556Srgrimes np->type = NTOFD; 139696922Stjr else if (c == '|') 139796922Stjr np->type = NCLOBBER; 13981556Srgrimes else { 13991556Srgrimes np->type = NTO; 14001556Srgrimes pungetc(); 14011556Srgrimes } 14021556Srgrimes } else { /* c == '<' */ 14031556Srgrimes np->nfile.fd = 0; 14041556Srgrimes c = pgetc(); 14051556Srgrimes if (c == '<') { 14061556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 14071556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 14081556Srgrimes np->nfile.fd = 0; 14091556Srgrimes } 14101556Srgrimes np->type = NHERE; 14111556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 14121556Srgrimes heredoc->here = np; 14131556Srgrimes if ((c = pgetc()) == '-') { 14141556Srgrimes heredoc->striptabs = 1; 14151556Srgrimes } else { 14161556Srgrimes heredoc->striptabs = 0; 14171556Srgrimes pungetc(); 14181556Srgrimes } 14191556Srgrimes } else if (c == '&') 14201556Srgrimes np->type = NFROMFD; 142166612Sbrian else if (c == '>') 142266612Sbrian np->type = NFROMTO; 14231556Srgrimes else { 14241556Srgrimes np->type = NFROM; 14251556Srgrimes pungetc(); 14261556Srgrimes } 14271556Srgrimes } 14281556Srgrimes if (fd != '\0') 14291556Srgrimes np->nfile.fd = digit_val(fd); 14301556Srgrimes redirnode = np; 14311556Srgrimes goto parseredir_return; 14321556Srgrimes} 14331556Srgrimes 14341556Srgrimes 14351556Srgrimes/* 14361556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 14371556Srgrimes * and nothing else. 14381556Srgrimes */ 14391556Srgrimes 14401556Srgrimesparsesub: { 1441179022Sstefanf char buf[10]; 14421556Srgrimes int subtype; 14431556Srgrimes int typeloc; 14441556Srgrimes int flags; 14451556Srgrimes char *p; 14461556Srgrimes static const char types[] = "}-+?="; 1447179022Sstefanf int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 1448179022Sstefanf int linno; 1449179387Sstefanf int length; 14501556Srgrimes 14511556Srgrimes c = pgetc(); 1452149026Sstefanf if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) && 1453149026Sstefanf !is_special(c)) { 14541556Srgrimes USTPUTC('$', out); 14551556Srgrimes pungetc(); 14561556Srgrimes } else if (c == '(') { /* $(command) or $((arith)) */ 14571556Srgrimes if (pgetc() == '(') { 14581556Srgrimes PARSEARITH(); 14591556Srgrimes } else { 14601556Srgrimes pungetc(); 1461206145Sjilles out = parsebackq(out, &bqlist, 0, 1462206145Sjilles state[level].syntax == DQSYNTAX && 1463206145Sjilles (eofmark == NULL || newvarnest > 0), 1464206145Sjilles state[level].syntax == DQSYNTAX || 1465206145Sjilles state[level].syntax == ARISYNTAX); 14661556Srgrimes } 14671556Srgrimes } else { 14681556Srgrimes USTPUTC(CTLVAR, out); 14691556Srgrimes typeloc = out - stackblock(); 14701556Srgrimes USTPUTC(VSNORMAL, out); 14711556Srgrimes subtype = VSNORMAL; 1472179022Sstefanf flags = 0; 14731556Srgrimes if (c == '{') { 147418202Speter bracketed_name = 1; 14751556Srgrimes c = pgetc(); 147617987Speter if (c == '#') { 147717987Speter if ((c = pgetc()) == '}') 147817987Speter c = '#'; 147917987Speter else 148017987Speter subtype = VSLENGTH; 148117987Speter } 148217987Speter else 148317987Speter subtype = 0; 14841556Srgrimes } 1485149026Sstefanf if (!is_eof(c) && is_name(c)) { 1486179387Sstefanf length = 0; 14871556Srgrimes do { 14881556Srgrimes STPUTC(c, out); 14891556Srgrimes c = pgetc(); 1490179387Sstefanf length++; 1491149026Sstefanf } while (!is_eof(c) && is_in_name(c)); 1492179387Sstefanf if (length == 6 && 1493179387Sstefanf strncmp(out - length, "LINENO", length) == 0) { 1494179022Sstefanf /* Replace the variable name with the 1495179022Sstefanf * current line number. */ 1496179022Sstefanf linno = plinno; 1497179022Sstefanf if (funclinno != 0) 1498179022Sstefanf linno -= funclinno - 1; 1499179022Sstefanf snprintf(buf, sizeof(buf), "%d", linno); 1500179022Sstefanf STADJUST(-6, out); 1501215783Sjilles STPUTS(buf, out); 1502179022Sstefanf flags |= VSLINENO; 1503179022Sstefanf } 150418202Speter } else if (is_digit(c)) { 150518202Speter if (bracketed_name) { 150618202Speter do { 150718202Speter STPUTC(c, out); 150818202Speter c = pgetc(); 150918202Speter } while (is_digit(c)); 151018202Speter } else { 151118202Speter STPUTC(c, out); 151218202Speter c = pgetc(); 151318202Speter } 15141556Srgrimes } else { 1515164003Sstefanf if (! is_special(c)) { 1516164003Sstefanf subtype = VSERROR; 1517164003Sstefanf if (c == '}') 1518164003Sstefanf pungetc(); 1519206144Sjilles else if (c == '\n' || c == PEOF) 1520206144Sjilles synerror("Unexpected end of line in substitution"); 1521164003Sstefanf else 1522164003Sstefanf USTPUTC(c, out); 1523164003Sstefanf } else { 1524164003Sstefanf USTPUTC(c, out); 1525164003Sstefanf c = pgetc(); 1526164003Sstefanf } 15271556Srgrimes } 15281556Srgrimes if (subtype == 0) { 152917987Speter switch (c) { 153017987Speter case ':': 1531179022Sstefanf flags |= VSNUL; 15321556Srgrimes c = pgetc(); 153317987Speter /*FALLTHROUGH*/ 153417987Speter default: 153517987Speter p = strchr(types, c); 1536164003Sstefanf if (p == NULL) { 1537206144Sjilles if (c == '\n' || c == PEOF) 1538206144Sjilles synerror("Unexpected end of line in substitution"); 1539164003Sstefanf if (flags == VSNUL) 1540164003Sstefanf STPUTC(':', out); 1541164003Sstefanf STPUTC(c, out); 1542164003Sstefanf subtype = VSERROR; 1543164003Sstefanf } else 1544164003Sstefanf subtype = p - types + VSNORMAL; 154517987Speter break; 154617987Speter case '%': 154720425Ssteve case '#': 154817987Speter { 154917987Speter int cc = c; 155017987Speter subtype = c == '#' ? VSTRIMLEFT : 155117987Speter VSTRIMRIGHT; 155217987Speter c = pgetc(); 155317987Speter if (c == cc) 155417987Speter subtype++; 155517987Speter else 155617987Speter pungetc(); 155717987Speter break; 155817987Speter } 15591556Srgrimes } 1560164003Sstefanf } else if (subtype != VSERROR) { 15611556Srgrimes pungetc(); 15621556Srgrimes } 1563164003Sstefanf STPUTC('=', out); 1564206145Sjilles if (subtype != VSLENGTH && (state[level].syntax == DQSYNTAX || 1565206145Sjilles state[level].syntax == ARISYNTAX)) 15661556Srgrimes flags |= VSQUOTE; 15671556Srgrimes *(stackblock() + typeloc) = subtype | flags; 1568206145Sjilles if (subtype != VSNORMAL) { 1569206145Sjilles if (level + 1 >= maxnest) { 1570206145Sjilles maxnest *= 2; 1571206145Sjilles if (state == state_static) { 1572206145Sjilles state = parser_temp_alloc( 1573206145Sjilles maxnest * sizeof(*state)); 1574206145Sjilles memcpy(state, state_static, 1575213811Sobrien MAXNEST_static * sizeof(*state)); 1576206145Sjilles } else 1577206145Sjilles state = parser_temp_realloc(state, 1578206145Sjilles maxnest * sizeof(*state)); 1579206145Sjilles } 1580206145Sjilles level++; 1581206145Sjilles state[level].parenlevel = 0; 1582206145Sjilles if (subtype == VSMINUS || subtype == VSPLUS || 1583206145Sjilles subtype == VSQUESTION || subtype == VSASSIGN) { 1584206145Sjilles /* 1585206145Sjilles * For operators that were in the Bourne shell, 1586206145Sjilles * inherit the double-quote state. 1587206145Sjilles */ 1588206145Sjilles state[level].syntax = state[level - 1].syntax; 1589206145Sjilles state[level].category = TSTATE_VAR_OLD; 1590206145Sjilles } else { 1591206145Sjilles /* 1592206145Sjilles * The other operators take a pattern, 1593206145Sjilles * so go to BASESYNTAX. 1594206145Sjilles * Also, ' and " are now special, even 1595206145Sjilles * in here documents. 1596206145Sjilles */ 1597206145Sjilles state[level].syntax = BASESYNTAX; 1598206145Sjilles state[level].category = TSTATE_VAR_NEW; 1599206145Sjilles newvarnest++; 1600206145Sjilles } 1601206145Sjilles } 16021556Srgrimes } 16031556Srgrimes goto parsesub_return; 16041556Srgrimes} 16051556Srgrimes 16061556Srgrimes 16071556Srgrimes/* 16081556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 16091556Srgrimes */ 16101556Srgrimesparsearith: { 16111556Srgrimes 1612206145Sjilles if (level + 1 >= maxnest) { 1613206145Sjilles maxnest *= 2; 1614206145Sjilles if (state == state_static) { 1615206145Sjilles state = parser_temp_alloc( 1616206145Sjilles maxnest * sizeof(*state)); 1617206145Sjilles memcpy(state, state_static, 1618213811Sobrien MAXNEST_static * sizeof(*state)); 1619206145Sjilles } else 1620206145Sjilles state = parser_temp_realloc(state, 1621206145Sjilles maxnest * sizeof(*state)); 16221556Srgrimes } 1623206145Sjilles level++; 1624206145Sjilles state[level].syntax = ARISYNTAX; 1625206145Sjilles state[level].parenlevel = 0; 1626206145Sjilles state[level].category = TSTATE_ARITH; 1627206145Sjilles USTPUTC(CTLARI, out); 1628206145Sjilles if (state[level - 1].syntax == DQSYNTAX) 1629206145Sjilles USTPUTC('"',out); 1630206145Sjilles else 1631206145Sjilles USTPUTC(' ',out); 16321556Srgrimes goto parsearith_return; 16331556Srgrimes} 16341556Srgrimes 16351556Srgrimes} /* end of readtoken */ 16361556Srgrimes 16371556Srgrimes 16381556Srgrimes 16391556Srgrimes#ifdef mkinit 16401556SrgrimesRESET { 16411556Srgrimes tokpushback = 0; 16421556Srgrimes checkkwd = 0; 16431556Srgrimes} 16441556Srgrimes#endif 16451556Srgrimes 16461556Srgrimes/* 16471556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 16481556Srgrimes * or backquotes). 16491556Srgrimes */ 16501556Srgrimes 1651213811Sobrienstatic int 165290111Simpnoexpand(char *text) 165390111Simp{ 165425230Ssteve char *p; 165525230Ssteve char c; 16561556Srgrimes 16571556Srgrimes p = text; 16581556Srgrimes while ((c = *p++) != '\0') { 165939137Stegge if ( c == CTLQUOTEMARK) 166039137Stegge continue; 16611556Srgrimes if (c == CTLESC) 16621556Srgrimes p++; 166383675Stegge else if (BASESYNTAX[(int)c] == CCTL) 16641556Srgrimes return 0; 16651556Srgrimes } 16661556Srgrimes return 1; 16671556Srgrimes} 16681556Srgrimes 16691556Srgrimes 16701556Srgrimes/* 16711556Srgrimes * Return true if the argument is a legal variable name (a letter or 16721556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 16731556Srgrimes */ 16741556Srgrimes 16751556Srgrimesint 1676200956Sjillesgoodname(const char *name) 167790111Simp{ 1678200956Sjilles const char *p; 16791556Srgrimes 16801556Srgrimes p = name; 16811556Srgrimes if (! is_name(*p)) 16821556Srgrimes return 0; 16831556Srgrimes while (*++p) { 16841556Srgrimes if (! is_in_name(*p)) 16851556Srgrimes return 0; 16861556Srgrimes } 16871556Srgrimes return 1; 16881556Srgrimes} 16891556Srgrimes 16901556Srgrimes 16911556Srgrimes/* 16921556Srgrimes * Called when an unexpected token is read during the parse. The argument 16931556Srgrimes * is the token that is expected, or -1 if more than one type of token can 16941556Srgrimes * occur at this point. 16951556Srgrimes */ 16961556Srgrimes 1697213811Sobrienstatic void 169890111Simpsynexpect(int token) 169917987Speter{ 17001556Srgrimes char msg[64]; 17011556Srgrimes 17021556Srgrimes if (token >= 0) { 17031556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 17041556Srgrimes tokname[lasttoken], tokname[token]); 17051556Srgrimes } else { 17061556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 17071556Srgrimes } 17081556Srgrimes synerror(msg); 17091556Srgrimes} 17101556Srgrimes 17111556Srgrimes 1712213811Sobrienstatic void 1713201053Sjillessynerror(const char *msg) 171490111Simp{ 17151556Srgrimes if (commandname) 1716201366Sjilles outfmt(out2, "%s: %d: ", commandname, startlinno); 1717201366Sjilles outfmt(out2, "Syntax error: %s\n", msg); 17181556Srgrimes error((char *)NULL); 17191556Srgrimes} 17201556Srgrimes 1721213811Sobrienstatic void 172290111Simpsetprompt(int which) 172390111Simp{ 17241556Srgrimes whichprompt = which; 17251556Srgrimes 172617987Speter#ifndef NO_HISTORY 17271556Srgrimes if (!el) 172817987Speter#endif 1729199629Sjilles { 17301556Srgrimes out2str(getprompt(NULL)); 1731199629Sjilles flushout(out2); 1732199629Sjilles } 17331556Srgrimes} 17341556Srgrimes 17351556Srgrimes/* 17361556Srgrimes * called by editline -- any expansions to the prompt 17371556Srgrimes * should be added here. 17381556Srgrimes */ 17391556Srgrimeschar * 174090111Simpgetprompt(void *unused __unused) 174125905Ssteve{ 1742142845Sobrien static char ps[PROMPTLEN]; 1743142845Sobrien char *fmt; 1744209653Sjilles const char *pwd; 1745209653Sjilles int i, trim; 1746214538Sjilles static char internal_error[] = "??"; 1747142845Sobrien 1748142845Sobrien /* 1749142845Sobrien * Select prompt format. 1750142845Sobrien */ 17511556Srgrimes switch (whichprompt) { 17521556Srgrimes case 0: 1753201053Sjilles fmt = nullstr; 1754142845Sobrien break; 17551556Srgrimes case 1: 1756142845Sobrien fmt = ps1val(); 1757142845Sobrien break; 17581556Srgrimes case 2: 1759142845Sobrien fmt = ps2val(); 1760142845Sobrien break; 17611556Srgrimes default: 1762201053Sjilles return internal_error; 17631556Srgrimes } 1764142845Sobrien 1765142845Sobrien /* 1766142845Sobrien * Format prompt string. 1767142845Sobrien */ 1768142845Sobrien for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1769142845Sobrien if (*fmt == '\\') 1770142845Sobrien switch (*++fmt) { 1771142845Sobrien 1772142845Sobrien /* 1773142845Sobrien * Hostname. 1774142845Sobrien * 1775142845Sobrien * \h specifies just the local hostname, 1776142845Sobrien * \H specifies fully-qualified hostname. 1777142845Sobrien */ 1778142845Sobrien case 'h': 1779142845Sobrien case 'H': 1780149024Sstefanf ps[i] = '\0'; 1781142845Sobrien gethostname(&ps[i], PROMPTLEN - i); 1782142845Sobrien /* Skip to end of hostname. */ 1783142845Sobrien trim = (*fmt == 'h') ? '.' : '\0'; 1784142845Sobrien while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1785142845Sobrien i++; 1786142845Sobrien break; 1787142845Sobrien 1788142845Sobrien /* 1789142845Sobrien * Working directory. 1790142845Sobrien * 1791142845Sobrien * \W specifies just the final component, 1792142845Sobrien * \w specifies the entire path. 1793142845Sobrien */ 1794142845Sobrien case 'W': 1795142845Sobrien case 'w': 1796209653Sjilles pwd = lookupvar("PWD"); 1797209653Sjilles if (pwd == NULL) 1798209653Sjilles pwd = "?"; 1799209653Sjilles if (*fmt == 'W' && 1800209653Sjilles *pwd == '/' && pwd[1] != '\0') 1801209653Sjilles strlcpy(&ps[i], strrchr(pwd, '/') + 1, 1802209653Sjilles PROMPTLEN - i); 1803209653Sjilles else 1804209653Sjilles strlcpy(&ps[i], pwd, PROMPTLEN - i); 1805142845Sobrien /* Skip to end of path. */ 1806142845Sobrien while (ps[i + 1] != '\0') 1807142845Sobrien i++; 1808142845Sobrien break; 1809142845Sobrien 1810142845Sobrien /* 1811142845Sobrien * Superuser status. 1812142845Sobrien * 1813142845Sobrien * '$' for normal users, '#' for root. 1814142845Sobrien */ 1815142845Sobrien case '$': 1816142845Sobrien ps[i] = (geteuid() != 0) ? '$' : '#'; 1817142845Sobrien break; 1818142845Sobrien 1819142845Sobrien /* 1820142845Sobrien * A literal \. 1821142845Sobrien */ 1822142845Sobrien case '\\': 1823142845Sobrien ps[i] = '\\'; 1824142845Sobrien break; 1825142845Sobrien 1826142845Sobrien /* 1827142845Sobrien * Emit unrecognized formats verbatim. 1828142845Sobrien */ 1829142845Sobrien default: 1830142845Sobrien ps[i++] = '\\'; 1831142845Sobrien ps[i] = *fmt; 1832142845Sobrien break; 1833142845Sobrien } 1834142845Sobrien else 1835142845Sobrien ps[i] = *fmt; 1836142845Sobrien ps[i] = '\0'; 1837142845Sobrien return (ps); 18381556Srgrimes} 1839