int cli_rl_autocomplete_cleanup (struct cli_state *state) { if (state->matches) cli_cmd_tokens_destroy (state->matches); state->matches = NULL; state->matchesp = NULL; return 0; }
int cli_cmd_process_line (struct cli_state *state, const char *text) { int count = 0; char **tokens = NULL; char **tokenp = NULL; char *token = NULL; char *copy = NULL; char *saveptr = NULL; int i = 0; int ret = -1; count = cli_cmd_input_token_count (text); tokens = calloc (count + 1, sizeof (*tokens)); if (!tokens) return -1; copy = strdup (text); if (!copy) goto out; tokenp = tokens; for (token = strtok_r (copy, " \t\r\n", &saveptr); token; token = strtok_r (NULL, " \t\r\n", &saveptr)) { *tokenp = strdup (token); if (!*tokenp) goto out; tokenp++; i++; } ret = cli_cmd_process (state, count, tokens); out: free (copy); if (tokens) cli_cmd_tokens_destroy (tokens); 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; }