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 * 36e7a0b024SSteve Price * $Id: parser.c,v 1.20 1997/04/28 03:22:09 steve Exp $ 374b88c807SRodney W. Grimes */ 384b88c807SRodney W. Grimes 394b88c807SRodney W. Grimes #ifndef lint 40ab0a2172SSteve Price static char const 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() { 2527920a31dSSteve Price union node *n1, *pipenode, *notnode; 2534b88c807SRodney W. Grimes struct nodelist *lp, *prev; 2547920a31dSSteve Price int negate = 0; 2554b88c807SRodney W. Grimes 2564b88c807SRodney W. Grimes TRACE(("pipeline: entered\n")); 2577920a31dSSteve Price while (readtoken() == TNOT) { 2587920a31dSSteve Price TRACE(("pipeline: TNOT recognized\n")); 2597920a31dSSteve Price negate = !negate; 2607920a31dSSteve Price } 2617920a31dSSteve Price 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++; 2807920a31dSSteve Price if (negate) { 2817920a31dSSteve Price notnode = (union node *)stalloc(sizeof(struct nnot)); 2827920a31dSSteve Price notnode->type = NNOT; 2837920a31dSSteve Price notnode->nnot.com = n1; 2847920a31dSSteve Price n1 = notnode; 2857920a31dSSteve Price } 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; 2977920a31dSSteve Price int t; 2984b88c807SRodney W. Grimes 2994b88c807SRodney W. Grimes checkkwd = 2; 300aa9caaf6SPeter Wemm redir = NULL; 301aa9caaf6SPeter Wemm n1 = NULL; 3024b88c807SRodney W. Grimes rpp = &redir; 303ab0a2172SSteve Price 3044b88c807SRodney W. Grimes /* Check for redirection which may precede command */ 3054b88c807SRodney W. Grimes while (readtoken() == TREDIR) { 3064b88c807SRodney W. Grimes *rpp = n2 = redirnode; 3074b88c807SRodney W. Grimes rpp = &n2->nfile.next; 3084b88c807SRodney W. Grimes parsefname(); 3094b88c807SRodney W. Grimes } 3104b88c807SRodney W. Grimes tokpushback++; 3114b88c807SRodney W. Grimes 3124b88c807SRodney W. Grimes switch (readtoken()) { 3134b88c807SRodney W. Grimes case TIF: 3144b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nif)); 3154b88c807SRodney W. Grimes n1->type = NIF; 3164b88c807SRodney W. Grimes n1->nif.test = list(0); 3174b88c807SRodney W. Grimes if (readtoken() != TTHEN) 3184b88c807SRodney W. Grimes synexpect(TTHEN); 3194b88c807SRodney W. Grimes n1->nif.ifpart = list(0); 3204b88c807SRodney W. Grimes n2 = n1; 3214b88c807SRodney W. Grimes while (readtoken() == TELIF) { 3224b88c807SRodney W. Grimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 3234b88c807SRodney W. Grimes n2 = n2->nif.elsepart; 3244b88c807SRodney W. Grimes n2->type = NIF; 3254b88c807SRodney W. Grimes n2->nif.test = list(0); 3264b88c807SRodney W. Grimes if (readtoken() != TTHEN) 3274b88c807SRodney W. Grimes synexpect(TTHEN); 3284b88c807SRodney W. Grimes n2->nif.ifpart = list(0); 3294b88c807SRodney W. Grimes } 3304b88c807SRodney W. Grimes if (lasttoken == TELSE) 3314b88c807SRodney W. Grimes n2->nif.elsepart = list(0); 3324b88c807SRodney W. Grimes else { 3334b88c807SRodney W. Grimes n2->nif.elsepart = NULL; 3344b88c807SRodney W. Grimes tokpushback++; 3354b88c807SRodney W. Grimes } 3364b88c807SRodney W. Grimes if (readtoken() != TFI) 3374b88c807SRodney W. Grimes synexpect(TFI); 3384b88c807SRodney W. Grimes checkkwd = 1; 3394b88c807SRodney W. Grimes break; 3404b88c807SRodney W. Grimes case TWHILE: 3414b88c807SRodney W. Grimes case TUNTIL: { 3424b88c807SRodney W. Grimes int got; 3434b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nbinary)); 3444b88c807SRodney W. Grimes n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 3454b88c807SRodney W. Grimes n1->nbinary.ch1 = list(0); 3464b88c807SRodney W. Grimes if ((got=readtoken()) != TDO) { 3474b88c807SRodney W. Grimes TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 3484b88c807SRodney W. Grimes synexpect(TDO); 3494b88c807SRodney W. Grimes } 3504b88c807SRodney W. Grimes n1->nbinary.ch2 = list(0); 3514b88c807SRodney W. Grimes if (readtoken() != TDONE) 3524b88c807SRodney W. Grimes synexpect(TDONE); 3534b88c807SRodney W. Grimes checkkwd = 1; 3544b88c807SRodney W. Grimes break; 3554b88c807SRodney W. Grimes } 3564b88c807SRodney W. Grimes case TFOR: 3574b88c807SRodney W. Grimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 3584b88c807SRodney W. Grimes synerror("Bad for loop variable"); 3594b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nfor)); 3604b88c807SRodney W. Grimes n1->type = NFOR; 3614b88c807SRodney W. Grimes n1->nfor.var = wordtext; 3624b88c807SRodney W. Grimes if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) { 3634b88c807SRodney W. Grimes app = ≈ 3644b88c807SRodney W. Grimes while (readtoken() == TWORD) { 3654b88c807SRodney W. Grimes n2 = (union node *)stalloc(sizeof (struct narg)); 3664b88c807SRodney W. Grimes n2->type = NARG; 3674b88c807SRodney W. Grimes n2->narg.text = wordtext; 3684b88c807SRodney W. Grimes n2->narg.backquote = backquotelist; 3694b88c807SRodney W. Grimes *app = n2; 3704b88c807SRodney W. Grimes app = &n2->narg.next; 3714b88c807SRodney W. Grimes } 3724b88c807SRodney W. Grimes *app = NULL; 3734b88c807SRodney W. Grimes n1->nfor.args = ap; 3744b88c807SRodney W. Grimes if (lasttoken != TNL && lasttoken != TSEMI) 3754b88c807SRodney W. Grimes synexpect(-1); 3764b88c807SRodney W. Grimes } else { 3774b88c807SRodney W. Grimes #ifndef GDB_HACK 3784b88c807SRodney W. Grimes static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, 3794b88c807SRodney W. Grimes '@', '=', '\0'}; 3804b88c807SRodney W. Grimes #endif 3814b88c807SRodney W. Grimes n2 = (union node *)stalloc(sizeof (struct narg)); 3824b88c807SRodney W. Grimes n2->type = NARG; 3834b88c807SRodney W. Grimes n2->narg.text = (char *)argvars; 3844b88c807SRodney W. Grimes n2->narg.backquote = NULL; 3854b88c807SRodney W. Grimes n2->narg.next = NULL; 3864b88c807SRodney W. Grimes n1->nfor.args = n2; 3874b88c807SRodney W. Grimes /* 3884b88c807SRodney W. Grimes * Newline or semicolon here is optional (but note 3894b88c807SRodney W. Grimes * that the original Bourne shell only allowed NL). 3904b88c807SRodney W. Grimes */ 3914b88c807SRodney W. Grimes if (lasttoken != TNL && lasttoken != TSEMI) 3924b88c807SRodney W. Grimes tokpushback++; 3934b88c807SRodney W. Grimes } 3944b88c807SRodney W. Grimes checkkwd = 2; 3954b88c807SRodney W. Grimes if ((t = readtoken()) == TDO) 3964b88c807SRodney W. Grimes t = TDONE; 3974b88c807SRodney W. Grimes else if (t == TBEGIN) 3984b88c807SRodney W. Grimes t = TEND; 3994b88c807SRodney W. Grimes else 4004b88c807SRodney W. Grimes synexpect(-1); 4014b88c807SRodney W. Grimes n1->nfor.body = list(0); 4024b88c807SRodney W. Grimes if (readtoken() != t) 4034b88c807SRodney W. Grimes synexpect(t); 4044b88c807SRodney W. Grimes checkkwd = 1; 4054b88c807SRodney W. Grimes break; 4064b88c807SRodney W. Grimes case TCASE: 4074b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct ncase)); 4084b88c807SRodney W. Grimes n1->type = NCASE; 4094b88c807SRodney W. Grimes if (readtoken() != TWORD) 4104b88c807SRodney W. Grimes synexpect(TWORD); 4114b88c807SRodney W. Grimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 4124b88c807SRodney W. Grimes n2->type = NARG; 4134b88c807SRodney W. Grimes n2->narg.text = wordtext; 4144b88c807SRodney W. Grimes n2->narg.backquote = backquotelist; 4154b88c807SRodney W. Grimes n2->narg.next = NULL; 4164b88c807SRodney W. Grimes while (readtoken() == TNL); 4174b88c807SRodney W. Grimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 4184b88c807SRodney W. Grimes synerror("expecting \"in\""); 4194b88c807SRodney W. Grimes cpp = &n1->ncase.cases; 4204417f629SPeter Wemm noaliases = 1; /* turn off alias expansion */ 421650488feSSean Eric Fagan checkkwd = 2, readtoken(); 422650488feSSean Eric Fagan do { 4234b88c807SRodney W. Grimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 4244b88c807SRodney W. Grimes cp->type = NCLIST; 4254b88c807SRodney W. Grimes app = &cp->nclist.pattern; 4264b88c807SRodney W. Grimes for (;;) { 4274b88c807SRodney W. Grimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 4284b88c807SRodney W. Grimes ap->type = NARG; 4294b88c807SRodney W. Grimes ap->narg.text = wordtext; 4304b88c807SRodney W. Grimes ap->narg.backquote = backquotelist; 431650488feSSean Eric Fagan if (checkkwd = 2, readtoken() != TPIPE) 4324b88c807SRodney W. Grimes break; 4334b88c807SRodney W. Grimes app = &ap->narg.next; 434650488feSSean Eric Fagan readtoken(); 4354b88c807SRodney W. Grimes } 4364b88c807SRodney W. Grimes ap->narg.next = NULL; 4374b88c807SRodney W. Grimes if (lasttoken != TRP) 4384417f629SPeter Wemm noaliases = 0, synexpect(TRP); 4394b88c807SRodney W. Grimes cp->nclist.body = list(0); 440650488feSSean Eric Fagan 441650488feSSean Eric Fagan checkkwd = 2; 442650488feSSean Eric Fagan if ((t = readtoken()) != TESAC) { 443650488feSSean Eric Fagan if (t != TENDCASE) 4444417f629SPeter Wemm noaliases = 0, synexpect(TENDCASE); 445650488feSSean Eric Fagan else 446650488feSSean Eric Fagan checkkwd = 2, readtoken(); 4474b88c807SRodney W. Grimes } 448650488feSSean Eric Fagan cpp = &cp->nclist.next; 449650488feSSean Eric Fagan } while(lasttoken != TESAC); 4504417f629SPeter Wemm noaliases = 0; /* reset alias expansion */ 4514b88c807SRodney W. Grimes *cpp = NULL; 4524b88c807SRodney W. Grimes checkkwd = 1; 4534b88c807SRodney W. Grimes break; 4544b88c807SRodney W. Grimes case TLP: 4554b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nredir)); 4564b88c807SRodney W. Grimes n1->type = NSUBSHELL; 4574b88c807SRodney W. Grimes n1->nredir.n = list(0); 4584b88c807SRodney W. Grimes n1->nredir.redirect = NULL; 4594b88c807SRodney W. Grimes if (readtoken() != TRP) 4604b88c807SRodney W. Grimes synexpect(TRP); 4614b88c807SRodney W. Grimes checkkwd = 1; 4624b88c807SRodney W. Grimes break; 4634b88c807SRodney W. Grimes case TBEGIN: 4644b88c807SRodney W. Grimes n1 = list(0); 4654b88c807SRodney W. Grimes if (readtoken() != TEND) 4664b88c807SRodney W. Grimes synexpect(TEND); 4674b88c807SRodney W. Grimes checkkwd = 1; 4684b88c807SRodney W. Grimes break; 4694b88c807SRodney W. Grimes /* Handle an empty command like other simple commands. */ 470248ffae5SJoerg Wunsch case TSEMI: 471aa9caaf6SPeter Wemm /* 472aa9caaf6SPeter Wemm * An empty command before a ; doesn't make much sense, and 473aa9caaf6SPeter Wemm * should certainly be disallowed in the case of `if ;'. 474aa9caaf6SPeter Wemm */ 475aa9caaf6SPeter Wemm if (!redir) 476aa9caaf6SPeter Wemm synexpect(-1); 477ab0a2172SSteve Price case TAND: 478ab0a2172SSteve Price case TOR: 479aa9caaf6SPeter Wemm case TNL: 480248ffae5SJoerg Wunsch case TEOF: 4814b88c807SRodney W. Grimes case TWORD: 482aa9caaf6SPeter Wemm case TRP: 4834b88c807SRodney W. Grimes tokpushback++; 4847920a31dSSteve Price return simplecmd(rpp, redir); 4854b88c807SRodney W. Grimes default: 4864b88c807SRodney W. Grimes synexpect(-1); 4874b88c807SRodney W. Grimes } 4884b88c807SRodney W. Grimes 4894b88c807SRodney W. Grimes /* Now check for redirection which may follow command */ 4904b88c807SRodney W. Grimes while (readtoken() == TREDIR) { 4914b88c807SRodney W. Grimes *rpp = n2 = redirnode; 4924b88c807SRodney W. Grimes rpp = &n2->nfile.next; 4934b88c807SRodney W. Grimes parsefname(); 4944b88c807SRodney W. Grimes } 4954b88c807SRodney W. Grimes tokpushback++; 4964b88c807SRodney W. Grimes *rpp = NULL; 4974b88c807SRodney W. Grimes if (redir) { 4984b88c807SRodney W. Grimes if (n1->type != NSUBSHELL) { 4994b88c807SRodney W. Grimes n2 = (union node *)stalloc(sizeof (struct nredir)); 5004b88c807SRodney W. Grimes n2->type = NREDIR; 5014b88c807SRodney W. Grimes n2->nredir.n = n1; 5024b88c807SRodney W. Grimes n1 = n2; 5034b88c807SRodney W. Grimes } 5044b88c807SRodney W. Grimes n1->nredir.redirect = redir; 5054b88c807SRodney W. Grimes } 5064b88c807SRodney W. Grimes return n1; 5074b88c807SRodney W. Grimes } 5084b88c807SRodney W. Grimes 5094b88c807SRodney W. Grimes 5104b88c807SRodney W. Grimes STATIC union node * 5114b88c807SRodney W. Grimes simplecmd(rpp, redir) 5124b88c807SRodney W. Grimes union node **rpp, *redir; 5134b88c807SRodney W. Grimes { 5144b88c807SRodney W. Grimes union node *args, **app; 5154b88c807SRodney W. Grimes union node **orig_rpp = rpp; 5167920a31dSSteve Price union node *n = NULL; 5174b88c807SRodney W. Grimes 5184b88c807SRodney W. Grimes /* If we don't have any redirections already, then we must reset */ 5194b88c807SRodney W. Grimes /* rpp to be the address of the local redir variable. */ 5204b88c807SRodney W. Grimes if (redir == 0) 5214b88c807SRodney W. Grimes rpp = &redir; 5224b88c807SRodney W. Grimes 5234b88c807SRodney W. Grimes args = NULL; 5244b88c807SRodney W. Grimes app = &args; 5254b88c807SRodney W. Grimes /* 5264b88c807SRodney W. Grimes * We save the incoming value, because we need this for shell 5274b88c807SRodney W. Grimes * functions. There can not be a redirect or an argument between 5284b88c807SRodney W. Grimes * the function name and the open parenthesis. 5294b88c807SRodney W. Grimes */ 5304b88c807SRodney W. Grimes orig_rpp = rpp; 5314b88c807SRodney W. Grimes 5324b88c807SRodney W. Grimes for (;;) { 5334b88c807SRodney W. Grimes if (readtoken() == TWORD) { 5344b88c807SRodney W. Grimes n = (union node *)stalloc(sizeof (struct narg)); 5354b88c807SRodney W. Grimes n->type = NARG; 5364b88c807SRodney W. Grimes n->narg.text = wordtext; 5374b88c807SRodney W. Grimes n->narg.backquote = backquotelist; 5384b88c807SRodney W. Grimes *app = n; 5394b88c807SRodney W. Grimes app = &n->narg.next; 5404b88c807SRodney W. Grimes } else if (lasttoken == TREDIR) { 5414b88c807SRodney W. Grimes *rpp = n = redirnode; 5424b88c807SRodney W. Grimes rpp = &n->nfile.next; 5434b88c807SRodney W. Grimes parsefname(); /* read name of redirection file */ 5444b88c807SRodney W. Grimes } else if (lasttoken == TLP && app == &args->narg.next 5454b88c807SRodney W. Grimes && rpp == orig_rpp) { 5464b88c807SRodney W. Grimes /* We have a function */ 5474b88c807SRodney W. Grimes if (readtoken() != TRP) 5484b88c807SRodney W. Grimes synexpect(TRP); 5494b88c807SRodney W. Grimes #ifdef notdef 5504b88c807SRodney W. Grimes if (! goodname(n->narg.text)) 5514b88c807SRodney W. Grimes synerror("Bad function name"); 5524b88c807SRodney W. Grimes #endif 5534b88c807SRodney W. Grimes n->type = NDEFUN; 5544b88c807SRodney W. Grimes n->narg.next = command(); 5557920a31dSSteve Price return n; 5564b88c807SRodney W. Grimes } else { 5574b88c807SRodney W. Grimes tokpushback++; 5584b88c807SRodney W. Grimes break; 5594b88c807SRodney W. Grimes } 5604b88c807SRodney W. Grimes } 5614b88c807SRodney W. Grimes *app = NULL; 5624b88c807SRodney W. Grimes *rpp = NULL; 5634b88c807SRodney W. Grimes n = (union node *)stalloc(sizeof (struct ncmd)); 5644b88c807SRodney W. Grimes n->type = NCMD; 5654b88c807SRodney W. Grimes n->ncmd.backgnd = 0; 5664b88c807SRodney W. Grimes n->ncmd.args = args; 5674b88c807SRodney W. Grimes n->ncmd.redirect = redir; 5684b88c807SRodney W. Grimes return n; 5694b88c807SRodney W. Grimes } 5704b88c807SRodney W. Grimes 571aa9caaf6SPeter Wemm STATIC union node * 572aa9caaf6SPeter Wemm makename() { 573aa9caaf6SPeter Wemm union node *n; 574aa9caaf6SPeter Wemm 575aa9caaf6SPeter Wemm n = (union node *)stalloc(sizeof (struct narg)); 576aa9caaf6SPeter Wemm n->type = NARG; 577aa9caaf6SPeter Wemm n->narg.next = NULL; 578aa9caaf6SPeter Wemm n->narg.text = wordtext; 579aa9caaf6SPeter Wemm n->narg.backquote = backquotelist; 580aa9caaf6SPeter Wemm return n; 581aa9caaf6SPeter Wemm } 582aa9caaf6SPeter Wemm 583aa9caaf6SPeter Wemm void fixredir(n, text, err) 584aa9caaf6SPeter Wemm union node *n; 585aa9caaf6SPeter Wemm const char *text; 586aa9caaf6SPeter Wemm int err; 587aa9caaf6SPeter Wemm { 588aa9caaf6SPeter Wemm TRACE(("Fix redir %s %d\n", text, err)); 589aa9caaf6SPeter Wemm if (!err) 590aa9caaf6SPeter Wemm n->ndup.vname = NULL; 591aa9caaf6SPeter Wemm 592aa9caaf6SPeter Wemm if (is_digit(text[0]) && text[1] == '\0') 593aa9caaf6SPeter Wemm n->ndup.dupfd = digit_val(text[0]); 594aa9caaf6SPeter Wemm else if (text[0] == '-' && text[1] == '\0') 595aa9caaf6SPeter Wemm n->ndup.dupfd = -1; 596aa9caaf6SPeter Wemm else { 597aa9caaf6SPeter Wemm 598aa9caaf6SPeter Wemm if (err) 599aa9caaf6SPeter Wemm synerror("Bad fd number"); 600aa9caaf6SPeter Wemm else 601aa9caaf6SPeter Wemm n->ndup.vname = makename(); 602aa9caaf6SPeter Wemm } 603aa9caaf6SPeter Wemm } 604aa9caaf6SPeter Wemm 6054b88c807SRodney W. Grimes 6064b88c807SRodney W. Grimes STATIC void 6074b88c807SRodney W. Grimes parsefname() { 6084b88c807SRodney W. Grimes union node *n = redirnode; 6094b88c807SRodney W. Grimes 6104b88c807SRodney W. Grimes if (readtoken() != TWORD) 6114b88c807SRodney W. Grimes synexpect(-1); 6124b88c807SRodney W. Grimes if (n->type == NHERE) { 6134b88c807SRodney W. Grimes struct heredoc *here = heredoc; 6144b88c807SRodney W. Grimes struct heredoc *p; 6154b88c807SRodney W. Grimes int i; 6164b88c807SRodney W. Grimes 6174b88c807SRodney W. Grimes if (quoteflag == 0) 6184b88c807SRodney W. Grimes n->type = NXHERE; 6194b88c807SRodney W. Grimes TRACE(("Here document %d\n", n->type)); 6204b88c807SRodney W. Grimes if (here->striptabs) { 6214b88c807SRodney W. Grimes while (*wordtext == '\t') 6224b88c807SRodney W. Grimes wordtext++; 6234b88c807SRodney W. Grimes } 6244b88c807SRodney W. Grimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 6254b88c807SRodney W. Grimes synerror("Illegal eof marker for << redirection"); 6264b88c807SRodney W. Grimes rmescapes(wordtext); 6274b88c807SRodney W. Grimes here->eofmark = wordtext; 6284b88c807SRodney W. Grimes here->next = NULL; 6294b88c807SRodney W. Grimes if (heredoclist == NULL) 6304b88c807SRodney W. Grimes heredoclist = here; 6314b88c807SRodney W. Grimes else { 6324b88c807SRodney W. Grimes for (p = heredoclist ; p->next ; p = p->next); 6334b88c807SRodney W. Grimes p->next = here; 6344b88c807SRodney W. Grimes } 6354b88c807SRodney W. Grimes } else if (n->type == NTOFD || n->type == NFROMFD) { 636aa9caaf6SPeter Wemm fixredir(n, wordtext, 0); 6374b88c807SRodney W. Grimes } else { 638aa9caaf6SPeter Wemm n->nfile.fname = makename(); 6394b88c807SRodney W. Grimes } 6404b88c807SRodney W. Grimes } 6414b88c807SRodney W. Grimes 6424b88c807SRodney W. Grimes 6434b88c807SRodney W. Grimes /* 6444b88c807SRodney W. Grimes * Input any here documents. 6454b88c807SRodney W. Grimes */ 6464b88c807SRodney W. Grimes 6474b88c807SRodney W. Grimes STATIC void 6484b88c807SRodney W. Grimes parseheredoc() { 6494b88c807SRodney W. Grimes struct heredoc *here; 6504b88c807SRodney W. Grimes union node *n; 6514b88c807SRodney W. Grimes 6524b88c807SRodney W. Grimes while (heredoclist) { 6534b88c807SRodney W. Grimes here = heredoclist; 6544b88c807SRodney W. Grimes heredoclist = here->next; 6554b88c807SRodney W. Grimes if (needprompt) { 6564b88c807SRodney W. Grimes setprompt(2); 6574b88c807SRodney W. Grimes needprompt = 0; 6584b88c807SRodney W. Grimes } 6594b88c807SRodney W. Grimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 6604b88c807SRodney W. Grimes here->eofmark, here->striptabs); 6614b88c807SRodney W. Grimes n = (union node *)stalloc(sizeof (struct narg)); 6624b88c807SRodney W. Grimes n->narg.type = NARG; 6634b88c807SRodney W. Grimes n->narg.next = NULL; 6644b88c807SRodney W. Grimes n->narg.text = wordtext; 6654b88c807SRodney W. Grimes n->narg.backquote = backquotelist; 6664b88c807SRodney W. Grimes here->here->nhere.doc = n; 6674b88c807SRodney W. Grimes } 6684b88c807SRodney W. Grimes } 6694b88c807SRodney W. Grimes 6704b88c807SRodney W. Grimes STATIC int 6714b88c807SRodney W. Grimes peektoken() { 6724b88c807SRodney W. Grimes int t; 6734b88c807SRodney W. Grimes 6744b88c807SRodney W. Grimes t = readtoken(); 6754b88c807SRodney W. Grimes tokpushback++; 6764b88c807SRodney W. Grimes return (t); 6774b88c807SRodney W. Grimes } 6784b88c807SRodney W. Grimes 6794b88c807SRodney W. Grimes STATIC int xxreadtoken(); 6804b88c807SRodney W. Grimes 6814b88c807SRodney W. Grimes STATIC int 6824b88c807SRodney W. Grimes readtoken() { 6834b88c807SRodney W. Grimes int t; 6844b88c807SRodney W. Grimes int savecheckkwd = checkkwd; 6854b88c807SRodney W. Grimes struct alias *ap; 6864b88c807SRodney W. Grimes #ifdef DEBUG 6874b88c807SRodney W. Grimes int alreadyseen = tokpushback; 6884b88c807SRodney W. Grimes #endif 6894b88c807SRodney W. Grimes 6904b88c807SRodney W. Grimes top: 6914b88c807SRodney W. Grimes t = xxreadtoken(); 6924b88c807SRodney W. Grimes 6934b88c807SRodney W. Grimes if (checkkwd) { 6944b88c807SRodney W. Grimes /* 6954b88c807SRodney W. Grimes * eat newlines 6964b88c807SRodney W. Grimes */ 6974b88c807SRodney W. Grimes if (checkkwd == 2) { 6984b88c807SRodney W. Grimes checkkwd = 0; 6994b88c807SRodney W. Grimes while (t == TNL) { 7004b88c807SRodney W. Grimes parseheredoc(); 7014b88c807SRodney W. Grimes t = xxreadtoken(); 7024b88c807SRodney W. Grimes } 7034b88c807SRodney W. Grimes } else 7044b88c807SRodney W. Grimes checkkwd = 0; 7054b88c807SRodney W. Grimes /* 7064b88c807SRodney W. Grimes * check for keywords and aliases 7074b88c807SRodney W. Grimes */ 708aa9caaf6SPeter Wemm if (t == TWORD && !quoteflag) 709aa9caaf6SPeter Wemm { 7107920a31dSSteve Price char * const *pp; 7114b88c807SRodney W. Grimes 7124b88c807SRodney W. Grimes for (pp = (char **)parsekwd; *pp; pp++) { 713aa9caaf6SPeter Wemm if (**pp == *wordtext && equal(*pp, wordtext)) 714aa9caaf6SPeter Wemm { 7154b88c807SRodney W. Grimes lasttoken = t = pp - parsekwd + KWDOFFSET; 7164b88c807SRodney W. Grimes TRACE(("keyword %s recognized\n", tokname[t])); 7174b88c807SRodney W. Grimes goto out; 7184b88c807SRodney W. Grimes } 7194b88c807SRodney W. Grimes } 7204417f629SPeter Wemm if (noaliases == 0 && 7214417f629SPeter Wemm (ap = lookupalias(wordtext, 1)) != NULL) { 7224b88c807SRodney W. Grimes pushstring(ap->val, strlen(ap->val), ap); 7234b88c807SRodney W. Grimes checkkwd = savecheckkwd; 7244b88c807SRodney W. Grimes goto top; 7254b88c807SRodney W. Grimes } 7264b88c807SRodney W. Grimes } 7274b88c807SRodney W. Grimes out: 7287920a31dSSteve Price checkkwd = 0; 7294b88c807SRodney W. Grimes } 7304b88c807SRodney W. Grimes #ifdef DEBUG 7314b88c807SRodney W. Grimes if (!alreadyseen) 7324b88c807SRodney W. Grimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7334b88c807SRodney W. Grimes else 7344b88c807SRodney W. Grimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7354b88c807SRodney W. Grimes #endif 7364b88c807SRodney W. Grimes return (t); 7374b88c807SRodney W. Grimes } 7384b88c807SRodney W. Grimes 7394b88c807SRodney W. Grimes 7404b88c807SRodney W. Grimes /* 7414b88c807SRodney W. Grimes * Read the next input token. 7424b88c807SRodney W. Grimes * If the token is a word, we set backquotelist to the list of cmds in 7434b88c807SRodney W. Grimes * backquotes. We set quoteflag to true if any part of the word was 7444b88c807SRodney W. Grimes * quoted. 7454b88c807SRodney W. Grimes * If the token is TREDIR, then we set redirnode to a structure containing 7464b88c807SRodney W. Grimes * the redirection. 7474b88c807SRodney W. Grimes * In all cases, the variable startlinno is set to the number of the line 7484b88c807SRodney W. Grimes * on which the token starts. 7494b88c807SRodney W. Grimes * 7504b88c807SRodney W. Grimes * [Change comment: here documents and internal procedures] 7514b88c807SRodney W. Grimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 7524b88c807SRodney W. Grimes * word parsing code into a separate routine. In this case, readtoken 7534b88c807SRodney W. Grimes * doesn't need to have any internal procedures, but parseword does. 7544b88c807SRodney W. Grimes * We could also make parseoperator in essence the main routine, and 7554b88c807SRodney W. Grimes * have parseword (readtoken1?) handle both words and redirection.] 7564b88c807SRodney W. Grimes */ 7574b88c807SRodney W. Grimes 7584b88c807SRodney W. Grimes #define RETURN(token) return lasttoken = token 7594b88c807SRodney W. Grimes 7604b88c807SRodney W. Grimes STATIC int 7614b88c807SRodney W. Grimes xxreadtoken() { 7627920a31dSSteve Price int c; 7634b88c807SRodney W. Grimes 7644b88c807SRodney W. Grimes if (tokpushback) { 7654b88c807SRodney W. Grimes tokpushback = 0; 7664b88c807SRodney W. Grimes return lasttoken; 7674b88c807SRodney W. Grimes } 7684b88c807SRodney W. Grimes if (needprompt) { 7694b88c807SRodney W. Grimes setprompt(2); 7704b88c807SRodney W. Grimes needprompt = 0; 7714b88c807SRodney W. Grimes } 7724b88c807SRodney W. Grimes startlinno = plinno; 7734b88c807SRodney W. Grimes for (;;) { /* until token or start of word found */ 7744b88c807SRodney W. Grimes c = pgetc_macro(); 7754b88c807SRodney W. Grimes if (c == ' ' || c == '\t') 7764b88c807SRodney W. Grimes continue; /* quick check for white space first */ 7774b88c807SRodney W. Grimes switch (c) { 7784b88c807SRodney W. Grimes case ' ': case '\t': 7794b88c807SRodney W. Grimes continue; 7804b88c807SRodney W. Grimes case '#': 7814b88c807SRodney W. Grimes while ((c = pgetc()) != '\n' && c != PEOF); 7824b88c807SRodney W. Grimes pungetc(); 7834b88c807SRodney W. Grimes continue; 7844b88c807SRodney W. Grimes case '\\': 7854b88c807SRodney W. Grimes if (pgetc() == '\n') { 7864b88c807SRodney W. Grimes startlinno = ++plinno; 7874b88c807SRodney W. Grimes if (doprompt) 7884b88c807SRodney W. Grimes setprompt(2); 7894b88c807SRodney W. Grimes else 7904b88c807SRodney W. Grimes setprompt(0); 7914b88c807SRodney W. Grimes continue; 7924b88c807SRodney W. Grimes } 7934b88c807SRodney W. Grimes pungetc(); 7944b88c807SRodney W. Grimes goto breakloop; 7954b88c807SRodney W. Grimes case '\n': 7964b88c807SRodney W. Grimes plinno++; 7974b88c807SRodney W. Grimes needprompt = doprompt; 7984b88c807SRodney W. Grimes RETURN(TNL); 7994b88c807SRodney W. Grimes case PEOF: 8004b88c807SRodney W. Grimes RETURN(TEOF); 8014b88c807SRodney W. Grimes case '&': 8024b88c807SRodney W. Grimes if (pgetc() == '&') 8034b88c807SRodney W. Grimes RETURN(TAND); 8044b88c807SRodney W. Grimes pungetc(); 8054b88c807SRodney W. Grimes RETURN(TBACKGND); 8064b88c807SRodney W. Grimes case '|': 8074b88c807SRodney W. Grimes if (pgetc() == '|') 8084b88c807SRodney W. Grimes RETURN(TOR); 8094b88c807SRodney W. Grimes pungetc(); 8104b88c807SRodney W. Grimes RETURN(TPIPE); 8114b88c807SRodney W. Grimes case ';': 8124b88c807SRodney W. Grimes if (pgetc() == ';') 8134b88c807SRodney W. Grimes RETURN(TENDCASE); 8144b88c807SRodney W. Grimes pungetc(); 8154b88c807SRodney W. Grimes RETURN(TSEMI); 8164b88c807SRodney W. Grimes case '(': 8174b88c807SRodney W. Grimes RETURN(TLP); 8184b88c807SRodney W. Grimes case ')': 8194b88c807SRodney W. Grimes RETURN(TRP); 8204b88c807SRodney W. Grimes default: 8214b88c807SRodney W. Grimes goto breakloop; 8224b88c807SRodney W. Grimes } 8234b88c807SRodney W. Grimes } 8244b88c807SRodney W. Grimes breakloop: 8254b88c807SRodney W. Grimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 8264b88c807SRodney W. Grimes #undef RETURN 8274b88c807SRodney W. Grimes } 8284b88c807SRodney W. Grimes 8294b88c807SRodney W. Grimes 8304b88c807SRodney W. Grimes 8314b88c807SRodney W. Grimes /* 8324b88c807SRodney W. Grimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 8334b88c807SRodney W. Grimes * is not NULL, read a here document. In the latter case, eofmark is the 8344b88c807SRodney W. Grimes * word which marks the end of the document and striptabs is true if 8354b88c807SRodney W. Grimes * leading tabs should be stripped from the document. The argument firstc 8364b88c807SRodney W. Grimes * is the first character of the input token or document. 8374b88c807SRodney W. Grimes * 8384b88c807SRodney W. Grimes * Because C does not have internal subroutines, I have simulated them 8394b88c807SRodney W. Grimes * using goto's to implement the subroutine linkage. The following macros 8404b88c807SRodney W. Grimes * will run code that appears at the end of readtoken1. 8414b88c807SRodney W. Grimes */ 8424b88c807SRodney W. Grimes 8434b88c807SRodney W. Grimes #define CHECKEND() {goto checkend; checkend_return:;} 8444b88c807SRodney W. Grimes #define PARSEREDIR() {goto parseredir; parseredir_return:;} 8454b88c807SRodney W. Grimes #define PARSESUB() {goto parsesub; parsesub_return:;} 8464b88c807SRodney W. Grimes #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 8474b88c807SRodney W. Grimes #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 8484b88c807SRodney W. Grimes #define PARSEARITH() {goto parsearith; parsearith_return:;} 8494b88c807SRodney W. Grimes 8504b88c807SRodney W. Grimes STATIC int 8514b88c807SRodney W. Grimes readtoken1(firstc, syntax, eofmark, striptabs) 8524b88c807SRodney W. Grimes int firstc; 8534b88c807SRodney W. Grimes char const *syntax; 8544b88c807SRodney W. Grimes char *eofmark; 8554b88c807SRodney W. Grimes int striptabs; 8564b88c807SRodney W. Grimes { 857aa9caaf6SPeter Wemm int c = firstc; 858aa9caaf6SPeter Wemm char *out; 8594b88c807SRodney W. Grimes int len; 8604b88c807SRodney W. Grimes char line[EOFMARKLEN + 1]; 8614b88c807SRodney W. Grimes struct nodelist *bqlist; 8624b88c807SRodney W. Grimes int quotef; 8634b88c807SRodney W. Grimes int dblquote; 8644b88c807SRodney W. Grimes int varnest; /* levels of variables expansion */ 8654b88c807SRodney W. Grimes int arinest; /* levels of arithmetic expansion */ 8664b88c807SRodney W. Grimes int parenlevel; /* levels of parens in arithmetic */ 8674b88c807SRodney W. Grimes int oldstyle; 8684b88c807SRodney W. Grimes char const *prevsyntax; /* syntax before arithmetic */ 869aa9caaf6SPeter Wemm #if __GNUC__ 870aa9caaf6SPeter Wemm /* Avoid longjmp clobbering */ 871aa9caaf6SPeter Wemm (void) &out; 872aa9caaf6SPeter Wemm (void) "ef; 873aa9caaf6SPeter Wemm (void) &dblquote; 874aa9caaf6SPeter Wemm (void) &varnest; 875aa9caaf6SPeter Wemm (void) &arinest; 876aa9caaf6SPeter Wemm (void) &parenlevel; 877aa9caaf6SPeter Wemm (void) &oldstyle; 878aa9caaf6SPeter Wemm (void) &prevsyntax; 879aa9caaf6SPeter Wemm (void) &syntax; 880aa9caaf6SPeter Wemm #endif 8814b88c807SRodney W. Grimes 8824b88c807SRodney W. Grimes startlinno = plinno; 8834b88c807SRodney W. Grimes dblquote = 0; 8844b88c807SRodney W. Grimes if (syntax == DQSYNTAX) 8854b88c807SRodney W. Grimes dblquote = 1; 8864b88c807SRodney W. Grimes quotef = 0; 8874b88c807SRodney W. Grimes bqlist = NULL; 8884b88c807SRodney W. Grimes varnest = 0; 8894b88c807SRodney W. Grimes arinest = 0; 8904b88c807SRodney W. Grimes parenlevel = 0; 8914b88c807SRodney W. Grimes 8924b88c807SRodney W. Grimes STARTSTACKSTR(out); 8934b88c807SRodney W. Grimes loop: { /* for each line, until end of word */ 8944b88c807SRodney W. Grimes #if ATTY 8954b88c807SRodney W. Grimes if (c == '\034' && doprompt 8964b88c807SRodney W. Grimes && attyset() && ! equal(termval(), "emacs")) { 8974b88c807SRodney W. Grimes attyline(); 8984b88c807SRodney W. Grimes if (syntax == BASESYNTAX) 8994b88c807SRodney W. Grimes return readtoken(); 9004b88c807SRodney W. Grimes c = pgetc(); 9014b88c807SRodney W. Grimes goto loop; 9024b88c807SRodney W. Grimes } 9034b88c807SRodney W. Grimes #endif 9044b88c807SRodney W. Grimes CHECKEND(); /* set c to PEOF if at end of here document */ 9054b88c807SRodney W. Grimes for (;;) { /* until end of line or end of word */ 9064b88c807SRodney W. Grimes CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 9074b88c807SRodney W. Grimes switch(syntax[c]) { 9084b88c807SRodney W. Grimes case CNL: /* '\n' */ 9094b88c807SRodney W. Grimes if (syntax == BASESYNTAX) 9104b88c807SRodney W. Grimes goto endword; /* exit outer loop */ 9114b88c807SRodney W. Grimes USTPUTC(c, out); 9124b88c807SRodney W. Grimes plinno++; 9134b88c807SRodney W. Grimes if (doprompt) 9144b88c807SRodney W. Grimes setprompt(2); 9154b88c807SRodney W. Grimes else 9164b88c807SRodney W. Grimes setprompt(0); 9174b88c807SRodney W. Grimes c = pgetc(); 9184b88c807SRodney W. Grimes goto loop; /* continue outer loop */ 9194b88c807SRodney W. Grimes case CWORD: 9204b88c807SRodney W. Grimes USTPUTC(c, out); 9214b88c807SRodney W. Grimes break; 9224b88c807SRodney W. Grimes case CCTL: 9234b88c807SRodney W. Grimes if (eofmark == NULL || dblquote) 9244b88c807SRodney W. Grimes USTPUTC(CTLESC, out); 9254b88c807SRodney W. Grimes USTPUTC(c, out); 9264b88c807SRodney W. Grimes break; 9274b88c807SRodney W. Grimes case CBACK: /* backslash */ 9284b88c807SRodney W. Grimes c = pgetc(); 9294b88c807SRodney W. Grimes if (c == PEOF) { 9304b88c807SRodney W. Grimes USTPUTC('\\', out); 9314b88c807SRodney W. Grimes pungetc(); 9324b88c807SRodney W. Grimes } else if (c == '\n') { 9334b88c807SRodney W. Grimes if (doprompt) 9344b88c807SRodney W. Grimes setprompt(2); 9354b88c807SRodney W. Grimes else 9364b88c807SRodney W. Grimes setprompt(0); 9374b88c807SRodney W. Grimes } else { 9384b88c807SRodney W. Grimes if (dblquote && c != '\\' && c != '`' && c != '$' 9394b88c807SRodney W. Grimes && (c != '"' || eofmark != NULL)) 9404b88c807SRodney W. Grimes USTPUTC('\\', out); 9414b88c807SRodney W. Grimes if (SQSYNTAX[c] == CCTL) 9424b88c807SRodney W. Grimes USTPUTC(CTLESC, out); 9434b88c807SRodney W. Grimes USTPUTC(c, out); 9444b88c807SRodney W. Grimes quotef++; 9454b88c807SRodney W. Grimes } 9464b88c807SRodney W. Grimes break; 9474b88c807SRodney W. Grimes case CSQUOTE: 9484b88c807SRodney W. Grimes syntax = SQSYNTAX; 9494b88c807SRodney W. Grimes break; 9504b88c807SRodney W. Grimes case CDQUOTE: 9514b88c807SRodney W. Grimes syntax = DQSYNTAX; 9524b88c807SRodney W. Grimes dblquote = 1; 9534b88c807SRodney W. Grimes break; 9544b88c807SRodney W. Grimes case CENDQUOTE: 9554b88c807SRodney W. Grimes if (eofmark) { 9564b88c807SRodney W. Grimes USTPUTC(c, out); 9574b88c807SRodney W. Grimes } else { 9584b88c807SRodney W. Grimes if (arinest) 9594b88c807SRodney W. Grimes syntax = ARISYNTAX; 9604b88c807SRodney W. Grimes else 9614b88c807SRodney W. Grimes syntax = BASESYNTAX; 9624b88c807SRodney W. Grimes quotef++; 9634b88c807SRodney W. Grimes dblquote = 0; 9644b88c807SRodney W. Grimes } 9654b88c807SRodney W. Grimes break; 9664b88c807SRodney W. Grimes case CVAR: /* '$' */ 9674b88c807SRodney W. Grimes PARSESUB(); /* parse substitution */ 9684b88c807SRodney W. Grimes break; 9694b88c807SRodney W. Grimes case CENDVAR: /* '}' */ 9704b88c807SRodney W. Grimes if (varnest > 0) { 9714b88c807SRodney W. Grimes varnest--; 9724b88c807SRodney W. Grimes USTPUTC(CTLENDVAR, out); 9734b88c807SRodney W. Grimes } else { 9744b88c807SRodney W. Grimes USTPUTC(c, out); 9754b88c807SRodney W. Grimes } 9764b88c807SRodney W. Grimes break; 9774b88c807SRodney W. Grimes case CLP: /* '(' in arithmetic */ 9784b88c807SRodney W. Grimes parenlevel++; 9794b88c807SRodney W. Grimes USTPUTC(c, out); 9804b88c807SRodney W. Grimes break; 9814b88c807SRodney W. Grimes case CRP: /* ')' in arithmetic */ 9824b88c807SRodney W. Grimes if (parenlevel > 0) { 9834b88c807SRodney W. Grimes USTPUTC(c, out); 9844b88c807SRodney W. Grimes --parenlevel; 9854b88c807SRodney W. Grimes } else { 9864b88c807SRodney W. Grimes if (pgetc() == ')') { 9874b88c807SRodney W. Grimes if (--arinest == 0) { 9884b88c807SRodney W. Grimes USTPUTC(CTLENDARI, out); 9894b88c807SRodney W. Grimes syntax = prevsyntax; 9904b88c807SRodney W. Grimes } else 9914b88c807SRodney W. Grimes USTPUTC(')', out); 9924b88c807SRodney W. Grimes } else { 9934b88c807SRodney W. Grimes /* 9944b88c807SRodney W. Grimes * unbalanced parens 9954b88c807SRodney W. Grimes * (don't 2nd guess - no error) 9964b88c807SRodney W. Grimes */ 9974b88c807SRodney W. Grimes pungetc(); 9984b88c807SRodney W. Grimes USTPUTC(')', out); 9994b88c807SRodney W. Grimes } 10004b88c807SRodney W. Grimes } 10014b88c807SRodney W. Grimes break; 10024b88c807SRodney W. Grimes case CBQUOTE: /* '`' */ 10034b88c807SRodney W. Grimes PARSEBACKQOLD(); 10044b88c807SRodney W. Grimes break; 10054b88c807SRodney W. Grimes case CEOF: 10064b88c807SRodney W. Grimes goto endword; /* exit outer loop */ 10074b88c807SRodney W. Grimes default: 10084b88c807SRodney W. Grimes if (varnest == 0) 10094b88c807SRodney W. Grimes goto endword; /* exit outer loop */ 10104b88c807SRodney W. Grimes USTPUTC(c, out); 10114b88c807SRodney W. Grimes } 10124b88c807SRodney W. Grimes c = pgetc_macro(); 10134b88c807SRodney W. Grimes } 10144b88c807SRodney W. Grimes } 10154b88c807SRodney W. Grimes endword: 10164b88c807SRodney W. Grimes if (syntax == ARISYNTAX) 10174b88c807SRodney W. Grimes synerror("Missing '))'"); 10184b88c807SRodney W. Grimes if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) 10194b88c807SRodney W. Grimes synerror("Unterminated quoted string"); 10204b88c807SRodney W. Grimes if (varnest != 0) { 10214b88c807SRodney W. Grimes startlinno = plinno; 10224b88c807SRodney W. Grimes synerror("Missing '}'"); 10234b88c807SRodney W. Grimes } 10244b88c807SRodney W. Grimes USTPUTC('\0', out); 10254b88c807SRodney W. Grimes len = out - stackblock(); 10264b88c807SRodney W. Grimes out = stackblock(); 10274b88c807SRodney W. Grimes if (eofmark == NULL) { 10284b88c807SRodney W. Grimes if ((c == '>' || c == '<') 10294b88c807SRodney W. Grimes && quotef == 0 10304b88c807SRodney W. Grimes && len <= 2 10314b88c807SRodney W. Grimes && (*out == '\0' || is_digit(*out))) { 10324b88c807SRodney W. Grimes PARSEREDIR(); 10334b88c807SRodney W. Grimes return lasttoken = TREDIR; 10344b88c807SRodney W. Grimes } else { 10354b88c807SRodney W. Grimes pungetc(); 10364b88c807SRodney W. Grimes } 10374b88c807SRodney W. Grimes } 10384b88c807SRodney W. Grimes quoteflag = quotef; 10394b88c807SRodney W. Grimes backquotelist = bqlist; 10404b88c807SRodney W. Grimes grabstackblock(len); 10414b88c807SRodney W. Grimes wordtext = out; 10424b88c807SRodney W. Grimes return lasttoken = TWORD; 10434b88c807SRodney W. Grimes /* end of readtoken routine */ 10444b88c807SRodney W. Grimes 10454b88c807SRodney W. Grimes 10464b88c807SRodney W. Grimes 10474b88c807SRodney W. Grimes /* 10484b88c807SRodney W. Grimes * Check to see whether we are at the end of the here document. When this 10494b88c807SRodney W. Grimes * is called, c is set to the first character of the next input line. If 10504b88c807SRodney W. Grimes * we are at the end of the here document, this routine sets the c to PEOF. 10514b88c807SRodney W. Grimes */ 10524b88c807SRodney W. Grimes 10534b88c807SRodney W. Grimes checkend: { 10544b88c807SRodney W. Grimes if (eofmark) { 10554b88c807SRodney W. Grimes if (striptabs) { 10564b88c807SRodney W. Grimes while (c == '\t') 10574b88c807SRodney W. Grimes c = pgetc(); 10584b88c807SRodney W. Grimes } 10594b88c807SRodney W. Grimes if (c == *eofmark) { 10604b88c807SRodney W. Grimes if (pfgets(line, sizeof line) != NULL) { 10617920a31dSSteve Price char *p, *q; 10624b88c807SRodney W. Grimes 10634b88c807SRodney W. Grimes p = line; 10644b88c807SRodney W. Grimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 10654b88c807SRodney W. Grimes if (*p == '\n' && *q == '\0') { 10664b88c807SRodney W. Grimes c = PEOF; 10674b88c807SRodney W. Grimes plinno++; 10684b88c807SRodney W. Grimes needprompt = doprompt; 10694b88c807SRodney W. Grimes } else { 10704b88c807SRodney W. Grimes pushstring(line, strlen(line), NULL); 10714b88c807SRodney W. Grimes } 10724b88c807SRodney W. Grimes } 10734b88c807SRodney W. Grimes } 10744b88c807SRodney W. Grimes } 10754b88c807SRodney W. Grimes goto checkend_return; 10764b88c807SRodney W. Grimes } 10774b88c807SRodney W. Grimes 10784b88c807SRodney W. Grimes 10794b88c807SRodney W. Grimes /* 10804b88c807SRodney W. Grimes * Parse a redirection operator. The variable "out" points to a string 10814b88c807SRodney W. Grimes * specifying the fd to be redirected. The variable "c" contains the 10824b88c807SRodney W. Grimes * first character of the redirection operator. 10834b88c807SRodney W. Grimes */ 10844b88c807SRodney W. Grimes 10854b88c807SRodney W. Grimes parseredir: { 10864b88c807SRodney W. Grimes char fd = *out; 10874b88c807SRodney W. Grimes union node *np; 10884b88c807SRodney W. Grimes 10894b88c807SRodney W. Grimes np = (union node *)stalloc(sizeof (struct nfile)); 10904b88c807SRodney W. Grimes if (c == '>') { 10914b88c807SRodney W. Grimes np->nfile.fd = 1; 10924b88c807SRodney W. Grimes c = pgetc(); 10934b88c807SRodney W. Grimes if (c == '>') 10944b88c807SRodney W. Grimes np->type = NAPPEND; 10954b88c807SRodney W. Grimes else if (c == '&') 10964b88c807SRodney W. Grimes np->type = NTOFD; 10974b88c807SRodney W. Grimes else { 10984b88c807SRodney W. Grimes np->type = NTO; 10994b88c807SRodney W. Grimes pungetc(); 11004b88c807SRodney W. Grimes } 11014b88c807SRodney W. Grimes } else { /* c == '<' */ 11024b88c807SRodney W. Grimes np->nfile.fd = 0; 11034b88c807SRodney W. Grimes c = pgetc(); 11044b88c807SRodney W. Grimes if (c == '<') { 11054b88c807SRodney W. Grimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 11064b88c807SRodney W. Grimes np = (union node *)stalloc(sizeof (struct nhere)); 11074b88c807SRodney W. Grimes np->nfile.fd = 0; 11084b88c807SRodney W. Grimes } 11094b88c807SRodney W. Grimes np->type = NHERE; 11104b88c807SRodney W. Grimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 11114b88c807SRodney W. Grimes heredoc->here = np; 11124b88c807SRodney W. Grimes if ((c = pgetc()) == '-') { 11134b88c807SRodney W. Grimes heredoc->striptabs = 1; 11144b88c807SRodney W. Grimes } else { 11154b88c807SRodney W. Grimes heredoc->striptabs = 0; 11164b88c807SRodney W. Grimes pungetc(); 11174b88c807SRodney W. Grimes } 11184b88c807SRodney W. Grimes } else if (c == '&') 11194b88c807SRodney W. Grimes np->type = NFROMFD; 11204b88c807SRodney W. Grimes else { 11214b88c807SRodney W. Grimes np->type = NFROM; 11224b88c807SRodney W. Grimes pungetc(); 11234b88c807SRodney W. Grimes } 11244b88c807SRodney W. Grimes } 11254b88c807SRodney W. Grimes if (fd != '\0') 11264b88c807SRodney W. Grimes np->nfile.fd = digit_val(fd); 11274b88c807SRodney W. Grimes redirnode = np; 11284b88c807SRodney W. Grimes goto parseredir_return; 11294b88c807SRodney W. Grimes } 11304b88c807SRodney W. Grimes 11314b88c807SRodney W. Grimes 11324b88c807SRodney W. Grimes /* 11334b88c807SRodney W. Grimes * Parse a substitution. At this point, we have read the dollar sign 11344b88c807SRodney W. Grimes * and nothing else. 11354b88c807SRodney W. Grimes */ 11364b88c807SRodney W. Grimes 11374b88c807SRodney W. Grimes parsesub: { 11384b88c807SRodney W. Grimes int subtype; 11394b88c807SRodney W. Grimes int typeloc; 11404b88c807SRodney W. Grimes int flags; 11414b88c807SRodney W. Grimes char *p; 11424b88c807SRodney W. Grimes #ifndef GDB_HACK 11434b88c807SRodney W. Grimes static const char types[] = "}-+?="; 11444b88c807SRodney W. Grimes #endif 11455c817731SPeter Wemm int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 11464b88c807SRodney W. Grimes 11474b88c807SRodney W. Grimes c = pgetc(); 11484b88c807SRodney W. Grimes if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) { 11494b88c807SRodney W. Grimes USTPUTC('$', out); 11504b88c807SRodney W. Grimes pungetc(); 11514b88c807SRodney W. Grimes } else if (c == '(') { /* $(command) or $((arith)) */ 11524b88c807SRodney W. Grimes if (pgetc() == '(') { 11534b88c807SRodney W. Grimes PARSEARITH(); 11544b88c807SRodney W. Grimes } else { 11554b88c807SRodney W. Grimes pungetc(); 11564b88c807SRodney W. Grimes PARSEBACKQNEW(); 11574b88c807SRodney W. Grimes } 11584b88c807SRodney W. Grimes } else { 11594b88c807SRodney W. Grimes USTPUTC(CTLVAR, out); 11604b88c807SRodney W. Grimes typeloc = out - stackblock(); 11614b88c807SRodney W. Grimes USTPUTC(VSNORMAL, out); 11624b88c807SRodney W. Grimes subtype = VSNORMAL; 11634b88c807SRodney W. Grimes if (c == '{') { 11645c817731SPeter Wemm bracketed_name = 1; 11654b88c807SRodney W. Grimes c = pgetc(); 1166aa9caaf6SPeter Wemm if (c == '#') { 1167aa9caaf6SPeter Wemm if ((c = pgetc()) == '}') 1168aa9caaf6SPeter Wemm c = '#'; 1169aa9caaf6SPeter Wemm else 1170aa9caaf6SPeter Wemm subtype = VSLENGTH; 1171aa9caaf6SPeter Wemm } 1172aa9caaf6SPeter Wemm else 11734b88c807SRodney W. Grimes subtype = 0; 11744b88c807SRodney W. Grimes } 11754b88c807SRodney W. Grimes if (is_name(c)) { 11764b88c807SRodney W. Grimes do { 11774b88c807SRodney W. Grimes STPUTC(c, out); 11784b88c807SRodney W. Grimes c = pgetc(); 11794b88c807SRodney W. Grimes } while (is_in_name(c)); 11805c817731SPeter Wemm } else if (is_digit(c)) { 11815c817731SPeter Wemm if (bracketed_name) { 11825c817731SPeter Wemm do { 11835c817731SPeter Wemm STPUTC(c, out); 11845c817731SPeter Wemm c = pgetc(); 11855c817731SPeter Wemm } while (is_digit(c)); 11865c817731SPeter Wemm } else { 11875c817731SPeter Wemm STPUTC(c, out); 11885c817731SPeter Wemm c = pgetc(); 11895c817731SPeter Wemm } 11904b88c807SRodney W. Grimes } else { 11914b88c807SRodney W. Grimes if (! is_special(c)) 11924b88c807SRodney W. Grimes badsub: synerror("Bad substitution"); 11934b88c807SRodney W. Grimes USTPUTC(c, out); 11944b88c807SRodney W. Grimes c = pgetc(); 11954b88c807SRodney W. Grimes } 11964b88c807SRodney W. Grimes STPUTC('=', out); 11974b88c807SRodney W. Grimes flags = 0; 11984b88c807SRodney W. Grimes if (subtype == 0) { 1199aa9caaf6SPeter Wemm switch (c) { 1200aa9caaf6SPeter Wemm case ':': 12014b88c807SRodney W. Grimes flags = VSNUL; 12024b88c807SRodney W. Grimes c = pgetc(); 1203aa9caaf6SPeter Wemm /*FALLTHROUGH*/ 1204aa9caaf6SPeter Wemm default: 12054b88c807SRodney W. Grimes p = strchr(types, c); 12064b88c807SRodney W. Grimes if (p == NULL) 12074b88c807SRodney W. Grimes goto badsub; 12084b88c807SRodney W. Grimes subtype = p - types + VSNORMAL; 1209aa9caaf6SPeter Wemm break; 1210aa9caaf6SPeter Wemm case '%': 1211aa9caaf6SPeter Wemm case '#': 1212aa9caaf6SPeter Wemm { 1213aa9caaf6SPeter Wemm int cc = c; 1214aa9caaf6SPeter Wemm subtype = c == '#' ? VSTRIMLEFT : 1215aa9caaf6SPeter Wemm VSTRIMRIGHT; 1216aa9caaf6SPeter Wemm c = pgetc(); 1217aa9caaf6SPeter Wemm if (c == cc) 1218aa9caaf6SPeter Wemm subtype++; 1219aa9caaf6SPeter Wemm else 1220aa9caaf6SPeter Wemm pungetc(); 1221aa9caaf6SPeter Wemm break; 1222aa9caaf6SPeter Wemm } 1223aa9caaf6SPeter Wemm } 12244b88c807SRodney W. Grimes } else { 12254b88c807SRodney W. Grimes pungetc(); 12264b88c807SRodney W. Grimes } 12274b88c807SRodney W. Grimes if (dblquote || arinest) 12284b88c807SRodney W. Grimes flags |= VSQUOTE; 12294b88c807SRodney W. Grimes *(stackblock() + typeloc) = subtype | flags; 12304b88c807SRodney W. Grimes if (subtype != VSNORMAL) 12314b88c807SRodney W. Grimes varnest++; 12324b88c807SRodney W. Grimes } 12334b88c807SRodney W. Grimes goto parsesub_return; 12344b88c807SRodney W. Grimes } 12354b88c807SRodney W. Grimes 12364b88c807SRodney W. Grimes 12374b88c807SRodney W. Grimes /* 12384b88c807SRodney W. Grimes * Called to parse command substitutions. Newstyle is set if the command 12394b88c807SRodney W. Grimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked 12404b88c807SRodney W. Grimes * list of commands (passed by reference), and savelen is the number of 12414b88c807SRodney W. Grimes * characters on the top of the stack which must be preserved. 12424b88c807SRodney W. Grimes */ 12434b88c807SRodney W. Grimes 12444b88c807SRodney W. Grimes parsebackq: { 12454b88c807SRodney W. Grimes struct nodelist **nlpp; 12464b88c807SRodney W. Grimes int savepbq; 12474b88c807SRodney W. Grimes union node *n; 12484b88c807SRodney W. Grimes char *volatile str; 12494b88c807SRodney W. Grimes struct jmploc jmploc; 12504b88c807SRodney W. Grimes struct jmploc *volatile savehandler; 12514b88c807SRodney W. Grimes int savelen; 1252ab0a2172SSteve Price int saveprompt; 1253ab0a2172SSteve Price #if __GNUC__ 1254ab0a2172SSteve Price /* Avoid longjmp clobbering */ 1255ab0a2172SSteve Price (void) &saveprompt; 1256ab0a2172SSteve Price #endif 12574b88c807SRodney W. Grimes 12584b88c807SRodney W. Grimes savepbq = parsebackquote; 12594b88c807SRodney W. Grimes if (setjmp(jmploc.loc)) { 12604b88c807SRodney W. Grimes if (str) 12614b88c807SRodney W. Grimes ckfree(str); 12624b88c807SRodney W. Grimes parsebackquote = 0; 12634b88c807SRodney W. Grimes handler = savehandler; 12644b88c807SRodney W. Grimes longjmp(handler->loc, 1); 12654b88c807SRodney W. Grimes } 12664b88c807SRodney W. Grimes INTOFF; 12674b88c807SRodney W. Grimes str = NULL; 12684b88c807SRodney W. Grimes savelen = out - stackblock(); 12694b88c807SRodney W. Grimes if (savelen > 0) { 12704b88c807SRodney W. Grimes str = ckmalloc(savelen); 1271aa9caaf6SPeter Wemm memcpy(str, stackblock(), savelen); 12724b88c807SRodney W. Grimes } 12734b88c807SRodney W. Grimes savehandler = handler; 12744b88c807SRodney W. Grimes handler = &jmploc; 12754b88c807SRodney W. Grimes INTON; 12764b88c807SRodney W. Grimes if (oldstyle) { 12774b88c807SRodney W. Grimes /* We must read until the closing backquote, giving special 12784b88c807SRodney W. Grimes treatment to some slashes, and then push the string and 12794b88c807SRodney W. Grimes reread it as input, interpreting it normally. */ 12807920a31dSSteve Price char *out; 12817920a31dSSteve Price int c; 12824b88c807SRodney W. Grimes int savelen; 12834b88c807SRodney W. Grimes char *str; 12844b88c807SRodney W. Grimes 1285ab0a2172SSteve Price 12864b88c807SRodney W. Grimes STARTSTACKSTR(out); 1287ab0a2172SSteve Price for (;;) { 1288ab0a2172SSteve Price if (needprompt) { 1289ab0a2172SSteve Price setprompt(2); 1290ab0a2172SSteve Price needprompt = 0; 129172348d41SJoerg Wunsch } 1292ab0a2172SSteve Price switch (c = pgetc()) { 1293ab0a2172SSteve Price case '`': 1294ab0a2172SSteve Price goto done; 1295ab0a2172SSteve Price 1296ab0a2172SSteve Price case '\\': 1297ab0a2172SSteve Price if ((c = pgetc()) == '\n') { 1298ab0a2172SSteve Price plinno++; 1299ab0a2172SSteve Price if (doprompt) 1300ab0a2172SSteve Price setprompt(2); 1301ab0a2172SSteve Price else 1302ab0a2172SSteve Price setprompt(0); 1303ab0a2172SSteve Price /* 1304ab0a2172SSteve Price * If eating a newline, avoid putting 1305ab0a2172SSteve Price * the newline into the new character 1306ab0a2172SSteve Price * stream (via the STPUTC after the 1307ab0a2172SSteve Price * switch). 1308ab0a2172SSteve Price */ 1309ab0a2172SSteve Price continue; 1310ab0a2172SSteve Price } 13111e95454eSPaul Richards if (c != '\\' && c != '`' && c != '$' 13124b88c807SRodney W. Grimes && (!dblquote || c != '"')) 13134b88c807SRodney W. Grimes STPUTC('\\', out); 1314ab0a2172SSteve Price break; 1315ab0a2172SSteve Price 1316ab0a2172SSteve Price case '\n': 1317ab0a2172SSteve Price plinno++; 1318ab0a2172SSteve Price needprompt = doprompt; 1319ab0a2172SSteve Price break; 1320ab0a2172SSteve Price 1321ab0a2172SSteve Price case PEOF: 1322ab0a2172SSteve Price startlinno = plinno; 1323ab0a2172SSteve Price synerror("EOF in backquote substitution"); 1324ab0a2172SSteve Price break; 1325ab0a2172SSteve Price 1326ab0a2172SSteve Price default: 1327ab0a2172SSteve Price break; 13284b88c807SRodney W. Grimes } 13294b88c807SRodney W. Grimes STPUTC(c, out); 13304b88c807SRodney W. Grimes } 1331ab0a2172SSteve Price done: 13324b88c807SRodney W. Grimes STPUTC('\0', out); 13334b88c807SRodney W. Grimes savelen = out - stackblock(); 13344b88c807SRodney W. Grimes if (savelen > 0) { 13354b88c807SRodney W. Grimes str = ckmalloc(savelen); 1336aa9caaf6SPeter Wemm memcpy(str, stackblock(), savelen); 13374b88c807SRodney W. Grimes setinputstring(str, 1); 13384b88c807SRodney W. Grimes } 1339aa9caaf6SPeter Wemm } 13404b88c807SRodney W. Grimes nlpp = &bqlist; 13414b88c807SRodney W. Grimes while (*nlpp) 13424b88c807SRodney W. Grimes nlpp = &(*nlpp)->next; 13434b88c807SRodney W. Grimes *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 13444b88c807SRodney W. Grimes (*nlpp)->next = NULL; 13454b88c807SRodney W. Grimes parsebackquote = oldstyle; 1346ab0a2172SSteve Price 1347ab0a2172SSteve Price if (oldstyle) { 1348ab0a2172SSteve Price saveprompt = doprompt; 1349ab0a2172SSteve Price doprompt = 0; 1350ab0a2172SSteve Price } 1351ab0a2172SSteve Price 13524b88c807SRodney W. Grimes n = list(0); 1353ab0a2172SSteve Price 13544b88c807SRodney W. Grimes if (oldstyle) 1355ab0a2172SSteve Price doprompt = saveprompt; 1356ab0a2172SSteve Price else { 1357ab0a2172SSteve Price if (readtoken() != TRP) 1358ab0a2172SSteve Price synexpect(TRP); 1359ab0a2172SSteve Price } 1360ab0a2172SSteve Price 1361ab0a2172SSteve Price (*nlpp)->n = n; 1362ab0a2172SSteve Price if (oldstyle) { 1363ab0a2172SSteve Price /* 1364ab0a2172SSteve Price * Start reading from old file again, ignoring any pushed back 1365ab0a2172SSteve Price * tokens left from the backquote parsing 1366ab0a2172SSteve Price */ 13674b88c807SRodney W. Grimes popfile(); 1368ab0a2172SSteve Price tokpushback = 0; 1369ab0a2172SSteve Price } 13704b88c807SRodney W. Grimes while (stackblocksize() <= savelen) 13714b88c807SRodney W. Grimes growstackblock(); 13724b88c807SRodney W. Grimes STARTSTACKSTR(out); 13734b88c807SRodney W. Grimes if (str) { 1374aa9caaf6SPeter Wemm memcpy(out, str, savelen); 13754b88c807SRodney W. Grimes STADJUST(savelen, out); 13764b88c807SRodney W. Grimes INTOFF; 13774b88c807SRodney W. Grimes ckfree(str); 13784b88c807SRodney W. Grimes str = NULL; 13794b88c807SRodney W. Grimes INTON; 13804b88c807SRodney W. Grimes } 13814b88c807SRodney W. Grimes parsebackquote = savepbq; 13824b88c807SRodney W. Grimes handler = savehandler; 13834b88c807SRodney W. Grimes if (arinest || dblquote) 13844b88c807SRodney W. Grimes USTPUTC(CTLBACKQ | CTLQUOTE, out); 13854b88c807SRodney W. Grimes else 13864b88c807SRodney W. Grimes USTPUTC(CTLBACKQ, out); 13874b88c807SRodney W. Grimes if (oldstyle) 13884b88c807SRodney W. Grimes goto parsebackq_oldreturn; 13894b88c807SRodney W. Grimes else 13904b88c807SRodney W. Grimes goto parsebackq_newreturn; 13914b88c807SRodney W. Grimes } 13924b88c807SRodney W. Grimes 13934b88c807SRodney W. Grimes /* 13944b88c807SRodney W. Grimes * Parse an arithmetic expansion (indicate start of one and set state) 13954b88c807SRodney W. Grimes */ 13964b88c807SRodney W. Grimes parsearith: { 13974b88c807SRodney W. Grimes 13984b88c807SRodney W. Grimes if (++arinest == 1) { 13994b88c807SRodney W. Grimes prevsyntax = syntax; 14004b88c807SRodney W. Grimes syntax = ARISYNTAX; 14014b88c807SRodney W. Grimes USTPUTC(CTLARI, out); 14024b88c807SRodney W. Grimes } else { 14034b88c807SRodney W. Grimes /* 14044b88c807SRodney W. Grimes * we collapse embedded arithmetic expansion to 14054b88c807SRodney W. Grimes * parenthesis, which should be equivalent 14064b88c807SRodney W. Grimes */ 14074b88c807SRodney W. Grimes USTPUTC('(', out); 14084b88c807SRodney W. Grimes } 14094b88c807SRodney W. Grimes goto parsearith_return; 14104b88c807SRodney W. Grimes } 14114b88c807SRodney W. Grimes 14124b88c807SRodney W. Grimes } /* end of readtoken */ 14134b88c807SRodney W. Grimes 14144b88c807SRodney W. Grimes 14154b88c807SRodney W. Grimes 14164b88c807SRodney W. Grimes #ifdef mkinit 14174b88c807SRodney W. Grimes RESET { 14184b88c807SRodney W. Grimes tokpushback = 0; 14194b88c807SRodney W. Grimes checkkwd = 0; 14204b88c807SRodney W. Grimes } 14214b88c807SRodney W. Grimes #endif 14224b88c807SRodney W. Grimes 14234b88c807SRodney W. Grimes /* 14244b88c807SRodney W. Grimes * Returns true if the text contains nothing to expand (no dollar signs 14254b88c807SRodney W. Grimes * or backquotes). 14264b88c807SRodney W. Grimes */ 14274b88c807SRodney W. Grimes 14284b88c807SRodney W. Grimes STATIC int 14294b88c807SRodney W. Grimes noexpand(text) 14304b88c807SRodney W. Grimes char *text; 14314b88c807SRodney W. Grimes { 14327920a31dSSteve Price char *p; 14337920a31dSSteve Price char c; 14344b88c807SRodney W. Grimes 14354b88c807SRodney W. Grimes p = text; 14364b88c807SRodney W. Grimes while ((c = *p++) != '\0') { 14374b88c807SRodney W. Grimes if (c == CTLESC) 14384b88c807SRodney W. Grimes p++; 14394b88c807SRodney W. Grimes else if (BASESYNTAX[c] == CCTL) 14404b88c807SRodney W. Grimes return 0; 14414b88c807SRodney W. Grimes } 14424b88c807SRodney W. Grimes return 1; 14434b88c807SRodney W. Grimes } 14444b88c807SRodney W. Grimes 14454b88c807SRodney W. Grimes 14464b88c807SRodney W. Grimes /* 14474b88c807SRodney W. Grimes * Return true if the argument is a legal variable name (a letter or 14484b88c807SRodney W. Grimes * underscore followed by zero or more letters, underscores, and digits). 14494b88c807SRodney W. Grimes */ 14504b88c807SRodney W. Grimes 14514b88c807SRodney W. Grimes int 14524b88c807SRodney W. Grimes goodname(name) 14534b88c807SRodney W. Grimes char *name; 14544b88c807SRodney W. Grimes { 14557920a31dSSteve Price char *p; 14564b88c807SRodney W. Grimes 14574b88c807SRodney W. Grimes p = name; 14584b88c807SRodney W. Grimes if (! is_name(*p)) 14594b88c807SRodney W. Grimes return 0; 14604b88c807SRodney W. Grimes while (*++p) { 14614b88c807SRodney W. Grimes if (! is_in_name(*p)) 14624b88c807SRodney W. Grimes return 0; 14634b88c807SRodney W. Grimes } 14644b88c807SRodney W. Grimes return 1; 14654b88c807SRodney W. Grimes } 14664b88c807SRodney W. Grimes 14674b88c807SRodney W. Grimes 14684b88c807SRodney W. Grimes /* 14694b88c807SRodney W. Grimes * Called when an unexpected token is read during the parse. The argument 14704b88c807SRodney W. Grimes * is the token that is expected, or -1 if more than one type of token can 14714b88c807SRodney W. Grimes * occur at this point. 14724b88c807SRodney W. Grimes */ 14734b88c807SRodney W. Grimes 14744b88c807SRodney W. Grimes STATIC void 1475aa9caaf6SPeter Wemm synexpect(token) 1476aa9caaf6SPeter Wemm int token; 1477aa9caaf6SPeter Wemm { 14784b88c807SRodney W. Grimes char msg[64]; 14794b88c807SRodney W. Grimes 14804b88c807SRodney W. Grimes if (token >= 0) { 14814b88c807SRodney W. Grimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 14824b88c807SRodney W. Grimes tokname[lasttoken], tokname[token]); 14834b88c807SRodney W. Grimes } else { 14844b88c807SRodney W. Grimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 14854b88c807SRodney W. Grimes } 14864b88c807SRodney W. Grimes synerror(msg); 14874b88c807SRodney W. Grimes } 14884b88c807SRodney W. Grimes 14894b88c807SRodney W. Grimes 14904b88c807SRodney W. Grimes STATIC void 14914b88c807SRodney W. Grimes synerror(msg) 14924b88c807SRodney W. Grimes char *msg; 14934b88c807SRodney W. Grimes { 14944b88c807SRodney W. Grimes if (commandname) 14954b88c807SRodney W. Grimes outfmt(&errout, "%s: %d: ", commandname, startlinno); 14964b88c807SRodney W. Grimes outfmt(&errout, "Syntax error: %s\n", msg); 14974b88c807SRodney W. Grimes error((char *)NULL); 14984b88c807SRodney W. Grimes } 14994b88c807SRodney W. Grimes 15004b88c807SRodney W. Grimes STATIC void 15014b88c807SRodney W. Grimes setprompt(which) 15024b88c807SRodney W. Grimes int which; 15034b88c807SRodney W. Grimes { 15044b88c807SRodney W. Grimes whichprompt = which; 15054b88c807SRodney W. Grimes 1506aa9caaf6SPeter Wemm #ifndef NO_HISTORY 15074b88c807SRodney W. Grimes if (!el) 1508aa9caaf6SPeter Wemm #endif 15094b88c807SRodney W. Grimes out2str(getprompt(NULL)); 15104b88c807SRodney W. Grimes } 15114b88c807SRodney W. Grimes 15124b88c807SRodney W. Grimes /* 15134b88c807SRodney W. Grimes * called by editline -- any expansions to the prompt 15144b88c807SRodney W. Grimes * should be added here. 15154b88c807SRodney W. Grimes */ 15164b88c807SRodney W. Grimes char * 15174b88c807SRodney W. Grimes getprompt(unused) 1518e7a0b024SSteve Price void *unused __unused; 15194b88c807SRodney W. Grimes { 15204b88c807SRodney W. Grimes switch (whichprompt) { 15214b88c807SRodney W. Grimes case 0: 15224b88c807SRodney W. Grimes return ""; 15234b88c807SRodney W. Grimes case 1: 15244b88c807SRodney W. Grimes return ps1val(); 15254b88c807SRodney W. Grimes case 2: 15264b88c807SRodney W. Grimes return ps2val(); 15274b88c807SRodney W. Grimes default: 15284b88c807SRodney W. Grimes return "<internal prompt error>"; 15294b88c807SRodney W. Grimes } 15304b88c807SRodney W. Grimes } 1531