diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2012-02-25 17:10:57 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2012-02-27 19:53:47 +0100 |
commit | aed0e6ba0396dc34a7b7af8a342b6682d9cd1ed0 (patch) | |
tree | 0148e7d1d1353c8ba5b622a3dad7589c14f315fc /common/hush.c | |
parent | 2034ee47b1a0dfb3988cf159094d73c3ec623597 (diff) | |
download | barebox-aed0e6ba0396dc34a7b7af8a342b6682d9cd1ed0.tar.gz barebox-aed0e6ba0396dc34a7b7af8a342b6682d9cd1ed0.tar.xz |
getopt: save and restore context
execute_command is the single point where commands are executed and thus a new
getopt context is needed. currently we call getopt_reset here to reset the
context. This breaks though when a command tries to run a command itself by
calling execute_command or run_command. In this case we have to store the
context and restore it afterwards. The same is necessary in builtin_getopt.
Currently noone does this so this one shouldn't fix a bug, but merely allows
us to do such things later.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common/hush.c')
-rw-r--r-- | common/hush.c | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/common/hush.c b/common/hush.c index b59e59dd17..97dc13cd2d 100644 --- a/common/hush.c +++ b/common/hush.c @@ -502,9 +502,10 @@ static void setup_string_in_str(struct in_str *i, const char *s) static int builtin_getopt(struct p_context *ctx, struct child_prog *child) { char *optstring, *var; - int opt; + int opt, ret = 0; char opta[2]; struct option *o; + struct getopt_context gc; if (child->argc != 3) return -2 - 1; @@ -512,7 +513,7 @@ static int builtin_getopt(struct p_context *ctx, struct child_prog *child) optstring = child->argv[1]; var = child->argv[2]; - getopt_reset(); + getopt_context_store(&gc); if (!ctx->options_parsed) { while((opt = getopt(ctx->global_argc, ctx->global_argv, optstring)) > 0) { @@ -525,8 +526,10 @@ static int builtin_getopt(struct p_context *ctx, struct child_prog *child) ctx->options_parsed = 1; - if (list_empty(&ctx->options)) - return -1; + if (list_empty(&ctx->options)) { + ret = -1; + goto out; + } o = list_first_entry(&ctx->options, struct option, list); @@ -538,8 +541,10 @@ static int builtin_getopt(struct p_context *ctx, struct child_prog *child) free(o->optarg); list_del(&o->list); free(o); +out: + getopt_context_restore(&gc); - return 0; + return ret; } BAREBOX_MAGICVAR(OPTARG, "optarg for hush builtin getopt"); |