From: Yehuda Sadeh Date: Thu, 12 Mar 2009 22:09:59 +0000 (-0700) Subject: conf: arguments parsing cleanup X-Git-Tag: v0.7.1^2~39 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=06b06ea3c20ade4591a955dca776c31763154cfd;p=ceph.git conf: arguments parsing cleanup --- diff --git a/src/cconf.cc b/src/cconf.cc index 82beeb0aa3ff..d3df67622adf 100644 --- a/src/cconf.cc +++ b/src/cconf.cc @@ -27,10 +27,12 @@ int main(int argc, const char **argv) const char *key = NULL, *defval = NULL; const char *list_sections = 0; char *val; + char *section; int param = 0; vector args, nargs; deque sections; unsigned i; + DEFINE_CONF_VARS(usage); argv_to_vec(argc, argv, args); env_to_vec(args); @@ -38,12 +40,9 @@ int main(int argc, const char **argv) if (args.size() < 2) usage(); - for (unsigned i=0; i& args, const char *module_type) { - DEFINE_CONF_VARS; + DEFINE_CONF_VARS(NULL); std::vector nargs; if (!g_conf.id) @@ -1012,7 +993,7 @@ ConfFile *conf_get_conf_file() void parse_config_options(std::vector& args) { int opt_len = sizeof(config_optionsp)/sizeof(config_option); - DEFINE_CONF_VARS; + DEFINE_CONF_VARS(NULL); std::vector nargs; FOR_EACH_ARG(args) { @@ -1024,7 +1005,7 @@ void parse_config_options(std::vector& args) } else if (CONF_ARG_EQ(config_optionsp[optn].name, config_optionsp[optn].char_option)) { if (__isarg || val_pos || config_optionsp[optn].type == OPT_BOOL) - CONF_SET_ARG_VAL(config_optionsp[optn].val_ptr, config_optionsp[optn].type); + CONF_SAFE_SET_ARG_VAL(config_optionsp[optn].val_ptr, config_optionsp[optn].type); else continue; } else { diff --git a/src/config.h b/src/config.h index 69ebc4b73010..ffddc65637aa 100644 --- a/src/config.h +++ b/src/config.h @@ -379,28 +379,43 @@ ConfFile *conf_get_conf_file(); char *conf_post_process_val(const char *val); int conf_read_key(const char *alt_section, const char *key, opt_type_t type, void *out, void *def); +bool conf_set_conf_val(void *field, opt_type_t type, const char *val); +bool conf_cmd_equals(const char *cmd, const char *opt, char char_opt, unsigned int *val_pos); + #define CONF_NEXT_VAL (val_pos ? &args[i][val_pos] : args[++i]) + #define CONF_SET_ARG_VAL(dest, type) \ conf_set_conf_val(dest, type, CONF_NEXT_VAL) + #define CONF_SAFE_SET_ARG_VAL(dest, type) \ do { \ - if (__isarg || val_pos) \ + if (type == OPT_BOOL) { \ + if (__isarg || val_pos) { \ + CONF_SET_ARG_VAL(dest, type); \ + } else \ + conf_set_conf_val(dest, type, "true"); \ + } else if (__isarg || val_pos) { \ CONF_SET_ARG_VAL(dest, type); \ + } else if (args_usage) \ + args_usage(); \ } while (0) + #define CONF_SET_BOOL_ARG_VAL(dest) \ conf_set_conf_val(dest, OPT_BOOL, (val_pos ? &args[i][val_pos] : "true")) + #define CONF_ARG_EQ(str_cmd, char_cmd) \ conf_cmd_equals(args[i], str_cmd, char_cmd, &val_pos) -#define DEFINE_CONF_VARS \ -unsigned int val_pos; \ -bool __isarg +#define DEFINE_CONF_VARS(usage_func) \ + unsigned int val_pos; \ + void (*args_usage)() = usage_func; \ + bool __isarg -#define FOR_EACH_ARG(args) \ -__isarg = 1 < args.size(); \ -for (unsigned i=0; i