Allow loading multiple configuration files

This commit is contained in:
pooler 2014-05-10 11:50:39 +02:00
parent d701fecd57
commit b01f8be868
2 changed files with 21 additions and 23 deletions

View file

@ -127,7 +127,6 @@ static int opt_retries = -1;
static int opt_fail_pause = 30; static int opt_fail_pause = 30;
int opt_timeout = 0; int opt_timeout = 0;
static int opt_scantime = 5; static int opt_scantime = 5;
static json_t *opt_config;
static const bool opt_time = true; static const bool opt_time = true;
static enum sha256_algos opt_algo = ALGO_SCRYPT; static enum sha256_algos opt_algo = ALGO_SCRYPT;
static int opt_n_threads; static int opt_n_threads;
@ -1448,6 +1447,8 @@ static void show_usage_and_exit(int status)
exit(status); exit(status);
} }
static void parse_config(json_t *config, char *pname, char *ref);
static void parse_arg(int key, char *arg, char *pname) static void parse_arg(int key, char *arg, char *pname)
{ {
char *p; char *p;
@ -1473,14 +1474,8 @@ static void parse_arg(int key, char *arg, char *pname)
break; break;
case 'c': { case 'c': {
json_error_t err; json_error_t err;
if (opt_config) json_t *config = JSON_LOAD_FILE(arg, &err);
json_decref(opt_config); if (!json_is_object(config)) {
#if JANSSON_VERSION_HEX >= 0x020000
opt_config = json_load_file(arg, 0, &err);
#else
opt_config = json_load_file(arg, &err);
#endif
if (!json_is_object(opt_config)) {
if (err.line < 0) if (err.line < 0)
fprintf(stderr, "%s: %s\n", pname, err.text); fprintf(stderr, "%s: %s\n", pname, err.text);
else else
@ -1488,6 +1483,8 @@ static void parse_arg(int key, char *arg, char *pname)
pname, arg, err.line, err.text); pname, arg, err.line, err.text);
exit(1); exit(1);
} }
parse_config(config, pname, arg);
json_decref(config);
break; break;
} }
case 'q': case 'q':
@ -1664,33 +1661,34 @@ static void parse_arg(int key, char *arg, char *pname)
} }
} }
static void parse_config(char *pname) static void parse_config(json_t *config, char *pname, char *ref)
{ {
int i; int i;
char *s;
json_t *val; json_t *val;
if (!json_is_object(opt_config))
return;
for (i = 0; i < ARRAY_SIZE(options); i++) { for (i = 0; i < ARRAY_SIZE(options); i++) {
if (!options[i].name) if (!options[i].name)
break; break;
if (!strcmp(options[i].name, "config"))
continue;
val = json_object_get(opt_config, options[i].name); val = json_object_get(config, options[i].name);
if (!val) if (!val)
continue; continue;
if (options[i].has_arg && json_is_string(val)) { if (options[i].has_arg && json_is_string(val)) {
char *s = strdup(json_string_value(val)); if (!strcmp(options[i].name, "config")) {
fprintf(stderr, "%s: %s: option '%s' not allowed here\n",
pname, ref, options[i].name);
exit(1);
}
s = strdup(json_string_value(val));
if (!s) if (!s)
break; break;
parse_arg(options[i].val, s, pname); parse_arg(options[i].val, s, pname);
free(s); free(s);
} else if (!options[i].has_arg && json_is_true(val)) } else if (!options[i].has_arg && json_is_true(val)) {
parse_arg(options[i].val, "", pname); parse_arg(options[i].val, "", pname);
else { } else {
fprintf(stderr, "%s: invalid argument for option '%s'\n", fprintf(stderr, "%s: invalid argument for option '%s'\n",
pname, options[i].name); pname, options[i].name);
exit(1); exit(1);
@ -1718,8 +1716,6 @@ static void parse_cmdline(int argc, char *argv[])
argv[0], argv[optind]); argv[0], argv[optind]);
show_usage_and_exit(1); show_usage_and_exit(1);
} }
parse_config(argv[0]);
} }
#ifndef WIN32 #ifndef WIN32

View file

@ -123,9 +123,11 @@ static inline void le32enc(void *pp, uint32_t x)
#endif #endif
#if JANSSON_MAJOR_VERSION >= 2 #if JANSSON_MAJOR_VERSION >= 2
#define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr)) #define JSON_LOADS(str, err_ptr) json_loads(str, 0, err_ptr)
#define JSON_LOAD_FILE(path, err_ptr) json_load_file(path, 0, err_ptr)
#else #else
#define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr)) #define JSON_LOADS(str, err_ptr) json_loads(str, err_ptr)
#define JSON_LOAD_FILE(path, err_ptr) json_load_file(path, err_ptr)
#endif #endif
#define USER_AGENT PACKAGE_NAME "/" PACKAGE_VERSION #define USER_AGENT PACKAGE_NAME "/" PACKAGE_VERSION