const char *key = NULL, *defval = NULL;
const char *list_sections = 0;
char *val;
+ char *section;
int param = 0;
vector<const char*> args, nargs;
deque<const char *> sections;
unsigned i;
+ DEFINE_CONF_VARS(usage);
argv_to_vec(argc, argv, args);
env_to_vec(args);
if (args.size() < 2)
usage();
- for (unsigned i=0; i<args.size(); i++) {
- if (strcmp(args[i], "-t") == 0) {
- if (i < args.size() - 1)
- type = args[++i];
- else
- usage();
+ FOR_EACH_ARG(args) {
+ if (CONF_ARG_EQ("type", 't')) {
+ CONF_SAFE_SET_ARG_VAL(&type, OPT_STR);
} else {
nargs.push_back(args[i]);
}
common_init(args, type, false);
- for (unsigned i=0; i<args.size(); i++) {
- if (strcmp(args[i], "-l") == 0 ||
- strcmp(args[i], "--list_sections") == 0) {
- if (i < args.size() - 1)
- list_sections = args[++i];
- else
- usage();
- } else if (strcmp(args[i], "-s") == 0) {
- if (i < args.size() - 1)
- sections.push_back(args[++i]);
- else
- usage();
- } else {
+ FOR_EACH_ARG(args) {
+ if (CONF_ARG_EQ("list_sections", 'l')) {
+ CONF_SAFE_SET_ARG_VAL(&list_sections, OPT_STR);
+ } else if (CONF_ARG_EQ("section", 's')) {
+ CONF_SAFE_SET_ARG_VAL(§ion, OPT_STR);
+ sections.push_back(section);
+ } else if (*args[i] != '-') {
switch (param) {
case 0:
key = args[i];
break;
}
param++;
+ } else {
+ cerr << "unrecognized argument: " << args[i] << std::endl;
+ usage();
}
}
cmd[1] && !cmd[2]);
}
-static bool conf_cmd_equals(const char *cmd, const char *opt, char char_opt, unsigned int *val_pos)
+bool conf_cmd_equals(const char *cmd, const char *opt, char char_opt, unsigned int *val_pos)
{
unsigned int i;
unsigned int len = strlen(opt);
}
}
-
-#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) \
- CONF_SET_ARG_VAL(dest, type); \
- } 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 FOR_EACH_ARG(args) \
-__isarg = 1 < args.size(); \
-for (unsigned i=0; i<args.size(); i++, __isarg = i+1 < args.size())
-
-
+bool is_bool_param(const char *param)
+{
+ return ((strcasecmp(param, "true")==0) || (strcasecmp(param, "false")==0));
+}
void parse_startup_config_options(std::vector<const char*>& args, const char *module_type)
{
- DEFINE_CONF_VARS;
+ DEFINE_CONF_VARS(NULL);
std::vector<const char *> nargs;
if (!g_conf.id)
void parse_config_options(std::vector<const char*>& args)
{
int opt_len = sizeof(config_optionsp)/sizeof(config_option);
- DEFINE_CONF_VARS;
+ DEFINE_CONF_VARS(NULL);
std::vector<const char*> nargs;
FOR_EACH_ARG(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 {
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<args.size(); i++, __isarg = i+1 < args.size())
+#define FOR_EACH_ARG(args) \
+ __isarg = 1 < args.size(); \
+ for (unsigned i=0; i<args.size(); i++, __isarg = i+1 < args.size())
#include "common/debug.h"