diff options
Diffstat (limited to 'cli/src/registry.c')
| -rw-r--r-- | cli/src/registry.c | 115 |
1 files changed, 69 insertions, 46 deletions
diff --git a/cli/src/registry.c b/cli/src/registry.c index c3634d974..c4abe3be0 100644 --- a/cli/src/registry.c +++ b/cli/src/registry.c @@ -1,22 +1,12 @@ /* - Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ #ifndef _CONFIG_H #define _CONFIG_H @@ -260,29 +250,49 @@ err: return NULL; } - -struct cli_cmd_word * -cli_cmd_nextword (struct cli_cmd_word *word, const char *token) +void * +cli_getunamb (const char *tok, void **choices, cli_selector_t sel) { - struct cli_cmd_word *next = NULL; - struct cli_cmd_word **trav = NULL; - int ret = 0; + void **wcon = NULL; + char *w = NULL; + unsigned mn = 0; + void *ret = NULL; - if (!word->nextwords) + if (!choices || !tok || !*tok) return NULL; - for (trav = word->nextwords; (next = *trav); trav++) { - if (next->match) { -// ret = next->match (); - } else { - ret = strcmp (next->word, token); - } + for (wcon = choices; *wcon; wcon++) { + w = strtail ((char *)sel (*wcon), tok); + if (!w) + /* no match */ + continue; + if (!*w) + /* exact match */ + return *wcon; - if (ret == 0) - break; + ret = *wcon; + mn++; } - return next; +#ifdef FORCE_MATCH_EXACT + return NULL; +#else + return (mn == 1) ? ret : NULL; +#endif +} + +static const char * +sel_cmd_word (void *wcon) +{ + return ((struct cli_cmd_word *)wcon)->word; +} + +struct cli_cmd_word * +cli_cmd_nextword (struct cli_cmd_word *word, const char *token) +{ + return (struct cli_cmd_word *)cli_getunamb (token, + (void **)word->nextwords, + sel_cmd_word); } @@ -319,7 +329,7 @@ cli_cmd_newword (struct cli_cmd_word *word, const char *token) int cli_cmd_ingest (struct cli_cmd_tree *tree, char **tokens, cli_cmd_cbk_t *cbkfn, - const char *desc) + const char *desc, const char *pattern) { int ret = 0; char **tokenp = NULL; @@ -351,6 +361,7 @@ cli_cmd_ingest (struct cli_cmd_tree *tree, char **tokens, cli_cmd_cbk_t *cbkfn, word->cbkfn = cbkfn; word->desc = desc; + word->pattern = pattern; /* end of static strings in command template */ @@ -361,28 +372,40 @@ cli_cmd_ingest (struct cli_cmd_tree *tree, char **tokens, cli_cmd_cbk_t *cbkfn, int -cli_cmd_register (struct cli_cmd_tree *tree, const char *template, - cli_cmd_cbk_t cbk, const char *desc) +cli_cmd_register (struct cli_cmd_tree *tree, struct cli_cmd *cmd) { char **tokens = NULL; int ret = 0; - if (!template) - return -1; + GF_ASSERT (cmd); - tokens = cli_cmd_tokenize (template); - if (!tokens) - return -1; + if (cmd->reg_cbk) + cmd->reg_cbk (cmd); - ret = cli_cmd_ingest (tree, tokens, cbk, desc); - if (ret) - goto err; + if (cmd->disable) { + ret = 0; + goto out; + } - return 0; -err: + tokens = cli_cmd_tokenize (cmd->pattern); + if (!tokens) { + ret = -1; + goto out; + } + + ret = cli_cmd_ingest (tree, tokens, cmd->cbk, cmd->desc, cmd->pattern); + if (ret) { + ret = -1; + goto out; + } + + ret = 0; + +out: if (tokens) cli_cmd_tokens_destroy (tokens); + gf_log ("cli", GF_LOG_DEBUG, "Returning %d", ret); return ret; } |
