cleanup tab completion more and introduce new callback based expansion functions to do real time tab completion in some areas, needs people to add more funcs etc

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@15952 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale
2009-12-14 20:10:06 +00:00
parent 7d6f7597d8
commit 4a86f888be
9 changed files with 476 additions and 156 deletions

View File

@@ -518,16 +518,42 @@ struct helper {
static int comp_callback(void *pArg, int argc, char **argv, char **columnNames)
{
struct helper *h = (struct helper *) pArg;
char *target = NULL;
char *target = NULL, *str = NULL, *cur = NULL;
switch_size_t x, y;
int i;
target = argv[0];
if (argc > 0) target = argv[0];
if (argc > 1) str = argv[1];
if (argc > 2) cur = argv[2];
if (cur) {
while (*cur == ' ') cur++;
}
if (zstr(cur)) cur = NULL;
if (zstr(str)) str = NULL;
if (!target) {
return -1;
}
if (!zstr(target) && *target == ':' && *(target+1) == ':') {
char *r_argv[3] = { 0 }, *r_cols[3] = { 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) {
if (!cur || !strncmp(m->val, cur, strlen(cur))) {
r_argv[0] = m->val;
comp_callback(h, 1, r_argv, r_cols);
}
}
switch_console_free_matches(&matches);
}
return 0;
}
if (!zstr(target)) {
fprintf(h->out, "[%20s]\t", target);
switch_copy_string(h->last, target, sizeof(h->last));
@@ -557,6 +583,54 @@ static int comp_callback(void *pArg, int argc, char **argv, char **columnNames)
return 0;
}
struct match_helper {
switch_console_callback_match_t *my_matches;
};
static int uuid_callback(void *pArg, int argc, char **argv, char **columnNames)
{
struct match_helper *h = (struct match_helper *) pArg;
switch_console_push_match(&h->my_matches, argv[0]);
return 0;
}
SWITCH_DECLARE(switch_status_t) switch_console_list_uuid(const char *line, const char *cursor, switch_console_callback_match_t **matches)
{
char *sql;
struct match_helper h = { 0 };
switch_cache_db_handle_t *db = NULL;
switch_status_t status = SWITCH_STATUS_FALSE;
char *errmsg;
switch_core_db_handle(&db);
if (!zstr(cursor)) {
sql = switch_mprintf("select distinct uuid from channels where uuid like '%q%%' and hostname='%q' order by uuid",
cursor, switch_core_get_variable("hostname"));
} else {
sql = switch_mprintf("select distinct uuid from channels where hostname='%q' order by uuid",
switch_core_get_variable("hostname"));
}
switch_cache_db_execute_sql_callback(db, sql, uuid_callback, &h, &errmsg);
free(sql);
switch_cache_db_release_db_handle(&db);
if (h.my_matches) {
*matches = h.my_matches;
status = SWITCH_STATUS_SUCCESS;
}
return status;
}
static unsigned char complete(EditLine * el, int ch)
{
switch_cache_db_handle_t *db = NULL;
@@ -608,11 +682,8 @@ static unsigned char complete(EditLine * el, int ch)
if (h.words == 0) {
sql = switch_mprintf("select distinct name from interfaces where type='api' and name like '%q%%' and hostname='%q' order by name",
buf, switch_core_get_variable("hostname"));
} else if (h.words == 1) {
sql = switch_mprintf("select distinct uuid from channels where uuid like '%q%%' and hostname='%q' order by uuid",
buf, switch_core_get_variable("hostname"));
}
if (sql) {
switch_cache_db_execute_sql_callback(db, sql, comp_callback, &h, &errmsg);
@@ -640,22 +711,29 @@ static unsigned char complete(EditLine * el, int ch)
stream.write_function(&stream, "select distinct a1 from complete where " "a1 not in (select name from interfaces where hostname='%s') %s ",
switch_core_get_variable("hostname"), argc ? "and" : "");
} else {
stream.write_function(&stream, "select distinct a%d from complete where ", h.words + 1);
if (db->type == SCDB_TYPE_CORE_DB) {
stream.write_function(&stream, "select distinct a%d,'%q','%q' from complete where ", h.words + 1, switch_str_nil(dup), switch_str_nil(lp));
} else {
stream.write_function(&stream, "select distinct a%d,'%q','%w' from complete where ", h.words + 1, switch_str_nil(dup), switch_str_nil(lp));
}
}
for (x = 0; x < argc && x < 11; x++) {
if (h.words + 1 > argc) {
if (db->type == SCDB_TYPE_CORE_DB) {
stream.write_function(&stream, "(a%d = '' or a%d = '%q')%q", x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d = '%q')%q",
x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
} else {
stream.write_function(&stream, "(a%d = '' or a%d = '%w')%w", x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d = '%w')%w",
x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
}
} else {
if (db->type == SCDB_TYPE_CORE_DB) {
stream.write_function(&stream, "(a%d = '' or a%d like '%q%%')%q", x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d like '%q%%')%q",
x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
} else {
stream.write_function(&stream, "(a%d = '' or a%d like '%w%%')%w", x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d like '%w%%')%w",
x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
}
}
}
@@ -700,6 +778,98 @@ static unsigned char complete(EditLine * el, int ch)
return (ret);
}
static struct {
switch_hash_t *func_hash;
switch_mutex_t *func_mutex;
} globals;
SWITCH_DECLARE(switch_status_t) switch_console_init(switch_memory_pool_t *pool)
{
switch_mutex_init(&globals.func_mutex, SWITCH_MUTEX_NESTED, pool);
switch_core_hash_init(&globals.func_hash, pool);
switch_console_add_complete_func("::console::list_uuid", switch_console_list_uuid);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_console_shutdown(void)
{
return switch_core_hash_destroy(&globals.func_hash);
}
SWITCH_DECLARE(switch_status_t) switch_console_add_complete_func(const char *name, switch_console_complete_callback_t cb)
{
switch_status_t status;
switch_mutex_lock(globals.func_mutex);
status = switch_core_hash_insert(globals.func_hash, name, (void *)(intptr_t)cb);
switch_mutex_unlock(globals.func_mutex);
return status;
}
SWITCH_DECLARE(switch_status_t) switch_console_del_complete_func(const char *name)
{
switch_status_t status;
switch_mutex_lock(globals.func_mutex);
status = switch_core_hash_insert(globals.func_hash, name, NULL);
switch_mutex_unlock(globals.func_mutex);
return status;
}
SWITCH_DECLARE(void) switch_console_free_matches(switch_console_callback_match_t **matches)
{
switch_console_callback_match_t *my_match = *matches;
switch_console_callback_match_node_t *m, *cur;
/* Don't play with matches */
*matches = NULL;
m = my_match->head;
while(m) {
cur = m;
m = m->next;
free(cur->val);
free(cur);
}
}
SWITCH_DECLARE(void) switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val)
{
switch_console_callback_match_node_t *match;
if (!*matches) {
switch_zmalloc(*matches, sizeof(**matches));
}
switch_zmalloc(match, sizeof(*match));
match->val = strdup(new_val);
if ((*matches)->head) {
(*matches)->end->next = match;
} else {
(*matches)->head = match;
}
(*matches)->end = match;
}
SWITCH_DECLARE(switch_status_t) switch_console_run_complete_func(const char *func, const char *line, const char *cursor,
switch_console_callback_match_t **matches)
{
switch_console_complete_callback_t cb;
switch_status_t status = SWITCH_STATUS_FALSE;
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, cursor, matches);
}
switch_mutex_unlock(globals.func_mutex);
return status;
}
SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string)
{