147139Sbostic /*- 247139Sbostic * Copyright (c) 1991 The Regents of the University of California. 347139Sbostic * All rights reserved. 447139Sbostic * 547139Sbostic * This code is derived from software contributed to Berkeley by 647139Sbostic * Kenneth Almquist. 747139Sbostic * 847139Sbostic * %sccs.include.redist.c% 947139Sbostic */ 1047139Sbostic 1147139Sbostic #ifndef lint 12*53178Smarc static char sccsid[] = "@(#)parser.c 5.4 (Berkeley) 04/16/92"; 1347139Sbostic #endif /* not lint */ 1447139Sbostic 1547139Sbostic #include "shell.h" 1647139Sbostic #include "parser.h" 1747139Sbostic #include "nodes.h" 1847139Sbostic #include "expand.h" /* defines rmescapes() */ 1947139Sbostic #include "redir.h" /* defines copyfd() */ 2047139Sbostic #include "syntax.h" 2147139Sbostic #include "options.h" 2247139Sbostic #include "input.h" 2347139Sbostic #include "output.h" 2447139Sbostic #include "var.h" 2547139Sbostic #include "error.h" 2647139Sbostic #include "memalloc.h" 2747139Sbostic #include "mystring.h" 2847139Sbostic 2947139Sbostic 3047139Sbostic /* 3147139Sbostic * Shell command parser. 3247139Sbostic */ 3347139Sbostic 3447139Sbostic #define EOFMARKLEN 79 3547139Sbostic 3647139Sbostic /* values returned by readtoken */ 3747139Sbostic #include "token.def" 3847139Sbostic 3947139Sbostic 4047139Sbostic 4147139Sbostic struct heredoc { 4247139Sbostic struct heredoc *next; /* next here document in list */ 4347139Sbostic union node *here; /* redirection node */ 4447139Sbostic char *eofmark; /* string indicating end of input */ 4547139Sbostic int striptabs; /* if set, strip leading tabs */ 4647139Sbostic }; 4747139Sbostic 4847139Sbostic 4947139Sbostic 5047139Sbostic struct heredoc *heredoclist; /* list of here documents to read */ 5147139Sbostic int parsebackquote; /* nonzero if we are inside backquotes */ 5247139Sbostic int doprompt; /* if set, prompt the user */ 5347139Sbostic int needprompt; /* true if interactive and at start of line */ 5447139Sbostic int lasttoken; /* last token read */ 5547139Sbostic MKINIT int tokpushback; /* last token pushed back */ 5647139Sbostic char *wordtext; /* text of last word returned by readtoken */ 5747981Smarc int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */ 5847139Sbostic struct nodelist *backquotelist; 5947139Sbostic union node *redirnode; 6047139Sbostic struct heredoc *heredoc; 6147139Sbostic int quoteflag; /* set if (part of) last token was quoted */ 6247139Sbostic int startlinno; /* line # where last token started */ 6347139Sbostic 6447139Sbostic 6547139Sbostic #define GDB_HACK 1 /* avoid local declarations which gdb can't handle */ 6647139Sbostic #ifdef GDB_HACK 6747139Sbostic static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'}; 6847139Sbostic static const char types[] = "}-+?="; 6947139Sbostic #endif 7047139Sbostic 7147139Sbostic 7247981Smarc STATIC union node *list __P((int)); 7347981Smarc STATIC union node *andor __P((void)); 7447981Smarc STATIC union node *pipeline __P((void)); 7547981Smarc STATIC union node *command __P((void)); 7647981Smarc STATIC union node *simplecmd __P((void)); 7747981Smarc STATIC void parsefname __P((void)); 7847981Smarc STATIC void parseheredoc __P((void)); 7947981Smarc STATIC int readtoken __P((void)); 8047981Smarc STATIC int readtoken1 __P((int, char const *, char *, int)); 8147981Smarc STATIC void attyline __P((void)); 8247981Smarc STATIC int noexpand __P((char *)); 8347981Smarc STATIC void synexpect __P((int)); 8447981Smarc STATIC void synerror __P((char *)); 8547139Sbostic 8647139Sbostic #if ATTY 8747981Smarc STATIC void putprompt __P((char *)); 8847139Sbostic #else /* not ATTY */ 8947139Sbostic #define putprompt(s) out2str(s) 9047139Sbostic #endif 9147139Sbostic 9247139Sbostic 9347139Sbostic 9447139Sbostic 9547139Sbostic /* 9647139Sbostic * Read and parse a command. Returns NEOF on end of file. (NULL is a 9747139Sbostic * valid parse tree indicating a blank line.) 9847139Sbostic */ 9947139Sbostic 10047139Sbostic union node * 10147139Sbostic parsecmd(interact) { 10247139Sbostic int t; 10347139Sbostic 10447139Sbostic doprompt = interact; 10547139Sbostic if (doprompt) 10647139Sbostic putprompt(ps1val()); 10747139Sbostic needprompt = 0; 10847139Sbostic if ((t = readtoken()) == TEOF) 10947139Sbostic return NEOF; 11047139Sbostic if (t == TNL) 11147139Sbostic return NULL; 11247139Sbostic tokpushback++; 11347139Sbostic return list(1); 11447139Sbostic } 11547139Sbostic 11647139Sbostic 11747139Sbostic STATIC union node * 11847139Sbostic list(nlflag) { 11947139Sbostic union node *n1, *n2, *n3; 12047139Sbostic 12147981Smarc checkkwd = 2; 12247981Smarc if (nlflag == 0 && tokendlist[peektoken()]) 12347139Sbostic return NULL; 12447139Sbostic n1 = andor(); 12547139Sbostic for (;;) { 12647139Sbostic switch (readtoken()) { 12747139Sbostic case TBACKGND: 12847139Sbostic if (n1->type == NCMD || n1->type == NPIPE) { 12947139Sbostic n1->ncmd.backgnd = 1; 13047139Sbostic } else if (n1->type == NREDIR) { 13147139Sbostic n1->type = NBACKGND; 13247139Sbostic } else { 13347139Sbostic n3 = (union node *)stalloc(sizeof (struct nredir)); 13447139Sbostic n3->type = NBACKGND; 13547139Sbostic n3->nredir.n = n1; 13647139Sbostic n3->nredir.redirect = NULL; 13747139Sbostic n1 = n3; 13847139Sbostic } 13947139Sbostic goto tsemi; 14047139Sbostic case TNL: 14147139Sbostic tokpushback++; 14247139Sbostic /* fall through */ 14347139Sbostic tsemi: case TSEMI: 14447139Sbostic if (readtoken() == TNL) { 14547139Sbostic parseheredoc(); 14647139Sbostic if (nlflag) 14747139Sbostic return n1; 14847139Sbostic } else { 14947139Sbostic tokpushback++; 15047139Sbostic } 15147981Smarc checkkwd = 2; 15247981Smarc if (tokendlist[peektoken()]) 15347139Sbostic return n1; 15447139Sbostic n2 = andor(); 15547139Sbostic n3 = (union node *)stalloc(sizeof (struct nbinary)); 15647139Sbostic n3->type = NSEMI; 15747139Sbostic n3->nbinary.ch1 = n1; 15847139Sbostic n3->nbinary.ch2 = n2; 15947139Sbostic n1 = n3; 16047139Sbostic break; 16147139Sbostic case TEOF: 16247139Sbostic if (heredoclist) 16347139Sbostic parseheredoc(); 16447139Sbostic else 16547139Sbostic pungetc(); /* push back EOF on input */ 16647139Sbostic return n1; 16747139Sbostic default: 16847139Sbostic if (nlflag) 16947139Sbostic synexpect(-1); 17047139Sbostic tokpushback++; 17147139Sbostic return n1; 17247139Sbostic } 17347139Sbostic } 17447139Sbostic } 17547139Sbostic 17647139Sbostic 17747139Sbostic 17847139Sbostic STATIC union node * 17947139Sbostic andor() { 18047139Sbostic union node *n1, *n2, *n3; 18147139Sbostic int t; 18247139Sbostic 18347139Sbostic n1 = pipeline(); 18447139Sbostic for (;;) { 18547139Sbostic if ((t = readtoken()) == TAND) { 18647139Sbostic t = NAND; 18747139Sbostic } else if (t == TOR) { 18847139Sbostic t = NOR; 18947139Sbostic } else { 19047139Sbostic tokpushback++; 19147139Sbostic return n1; 19247139Sbostic } 19347139Sbostic n2 = pipeline(); 19447139Sbostic n3 = (union node *)stalloc(sizeof (struct nbinary)); 19547139Sbostic n3->type = t; 19647139Sbostic n3->nbinary.ch1 = n1; 19747139Sbostic n3->nbinary.ch2 = n2; 19847139Sbostic n1 = n3; 19947139Sbostic } 20047139Sbostic } 20147139Sbostic 20247139Sbostic 20347139Sbostic 20447139Sbostic STATIC union node * 20547139Sbostic pipeline() { 206*53178Smarc union node *n1, *pipenode, *notnode; 20747139Sbostic struct nodelist *lp, *prev; 208*53178Smarc int negate = 0; 20947139Sbostic 210*53178Smarc TRACE(("pipeline: entered\n")); 211*53178Smarc while (readtoken() == TNOT) { 212*53178Smarc TRACE(("pipeline: TNOT recognized\n")); 213*53178Smarc negate = !negate; 214*53178Smarc } 215*53178Smarc tokpushback++; 21647139Sbostic n1 = command(); 21747139Sbostic if (readtoken() == TPIPE) { 21847139Sbostic pipenode = (union node *)stalloc(sizeof (struct npipe)); 21947139Sbostic pipenode->type = NPIPE; 22047139Sbostic pipenode->npipe.backgnd = 0; 22147139Sbostic lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 22247139Sbostic pipenode->npipe.cmdlist = lp; 22347139Sbostic lp->n = n1; 22447139Sbostic do { 22547139Sbostic prev = lp; 22647139Sbostic lp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 22747139Sbostic lp->n = command(); 22847139Sbostic prev->next = lp; 22947139Sbostic } while (readtoken() == TPIPE); 23047139Sbostic lp->next = NULL; 23147139Sbostic n1 = pipenode; 23247139Sbostic } 23347139Sbostic tokpushback++; 234*53178Smarc if (negate) { 235*53178Smarc notnode = (union node *)stalloc(sizeof (struct nnot)); 236*53178Smarc notnode->type = NNOT; 237*53178Smarc notnode->nnot.com = n1; 238*53178Smarc n1 = notnode; 239*53178Smarc } 24047139Sbostic return n1; 24147139Sbostic } 24247139Sbostic 24347139Sbostic 24447139Sbostic 24547139Sbostic STATIC union node * 24647139Sbostic command() { 24747139Sbostic union node *n1, *n2; 24847139Sbostic union node *ap, **app; 24947139Sbostic union node *cp, **cpp; 25047139Sbostic union node *redir, **rpp; 25147139Sbostic int t; 25247139Sbostic 25347981Smarc checkkwd = 2; 25447139Sbostic switch (readtoken()) { 25547139Sbostic case TIF: 25647139Sbostic n1 = (union node *)stalloc(sizeof (struct nif)); 25747139Sbostic n1->type = NIF; 25847139Sbostic n1->nif.test = list(0); 25947139Sbostic if (readtoken() != TTHEN) 26047139Sbostic synexpect(TTHEN); 26147139Sbostic n1->nif.ifpart = list(0); 26247139Sbostic n2 = n1; 26347139Sbostic while (readtoken() == TELIF) { 26447139Sbostic n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif)); 26547139Sbostic n2 = n2->nif.elsepart; 26647139Sbostic n2->type = NIF; 26747139Sbostic n2->nif.test = list(0); 26847139Sbostic if (readtoken() != TTHEN) 26947139Sbostic synexpect(TTHEN); 27047139Sbostic n2->nif.ifpart = list(0); 27147139Sbostic } 27247139Sbostic if (lasttoken == TELSE) 27347139Sbostic n2->nif.elsepart = list(0); 27447139Sbostic else { 27547139Sbostic n2->nif.elsepart = NULL; 27647139Sbostic tokpushback++; 27747139Sbostic } 27847139Sbostic if (readtoken() != TFI) 27947139Sbostic synexpect(TFI); 28047981Smarc checkkwd = 1; 28147139Sbostic break; 28247139Sbostic case TWHILE: 28347981Smarc case TUNTIL: { 28447981Smarc int got; 28547139Sbostic n1 = (union node *)stalloc(sizeof (struct nbinary)); 28647139Sbostic n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL; 28747139Sbostic n1->nbinary.ch1 = list(0); 28847981Smarc if ((got=readtoken()) != TDO) { 28947981Smarc TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : "")); 29047139Sbostic synexpect(TDO); 29147981Smarc } 29247139Sbostic n1->nbinary.ch2 = list(0); 29347139Sbostic if (readtoken() != TDONE) 29447139Sbostic synexpect(TDONE); 29547981Smarc checkkwd = 1; 29647139Sbostic break; 29747981Smarc } 29847139Sbostic case TFOR: 29947139Sbostic if (readtoken() != TWORD || quoteflag || ! goodname(wordtext)) 30047139Sbostic synerror("Bad for loop variable"); 30147139Sbostic n1 = (union node *)stalloc(sizeof (struct nfor)); 30247139Sbostic n1->type = NFOR; 30347139Sbostic n1->nfor.var = wordtext; 30447139Sbostic if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) { 30547139Sbostic app = ≈ 30647139Sbostic while (readtoken() == TWORD) { 30747139Sbostic n2 = (union node *)stalloc(sizeof (struct narg)); 30847139Sbostic n2->type = NARG; 30947139Sbostic n2->narg.text = wordtext; 31047139Sbostic n2->narg.backquote = backquotelist; 31147139Sbostic *app = n2; 31247139Sbostic app = &n2->narg.next; 31347139Sbostic } 31447139Sbostic *app = NULL; 31547139Sbostic n1->nfor.args = ap; 31647139Sbostic } else { 31747139Sbostic #ifndef GDB_HACK 31847139Sbostic static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, 31947139Sbostic '@', '=', '\0'}; 32047139Sbostic #endif 32147139Sbostic n2 = (union node *)stalloc(sizeof (struct narg)); 32247139Sbostic n2->type = NARG; 32347139Sbostic n2->narg.text = (char *)argvars; 32447139Sbostic n2->narg.backquote = NULL; 32547139Sbostic n2->narg.next = NULL; 32647139Sbostic n1->nfor.args = n2; 32747139Sbostic } 32847139Sbostic if (lasttoken != TNL && lasttoken != TSEMI) 32947139Sbostic synexpect(-1); 33047981Smarc checkkwd = 2; 33147139Sbostic if ((t = readtoken()) == TDO) 33247139Sbostic t = TDONE; 33347139Sbostic else if (t == TBEGIN) 33447139Sbostic t = TEND; 33547139Sbostic else 33647139Sbostic synexpect(-1); 33747139Sbostic n1->nfor.body = list(0); 33847139Sbostic if (readtoken() != t) 33947139Sbostic synexpect(t); 34047981Smarc checkkwd = 1; 34147139Sbostic break; 34247139Sbostic case TCASE: 34347139Sbostic n1 = (union node *)stalloc(sizeof (struct ncase)); 34447139Sbostic n1->type = NCASE; 34547139Sbostic if (readtoken() != TWORD) 34647139Sbostic synexpect(TWORD); 34747139Sbostic n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg)); 34847139Sbostic n2->type = NARG; 34947139Sbostic n2->narg.text = wordtext; 35047139Sbostic n2->narg.backquote = backquotelist; 35147139Sbostic n2->narg.next = NULL; 35247139Sbostic while (readtoken() == TNL); 35347139Sbostic if (lasttoken != TWORD || ! equal(wordtext, "in")) 35447139Sbostic synerror("expecting \"in\""); 35547139Sbostic cpp = &n1->ncase.cases; 35647981Smarc while (checkkwd = 2, readtoken() == TWORD) { 35747139Sbostic *cpp = cp = (union node *)stalloc(sizeof (struct nclist)); 35847139Sbostic cp->type = NCLIST; 35947139Sbostic app = &cp->nclist.pattern; 36047139Sbostic for (;;) { 36147139Sbostic *app = ap = (union node *)stalloc(sizeof (struct narg)); 36247139Sbostic ap->type = NARG; 36347139Sbostic ap->narg.text = wordtext; 36447139Sbostic ap->narg.backquote = backquotelist; 36547139Sbostic if (readtoken() != TPIPE) 36647139Sbostic break; 36747139Sbostic app = &ap->narg.next; 36847139Sbostic if (readtoken() != TWORD) 36947139Sbostic synexpect(TWORD); 37047139Sbostic } 37147139Sbostic ap->narg.next = NULL; 37247139Sbostic if (lasttoken != TRP) 37347139Sbostic synexpect(TRP); 37447139Sbostic cp->nclist.body = list(0); 37547139Sbostic if ((t = readtoken()) == TESAC) 37647139Sbostic tokpushback++; 37747139Sbostic else if (t != TENDCASE) 37847139Sbostic synexpect(TENDCASE); 37947139Sbostic cpp = &cp->nclist.next; 38047139Sbostic } 38147139Sbostic *cpp = NULL; 38247139Sbostic if (lasttoken != TESAC) 38347139Sbostic synexpect(TESAC); 38447981Smarc checkkwd = 1; 38547139Sbostic break; 38647139Sbostic case TLP: 38747139Sbostic n1 = (union node *)stalloc(sizeof (struct nredir)); 38847139Sbostic n1->type = NSUBSHELL; 38947139Sbostic n1->nredir.n = list(0); 39047139Sbostic n1->nredir.redirect = NULL; 39147139Sbostic if (readtoken() != TRP) 39247139Sbostic synexpect(TRP); 39347981Smarc checkkwd = 1; 39447139Sbostic break; 39547139Sbostic case TBEGIN: 39647139Sbostic n1 = list(0); 39747139Sbostic if (readtoken() != TEND) 39847139Sbostic synexpect(TEND); 39947981Smarc checkkwd = 1; 40047139Sbostic break; 40147139Sbostic case TWORD: 40247139Sbostic case TREDIR: 40347139Sbostic tokpushback++; 40447139Sbostic return simplecmd(); 40547139Sbostic default: 40647139Sbostic synexpect(-1); 40747139Sbostic } 40847139Sbostic 40947139Sbostic /* Now check for redirection which may follow command */ 41047139Sbostic rpp = &redir; 41147139Sbostic while (readtoken() == TREDIR) { 41247139Sbostic *rpp = n2 = redirnode; 41347139Sbostic rpp = &n2->nfile.next; 41447139Sbostic parsefname(); 41547139Sbostic } 41647139Sbostic tokpushback++; 41747139Sbostic *rpp = NULL; 41847139Sbostic if (redir) { 41947139Sbostic if (n1->type != NSUBSHELL) { 42047139Sbostic n2 = (union node *)stalloc(sizeof (struct nredir)); 42147139Sbostic n2->type = NREDIR; 42247139Sbostic n2->nredir.n = n1; 42347139Sbostic n1 = n2; 42447139Sbostic } 42547139Sbostic n1->nredir.redirect = redir; 42647139Sbostic } 42747139Sbostic return n1; 42847139Sbostic } 42947139Sbostic 43047139Sbostic 43147139Sbostic STATIC union node * 43247139Sbostic simplecmd() { 43347139Sbostic union node *args, **app; 43447139Sbostic union node *redir, **rpp; 43547139Sbostic union node *n; 43647139Sbostic 43747139Sbostic args = NULL; 43847139Sbostic app = &args; 43947139Sbostic rpp = &redir; 44047139Sbostic for (;;) { 44147139Sbostic if (readtoken() == TWORD) { 44247139Sbostic n = (union node *)stalloc(sizeof (struct narg)); 44347139Sbostic n->type = NARG; 44447139Sbostic n->narg.text = wordtext; 44547139Sbostic n->narg.backquote = backquotelist; 44647139Sbostic *app = n; 44747139Sbostic app = &n->narg.next; 44847139Sbostic } else if (lasttoken == TREDIR) { 44947139Sbostic *rpp = n = redirnode; 45047139Sbostic rpp = &n->nfile.next; 45147139Sbostic parsefname(); /* read name of redirection file */ 45247139Sbostic } else if (lasttoken == TLP && app == &args->narg.next 45347139Sbostic && rpp == &redir) { 45447139Sbostic /* We have a function */ 45547139Sbostic if (readtoken() != TRP) 45647139Sbostic synexpect(TRP); 45747300Smarc #ifdef notdef 45847139Sbostic if (! goodname(n->narg.text)) 45947139Sbostic synerror("Bad function name"); 46047300Smarc #endif 46147139Sbostic n->type = NDEFUN; 46247139Sbostic n->narg.next = command(); 46347139Sbostic return n; 46447139Sbostic } else { 46547139Sbostic tokpushback++; 46647139Sbostic break; 46747139Sbostic } 46847139Sbostic } 46947139Sbostic *app = NULL; 47047139Sbostic *rpp = NULL; 47147139Sbostic n = (union node *)stalloc(sizeof (struct ncmd)); 47247139Sbostic n->type = NCMD; 47347139Sbostic n->ncmd.backgnd = 0; 47447139Sbostic n->ncmd.args = args; 47547139Sbostic n->ncmd.redirect = redir; 47647139Sbostic return n; 47747139Sbostic } 47847139Sbostic 47947139Sbostic 48047139Sbostic STATIC void 48147139Sbostic parsefname() { 48247139Sbostic union node *n = redirnode; 48347139Sbostic 48447139Sbostic if (readtoken() != TWORD) 48547139Sbostic synexpect(-1); 48647139Sbostic if (n->type == NHERE) { 48747139Sbostic struct heredoc *here = heredoc; 48847139Sbostic struct heredoc *p; 48947139Sbostic int i; 49047139Sbostic 49147139Sbostic if (quoteflag == 0) 49247139Sbostic n->type = NXHERE; 49347139Sbostic TRACE(("Here document %d\n", n->type)); 49447139Sbostic if (here->striptabs) { 49547139Sbostic while (*wordtext == '\t') 49647139Sbostic wordtext++; 49747139Sbostic } 49847139Sbostic if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) 49947139Sbostic synerror("Illegal eof marker for << redirection"); 50047139Sbostic rmescapes(wordtext); 50147139Sbostic here->eofmark = wordtext; 50247139Sbostic here->next = NULL; 50347139Sbostic if (heredoclist == NULL) 50447139Sbostic heredoclist = here; 50547139Sbostic else { 50647139Sbostic for (p = heredoclist ; p->next ; p = p->next); 50747139Sbostic p->next = here; 50847139Sbostic } 50947139Sbostic } else if (n->type == NTOFD || n->type == NFROMFD) { 51047139Sbostic if (is_digit(wordtext[0])) 51147139Sbostic n->ndup.dupfd = digit_val(wordtext[0]); 51247139Sbostic else if (wordtext[0] == '-') 51347139Sbostic n->ndup.dupfd = -1; 51447139Sbostic else 51547139Sbostic goto bad; 51647139Sbostic if (wordtext[1] != '\0') { 51747139Sbostic bad: 51847139Sbostic synerror("Bad fd number"); 51947139Sbostic } 52047139Sbostic } else { 52147139Sbostic n->nfile.fname = (union node *)stalloc(sizeof (struct narg)); 52247139Sbostic n = n->nfile.fname; 52347139Sbostic n->type = NARG; 52447139Sbostic n->narg.next = NULL; 52547139Sbostic n->narg.text = wordtext; 52647139Sbostic n->narg.backquote = backquotelist; 52747139Sbostic } 52847139Sbostic } 52947139Sbostic 53047139Sbostic 53147139Sbostic /* 53247139Sbostic * Input any here documents. 53347139Sbostic */ 53447139Sbostic 53547139Sbostic STATIC void 53647139Sbostic parseheredoc() { 53747139Sbostic struct heredoc *here; 53847139Sbostic union node *n; 53947139Sbostic 54047139Sbostic while (heredoclist) { 54147139Sbostic here = heredoclist; 54247139Sbostic heredoclist = here->next; 54347139Sbostic if (needprompt) { 54447139Sbostic putprompt(ps2val()); 54547139Sbostic needprompt = 0; 54647139Sbostic } 54747139Sbostic readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, 54847139Sbostic here->eofmark, here->striptabs); 54947139Sbostic n = (union node *)stalloc(sizeof (struct narg)); 55047139Sbostic n->narg.type = NARG; 55147139Sbostic n->narg.next = NULL; 55247139Sbostic n->narg.text = wordtext; 55347139Sbostic n->narg.backquote = backquotelist; 55447139Sbostic here->here->nhere.doc = n; 55547139Sbostic } 55647139Sbostic } 55747139Sbostic 55847981Smarc STATIC int 55947981Smarc peektoken() { 56047139Sbostic int t; 56147139Sbostic 56247981Smarc t = readtoken(); 56347139Sbostic tokpushback++; 56447981Smarc return (t); 56547139Sbostic } 56647139Sbostic 56747139Sbostic STATIC int xxreadtoken(); 56847139Sbostic 56947139Sbostic STATIC int 57047139Sbostic readtoken() { 57147139Sbostic int t; 57247981Smarc #ifdef DEBUG 57347981Smarc int alreadyseen = tokpushback; 57447981Smarc #endif 57547981Smarc 57647981Smarc t = xxreadtoken(); 57747139Sbostic 57847981Smarc if (checkkwd) { 57947981Smarc /* 58047981Smarc * eat newlines 58147981Smarc */ 58247981Smarc if (checkkwd == 2) { 58347981Smarc checkkwd = 0; 58447981Smarc while (t == TNL) { 58547981Smarc parseheredoc(); 58647981Smarc t = xxreadtoken(); 58747981Smarc } 58847981Smarc } else 58947981Smarc checkkwd = 0; 59047981Smarc /* 59147981Smarc * check for keywords 59247981Smarc */ 59347981Smarc if (t == TWORD && !quoteflag) { 59447981Smarc register char **pp; 59547981Smarc 59647981Smarc for (pp = parsekwd; *pp; pp++) { 59747981Smarc if (**pp == *wordtext && equal(*pp, wordtext)) { 59847981Smarc lasttoken = t = pp - parsekwd + KWDOFFSET; 59947981Smarc TRACE(("keyword %s recognized\n", tokname[t])); 60047981Smarc break; 60147981Smarc } 60247981Smarc } 60347981Smarc } 60447139Sbostic } 60547981Smarc #ifdef DEBUG 60647981Smarc if (!alreadyseen) 60747981Smarc TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 60847981Smarc else 60947981Smarc TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : "")); 61047981Smarc #endif 61147981Smarc return (t); 61247139Sbostic } 61347139Sbostic 61447139Sbostic 61547139Sbostic /* 61647139Sbostic * Read the next input token. 61747139Sbostic * If the token is a word, we set backquotelist to the list of cmds in 61847139Sbostic * backquotes. We set quoteflag to true if any part of the word was 61947139Sbostic * quoted. 62047139Sbostic * If the token is TREDIR, then we set redirnode to a structure containing 62147139Sbostic * the redirection. 62247139Sbostic * In all cases, the variable startlinno is set to the number of the line 62347139Sbostic * on which the token starts. 62447139Sbostic * 62547139Sbostic * [Change comment: here documents and internal procedures] 62647139Sbostic * [Readtoken shouldn't have any arguments. Perhaps we should make the 62747139Sbostic * word parsing code into a separate routine. In this case, readtoken 62847139Sbostic * doesn't need to have any internal procedures, but parseword does. 62947139Sbostic * We could also make parseoperator in essence the main routine, and 63047139Sbostic * have parseword (readtoken1?) handle both words and redirection.] 63147139Sbostic */ 63247139Sbostic 63347139Sbostic #define RETURN(token) return lasttoken = token 63447139Sbostic 63547139Sbostic STATIC int 63647139Sbostic xxreadtoken() { 63747139Sbostic register c; 63847139Sbostic 63947139Sbostic if (tokpushback) { 64047139Sbostic tokpushback = 0; 64147139Sbostic return lasttoken; 64247139Sbostic } 64347139Sbostic if (needprompt) { 64447139Sbostic putprompt(ps2val()); 64547139Sbostic needprompt = 0; 64647139Sbostic } 64747139Sbostic startlinno = plinno; 64847139Sbostic for (;;) { /* until token or start of word found */ 64947139Sbostic c = pgetc_macro(); 65047139Sbostic if (c == ' ' || c == '\t') 65147139Sbostic continue; /* quick check for white space first */ 65247139Sbostic switch (c) { 65347139Sbostic case ' ': case '\t': 65447139Sbostic continue; 65547139Sbostic case '#': 65647139Sbostic while ((c = pgetc()) != '\n' && c != PEOF); 65747139Sbostic pungetc(); 65847139Sbostic continue; 65947139Sbostic case '\\': 66047139Sbostic if (pgetc() == '\n') { 66147139Sbostic startlinno = ++plinno; 66247139Sbostic if (doprompt) 66347139Sbostic putprompt(ps2val()); 66447139Sbostic continue; 66547139Sbostic } 66647139Sbostic pungetc(); 66747139Sbostic goto breakloop; 66847139Sbostic case '\n': 66947139Sbostic plinno++; 67047139Sbostic needprompt = doprompt; 67147139Sbostic RETURN(TNL); 67247139Sbostic case PEOF: 67347139Sbostic RETURN(TEOF); 67447139Sbostic case '&': 67547139Sbostic if (pgetc() == '&') 67647139Sbostic RETURN(TAND); 67747139Sbostic pungetc(); 67847139Sbostic RETURN(TBACKGND); 67947139Sbostic case '|': 68047139Sbostic if (pgetc() == '|') 68147139Sbostic RETURN(TOR); 68247139Sbostic pungetc(); 68347139Sbostic RETURN(TPIPE); 68447139Sbostic case ';': 68547139Sbostic if (pgetc() == ';') 68647139Sbostic RETURN(TENDCASE); 68747139Sbostic pungetc(); 68847139Sbostic RETURN(TSEMI); 68947139Sbostic case '(': 69047139Sbostic RETURN(TLP); 69147139Sbostic case ')': 69247139Sbostic RETURN(TRP); 69347139Sbostic default: 69447139Sbostic goto breakloop; 69547139Sbostic } 69647139Sbostic } 69747139Sbostic breakloop: 69847139Sbostic return readtoken1(c, BASESYNTAX, (char *)NULL, 0); 69947139Sbostic #undef RETURN 70047139Sbostic } 70147139Sbostic 70247139Sbostic 70347139Sbostic 70447139Sbostic /* 70547139Sbostic * If eofmark is NULL, read a word or a redirection symbol. If eofmark 70647139Sbostic * is not NULL, read a here document. In the latter case, eofmark is the 70747139Sbostic * word which marks the end of the document and striptabs is true if 70847139Sbostic * leading tabs should be stripped from the document. The argument firstc 70947139Sbostic * is the first character of the input token or document. 71047139Sbostic * 71147139Sbostic * Because C does not have internal subroutines, I have simulated them 71247139Sbostic * using goto's to implement the subroutine linkage. The following macros 71347139Sbostic * will run code that appears at the end of readtoken1. 71447139Sbostic */ 71547139Sbostic 71647139Sbostic #define CHECKEND() {goto checkend; checkend_return:;} 71747139Sbostic #define PARSEREDIR() {goto parseredir; parseredir_return:;} 71847139Sbostic #define PARSESUB() {goto parsesub; parsesub_return:;} 71947139Sbostic #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} 72047139Sbostic #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} 72147139Sbostic 72247139Sbostic STATIC int 72347139Sbostic readtoken1(firstc, syntax, eofmark, striptabs) 72447139Sbostic int firstc; 72547139Sbostic char const *syntax; 72647139Sbostic char *eofmark; 72747139Sbostic int striptabs; 72847139Sbostic { 72947139Sbostic register c = firstc; 73047139Sbostic register char *out; 73147139Sbostic int len; 73247139Sbostic char line[EOFMARKLEN + 1]; 73347139Sbostic struct nodelist *bqlist; 73447139Sbostic int quotef; 73547139Sbostic int dblquote; 73647139Sbostic int varnest; 73747139Sbostic int oldstyle; 73847139Sbostic 73947139Sbostic startlinno = plinno; 74047139Sbostic dblquote = 0; 74147139Sbostic if (syntax == DQSYNTAX) 74247139Sbostic dblquote = 1; 74347139Sbostic quotef = 0; 74447139Sbostic bqlist = NULL; 74547139Sbostic varnest = 0; 74647139Sbostic STARTSTACKSTR(out); 74747139Sbostic loop: { /* for each line, until end of word */ 74847139Sbostic #if ATTY 74947139Sbostic if (c == '\034' && doprompt 75047139Sbostic && attyset() && ! equal(termval(), "emacs")) { 75147139Sbostic attyline(); 75247139Sbostic if (syntax == BASESYNTAX) 75347139Sbostic return readtoken(); 75447139Sbostic c = pgetc(); 75547139Sbostic goto loop; 75647139Sbostic } 75747139Sbostic #endif 75847139Sbostic CHECKEND(); /* set c to PEOF if at end of here document */ 75947139Sbostic for (;;) { /* until end of line or end of word */ 76047139Sbostic CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */ 76147139Sbostic switch(syntax[c]) { 76247139Sbostic case CNL: /* '\n' */ 76347139Sbostic if (syntax == BASESYNTAX) 76447139Sbostic goto endword; /* exit outer loop */ 76547139Sbostic USTPUTC(c, out); 76647139Sbostic plinno++; 76747139Sbostic if (doprompt) { 76847139Sbostic putprompt(ps2val()); 76947139Sbostic } 77047139Sbostic c = pgetc(); 77147139Sbostic goto loop; /* continue outer loop */ 77247139Sbostic case CWORD: 77347139Sbostic USTPUTC(c, out); 77447139Sbostic break; 77547139Sbostic case CCTL: 77647139Sbostic if (eofmark == NULL || dblquote) 77747139Sbostic USTPUTC(CTLESC, out); 77847139Sbostic USTPUTC(c, out); 77947139Sbostic break; 78047139Sbostic case CBACK: /* backslash */ 78147139Sbostic c = pgetc(); 78247139Sbostic if (c == PEOF) { 78347139Sbostic USTPUTC('\\', out); 78447139Sbostic pungetc(); 78547139Sbostic } else if (c == '\n') { 78647139Sbostic if (doprompt) 78747139Sbostic putprompt(ps2val()); 78847139Sbostic } else { 78947139Sbostic if (dblquote && c != '\\' && c != '`' && c != '$' 79047139Sbostic && (c != '"' || eofmark != NULL)) 79147139Sbostic USTPUTC('\\', out); 79247139Sbostic if (SQSYNTAX[c] == CCTL) 79347139Sbostic USTPUTC(CTLESC, out); 79447139Sbostic USTPUTC(c, out); 79547139Sbostic quotef++; 79647139Sbostic } 79747139Sbostic break; 79847139Sbostic case CSQUOTE: 79947139Sbostic syntax = SQSYNTAX; 80047139Sbostic break; 80147139Sbostic case CDQUOTE: 80247139Sbostic syntax = DQSYNTAX; 80347139Sbostic dblquote = 1; 80447139Sbostic break; 80547139Sbostic case CENDQUOTE: 80647139Sbostic if (eofmark) { 80747139Sbostic USTPUTC(c, out); 80847139Sbostic } else { 80947139Sbostic syntax = BASESYNTAX; 81047139Sbostic quotef++; 81147139Sbostic dblquote = 0; 81247139Sbostic } 81347139Sbostic break; 81447139Sbostic case CVAR: /* '$' */ 81547139Sbostic PARSESUB(); /* parse substitution */ 81647139Sbostic break; 81747139Sbostic case CENDVAR: /* '}' */ 81847139Sbostic if (varnest > 0) { 81947139Sbostic varnest--; 82047139Sbostic USTPUTC(CTLENDVAR, out); 82147139Sbostic } else { 82247139Sbostic USTPUTC(c, out); 82347139Sbostic } 82447139Sbostic break; 82547139Sbostic case CBQUOTE: /* '`' */ 82647139Sbostic if (parsebackquote && syntax == BASESYNTAX) { 82747139Sbostic if (out == stackblock()) 82847139Sbostic return lasttoken = TENDBQUOTE; 82947139Sbostic else 83047139Sbostic goto endword; /* exit outer loop */ 83147139Sbostic } 83247139Sbostic PARSEBACKQOLD(); 83347139Sbostic break; 83447139Sbostic case CEOF: 83547139Sbostic goto endword; /* exit outer loop */ 83647139Sbostic default: 83747139Sbostic if (varnest == 0) 83847139Sbostic goto endword; /* exit outer loop */ 83947139Sbostic USTPUTC(c, out); 84047139Sbostic } 84147139Sbostic c = pgetc_macro(); 84247139Sbostic } 84347139Sbostic } 84447139Sbostic endword: 84547139Sbostic if (syntax != BASESYNTAX && eofmark == NULL) 84647139Sbostic synerror("Unterminated quoted string"); 84747139Sbostic if (varnest != 0) { 84847139Sbostic startlinno = plinno; 84947139Sbostic synerror("Missing '}'"); 85047139Sbostic } 85147139Sbostic USTPUTC('\0', out); 85247139Sbostic len = out - stackblock(); 85347139Sbostic out = stackblock(); 85447139Sbostic if (eofmark == NULL) { 85547139Sbostic if ((c == '>' || c == '<') 85647139Sbostic && quotef == 0 85747139Sbostic && len <= 2 85847139Sbostic && (*out == '\0' || is_digit(*out))) { 85947139Sbostic PARSEREDIR(); 86047139Sbostic return lasttoken = TREDIR; 86147139Sbostic } else { 86247139Sbostic pungetc(); 86347139Sbostic } 86447139Sbostic } 86547139Sbostic quoteflag = quotef; 86647139Sbostic backquotelist = bqlist; 86747139Sbostic grabstackblock(len); 86847139Sbostic wordtext = out; 86947139Sbostic return lasttoken = TWORD; 87047139Sbostic /* end of readtoken routine */ 87147139Sbostic 87247139Sbostic 87347139Sbostic 87447139Sbostic /* 87547139Sbostic * Check to see whether we are at the end of the here document. When this 87647139Sbostic * is called, c is set to the first character of the next input line. If 87747139Sbostic * we are at the end of the here document, this routine sets the c to PEOF. 87847139Sbostic */ 87947139Sbostic 88047139Sbostic checkend: { 88147139Sbostic if (eofmark) { 88247139Sbostic if (striptabs) { 88347139Sbostic while (c == '\t') 88447139Sbostic c = pgetc(); 88547139Sbostic } 88647139Sbostic if (c == *eofmark) { 88747139Sbostic if (pfgets(line, sizeof line) != NULL) { 88847139Sbostic register char *p, *q; 88947139Sbostic 89047139Sbostic p = line; 89147139Sbostic for (q = eofmark + 1 ; *q && *p == *q ; p++, q++); 89247139Sbostic if (*p == '\n' && *q == '\0') { 89347139Sbostic c = PEOF; 89447139Sbostic plinno++; 89547139Sbostic needprompt = doprompt; 89647139Sbostic } else { 89747139Sbostic ppushback(line, strlen(line)); 89847139Sbostic } 89947139Sbostic } 90047139Sbostic } 90147139Sbostic } 90247139Sbostic goto checkend_return; 90347139Sbostic } 90447139Sbostic 90547139Sbostic 90647139Sbostic /* 90747139Sbostic * Parse a redirection operator. The variable "out" points to a string 90847139Sbostic * specifying the fd to be redirected. The variable "c" contains the 90947139Sbostic * first character of the redirection operator. 91047139Sbostic */ 91147139Sbostic 91247139Sbostic parseredir: { 91347139Sbostic char fd = *out; 91447139Sbostic union node *np; 91547139Sbostic 91647139Sbostic np = (union node *)stalloc(sizeof (struct nfile)); 91747139Sbostic if (c == '>') { 91847139Sbostic np->nfile.fd = 1; 91947139Sbostic c = pgetc(); 92047139Sbostic if (c == '>') 92147139Sbostic np->type = NAPPEND; 92247139Sbostic else if (c == '&') 92347139Sbostic np->type = NTOFD; 92447139Sbostic else { 92547139Sbostic np->type = NTO; 92647139Sbostic pungetc(); 92747139Sbostic } 92847139Sbostic } else { /* c == '<' */ 92947139Sbostic np->nfile.fd = 0; 93047139Sbostic c = pgetc(); 93147139Sbostic if (c == '<') { 93247139Sbostic if (sizeof (struct nfile) != sizeof (struct nhere)) { 93347139Sbostic np = (union node *)stalloc(sizeof (struct nhere)); 93447139Sbostic np->nfile.fd = 0; 93547139Sbostic } 93647139Sbostic np->type = NHERE; 93747139Sbostic heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc)); 93847139Sbostic heredoc->here = np; 93947139Sbostic if ((c = pgetc()) == '-') { 94047139Sbostic heredoc->striptabs = 1; 94147139Sbostic } else { 94247139Sbostic heredoc->striptabs = 0; 94347139Sbostic pungetc(); 94447139Sbostic } 94547139Sbostic } else if (c == '&') 94647139Sbostic np->type = NFROMFD; 94747139Sbostic else { 94847139Sbostic np->type = NFROM; 94947139Sbostic pungetc(); 95047139Sbostic } 95147139Sbostic } 95247139Sbostic if (fd != '\0') 95347139Sbostic np->nfile.fd = digit_val(fd); 95447139Sbostic redirnode = np; 95547139Sbostic goto parseredir_return; 95647139Sbostic } 95747139Sbostic 95847139Sbostic 95947139Sbostic /* 96047139Sbostic * Parse a substitution. At this point, we have read the dollar sign 96147139Sbostic * and nothing else. 96247139Sbostic */ 96347139Sbostic 96447139Sbostic parsesub: { 96547139Sbostic int subtype; 96647139Sbostic int typeloc; 96747139Sbostic int flags; 96847139Sbostic char *p; 96947139Sbostic #ifndef GDB_HACK 97047139Sbostic static const char types[] = "}-+?="; 97147139Sbostic #endif 97247139Sbostic 97347139Sbostic c = pgetc(); 97447300Smarc if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) { 97547139Sbostic USTPUTC('$', out); 97647139Sbostic pungetc(); 97747139Sbostic } else if (c == '(') { /* $(command) */ 97847139Sbostic PARSEBACKQNEW(); 97947139Sbostic } else { 98047139Sbostic USTPUTC(CTLVAR, out); 98147139Sbostic typeloc = out - stackblock(); 98247139Sbostic USTPUTC(VSNORMAL, out); 98347139Sbostic subtype = VSNORMAL; 98447139Sbostic if (c == '{') { 98547139Sbostic c = pgetc(); 98647139Sbostic subtype = 0; 98747139Sbostic } 98847139Sbostic if (is_name(c)) { 98947139Sbostic do { 99047139Sbostic STPUTC(c, out); 99147139Sbostic c = pgetc(); 99247139Sbostic } while (is_in_name(c)); 99347139Sbostic } else { 99447139Sbostic if (! is_special(c)) 99547300Smarc badsub: synerror("Bad substitution"); 99647139Sbostic USTPUTC(c, out); 99747139Sbostic c = pgetc(); 99847139Sbostic } 99947139Sbostic STPUTC('=', out); 100047139Sbostic flags = 0; 100147139Sbostic if (subtype == 0) { 100247139Sbostic if (c == ':') { 100347139Sbostic flags = VSNUL; 100447139Sbostic c = pgetc(); 100547139Sbostic } 100647139Sbostic p = strchr(types, c); 100747139Sbostic if (p == NULL) 100847139Sbostic goto badsub; 100947139Sbostic subtype = p - types + VSNORMAL; 101047139Sbostic } else { 101147139Sbostic pungetc(); 101247139Sbostic } 101347139Sbostic if (dblquote) 101447139Sbostic flags |= VSQUOTE; 101547139Sbostic *(stackblock() + typeloc) = subtype | flags; 101647139Sbostic if (subtype != VSNORMAL) 101747139Sbostic varnest++; 101847139Sbostic } 101947139Sbostic goto parsesub_return; 102047139Sbostic } 102147139Sbostic 102247139Sbostic 102347139Sbostic /* 102447139Sbostic * Called to parse command substitutions. Newstyle is set if the command 102547139Sbostic * is enclosed inside $(...); nlpp is a pointer to the head of the linked 102647139Sbostic * list of commands (passed by reference), and savelen is the number of 102747139Sbostic * characters on the top of the stack which must be preserved. 102847139Sbostic */ 102947139Sbostic 103047139Sbostic parsebackq: { 103147139Sbostic struct nodelist **nlpp; 103247139Sbostic int savepbq; 103347139Sbostic union node *n; 103447139Sbostic char *volatile str; 103547139Sbostic struct jmploc jmploc; 103647139Sbostic struct jmploc *volatile savehandler; 103747139Sbostic int savelen; 103847139Sbostic int t; 103947139Sbostic 104047139Sbostic savepbq = parsebackquote; 104147139Sbostic if (setjmp(jmploc.loc)) { 104247139Sbostic if (str) 104347139Sbostic ckfree(str); 104447139Sbostic parsebackquote = 0; 104547139Sbostic handler = savehandler; 104647139Sbostic longjmp(handler, 1); 104747139Sbostic } 104847139Sbostic INTOFF; 104947139Sbostic str = NULL; 105047139Sbostic savelen = out - stackblock(); 105147139Sbostic if (savelen > 0) { 105247139Sbostic str = ckmalloc(savelen); 105347139Sbostic bcopy(stackblock(), str, savelen); 105447139Sbostic } 105547139Sbostic savehandler = handler; 105647139Sbostic handler = &jmploc; 105747139Sbostic INTON; 105847139Sbostic nlpp = &bqlist; 105947139Sbostic while (*nlpp) 106047139Sbostic nlpp = &(*nlpp)->next; 106147139Sbostic *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist)); 106247139Sbostic (*nlpp)->next = NULL; 106347139Sbostic parsebackquote = oldstyle; 106447139Sbostic n = list(0); 106547139Sbostic t = oldstyle? TENDBQUOTE : TRP; 106647139Sbostic if (readtoken() != t) 106747139Sbostic synexpect(t); 106847139Sbostic (*nlpp)->n = n; 106947139Sbostic while (stackblocksize() <= savelen) 107047139Sbostic growstackblock(); 107147139Sbostic STARTSTACKSTR(out); 107247139Sbostic if (str) { 107347139Sbostic bcopy(str, out, savelen); 107447139Sbostic STADJUST(savelen, out); 107547139Sbostic INTOFF; 107647139Sbostic ckfree(str); 107747139Sbostic str = NULL; 107847139Sbostic INTON; 107947139Sbostic } 108047139Sbostic parsebackquote = savepbq; 108147139Sbostic handler = savehandler; 108247139Sbostic USTPUTC(CTLBACKQ + dblquote, out); 108347139Sbostic if (oldstyle) 108447139Sbostic goto parsebackq_oldreturn; 108547139Sbostic else 108647139Sbostic goto parsebackq_newreturn; 108747139Sbostic } 108847139Sbostic 108947139Sbostic } /* end of readtoken */ 109047139Sbostic 109147139Sbostic 109247139Sbostic 109347139Sbostic #ifdef mkinit 109447139Sbostic RESET { 109547139Sbostic tokpushback = 0; 109647139Sbostic } 109747139Sbostic #endif 109847139Sbostic 109947139Sbostic 110047139Sbostic #if ATTY 110147139Sbostic /* 110247139Sbostic * Called to process a command generated by atty. We execute the line, 110347139Sbostic * and catch any errors that occur so they don't propagate outside of 110447139Sbostic * this routine. 110547139Sbostic */ 110647139Sbostic 110747139Sbostic STATIC void 110847139Sbostic attyline() { 110947139Sbostic char line[256]; 111047139Sbostic struct stackmark smark; 111147139Sbostic struct jmploc jmploc; 111247139Sbostic struct jmploc *volatile savehandler; 111347139Sbostic 111447139Sbostic if (pfgets(line, sizeof line) == NULL) 111547139Sbostic return; /* "can't happen" */ 111647139Sbostic if (setjmp(jmploc.loc)) { 111747139Sbostic if (exception == EXERROR) 111847139Sbostic out2str("\033]D\n"); 111947139Sbostic handler = savehandler; 112047139Sbostic longjmp(handler, 1); 112147139Sbostic } 112247139Sbostic savehandler = handler; 112347139Sbostic handler = &jmploc; 112447139Sbostic setstackmark(&smark); 112547139Sbostic evalstring(line); 112647139Sbostic popstackmark(&smark); 112747139Sbostic handler = savehandler; 112847139Sbostic doprompt = 1; 112947139Sbostic } 113047139Sbostic 113147139Sbostic 113247139Sbostic /* 113347139Sbostic * Output a prompt for atty. We output the prompt as part of the 113447139Sbostic * appropriate escape sequence. 113547139Sbostic */ 113647139Sbostic 113747139Sbostic STATIC void 113847139Sbostic putprompt(s) 113947139Sbostic char *s; 114047139Sbostic { 114147139Sbostic register char *p; 114247139Sbostic 114347139Sbostic if (attyset() && ! equal(termval(), "emacs")) { 114447139Sbostic if (strchr(s, '\7')) 114547139Sbostic out2c('\7'); 114647139Sbostic out2str("\033]P1;"); 114747139Sbostic for (p = s ; *p ; p++) { 114847139Sbostic if ((unsigned)(*p - ' ') <= '~' - ' ') 114947139Sbostic out2c(*p); 115047139Sbostic } 115147139Sbostic out2c('\n'); 115247139Sbostic } else { 115347139Sbostic out2str(s); 115447139Sbostic } 115547139Sbostic } 115647139Sbostic #endif 115747139Sbostic 115847139Sbostic 115947139Sbostic 116047139Sbostic /* 116147139Sbostic * Returns true if the text contains nothing to expand (no dollar signs 116247139Sbostic * or backquotes). 116347139Sbostic */ 116447139Sbostic 116547139Sbostic STATIC int 116647139Sbostic noexpand(text) 116747139Sbostic char *text; 116847139Sbostic { 116947139Sbostic register char *p; 117047139Sbostic register char c; 117147139Sbostic 117247139Sbostic p = text; 117347139Sbostic while ((c = *p++) != '\0') { 117447139Sbostic if (c == CTLESC) 117547139Sbostic p++; 117647139Sbostic else if (BASESYNTAX[c] == CCTL) 117747139Sbostic return 0; 117847139Sbostic } 117947139Sbostic return 1; 118047139Sbostic } 118147139Sbostic 118247139Sbostic 118347139Sbostic /* 118447139Sbostic * Return true if the argument is a legal variable name (a letter or 118547139Sbostic * underscore followed by zero or more letters, underscores, and digits). 118647139Sbostic */ 118747139Sbostic 118847139Sbostic int 118947139Sbostic goodname(name) 119047139Sbostic char *name; 119147139Sbostic { 119247139Sbostic register char *p; 119347139Sbostic 119447139Sbostic p = name; 119547139Sbostic if (! is_name(*p)) 119647139Sbostic return 0; 119747139Sbostic while (*++p) { 119847139Sbostic if (! is_in_name(*p)) 119947139Sbostic return 0; 120047139Sbostic } 120147139Sbostic return 1; 120247139Sbostic } 120347139Sbostic 120447139Sbostic 120547139Sbostic /* 120647139Sbostic * Called when an unexpected token is read during the parse. The argument 120747139Sbostic * is the token that is expected, or -1 if more than one type of token can 120847139Sbostic * occur at this point. 120947139Sbostic */ 121047139Sbostic 121147139Sbostic STATIC void 121247139Sbostic synexpect(token) { 121347139Sbostic char msg[64]; 121447139Sbostic 121547139Sbostic if (token >= 0) { 121647139Sbostic fmtstr(msg, 64, "%s unexpected (expecting %s)", 121747139Sbostic tokname[lasttoken], tokname[token]); 121847139Sbostic } else { 121947139Sbostic fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]); 122047139Sbostic } 122147139Sbostic synerror(msg); 122247139Sbostic } 122347139Sbostic 122447139Sbostic 122547139Sbostic STATIC void 122647139Sbostic synerror(msg) 122747139Sbostic char *msg; 122847139Sbostic { 122947139Sbostic if (commandname) 123047139Sbostic outfmt(&errout, "%s: %d: ", commandname, startlinno); 123147139Sbostic outfmt(&errout, "Syntax error: %s\n", msg); 123247139Sbostic error((char *)NULL); 123347139Sbostic } 1234