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 * 4. Neither the name of the University nor the names of its contributors 174b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 184b88c807SRodney W. Grimes * without specific prior written permission. 194b88c807SRodney W. Grimes * 204b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 214b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 244b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304b88c807SRodney W. Grimes * SUCH DAMAGE. 314b88c807SRodney W. Grimes */ 324b88c807SRodney W. Grimes 334b88c807SRodney W. Grimes #ifndef lint 343d7b5b93SPhilippe Charnier #if 0 353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; 363d7b5b93SPhilippe Charnier #endif 374b88c807SRodney W. Grimes #endif /* not lint */ 382749b141SDavid E. O'Brien #include <sys/cdefs.h> 392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 404b88c807SRodney W. Grimes 41aa9caaf6SPeter Wemm #include <stdlib.h> 42955e9f68SStefan Farfeleder #include <unistd.h> 43aa9caaf6SPeter Wemm 444b88c807SRodney W. Grimes #include "shell.h" 454b88c807SRodney W. Grimes #include "parser.h" 464b88c807SRodney W. Grimes #include "nodes.h" 474b88c807SRodney W. Grimes #include "expand.h" /* defines rmescapes() */ 484b88c807SRodney W. Grimes #include "syntax.h" 494b88c807SRodney W. Grimes #include "options.h" 504b88c807SRodney W. Grimes #include "input.h" 514b88c807SRodney W. Grimes #include "output.h" 524b88c807SRodney W. Grimes #include "var.h" 534b88c807SRodney W. Grimes #include "error.h" 544b88c807SRodney W. Grimes #include "memalloc.h" 554b88c807SRodney W. Grimes #include "mystring.h" 564b88c807SRodney W. Grimes #include "alias.h" 57aa9caaf6SPeter Wemm #include "show.h" 58f01e3d0cSMartin Cracauer #include "eval.h" 59aa9caaf6SPeter Wemm #ifndef NO_HISTORY 604b88c807SRodney W. Grimes #include "myhistedit.h" 61aa9caaf6SPeter Wemm #endif 624b88c807SRodney W. Grimes 634b88c807SRodney W. Grimes /* 644b88c807SRodney W. Grimes * Shell command parser. 654b88c807SRodney W. Grimes */ 664b88c807SRodney W. Grimes 674b88c807SRodney W. Grimes #define EOFMARKLEN 79 68f0c73601SDavid E. O'Brien #define PROMPTLEN 128 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 842ba1b30bSDiomidis Spinellis STATIC struct heredoc *heredoclist; /* list of here documents to read */ 852ba1b30bSDiomidis Spinellis STATIC int parsebackquote; /* nonzero if we are inside backquotes */ 862ba1b30bSDiomidis Spinellis STATIC int doprompt; /* if set, prompt the user */ 872ba1b30bSDiomidis Spinellis STATIC int needprompt; /* true if interactive and at start of line */ 882ba1b30bSDiomidis Spinellis STATIC int lasttoken; /* last token read */ 894b88c807SRodney W. Grimes MKINIT int tokpushback; /* last token pushed back */ 902ba1b30bSDiomidis Spinellis STATIC char *wordtext; /* text of last word returned by readtoken */ 914b88c807SRodney W. Grimes MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ 922ba1b30bSDiomidis Spinellis STATIC struct nodelist *backquotelist; 932ba1b30bSDiomidis Spinellis STATIC union node *redirnode; 942ba1b30bSDiomidis Spinellis STATIC struct heredoc *heredoc; 952ba1b30bSDiomidis Spinellis STATIC int quoteflag; /* set if (part of) last token was quoted */ 962ba1b30bSDiomidis Spinellis STATIC 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 1085134c3f7SWarner Losh STATIC union node *list(int); 1095134c3f7SWarner Losh STATIC union node *andor(void); 1105134c3f7SWarner Losh STATIC union node *pipeline(void); 1115134c3f7SWarner Losh STATIC union node *command(void); 1125134c3f7SWarner Losh STATIC union node *simplecmd(union node **, union node *); 1135134c3f7SWarner Losh STATIC union node *makename(void); 1145134c3f7SWarner Losh STATIC void parsefname(void); 1155134c3f7SWarner Losh STATIC void parseheredoc(void); 1165134c3f7SWarner Losh STATIC int peektoken(void); 1175134c3f7SWarner Losh STATIC int readtoken(void); 1185134c3f7SWarner Losh STATIC int xxreadtoken(void); 1195134c3f7SWarner Losh STATIC int readtoken1(int, char const *, char *, int); 1205134c3f7SWarner Losh STATIC int noexpand(char *); 1215134c3f7SWarner Losh STATIC void synexpect(int); 1225134c3f7SWarner Losh STATIC void synerror(char *); 1235134c3f7SWarner Losh STATIC void setprompt(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 * 1325134c3f7SWarner Losh parsecmd(int interact) 133aa9caaf6SPeter Wemm { 1344b88c807SRodney W. Grimes int t; 1354b88c807SRodney W. Grimes 13698e05fd3SMartin Cracauer tokpushback = 0; 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 * 1545134c3f7SWarner Losh list(int nlflag) 155aa9caaf6SPeter Wemm { 1564b88c807SRodney W. Grimes union node *n1, *n2, *n3; 157aa9caaf6SPeter Wemm int tok; 1584b88c807SRodney W. Grimes 1594b88c807SRodney W. Grimes checkkwd = 2; 1604b88c807SRodney W. Grimes if (nlflag == 0 && tokendlist[peektoken()]) 1614b88c807SRodney W. Grimes return NULL; 162aa9caaf6SPeter Wemm n1 = NULL; 1634b88c807SRodney W. Grimes for (;;) { 1644b88c807SRodney W. Grimes n2 = andor(); 165aa9caaf6SPeter Wemm tok = readtoken(); 166aa9caaf6SPeter Wemm if (tok == TBACKGND) { 167aa9caaf6SPeter Wemm if (n2->type == NCMD || n2->type == NPIPE) { 168aa9caaf6SPeter Wemm n2->ncmd.backgnd = 1; 169aa9caaf6SPeter Wemm } else if (n2->type == NREDIR) { 170aa9caaf6SPeter Wemm n2->type = NBACKGND; 171aa9caaf6SPeter Wemm } else { 172aa9caaf6SPeter Wemm n3 = (union node *)stalloc(sizeof (struct nredir)); 173aa9caaf6SPeter Wemm n3->type = NBACKGND; 174aa9caaf6SPeter Wemm n3->nredir.n = n2; 175aa9caaf6SPeter Wemm n3->nredir.redirect = NULL; 176aa9caaf6SPeter Wemm n2 = n3; 177aa9caaf6SPeter Wemm } 178aa9caaf6SPeter Wemm } 179aa9caaf6SPeter Wemm if (n1 == NULL) { 180aa9caaf6SPeter Wemm n1 = n2; 181aa9caaf6SPeter Wemm } 182aa9caaf6SPeter Wemm else { 1834b88c807SRodney W. Grimes n3 = (union node *)stalloc(sizeof (struct nbinary)); 1844b88c807SRodney W. Grimes n3->type = NSEMI; 1854b88c807SRodney W. Grimes n3->nbinary.ch1 = n1; 1864b88c807SRodney W. Grimes n3->nbinary.ch2 = n2; 1874b88c807SRodney W. Grimes n1 = n3; 188aa9caaf6SPeter Wemm } 189aa9caaf6SPeter Wemm switch (tok) { 190aa9caaf6SPeter Wemm case TBACKGND: 191aa9caaf6SPeter Wemm case TSEMI: 192aa9caaf6SPeter Wemm tok = readtoken(); 1930d9f1a69SPhilippe Charnier /* FALLTHROUGH */ 194aa9caaf6SPeter Wemm case TNL: 195aa9caaf6SPeter Wemm if (tok == TNL) { 196aa9caaf6SPeter Wemm parseheredoc(); 197aa9caaf6SPeter Wemm if (nlflag) 198aa9caaf6SPeter Wemm return n1; 199aa9caaf6SPeter Wemm } else { 200aa9caaf6SPeter Wemm tokpushback++; 201aa9caaf6SPeter Wemm } 202aa9caaf6SPeter Wemm checkkwd = 2; 203aa9caaf6SPeter Wemm if (tokendlist[peektoken()]) 204aa9caaf6SPeter Wemm return n1; 2054b88c807SRodney W. Grimes break; 2064b88c807SRodney W. Grimes case TEOF: 2074b88c807SRodney W. Grimes if (heredoclist) 2084b88c807SRodney W. Grimes parseheredoc(); 2094b88c807SRodney W. Grimes else 2104b88c807SRodney W. Grimes pungetc(); /* push back EOF on input */ 2114b88c807SRodney W. Grimes return n1; 2124b88c807SRodney W. Grimes default: 2134b88c807SRodney W. Grimes if (nlflag) 2144b88c807SRodney W. Grimes synexpect(-1); 2154b88c807SRodney W. Grimes tokpushback++; 2164b88c807SRodney W. Grimes return n1; 2174b88c807SRodney W. Grimes } 2184b88c807SRodney W. Grimes } 2194b88c807SRodney W. Grimes } 2204b88c807SRodney W. Grimes 2214b88c807SRodney W. Grimes 2224b88c807SRodney W. Grimes 2234b88c807SRodney W. Grimes STATIC union node * 2245134c3f7SWarner Losh andor(void) 2255134c3f7SWarner Losh { 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 * 2515134c3f7SWarner Losh pipeline(void) 2525134c3f7SWarner Losh { 253b785bd7dSBrian Somers union node *n1, *n2, *pipenode; 2544b88c807SRodney W. Grimes struct nodelist *lp, *prev; 255b785bd7dSBrian Somers int negate; 2564b88c807SRodney W. Grimes 257b785bd7dSBrian Somers negate = 0; 2584b88c807SRodney W. Grimes TRACE(("pipeline: entered\n")); 259b785bd7dSBrian Somers while (readtoken() == TNOT) 260b785bd7dSBrian Somers negate = !negate; 261b785bd7dSBrian Somers 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++; 280b785bd7dSBrian Somers if (negate) { 281b785bd7dSBrian Somers n2 = (union node *)stalloc(sizeof (struct nnot)); 282b785bd7dSBrian Somers n2->type = NNOT; 283b785bd7dSBrian Somers n2->nnot.com = n1; 284b785bd7dSBrian Somers return n2; 285b785bd7dSBrian Somers } else 2864b88c807SRodney W. Grimes return n1; 2874b88c807SRodney W. Grimes } 2884b88c807SRodney W. Grimes 2894b88c807SRodney W. Grimes 2904b88c807SRodney W. Grimes 2914b88c807SRodney W. Grimes STATIC union node * 2925134c3f7SWarner Losh command(void) 2935134c3f7SWarner Losh { 2944b88c807SRodney W. Grimes union node *n1, *n2; 2954b88c807SRodney W. Grimes union node *ap, **app; 2964b88c807SRodney W. Grimes union node *cp, **cpp; 2974b88c807SRodney W. Grimes union node *redir, **rpp; 2986c0bde79SBrian Somers int t, negate = 0; 2994b88c807SRodney W. Grimes 3004b88c807SRodney W. Grimes checkkwd = 2; 301aa9caaf6SPeter Wemm redir = NULL; 302aa9caaf6SPeter Wemm n1 = NULL; 3034b88c807SRodney W. Grimes rpp = &redir; 304ab0a2172SSteve Price 3054b88c807SRodney W. Grimes /* Check for redirection which may precede command */ 3064b88c807SRodney W. Grimes while (readtoken() == TREDIR) { 3074b88c807SRodney W. Grimes *rpp = n2 = redirnode; 3084b88c807SRodney W. Grimes rpp = &n2->nfile.next; 3094b88c807SRodney W. Grimes parsefname(); 3104b88c807SRodney W. Grimes } 3114b88c807SRodney W. Grimes tokpushback++; 3124b88c807SRodney W. Grimes 3136c0bde79SBrian Somers while (readtoken() == TNOT) { 3146c0bde79SBrian Somers TRACE(("command: TNOT recognized\n")); 3156c0bde79SBrian Somers negate = !negate; 3166c0bde79SBrian Somers } 3176c0bde79SBrian Somers tokpushback++; 3186c0bde79SBrian Somers 3194b88c807SRodney W. Grimes switch (readtoken()) { 3204b88c807SRodney W. Grimes case TIF: 3214b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nif)); 3224b88c807SRodney W. Grimes n1->type = NIF; 323427748f7STim J. Robbins if ((n1->nif.test = list(0)) == NULL) 324427748f7STim J. Robbins synexpect(-1); 3254b88c807SRodney W. Grimes if (readtoken() != TTHEN) 3264b88c807SRodney W. Grimes synexpect(TTHEN); 3274b88c807SRodney W. Grimes n1->nif.ifpart = list(0); 3284b88c807SRodney W. Grimes n2 = n1; 3294b88c807SRodney W. Grimes while (readtoken() == TELIF) { 3304b88c807SRodney W. Grimes n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 3314b88c807SRodney W. Grimes n2 = n2->nif.elsepart; 3324b88c807SRodney W. Grimes n2->type = NIF; 333427748f7STim J. Robbins if ((n2->nif.test = list(0)) == NULL) 334427748f7STim J. Robbins synexpect(-1); 3354b88c807SRodney W. Grimes if (readtoken() != TTHEN) 3364b88c807SRodney W. Grimes synexpect(TTHEN); 3374b88c807SRodney W. Grimes n2->nif.ifpart = list(0); 3384b88c807SRodney W. Grimes } 3394b88c807SRodney W. Grimes if (lasttoken == TELSE) 3404b88c807SRodney W. Grimes n2->nif.elsepart = list(0); 3414b88c807SRodney W. Grimes else { 3424b88c807SRodney W. Grimes n2->nif.elsepart = NULL; 3434b88c807SRodney W. Grimes tokpushback++; 3444b88c807SRodney W. Grimes } 3454b88c807SRodney W. Grimes if (readtoken() != TFI) 3464b88c807SRodney W. Grimes synexpect(TFI); 3474b88c807SRodney W. Grimes checkkwd = 1; 3484b88c807SRodney W. Grimes break; 3494b88c807SRodney W. Grimes case TWHILE: 3504b88c807SRodney W. Grimes case TUNTIL: { 3514b88c807SRodney W. Grimes int got; 3524b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nbinary)); 3534b88c807SRodney W. Grimes n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 354427748f7STim J. Robbins if ((n1->nbinary.ch1 = list(0)) == NULL) 355427748f7STim J. Robbins synexpect(-1); 3564b88c807SRodney W. Grimes if ((got=readtoken()) != TDO) { 3574b88c807SRodney W. Grimes TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 3584b88c807SRodney W. Grimes synexpect(TDO); 3594b88c807SRodney W. Grimes } 3604b88c807SRodney W. Grimes n1->nbinary.ch2 = list(0); 3614b88c807SRodney W. Grimes if (readtoken() != TDONE) 3624b88c807SRodney W. Grimes synexpect(TDONE); 3634b88c807SRodney W. Grimes checkkwd = 1; 3644b88c807SRodney W. Grimes break; 3654b88c807SRodney W. Grimes } 3664b88c807SRodney W. Grimes case TFOR: 3674b88c807SRodney W. Grimes if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 3684b88c807SRodney W. Grimes synerror("Bad for loop variable"); 3694b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nfor)); 3704b88c807SRodney W. Grimes n1->type = NFOR; 3714b88c807SRodney W. Grimes n1->nfor.var = wordtext; 3724b88c807SRodney W. Grimes if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) { 3734b88c807SRodney W. Grimes app = ≈ 3744b88c807SRodney W. Grimes while (readtoken() == TWORD) { 3754b88c807SRodney W. Grimes n2 = (union node *)stalloc(sizeof (struct narg)); 3764b88c807SRodney W. Grimes n2->type = NARG; 3774b88c807SRodney W. Grimes n2->narg.text = wordtext; 3784b88c807SRodney W. Grimes n2->narg.backquote = backquotelist; 3794b88c807SRodney W. Grimes *app = n2; 3804b88c807SRodney W. Grimes app = &n2->narg.next; 3814b88c807SRodney W. Grimes } 3824b88c807SRodney W. Grimes *app = NULL; 3834b88c807SRodney W. Grimes n1->nfor.args = ap; 3844b88c807SRodney W. Grimes if (lasttoken != TNL && lasttoken != TSEMI) 3854b88c807SRodney W. Grimes synexpect(-1); 3864b88c807SRodney W. Grimes } else { 3874b88c807SRodney W. Grimes #ifndef GDB_HACK 3884b88c807SRodney W. Grimes static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, 3894b88c807SRodney W. Grimes '@', '=', '\0'}; 3904b88c807SRodney W. Grimes #endif 3914b88c807SRodney W. Grimes n2 = (union node *)stalloc(sizeof (struct narg)); 3924b88c807SRodney W. Grimes n2->type = NARG; 3934b88c807SRodney W. Grimes n2->narg.text = (char *)argvars; 3944b88c807SRodney W. Grimes n2->narg.backquote = NULL; 3954b88c807SRodney W. Grimes n2->narg.next = NULL; 3964b88c807SRodney W. Grimes n1->nfor.args = n2; 3974b88c807SRodney W. Grimes /* 3984b88c807SRodney W. Grimes * Newline or semicolon here is optional (but note 3994b88c807SRodney W. Grimes * that the original Bourne shell only allowed NL). 4004b88c807SRodney W. Grimes */ 4014b88c807SRodney W. Grimes if (lasttoken != TNL && lasttoken != TSEMI) 4024b88c807SRodney W. Grimes tokpushback++; 4034b88c807SRodney W. Grimes } 4044b88c807SRodney W. Grimes checkkwd = 2; 4054b88c807SRodney W. Grimes if ((t = readtoken()) == TDO) 4064b88c807SRodney W. Grimes t = TDONE; 4074b88c807SRodney W. Grimes else if (t == TBEGIN) 4084b88c807SRodney W. Grimes t = TEND; 4094b88c807SRodney W. Grimes else 4104b88c807SRodney W. Grimes synexpect(-1); 4114b88c807SRodney W. Grimes n1->nfor.body = list(0); 4124b88c807SRodney W. Grimes if (readtoken() != t) 4134b88c807SRodney W. Grimes synexpect(t); 4144b88c807SRodney W. Grimes checkkwd = 1; 4154b88c807SRodney W. Grimes break; 4164b88c807SRodney W. Grimes case TCASE: 4174b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct ncase)); 4184b88c807SRodney W. Grimes n1->type = NCASE; 4194b88c807SRodney W. Grimes if (readtoken() != TWORD) 4204b88c807SRodney W. Grimes synexpect(TWORD); 4214b88c807SRodney W. Grimes n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 4224b88c807SRodney W. Grimes n2->type = NARG; 4234b88c807SRodney W. Grimes n2->narg.text = wordtext; 4244b88c807SRodney W. Grimes n2->narg.backquote = backquotelist; 4254b88c807SRodney W. Grimes n2->narg.next = NULL; 4264b88c807SRodney W. Grimes while (readtoken() == TNL); 4274b88c807SRodney W. Grimes if (lasttoken != TWORD || ! equal(wordtext, "in")) 4284b88c807SRodney W. Grimes synerror("expecting \"in\""); 4294b88c807SRodney W. Grimes cpp = &n1->ncase.cases; 4304417f629SPeter Wemm noaliases = 1; /* turn off alias expansion */ 431650488feSSean Eric Fagan checkkwd = 2, readtoken(); 432e00e16adSTim J. Robbins while (lasttoken != TESAC) { 4334b88c807SRodney W. Grimes *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 4344b88c807SRodney W. Grimes cp->type = NCLIST; 4354b88c807SRodney W. Grimes app = &cp->nclist.pattern; 436f7a9b7feSTim J. Robbins if (lasttoken == TLP) 437f7a9b7feSTim J. Robbins readtoken(); 4384b88c807SRodney W. Grimes for (;;) { 4394b88c807SRodney W. Grimes *app = ap = (union node *)stalloc(sizeof (struct narg)); 4404b88c807SRodney W. Grimes ap->type = NARG; 4414b88c807SRodney W. Grimes ap->narg.text = wordtext; 4424b88c807SRodney W. Grimes ap->narg.backquote = backquotelist; 443650488feSSean Eric Fagan if (checkkwd = 2, readtoken() != TPIPE) 4444b88c807SRodney W. Grimes break; 4454b88c807SRodney W. Grimes app = &ap->narg.next; 446650488feSSean Eric Fagan readtoken(); 4474b88c807SRodney W. Grimes } 4484b88c807SRodney W. Grimes ap->narg.next = NULL; 4494b88c807SRodney W. Grimes if (lasttoken != TRP) 4504417f629SPeter Wemm noaliases = 0, synexpect(TRP); 4514b88c807SRodney W. Grimes cp->nclist.body = list(0); 452650488feSSean Eric Fagan 453650488feSSean Eric Fagan checkkwd = 2; 454650488feSSean Eric Fagan if ((t = readtoken()) != TESAC) { 455650488feSSean Eric Fagan if (t != TENDCASE) 4564417f629SPeter Wemm noaliases = 0, synexpect(TENDCASE); 457650488feSSean Eric Fagan else 458650488feSSean Eric Fagan checkkwd = 2, readtoken(); 4594b88c807SRodney W. Grimes } 460650488feSSean Eric Fagan cpp = &cp->nclist.next; 461e00e16adSTim J. Robbins } 4624417f629SPeter Wemm noaliases = 0; /* reset alias expansion */ 4634b88c807SRodney W. Grimes *cpp = NULL; 4644b88c807SRodney W. Grimes checkkwd = 1; 4654b88c807SRodney W. Grimes break; 4664b88c807SRodney W. Grimes case TLP: 4674b88c807SRodney W. Grimes n1 = (union node *)stalloc(sizeof (struct nredir)); 4684b88c807SRodney W. Grimes n1->type = NSUBSHELL; 4694b88c807SRodney W. Grimes n1->nredir.n = list(0); 4704b88c807SRodney W. Grimes n1->nredir.redirect = NULL; 4714b88c807SRodney W. Grimes if (readtoken() != TRP) 4724b88c807SRodney W. Grimes synexpect(TRP); 4734b88c807SRodney W. Grimes checkkwd = 1; 4744b88c807SRodney W. Grimes break; 4754b88c807SRodney W. Grimes case TBEGIN: 4764b88c807SRodney W. Grimes n1 = list(0); 4774b88c807SRodney W. Grimes if (readtoken() != TEND) 4784b88c807SRodney W. Grimes synexpect(TEND); 4794b88c807SRodney W. Grimes checkkwd = 1; 4804b88c807SRodney W. Grimes break; 4814b88c807SRodney W. Grimes /* Handle an empty command like other simple commands. */ 482248ffae5SJoerg Wunsch case TSEMI: 483d8d737d7STim J. Robbins case TAND: 484d8d737d7STim J. Robbins case TOR: 485aa9caaf6SPeter Wemm /* 486aa9caaf6SPeter Wemm * An empty command before a ; doesn't make much sense, and 487aa9caaf6SPeter Wemm * should certainly be disallowed in the case of `if ;'. 488aa9caaf6SPeter Wemm */ 489aa9caaf6SPeter Wemm if (!redir) 490aa9caaf6SPeter Wemm synexpect(-1); 491aa9caaf6SPeter Wemm case TNL: 492248ffae5SJoerg Wunsch case TEOF: 4934b88c807SRodney W. Grimes case TWORD: 494aa9caaf6SPeter Wemm case TRP: 4954b88c807SRodney W. Grimes tokpushback++; 4966c0bde79SBrian Somers n1 = simplecmd(rpp, redir); 4976c0bde79SBrian Somers goto checkneg; 4984b88c807SRodney W. Grimes default: 4994b88c807SRodney W. Grimes synexpect(-1); 5004b88c807SRodney W. Grimes } 5014b88c807SRodney W. Grimes 5024b88c807SRodney W. Grimes /* Now check for redirection which may follow command */ 5034b88c807SRodney W. Grimes while (readtoken() == TREDIR) { 5044b88c807SRodney W. Grimes *rpp = n2 = redirnode; 5054b88c807SRodney W. Grimes rpp = &n2->nfile.next; 5064b88c807SRodney W. Grimes parsefname(); 5074b88c807SRodney W. Grimes } 5084b88c807SRodney W. Grimes tokpushback++; 5094b88c807SRodney W. Grimes *rpp = NULL; 5104b88c807SRodney W. Grimes if (redir) { 5114b88c807SRodney W. Grimes if (n1->type != NSUBSHELL) { 5124b88c807SRodney W. Grimes n2 = (union node *)stalloc(sizeof (struct nredir)); 5134b88c807SRodney W. Grimes n2->type = NREDIR; 5144b88c807SRodney W. Grimes n2->nredir.n = n1; 5154b88c807SRodney W. Grimes n1 = n2; 5164b88c807SRodney W. Grimes } 5174b88c807SRodney W. Grimes n1->nredir.redirect = redir; 5184b88c807SRodney W. Grimes } 5196c0bde79SBrian Somers 5206c0bde79SBrian Somers checkneg: 5216c0bde79SBrian Somers if (negate) { 5226c0bde79SBrian Somers n2 = (union node *)stalloc(sizeof (struct nnot)); 5236c0bde79SBrian Somers n2->type = NNOT; 5246c0bde79SBrian Somers n2->nnot.com = n1; 5256c0bde79SBrian Somers return n2; 5266c0bde79SBrian Somers } 5276c0bde79SBrian Somers else 5284b88c807SRodney W. Grimes return n1; 5294b88c807SRodney W. Grimes } 5304b88c807SRodney W. Grimes 5314b88c807SRodney W. Grimes 5324b88c807SRodney W. Grimes STATIC union node * 5335134c3f7SWarner Losh simplecmd(union node **rpp, union node *redir) 5344b88c807SRodney W. Grimes { 5354b88c807SRodney W. Grimes union node *args, **app; 5364b88c807SRodney W. Grimes union node **orig_rpp = rpp; 5376c0bde79SBrian Somers union node *n = NULL, *n2; 5386c0bde79SBrian Somers int negate = 0; 5394b88c807SRodney W. Grimes 5404b88c807SRodney W. Grimes /* If we don't have any redirections already, then we must reset */ 5414b88c807SRodney W. Grimes /* rpp to be the address of the local redir variable. */ 5424b88c807SRodney W. Grimes if (redir == 0) 5434b88c807SRodney W. Grimes rpp = &redir; 5444b88c807SRodney W. Grimes 5454b88c807SRodney W. Grimes args = NULL; 5464b88c807SRodney W. Grimes app = &args; 5474b88c807SRodney W. Grimes /* 5484b88c807SRodney W. Grimes * We save the incoming value, because we need this for shell 5494b88c807SRodney W. Grimes * functions. There can not be a redirect or an argument between 5504b88c807SRodney W. Grimes * the function name and the open parenthesis. 5514b88c807SRodney W. Grimes */ 5524b88c807SRodney W. Grimes orig_rpp = rpp; 5534b88c807SRodney W. Grimes 5546c0bde79SBrian Somers while (readtoken() == TNOT) { 5556c0bde79SBrian Somers TRACE(("command: TNOT recognized\n")); 5566c0bde79SBrian Somers negate = !negate; 5576c0bde79SBrian Somers } 5586c0bde79SBrian Somers tokpushback++; 5596c0bde79SBrian Somers 5604b88c807SRodney W. Grimes for (;;) { 5614b88c807SRodney W. Grimes if (readtoken() == TWORD) { 5624b88c807SRodney W. Grimes n = (union node *)stalloc(sizeof (struct narg)); 5634b88c807SRodney W. Grimes n->type = NARG; 5644b88c807SRodney W. Grimes n->narg.text = wordtext; 5654b88c807SRodney W. Grimes n->narg.backquote = backquotelist; 5664b88c807SRodney W. Grimes *app = n; 5674b88c807SRodney W. Grimes app = &n->narg.next; 5684b88c807SRodney W. Grimes } else if (lasttoken == TREDIR) { 5694b88c807SRodney W. Grimes *rpp = n = redirnode; 5704b88c807SRodney W. Grimes rpp = &n->nfile.next; 5714b88c807SRodney W. Grimes parsefname(); /* read name of redirection file */ 5724b88c807SRodney W. Grimes } else if (lasttoken == TLP && app == &args->narg.next 5734b88c807SRodney W. Grimes && rpp == orig_rpp) { 5744b88c807SRodney W. Grimes /* We have a function */ 5754b88c807SRodney W. Grimes if (readtoken() != TRP) 5764b88c807SRodney W. Grimes synexpect(TRP); 5774b88c807SRodney W. Grimes #ifdef notdef 5784b88c807SRodney W. Grimes if (! goodname(n->narg.text)) 5794b88c807SRodney W. Grimes synerror("Bad function name"); 5804b88c807SRodney W. Grimes #endif 5814b88c807SRodney W. Grimes n->type = NDEFUN; 5824b88c807SRodney W. Grimes n->narg.next = command(); 5836c0bde79SBrian Somers goto checkneg; 5844b88c807SRodney W. Grimes } else { 5854b88c807SRodney W. Grimes tokpushback++; 5864b88c807SRodney W. Grimes break; 5874b88c807SRodney W. Grimes } 5884b88c807SRodney W. Grimes } 5894b88c807SRodney W. Grimes *app = NULL; 5904b88c807SRodney W. Grimes *rpp = NULL; 5914b88c807SRodney W. Grimes n = (union node *)stalloc(sizeof (struct ncmd)); 5924b88c807SRodney W. Grimes n->type = NCMD; 5934b88c807SRodney W. Grimes n->ncmd.backgnd = 0; 5944b88c807SRodney W. Grimes n->ncmd.args = args; 5954b88c807SRodney W. Grimes n->ncmd.redirect = redir; 5966c0bde79SBrian Somers 5976c0bde79SBrian Somers checkneg: 5986c0bde79SBrian Somers if (negate) { 5996c0bde79SBrian Somers n2 = (union node *)stalloc(sizeof (struct nnot)); 6006c0bde79SBrian Somers n2->type = NNOT; 6016c0bde79SBrian Somers n2->nnot.com = n; 6026c0bde79SBrian Somers return n2; 6036c0bde79SBrian Somers } 6046c0bde79SBrian Somers else 6054b88c807SRodney W. Grimes return n; 6064b88c807SRodney W. Grimes } 6074b88c807SRodney W. Grimes 608aa9caaf6SPeter Wemm STATIC union node * 6095134c3f7SWarner Losh makename(void) 6105134c3f7SWarner Losh { 611aa9caaf6SPeter Wemm union node *n; 612aa9caaf6SPeter Wemm 613aa9caaf6SPeter Wemm n = (union node *)stalloc(sizeof (struct narg)); 614aa9caaf6SPeter Wemm n->type = NARG; 615aa9caaf6SPeter Wemm n->narg.next = NULL; 616aa9caaf6SPeter Wemm n->narg.text = wordtext; 617aa9caaf6SPeter Wemm n->narg.backquote = backquotelist; 618aa9caaf6SPeter Wemm return n; 619aa9caaf6SPeter Wemm } 620aa9caaf6SPeter Wemm 6215134c3f7SWarner Losh void fixredir(union node *n, const char *text, int err) 622aa9caaf6SPeter Wemm { 623aa9caaf6SPeter Wemm TRACE(("Fix redir %s %d\n", text, err)); 624aa9caaf6SPeter Wemm if (!err) 625aa9caaf6SPeter Wemm n->ndup.vname = NULL; 626aa9caaf6SPeter Wemm 627aa9caaf6SPeter Wemm if (is_digit(text[0]) && text[1] == '\0') 628aa9caaf6SPeter Wemm n->ndup.dupfd = digit_val(text[0]); 629aa9caaf6SPeter Wemm else if (text[0] == '-' && text[1] == '\0') 630aa9caaf6SPeter Wemm n->ndup.dupfd = -1; 631aa9caaf6SPeter Wemm else { 632aa9caaf6SPeter Wemm 633aa9caaf6SPeter Wemm if (err) 634aa9caaf6SPeter Wemm synerror("Bad fd number"); 635aa9caaf6SPeter Wemm else 636aa9caaf6SPeter Wemm n->ndup.vname = makename(); 637aa9caaf6SPeter Wemm } 638aa9caaf6SPeter Wemm } 639aa9caaf6SPeter Wemm 6404b88c807SRodney W. Grimes 6414b88c807SRodney W. Grimes STATIC void 6425134c3f7SWarner Losh parsefname(void) 6435134c3f7SWarner Losh { 6444b88c807SRodney W. Grimes union node *n = redirnode; 6454b88c807SRodney W. Grimes 6464b88c807SRodney W. Grimes if (readtoken() != TWORD) 6474b88c807SRodney W. Grimes synexpect(-1); 6484b88c807SRodney W. Grimes if (n->type == NHERE) { 6494b88c807SRodney W. Grimes struct heredoc *here = heredoc; 6504b88c807SRodney W. Grimes struct heredoc *p; 6514b88c807SRodney W. Grimes int i; 6524b88c807SRodney W. Grimes 6534b88c807SRodney W. Grimes if (quoteflag == 0) 6544b88c807SRodney W. Grimes n->type = NXHERE; 6554b88c807SRodney W. Grimes TRACE(("Here document %d\n", n->type)); 6564b88c807SRodney W. Grimes if (here->striptabs) { 6574b88c807SRodney W. Grimes while (*wordtext == '\t') 6584b88c807SRodney W. Grimes wordtext++; 6594b88c807SRodney W. Grimes } 6604b88c807SRodney W. Grimes if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 6614b88c807SRodney W. Grimes synerror("Illegal eof marker for << redirection"); 6624b88c807SRodney W. Grimes rmescapes(wordtext); 6634b88c807SRodney W. Grimes here->eofmark = wordtext; 6644b88c807SRodney W. Grimes here->next = NULL; 6654b88c807SRodney W. Grimes if (heredoclist == NULL) 6664b88c807SRodney W. Grimes heredoclist = here; 6674b88c807SRodney W. Grimes else { 6684b88c807SRodney W. Grimes for (p = heredoclist ; p->next ; p = p->next); 6694b88c807SRodney W. Grimes p->next = here; 6704b88c807SRodney W. Grimes } 6714b88c807SRodney W. Grimes } else if (n->type == NTOFD || n->type == NFROMFD) { 672aa9caaf6SPeter Wemm fixredir(n, wordtext, 0); 6734b88c807SRodney W. Grimes } else { 674aa9caaf6SPeter Wemm n->nfile.fname = makename(); 6754b88c807SRodney W. Grimes } 6764b88c807SRodney W. Grimes } 6774b88c807SRodney W. Grimes 6784b88c807SRodney W. Grimes 6794b88c807SRodney W. Grimes /* 6804b88c807SRodney W. Grimes * Input any here documents. 6814b88c807SRodney W. Grimes */ 6824b88c807SRodney W. Grimes 6834b88c807SRodney W. Grimes STATIC void 6845134c3f7SWarner Losh parseheredoc(void) 6855134c3f7SWarner Losh { 6864b88c807SRodney W. Grimes struct heredoc *here; 6874b88c807SRodney W. Grimes union node *n; 6884b88c807SRodney W. Grimes 6894b88c807SRodney W. Grimes while (heredoclist) { 6904b88c807SRodney W. Grimes here = heredoclist; 6914b88c807SRodney W. Grimes heredoclist = here->next; 6924b88c807SRodney W. Grimes if (needprompt) { 6934b88c807SRodney W. Grimes setprompt(2); 6944b88c807SRodney W. Grimes needprompt = 0; 6954b88c807SRodney W. Grimes } 6964b88c807SRodney W. Grimes readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 6974b88c807SRodney W. Grimes here->eofmark, here->striptabs); 6984b88c807SRodney W. Grimes n = (union node *)stalloc(sizeof (struct narg)); 6994b88c807SRodney W. Grimes n->narg.type = NARG; 7004b88c807SRodney W. Grimes n->narg.next = NULL; 7014b88c807SRodney W. Grimes n->narg.text = wordtext; 7024b88c807SRodney W. Grimes n->narg.backquote = backquotelist; 7034b88c807SRodney W. Grimes here->here->nhere.doc = n; 7044b88c807SRodney W. Grimes } 7054b88c807SRodney W. Grimes } 7064b88c807SRodney W. Grimes 7074b88c807SRodney W. Grimes STATIC int 7085134c3f7SWarner Losh peektoken(void) 7095134c3f7SWarner Losh { 7104b88c807SRodney W. Grimes int t; 7114b88c807SRodney W. Grimes 7124b88c807SRodney W. Grimes t = readtoken(); 7134b88c807SRodney W. Grimes tokpushback++; 7144b88c807SRodney W. Grimes return (t); 7154b88c807SRodney W. Grimes } 7164b88c807SRodney W. Grimes 7174b88c807SRodney W. Grimes STATIC int 7185134c3f7SWarner Losh readtoken(void) 7195134c3f7SWarner Losh { 7204b88c807SRodney W. Grimes int t; 7214b88c807SRodney W. Grimes int savecheckkwd = checkkwd; 7224b88c807SRodney W. Grimes struct alias *ap; 7234b88c807SRodney W. Grimes #ifdef DEBUG 7244b88c807SRodney W. Grimes int alreadyseen = tokpushback; 7254b88c807SRodney W. Grimes #endif 7264b88c807SRodney W. Grimes 7274b88c807SRodney W. Grimes top: 7284b88c807SRodney W. Grimes t = xxreadtoken(); 7294b88c807SRodney W. Grimes 7304b88c807SRodney W. Grimes if (checkkwd) { 7314b88c807SRodney W. Grimes /* 7324b88c807SRodney W. Grimes * eat newlines 7334b88c807SRodney W. Grimes */ 7344b88c807SRodney W. Grimes if (checkkwd == 2) { 7354b88c807SRodney W. Grimes checkkwd = 0; 7364b88c807SRodney W. Grimes while (t == TNL) { 7374b88c807SRodney W. Grimes parseheredoc(); 7384b88c807SRodney W. Grimes t = xxreadtoken(); 7394b88c807SRodney W. Grimes } 7404b88c807SRodney W. Grimes } else 7414b88c807SRodney W. Grimes checkkwd = 0; 7424b88c807SRodney W. Grimes /* 7434b88c807SRodney W. Grimes * check for keywords and aliases 7444b88c807SRodney W. Grimes */ 745aa9caaf6SPeter Wemm if (t == TWORD && !quoteflag) 746aa9caaf6SPeter Wemm { 7478b7808bcSJuli Mallett const char * const *pp; 7484b88c807SRodney W. Grimes 7498b7808bcSJuli Mallett for (pp = parsekwd; *pp; pp++) { 750aa9caaf6SPeter Wemm if (**pp == *wordtext && equal(*pp, wordtext)) 751aa9caaf6SPeter Wemm { 7524b88c807SRodney W. Grimes lasttoken = t = pp - parsekwd + KWDOFFSET; 7534b88c807SRodney W. Grimes TRACE(("keyword %s recognized\n", tokname[t])); 7544b88c807SRodney W. Grimes goto out; 7554b88c807SRodney W. Grimes } 7564b88c807SRodney W. Grimes } 7574417f629SPeter Wemm if (noaliases == 0 && 7584417f629SPeter Wemm (ap = lookupalias(wordtext, 1)) != NULL) { 7594b88c807SRodney W. Grimes pushstring(ap->val, strlen(ap->val), ap); 7604b88c807SRodney W. Grimes checkkwd = savecheckkwd; 7614b88c807SRodney W. Grimes goto top; 7624b88c807SRodney W. Grimes } 7634b88c807SRodney W. Grimes } 7644b88c807SRodney W. Grimes out: 7656c0bde79SBrian Somers checkkwd = (t == TNOT) ? savecheckkwd : 0; 7664b88c807SRodney W. Grimes } 7674b88c807SRodney W. Grimes #ifdef DEBUG 7684b88c807SRodney W. Grimes if (!alreadyseen) 7694b88c807SRodney W. Grimes TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7704b88c807SRodney W. Grimes else 7714b88c807SRodney W. Grimes TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 7724b88c807SRodney W. Grimes #endif 7734b88c807SRodney W. Grimes return (t); 7744b88c807SRodney W. Grimes } 7754b88c807SRodney W. Grimes 7764b88c807SRodney W. Grimes 7774b88c807SRodney W. Grimes /* 7784b88c807SRodney W. Grimes * Read the next input token. 7794b88c807SRodney W. Grimes * If the token is a word, we set backquotelist to the list of cmds in 7804b88c807SRodney W. Grimes * backquotes. We set quoteflag to true if any part of the word was 7814b88c807SRodney W. Grimes * quoted. 7824b88c807SRodney W. Grimes * If the token is TREDIR, then we set redirnode to a structure containing 7834b88c807SRodney W. Grimes * the redirection. 7844b88c807SRodney W. Grimes * In all cases, the variable startlinno is set to the number of the line 7854b88c807SRodney W. Grimes * on which the token starts. 7864b88c807SRodney W. Grimes * 7874b88c807SRodney W. Grimes * [Change comment: here documents and internal procedures] 7884b88c807SRodney W. Grimes * [Readtoken shouldn't have any arguments. Perhaps we should make the 7894b88c807SRodney W. Grimes * word parsing code into a separate routine. In this case, readtoken 7904b88c807SRodney W. Grimes * doesn't need to have any internal procedures, but parseword does. 7914b88c807SRodney W. Grimes * We could also make parseoperator in essence the main routine, and 7924b88c807SRodney W. Grimes * have parseword (readtoken1?) handle both words and redirection.] 7934b88c807SRodney W. Grimes */ 7944b88c807SRodney W. Grimes 7954b88c807SRodney W. Grimes #define RETURN(token) return lasttoken = token 7964b88c807SRodney W. Grimes 7974b88c807SRodney W. Grimes STATIC int 7985134c3f7SWarner Losh xxreadtoken(void) 7995134c3f7SWarner Losh { 8007920a31dSSteve Price int c; 8014b88c807SRodney W. Grimes 8024b88c807SRodney W. Grimes if (tokpushback) { 8034b88c807SRodney W. Grimes tokpushback = 0; 8044b88c807SRodney W. Grimes return lasttoken; 8054b88c807SRodney W. Grimes } 8064b88c807SRodney W. Grimes if (needprompt) { 8074b88c807SRodney W. Grimes setprompt(2); 8084b88c807SRodney W. Grimes needprompt = 0; 8094b88c807SRodney W. Grimes } 8104b88c807SRodney W. Grimes startlinno = plinno; 8114b88c807SRodney W. Grimes for (;;) { /* until token or start of word found */ 8124b88c807SRodney W. Grimes c = pgetc_macro(); 8134b88c807SRodney W. Grimes if (c == ' ' || c == '\t') 8144b88c807SRodney W. Grimes continue; /* quick check for white space first */ 8154b88c807SRodney W. Grimes switch (c) { 8164b88c807SRodney W. Grimes case ' ': case '\t': 8174b88c807SRodney W. Grimes continue; 8184b88c807SRodney W. Grimes case '#': 8194b88c807SRodney W. Grimes while ((c = pgetc()) != '\n' && c != PEOF); 8204b88c807SRodney W. Grimes pungetc(); 8214b88c807SRodney W. Grimes continue; 8224b88c807SRodney W. Grimes case '\\': 8234b88c807SRodney W. Grimes if (pgetc() == '\n') { 8244b88c807SRodney W. Grimes startlinno = ++plinno; 8254b88c807SRodney W. Grimes if (doprompt) 8264b88c807SRodney W. Grimes setprompt(2); 8274b88c807SRodney W. Grimes else 8284b88c807SRodney W. Grimes setprompt(0); 8294b88c807SRodney W. Grimes continue; 8304b88c807SRodney W. Grimes } 8314b88c807SRodney W. Grimes pungetc(); 8324b88c807SRodney W. Grimes goto breakloop; 8334b88c807SRodney W. Grimes case '\n': 8344b88c807SRodney W. Grimes plinno++; 8354b88c807SRodney W. Grimes needprompt = doprompt; 8364b88c807SRodney W. Grimes RETURN(TNL); 8374b88c807SRodney W. Grimes case PEOF: 8384b88c807SRodney W. Grimes RETURN(TEOF); 8394b88c807SRodney W. Grimes case '&': 8404b88c807SRodney W. Grimes if (pgetc() == '&') 8414b88c807SRodney W. Grimes RETURN(TAND); 8424b88c807SRodney W. Grimes pungetc(); 8434b88c807SRodney W. Grimes RETURN(TBACKGND); 8444b88c807SRodney W. Grimes case '|': 8454b88c807SRodney W. Grimes if (pgetc() == '|') 8464b88c807SRodney W. Grimes RETURN(TOR); 8474b88c807SRodney W. Grimes pungetc(); 8484b88c807SRodney W. Grimes RETURN(TPIPE); 8494b88c807SRodney W. Grimes case ';': 8504b88c807SRodney W. Grimes if (pgetc() == ';') 8514b88c807SRodney W. Grimes RETURN(TENDCASE); 8524b88c807SRodney W. Grimes pungetc(); 8534b88c807SRodney W. Grimes RETURN(TSEMI); 8544b88c807SRodney W. Grimes case '(': 8554b88c807SRodney W. Grimes RETURN(TLP); 8564b88c807SRodney W. Grimes case ')': 8574b88c807SRodney W. Grimes RETURN(TRP); 8584b88c807SRodney W. Grimes default: 8594b88c807SRodney W. Grimes goto breakloop; 8604b88c807SRodney W. Grimes } 8614b88c807SRodney W. Grimes } 8624b88c807SRodney W. Grimes breakloop: 8634b88c807SRodney W. Grimes return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 8644b88c807SRodney W. Grimes #undef RETURN 8654b88c807SRodney W. Grimes } 8664b88c807SRodney W. Grimes 8674b88c807SRodney W. Grimes 8684b88c807SRodney W. Grimes 8694b88c807SRodney W. Grimes /* 8704b88c807SRodney W. Grimes * If eofmark is NULL, read a word or a redirection symbol. If eofmark 8714b88c807SRodney W. Grimes * is not NULL, read a here document. In the latter case, eofmark is the 8724b88c807SRodney W. Grimes * word which marks the end of the document and striptabs is true if 8734b88c807SRodney W. Grimes * leading tabs should be stripped from the document. The argument firstc 8744b88c807SRodney W. Grimes * is the first character of the input token or document. 8754b88c807SRodney W. Grimes * 8764b88c807SRodney W. Grimes * Because C does not have internal subroutines, I have simulated them 8774b88c807SRodney W. Grimes * using goto's to implement the subroutine linkage. The following macros 8784b88c807SRodney W. Grimes * will run code that appears at the end of readtoken1. 8794b88c807SRodney W. Grimes */ 8804b88c807SRodney W. Grimes 8814b88c807SRodney W. Grimes #define CHECKEND() {goto checkend; checkend_return:;} 8824b88c807SRodney W. Grimes #define PARSEREDIR() {goto parseredir; parseredir_return:;} 8834b88c807SRodney W. Grimes #define PARSESUB() {goto parsesub; parsesub_return:;} 8844b88c807SRodney W. Grimes #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 8854b88c807SRodney W. Grimes #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 8864b88c807SRodney W. Grimes #define PARSEARITH() {goto parsearith; parsearith_return:;} 8874b88c807SRodney W. Grimes 8884b88c807SRodney W. Grimes STATIC int 8895134c3f7SWarner Losh readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs) 8904b88c807SRodney W. Grimes { 891aa9caaf6SPeter Wemm int c = firstc; 892aa9caaf6SPeter Wemm char *out; 8934b88c807SRodney W. Grimes int len; 8944b88c807SRodney W. Grimes char line[EOFMARKLEN + 1]; 8954b88c807SRodney W. Grimes struct nodelist *bqlist; 8964b88c807SRodney W. Grimes int quotef; 8974b88c807SRodney W. Grimes int dblquote; 8984b88c807SRodney W. Grimes int varnest; /* levels of variables expansion */ 8994b88c807SRodney W. Grimes int arinest; /* levels of arithmetic expansion */ 9004b88c807SRodney W. Grimes int parenlevel; /* levels of parens in arithmetic */ 9014b88c807SRodney W. Grimes int oldstyle; 9024b88c807SRodney W. Grimes char const *prevsyntax; /* syntax before arithmetic */ 9032dde9ce3SMartin Cracauer int synentry; 904aa9caaf6SPeter Wemm #if __GNUC__ 905aa9caaf6SPeter Wemm /* Avoid longjmp clobbering */ 906aa9caaf6SPeter Wemm (void) &out; 907aa9caaf6SPeter Wemm (void) "ef; 908aa9caaf6SPeter Wemm (void) &dblquote; 909aa9caaf6SPeter Wemm (void) &varnest; 910aa9caaf6SPeter Wemm (void) &arinest; 911aa9caaf6SPeter Wemm (void) &parenlevel; 912aa9caaf6SPeter Wemm (void) &oldstyle; 913aa9caaf6SPeter Wemm (void) &prevsyntax; 914aa9caaf6SPeter Wemm (void) &syntax; 9152dde9ce3SMartin Cracauer (void) &synentry; 916aa9caaf6SPeter Wemm #endif 9174b88c807SRodney W. Grimes 9184b88c807SRodney W. Grimes startlinno = plinno; 9194b88c807SRodney W. Grimes dblquote = 0; 9204b88c807SRodney W. Grimes if (syntax == DQSYNTAX) 9214b88c807SRodney W. Grimes dblquote = 1; 9224b88c807SRodney W. Grimes quotef = 0; 9234b88c807SRodney W. Grimes bqlist = NULL; 9244b88c807SRodney W. Grimes varnest = 0; 9254b88c807SRodney W. Grimes arinest = 0; 9264b88c807SRodney W. Grimes parenlevel = 0; 9274b88c807SRodney W. Grimes 9284b88c807SRodney W. Grimes STARTSTACKSTR(out); 9294b88c807SRodney W. Grimes loop: { /* for each line, until end of word */ 9304b88c807SRodney W. Grimes CHECKEND(); /* set c to PEOF if at end of here document */ 9314b88c807SRodney W. Grimes for (;;) { /* until end of line or end of word */ 9324b88c807SRodney W. Grimes CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 9332dde9ce3SMartin Cracauer 9342dde9ce3SMartin Cracauer synentry = syntax[c]; 9352dde9ce3SMartin Cracauer 9362dde9ce3SMartin Cracauer switch(synentry) { 9374b88c807SRodney W. Grimes case CNL: /* '\n' */ 9384b88c807SRodney W. Grimes if (syntax == BASESYNTAX) 9394b88c807SRodney W. Grimes goto endword; /* exit outer loop */ 9404b88c807SRodney W. Grimes USTPUTC(c, out); 9414b88c807SRodney W. Grimes plinno++; 9424b88c807SRodney W. Grimes if (doprompt) 9434b88c807SRodney W. Grimes setprompt(2); 9444b88c807SRodney W. Grimes else 9454b88c807SRodney W. Grimes setprompt(0); 9464b88c807SRodney W. Grimes c = pgetc(); 9474b88c807SRodney W. Grimes goto loop; /* continue outer loop */ 9484b88c807SRodney W. Grimes case CWORD: 9494b88c807SRodney W. Grimes USTPUTC(c, out); 9504b88c807SRodney W. Grimes break; 9514b88c807SRodney W. Grimes case CCTL: 9524b88c807SRodney W. Grimes if (eofmark == NULL || dblquote) 9534b88c807SRodney W. Grimes USTPUTC(CTLESC, out); 9544b88c807SRodney W. Grimes USTPUTC(c, out); 9554b88c807SRodney W. Grimes break; 9564b88c807SRodney W. Grimes case CBACK: /* backslash */ 9574b88c807SRodney W. Grimes c = pgetc(); 9584b88c807SRodney W. Grimes if (c == PEOF) { 9594b88c807SRodney W. Grimes USTPUTC('\\', out); 9604b88c807SRodney W. Grimes pungetc(); 9614b88c807SRodney W. Grimes } else if (c == '\n') { 9624b88c807SRodney W. Grimes if (doprompt) 9634b88c807SRodney W. Grimes setprompt(2); 9644b88c807SRodney W. Grimes else 9654b88c807SRodney W. Grimes setprompt(0); 9664b88c807SRodney W. Grimes } else { 96773f612b5SMartin Cracauer if (dblquote && c != '\\' && 96873f612b5SMartin Cracauer c != '`' && c != '$' && 96973f612b5SMartin Cracauer (c != '"' || eofmark != NULL)) 9704b88c807SRodney W. Grimes USTPUTC('\\', out); 9710c4eeddaSTor Egge if (SQSYNTAX[c] == CCTL) 9724b88c807SRodney W. Grimes USTPUTC(CTLESC, out); 9735557a02aSTor Egge else if (eofmark == NULL) 9746f47734fSTor Egge USTPUTC(CTLQUOTEMARK, out); 9754b88c807SRodney W. Grimes USTPUTC(c, out); 9764b88c807SRodney W. Grimes quotef++; 9774b88c807SRodney W. Grimes } 9784b88c807SRodney W. Grimes break; 9794b88c807SRodney W. Grimes case CSQUOTE: 9805557a02aSTor Egge if (eofmark == NULL) 9816f47734fSTor Egge USTPUTC(CTLQUOTEMARK, out); 9824b88c807SRodney W. Grimes syntax = SQSYNTAX; 9834b88c807SRodney W. Grimes break; 9844b88c807SRodney W. Grimes case CDQUOTE: 9855557a02aSTor Egge if (eofmark == NULL) 9866f47734fSTor Egge USTPUTC(CTLQUOTEMARK, out); 9874b88c807SRodney W. Grimes syntax = DQSYNTAX; 9884b88c807SRodney W. Grimes dblquote = 1; 9894b88c807SRodney W. Grimes break; 9904b88c807SRodney W. Grimes case CENDQUOTE: 9915557a02aSTor Egge if (eofmark != NULL && arinest == 0 && 9925557a02aSTor Egge varnest == 0) { 9934b88c807SRodney W. Grimes USTPUTC(c, out); 9944b88c807SRodney W. Grimes } else { 9955557a02aSTor Egge if (arinest) { 9964b88c807SRodney W. Grimes syntax = ARISYNTAX; 9974b88c807SRodney W. Grimes dblquote = 0; 9985557a02aSTor Egge } else if (eofmark == NULL) { 9995557a02aSTor Egge syntax = BASESYNTAX; 10005557a02aSTor Egge dblquote = 0; 10015557a02aSTor Egge } 10025557a02aSTor Egge quotef++; 10034b88c807SRodney W. Grimes } 10044b88c807SRodney W. Grimes break; 10054b88c807SRodney W. Grimes case CVAR: /* '$' */ 10064b88c807SRodney W. Grimes PARSESUB(); /* parse substitution */ 10074b88c807SRodney W. Grimes break; 10084b88c807SRodney W. Grimes case CENDVAR: /* '}' */ 10094b88c807SRodney W. Grimes if (varnest > 0) { 10104b88c807SRodney W. Grimes varnest--; 10114b88c807SRodney W. Grimes USTPUTC(CTLENDVAR, out); 10124b88c807SRodney W. Grimes } else { 10134b88c807SRodney W. Grimes USTPUTC(c, out); 10144b88c807SRodney W. Grimes } 10154b88c807SRodney W. Grimes break; 10164b88c807SRodney W. Grimes case CLP: /* '(' in arithmetic */ 10174b88c807SRodney W. Grimes parenlevel++; 10184b88c807SRodney W. Grimes USTPUTC(c, out); 10194b88c807SRodney W. Grimes break; 10204b88c807SRodney W. Grimes case CRP: /* ')' in arithmetic */ 10214b88c807SRodney W. Grimes if (parenlevel > 0) { 10224b88c807SRodney W. Grimes USTPUTC(c, out); 10234b88c807SRodney W. Grimes --parenlevel; 10244b88c807SRodney W. Grimes } else { 10254b88c807SRodney W. Grimes if (pgetc() == ')') { 10264b88c807SRodney W. Grimes if (--arinest == 0) { 10274b88c807SRodney W. Grimes USTPUTC(CTLENDARI, out); 10284b88c807SRodney W. Grimes syntax = prevsyntax; 10295557a02aSTor Egge if (syntax == DQSYNTAX) 10305557a02aSTor Egge dblquote = 1; 10315557a02aSTor Egge else 10325557a02aSTor Egge dblquote = 0; 10334b88c807SRodney W. Grimes } else 10344b88c807SRodney W. Grimes USTPUTC(')', out); 10354b88c807SRodney W. Grimes } else { 10364b88c807SRodney W. Grimes /* 10374b88c807SRodney W. Grimes * unbalanced parens 10384b88c807SRodney W. Grimes * (don't 2nd guess - no error) 10394b88c807SRodney W. Grimes */ 10404b88c807SRodney W. Grimes pungetc(); 10414b88c807SRodney W. Grimes USTPUTC(')', out); 10424b88c807SRodney W. Grimes } 10434b88c807SRodney W. Grimes } 10444b88c807SRodney W. Grimes break; 10454b88c807SRodney W. Grimes case CBQUOTE: /* '`' */ 10464b88c807SRodney W. Grimes PARSEBACKQOLD(); 10474b88c807SRodney W. Grimes break; 10484b88c807SRodney W. Grimes case CEOF: 10494b88c807SRodney W. Grimes goto endword; /* exit outer loop */ 10504b88c807SRodney W. Grimes default: 10514b88c807SRodney W. Grimes if (varnest == 0) 10524b88c807SRodney W. Grimes goto endword; /* exit outer loop */ 10534b88c807SRodney W. Grimes USTPUTC(c, out); 10544b88c807SRodney W. Grimes } 10554b88c807SRodney W. Grimes c = pgetc_macro(); 10564b88c807SRodney W. Grimes } 10574b88c807SRodney W. Grimes } 10584b88c807SRodney W. Grimes endword: 10594b88c807SRodney W. Grimes if (syntax == ARISYNTAX) 10604b88c807SRodney W. Grimes synerror("Missing '))'"); 10614b88c807SRodney W. Grimes if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL) 10624b88c807SRodney W. Grimes synerror("Unterminated quoted string"); 10634b88c807SRodney W. Grimes if (varnest != 0) { 10644b88c807SRodney W. Grimes startlinno = plinno; 10654b88c807SRodney W. Grimes synerror("Missing '}'"); 10664b88c807SRodney W. Grimes } 10674b88c807SRodney W. Grimes USTPUTC('\0', out); 10684b88c807SRodney W. Grimes len = out - stackblock(); 10694b88c807SRodney W. Grimes out = stackblock(); 10704b88c807SRodney W. Grimes if (eofmark == NULL) { 10714b88c807SRodney W. Grimes if ((c == '>' || c == '<') 10724b88c807SRodney W. Grimes && quotef == 0 10734b88c807SRodney W. Grimes && len <= 2 10744b88c807SRodney W. Grimes && (*out == '\0' || is_digit(*out))) { 10754b88c807SRodney W. Grimes PARSEREDIR(); 10764b88c807SRodney W. Grimes return lasttoken = TREDIR; 10774b88c807SRodney W. Grimes } else { 10784b88c807SRodney W. Grimes pungetc(); 10794b88c807SRodney W. Grimes } 10804b88c807SRodney W. Grimes } 10814b88c807SRodney W. Grimes quoteflag = quotef; 10824b88c807SRodney W. Grimes backquotelist = bqlist; 10834b88c807SRodney W. Grimes grabstackblock(len); 10844b88c807SRodney W. Grimes wordtext = out; 10854b88c807SRodney W. Grimes return lasttoken = TWORD; 10864b88c807SRodney W. Grimes /* end of readtoken routine */ 10874b88c807SRodney W. Grimes 10884b88c807SRodney W. Grimes 10894b88c807SRodney W. Grimes 10904b88c807SRodney W. Grimes /* 10914b88c807SRodney W. Grimes * Check to see whether we are at the end of the here document. When this 10924b88c807SRodney W. Grimes * is called, c is set to the first character of the next input line. If 10934b88c807SRodney W. Grimes * we are at the end of the here document, this routine sets the c to PEOF. 10944b88c807SRodney W. Grimes */ 10954b88c807SRodney W. Grimes 10964b88c807SRodney W. Grimes checkend: { 10974b88c807SRodney W. Grimes if (eofmark) { 10984b88c807SRodney W. Grimes if (striptabs) { 10994b88c807SRodney W. Grimes while (c == '\t') 11004b88c807SRodney W. Grimes c = pgetc(); 11014b88c807SRodney W. Grimes } 11024b88c807SRodney W. Grimes if (c == *eofmark) { 11034b88c807SRodney W. Grimes if (pfgets(line, sizeof line) != NULL) { 11047920a31dSSteve Price char *p, *q; 11054b88c807SRodney W. Grimes 11064b88c807SRodney W. Grimes p = line; 11074b88c807SRodney W. Grimes for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 11084b88c807SRodney W. Grimes if (*p == '\n' && *q == '\0') { 11094b88c807SRodney W. Grimes c = PEOF; 11104b88c807SRodney W. Grimes plinno++; 11114b88c807SRodney W. Grimes needprompt = doprompt; 11124b88c807SRodney W. Grimes } else { 11134b88c807SRodney W. Grimes pushstring(line, strlen(line), NULL); 11144b88c807SRodney W. Grimes } 11154b88c807SRodney W. Grimes } 11164b88c807SRodney W. Grimes } 11174b88c807SRodney W. Grimes } 11184b88c807SRodney W. Grimes goto checkend_return; 11194b88c807SRodney W. Grimes } 11204b88c807SRodney W. Grimes 11214b88c807SRodney W. Grimes 11224b88c807SRodney W. Grimes /* 11234b88c807SRodney W. Grimes * Parse a redirection operator. The variable "out" points to a string 11244b88c807SRodney W. Grimes * specifying the fd to be redirected. The variable "c" contains the 11254b88c807SRodney W. Grimes * first character of the redirection operator. 11264b88c807SRodney W. Grimes */ 11274b88c807SRodney W. Grimes 11284b88c807SRodney W. Grimes parseredir: { 11294b88c807SRodney W. Grimes char fd = *out; 11304b88c807SRodney W. Grimes union node *np; 11314b88c807SRodney W. Grimes 11324b88c807SRodney W. Grimes np = (union node *)stalloc(sizeof (struct nfile)); 11334b88c807SRodney W. Grimes if (c == '>') { 11344b88c807SRodney W. Grimes np->nfile.fd = 1; 11354b88c807SRodney W. Grimes c = pgetc(); 11364b88c807SRodney W. Grimes if (c == '>') 11374b88c807SRodney W. Grimes np->type = NAPPEND; 11384b88c807SRodney W. Grimes else if (c == '&') 11394b88c807SRodney W. Grimes np->type = NTOFD; 11401a958c66STim J. Robbins else if (c == '|') 11411a958c66STim J. Robbins np->type = NCLOBBER; 11424b88c807SRodney W. Grimes else { 11434b88c807SRodney W. Grimes np->type = NTO; 11444b88c807SRodney W. Grimes pungetc(); 11454b88c807SRodney W. Grimes } 11464b88c807SRodney W. Grimes } else { /* c == '<' */ 11474b88c807SRodney W. Grimes np->nfile.fd = 0; 11484b88c807SRodney W. Grimes c = pgetc(); 11494b88c807SRodney W. Grimes if (c == '<') { 11504b88c807SRodney W. Grimes if (sizeof (struct nfile) != sizeof (struct nhere)) { 11514b88c807SRodney W. Grimes np = (union node *)stalloc(sizeof (struct nhere)); 11524b88c807SRodney W. Grimes np->nfile.fd = 0; 11534b88c807SRodney W. Grimes } 11544b88c807SRodney W. Grimes np->type = NHERE; 11554b88c807SRodney W. Grimes heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 11564b88c807SRodney W. Grimes heredoc->here = np; 11574b88c807SRodney W. Grimes if ((c = pgetc()) == '-') { 11584b88c807SRodney W. Grimes heredoc->striptabs = 1; 11594b88c807SRodney W. Grimes } else { 11604b88c807SRodney W. Grimes heredoc->striptabs = 0; 11614b88c807SRodney W. Grimes pungetc(); 11624b88c807SRodney W. Grimes } 11634b88c807SRodney W. Grimes } else if (c == '&') 11644b88c807SRodney W. Grimes np->type = NFROMFD; 11654682f420SBrian Somers else if (c == '>') 11664682f420SBrian Somers np->type = NFROMTO; 11674b88c807SRodney W. Grimes else { 11684b88c807SRodney W. Grimes np->type = NFROM; 11694b88c807SRodney W. Grimes pungetc(); 11704b88c807SRodney W. Grimes } 11714b88c807SRodney W. Grimes } 11724b88c807SRodney W. Grimes if (fd != '\0') 11734b88c807SRodney W. Grimes np->nfile.fd = digit_val(fd); 11744b88c807SRodney W. Grimes redirnode = np; 11754b88c807SRodney W. Grimes goto parseredir_return; 11764b88c807SRodney W. Grimes } 11774b88c807SRodney W. Grimes 11784b88c807SRodney W. Grimes 11794b88c807SRodney W. Grimes /* 11804b88c807SRodney W. Grimes * Parse a substitution. At this point, we have read the dollar sign 11814b88c807SRodney W. Grimes * and nothing else. 11824b88c807SRodney W. Grimes */ 11834b88c807SRodney W. Grimes 11844b88c807SRodney W. Grimes parsesub: { 11854b88c807SRodney W. Grimes int subtype; 11864b88c807SRodney W. Grimes int typeloc; 11874b88c807SRodney W. Grimes int flags; 11884b88c807SRodney W. Grimes char *p; 11894b88c807SRodney W. Grimes #ifndef GDB_HACK 11904b88c807SRodney W. Grimes static const char types[] = "}-+?="; 11914b88c807SRodney W. Grimes #endif 11925c817731SPeter Wemm int bracketed_name = 0; /* used to handle ${[0-9]*} variables */ 11934b88c807SRodney W. Grimes 11944b88c807SRodney W. Grimes c = pgetc(); 11954b88c807SRodney W. Grimes if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) { 11964b88c807SRodney W. Grimes USTPUTC('$', out); 11974b88c807SRodney W. Grimes pungetc(); 11984b88c807SRodney W. Grimes } else if (c == '(') { /* $(command) or $((arith)) */ 11994b88c807SRodney W. Grimes if (pgetc() == '(') { 12004b88c807SRodney W. Grimes PARSEARITH(); 12014b88c807SRodney W. Grimes } else { 12024b88c807SRodney W. Grimes pungetc(); 12034b88c807SRodney W. Grimes PARSEBACKQNEW(); 12044b88c807SRodney W. Grimes } 12054b88c807SRodney W. Grimes } else { 12064b88c807SRodney W. Grimes USTPUTC(CTLVAR, out); 12074b88c807SRodney W. Grimes typeloc = out - stackblock(); 12084b88c807SRodney W. Grimes USTPUTC(VSNORMAL, out); 12094b88c807SRodney W. Grimes subtype = VSNORMAL; 12104b88c807SRodney W. Grimes if (c == '{') { 12115c817731SPeter Wemm bracketed_name = 1; 12124b88c807SRodney W. Grimes c = pgetc(); 1213aa9caaf6SPeter Wemm if (c == '#') { 1214aa9caaf6SPeter Wemm if ((c = pgetc()) == '}') 1215aa9caaf6SPeter Wemm c = '#'; 1216aa9caaf6SPeter Wemm else 1217aa9caaf6SPeter Wemm subtype = VSLENGTH; 1218aa9caaf6SPeter Wemm } 1219aa9caaf6SPeter Wemm else 12204b88c807SRodney W. Grimes subtype = 0; 12214b88c807SRodney W. Grimes } 12224b88c807SRodney W. Grimes if (is_name(c)) { 12234b88c807SRodney W. Grimes do { 12244b88c807SRodney W. Grimes STPUTC(c, out); 12254b88c807SRodney W. Grimes c = pgetc(); 12264b88c807SRodney W. Grimes } while (is_in_name(c)); 12275c817731SPeter Wemm } else if (is_digit(c)) { 12285c817731SPeter Wemm if (bracketed_name) { 12295c817731SPeter Wemm do { 12305c817731SPeter Wemm STPUTC(c, out); 12315c817731SPeter Wemm c = pgetc(); 12325c817731SPeter Wemm } while (is_digit(c)); 12335c817731SPeter Wemm } else { 12345c817731SPeter Wemm STPUTC(c, out); 12355c817731SPeter Wemm c = pgetc(); 12365c817731SPeter Wemm } 12374b88c807SRodney W. Grimes } else { 12384b88c807SRodney W. Grimes if (! is_special(c)) 12394b88c807SRodney W. Grimes badsub: synerror("Bad substitution"); 12404b88c807SRodney W. Grimes USTPUTC(c, out); 12414b88c807SRodney W. Grimes c = pgetc(); 12424b88c807SRodney W. Grimes } 12434b88c807SRodney W. Grimes STPUTC('=', out); 12444b88c807SRodney W. Grimes flags = 0; 12454b88c807SRodney W. Grimes if (subtype == 0) { 1246aa9caaf6SPeter Wemm switch (c) { 1247aa9caaf6SPeter Wemm case ':': 12484b88c807SRodney W. Grimes flags = VSNUL; 12494b88c807SRodney W. Grimes c = pgetc(); 1250aa9caaf6SPeter Wemm /*FALLTHROUGH*/ 1251aa9caaf6SPeter Wemm default: 12524b88c807SRodney W. Grimes p = strchr(types, c); 12534b88c807SRodney W. Grimes if (p == NULL) 12544b88c807SRodney W. Grimes goto badsub; 12554b88c807SRodney W. Grimes subtype = p - types + VSNORMAL; 1256aa9caaf6SPeter Wemm break; 1257aa9caaf6SPeter Wemm case '%': 1258aa9caaf6SPeter Wemm case '#': 1259aa9caaf6SPeter Wemm { 1260aa9caaf6SPeter Wemm int cc = c; 1261aa9caaf6SPeter Wemm subtype = c == '#' ? VSTRIMLEFT : 1262aa9caaf6SPeter Wemm VSTRIMRIGHT; 1263aa9caaf6SPeter Wemm c = pgetc(); 1264aa9caaf6SPeter Wemm if (c == cc) 1265aa9caaf6SPeter Wemm subtype++; 1266aa9caaf6SPeter Wemm else 1267aa9caaf6SPeter Wemm pungetc(); 1268aa9caaf6SPeter Wemm break; 1269aa9caaf6SPeter Wemm } 1270aa9caaf6SPeter Wemm } 12714b88c807SRodney W. Grimes } else { 12724b88c807SRodney W. Grimes pungetc(); 12734b88c807SRodney W. Grimes } 1274c11e75cfSMartin Cracauer if (subtype != VSLENGTH && (dblquote || arinest)) 12754b88c807SRodney W. Grimes flags |= VSQUOTE; 12764b88c807SRodney W. Grimes *(stackblock() + typeloc) = subtype | flags; 12774b88c807SRodney W. Grimes if (subtype != VSNORMAL) 12784b88c807SRodney W. Grimes varnest++; 12794b88c807SRodney W. Grimes } 12804b88c807SRodney W. Grimes goto parsesub_return; 12814b88c807SRodney W. Grimes } 12824b88c807SRodney W. Grimes 12834b88c807SRodney W. Grimes 12844b88c807SRodney W. Grimes /* 12854b88c807SRodney W. Grimes * Called to parse command substitutions. Newstyle is set if the command 12864b88c807SRodney W. Grimes * is enclosed inside $(...); nlpp is a pointer to the head of the linked 12874b88c807SRodney W. Grimes * list of commands (passed by reference), and savelen is the number of 12884b88c807SRodney W. Grimes * characters on the top of the stack which must be preserved. 12894b88c807SRodney W. Grimes */ 12904b88c807SRodney W. Grimes 12914b88c807SRodney W. Grimes parsebackq: { 12924b88c807SRodney W. Grimes struct nodelist **nlpp; 12934b88c807SRodney W. Grimes int savepbq; 12944b88c807SRodney W. Grimes union node *n; 12954b88c807SRodney W. Grimes char *volatile str; 12964b88c807SRodney W. Grimes struct jmploc jmploc; 12974b88c807SRodney W. Grimes struct jmploc *volatile savehandler; 12984b88c807SRodney W. Grimes int savelen; 1299ab0a2172SSteve Price int saveprompt; 1300ab0a2172SSteve Price #if __GNUC__ 1301ab0a2172SSteve Price /* Avoid longjmp clobbering */ 1302ab0a2172SSteve Price (void) &saveprompt; 1303ab0a2172SSteve Price #endif 13044b88c807SRodney W. Grimes 13054b88c807SRodney W. Grimes savepbq = parsebackquote; 13064b88c807SRodney W. Grimes if (setjmp(jmploc.loc)) { 13074b88c807SRodney W. Grimes if (str) 13084b88c807SRodney W. Grimes ckfree(str); 13094b88c807SRodney W. Grimes parsebackquote = 0; 13104b88c807SRodney W. Grimes handler = savehandler; 13114b88c807SRodney W. Grimes longjmp(handler->loc, 1); 13124b88c807SRodney W. Grimes } 13134b88c807SRodney W. Grimes INTOFF; 13144b88c807SRodney W. Grimes str = NULL; 13154b88c807SRodney W. Grimes savelen = out - stackblock(); 13164b88c807SRodney W. Grimes if (savelen > 0) { 13174b88c807SRodney W. Grimes str = ckmalloc(savelen); 1318aa9caaf6SPeter Wemm memcpy(str, stackblock(), savelen); 13194b88c807SRodney W. Grimes } 13204b88c807SRodney W. Grimes savehandler = handler; 13214b88c807SRodney W. Grimes handler = &jmploc; 13224b88c807SRodney W. Grimes INTON; 13234b88c807SRodney W. Grimes if (oldstyle) { 13244b88c807SRodney W. Grimes /* We must read until the closing backquote, giving special 13254b88c807SRodney W. Grimes treatment to some slashes, and then push the string and 13264b88c807SRodney W. Grimes reread it as input, interpreting it normally. */ 13277920a31dSSteve Price char *out; 13287920a31dSSteve Price int c; 13294b88c807SRodney W. Grimes int savelen; 13304b88c807SRodney W. Grimes char *str; 13314b88c807SRodney W. Grimes 1332ab0a2172SSteve Price 13334b88c807SRodney W. Grimes STARTSTACKSTR(out); 1334ab0a2172SSteve Price for (;;) { 1335ab0a2172SSteve Price if (needprompt) { 1336ab0a2172SSteve Price setprompt(2); 1337ab0a2172SSteve Price needprompt = 0; 133872348d41SJoerg Wunsch } 1339ab0a2172SSteve Price switch (c = pgetc()) { 1340ab0a2172SSteve Price case '`': 1341ab0a2172SSteve Price goto done; 1342ab0a2172SSteve Price 1343ab0a2172SSteve Price case '\\': 1344ab0a2172SSteve Price if ((c = pgetc()) == '\n') { 1345ab0a2172SSteve Price plinno++; 1346ab0a2172SSteve Price if (doprompt) 1347ab0a2172SSteve Price setprompt(2); 1348ab0a2172SSteve Price else 1349ab0a2172SSteve Price setprompt(0); 1350ab0a2172SSteve Price /* 1351ab0a2172SSteve Price * If eating a newline, avoid putting 1352ab0a2172SSteve Price * the newline into the new character 1353ab0a2172SSteve Price * stream (via the STPUTC after the 1354ab0a2172SSteve Price * switch). 1355ab0a2172SSteve Price */ 1356ab0a2172SSteve Price continue; 1357ab0a2172SSteve Price } 13581e95454eSPaul Richards if (c != '\\' && c != '`' && c != '$' 13594b88c807SRodney W. Grimes && (!dblquote || c != '"')) 13604b88c807SRodney W. Grimes STPUTC('\\', out); 1361ab0a2172SSteve Price break; 1362ab0a2172SSteve Price 1363ab0a2172SSteve Price case '\n': 1364ab0a2172SSteve Price plinno++; 1365ab0a2172SSteve Price needprompt = doprompt; 1366ab0a2172SSteve Price break; 1367ab0a2172SSteve Price 1368ab0a2172SSteve Price case PEOF: 1369ab0a2172SSteve Price startlinno = plinno; 1370ab0a2172SSteve Price synerror("EOF in backquote substitution"); 1371ab0a2172SSteve Price break; 1372ab0a2172SSteve Price 1373ab0a2172SSteve Price default: 1374ab0a2172SSteve Price break; 13754b88c807SRodney W. Grimes } 13764b88c807SRodney W. Grimes STPUTC(c, out); 13774b88c807SRodney W. Grimes } 1378ab0a2172SSteve Price done: 13794b88c807SRodney W. Grimes STPUTC('\0', out); 13804b88c807SRodney W. Grimes savelen = out - stackblock(); 13814b88c807SRodney W. Grimes if (savelen > 0) { 13824b88c807SRodney W. Grimes str = ckmalloc(savelen); 1383aa9caaf6SPeter Wemm memcpy(str, stackblock(), savelen); 13844b88c807SRodney W. Grimes setinputstring(str, 1); 13854b88c807SRodney W. Grimes } 1386aa9caaf6SPeter Wemm } 13874b88c807SRodney W. Grimes nlpp = &bqlist; 13884b88c807SRodney W. Grimes while (*nlpp) 13894b88c807SRodney W. Grimes nlpp = &(*nlpp)->next; 13904b88c807SRodney W. Grimes *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 13914b88c807SRodney W. Grimes (*nlpp)->next = NULL; 13924b88c807SRodney W. Grimes parsebackquote = oldstyle; 1393ab0a2172SSteve Price 1394ab0a2172SSteve Price if (oldstyle) { 1395ab0a2172SSteve Price saveprompt = doprompt; 1396ab0a2172SSteve Price doprompt = 0; 1397ab0a2172SSteve Price } 1398ab0a2172SSteve Price 13994b88c807SRodney W. Grimes n = list(0); 1400ab0a2172SSteve Price 14014b88c807SRodney W. Grimes if (oldstyle) 1402ab0a2172SSteve Price doprompt = saveprompt; 1403ab0a2172SSteve Price else { 1404ab0a2172SSteve Price if (readtoken() != TRP) 1405ab0a2172SSteve Price synexpect(TRP); 1406ab0a2172SSteve Price } 1407ab0a2172SSteve Price 1408ab0a2172SSteve Price (*nlpp)->n = n; 1409ab0a2172SSteve Price if (oldstyle) { 1410ab0a2172SSteve Price /* 1411ab0a2172SSteve Price * Start reading from old file again, ignoring any pushed back 1412ab0a2172SSteve Price * tokens left from the backquote parsing 1413ab0a2172SSteve Price */ 14144b88c807SRodney W. Grimes popfile(); 1415ab0a2172SSteve Price tokpushback = 0; 1416ab0a2172SSteve Price } 14174b88c807SRodney W. Grimes while (stackblocksize() <= savelen) 14184b88c807SRodney W. Grimes growstackblock(); 14194b88c807SRodney W. Grimes STARTSTACKSTR(out); 14204b88c807SRodney W. Grimes if (str) { 1421aa9caaf6SPeter Wemm memcpy(out, str, savelen); 14224b88c807SRodney W. Grimes STADJUST(savelen, out); 14234b88c807SRodney W. Grimes INTOFF; 14244b88c807SRodney W. Grimes ckfree(str); 14254b88c807SRodney W. Grimes str = NULL; 14264b88c807SRodney W. Grimes INTON; 14274b88c807SRodney W. Grimes } 14284b88c807SRodney W. Grimes parsebackquote = savepbq; 14294b88c807SRodney W. Grimes handler = savehandler; 14304b88c807SRodney W. Grimes if (arinest || dblquote) 14314b88c807SRodney W. Grimes USTPUTC(CTLBACKQ | CTLQUOTE, out); 14324b88c807SRodney W. Grimes else 14334b88c807SRodney W. Grimes USTPUTC(CTLBACKQ, out); 14344b88c807SRodney W. Grimes if (oldstyle) 14354b88c807SRodney W. Grimes goto parsebackq_oldreturn; 14364b88c807SRodney W. Grimes else 14374b88c807SRodney W. Grimes goto parsebackq_newreturn; 14384b88c807SRodney W. Grimes } 14394b88c807SRodney W. Grimes 14404b88c807SRodney W. Grimes /* 14414b88c807SRodney W. Grimes * Parse an arithmetic expansion (indicate start of one and set state) 14424b88c807SRodney W. Grimes */ 14434b88c807SRodney W. Grimes parsearith: { 14444b88c807SRodney W. Grimes 14454b88c807SRodney W. Grimes if (++arinest == 1) { 14464b88c807SRodney W. Grimes prevsyntax = syntax; 14474b88c807SRodney W. Grimes syntax = ARISYNTAX; 14484b88c807SRodney W. Grimes USTPUTC(CTLARI, out); 14496f47734fSTor Egge if (dblquote) 14506f47734fSTor Egge USTPUTC('"',out); 14516f47734fSTor Egge else 14526f47734fSTor Egge USTPUTC(' ',out); 14534b88c807SRodney W. Grimes } else { 14544b88c807SRodney W. Grimes /* 14554b88c807SRodney W. Grimes * we collapse embedded arithmetic expansion to 14564b88c807SRodney W. Grimes * parenthesis, which should be equivalent 14574b88c807SRodney W. Grimes */ 14584b88c807SRodney W. Grimes USTPUTC('(', out); 14594b88c807SRodney W. Grimes } 14604b88c807SRodney W. Grimes goto parsearith_return; 14614b88c807SRodney W. Grimes } 14624b88c807SRodney W. Grimes 14634b88c807SRodney W. Grimes } /* end of readtoken */ 14644b88c807SRodney W. Grimes 14654b88c807SRodney W. Grimes 14664b88c807SRodney W. Grimes 14674b88c807SRodney W. Grimes #ifdef mkinit 14684b88c807SRodney W. Grimes RESET { 14694b88c807SRodney W. Grimes tokpushback = 0; 14704b88c807SRodney W. Grimes checkkwd = 0; 14714b88c807SRodney W. Grimes } 14724b88c807SRodney W. Grimes #endif 14734b88c807SRodney W. Grimes 14744b88c807SRodney W. Grimes /* 14754b88c807SRodney W. Grimes * Returns true if the text contains nothing to expand (no dollar signs 14764b88c807SRodney W. Grimes * or backquotes). 14774b88c807SRodney W. Grimes */ 14784b88c807SRodney W. Grimes 14794b88c807SRodney W. Grimes STATIC int 14805134c3f7SWarner Losh noexpand(char *text) 14814b88c807SRodney W. Grimes { 14827920a31dSSteve Price char *p; 14837920a31dSSteve Price char c; 14844b88c807SRodney W. Grimes 14854b88c807SRodney W. Grimes p = text; 14864b88c807SRodney W. Grimes while ((c = *p++) != '\0') { 14875557a02aSTor Egge if ( c == CTLQUOTEMARK) 14885557a02aSTor Egge continue; 14894b88c807SRodney W. Grimes if (c == CTLESC) 14904b88c807SRodney W. Grimes p++; 14910c4eeddaSTor Egge else if (BASESYNTAX[(int)c] == CCTL) 14924b88c807SRodney W. Grimes return 0; 14934b88c807SRodney W. Grimes } 14944b88c807SRodney W. Grimes return 1; 14954b88c807SRodney W. Grimes } 14964b88c807SRodney W. Grimes 14974b88c807SRodney W. Grimes 14984b88c807SRodney W. Grimes /* 14994b88c807SRodney W. Grimes * Return true if the argument is a legal variable name (a letter or 15004b88c807SRodney W. Grimes * underscore followed by zero or more letters, underscores, and digits). 15014b88c807SRodney W. Grimes */ 15024b88c807SRodney W. Grimes 15034b88c807SRodney W. Grimes int 15045134c3f7SWarner Losh goodname(char *name) 15054b88c807SRodney W. Grimes { 15067920a31dSSteve Price char *p; 15074b88c807SRodney W. Grimes 15084b88c807SRodney W. Grimes p = name; 15094b88c807SRodney W. Grimes if (! is_name(*p)) 15104b88c807SRodney W. Grimes return 0; 15114b88c807SRodney W. Grimes while (*++p) { 15124b88c807SRodney W. Grimes if (! is_in_name(*p)) 15134b88c807SRodney W. Grimes return 0; 15144b88c807SRodney W. Grimes } 15154b88c807SRodney W. Grimes return 1; 15164b88c807SRodney W. Grimes } 15174b88c807SRodney W. Grimes 15184b88c807SRodney W. Grimes 15194b88c807SRodney W. Grimes /* 15204b88c807SRodney W. Grimes * Called when an unexpected token is read during the parse. The argument 15214b88c807SRodney W. Grimes * is the token that is expected, or -1 if more than one type of token can 15224b88c807SRodney W. Grimes * occur at this point. 15234b88c807SRodney W. Grimes */ 15244b88c807SRodney W. Grimes 15254b88c807SRodney W. Grimes STATIC void 15265134c3f7SWarner Losh synexpect(int token) 1527aa9caaf6SPeter Wemm { 15284b88c807SRodney W. Grimes char msg[64]; 15294b88c807SRodney W. Grimes 15304b88c807SRodney W. Grimes if (token >= 0) { 15314b88c807SRodney W. Grimes fmtstr(msg, 64, "%s unexpected (expecting %s)", 15324b88c807SRodney W. Grimes tokname[lasttoken], tokname[token]); 15334b88c807SRodney W. Grimes } else { 15344b88c807SRodney W. Grimes fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 15354b88c807SRodney W. Grimes } 15364b88c807SRodney W. Grimes synerror(msg); 15374b88c807SRodney W. Grimes } 15384b88c807SRodney W. Grimes 15394b88c807SRodney W. Grimes 15404b88c807SRodney W. Grimes STATIC void 15415134c3f7SWarner Losh synerror(char *msg) 15424b88c807SRodney W. Grimes { 15434b88c807SRodney W. Grimes if (commandname) 15444b88c807SRodney W. Grimes outfmt(&errout, "%s: %d: ", commandname, startlinno); 15454b88c807SRodney W. Grimes outfmt(&errout, "Syntax error: %s\n", msg); 15464b88c807SRodney W. Grimes error((char *)NULL); 15474b88c807SRodney W. Grimes } 15484b88c807SRodney W. Grimes 15494b88c807SRodney W. Grimes STATIC void 15505134c3f7SWarner Losh setprompt(int which) 15514b88c807SRodney W. Grimes { 15524b88c807SRodney W. Grimes whichprompt = which; 15534b88c807SRodney W. Grimes 1554aa9caaf6SPeter Wemm #ifndef NO_HISTORY 15554b88c807SRodney W. Grimes if (!el) 1556aa9caaf6SPeter Wemm #endif 15574b88c807SRodney W. Grimes out2str(getprompt(NULL)); 15584b88c807SRodney W. Grimes } 15594b88c807SRodney W. Grimes 15604b88c807SRodney W. Grimes /* 15614b88c807SRodney W. Grimes * called by editline -- any expansions to the prompt 15624b88c807SRodney W. Grimes * should be added here. 15634b88c807SRodney W. Grimes */ 15644b88c807SRodney W. Grimes char * 15655134c3f7SWarner Losh getprompt(void *unused __unused) 15664b88c807SRodney W. Grimes { 1567f0c73601SDavid E. O'Brien static char ps[PROMPTLEN]; 1568f0c73601SDavid E. O'Brien char *fmt; 1569f0c73601SDavid E. O'Brien int i, j, trim; 1570f0c73601SDavid E. O'Brien 1571f0c73601SDavid E. O'Brien /* 1572f0c73601SDavid E. O'Brien * Select prompt format. 1573f0c73601SDavid E. O'Brien */ 15744b88c807SRodney W. Grimes switch (whichprompt) { 15754b88c807SRodney W. Grimes case 0: 1576f0c73601SDavid E. O'Brien fmt = ""; 1577f0c73601SDavid E. O'Brien break; 15784b88c807SRodney W. Grimes case 1: 1579f0c73601SDavid E. O'Brien fmt = ps1val(); 1580f0c73601SDavid E. O'Brien break; 15814b88c807SRodney W. Grimes case 2: 1582f0c73601SDavid E. O'Brien fmt = ps2val(); 1583f0c73601SDavid E. O'Brien break; 15844b88c807SRodney W. Grimes default: 15854b88c807SRodney W. Grimes return "<internal prompt error>"; 15864b88c807SRodney W. Grimes } 1587f0c73601SDavid E. O'Brien 1588f0c73601SDavid E. O'Brien /* 1589f0c73601SDavid E. O'Brien * Format prompt string. 1590f0c73601SDavid E. O'Brien */ 1591f0c73601SDavid E. O'Brien for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++) 1592f0c73601SDavid E. O'Brien if (*fmt == '\\') 1593f0c73601SDavid E. O'Brien switch (*++fmt) { 1594f0c73601SDavid E. O'Brien 1595f0c73601SDavid E. O'Brien /* 1596f0c73601SDavid E. O'Brien * Hostname. 1597f0c73601SDavid E. O'Brien * 1598f0c73601SDavid E. O'Brien * \h specifies just the local hostname, 1599f0c73601SDavid E. O'Brien * \H specifies fully-qualified hostname. 1600f0c73601SDavid E. O'Brien */ 1601f0c73601SDavid E. O'Brien case 'h': 1602f0c73601SDavid E. O'Brien case 'H': 16038d999570SStefan Farfeleder ps[i] = '\0'; 1604f0c73601SDavid E. O'Brien gethostname(&ps[i], PROMPTLEN - i); 1605f0c73601SDavid E. O'Brien /* Skip to end of hostname. */ 1606f0c73601SDavid E. O'Brien trim = (*fmt == 'h') ? '.' : '\0'; 1607f0c73601SDavid E. O'Brien while ((ps[i+1] != '\0') && (ps[i+1] != trim)) 1608f0c73601SDavid E. O'Brien i++; 1609f0c73601SDavid E. O'Brien break; 1610f0c73601SDavid E. O'Brien 1611f0c73601SDavid E. O'Brien /* 1612f0c73601SDavid E. O'Brien * Working directory. 1613f0c73601SDavid E. O'Brien * 1614f0c73601SDavid E. O'Brien * \W specifies just the final component, 1615f0c73601SDavid E. O'Brien * \w specifies the entire path. 1616f0c73601SDavid E. O'Brien */ 1617f0c73601SDavid E. O'Brien case 'W': 1618f0c73601SDavid E. O'Brien case 'w': 16198d999570SStefan Farfeleder ps[i] = '\0'; 1620f0c73601SDavid E. O'Brien getcwd(&ps[i], PROMPTLEN - i); 1621f0c73601SDavid E. O'Brien if (*fmt == 'W') { 1622f0c73601SDavid E. O'Brien /* Final path component only. */ 1623f0c73601SDavid E. O'Brien trim = 1; 1624f0c73601SDavid E. O'Brien for (j = i; ps[j] != '\0'; j++) 1625f0c73601SDavid E. O'Brien if (ps[j] == '/') 1626f0c73601SDavid E. O'Brien trim = j + 1; 1627f0c73601SDavid E. O'Brien memmove(&ps[i], &ps[trim], 1628f0c73601SDavid E. O'Brien j - trim + 1); 1629f0c73601SDavid E. O'Brien } 1630f0c73601SDavid E. O'Brien /* Skip to end of path. */ 1631f0c73601SDavid E. O'Brien while (ps[i + 1] != '\0') 1632f0c73601SDavid E. O'Brien i++; 1633f0c73601SDavid E. O'Brien break; 1634f0c73601SDavid E. O'Brien 1635f0c73601SDavid E. O'Brien /* 1636f0c73601SDavid E. O'Brien * Superuser status. 1637f0c73601SDavid E. O'Brien * 1638f0c73601SDavid E. O'Brien * '$' for normal users, '#' for root. 1639f0c73601SDavid E. O'Brien */ 1640f0c73601SDavid E. O'Brien case '$': 1641f0c73601SDavid E. O'Brien ps[i] = (geteuid() != 0) ? '$' : '#'; 1642f0c73601SDavid E. O'Brien break; 1643f0c73601SDavid E. O'Brien 1644f0c73601SDavid E. O'Brien /* 1645f0c73601SDavid E. O'Brien * A literal \. 1646f0c73601SDavid E. O'Brien */ 1647f0c73601SDavid E. O'Brien case '\\': 1648f0c73601SDavid E. O'Brien ps[i] = '\\'; 1649f0c73601SDavid E. O'Brien break; 1650f0c73601SDavid E. O'Brien 1651f0c73601SDavid E. O'Brien /* 1652f0c73601SDavid E. O'Brien * Emit unrecognized formats verbatim. 1653f0c73601SDavid E. O'Brien */ 1654f0c73601SDavid E. O'Brien default: 1655f0c73601SDavid E. O'Brien ps[i++] = '\\'; 1656f0c73601SDavid E. O'Brien ps[i] = *fmt; 1657f0c73601SDavid E. O'Brien break; 1658f0c73601SDavid E. O'Brien } 1659f0c73601SDavid E. O'Brien else 1660f0c73601SDavid E. O'Brien ps[i] = *fmt; 1661f0c73601SDavid E. O'Brien ps[i] = '\0'; 1662f0c73601SDavid E. O'Brien return (ps); 16634b88c807SRodney W. Grimes } 1664