parser.c revision 54679
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 4136150Scharnierstatic const char rcsid[] = 4250471Speter "$FreeBSD: head/bin/sh/parser.c 54679 1999-12-16 12:03:46Z cracauer $"; 431556Srgrimes#endif /* not lint */ 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" 6217987Speter#ifndef NO_HISTORY 631556Srgrimes#include "myhistedit.h" 6417987Speter#endif 651556Srgrimes 661556Srgrimes/* 671556Srgrimes * Shell command parser. 681556Srgrimes */ 691556Srgrimes 701556Srgrimes#define EOFMARKLEN 79 711556Srgrimes 721556Srgrimes/* values returned by readtoken */ 7317987Speter#include "token.h" 741556Srgrimes 751556Srgrimes 761556Srgrimes 771556Srgrimesstruct heredoc { 781556Srgrimes struct heredoc *next; /* next here document in list */ 791556Srgrimes union node *here; /* redirection node */ 801556Srgrimes char *eofmark; /* string indicating end of input */ 811556Srgrimes int striptabs; /* if set, strip leading tabs */ 821556Srgrimes}; 831556Srgrimes 841556Srgrimes 851556Srgrimes 861556Srgrimesstruct heredoc *heredoclist; /* list of here documents to read */ 871556Srgrimesint parsebackquote; /* nonzero if we are inside backquotes */ 881556Srgrimesint doprompt; /* if set, prompt the user */ 891556Srgrimesint needprompt; /* true if interactive and at start of line */ 901556Srgrimesint lasttoken; /* last token read */ 911556SrgrimesMKINIT int tokpushback; /* last token pushed back */ 921556Srgrimeschar *wordtext; /* text of last word returned by readtoken */ 931556SrgrimesMKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ 941556Srgrimesstruct nodelist *backquotelist; 951556Srgrimesunion node *redirnode; 961556Srgrimesstruct heredoc *heredoc; 971556Srgrimesint quoteflag; /* set if (part of) last token was quoted */ 981556Srgrimesint startlinno; /* line # where last token started */ 991556Srgrimes 10018018Speter/* XXX When 'noaliases' is set to one, no alias expansion takes place. */ 10118018Speterstatic int noaliases = 0; 1021556Srgrimes 1031556Srgrimes#define GDB_HACK 1 /* avoid local declarations which gdb can't handle */ 1041556Srgrimes#ifdef GDB_HACK 1051556Srgrimesstatic const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'}; 1061556Srgrimesstatic const char types[] = "}-+?="; 1071556Srgrimes#endif 1081556Srgrimes 1091556Srgrimes 1101556SrgrimesSTATIC union node *list __P((int)); 1111556SrgrimesSTATIC union node *andor __P((void)); 1121556SrgrimesSTATIC union node *pipeline __P((void)); 1131556SrgrimesSTATIC union node *command __P((void)); 1141556SrgrimesSTATIC union node *simplecmd __P((union node **, union node *)); 11517987SpeterSTATIC union node *makename __P((void)); 1161556SrgrimesSTATIC void parsefname __P((void)); 1171556SrgrimesSTATIC void parseheredoc __P((void)); 11817987SpeterSTATIC int peektoken __P((void)); 1191556SrgrimesSTATIC int readtoken __P((void)); 12017987SpeterSTATIC int xxreadtoken __P((void)); 1211556SrgrimesSTATIC int readtoken1 __P((int, char const *, char *, int)); 1221556SrgrimesSTATIC int noexpand __P((char *)); 1231556SrgrimesSTATIC void synexpect __P((int)); 1241556SrgrimesSTATIC void synerror __P((char *)); 12520425SsteveSTATIC void setprompt __P((int)); 1261556Srgrimes 12717987Speter 1281556Srgrimes/* 1291556Srgrimes * Read and parse a command. Returns NEOF on end of file. (NULL is a 1301556Srgrimes * valid parse tree indicating a blank line.) 1311556Srgrimes */ 1321556Srgrimes 1331556Srgrimesunion node * 13420425Ssteveparsecmd(interact) 13517987Speter int interact; 13617987Speter{ 1371556Srgrimes int t; 1381556Srgrimes 1391556Srgrimes doprompt = interact; 1401556Srgrimes if (doprompt) 1411556Srgrimes setprompt(1); 1421556Srgrimes else 1431556Srgrimes setprompt(0); 1441556Srgrimes needprompt = 0; 1451556Srgrimes t = readtoken(); 1461556Srgrimes if (t == TEOF) 1471556Srgrimes return NEOF; 1481556Srgrimes if (t == TNL) 1491556Srgrimes return NULL; 1501556Srgrimes tokpushback++; 1511556Srgrimes return list(1); 1521556Srgrimes} 1531556Srgrimes 1541556Srgrimes 1551556SrgrimesSTATIC union node * 15620425Sstevelist(nlflag) 15717987Speter 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(); 19617987Speter /* fall through */ 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 * 2271556Srgrimesandor() { 2281556Srgrimes union node *n1, *n2, *n3; 2291556Srgrimes int t; 2301556Srgrimes 2311556Srgrimes n1 = pipeline(); 2321556Srgrimes for (;;) { 2331556Srgrimes if ((t = readtoken()) == TAND) { 2341556Srgrimes t = NAND; 2351556Srgrimes } else if (t == TOR) { 2361556Srgrimes t = NOR; 2371556Srgrimes } else { 2381556Srgrimes tokpushback++; 2391556Srgrimes return n1; 2401556Srgrimes } 2411556Srgrimes n2 = pipeline(); 2421556Srgrimes n3 = (union node *)stalloc(sizeof (struct nbinary)); 2431556Srgrimes n3->type = t; 2441556Srgrimes n3->nbinary.ch1 = n1; 2451556Srgrimes n3->nbinary.ch2 = n2; 2461556Srgrimes n1 = n3; 2471556Srgrimes } 2481556Srgrimes} 2491556Srgrimes 2501556Srgrimes 2511556Srgrimes 2521556SrgrimesSTATIC union node * 2531556Srgrimespipeline() { 25425230Ssteve union node *n1, *pipenode, *notnode; 2551556Srgrimes struct nodelist *lp, *prev; 25625230Ssteve int negate = 0; 2571556Srgrimes 2581556Srgrimes TRACE(("pipeline: entered\n")); 25925230Ssteve while (readtoken() == TNOT) { 26025230Ssteve TRACE(("pipeline: TNOT recognized\n")); 26125230Ssteve negate = !negate; 26225230Ssteve } 26325230Ssteve tokpushback++; 2641556Srgrimes n1 = command(); 2651556Srgrimes if (readtoken() == TPIPE) { 2661556Srgrimes pipenode = (union node *)stalloc(sizeof (struct npipe)); 2671556Srgrimes pipenode->type = NPIPE; 2681556Srgrimes pipenode->npipe.backgnd = 0; 2691556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 2701556Srgrimes pipenode->npipe.cmdlist = lp; 2711556Srgrimes lp->n = n1; 2721556Srgrimes do { 2731556Srgrimes prev = lp; 2741556Srgrimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 2751556Srgrimes lp->n = command(); 2761556Srgrimes prev->next = lp; 2771556Srgrimes } while (readtoken() == TPIPE); 2781556Srgrimes lp->next = NULL; 2791556Srgrimes n1 = pipenode; 2801556Srgrimes } 2811556Srgrimes tokpushback++; 28225230Ssteve if (negate) { 28325230Ssteve notnode = (union node *)stalloc(sizeof(struct nnot)); 28425230Ssteve notnode->type = NNOT; 28525230Ssteve notnode->nnot.com = n1; 28625230Ssteve n1 = notnode; 28725230Ssteve } 2881556Srgrimes return n1; 2891556Srgrimes} 2901556Srgrimes 2911556Srgrimes 2921556Srgrimes 2931556SrgrimesSTATIC union node * 2941556Srgrimescommand() { 2951556Srgrimes union node *n1, *n2; 2961556Srgrimes union node *ap, **app; 2971556Srgrimes union node *cp, **cpp; 2981556Srgrimes union node *redir, **rpp; 29925230Ssteve int t; 3001556Srgrimes 3011556Srgrimes checkkwd = 2; 30217987Speter redir = NULL; 30317987Speter n1 = NULL; 3041556Srgrimes rpp = &redir; 30520425Ssteve 3061556Srgrimes /* Check for redirection which may precede command */ 3071556Srgrimes while (readtoken() == TREDIR) { 3081556Srgrimes *rpp = n2 = redirnode; 3091556Srgrimes rpp = &n2->nfile.next; 3101556Srgrimes parsefname(); 3111556Srgrimes } 3121556Srgrimes tokpushback++; 3131556Srgrimes 3141556Srgrimes switch (readtoken()) { 3151556Srgrimes case TIF: 3161556Srgrimes n1 = (union node *)stalloc(sizeof (struct nif)); 3171556Srgrimes n1->type = NIF; 3181556Srgrimes n1->nif.test = list(0); 3191556Srgrimes if (readtoken() != TTHEN) 3201556Srgrimes synexpect(TTHEN); 3211556Srgrimes n1->nif.ifpart = list(0); 3221556Srgrimes n2 = n1; 3231556Srgrimes while (readtoken() == TELIF) { 3241556Srgrimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 3251556Srgrimes n2 = n2->nif.elsepart; 3261556Srgrimes n2->type = NIF; 3271556Srgrimes n2->nif.test = list(0); 3281556Srgrimes if (readtoken() != TTHEN) 3291556Srgrimes synexpect(TTHEN); 3301556Srgrimes n2->nif.ifpart = list(0); 3311556Srgrimes } 3321556Srgrimes if (lasttoken == TELSE) 3331556Srgrimes n2->nif.elsepart = list(0); 3341556Srgrimes else { 3351556Srgrimes n2->nif.elsepart = NULL; 3361556Srgrimes tokpushback++; 3371556Srgrimes } 3381556Srgrimes if (readtoken() != TFI) 3391556Srgrimes synexpect(TFI); 3401556Srgrimes checkkwd = 1; 3411556Srgrimes break; 3421556Srgrimes case TWHILE: 3431556Srgrimes case TUNTIL: { 3441556Srgrimes int got; 3451556Srgrimes n1 = (union node *)stalloc(sizeof (struct nbinary)); 3461556Srgrimes n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 3471556Srgrimes n1->nbinary.ch1 = list(0); 3481556Srgrimes if ((got=readtoken()) != TDO) { 3491556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 3501556Srgrimes synexpect(TDO); 3511556Srgrimes } 3521556Srgrimes n1->nbinary.ch2 = list(0); 3531556Srgrimes if (readtoken() != TDONE) 3541556Srgrimes synexpect(TDONE); 3551556Srgrimes checkkwd = 1; 3561556Srgrimes break; 3571556Srgrimes } 3581556Srgrimes case TFOR: 3591556Srgrimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 3601556Srgrimes synerror("Bad for loop variable"); 3611556Srgrimes n1 = (union node *)stalloc(sizeof (struct nfor)); 3621556Srgrimes n1->type = NFOR; 3631556Srgrimes n1->nfor.var = wordtext; 3641556Srgrimes if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) { 3651556Srgrimes app = ≈ 3661556Srgrimes while (readtoken() == TWORD) { 3671556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3681556Srgrimes n2->type = NARG; 3691556Srgrimes n2->narg.text = wordtext; 3701556Srgrimes n2->narg.backquote = backquotelist; 3711556Srgrimes *app = n2; 3721556Srgrimes app = &n2->narg.next; 3731556Srgrimes } 3741556Srgrimes *app = NULL; 3751556Srgrimes n1->nfor.args = ap; 3761556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 3771556Srgrimes synexpect(-1); 3781556Srgrimes } else { 3791556Srgrimes#ifndef GDB_HACK 3801556Srgrimes static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, 3811556Srgrimes '@', '=', '\0'}; 3821556Srgrimes#endif 3831556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3841556Srgrimes n2->type = NARG; 3851556Srgrimes n2->narg.text = (char *)argvars; 3861556Srgrimes n2->narg.backquote = NULL; 3871556Srgrimes n2->narg.next = NULL; 3881556Srgrimes n1->nfor.args = n2; 3891556Srgrimes /* 3901556Srgrimes * Newline or semicolon here is optional (but note 3911556Srgrimes * that the original Bourne shell only allowed NL). 3921556Srgrimes */ 3931556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 3941556Srgrimes tokpushback++; 3951556Srgrimes } 3961556Srgrimes checkkwd = 2; 3971556Srgrimes if ((t = readtoken()) == TDO) 3981556Srgrimes t = TDONE; 3991556Srgrimes else if (t == TBEGIN) 4001556Srgrimes t = TEND; 4011556Srgrimes else 4021556Srgrimes synexpect(-1); 4031556Srgrimes n1->nfor.body = list(0); 4041556Srgrimes if (readtoken() != t) 4051556Srgrimes synexpect(t); 4061556Srgrimes checkkwd = 1; 4071556Srgrimes break; 4081556Srgrimes case TCASE: 4091556Srgrimes n1 = (union node *)stalloc(sizeof (struct ncase)); 4101556Srgrimes n1->type = NCASE; 4111556Srgrimes if (readtoken() != TWORD) 4121556Srgrimes synexpect(TWORD); 4131556Srgrimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 4141556Srgrimes n2->type = NARG; 4151556Srgrimes n2->narg.text = wordtext; 4161556Srgrimes n2->narg.backquote = backquotelist; 4171556Srgrimes n2->narg.next = NULL; 4181556Srgrimes while (readtoken() == TNL); 4191556Srgrimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 4201556Srgrimes synerror("expecting \"in\""); 4211556Srgrimes cpp = &n1->ncase.cases; 42218018Speter noaliases = 1; /* turn off alias expansion */ 4232760Ssef checkkwd = 2, readtoken(); 4242760Ssef do { 4251556Srgrimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 4261556Srgrimes cp->type = NCLIST; 4271556Srgrimes app = &cp->nclist.pattern; 4281556Srgrimes for (;;) { 4291556Srgrimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 4301556Srgrimes ap->type = NARG; 4311556Srgrimes ap->narg.text = wordtext; 4321556Srgrimes ap->narg.backquote = backquotelist; 4332760Ssef if (checkkwd = 2, readtoken() != TPIPE) 4341556Srgrimes break; 4351556Srgrimes app = &ap->narg.next; 4362760Ssef readtoken(); 4371556Srgrimes } 4381556Srgrimes ap->narg.next = NULL; 4391556Srgrimes if (lasttoken != TRP) 44018018Speter noaliases = 0, synexpect(TRP); 4411556Srgrimes cp->nclist.body = list(0); 4422760Ssef 4432760Ssef checkkwd = 2; 4442760Ssef if ((t = readtoken()) != TESAC) { 4452760Ssef if (t != TENDCASE) 44618018Speter noaliases = 0, synexpect(TENDCASE); 4472760Ssef else 4482760Ssef checkkwd = 2, readtoken(); 4492760Ssef } 4501556Srgrimes cpp = &cp->nclist.next; 4512760Ssef } while(lasttoken != TESAC); 45218018Speter noaliases = 0; /* reset alias expansion */ 4531556Srgrimes *cpp = NULL; 4541556Srgrimes checkkwd = 1; 4551556Srgrimes break; 4561556Srgrimes case TLP: 4571556Srgrimes n1 = (union node *)stalloc(sizeof (struct nredir)); 4581556Srgrimes n1->type = NSUBSHELL; 4591556Srgrimes n1->nredir.n = list(0); 4601556Srgrimes n1->nredir.redirect = NULL; 4611556Srgrimes if (readtoken() != TRP) 4621556Srgrimes synexpect(TRP); 4631556Srgrimes checkkwd = 1; 4641556Srgrimes break; 4651556Srgrimes case TBEGIN: 4661556Srgrimes n1 = list(0); 4671556Srgrimes if (readtoken() != TEND) 4681556Srgrimes synexpect(TEND); 4691556Srgrimes checkkwd = 1; 4701556Srgrimes break; 4711556Srgrimes /* Handle an empty command like other simple commands. */ 47217987Speter case TSEMI: 47317987Speter /* 47417987Speter * An empty command before a ; doesn't make much sense, and 47517987Speter * should certainly be disallowed in the case of `if ;'. 47617987Speter */ 47717987Speter if (!redir) 47817987Speter synexpect(-1); 47920425Ssteve case TAND: 48020425Ssteve case TOR: 4811556Srgrimes case TNL: 48210399Sjoerg case TEOF: 4831556Srgrimes case TWORD: 48417987Speter case TRP: 4851556Srgrimes tokpushback++; 48625230Ssteve return simplecmd(rpp, redir); 4871556Srgrimes default: 4881556Srgrimes synexpect(-1); 4891556Srgrimes } 4901556Srgrimes 4911556Srgrimes /* Now check for redirection which may follow command */ 4921556Srgrimes while (readtoken() == TREDIR) { 4931556Srgrimes *rpp = n2 = redirnode; 4941556Srgrimes rpp = &n2->nfile.next; 4951556Srgrimes parsefname(); 4961556Srgrimes } 4971556Srgrimes tokpushback++; 4981556Srgrimes *rpp = NULL; 4991556Srgrimes if (redir) { 5001556Srgrimes if (n1->type != NSUBSHELL) { 5011556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 5021556Srgrimes n2->type = NREDIR; 5031556Srgrimes n2->nredir.n = n1; 5041556Srgrimes n1 = n2; 5051556Srgrimes } 5061556Srgrimes n1->nredir.redirect = redir; 5071556Srgrimes } 50825230Ssteve return n1; 5091556Srgrimes} 5101556Srgrimes 5111556Srgrimes 5121556SrgrimesSTATIC union node * 5138855Srgrimessimplecmd(rpp, redir) 5141556Srgrimes union node **rpp, *redir; 5151556Srgrimes { 5161556Srgrimes union node *args, **app; 5171556Srgrimes union node **orig_rpp = rpp; 51825230Ssteve union node *n = NULL; 5191556Srgrimes 5201556Srgrimes /* If we don't have any redirections already, then we must reset */ 5211556Srgrimes /* rpp to be the address of the local redir variable. */ 5221556Srgrimes if (redir == 0) 5231556Srgrimes rpp = &redir; 5241556Srgrimes 5251556Srgrimes args = NULL; 5261556Srgrimes app = &args; 5278855Srgrimes /* 5281556Srgrimes * We save the incoming value, because we need this for shell 5291556Srgrimes * functions. There can not be a redirect or an argument between 5308855Srgrimes * the function name and the open parenthesis. 5311556Srgrimes */ 5321556Srgrimes orig_rpp = rpp; 5331556Srgrimes 5341556Srgrimes for (;;) { 5351556Srgrimes if (readtoken() == TWORD) { 5361556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 5371556Srgrimes n->type = NARG; 5381556Srgrimes n->narg.text = wordtext; 5391556Srgrimes n->narg.backquote = backquotelist; 5401556Srgrimes *app = n; 5411556Srgrimes app = &n->narg.next; 5421556Srgrimes } else if (lasttoken == TREDIR) { 5431556Srgrimes *rpp = n = redirnode; 5441556Srgrimes rpp = &n->nfile.next; 5451556Srgrimes parsefname(); /* read name of redirection file */ 5461556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 5471556Srgrimes && rpp == orig_rpp) { 5481556Srgrimes /* We have a function */ 5491556Srgrimes if (readtoken() != TRP) 5501556Srgrimes synexpect(TRP); 5511556Srgrimes#ifdef notdef 5521556Srgrimes if (! goodname(n->narg.text)) 5531556Srgrimes synerror("Bad function name"); 5541556Srgrimes#endif 5551556Srgrimes n->type = NDEFUN; 5561556Srgrimes n->narg.next = command(); 55725230Ssteve return n; 5581556Srgrimes } else { 5591556Srgrimes tokpushback++; 5601556Srgrimes break; 5611556Srgrimes } 5621556Srgrimes } 5631556Srgrimes *app = NULL; 5641556Srgrimes *rpp = NULL; 5651556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 5661556Srgrimes n->type = NCMD; 5671556Srgrimes n->ncmd.backgnd = 0; 5681556Srgrimes n->ncmd.args = args; 5691556Srgrimes n->ncmd.redirect = redir; 57025230Ssteve return n; 5711556Srgrimes} 5721556Srgrimes 57317987SpeterSTATIC union node * 57417987Spetermakename() { 57517987Speter union node *n; 5761556Srgrimes 57717987Speter n = (union node *)stalloc(sizeof (struct narg)); 57817987Speter n->type = NARG; 57917987Speter n->narg.next = NULL; 58017987Speter n->narg.text = wordtext; 58117987Speter n->narg.backquote = backquotelist; 58217987Speter return n; 58317987Speter} 58417987Speter 58517987Spetervoid fixredir(n, text, err) 58617987Speter union node *n; 58717987Speter const char *text; 58817987Speter int err; 58917987Speter { 59017987Speter TRACE(("Fix redir %s %d\n", text, err)); 59117987Speter if (!err) 59217987Speter n->ndup.vname = NULL; 59317987Speter 59417987Speter if (is_digit(text[0]) && text[1] == '\0') 59517987Speter n->ndup.dupfd = digit_val(text[0]); 59617987Speter else if (text[0] == '-' && text[1] == '\0') 59717987Speter n->ndup.dupfd = -1; 59817987Speter else { 59920425Ssteve 60017987Speter if (err) 60117987Speter synerror("Bad fd number"); 60217987Speter else 60317987Speter n->ndup.vname = makename(); 60417987Speter } 60517987Speter} 60617987Speter 60717987Speter 6081556SrgrimesSTATIC void 6091556Srgrimesparsefname() { 6101556Srgrimes union node *n = redirnode; 6111556Srgrimes 6121556Srgrimes if (readtoken() != TWORD) 6131556Srgrimes synexpect(-1); 6141556Srgrimes if (n->type == NHERE) { 6151556Srgrimes struct heredoc *here = heredoc; 6161556Srgrimes struct heredoc *p; 6171556Srgrimes int i; 6181556Srgrimes 6191556Srgrimes if (quoteflag == 0) 6201556Srgrimes n->type = NXHERE; 6211556Srgrimes TRACE(("Here document %d\n", n->type)); 6221556Srgrimes if (here->striptabs) { 6231556Srgrimes while (*wordtext == '\t') 6241556Srgrimes wordtext++; 6251556Srgrimes } 6261556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 6271556Srgrimes synerror("Illegal eof marker for << redirection"); 6281556Srgrimes rmescapes(wordtext); 6291556Srgrimes here->eofmark = wordtext; 6301556Srgrimes here->next = NULL; 6311556Srgrimes if (heredoclist == NULL) 6321556Srgrimes heredoclist = here; 6331556Srgrimes else { 6341556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 6351556Srgrimes p->next = here; 6361556Srgrimes } 6371556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 63817987Speter fixredir(n, wordtext, 0); 6391556Srgrimes } else { 64017987Speter n->nfile.fname = makename(); 6411556Srgrimes } 6421556Srgrimes} 6431556Srgrimes 6441556Srgrimes 6451556Srgrimes/* 6461556Srgrimes * Input any here documents. 6471556Srgrimes */ 6481556Srgrimes 6491556SrgrimesSTATIC void 6501556Srgrimesparseheredoc() { 6511556Srgrimes struct heredoc *here; 6521556Srgrimes union node *n; 6531556Srgrimes 6541556Srgrimes while (heredoclist) { 6551556Srgrimes here = heredoclist; 6561556Srgrimes heredoclist = here->next; 6571556Srgrimes if (needprompt) { 6581556Srgrimes setprompt(2); 6591556Srgrimes needprompt = 0; 6601556Srgrimes } 6611556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 6621556Srgrimes here->eofmark, here->striptabs); 6631556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 6641556Srgrimes n->narg.type = NARG; 6651556Srgrimes n->narg.next = NULL; 6661556Srgrimes n->narg.text = wordtext; 6671556Srgrimes n->narg.backquote = backquotelist; 6681556Srgrimes here->here->nhere.doc = n; 6691556Srgrimes } 6701556Srgrimes} 6711556Srgrimes 6721556SrgrimesSTATIC int 6731556Srgrimespeektoken() { 6741556Srgrimes int t; 6751556Srgrimes 6761556Srgrimes t = readtoken(); 6771556Srgrimes tokpushback++; 6781556Srgrimes return (t); 6791556Srgrimes} 6801556Srgrimes 6811556SrgrimesSTATIC int xxreadtoken(); 6821556Srgrimes 6831556SrgrimesSTATIC int 6841556Srgrimesreadtoken() { 6851556Srgrimes int t; 6861556Srgrimes int savecheckkwd = checkkwd; 6871556Srgrimes struct alias *ap; 6881556Srgrimes#ifdef DEBUG 6891556Srgrimes int alreadyseen = tokpushback; 6901556Srgrimes#endif 6918855Srgrimes 6921556Srgrimes top: 6931556Srgrimes t = xxreadtoken(); 6941556Srgrimes 6951556Srgrimes if (checkkwd) { 6961556Srgrimes /* 6971556Srgrimes * eat newlines 6981556Srgrimes */ 6991556Srgrimes if (checkkwd == 2) { 7001556Srgrimes checkkwd = 0; 7011556Srgrimes while (t == TNL) { 7021556Srgrimes parseheredoc(); 7031556Srgrimes t = xxreadtoken(); 7041556Srgrimes } 7051556Srgrimes } else 7061556Srgrimes checkkwd = 0; 7071556Srgrimes /* 7081556Srgrimes * check for keywords and aliases 7091556Srgrimes */ 71020425Ssteve if (t == TWORD && !quoteflag) 71117987Speter { 71225230Ssteve char * const *pp; 7131556Srgrimes 7141556Srgrimes for (pp = (char **)parsekwd; *pp; pp++) { 71520425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 71617987Speter { 7171556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 7181556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 7191556Srgrimes goto out; 7201556Srgrimes } 7211556Srgrimes } 72218018Speter if (noaliases == 0 && 72318018Speter (ap = lookupalias(wordtext, 1)) != NULL) { 7241556Srgrimes pushstring(ap->val, strlen(ap->val), ap); 7251556Srgrimes checkkwd = savecheckkwd; 7261556Srgrimes goto top; 7271556Srgrimes } 7281556Srgrimes } 7291556Srgrimesout: 73025230Ssteve checkkwd = 0; 7311556Srgrimes } 7321556Srgrimes#ifdef DEBUG 7331556Srgrimes if (!alreadyseen) 7341556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7351556Srgrimes else 7361556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7371556Srgrimes#endif 7381556Srgrimes return (t); 7391556Srgrimes} 7401556Srgrimes 7411556Srgrimes 7421556Srgrimes/* 7431556Srgrimes * Read the next input token. 7441556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 7451556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 7461556Srgrimes * quoted. 7471556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 7481556Srgrimes * the redirection. 7491556Srgrimes * In all cases, the variable startlinno is set to the number of the line 7501556Srgrimes * on which the token starts. 7511556Srgrimes * 7521556Srgrimes * [Change comment: here documents and internal procedures] 7531556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 7541556Srgrimes * word parsing code into a separate routine. In this case, readtoken 7551556Srgrimes * doesn't need to have any internal procedures, but parseword does. 7561556Srgrimes * We could also make parseoperator in essence the main routine, and 7571556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 7581556Srgrimes */ 7591556Srgrimes 7601556Srgrimes#define RETURN(token) return lasttoken = token 7611556Srgrimes 7621556SrgrimesSTATIC int 7631556Srgrimesxxreadtoken() { 76425230Ssteve int c; 7651556Srgrimes 7661556Srgrimes if (tokpushback) { 7671556Srgrimes tokpushback = 0; 7681556Srgrimes return lasttoken; 7691556Srgrimes } 7701556Srgrimes if (needprompt) { 7711556Srgrimes setprompt(2); 7721556Srgrimes needprompt = 0; 7731556Srgrimes } 7741556Srgrimes startlinno = plinno; 7751556Srgrimes for (;;) { /* until token or start of word found */ 7761556Srgrimes c = pgetc_macro(); 7771556Srgrimes if (c == ' ' || c == '\t') 7781556Srgrimes continue; /* quick check for white space first */ 7791556Srgrimes switch (c) { 7801556Srgrimes case ' ': case '\t': 7811556Srgrimes continue; 7821556Srgrimes case '#': 7831556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 7841556Srgrimes pungetc(); 7851556Srgrimes continue; 7861556Srgrimes case '\\': 7871556Srgrimes if (pgetc() == '\n') { 7881556Srgrimes startlinno = ++plinno; 7891556Srgrimes if (doprompt) 7901556Srgrimes setprompt(2); 7911556Srgrimes else 7921556Srgrimes setprompt(0); 7931556Srgrimes continue; 7941556Srgrimes } 7951556Srgrimes pungetc(); 7961556Srgrimes goto breakloop; 7971556Srgrimes case '\n': 7981556Srgrimes plinno++; 7991556Srgrimes needprompt = doprompt; 8001556Srgrimes RETURN(TNL); 8011556Srgrimes case PEOF: 8021556Srgrimes RETURN(TEOF); 8031556Srgrimes case '&': 8041556Srgrimes if (pgetc() == '&') 8051556Srgrimes RETURN(TAND); 8061556Srgrimes pungetc(); 8071556Srgrimes RETURN(TBACKGND); 8081556Srgrimes case '|': 8091556Srgrimes if (pgetc() == '|') 8101556Srgrimes RETURN(TOR); 8111556Srgrimes pungetc(); 8121556Srgrimes RETURN(TPIPE); 8131556Srgrimes case ';': 8141556Srgrimes if (pgetc() == ';') 8151556Srgrimes RETURN(TENDCASE); 8161556Srgrimes pungetc(); 8171556Srgrimes RETURN(TSEMI); 8181556Srgrimes case '(': 8191556Srgrimes RETURN(TLP); 8201556Srgrimes case ')': 8211556Srgrimes RETURN(TRP); 8221556Srgrimes default: 8231556Srgrimes goto breakloop; 8241556Srgrimes } 8251556Srgrimes } 8261556Srgrimesbreakloop: 8271556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 8281556Srgrimes#undef RETURN 8291556Srgrimes} 8301556Srgrimes 8311556Srgrimes 8321556Srgrimes 8331556Srgrimes/* 8341556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 8351556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 8361556Srgrimes * word which marks the end of the document and striptabs is true if 8371556Srgrimes * leading tabs should be stripped from the document. The argument firstc 8381556Srgrimes * is the first character of the input token or document. 8391556Srgrimes * 8401556Srgrimes * Because C does not have internal subroutines, I have simulated them 8411556Srgrimes * using goto's to implement the subroutine linkage. The following macros 8421556Srgrimes * will run code that appears at the end of readtoken1. 8431556Srgrimes */ 8441556Srgrimes 8451556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 8461556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 8471556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 8481556Srgrimes#define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 8491556Srgrimes#define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 8501556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 8511556Srgrimes 8521556SrgrimesSTATIC int 8531556Srgrimesreadtoken1(firstc, syntax, eofmark, striptabs) 8541556Srgrimes int firstc; 8551556Srgrimes char const *syntax; 8561556Srgrimes char *eofmark; 8571556Srgrimes int striptabs; 8581556Srgrimes { 85917987Speter int c = firstc; 86017987Speter char *out; 8611556Srgrimes int len; 8621556Srgrimes char line[EOFMARKLEN + 1]; 8631556Srgrimes struct nodelist *bqlist; 8641556Srgrimes int quotef; 8651556Srgrimes int dblquote; 8661556Srgrimes int varnest; /* levels of variables expansion */ 8671556Srgrimes int arinest; /* levels of arithmetic expansion */ 8681556Srgrimes int parenlevel; /* levels of parens in arithmetic */ 8691556Srgrimes int oldstyle; 8701556Srgrimes char const *prevsyntax; /* syntax before arithmetic */ 87154679Scracauer int synentry; 87217987Speter#if __GNUC__ 87317987Speter /* Avoid longjmp clobbering */ 87417987Speter (void) &out; 87517987Speter (void) "ef; 87617987Speter (void) &dblquote; 87717987Speter (void) &varnest; 87817987Speter (void) &arinest; 87917987Speter (void) &parenlevel; 88017987Speter (void) &oldstyle; 88117987Speter (void) &prevsyntax; 88217987Speter (void) &syntax; 88354679Scracauer (void) &synentry; 88417987Speter#endif 8851556Srgrimes 8861556Srgrimes startlinno = plinno; 8871556Srgrimes dblquote = 0; 8881556Srgrimes if (syntax == DQSYNTAX) 8891556Srgrimes dblquote = 1; 8901556Srgrimes quotef = 0; 8911556Srgrimes bqlist = NULL; 8921556Srgrimes varnest = 0; 8931556Srgrimes arinest = 0; 8941556Srgrimes parenlevel = 0; 8951556Srgrimes 8961556Srgrimes STARTSTACKSTR(out); 8971556Srgrimes loop: { /* for each line, until end of word */ 8981556Srgrimes#if ATTY 8991556Srgrimes if (c == '\034' && doprompt 9001556Srgrimes && attyset() && ! equal(termval(), "emacs")) { 9011556Srgrimes attyline(); 9021556Srgrimes if (syntax == BASESYNTAX) 9031556Srgrimes return readtoken(); 9041556Srgrimes c = pgetc(); 9051556Srgrimes goto loop; 9061556Srgrimes } 9071556Srgrimes#endif 9081556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 9091556Srgrimes for (;;) { /* until end of line or end of word */ 9101556Srgrimes CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 91154679Scracauer 91254679Scracauer if (c < 0 && c != PEOF) 91354679Scracauer synentry = CWORD; 91454679Scracauer else 91554679Scracauer synentry = syntax[c]; 91654679Scracauer 91754679Scracauer switch(synentry) { 9181556Srgrimes case CNL: /* '\n' */ 9191556Srgrimes if (syntax == BASESYNTAX) 9201556Srgrimes goto endword; /* exit outer loop */ 9211556Srgrimes USTPUTC(c, out); 9221556Srgrimes plinno++; 9231556Srgrimes if (doprompt) 9241556Srgrimes setprompt(2); 9251556Srgrimes else 9261556Srgrimes setprompt(0); 9271556Srgrimes c = pgetc(); 9281556Srgrimes goto loop; /* continue outer loop */ 9291556Srgrimes case CWORD: 9301556Srgrimes USTPUTC(c, out); 9311556Srgrimes break; 9321556Srgrimes case CCTL: 9331556Srgrimes if (eofmark == NULL || dblquote) 9341556Srgrimes USTPUTC(CTLESC, out); 9351556Srgrimes USTPUTC(c, out); 9361556Srgrimes break; 9371556Srgrimes case CBACK: /* backslash */ 9381556Srgrimes c = pgetc(); 9391556Srgrimes if (c == PEOF) { 9401556Srgrimes USTPUTC('\\', out); 9411556Srgrimes pungetc(); 9421556Srgrimes } else if (c == '\n') { 9431556Srgrimes if (doprompt) 9441556Srgrimes setprompt(2); 9451556Srgrimes else 9461556Srgrimes setprompt(0); 9471556Srgrimes } else { 94854631Scracauer if (dblquote && c != '\\' && 94954631Scracauer c != '`' && c != '$' && 95054631Scracauer (c != '"' || eofmark != NULL)) 9511556Srgrimes USTPUTC('\\', out); 95254631Scracauer if (c >= 0 && SQSYNTAX[c] == CCTL) 9531556Srgrimes USTPUTC(CTLESC, out); 95439137Stegge else if (eofmark == NULL) 95538887Stegge USTPUTC(CTLQUOTEMARK, out); 9561556Srgrimes USTPUTC(c, out); 9571556Srgrimes quotef++; 9581556Srgrimes } 9591556Srgrimes break; 9601556Srgrimes case CSQUOTE: 96139137Stegge if (eofmark == NULL) 96239137Stegge USTPUTC(CTLQUOTEMARK, out); 9631556Srgrimes syntax = SQSYNTAX; 9641556Srgrimes break; 9651556Srgrimes case CDQUOTE: 96639137Stegge if (eofmark == NULL) 96739137Stegge USTPUTC(CTLQUOTEMARK, out); 9681556Srgrimes syntax = DQSYNTAX; 9691556Srgrimes dblquote = 1; 9701556Srgrimes break; 9711556Srgrimes case CENDQUOTE: 97239137Stegge if (eofmark != NULL && arinest == 0 && 97339137Stegge varnest == 0) { 9741556Srgrimes USTPUTC(c, out); 9751556Srgrimes } else { 97639137Stegge if (arinest) { 9771556Srgrimes syntax = ARISYNTAX; 97839137Stegge dblquote = 0; 97939137Stegge } else if (eofmark == NULL) { 9801556Srgrimes syntax = BASESYNTAX; 98139137Stegge dblquote = 0; 98239137Stegge } 9831556Srgrimes quotef++; 9841556Srgrimes } 9851556Srgrimes break; 9861556Srgrimes case CVAR: /* '$' */ 9871556Srgrimes PARSESUB(); /* parse substitution */ 9881556Srgrimes break; 9891556Srgrimes case CENDVAR: /* '}' */ 9901556Srgrimes if (varnest > 0) { 9911556Srgrimes varnest--; 9921556Srgrimes USTPUTC(CTLENDVAR, out); 9931556Srgrimes } else { 9941556Srgrimes USTPUTC(c, out); 9951556Srgrimes } 9961556Srgrimes break; 9971556Srgrimes case CLP: /* '(' in arithmetic */ 9981556Srgrimes parenlevel++; 9991556Srgrimes USTPUTC(c, out); 10001556Srgrimes break; 10011556Srgrimes case CRP: /* ')' in arithmetic */ 10021556Srgrimes if (parenlevel > 0) { 10031556Srgrimes USTPUTC(c, out); 10041556Srgrimes --parenlevel; 10051556Srgrimes } else { 10061556Srgrimes if (pgetc() == ')') { 10071556Srgrimes if (--arinest == 0) { 10081556Srgrimes USTPUTC(CTLENDARI, out); 10091556Srgrimes syntax = prevsyntax; 101039137Stegge if (syntax == DQSYNTAX) 101139137Stegge dblquote = 1; 101239137Stegge else 101339137Stegge dblquote = 0; 10141556Srgrimes } else 10151556Srgrimes USTPUTC(')', out); 10161556Srgrimes } else { 10178855Srgrimes /* 10181556Srgrimes * unbalanced parens 10191556Srgrimes * (don't 2nd guess - no error) 10201556Srgrimes */ 10211556Srgrimes pungetc(); 10221556Srgrimes USTPUTC(')', out); 10231556Srgrimes } 10241556Srgrimes } 10251556Srgrimes break; 10261556Srgrimes case CBQUOTE: /* '`' */ 10271556Srgrimes PARSEBACKQOLD(); 10281556Srgrimes break; 10291556Srgrimes case CEOF: 10301556Srgrimes goto endword; /* exit outer loop */ 10311556Srgrimes default: 10321556Srgrimes if (varnest == 0) 10331556Srgrimes goto endword; /* exit outer loop */ 10341556Srgrimes USTPUTC(c, out); 10351556Srgrimes } 10361556Srgrimes c = pgetc_macro(); 10371556Srgrimes } 10381556Srgrimes } 10391556Srgrimesendword: 10401556Srgrimes if (syntax == ARISYNTAX) 10411556Srgrimes synerror("Missing '))'"); 10421556Srgrimes if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) 10431556Srgrimes synerror("Unterminated quoted string"); 10441556Srgrimes if (varnest != 0) { 10451556Srgrimes startlinno = plinno; 10461556Srgrimes synerror("Missing '}'"); 10471556Srgrimes } 10481556Srgrimes USTPUTC('\0', out); 10491556Srgrimes len = out - stackblock(); 10501556Srgrimes out = stackblock(); 10511556Srgrimes if (eofmark == NULL) { 10521556Srgrimes if ((c == '>' || c == '<') 10531556Srgrimes && quotef == 0 10541556Srgrimes && len <= 2 10551556Srgrimes && (*out == '\0' || is_digit(*out))) { 10561556Srgrimes PARSEREDIR(); 10571556Srgrimes return lasttoken = TREDIR; 10581556Srgrimes } else { 10591556Srgrimes pungetc(); 10601556Srgrimes } 10611556Srgrimes } 10621556Srgrimes quoteflag = quotef; 10631556Srgrimes backquotelist = bqlist; 10641556Srgrimes grabstackblock(len); 10651556Srgrimes wordtext = out; 10661556Srgrimes return lasttoken = TWORD; 10671556Srgrimes/* end of readtoken routine */ 10681556Srgrimes 10691556Srgrimes 10701556Srgrimes 10711556Srgrimes/* 10721556Srgrimes * Check to see whether we are at the end of the here document. When this 10731556Srgrimes * is called, c is set to the first character of the next input line. If 10741556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 10751556Srgrimes */ 10761556Srgrimes 10771556Srgrimescheckend: { 10781556Srgrimes if (eofmark) { 10791556Srgrimes if (striptabs) { 10801556Srgrimes while (c == '\t') 10811556Srgrimes c = pgetc(); 10821556Srgrimes } 10831556Srgrimes if (c == *eofmark) { 10841556Srgrimes if (pfgets(line, sizeof line) != NULL) { 108525230Ssteve char *p, *q; 10861556Srgrimes 10871556Srgrimes p = line; 10881556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 10891556Srgrimes if (*p == '\n' && *q == '\0') { 10901556Srgrimes c = PEOF; 10911556Srgrimes plinno++; 10921556Srgrimes needprompt = doprompt; 10931556Srgrimes } else { 10941556Srgrimes pushstring(line, strlen(line), NULL); 10951556Srgrimes } 10961556Srgrimes } 10971556Srgrimes } 10981556Srgrimes } 10991556Srgrimes goto checkend_return; 11001556Srgrimes} 11011556Srgrimes 11021556Srgrimes 11031556Srgrimes/* 11041556Srgrimes * Parse a redirection operator. The variable "out" points to a string 11051556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 11061556Srgrimes * first character of the redirection operator. 11071556Srgrimes */ 11081556Srgrimes 11091556Srgrimesparseredir: { 11101556Srgrimes char fd = *out; 11111556Srgrimes union node *np; 11121556Srgrimes 11131556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 11141556Srgrimes if (c == '>') { 11151556Srgrimes np->nfile.fd = 1; 11161556Srgrimes c = pgetc(); 11171556Srgrimes if (c == '>') 11181556Srgrimes np->type = NAPPEND; 11191556Srgrimes else if (c == '&') 11201556Srgrimes np->type = NTOFD; 11211556Srgrimes else { 11221556Srgrimes np->type = NTO; 11231556Srgrimes pungetc(); 11241556Srgrimes } 11251556Srgrimes } else { /* c == '<' */ 11261556Srgrimes np->nfile.fd = 0; 11271556Srgrimes c = pgetc(); 11281556Srgrimes if (c == '<') { 11291556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 11301556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 11311556Srgrimes np->nfile.fd = 0; 11321556Srgrimes } 11331556Srgrimes np->type = NHERE; 11341556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 11351556Srgrimes heredoc->here = np; 11361556Srgrimes if ((c = pgetc()) == '-') { 11371556Srgrimes heredoc->striptabs = 1; 11381556Srgrimes } else { 11391556Srgrimes heredoc->striptabs = 0; 11401556Srgrimes pungetc(); 11411556Srgrimes } 11421556Srgrimes } else if (c == '&') 11431556Srgrimes np->type = NFROMFD; 11441556Srgrimes else { 11451556Srgrimes np->type = NFROM; 11461556Srgrimes pungetc(); 11471556Srgrimes } 11481556Srgrimes } 11491556Srgrimes if (fd != '\0') 11501556Srgrimes np->nfile.fd = digit_val(fd); 11511556Srgrimes redirnode = np; 11521556Srgrimes goto parseredir_return; 11531556Srgrimes} 11541556Srgrimes 11551556Srgrimes 11561556Srgrimes/* 11571556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 11581556Srgrimes * and nothing else. 11591556Srgrimes */ 11601556Srgrimes 11611556Srgrimesparsesub: { 11621556Srgrimes int subtype; 11631556Srgrimes int typeloc; 11641556Srgrimes int flags; 11651556Srgrimes char *p; 11661556Srgrimes#ifndef GDB_HACK 11671556Srgrimes static const char types[] = "}-+?="; 11681556Srgrimes#endif 116918202Speter int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 11701556Srgrimes 11711556Srgrimes c = pgetc(); 11721556Srgrimes if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) { 11731556Srgrimes USTPUTC('$', out); 11741556Srgrimes pungetc(); 11751556Srgrimes } else if (c == '(') { /* $(command) or $((arith)) */ 11761556Srgrimes if (pgetc() == '(') { 11771556Srgrimes PARSEARITH(); 11781556Srgrimes } else { 11791556Srgrimes pungetc(); 11801556Srgrimes PARSEBACKQNEW(); 11811556Srgrimes } 11821556Srgrimes } else { 11831556Srgrimes USTPUTC(CTLVAR, out); 11841556Srgrimes typeloc = out - stackblock(); 11851556Srgrimes USTPUTC(VSNORMAL, out); 11861556Srgrimes subtype = VSNORMAL; 11871556Srgrimes if (c == '{') { 118818202Speter bracketed_name = 1; 11891556Srgrimes c = pgetc(); 119017987Speter if (c == '#') { 119117987Speter if ((c = pgetc()) == '}') 119217987Speter c = '#'; 119317987Speter else 119417987Speter subtype = VSLENGTH; 119517987Speter } 119617987Speter else 119717987Speter subtype = 0; 11981556Srgrimes } 11991556Srgrimes if (is_name(c)) { 12001556Srgrimes do { 12011556Srgrimes STPUTC(c, out); 12021556Srgrimes c = pgetc(); 12031556Srgrimes } while (is_in_name(c)); 120418202Speter } else if (is_digit(c)) { 120518202Speter if (bracketed_name) { 120618202Speter do { 120718202Speter STPUTC(c, out); 120818202Speter c = pgetc(); 120918202Speter } while (is_digit(c)); 121018202Speter } else { 121118202Speter STPUTC(c, out); 121218202Speter c = pgetc(); 121318202Speter } 12141556Srgrimes } else { 12151556Srgrimes if (! is_special(c)) 12161556Srgrimesbadsub: synerror("Bad substitution"); 12171556Srgrimes USTPUTC(c, out); 12181556Srgrimes c = pgetc(); 12191556Srgrimes } 12201556Srgrimes STPUTC('=', out); 12211556Srgrimes flags = 0; 12221556Srgrimes if (subtype == 0) { 122317987Speter switch (c) { 122417987Speter case ':': 12251556Srgrimes flags = VSNUL; 12261556Srgrimes c = pgetc(); 122717987Speter /*FALLTHROUGH*/ 122817987Speter default: 122917987Speter p = strchr(types, c); 123017987Speter if (p == NULL) 123117987Speter goto badsub; 123217987Speter subtype = p - types + VSNORMAL; 123317987Speter break; 123417987Speter case '%': 123520425Ssteve case '#': 123617987Speter { 123717987Speter int cc = c; 123817987Speter subtype = c == '#' ? VSTRIMLEFT : 123917987Speter VSTRIMRIGHT; 124017987Speter c = pgetc(); 124117987Speter if (c == cc) 124217987Speter subtype++; 124317987Speter else 124417987Speter pungetc(); 124517987Speter break; 124617987Speter } 12471556Srgrimes } 12481556Srgrimes } else { 12491556Srgrimes pungetc(); 12501556Srgrimes } 12511556Srgrimes if (dblquote || arinest) 12521556Srgrimes flags |= VSQUOTE; 12531556Srgrimes *(stackblock() + typeloc) = subtype | flags; 12541556Srgrimes if (subtype != VSNORMAL) 12551556Srgrimes varnest++; 12561556Srgrimes } 12571556Srgrimes goto parsesub_return; 12581556Srgrimes} 12591556Srgrimes 12601556Srgrimes 12611556Srgrimes/* 12621556Srgrimes * Called to parse command substitutions. Newstyle is set if the command 12631556Srgrimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked 12641556Srgrimes * list of commands (passed by reference), and savelen is the number of 12651556Srgrimes * characters on the top of the stack which must be preserved. 12661556Srgrimes */ 12671556Srgrimes 12681556Srgrimesparsebackq: { 12691556Srgrimes struct nodelist **nlpp; 12701556Srgrimes int savepbq; 12711556Srgrimes union node *n; 12721556Srgrimes char *volatile str; 12731556Srgrimes struct jmploc jmploc; 12741556Srgrimes struct jmploc *volatile savehandler; 12751556Srgrimes int savelen; 127620425Ssteve int saveprompt; 127720425Ssteve#if __GNUC__ 127820425Ssteve /* Avoid longjmp clobbering */ 127920425Ssteve (void) &saveprompt; 128020425Ssteve#endif 12811556Srgrimes 12821556Srgrimes savepbq = parsebackquote; 12831556Srgrimes if (setjmp(jmploc.loc)) { 12841556Srgrimes if (str) 12851556Srgrimes ckfree(str); 12861556Srgrimes parsebackquote = 0; 12871556Srgrimes handler = savehandler; 12881556Srgrimes longjmp(handler->loc, 1); 12891556Srgrimes } 12901556Srgrimes INTOFF; 12911556Srgrimes str = NULL; 12921556Srgrimes savelen = out - stackblock(); 12931556Srgrimes if (savelen > 0) { 12941556Srgrimes str = ckmalloc(savelen); 129517987Speter memcpy(str, stackblock(), savelen); 12961556Srgrimes } 12971556Srgrimes savehandler = handler; 12981556Srgrimes handler = &jmploc; 12991556Srgrimes INTON; 13001556Srgrimes if (oldstyle) { 13011556Srgrimes /* We must read until the closing backquote, giving special 13021556Srgrimes treatment to some slashes, and then push the string and 13031556Srgrimes reread it as input, interpreting it normally. */ 130425230Ssteve char *out; 130525230Ssteve int c; 13061556Srgrimes int savelen; 13071556Srgrimes char *str; 13088855Srgrimes 130920425Ssteve 13101556Srgrimes STARTSTACKSTR(out); 131120425Ssteve for (;;) { 131220425Ssteve if (needprompt) { 131320425Ssteve setprompt(2); 131420425Ssteve needprompt = 0; 131520425Ssteve } 131620425Ssteve switch (c = pgetc()) { 131720425Ssteve case '`': 131820425Ssteve goto done; 131920425Ssteve 132020425Ssteve case '\\': 132120425Ssteve if ((c = pgetc()) == '\n') { 132220425Ssteve plinno++; 132320425Ssteve if (doprompt) 132420425Ssteve setprompt(2); 132520425Ssteve else 132620425Ssteve setprompt(0); 132720425Ssteve /* 132820425Ssteve * If eating a newline, avoid putting 132920425Ssteve * the newline into the new character 133020425Ssteve * stream (via the STPUTC after the 133120425Ssteve * switch). 133220425Ssteve */ 133320425Ssteve continue; 133420425Ssteve } 133517987Speter if (c != '\\' && c != '`' && c != '$' 13361556Srgrimes && (!dblquote || c != '"')) 13371556Srgrimes STPUTC('\\', out); 133820425Ssteve break; 133920425Ssteve 134020425Ssteve case '\n': 134120425Ssteve plinno++; 134220425Ssteve needprompt = doprompt; 134320425Ssteve break; 134420425Ssteve 134520425Ssteve case PEOF: 134620425Ssteve startlinno = plinno; 134720425Ssteve synerror("EOF in backquote substitution"); 134820425Ssteve break; 134920425Ssteve 135020425Ssteve default: 135120425Ssteve break; 135220425Ssteve } 135320425Ssteve STPUTC(c, out); 13541556Srgrimes } 135520425Sstevedone: 13561556Srgrimes STPUTC('\0', out); 13571556Srgrimes savelen = out - stackblock(); 13581556Srgrimes if (savelen > 0) { 13591556Srgrimes str = ckmalloc(savelen); 136017987Speter memcpy(str, stackblock(), savelen); 136117987Speter setinputstring(str, 1); 13621556Srgrimes } 13631556Srgrimes } 13641556Srgrimes nlpp = &bqlist; 13651556Srgrimes while (*nlpp) 13661556Srgrimes nlpp = &(*nlpp)->next; 13671556Srgrimes *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 13681556Srgrimes (*nlpp)->next = NULL; 13691556Srgrimes parsebackquote = oldstyle; 137020425Ssteve 137120425Ssteve if (oldstyle) { 137220425Ssteve saveprompt = doprompt; 137320425Ssteve doprompt = 0; 137420425Ssteve } 137520425Ssteve 13761556Srgrimes n = list(0); 137720425Ssteve 137820425Ssteve if (oldstyle) 137920425Ssteve doprompt = saveprompt; 138020425Ssteve else { 138120425Ssteve if (readtoken() != TRP) 138220425Ssteve synexpect(TRP); 138320425Ssteve } 138420425Ssteve 13851556Srgrimes (*nlpp)->n = n; 138620425Ssteve if (oldstyle) { 138720425Ssteve /* 138820425Ssteve * Start reading from old file again, ignoring any pushed back 138920425Ssteve * tokens left from the backquote parsing 139020425Ssteve */ 13911556Srgrimes popfile(); 139220425Ssteve tokpushback = 0; 139320425Ssteve } 13941556Srgrimes while (stackblocksize() <= savelen) 13951556Srgrimes growstackblock(); 13961556Srgrimes STARTSTACKSTR(out); 13971556Srgrimes if (str) { 139817987Speter memcpy(out, str, savelen); 13991556Srgrimes STADJUST(savelen, out); 14001556Srgrimes INTOFF; 14011556Srgrimes ckfree(str); 14021556Srgrimes str = NULL; 14031556Srgrimes INTON; 14041556Srgrimes } 14051556Srgrimes parsebackquote = savepbq; 14061556Srgrimes handler = savehandler; 14071556Srgrimes if (arinest || dblquote) 14081556Srgrimes USTPUTC(CTLBACKQ | CTLQUOTE, out); 14091556Srgrimes else 14101556Srgrimes USTPUTC(CTLBACKQ, out); 14111556Srgrimes if (oldstyle) 14121556Srgrimes goto parsebackq_oldreturn; 14131556Srgrimes else 14141556Srgrimes goto parsebackq_newreturn; 14151556Srgrimes} 14161556Srgrimes 14171556Srgrimes/* 14181556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 14191556Srgrimes */ 14201556Srgrimesparsearith: { 14211556Srgrimes 14221556Srgrimes if (++arinest == 1) { 14231556Srgrimes prevsyntax = syntax; 14241556Srgrimes syntax = ARISYNTAX; 14251556Srgrimes USTPUTC(CTLARI, out); 142638887Stegge if (dblquote) 142738887Stegge USTPUTC('"',out); 142838887Stegge else 142938887Stegge USTPUTC(' ',out); 14301556Srgrimes } else { 14311556Srgrimes /* 14321556Srgrimes * we collapse embedded arithmetic expansion to 14331556Srgrimes * parenthesis, which should be equivalent 14341556Srgrimes */ 14351556Srgrimes USTPUTC('(', out); 14361556Srgrimes } 14371556Srgrimes goto parsearith_return; 14381556Srgrimes} 14391556Srgrimes 14401556Srgrimes} /* end of readtoken */ 14411556Srgrimes 14421556Srgrimes 14431556Srgrimes 14441556Srgrimes#ifdef mkinit 14451556SrgrimesRESET { 14461556Srgrimes tokpushback = 0; 14471556Srgrimes checkkwd = 0; 14481556Srgrimes} 14491556Srgrimes#endif 14501556Srgrimes 14511556Srgrimes/* 14521556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 14531556Srgrimes * or backquotes). 14541556Srgrimes */ 14551556Srgrimes 14561556SrgrimesSTATIC int 14571556Srgrimesnoexpand(text) 14581556Srgrimes char *text; 14591556Srgrimes { 146025230Ssteve char *p; 146125230Ssteve char c; 14621556Srgrimes 14631556Srgrimes p = text; 14641556Srgrimes while ((c = *p++) != '\0') { 146539137Stegge if ( c == CTLQUOTEMARK) 146639137Stegge continue; 14671556Srgrimes if (c == CTLESC) 14681556Srgrimes p++; 146954631Scracauer else if (c >= 0 && BASESYNTAX[(int)c] == CCTL) 14701556Srgrimes return 0; 14711556Srgrimes } 14721556Srgrimes return 1; 14731556Srgrimes} 14741556Srgrimes 14751556Srgrimes 14761556Srgrimes/* 14771556Srgrimes * Return true if the argument is a legal variable name (a letter or 14781556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 14791556Srgrimes */ 14801556Srgrimes 14811556Srgrimesint 14821556Srgrimesgoodname(name) 14831556Srgrimes char *name; 14841556Srgrimes { 148525230Ssteve char *p; 14861556Srgrimes 14871556Srgrimes p = name; 14881556Srgrimes if (! is_name(*p)) 14891556Srgrimes return 0; 14901556Srgrimes while (*++p) { 14911556Srgrimes if (! is_in_name(*p)) 14921556Srgrimes return 0; 14931556Srgrimes } 14941556Srgrimes return 1; 14951556Srgrimes} 14961556Srgrimes 14971556Srgrimes 14981556Srgrimes/* 14991556Srgrimes * Called when an unexpected token is read during the parse. The argument 15001556Srgrimes * is the token that is expected, or -1 if more than one type of token can 15011556Srgrimes * occur at this point. 15021556Srgrimes */ 15031556Srgrimes 15041556SrgrimesSTATIC void 150520425Sstevesynexpect(token) 150617987Speter int token; 150717987Speter{ 15081556Srgrimes char msg[64]; 15091556Srgrimes 15101556Srgrimes if (token >= 0) { 15111556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 15121556Srgrimes tokname[lasttoken], tokname[token]); 15131556Srgrimes } else { 15141556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 15151556Srgrimes } 15161556Srgrimes synerror(msg); 15171556Srgrimes} 15181556Srgrimes 15191556Srgrimes 15201556SrgrimesSTATIC void 15211556Srgrimessynerror(msg) 15221556Srgrimes char *msg; 15231556Srgrimes { 15241556Srgrimes if (commandname) 15251556Srgrimes outfmt(&errout, "%s: %d: ", commandname, startlinno); 15261556Srgrimes outfmt(&errout, "Syntax error: %s\n", msg); 15271556Srgrimes error((char *)NULL); 15281556Srgrimes} 15291556Srgrimes 15301556SrgrimesSTATIC void 15311556Srgrimessetprompt(which) 15321556Srgrimes int which; 15331556Srgrimes { 15341556Srgrimes whichprompt = which; 15351556Srgrimes 153617987Speter#ifndef NO_HISTORY 15371556Srgrimes if (!el) 153817987Speter#endif 15391556Srgrimes out2str(getprompt(NULL)); 15401556Srgrimes} 15411556Srgrimes 15421556Srgrimes/* 15431556Srgrimes * called by editline -- any expansions to the prompt 15441556Srgrimes * should be added here. 15451556Srgrimes */ 15461556Srgrimeschar * 15471556Srgrimesgetprompt(unused) 154825905Ssteve void *unused __unused; 154925905Ssteve{ 15501556Srgrimes switch (whichprompt) { 15511556Srgrimes case 0: 15521556Srgrimes return ""; 15531556Srgrimes case 1: 15541556Srgrimes return ps1val(); 15551556Srgrimes case 2: 15561556Srgrimes return ps2val(); 15571556Srgrimes default: 15581556Srgrimes return "<internal prompt error>"; 15591556Srgrimes } 15601556Srgrimes} 1561