parser.c revision 75336
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 75336 2001-04-09 12:46:19Z brian $"; 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" 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 1111556SrgrimesSTATIC union node *list __P((int)); 1121556SrgrimesSTATIC union node *andor __P((void)); 1131556SrgrimesSTATIC union node *pipeline __P((void)); 1141556SrgrimesSTATIC union node *command __P((void)); 1151556SrgrimesSTATIC union node *simplecmd __P((union node **, union node *)); 11617987SpeterSTATIC union node *makename __P((void)); 1171556SrgrimesSTATIC void parsefname __P((void)); 1181556SrgrimesSTATIC void parseheredoc __P((void)); 11917987SpeterSTATIC int peektoken __P((void)); 1201556SrgrimesSTATIC int readtoken __P((void)); 12117987SpeterSTATIC int xxreadtoken __P((void)); 1221556SrgrimesSTATIC int readtoken1 __P((int, char const *, char *, int)); 1231556SrgrimesSTATIC int noexpand __P((char *)); 1241556SrgrimesSTATIC void synexpect __P((int)); 1251556SrgrimesSTATIC void synerror __P((char *)); 12620425SsteveSTATIC void setprompt __P((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 * 13520425Ssteveparsecmd(interact) 13617987Speter int interact; 13717987Speter{ 1381556Srgrimes int t; 1391556Srgrimes 14060593Scracauer tokpushback = 0; 1411556Srgrimes doprompt = interact; 1421556Srgrimes if (doprompt) 1431556Srgrimes setprompt(1); 1441556Srgrimes else 1451556Srgrimes setprompt(0); 1461556Srgrimes needprompt = 0; 1471556Srgrimes t = readtoken(); 1481556Srgrimes if (t == TEOF) 1491556Srgrimes return NEOF; 1501556Srgrimes if (t == TNL) 1511556Srgrimes return NULL; 1521556Srgrimes tokpushback++; 1531556Srgrimes return list(1); 1541556Srgrimes} 1551556Srgrimes 1561556Srgrimes 1571556SrgrimesSTATIC union node * 15820425Sstevelist(nlflag) 15917987Speter int nlflag; 16017987Speter{ 1611556Srgrimes union node *n1, *n2, *n3; 16217987Speter int tok; 1631556Srgrimes 1641556Srgrimes checkkwd = 2; 1651556Srgrimes if (nlflag == 0 && tokendlist[peektoken()]) 1661556Srgrimes return NULL; 16717987Speter n1 = NULL; 1681556Srgrimes for (;;) { 16917987Speter n2 = andor(); 17017987Speter tok = readtoken(); 17117987Speter if (tok == TBACKGND) { 17217987Speter if (n2->type == NCMD || n2->type == NPIPE) { 17317987Speter n2->ncmd.backgnd = 1; 17417987Speter } else if (n2->type == NREDIR) { 17517987Speter n2->type = NBACKGND; 17617987Speter } else { 17717987Speter n3 = (union node *)stalloc(sizeof (struct nredir)); 17817987Speter n3->type = NBACKGND; 17917987Speter n3->nredir.n = n2; 18017987Speter n3->nredir.redirect = NULL; 18117987Speter n2 = n3; 18217987Speter } 18317987Speter } 18417987Speter if (n1 == NULL) { 18517987Speter n1 = n2; 18617987Speter } 18717987Speter else { 18817987Speter n3 = (union node *)stalloc(sizeof (struct nbinary)); 18917987Speter n3->type = NSEMI; 19017987Speter n3->nbinary.ch1 = n1; 19117987Speter n3->nbinary.ch2 = n2; 19217987Speter n1 = n3; 19317987Speter } 19417987Speter switch (tok) { 19513882Sjoerg case TBACKGND: 19617987Speter case TSEMI: 19717987Speter tok = readtoken(); 19817987Speter /* fall through */ 1991556Srgrimes case TNL: 20017987Speter if (tok == TNL) { 20117987Speter parseheredoc(); 20217987Speter if (nlflag) 20317987Speter return n1; 20417987Speter } else { 20517987Speter tokpushback++; 20617987Speter } 2071556Srgrimes checkkwd = 2; 2081556Srgrimes if (tokendlist[peektoken()]) 2091556Srgrimes return n1; 2101556Srgrimes break; 2111556Srgrimes case TEOF: 2121556Srgrimes if (heredoclist) 2131556Srgrimes parseheredoc(); 2141556Srgrimes else 2151556Srgrimes pungetc(); /* push back EOF on input */ 2161556Srgrimes return n1; 2171556Srgrimes default: 2181556Srgrimes if (nlflag) 2191556Srgrimes synexpect(-1); 2201556Srgrimes tokpushback++; 2211556Srgrimes return n1; 2221556Srgrimes } 2231556Srgrimes } 2241556Srgrimes} 2251556Srgrimes 2261556Srgrimes 2271556Srgrimes 2281556SrgrimesSTATIC union node * 2291556Srgrimesandor() { 2301556Srgrimes union node *n1, *n2, *n3; 2311556Srgrimes int t; 2321556Srgrimes 2331556Srgrimes n1 = pipeline(); 2341556Srgrimes for (;;) { 2351556Srgrimes if ((t = readtoken()) == TAND) { 2361556Srgrimes t = NAND; 2371556Srgrimes } else if (t == TOR) { 2381556Srgrimes t = NOR; 2391556Srgrimes } else { 2401556Srgrimes tokpushback++; 2411556Srgrimes return n1; 2421556Srgrimes } 2431556Srgrimes n2 = pipeline(); 2441556Srgrimes n3 = (union node *)stalloc(sizeof (struct nbinary)); 2451556Srgrimes n3->type = t; 2461556Srgrimes n3->nbinary.ch1 = n1; 2471556Srgrimes n3->nbinary.ch2 = n2; 2481556Srgrimes n1 = n3; 2491556Srgrimes } 2501556Srgrimes} 2511556Srgrimes 2521556Srgrimes 2531556Srgrimes 2541556SrgrimesSTATIC union node * 2551556Srgrimespipeline() { 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 * 2951556Srgrimescommand() { 2961556Srgrimes union node *n1, *n2; 2971556Srgrimes union node *ap, **app; 2981556Srgrimes union node *cp, **cpp; 2991556Srgrimes union node *redir, **rpp; 30075160Sbrian int t, negate = 0; 3011556Srgrimes 3021556Srgrimes checkkwd = 2; 30317987Speter redir = NULL; 30417987Speter n1 = NULL; 3051556Srgrimes rpp = &redir; 30620425Ssteve 3071556Srgrimes /* Check for redirection which may precede command */ 3081556Srgrimes while (readtoken() == TREDIR) { 3091556Srgrimes *rpp = n2 = redirnode; 3101556Srgrimes rpp = &n2->nfile.next; 3111556Srgrimes parsefname(); 3121556Srgrimes } 3131556Srgrimes tokpushback++; 3141556Srgrimes 31575160Sbrian while (readtoken() == TNOT) { 31675160Sbrian TRACE(("command: TNOT recognized\n")); 31775160Sbrian negate = !negate; 31875160Sbrian } 31975160Sbrian tokpushback++; 32075160Sbrian 3211556Srgrimes switch (readtoken()) { 3221556Srgrimes case TIF: 3231556Srgrimes n1 = (union node *)stalloc(sizeof (struct nif)); 3241556Srgrimes n1->type = NIF; 3251556Srgrimes n1->nif.test = list(0); 3261556Srgrimes if (readtoken() != TTHEN) 3271556Srgrimes synexpect(TTHEN); 3281556Srgrimes n1->nif.ifpart = list(0); 3291556Srgrimes n2 = n1; 3301556Srgrimes while (readtoken() == TELIF) { 3311556Srgrimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 3321556Srgrimes n2 = n2->nif.elsepart; 3331556Srgrimes n2->type = NIF; 3341556Srgrimes n2->nif.test = list(0); 3351556Srgrimes if (readtoken() != TTHEN) 3361556Srgrimes synexpect(TTHEN); 3371556Srgrimes n2->nif.ifpart = list(0); 3381556Srgrimes } 3391556Srgrimes if (lasttoken == TELSE) 3401556Srgrimes n2->nif.elsepart = list(0); 3411556Srgrimes else { 3421556Srgrimes n2->nif.elsepart = NULL; 3431556Srgrimes tokpushback++; 3441556Srgrimes } 3451556Srgrimes if (readtoken() != TFI) 3461556Srgrimes synexpect(TFI); 3471556Srgrimes checkkwd = 1; 3481556Srgrimes break; 3491556Srgrimes case TWHILE: 3501556Srgrimes case TUNTIL: { 3511556Srgrimes int got; 3521556Srgrimes n1 = (union node *)stalloc(sizeof (struct nbinary)); 3531556Srgrimes n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 3541556Srgrimes n1->nbinary.ch1 = list(0); 3551556Srgrimes if ((got=readtoken()) != TDO) { 3561556SrgrimesTRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 3571556Srgrimes synexpect(TDO); 3581556Srgrimes } 3591556Srgrimes n1->nbinary.ch2 = list(0); 3601556Srgrimes if (readtoken() != TDONE) 3611556Srgrimes synexpect(TDONE); 3621556Srgrimes checkkwd = 1; 3631556Srgrimes break; 3641556Srgrimes } 3651556Srgrimes case TFOR: 3661556Srgrimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 3671556Srgrimes synerror("Bad for loop variable"); 3681556Srgrimes n1 = (union node *)stalloc(sizeof (struct nfor)); 3691556Srgrimes n1->type = NFOR; 3701556Srgrimes n1->nfor.var = wordtext; 3711556Srgrimes if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) { 3721556Srgrimes app = ≈ 3731556Srgrimes while (readtoken() == TWORD) { 3741556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3751556Srgrimes n2->type = NARG; 3761556Srgrimes n2->narg.text = wordtext; 3771556Srgrimes n2->narg.backquote = backquotelist; 3781556Srgrimes *app = n2; 3791556Srgrimes app = &n2->narg.next; 3801556Srgrimes } 3811556Srgrimes *app = NULL; 3821556Srgrimes n1->nfor.args = ap; 3831556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 3841556Srgrimes synexpect(-1); 3851556Srgrimes } else { 3861556Srgrimes#ifndef GDB_HACK 3871556Srgrimes static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, 3881556Srgrimes '@', '=', '\0'}; 3891556Srgrimes#endif 3901556Srgrimes n2 = (union node *)stalloc(sizeof (struct narg)); 3911556Srgrimes n2->type = NARG; 3921556Srgrimes n2->narg.text = (char *)argvars; 3931556Srgrimes n2->narg.backquote = NULL; 3941556Srgrimes n2->narg.next = NULL; 3951556Srgrimes n1->nfor.args = n2; 3961556Srgrimes /* 3971556Srgrimes * Newline or semicolon here is optional (but note 3981556Srgrimes * that the original Bourne shell only allowed NL). 3991556Srgrimes */ 4001556Srgrimes if (lasttoken != TNL && lasttoken != TSEMI) 4011556Srgrimes tokpushback++; 4021556Srgrimes } 4031556Srgrimes checkkwd = 2; 4041556Srgrimes if ((t = readtoken()) == TDO) 4051556Srgrimes t = TDONE; 4061556Srgrimes else if (t == TBEGIN) 4071556Srgrimes t = TEND; 4081556Srgrimes else 4091556Srgrimes synexpect(-1); 4101556Srgrimes n1->nfor.body = list(0); 4111556Srgrimes if (readtoken() != t) 4121556Srgrimes synexpect(t); 4131556Srgrimes checkkwd = 1; 4141556Srgrimes break; 4151556Srgrimes case TCASE: 4161556Srgrimes n1 = (union node *)stalloc(sizeof (struct ncase)); 4171556Srgrimes n1->type = NCASE; 4181556Srgrimes if (readtoken() != TWORD) 4191556Srgrimes synexpect(TWORD); 4201556Srgrimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 4211556Srgrimes n2->type = NARG; 4221556Srgrimes n2->narg.text = wordtext; 4231556Srgrimes n2->narg.backquote = backquotelist; 4241556Srgrimes n2->narg.next = NULL; 4251556Srgrimes while (readtoken() == TNL); 4261556Srgrimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 4271556Srgrimes synerror("expecting \"in\""); 4281556Srgrimes cpp = &n1->ncase.cases; 42918018Speter noaliases = 1; /* turn off alias expansion */ 4302760Ssef checkkwd = 2, readtoken(); 4312760Ssef do { 4321556Srgrimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 4331556Srgrimes cp->type = NCLIST; 4341556Srgrimes app = &cp->nclist.pattern; 4351556Srgrimes for (;;) { 4361556Srgrimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 4371556Srgrimes ap->type = NARG; 4381556Srgrimes ap->narg.text = wordtext; 4391556Srgrimes ap->narg.backquote = backquotelist; 4402760Ssef if (checkkwd = 2, readtoken() != TPIPE) 4411556Srgrimes break; 4421556Srgrimes app = &ap->narg.next; 4432760Ssef readtoken(); 4441556Srgrimes } 4451556Srgrimes ap->narg.next = NULL; 4461556Srgrimes if (lasttoken != TRP) 44718018Speter noaliases = 0, synexpect(TRP); 4481556Srgrimes cp->nclist.body = list(0); 4492760Ssef 4502760Ssef checkkwd = 2; 4512760Ssef if ((t = readtoken()) != TESAC) { 4522760Ssef if (t != TENDCASE) 45318018Speter noaliases = 0, synexpect(TENDCASE); 4542760Ssef else 4552760Ssef checkkwd = 2, readtoken(); 4562760Ssef } 4571556Srgrimes cpp = &cp->nclist.next; 4582760Ssef } while(lasttoken != TESAC); 45918018Speter noaliases = 0; /* reset alias expansion */ 4601556Srgrimes *cpp = NULL; 4611556Srgrimes checkkwd = 1; 4621556Srgrimes break; 4631556Srgrimes case TLP: 4641556Srgrimes n1 = (union node *)stalloc(sizeof (struct nredir)); 4651556Srgrimes n1->type = NSUBSHELL; 4661556Srgrimes n1->nredir.n = list(0); 4671556Srgrimes n1->nredir.redirect = NULL; 4681556Srgrimes if (readtoken() != TRP) 4691556Srgrimes synexpect(TRP); 4701556Srgrimes checkkwd = 1; 4711556Srgrimes break; 4721556Srgrimes case TBEGIN: 4731556Srgrimes n1 = list(0); 4741556Srgrimes if (readtoken() != TEND) 4751556Srgrimes synexpect(TEND); 4761556Srgrimes checkkwd = 1; 4771556Srgrimes break; 4781556Srgrimes /* Handle an empty command like other simple commands. */ 47917987Speter case TSEMI: 48017987Speter /* 48117987Speter * An empty command before a ; doesn't make much sense, and 48217987Speter * should certainly be disallowed in the case of `if ;'. 48317987Speter */ 48417987Speter if (!redir) 48517987Speter synexpect(-1); 48620425Ssteve case TAND: 48720425Ssteve case TOR: 4881556Srgrimes case TNL: 48910399Sjoerg case TEOF: 4901556Srgrimes case TWORD: 49117987Speter case TRP: 4921556Srgrimes tokpushback++; 49375160Sbrian n1 = simplecmd(rpp, redir); 49475160Sbrian goto checkneg; 4951556Srgrimes default: 4961556Srgrimes synexpect(-1); 4971556Srgrimes } 4981556Srgrimes 4991556Srgrimes /* Now check for redirection which may follow command */ 5001556Srgrimes while (readtoken() == TREDIR) { 5011556Srgrimes *rpp = n2 = redirnode; 5021556Srgrimes rpp = &n2->nfile.next; 5031556Srgrimes parsefname(); 5041556Srgrimes } 5051556Srgrimes tokpushback++; 5061556Srgrimes *rpp = NULL; 5071556Srgrimes if (redir) { 5081556Srgrimes if (n1->type != NSUBSHELL) { 5091556Srgrimes n2 = (union node *)stalloc(sizeof (struct nredir)); 5101556Srgrimes n2->type = NREDIR; 5111556Srgrimes n2->nredir.n = n1; 5121556Srgrimes n1 = n2; 5131556Srgrimes } 5141556Srgrimes n1->nredir.redirect = redir; 5151556Srgrimes } 51675160Sbrian 51775160Sbriancheckneg: 51875160Sbrian if (negate) { 51975160Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 52075160Sbrian n2->type = NNOT; 52175160Sbrian n2->nnot.com = n1; 52275160Sbrian return n2; 52375160Sbrian } 52475160Sbrian else 52575160Sbrian return n1; 5261556Srgrimes} 5271556Srgrimes 5281556Srgrimes 5291556SrgrimesSTATIC union node * 5308855Srgrimessimplecmd(rpp, redir) 5311556Srgrimes union node **rpp, *redir; 5321556Srgrimes { 5331556Srgrimes union node *args, **app; 5341556Srgrimes union node **orig_rpp = rpp; 53575160Sbrian union node *n = NULL, *n2; 53675160Sbrian int negate = 0; 5371556Srgrimes 5381556Srgrimes /* If we don't have any redirections already, then we must reset */ 5391556Srgrimes /* rpp to be the address of the local redir variable. */ 5401556Srgrimes if (redir == 0) 5411556Srgrimes rpp = &redir; 5421556Srgrimes 5431556Srgrimes args = NULL; 5441556Srgrimes app = &args; 5458855Srgrimes /* 5461556Srgrimes * We save the incoming value, because we need this for shell 5471556Srgrimes * functions. There can not be a redirect or an argument between 5488855Srgrimes * the function name and the open parenthesis. 5491556Srgrimes */ 5501556Srgrimes orig_rpp = rpp; 5511556Srgrimes 55275160Sbrian while (readtoken() == TNOT) { 55375160Sbrian TRACE(("command: TNOT recognized\n")); 55475160Sbrian negate = !negate; 55575160Sbrian } 55675160Sbrian tokpushback++; 55775160Sbrian 5581556Srgrimes for (;;) { 5591556Srgrimes if (readtoken() == TWORD) { 5601556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 5611556Srgrimes n->type = NARG; 5621556Srgrimes n->narg.text = wordtext; 5631556Srgrimes n->narg.backquote = backquotelist; 5641556Srgrimes *app = n; 5651556Srgrimes app = &n->narg.next; 5661556Srgrimes } else if (lasttoken == TREDIR) { 5671556Srgrimes *rpp = n = redirnode; 5681556Srgrimes rpp = &n->nfile.next; 5691556Srgrimes parsefname(); /* read name of redirection file */ 5701556Srgrimes } else if (lasttoken == TLP && app == &args->narg.next 5711556Srgrimes && rpp == orig_rpp) { 5721556Srgrimes /* We have a function */ 5731556Srgrimes if (readtoken() != TRP) 5741556Srgrimes synexpect(TRP); 5751556Srgrimes#ifdef notdef 5761556Srgrimes if (! goodname(n->narg.text)) 5771556Srgrimes synerror("Bad function name"); 5781556Srgrimes#endif 5791556Srgrimes n->type = NDEFUN; 5801556Srgrimes n->narg.next = command(); 58175160Sbrian goto checkneg; 5821556Srgrimes } else { 5831556Srgrimes tokpushback++; 5841556Srgrimes break; 5851556Srgrimes } 5861556Srgrimes } 5871556Srgrimes *app = NULL; 5881556Srgrimes *rpp = NULL; 5891556Srgrimes n = (union node *)stalloc(sizeof (struct ncmd)); 5901556Srgrimes n->type = NCMD; 5911556Srgrimes n->ncmd.backgnd = 0; 5921556Srgrimes n->ncmd.args = args; 5931556Srgrimes n->ncmd.redirect = redir; 59475160Sbrian 59575160Sbriancheckneg: 59675160Sbrian if (negate) { 59775160Sbrian n2 = (union node *)stalloc(sizeof (struct nnot)); 59875160Sbrian n2->type = NNOT; 59975160Sbrian n2->nnot.com = n; 60075160Sbrian return n2; 60175160Sbrian } 60275160Sbrian else 60375160Sbrian return n; 6041556Srgrimes} 6051556Srgrimes 60617987SpeterSTATIC union node * 60717987Spetermakename() { 60817987Speter union node *n; 6091556Srgrimes 61017987Speter n = (union node *)stalloc(sizeof (struct narg)); 61117987Speter n->type = NARG; 61217987Speter n->narg.next = NULL; 61317987Speter n->narg.text = wordtext; 61417987Speter n->narg.backquote = backquotelist; 61517987Speter return n; 61617987Speter} 61717987Speter 61817987Spetervoid fixredir(n, text, err) 61917987Speter union node *n; 62017987Speter const char *text; 62117987Speter int err; 62217987Speter { 62317987Speter TRACE(("Fix redir %s %d\n", text, err)); 62417987Speter if (!err) 62517987Speter n->ndup.vname = NULL; 62617987Speter 62717987Speter if (is_digit(text[0]) && text[1] == '\0') 62817987Speter n->ndup.dupfd = digit_val(text[0]); 62917987Speter else if (text[0] == '-' && text[1] == '\0') 63017987Speter n->ndup.dupfd = -1; 63117987Speter else { 63220425Ssteve 63317987Speter if (err) 63417987Speter synerror("Bad fd number"); 63517987Speter else 63617987Speter n->ndup.vname = makename(); 63717987Speter } 63817987Speter} 63917987Speter 64017987Speter 6411556SrgrimesSTATIC void 6421556Srgrimesparsefname() { 6431556Srgrimes union node *n = redirnode; 6441556Srgrimes 6451556Srgrimes if (readtoken() != TWORD) 6461556Srgrimes synexpect(-1); 6471556Srgrimes if (n->type == NHERE) { 6481556Srgrimes struct heredoc *here = heredoc; 6491556Srgrimes struct heredoc *p; 6501556Srgrimes int i; 6511556Srgrimes 6521556Srgrimes if (quoteflag == 0) 6531556Srgrimes n->type = NXHERE; 6541556Srgrimes TRACE(("Here document %d\n", n->type)); 6551556Srgrimes if (here->striptabs) { 6561556Srgrimes while (*wordtext == '\t') 6571556Srgrimes wordtext++; 6581556Srgrimes } 6591556Srgrimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 6601556Srgrimes synerror("Illegal eof marker for << redirection"); 6611556Srgrimes rmescapes(wordtext); 6621556Srgrimes here->eofmark = wordtext; 6631556Srgrimes here->next = NULL; 6641556Srgrimes if (heredoclist == NULL) 6651556Srgrimes heredoclist = here; 6661556Srgrimes else { 6671556Srgrimes for (p = heredoclist ; p->next ; p = p->next); 6681556Srgrimes p->next = here; 6691556Srgrimes } 6701556Srgrimes } else if (n->type == NTOFD || n->type == NFROMFD) { 67117987Speter fixredir(n, wordtext, 0); 6721556Srgrimes } else { 67317987Speter n->nfile.fname = makename(); 6741556Srgrimes } 6751556Srgrimes} 6761556Srgrimes 6771556Srgrimes 6781556Srgrimes/* 6791556Srgrimes * Input any here documents. 6801556Srgrimes */ 6811556Srgrimes 6821556SrgrimesSTATIC void 6831556Srgrimesparseheredoc() { 6841556Srgrimes struct heredoc *here; 6851556Srgrimes union node *n; 6861556Srgrimes 6871556Srgrimes while (heredoclist) { 6881556Srgrimes here = heredoclist; 6891556Srgrimes heredoclist = here->next; 6901556Srgrimes if (needprompt) { 6911556Srgrimes setprompt(2); 6921556Srgrimes needprompt = 0; 6931556Srgrimes } 6941556Srgrimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 6951556Srgrimes here->eofmark, here->striptabs); 6961556Srgrimes n = (union node *)stalloc(sizeof (struct narg)); 6971556Srgrimes n->narg.type = NARG; 6981556Srgrimes n->narg.next = NULL; 6991556Srgrimes n->narg.text = wordtext; 7001556Srgrimes n->narg.backquote = backquotelist; 7011556Srgrimes here->here->nhere.doc = n; 7021556Srgrimes } 7031556Srgrimes} 7041556Srgrimes 7051556SrgrimesSTATIC int 7061556Srgrimespeektoken() { 7071556Srgrimes int t; 7081556Srgrimes 7091556Srgrimes t = readtoken(); 7101556Srgrimes tokpushback++; 7111556Srgrimes return (t); 7121556Srgrimes} 7131556Srgrimes 7141556SrgrimesSTATIC int 7151556Srgrimesreadtoken() { 7161556Srgrimes int t; 7171556Srgrimes int savecheckkwd = checkkwd; 7181556Srgrimes struct alias *ap; 7191556Srgrimes#ifdef DEBUG 7201556Srgrimes int alreadyseen = tokpushback; 7211556Srgrimes#endif 7228855Srgrimes 7231556Srgrimes top: 7241556Srgrimes t = xxreadtoken(); 7251556Srgrimes 7261556Srgrimes if (checkkwd) { 7271556Srgrimes /* 7281556Srgrimes * eat newlines 7291556Srgrimes */ 7301556Srgrimes if (checkkwd == 2) { 7311556Srgrimes checkkwd = 0; 7321556Srgrimes while (t == TNL) { 7331556Srgrimes parseheredoc(); 7341556Srgrimes t = xxreadtoken(); 7351556Srgrimes } 7361556Srgrimes } else 7371556Srgrimes checkkwd = 0; 7381556Srgrimes /* 7391556Srgrimes * check for keywords and aliases 7401556Srgrimes */ 74120425Ssteve if (t == TWORD && !quoteflag) 74217987Speter { 74325230Ssteve char * const *pp; 7441556Srgrimes 7451556Srgrimes for (pp = (char **)parsekwd; *pp; pp++) { 74620425Ssteve if (**pp == *wordtext && equal(*pp, wordtext)) 74717987Speter { 7481556Srgrimes lasttoken = t = pp - parsekwd + KWDOFFSET; 7491556Srgrimes TRACE(("keyword %s recognized\n", tokname[t])); 7501556Srgrimes goto out; 7511556Srgrimes } 7521556Srgrimes } 75318018Speter if (noaliases == 0 && 75418018Speter (ap = lookupalias(wordtext, 1)) != NULL) { 7551556Srgrimes pushstring(ap->val, strlen(ap->val), ap); 7561556Srgrimes checkkwd = savecheckkwd; 7571556Srgrimes goto top; 7581556Srgrimes } 7591556Srgrimes } 7601556Srgrimesout: 76175160Sbrian checkkwd = (t == TNOT) ? savecheckkwd : 0; 7621556Srgrimes } 7631556Srgrimes#ifdef DEBUG 7641556Srgrimes if (!alreadyseen) 7651556Srgrimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7661556Srgrimes else 7671556Srgrimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7681556Srgrimes#endif 7691556Srgrimes return (t); 7701556Srgrimes} 7711556Srgrimes 7721556Srgrimes 7731556Srgrimes/* 7741556Srgrimes * Read the next input token. 7751556Srgrimes * If the token is a word, we set backquotelist to the list of cmds in 7761556Srgrimes * backquotes. We set quoteflag to true if any part of the word was 7771556Srgrimes * quoted. 7781556Srgrimes * If the token is TREDIR, then we set redirnode to a structure containing 7791556Srgrimes * the redirection. 7801556Srgrimes * In all cases, the variable startlinno is set to the number of the line 7811556Srgrimes * on which the token starts. 7821556Srgrimes * 7831556Srgrimes * [Change comment: here documents and internal procedures] 7841556Srgrimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 7851556Srgrimes * word parsing code into a separate routine. In this case, readtoken 7861556Srgrimes * doesn't need to have any internal procedures, but parseword does. 7871556Srgrimes * We could also make parseoperator in essence the main routine, and 7881556Srgrimes * have parseword (readtoken1?) handle both words and redirection.] 7891556Srgrimes */ 7901556Srgrimes 7911556Srgrimes#define RETURN(token) return lasttoken = token 7921556Srgrimes 7931556SrgrimesSTATIC int 7941556Srgrimesxxreadtoken() { 79525230Ssteve int c; 7961556Srgrimes 7971556Srgrimes if (tokpushback) { 7981556Srgrimes tokpushback = 0; 7991556Srgrimes return lasttoken; 8001556Srgrimes } 8011556Srgrimes if (needprompt) { 8021556Srgrimes setprompt(2); 8031556Srgrimes needprompt = 0; 8041556Srgrimes } 8051556Srgrimes startlinno = plinno; 8061556Srgrimes for (;;) { /* until token or start of word found */ 8071556Srgrimes c = pgetc_macro(); 8081556Srgrimes if (c == ' ' || c == '\t') 8091556Srgrimes continue; /* quick check for white space first */ 8101556Srgrimes switch (c) { 8111556Srgrimes case ' ': case '\t': 8121556Srgrimes continue; 8131556Srgrimes case '#': 8141556Srgrimes while ((c = pgetc()) != '\n' && c != PEOF); 8151556Srgrimes pungetc(); 8161556Srgrimes continue; 8171556Srgrimes case '\\': 8181556Srgrimes if (pgetc() == '\n') { 8191556Srgrimes startlinno = ++plinno; 8201556Srgrimes if (doprompt) 8211556Srgrimes setprompt(2); 8221556Srgrimes else 8231556Srgrimes setprompt(0); 8241556Srgrimes continue; 8251556Srgrimes } 8261556Srgrimes pungetc(); 8271556Srgrimes goto breakloop; 8281556Srgrimes case '\n': 8291556Srgrimes plinno++; 8301556Srgrimes needprompt = doprompt; 8311556Srgrimes RETURN(TNL); 8321556Srgrimes case PEOF: 8331556Srgrimes RETURN(TEOF); 8341556Srgrimes case '&': 8351556Srgrimes if (pgetc() == '&') 8361556Srgrimes RETURN(TAND); 8371556Srgrimes pungetc(); 8381556Srgrimes RETURN(TBACKGND); 8391556Srgrimes case '|': 8401556Srgrimes if (pgetc() == '|') 8411556Srgrimes RETURN(TOR); 8421556Srgrimes pungetc(); 8431556Srgrimes RETURN(TPIPE); 8441556Srgrimes case ';': 8451556Srgrimes if (pgetc() == ';') 8461556Srgrimes RETURN(TENDCASE); 8471556Srgrimes pungetc(); 8481556Srgrimes RETURN(TSEMI); 8491556Srgrimes case '(': 8501556Srgrimes RETURN(TLP); 8511556Srgrimes case ')': 8521556Srgrimes RETURN(TRP); 8531556Srgrimes default: 8541556Srgrimes goto breakloop; 8551556Srgrimes } 8561556Srgrimes } 8571556Srgrimesbreakloop: 8581556Srgrimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 8591556Srgrimes#undef RETURN 8601556Srgrimes} 8611556Srgrimes 8621556Srgrimes 8631556Srgrimes 8641556Srgrimes/* 8651556Srgrimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 8661556Srgrimes * is not NULL, read a here document. In the latter case, eofmark is the 8671556Srgrimes * word which marks the end of the document and striptabs is true if 8681556Srgrimes * leading tabs should be stripped from the document. The argument firstc 8691556Srgrimes * is the first character of the input token or document. 8701556Srgrimes * 8711556Srgrimes * Because C does not have internal subroutines, I have simulated them 8721556Srgrimes * using goto's to implement the subroutine linkage. The following macros 8731556Srgrimes * will run code that appears at the end of readtoken1. 8741556Srgrimes */ 8751556Srgrimes 8761556Srgrimes#define CHECKEND() {goto checkend; checkend_return:;} 8771556Srgrimes#define PARSEREDIR() {goto parseredir; parseredir_return:;} 8781556Srgrimes#define PARSESUB() {goto parsesub; parsesub_return:;} 8791556Srgrimes#define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 8801556Srgrimes#define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 8811556Srgrimes#define PARSEARITH() {goto parsearith; parsearith_return:;} 8821556Srgrimes 8831556SrgrimesSTATIC int 8841556Srgrimesreadtoken1(firstc, syntax, eofmark, striptabs) 8851556Srgrimes int firstc; 8861556Srgrimes char const *syntax; 8871556Srgrimes char *eofmark; 8881556Srgrimes int striptabs; 8891556Srgrimes { 89017987Speter int c = firstc; 89117987Speter char *out; 8921556Srgrimes int len; 8931556Srgrimes char line[EOFMARKLEN + 1]; 8941556Srgrimes struct nodelist *bqlist; 8951556Srgrimes int quotef; 8961556Srgrimes int dblquote; 8971556Srgrimes int varnest; /* levels of variables expansion */ 8981556Srgrimes int arinest; /* levels of arithmetic expansion */ 8991556Srgrimes int parenlevel; /* levels of parens in arithmetic */ 9001556Srgrimes int oldstyle; 9011556Srgrimes char const *prevsyntax; /* syntax before arithmetic */ 90254679Scracauer int synentry; 90317987Speter#if __GNUC__ 90417987Speter /* Avoid longjmp clobbering */ 90517987Speter (void) &out; 90617987Speter (void) "ef; 90717987Speter (void) &dblquote; 90817987Speter (void) &varnest; 90917987Speter (void) &arinest; 91017987Speter (void) &parenlevel; 91117987Speter (void) &oldstyle; 91217987Speter (void) &prevsyntax; 91317987Speter (void) &syntax; 91454679Scracauer (void) &synentry; 91517987Speter#endif 9161556Srgrimes 9171556Srgrimes startlinno = plinno; 9181556Srgrimes dblquote = 0; 9191556Srgrimes if (syntax == DQSYNTAX) 9201556Srgrimes dblquote = 1; 9211556Srgrimes quotef = 0; 9221556Srgrimes bqlist = NULL; 9231556Srgrimes varnest = 0; 9241556Srgrimes arinest = 0; 9251556Srgrimes parenlevel = 0; 9261556Srgrimes 9271556Srgrimes STARTSTACKSTR(out); 9281556Srgrimes loop: { /* for each line, until end of word */ 9291556Srgrimes#if ATTY 9301556Srgrimes if (c == '\034' && doprompt 9311556Srgrimes && attyset() && ! equal(termval(), "emacs")) { 9321556Srgrimes attyline(); 9331556Srgrimes if (syntax == BASESYNTAX) 9341556Srgrimes return readtoken(); 9351556Srgrimes c = pgetc(); 9361556Srgrimes goto loop; 9371556Srgrimes } 9381556Srgrimes#endif 9391556Srgrimes CHECKEND(); /* set c to PEOF if at end of here document */ 9401556Srgrimes for (;;) { /* until end of line or end of word */ 9411556Srgrimes CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 94254679Scracauer 94364705Scracauer synentry = syntax[c]; 94454679Scracauer 94554679Scracauer switch(synentry) { 9461556Srgrimes case CNL: /* '\n' */ 9471556Srgrimes if (syntax == BASESYNTAX) 9481556Srgrimes goto endword; /* exit outer loop */ 9491556Srgrimes USTPUTC(c, out); 9501556Srgrimes plinno++; 9511556Srgrimes if (doprompt) 9521556Srgrimes setprompt(2); 9531556Srgrimes else 9541556Srgrimes setprompt(0); 9551556Srgrimes c = pgetc(); 9561556Srgrimes goto loop; /* continue outer loop */ 9571556Srgrimes case CWORD: 9581556Srgrimes USTPUTC(c, out); 9591556Srgrimes break; 9601556Srgrimes case CCTL: 9611556Srgrimes if (eofmark == NULL || dblquote) 9621556Srgrimes USTPUTC(CTLESC, out); 9631556Srgrimes USTPUTC(c, out); 9641556Srgrimes break; 9651556Srgrimes case CBACK: /* backslash */ 9661556Srgrimes c = pgetc(); 9671556Srgrimes if (c == PEOF) { 9681556Srgrimes USTPUTC('\\', out); 9691556Srgrimes pungetc(); 9701556Srgrimes } else if (c == '\n') { 9711556Srgrimes if (doprompt) 9721556Srgrimes setprompt(2); 9731556Srgrimes else 9741556Srgrimes setprompt(0); 9751556Srgrimes } else { 97654631Scracauer if (dblquote && c != '\\' && 97754631Scracauer c != '`' && c != '$' && 97854631Scracauer (c != '"' || eofmark != NULL)) 9791556Srgrimes USTPUTC('\\', out); 98054631Scracauer if (c >= 0 && SQSYNTAX[c] == CCTL) 9811556Srgrimes USTPUTC(CTLESC, out); 98239137Stegge else if (eofmark == NULL) 98338887Stegge USTPUTC(CTLQUOTEMARK, out); 9841556Srgrimes USTPUTC(c, out); 9851556Srgrimes quotef++; 9861556Srgrimes } 9871556Srgrimes break; 9881556Srgrimes case CSQUOTE: 98939137Stegge if (eofmark == NULL) 99039137Stegge USTPUTC(CTLQUOTEMARK, out); 9911556Srgrimes syntax = SQSYNTAX; 9921556Srgrimes break; 9931556Srgrimes case CDQUOTE: 99439137Stegge if (eofmark == NULL) 99539137Stegge USTPUTC(CTLQUOTEMARK, out); 9961556Srgrimes syntax = DQSYNTAX; 9971556Srgrimes dblquote = 1; 9981556Srgrimes break; 9991556Srgrimes case CENDQUOTE: 100039137Stegge if (eofmark != NULL && arinest == 0 && 100139137Stegge varnest == 0) { 10021556Srgrimes USTPUTC(c, out); 10031556Srgrimes } else { 100439137Stegge if (arinest) { 10051556Srgrimes syntax = ARISYNTAX; 100639137Stegge dblquote = 0; 100739137Stegge } else if (eofmark == NULL) { 10081556Srgrimes syntax = BASESYNTAX; 100939137Stegge dblquote = 0; 101039137Stegge } 10111556Srgrimes quotef++; 10121556Srgrimes } 10131556Srgrimes break; 10141556Srgrimes case CVAR: /* '$' */ 10151556Srgrimes PARSESUB(); /* parse substitution */ 10161556Srgrimes break; 10171556Srgrimes case CENDVAR: /* '}' */ 10181556Srgrimes if (varnest > 0) { 10191556Srgrimes varnest--; 10201556Srgrimes USTPUTC(CTLENDVAR, out); 10211556Srgrimes } else { 10221556Srgrimes USTPUTC(c, out); 10231556Srgrimes } 10241556Srgrimes break; 10251556Srgrimes case CLP: /* '(' in arithmetic */ 10261556Srgrimes parenlevel++; 10271556Srgrimes USTPUTC(c, out); 10281556Srgrimes break; 10291556Srgrimes case CRP: /* ')' in arithmetic */ 10301556Srgrimes if (parenlevel > 0) { 10311556Srgrimes USTPUTC(c, out); 10321556Srgrimes --parenlevel; 10331556Srgrimes } else { 10341556Srgrimes if (pgetc() == ')') { 10351556Srgrimes if (--arinest == 0) { 10361556Srgrimes USTPUTC(CTLENDARI, out); 10371556Srgrimes syntax = prevsyntax; 103839137Stegge if (syntax == DQSYNTAX) 103939137Stegge dblquote = 1; 104039137Stegge else 104139137Stegge dblquote = 0; 10421556Srgrimes } else 10431556Srgrimes USTPUTC(')', out); 10441556Srgrimes } else { 10458855Srgrimes /* 10461556Srgrimes * unbalanced parens 10471556Srgrimes * (don't 2nd guess - no error) 10481556Srgrimes */ 10491556Srgrimes pungetc(); 10501556Srgrimes USTPUTC(')', out); 10511556Srgrimes } 10521556Srgrimes } 10531556Srgrimes break; 10541556Srgrimes case CBQUOTE: /* '`' */ 10551556Srgrimes PARSEBACKQOLD(); 10561556Srgrimes break; 10571556Srgrimes case CEOF: 10581556Srgrimes goto endword; /* exit outer loop */ 10591556Srgrimes default: 10601556Srgrimes if (varnest == 0) 10611556Srgrimes goto endword; /* exit outer loop */ 10621556Srgrimes USTPUTC(c, out); 10631556Srgrimes } 10641556Srgrimes c = pgetc_macro(); 10651556Srgrimes } 10661556Srgrimes } 10671556Srgrimesendword: 10681556Srgrimes if (syntax == ARISYNTAX) 10691556Srgrimes synerror("Missing '))'"); 10701556Srgrimes if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) 10711556Srgrimes synerror("Unterminated quoted string"); 10721556Srgrimes if (varnest != 0) { 10731556Srgrimes startlinno = plinno; 10741556Srgrimes synerror("Missing '}'"); 10751556Srgrimes } 10761556Srgrimes USTPUTC('\0', out); 10771556Srgrimes len = out - stackblock(); 10781556Srgrimes out = stackblock(); 10791556Srgrimes if (eofmark == NULL) { 10801556Srgrimes if ((c == '>' || c == '<') 10811556Srgrimes && quotef == 0 10821556Srgrimes && len <= 2 10831556Srgrimes && (*out == '\0' || is_digit(*out))) { 10841556Srgrimes PARSEREDIR(); 10851556Srgrimes return lasttoken = TREDIR; 10861556Srgrimes } else { 10871556Srgrimes pungetc(); 10881556Srgrimes } 10891556Srgrimes } 10901556Srgrimes quoteflag = quotef; 10911556Srgrimes backquotelist = bqlist; 10921556Srgrimes grabstackblock(len); 10931556Srgrimes wordtext = out; 10941556Srgrimes return lasttoken = TWORD; 10951556Srgrimes/* end of readtoken routine */ 10961556Srgrimes 10971556Srgrimes 10981556Srgrimes 10991556Srgrimes/* 11001556Srgrimes * Check to see whether we are at the end of the here document. When this 11011556Srgrimes * is called, c is set to the first character of the next input line. If 11021556Srgrimes * we are at the end of the here document, this routine sets the c to PEOF. 11031556Srgrimes */ 11041556Srgrimes 11051556Srgrimescheckend: { 11061556Srgrimes if (eofmark) { 11071556Srgrimes if (striptabs) { 11081556Srgrimes while (c == '\t') 11091556Srgrimes c = pgetc(); 11101556Srgrimes } 11111556Srgrimes if (c == *eofmark) { 11121556Srgrimes if (pfgets(line, sizeof line) != NULL) { 111325230Ssteve char *p, *q; 11141556Srgrimes 11151556Srgrimes p = line; 11161556Srgrimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 11171556Srgrimes if (*p == '\n' && *q == '\0') { 11181556Srgrimes c = PEOF; 11191556Srgrimes plinno++; 11201556Srgrimes needprompt = doprompt; 11211556Srgrimes } else { 11221556Srgrimes pushstring(line, strlen(line), NULL); 11231556Srgrimes } 11241556Srgrimes } 11251556Srgrimes } 11261556Srgrimes } 11271556Srgrimes goto checkend_return; 11281556Srgrimes} 11291556Srgrimes 11301556Srgrimes 11311556Srgrimes/* 11321556Srgrimes * Parse a redirection operator. The variable "out" points to a string 11331556Srgrimes * specifying the fd to be redirected. The variable "c" contains the 11341556Srgrimes * first character of the redirection operator. 11351556Srgrimes */ 11361556Srgrimes 11371556Srgrimesparseredir: { 11381556Srgrimes char fd = *out; 11391556Srgrimes union node *np; 11401556Srgrimes 11411556Srgrimes np = (union node *)stalloc(sizeof (struct nfile)); 11421556Srgrimes if (c == '>') { 11431556Srgrimes np->nfile.fd = 1; 11441556Srgrimes c = pgetc(); 11451556Srgrimes if (c == '>') 11461556Srgrimes np->type = NAPPEND; 11471556Srgrimes else if (c == '&') 11481556Srgrimes np->type = NTOFD; 11491556Srgrimes else { 11501556Srgrimes np->type = NTO; 11511556Srgrimes pungetc(); 11521556Srgrimes } 11531556Srgrimes } else { /* c == '<' */ 11541556Srgrimes np->nfile.fd = 0; 11551556Srgrimes c = pgetc(); 11561556Srgrimes if (c == '<') { 11571556Srgrimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 11581556Srgrimes np = (union node *)stalloc(sizeof (struct nhere)); 11591556Srgrimes np->nfile.fd = 0; 11601556Srgrimes } 11611556Srgrimes np->type = NHERE; 11621556Srgrimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 11631556Srgrimes heredoc->here = np; 11641556Srgrimes if ((c = pgetc()) == '-') { 11651556Srgrimes heredoc->striptabs = 1; 11661556Srgrimes } else { 11671556Srgrimes heredoc->striptabs = 0; 11681556Srgrimes pungetc(); 11691556Srgrimes } 11701556Srgrimes } else if (c == '&') 11711556Srgrimes np->type = NFROMFD; 117266612Sbrian else if (c == '>') 117366612Sbrian np->type = NFROMTO; 11741556Srgrimes else { 11751556Srgrimes np->type = NFROM; 11761556Srgrimes pungetc(); 11771556Srgrimes } 11781556Srgrimes } 11791556Srgrimes if (fd != '\0') 11801556Srgrimes np->nfile.fd = digit_val(fd); 11811556Srgrimes redirnode = np; 11821556Srgrimes goto parseredir_return; 11831556Srgrimes} 11841556Srgrimes 11851556Srgrimes 11861556Srgrimes/* 11871556Srgrimes * Parse a substitution. At this point, we have read the dollar sign 11881556Srgrimes * and nothing else. 11891556Srgrimes */ 11901556Srgrimes 11911556Srgrimesparsesub: { 11921556Srgrimes int subtype; 11931556Srgrimes int typeloc; 11941556Srgrimes int flags; 11951556Srgrimes char *p; 11961556Srgrimes#ifndef GDB_HACK 11971556Srgrimes static const char types[] = "}-+?="; 11981556Srgrimes#endif 119918202Speter int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 12001556Srgrimes 12011556Srgrimes c = pgetc(); 12021556Srgrimes if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) { 12031556Srgrimes USTPUTC('$', out); 12041556Srgrimes pungetc(); 12051556Srgrimes } else if (c == '(') { /* $(command) or $((arith)) */ 12061556Srgrimes if (pgetc() == '(') { 12071556Srgrimes PARSEARITH(); 12081556Srgrimes } else { 12091556Srgrimes pungetc(); 12101556Srgrimes PARSEBACKQNEW(); 12111556Srgrimes } 12121556Srgrimes } else { 12131556Srgrimes USTPUTC(CTLVAR, out); 12141556Srgrimes typeloc = out - stackblock(); 12151556Srgrimes USTPUTC(VSNORMAL, out); 12161556Srgrimes subtype = VSNORMAL; 12171556Srgrimes if (c == '{') { 121818202Speter bracketed_name = 1; 12191556Srgrimes c = pgetc(); 122017987Speter if (c == '#') { 122117987Speter if ((c = pgetc()) == '}') 122217987Speter c = '#'; 122317987Speter else 122417987Speter subtype = VSLENGTH; 122517987Speter } 122617987Speter else 122717987Speter subtype = 0; 12281556Srgrimes } 12291556Srgrimes if (is_name(c)) { 12301556Srgrimes do { 12311556Srgrimes STPUTC(c, out); 12321556Srgrimes c = pgetc(); 12331556Srgrimes } while (is_in_name(c)); 123418202Speter } else if (is_digit(c)) { 123518202Speter if (bracketed_name) { 123618202Speter do { 123718202Speter STPUTC(c, out); 123818202Speter c = pgetc(); 123918202Speter } while (is_digit(c)); 124018202Speter } else { 124118202Speter STPUTC(c, out); 124218202Speter c = pgetc(); 124318202Speter } 12441556Srgrimes } else { 12451556Srgrimes if (! is_special(c)) 12461556Srgrimesbadsub: synerror("Bad substitution"); 12471556Srgrimes USTPUTC(c, out); 12481556Srgrimes c = pgetc(); 12491556Srgrimes } 12501556Srgrimes STPUTC('=', out); 12511556Srgrimes flags = 0; 12521556Srgrimes if (subtype == 0) { 125317987Speter switch (c) { 125417987Speter case ':': 12551556Srgrimes flags = VSNUL; 12561556Srgrimes c = pgetc(); 125717987Speter /*FALLTHROUGH*/ 125817987Speter default: 125917987Speter p = strchr(types, c); 126017987Speter if (p == NULL) 126117987Speter goto badsub; 126217987Speter subtype = p - types + VSNORMAL; 126317987Speter break; 126417987Speter case '%': 126520425Ssteve case '#': 126617987Speter { 126717987Speter int cc = c; 126817987Speter subtype = c == '#' ? VSTRIMLEFT : 126917987Speter VSTRIMRIGHT; 127017987Speter c = pgetc(); 127117987Speter if (c == cc) 127217987Speter subtype++; 127317987Speter else 127417987Speter pungetc(); 127517987Speter break; 127617987Speter } 12771556Srgrimes } 12781556Srgrimes } else { 12791556Srgrimes pungetc(); 12801556Srgrimes } 128157225Scracauer if (subtype != VSLENGTH && (dblquote || arinest)) 12821556Srgrimes flags |= VSQUOTE; 12831556Srgrimes *(stackblock() + typeloc) = subtype | flags; 12841556Srgrimes if (subtype != VSNORMAL) 12851556Srgrimes varnest++; 12861556Srgrimes } 12871556Srgrimes goto parsesub_return; 12881556Srgrimes} 12891556Srgrimes 12901556Srgrimes 12911556Srgrimes/* 12921556Srgrimes * Called to parse command substitutions. Newstyle is set if the command 12931556Srgrimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked 12941556Srgrimes * list of commands (passed by reference), and savelen is the number of 12951556Srgrimes * characters on the top of the stack which must be preserved. 12961556Srgrimes */ 12971556Srgrimes 12981556Srgrimesparsebackq: { 12991556Srgrimes struct nodelist **nlpp; 13001556Srgrimes int savepbq; 13011556Srgrimes union node *n; 13021556Srgrimes char *volatile str; 13031556Srgrimes struct jmploc jmploc; 13041556Srgrimes struct jmploc *volatile savehandler; 13051556Srgrimes int savelen; 130620425Ssteve int saveprompt; 130720425Ssteve#if __GNUC__ 130820425Ssteve /* Avoid longjmp clobbering */ 130920425Ssteve (void) &saveprompt; 131020425Ssteve#endif 13111556Srgrimes 13121556Srgrimes savepbq = parsebackquote; 13131556Srgrimes if (setjmp(jmploc.loc)) { 13141556Srgrimes if (str) 13151556Srgrimes ckfree(str); 13161556Srgrimes parsebackquote = 0; 13171556Srgrimes handler = savehandler; 13181556Srgrimes longjmp(handler->loc, 1); 13191556Srgrimes } 13201556Srgrimes INTOFF; 13211556Srgrimes str = NULL; 13221556Srgrimes savelen = out - stackblock(); 13231556Srgrimes if (savelen > 0) { 13241556Srgrimes str = ckmalloc(savelen); 132517987Speter memcpy(str, stackblock(), savelen); 13261556Srgrimes } 13271556Srgrimes savehandler = handler; 13281556Srgrimes handler = &jmploc; 13291556Srgrimes INTON; 13301556Srgrimes if (oldstyle) { 13311556Srgrimes /* We must read until the closing backquote, giving special 13321556Srgrimes treatment to some slashes, and then push the string and 13331556Srgrimes reread it as input, interpreting it normally. */ 133425230Ssteve char *out; 133525230Ssteve int c; 13361556Srgrimes int savelen; 13371556Srgrimes char *str; 13388855Srgrimes 133920425Ssteve 13401556Srgrimes STARTSTACKSTR(out); 134120425Ssteve for (;;) { 134220425Ssteve if (needprompt) { 134320425Ssteve setprompt(2); 134420425Ssteve needprompt = 0; 134520425Ssteve } 134620425Ssteve switch (c = pgetc()) { 134720425Ssteve case '`': 134820425Ssteve goto done; 134920425Ssteve 135020425Ssteve case '\\': 135120425Ssteve if ((c = pgetc()) == '\n') { 135220425Ssteve plinno++; 135320425Ssteve if (doprompt) 135420425Ssteve setprompt(2); 135520425Ssteve else 135620425Ssteve setprompt(0); 135720425Ssteve /* 135820425Ssteve * If eating a newline, avoid putting 135920425Ssteve * the newline into the new character 136020425Ssteve * stream (via the STPUTC after the 136120425Ssteve * switch). 136220425Ssteve */ 136320425Ssteve continue; 136420425Ssteve } 136517987Speter if (c != '\\' && c != '`' && c != '$' 13661556Srgrimes && (!dblquote || c != '"')) 13671556Srgrimes STPUTC('\\', out); 136820425Ssteve break; 136920425Ssteve 137020425Ssteve case '\n': 137120425Ssteve plinno++; 137220425Ssteve needprompt = doprompt; 137320425Ssteve break; 137420425Ssteve 137520425Ssteve case PEOF: 137620425Ssteve startlinno = plinno; 137720425Ssteve synerror("EOF in backquote substitution"); 137820425Ssteve break; 137920425Ssteve 138020425Ssteve default: 138120425Ssteve break; 138220425Ssteve } 138320425Ssteve STPUTC(c, out); 13841556Srgrimes } 138520425Sstevedone: 13861556Srgrimes STPUTC('\0', out); 13871556Srgrimes savelen = out - stackblock(); 13881556Srgrimes if (savelen > 0) { 13891556Srgrimes str = ckmalloc(savelen); 139017987Speter memcpy(str, stackblock(), savelen); 139117987Speter setinputstring(str, 1); 13921556Srgrimes } 13931556Srgrimes } 13941556Srgrimes nlpp = &bqlist; 13951556Srgrimes while (*nlpp) 13961556Srgrimes nlpp = &(*nlpp)->next; 13971556Srgrimes *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 13981556Srgrimes (*nlpp)->next = NULL; 13991556Srgrimes parsebackquote = oldstyle; 140020425Ssteve 140120425Ssteve if (oldstyle) { 140220425Ssteve saveprompt = doprompt; 140320425Ssteve doprompt = 0; 140420425Ssteve } 140520425Ssteve 14061556Srgrimes n = list(0); 140720425Ssteve 140820425Ssteve if (oldstyle) 140920425Ssteve doprompt = saveprompt; 141020425Ssteve else { 141120425Ssteve if (readtoken() != TRP) 141220425Ssteve synexpect(TRP); 141320425Ssteve } 141420425Ssteve 14151556Srgrimes (*nlpp)->n = n; 141620425Ssteve if (oldstyle) { 141720425Ssteve /* 141820425Ssteve * Start reading from old file again, ignoring any pushed back 141920425Ssteve * tokens left from the backquote parsing 142020425Ssteve */ 14211556Srgrimes popfile(); 142220425Ssteve tokpushback = 0; 142320425Ssteve } 14241556Srgrimes while (stackblocksize() <= savelen) 14251556Srgrimes growstackblock(); 14261556Srgrimes STARTSTACKSTR(out); 14271556Srgrimes if (str) { 142817987Speter memcpy(out, str, savelen); 14291556Srgrimes STADJUST(savelen, out); 14301556Srgrimes INTOFF; 14311556Srgrimes ckfree(str); 14321556Srgrimes str = NULL; 14331556Srgrimes INTON; 14341556Srgrimes } 14351556Srgrimes parsebackquote = savepbq; 14361556Srgrimes handler = savehandler; 14371556Srgrimes if (arinest || dblquote) 14381556Srgrimes USTPUTC(CTLBACKQ | CTLQUOTE, out); 14391556Srgrimes else 14401556Srgrimes USTPUTC(CTLBACKQ, out); 14411556Srgrimes if (oldstyle) 14421556Srgrimes goto parsebackq_oldreturn; 14431556Srgrimes else 14441556Srgrimes goto parsebackq_newreturn; 14451556Srgrimes} 14461556Srgrimes 14471556Srgrimes/* 14481556Srgrimes * Parse an arithmetic expansion (indicate start of one and set state) 14491556Srgrimes */ 14501556Srgrimesparsearith: { 14511556Srgrimes 14521556Srgrimes if (++arinest == 1) { 14531556Srgrimes prevsyntax = syntax; 14541556Srgrimes syntax = ARISYNTAX; 14551556Srgrimes USTPUTC(CTLARI, out); 145638887Stegge if (dblquote) 145738887Stegge USTPUTC('"',out); 145838887Stegge else 145938887Stegge USTPUTC(' ',out); 14601556Srgrimes } else { 14611556Srgrimes /* 14621556Srgrimes * we collapse embedded arithmetic expansion to 14631556Srgrimes * parenthesis, which should be equivalent 14641556Srgrimes */ 14651556Srgrimes USTPUTC('(', out); 14661556Srgrimes } 14671556Srgrimes goto parsearith_return; 14681556Srgrimes} 14691556Srgrimes 14701556Srgrimes} /* end of readtoken */ 14711556Srgrimes 14721556Srgrimes 14731556Srgrimes 14741556Srgrimes#ifdef mkinit 14751556SrgrimesRESET { 14761556Srgrimes tokpushback = 0; 14771556Srgrimes checkkwd = 0; 14781556Srgrimes} 14791556Srgrimes#endif 14801556Srgrimes 14811556Srgrimes/* 14821556Srgrimes * Returns true if the text contains nothing to expand (no dollar signs 14831556Srgrimes * or backquotes). 14841556Srgrimes */ 14851556Srgrimes 14861556SrgrimesSTATIC int 14871556Srgrimesnoexpand(text) 14881556Srgrimes char *text; 14891556Srgrimes { 149025230Ssteve char *p; 149125230Ssteve char c; 14921556Srgrimes 14931556Srgrimes p = text; 14941556Srgrimes while ((c = *p++) != '\0') { 149539137Stegge if ( c == CTLQUOTEMARK) 149639137Stegge continue; 14971556Srgrimes if (c == CTLESC) 14981556Srgrimes p++; 149954631Scracauer else if (c >= 0 && BASESYNTAX[(int)c] == CCTL) 15001556Srgrimes return 0; 15011556Srgrimes } 15021556Srgrimes return 1; 15031556Srgrimes} 15041556Srgrimes 15051556Srgrimes 15061556Srgrimes/* 15071556Srgrimes * Return true if the argument is a legal variable name (a letter or 15081556Srgrimes * underscore followed by zero or more letters, underscores, and digits). 15091556Srgrimes */ 15101556Srgrimes 15111556Srgrimesint 15121556Srgrimesgoodname(name) 15131556Srgrimes char *name; 15141556Srgrimes { 151525230Ssteve char *p; 15161556Srgrimes 15171556Srgrimes p = name; 15181556Srgrimes if (! is_name(*p)) 15191556Srgrimes return 0; 15201556Srgrimes while (*++p) { 15211556Srgrimes if (! is_in_name(*p)) 15221556Srgrimes return 0; 15231556Srgrimes } 15241556Srgrimes return 1; 15251556Srgrimes} 15261556Srgrimes 15271556Srgrimes 15281556Srgrimes/* 15291556Srgrimes * Called when an unexpected token is read during the parse. The argument 15301556Srgrimes * is the token that is expected, or -1 if more than one type of token can 15311556Srgrimes * occur at this point. 15321556Srgrimes */ 15331556Srgrimes 15341556SrgrimesSTATIC void 153520425Sstevesynexpect(token) 153617987Speter int token; 153717987Speter{ 15381556Srgrimes char msg[64]; 15391556Srgrimes 15401556Srgrimes if (token >= 0) { 15411556Srgrimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 15421556Srgrimes tokname[lasttoken], tokname[token]); 15431556Srgrimes } else { 15441556Srgrimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 15451556Srgrimes } 15461556Srgrimes synerror(msg); 15471556Srgrimes} 15481556Srgrimes 15491556Srgrimes 15501556SrgrimesSTATIC void 15511556Srgrimessynerror(msg) 15521556Srgrimes char *msg; 15531556Srgrimes { 15541556Srgrimes if (commandname) 15551556Srgrimes outfmt(&errout, "%s: %d: ", commandname, startlinno); 15561556Srgrimes outfmt(&errout, "Syntax error: %s\n", msg); 15571556Srgrimes error((char *)NULL); 15581556Srgrimes} 15591556Srgrimes 15601556SrgrimesSTATIC void 15611556Srgrimessetprompt(which) 15621556Srgrimes int which; 15631556Srgrimes { 15641556Srgrimes whichprompt = which; 15651556Srgrimes 156617987Speter#ifndef NO_HISTORY 15671556Srgrimes if (!el) 156817987Speter#endif 15691556Srgrimes out2str(getprompt(NULL)); 15701556Srgrimes} 15711556Srgrimes 15721556Srgrimes/* 15731556Srgrimes * called by editline -- any expansions to the prompt 15741556Srgrimes * should be added here. 15751556Srgrimes */ 15761556Srgrimeschar * 15771556Srgrimesgetprompt(unused) 157825905Ssteve void *unused __unused; 157925905Ssteve{ 15801556Srgrimes switch (whichprompt) { 15811556Srgrimes case 0: 15821556Srgrimes return ""; 15831556Srgrimes case 1: 15841556Srgrimes return ps1val(); 15851556Srgrimes case 2: 15861556Srgrimes return ps2val(); 15871556Srgrimes default: 15881556Srgrimes return "<internal prompt error>"; 15891556Srgrimes } 15901556Srgrimes} 1591