parser.c revision 213811
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 213811 2010-10-13 22:18:03Z obrien $"); 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" 6017987Speter#ifndef NO_HISTORY 611556Srgrimes#include "myhistedit.h" 6217987Speter#endif 631556Srgrimes 641556Srgrimes/* 651556Srgrimes * Shell command parser. 661556Srgrimes */ 671556Srgrimes 68142845Sobrien#define EOFMARKLEN 79 69142845Sobrien#define PROMPTLEN 128 701556Srgrimes 711556Srgrimes/* values returned by readtoken */ 7217987Speter#include "token.h" 731556Srgrimes 741556Srgrimes 751556Srgrimes 761556Srgrimesstruct heredoc { 771556Srgrimes struct heredoc *next; /* next here document in list */ 781556Srgrimes union node *here; /* redirection node */ 791556Srgrimes char *eofmark; /* string indicating end of input */ 801556Srgrimes int striptabs; /* if set, strip leading tabs */ 811556Srgrimes}; 821556Srgrimes 83206145Sjillesstruct parser_temp { 84206145Sjilles struct parser_temp *next; 85206145Sjilles void *data; 86206145Sjilles}; 871556Srgrimes 881556Srgrimes 89213760Sobrienstatic struct heredoc *heredoclist; /* list of here documents to read */ 90213760Sobrienstatic int doprompt; /* if set, prompt the user */ 91213760Sobrienstatic int needprompt; /* true if interactive and at start of line */ 92213760Sobrienstatic int lasttoken; /* last token read */ 931556SrgrimesMKINIT int tokpushback; /* last token pushed back */ 94213760Sobrienstatic char *wordtext; /* text of last word returned by readtoken */ 951556SrgrimesMKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ 96213760Sobrienstatic struct nodelist *backquotelist; 97213760Sobrienstatic union node *redirnode; 98213760Sobrienstatic struct heredoc *heredoc; 99213760Sobrienstatic int quoteflag; /* set if (part of) last token was quoted */ 100213760Sobrienstatic int startlinno; /* line # where last token started */ 101213760Sobrienstatic int funclinno; /* line # where the current function started */ 102213760Sobrienstatic struct parser_temp *parser_temp; 1031556Srgrimes 10418018Speter/* XXX When 'noaliases' is set to one, no alias expansion takes place. */ 10518018Speterstatic int noaliases = 0; 1061556Srgrimes 1071556Srgrimes 108213811Sobrienstatic union node *list(int); 109213811Sobrienstatic union node *andor(void); 110213811Sobrienstatic union node *pipeline(void); 111213811Sobrienstatic union node *command(void); 112213811Sobrienstatic union node *simplecmd(union node **, union node *); 113213811Sobrienstatic union node *makename(void); 114213811Sobrienstatic void parsefname(void); 115213811Sobrienstatic void parseheredoc(void); 116213811Sobrienstatic int peektoken(void); 117213811Sobrienstatic int readtoken(void); 118213811Sobrienstatic int xxreadtoken(void); 119213811Sobrienstatic int readtoken1(int, char const *, char *, int); 120213811Sobrienstatic int noexpand(char *); 121213811Sobrienstatic void synexpect(int) __dead2; 122213811Sobrienstatic void synerror(const char *) __dead2; 123213811Sobrienstatic void setprompt(int); 1241556Srgrimes 12517987Speter 126213811Sobrienstatic void * 127206145Sjillesparser_temp_alloc(size_t len) 128206145Sjilles{ 129206145Sjilles struct parser_temp *t; 130206145Sjilles 131206145Sjilles INTOFF; 132206145Sjilles t = ckmalloc(sizeof(*t)); 133206145Sjilles t->data = NULL; 134206145Sjilles t->next = parser_temp; 135206145Sjilles parser_temp = t; 136206145Sjilles t->data = ckmalloc(len); 137206145Sjilles INTON; 138206145Sjilles return t->data; 139206145Sjilles} 140206145Sjilles 141206145Sjilles 142213811Sobrienstatic void * 143206145Sjillesparser_temp_realloc(void *ptr, size_t len) 144206145Sjilles{ 145206145Sjilles struct parser_temp *t; 146206145Sjilles 147206145Sjilles INTOFF; 148206145Sjilles t = parser_temp; 149206145Sjilles if (ptr != t->data) 150206145Sjilles error("bug: parser_temp_realloc misused"); 151206145Sjilles t->data = ckrealloc(t->data, len); 152206145Sjilles INTON; 153206145Sjilles return t->data; 154206145Sjilles} 155206145Sjilles 156206145Sjilles 157213811Sobrienstatic void 158206145Sjillesparser_temp_free_upto(void *ptr) 159206145Sjilles{ 160206145Sjilles struct parser_temp *t; 161206145Sjilles int done = 0; 162206145Sjilles 163206145Sjilles INTOFF; 164206145Sjilles while (parser_temp != NULL && !done) { 165206145Sjilles t = parser_temp; 166206145Sjilles parser_temp = t->next; 167206145Sjilles done = t->data == ptr; 168206145Sjilles ckfree(t->data); 169206145Sjilles ckfree(t); 170206145Sjilles } 171206145Sjilles INTON; 172206145Sjilles if (!done) 173206145Sjilles error("bug: parser_temp_free_upto misused"); 174206145Sjilles} 175206145Sjilles 176206145Sjilles 177213811Sobrienstatic void 178206145Sjillesparser_temp_free_all(void) 179206145Sjilles{ 180206145Sjilles struct parser_temp *t; 181206145Sjilles 182206145Sjilles INTOFF; 183206145Sjilles while (parser_temp != NULL) { 184206145Sjilles t = parser_temp; 185206145Sjilles parser_temp = t->next; 186206145Sjilles ckfree(t->data); 187206145Sjilles ckfree(t); 188206145Sjilles } 189206145Sjilles INTON; 190206145Sjilles} 191206145Sjilles 192206145Sjilles 1931556Srgrimes/* 1941556Srgrimes * Read and parse a command. Returns NEOF on end of file. (NULL is a 1951556Srgrimes * valid parse tree indicating a blank line.) 1961556Srgrimes */ 1971556Srgrimes 1981556Srgrimesunion node * 19990111Simpparsecmd(int interact) 20017987Speter{ 2011556Srgrimes int t; 2021556Srgrimes 203206145Sjilles /* This assumes the parser is not re-entered, 204206145Sjilles * which could happen if we add command substitution on PS1/PS2. 205206145Sjilles */ 206206145Sjilles parser_temp_free_all(); 207208656Sjilles heredoclist = NULL; 208206145Sjilles 20960593Scracauer tokpushback = 0; 2101556Srgrimes doprompt = interact; 2111556Srgrimes if (doprompt) 2121556Srgrimes setprompt(1); 2131556Srgrimes else 2141556Srgrimes setprompt(0); 2151556Srgrimes needprompt = 0; 2161556Srgrimes t = readtoken(); 2171556Srgrimes if (t == TEOF) 2181556Srgrimes return NEOF; 2191556Srgrimes if (t == TNL) 2201556Srgrimes return NULL; 2211556Srgrimes tokpushback++; 2221556Srgrimes return list(1); 2231556Srgrimes} 2241556Srgrimes 2251556Srgrimes 226213811Sobrienstatic union node * 22790111Simplist(int nlflag) 22817987Speter{ 2291556Srgrimes union node *n1, *n2, *n3; 23017987Speter int tok; 2311556Srgrimes 2321556Srgrimes checkkwd = 2; 2331556Srgrimes if (nlflag == 0 && tokendlist[peektoken()]) 2341556Srgrimes return NULL; 23517987Speter n1 = NULL; 2361556Srgrimes for (;;) { 23717987Speter n2 = andor(); 23817987Speter tok = readtoken(); 23917987Speter if (tok == TBACKGND) { 24017987Speter if (n2->type == NCMD || n2->type == NPIPE) { 24117987Speter n2->ncmd.backgnd = 1; 24217987Speter } else if (n2->type == NREDIR) { 24317987Speter n2->type = NBACKGND; 24417987Speter } else { 24517987Speter n3 = (union node *)stalloc(sizeof (struct nredir)); 24617987Speter n3->type = NBACKGND; 24717987Speter n3->nredir.n = n2; 24817987Speter n3->nredir.redirect = NULL; 24917987Speter n2 = n3; 25017987Speter } 25117987Speter } 25217987Speter if (n1 == NULL) { 25317987Speter n1 = n2; 25417987Speter } 25517987Speter else { 25617987Speter n3 = (union node *)stalloc(sizeof (struct nbinary)); 25717987Speter n3->type = NSEMI; 25817987Speter n3->nbinary.ch1 = n1; 25917987Speter n3->nbinary.ch2 = n2; 26017987Speter n1 = n3; 26117987Speter } 26217987Speter switch (tok) { 26313882Sjoerg case TBACKGND: 26417987Speter case TSEMI: 26517987Speter tok = readtoken(); 266102410Scharnier /* FALLTHROUGH */ 2671556Srgrimes case TNL: 26817987Speter if (tok == TNL) { 26917987Speter parseheredoc(); 27017987Speter if (nlflag) 27117987Speter return n1; 272210488Sjilles } else if (tok == TEOF && nlflag) { 273210488Sjilles parseheredoc(); 274210488Sjilles return n1; 27517987Speter } else { 27617987Speter tokpushback++; 27717987Speter } 2781556Srgrimes checkkwd = 2; 2791556Srgrimes if (tokendlist[peektoken()]) 2801556Srgrimes return n1; 2811556Srgrimes break; 2821556Srgrimes case TEOF: 2831556Srgrimes if (heredoclist) 2841556Srgrimes parseheredoc(); 2851556Srgrimes else 2861556Srgrimes pungetc(); /* push back EOF on input */ 2871556Srgrimes return n1; 2881556Srgrimes default: 2891556Srgrimes if (nlflag) 2901556Srgrimes synexpect(-1); 2911556Srgrimes tokpushback++; 2921556Srgrimes return n1; 2931556Srgrimes } 2941556Srgrimes } 2951556Srgrimes} 2961556Srgrimes 2971556Srgrimes 2981556Srgrimes 299213811Sobrienstatic union node * 30090111Simpandor(void) 30190111Simp{ 3021556Srgrimes union node *n1, *n2, *n3; 3031556Srgrimes int t; 3041556Srgrimes 3051556Srgrimes n1 = pipeline(); 3061556Srgrimes for (;;) { 3071556Srgrimes if ((t = readtoken()) == TAND) { 3081556Srgrimes t = NAND; 3091556Srgrimes } else if (t == TOR) { 3101556Srgrimes t = NOR; 3111556Srgrimes } else { 3121556Srgrimes tokpushback++; 3131556Srgrimes return n1; 3141556Srgrimes } 3151556Srgrimes n2 = pipeline(); 3161556Srgrimes n3 = (union node *)stalloc(sizeof (struct nbinary)); 3171556Srgrimes n3->type = t; 3181556Srgrimes n3->nbinary.ch1 = n1; 3191556Srgrimes n3->nbinary.ch2 = n2; 3201556Srgrimes n1 = n3; 3211556Srgrimes } 3221556Srgrimes} 3231556Srgrimes 3241556Srgrimes 3251556Srgrimes 326213811Sobrienstatic union node * 32790111Simppipeline(void) 32890111Simp{ 32975336Sbrian union node *n1, *n2, *pipenode; 3301556Srgrimes struct nodelist *lp, *prev; 33175336Sbrian int negate; 3321556Srgrimes 33375336Sbrian negate = 0; 334191009Sstefanf checkkwd = 2; 3351556Srgrimes TRACE(("pipeline: entered\n")); 33675336Sbrian while (readtoken() == TNOT) 33775336Sbrian negate = !negate; 33875336Sbrian tokpushback++; 3391556Srgrimes n1 = command(); 3401556Srgrimes if (readtoken() == TPIPE) { 3411556Srgrimes pipenode = (union node *)stalloc(sizeof (struct npipe)); 3421556Srgrimes pipenode->type = NPIPE; 3431556Srgrimes pipenode->npipe.backgnd = 0; 3441556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 3451556Srgrimes pipenode->npipe.cmdlist = lp; 3461556Srgrimes lp->n = n1; 3471556Srgrimes do { 3481556Srgrimes prev = lp; 3491556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 3501556Srgrimes lp->n = command(); 3511556Srgrimes prev->next = lp; 3521556Srgrimes } while (readtoken() == TPIPE); 3531556Srgrimes lp->next = NULL; 3541556Srgrimes n1 = pipenode; 3551556Srgrimes } 3561556Srgrimes tokpushback++; 35775336Sbrian if (negate) { 35875336Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 35975336Sbrian n2->type = NNOT; 36075336Sbrian n2->nnot.com = n1; 36175336Sbrian return n2; 36275336Sbrian } else 36375336Sbrian return n1; 3641556Srgrimes} 3651556Srgrimes 3661556Srgrimes 3671556Srgrimes 368213811Sobrienstatic union node * 36990111Simpcommand(void) 37090111Simp{ 3711556Srgrimes union node *n1, *n2; 3721556Srgrimes union node *ap, **app; 3731556Srgrimes union node *cp, **cpp; 3741556Srgrimes union node *redir, **rpp; 37575160Sbrian int t, negate = 0; 3761556Srgrimes 3771556Srgrimes checkkwd = 2; 37817987Speter redir = NULL; 37917987Speter n1 = NULL; 3801556Srgrimes rpp = &redir; 38120425Ssteve 3821556Srgrimes /* Check for redirection which may precede command */ 3831556Srgrimes while (readtoken() == TREDIR) { 3841556Srgrimes *rpp = n2 = redirnode; 3851556Srgrimes rpp = &n2->nfile.next; 3861556Srgrimes parsefname(); 3871556Srgrimes } 3881556Srgrimes tokpushback++; 3891556Srgrimes 39075160Sbrian while (readtoken() == TNOT) { 39175160Sbrian TRACE(("command: TNOT recognized\n")); 39275160Sbrian negate = !negate; 39375160Sbrian } 39475160Sbrian tokpushback++; 39575160Sbrian 3961556Srgrimes switch (readtoken()) { 3971556Srgrimes case TIF: 3981556Srgrimes n1 = (union node *)stalloc(sizeof (struct nif)); 3991556Srgrimes n1->type = NIF; 400104554Stjr if ((n1->nif.test = list(0)) == NULL) 401104554Stjr synexpect(-1); 4021556Srgrimes if (readtoken() != TTHEN) 4031556Srgrimes synexpect(TTHEN); 4041556Srgrimes n1->nif.ifpart = list(0); 4051556Srgrimes n2 = n1; 4061556Srgrimes while (readtoken() == TELIF) { 4071556Srgrimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 4081556Srgrimes n2 = n2->nif.elsepart; 4091556Srgrimes n2->type = NIF; 410104554Stjr if ((n2->nif.test = list(0)) == NULL) 411104554Stjr synexpect(-1); 4121556Srgrimes if (readtoken() != TTHEN) 4131556Srgrimes synexpect(TTHEN); 4141556Srgrimes n2->nif.ifpart = list(0); 4151556Srgrimes } 4161556Srgrimes if (lasttoken == TELSE) 4171556Srgrimes n2->nif.elsepart = list(0); 4181556Srgrimes else { 4191556Srgrimes n2->nif.elsepart = NULL; 4201556Srgrimes tokpushback++; 4211556Srgrimes } 4221556Srgrimes if (readtoken() != TFI) 4231556Srgrimes synexpect(TFI); 4241556Srgrimes checkkwd = 1; 4251556Srgrimes break; 4261556Srgrimes case TWHILE: 4271556Srgrimes case TUNTIL: { 4281556Srgrimes int got; 4291556Srgrimes n1 = (union node *)stalloc(sizeof (struct nbinary)); 4301556Srgrimes n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 431104554Stjr if ((n1->nbinary.ch1 = list(0)) == NULL) 432104554Stjr synexpect(-1); 4331556Srgrimes if ((got=readtoken()) != TDO) { 4341556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 4351556Srgrimes synexpect(TDO); 4361556Srgrimes } 4371556Srgrimes n1->nbinary.ch2 = list(0); 4381556Srgrimes if (readtoken() != TDONE) 4391556Srgrimes synexpect(TDONE); 4401556Srgrimes checkkwd = 1; 4411556Srgrimes break; 4421556Srgrimes } 4431556Srgrimes case TFOR: 4441556Srgrimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 4451556Srgrimes synerror("Bad for loop variable"); 4461556Srgrimes n1 = (union node *)stalloc(sizeof (struct nfor)); 4471556Srgrimes n1->type = NFOR; 4481556Srgrimes n1->nfor.var = wordtext; 449199282Sjilles while (readtoken() == TNL) 450199282Sjilles ; 451199282Sjilles if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) { 4521556Srgrimes app = ≈ 4531556Srgrimes while (readtoken() == TWORD) { 4541556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 4551556Srgrimes n2->type = NARG; 4561556Srgrimes n2->narg.text = wordtext; 4571556Srgrimes n2->narg.backquote = backquotelist; 4581556Srgrimes *app = n2; 4591556Srgrimes app = &n2->narg.next; 4601556Srgrimes } 4611556Srgrimes *app = NULL; 4621556Srgrimes n1->nfor.args = ap; 4631556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 4641556Srgrimes synexpect(-1); 4651556Srgrimes } else { 466149096Sstefanf static char argvars[5] = { 467149096Sstefanf CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' 468149096Sstefanf }; 4691556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 4701556Srgrimes n2->type = NARG; 471149096Sstefanf n2->narg.text = argvars; 4721556Srgrimes n2->narg.backquote = NULL; 4731556Srgrimes n2->narg.next = NULL; 4741556Srgrimes n1->nfor.args = n2; 4751556Srgrimes /* 4761556Srgrimes * Newline or semicolon here is optional (but note 4771556Srgrimes * that the original Bourne shell only allowed NL). 4781556Srgrimes */ 4791556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 4801556Srgrimes tokpushback++; 4811556Srgrimes } 4821556Srgrimes checkkwd = 2; 4831556Srgrimes if ((t = readtoken()) == TDO) 4841556Srgrimes t = TDONE; 4851556Srgrimes else if (t == TBEGIN) 4861556Srgrimes t = TEND; 4871556Srgrimes else 4881556Srgrimes synexpect(-1); 4891556Srgrimes n1->nfor.body = list(0); 4901556Srgrimes if (readtoken() != t) 4911556Srgrimes synexpect(t); 4921556Srgrimes checkkwd = 1; 4931556Srgrimes break; 4941556Srgrimes case TCASE: 4951556Srgrimes n1 = (union node *)stalloc(sizeof (struct ncase)); 4961556Srgrimes n1->type = NCASE; 4971556Srgrimes if (readtoken() != TWORD) 4981556Srgrimes synexpect(TWORD); 4991556Srgrimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 5001556Srgrimes n2->type = NARG; 5011556Srgrimes n2->narg.text = wordtext; 5021556Srgrimes n2->narg.backquote = backquotelist; 5031556Srgrimes n2->narg.next = NULL; 5041556Srgrimes while (readtoken() == TNL); 5051556Srgrimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 5061556Srgrimes synerror("expecting \"in\""); 5071556Srgrimes cpp = &n1->ncase.cases; 50818018Speter noaliases = 1; /* turn off alias expansion */ 5092760Ssef checkkwd = 2, readtoken(); 510104202Stjr while (lasttoken != TESAC) { 5111556Srgrimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 5121556Srgrimes cp->type = NCLIST; 5131556Srgrimes app = &cp->nclist.pattern; 514104207Stjr if (lasttoken == TLP) 515104207Stjr readtoken(); 5161556Srgrimes for (;;) { 5171556Srgrimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 5181556Srgrimes ap->type = NARG; 5191556Srgrimes ap->narg.text = wordtext; 5201556Srgrimes ap->narg.backquote = backquotelist; 5212760Ssef if (checkkwd = 2, readtoken() != TPIPE) 5221556Srgrimes break; 5231556Srgrimes app = &ap->narg.next; 5242760Ssef readtoken(); 5251556Srgrimes } 5261556Srgrimes ap->narg.next = NULL; 5271556Srgrimes if (lasttoken != TRP) 52818018Speter noaliases = 0, synexpect(TRP); 5291556Srgrimes cp->nclist.body = list(0); 5302760Ssef 5312760Ssef checkkwd = 2; 5322760Ssef if ((t = readtoken()) != TESAC) { 5332760Ssef if (t != TENDCASE) 53418018Speter noaliases = 0, synexpect(TENDCASE); 5352760Ssef else 5362760Ssef checkkwd = 2, readtoken(); 5372760Ssef } 5381556Srgrimes cpp = &cp->nclist.next; 539104202Stjr } 54018018Speter noaliases = 0; /* reset alias expansion */ 5411556Srgrimes *cpp = NULL; 5421556Srgrimes checkkwd = 1; 5431556Srgrimes break; 5441556Srgrimes case TLP: 5451556Srgrimes n1 = (union node *)stalloc(sizeof (struct nredir)); 5461556Srgrimes n1->type = NSUBSHELL; 5471556Srgrimes n1->nredir.n = list(0); 5481556Srgrimes n1->nredir.redirect = NULL; 5491556Srgrimes if (readtoken() != TRP) 5501556Srgrimes synexpect(TRP); 5511556Srgrimes checkkwd = 1; 5521556Srgrimes break; 5531556Srgrimes case TBEGIN: 5541556Srgrimes n1 = list(0); 5551556Srgrimes if (readtoken() != TEND) 5561556Srgrimes synexpect(TEND); 5571556Srgrimes checkkwd = 1; 5581556Srgrimes break; 5591556Srgrimes /* Handle an empty command like other simple commands. */ 560210221Sjilles case TBACKGND: 56117987Speter case TSEMI: 562101662Stjr case TAND: 563101662Stjr case TOR: 56417987Speter /* 56517987Speter * An empty command before a ; doesn't make much sense, and 56617987Speter * should certainly be disallowed in the case of `if ;'. 56717987Speter */ 56817987Speter if (!redir) 56917987Speter synexpect(-1); 5701556Srgrimes case TNL: 57110399Sjoerg case TEOF: 5721556Srgrimes case TWORD: 57317987Speter case TRP: 5741556Srgrimes tokpushback++; 57575160Sbrian n1 = simplecmd(rpp, redir); 57675160Sbrian goto checkneg; 5771556Srgrimes default: 5781556Srgrimes synexpect(-1); 5791556Srgrimes } 5801556Srgrimes 5811556Srgrimes /* Now check for redirection which may follow command */ 5821556Srgrimes while (readtoken() == TREDIR) { 5831556Srgrimes *rpp = n2 = redirnode; 5841556Srgrimes rpp = &n2->nfile.next; 5851556Srgrimes parsefname(); 5861556Srgrimes } 5871556Srgrimes tokpushback++; 5881556Srgrimes *rpp = NULL; 5891556Srgrimes if (redir) { 5901556Srgrimes if (n1->type != NSUBSHELL) { 5911556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 5921556Srgrimes n2->type = NREDIR; 5931556Srgrimes n2->nredir.n = n1; 5941556Srgrimes n1 = n2; 5951556Srgrimes } 5961556Srgrimes n1->nredir.redirect = redir; 5971556Srgrimes } 59875160Sbrian 59975160Sbriancheckneg: 60075160Sbrian if (negate) { 60175160Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 60275160Sbrian n2->type = NNOT; 60375160Sbrian n2->nnot.com = n1; 60475160Sbrian return n2; 60575160Sbrian } 60675160Sbrian else 60775160Sbrian return n1; 6081556Srgrimes} 6091556Srgrimes 6101556Srgrimes 611213811Sobrienstatic union node * 61290111Simpsimplecmd(union node **rpp, union node *redir) 61390111Simp{ 6141556Srgrimes union node *args, **app; 6151556Srgrimes union node **orig_rpp = rpp; 616210087Sjilles union node *n = NULL; 6171556Srgrimes 6181556Srgrimes /* If we don't have any redirections already, then we must reset */ 6191556Srgrimes /* rpp to be the address of the local redir variable. */ 6201556Srgrimes if (redir == 0) 6211556Srgrimes rpp = &redir; 6221556Srgrimes 6231556Srgrimes args = NULL; 6241556Srgrimes app = &args; 6258855Srgrimes /* 6261556Srgrimes * We save the incoming value, because we need this for shell 6271556Srgrimes * functions. There can not be a redirect or an argument between 6288855Srgrimes * the function name and the open parenthesis. 6291556Srgrimes */ 6301556Srgrimes orig_rpp = rpp; 6311556Srgrimes 6321556Srgrimes for (;;) { 6331556Srgrimes if (readtoken() == TWORD) { 6341556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 6351556Srgrimes n->type = NARG; 6361556Srgrimes n->narg.text = wordtext; 6371556Srgrimes n->narg.backquote = backquotelist; 6381556Srgrimes *app = n; 6391556Srgrimes app = &n->narg.next; 6401556Srgrimes } else if (lasttoken == TREDIR) { 6411556Srgrimes *rpp = n = redirnode; 6421556Srgrimes rpp = &n->nfile.next; 6431556Srgrimes parsefname(); /* read name of redirection file */ 6441556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 6451556Srgrimes && rpp == orig_rpp) { 6461556Srgrimes /* We have a function */ 6471556Srgrimes if (readtoken() != TRP) 6481556Srgrimes synexpect(TRP); 649179022Sstefanf funclinno = plinno; 6501556Srgrimes#ifdef notdef 6511556Srgrimes if (! goodname(n->narg.text)) 6521556Srgrimes synerror("Bad function name"); 6531556Srgrimes#endif 6541556Srgrimes n->type = NDEFUN; 6551556Srgrimes n->narg.next = command(); 656179022Sstefanf funclinno = 0; 657210087Sjilles return n; 6581556Srgrimes } else { 6591556Srgrimes tokpushback++; 6601556Srgrimes break; 6611556Srgrimes } 6621556Srgrimes } 6631556Srgrimes *app = NULL; 6641556Srgrimes *rpp = NULL; 6651556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 6661556Srgrimes n->type = NCMD; 6671556Srgrimes n->ncmd.backgnd = 0; 6681556Srgrimes n->ncmd.args = args; 6691556Srgrimes n->ncmd.redirect = redir; 670210087Sjilles return n; 6711556Srgrimes} 6721556Srgrimes 673213811Sobrienstatic union node * 67490111Simpmakename(void) 67590111Simp{ 67617987Speter union node *n; 6771556Srgrimes 67817987Speter n = (union node *)stalloc(sizeof (struct narg)); 67917987Speter n->type = NARG; 68017987Speter n->narg.next = NULL; 68117987Speter n->narg.text = wordtext; 68217987Speter n->narg.backquote = backquotelist; 68317987Speter return n; 68417987Speter} 68517987Speter 686213760Sobrienvoid 687213760Sobrienfixredir(union node *n, const char *text, int err) 68890111Simp{ 68917987Speter TRACE(("Fix redir %s %d\n", text, err)); 69017987Speter if (!err) 69117987Speter n->ndup.vname = NULL; 69217987Speter 69317987Speter if (is_digit(text[0]) && text[1] == '\0') 69417987Speter n->ndup.dupfd = digit_val(text[0]); 69517987Speter else if (text[0] == '-' && text[1] == '\0') 69617987Speter n->ndup.dupfd = -1; 69717987Speter else { 69820425Ssteve 69917987Speter if (err) 70017987Speter synerror("Bad fd number"); 70117987Speter else 70217987Speter n->ndup.vname = makename(); 70317987Speter } 70417987Speter} 70517987Speter 70617987Speter 707213811Sobrienstatic void 70890111Simpparsefname(void) 70990111Simp{ 7101556Srgrimes union node *n = redirnode; 7111556Srgrimes 7121556Srgrimes if (readtoken() != TWORD) 7131556Srgrimes synexpect(-1); 7141556Srgrimes if (n->type == NHERE) { 7151556Srgrimes struct heredoc *here = heredoc; 7161556Srgrimes struct heredoc *p; 7171556Srgrimes int i; 7181556Srgrimes 7191556Srgrimes if (quoteflag == 0) 7201556Srgrimes n->type = NXHERE; 7211556Srgrimes TRACE(("Here document %d\n", n->type)); 7221556Srgrimes if (here->striptabs) { 7231556Srgrimes while (*wordtext == '\t') 7241556Srgrimes wordtext++; 7251556Srgrimes } 7261556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 7271556Srgrimes synerror("Illegal eof marker for << redirection"); 7281556Srgrimes rmescapes(wordtext); 7291556Srgrimes here->eofmark = wordtext; 7301556Srgrimes here->next = NULL; 7311556Srgrimes if (heredoclist == NULL) 7321556Srgrimes heredoclist = here; 7331556Srgrimes else { 7341556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 7351556Srgrimes p->next = here; 7361556Srgrimes } 7371556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 73817987Speter fixredir(n, wordtext, 0); 7391556Srgrimes } else { 74017987Speter n->nfile.fname = makename(); 7411556Srgrimes } 7421556Srgrimes} 7431556Srgrimes 7441556Srgrimes 7451556Srgrimes/* 7461556Srgrimes * Input any here documents. 7471556Srgrimes */ 7481556Srgrimes 749213811Sobrienstatic void 75090111Simpparseheredoc(void) 75190111Simp{ 7521556Srgrimes struct heredoc *here; 7531556Srgrimes union node *n; 7541556Srgrimes 7551556Srgrimes while (heredoclist) { 7561556Srgrimes here = heredoclist; 7571556Srgrimes heredoclist = here->next; 7581556Srgrimes if (needprompt) { 7591556Srgrimes setprompt(2); 7601556Srgrimes needprompt = 0; 7611556Srgrimes } 7621556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 7631556Srgrimes here->eofmark, here->striptabs); 7641556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 7651556Srgrimes n->narg.type = NARG; 7661556Srgrimes n->narg.next = NULL; 7671556Srgrimes n->narg.text = wordtext; 7681556Srgrimes n->narg.backquote = backquotelist; 7691556Srgrimes here->here->nhere.doc = n; 7701556Srgrimes } 7711556Srgrimes} 7721556Srgrimes 773213811Sobrienstatic int 77490111Simppeektoken(void) 77590111Simp{ 7761556Srgrimes int t; 7771556Srgrimes 7781556Srgrimes t = readtoken(); 7791556Srgrimes tokpushback++; 7801556Srgrimes return (t); 7811556Srgrimes} 7821556Srgrimes 783213811Sobrienstatic int 78490111Simpreadtoken(void) 78590111Simp{ 7861556Srgrimes int t; 7871556Srgrimes int savecheckkwd = checkkwd; 7881556Srgrimes struct alias *ap; 7891556Srgrimes#ifdef DEBUG 7901556Srgrimes int alreadyseen = tokpushback; 7911556Srgrimes#endif 7928855Srgrimes 7931556Srgrimes top: 7941556Srgrimes t = xxreadtoken(); 7951556Srgrimes 7961556Srgrimes if (checkkwd) { 7971556Srgrimes /* 7981556Srgrimes * eat newlines 7991556Srgrimes */ 8001556Srgrimes if (checkkwd == 2) { 8011556Srgrimes checkkwd = 0; 8021556Srgrimes while (t == TNL) { 8031556Srgrimes parseheredoc(); 8041556Srgrimes t = xxreadtoken(); 8051556Srgrimes } 8061556Srgrimes } else 8071556Srgrimes checkkwd = 0; 8081556Srgrimes /* 8091556Srgrimes * check for keywords and aliases 8101556Srgrimes */ 81120425Ssteve if (t == TWORD && !quoteflag) 81217987Speter { 81398463Sjmallett const char * const *pp; 8141556Srgrimes 81598463Sjmallett for (pp = parsekwd; *pp; pp++) { 81620425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 81717987Speter { 8181556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 8191556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 8201556Srgrimes goto out; 8211556Srgrimes } 8221556Srgrimes } 82318018Speter if (noaliases == 0 && 82418018Speter (ap = lookupalias(wordtext, 1)) != NULL) { 8251556Srgrimes pushstring(ap->val, strlen(ap->val), ap); 8261556Srgrimes checkkwd = savecheckkwd; 8271556Srgrimes goto top; 8281556Srgrimes } 8291556Srgrimes } 8301556Srgrimesout: 83175160Sbrian checkkwd = (t == TNOT) ? savecheckkwd : 0; 8321556Srgrimes } 8331556Srgrimes#ifdef DEBUG 8341556Srgrimes if (!alreadyseen) 8351556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8361556Srgrimes else 8371556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 8381556Srgrimes#endif 8391556Srgrimes return (t); 8401556Srgrimes} 8411556Srgrimes 8421556Srgrimes 8431556Srgrimes/* 8441556Srgrimes * Read the next input token. 8451556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 8461556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 8471556Srgrimes * quoted. 8481556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 8491556Srgrimes * the redirection. 8501556Srgrimes * In all cases, the variable startlinno is set to the number of the line 8511556Srgrimes * on which the token starts. 8521556Srgrimes * 8531556Srgrimes * [Change comment: here documents and internal procedures] 8541556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 8551556Srgrimes * word parsing code into a separate routine. In this case, readtoken 8561556Srgrimes * doesn't need to have any internal procedures, but parseword does. 8571556Srgrimes * We could also make parseoperator in essence the main routine, and 8581556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 8591556Srgrimes */ 8601556Srgrimes 8611556Srgrimes#define RETURN(token) return lasttoken = token 8621556Srgrimes 863213811Sobrienstatic int 86490111Simpxxreadtoken(void) 86590111Simp{ 86625230Ssteve int c; 8671556Srgrimes 8681556Srgrimes if (tokpushback) { 8691556Srgrimes tokpushback = 0; 8701556Srgrimes return lasttoken; 8711556Srgrimes } 8721556Srgrimes if (needprompt) { 8731556Srgrimes setprompt(2); 8741556Srgrimes needprompt = 0; 8751556Srgrimes } 8761556Srgrimes startlinno = plinno; 8771556Srgrimes for (;;) { /* until token or start of word found */ 8781556Srgrimes c = pgetc_macro(); 8791556Srgrimes if (c == ' ' || c == '\t') 8801556Srgrimes continue; /* quick check for white space first */ 8811556Srgrimes switch (c) { 8821556Srgrimes case ' ': case '\t': 8831556Srgrimes continue; 8841556Srgrimes case '#': 8851556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 8861556Srgrimes pungetc(); 8871556Srgrimes continue; 8881556Srgrimes case '\\': 8891556Srgrimes if (pgetc() == '\n') { 8901556Srgrimes startlinno = ++plinno; 8911556Srgrimes if (doprompt) 8921556Srgrimes setprompt(2); 8931556Srgrimes else 8941556Srgrimes setprompt(0); 8951556Srgrimes continue; 8961556Srgrimes } 8971556Srgrimes pungetc(); 8981556Srgrimes goto breakloop; 8991556Srgrimes case '\n': 9001556Srgrimes plinno++; 9011556Srgrimes needprompt = doprompt; 9021556Srgrimes RETURN(TNL); 9031556Srgrimes case PEOF: 9041556Srgrimes RETURN(TEOF); 9051556Srgrimes case '&': 9061556Srgrimes if (pgetc() == '&') 9071556Srgrimes RETURN(TAND); 9081556Srgrimes pungetc(); 9091556Srgrimes RETURN(TBACKGND); 9101556Srgrimes case '|': 9111556Srgrimes if (pgetc() == '|') 9121556Srgrimes RETURN(TOR); 9131556Srgrimes pungetc(); 9141556Srgrimes RETURN(TPIPE); 9151556Srgrimes case ';': 9161556Srgrimes if (pgetc() == ';') 9171556Srgrimes RETURN(TENDCASE); 9181556Srgrimes pungetc(); 9191556Srgrimes RETURN(TSEMI); 9201556Srgrimes case '(': 9211556Srgrimes RETURN(TLP); 9221556Srgrimes case ')': 9231556Srgrimes RETURN(TRP); 9241556Srgrimes default: 9251556Srgrimes goto breakloop; 9261556Srgrimes } 9271556Srgrimes } 9281556Srgrimesbreakloop: 9291556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 9301556Srgrimes#undef RETURN 9311556Srgrimes} 9321556Srgrimes 9331556Srgrimes 934213811Sobrien#define MAXNEST_static 8 935206145Sjillesstruct tokenstate 936206145Sjilles{ 937206145Sjilles const char *syntax; /* *SYNTAX */ 938206145Sjilles int parenlevel; /* levels of parentheses in arithmetic */ 939206145Sjilles enum tokenstate_category 940206145Sjilles { 941206145Sjilles TSTATE_TOP, 942206145Sjilles TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */ 943206145Sjilles TSTATE_VAR_NEW, /* other ${var...}, own dquote state */ 944206145Sjilles TSTATE_ARITH 945206145Sjilles } category; 946206145Sjilles}; 947206145Sjilles 948206145Sjilles 949205130Sjilles/* 950205130Sjilles * Called to parse command substitutions. 951205130Sjilles */ 9521556Srgrimes 953213811Sobrienstatic char * 954205130Sjillesparsebackq(char *out, struct nodelist **pbqlist, 955205130Sjilles int oldstyle, int dblquote, int quoted) 956205130Sjilles{ 957205130Sjilles struct nodelist **nlpp; 958205130Sjilles union node *n; 959205130Sjilles char *volatile str; 960205130Sjilles struct jmploc jmploc; 961205130Sjilles struct jmploc *const savehandler = handler; 962205130Sjilles int savelen; 963205130Sjilles int saveprompt; 964205130Sjilles const int bq_startlinno = plinno; 965205130Sjilles char *volatile ostr = NULL; 966205130Sjilles struct parsefile *const savetopfile = getcurrentfile(); 967208655Sjilles struct heredoc *const saveheredoclist = heredoclist; 968208655Sjilles struct heredoc *here; 969205130Sjilles 970205130Sjilles str = NULL; 971205130Sjilles if (setjmp(jmploc.loc)) { 972205130Sjilles popfilesupto(savetopfile); 973205130Sjilles if (str) 974205130Sjilles ckfree(str); 975205130Sjilles if (ostr) 976205130Sjilles ckfree(ostr); 977208655Sjilles heredoclist = saveheredoclist; 978205130Sjilles handler = savehandler; 979205130Sjilles if (exception == EXERROR) { 980205130Sjilles startlinno = bq_startlinno; 981205130Sjilles synerror("Error in command substitution"); 982205130Sjilles } 983205130Sjilles longjmp(handler->loc, 1); 984205130Sjilles } 985205130Sjilles INTOFF; 986205130Sjilles savelen = out - stackblock(); 987205130Sjilles if (savelen > 0) { 988205130Sjilles str = ckmalloc(savelen); 989205130Sjilles memcpy(str, stackblock(), savelen); 990205130Sjilles } 991205130Sjilles handler = &jmploc; 992208655Sjilles heredoclist = NULL; 993205130Sjilles INTON; 994205130Sjilles if (oldstyle) { 995205130Sjilles /* We must read until the closing backquote, giving special 996205130Sjilles treatment to some slashes, and then push the string and 997205130Sjilles reread it as input, interpreting it normally. */ 998205130Sjilles char *oout; 999205130Sjilles int c; 1000205130Sjilles int olen; 1001205130Sjilles 1002205130Sjilles 1003205130Sjilles STARTSTACKSTR(oout); 1004205130Sjilles for (;;) { 1005205130Sjilles if (needprompt) { 1006205130Sjilles setprompt(2); 1007205130Sjilles needprompt = 0; 1008205130Sjilles } 1009205130Sjilles switch (c = pgetc()) { 1010205130Sjilles case '`': 1011205130Sjilles goto done; 1012205130Sjilles 1013205130Sjilles case '\\': 1014205130Sjilles if ((c = pgetc()) == '\n') { 1015205130Sjilles plinno++; 1016205130Sjilles if (doprompt) 1017205130Sjilles setprompt(2); 1018205130Sjilles else 1019205130Sjilles setprompt(0); 1020205130Sjilles /* 1021205130Sjilles * If eating a newline, avoid putting 1022205130Sjilles * the newline into the new character 1023205130Sjilles * stream (via the STPUTC after the 1024205130Sjilles * switch). 1025205130Sjilles */ 1026205130Sjilles continue; 1027205130Sjilles } 1028205130Sjilles if (c != '\\' && c != '`' && c != '$' 1029205130Sjilles && (!dblquote || c != '"')) 1030205130Sjilles STPUTC('\\', oout); 1031205130Sjilles break; 1032205130Sjilles 1033205130Sjilles case '\n': 1034205130Sjilles plinno++; 1035205130Sjilles needprompt = doprompt; 1036205130Sjilles break; 1037205130Sjilles 1038205130Sjilles case PEOF: 1039205130Sjilles startlinno = plinno; 1040205130Sjilles synerror("EOF in backquote substitution"); 1041205130Sjilles break; 1042205130Sjilles 1043205130Sjilles default: 1044205130Sjilles break; 1045205130Sjilles } 1046205130Sjilles STPUTC(c, oout); 1047205130Sjilles } 1048205130Sjillesdone: 1049205130Sjilles STPUTC('\0', oout); 1050205130Sjilles olen = oout - stackblock(); 1051205130Sjilles INTOFF; 1052205130Sjilles ostr = ckmalloc(olen); 1053205130Sjilles memcpy(ostr, stackblock(), olen); 1054205130Sjilles setinputstring(ostr, 1); 1055205130Sjilles INTON; 1056205130Sjilles } 1057205130Sjilles nlpp = pbqlist; 1058205130Sjilles while (*nlpp) 1059205130Sjilles nlpp = &(*nlpp)->next; 1060205130Sjilles *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 1061205130Sjilles (*nlpp)->next = NULL; 1062205130Sjilles 1063205130Sjilles if (oldstyle) { 1064205130Sjilles saveprompt = doprompt; 1065205130Sjilles doprompt = 0; 1066205130Sjilles } 1067205130Sjilles 1068205130Sjilles n = list(0); 1069205130Sjilles 1070205130Sjilles if (oldstyle) 1071205130Sjilles doprompt = saveprompt; 1072205130Sjilles else { 1073205130Sjilles if (readtoken() != TRP) 1074205130Sjilles synexpect(TRP); 1075205130Sjilles } 1076205130Sjilles 1077205130Sjilles (*nlpp)->n = n; 1078205130Sjilles if (oldstyle) { 1079205130Sjilles /* 1080205130Sjilles * Start reading from old file again, ignoring any pushed back 1081205130Sjilles * tokens left from the backquote parsing 1082205130Sjilles */ 1083205130Sjilles popfile(); 1084205130Sjilles tokpushback = 0; 1085205130Sjilles } 1086205130Sjilles while (stackblocksize() <= savelen) 1087205130Sjilles growstackblock(); 1088205130Sjilles STARTSTACKSTR(out); 1089208655Sjilles INTOFF; 1090205130Sjilles if (str) { 1091205130Sjilles memcpy(out, str, savelen); 1092205130Sjilles STADJUST(savelen, out); 1093205130Sjilles ckfree(str); 1094205130Sjilles str = NULL; 1095205130Sjilles } 1096205130Sjilles if (ostr) { 1097205130Sjilles ckfree(ostr); 1098205130Sjilles ostr = NULL; 1099205130Sjilles } 1100208655Sjilles here = saveheredoclist; 1101208655Sjilles if (here != NULL) { 1102208655Sjilles while (here->next != NULL) 1103208655Sjilles here = here->next; 1104208655Sjilles here->next = heredoclist; 1105208655Sjilles heredoclist = saveheredoclist; 1106208655Sjilles } 1107205130Sjilles handler = savehandler; 1108208655Sjilles INTON; 1109205130Sjilles if (quoted) 1110205130Sjilles USTPUTC(CTLBACKQ | CTLQUOTE, out); 1111205130Sjilles else 1112205130Sjilles USTPUTC(CTLBACKQ, out); 1113205130Sjilles return out; 1114205130Sjilles} 1115205130Sjilles 1116205130Sjilles 11171556Srgrimes/* 11181556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 11191556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 11201556Srgrimes * word which marks the end of the document and striptabs is true if 11211556Srgrimes * leading tabs should be stripped from the document. The argument firstc 11221556Srgrimes * is the first character of the input token or document. 11231556Srgrimes * 11241556Srgrimes * Because C does not have internal subroutines, I have simulated them 11251556Srgrimes * using goto's to implement the subroutine linkage. The following macros 11261556Srgrimes * will run code that appears at the end of readtoken1. 11271556Srgrimes */ 11281556Srgrimes 11291556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 11301556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 11311556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 11321556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 11331556Srgrimes 1134213811Sobrienstatic int 1135206145Sjillesreadtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs) 113690111Simp{ 113717987Speter int c = firstc; 113817987Speter char *out; 11391556Srgrimes int len; 11401556Srgrimes char line[EOFMARKLEN + 1]; 11411556Srgrimes struct nodelist *bqlist; 11421556Srgrimes int quotef; 1143206145Sjilles int newvarnest; 1144206145Sjilles int level; 114554679Scracauer int synentry; 1146213811Sobrien struct tokenstate state_static[MAXNEST_static]; 1147213811Sobrien int maxnest = MAXNEST_static; 1148206145Sjilles struct tokenstate *state = state_static; 11491556Srgrimes 11501556Srgrimes startlinno = plinno; 11511556Srgrimes quotef = 0; 11521556Srgrimes bqlist = NULL; 1153206145Sjilles newvarnest = 0; 1154206145Sjilles level = 0; 1155206145Sjilles state[level].syntax = initialsyntax; 1156206145Sjilles state[level].parenlevel = 0; 1157206145Sjilles state[level].category = TSTATE_TOP; 11581556Srgrimes 11591556Srgrimes STARTSTACKSTR(out); 11601556Srgrimes loop: { /* for each line, until end of word */ 11611556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 11621556Srgrimes for (;;) { /* until end of line or end of word */ 11631556Srgrimes CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 116454679Scracauer 1165206145Sjilles synentry = state[level].syntax[c]; 116654679Scracauer 116754679Scracauer switch(synentry) { 11681556Srgrimes case CNL: /* '\n' */ 1169206145Sjilles if (state[level].syntax == BASESYNTAX) 11701556Srgrimes goto endword; /* exit outer loop */ 11711556Srgrimes USTPUTC(c, out); 11721556Srgrimes plinno++; 11731556Srgrimes if (doprompt) 11741556Srgrimes setprompt(2); 11751556Srgrimes else 11761556Srgrimes setprompt(0); 11771556Srgrimes c = pgetc(); 11781556Srgrimes goto loop; /* continue outer loop */ 11791556Srgrimes case CWORD: 11801556Srgrimes USTPUTC(c, out); 11811556Srgrimes break; 11821556Srgrimes case CCTL: 1183206145Sjilles if (eofmark == NULL || initialsyntax != SQSYNTAX) 11841556Srgrimes USTPUTC(CTLESC, out); 11851556Srgrimes USTPUTC(c, out); 11861556Srgrimes break; 11871556Srgrimes case CBACK: /* backslash */ 11881556Srgrimes c = pgetc(); 11891556Srgrimes if (c == PEOF) { 11901556Srgrimes USTPUTC('\\', out); 11911556Srgrimes pungetc(); 11921556Srgrimes } else if (c == '\n') { 1193160849Syar plinno++; 11941556Srgrimes if (doprompt) 11951556Srgrimes setprompt(2); 11961556Srgrimes else 11971556Srgrimes setprompt(0); 11981556Srgrimes } else { 1199206145Sjilles if (state[level].syntax == DQSYNTAX && 1200206145Sjilles c != '\\' && c != '`' && c != '$' && 1201206145Sjilles (c != '"' || (eofmark != NULL && 1202206145Sjilles newvarnest == 0)) && 1203206145Sjilles (c != '}' || state[level].category != TSTATE_VAR_OLD)) 12041556Srgrimes USTPUTC('\\', out); 120583675Stegge if (SQSYNTAX[c] == CCTL) 12061556Srgrimes USTPUTC(CTLESC, out); 1207206145Sjilles else if (eofmark == NULL || 1208206145Sjilles newvarnest > 0) 120938887Stegge USTPUTC(CTLQUOTEMARK, out); 12101556Srgrimes USTPUTC(c, out); 12111556Srgrimes quotef++; 12121556Srgrimes } 12131556Srgrimes break; 12141556Srgrimes case CSQUOTE: 1215206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1216206145Sjilles state[level].syntax = SQSYNTAX; 12171556Srgrimes break; 12181556Srgrimes case CDQUOTE: 1219206145Sjilles USTPUTC(CTLQUOTEMARK, out); 1220206145Sjilles state[level].syntax = DQSYNTAX; 12211556Srgrimes break; 12221556Srgrimes case CENDQUOTE: 1223206145Sjilles if (eofmark != NULL && newvarnest == 0) 12241556Srgrimes USTPUTC(c, out); 1225206145Sjilles else { 1226206473Sjilles if (state[level].category == TSTATE_ARITH) 1227206473Sjilles state[level].syntax = ARISYNTAX; 1228206473Sjilles else 1229206473Sjilles state[level].syntax = BASESYNTAX; 12301556Srgrimes quotef++; 12311556Srgrimes } 12321556Srgrimes break; 12331556Srgrimes case CVAR: /* '$' */ 12341556Srgrimes PARSESUB(); /* parse substitution */ 12351556Srgrimes break; 12361556Srgrimes case CENDVAR: /* '}' */ 1237206145Sjilles if (level > 0 && 1238206145Sjilles (state[level].category == TSTATE_VAR_OLD || 1239206145Sjilles state[level].category == TSTATE_VAR_NEW)) { 1240206145Sjilles if (state[level].category == TSTATE_VAR_OLD) 1241206145Sjilles state[level - 1].syntax = state[level].syntax; 1242206145Sjilles else 1243206145Sjilles newvarnest--; 1244206145Sjilles level--; 12451556Srgrimes USTPUTC(CTLENDVAR, out); 12461556Srgrimes } else { 12471556Srgrimes USTPUTC(c, out); 12481556Srgrimes } 12491556Srgrimes break; 12501556Srgrimes case CLP: /* '(' in arithmetic */ 1251206145Sjilles state[level].parenlevel++; 12521556Srgrimes USTPUTC(c, out); 12531556Srgrimes break; 12541556Srgrimes case CRP: /* ')' in arithmetic */ 1255206145Sjilles if (state[level].parenlevel > 0) { 12561556Srgrimes USTPUTC(c, out); 1257206145Sjilles --state[level].parenlevel; 12581556Srgrimes } else { 12591556Srgrimes if (pgetc() == ')') { 1260206145Sjilles if (level > 0 && 1261206145Sjilles state[level].category == TSTATE_ARITH) { 1262206145Sjilles level--; 12631556Srgrimes USTPUTC(CTLENDARI, out); 12641556Srgrimes } else 12651556Srgrimes USTPUTC(')', out); 12661556Srgrimes } else { 12678855Srgrimes /* 12681556Srgrimes * unbalanced parens 12691556Srgrimes * (don't 2nd guess - no error) 12701556Srgrimes */ 12711556Srgrimes pungetc(); 12721556Srgrimes USTPUTC(')', out); 12731556Srgrimes } 12741556Srgrimes } 12751556Srgrimes break; 12761556Srgrimes case CBQUOTE: /* '`' */ 1277206145Sjilles out = parsebackq(out, &bqlist, 1, 1278206145Sjilles state[level].syntax == DQSYNTAX && 1279206145Sjilles (eofmark == NULL || newvarnest > 0), 1280206145Sjilles state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX); 12811556Srgrimes break; 12821556Srgrimes case CEOF: 12831556Srgrimes goto endword; /* exit outer loop */ 12841556Srgrimes default: 1285206145Sjilles if (level == 0) 12861556Srgrimes goto endword; /* exit outer loop */ 12871556Srgrimes USTPUTC(c, out); 12881556Srgrimes } 12891556Srgrimes c = pgetc_macro(); 12901556Srgrimes } 12911556Srgrimes } 12921556Srgrimesendword: 1293206145Sjilles if (state[level].syntax == ARISYNTAX) 12941556Srgrimes synerror("Missing '))'"); 1295206145Sjilles if (state[level].syntax != BASESYNTAX && eofmark == NULL) 12961556Srgrimes synerror("Unterminated quoted string"); 1297206145Sjilles if (state[level].category == TSTATE_VAR_OLD || 1298206145Sjilles state[level].category == TSTATE_VAR_NEW) { 12991556Srgrimes startlinno = plinno; 13001556Srgrimes synerror("Missing '}'"); 13011556Srgrimes } 1302206145Sjilles if (state != state_static) 1303206145Sjilles parser_temp_free_upto(state); 13041556Srgrimes USTPUTC('\0', out); 13051556Srgrimes len = out - stackblock(); 13061556Srgrimes out = stackblock(); 13071556Srgrimes if (eofmark == NULL) { 13081556Srgrimes if ((c == '>' || c == '<') 13091556Srgrimes && quotef == 0 13101556Srgrimes && len <= 2 13111556Srgrimes && (*out == '\0' || is_digit(*out))) { 13121556Srgrimes PARSEREDIR(); 13131556Srgrimes return lasttoken = TREDIR; 13141556Srgrimes } else { 13151556Srgrimes pungetc(); 13161556Srgrimes } 13171556Srgrimes } 13181556Srgrimes quoteflag = quotef; 13191556Srgrimes backquotelist = bqlist; 13201556Srgrimes grabstackblock(len); 13211556Srgrimes wordtext = out; 13221556Srgrimes return lasttoken = TWORD; 13231556Srgrimes/* end of readtoken routine */ 13241556Srgrimes 13251556Srgrimes 13261556Srgrimes/* 13271556Srgrimes * Check to see whether we are at the end of the here document. When this 13281556Srgrimes * is called, c is set to the first character of the next input line. If 13291556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 13301556Srgrimes */ 13311556Srgrimes 13321556Srgrimescheckend: { 13331556Srgrimes if (eofmark) { 13341556Srgrimes if (striptabs) { 13351556Srgrimes while (c == '\t') 13361556Srgrimes c = pgetc(); 13371556Srgrimes } 13381556Srgrimes if (c == *eofmark) { 13391556Srgrimes if (pfgets(line, sizeof line) != NULL) { 134025230Ssteve char *p, *q; 13411556Srgrimes 13421556Srgrimes p = line; 13431556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 13441556Srgrimes if (*p == '\n' && *q == '\0') { 13451556Srgrimes c = PEOF; 13461556Srgrimes plinno++; 13471556Srgrimes needprompt = doprompt; 13481556Srgrimes } else { 13491556Srgrimes pushstring(line, strlen(line), NULL); 13501556Srgrimes } 13511556Srgrimes } 13521556Srgrimes } 13531556Srgrimes } 13541556Srgrimes goto checkend_return; 13551556Srgrimes} 13561556Srgrimes 13571556Srgrimes 13581556Srgrimes/* 13591556Srgrimes * Parse a redirection operator. The variable "out" points to a string 13601556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 13611556Srgrimes * first character of the redirection operator. 13621556Srgrimes */ 13631556Srgrimes 13641556Srgrimesparseredir: { 13651556Srgrimes char fd = *out; 13661556Srgrimes union node *np; 13671556Srgrimes 13681556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 13691556Srgrimes if (c == '>') { 13701556Srgrimes np->nfile.fd = 1; 13711556Srgrimes c = pgetc(); 13721556Srgrimes if (c == '>') 13731556Srgrimes np->type = NAPPEND; 13741556Srgrimes else if (c == '&') 13751556Srgrimes np->type = NTOFD; 137696922Stjr else if (c == '|') 137796922Stjr np->type = NCLOBBER; 13781556Srgrimes else { 13791556Srgrimes np->type = NTO; 13801556Srgrimes pungetc(); 13811556Srgrimes } 13821556Srgrimes } else { /* c == '<' */ 13831556Srgrimes np->nfile.fd = 0; 13841556Srgrimes c = pgetc(); 13851556Srgrimes if (c == '<') { 13861556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 13871556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 13881556Srgrimes np->nfile.fd = 0; 13891556Srgrimes } 13901556Srgrimes np->type = NHERE; 13911556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 13921556Srgrimes heredoc->here = np; 13931556Srgrimes if ((c = pgetc()) == '-') { 13941556Srgrimes heredoc->striptabs = 1; 13951556Srgrimes } else { 13961556Srgrimes heredoc->striptabs = 0; 13971556Srgrimes pungetc(); 13981556Srgrimes } 13991556Srgrimes } else if (c == '&') 14001556Srgrimes np->type = NFROMFD; 140166612Sbrian else if (c == '>') 140266612Sbrian np->type = NFROMTO; 14031556Srgrimes else { 14041556Srgrimes np->type = NFROM; 14051556Srgrimes pungetc(); 14061556Srgrimes } 14071556Srgrimes } 14081556Srgrimes if (fd != '\0') 14091556Srgrimes np->nfile.fd = digit_val(fd); 14101556Srgrimes redirnode = np; 14111556Srgrimes goto parseredir_return; 14121556Srgrimes} 14131556Srgrimes 14141556Srgrimes 14151556Srgrimes/* 14161556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 14171556Srgrimes * and nothing else. 14181556Srgrimes */ 14191556Srgrimes 14201556Srgrimesparsesub: { 1421179022Sstefanf char buf[10]; 14221556Srgrimes int subtype; 14231556Srgrimes int typeloc; 14241556Srgrimes int flags; 14251556Srgrimes char *p; 14261556Srgrimes static const char types[] = "}-+?="; 1427179022Sstefanf int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 1428179022Sstefanf int i; 1429179022Sstefanf int linno; 1430179387Sstefanf int length; 14311556Srgrimes 14321556Srgrimes c = pgetc(); 1433149026Sstefanf if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) && 1434149026Sstefanf !is_special(c)) { 14351556Srgrimes USTPUTC('$', out); 14361556Srgrimes pungetc(); 14371556Srgrimes } else if (c == '(') { /* $(command) or $((arith)) */ 14381556Srgrimes if (pgetc() == '(') { 14391556Srgrimes PARSEARITH(); 14401556Srgrimes } else { 14411556Srgrimes pungetc(); 1442206145Sjilles out = parsebackq(out, &bqlist, 0, 1443206145Sjilles state[level].syntax == DQSYNTAX && 1444206145Sjilles (eofmark == NULL || newvarnest > 0), 1445206145Sjilles state[level].syntax == DQSYNTAX || 1446206145Sjilles state[level].syntax == ARISYNTAX); 14471556Srgrimes } 14481556Srgrimes } else { 14491556Srgrimes USTPUTC(CTLVAR, out); 14501556Srgrimes typeloc = out - stackblock(); 14511556Srgrimes USTPUTC(VSNORMAL, out); 14521556Srgrimes subtype = VSNORMAL; 1453179022Sstefanf flags = 0; 14541556Srgrimes if (c == '{') { 145518202Speter bracketed_name = 1; 14561556Srgrimes c = pgetc(); 145717987Speter if (c == '#') { 145817987Speter if ((c = pgetc()) == '}') 145917987Speter c = '#'; 146017987Speter else 146117987Speter subtype = VSLENGTH; 146217987Speter } 146317987Speter else 146417987Speter subtype = 0; 14651556Srgrimes } 1466149026Sstefanf if (!is_eof(c) && is_name(c)) { 1467179387Sstefanf length = 0; 14681556Srgrimes do { 14691556Srgrimes STPUTC(c, out); 14701556Srgrimes c = pgetc(); 1471179387Sstefanf length++; 1472149026Sstefanf } while (!is_eof(c) && is_in_name(c)); 1473179387Sstefanf if (length == 6 && 1474179387Sstefanf strncmp(out - length, "LINENO", length) == 0) { 1475179022Sstefanf /* Replace the variable name with the 1476179022Sstefanf * current line number. */ 1477179022Sstefanf linno = plinno; 1478179022Sstefanf if (funclinno != 0) 1479179022Sstefanf linno -= funclinno - 1; 1480179022Sstefanf snprintf(buf, sizeof(buf), "%d", linno); 1481179022Sstefanf STADJUST(-6, out); 1482179022Sstefanf for (i = 0; buf[i] != '\0'; i++) 1483179022Sstefanf STPUTC(buf[i], out); 1484179022Sstefanf flags |= VSLINENO; 1485179022Sstefanf } 148618202Speter } else if (is_digit(c)) { 148718202Speter if (bracketed_name) { 148818202Speter do { 148918202Speter STPUTC(c, out); 149018202Speter c = pgetc(); 149118202Speter } while (is_digit(c)); 149218202Speter } else { 149318202Speter STPUTC(c, out); 149418202Speter c = pgetc(); 149518202Speter } 14961556Srgrimes } else { 1497164003Sstefanf if (! is_special(c)) { 1498164003Sstefanf subtype = VSERROR; 1499164003Sstefanf if (c == '}') 1500164003Sstefanf pungetc(); 1501206144Sjilles else if (c == '\n' || c == PEOF) 1502206144Sjilles synerror("Unexpected end of line in substitution"); 1503164003Sstefanf else 1504164003Sstefanf USTPUTC(c, out); 1505164003Sstefanf } else { 1506164003Sstefanf USTPUTC(c, out); 1507164003Sstefanf c = pgetc(); 1508164003Sstefanf } 15091556Srgrimes } 15101556Srgrimes if (subtype == 0) { 151117987Speter switch (c) { 151217987Speter case ':': 1513179022Sstefanf flags |= VSNUL; 15141556Srgrimes c = pgetc(); 151517987Speter /*FALLTHROUGH*/ 151617987Speter default: 151717987Speter p = strchr(types, c); 1518164003Sstefanf if (p == NULL) { 1519206144Sjilles if (c == '\n' || c == PEOF) 1520206144Sjilles synerror("Unexpected end of line in substitution"); 1521164003Sstefanf if (flags == VSNUL) 1522164003Sstefanf STPUTC(':', out); 1523164003Sstefanf STPUTC(c, out); 1524164003Sstefanf subtype = VSERROR; 1525164003Sstefanf } else 1526164003Sstefanf subtype = p - types + VSNORMAL; 152717987Speter break; 152817987Speter case '%': 152920425Ssteve case '#': 153017987Speter { 153117987Speter int cc = c; 153217987Speter subtype = c == '#' ? VSTRIMLEFT : 153317987Speter VSTRIMRIGHT; 153417987Speter c = pgetc(); 153517987Speter if (c == cc) 153617987Speter subtype++; 153717987Speter else 153817987Speter pungetc(); 153917987Speter break; 154017987Speter } 15411556Srgrimes } 1542164003Sstefanf } else if (subtype != VSERROR) { 15431556Srgrimes pungetc(); 15441556Srgrimes } 1545164003Sstefanf STPUTC('=', out); 1546206145Sjilles if (subtype != VSLENGTH && (state[level].syntax == DQSYNTAX || 1547206145Sjilles state[level].syntax == ARISYNTAX)) 15481556Srgrimes flags |= VSQUOTE; 15491556Srgrimes *(stackblock() + typeloc) = subtype | flags; 1550206145Sjilles if (subtype != VSNORMAL) { 1551206145Sjilles if (level + 1 >= maxnest) { 1552206145Sjilles maxnest *= 2; 1553206145Sjilles if (state == state_static) { 1554206145Sjilles state = parser_temp_alloc( 1555206145Sjilles maxnest * sizeof(*state)); 1556206145Sjilles memcpy(state, state_static, 1557213811Sobrien MAXNEST_static * sizeof(*state)); 1558206145Sjilles } else 1559206145Sjilles state = parser_temp_realloc(state, 1560206145Sjilles maxnest * sizeof(*state)); 1561206145Sjilles } 1562206145Sjilles level++; 1563206145Sjilles state[level].parenlevel = 0; 1564206145Sjilles if (subtype == VSMINUS || subtype == VSPLUS || 1565206145Sjilles subtype == VSQUESTION || subtype == VSASSIGN) { 1566206145Sjilles /* 1567206145Sjilles * For operators that were in the Bourne shell, 1568206145Sjilles * inherit the double-quote state. 1569206145Sjilles */ 1570206145Sjilles state[level].syntax = state[level - 1].syntax; 1571206145Sjilles state[level].category = TSTATE_VAR_OLD; 1572206145Sjilles } else { 1573206145Sjilles /* 1574206145Sjilles * The other operators take a pattern, 1575206145Sjilles * so go to BASESYNTAX. 1576206145Sjilles * Also, ' and " are now special, even 1577206145Sjilles * in here documents. 1578206145Sjilles */ 1579206145Sjilles state[level].syntax = BASESYNTAX; 1580206145Sjilles state[level].category = TSTATE_VAR_NEW; 1581206145Sjilles newvarnest++; 1582206145Sjilles } 1583206145Sjilles } 15841556Srgrimes } 15851556Srgrimes goto parsesub_return; 15861556Srgrimes} 15871556Srgrimes 15881556Srgrimes 15891556Srgrimes/* 15901556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 15911556Srgrimes */ 15921556Srgrimesparsearith: { 15931556Srgrimes 1594206145Sjilles if (level + 1 >= maxnest) { 1595206145Sjilles maxnest *= 2; 1596206145Sjilles if (state == state_static) { 1597206145Sjilles state = parser_temp_alloc( 1598206145Sjilles maxnest * sizeof(*state)); 1599206145Sjilles memcpy(state, state_static, 1600213811Sobrien MAXNEST_static * sizeof(*state)); 1601206145Sjilles } else 1602206145Sjilles state = parser_temp_realloc(state, 1603206145Sjilles maxnest * sizeof(*state)); 16041556Srgrimes } 1605206145Sjilles level++; 1606206145Sjilles state[level].syntax = ARISYNTAX; 1607206145Sjilles state[level].parenlevel = 0; 1608206145Sjilles state[level].category = TSTATE_ARITH; 1609206145Sjilles USTPUTC(CTLARI, out); 1610206145Sjilles if (state[level - 1].syntax == DQSYNTAX) 1611206145Sjilles USTPUTC('"',out); 1612206145Sjilles else 1613206145Sjilles USTPUTC(' ',out); 16141556Srgrimes goto parsearith_return; 16151556Srgrimes} 16161556Srgrimes 16171556Srgrimes} /* end of readtoken */ 16181556Srgrimes 16191556Srgrimes 16201556Srgrimes 16211556Srgrimes#ifdef mkinit 16221556SrgrimesRESET { 16231556Srgrimes tokpushback = 0; 16241556Srgrimes checkkwd = 0; 16251556Srgrimes} 16261556Srgrimes#endif 16271556Srgrimes 16281556Srgrimes/* 16291556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 16301556Srgrimes * or backquotes). 16311556Srgrimes */ 16321556Srgrimes 1633213811Sobrienstatic int 163490111Simpnoexpand(char *text) 163590111Simp{ 163625230Ssteve char *p; 163725230Ssteve char c; 16381556Srgrimes 16391556Srgrimes p = text; 16401556Srgrimes while ((c = *p++) != '\0') { 164139137Stegge if ( c == CTLQUOTEMARK) 164239137Stegge continue; 16431556Srgrimes if (c == CTLESC) 16441556Srgrimes p++; 164583675Stegge else if (BASESYNTAX[(int)c] == CCTL) 16461556Srgrimes return 0; 16471556Srgrimes } 16481556Srgrimes return 1; 16491556Srgrimes} 16501556Srgrimes 16511556Srgrimes 16521556Srgrimes/* 16531556Srgrimes * Return true if the argument is a legal variable name (a letter or 16541556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 16551556Srgrimes */ 16561556Srgrimes 16571556Srgrimesint 1658200956Sjillesgoodname(const char *name) 165990111Simp{ 1660200956Sjilles const char *p; 16611556Srgrimes 16621556Srgrimes p = name; 16631556Srgrimes if (! is_name(*p)) 16641556Srgrimes return 0; 16651556Srgrimes while (*++p) { 16661556Srgrimes if (! is_in_name(*p)) 16671556Srgrimes return 0; 16681556Srgrimes } 16691556Srgrimes return 1; 16701556Srgrimes} 16711556Srgrimes 16721556Srgrimes 16731556Srgrimes/* 16741556Srgrimes * Called when an unexpected token is read during the parse. The argument 16751556Srgrimes * is the token that is expected, or -1 if more than one type of token can 16761556Srgrimes * occur at this point. 16771556Srgrimes */ 16781556Srgrimes 1679213811Sobrienstatic void 168090111Simpsynexpect(int token) 168117987Speter{ 16821556Srgrimes char msg[64]; 16831556Srgrimes 16841556Srgrimes if (token >= 0) { 16851556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 16861556Srgrimes tokname[lasttoken], tokname[token]); 16871556Srgrimes } else { 16881556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 16891556Srgrimes } 16901556Srgrimes synerror(msg); 16911556Srgrimes} 16921556Srgrimes 16931556Srgrimes 1694213811Sobrienstatic void 1695201053Sjillessynerror(const char *msg) 169690111Simp{ 16971556Srgrimes if (commandname) 1698201366Sjilles outfmt(out2, "%s: %d: ", commandname, startlinno); 1699201366Sjilles outfmt(out2, "Syntax error: %s\n", msg); 17001556Srgrimes error((char *)NULL); 17011556Srgrimes} 17021556Srgrimes 1703213811Sobrienstatic void 170490111Simpsetprompt(int which) 170590111Simp{ 17061556Srgrimes whichprompt = which; 17071556Srgrimes 170817987Speter#ifndef NO_HISTORY 17091556Srgrimes if (!el) 171017987Speter#endif 1711199629Sjilles { 17121556Srgrimes out2str(getprompt(NULL)); 1713199629Sjilles flushout(out2); 1714199629Sjilles } 17151556Srgrimes} 17161556Srgrimes 17171556Srgrimes/* 17181556Srgrimes * called by editline -- any expansions to the prompt 17191556Srgrimes * should be added here. 17201556Srgrimes */ 17211556Srgrimeschar * 172290111Simpgetprompt(void *unused __unused) 172325905Ssteve{ 1724142845Sobrien static char ps[PROMPTLEN]; 1725142845Sobrien char *fmt; 1726209653Sjilles const char *pwd; 1727209653Sjilles int i, trim; 1728201053Sjilles static char internal_error[] = "<internal prompt error>"; 1729142845Sobrien 1730142845Sobrien /* 1731142845Sobrien * Select prompt format. 1732142845Sobrien */ 17331556Srgrimes switch (whichprompt) { 17341556Srgrimes case 0: 1735201053Sjilles fmt = nullstr; 1736142845Sobrien break; 17371556Srgrimes case 1: 1738142845Sobrien fmt = ps1val(); 1739142845Sobrien break; 17401556Srgrimes case 2: 1741142845Sobrien fmt = ps2val(); 1742142845Sobrien break; 17431556Srgrimes default: 1744201053Sjilles return internal_error; 17451556Srgrimes } 1746142845Sobrien 1747142845Sobrien /* 1748142845Sobrien * Format prompt string. 1749142845Sobrien */ 1750142845Sobrien for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1751142845Sobrien if (*fmt == '\\') 1752142845Sobrien switch (*++fmt) { 1753142845Sobrien 1754142845Sobrien /* 1755142845Sobrien * Hostname. 1756142845Sobrien * 1757142845Sobrien * \h specifies just the local hostname, 1758142845Sobrien * \H specifies fully-qualified hostname. 1759142845Sobrien */ 1760142845Sobrien case 'h': 1761142845Sobrien case 'H': 1762149024Sstefanf ps[i] = '\0'; 1763142845Sobrien gethostname(&ps[i], PROMPTLEN - i); 1764142845Sobrien /* Skip to end of hostname. */ 1765142845Sobrien trim = (*fmt == 'h') ? '.' : '\0'; 1766142845Sobrien while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1767142845Sobrien i++; 1768142845Sobrien break; 1769142845Sobrien 1770142845Sobrien /* 1771142845Sobrien * Working directory. 1772142845Sobrien * 1773142845Sobrien * \W specifies just the final component, 1774142845Sobrien * \w specifies the entire path. 1775142845Sobrien */ 1776142845Sobrien case 'W': 1777142845Sobrien case 'w': 1778209653Sjilles pwd = lookupvar("PWD"); 1779209653Sjilles if (pwd == NULL) 1780209653Sjilles pwd = "?"; 1781209653Sjilles if (*fmt == 'W' && 1782209653Sjilles *pwd == '/' && pwd[1] != '\0') 1783209653Sjilles strlcpy(&ps[i], strrchr(pwd, '/') + 1, 1784209653Sjilles PROMPTLEN - i); 1785209653Sjilles else 1786209653Sjilles strlcpy(&ps[i], pwd, PROMPTLEN - i); 1787142845Sobrien /* Skip to end of path. */ 1788142845Sobrien while (ps[i + 1] != '\0') 1789142845Sobrien i++; 1790142845Sobrien break; 1791142845Sobrien 1792142845Sobrien /* 1793142845Sobrien * Superuser status. 1794142845Sobrien * 1795142845Sobrien * '$' for normal users, '#' for root. 1796142845Sobrien */ 1797142845Sobrien case '$': 1798142845Sobrien ps[i] = (geteuid() != 0) ? '$' : '#'; 1799142845Sobrien break; 1800142845Sobrien 1801142845Sobrien /* 1802142845Sobrien * A literal \. 1803142845Sobrien */ 1804142845Sobrien case '\\': 1805142845Sobrien ps[i] = '\\'; 1806142845Sobrien break; 1807142845Sobrien 1808142845Sobrien /* 1809142845Sobrien * Emit unrecognized formats verbatim. 1810142845Sobrien */ 1811142845Sobrien default: 1812142845Sobrien ps[i++] = '\\'; 1813142845Sobrien ps[i] = *fmt; 1814142845Sobrien break; 1815142845Sobrien } 1816142845Sobrien else 1817142845Sobrien ps[i] = *fmt; 1818142845Sobrien ps[i] = '\0'; 1819142845Sobrien return (ps); 18201556Srgrimes} 1821