From 28bf671d6d92d0c2842bd5f4555f5af937a0a6db Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Thu, 14 Feb 2008 15:01:47 +0000 Subject: 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 --- shell/ash.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'shell/ash.c') diff --git a/shell/ash.c b/shell/ash.c index 0634f1868..612172043 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -9076,7 +9076,7 @@ setparam(char **argv) * Process shell options. The global variable argptr contains a pointer * to the argument list; we advance it past the options. */ -static void +static int minus_o(char *name, int val) { int i; @@ -9085,15 +9085,17 @@ minus_o(char *name, int val) for (i = 0; i < NOPTS; i++) { if (strcmp(name, optnames(i)) == 0) { 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"); for (i = 0; i < NOPTS; i++) out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); + return 0; } static void setoption(int flag, int val) @@ -9109,7 +9111,7 @@ setoption(int flag, int val) ash_msg_and_raise_error("illegal option -%c", flag); /* NOTREACHED */ } -static void +static int options(int cmdline) { char *p; @@ -9144,7 +9146,10 @@ options(int cmdline) if (c == 'c' && cmdline) { minusc = p; /* command is after shell args */ } else if (c == 'o') { - minus_o(*argptr, val); + if (minus_o(*argptr, val)) { + /* it already printed err message */ + return 1; /* error */ + } if (*argptr) argptr++; } 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 setcmd(int argc, char **argv) { + int retval; + if (argc == 1) return showvars(nullstr, 0, VUNSET); INT_OFF; - options(0); - optschanged(); - if (*argptr != NULL) { - setparam(argptr); + retval = 1; + if (!options(0)) { /* if no parse error... */ + retval = 0; + optschanged(); + if (*argptr != NULL) { + setparam(argptr); + } } INT_ON; - return 0; + return retval; } #if ENABLE_ASH_RANDOM_SUPPORT @@ -12790,7 +12801,10 @@ procargs(int argc, char **argv) for (i = 0; i < NOPTS; i++) optlist[i] = 2; argptr = xargv; - options(1); + if (options(1)) { + /* it already printed err message */ + raise_exception(EXERROR); + } xargv = argptr; xminusc = minusc; if (*xargv == NULL) { -- cgit v1.2.3