ash: make ash -c 'if set -o barfoo 2>/dev/null; then echo foo; else echo bar; fi' work

(fixes bug 1142)

function                                             old     new   delta
options                                              551     565     +14
ash_main                                            1397    1411     +14
setcmd                                                77      90     +13
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 41/0)               Total: 41 bytes
This commit is contained in:
Denis Vlasenko 2008-02-14 15:01:47 +00:00
parent f1d93ec420
commit 28bf671d6d

View File

@ -9076,7 +9076,7 @@ setparam(char **argv)
* Process shell options. The global variable argptr contains a pointer * Process shell options. The global variable argptr contains a pointer
* to the argument list; we advance it past the options. * to the argument list; we advance it past the options.
*/ */
static void static int
minus_o(char *name, int val) minus_o(char *name, int val)
{ {
int i; int i;
@ -9085,15 +9085,17 @@ minus_o(char *name, int val)
for (i = 0; i < NOPTS; i++) { for (i = 0; i < NOPTS; i++) {
if (strcmp(name, optnames(i)) == 0) { if (strcmp(name, optnames(i)) == 0) {
optlist[i] = val; optlist[i] = val;
return; return 0;
} }
} }
ash_msg_and_raise_error("illegal option -o %s", name); ash_msg("illegal option -o %s", name);
return 1;
} }
out1str("Current option settings\n"); out1str("Current option settings\n");
for (i = 0; i < NOPTS; i++) for (i = 0; i < NOPTS; i++)
out1fmt("%-16s%s\n", optnames(i), out1fmt("%-16s%s\n", optnames(i),
optlist[i] ? "on" : "off"); optlist[i] ? "on" : "off");
return 0;
} }
static void static void
setoption(int flag, int val) setoption(int flag, int val)
@ -9109,7 +9111,7 @@ setoption(int flag, int val)
ash_msg_and_raise_error("illegal option -%c", flag); ash_msg_and_raise_error("illegal option -%c", flag);
/* NOTREACHED */ /* NOTREACHED */
} }
static void static int
options(int cmdline) options(int cmdline)
{ {
char *p; char *p;
@ -9144,7 +9146,10 @@ options(int cmdline)
if (c == 'c' && cmdline) { if (c == 'c' && cmdline) {
minusc = p; /* command is after shell args */ minusc = p; /* command is after shell args */
} else if (c == 'o') { } else if (c == 'o') {
minus_o(*argptr, val); if (minus_o(*argptr, val)) {
/* it already printed err message */
return 1; /* error */
}
if (*argptr) if (*argptr)
argptr++; argptr++;
} else if (cmdline && (c == 'l')) { /* -l or +l == --login */ } else if (cmdline && (c == 'l')) { /* -l or +l == --login */
@ -9159,6 +9164,7 @@ options(int cmdline)
} }
} }
} }
return 0;
} }
/* /*
@ -9228,16 +9234,21 @@ showvars(const char *sep_prefix, int on, int off)
static int static int
setcmd(int argc, char **argv) setcmd(int argc, char **argv)
{ {
int retval;
if (argc == 1) if (argc == 1)
return showvars(nullstr, 0, VUNSET); return showvars(nullstr, 0, VUNSET);
INT_OFF; INT_OFF;
options(0); retval = 1;
optschanged(); if (!options(0)) { /* if no parse error... */
if (*argptr != NULL) { retval = 0;
setparam(argptr); optschanged();
if (*argptr != NULL) {
setparam(argptr);
}
} }
INT_ON; INT_ON;
return 0; return retval;
} }
#if ENABLE_ASH_RANDOM_SUPPORT #if ENABLE_ASH_RANDOM_SUPPORT
@ -12790,7 +12801,10 @@ procargs(int argc, char **argv)
for (i = 0; i < NOPTS; i++) for (i = 0; i < NOPTS; i++)
optlist[i] = 2; optlist[i] = 2;
argptr = xargv; argptr = xargv;
options(1); if (options(1)) {
/* it already printed err message */
raise_exception(EXERROR);
}
xargv = argptr; xargv = argptr;
xminusc = minusc; xminusc = minusc;
if (*xargv == NULL) { if (*xargv == NULL) {