sh: Reduce size of options table.

This commit is contained in:
jilles 2016-01-07 23:13:20 +00:00
parent 66aa33e3b5
commit acacb5074b
4 changed files with 71 additions and 71 deletions

View File

@ -951,8 +951,8 @@ varvalue(const char *name, int quoted, int subtype, int flag,
case '-':
p = buf;
for (i = 0 ; i < NSHORTOPTS ; i++) {
if (optlist[i].val)
*p++ = optlist[i].letter;
if (optval[i])
*p++ = optletter[i];
}
*p = '\0';
strtodest(buf, flag, subtype, quoted, dst);

View File

@ -91,7 +91,7 @@ procargs(int argc, char **argv)
if (argc > 0)
argptr++;
for (i = 0; i < NOPTS; i++)
optlist[i].val = 2;
optval[i] = 2;
privileged = (getuid() != geteuid() || getgid() != getegid());
options(1);
if (*argptr == NULL && minusc == NULL)
@ -104,8 +104,8 @@ procargs(int argc, char **argv)
if (mflag == 2)
mflag = iflag;
for (i = 0; i < NOPTS; i++)
if (optlist[i].val == 2)
optlist[i].val = 0;
if (optval[i] == 2)
optval[i] = 0;
arg0 = argv[0];
if (sflag == 0 && minusc == NULL) {
scriptname = *argptr++;
@ -250,26 +250,29 @@ static void
minus_o(char *name, int val)
{
int i;
const unsigned char *on;
size_t len;
if (name == NULL) {
if (val) {
/* "Pretty" output. */
out1str("Current option settings\n");
for (i = 0; i < NOPTS; i++)
out1fmt("%-16s%s\n", optlist[i].name,
optlist[i].val ? "on" : "off");
for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
out1fmt("%-16.*s%s\n", *on, on + 1,
optval[i] ? "on" : "off");
} else {
/* Output suitable for re-input to shell. */
for (i = 0; i < NOPTS; i++)
out1fmt("%s %co %s%s",
for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
out1fmt("%s %co %.*s%s",
i % 6 == 0 ? "set" : "",
optlist[i].val ? '-' : '+',
optlist[i].name,
optval[i] ? '-' : '+',
*on, on + 1,
i % 6 == 5 || i == NOPTS - 1 ? "\n" : "");
}
} else {
for (i = 0; i < NOPTS; i++)
if (equal(name, optlist[i].name)) {
len = strlen(name);
for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
if (*on == len && memcmp(on + 1, name, len) == 0) {
setoptionbyindex(i, val);
return;
}
@ -281,18 +284,18 @@ minus_o(char *name, int val)
static void
setoptionbyindex(int idx, int val)
{
if (optlist[idx].letter == 'p' && !val && privileged) {
if (optletter[idx] == 'p' && !val && privileged) {
if (setgid(getgid()) == -1)
error("setgid");
if (setuid(getuid()) == -1)
error("setuid");
}
optlist[idx].val = val;
optval[idx] = val;
if (val) {
/* #%$ hack for ksh semantics */
if (optlist[idx].letter == 'V')
if (optletter[idx] == 'V')
Eflag = 0;
else if (optlist[idx].letter == 'E')
else if (optletter[idx] == 'E')
Vflag = 0;
}
}
@ -303,7 +306,7 @@ setoption(int flag, int val)
int i;
for (i = 0; i < NSHORTOPTS; i++)
if (optlist[i].letter == flag) {
if (optletter[i] == flag) {
setoptionbyindex(i, val);
return;
}

View File

@ -45,60 +45,57 @@ struct shparam {
#define eflag optlist[0].val
#define fflag optlist[1].val
#define Iflag optlist[2].val
#define iflag optlist[3].val
#define mflag optlist[4].val
#define nflag optlist[5].val
#define sflag optlist[6].val
#define xflag optlist[7].val
#define vflag optlist[8].val
#define Vflag optlist[9].val
#define Eflag optlist[10].val
#define Cflag optlist[11].val
#define aflag optlist[12].val
#define bflag optlist[13].val
#define uflag optlist[14].val
#define privileged optlist[15].val
#define Tflag optlist[16].val
#define Pflag optlist[17].val
#define hflag optlist[18].val
#define nologflag optlist[19].val
#define eflag optval[0]
#define fflag optval[1]
#define Iflag optval[2]
#define iflag optval[3]
#define mflag optval[4]
#define nflag optval[5]
#define sflag optval[6]
#define xflag optval[7]
#define vflag optval[8]
#define Vflag optval[9]
#define Eflag optval[10]
#define Cflag optval[11]
#define aflag optval[12]
#define bflag optval[13]
#define uflag optval[14]
#define privileged optval[15]
#define Tflag optval[16]
#define Pflag optval[17]
#define hflag optval[18]
#define nologflag optval[19]
#define NSHORTOPTS 19
#define NOPTS 20
struct optent {
const char *name;
const char letter;
char val;
};
extern struct optent optlist[NOPTS];
extern char optval[NOPTS];
extern const char optletter[NSHORTOPTS];
#ifdef DEFINE_OPTIONS
struct optent optlist[NOPTS] = {
{ "errexit", 'e', 0 },
{ "noglob", 'f', 0 },
{ "ignoreeof", 'I', 0 },
{ "interactive",'i', 0 },
{ "monitor", 'm', 0 },
{ "noexec", 'n', 0 },
{ "stdin", 's', 0 },
{ "xtrace", 'x', 0 },
{ "verbose", 'v', 0 },
{ "vi", 'V', 0 },
{ "emacs", 'E', 0 },
{ "noclobber", 'C', 0 },
{ "allexport", 'a', 0 },
{ "notify", 'b', 0 },
{ "nounset", 'u', 0 },
{ "privileged", 'p', 0 },
{ "trapsasync", 'T', 0 },
{ "physical", 'P', 0 },
{ "trackall", 'h', 0 },
{ "nolog", '\0', 0 },
};
char optval[NOPTS];
const char optletter[NSHORTOPTS] = "efIimnsxvVECabupTPh";
static const unsigned char optname[] =
"\007errexit"
"\006noglob"
"\011ignoreeof"
"\013interactive"
"\007monitor"
"\006noexec"
"\005stdin"
"\006xtrace"
"\007verbose"
"\002vi"
"\005emacs"
"\011noclobber"
"\011allexport"
"\006notify"
"\007nounset"
"\012privileged"
"\012trapsasync"
"\010physical"
"\010trackall"
"\005nolog"
;
#endif

View File

@ -754,8 +754,8 @@ mklocal(char *name)
INTOFF;
lvp = ckmalloc(sizeof (struct localvar));
if (name[0] == '-' && name[1] == '\0') {
lvp->text = ckmalloc(sizeof optlist);
memcpy(lvp->text, optlist, sizeof optlist);
lvp->text = ckmalloc(sizeof optval);
memcpy(lvp->text, optval, sizeof optval);
vp = NULL;
} else {
vp = find_var(name, &vpp, NULL);
@ -797,7 +797,7 @@ poplocalvars(void)
localvars = lvp->next;
vp = lvp->vp;
if (vp == NULL) { /* $- saved */
memcpy(optlist, lvp->text, sizeof optlist);
memcpy(optval, lvp->text, sizeof optval);
ckfree(lvp->text);
optschanged();
} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {