int cli_cmd_process (struct cli_state *state, int argc, char **argv) { int ret = 0; struct cli_cmd_word *word = NULL; struct cli_cmd_word *next = NULL; int i = 0; word = &state->tree.root; if (!argc) return 0; for (i = 0; i < argc; i++) { next = cli_cmd_nextword (word, argv[i]); word = next; if (!word) break; if (word->cbkfn) break; } if (!word) { cli_out ("unrecognized word: %s (position %d)", argv[i], i); return -1; } if (!word->cbkfn) { cli_out ("unrecognized command"); return -1; } if ( strcmp (word->word,"help")==0 ) goto callback; state->await_connected = cli_cmd_needs_connection (word); ret = cli_cmd_await_connected (state->await_connected); if (ret) { cli_out ("Connection failed. Please check if gluster " "daemon is operational."); gf_log ("", GF_LOG_INFO, "Exiting with: %d", ret); exit (ret); } callback: ret = word->cbkfn (state, word, (const char **)argv, argc); (void) cli_cmd_status_reset (); return ret; }
int cli_rl_autocomplete_prepare (struct cli_state *state, const char *text) { struct cli_cmd_word *word = NULL; struct cli_cmd_word *next = NULL; char **tokens = NULL; char **tokenp = NULL; char *token = NULL; char **matches = NULL; tokens = cli_rl_tokenize (text); if (!tokens) return 0; word = &state->tree.root; for (tokenp = tokens; (token = *tokenp); tokenp++) { if (!*(tokenp+1)) { /* last word */ break; } next = cli_cmd_nextword (word, token); word = next; if (!word) break; } if (!word) goto out; matches = cli_rl_get_matches (state, word, token); state->matches = matches; state->matchesp = matches; out: cli_cmd_tokens_destroy (tokens); return 0; }