From 17f5e296a9cfd01eb15a6a7e162f022e972d114e Mon Sep 17 00:00:00 2001 From: troky Date: Wed, 26 Mar 2014 10:46:17 +0100 Subject: [PATCH] Fixed configuration/command line parsing. --- sgminer.c | 68 ++++++++++++++++++++----------------------------------- 1 file changed, 24 insertions(+), 44 deletions(-) diff --git a/sgminer.c b/sgminer.c index c53ff29c..b9fd4ce9 100644 --- a/sgminer.c +++ b/sgminer.c @@ -230,7 +230,7 @@ int total_pools, enabled_pools; enum pool_strategy pool_strategy = POOL_FAILOVER; int opt_rotate_period; static int total_urls, total_users, total_passes, total_userpasses; -static int json_array_index; +static int json_array_index = -1; static #ifndef HAVE_CURSES @@ -557,6 +557,20 @@ struct pool *add_pool(void) return pool; } +static struct pool* get_current_pool() +{ + while ((json_array_index + 1) > total_pools) + add_pool(); + + if (json_array_index < 0) { + if (!total_pools) + add_pool(); + return pools[total_pools - 1]; + } + + return pools[json_array_index]; +} + /* Pool variant of test and set */ static bool pool_tset(struct pool *pool, bool *var) { @@ -755,12 +769,8 @@ static char *set_url(char *arg) static char *set_poolname(char *arg) { - struct pool *pool; - - while ((json_array_index + 1) > total_pools) - add_pool(); - pool = pools[json_array_index]; - + struct pool *pool = get_current_pool(); + applog(LOG_DEBUG, "Setting pool %i name to %s", pool->pool_no, arg); opt_set_charp(arg, &pool->name); @@ -823,12 +833,7 @@ void remove_pool(struct pool *pool) static char *set_pool_state(char *arg) { - struct pool *pool; - - /* TODO: consider using j_a_i everywhere */ - while ((json_array_index + 1) > total_pools) - add_pool(); - pool = pools[json_array_index]; + struct pool *pool = get_current_pool(); applog(LOG_INFO, "Setting pool %s state to %s", get_pool_name(pool), arg); if (strcmp(arg, "disabled") == 0) { @@ -877,15 +882,8 @@ static char *set_quota(char *arg) static char *set_user(const char *arg) { - struct pool *pool; + struct pool *pool = get_current_pool(); - if (total_userpasses) - return "Use only user + pass or userpass, but not both"; - total_users++; - if (total_users > total_pools) - add_pool(); - - pool = pools[total_users - 1]; opt_set_charp(arg, &pool->rpc_user); return NULL; @@ -893,15 +891,8 @@ static char *set_user(const char *arg) static char *set_pass(const char *arg) { - struct pool *pool; + struct pool *pool = get_current_pool(); - if (total_userpasses) - return "Use only user + pass or userpass, but not both"; - total_passes++; - if (total_passes > total_pools) - add_pool(); - - pool = pools[total_passes - 1]; opt_set_charp(arg, &pool->rpc_pass); return NULL; @@ -909,16 +900,9 @@ static char *set_pass(const char *arg) static char *set_userpass(const char *arg) { - struct pool *pool; + struct pool *pool = get_current_pool(); char *updup; - if (total_users || total_passes) - return "Use only user + pass or userpass, but not both"; - total_userpasses++; - if (total_userpasses > total_pools) - add_pool(); - - pool = pools[total_userpasses - 1]; updup = strdup(arg); opt_set_charp(arg, &pool->rpc_userpass); pool->rpc_user = strtok(updup, ":"); @@ -926,18 +910,15 @@ static char *set_userpass(const char *arg) return "Failed to find : delimited user info"; pool->rpc_pass = strtok(NULL, ":"); if (!pool->rpc_pass) - return "Failed to find : delimited pass info"; + pool->rpc_pass = ""; return NULL; } static char *set_pool_priority(char *arg) { - struct pool *pool; + struct pool *pool = get_current_pool(); - while ((json_array_index + 1) > total_pools) - add_pool(); - pool = pools[json_array_index]; applog(LOG_DEBUG, "Setting pool %i priority to %s", pool->pool_no, arg); opt_set_intval(arg, &pool->prio); @@ -1474,7 +1455,6 @@ static char *parse_config(json_t *config, bool fileconf, int parent_iteration) else if (json_is_object(json_array_get(val, n))) { err = parse_config(json_array_get(val, n), false, n); - json_array_index = parent_iteration; } } } else if ((opt->type & OPT_NOARG) && json_is_boolean(val)) { @@ -1541,7 +1521,7 @@ static char *load_config(const char *arg, void __maybe_unused *unused) /* Parse the config now, so we can override it. That can keep pointers * so don't free config object. */ - return parse_config(config, true, 0); + return parse_config(config, true, -1); } static char *set_default_config(const char *arg)