14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1991, 1993 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Kenneth Almquist. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 164b88c807SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 174b88c807SRodney W. Grimes * must display the following acknowledgement: 184b88c807SRodney W. Grimes * This product includes software developed by the University of 194b88c807SRodney W. Grimes * California, Berkeley and its contributors. 204b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 214b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 224b88c807SRodney W. Grimes * without specific prior written permission. 234b88c807SRodney W. Grimes * 244b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 254b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 264b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 274b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 284b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 294b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 304b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 314b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 324b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 334b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 344b88c807SRodney W. Grimes * SUCH DAMAGE. 3589730b29SDavid Greenman * 365c817731SPeter Wemm * $Id$ 374b88c807SRodney W. Grimes */ 384b88c807SRodney W. Grimes 394b88c807SRodney W. Grimes #ifndef lint 40aa9caaf6SPeter Wemm static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; 414b88c807SRodney W. Grimes #endif /* not lint */ 424b88c807SRodney W. Grimes 43aa9caaf6SPeter Wemm #include <stdlib.h> 44aa9caaf6SPeter Wemm 454b88c807SRodney W. Grimes #include "shell.h" 464b88c807SRodney W. Grimes #include "parser.h" 474b88c807SRodney W. Grimes #include "nodes.h" 484b88c807SRodney W. Grimes #include "expand.h" /* defines rmescapes() */ 494b88c807SRodney W. Grimes #include "redir.h" /* defines copyfd() */ 504b88c807SRodney W. Grimes #include "syntax.h" 514b88c807SRodney W. Grimes #include "options.h" 524b88c807SRodney W. Grimes #include "input.h" 534b88c807SRodney W. Grimes #include "output.h" 544b88c807SRodney W. Grimes #include "var.h" 554b88c807SRodney W. Grimes #include "error.h" 564b88c807SRodney W. Grimes #include "memalloc.h" 574b88c807SRodney W. Grimes #include "mystring.h" 584b88c807SRodney W. Grimes #include "alias.h" 59aa9caaf6SPeter Wemm #include "show.h" 60aa9caaf6SPeter Wemm #ifndef NO_HISTORY 614b88c807SRodney W. Grimes #include "myhistedit.h" 62aa9caaf6SPeter Wemm #endif 634b88c807SRodney W. Grimes 644b88c807SRodney W. Grimes /* 654b88c807SRodney W. Grimes * Shell command parser. 664b88c807SRodney W. Grimes */ 674b88c807SRodney W. Grimes 684b88c807SRodney W. Grimes #define EOFMARKLEN 79 694b88c807SRodney W. Grimes 704b88c807SRodney W. Grimes /* values returned by readtoken */ 71aa9caaf6SPeter Wemm #include "token.h" 724b88c807SRodney W. Grimes 734b88c807SRodney W. Grimes 744b88c807SRodney W. Grimes 754b88c807SRodney W. Grimes struct heredoc { 764b88c807SRodney W. Grimes struct heredoc *next; /* next here document in list */ 774b88c807SRodney W. Grimes union node *here; /* redirection node */ 784b88c807SRodney W. Grimes char *eofmark; /* string indicating end of input */ 794b88c807SRodney W. Grimes int striptabs; /* if set, strip leading tabs */ 804b88c807SRodney W. Grimes }; 814b88c807SRodney W. Grimes 824b88c807SRodney W. Grimes 834b88c807SRodney W. Grimes 844b88c807SRodney W. Grimes struct heredoc *heredoclist; /* list of here documents to read */ 854b88c807SRodney W. Grimes int parsebackquote; /* nonzero if we are inside backquotes */ 864b88c807SRodney W. Grimes int doprompt; /* if set, prompt the user */ 874b88c807SRodney W. Grimes int needprompt; /* true if interactive and at start of line */ 884b88c807SRodney W. Grimes int lasttoken; /* last token read */ 894b88c807SRodney W. Grimes MKINIT int tokpushback; /* last token pushed back */ 904b88c807SRodney W. Grimes char *wordtext; /* text of last word returned by readtoken */ 914b88c807SRodney W. Grimes MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ 924b88c807SRodney W. Grimes struct nodelist *backquotelist; 934b88c807SRodney W. Grimes union node *redirnode; 944b88c807SRodney W. Grimes struct heredoc *heredoc; 954b88c807SRodney W. Grimes int quoteflag; /* set if (part of) last token was quoted */ 964b88c807SRodney W. Grimes int startlinno; /* line # where last token started */ 974b88c807SRodney W. Grimes 984417f629SPeter Wemm /* XXX When 'noaliases' is set to one, no alias expansion takes place. */ 994417f629SPeter Wemm static int noaliases = 0; 1004b88c807SRodney W. Grimes 1014b88c807SRodney W. Grimes #define GDB_HACK 1 /* avoid local declarations which gdb can't handle */ 1024b88c807SRodney W. Grimes #ifdef GDB_HACK 1034b88c807SRodney W. Grimes static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'}; 1044b88c807SRodney W. Grimes static const char types[] = "}-+?="; 1054b88c807SRodney W. Grimes #endif 1064b88c807SRodney W. Grimes 1074b88c807SRodney W. Grimes 1084b88c807SRodney W. Grimes STATIC union node *list __P((int)); 1094b88c807SRodney W. Grimes STATIC union node *andor __P((void)); 1104b88c807SRodney W. Grimes STATIC union node *pipeline __P((void)); 1114b88c807SRodney W. Grimes STATIC union node *command __P((void)); 1124b88c807SRodney W. Grimes STATIC union node *simplecmd __P((union node **, union node *)); 113aa9caaf6SPeter Wemm STATIC union node *makename __P((void)); 1144b88c807SRodney W. Grimes STATIC void parsefname __P((void)); 1154b88c807SRodney W. Grimes STATIC void parseheredoc __P((void)); 116aa9caaf6SPeter Wemm STATIC int peektoken __P((void)); 1174b88c807SRodney W. Grimes STATIC int readtoken __P((void)); 118aa9caaf6SPeter Wemm STATIC int xxreadtoken __P((void)); 1194b88c807SRodney W. Grimes STATIC int readtoken1 __P((int, char const *, char *, int)); 1204b88c807SRodney W. Grimes STATIC int noexpand __P((char *)); 1214b88c807SRodney W. Grimes STATIC void synexpect __P((int)); 1224b88c807SRodney W. Grimes STATIC void synerror __P((char *)); 1234b88c807SRodney W. Grimes STATIC void setprompt __P((int)); 1244b88c807SRodney W. Grimes 125aa9caaf6SPeter Wemm 1264b88c807SRodney W. Grimes /* 1274b88c807SRodney W. Grimes * Read and parse a command. Returns NEOF on end of file. (NULL is a 1284b88c807SRodney W. Grimes * valid parse tree indicating a blank line.) 1294b88c807SRodney W. Grimes */ 1304b88c807SRodney W. Grimes 1314b88c807SRodney W. Grimes union node * 132aa9caaf6SPeter Wemm parsecmd(interact) 133aa9caaf6SPeter Wemm int interact; 134aa9caaf6SPeter Wemm { 1354b88c807SRodney W. Grimes int t; 1364b88c807SRodney W. Grimes 1374b88c807SRodney W. Grimes doprompt = interact; 1384b88c807SRodney W. Grimes if (doprompt) 1394b88c807SRodney W. Grimes setprompt(1); 1404b88c807SRodney W. Grimes else 1414b88c807SRodney W. Grimes setprompt(0); 1424b88c807SRodney W. Grimes needprompt = 0; 1434b88c807SRodney W. Grimes t = readtoken(); 1444b88c807SRodney W. Grimes if (t == TEOF) 1454b88c807SRodney W. Grimes return NEOF; 1464b88c807SRodney W. Grimes if (t == TNL) 1474b88c807SRodney W. Grimes return NULL; 1484b88c807SRodney W. Grimes tokpushback++; 1494b88c807SRodney W. Grimes return list(1); 1504b88c807SRodney W. Grimes } 1514b88c807SRodney W. Grimes 1524b88c807SRodney W. Grimes 1534b88c807SRodney W. Grimes STATIC union node * 154aa9caaf6SPeter Wemm list(nlflag) 155aa9caaf6SPeter Wemm int nlflag; 156aa9caaf6SPeter Wemm { 1574b88c807SRodney W. Grimes union node *n1, *n2, *n3; 158aa9caaf6SPeter Wemm int tok; 1594b88c807SRodney W. Grimes 1604b88c807SRodney W. Grimes checkkwd = 2; 1614b88c807SRodney W. Grimes if (nlflag == 0 && tokendlist[peektoken()]) 1624b88c807SRodney W. Grimes return NULL; 163aa9caaf6SPeter Wemm n1 = NULL; 1644b88c807SRodney W. Grimes for (;;) { 1654b88c807SRodney W. Grimes n2 = andor(); 166aa9caaf6SPeter Wemm tok = readtoken(); 167aa9caaf6SPeter Wemm if (tok == TBACKGND) { 168aa9caaf6SPeter Wemm if (n2->type == NCMD || n2->type == NPIPE) { 169aa9caaf6SPeter Wemm n2->ncmd.backgnd = 1; 170aa9caaf6SPeter Wemm } else if (n2->type == NREDIR) { 171aa9caaf6SPeter Wemm n2->type = NBACKGND; 172aa9caaf6SPeter Wemm } else { 173aa9caaf6SPeter Wemm n3 = (union node *)stalloc(sizeof (struct nredir)); 174aa9caaf6SPeter Wemm n3->type = NBACKGND; 175aa9caaf6SPeter Wemm n3->nredir.n = n2; 176aa9caaf6SPeter Wemm n3->nredir.redirect = NULL; 177aa9caaf6SPeter Wemm n2 = n3; 178aa9caaf6SPeter Wemm } 179aa9caaf6SPeter Wemm } 180aa9caaf6SPeter Wemm if (n1 == NULL) { 181aa9caaf6SPeter Wemm n1 = n2; 182aa9caaf6SPeter Wemm } 183aa9caaf6SPeter Wemm else { 1844b88c807SRodney W. Grimes n3 = (union node *)stalloc(sizeof (struct nbinary)); 1854b88c807SRodney W. Grimes n3->type = NSEMI; 1864b88c807SRodney W. Grimes n3->nbinary.ch1 = n1; 1874b88c807SRodney W. Grimes n3->nbinary.ch2 = n2; 1884b88c807SRodney W. Grimes n1 = n3; 189aa9caaf6SPeter Wemm } 190aa9caaf6SPeter Wemm switch (tok) { 191aa9caaf6SPeter Wemm case TBACKGND: 192aa9caaf6SPeter Wemm case TSEMI: 193aa9caaf6SPeter Wemm tok = readtoken(); 194aa9caaf6SPeter Wemm /* fall through */ 195aa9caaf6SPeter Wemm case TNL: 196aa9caaf6SPeter Wemm if (tok == TNL) { 197aa9caaf6SPeter Wemm parseheredoc(); 198aa9caaf6SPeter Wemm if (nlflag) 199aa9caaf6SPeter Wemm return n1; 200aa9caaf6SPeter Wemm } else { 201aa9caaf6SPeter Wemm tokpushback++; 202aa9caaf6SPeter Wemm } 203aa9caaf6SPeter Wemm checkkwd = 2; 204aa9caaf6SPeter Wemm if (tokendlist[peektoken()]) 205aa9caaf6SPeter Wemm return n1; 2064b88c807SRodney W. Grimes break; 2074b88c807SRodney W. Grimes case TEOF: 2084b88c807SRodney W. Grimes if (heredoclist) 2094b88c807SRodney W. Grimes parseheredoc(); 2104b88c807SRodney W. Grimes else 2114b88c807SRodney W. Grimes pungetc(); /* push back EOF on input */ 2124b88c807SRodney W. Grimes return n1; 2134b88c807SRodney W. Grimes default: 2144b88c807SRodney W. Grimes if (nlflag) 2154b88c807SRodney W. Grimes synexpect(-1); 2164b88c807SRodney W. Grimes tokpushback++; 2174b88c807SRodney W. Grimes return n1; 2184b88c807SRodney W. Grimes } 2194b88c807SRodney W. Grimes } 2204b88c807SRodney W. Grimes } 2214b88c807SRodney W. Grimes 2224b88c807SRodney W. Grimes 2234b88c807SRodney W. Grimes 2244b88c807SRodney W. Grimes STATIC union node * 2254b88c807SRodney W. Grimes andor() { 2264b88c807SRodney W. Grimes union node *n1, *n2, *n3; 2274b88c807SRodney W. Grimes int t; 2284b88c807SRodney W. Grimes 2294b88c807SRodney W. Grimes n1 = pipeline(); 2304b88c807SRodney W. Grimes for (;;) { 2314b88c807SRodney W. Grimes if ((t = readtoken()) == TAND) { 2324b88c807SRodney W. Grimes t = NAND; 2334b88c807SRodney W. Grimes } else if (t == TOR) { 2344b88c807SRodney W. Grimes t = NOR; 2354b88c807SRodney W. Grimes } else { 2364b88c807SRodney W. Grimes tokpushback++; 2374b88c807SRodney W. Grimes return n1; 2384b88c807SRodney W. Grimes } 2394b88c807SRodney W. Grimes n2 = pipeline(); 2404b88c807SRodney W. Grimes n3 = (union node *)stalloc(sizeof (struct nbinary)); 2414b88c807SRodney W. Grimes n3->type = t; 2424b88c807SRodney W. Grimes n3->nbinary.ch1 = n1; 2434b88c807SRodney W. Grimes n3->nbinary.ch2 = n2; 2444b88c807SRodney W. Grimes n1 = n3; 2454b88c807SRodney W. Grimes } 2464b88c807SRodney W. Grimes } 2474b88c807SRodney W. Grimes 2484b88c807SRodney W. Grimes 2494b88c807SRodney W. Grimes 2504b88c807SRodney W. Grimes STATIC union node * 2514b88c807SRodney W. Grimes pipeline() { 2524b88c807SRodney W. Grimes union node *n1, *pipenode, *notnode; 2534b88c807SRodney W. Grimes struct nodelist *lp, *prev; 2544b88c807SRodney W. Grimes int negate = 0; 2554b88c807SRodney W. Grimes 2564b88c807SRodney W. Grimes TRACE(("pipeline: entered\n")); 2574b88c807SRodney W. Grimes while (readtoken() == TNOT) { 2584b88c807SRodney W. Grimes TRACE(("pipeline: TNOT recognized\n")); 2594b88c807SRodney W. Grimes negate = !negate; 2604b88c807SRodney W. Grimes } 2614b88c807SRodney W. Grimes tokpushback++; 2624b88c807SRodney W. Grimes n1 = command(); 2634b88c807SRodney W. Grimes if (readtoken() == TPIPE) { 2644b88c807SRodney W. Grimes pipenode = (union node *)stalloc(sizeof (struct npipe)); 2654b88c807SRodney W. Grimes pipenode->type = NPIPE; 2664b88c807SRodney W. Grimes pipenode->npipe.backgnd = 0; 2674b88c807SRodney W. Grimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 2684b88c807SRodney W. Grimes pipenode->npipe.cmdlist = lp; 2694b88c807SRodney W. Grimes lp->n = n1; 2704b88c807SRodney W. Grimes do { 2714b88c807SRodney W. Grimes prev = lp; 2724b88c807SRodney W. Grimes lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 2734b88c807SRodney W. Grimes lp->n = command(); 2744b88c807SRodney W. Grimes prev->next = lp; 2754b88c807SRodney W. Grimes } while (readtoken() == TPIPE); 2764b88c807SRodney W. Grimes lp->next = NULL; 2774b88c807SRodney W. Grimes n1 = pipenode; 2784b88c807SRodney W. Grimes } 2794b88c807SRodney W. Grimes tokpushback++; 2804b88c807SRodney W. Grimes if (negate) { 2814b88c807SRodney W. Grimes notnode = (union node *)stalloc(sizeof (struct nnot)); 2824b88c807SRodney W. Grimes notnode->type = NNOT; 2834b88c807SRodney W. Grimes notnode->nnot.com = n1; 2844b88c807SRodney W. Grimes n1 = notnode; 2854b88c807SRodney W. Grimes } 2864b88c807SRodney W. Grimes return n1; 2874b88c807SRodney W. Grimes } 2884b88c807SRodney W. Grimes 2894b88c807SRodney W. Grimes 2904b88c807SRodney W. Grimes 2914b88c807SRodney W. Grimes STATIC union node * 2924b88c807SRodney W. Grimes command() { 2934b88c807SRodney W. Grimes union node *n1, *n2; 2944b88c807SRodney W. Grimes union node *ap, **app; 2954b88c807SRodney W. Grimes union node *cp, **cpp; 2964b88c807SRodney W. Grimes union node *redir, **rpp; 2974b88c807SRodney W. Grimes int t; 2984b88c807SRodney W. Grimes 2994b88c807SRodney W. Grimes checkkwd = 2; 300aa9caaf6SPeter Wemm redir = NULL; 301aa9caaf6SPeter Wemm n1 = NULL; 3024b88c807SRodney W. Grimes rpp = &redir; 3034b88c807SRodney W. Grimes /* Check for redirection which may precede command */ 3044b88c807SRodney W. Grimes while (readtoken() == TREDIR) { 3054b88c807SRodney W. Grimes *rpp = n2 = redirnode; 3064b88c807SRodney W. Grimes rpp = &n2->nfile.next; 3074b88c807SRodney W. Grimes parsefname(); 3084b88c807SRodney W. Grimes } 3094b88c807SRodney W. Grimes tokpushback++; 3104b88c807SRodney W. Grimes 3114b88c807SRodney W. Grimes switch (readtoken()) { 3124b88c807SRodney W. Grimes case TIF: 3134b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nif)); 3144b88c807SRodney W. Grimes n1->type = NIF; 3154b88c807SRodney W. Grimes n1->nif.test = list(0); 3164b88c807SRodney W. Grimes if (readtoken() != TTHEN) 3174b88c807SRodney W. Grimes synexpect(TTHEN); 3184b88c807SRodney W. Grimes n1->nif.ifpart = list(0); 3194b88c807SRodney W. Grimes n2 = n1; 3204b88c807SRodney W. Grimes while (readtoken() == TELIF) { 3214b88c807SRodney W. Grimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 3224b88c807SRodney W. Grimes n2 = n2->nif.elsepart; 3234b88c807SRodney W. Grimes n2->type = NIF; 3244b88c807SRodney W. Grimes n2->nif.test = list(0); 3254b88c807SRodney W. Grimes if (readtoken() != TTHEN) 3264b88c807SRodney W. Grimes synexpect(TTHEN); 3274b88c807SRodney W. Grimes n2->nif.ifpart = list(0); 3284b88c807SRodney W. Grimes } 3294b88c807SRodney W. Grimes if (lasttoken == TELSE) 3304b88c807SRodney W. Grimes n2->nif.elsepart = list(0); 3314b88c807SRodney W. Grimes else { 3324b88c807SRodney W. Grimes n2->nif.elsepart = NULL; 3334b88c807SRodney W. Grimes tokpushback++; 3344b88c807SRodney W. Grimes } 3354b88c807SRodney W. Grimes if (readtoken() != TFI) 3364b88c807SRodney W. Grimes synexpect(TFI); 3374b88c807SRodney W. Grimes checkkwd = 1; 3384b88c807SRodney W. Grimes break; 3394b88c807SRodney W. Grimes case TWHILE: 3404b88c807SRodney W. Grimes case TUNTIL: { 3414b88c807SRodney W. Grimes int got; 3424b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nbinary)); 3434b88c807SRodney W. Grimes n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 3444b88c807SRodney W. Grimes n1->nbinary.ch1 = list(0); 3454b88c807SRodney W. Grimes if ((got=readtoken()) != TDO) { 3464b88c807SRodney W. Grimes TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 3474b88c807SRodney W. Grimes synexpect(TDO); 3484b88c807SRodney W. Grimes } 3494b88c807SRodney W. Grimes n1->nbinary.ch2 = list(0); 3504b88c807SRodney W. Grimes if (readtoken() != TDONE) 3514b88c807SRodney W. Grimes synexpect(TDONE); 3524b88c807SRodney W. Grimes checkkwd = 1; 3534b88c807SRodney W. Grimes break; 3544b88c807SRodney W. Grimes } 3554b88c807SRodney W. Grimes case TFOR: 3564b88c807SRodney W. Grimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 3574b88c807SRodney W. Grimes synerror("Bad for loop variable"); 3584b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nfor)); 3594b88c807SRodney W. Grimes n1->type = NFOR; 3604b88c807SRodney W. Grimes n1->nfor.var = wordtext; 3614b88c807SRodney W. Grimes if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) { 3624b88c807SRodney W. Grimes app = ≈ 3634b88c807SRodney W. Grimes while (readtoken() == TWORD) { 3644b88c807SRodney W. Grimes n2 = (union node *)stalloc(sizeof (struct narg)); 3654b88c807SRodney W. Grimes n2->type = NARG; 3664b88c807SRodney W. Grimes n2->narg.text = wordtext; 3674b88c807SRodney W. Grimes n2->narg.backquote = backquotelist; 3684b88c807SRodney W. Grimes *app = n2; 3694b88c807SRodney W. Grimes app = &n2->narg.next; 3704b88c807SRodney W. Grimes } 3714b88c807SRodney W. Grimes *app = NULL; 3724b88c807SRodney W. Grimes n1->nfor.args = ap; 3734b88c807SRodney W. Grimes if (lasttoken != TNL && lasttoken != TSEMI) 3744b88c807SRodney W. Grimes synexpect(-1); 3754b88c807SRodney W. Grimes } else { 3764b88c807SRodney W. Grimes #ifndef GDB_HACK 3774b88c807SRodney W. Grimes static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, 3784b88c807SRodney W. Grimes '@', '=', '\0'}; 3794b88c807SRodney W. Grimes #endif 3804b88c807SRodney W. Grimes n2 = (union node *)stalloc(sizeof (struct narg)); 3814b88c807SRodney W. Grimes n2->type = NARG; 3824b88c807SRodney W. Grimes n2->narg.text = (char *)argvars; 3834b88c807SRodney W. Grimes n2->narg.backquote = NULL; 3844b88c807SRodney W. Grimes n2->narg.next = NULL; 3854b88c807SRodney W. Grimes n1->nfor.args = n2; 3864b88c807SRodney W. Grimes /* 3874b88c807SRodney W. Grimes * Newline or semicolon here is optional (but note 3884b88c807SRodney W. Grimes * that the original Bourne shell only allowed NL). 3894b88c807SRodney W. Grimes */ 3904b88c807SRodney W. Grimes if (lasttoken != TNL && lasttoken != TSEMI) 3914b88c807SRodney W. Grimes tokpushback++; 3924b88c807SRodney W. Grimes } 3934b88c807SRodney W. Grimes checkkwd = 2; 3944b88c807SRodney W. Grimes if ((t = readtoken()) == TDO) 3954b88c807SRodney W. Grimes t = TDONE; 3964b88c807SRodney W. Grimes else if (t == TBEGIN) 3974b88c807SRodney W. Grimes t = TEND; 3984b88c807SRodney W. Grimes else 3994b88c807SRodney W. Grimes synexpect(-1); 4004b88c807SRodney W. Grimes n1->nfor.body = list(0); 4014b88c807SRodney W. Grimes if (readtoken() != t) 4024b88c807SRodney W. Grimes synexpect(t); 4034b88c807SRodney W. Grimes checkkwd = 1; 4044b88c807SRodney W. Grimes break; 4054b88c807SRodney W. Grimes case TCASE: 4064b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct ncase)); 4074b88c807SRodney W. Grimes n1->type = NCASE; 4084b88c807SRodney W. Grimes if (readtoken() != TWORD) 4094b88c807SRodney W. Grimes synexpect(TWORD); 4104b88c807SRodney W. Grimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 4114b88c807SRodney W. Grimes n2->type = NARG; 4124b88c807SRodney W. Grimes n2->narg.text = wordtext; 4134b88c807SRodney W. Grimes n2->narg.backquote = backquotelist; 4144b88c807SRodney W. Grimes n2->narg.next = NULL; 4154b88c807SRodney W. Grimes while (readtoken() == TNL); 4164b88c807SRodney W. Grimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 4174b88c807SRodney W. Grimes synerror("expecting \"in\""); 4184b88c807SRodney W. Grimes cpp = &n1->ncase.cases; 4194417f629SPeter Wemm noaliases = 1; /* turn off alias expansion */ 420650488feSSean Eric Fagan checkkwd = 2, readtoken(); 421650488feSSean Eric Fagan do { 4224b88c807SRodney W. Grimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 4234b88c807SRodney W. Grimes cp->type = NCLIST; 4244b88c807SRodney W. Grimes app = &cp->nclist.pattern; 4254b88c807SRodney W. Grimes for (;;) { 4264b88c807SRodney W. Grimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 4274b88c807SRodney W. Grimes ap->type = NARG; 4284b88c807SRodney W. Grimes ap->narg.text = wordtext; 4294b88c807SRodney W. Grimes ap->narg.backquote = backquotelist; 430650488feSSean Eric Fagan if (checkkwd = 2, readtoken() != TPIPE) 4314b88c807SRodney W. Grimes break; 4324b88c807SRodney W. Grimes app = &ap->narg.next; 433650488feSSean Eric Fagan readtoken(); 4344b88c807SRodney W. Grimes } 4354b88c807SRodney W. Grimes ap->narg.next = NULL; 4364b88c807SRodney W. Grimes if (lasttoken != TRP) 4374417f629SPeter Wemm noaliases = 0, synexpect(TRP); 4384b88c807SRodney W. Grimes cp->nclist.body = list(0); 439650488feSSean Eric Fagan 440650488feSSean Eric Fagan checkkwd = 2; 441650488feSSean Eric Fagan if ((t = readtoken()) != TESAC) { 442650488feSSean Eric Fagan if (t != TENDCASE) 4434417f629SPeter Wemm noaliases = 0, synexpect(TENDCASE); 444650488feSSean Eric Fagan else 445650488feSSean Eric Fagan checkkwd = 2, readtoken(); 4464b88c807SRodney W. Grimes } 447650488feSSean Eric Fagan cpp = &cp->nclist.next; 448650488feSSean Eric Fagan } while(lasttoken != TESAC); 4494417f629SPeter Wemm noaliases = 0; /* reset alias expansion */ 4504b88c807SRodney W. Grimes *cpp = NULL; 4514b88c807SRodney W. Grimes checkkwd = 1; 4524b88c807SRodney W. Grimes break; 4534b88c807SRodney W. Grimes case TLP: 4544b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nredir)); 4554b88c807SRodney W. Grimes n1->type = NSUBSHELL; 4564b88c807SRodney W. Grimes n1->nredir.n = list(0); 4574b88c807SRodney W. Grimes n1->nredir.redirect = NULL; 4584b88c807SRodney W. Grimes if (readtoken() != TRP) 4594b88c807SRodney W. Grimes synexpect(TRP); 4604b88c807SRodney W. Grimes checkkwd = 1; 4614b88c807SRodney W. Grimes break; 4624b88c807SRodney W. Grimes case TBEGIN: 4634b88c807SRodney W. Grimes n1 = list(0); 4644b88c807SRodney W. Grimes if (readtoken() != TEND) 4654b88c807SRodney W. Grimes synexpect(TEND); 4664b88c807SRodney W. Grimes checkkwd = 1; 4674b88c807SRodney W. Grimes break; 4684b88c807SRodney W. Grimes /* Handle an empty command like other simple commands. */ 469248ffae5SJoerg Wunsch case TSEMI: 470aa9caaf6SPeter Wemm /* 471aa9caaf6SPeter Wemm * An empty command before a ; doesn't make much sense, and 472aa9caaf6SPeter Wemm * should certainly be disallowed in the case of `if ;'. 473aa9caaf6SPeter Wemm */ 474aa9caaf6SPeter Wemm if (!redir) 475aa9caaf6SPeter Wemm synexpect(-1); 476aa9caaf6SPeter Wemm case TAND: /* XXX merge query! */ 477aa9caaf6SPeter Wemm case TOR: /* XXX merge query! */ 478aa9caaf6SPeter Wemm case TNL: 479248ffae5SJoerg Wunsch case TEOF: 4804b88c807SRodney W. Grimes case TWORD: 481aa9caaf6SPeter Wemm case TRP: 4824b88c807SRodney W. Grimes tokpushback++; 4834b88c807SRodney W. Grimes return simplecmd(rpp, redir); 4844b88c807SRodney W. Grimes default: 4854b88c807SRodney W. Grimes synexpect(-1); 4864b88c807SRodney W. Grimes } 4874b88c807SRodney W. Grimes 4884b88c807SRodney W. Grimes /* Now check for redirection which may follow command */ 4894b88c807SRodney W. Grimes while (readtoken() == TREDIR) { 4904b88c807SRodney W. Grimes *rpp = n2 = redirnode; 4914b88c807SRodney W. Grimes rpp = &n2->nfile.next; 4924b88c807SRodney W. Grimes parsefname(); 4934b88c807SRodney W. Grimes } 4944b88c807SRodney W. Grimes tokpushback++; 4954b88c807SRodney W. Grimes *rpp = NULL; 4964b88c807SRodney W. Grimes if (redir) { 4974b88c807SRodney W. Grimes if (n1->type != NSUBSHELL) { 4984b88c807SRodney W. Grimes n2 = (union node *)stalloc(sizeof (struct nredir)); 4994b88c807SRodney W. Grimes n2->type = NREDIR; 5004b88c807SRodney W. Grimes n2->nredir.n = n1; 5014b88c807SRodney W. Grimes n1 = n2; 5024b88c807SRodney W. Grimes } 5034b88c807SRodney W. Grimes n1->nredir.redirect = redir; 5044b88c807SRodney W. Grimes } 5054b88c807SRodney W. Grimes return n1; 5064b88c807SRodney W. Grimes } 5074b88c807SRodney W. Grimes 5084b88c807SRodney W. Grimes 5094b88c807SRodney W. Grimes STATIC union node * 5104b88c807SRodney W. Grimes simplecmd(rpp, redir) 5114b88c807SRodney W. Grimes union node **rpp, *redir; 5124b88c807SRodney W. Grimes { 5134b88c807SRodney W. Grimes union node *args, **app; 5144b88c807SRodney W. Grimes union node **orig_rpp = rpp; 515aa9caaf6SPeter Wemm union node *n = NULL; 5164b88c807SRodney W. Grimes 5174b88c807SRodney W. Grimes /* If we don't have any redirections already, then we must reset */ 5184b88c807SRodney W. Grimes /* rpp to be the address of the local redir variable. */ 5194b88c807SRodney W. Grimes if (redir == 0) 5204b88c807SRodney W. Grimes rpp = &redir; 5214b88c807SRodney W. Grimes 5224b88c807SRodney W. Grimes args = NULL; 5234b88c807SRodney W. Grimes app = &args; 5244b88c807SRodney W. Grimes /* 5254b88c807SRodney W. Grimes * We save the incoming value, because we need this for shell 5264b88c807SRodney W. Grimes * functions. There can not be a redirect or an argument between 5274b88c807SRodney W. Grimes * the function name and the open parenthesis. 5284b88c807SRodney W. Grimes */ 5294b88c807SRodney W. Grimes orig_rpp = rpp; 5304b88c807SRodney W. Grimes 5314b88c807SRodney W. Grimes for (;;) { 5324b88c807SRodney W. Grimes if (readtoken() == TWORD) { 5334b88c807SRodney W. Grimes n = (union node *)stalloc(sizeof (struct narg)); 5344b88c807SRodney W. Grimes n->type = NARG; 5354b88c807SRodney W. Grimes n->narg.text = wordtext; 5364b88c807SRodney W. Grimes n->narg.backquote = backquotelist; 5374b88c807SRodney W. Grimes *app = n; 5384b88c807SRodney W. Grimes app = &n->narg.next; 5394b88c807SRodney W. Grimes } else if (lasttoken == TREDIR) { 5404b88c807SRodney W. Grimes *rpp = n = redirnode; 5414b88c807SRodney W. Grimes rpp = &n->nfile.next; 5424b88c807SRodney W. Grimes parsefname(); /* read name of redirection file */ 5434b88c807SRodney W. Grimes } else if (lasttoken == TLP && app == &args->narg.next 5444b88c807SRodney W. Grimes && rpp == orig_rpp) { 5454b88c807SRodney W. Grimes /* We have a function */ 5464b88c807SRodney W. Grimes if (readtoken() != TRP) 5474b88c807SRodney W. Grimes synexpect(TRP); 5484b88c807SRodney W. Grimes #ifdef notdef 5494b88c807SRodney W. Grimes if (! goodname(n->narg.text)) 5504b88c807SRodney W. Grimes synerror("Bad function name"); 5514b88c807SRodney W. Grimes #endif 5524b88c807SRodney W. Grimes n->type = NDEFUN; 5534b88c807SRodney W. Grimes n->narg.next = command(); 5544b88c807SRodney W. Grimes return n; 5554b88c807SRodney W. Grimes } else { 5564b88c807SRodney W. Grimes tokpushback++; 5574b88c807SRodney W. Grimes break; 5584b88c807SRodney W. Grimes } 5594b88c807SRodney W. Grimes } 5604b88c807SRodney W. Grimes *app = NULL; 5614b88c807SRodney W. Grimes *rpp = NULL; 5624b88c807SRodney W. Grimes n = (union node *)stalloc(sizeof (struct ncmd)); 5634b88c807SRodney W. Grimes n->type = NCMD; 5644b88c807SRodney W. Grimes n->ncmd.backgnd = 0; 5654b88c807SRodney W. Grimes n->ncmd.args = args; 5664b88c807SRodney W. Grimes n->ncmd.redirect = redir; 5674b88c807SRodney W. Grimes return n; 5684b88c807SRodney W. Grimes } 5694b88c807SRodney W. Grimes 570aa9caaf6SPeter Wemm STATIC union node * 571aa9caaf6SPeter Wemm makename() { 572aa9caaf6SPeter Wemm union node *n; 573aa9caaf6SPeter Wemm 574aa9caaf6SPeter Wemm n = (union node *)stalloc(sizeof (struct narg)); 575aa9caaf6SPeter Wemm n->type = NARG; 576aa9caaf6SPeter Wemm n->narg.next = NULL; 577aa9caaf6SPeter Wemm n->narg.text = wordtext; 578aa9caaf6SPeter Wemm n->narg.backquote = backquotelist; 579aa9caaf6SPeter Wemm return n; 580aa9caaf6SPeter Wemm } 581aa9caaf6SPeter Wemm 582aa9caaf6SPeter Wemm void fixredir(n, text, err) 583aa9caaf6SPeter Wemm union node *n; 584aa9caaf6SPeter Wemm const char *text; 585aa9caaf6SPeter Wemm int err; 586aa9caaf6SPeter Wemm { 587aa9caaf6SPeter Wemm TRACE(("Fix redir %s %d\n", text, err)); 588aa9caaf6SPeter Wemm if (!err) 589aa9caaf6SPeter Wemm n->ndup.vname = NULL; 590aa9caaf6SPeter Wemm 591aa9caaf6SPeter Wemm if (is_digit(text[0]) && text[1] == '\0') 592aa9caaf6SPeter Wemm n->ndup.dupfd = digit_val(text[0]); 593aa9caaf6SPeter Wemm else if (text[0] == '-' && text[1] == '\0') 594aa9caaf6SPeter Wemm n->ndup.dupfd = -1; 595aa9caaf6SPeter Wemm else { 596aa9caaf6SPeter Wemm 597aa9caaf6SPeter Wemm if (err) 598aa9caaf6SPeter Wemm synerror("Bad fd number"); 599aa9caaf6SPeter Wemm else 600aa9caaf6SPeter Wemm n->ndup.vname = makename(); 601aa9caaf6SPeter Wemm } 602aa9caaf6SPeter Wemm } 603aa9caaf6SPeter Wemm 6044b88c807SRodney W. Grimes 6054b88c807SRodney W. Grimes STATIC void 6064b88c807SRodney W. Grimes parsefname() { 6074b88c807SRodney W. Grimes union node *n = redirnode; 6084b88c807SRodney W. Grimes 6094b88c807SRodney W. Grimes if (readtoken() != TWORD) 6104b88c807SRodney W. Grimes synexpect(-1); 6114b88c807SRodney W. Grimes if (n->type == NHERE) { 6124b88c807SRodney W. Grimes struct heredoc *here = heredoc; 6134b88c807SRodney W. Grimes struct heredoc *p; 6144b88c807SRodney W. Grimes int i; 6154b88c807SRodney W. Grimes 6164b88c807SRodney W. Grimes if (quoteflag == 0) 6174b88c807SRodney W. Grimes n->type = NXHERE; 6184b88c807SRodney W. Grimes TRACE(("Here document %d\n", n->type)); 6194b88c807SRodney W. Grimes if (here->striptabs) { 6204b88c807SRodney W. Grimes while (*wordtext == '\t') 6214b88c807SRodney W. Grimes wordtext++; 6224b88c807SRodney W. Grimes } 6234b88c807SRodney W. Grimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 6244b88c807SRodney W. Grimes synerror("Illegal eof marker for << redirection"); 6254b88c807SRodney W. Grimes rmescapes(wordtext); 6264b88c807SRodney W. Grimes here->eofmark = wordtext; 6274b88c807SRodney W. Grimes here->next = NULL; 6284b88c807SRodney W. Grimes if (heredoclist == NULL) 6294b88c807SRodney W. Grimes heredoclist = here; 6304b88c807SRodney W. Grimes else { 6314b88c807SRodney W. Grimes for (p = heredoclist ; p->next ; p = p->next); 6324b88c807SRodney W. Grimes p->next = here; 6334b88c807SRodney W. Grimes } 6344b88c807SRodney W. Grimes } else if (n->type == NTOFD || n->type == NFROMFD) { 635aa9caaf6SPeter Wemm fixredir(n, wordtext, 0); 6364b88c807SRodney W. Grimes } else { 637aa9caaf6SPeter Wemm n->nfile.fname = makename(); 6384b88c807SRodney W. Grimes } 6394b88c807SRodney W. Grimes } 6404b88c807SRodney W. Grimes 6414b88c807SRodney W. Grimes 6424b88c807SRodney W. Grimes /* 6434b88c807SRodney W. Grimes * Input any here documents. 6444b88c807SRodney W. Grimes */ 6454b88c807SRodney W. Grimes 6464b88c807SRodney W. Grimes STATIC void 6474b88c807SRodney W. Grimes parseheredoc() { 6484b88c807SRodney W. Grimes struct heredoc *here; 6494b88c807SRodney W. Grimes union node *n; 6504b88c807SRodney W. Grimes 6514b88c807SRodney W. Grimes while (heredoclist) { 6524b88c807SRodney W. Grimes here = heredoclist; 6534b88c807SRodney W. Grimes heredoclist = here->next; 6544b88c807SRodney W. Grimes if (needprompt) { 6554b88c807SRodney W. Grimes setprompt(2); 6564b88c807SRodney W. Grimes needprompt = 0; 6574b88c807SRodney W. Grimes } 6584b88c807SRodney W. Grimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 6594b88c807SRodney W. Grimes here->eofmark, here->striptabs); 6604b88c807SRodney W. Grimes n = (union node *)stalloc(sizeof (struct narg)); 6614b88c807SRodney W. Grimes n->narg.type = NARG; 6624b88c807SRodney W. Grimes n->narg.next = NULL; 6634b88c807SRodney W. Grimes n->narg.text = wordtext; 6644b88c807SRodney W. Grimes n->narg.backquote = backquotelist; 6654b88c807SRodney W. Grimes here->here->nhere.doc = n; 6664b88c807SRodney W. Grimes } 6674b88c807SRodney W. Grimes } 6684b88c807SRodney W. Grimes 6694b88c807SRodney W. Grimes STATIC int 6704b88c807SRodney W. Grimes peektoken() { 6714b88c807SRodney W. Grimes int t; 6724b88c807SRodney W. Grimes 6734b88c807SRodney W. Grimes t = readtoken(); 6744b88c807SRodney W. Grimes tokpushback++; 6754b88c807SRodney W. Grimes return (t); 6764b88c807SRodney W. Grimes } 6774b88c807SRodney W. Grimes 6784b88c807SRodney W. Grimes STATIC int xxreadtoken(); 6794b88c807SRodney W. Grimes 6804b88c807SRodney W. Grimes STATIC int 6814b88c807SRodney W. Grimes readtoken() { 6824b88c807SRodney W. Grimes int t; 6834b88c807SRodney W. Grimes int savecheckkwd = checkkwd; 6844b88c807SRodney W. Grimes struct alias *ap; 6854b88c807SRodney W. Grimes #ifdef DEBUG 6864b88c807SRodney W. Grimes int alreadyseen = tokpushback; 6874b88c807SRodney W. Grimes #endif 6884b88c807SRodney W. Grimes 6894b88c807SRodney W. Grimes top: 6904b88c807SRodney W. Grimes t = xxreadtoken(); 6914b88c807SRodney W. Grimes 6924b88c807SRodney W. Grimes if (checkkwd) { 6934b88c807SRodney W. Grimes /* 6944b88c807SRodney W. Grimes * eat newlines 6954b88c807SRodney W. Grimes */ 6964b88c807SRodney W. Grimes if (checkkwd == 2) { 6974b88c807SRodney W. Grimes checkkwd = 0; 6984b88c807SRodney W. Grimes while (t == TNL) { 6994b88c807SRodney W. Grimes parseheredoc(); 7004b88c807SRodney W. Grimes t = xxreadtoken(); 7014b88c807SRodney W. Grimes } 7024b88c807SRodney W. Grimes } else 7034b88c807SRodney W. Grimes checkkwd = 0; 7044b88c807SRodney W. Grimes /* 7054b88c807SRodney W. Grimes * check for keywords and aliases 7064b88c807SRodney W. Grimes */ 707aa9caaf6SPeter Wemm if (t == TWORD && !quoteflag) 708aa9caaf6SPeter Wemm { 709aa9caaf6SPeter Wemm register char * const *pp; 7104b88c807SRodney W. Grimes 7114b88c807SRodney W. Grimes for (pp = (char **)parsekwd; *pp; pp++) { 712aa9caaf6SPeter Wemm if (**pp == *wordtext && equal(*pp, wordtext)) 713aa9caaf6SPeter Wemm { 7144b88c807SRodney W. Grimes lasttoken = t = pp - parsekwd + KWDOFFSET; 7154b88c807SRodney W. Grimes TRACE(("keyword %s recognized\n", tokname[t])); 7164b88c807SRodney W. Grimes goto out; 7174b88c807SRodney W. Grimes } 7184b88c807SRodney W. Grimes } 7194417f629SPeter Wemm if (noaliases == 0 && 7204417f629SPeter Wemm (ap = lookupalias(wordtext, 1)) != NULL) { 7214b88c807SRodney W. Grimes pushstring(ap->val, strlen(ap->val), ap); 7224b88c807SRodney W. Grimes checkkwd = savecheckkwd; 7234b88c807SRodney W. Grimes goto top; 7244b88c807SRodney W. Grimes } 7254b88c807SRodney W. Grimes } 7264b88c807SRodney W. Grimes out: 7274b88c807SRodney W. Grimes checkkwd = 0; 7284b88c807SRodney W. Grimes } 7294b88c807SRodney W. Grimes #ifdef DEBUG 7304b88c807SRodney W. Grimes if (!alreadyseen) 7314b88c807SRodney W. Grimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7324b88c807SRodney W. Grimes else 7334b88c807SRodney W. Grimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7344b88c807SRodney W. Grimes #endif 7354b88c807SRodney W. Grimes return (t); 7364b88c807SRodney W. Grimes } 7374b88c807SRodney W. Grimes 7384b88c807SRodney W. Grimes 7394b88c807SRodney W. Grimes /* 7404b88c807SRodney W. Grimes * Read the next input token. 7414b88c807SRodney W. Grimes * If the token is a word, we set backquotelist to the list of cmds in 7424b88c807SRodney W. Grimes * backquotes. We set quoteflag to true if any part of the word was 7434b88c807SRodney W. Grimes * quoted. 7444b88c807SRodney W. Grimes * If the token is TREDIR, then we set redirnode to a structure containing 7454b88c807SRodney W. Grimes * the redirection. 7464b88c807SRodney W. Grimes * In all cases, the variable startlinno is set to the number of the line 7474b88c807SRodney W. Grimes * on which the token starts. 7484b88c807SRodney W. Grimes * 7494b88c807SRodney W. Grimes * [Change comment: here documents and internal procedures] 7504b88c807SRodney W. Grimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 7514b88c807SRodney W. Grimes * word parsing code into a separate routine. In this case, readtoken 7524b88c807SRodney W. Grimes * doesn't need to have any internal procedures, but parseword does. 7534b88c807SRodney W. Grimes * We could also make parseoperator in essence the main routine, and 7544b88c807SRodney W. Grimes * have parseword (readtoken1?) handle both words and redirection.] 7554b88c807SRodney W. Grimes */ 7564b88c807SRodney W. Grimes 7574b88c807SRodney W. Grimes #define RETURN(token) return lasttoken = token 7584b88c807SRodney W. Grimes 7594b88c807SRodney W. Grimes STATIC int 7604b88c807SRodney W. Grimes xxreadtoken() { 7614b88c807SRodney W. Grimes register c; 7624b88c807SRodney W. Grimes 7634b88c807SRodney W. Grimes if (tokpushback) { 7644b88c807SRodney W. Grimes tokpushback = 0; 7654b88c807SRodney W. Grimes return lasttoken; 7664b88c807SRodney W. Grimes } 7674b88c807SRodney W. Grimes if (needprompt) { 7684b88c807SRodney W. Grimes setprompt(2); 7694b88c807SRodney W. Grimes needprompt = 0; 7704b88c807SRodney W. Grimes } 7714b88c807SRodney W. Grimes startlinno = plinno; 7724b88c807SRodney W. Grimes for (;;) { /* until token or start of word found */ 7734b88c807SRodney W. Grimes c = pgetc_macro(); 7744b88c807SRodney W. Grimes if (c == ' ' || c == '\t') 7754b88c807SRodney W. Grimes continue; /* quick check for white space first */ 7764b88c807SRodney W. Grimes switch (c) { 7774b88c807SRodney W. Grimes case ' ': case '\t': 7784b88c807SRodney W. Grimes continue; 7794b88c807SRodney W. Grimes case '#': 7804b88c807SRodney W. Grimes while ((c = pgetc()) != '\n' && c != PEOF); 7814b88c807SRodney W. Grimes pungetc(); 7824b88c807SRodney W. Grimes continue; 7834b88c807SRodney W. Grimes case '\\': 7844b88c807SRodney W. Grimes if (pgetc() == '\n') { 7854b88c807SRodney W. Grimes startlinno = ++plinno; 7864b88c807SRodney W. Grimes if (doprompt) 7874b88c807SRodney W. Grimes setprompt(2); 7884b88c807SRodney W. Grimes else 7894b88c807SRodney W. Grimes setprompt(0); 7904b88c807SRodney W. Grimes continue; 7914b88c807SRodney W. Grimes } 7924b88c807SRodney W. Grimes pungetc(); 7934b88c807SRodney W. Grimes goto breakloop; 7944b88c807SRodney W. Grimes case '\n': 7954b88c807SRodney W. Grimes plinno++; 7964b88c807SRodney W. Grimes needprompt = doprompt; 7974b88c807SRodney W. Grimes RETURN(TNL); 7984b88c807SRodney W. Grimes case PEOF: 7994b88c807SRodney W. Grimes RETURN(TEOF); 8004b88c807SRodney W. Grimes case '&': 8014b88c807SRodney W. Grimes if (pgetc() == '&') 8024b88c807SRodney W. Grimes RETURN(TAND); 8034b88c807SRodney W. Grimes pungetc(); 8044b88c807SRodney W. Grimes RETURN(TBACKGND); 8054b88c807SRodney W. Grimes case '|': 8064b88c807SRodney W. Grimes if (pgetc() == '|') 8074b88c807SRodney W. Grimes RETURN(TOR); 8084b88c807SRodney W. Grimes pungetc(); 8094b88c807SRodney W. Grimes RETURN(TPIPE); 8104b88c807SRodney W. Grimes case ';': 8114b88c807SRodney W. Grimes if (pgetc() == ';') 8124b88c807SRodney W. Grimes RETURN(TENDCASE); 8134b88c807SRodney W. Grimes pungetc(); 8144b88c807SRodney W. Grimes RETURN(TSEMI); 8154b88c807SRodney W. Grimes case '(': 8164b88c807SRodney W. Grimes RETURN(TLP); 8174b88c807SRodney W. Grimes case ')': 8184b88c807SRodney W. Grimes RETURN(TRP); 8194b88c807SRodney W. Grimes default: 8204b88c807SRodney W. Grimes goto breakloop; 8214b88c807SRodney W. Grimes } 8224b88c807SRodney W. Grimes } 8234b88c807SRodney W. Grimes breakloop: 8244b88c807SRodney W. Grimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 8254b88c807SRodney W. Grimes #undef RETURN 8264b88c807SRodney W. Grimes } 8274b88c807SRodney W. Grimes 8284b88c807SRodney W. Grimes 8294b88c807SRodney W. Grimes 8304b88c807SRodney W. Grimes /* 8314b88c807SRodney W. Grimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 8324b88c807SRodney W. Grimes * is not NULL, read a here document. In the latter case, eofmark is the 8334b88c807SRodney W. Grimes * word which marks the end of the document and striptabs is true if 8344b88c807SRodney W. Grimes * leading tabs should be stripped from the document. The argument firstc 8354b88c807SRodney W. Grimes * is the first character of the input token or document. 8364b88c807SRodney W. Grimes * 8374b88c807SRodney W. Grimes * Because C does not have internal subroutines, I have simulated them 8384b88c807SRodney W. Grimes * using goto's to implement the subroutine linkage. The following macros 8394b88c807SRodney W. Grimes * will run code that appears at the end of readtoken1. 8404b88c807SRodney W. Grimes */ 8414b88c807SRodney W. Grimes 8424b88c807SRodney W. Grimes #define CHECKEND() {goto checkend; checkend_return:;} 8434b88c807SRodney W. Grimes #define PARSEREDIR() {goto parseredir; parseredir_return:;} 8444b88c807SRodney W. Grimes #define PARSESUB() {goto parsesub; parsesub_return:;} 8454b88c807SRodney W. Grimes #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 8464b88c807SRodney W. Grimes #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 8474b88c807SRodney W. Grimes #define PARSEARITH() {goto parsearith; parsearith_return:;} 8484b88c807SRodney W. Grimes 8494b88c807SRodney W. Grimes STATIC int 8504b88c807SRodney W. Grimes readtoken1(firstc, syntax, eofmark, striptabs) 8514b88c807SRodney W. Grimes int firstc; 8524b88c807SRodney W. Grimes char const *syntax; 8534b88c807SRodney W. Grimes char *eofmark; 8544b88c807SRodney W. Grimes int striptabs; 8554b88c807SRodney W. Grimes { 856aa9caaf6SPeter Wemm int c = firstc; 857aa9caaf6SPeter Wemm char *out; 8584b88c807SRodney W. Grimes int len; 8594b88c807SRodney W. Grimes char line[EOFMARKLEN + 1]; 8604b88c807SRodney W. Grimes struct nodelist *bqlist; 8614b88c807SRodney W. Grimes int quotef; 8624b88c807SRodney W. Grimes int dblquote; 8634b88c807SRodney W. Grimes int varnest; /* levels of variables expansion */ 8644b88c807SRodney W. Grimes int arinest; /* levels of arithmetic expansion */ 8654b88c807SRodney W. Grimes int parenlevel; /* levels of parens in arithmetic */ 8664b88c807SRodney W. Grimes int oldstyle; 8674b88c807SRodney W. Grimes char const *prevsyntax; /* syntax before arithmetic */ 868aa9caaf6SPeter Wemm #if __GNUC__ 869aa9caaf6SPeter Wemm /* Avoid longjmp clobbering */ 870aa9caaf6SPeter Wemm (void) &out; 871aa9caaf6SPeter Wemm (void) "ef; 872aa9caaf6SPeter Wemm (void) &dblquote; 873aa9caaf6SPeter Wemm (void) &varnest; 874aa9caaf6SPeter Wemm (void) &arinest; 875aa9caaf6SPeter Wemm (void) &parenlevel; 876aa9caaf6SPeter Wemm (void) &oldstyle; 877aa9caaf6SPeter Wemm (void) &prevsyntax; 878aa9caaf6SPeter Wemm (void) &syntax; 879aa9caaf6SPeter Wemm #endif 8804b88c807SRodney W. Grimes 8814b88c807SRodney W. Grimes startlinno = plinno; 8824b88c807SRodney W. Grimes dblquote = 0; 8834b88c807SRodney W. Grimes if (syntax == DQSYNTAX) 8844b88c807SRodney W. Grimes dblquote = 1; 8854b88c807SRodney W. Grimes quotef = 0; 8864b88c807SRodney W. Grimes bqlist = NULL; 8874b88c807SRodney W. Grimes varnest = 0; 8884b88c807SRodney W. Grimes arinest = 0; 8894b88c807SRodney W. Grimes parenlevel = 0; 8904b88c807SRodney W. Grimes 8914b88c807SRodney W. Grimes STARTSTACKSTR(out); 8924b88c807SRodney W. Grimes loop: { /* for each line, until end of word */ 8934b88c807SRodney W. Grimes #if ATTY 8944b88c807SRodney W. Grimes if (c == '\034' && doprompt 8954b88c807SRodney W. Grimes && attyset() && ! equal(termval(), "emacs")) { 8964b88c807SRodney W. Grimes attyline(); 8974b88c807SRodney W. Grimes if (syntax == BASESYNTAX) 8984b88c807SRodney W. Grimes return readtoken(); 8994b88c807SRodney W. Grimes c = pgetc(); 9004b88c807SRodney W. Grimes goto loop; 9014b88c807SRodney W. Grimes } 9024b88c807SRodney W. Grimes #endif 9034b88c807SRodney W. Grimes CHECKEND(); /* set c to PEOF if at end of here document */ 9044b88c807SRodney W. Grimes for (;;) { /* until end of line or end of word */ 9054b88c807SRodney W. Grimes CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 9064b88c807SRodney W. Grimes switch(syntax[c]) { 9074b88c807SRodney W. Grimes case CNL: /* '\n' */ 9084b88c807SRodney W. Grimes if (syntax == BASESYNTAX) 9094b88c807SRodney W. Grimes goto endword; /* exit outer loop */ 9104b88c807SRodney W. Grimes USTPUTC(c, out); 9114b88c807SRodney W. Grimes plinno++; 9124b88c807SRodney W. Grimes if (doprompt) 9134b88c807SRodney W. Grimes setprompt(2); 9144b88c807SRodney W. Grimes else 9154b88c807SRodney W. Grimes setprompt(0); 9164b88c807SRodney W. Grimes c = pgetc(); 9174b88c807SRodney W. Grimes goto loop; /* continue outer loop */ 9184b88c807SRodney W. Grimes case CWORD: 9194b88c807SRodney W. Grimes USTPUTC(c, out); 9204b88c807SRodney W. Grimes break; 9214b88c807SRodney W. Grimes case CCTL: 9224b88c807SRodney W. Grimes if (eofmark == NULL || dblquote) 9234b88c807SRodney W. Grimes USTPUTC(CTLESC, out); 9244b88c807SRodney W. Grimes USTPUTC(c, out); 9254b88c807SRodney W. Grimes break; 9264b88c807SRodney W. Grimes case CBACK: /* backslash */ 9274b88c807SRodney W. Grimes c = pgetc(); 9284b88c807SRodney W. Grimes if (c == PEOF) { 9294b88c807SRodney W. Grimes USTPUTC('\\', out); 9304b88c807SRodney W. Grimes pungetc(); 9314b88c807SRodney W. Grimes } else if (c == '\n') { 9324b88c807SRodney W. Grimes if (doprompt) 9334b88c807SRodney W. Grimes setprompt(2); 9344b88c807SRodney W. Grimes else 9354b88c807SRodney W. Grimes setprompt(0); 9364b88c807SRodney W. Grimes } else { 9374b88c807SRodney W. Grimes if (dblquote && c != '\\' && c != '`' && c != '$' 9384b88c807SRodney W. Grimes && (c != '"' || eofmark != NULL)) 9394b88c807SRodney W. Grimes USTPUTC('\\', out); 9404b88c807SRodney W. Grimes if (SQSYNTAX[c] == CCTL) 9414b88c807SRodney W. Grimes USTPUTC(CTLESC, out); 9424b88c807SRodney W. Grimes USTPUTC(c, out); 9434b88c807SRodney W. Grimes quotef++; 9444b88c807SRodney W. Grimes } 9454b88c807SRodney W. Grimes break; 9464b88c807SRodney W. Grimes case CSQUOTE: 9474b88c807SRodney W. Grimes syntax = SQSYNTAX; 9484b88c807SRodney W. Grimes break; 9494b88c807SRodney W. Grimes case CDQUOTE: 9504b88c807SRodney W. Grimes syntax = DQSYNTAX; 9514b88c807SRodney W. Grimes dblquote = 1; 9524b88c807SRodney W. Grimes break; 9534b88c807SRodney W. Grimes case CENDQUOTE: 9544b88c807SRodney W. Grimes if (eofmark) { 9554b88c807SRodney W. Grimes USTPUTC(c, out); 9564b88c807SRodney W. Grimes } else { 9574b88c807SRodney W. Grimes if (arinest) 9584b88c807SRodney W. Grimes syntax = ARISYNTAX; 9594b88c807SRodney W. Grimes else 9604b88c807SRodney W. Grimes syntax = BASESYNTAX; 9614b88c807SRodney W. Grimes quotef++; 9624b88c807SRodney W. Grimes dblquote = 0; 9634b88c807SRodney W. Grimes } 9644b88c807SRodney W. Grimes break; 9654b88c807SRodney W. Grimes case CVAR: /* '$' */ 9664b88c807SRodney W. Grimes PARSESUB(); /* parse substitution */ 9674b88c807SRodney W. Grimes break; 9684b88c807SRodney W. Grimes case CENDVAR: /* '}' */ 9694b88c807SRodney W. Grimes if (varnest > 0) { 9704b88c807SRodney W. Grimes varnest--; 9714b88c807SRodney W. Grimes USTPUTC(CTLENDVAR, out); 9724b88c807SRodney W. Grimes } else { 9734b88c807SRodney W. Grimes USTPUTC(c, out); 9744b88c807SRodney W. Grimes } 9754b88c807SRodney W. Grimes break; 9764b88c807SRodney W. Grimes case CLP: /* '(' in arithmetic */ 9774b88c807SRodney W. Grimes parenlevel++; 9784b88c807SRodney W. Grimes USTPUTC(c, out); 9794b88c807SRodney W. Grimes break; 9804b88c807SRodney W. Grimes case CRP: /* ')' in arithmetic */ 9814b88c807SRodney W. Grimes if (parenlevel > 0) { 9824b88c807SRodney W. Grimes USTPUTC(c, out); 9834b88c807SRodney W. Grimes --parenlevel; 9844b88c807SRodney W. Grimes } else { 9854b88c807SRodney W. Grimes if (pgetc() == ')') { 9864b88c807SRodney W. Grimes if (--arinest == 0) { 9874b88c807SRodney W. Grimes USTPUTC(CTLENDARI, out); 9884b88c807SRodney W. Grimes syntax = prevsyntax; 9894b88c807SRodney W. Grimes } else 9904b88c807SRodney W. Grimes USTPUTC(')', out); 9914b88c807SRodney W. Grimes } else { 9924b88c807SRodney W. Grimes /* 9934b88c807SRodney W. Grimes * unbalanced parens 9944b88c807SRodney W. Grimes * (don't 2nd guess - no error) 9954b88c807SRodney W. Grimes */ 9964b88c807SRodney W. Grimes pungetc(); 9974b88c807SRodney W. Grimes USTPUTC(')', out); 9984b88c807SRodney W. Grimes } 9994b88c807SRodney W. Grimes } 10004b88c807SRodney W. Grimes break; 10014b88c807SRodney W. Grimes case CBQUOTE: /* '`' */ 10024b88c807SRodney W. Grimes PARSEBACKQOLD(); 10034b88c807SRodney W. Grimes break; 10044b88c807SRodney W. Grimes case CEOF: 10054b88c807SRodney W. Grimes goto endword; /* exit outer loop */ 10064b88c807SRodney W. Grimes default: 10074b88c807SRodney W. Grimes if (varnest == 0) 10084b88c807SRodney W. Grimes goto endword; /* exit outer loop */ 10094b88c807SRodney W. Grimes USTPUTC(c, out); 10104b88c807SRodney W. Grimes } 10114b88c807SRodney W. Grimes c = pgetc_macro(); 10124b88c807SRodney W. Grimes } 10134b88c807SRodney W. Grimes } 10144b88c807SRodney W. Grimes endword: 10154b88c807SRodney W. Grimes if (syntax == ARISYNTAX) 10164b88c807SRodney W. Grimes synerror("Missing '))'"); 10174b88c807SRodney W. Grimes if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) 10184b88c807SRodney W. Grimes synerror("Unterminated quoted string"); 10194b88c807SRodney W. Grimes if (varnest != 0) { 10204b88c807SRodney W. Grimes startlinno = plinno; 10214b88c807SRodney W. Grimes synerror("Missing '}'"); 10224b88c807SRodney W. Grimes } 10234b88c807SRodney W. Grimes USTPUTC('\0', out); 10244b88c807SRodney W. Grimes len = out - stackblock(); 10254b88c807SRodney W. Grimes out = stackblock(); 10264b88c807SRodney W. Grimes if (eofmark == NULL) { 10274b88c807SRodney W. Grimes if ((c == '>' || c == '<') 10284b88c807SRodney W. Grimes && quotef == 0 10294b88c807SRodney W. Grimes && len <= 2 10304b88c807SRodney W. Grimes && (*out == '\0' || is_digit(*out))) { 10314b88c807SRodney W. Grimes PARSEREDIR(); 10324b88c807SRodney W. Grimes return lasttoken = TREDIR; 10334b88c807SRodney W. Grimes } else { 10344b88c807SRodney W. Grimes pungetc(); 10354b88c807SRodney W. Grimes } 10364b88c807SRodney W. Grimes } 10374b88c807SRodney W. Grimes quoteflag = quotef; 10384b88c807SRodney W. Grimes backquotelist = bqlist; 10394b88c807SRodney W. Grimes grabstackblock(len); 10404b88c807SRodney W. Grimes wordtext = out; 10414b88c807SRodney W. Grimes return lasttoken = TWORD; 10424b88c807SRodney W. Grimes /* end of readtoken routine */ 10434b88c807SRodney W. Grimes 10444b88c807SRodney W. Grimes 10454b88c807SRodney W. Grimes 10464b88c807SRodney W. Grimes /* 10474b88c807SRodney W. Grimes * Check to see whether we are at the end of the here document. When this 10484b88c807SRodney W. Grimes * is called, c is set to the first character of the next input line. If 10494b88c807SRodney W. Grimes * we are at the end of the here document, this routine sets the c to PEOF. 10504b88c807SRodney W. Grimes */ 10514b88c807SRodney W. Grimes 10524b88c807SRodney W. Grimes checkend: { 10534b88c807SRodney W. Grimes if (eofmark) { 10544b88c807SRodney W. Grimes if (striptabs) { 10554b88c807SRodney W. Grimes while (c == '\t') 10564b88c807SRodney W. Grimes c = pgetc(); 10574b88c807SRodney W. Grimes } 10584b88c807SRodney W. Grimes if (c == *eofmark) { 10594b88c807SRodney W. Grimes if (pfgets(line, sizeof line) != NULL) { 10604b88c807SRodney W. Grimes register char *p, *q; 10614b88c807SRodney W. Grimes 10624b88c807SRodney W. Grimes p = line; 10634b88c807SRodney W. Grimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 10644b88c807SRodney W. Grimes if (*p == '\n' && *q == '\0') { 10654b88c807SRodney W. Grimes c = PEOF; 10664b88c807SRodney W. Grimes plinno++; 10674b88c807SRodney W. Grimes needprompt = doprompt; 10684b88c807SRodney W. Grimes } else { 10694b88c807SRodney W. Grimes pushstring(line, strlen(line), NULL); 10704b88c807SRodney W. Grimes } 10714b88c807SRodney W. Grimes } 10724b88c807SRodney W. Grimes } 10734b88c807SRodney W. Grimes } 10744b88c807SRodney W. Grimes goto checkend_return; 10754b88c807SRodney W. Grimes } 10764b88c807SRodney W. Grimes 10774b88c807SRodney W. Grimes 10784b88c807SRodney W. Grimes /* 10794b88c807SRodney W. Grimes * Parse a redirection operator. The variable "out" points to a string 10804b88c807SRodney W. Grimes * specifying the fd to be redirected. The variable "c" contains the 10814b88c807SRodney W. Grimes * first character of the redirection operator. 10824b88c807SRodney W. Grimes */ 10834b88c807SRodney W. Grimes 10844b88c807SRodney W. Grimes parseredir: { 10854b88c807SRodney W. Grimes char fd = *out; 10864b88c807SRodney W. Grimes union node *np; 10874b88c807SRodney W. Grimes 10884b88c807SRodney W. Grimes np = (union node *)stalloc(sizeof (struct nfile)); 10894b88c807SRodney W. Grimes if (c == '>') { 10904b88c807SRodney W. Grimes np->nfile.fd = 1; 10914b88c807SRodney W. Grimes c = pgetc(); 10924b88c807SRodney W. Grimes if (c == '>') 10934b88c807SRodney W. Grimes np->type = NAPPEND; 10944b88c807SRodney W. Grimes else if (c == '&') 10954b88c807SRodney W. Grimes np->type = NTOFD; 10964b88c807SRodney W. Grimes else { 10974b88c807SRodney W. Grimes np->type = NTO; 10984b88c807SRodney W. Grimes pungetc(); 10994b88c807SRodney W. Grimes } 11004b88c807SRodney W. Grimes } else { /* c == '<' */ 11014b88c807SRodney W. Grimes np->nfile.fd = 0; 11024b88c807SRodney W. Grimes c = pgetc(); 11034b88c807SRodney W. Grimes if (c == '<') { 11044b88c807SRodney W. Grimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 11054b88c807SRodney W. Grimes np = (union node *)stalloc(sizeof (struct nhere)); 11064b88c807SRodney W. Grimes np->nfile.fd = 0; 11074b88c807SRodney W. Grimes } 11084b88c807SRodney W. Grimes np->type = NHERE; 11094b88c807SRodney W. Grimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 11104b88c807SRodney W. Grimes heredoc->here = np; 11114b88c807SRodney W. Grimes if ((c = pgetc()) == '-') { 11124b88c807SRodney W. Grimes heredoc->striptabs = 1; 11134b88c807SRodney W. Grimes } else { 11144b88c807SRodney W. Grimes heredoc->striptabs = 0; 11154b88c807SRodney W. Grimes pungetc(); 11164b88c807SRodney W. Grimes } 11174b88c807SRodney W. Grimes } else if (c == '&') 11184b88c807SRodney W. Grimes np->type = NFROMFD; 11194b88c807SRodney W. Grimes else { 11204b88c807SRodney W. Grimes np->type = NFROM; 11214b88c807SRodney W. Grimes pungetc(); 11224b88c807SRodney W. Grimes } 11234b88c807SRodney W. Grimes } 11244b88c807SRodney W. Grimes if (fd != '\0') 11254b88c807SRodney W. Grimes np->nfile.fd = digit_val(fd); 11264b88c807SRodney W. Grimes redirnode = np; 11274b88c807SRodney W. Grimes goto parseredir_return; 11284b88c807SRodney W. Grimes } 11294b88c807SRodney W. Grimes 11304b88c807SRodney W. Grimes 11314b88c807SRodney W. Grimes /* 11324b88c807SRodney W. Grimes * Parse a substitution. At this point, we have read the dollar sign 11334b88c807SRodney W. Grimes * and nothing else. 11344b88c807SRodney W. Grimes */ 11354b88c807SRodney W. Grimes 11364b88c807SRodney W. Grimes parsesub: { 11374b88c807SRodney W. Grimes int subtype; 11384b88c807SRodney W. Grimes int typeloc; 11394b88c807SRodney W. Grimes int flags; 11404b88c807SRodney W. Grimes char *p; 11414b88c807SRodney W. Grimes #ifndef GDB_HACK 11424b88c807SRodney W. Grimes static const char types[] = "}-+?="; 11434b88c807SRodney W. Grimes #endif 11445c817731SPeter Wemm int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 11454b88c807SRodney W. Grimes 11464b88c807SRodney W. Grimes c = pgetc(); 11474b88c807SRodney W. Grimes if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) { 11484b88c807SRodney W. Grimes USTPUTC('$', out); 11494b88c807SRodney W. Grimes pungetc(); 11504b88c807SRodney W. Grimes } else if (c == '(') { /* $(command) or $((arith)) */ 11514b88c807SRodney W. Grimes if (pgetc() == '(') { 11524b88c807SRodney W. Grimes PARSEARITH(); 11534b88c807SRodney W. Grimes } else { 11544b88c807SRodney W. Grimes pungetc(); 11554b88c807SRodney W. Grimes PARSEBACKQNEW(); 11564b88c807SRodney W. Grimes } 11574b88c807SRodney W. Grimes } else { 11584b88c807SRodney W. Grimes USTPUTC(CTLVAR, out); 11594b88c807SRodney W. Grimes typeloc = out - stackblock(); 11604b88c807SRodney W. Grimes USTPUTC(VSNORMAL, out); 11614b88c807SRodney W. Grimes subtype = VSNORMAL; 11624b88c807SRodney W. Grimes if (c == '{') { 11635c817731SPeter Wemm bracketed_name = 1; 11644b88c807SRodney W. Grimes c = pgetc(); 1165aa9caaf6SPeter Wemm if (c == '#') { 1166aa9caaf6SPeter Wemm if ((c = pgetc()) == '}') 1167aa9caaf6SPeter Wemm c = '#'; 1168aa9caaf6SPeter Wemm else 1169aa9caaf6SPeter Wemm subtype = VSLENGTH; 1170aa9caaf6SPeter Wemm } 1171aa9caaf6SPeter Wemm else 11724b88c807SRodney W. Grimes subtype = 0; 11734b88c807SRodney W. Grimes } 11744b88c807SRodney W. Grimes if (is_name(c)) { 11754b88c807SRodney W. Grimes do { 11764b88c807SRodney W. Grimes STPUTC(c, out); 11774b88c807SRodney W. Grimes c = pgetc(); 11784b88c807SRodney W. Grimes } while (is_in_name(c)); 11795c817731SPeter Wemm } else if (is_digit(c)) { 11805c817731SPeter Wemm if (bracketed_name) { 11815c817731SPeter Wemm do { 11825c817731SPeter Wemm STPUTC(c, out); 11835c817731SPeter Wemm c = pgetc(); 11845c817731SPeter Wemm } while (is_digit(c)); 11855c817731SPeter Wemm } else { 11865c817731SPeter Wemm STPUTC(c, out); 11875c817731SPeter Wemm c = pgetc(); 11885c817731SPeter Wemm } 11894b88c807SRodney W. Grimes } else { 11904b88c807SRodney W. Grimes if (! is_special(c)) 11914b88c807SRodney W. Grimes badsub: synerror("Bad substitution"); 11924b88c807SRodney W. Grimes USTPUTC(c, out); 11934b88c807SRodney W. Grimes c = pgetc(); 11944b88c807SRodney W. Grimes } 11954b88c807SRodney W. Grimes STPUTC('=', out); 11964b88c807SRodney W. Grimes flags = 0; 11974b88c807SRodney W. Grimes if (subtype == 0) { 1198aa9caaf6SPeter Wemm switch (c) { 1199aa9caaf6SPeter Wemm case ':': 12004b88c807SRodney W. Grimes flags = VSNUL; 12014b88c807SRodney W. Grimes c = pgetc(); 1202aa9caaf6SPeter Wemm /*FALLTHROUGH*/ 1203aa9caaf6SPeter Wemm default: 12044b88c807SRodney W. Grimes p = strchr(types, c); 12054b88c807SRodney W. Grimes if (p == NULL) 12064b88c807SRodney W. Grimes goto badsub; 12074b88c807SRodney W. Grimes subtype = p - types + VSNORMAL; 1208aa9caaf6SPeter Wemm break; 1209aa9caaf6SPeter Wemm case '%': 1210aa9caaf6SPeter Wemm case '#': 1211aa9caaf6SPeter Wemm { 1212aa9caaf6SPeter Wemm int cc = c; 1213aa9caaf6SPeter Wemm subtype = c == '#' ? VSTRIMLEFT : 1214aa9caaf6SPeter Wemm VSTRIMRIGHT; 1215aa9caaf6SPeter Wemm c = pgetc(); 1216aa9caaf6SPeter Wemm if (c == cc) 1217aa9caaf6SPeter Wemm subtype++; 1218aa9caaf6SPeter Wemm else 1219aa9caaf6SPeter Wemm pungetc(); 1220aa9caaf6SPeter Wemm break; 1221aa9caaf6SPeter Wemm } 1222aa9caaf6SPeter Wemm } 12234b88c807SRodney W. Grimes } else { 12244b88c807SRodney W. Grimes pungetc(); 12254b88c807SRodney W. Grimes } 12264b88c807SRodney W. Grimes if (dblquote || arinest) 12274b88c807SRodney W. Grimes flags |= VSQUOTE; 12284b88c807SRodney W. Grimes *(stackblock() + typeloc) = subtype | flags; 12294b88c807SRodney W. Grimes if (subtype != VSNORMAL) 12304b88c807SRodney W. Grimes varnest++; 12314b88c807SRodney W. Grimes } 12324b88c807SRodney W. Grimes goto parsesub_return; 12334b88c807SRodney W. Grimes } 12344b88c807SRodney W. Grimes 12354b88c807SRodney W. Grimes 12364b88c807SRodney W. Grimes /* 12374b88c807SRodney W. Grimes * Called to parse command substitutions. Newstyle is set if the command 12384b88c807SRodney W. Grimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked 12394b88c807SRodney W. Grimes * list of commands (passed by reference), and savelen is the number of 12404b88c807SRodney W. Grimes * characters on the top of the stack which must be preserved. 12414b88c807SRodney W. Grimes */ 12424b88c807SRodney W. Grimes 12434b88c807SRodney W. Grimes parsebackq: { 12444b88c807SRodney W. Grimes struct nodelist **nlpp; 12454b88c807SRodney W. Grimes int savepbq; 12464b88c807SRodney W. Grimes union node *n; 12474b88c807SRodney W. Grimes char *volatile str; 12484b88c807SRodney W. Grimes struct jmploc jmploc; 12494b88c807SRodney W. Grimes struct jmploc *volatile savehandler; 12504b88c807SRodney W. Grimes int savelen; 12514b88c807SRodney W. Grimes 12524b88c807SRodney W. Grimes savepbq = parsebackquote; 12534b88c807SRodney W. Grimes if (setjmp(jmploc.loc)) { 12544b88c807SRodney W. Grimes if (str) 12554b88c807SRodney W. Grimes ckfree(str); 12564b88c807SRodney W. Grimes parsebackquote = 0; 12574b88c807SRodney W. Grimes handler = savehandler; 12584b88c807SRodney W. Grimes longjmp(handler->loc, 1); 12594b88c807SRodney W. Grimes } 12604b88c807SRodney W. Grimes INTOFF; 12614b88c807SRodney W. Grimes str = NULL; 12624b88c807SRodney W. Grimes savelen = out - stackblock(); 12634b88c807SRodney W. Grimes if (savelen > 0) { 12644b88c807SRodney W. Grimes str = ckmalloc(savelen); 1265aa9caaf6SPeter Wemm memcpy(str, stackblock(), savelen); 12664b88c807SRodney W. Grimes } 12674b88c807SRodney W. Grimes savehandler = handler; 12684b88c807SRodney W. Grimes handler = &jmploc; 12694b88c807SRodney W. Grimes INTON; 12704b88c807SRodney W. Grimes if (oldstyle) { 12714b88c807SRodney W. Grimes /* We must read until the closing backquote, giving special 12724b88c807SRodney W. Grimes treatment to some slashes, and then push the string and 12734b88c807SRodney W. Grimes reread it as input, interpreting it normally. */ 12744b88c807SRodney W. Grimes register char *out; 12754b88c807SRodney W. Grimes register c; 12764b88c807SRodney W. Grimes int savelen; 12774b88c807SRodney W. Grimes char *str; 12784b88c807SRodney W. Grimes 12794b88c807SRodney W. Grimes STARTSTACKSTR(out); 12804b88c807SRodney W. Grimes while ((c = pgetc ()) != '`') { 128172348d41SJoerg Wunsch if (c == PEOF) { 128272348d41SJoerg Wunsch startlinno = plinno; 128372348d41SJoerg Wunsch synerror("EOF in backquote substitution"); 128472348d41SJoerg Wunsch } 12854b88c807SRodney W. Grimes if (c == '\\') { 12864b88c807SRodney W. Grimes c = pgetc (); 12871e95454eSPaul Richards if (c != '\\' && c != '`' && c != '$' 12884b88c807SRodney W. Grimes && (!dblquote || c != '"')) 12894b88c807SRodney W. Grimes STPUTC('\\', out); 12904b88c807SRodney W. Grimes } 12914b88c807SRodney W. Grimes STPUTC(c, out); 12924b88c807SRodney W. Grimes } 12934b88c807SRodney W. Grimes STPUTC('\0', out); 12944b88c807SRodney W. Grimes savelen = out - stackblock(); 12954b88c807SRodney W. Grimes if (savelen > 0) { 12964b88c807SRodney W. Grimes str = ckmalloc(savelen); 1297aa9caaf6SPeter Wemm memcpy(str, stackblock(), savelen); 12984b88c807SRodney W. Grimes setinputstring(str, 1); 12994b88c807SRodney W. Grimes } 1300aa9caaf6SPeter Wemm } 13014b88c807SRodney W. Grimes nlpp = &bqlist; 13024b88c807SRodney W. Grimes while (*nlpp) 13034b88c807SRodney W. Grimes nlpp = &(*nlpp)->next; 13044b88c807SRodney W. Grimes *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 13054b88c807SRodney W. Grimes (*nlpp)->next = NULL; 13064b88c807SRodney W. Grimes parsebackquote = oldstyle; 13074b88c807SRodney W. Grimes n = list(0); 13084b88c807SRodney W. Grimes if (!oldstyle && (readtoken() != TRP)) 13094b88c807SRodney W. Grimes synexpect(TRP); 13104b88c807SRodney W. Grimes (*nlpp)->n = n; 13114b88c807SRodney W. Grimes /* Start reading from old file again. */ 13124b88c807SRodney W. Grimes if (oldstyle) 13134b88c807SRodney W. Grimes popfile(); 13144b88c807SRodney W. Grimes while (stackblocksize() <= savelen) 13154b88c807SRodney W. Grimes growstackblock(); 13164b88c807SRodney W. Grimes STARTSTACKSTR(out); 13174b88c807SRodney W. Grimes if (str) { 1318aa9caaf6SPeter Wemm memcpy(out, str, savelen); 13194b88c807SRodney W. Grimes STADJUST(savelen, out); 13204b88c807SRodney W. Grimes INTOFF; 13214b88c807SRodney W. Grimes ckfree(str); 13224b88c807SRodney W. Grimes str = NULL; 13234b88c807SRodney W. Grimes INTON; 13244b88c807SRodney W. Grimes } 13254b88c807SRodney W. Grimes parsebackquote = savepbq; 13264b88c807SRodney W. Grimes handler = savehandler; 13274b88c807SRodney W. Grimes if (arinest || dblquote) 13284b88c807SRodney W. Grimes USTPUTC(CTLBACKQ | CTLQUOTE, out); 13294b88c807SRodney W. Grimes else 13304b88c807SRodney W. Grimes USTPUTC(CTLBACKQ, out); 13314b88c807SRodney W. Grimes if (oldstyle) 13324b88c807SRodney W. Grimes goto parsebackq_oldreturn; 13334b88c807SRodney W. Grimes else 13344b88c807SRodney W. Grimes goto parsebackq_newreturn; 13354b88c807SRodney W. Grimes } 13364b88c807SRodney W. Grimes 13374b88c807SRodney W. Grimes /* 13384b88c807SRodney W. Grimes * Parse an arithmetic expansion (indicate start of one and set state) 13394b88c807SRodney W. Grimes */ 13404b88c807SRodney W. Grimes parsearith: { 13414b88c807SRodney W. Grimes 13424b88c807SRodney W. Grimes if (++arinest == 1) { 13434b88c807SRodney W. Grimes prevsyntax = syntax; 13444b88c807SRodney W. Grimes syntax = ARISYNTAX; 13454b88c807SRodney W. Grimes USTPUTC(CTLARI, out); 13464b88c807SRodney W. Grimes } else { 13474b88c807SRodney W. Grimes /* 13484b88c807SRodney W. Grimes * we collapse embedded arithmetic expansion to 13494b88c807SRodney W. Grimes * parenthesis, which should be equivalent 13504b88c807SRodney W. Grimes */ 13514b88c807SRodney W. Grimes USTPUTC('(', out); 13524b88c807SRodney W. Grimes } 13534b88c807SRodney W. Grimes goto parsearith_return; 13544b88c807SRodney W. Grimes } 13554b88c807SRodney W. Grimes 13564b88c807SRodney W. Grimes } /* end of readtoken */ 13574b88c807SRodney W. Grimes 13584b88c807SRodney W. Grimes 13594b88c807SRodney W. Grimes 13604b88c807SRodney W. Grimes #ifdef mkinit 13614b88c807SRodney W. Grimes RESET { 13624b88c807SRodney W. Grimes tokpushback = 0; 13634b88c807SRodney W. Grimes checkkwd = 0; 13644b88c807SRodney W. Grimes } 13654b88c807SRodney W. Grimes #endif 13664b88c807SRodney W. Grimes 13674b88c807SRodney W. Grimes /* 13684b88c807SRodney W. Grimes * Returns true if the text contains nothing to expand (no dollar signs 13694b88c807SRodney W. Grimes * or backquotes). 13704b88c807SRodney W. Grimes */ 13714b88c807SRodney W. Grimes 13724b88c807SRodney W. Grimes STATIC int 13734b88c807SRodney W. Grimes noexpand(text) 13744b88c807SRodney W. Grimes char *text; 13754b88c807SRodney W. Grimes { 13764b88c807SRodney W. Grimes register char *p; 13774b88c807SRodney W. Grimes register char c; 13784b88c807SRodney W. Grimes 13794b88c807SRodney W. Grimes p = text; 13804b88c807SRodney W. Grimes while ((c = *p++) != '\0') { 13814b88c807SRodney W. Grimes if (c == CTLESC) 13824b88c807SRodney W. Grimes p++; 13834b88c807SRodney W. Grimes else if (BASESYNTAX[c] == CCTL) 13844b88c807SRodney W. Grimes return 0; 13854b88c807SRodney W. Grimes } 13864b88c807SRodney W. Grimes return 1; 13874b88c807SRodney W. Grimes } 13884b88c807SRodney W. Grimes 13894b88c807SRodney W. Grimes 13904b88c807SRodney W. Grimes /* 13914b88c807SRodney W. Grimes * Return true if the argument is a legal variable name (a letter or 13924b88c807SRodney W. Grimes * underscore followed by zero or more letters, underscores, and digits). 13934b88c807SRodney W. Grimes */ 13944b88c807SRodney W. Grimes 13954b88c807SRodney W. Grimes int 13964b88c807SRodney W. Grimes goodname(name) 13974b88c807SRodney W. Grimes char *name; 13984b88c807SRodney W. Grimes { 13994b88c807SRodney W. Grimes register char *p; 14004b88c807SRodney W. Grimes 14014b88c807SRodney W. Grimes p = name; 14024b88c807SRodney W. Grimes if (! is_name(*p)) 14034b88c807SRodney W. Grimes return 0; 14044b88c807SRodney W. Grimes while (*++p) { 14054b88c807SRodney W. Grimes if (! is_in_name(*p)) 14064b88c807SRodney W. Grimes return 0; 14074b88c807SRodney W. Grimes } 14084b88c807SRodney W. Grimes return 1; 14094b88c807SRodney W. Grimes } 14104b88c807SRodney W. Grimes 14114b88c807SRodney W. Grimes 14124b88c807SRodney W. Grimes /* 14134b88c807SRodney W. Grimes * Called when an unexpected token is read during the parse. The argument 14144b88c807SRodney W. Grimes * is the token that is expected, or -1 if more than one type of token can 14154b88c807SRodney W. Grimes * occur at this point. 14164b88c807SRodney W. Grimes */ 14174b88c807SRodney W. Grimes 14184b88c807SRodney W. Grimes STATIC void 1419aa9caaf6SPeter Wemm synexpect(token) 1420aa9caaf6SPeter Wemm int token; 1421aa9caaf6SPeter Wemm { 14224b88c807SRodney W. Grimes char msg[64]; 14234b88c807SRodney W. Grimes 14244b88c807SRodney W. Grimes if (token >= 0) { 14254b88c807SRodney W. Grimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 14264b88c807SRodney W. Grimes tokname[lasttoken], tokname[token]); 14274b88c807SRodney W. Grimes } else { 14284b88c807SRodney W. Grimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 14294b88c807SRodney W. Grimes } 14304b88c807SRodney W. Grimes synerror(msg); 14314b88c807SRodney W. Grimes } 14324b88c807SRodney W. Grimes 14334b88c807SRodney W. Grimes 14344b88c807SRodney W. Grimes STATIC void 14354b88c807SRodney W. Grimes synerror(msg) 14364b88c807SRodney W. Grimes char *msg; 14374b88c807SRodney W. Grimes { 14384b88c807SRodney W. Grimes if (commandname) 14394b88c807SRodney W. Grimes outfmt(&errout, "%s: %d: ", commandname, startlinno); 14404b88c807SRodney W. Grimes outfmt(&errout, "Syntax error: %s\n", msg); 14414b88c807SRodney W. Grimes error((char *)NULL); 14424b88c807SRodney W. Grimes } 14434b88c807SRodney W. Grimes 14444b88c807SRodney W. Grimes STATIC void 14454b88c807SRodney W. Grimes setprompt(which) 14464b88c807SRodney W. Grimes int which; 14474b88c807SRodney W. Grimes { 14484b88c807SRodney W. Grimes whichprompt = which; 14494b88c807SRodney W. Grimes 1450aa9caaf6SPeter Wemm #ifndef NO_HISTORY 14514b88c807SRodney W. Grimes if (!el) 1452aa9caaf6SPeter Wemm #endif 14534b88c807SRodney W. Grimes out2str(getprompt(NULL)); 14544b88c807SRodney W. Grimes } 14554b88c807SRodney W. Grimes 14564b88c807SRodney W. Grimes /* 14574b88c807SRodney W. Grimes * called by editline -- any expansions to the prompt 14584b88c807SRodney W. Grimes * should be added here. 14594b88c807SRodney W. Grimes */ 14604b88c807SRodney W. Grimes char * 14614b88c807SRodney W. Grimes getprompt(unused) 14624b88c807SRodney W. Grimes void *unused; 14634b88c807SRodney W. Grimes { 14644b88c807SRodney W. Grimes switch (whichprompt) { 14654b88c807SRodney W. Grimes case 0: 14664b88c807SRodney W. Grimes return ""; 14674b88c807SRodney W. Grimes case 1: 14684b88c807SRodney W. Grimes return ps1val(); 14694b88c807SRodney W. Grimes case 2: 14704b88c807SRodney W. Grimes return ps2val(); 14714b88c807SRodney W. Grimes default: 14724b88c807SRodney W. Grimes return "<internal prompt error>"; 14734b88c807SRodney W. Grimes } 14744b88c807SRodney W. Grimes } 1475