提交 f40fecf4 authored 作者: Anthony Minessale's avatar Anthony Minessale

rearrange for build

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@15961 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 c94dcbdb
...@@ -36,6 +36,14 @@ ...@@ -36,6 +36,14 @@
#define CMD_BUFLEN 1024; #define CMD_BUFLEN 1024;
#ifdef SWITCH_HAVE_LIBEDIT #ifdef SWITCH_HAVE_LIBEDIT
#include <histedit.h>
static EditLine *el;
static History *myhistory;
static HistEvent ev;
static char *hfile = NULL;
/* /*
* store a strdup() of the string configured in XML * store a strdup() of the string configured in XML
* bound to each of the 12 function key * bound to each of the 12 function key
...@@ -83,6 +91,19 @@ static switch_status_t console_xml_config(void) ...@@ -83,6 +91,19 @@ static switch_status_t console_xml_config(void)
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
#else
#define CC_NORM 0
#define CC_NEWLINE 1
#define CC_EOF 2
#define CC_ARGHACK 3
#define CC_REFRESH 4
#define CC_CURSOR 5
#define CC_ERROR 6
#define CC_FATAL 7
#define CC_REDISPLAY 8
#define CC_REFRESH_BEEP 9
#endif #endif
SWITCH_DECLARE_NONSTD(switch_status_t) switch_console_stream_raw_write(switch_stream_handle_t *handle, uint8_t *data, switch_size_t datalen) SWITCH_DECLARE_NONSTD(switch_status_t) switch_console_stream_raw_write(switch_stream_handle_t *handle, uint8_t *data, switch_size_t datalen)
...@@ -364,145 +385,6 @@ SWITCH_DECLARE(void) switch_console_printf(switch_text_channel_t channel, const ...@@ -364,145 +385,6 @@ SWITCH_DECLARE(void) switch_console_printf(switch_text_channel_t channel, const
static char hostname[256] = ""; static char hostname[256] = "";
static int32_t running = 1; static int32_t running = 1;
#ifdef SWITCH_HAVE_LIBEDIT
#include <histedit.h>
static char prompt_str[512] = "";
/*
* If a fnkey is configured then process the command
*/
static unsigned char console_fnkey_pressed(int i)
{
char *c, *cmd;
assert((i > 0) && (i <= 12));
c = console_fnkeys[i - 1];
/* This new line is necessary to avoid output to begin after the ">" of the CLI's prompt */
switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_CONSOLE, "\n");
if (c == NULL) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "FUNCTION KEY F%d IS NOT BOUND, please edit switch.conf XML file\n", i);
return CC_REDISPLAY;
}
cmd = strdup(c);
switch_console_process(cmd, 0);
free(cmd);
return CC_REDISPLAY;
}
static unsigned char console_f1key(EditLine * el, int ch)
{
return console_fnkey_pressed(1);
}
static unsigned char console_f2key(EditLine * el, int ch)
{
return console_fnkey_pressed(2);
}
static unsigned char console_f3key(EditLine * el, int ch)
{
return console_fnkey_pressed(3);
}
static unsigned char console_f4key(EditLine * el, int ch)
{
return console_fnkey_pressed(4);
}
static unsigned char console_f5key(EditLine * el, int ch)
{
return console_fnkey_pressed(5);
}
static unsigned char console_f6key(EditLine * el, int ch)
{
return console_fnkey_pressed(6);
}
static unsigned char console_f7key(EditLine * el, int ch)
{
return console_fnkey_pressed(7);
}
static unsigned char console_f8key(EditLine * el, int ch)
{
return console_fnkey_pressed(8);
}
static unsigned char console_f9key(EditLine * el, int ch)
{
return console_fnkey_pressed(9);
}
static unsigned char console_f10key(EditLine * el, int ch)
{
return console_fnkey_pressed(10);
}
static unsigned char console_f11key(EditLine * el, int ch)
{
return console_fnkey_pressed(11);
}
static unsigned char console_f12key(EditLine * el, int ch)
{
return console_fnkey_pressed(12);
}
char *prompt(EditLine * e)
{
if (*prompt_str == '\0') {
switch_snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", hostname);
}
return prompt_str;
}
static EditLine *el;
static History *myhistory;
static HistEvent ev;
static char *hfile = NULL;
static void *SWITCH_THREAD_FUNC console_thread(switch_thread_t *thread, void *obj)
{
int count;
const char *line;
switch_memory_pool_t *pool = (switch_memory_pool_t *) obj;
while (running) {
int32_t arg = 0;
if (getppid() == 1) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "We've become an orphan, no more console for us.\n");
break;
}
switch_core_session_ctl(SCSC_CHECK_RUNNING, &arg);
if (!arg) {
break;
}
line = el_gets(el, &count);
if (count > 1) {
if (!zstr(line)) {
char *cmd = strdup(line);
char *p;
const LineInfo *lf = el_line(el);
char *foo = (char *) lf->buffer;
if ((p = strrchr(cmd, '\r')) || (p = strrchr(cmd, '\n'))) {
*p = '\0';
}
assert(cmd != NULL);
history(myhistory, &ev, H_ENTER, line);
running = switch_console_process(cmd, 0);
el_deletestr(el, strlen(foo) + 1);
memset(foo, 0, strlen(foo));
free(cmd);
}
}
switch_cond_next();
}
switch_core_destroy_memory_pool(&pool);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Editline thread exiting\n");
return NULL;
}
struct helper { struct helper {
...@@ -654,6 +536,12 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch ...@@ -654,6 +536,12 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch
unsigned char ret = CC_REDISPLAY; unsigned char ret = CC_REDISPLAY;
int pos = 0; int pos = 0;
#ifndef SWITCH_HAVE_LIBEDIT
if (!stream) {
return CC_ERROR;
}
#endif
switch_core_db_handle(&db); switch_core_db_handle(&db);
if (!zstr(cursor) && !zstr(line)) { if (!zstr(cursor) && !zstr(line)) {
...@@ -785,6 +673,7 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch ...@@ -785,6 +673,7 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch
} }
} }
#ifdef SWITCH_HAVE_LIBEDIT
if (h.out) { if (h.out) {
if (h.hits == 1 && !zstr(h.last)) { if (h.hits == 1 && !zstr(h.last)) {
el_deletestr(el, h.len); el_deletestr(el, h.len);
...@@ -795,6 +684,7 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch ...@@ -795,6 +684,7 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch
el_insertstr(el, h.partial); el_insertstr(el, h.partial);
} }
} }
#endif
end: end:
...@@ -810,245 +700,151 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch ...@@ -810,245 +700,151 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch
return (ret); return (ret);
} }
static unsigned char complete(EditLine * el, int ch)
{
const LineInfo *lf = el_line(el);
return switch_console_complete(lf->buffer, lf->cursor, switch_core_get_console(), NULL);
}
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) #ifdef SWITCH_HAVE_LIBEDIT
{ static char prompt_str[512] = "";
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) /*
* If a fnkey is configured then process the command
*/
static unsigned char console_fnkey_pressed(int i)
{ {
switch_status_t status; char *c, *cmd;
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; assert((i > 0) && (i <= 12));
}
SWITCH_DECLARE(switch_status_t) switch_console_del_complete_func(const char *name) c = console_fnkeys[i - 1];
{
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; /* This new line is necessary to avoid output to begin after the ">" of the CLI's prompt */
} switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_CONSOLE, "\n");
SWITCH_DECLARE(void) switch_console_free_matches(switch_console_callback_match_t **matches) if (c == NULL) {
{ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "FUNCTION KEY F%d IS NOT BOUND, please edit switch.conf XML file\n", i);
switch_console_callback_match_t *my_match = *matches; return CC_REDISPLAY;
switch_console_callback_match_node_t *m, *cur; }
/* Don't play with matches */ cmd = strdup(c);
*matches = NULL; switch_console_process(cmd, 0);
free(cmd);
m = my_match->head; return CC_REDISPLAY;
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) static unsigned char console_f1key(EditLine * el, int ch)
{ {
switch_console_callback_match_node_t *match; return console_fnkey_pressed(1);
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;
} }
static unsigned char console_f2key(EditLine * el, int ch)
SWITCH_DECLARE(switch_status_t) switch_console_run_complete_func(const char *func, const char *line, const char *last_word,
switch_console_callback_match_t **matches)
{ {
switch_console_complete_callback_t cb; return console_fnkey_pressed(2);
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, last_word, matches);
}
switch_mutex_unlock(globals.func_mutex);
return status;
} }
static unsigned char console_f3key(EditLine * el, int ch)
SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string)
{ {
char *mydata = NULL, *argv[11] = { 0 }; return console_fnkey_pressed(3);
int argc, x; }
switch_status_t status = SWITCH_STATUS_FALSE; static unsigned char console_f4key(EditLine * el, int ch)
{
if (string && (mydata = strdup(string))) { return console_fnkey_pressed(4);
if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) { }
switch_cache_db_handle_t *db = NULL; static unsigned char console_f5key(EditLine * el, int ch)
switch_stream_handle_t mystream = { 0 }; {
SWITCH_STANDARD_STREAM(mystream); return console_fnkey_pressed(5);
}
switch_core_db_handle(&db); static unsigned char console_f6key(EditLine * el, int ch)
{
return console_fnkey_pressed(6);
}
static unsigned char console_f7key(EditLine * el, int ch)
{
return console_fnkey_pressed(7);
}
static unsigned char console_f8key(EditLine * el, int ch)
{
return console_fnkey_pressed(8);
}
static unsigned char console_f9key(EditLine * el, int ch)
{
return console_fnkey_pressed(9);
}
static unsigned char console_f10key(EditLine * el, int ch)
{
return console_fnkey_pressed(10);
}
static unsigned char console_f11key(EditLine * el, int ch)
{
return console_fnkey_pressed(11);
}
static unsigned char console_f12key(EditLine * el, int ch)
{
return console_fnkey_pressed(12);
}
if (!strcasecmp(argv[0], "stickyadd")) {
mystream.write_function(&mystream, "insert into complete values (1,");
for (x = 0; x < 10; x++) {
if (argv[x + 1] && !strcasecmp(argv[x + 1], "_any_")) {
mystream.write_function(&mystream, "%s", "'', ");
} else {
if (db->type == SCDB_TYPE_CORE_DB) {
mystream.write_function(&mystream, "'%q', ", switch_str_nil(argv[x + 1]));
} else {
mystream.write_function(&mystream, "'%w', ", switch_str_nil(argv[x + 1]));
}
}
}
mystream.write_function(&mystream, " '%s')", switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, mystream.data, 5);
status = SWITCH_STATUS_SUCCESS;
} else if (!strcasecmp(argv[0], "add")) {
mystream.write_function(&mystream, "insert into complete values (0,");
for (x = 0; x < 10; x++) {
if (argv[x + 1] && !strcasecmp(argv[x + 1], "_any_")) {
mystream.write_function(&mystream, "%s", "'', ");
} else {
if (db->type == SCDB_TYPE_CORE_DB) {
mystream.write_function(&mystream, "'%q', ", switch_str_nil(argv[x + 1]));
} else {
mystream.write_function(&mystream, "'%w', ", switch_str_nil(argv[x + 1]));
}
}
}
mystream.write_function(&mystream, " '%s')", switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, mystream.data, 5);
status = SWITCH_STATUS_SUCCESS;
} else if (!strcasecmp(argv[0], "del")) {
char *what = argv[1];
if (!strcasecmp(what, "*")) {
switch_cache_db_persistant_execute(db, "delete from complete", 1);
} else {
mystream.write_function(&mystream, "delete from complete where ");
for (x = 0; x < argc - 1; x++) {
if (db->type == SCDB_TYPE_CORE_DB) {
mystream.write_function(&mystream, "a%d = '%q'%q", x + 1, switch_str_nil(argv[x + 1]), x == argc - 2 ? "" : " and ");
} else {
mystream.write_function(&mystream, "a%d = '%w'%w", x + 1, switch_str_nil(argv[x + 1]), x == argc - 2 ? "" : " and ");
}
}
mystream.write_function(&mystream, " and hostname='%s'", switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, mystream.data, 1);
}
status = SWITCH_STATUS_SUCCESS;
}
switch_safe_free(mystream.data); char *prompt(EditLine * e)
switch_cache_db_release_db_handle(&db); {
} if (*prompt_str == '\0') {
switch_snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", hostname);
} }
switch_safe_free(mydata); return prompt_str;
return status;
} }
static void *SWITCH_THREAD_FUNC console_thread(switch_thread_t *thread, void *obj)
SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string)
{ {
char *mydata = NULL, *argv[3] = { 0 }; int count;
int argc; const char *line;
switch_status_t status = SWITCH_STATUS_FALSE; switch_memory_pool_t *pool = (switch_memory_pool_t *) obj;
if (string && (mydata = strdup(string))) { while (running) {
if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) >= 2) { int32_t arg = 0;
switch_cache_db_handle_t *db = NULL;
char *sql = NULL;
switch_core_db_handle(&db); if (getppid() == 1) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "We've become an orphan, no more console for us.\n");
break;
}
switch_core_session_ctl(SCSC_CHECK_RUNNING, &arg);
if (!arg) {
break;
}
if (!strcasecmp(argv[0], "stickyadd") && argc == 3) { line = el_gets(el, &count);
sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, sql, 5); if (count > 1) {
switch_safe_free(sql); if (!zstr(line)) {
if (db->type == SCDB_TYPE_CORE_DB) { char *cmd = strdup(line);
sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (1, '%q','%q','%q')", char *p;
argv[1], argv[2], switch_core_get_variable("hostname")); const LineInfo *lf = el_line(el);
} else { char *foo = (char *) lf->buffer;
sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (1, '%w','%w','%w')", if ((p = strrchr(cmd, '\r')) || (p = strrchr(cmd, '\n'))) {
argv[1], argv[2], switch_core_get_variable("hostname")); *p = '\0';
}
switch_cache_db_persistant_execute(db, sql, 5);
status = SWITCH_STATUS_SUCCESS;
} else if (!strcasecmp(argv[0], "add") && argc == 3) {
sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, sql, 5);
switch_safe_free(sql);
if (db->type == SCDB_TYPE_CORE_DB) {
sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (0, '%q','%q','%q')",
argv[1], argv[2], switch_core_get_variable("hostname"));
} else {
sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (0, '%w','%w','%w')",
argv[1], argv[2], switch_core_get_variable("hostname"));
}
switch_cache_db_persistant_execute(db, sql, 5);
status = SWITCH_STATUS_SUCCESS;
} else if (!strcasecmp(argv[0], "del") && argc == 2) {
char *what = argv[1];
if (!strcasecmp(what, "*")) {
sql = switch_mprintf("delete from aliases where hostname='%q'", switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, sql, 1);
} else {
sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, sql, 5);
} }
status = SWITCH_STATUS_SUCCESS; assert(cmd != NULL);
history(myhistory, &ev, H_ENTER, line);
running = switch_console_process(cmd, 0);
el_deletestr(el, strlen(foo) + 1);
memset(foo, 0, strlen(foo));
free(cmd);
} }
switch_safe_free(sql);
switch_cache_db_release_db_handle(&db);
} }
switch_cond_next();
} }
switch_safe_free(mydata); switch_core_destroy_memory_pool(&pool);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Editline thread exiting\n");
return NULL;
}
return status; static unsigned char complete(EditLine * el, int ch)
{
const LineInfo *lf = el_line(el);
return switch_console_complete(lf->buffer, lf->cursor, switch_core_get_console(), NULL);
} }
SWITCH_DECLARE(void) switch_console_loop(void) SWITCH_DECLARE(void) switch_console_loop(void)
{ {
switch_thread_t *thread; switch_thread_t *thread;
...@@ -1146,21 +942,13 @@ SWITCH_DECLARE(void) switch_console_loop(void) ...@@ -1146,21 +942,13 @@ SWITCH_DECLARE(void) switch_console_loop(void)
} }
#else #else
SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string)
{
return SWITCH_STATUS_FALSE;
}
SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string)
{
return SWITCH_STATUS_FALSE;
}
SWITCH_DECLARE(void) switch_console_loop(void) SWITCH_DECLARE(void) switch_console_loop(void)
{ {
char cmd[2048] = ""; char cmd[2048] = "";
int32_t activity = 1; int32_t x = 0, activity = 1;
gethostname(hostname, sizeof(hostname)); gethostname(hostname, sizeof(hostname));
while (running) { while (running) {
...@@ -1253,8 +1041,247 @@ SWITCH_DECLARE(void) switch_console_loop(void) ...@@ -1253,8 +1041,247 @@ SWITCH_DECLARE(void) switch_console_loop(void)
#endif #endif
} }
} }
#endif #endif
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 *last_word,
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, last_word, matches);
}
switch_mutex_unlock(globals.func_mutex);
return status;
}
SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string)
{
char *mydata = NULL, *argv[11] = { 0 };
int argc, x;
switch_status_t status = SWITCH_STATUS_FALSE;
if (string && (mydata = strdup(string))) {
if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
switch_cache_db_handle_t *db = NULL;
switch_stream_handle_t mystream = { 0 };
SWITCH_STANDARD_STREAM(mystream);
switch_core_db_handle(&db);
if (!strcasecmp(argv[0], "stickyadd")) {
mystream.write_function(&mystream, "insert into complete values (1,");
for (x = 0; x < 10; x++) {
if (argv[x + 1] && !strcasecmp(argv[x + 1], "_any_")) {
mystream.write_function(&mystream, "%s", "'', ");
} else {
if (db->type == SCDB_TYPE_CORE_DB) {
mystream.write_function(&mystream, "'%q', ", switch_str_nil(argv[x + 1]));
} else {
mystream.write_function(&mystream, "'%w', ", switch_str_nil(argv[x + 1]));
}
}
}
mystream.write_function(&mystream, " '%s')", switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, mystream.data, 5);
status = SWITCH_STATUS_SUCCESS;
} else if (!strcasecmp(argv[0], "add")) {
mystream.write_function(&mystream, "insert into complete values (0,");
for (x = 0; x < 10; x++) {
if (argv[x + 1] && !strcasecmp(argv[x + 1], "_any_")) {
mystream.write_function(&mystream, "%s", "'', ");
} else {
if (db->type == SCDB_TYPE_CORE_DB) {
mystream.write_function(&mystream, "'%q', ", switch_str_nil(argv[x + 1]));
} else {
mystream.write_function(&mystream, "'%w', ", switch_str_nil(argv[x + 1]));
}
}
}
mystream.write_function(&mystream, " '%s')", switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, mystream.data, 5);
status = SWITCH_STATUS_SUCCESS;
} else if (!strcasecmp(argv[0], "del")) {
char *what = argv[1];
if (!strcasecmp(what, "*")) {
switch_cache_db_persistant_execute(db, "delete from complete", 1);
} else {
mystream.write_function(&mystream, "delete from complete where ");
for (x = 0; x < argc - 1; x++) {
if (db->type == SCDB_TYPE_CORE_DB) {
mystream.write_function(&mystream, "a%d = '%q'%q", x + 1, switch_str_nil(argv[x + 1]), x == argc - 2 ? "" : " and ");
} else {
mystream.write_function(&mystream, "a%d = '%w'%w", x + 1, switch_str_nil(argv[x + 1]), x == argc - 2 ? "" : " and ");
}
}
mystream.write_function(&mystream, " and hostname='%s'", switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, mystream.data, 1);
}
status = SWITCH_STATUS_SUCCESS;
}
switch_safe_free(mystream.data);
switch_cache_db_release_db_handle(&db);
}
}
switch_safe_free(mydata);
return status;
}
SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string)
{
char *mydata = NULL, *argv[3] = { 0 };
int argc;
switch_status_t status = SWITCH_STATUS_FALSE;
if (string && (mydata = strdup(string))) {
if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) >= 2) {
switch_cache_db_handle_t *db = NULL;
char *sql = NULL;
switch_core_db_handle(&db);
if (!strcasecmp(argv[0], "stickyadd") && argc == 3) {
sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, sql, 5);
switch_safe_free(sql);
if (db->type == SCDB_TYPE_CORE_DB) {
sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (1, '%q','%q','%q')",
argv[1], argv[2], switch_core_get_variable("hostname"));
} else {
sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (1, '%w','%w','%w')",
argv[1], argv[2], switch_core_get_variable("hostname"));
}
switch_cache_db_persistant_execute(db, sql, 5);
status = SWITCH_STATUS_SUCCESS;
} else if (!strcasecmp(argv[0], "add") && argc == 3) {
sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, sql, 5);
switch_safe_free(sql);
if (db->type == SCDB_TYPE_CORE_DB) {
sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (0, '%q','%q','%q')",
argv[1], argv[2], switch_core_get_variable("hostname"));
} else {
sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (0, '%w','%w','%w')",
argv[1], argv[2], switch_core_get_variable("hostname"));
}
switch_cache_db_persistant_execute(db, sql, 5);
status = SWITCH_STATUS_SUCCESS;
} else if (!strcasecmp(argv[0], "del") && argc == 2) {
char *what = argv[1];
if (!strcasecmp(what, "*")) {
sql = switch_mprintf("delete from aliases where hostname='%q'", switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, sql, 1);
} else {
sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_variable("hostname"));
switch_cache_db_persistant_execute(db, sql, 5);
}
status = SWITCH_STATUS_SUCCESS;
}
switch_safe_free(sql);
switch_cache_db_release_db_handle(&db);
}
}
switch_safe_free(mydata);
return status;
}
/* For Emacs: /* For Emacs:
* Local Variables: * Local Variables:
* mode:c * mode:c
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论