parser.c revision 104554
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 * 3. All advertising materials mentioning features or use of this software 171556Srgrimes * must display the following acknowledgement: 181556Srgrimes * This product includes software developed by the University of 191556Srgrimes * California, Berkeley and its contributors. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341556Srgrimes * SUCH DAMAGE. 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 3836150Scharnier#if 0 3936150Scharnierstatic char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; 4036150Scharnier#endif 411556Srgrimes#endif /* not lint */ 4299110Sobrien#include <sys/cdefs.h> 4399110Sobrien__FBSDID("$FreeBSD: head/bin/sh/parser.c 104554 2002-10-06 06:35:51Z tjr $"); 441556Srgrimes 4517987Speter#include <stdlib.h> 4617987Speter 471556Srgrimes#include "shell.h" 481556Srgrimes#include "parser.h" 491556Srgrimes#include "nodes.h" 501556Srgrimes#include "expand.h" /* defines rmescapes() */ 511556Srgrimes#include "redir.h" /* defines copyfd() */ 521556Srgrimes#include "syntax.h" 531556Srgrimes#include "options.h" 541556Srgrimes#include "input.h" 551556Srgrimes#include "output.h" 561556Srgrimes#include "var.h" 571556Srgrimes#include "error.h" 581556Srgrimes#include "memalloc.h" 591556Srgrimes#include "mystring.h" 601556Srgrimes#include "alias.h" 6117987Speter#include "show.h" 6259436Scracauer#include "eval.h" 6317987Speter#ifndef NO_HISTORY 641556Srgrimes#include "myhistedit.h" 6517987Speter#endif 661556Srgrimes 671556Srgrimes/* 681556Srgrimes * Shell command parser. 691556Srgrimes */ 701556Srgrimes 711556Srgrimes#define EOFMARKLEN 79 721556Srgrimes 731556Srgrimes/* values returned by readtoken */ 7417987Speter#include "token.h" 751556Srgrimes 761556Srgrimes 771556Srgrimes 781556Srgrimesstruct heredoc { 791556Srgrimes struct heredoc *next; /* next here document in list */ 801556Srgrimes union node *here; /* redirection node */ 811556Srgrimes char *eofmark; /* string indicating end of input */ 821556Srgrimes int striptabs; /* if set, strip leading tabs */ 831556Srgrimes}; 841556Srgrimes 851556Srgrimes 861556Srgrimes 871556Srgrimesstruct heredoc *heredoclist; /* list of here documents to read */ 881556Srgrimesint parsebackquote; /* nonzero if we are inside backquotes */ 891556Srgrimesint doprompt; /* if set, prompt the user */ 901556Srgrimesint needprompt; /* true if interactive and at start of line */ 911556Srgrimesint lasttoken; /* last token read */ 921556SrgrimesMKINIT int tokpushback; /* last token pushed back */ 931556Srgrimeschar *wordtext; /* text of last word returned by readtoken */ 941556SrgrimesMKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ 951556Srgrimesstruct nodelist *backquotelist; 961556Srgrimesunion node *redirnode; 971556Srgrimesstruct heredoc *heredoc; 981556Srgrimesint quoteflag; /* set if (part of) last token was quoted */ 991556Srgrimesint startlinno; /* line # where last token started */ 1001556Srgrimes 10118018Speter/* XXX When 'noaliases' is set to one, no alias expansion takes place. */ 10218018Speterstatic int noaliases = 0; 1031556Srgrimes 1041556Srgrimes#define GDB_HACK 1 /* avoid local declarations which gdb can't handle */ 1051556Srgrimes#ifdef GDB_HACK 1061556Srgrimesstatic const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'}; 1071556Srgrimesstatic const char types[] = "}-+?="; 1081556Srgrimes#endif 1091556Srgrimes 1101556Srgrimes 11190111SimpSTATIC union node *list(int); 11290111SimpSTATIC union node *andor(void); 11390111SimpSTATIC union node *pipeline(void); 11490111SimpSTATIC union node *command(void); 11590111SimpSTATIC union node *simplecmd(union node **, union node *); 11690111SimpSTATIC union node *makename(void); 11790111SimpSTATIC void parsefname(void); 11890111SimpSTATIC void parseheredoc(void); 11990111SimpSTATIC int peektoken(void); 12090111SimpSTATIC int readtoken(void); 12190111SimpSTATIC int xxreadtoken(void); 12290111SimpSTATIC int readtoken1(int, char const *, char *, int); 12390111SimpSTATIC int noexpand(char *); 12490111SimpSTATIC void synexpect(int); 12590111SimpSTATIC void synerror(char *); 12690111SimpSTATIC void setprompt(int); 1271556Srgrimes 12817987Speter 1291556Srgrimes/* 1301556Srgrimes * Read and parse a command. Returns NEOF on end of file. (NULL is a 1311556Srgrimes * valid parse tree indicating a blank line.) 1321556Srgrimes */ 1331556Srgrimes 1341556Srgrimesunion node * 13590111Simpparsecmd(int interact) 13617987Speter{ 1371556Srgrimes int t; 1381556Srgrimes 13960593Scracauer tokpushback = 0; 1401556Srgrimes doprompt = interact; 1411556Srgrimes if (doprompt) 1421556Srgrimes setprompt(1); 1431556Srgrimes else 1441556Srgrimes setprompt(0); 1451556Srgrimes needprompt = 0; 1461556Srgrimes t = readtoken(); 1471556Srgrimes if (t == TEOF) 1481556Srgrimes return NEOF; 1491556Srgrimes if (t == TNL) 1501556Srgrimes return NULL; 1511556Srgrimes tokpushback++; 1521556Srgrimes return list(1); 1531556Srgrimes} 1541556Srgrimes 1551556Srgrimes 1561556SrgrimesSTATIC union node * 15790111Simplist(int nlflag) 15817987Speter{ 1591556Srgrimes union node *n1, *n2, *n3; 16017987Speter int tok; 1611556Srgrimes 1621556Srgrimes checkkwd = 2; 1631556Srgrimes if (nlflag == 0 && tokendlist[peektoken()]) 1641556Srgrimes return NULL; 16517987Speter n1 = NULL; 1661556Srgrimes for (;;) { 16717987Speter n2 = andor(); 16817987Speter tok = readtoken(); 16917987Speter if (tok == TBACKGND) { 17017987Speter if (n2->type == NCMD || n2->type == NPIPE) { 17117987Speter n2->ncmd.backgnd = 1; 17217987Speter } else if (n2->type == NREDIR) { 17317987Speter n2->type = NBACKGND; 17417987Speter } else { 17517987Speter n3 = (union node *)stalloc(sizeof (struct nredir)); 17617987Speter n3->type = NBACKGND; 17717987Speter n3->nredir.n = n2; 17817987Speter n3->nredir.redirect = NULL; 17917987Speter n2 = n3; 18017987Speter } 18117987Speter } 18217987Speter if (n1 == NULL) { 18317987Speter n1 = n2; 18417987Speter } 18517987Speter else { 18617987Speter n3 = (union node *)stalloc(sizeof (struct nbinary)); 18717987Speter n3->type = NSEMI; 18817987Speter n3->nbinary.ch1 = n1; 18917987Speter n3->nbinary.ch2 = n2; 19017987Speter n1 = n3; 19117987Speter } 19217987Speter switch (tok) { 19313882Sjoerg case TBACKGND: 19417987Speter case TSEMI: 19517987Speter tok = readtoken(); 196102410Scharnier /* FALLTHROUGH */ 1971556Srgrimes case TNL: 19817987Speter if (tok == TNL) { 19917987Speter parseheredoc(); 20017987Speter if (nlflag) 20117987Speter return n1; 20217987Speter } else { 20317987Speter tokpushback++; 20417987Speter } 2051556Srgrimes checkkwd = 2; 2061556Srgrimes if (tokendlist[peektoken()]) 2071556Srgrimes return n1; 2081556Srgrimes break; 2091556Srgrimes case TEOF: 2101556Srgrimes if (heredoclist) 2111556Srgrimes parseheredoc(); 2121556Srgrimes else 2131556Srgrimes pungetc(); /* push back EOF on input */ 2141556Srgrimes return n1; 2151556Srgrimes default: 2161556Srgrimes if (nlflag) 2171556Srgrimes synexpect(-1); 2181556Srgrimes tokpushback++; 2191556Srgrimes return n1; 2201556Srgrimes } 2211556Srgrimes } 2221556Srgrimes} 2231556Srgrimes 2241556Srgrimes 2251556Srgrimes 2261556SrgrimesSTATIC union node * 22790111Simpandor(void) 22890111Simp{ 2291556Srgrimes union node *n1, *n2, *n3; 2301556Srgrimes int t; 2311556Srgrimes 2321556Srgrimes n1 = pipeline(); 2331556Srgrimes for (;;) { 2341556Srgrimes if ((t = readtoken()) == TAND) { 2351556Srgrimes t = NAND; 2361556Srgrimes } else if (t == TOR) { 2371556Srgrimes t = NOR; 2381556Srgrimes } else { 2391556Srgrimes tokpushback++; 2401556Srgrimes return n1; 2411556Srgrimes } 2421556Srgrimes n2 = pipeline(); 2431556Srgrimes n3 = (union node *)stalloc(sizeof (struct nbinary)); 2441556Srgrimes n3->type = t; 2451556Srgrimes n3->nbinary.ch1 = n1; 2461556Srgrimes n3->nbinary.ch2 = n2; 2471556Srgrimes n1 = n3; 2481556Srgrimes } 2491556Srgrimes} 2501556Srgrimes 2511556Srgrimes 2521556Srgrimes 2531556SrgrimesSTATIC union node * 25490111Simppipeline(void) 25590111Simp{ 25675336Sbrian union node *n1, *n2, *pipenode; 2571556Srgrimes struct nodelist *lp, *prev; 25875336Sbrian int negate; 2591556Srgrimes 26075336Sbrian negate = 0; 2611556Srgrimes TRACE(("pipeline: entered\n")); 26275336Sbrian while (readtoken() == TNOT) 26375336Sbrian negate = !negate; 26475336Sbrian tokpushback++; 2651556Srgrimes n1 = command(); 2661556Srgrimes if (readtoken() == TPIPE) { 2671556Srgrimes pipenode = (union node *)stalloc(sizeof (struct npipe)); 2681556Srgrimes pipenode->type = NPIPE; 2691556Srgrimes pipenode->npipe.backgnd = 0; 2701556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 2711556Srgrimes pipenode->npipe.cmdlist = lp; 2721556Srgrimes lp->n = n1; 2731556Srgrimes do { 2741556Srgrimes prev = lp; 2751556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 2761556Srgrimes lp->n = command(); 2771556Srgrimes prev->next = lp; 2781556Srgrimes } while (readtoken() == TPIPE); 2791556Srgrimes lp->next = NULL; 2801556Srgrimes n1 = pipenode; 2811556Srgrimes } 2821556Srgrimes tokpushback++; 28375336Sbrian if (negate) { 28475336Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 28575336Sbrian n2->type = NNOT; 28675336Sbrian n2->nnot.com = n1; 28775336Sbrian return n2; 28875336Sbrian } else 28975336Sbrian return n1; 2901556Srgrimes} 2911556Srgrimes 2921556Srgrimes 2931556Srgrimes 2941556SrgrimesSTATIC union node * 29590111Simpcommand(void) 29690111Simp{ 2971556Srgrimes union node *n1, *n2; 2981556Srgrimes union node *ap, **app; 2991556Srgrimes union node *cp, **cpp; 3001556Srgrimes union node *redir, **rpp; 30175160Sbrian int t, negate = 0; 3021556Srgrimes 3031556Srgrimes checkkwd = 2; 30417987Speter redir = NULL; 30517987Speter n1 = NULL; 3061556Srgrimes rpp = &redir; 30720425Ssteve 3081556Srgrimes /* Check for redirection which may precede command */ 3091556Srgrimes while (readtoken() == TREDIR) { 3101556Srgrimes *rpp = n2 = redirnode; 3111556Srgrimes rpp = &n2->nfile.next; 3121556Srgrimes parsefname(); 3131556Srgrimes } 3141556Srgrimes tokpushback++; 3151556Srgrimes 31675160Sbrian while (readtoken() == TNOT) { 31775160Sbrian TRACE(("command: TNOT recognized\n")); 31875160Sbrian negate = !negate; 31975160Sbrian } 32075160Sbrian tokpushback++; 32175160Sbrian 3221556Srgrimes switch (readtoken()) { 3231556Srgrimes case TIF: 3241556Srgrimes n1 = (union node *)stalloc(sizeof (struct nif)); 3251556Srgrimes n1->type = NIF; 326104554Stjr if ((n1->nif.test = list(0)) == NULL) 327104554Stjr synexpect(-1); 3281556Srgrimes if (readtoken() != TTHEN) 3291556Srgrimes synexpect(TTHEN); 3301556Srgrimes n1->nif.ifpart = list(0); 3311556Srgrimes n2 = n1; 3321556Srgrimes while (readtoken() == TELIF) { 3331556Srgrimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 3341556Srgrimes n2 = n2->nif.elsepart; 3351556Srgrimes n2->type = NIF; 336104554Stjr if ((n2->nif.test = list(0)) == NULL) 337104554Stjr synexpect(-1); 3381556Srgrimes if (readtoken() != TTHEN) 3391556Srgrimes synexpect(TTHEN); 3401556Srgrimes n2->nif.ifpart = list(0); 3411556Srgrimes } 3421556Srgrimes if (lasttoken == TELSE) 3431556Srgrimes n2->nif.elsepart = list(0); 3441556Srgrimes else { 3451556Srgrimes n2->nif.elsepart = NULL; 3461556Srgrimes tokpushback++; 3471556Srgrimes } 3481556Srgrimes if (readtoken() != TFI) 3491556Srgrimes synexpect(TFI); 3501556Srgrimes checkkwd = 1; 3511556Srgrimes break; 3521556Srgrimes case TWHILE: 3531556Srgrimes case TUNTIL: { 3541556Srgrimes int got; 3551556Srgrimes n1 = (union node *)stalloc(sizeof (struct nbinary)); 3561556Srgrimes n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 357104554Stjr if ((n1->nbinary.ch1 = list(0)) == NULL) 358104554Stjr synexpect(-1); 3591556Srgrimes if ((got=readtoken()) != TDO) { 3601556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 3611556Srgrimes synexpect(TDO); 3621556Srgrimes } 3631556Srgrimes n1->nbinary.ch2 = list(0); 3641556Srgrimes if (readtoken() != TDONE) 3651556Srgrimes synexpect(TDONE); 3661556Srgrimes checkkwd = 1; 3671556Srgrimes break; 3681556Srgrimes } 3691556Srgrimes case TFOR: 3701556Srgrimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 3711556Srgrimes synerror("Bad for loop variable"); 3721556Srgrimes n1 = (union node *)stalloc(sizeof (struct nfor)); 3731556Srgrimes n1->type = NFOR; 3741556Srgrimes n1->nfor.var = wordtext; 3751556Srgrimes if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) { 3761556Srgrimes app = ≈ 3771556Srgrimes while (readtoken() == TWORD) { 3781556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3791556Srgrimes n2->type = NARG; 3801556Srgrimes n2->narg.text = wordtext; 3811556Srgrimes n2->narg.backquote = backquotelist; 3821556Srgrimes *app = n2; 3831556Srgrimes app = &n2->narg.next; 3841556Srgrimes } 3851556Srgrimes *app = NULL; 3861556Srgrimes n1->nfor.args = ap; 3871556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 3881556Srgrimes synexpect(-1); 3891556Srgrimes } else { 3901556Srgrimes#ifndef GDB_HACK 3911556Srgrimes static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, 3921556Srgrimes '@', '=', '\0'}; 3931556Srgrimes#endif 3941556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3951556Srgrimes n2->type = NARG; 3961556Srgrimes n2->narg.text = (char *)argvars; 3971556Srgrimes n2->narg.backquote = NULL; 3981556Srgrimes n2->narg.next = NULL; 3991556Srgrimes n1->nfor.args = n2; 4001556Srgrimes /* 4011556Srgrimes * Newline or semicolon here is optional (but note 4021556Srgrimes * that the original Bourne shell only allowed NL). 4031556Srgrimes */ 4041556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 4051556Srgrimes tokpushback++; 4061556Srgrimes } 4071556Srgrimes checkkwd = 2; 4081556Srgrimes if ((t = readtoken()) == TDO) 4091556Srgrimes t = TDONE; 4101556Srgrimes else if (t == TBEGIN) 4111556Srgrimes t = TEND; 4121556Srgrimes else 4131556Srgrimes synexpect(-1); 4141556Srgrimes n1->nfor.body = list(0); 4151556Srgrimes if (readtoken() != t) 4161556Srgrimes synexpect(t); 4171556Srgrimes checkkwd = 1; 4181556Srgrimes break; 4191556Srgrimes case TCASE: 4201556Srgrimes n1 = (union node *)stalloc(sizeof (struct ncase)); 4211556Srgrimes n1->type = NCASE; 4221556Srgrimes if (readtoken() != TWORD) 4231556Srgrimes synexpect(TWORD); 4241556Srgrimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 4251556Srgrimes n2->type = NARG; 4261556Srgrimes n2->narg.text = wordtext; 4271556Srgrimes n2->narg.backquote = backquotelist; 4281556Srgrimes n2->narg.next = NULL; 4291556Srgrimes while (readtoken() == TNL); 4301556Srgrimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 4311556Srgrimes synerror("expecting \"in\""); 4321556Srgrimes cpp = &n1->ncase.cases; 43318018Speter noaliases = 1; /* turn off alias expansion */ 4342760Ssef checkkwd = 2, readtoken(); 435104202Stjr while (lasttoken != TESAC) { 4361556Srgrimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 4371556Srgrimes cp->type = NCLIST; 4381556Srgrimes app = &cp->nclist.pattern; 439104207Stjr if (lasttoken == TLP) 440104207Stjr readtoken(); 4411556Srgrimes for (;;) { 4421556Srgrimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 4431556Srgrimes ap->type = NARG; 4441556Srgrimes ap->narg.text = wordtext; 4451556Srgrimes ap->narg.backquote = backquotelist; 4462760Ssef if (checkkwd = 2, readtoken() != TPIPE) 4471556Srgrimes break; 4481556Srgrimes app = &ap->narg.next; 4492760Ssef readtoken(); 4501556Srgrimes } 4511556Srgrimes ap->narg.next = NULL; 4521556Srgrimes if (lasttoken != TRP) 45318018Speter noaliases = 0, synexpect(TRP); 4541556Srgrimes cp->nclist.body = list(0); 4552760Ssef 4562760Ssef checkkwd = 2; 4572760Ssef if ((t = readtoken()) != TESAC) { 4582760Ssef if (t != TENDCASE) 45918018Speter noaliases = 0, synexpect(TENDCASE); 4602760Ssef else 4612760Ssef checkkwd = 2, readtoken(); 4622760Ssef } 4631556Srgrimes cpp = &cp->nclist.next; 464104202Stjr } 46518018Speter noaliases = 0; /* reset alias expansion */ 4661556Srgrimes *cpp = NULL; 4671556Srgrimes checkkwd = 1; 4681556Srgrimes break; 4691556Srgrimes case TLP: 4701556Srgrimes n1 = (union node *)stalloc(sizeof (struct nredir)); 4711556Srgrimes n1->type = NSUBSHELL; 4721556Srgrimes n1->nredir.n = list(0); 4731556Srgrimes n1->nredir.redirect = NULL; 4741556Srgrimes if (readtoken() != TRP) 4751556Srgrimes synexpect(TRP); 4761556Srgrimes checkkwd = 1; 4771556Srgrimes break; 4781556Srgrimes case TBEGIN: 4791556Srgrimes n1 = list(0); 4801556Srgrimes if (readtoken() != TEND) 4811556Srgrimes synexpect(TEND); 4821556Srgrimes checkkwd = 1; 4831556Srgrimes break; 4841556Srgrimes /* Handle an empty command like other simple commands. */ 48517987Speter case TSEMI: 486101662Stjr case TAND: 487101662Stjr case TOR: 48817987Speter /* 48917987Speter * An empty command before a ; doesn't make much sense, and 49017987Speter * should certainly be disallowed in the case of `if ;'. 49117987Speter */ 49217987Speter if (!redir) 49317987Speter synexpect(-1); 4941556Srgrimes case TNL: 49510399Sjoerg case TEOF: 4961556Srgrimes case TWORD: 49717987Speter case TRP: 4981556Srgrimes tokpushback++; 49975160Sbrian n1 = simplecmd(rpp, redir); 50075160Sbrian goto checkneg; 5011556Srgrimes default: 5021556Srgrimes synexpect(-1); 5031556Srgrimes } 5041556Srgrimes 5051556Srgrimes /* Now check for redirection which may follow command */ 5061556Srgrimes while (readtoken() == TREDIR) { 5071556Srgrimes *rpp = n2 = redirnode; 5081556Srgrimes rpp = &n2->nfile.next; 5091556Srgrimes parsefname(); 5101556Srgrimes } 5111556Srgrimes tokpushback++; 5121556Srgrimes *rpp = NULL; 5131556Srgrimes if (redir) { 5141556Srgrimes if (n1->type != NSUBSHELL) { 5151556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 5161556Srgrimes n2->type = NREDIR; 5171556Srgrimes n2->nredir.n = n1; 5181556Srgrimes n1 = n2; 5191556Srgrimes } 5201556Srgrimes n1->nredir.redirect = redir; 5211556Srgrimes } 52275160Sbrian 52375160Sbriancheckneg: 52475160Sbrian if (negate) { 52575160Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 52675160Sbrian n2->type = NNOT; 52775160Sbrian n2->nnot.com = n1; 52875160Sbrian return n2; 52975160Sbrian } 53075160Sbrian else 53175160Sbrian return n1; 5321556Srgrimes} 5331556Srgrimes 5341556Srgrimes 5351556SrgrimesSTATIC union node * 53690111Simpsimplecmd(union node **rpp, union node *redir) 53790111Simp{ 5381556Srgrimes union node *args, **app; 5391556Srgrimes union node **orig_rpp = rpp; 54075160Sbrian union node *n = NULL, *n2; 54175160Sbrian int negate = 0; 5421556Srgrimes 5431556Srgrimes /* If we don't have any redirections already, then we must reset */ 5441556Srgrimes /* rpp to be the address of the local redir variable. */ 5451556Srgrimes if (redir == 0) 5461556Srgrimes rpp = &redir; 5471556Srgrimes 5481556Srgrimes args = NULL; 5491556Srgrimes app = &args; 5508855Srgrimes /* 5511556Srgrimes * We save the incoming value, because we need this for shell 5521556Srgrimes * functions. There can not be a redirect or an argument between 5538855Srgrimes * the function name and the open parenthesis. 5541556Srgrimes */ 5551556Srgrimes orig_rpp = rpp; 5561556Srgrimes 55775160Sbrian while (readtoken() == TNOT) { 55875160Sbrian TRACE(("command: TNOT recognized\n")); 55975160Sbrian negate = !negate; 56075160Sbrian } 56175160Sbrian tokpushback++; 56275160Sbrian 5631556Srgrimes for (;;) { 5641556Srgrimes if (readtoken() == TWORD) { 5651556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 5661556Srgrimes n->type = NARG; 5671556Srgrimes n->narg.text = wordtext; 5681556Srgrimes n->narg.backquote = backquotelist; 5691556Srgrimes *app = n; 5701556Srgrimes app = &n->narg.next; 5711556Srgrimes } else if (lasttoken == TREDIR) { 5721556Srgrimes *rpp = n = redirnode; 5731556Srgrimes rpp = &n->nfile.next; 5741556Srgrimes parsefname(); /* read name of redirection file */ 5751556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 5761556Srgrimes && rpp == orig_rpp) { 5771556Srgrimes /* We have a function */ 5781556Srgrimes if (readtoken() != TRP) 5791556Srgrimes synexpect(TRP); 5801556Srgrimes#ifdef notdef 5811556Srgrimes if (! goodname(n->narg.text)) 5821556Srgrimes synerror("Bad function name"); 5831556Srgrimes#endif 5841556Srgrimes n->type = NDEFUN; 5851556Srgrimes n->narg.next = command(); 58675160Sbrian goto checkneg; 5871556Srgrimes } else { 5881556Srgrimes tokpushback++; 5891556Srgrimes break; 5901556Srgrimes } 5911556Srgrimes } 5921556Srgrimes *app = NULL; 5931556Srgrimes *rpp = NULL; 5941556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 5951556Srgrimes n->type = NCMD; 5961556Srgrimes n->ncmd.backgnd = 0; 5971556Srgrimes n->ncmd.args = args; 5981556Srgrimes n->ncmd.redirect = redir; 59975160Sbrian 60075160Sbriancheckneg: 60175160Sbrian if (negate) { 60275160Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 60375160Sbrian n2->type = NNOT; 60475160Sbrian n2->nnot.com = n; 60575160Sbrian return n2; 60675160Sbrian } 60775160Sbrian else 60875160Sbrian return n; 6091556Srgrimes} 6101556Srgrimes 61117987SpeterSTATIC union node * 61290111Simpmakename(void) 61390111Simp{ 61417987Speter union node *n; 6151556Srgrimes 61617987Speter n = (union node *)stalloc(sizeof (struct narg)); 61717987Speter n->type = NARG; 61817987Speter n->narg.next = NULL; 61917987Speter n->narg.text = wordtext; 62017987Speter n->narg.backquote = backquotelist; 62117987Speter return n; 62217987Speter} 62317987Speter 62490111Simpvoid fixredir(union node *n, const char *text, int err) 62590111Simp{ 62617987Speter TRACE(("Fix redir %s %d\n", text, err)); 62717987Speter if (!err) 62817987Speter n->ndup.vname = NULL; 62917987Speter 63017987Speter if (is_digit(text[0]) && text[1] == '\0') 63117987Speter n->ndup.dupfd = digit_val(text[0]); 63217987Speter else if (text[0] == '-' && text[1] == '\0') 63317987Speter n->ndup.dupfd = -1; 63417987Speter else { 63520425Ssteve 63617987Speter if (err) 63717987Speter synerror("Bad fd number"); 63817987Speter else 63917987Speter n->ndup.vname = makename(); 64017987Speter } 64117987Speter} 64217987Speter 64317987Speter 6441556SrgrimesSTATIC void 64590111Simpparsefname(void) 64690111Simp{ 6471556Srgrimes union node *n = redirnode; 6481556Srgrimes 6491556Srgrimes if (readtoken() != TWORD) 6501556Srgrimes synexpect(-1); 6511556Srgrimes if (n->type == NHERE) { 6521556Srgrimes struct heredoc *here = heredoc; 6531556Srgrimes struct heredoc *p; 6541556Srgrimes int i; 6551556Srgrimes 6561556Srgrimes if (quoteflag == 0) 6571556Srgrimes n->type = NXHERE; 6581556Srgrimes TRACE(("Here document %d\n", n->type)); 6591556Srgrimes if (here->striptabs) { 6601556Srgrimes while (*wordtext == '\t') 6611556Srgrimes wordtext++; 6621556Srgrimes } 6631556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 6641556Srgrimes synerror("Illegal eof marker for << redirection"); 6651556Srgrimes rmescapes(wordtext); 6661556Srgrimes here->eofmark = wordtext; 6671556Srgrimes here->next = NULL; 6681556Srgrimes if (heredoclist == NULL) 6691556Srgrimes heredoclist = here; 6701556Srgrimes else { 6711556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 6721556Srgrimes p->next = here; 6731556Srgrimes } 6741556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 67517987Speter fixredir(n, wordtext, 0); 6761556Srgrimes } else { 67717987Speter n->nfile.fname = makename(); 6781556Srgrimes } 6791556Srgrimes} 6801556Srgrimes 6811556Srgrimes 6821556Srgrimes/* 6831556Srgrimes * Input any here documents. 6841556Srgrimes */ 6851556Srgrimes 6861556SrgrimesSTATIC void 68790111Simpparseheredoc(void) 68890111Simp{ 6891556Srgrimes struct heredoc *here; 6901556Srgrimes union node *n; 6911556Srgrimes 6921556Srgrimes while (heredoclist) { 6931556Srgrimes here = heredoclist; 6941556Srgrimes heredoclist = here->next; 6951556Srgrimes if (needprompt) { 6961556Srgrimes setprompt(2); 6971556Srgrimes needprompt = 0; 6981556Srgrimes } 6991556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 7001556Srgrimes here->eofmark, here->striptabs); 7011556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 7021556Srgrimes n->narg.type = NARG; 7031556Srgrimes n->narg.next = NULL; 7041556Srgrimes n->narg.text = wordtext; 7051556Srgrimes n->narg.backquote = backquotelist; 7061556Srgrimes here->here->nhere.doc = n; 7071556Srgrimes } 7081556Srgrimes} 7091556Srgrimes 7101556SrgrimesSTATIC int 71190111Simppeektoken(void) 71290111Simp{ 7131556Srgrimes int t; 7141556Srgrimes 7151556Srgrimes t = readtoken(); 7161556Srgrimes tokpushback++; 7171556Srgrimes return (t); 7181556Srgrimes} 7191556Srgrimes 7201556SrgrimesSTATIC int 72190111Simpreadtoken(void) 72290111Simp{ 7231556Srgrimes int t; 7241556Srgrimes int savecheckkwd = checkkwd; 7251556Srgrimes struct alias *ap; 7261556Srgrimes#ifdef DEBUG 7271556Srgrimes int alreadyseen = tokpushback; 7281556Srgrimes#endif 7298855Srgrimes 7301556Srgrimes top: 7311556Srgrimes t = xxreadtoken(); 7321556Srgrimes 7331556Srgrimes if (checkkwd) { 7341556Srgrimes /* 7351556Srgrimes * eat newlines 7361556Srgrimes */ 7371556Srgrimes if (checkkwd == 2) { 7381556Srgrimes checkkwd = 0; 7391556Srgrimes while (t == TNL) { 7401556Srgrimes parseheredoc(); 7411556Srgrimes t = xxreadtoken(); 7421556Srgrimes } 7431556Srgrimes } else 7441556Srgrimes checkkwd = 0; 7451556Srgrimes /* 7461556Srgrimes * check for keywords and aliases 7471556Srgrimes */ 74820425Ssteve if (t == TWORD && !quoteflag) 74917987Speter { 75098463Sjmallett const char * const *pp; 7511556Srgrimes 75298463Sjmallett for (pp = parsekwd; *pp; pp++) { 75320425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 75417987Speter { 7551556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 7561556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 7571556Srgrimes goto out; 7581556Srgrimes } 7591556Srgrimes } 76018018Speter if (noaliases == 0 && 76118018Speter (ap = lookupalias(wordtext, 1)) != NULL) { 7621556Srgrimes pushstring(ap->val, strlen(ap->val), ap); 7631556Srgrimes checkkwd = savecheckkwd; 7641556Srgrimes goto top; 7651556Srgrimes } 7661556Srgrimes } 7671556Srgrimesout: 76875160Sbrian checkkwd = (t == TNOT) ? savecheckkwd : 0; 7691556Srgrimes } 7701556Srgrimes#ifdef DEBUG 7711556Srgrimes if (!alreadyseen) 7721556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7731556Srgrimes else 7741556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7751556Srgrimes#endif 7761556Srgrimes return (t); 7771556Srgrimes} 7781556Srgrimes 7791556Srgrimes 7801556Srgrimes/* 7811556Srgrimes * Read the next input token. 7821556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 7831556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 7841556Srgrimes * quoted. 7851556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 7861556Srgrimes * the redirection. 7871556Srgrimes * In all cases, the variable startlinno is set to the number of the line 7881556Srgrimes * on which the token starts. 7891556Srgrimes * 7901556Srgrimes * [Change comment: here documents and internal procedures] 7911556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 7921556Srgrimes * word parsing code into a separate routine. In this case, readtoken 7931556Srgrimes * doesn't need to have any internal procedures, but parseword does. 7941556Srgrimes * We could also make parseoperator in essence the main routine, and 7951556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 7961556Srgrimes */ 7971556Srgrimes 7981556Srgrimes#define RETURN(token) return lasttoken = token 7991556Srgrimes 8001556SrgrimesSTATIC int 80190111Simpxxreadtoken(void) 80290111Simp{ 80325230Ssteve int c; 8041556Srgrimes 8051556Srgrimes if (tokpushback) { 8061556Srgrimes tokpushback = 0; 8071556Srgrimes return lasttoken; 8081556Srgrimes } 8091556Srgrimes if (needprompt) { 8101556Srgrimes setprompt(2); 8111556Srgrimes needprompt = 0; 8121556Srgrimes } 8131556Srgrimes startlinno = plinno; 8141556Srgrimes for (;;) { /* until token or start of word found */ 8151556Srgrimes c = pgetc_macro(); 8161556Srgrimes if (c == ' ' || c == '\t') 8171556Srgrimes continue; /* quick check for white space first */ 8181556Srgrimes switch (c) { 8191556Srgrimes case ' ': case '\t': 8201556Srgrimes continue; 8211556Srgrimes case '#': 8221556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 8231556Srgrimes pungetc(); 8241556Srgrimes continue; 8251556Srgrimes case '\\': 8261556Srgrimes if (pgetc() == '\n') { 8271556Srgrimes startlinno = ++plinno; 8281556Srgrimes if (doprompt) 8291556Srgrimes setprompt(2); 8301556Srgrimes else 8311556Srgrimes setprompt(0); 8321556Srgrimes continue; 8331556Srgrimes } 8341556Srgrimes pungetc(); 8351556Srgrimes goto breakloop; 8361556Srgrimes case '\n': 8371556Srgrimes plinno++; 8381556Srgrimes needprompt = doprompt; 8391556Srgrimes RETURN(TNL); 8401556Srgrimes case PEOF: 8411556Srgrimes RETURN(TEOF); 8421556Srgrimes case '&': 8431556Srgrimes if (pgetc() == '&') 8441556Srgrimes RETURN(TAND); 8451556Srgrimes pungetc(); 8461556Srgrimes RETURN(TBACKGND); 8471556Srgrimes case '|': 8481556Srgrimes if (pgetc() == '|') 8491556Srgrimes RETURN(TOR); 8501556Srgrimes pungetc(); 8511556Srgrimes RETURN(TPIPE); 8521556Srgrimes case ';': 8531556Srgrimes if (pgetc() == ';') 8541556Srgrimes RETURN(TENDCASE); 8551556Srgrimes pungetc(); 8561556Srgrimes RETURN(TSEMI); 8571556Srgrimes case '(': 8581556Srgrimes RETURN(TLP); 8591556Srgrimes case ')': 8601556Srgrimes RETURN(TRP); 8611556Srgrimes default: 8621556Srgrimes goto breakloop; 8631556Srgrimes } 8641556Srgrimes } 8651556Srgrimesbreakloop: 8661556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 8671556Srgrimes#undef RETURN 8681556Srgrimes} 8691556Srgrimes 8701556Srgrimes 8711556Srgrimes 8721556Srgrimes/* 8731556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 8741556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 8751556Srgrimes * word which marks the end of the document and striptabs is true if 8761556Srgrimes * leading tabs should be stripped from the document. The argument firstc 8771556Srgrimes * is the first character of the input token or document. 8781556Srgrimes * 8791556Srgrimes * Because C does not have internal subroutines, I have simulated them 8801556Srgrimes * using goto's to implement the subroutine linkage. The following macros 8811556Srgrimes * will run code that appears at the end of readtoken1. 8821556Srgrimes */ 8831556Srgrimes 8841556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 8851556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 8861556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 8871556Srgrimes#define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 8881556Srgrimes#define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 8891556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 8901556Srgrimes 8911556SrgrimesSTATIC int 89290111Simpreadtoken1(int firstc, char const *syntax, char *eofmark, int striptabs) 89390111Simp{ 89417987Speter int c = firstc; 89517987Speter char *out; 8961556Srgrimes int len; 8971556Srgrimes char line[EOFMARKLEN + 1]; 8981556Srgrimes struct nodelist *bqlist; 8991556Srgrimes int quotef; 9001556Srgrimes int dblquote; 9011556Srgrimes int varnest; /* levels of variables expansion */ 9021556Srgrimes int arinest; /* levels of arithmetic expansion */ 9031556Srgrimes int parenlevel; /* levels of parens in arithmetic */ 9041556Srgrimes int oldstyle; 9051556Srgrimes char const *prevsyntax; /* syntax before arithmetic */ 90654679Scracauer int synentry; 90717987Speter#if __GNUC__ 90817987Speter /* Avoid longjmp clobbering */ 90917987Speter (void) &out; 91017987Speter (void) "ef; 91117987Speter (void) &dblquote; 91217987Speter (void) &varnest; 91317987Speter (void) &arinest; 91417987Speter (void) &parenlevel; 91517987Speter (void) &oldstyle; 91617987Speter (void) &prevsyntax; 91717987Speter (void) &syntax; 91854679Scracauer (void) &synentry; 91917987Speter#endif 9201556Srgrimes 9211556Srgrimes startlinno = plinno; 9221556Srgrimes dblquote = 0; 9231556Srgrimes if (syntax == DQSYNTAX) 9241556Srgrimes dblquote = 1; 9251556Srgrimes quotef = 0; 9261556Srgrimes bqlist = NULL; 9271556Srgrimes varnest = 0; 9281556Srgrimes arinest = 0; 9291556Srgrimes parenlevel = 0; 9301556Srgrimes 9311556Srgrimes STARTSTACKSTR(out); 9321556Srgrimes loop: { /* for each line, until end of word */ 9331556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 9341556Srgrimes for (;;) { /* until end of line or end of word */ 9351556Srgrimes CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 93654679Scracauer 93764705Scracauer synentry = syntax[c]; 93854679Scracauer 93954679Scracauer switch(synentry) { 9401556Srgrimes case CNL: /* '\n' */ 9411556Srgrimes if (syntax == BASESYNTAX) 9421556Srgrimes goto endword; /* exit outer loop */ 9431556Srgrimes USTPUTC(c, out); 9441556Srgrimes plinno++; 9451556Srgrimes if (doprompt) 9461556Srgrimes setprompt(2); 9471556Srgrimes else 9481556Srgrimes setprompt(0); 9491556Srgrimes c = pgetc(); 9501556Srgrimes goto loop; /* continue outer loop */ 9511556Srgrimes case CWORD: 9521556Srgrimes USTPUTC(c, out); 9531556Srgrimes break; 9541556Srgrimes case CCTL: 9551556Srgrimes if (eofmark == NULL || dblquote) 9561556Srgrimes USTPUTC(CTLESC, out); 9571556Srgrimes USTPUTC(c, out); 9581556Srgrimes break; 9591556Srgrimes case CBACK: /* backslash */ 9601556Srgrimes c = pgetc(); 9611556Srgrimes if (c == PEOF) { 9621556Srgrimes USTPUTC('\\', out); 9631556Srgrimes pungetc(); 9641556Srgrimes } else if (c == '\n') { 9651556Srgrimes if (doprompt) 9661556Srgrimes setprompt(2); 9671556Srgrimes else 9681556Srgrimes setprompt(0); 9691556Srgrimes } else { 97054631Scracauer if (dblquote && c != '\\' && 97154631Scracauer c != '`' && c != '$' && 97254631Scracauer (c != '"' || eofmark != NULL)) 9731556Srgrimes USTPUTC('\\', out); 97483675Stegge if (SQSYNTAX[c] == CCTL) 9751556Srgrimes USTPUTC(CTLESC, out); 97639137Stegge else if (eofmark == NULL) 97738887Stegge USTPUTC(CTLQUOTEMARK, out); 9781556Srgrimes USTPUTC(c, out); 9791556Srgrimes quotef++; 9801556Srgrimes } 9811556Srgrimes break; 9821556Srgrimes case CSQUOTE: 98339137Stegge if (eofmark == NULL) 98439137Stegge USTPUTC(CTLQUOTEMARK, out); 9851556Srgrimes syntax = SQSYNTAX; 9861556Srgrimes break; 9871556Srgrimes case CDQUOTE: 98839137Stegge if (eofmark == NULL) 98939137Stegge USTPUTC(CTLQUOTEMARK, out); 9901556Srgrimes syntax = DQSYNTAX; 9911556Srgrimes dblquote = 1; 9921556Srgrimes break; 9931556Srgrimes case CENDQUOTE: 99439137Stegge if (eofmark != NULL && arinest == 0 && 99539137Stegge varnest == 0) { 9961556Srgrimes USTPUTC(c, out); 9971556Srgrimes } else { 99839137Stegge if (arinest) { 9991556Srgrimes syntax = ARISYNTAX; 100039137Stegge dblquote = 0; 100139137Stegge } else if (eofmark == NULL) { 10021556Srgrimes syntax = BASESYNTAX; 100339137Stegge dblquote = 0; 100439137Stegge } 10051556Srgrimes quotef++; 10061556Srgrimes } 10071556Srgrimes break; 10081556Srgrimes case CVAR: /* '$' */ 10091556Srgrimes PARSESUB(); /* parse substitution */ 10101556Srgrimes break; 10111556Srgrimes case CENDVAR: /* '}' */ 10121556Srgrimes if (varnest > 0) { 10131556Srgrimes varnest--; 10141556Srgrimes USTPUTC(CTLENDVAR, out); 10151556Srgrimes } else { 10161556Srgrimes USTPUTC(c, out); 10171556Srgrimes } 10181556Srgrimes break; 10191556Srgrimes case CLP: /* '(' in arithmetic */ 10201556Srgrimes parenlevel++; 10211556Srgrimes USTPUTC(c, out); 10221556Srgrimes break; 10231556Srgrimes case CRP: /* ')' in arithmetic */ 10241556Srgrimes if (parenlevel > 0) { 10251556Srgrimes USTPUTC(c, out); 10261556Srgrimes --parenlevel; 10271556Srgrimes } else { 10281556Srgrimes if (pgetc() == ')') { 10291556Srgrimes if (--arinest == 0) { 10301556Srgrimes USTPUTC(CTLENDARI, out); 10311556Srgrimes syntax = prevsyntax; 103239137Stegge if (syntax == DQSYNTAX) 103339137Stegge dblquote = 1; 103439137Stegge else 103539137Stegge dblquote = 0; 10361556Srgrimes } else 10371556Srgrimes USTPUTC(')', out); 10381556Srgrimes } else { 10398855Srgrimes /* 10401556Srgrimes * unbalanced parens 10411556Srgrimes * (don't 2nd guess - no error) 10421556Srgrimes */ 10431556Srgrimes pungetc(); 10441556Srgrimes USTPUTC(')', out); 10451556Srgrimes } 10461556Srgrimes } 10471556Srgrimes break; 10481556Srgrimes case CBQUOTE: /* '`' */ 10491556Srgrimes PARSEBACKQOLD(); 10501556Srgrimes break; 10511556Srgrimes case CEOF: 10521556Srgrimes goto endword; /* exit outer loop */ 10531556Srgrimes default: 10541556Srgrimes if (varnest == 0) 10551556Srgrimes goto endword; /* exit outer loop */ 10561556Srgrimes USTPUTC(c, out); 10571556Srgrimes } 10581556Srgrimes c = pgetc_macro(); 10591556Srgrimes } 10601556Srgrimes } 10611556Srgrimesendword: 10621556Srgrimes if (syntax == ARISYNTAX) 10631556Srgrimes synerror("Missing '))'"); 10641556Srgrimes if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) 10651556Srgrimes synerror("Unterminated quoted string"); 10661556Srgrimes if (varnest != 0) { 10671556Srgrimes startlinno = plinno; 10681556Srgrimes synerror("Missing '}'"); 10691556Srgrimes } 10701556Srgrimes USTPUTC('\0', out); 10711556Srgrimes len = out - stackblock(); 10721556Srgrimes out = stackblock(); 10731556Srgrimes if (eofmark == NULL) { 10741556Srgrimes if ((c == '>' || c == '<') 10751556Srgrimes && quotef == 0 10761556Srgrimes && len <= 2 10771556Srgrimes && (*out == '\0' || is_digit(*out))) { 10781556Srgrimes PARSEREDIR(); 10791556Srgrimes return lasttoken = TREDIR; 10801556Srgrimes } else { 10811556Srgrimes pungetc(); 10821556Srgrimes } 10831556Srgrimes } 10841556Srgrimes quoteflag = quotef; 10851556Srgrimes backquotelist = bqlist; 10861556Srgrimes grabstackblock(len); 10871556Srgrimes wordtext = out; 10881556Srgrimes return lasttoken = TWORD; 10891556Srgrimes/* end of readtoken routine */ 10901556Srgrimes 10911556Srgrimes 10921556Srgrimes 10931556Srgrimes/* 10941556Srgrimes * Check to see whether we are at the end of the here document. When this 10951556Srgrimes * is called, c is set to the first character of the next input line. If 10961556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 10971556Srgrimes */ 10981556Srgrimes 10991556Srgrimescheckend: { 11001556Srgrimes if (eofmark) { 11011556Srgrimes if (striptabs) { 11021556Srgrimes while (c == '\t') 11031556Srgrimes c = pgetc(); 11041556Srgrimes } 11051556Srgrimes if (c == *eofmark) { 11061556Srgrimes if (pfgets(line, sizeof line) != NULL) { 110725230Ssteve char *p, *q; 11081556Srgrimes 11091556Srgrimes p = line; 11101556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 11111556Srgrimes if (*p == '\n' && *q == '\0') { 11121556Srgrimes c = PEOF; 11131556Srgrimes plinno++; 11141556Srgrimes needprompt = doprompt; 11151556Srgrimes } else { 11161556Srgrimes pushstring(line, strlen(line), NULL); 11171556Srgrimes } 11181556Srgrimes } 11191556Srgrimes } 11201556Srgrimes } 11211556Srgrimes goto checkend_return; 11221556Srgrimes} 11231556Srgrimes 11241556Srgrimes 11251556Srgrimes/* 11261556Srgrimes * Parse a redirection operator. The variable "out" points to a string 11271556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 11281556Srgrimes * first character of the redirection operator. 11291556Srgrimes */ 11301556Srgrimes 11311556Srgrimesparseredir: { 11321556Srgrimes char fd = *out; 11331556Srgrimes union node *np; 11341556Srgrimes 11351556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 11361556Srgrimes if (c == '>') { 11371556Srgrimes np->nfile.fd = 1; 11381556Srgrimes c = pgetc(); 11391556Srgrimes if (c == '>') 11401556Srgrimes np->type = NAPPEND; 11411556Srgrimes else if (c == '&') 11421556Srgrimes np->type = NTOFD; 114396922Stjr else if (c == '|') 114496922Stjr np->type = NCLOBBER; 11451556Srgrimes else { 11461556Srgrimes np->type = NTO; 11471556Srgrimes pungetc(); 11481556Srgrimes } 11491556Srgrimes } else { /* c == '<' */ 11501556Srgrimes np->nfile.fd = 0; 11511556Srgrimes c = pgetc(); 11521556Srgrimes if (c == '<') { 11531556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 11541556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 11551556Srgrimes np->nfile.fd = 0; 11561556Srgrimes } 11571556Srgrimes np->type = NHERE; 11581556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 11591556Srgrimes heredoc->here = np; 11601556Srgrimes if ((c = pgetc()) == '-') { 11611556Srgrimes heredoc->striptabs = 1; 11621556Srgrimes } else { 11631556Srgrimes heredoc->striptabs = 0; 11641556Srgrimes pungetc(); 11651556Srgrimes } 11661556Srgrimes } else if (c == '&') 11671556Srgrimes np->type = NFROMFD; 116866612Sbrian else if (c == '>') 116966612Sbrian np->type = NFROMTO; 11701556Srgrimes else { 11711556Srgrimes np->type = NFROM; 11721556Srgrimes pungetc(); 11731556Srgrimes } 11741556Srgrimes } 11751556Srgrimes if (fd != '\0') 11761556Srgrimes np->nfile.fd = digit_val(fd); 11771556Srgrimes redirnode = np; 11781556Srgrimes goto parseredir_return; 11791556Srgrimes} 11801556Srgrimes 11811556Srgrimes 11821556Srgrimes/* 11831556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 11841556Srgrimes * and nothing else. 11851556Srgrimes */ 11861556Srgrimes 11871556Srgrimesparsesub: { 11881556Srgrimes int subtype; 11891556Srgrimes int typeloc; 11901556Srgrimes int flags; 11911556Srgrimes char *p; 11921556Srgrimes#ifndef GDB_HACK 11931556Srgrimes static const char types[] = "}-+?="; 11941556Srgrimes#endif 119518202Speter int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 11961556Srgrimes 11971556Srgrimes c = pgetc(); 11981556Srgrimes if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) { 11991556Srgrimes USTPUTC('$', out); 12001556Srgrimes pungetc(); 12011556Srgrimes } else if (c == '(') { /* $(command) or $((arith)) */ 12021556Srgrimes if (pgetc() == '(') { 12031556Srgrimes PARSEARITH(); 12041556Srgrimes } else { 12051556Srgrimes pungetc(); 12061556Srgrimes PARSEBACKQNEW(); 12071556Srgrimes } 12081556Srgrimes } else { 12091556Srgrimes USTPUTC(CTLVAR, out); 12101556Srgrimes typeloc = out - stackblock(); 12111556Srgrimes USTPUTC(VSNORMAL, out); 12121556Srgrimes subtype = VSNORMAL; 12131556Srgrimes if (c == '{') { 121418202Speter bracketed_name = 1; 12151556Srgrimes c = pgetc(); 121617987Speter if (c == '#') { 121717987Speter if ((c = pgetc()) == '}') 121817987Speter c = '#'; 121917987Speter else 122017987Speter subtype = VSLENGTH; 122117987Speter } 122217987Speter else 122317987Speter subtype = 0; 12241556Srgrimes } 12251556Srgrimes if (is_name(c)) { 12261556Srgrimes do { 12271556Srgrimes STPUTC(c, out); 12281556Srgrimes c = pgetc(); 12291556Srgrimes } while (is_in_name(c)); 123018202Speter } else if (is_digit(c)) { 123118202Speter if (bracketed_name) { 123218202Speter do { 123318202Speter STPUTC(c, out); 123418202Speter c = pgetc(); 123518202Speter } while (is_digit(c)); 123618202Speter } else { 123718202Speter STPUTC(c, out); 123818202Speter c = pgetc(); 123918202Speter } 12401556Srgrimes } else { 12411556Srgrimes if (! is_special(c)) 12421556Srgrimesbadsub: synerror("Bad substitution"); 12431556Srgrimes USTPUTC(c, out); 12441556Srgrimes c = pgetc(); 12451556Srgrimes } 12461556Srgrimes STPUTC('=', out); 12471556Srgrimes flags = 0; 12481556Srgrimes if (subtype == 0) { 124917987Speter switch (c) { 125017987Speter case ':': 12511556Srgrimes flags = VSNUL; 12521556Srgrimes c = pgetc(); 125317987Speter /*FALLTHROUGH*/ 125417987Speter default: 125517987Speter p = strchr(types, c); 125617987Speter if (p == NULL) 125717987Speter goto badsub; 125817987Speter subtype = p - types + VSNORMAL; 125917987Speter break; 126017987Speter case '%': 126120425Ssteve case '#': 126217987Speter { 126317987Speter int cc = c; 126417987Speter subtype = c == '#' ? VSTRIMLEFT : 126517987Speter VSTRIMRIGHT; 126617987Speter c = pgetc(); 126717987Speter if (c == cc) 126817987Speter subtype++; 126917987Speter else 127017987Speter pungetc(); 127117987Speter break; 127217987Speter } 12731556Srgrimes } 12741556Srgrimes } else { 12751556Srgrimes pungetc(); 12761556Srgrimes } 127757225Scracauer if (subtype != VSLENGTH && (dblquote || arinest)) 12781556Srgrimes flags |= VSQUOTE; 12791556Srgrimes *(stackblock() + typeloc) = subtype | flags; 12801556Srgrimes if (subtype != VSNORMAL) 12811556Srgrimes varnest++; 12821556Srgrimes } 12831556Srgrimes goto parsesub_return; 12841556Srgrimes} 12851556Srgrimes 12861556Srgrimes 12871556Srgrimes/* 12881556Srgrimes * Called to parse command substitutions. Newstyle is set if the command 12891556Srgrimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked 12901556Srgrimes * list of commands (passed by reference), and savelen is the number of 12911556Srgrimes * characters on the top of the stack which must be preserved. 12921556Srgrimes */ 12931556Srgrimes 12941556Srgrimesparsebackq: { 12951556Srgrimes struct nodelist **nlpp; 12961556Srgrimes int savepbq; 12971556Srgrimes union node *n; 12981556Srgrimes char *volatile str; 12991556Srgrimes struct jmploc jmploc; 13001556Srgrimes struct jmploc *volatile savehandler; 13011556Srgrimes int savelen; 130220425Ssteve int saveprompt; 130320425Ssteve#if __GNUC__ 130420425Ssteve /* Avoid longjmp clobbering */ 130520425Ssteve (void) &saveprompt; 130620425Ssteve#endif 13071556Srgrimes 13081556Srgrimes savepbq = parsebackquote; 13091556Srgrimes if (setjmp(jmploc.loc)) { 13101556Srgrimes if (str) 13111556Srgrimes ckfree(str); 13121556Srgrimes parsebackquote = 0; 13131556Srgrimes handler = savehandler; 13141556Srgrimes longjmp(handler->loc, 1); 13151556Srgrimes } 13161556Srgrimes INTOFF; 13171556Srgrimes str = NULL; 13181556Srgrimes savelen = out - stackblock(); 13191556Srgrimes if (savelen > 0) { 13201556Srgrimes str = ckmalloc(savelen); 132117987Speter memcpy(str, stackblock(), savelen); 13221556Srgrimes } 13231556Srgrimes savehandler = handler; 13241556Srgrimes handler = &jmploc; 13251556Srgrimes INTON; 13261556Srgrimes if (oldstyle) { 13271556Srgrimes /* We must read until the closing backquote, giving special 13281556Srgrimes treatment to some slashes, and then push the string and 13291556Srgrimes reread it as input, interpreting it normally. */ 133025230Ssteve char *out; 133125230Ssteve int c; 13321556Srgrimes int savelen; 13331556Srgrimes char *str; 13348855Srgrimes 133520425Ssteve 13361556Srgrimes STARTSTACKSTR(out); 133720425Ssteve for (;;) { 133820425Ssteve if (needprompt) { 133920425Ssteve setprompt(2); 134020425Ssteve needprompt = 0; 134120425Ssteve } 134220425Ssteve switch (c = pgetc()) { 134320425Ssteve case '`': 134420425Ssteve goto done; 134520425Ssteve 134620425Ssteve case '\\': 134720425Ssteve if ((c = pgetc()) == '\n') { 134820425Ssteve plinno++; 134920425Ssteve if (doprompt) 135020425Ssteve setprompt(2); 135120425Ssteve else 135220425Ssteve setprompt(0); 135320425Ssteve /* 135420425Ssteve * If eating a newline, avoid putting 135520425Ssteve * the newline into the new character 135620425Ssteve * stream (via the STPUTC after the 135720425Ssteve * switch). 135820425Ssteve */ 135920425Ssteve continue; 136020425Ssteve } 136117987Speter if (c != '\\' && c != '`' && c != '$' 13621556Srgrimes && (!dblquote || c != '"')) 13631556Srgrimes STPUTC('\\', out); 136420425Ssteve break; 136520425Ssteve 136620425Ssteve case '\n': 136720425Ssteve plinno++; 136820425Ssteve needprompt = doprompt; 136920425Ssteve break; 137020425Ssteve 137120425Ssteve case PEOF: 137220425Ssteve startlinno = plinno; 137320425Ssteve synerror("EOF in backquote substitution"); 137420425Ssteve break; 137520425Ssteve 137620425Ssteve default: 137720425Ssteve break; 137820425Ssteve } 137920425Ssteve STPUTC(c, out); 13801556Srgrimes } 138120425Sstevedone: 13821556Srgrimes STPUTC('\0', out); 13831556Srgrimes savelen = out - stackblock(); 13841556Srgrimes if (savelen > 0) { 13851556Srgrimes str = ckmalloc(savelen); 138617987Speter memcpy(str, stackblock(), savelen); 138717987Speter setinputstring(str, 1); 13881556Srgrimes } 13891556Srgrimes } 13901556Srgrimes nlpp = &bqlist; 13911556Srgrimes while (*nlpp) 13921556Srgrimes nlpp = &(*nlpp)->next; 13931556Srgrimes *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 13941556Srgrimes (*nlpp)->next = NULL; 13951556Srgrimes parsebackquote = oldstyle; 139620425Ssteve 139720425Ssteve if (oldstyle) { 139820425Ssteve saveprompt = doprompt; 139920425Ssteve doprompt = 0; 140020425Ssteve } 140120425Ssteve 14021556Srgrimes n = list(0); 140320425Ssteve 140420425Ssteve if (oldstyle) 140520425Ssteve doprompt = saveprompt; 140620425Ssteve else { 140720425Ssteve if (readtoken() != TRP) 140820425Ssteve synexpect(TRP); 140920425Ssteve } 141020425Ssteve 14111556Srgrimes (*nlpp)->n = n; 141220425Ssteve if (oldstyle) { 141320425Ssteve /* 141420425Ssteve * Start reading from old file again, ignoring any pushed back 141520425Ssteve * tokens left from the backquote parsing 141620425Ssteve */ 14171556Srgrimes popfile(); 141820425Ssteve tokpushback = 0; 141920425Ssteve } 14201556Srgrimes while (stackblocksize() <= savelen) 14211556Srgrimes growstackblock(); 14221556Srgrimes STARTSTACKSTR(out); 14231556Srgrimes if (str) { 142417987Speter memcpy(out, str, savelen); 14251556Srgrimes STADJUST(savelen, out); 14261556Srgrimes INTOFF; 14271556Srgrimes ckfree(str); 14281556Srgrimes str = NULL; 14291556Srgrimes INTON; 14301556Srgrimes } 14311556Srgrimes parsebackquote = savepbq; 14321556Srgrimes handler = savehandler; 14331556Srgrimes if (arinest || dblquote) 14341556Srgrimes USTPUTC(CTLBACKQ | CTLQUOTE, out); 14351556Srgrimes else 14361556Srgrimes USTPUTC(CTLBACKQ, out); 14371556Srgrimes if (oldstyle) 14381556Srgrimes goto parsebackq_oldreturn; 14391556Srgrimes else 14401556Srgrimes goto parsebackq_newreturn; 14411556Srgrimes} 14421556Srgrimes 14431556Srgrimes/* 14441556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 14451556Srgrimes */ 14461556Srgrimesparsearith: { 14471556Srgrimes 14481556Srgrimes if (++arinest == 1) { 14491556Srgrimes prevsyntax = syntax; 14501556Srgrimes syntax = ARISYNTAX; 14511556Srgrimes USTPUTC(CTLARI, out); 145238887Stegge if (dblquote) 145338887Stegge USTPUTC('"',out); 145438887Stegge else 145538887Stegge USTPUTC(' ',out); 14561556Srgrimes } else { 14571556Srgrimes /* 14581556Srgrimes * we collapse embedded arithmetic expansion to 14591556Srgrimes * parenthesis, which should be equivalent 14601556Srgrimes */ 14611556Srgrimes USTPUTC('(', out); 14621556Srgrimes } 14631556Srgrimes goto parsearith_return; 14641556Srgrimes} 14651556Srgrimes 14661556Srgrimes} /* end of readtoken */ 14671556Srgrimes 14681556Srgrimes 14691556Srgrimes 14701556Srgrimes#ifdef mkinit 14711556SrgrimesRESET { 14721556Srgrimes tokpushback = 0; 14731556Srgrimes checkkwd = 0; 14741556Srgrimes} 14751556Srgrimes#endif 14761556Srgrimes 14771556Srgrimes/* 14781556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 14791556Srgrimes * or backquotes). 14801556Srgrimes */ 14811556Srgrimes 14821556SrgrimesSTATIC int 148390111Simpnoexpand(char *text) 148490111Simp{ 148525230Ssteve char *p; 148625230Ssteve char c; 14871556Srgrimes 14881556Srgrimes p = text; 14891556Srgrimes while ((c = *p++) != '\0') { 149039137Stegge if ( c == CTLQUOTEMARK) 149139137Stegge continue; 14921556Srgrimes if (c == CTLESC) 14931556Srgrimes p++; 149483675Stegge else if (BASESYNTAX[(int)c] == CCTL) 14951556Srgrimes return 0; 14961556Srgrimes } 14971556Srgrimes return 1; 14981556Srgrimes} 14991556Srgrimes 15001556Srgrimes 15011556Srgrimes/* 15021556Srgrimes * Return true if the argument is a legal variable name (a letter or 15031556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 15041556Srgrimes */ 15051556Srgrimes 15061556Srgrimesint 150790111Simpgoodname(char *name) 150890111Simp{ 150925230Ssteve char *p; 15101556Srgrimes 15111556Srgrimes p = name; 15121556Srgrimes if (! is_name(*p)) 15131556Srgrimes return 0; 15141556Srgrimes while (*++p) { 15151556Srgrimes if (! is_in_name(*p)) 15161556Srgrimes return 0; 15171556Srgrimes } 15181556Srgrimes return 1; 15191556Srgrimes} 15201556Srgrimes 15211556Srgrimes 15221556Srgrimes/* 15231556Srgrimes * Called when an unexpected token is read during the parse. The argument 15241556Srgrimes * is the token that is expected, or -1 if more than one type of token can 15251556Srgrimes * occur at this point. 15261556Srgrimes */ 15271556Srgrimes 15281556SrgrimesSTATIC void 152990111Simpsynexpect(int token) 153017987Speter{ 15311556Srgrimes char msg[64]; 15321556Srgrimes 15331556Srgrimes if (token >= 0) { 15341556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 15351556Srgrimes tokname[lasttoken], tokname[token]); 15361556Srgrimes } else { 15371556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 15381556Srgrimes } 15391556Srgrimes synerror(msg); 15401556Srgrimes} 15411556Srgrimes 15421556Srgrimes 15431556SrgrimesSTATIC void 154490111Simpsynerror(char *msg) 154590111Simp{ 15461556Srgrimes if (commandname) 15471556Srgrimes outfmt(&errout, "%s: %d: ", commandname, startlinno); 15481556Srgrimes outfmt(&errout, "Syntax error: %s\n", msg); 15491556Srgrimes error((char *)NULL); 15501556Srgrimes} 15511556Srgrimes 15521556SrgrimesSTATIC void 155390111Simpsetprompt(int which) 155490111Simp{ 15551556Srgrimes whichprompt = which; 15561556Srgrimes 155717987Speter#ifndef NO_HISTORY 15581556Srgrimes if (!el) 155917987Speter#endif 15601556Srgrimes out2str(getprompt(NULL)); 15611556Srgrimes} 15621556Srgrimes 15631556Srgrimes/* 15641556Srgrimes * called by editline -- any expansions to the prompt 15651556Srgrimes * should be added here. 15661556Srgrimes */ 15671556Srgrimeschar * 156890111Simpgetprompt(void *unused __unused) 156925905Ssteve{ 15701556Srgrimes switch (whichprompt) { 15711556Srgrimes case 0: 15721556Srgrimes return ""; 15731556Srgrimes case 1: 15741556Srgrimes return ps1val(); 15751556Srgrimes case 2: 15761556Srgrimes return ps2val(); 15771556Srgrimes default: 15781556Srgrimes return "<internal prompt error>"; 15791556Srgrimes } 15801556Srgrimes} 1581