mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-05 20:20:07 +00:00
CLI: Finish conversion of completion handling to vectors.
Change-Id: Ib81318f4ee52a5e73b003316e13fe9be1dd897a1
This commit is contained in:
@@ -3130,10 +3130,9 @@ static char *cli_prompt(EditLine *editline)
|
|||||||
return ast_str_buffer(prompt);
|
return ast_str_buffer(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char **ast_el_strtoarr(char *buf)
|
static struct ast_vector_string *ast_el_strtoarr(char *buf)
|
||||||
{
|
{
|
||||||
char *retstr;
|
char *retstr;
|
||||||
char **match_list;
|
|
||||||
struct ast_vector_string *vec = ast_calloc(1, sizeof(*vec));
|
struct ast_vector_string *vec = ast_calloc(1, sizeof(*vec));
|
||||||
|
|
||||||
if (!vec) {
|
if (!vec) {
|
||||||
@@ -3145,8 +3144,14 @@ static char **ast_el_strtoarr(char *buf)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Older daemons sent duplicates. */
|
||||||
|
if (AST_VECTOR_GET_CMP(vec, retstr, strcasecmp)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
retstr = ast_strdup(retstr);
|
retstr = ast_strdup(retstr);
|
||||||
if (!retstr || AST_VECTOR_APPEND(vec, retstr)) {
|
/* Older daemons sent unsorted. */
|
||||||
|
if (!retstr || AST_VECTOR_ADD_SORTED(vec, retstr, strcasecmp)) {
|
||||||
ast_free(retstr);
|
ast_free(retstr);
|
||||||
goto vector_cleanup;
|
goto vector_cleanup;
|
||||||
}
|
}
|
||||||
@@ -3156,15 +3161,7 @@ static char **ast_el_strtoarr(char *buf)
|
|||||||
goto vector_cleanup;
|
goto vector_cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (AST_VECTOR_APPEND(vec, NULL)) {
|
return vec;
|
||||||
/* We failed to NULL terminate the elements */
|
|
||||||
goto vector_cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
match_list = AST_VECTOR_STEAL_ELEMENTS(vec);
|
|
||||||
AST_VECTOR_PTR_FREE(vec);
|
|
||||||
|
|
||||||
return match_list;
|
|
||||||
|
|
||||||
vector_cleanup:
|
vector_cleanup:
|
||||||
AST_VECTOR_CALLBACK_VOID(vec, ast_free);
|
AST_VECTOR_CALLBACK_VOID(vec, ast_free);
|
||||||
@@ -3173,17 +3170,7 @@ vector_cleanup:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ast_el_sort_compare(const void *i1, const void *i2)
|
static void ast_cli_display_match_list(struct ast_vector_string *matches, int max)
|
||||||
{
|
|
||||||
char *s1, *s2;
|
|
||||||
|
|
||||||
s1 = ((char **)i1)[0];
|
|
||||||
s2 = ((char **)i2)[0];
|
|
||||||
|
|
||||||
return strcasecmp(s1, s2);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ast_cli_display_match_list(char **matches, int max)
|
|
||||||
{
|
{
|
||||||
int idx = 1;
|
int idx = 1;
|
||||||
/* find out how many entries can be put on one line, with two spaces between strings */
|
/* find out how many entries can be put on one line, with two spaces between strings */
|
||||||
@@ -3196,14 +3183,9 @@ static void ast_cli_display_match_list(char **matches, int max)
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
int numoutputline;
|
int numoutputline;
|
||||||
|
|
||||||
for (numoutputline = 0; numoutputline < limit && matches[idx]; idx++) {
|
for (numoutputline = 0; numoutputline < limit && idx < AST_VECTOR_SIZE(matches); idx++) {
|
||||||
/* Don't print dupes */
|
|
||||||
if ( (matches[idx+1] != NULL && strcmp(matches[idx], matches[idx+1]) == 0 ) ) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
numoutputline++;
|
numoutputline++;
|
||||||
fprintf(stdout, "%-*s ", max, matches[idx]);
|
fprintf(stdout, "%-*s ", max, AST_VECTOR_GET(matches, idx));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!numoutputline) {
|
if (!numoutputline) {
|
||||||
@@ -3219,8 +3201,7 @@ static char *cli_complete(EditLine *editline, int ch)
|
|||||||
{
|
{
|
||||||
int len = 0;
|
int len = 0;
|
||||||
char *ptr;
|
char *ptr;
|
||||||
int nummatches = 0;
|
struct ast_vector_string *matches;
|
||||||
char **matches;
|
|
||||||
int retval = CC_ERROR;
|
int retval = CC_ERROR;
|
||||||
char savechr;
|
char savechr;
|
||||||
int res;
|
int res;
|
||||||
@@ -3294,44 +3275,28 @@ static char *cli_complete(EditLine *editline, int ch)
|
|||||||
matches = ast_el_strtoarr(mbuf);
|
matches = ast_el_strtoarr(mbuf);
|
||||||
ast_free(mbuf);
|
ast_free(mbuf);
|
||||||
} else {
|
} else {
|
||||||
matches = ast_cli_completion_matches((char *)lf->buffer,ptr);
|
matches = ast_cli_completion_vector((char *)lf->buffer, ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matches) {
|
if (matches) {
|
||||||
int i;
|
int i;
|
||||||
int maxlen, match_len;
|
int maxlen, match_len;
|
||||||
|
const char *best_match = AST_VECTOR_GET(matches, 0);
|
||||||
|
|
||||||
while (matches[nummatches + 1]) {
|
if (!ast_strlen_zero(best_match)) {
|
||||||
nummatches++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ast_opt_remote && nummatches > 1) {
|
|
||||||
qsort(&matches[0], (size_t)(nummatches), sizeof(char *), ast_el_sort_compare);
|
|
||||||
nummatches = 1;
|
|
||||||
i = 1;
|
|
||||||
while (matches[i + 1]) {
|
|
||||||
if (strcasecmp(matches[i], matches[i + 1])) {
|
|
||||||
/* don't count duplicates. */
|
|
||||||
nummatches++;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matches[0][0] != '\0') {
|
|
||||||
el_deletestr(editline, (int) len);
|
el_deletestr(editline, (int) len);
|
||||||
el_insertstr(editline, matches[0]);
|
el_insertstr(editline, best_match);
|
||||||
retval = CC_REFRESH;
|
retval = CC_REFRESH;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nummatches == 1) {
|
if (AST_VECTOR_SIZE(matches) == 2) {
|
||||||
/* Found an exact match */
|
/* Found an exact match */
|
||||||
el_insertstr(editline, " ");
|
el_insertstr(editline, " ");
|
||||||
retval = CC_REFRESH;
|
retval = CC_REFRESH;
|
||||||
} else {
|
} else {
|
||||||
/* Must be more than one match */
|
/* Must be more than one match */
|
||||||
for (i = 1, maxlen = 0; matches[i]; i++) {
|
for (i = 1, maxlen = 0; i < AST_VECTOR_SIZE(matches); i++) {
|
||||||
match_len = strlen(matches[i]);
|
match_len = strlen(AST_VECTOR_GET(matches, i));
|
||||||
if (match_len > maxlen) {
|
if (match_len > maxlen) {
|
||||||
maxlen = match_len;
|
maxlen = match_len;
|
||||||
}
|
}
|
||||||
@@ -3341,10 +3306,8 @@ static char *cli_complete(EditLine *editline, int ch)
|
|||||||
ast_cli_display_match_list(matches, maxlen);
|
ast_cli_display_match_list(matches, maxlen);
|
||||||
retval = CC_REDISPLAY;
|
retval = CC_REDISPLAY;
|
||||||
}
|
}
|
||||||
for (i = 0; matches[i]; i++) {
|
AST_VECTOR_CALLBACK_VOID(matches, ast_free);
|
||||||
ast_free(matches[i]);
|
AST_VECTOR_PTR_FREE(matches);
|
||||||
}
|
|
||||||
ast_free(matches);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*((char *) lf->cursor) = savechr;
|
*((char *) lf->cursor) = savechr;
|
||||||
|
Reference in New Issue
Block a user