From cbb07feb2f78629297b21609f526fcec6e4ad8ee Mon Sep 17 00:00:00 2001 From: zrj Date: Tue, 6 Feb 2018 23:56:48 +0200 Subject: [PATCH] sh(1): Add and use pregenerated headers. These only add 30k to the repo while ensuring that bin/sh would not be affected by bad shell script invocation etc. As the bonus it solves the issue in share/initrd too. Nothing in the generated headers should fail for our use cases. If that would change, please report it. --- bin/sh/Makefile | 24 ++- bin/sh/mknodes/Makefile | 10 + bin/sh/mksyntax/Makefile | 10 + bin/sh/pregenerated/builtins.c | 101 +++++++++ bin/sh/pregenerated/builtins.h | 97 +++++++++ bin/sh/pregenerated/nodes.c | 368 +++++++++++++++++++++++++++++++++ bin/sh/pregenerated/nodes.h | 163 +++++++++++++++ bin/sh/pregenerated/syntax.c | 192 +++++++++++++++++ bin/sh/pregenerated/syntax.h | 54 +++++ bin/sh/pregenerated/token.h | 112 ++++++++++ share/initrd/bin/Makefile | 3 +- share/mk/bsd.crunchgen.mk | 2 +- 12 files changed, 1124 insertions(+), 12 deletions(-) create mode 100644 bin/sh/mknodes/Makefile create mode 100644 bin/sh/mksyntax/Makefile create mode 100644 bin/sh/pregenerated/builtins.c create mode 100644 bin/sh/pregenerated/builtins.h create mode 100644 bin/sh/pregenerated/nodes.c create mode 100644 bin/sh/pregenerated/nodes.h create mode 100644 bin/sh/pregenerated/syntax.c create mode 100644 bin/sh/pregenerated/syntax.h create mode 100644 bin/sh/pregenerated/token.h diff --git a/bin/sh/Makefile b/bin/sh/Makefile index 8549946b9d..8b9b843281 100644 --- a/bin/sh/Makefile +++ b/bin/sh/Makefile @@ -41,12 +41,17 @@ LDFLAGS+= ${PRIVATELIB_LDFLAGS} ${.CURDIR}/../test \ ${.CURDIR}/../../usr.bin/printf -CLEANFILES+= mknodes.nx mknodes.no \ - mksyntax.nx mksyntax.no +# +# NOTE: the headers and sources in pregenerated/ must be updated upon any +# changes to the nodetypes nodes.c.pat mkbuiltins mknodes.c mksyntax.c mktokens +# files using following scripts and build programs. +# +.if 1 # not updating sh(1) +.PATH: ${.CURDIR}/pregenerated +CFLAGS+= -I${.CURDIR}/pregenerated +.else CLEANFILES+= ${GENSRCS} ${GENHDRS} -build-tools: mknodes.nx mksyntax.nx - .ORDER: builtins.c builtins.h builtins.c builtins.h: mkbuiltins builtins.def sh ${.CURDIR}/mkbuiltins ${.CURDIR} @@ -56,19 +61,18 @@ builtins.c builtins.h: mkbuiltins builtins.def # XXX we have a default .c rule, but no default .o rule. .o: ${CC} ${CFLAGS} ${LDFLAGS} ${.IMPSRC} ${LDLIBS} -o ${.TARGET} -mknodes.nx: mknodes.no -mksyntax.nx: mksyntax.no .ORDER: nodes.c nodes.h -nodes.c nodes.h: mknodes.nx nodetypes nodes.c.pat - ./mknodes.nx ${.CURDIR}/nodetypes ${.CURDIR}/nodes.c.pat +nodes.c nodes.h: nodetypes nodes.c.pat + ${.OBJDIR}/mknodes/mknodes.nx ${.ALLSRC} .ORDER: syntax.c syntax.h -syntax.c syntax.h: mksyntax.nx - ./mksyntax.nx +syntax.c syntax.h: + ${.OBJDIR}/mksyntax/mksyntax.nx token.h: mktokens sh ${.CURDIR}/mktokens +.endif regress: sh cd ${.CURDIR}/../../tools/regression/bin/sh && ${MAKE} SH=${.OBJDIR}/sh diff --git a/bin/sh/mknodes/Makefile b/bin/sh/mknodes/Makefile new file mode 100644 index 0000000000..b8fb8e54a4 --- /dev/null +++ b/bin/sh/mknodes/Makefile @@ -0,0 +1,10 @@ +.PATH: ${.CURDIR}/.. + +PROG= mknodes + +build-tools: mknodes.nx + +# this mknodes is used in place, it is not installed anywhere +install: + +.include diff --git a/bin/sh/mksyntax/Makefile b/bin/sh/mksyntax/Makefile new file mode 100644 index 0000000000..59b413377f --- /dev/null +++ b/bin/sh/mksyntax/Makefile @@ -0,0 +1,10 @@ +.PATH: ${.CURDIR}/.. + +PROG= mksyntax + +build-tools: mksyntax.nx + +# this mksyntax is used in place, it is not installed anywhere +install: + +.include diff --git a/bin/sh/pregenerated/builtins.c b/bin/sh/pregenerated/builtins.c new file mode 100644 index 0000000000..cabca99a8a --- /dev/null +++ b/bin/sh/pregenerated/builtins.c @@ -0,0 +1,101 @@ +/* + * This file was generated by the mkbuiltins program. + */ + +#include +#include "shell.h" +#include "builtins.h" + +int (*const builtinfunc[])(int, char **) = { + bltincmd, + aliascmd, + bgcmd, + bindcmd, + breakcmd, + cdcmd, + commandcmd, + dotcmd, + echocmd, + evalcmd, + execcmd, + exitcmd, + letcmd, + exportcmd, + falsecmd, + fgcmd, + getoptscmd, + hashcmd, + histcmd, + jobidcmd, + jobscmd, + killcmd, + localcmd, + printfcmd, + pwdcmd, + readcmd, + returncmd, + setcmd, + setvarcmd, + shiftcmd, + testcmd, + timescmd, + trapcmd, + truecmd, + typecmd, + ulimitcmd, + umaskcmd, + unaliascmd, + unsetcmd, + waitcmd, + wordexpcmd, +}; + +const struct builtincmd builtincmd[] = { + { "builtin", 0, 0 }, + { "alias", 1, 0 }, + { "bg", 2, 0 }, + { "bind", 3, 0 }, + { "break", 4, 1 }, + { "continue", 4, 1 }, + { "cd", 5, 0 }, + { "chdir", 5, 0 }, + { "command", 6, 0 }, + { ".", 7, 1 }, + { "echo", 8, 0 }, + { "eval", 9, 1 }, + { "exec", 10, 1 }, + { "exit", 11, 1 }, + { "let", 12, 0 }, + { "export", 13, 1 }, + { "readonly", 13, 1 }, + { "false", 14, 0 }, + { "fg", 15, 0 }, + { "getopts", 16, 0 }, + { "hash", 17, 0 }, + { "fc", 18, 0 }, + { "jobid", 19, 0 }, + { "jobs", 20, 0 }, + { "kill", 21, 0 }, + { "local", 22, 0 }, + { "printf", 23, 0 }, + { "pwd", 24, 0 }, + { "read", 25, 0 }, + { "return", 26, 1 }, + { "set", 27, 1 }, + { "setvar", 28, 0 }, + { "shift", 29, 1 }, + { "test", 30, 0 }, + { "[", 30, 0 }, + { "times", 31, 1 }, + { "trap", 32, 1 }, + { ":", 33, 1 }, + { "true", 33, 0 }, + { "type", 34, 0 }, + { "ulimit", 35, 0 }, + { "umask", 36, 0 }, + { "unalias", 37, 0 }, + { "unset", 38, 1 }, + { "wait", 39, 0 }, + { "wordexp", 40, 0 }, + { NULL, 0, 0 } +}; diff --git a/bin/sh/pregenerated/builtins.h b/bin/sh/pregenerated/builtins.h new file mode 100644 index 0000000000..cade05ff64 --- /dev/null +++ b/bin/sh/pregenerated/builtins.h @@ -0,0 +1,97 @@ +/* + * This file was generated by the mkbuiltins program. + */ + +#include +#define BLTINCMD 0 +#define ALIASCMD 1 +#define BGCMD 2 +#define BINDCMD 3 +#define BREAKCMD 4 +#define CDCMD 5 +#define COMMANDCMD 6 +#define DOTCMD 7 +#define ECHOCMD 8 +#define EVALCMD 9 +#define EXECCMD 10 +#define EXITCMD 11 +#define LETCMD 12 +#define EXPORTCMD 13 +#define FALSECMD 14 +#define FGCMD 15 +#define GETOPTSCMD 16 +#define HASHCMD 17 +#define HISTCMD 18 +#define JOBIDCMD 19 +#define JOBSCMD 20 +#define KILLCMD 21 +#define LOCALCMD 22 +#define PRINTFCMD 23 +#define PWDCMD 24 +#define READCMD 25 +#define RETURNCMD 26 +#define SETCMD 27 +#define SETVARCMD 28 +#define SHIFTCMD 29 +#define TESTCMD 30 +#define TIMESCMD 31 +#define TRAPCMD 32 +#define TRUECMD 33 +#define TYPECMD 34 +#define ULIMITCMD 35 +#define UMASKCMD 36 +#define UNALIASCMD 37 +#define UNSETCMD 38 +#define WAITCMD 39 +#define WORDEXPCMD 40 + +struct builtincmd { + const char *name; + int code; + int special; +}; + +extern int (*const builtinfunc[])(int, char **); +extern const struct builtincmd builtincmd[]; + +int bltincmd(int, char **); +int aliascmd(int, char **); +int bgcmd(int, char **); +int bindcmd(int, char **); +int breakcmd(int, char **); +int cdcmd(int, char **); +int commandcmd(int, char **); +int dotcmd(int, char **); +int echocmd(int, char **); +int evalcmd(int, char **); +int execcmd(int, char **); +int exitcmd(int, char **); +int letcmd(int, char **); +int exportcmd(int, char **); +int falsecmd(int, char **); +int fgcmd(int, char **); +int getoptscmd(int, char **); +int hashcmd(int, char **); +int histcmd(int, char **); +int jobidcmd(int, char **); +int jobscmd(int, char **); +int killcmd(int, char **); +int localcmd(int, char **); +int printfcmd(int, char **); +int pwdcmd(int, char **); +int readcmd(int, char **); +int returncmd(int, char **); +int setcmd(int, char **); +int setvarcmd(int, char **); +int shiftcmd(int, char **); +int testcmd(int, char **); +int timescmd(int, char **); +int trapcmd(int, char **); +int truecmd(int, char **); +int typecmd(int, char **); +int ulimitcmd(int, char **); +int umaskcmd(int, char **); +int unaliascmd(int, char **); +int unsetcmd(int, char **); +int waitcmd(int, char **); +int wordexpcmd(int, char **); diff --git a/bin/sh/pregenerated/nodes.c b/bin/sh/pregenerated/nodes.c new file mode 100644 index 0000000000..868553c96c --- /dev/null +++ b/bin/sh/pregenerated/nodes.c @@ -0,0 +1,368 @@ +/* + * This file was generated by the mknodes program. + */ + +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Kenneth Almquist. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)nodes.c.pat 8.2 (Berkeley) 5/4/95 + * $FreeBSD$ + */ + +#include +#include +#include +/* + * Routine for dealing with parsed shell commands. + */ + +#include "shell.h" +#include "nodes.h" +#include "memalloc.h" +#include "mystring.h" + + +static int funcblocksize; /* size of structures in function */ +static int funcstringsize; /* size of strings in node */ +static pointer funcblock; /* block to allocate function from */ +static char *funcstring; /* block to allocate strings from */ + +static const short nodesize[27] = { + ALIGN(sizeof (struct nbinary)), + ALIGN(sizeof (struct ncmd)), + ALIGN(sizeof (struct npipe)), + ALIGN(sizeof (struct nredir)), + ALIGN(sizeof (struct nredir)), + ALIGN(sizeof (struct nredir)), + ALIGN(sizeof (struct nbinary)), + ALIGN(sizeof (struct nbinary)), + ALIGN(sizeof (struct nif)), + ALIGN(sizeof (struct nbinary)), + ALIGN(sizeof (struct nbinary)), + ALIGN(sizeof (struct nfor)), + ALIGN(sizeof (struct ncase)), + ALIGN(sizeof (struct nclist)), + ALIGN(sizeof (struct nclist)), + ALIGN(sizeof (struct narg)), + ALIGN(sizeof (struct narg)), + ALIGN(sizeof (struct nfile)), + ALIGN(sizeof (struct nfile)), + ALIGN(sizeof (struct nfile)), + ALIGN(sizeof (struct nfile)), + ALIGN(sizeof (struct nfile)), + ALIGN(sizeof (struct ndup)), + ALIGN(sizeof (struct ndup)), + ALIGN(sizeof (struct nhere)), + ALIGN(sizeof (struct nhere)), + ALIGN(sizeof (struct nnot)), +}; + + +static void calcsize(union node *); +static void sizenodelist(struct nodelist *); +static union node *copynode(union node *); +static struct nodelist *copynodelist(struct nodelist *); +static char *nodesavestr(const char *); + + +struct funcdef { + unsigned int refcount; + union node n; +}; + +/* + * Make a copy of a parse tree. + */ + +struct funcdef * +copyfunc(union node *n) +{ + struct funcdef *fn; + + if (n == NULL) + return NULL; + funcblocksize = offsetof(struct funcdef, n); + funcstringsize = 0; + calcsize(n); + fn = ckmalloc(funcblocksize + funcstringsize); + fn->refcount = 1; + funcblock = (char *)fn + offsetof(struct funcdef, n); + funcstring = (char *)fn + funcblocksize; + copynode(n); + return fn; +} + + +union node * +getfuncnode(struct funcdef *fn) +{ + return fn == NULL ? NULL : &fn->n; +} + + +static void +calcsize(union node *n) +{ + if (n == NULL) + return; + funcblocksize += nodesize[n->type]; + switch (n->type) { + case NSEMI: + case NAND: + case NOR: + case NWHILE: + case NUNTIL: + calcsize(n->nbinary.ch2); + calcsize(n->nbinary.ch1); + break; + case NCMD: + calcsize(n->ncmd.redirect); + calcsize(n->ncmd.args); + break; + case NPIPE: + sizenodelist(n->npipe.cmdlist); + break; + case NREDIR: + case NBACKGND: + case NSUBSHELL: + calcsize(n->nredir.redirect); + calcsize(n->nredir.n); + break; + case NIF: + calcsize(n->nif.elsepart); + calcsize(n->nif.ifpart); + calcsize(n->nif.test); + break; + case NFOR: + funcstringsize += strlen(n->nfor.var) + 1; + calcsize(n->nfor.body); + calcsize(n->nfor.args); + break; + case NCASE: + calcsize(n->ncase.cases); + calcsize(n->ncase.expr); + break; + case NCLIST: + case NCLISTFALLTHRU: + calcsize(n->nclist.body); + calcsize(n->nclist.pattern); + calcsize(n->nclist.next); + break; + case NDEFUN: + case NARG: + sizenodelist(n->narg.backquote); + funcstringsize += strlen(n->narg.text) + 1; + calcsize(n->narg.next); + break; + case NTO: + case NFROM: + case NFROMTO: + case NAPPEND: + case NCLOBBER: + calcsize(n->nfile.fname); + calcsize(n->nfile.next); + break; + case NTOFD: + case NFROMFD: + calcsize(n->ndup.vname); + calcsize(n->ndup.next); + break; + case NHERE: + case NXHERE: + calcsize(n->nhere.doc); + calcsize(n->nhere.next); + break; + case NNOT: + calcsize(n->nnot.com); + break; + }; +} + + + +static void +sizenodelist(struct nodelist *lp) +{ + while (lp) { + funcblocksize += ALIGN(sizeof(struct nodelist)); + calcsize(lp->n); + lp = lp->next; + } +} + + + +static union node * +copynode(union node *n) +{ + union node *new; + + if (n == NULL) + return NULL; + new = funcblock; + funcblock = (char *)funcblock + nodesize[n->type]; + switch (n->type) { + case NSEMI: + case NAND: + case NOR: + case NWHILE: + case NUNTIL: + new->nbinary.ch2 = copynode(n->nbinary.ch2); + new->nbinary.ch1 = copynode(n->nbinary.ch1); + break; + case NCMD: + new->ncmd.redirect = copynode(n->ncmd.redirect); + new->ncmd.args = copynode(n->ncmd.args); + break; + case NPIPE: + new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); + new->npipe.backgnd = n->npipe.backgnd; + break; + case NREDIR: + case NBACKGND: + case NSUBSHELL: + new->nredir.redirect = copynode(n->nredir.redirect); + new->nredir.n = copynode(n->nredir.n); + break; + case NIF: + new->nif.elsepart = copynode(n->nif.elsepart); + new->nif.ifpart = copynode(n->nif.ifpart); + new->nif.test = copynode(n->nif.test); + break; + case NFOR: + new->nfor.var = nodesavestr(n->nfor.var); + new->nfor.body = copynode(n->nfor.body); + new->nfor.args = copynode(n->nfor.args); + break; + case NCASE: + new->ncase.cases = copynode(n->ncase.cases); + new->ncase.expr = copynode(n->ncase.expr); + break; + case NCLIST: + case NCLISTFALLTHRU: + new->nclist.body = copynode(n->nclist.body); + new->nclist.pattern = copynode(n->nclist.pattern); + new->nclist.next = copynode(n->nclist.next); + break; + case NDEFUN: + case NARG: + new->narg.backquote = copynodelist(n->narg.backquote); + new->narg.text = nodesavestr(n->narg.text); + new->narg.next = copynode(n->narg.next); + break; + case NTO: + case NFROM: + case NFROMTO: + case NAPPEND: + case NCLOBBER: + new->nfile.fname = copynode(n->nfile.fname); + new->nfile.next = copynode(n->nfile.next); + new->nfile.fd = n->nfile.fd; + break; + case NTOFD: + case NFROMFD: + new->ndup.vname = copynode(n->ndup.vname); + new->ndup.dupfd = n->ndup.dupfd; + new->ndup.next = copynode(n->ndup.next); + new->ndup.fd = n->ndup.fd; + break; + case NHERE: + case NXHERE: + new->nhere.doc = copynode(n->nhere.doc); + new->nhere.next = copynode(n->nhere.next); + new->nhere.fd = n->nhere.fd; + break; + case NNOT: + new->nnot.com = copynode(n->nnot.com); + break; + }; + new->type = n->type; + return new; +} + + +static struct nodelist * +copynodelist(struct nodelist *lp) +{ + struct nodelist *start; + struct nodelist **lpp; + + lpp = &start; + while (lp) { + *lpp = funcblock; + funcblock = (char *)funcblock + ALIGN(sizeof(struct nodelist)); + (*lpp)->n = copynode(lp->n); + lp = lp->next; + lpp = &(*lpp)->next; + } + *lpp = NULL; + return start; +} + + + +static char * +nodesavestr(const char *s) +{ + const char *p = s; + char *q = funcstring; + char *rtn = funcstring; + + while ((*q++ = *p++) != '\0') + continue; + funcstring = q; + return rtn; +} + + +void +reffunc(struct funcdef *fn) +{ + if (fn) + fn->refcount++; +} + + +/* + * Decrement the reference count of a function definition, freeing it + * if it falls to 0. + */ + +void +unreffunc(struct funcdef *fn) +{ + if (fn) { + fn->refcount--; + if (fn->refcount > 0) + return; + ckfree(fn); + } +} diff --git a/bin/sh/pregenerated/nodes.h b/bin/sh/pregenerated/nodes.h new file mode 100644 index 0000000000..dddbf60970 --- /dev/null +++ b/bin/sh/pregenerated/nodes.h @@ -0,0 +1,163 @@ +/* + * This file was generated by the mknodes program. + */ + +#define NSEMI 0 +#define NCMD 1 +#define NPIPE 2 +#define NREDIR 3 +#define NBACKGND 4 +#define NSUBSHELL 5 +#define NAND 6 +#define NOR 7 +#define NIF 8 +#define NWHILE 9 +#define NUNTIL 10 +#define NFOR 11 +#define NCASE 12 +#define NCLIST 13 +#define NCLISTFALLTHRU 14 +#define NDEFUN 15 +#define NARG 16 +#define NTO 17 +#define NFROM 18 +#define NFROMTO 19 +#define NAPPEND 20 +#define NCLOBBER 21 +#define NTOFD 22 +#define NFROMFD 23 +#define NHERE 24 +#define NXHERE 25 +#define NNOT 26 + + + +struct nbinary { + int type; + union node *ch1; + union node *ch2; +}; + + +struct ncmd { + int type; + union node *args; + union node *redirect; +}; + + +struct npipe { + int type; + int backgnd; + struct nodelist *cmdlist; +}; + + +struct nredir { + int type; + union node *n; + union node *redirect; +}; + + +struct nif { + int type; + union node *test; + union node *ifpart; + union node *elsepart; +}; + + +struct nfor { + int type; + union node *args; + union node *body; + char *var; +}; + + +struct ncase { + int type; + union node *expr; + union node *cases; +}; + + +struct nclist { + int type; + union node *next; + union node *pattern; + union node *body; +}; + + +struct narg { + int type; + union node *next; + char *text; + struct nodelist *backquote; +}; + + +struct nfile { + int type; + int fd; + union node *next; + union node *fname; + char *expfname; +}; + + +struct ndup { + int type; + int fd; + union node *next; + int dupfd; + union node *vname; +}; + + +struct nhere { + int type; + int fd; + union node *next; + union node *doc; + const char *expdoc; +}; + + +struct nnot { + int type; + union node *com; +}; + + +union node { + int type; + struct nbinary nbinary; + struct ncmd ncmd; + struct npipe npipe; + struct nredir nredir; + struct nif nif; + struct nfor nfor; + struct ncase ncase; + struct nclist nclist; + struct narg narg; + struct nfile nfile; + struct ndup ndup; + struct nhere nhere; + struct nnot nnot; +}; + + +struct nodelist { + struct nodelist *next; + union node *n; +}; + + +struct funcdef; +struct funcdef *copyfunc(union node *); +union node *getfuncnode(struct funcdef *); +void reffunc(struct funcdef *); +void unreffunc(struct funcdef *); diff --git a/bin/sh/pregenerated/syntax.c b/bin/sh/pregenerated/syntax.c new file mode 100644 index 0000000000..28f9b62d83 --- /dev/null +++ b/bin/sh/pregenerated/syntax.c @@ -0,0 +1,192 @@ +/* + * This file was generated by the mksyntax program. + */ + +#include "parser.h" +#include "shell.h" +#include "syntax.h" + +/* syntax table used when not in quotes */ +const char basesyntax[SYNBASE + CHAR_MAX + 1] = { + [SYNBASE + PEOF] = CEOF, + [SYNBASE + CTLESC] = CCTL, + [SYNBASE + CTLVAR] = CCTL, + [SYNBASE + CTLENDVAR] = CCTL, + [SYNBASE + CTLBACKQ] = CCTL, + [SYNBASE + CTLBACKQ + CTLQUOTE] = CCTL, + [SYNBASE + CTLARI] = CCTL, + [SYNBASE + CTLENDARI] = CCTL, + [SYNBASE + CTLQUOTEMARK] = CCTL, + [SYNBASE + CTLQUOTEEND] = CCTL, + [SYNBASE + '\n'] = CNL, + [SYNBASE + '\\'] = CBACK, + [SYNBASE + '\''] = CSQUOTE, + [SYNBASE + '"'] = CDQUOTE, + [SYNBASE + '`'] = CBQUOTE, + [SYNBASE + '$'] = CVAR, + [SYNBASE + '}'] = CENDVAR, + [SYNBASE + '<'] = CSPCL, + [SYNBASE + '>'] = CSPCL, + [SYNBASE + '('] = CSPCL, + [SYNBASE + ')'] = CSPCL, + [SYNBASE + ';'] = CSPCL, + [SYNBASE + '&'] = CSPCL, + [SYNBASE + '|'] = CSPCL, + [SYNBASE + ' '] = CSPCL, + [SYNBASE + '\t'] = CSPCL, +}; + +/* syntax table used when in double quotes */ +const char dqsyntax[SYNBASE + CHAR_MAX + 1] = { + [SYNBASE + PEOF] = CEOF, + [SYNBASE + CTLESC] = CCTL, + [SYNBASE + CTLVAR] = CCTL, + [SYNBASE + CTLENDVAR] = CCTL, + [SYNBASE + CTLBACKQ] = CCTL, + [SYNBASE + CTLBACKQ + CTLQUOTE] = CCTL, + [SYNBASE + CTLARI] = CCTL, + [SYNBASE + CTLENDARI] = CCTL, + [SYNBASE + CTLQUOTEMARK] = CCTL, + [SYNBASE + CTLQUOTEEND] = CCTL, + [SYNBASE + '\n'] = CNL, + [SYNBASE + '\\'] = CBACK, + [SYNBASE + '"'] = CENDQUOTE, + [SYNBASE + '`'] = CBQUOTE, + [SYNBASE + '$'] = CVAR, + [SYNBASE + '}'] = CENDVAR, + [SYNBASE + '!'] = CCTL, + [SYNBASE + '*'] = CCTL, + [SYNBASE + '?'] = CCTL, + [SYNBASE + '['] = CCTL, + [SYNBASE + ']'] = CCTL, + [SYNBASE + '='] = CCTL, + [SYNBASE + '~'] = CCTL, + [SYNBASE + ':'] = CCTL, + [SYNBASE + '/'] = CCTL, + [SYNBASE + '-'] = CCTL, + [SYNBASE + '^'] = CCTL, +}; + +/* syntax table used when in single quotes */ +const char sqsyntax[SYNBASE + CHAR_MAX + 1] = { + [SYNBASE + PEOF] = CEOF, + [SYNBASE + CTLESC] = CCTL, + [SYNBASE + CTLVAR] = CCTL, + [SYNBASE + CTLENDVAR] = CCTL, + [SYNBASE + CTLBACKQ] = CCTL, + [SYNBASE + CTLBACKQ + CTLQUOTE] = CCTL, + [SYNBASE + CTLARI] = CCTL, + [SYNBASE + CTLENDARI] = CCTL, + [SYNBASE + CTLQUOTEMARK] = CCTL, + [SYNBASE + CTLQUOTEEND] = CCTL, + [SYNBASE + '\n'] = CNL, + [SYNBASE + '\\'] = CSBACK, + [SYNBASE + '\''] = CENDQUOTE, + [SYNBASE + '!'] = CCTL, + [SYNBASE + '*'] = CCTL, + [SYNBASE + '?'] = CCTL, + [SYNBASE + '['] = CCTL, + [SYNBASE + ']'] = CCTL, + [SYNBASE + '='] = CCTL, + [SYNBASE + '~'] = CCTL, + [SYNBASE + ':'] = CCTL, + [SYNBASE + '/'] = CCTL, + [SYNBASE + '-'] = CCTL, + [SYNBASE + '^'] = CCTL, +}; + +/* syntax table used when in arithmetic */ +const char arisyntax[SYNBASE + CHAR_MAX + 1] = { + [SYNBASE + PEOF] = CEOF, + [SYNBASE + CTLESC] = CCTL, + [SYNBASE + CTLVAR] = CCTL, + [SYNBASE + CTLENDVAR] = CCTL, + [SYNBASE + CTLBACKQ] = CCTL, + [SYNBASE + CTLBACKQ + CTLQUOTE] = CCTL, + [SYNBASE + CTLARI] = CCTL, + [SYNBASE + CTLENDARI] = CCTL, + [SYNBASE + CTLQUOTEMARK] = CCTL, + [SYNBASE + CTLQUOTEEND] = CCTL, + [SYNBASE + '\n'] = CNL, + [SYNBASE + '\\'] = CBACK, + [SYNBASE + '`'] = CBQUOTE, + [SYNBASE + '"'] = CIGN, + [SYNBASE + '$'] = CVAR, + [SYNBASE + '}'] = CENDVAR, + [SYNBASE + '('] = CLP, + [SYNBASE + ')'] = CRP, +}; + +/* character classification table */ +const char is_type[SYNBASE + CHAR_MAX + 1] = { + [SYNBASE + '0'] = ISDIGIT, + [SYNBASE + '1'] = ISDIGIT, + [SYNBASE + '2'] = ISDIGIT, + [SYNBASE + '3'] = ISDIGIT, + [SYNBASE + '4'] = ISDIGIT, + [SYNBASE + '5'] = ISDIGIT, + [SYNBASE + '6'] = ISDIGIT, + [SYNBASE + '7'] = ISDIGIT, + [SYNBASE + '8'] = ISDIGIT, + [SYNBASE + '9'] = ISDIGIT, + [SYNBASE + 'a'] = ISLOWER, + [SYNBASE + 'b'] = ISLOWER, + [SYNBASE + 'c'] = ISLOWER, + [SYNBASE + 'd'] = ISLOWER, + [SYNBASE + 'e'] = ISLOWER, + [SYNBASE + 'f'] = ISLOWER, + [SYNBASE + 'g'] = ISLOWER, + [SYNBASE + 'h'] = ISLOWER, + [SYNBASE + 'i'] = ISLOWER, + [SYNBASE + 'j'] = ISLOWER, + [SYNBASE + 'k'] = ISLOWER, + [SYNBASE + 'l'] = ISLOWER, + [SYNBASE + 'm'] = ISLOWER, + [SYNBASE + 'n'] = ISLOWER, + [SYNBASE + 'o'] = ISLOWER, + [SYNBASE + 'p'] = ISLOWER, + [SYNBASE + 'q'] = ISLOWER, + [SYNBASE + 'r'] = ISLOWER, + [SYNBASE + 's'] = ISLOWER, + [SYNBASE + 't'] = ISLOWER, + [SYNBASE + 'u'] = ISLOWER, + [SYNBASE + 'v'] = ISLOWER, + [SYNBASE + 'w'] = ISLOWER, + [SYNBASE + 'x'] = ISLOWER, + [SYNBASE + 'y'] = ISLOWER, + [SYNBASE + 'z'] = ISLOWER, + [SYNBASE + 'A'] = ISUPPER, + [SYNBASE + 'B'] = ISUPPER, + [SYNBASE + 'C'] = ISUPPER, + [SYNBASE + 'D'] = ISUPPER, + [SYNBASE + 'E'] = ISUPPER, + [SYNBASE + 'F'] = ISUPPER, + [SYNBASE + 'G'] = ISUPPER, + [SYNBASE + 'H'] = ISUPPER, + [SYNBASE + 'I'] = ISUPPER, + [SYNBASE + 'J'] = ISUPPER, + [SYNBASE + 'K'] = ISUPPER, + [SYNBASE + 'L'] = ISUPPER, + [SYNBASE + 'M'] = ISUPPER, + [SYNBASE + 'N'] = ISUPPER, + [SYNBASE + 'O'] = ISUPPER, + [SYNBASE + 'P'] = ISUPPER, + [SYNBASE + 'Q'] = ISUPPER, + [SYNBASE + 'R'] = ISUPPER, + [SYNBASE + 'S'] = ISUPPER, + [SYNBASE + 'T'] = ISUPPER, + [SYNBASE + 'U'] = ISUPPER, + [SYNBASE + 'V'] = ISUPPER, + [SYNBASE + 'W'] = ISUPPER, + [SYNBASE + 'X'] = ISUPPER, + [SYNBASE + 'Y'] = ISUPPER, + [SYNBASE + 'Z'] = ISUPPER, + [SYNBASE + '_'] = ISUNDER, + [SYNBASE + '#'] = ISSPECL, + [SYNBASE + '?'] = ISSPECL, + [SYNBASE + '$'] = ISSPECL, + [SYNBASE + '!'] = ISSPECL, + [SYNBASE + '-'] = ISSPECL, + [SYNBASE + '*'] = ISSPECL, + [SYNBASE + '@'] = ISSPECL, +}; diff --git a/bin/sh/pregenerated/syntax.h b/bin/sh/pregenerated/syntax.h new file mode 100644 index 0000000000..e9cf2e9026 --- /dev/null +++ b/bin/sh/pregenerated/syntax.h @@ -0,0 +1,54 @@ +/* + * This file was generated by the mksyntax program. + */ + +#include +#include + +/* Syntax classes */ +#define CWORD 0 /* character is nothing special */ +#define CNL 1 /* newline character */ +#define CBACK 2 /* a backslash character */ +#define CSBACK 3 /* a backslash character in single quotes */ +#define CSQUOTE 4 /* single quote */ +#define CDQUOTE 5 /* double quote */ +#define CENDQUOTE 6 /* a terminating quote */ +#define CBQUOTE 7 /* backwards single quote */ +#define CVAR 8 /* a dollar sign */ +#define CENDVAR 9 /* a '}' character */ +#define CLP 10 /* a left paren in arithmetic */ +#define CRP 11 /* a right paren in arithmetic */ +#define CEOF 12 /* end of file */ +#define CCTL 13 /* like CWORD, except it must be escaped */ +#define CSPCL 14 /* these terminate a word */ +#define CIGN 15 /* character should be ignored */ + +/* Syntax classes for is_ functions */ +#define ISDIGIT 01 /* a digit */ +#define ISUPPER 02 /* an upper case letter */ +#define ISLOWER 04 /* a lower case letter */ +#define ISUNDER 010 /* an underscore */ +#define ISSPECL 020 /* the name of a special parameter */ + +#define SYNBASE (1 - CHAR_MIN) +#define PEOF -SYNBASE + + +#define BASESYNTAX (basesyntax + SYNBASE) +#define DQSYNTAX (dqsyntax + SYNBASE) +#define SQSYNTAX (sqsyntax + SYNBASE) +#define ARISYNTAX (arisyntax + SYNBASE) + +#define is_digit(c) ((unsigned int)((c) - '0') <= 9) +#define is_eof(c) ((c) == PEOF) +#define is_alpha(c) ((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER)) +#define is_name(c) ((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER)) +#define is_in_name(c) ((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT)) +#define is_special(c) ((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT)) +#define digit_val(c) ((c) - '0') + +extern const char basesyntax[]; +extern const char dqsyntax[]; +extern const char sqsyntax[]; +extern const char arisyntax[]; +extern const char is_type[]; diff --git a/bin/sh/pregenerated/token.h b/bin/sh/pregenerated/token.h new file mode 100644 index 0000000000..205dfe8d1a --- /dev/null +++ b/bin/sh/pregenerated/token.h @@ -0,0 +1,112 @@ +#define TEOF 0 +#define TNL 1 +#define TSEMI 2 +#define TBACKGND 3 +#define TAND 4 +#define TOR 5 +#define TPIPE 6 +#define TLP 7 +#define TRP 8 +#define TENDCASE 9 +#define TFALLTHRU 10 +#define TREDIR 11 +#define TWORD 12 +#define TIF 13 +#define TTHEN 14 +#define TELSE 15 +#define TELIF 16 +#define TFI 17 +#define TWHILE 18 +#define TUNTIL 19 +#define TFOR 20 +#define TDO 21 +#define TDONE 22 +#define TBEGIN 23 +#define TEND 24 +#define TCASE 25 +#define TESAC 26 +#define TNOT 27 + +/* Array indicating which tokens mark the end of a list */ +static const char tokendlist[] = { + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, +}; + +static const char *const tokname[] = { + "end of file", + "newline", + "\";\"", + "\"&\"", + "\"&&\"", + "\"||\"", + "\"|\"", + "\"(\"", + "\")\"", + "\";;\"", + "\";&\"", + "redirection", + "word", + "\"if\"", + "\"then\"", + "\"else\"", + "\"elif\"", + "\"fi\"", + "\"while\"", + "\"until\"", + "\"for\"", + "\"do\"", + "\"done\"", + "\"{\"", + "\"}\"", + "\"case\"", + "\"esac\"", + "\"!\"", +}; + +#define KWDOFFSET 13 + +const char *const parsekwd[] = { + "if", + "then", + "else", + "elif", + "fi", + "while", + "until", + "for", + "do", + "done", + "{", + "}", + "case", + "esac", + "!", + 0 +}; diff --git a/share/initrd/bin/Makefile b/share/initrd/bin/Makefile index 4d9ecc720f..7e614947f8 100644 --- a/share/initrd/bin/Makefile +++ b/share/initrd/bin/Makefile @@ -40,7 +40,8 @@ CRUNCH_PROGS_bin= \ test CRUNCH_LIBS+= -lcrypt -lprivate_edit -lkvm -ll -lmd -lprivate_ncurses -lutil -CRUNCH_BUILDTOOLS+= bin/sh +# No longer needed if bin/sh uses pregenerated/ headers. +#CRUNCH_BUILDTOOLS+= bin/sh bin/sh/mknodes bin/sh/mksyntax # Additional options for specific programs CRUNCH_ALIAS_test= [ diff --git a/share/mk/bsd.crunchgen.mk b/share/mk/bsd.crunchgen.mk index 1b2a8cb7ae..b7c70f0b4e 100644 --- a/share/mk/bsd.crunchgen.mk +++ b/share/mk/bsd.crunchgen.mk @@ -126,7 +126,7 @@ objs: ${OUTMK} .META ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} \ ${MAKE} -f ${OUTMK} objs -# Someone should replace the bin/csh and bin/sh build-tools with +# Someone should replace the bin/csh build-tools with # shell scripts so we can remove this nonsense. .for _tool in ${CRUNCH_BUILDTOOLS} build-tools-${_tool}: -- 2.41.0