From 86304c02474a8dc9571bcf1c3074caa3bc2b9bc0 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 15 Dec 2009 23:05:49 +0000 Subject: [PATCH] more tab completion cleanup git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@15977 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_console.h | 4 +- src/include/switch_types.h | 1 + src/mod/endpoints/mod_sofia/mod_sofia.c | 2 +- src/switch_console.c | 65 +++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/include/switch_console.h b/src/include/switch_console.h index e8d666f255..e31600defb 100644 --- a/src/include/switch_console.h +++ b/src/include/switch_console.h @@ -85,7 +85,9 @@ SWITCH_DECLARE(switch_status_t) switch_console_run_complete_func(const char *fun const char *last_word, switch_console_callback_match_t **matches); SWITCH_DECLARE(void) switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val); SWITCH_DECLARE(void) switch_console_free_matches(switch_console_callback_match_t **matches); -SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const char *last_word, FILE *console_out, switch_stream_handle_t *stream, switch_xml_t xml); +SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const char *last_word, + FILE *console_out, switch_stream_handle_t *stream, switch_xml_t xml); +SWITCH_DECLARE(void) switch_console_sort_matches(switch_console_callback_match_t *matches); SWITCH_END_EXTERN_C #endif diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 2659355cdd..9ce407f31e 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -1494,6 +1494,7 @@ typedef struct switch_console_callback_match_node switch_console_callback_match_ struct switch_console_callback_match { struct switch_console_callback_match_node *head; struct switch_console_callback_match_node *end; + int count; int dynamic; }; typedef struct switch_console_callback_match switch_console_callback_match_t; diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c index b12456ede4..565efc445d 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.c +++ b/src/mod/endpoints/mod_sofia/mod_sofia.c @@ -3698,7 +3698,7 @@ static switch_status_t list_profile_gateway(const char *line, const char *cursor dup = strdup(line); argc = switch_split(dup, ' ', argv); - if (!argv[2]) { + if (zstr(argv[2]) || !strcmp(argv[2], " ")) { goto end; } diff --git a/src/switch_console.c b/src/switch_console.c index 3c5bfff13a..5c306fd944 100644 --- a/src/switch_console.c +++ b/src/switch_console.c @@ -422,9 +422,8 @@ static int comp_callback(void *pArg, int argc, char **argv, char **columnNames) } if (!zstr(target) && *target == ':' && *(target+1) == ':') { - char *r_argv[3] = { 0 }, *r_cols[3] = { 0 }; + char *r_argv[1] = { 0 }, *r_cols[1] = { 0 }; switch_console_callback_match_t *matches; - r_cols[0] = "match"; if (switch_console_run_complete_func(target, str, cur, &matches) == SWITCH_STATUS_SUCCESS) { switch_console_callback_match_node_t *m; for (m = matches->head; m; m = m->next) { @@ -529,7 +528,8 @@ SWITCH_DECLARE(switch_status_t) switch_console_list_uuid(const char *line, const } -SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const char *cursor, FILE *console_out, switch_stream_handle_t *stream, switch_xml_t xml) +SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const char *cursor, FILE *console_out, + switch_stream_handle_t *stream, switch_xml_t xml) { switch_cache_db_handle_t *db = NULL; char *sql = NULL; @@ -573,6 +573,7 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch if (*p == ' ') { lp = p; h.words++; + while(*p == ' ') p++; } } @@ -1131,6 +1132,58 @@ SWITCH_DECLARE(void) switch_console_free_matches(switch_console_callback_match_t } } +SWITCH_DECLARE(void) switch_console_sort_matches(switch_console_callback_match_t *matches) +{ + switch_console_callback_match_node_t *p = NULL, *sort[4] = { 0 }; + int i, j; + + switch_assert(matches); + + if (matches->count < 2) { + return; + } + + for(i = 1; i < matches->count; i++) { + sort[0] = NULL; + sort[1] = matches->head; + sort[2] = sort[1] ? sort[1]->next : NULL; + sort[3] = sort[2] ? sort[2]->next : NULL; + + for(j = 1; j <= (matches->count - i); j++) { + if (strcmp(sort[1]->val, sort[2]->val) > 0) { + sort[1]->next = sort[3]; + sort[2]->next = sort[1]; + + if (sort[0]) sort[0]->next = sort[2]; + if (sort[1] == matches->head) matches->head = sort[2]; + + + + + sort[0] = sort[2]; + sort[2] = sort[1]->next; + if (sort[3] && sort[3]->next) sort[3] = sort[3]->next; + + } else { + sort[0] = sort[1]; + sort[1] = sort[2]; + sort[2] = sort[3]; + if (sort[3] && sort[3]->next) sort[3] = sort[3]->next; + } + + } + } + + p = matches->head; + + for(i = 1; i < matches->count; i++) p = p->next; + + if (p) { + p->next = NULL; + matches->end = p; + } +} + SWITCH_DECLARE(void) switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val) { switch_console_callback_match_node_t *match; @@ -1149,6 +1202,8 @@ SWITCH_DECLARE(void) switch_console_push_match(switch_console_callback_match_t * (*matches)->head = match; } + (*matches)->count++; + (*matches)->end = match; } @@ -1160,7 +1215,9 @@ SWITCH_DECLARE(switch_status_t) switch_console_run_complete_func(const char *fun switch_mutex_lock(globals.func_mutex); if ((cb = (switch_console_complete_callback_t)(intptr_t)switch_core_hash_find(globals.func_hash, func))) { - status = cb(line, last_word, matches); + if ((status = cb(line, last_word, matches)) == SWITCH_STATUS_SUCCESS) { + switch_console_sort_matches(*matches); + } } switch_mutex_unlock(globals.func_mutex);