diff options
author | Csaba Henk <csaba@gluster.com> | 2010-09-15 08:11:15 +0000 |
---|---|---|
committer | Vijay Bellur <vijay@dev.gluster.com> | 2010-09-16 23:07:27 -0700 |
commit | 63ec13f022ae99c1664fbdbc9e6623903ba843af (patch) | |
tree | fdde04563f5081e4322c0bf75541631293100fd8 | |
parent | 47bc630dca589235d7c10e2748cebaa5e419d5d7 (diff) |
cli: add a simplistic option parser, convert "mode script" to "--mode=script"
Signed-off-by: Csaba Henk <csaba@gluster.com>
Signed-off-by: Vijay Bellur <vijay@dev.gluster.com>
BUG: 1570 (geosync related changes)
URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=1570
-rw-r--r-- | cli/src/cli-cmd-misc.c | 10 | ||||
-rw-r--r-- | cli/src/cli-cmd-volume.c | 2 | ||||
-rw-r--r-- | cli/src/cli.c | 38 | ||||
-rw-r--r-- | cli/src/cli.h | 5 | ||||
-rw-r--r-- | cli/src/input.c | 21 | ||||
-rw-r--r-- | libglusterfs/src/common-utils.c | 13 | ||||
-rw-r--r-- | libglusterfs/src/common-utils.h | 1 |
7 files changed, 58 insertions, 32 deletions
diff --git a/cli/src/cli-cmd-misc.c b/cli/src/cli-cmd-misc.c index 7833456b7..306a7628f 100644 --- a/cli/src/cli-cmd-misc.c +++ b/cli/src/cli-cmd-misc.c @@ -61,10 +61,8 @@ cli_cmd_display_help (struct cli_state *state, struct cli_cmd_word *in_word, for (cmd = cli_probe_cmds; cmd->pattern; cmd++) cli_out ("%s - %s", cmd->pattern, cmd->desc); - for (cmd = cli_misc_cmds; cmd->pattern; cmd++) { - if (cmd->cbk) - cli_out ("%s - %s", cmd->pattern, cmd->desc); - } + for (cmd = cli_misc_cmds; cmd->pattern; cmd++) + cli_out ("%s - %s", cmd->pattern, cmd->desc); if (!state->rl_enabled) exit (0); @@ -81,10 +79,6 @@ struct cli_cmd cli_misc_cmds[] = { cli_cmd_display_help, "display command options"}, - { "mode script", - NULL, - "assign gluster execution mode"}, - { NULL, NULL, NULL } }; diff --git a/cli/src/cli-cmd-volume.c b/cli/src/cli-cmd-volume.c index 539c7eeac..b8a5df6d6 100644 --- a/cli/src/cli-cmd-volume.c +++ b/cli/src/cli-cmd-volume.c @@ -242,7 +242,7 @@ cli_cmd_get_confirmation (struct cli_state *state, const char *question) char answer = '\0'; char flush = '\0'; - if (GLUSTER_MODE_SCRIPT == state->mode) + if (state->mode & GLUSTER_MODE_SCRIPT) return GF_ANSWER_YES; printf ("%s (y/n) ", question); answer = getchar (); diff --git a/cli/src/cli.c b/cli/src/cli.c index 58cb61bb4..4e25417f9 100644 --- a/cli/src/cli.c +++ b/cli/src/cli.c @@ -325,13 +325,49 @@ cli_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, } int +cli_opt_parse (char *opt, struct cli_state *state) +{ + char *oarg; + + oarg = strtail (opt, "mode="); + if (oarg) { + if (strcmp (oarg, "script") == 0) { + state->mode |= GLUSTER_MODE_SCRIPT; + return 0; + } + if (strcmp (oarg, "interactive") == 0) + return 0; + return -1; + } + + return -1; +} + +int parse_cmdline (int argc, char *argv[], struct cli_state *state) { int ret = 0; + int i = 0; + int j = 0; + char *opt = NULL; - state->argc=argc-1; + state->argc=argc-1; state->argv=&argv[1]; + for (i = 0; i < state->argc; i++) { + opt = strtail (state->argv[i], "--"); + if (opt) { + ret = cli_opt_parse (opt, state); + if (ret == -1) { + cli_out ("unrecognized option --%s", opt); + return ret; + } + for (j = i; j < state->argc - 1; j++) + state->argv[j] = state->argv[j + 1]; + state->argc--; + } + } + return ret; } diff --git a/cli/src/cli.h b/cli/src/cli.h index eaa638c29..62aa02079 100644 --- a/cli/src/cli.h +++ b/cli/src/cli.h @@ -39,9 +39,8 @@ enum argp_option_keys { ARGP_PORT_KEY = 'p', }; -enum gluster_mode { - GLUSTER_MODE_SCRIPT = 1 -}; +#define GLUSTER_MODE_SCRIPT (1 << 0) + struct cli_state; struct cli_cmd_word; struct cli_cmd_tree; diff --git a/cli/src/input.c b/cli/src/input.c index 2c67b354d..7c3e78b16 100644 --- a/cli/src/input.c +++ b/cli/src/input.c @@ -41,10 +41,7 @@ cli_batch (void *d) state = d; - if (state->mode == GLUSTER_MODE_SCRIPT) - ret = cli_cmd_process (state, state->argc - 2, state->argv + 2); - else - ret = cli_cmd_process (state, state->argc, state->argv); + ret = cli_cmd_process (state, state->argc, state->argv); gf_log ("", GF_LOG_NORMAL, "Exiting with: %d", ret); exit (ret); @@ -86,22 +83,8 @@ int cli_input_init (struct cli_state *state) { int ret = 0; - gf_boolean_t is_batch = _gf_false; - - if (1 < state->argc) { - if (!strcmp ("mode", state->argv[0]) && - !strcmp ("script", state->argv[1])) { - state->mode = GLUSTER_MODE_SCRIPT; - if (2 < state->argc) - is_batch = _gf_true; - } else { - is_batch = _gf_true; - } - } else if (1 == state->argc) { - is_batch = _gf_true; - } - if (is_batch) { + if (state->argc) { ret = pthread_create (&state->input, NULL, cli_batch, state); return ret; } diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c index f3386fe2b..022fdf355 100644 --- a/libglusterfs/src/common-utils.c +++ b/libglusterfs/src/common-utils.c @@ -1548,3 +1548,16 @@ out: return ret; } + +char * +strtail (char *str, const char *pattern) +{ + int i = 0; + + for (i = 0; str[i] == pattern[i] && str[i]; i++); + + if (pattern[i] == '\0') + return str + i; + + return NULL; +} diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h index 54f52a5a0..293e6b880 100644 --- a/libglusterfs/src/common-utils.h +++ b/libglusterfs/src/common-utils.h @@ -331,5 +331,6 @@ int log_base2 (unsigned long x); int gf_system (const char *command); int get_checksum_for_path (char *path, uint32_t *checksum); +char *strtail (char *str, const char *pattern); #endif /* _COMMON_UTILS_H */ |