mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-03 11:25:35 +00:00
Merge "CLI: Create ast_cli_completion_vector."
This commit is contained in:
@@ -305,6 +305,27 @@ int ast_cli_generatornummatches(const char *, const char *);
|
|||||||
*/
|
*/
|
||||||
char **ast_cli_completion_matches(const char *, const char *);
|
char **ast_cli_completion_matches(const char *, const char *);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Generates a vector of strings for CLI completion.
|
||||||
|
*
|
||||||
|
* \param text Complete input being matched.
|
||||||
|
* \param word Current word being matched
|
||||||
|
*
|
||||||
|
* The results contain strings that both:
|
||||||
|
* 1) Begin with the string in \a word.
|
||||||
|
* 2) Are valid in a command after the string in \a text.
|
||||||
|
*
|
||||||
|
* The first entry (offset 0) of the result is the longest common substring
|
||||||
|
* in the results, useful to extend the string that has been completed.
|
||||||
|
* Subsequent entries are all possible values.
|
||||||
|
*
|
||||||
|
* \note All strings and the vector itself are malloc'ed and must be freed
|
||||||
|
* by the caller.
|
||||||
|
*
|
||||||
|
* \note The vector is sorted and does not contain any duplicates.
|
||||||
|
*/
|
||||||
|
struct ast_vector_string *ast_cli_completion_vector(const char *text, const char *word);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Command completion for the list of active channels.
|
* \brief Command completion for the list of active channels.
|
||||||
*
|
*
|
||||||
|
134
main/cli.c
134
main/cli.c
@@ -2488,76 +2488,98 @@ int ast_cli_generatornummatches(const char *text, const char *word)
|
|||||||
return matches;
|
return matches;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void destroy_match_list(char **match_list, int matches)
|
|
||||||
{
|
|
||||||
if (match_list) {
|
|
||||||
int idx;
|
|
||||||
|
|
||||||
for (idx = 1; idx < matches; ++idx) {
|
|
||||||
ast_free(match_list[idx]);
|
|
||||||
}
|
|
||||||
ast_free(match_list);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char **ast_cli_completion_matches(const char *text, const char *word)
|
char **ast_cli_completion_matches(const char *text, const char *word)
|
||||||
{
|
{
|
||||||
char **match_list = NULL, *retstr, *prevstr;
|
struct ast_vector_string *vec = ast_cli_completion_vector(text, word);
|
||||||
char **new_list;
|
char **match_list;
|
||||||
size_t match_list_len, max_equal, which, i;
|
|
||||||
int matches = 0;
|
|
||||||
|
|
||||||
/* leave entry 0 free for the longest common substring */
|
if (!vec) {
|
||||||
match_list_len = 1;
|
return NULL;
|
||||||
while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
|
}
|
||||||
if (matches + 1 >= match_list_len) {
|
|
||||||
match_list_len <<= 1;
|
if (AST_VECTOR_APPEND(vec, NULL)) {
|
||||||
new_list = ast_realloc(match_list, match_list_len * sizeof(*match_list));
|
/* We failed to NULL terminate the elements */
|
||||||
if (!new_list) {
|
AST_VECTOR_CALLBACK_VOID(vec, ast_free);
|
||||||
destroy_match_list(match_list, matches);
|
AST_VECTOR_PTR_FREE(vec);
|
||||||
return NULL;
|
|
||||||
}
|
return NULL;
|
||||||
match_list = new_list;
|
}
|
||||||
|
|
||||||
|
match_list = AST_VECTOR_STEAL_ELEMENTS(vec);
|
||||||
|
AST_VECTOR_PTR_FREE(vec);
|
||||||
|
|
||||||
|
return match_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ast_vector_string *ast_cli_completion_vector(const char *text, const char *word)
|
||||||
|
{
|
||||||
|
char *retstr, *prevstr;
|
||||||
|
size_t max_equal;
|
||||||
|
size_t which = 0;
|
||||||
|
struct ast_vector_string *vec = ast_calloc(1, sizeof(*vec));
|
||||||
|
|
||||||
|
if (!vec) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((retstr = ast_cli_generator(text, word, which)) != NULL) {
|
||||||
|
if (AST_VECTOR_ADD_SORTED(vec, retstr, strcasecmp)) {
|
||||||
|
ast_free(retstr);
|
||||||
|
|
||||||
|
goto vector_cleanup;
|
||||||
}
|
}
|
||||||
match_list[++matches] = retstr;
|
|
||||||
|
++which;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!match_list) {
|
if (!AST_VECTOR_SIZE(vec)) {
|
||||||
return match_list; /* NULL */
|
AST_VECTOR_PTR_FREE(vec);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
prevstr = AST_VECTOR_GET(vec, 0);
|
||||||
|
max_equal = strlen(prevstr);
|
||||||
|
which = 1;
|
||||||
|
|
||||||
/* Find the longest substring that is common to all results
|
/* Find the longest substring that is common to all results
|
||||||
* (it is a candidate for completion), and store a copy in entry 0.
|
* (it is a candidate for completion), and store a copy in entry 0.
|
||||||
*/
|
*/
|
||||||
prevstr = match_list[1];
|
while (which < AST_VECTOR_SIZE(vec)) {
|
||||||
max_equal = strlen(prevstr);
|
size_t i = 0;
|
||||||
for (which = 2; which <= matches; which++) {
|
|
||||||
for (i = 0; i < max_equal && toupper(prevstr[i]) == toupper(match_list[which][i]); i++)
|
|
||||||
continue;
|
|
||||||
max_equal = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
retstr = ast_malloc(max_equal + 1);
|
retstr = AST_VECTOR_GET(vec, which);
|
||||||
if (!retstr) {
|
/* Check for and remove duplicate strings. */
|
||||||
destroy_match_list(match_list, matches);
|
if (!strcasecmp(prevstr, retstr)) {
|
||||||
return NULL;
|
AST_VECTOR_REMOVE(vec, which, 1);
|
||||||
}
|
|
||||||
ast_copy_string(retstr, match_list[1], max_equal + 1);
|
|
||||||
match_list[0] = retstr;
|
|
||||||
|
|
||||||
/* ensure that the array is NULL terminated */
|
|
||||||
if (matches + 1 >= match_list_len) {
|
|
||||||
new_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list));
|
|
||||||
if (!new_list) {
|
|
||||||
ast_free(retstr);
|
ast_free(retstr);
|
||||||
destroy_match_list(match_list, matches);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
match_list = new_list;
|
|
||||||
}
|
|
||||||
match_list[matches + 1] = NULL;
|
|
||||||
|
|
||||||
return match_list;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < max_equal && toupper(prevstr[i]) == toupper(retstr[i])) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
max_equal = i;
|
||||||
|
prevstr = retstr;
|
||||||
|
++which;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Insert longest match to position 0. */
|
||||||
|
retstr = ast_strndup(AST_VECTOR_GET(vec, 0), max_equal);
|
||||||
|
if (!retstr || AST_VECTOR_INSERT_AT(vec, 0, retstr)) {
|
||||||
|
ast_free(retstr);
|
||||||
|
goto vector_cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
return vec;
|
||||||
|
|
||||||
|
vector_cleanup:
|
||||||
|
AST_VECTOR_CALLBACK_VOID(vec, ast_free);
|
||||||
|
AST_VECTOR_PTR_FREE(vec);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! \brief returns true if there are more words to match */
|
/*! \brief returns true if there are more words to match */
|
||||||
|
Reference in New Issue
Block a user