switch_console.h File Reference

Simple Console. More...

#include <switch.h>

Include dependency graph for switch_console.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define SWITCH_CMD_CHUNK_LEN   1024
#define SWITCH_STANDARD_STREAM(s)

Functions

void switch_console_loop (void)
 A simple comand loop that reads input from the terminal.
void switch_console_printf (switch_text_channel_t channel, const char *file, const char *func, int line, const char *fmt,...) PRINTF_FUNCTION(5
 A method akin to printf that allows you to redirect output to a specific console "channel".
void switch_status_t switch_console_stream_raw_write (switch_stream_handle_t *handle, uint8_t *data, switch_size_t datalen)
switch_status_t switch_console_stream_write (switch_stream_handle_t *handle, const char *fmt,...) PRINTF_FUNCTION(2
 A method akin to printf for dealing with api streams.
switch_status_t switch_status_t switch_stream_write_file_contents (switch_stream_handle_t *stream, const char *path)
switch_status_t switch_console_init (switch_memory_pool_t *pool)
switch_status_t switch_console_shutdown (void)
switch_status_t switch_console_add_complete_func (const char *name, switch_console_complete_callback_t cb)
switch_status_t switch_console_del_complete_func (const char *name)
switch_status_t switch_console_run_complete_func (const char *func, const char *line, const char *last_word, switch_console_callback_match_t **matches)
void switch_console_push_match (switch_console_callback_match_t **matches, const char *new_val)
void switch_console_free_matches (switch_console_callback_match_t **matches)
unsigned char switch_console_complete (const char *line, const char *last_word, FILE *console_out, switch_stream_handle_t *stream, switch_xml_t xml)
void switch_console_sort_matches (switch_console_callback_match_t *matches)
void switch_console_save_history (void)
char * switch_console_expand_alias (char *cmd, char *arg)
switch_status_t switch_console_execute (char *xcmd, int rec, switch_stream_handle_t *istream)


Detailed Description

Simple Console.

This module implements a basic console i/o and by basic I mean, um yeah, basic Right now the primary function of this portion of the program is to keep it from exiting.

Definition in file switch_console.h.


Define Documentation

#define SWITCH_CMD_CHUNK_LEN   1024

Definition at line 45 of file switch_console.h.

#define SWITCH_STANDARD_STREAM (  ) 

Value:

memset(&s, 0, sizeof(s)); s.data = malloc(SWITCH_CMD_CHUNK_LEN); \
        switch_assert(s.data);                                                                                          \
        memset(s.data, 0, SWITCH_CMD_CHUNK_LEN);                                                        \
        s.end = s.data;                                                                                                         \
        s.data_size = SWITCH_CMD_CHUNK_LEN;                                                                     \
        s.write_function = switch_console_stream_write;                                         \
        s.raw_write_function = switch_console_stream_raw_write;                         \
        s.alloc_len = SWITCH_CMD_CHUNK_LEN;                                                                     \
        s.alloc_chunk = SWITCH_CMD_CHUNK_LEN

Definition at line 46 of file switch_console.h.

Referenced by api_hook(), audio_bridge_thread(), do_api_on(), API::execute(), API::executeString(), parse_presence_data_cols(), record_callback(), Stream::Stream(), switch_channel_build_param_string(), switch_channel_expand_variables_check(), switch_channel_get_cap_string(), switch_channel_get_flag_string(), switch_console_complete(), switch_console_process(), switch_console_set_complete(), switch_core_init_and_modload(), switch_event_build_param_string(), switch_event_expand_headers_check(), switch_ivr_record_file(), switch_nat_republish(), switch_nat_status(), and switch_say_file_handle_create().


Function Documentation

switch_status_t switch_console_add_complete_func ( const char *  name,
switch_console_complete_callback_t  cb 
)

Definition at line 1617 of file switch_console.c.

References globals, switch_core_hash_insert(), switch_mutex_lock(), and switch_mutex_unlock().

Referenced by switch_console_init().

01618 {
01619         switch_status_t status;
01620 
01621         switch_mutex_lock(globals.func_mutex);
01622         status = switch_core_hash_insert(globals.func_hash, name, (void *) (intptr_t) cb);
01623         switch_mutex_unlock(globals.func_mutex);
01624 
01625         return status;
01626 }

unsigned char switch_console_complete ( const char *  line,
const char *  last_word,
FILE *  console_out,
switch_stream_handle_t stream,
switch_xml_t  xml 
)

Definition at line 663 of file switch_console.c.

References buf, CC_ERROR, CC_REDISPLAY, comp_callback(), switch_stream_handle::data, end_of_p, helper::hits, helper::len, helper::out, SCDB_TYPE_CORE_DB, helper::stream, switch_assert, switch_cache_db_execute_sql_callback(), switch_cache_db_get_type(), SWITCH_CHANNEL_LOG, switch_core_db_handle, switch_core_get_switchname(), SWITCH_LOG_ERROR, switch_log_printf(), switch_mprintf(), switch_safe_free, switch_separate_string(), SWITCH_STANDARD_STREAM, SWITCH_STATUS_SUCCESS, switch_str_nil, helper::words, switch_stream_handle::write_function, helper::xml, and zstr.

00665 {
00666         switch_cache_db_handle_t *db = NULL;
00667         char *sql = NULL;
00668         char *dup = strdup(line);
00669         char *buf = dup;
00670         char *p, *lp = NULL;
00671         char *errmsg = NULL;
00672         struct helper h = { 0 };
00673         unsigned char ret = CC_REDISPLAY;
00674         int pos = 0;
00675         int sc = 0;
00676 
00677 #ifndef SWITCH_HAVE_LIBEDIT
00678 #ifndef _MSC_VER
00679         if (!stream) {
00680                 return CC_ERROR;
00681         }
00682 #endif
00683 #endif
00684 
00685         if (switch_core_db_handle(&db) != SWITCH_STATUS_SUCCESS) {
00686                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Database Error\n");
00687                 return CC_ERROR;
00688         }
00689 
00690         if (!zstr(cursor) && !zstr(line)) {
00691                 pos = (cursor - line);
00692         }
00693 
00694         h.out = console_out;
00695         h.stream = stream;
00696         h.xml = xml;
00697 
00698         if (pos > 0) {
00699                 *(buf + pos) = '\0';
00700         }
00701 
00702         if ((p = strchr(buf, '\r')) || (p = strchr(buf, '\n'))) {
00703                 *p = '\0';
00704         }
00705 
00706         while (*buf == ' ') {
00707                 sc++;
00708                 buf++;
00709         }
00710 
00711         if (!*buf) {
00712 #ifdef SWITCH_HAVE_LIBEDIT
00713                 if (h.out && sc) {
00714                         el_deletestr(el, sc);
00715                 }
00716 #endif
00717         }
00718 
00719         sc = 0;
00720         p = end_of_p(buf);
00721         while (p >= buf && *p == ' ') {
00722                 sc++;
00723                 p--;
00724         }
00725 
00726         if (sc > 1) {
00727 #ifdef SWITCH_HAVE_LIBEDIT
00728                 if (h.out) {
00729                         el_deletestr(el, sc - 1);
00730                 }
00731 #endif
00732                 *(p + 2) = '\0';
00733         }
00734 
00735         for (p = buf; p && *p; p++) {
00736                 if (*p == ' ') {
00737                         lp = p;
00738                         h.words++;
00739                         while (*p == ' ')
00740                                 p++;
00741                         if (!*p)
00742                                 break;
00743                 }
00744         }
00745 
00746         if (lp) {
00747                 buf = lp + 1;
00748         }
00749 
00750         h.len = strlen(buf);
00751 
00752         if (h.out) {
00753                 fprintf(h.out, "\n\n");
00754         }
00755 
00756         if (h.stream) {
00757                 h.stream->write_function(h.stream, "\n\n");
00758         }
00759 
00760 
00761 
00762         if (h.words == 0) {
00763                 sql = switch_mprintf("select distinct name from interfaces where type='api' and name like '%q%%' and hostname='%q' order by name",
00764                                                          buf, switch_core_get_switchname());
00765         }
00766 
00767         if (sql) {
00768                 switch_cache_db_execute_sql_callback(db, sql, comp_callback, &h, &errmsg);
00769 
00770                 if (errmsg) {
00771                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg);
00772                         free(errmsg);
00773                         ret = CC_ERROR;
00774                         goto end;
00775                 }
00776                 free(sql);
00777                 sql = NULL;
00778         }
00779 
00780         if (h.hits != -1) {
00781                 char *dupdup = strdup(dup);
00782                 int x, argc = 0;
00783                 char *argv[10] = { 0 };
00784                 switch_stream_handle_t stream = { 0 };
00785                 SWITCH_STANDARD_STREAM(stream);
00786                 switch_assert(dupdup);
00787 
00788                 argc = switch_separate_string(dupdup, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
00789 
00790                 if (h.words == 0) {
00791                         stream.write_function(&stream, "select distinct a1 from complete where " "a1 not in (select name from interfaces where hostname='%s') %s ",
00792                                                                   switch_core_get_switchname(), argc ? "and" : "");
00793                 } else {
00794                         if (switch_cache_db_get_type(db) == SCDB_TYPE_CORE_DB) {
00795                                 stream.write_function(&stream, "select distinct a%d,'%q','%q' from complete where ", h.words + 1, switch_str_nil(dup), switch_str_nil(lp));
00796                         } else {
00797                                 stream.write_function(&stream, "select distinct a%d,'%q','%w' from complete where ", h.words + 1, switch_str_nil(dup), switch_str_nil(lp));
00798                         }
00799                 }
00800 
00801                 for (x = 0; x < argc && x < 11; x++) {
00802                         if (h.words + 1 > argc) {
00803                                 if (switch_cache_db_get_type(db) == SCDB_TYPE_CORE_DB) {
00804                                         stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d = '%q')%q",
00805                                                                                   x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
00806                                 } else {
00807                                         stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d = '%w')%w",
00808                                                                                   x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
00809                                 }
00810                         } else {
00811                                 if (switch_cache_db_get_type(db) == SCDB_TYPE_CORE_DB) {
00812                                         stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d like '%q%%')%q",
00813                                                                                   x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
00814                                 } else {
00815                                         stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d like '%w%%')%w",
00816                                                                                   x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
00817                                 }
00818                         }
00819                 }
00820 
00821                 stream.write_function(&stream, " and hostname='%s' order by a%d", switch_core_get_switchname(), h.words + 1);
00822                 
00823                 switch_cache_db_execute_sql_callback(db, stream.data, comp_callback, &h, &errmsg);
00824 
00825                 if (errmsg) {
00826                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", (char *) stream.data, errmsg);
00827                         free(errmsg);
00828                         ret = CC_ERROR;
00829                 }
00830 
00831                 switch_safe_free(dupdup);
00832                 switch_safe_free(stream.data);
00833 
00834                 if (ret == CC_ERROR) {
00835                         goto end;
00836                 }
00837         }
00838 
00839         if (h.out) {
00840                 fprintf(h.out, "\n\n");
00841         }
00842 
00843         if (h.stream) {
00844                 h.stream->write_function(h.stream, "\n\n");
00845                 if (h.hits == 1 && !zstr(h.last)) {
00846                         h.stream->write_function(h.stream, "write=%d:%s ", h.len, h.last);
00847                 } else if (h.hits > 1 && !zstr(h.partial)) {
00848                         h.stream->write_function(h.stream, "write=%d:%s", h.len, h.partial);
00849                 }
00850         }
00851 
00852         if (h.xml) {
00853                 switch_xml_t x_write = switch_xml_add_child_d(h.xml, "write", h.xml_off++);
00854                 char buf[32];
00855 
00856                 snprintf(buf, sizeof(buf), "%d", h.len);
00857                 switch_xml_set_attr_d_buf(x_write, "length", buf);
00858 
00859                 if (h.hits == 1 && !zstr(h.last)) {
00860                         switch_xml_set_txt_d(x_write, h.last);
00861                 } else if (h.hits > 1 && !zstr(h.partial)) {
00862                         switch_xml_set_txt_d(x_write, h.partial);
00863                 }
00864         }
00865 #ifdef SWITCH_HAVE_LIBEDIT
00866         if (h.out) {
00867                 if (h.hits == 1 && !zstr(h.last)) {
00868                         el_deletestr(el, h.len);
00869                         el_insertstr(el, h.last);
00870                         el_insertstr(el, " ");
00871                 } else if (h.hits > 1 && !zstr(h.partial)) {
00872                         el_deletestr(el, h.len);
00873                         el_insertstr(el, h.partial);
00874                 }
00875         }
00876 #else
00877 #ifdef _MSC_VER
00878         if (h.out) {
00879                 if (h.hits == 1 && !zstr(h.last)) {
00880                         console_bufferInput(0, h.len, (char *) line, DELETE_REFRESH_OP);
00881                         console_bufferInput(h.last, (int) strlen(h.last), (char *) line, 0);
00882                         console_bufferInput(" ", 1, (char *) line, 0);
00883                 } else if (h.hits > 1 && !zstr(h.partial)) {
00884                         console_bufferInput(0, h.len, (char *) line, DELETE_REFRESH_OP);
00885                         console_bufferInput(h.partial, (int) strlen(h.partial), (char *) line, 0);
00886                 } else {
00887                         console_bufferInput(0, 0, (char *) line, DELETE_REFRESH_OP);
00888                 }
00889         }
00890 #endif
00891 #endif
00892 
00893   end:
00894 
00895         if (h.out) {
00896                 fflush(h.out);
00897         }
00898 
00899         switch_safe_free(sql);
00900         switch_safe_free(dup);
00901 
00902         switch_cache_db_release_db_handle(&db);
00903 
00904         return (ret);
00905 }

switch_status_t switch_console_del_complete_func ( const char *  name  ) 

Definition at line 1628 of file switch_console.c.

References globals, switch_core_hash_insert(), switch_mutex_lock(), and switch_mutex_unlock().

01629 {
01630         switch_status_t status;
01631 
01632         switch_mutex_lock(globals.func_mutex);
01633         status = switch_core_hash_insert(globals.func_hash, name, NULL);
01634         switch_mutex_unlock(globals.func_mutex);
01635 
01636         return status;
01637 }

switch_status_t switch_console_execute ( char *  xcmd,
int  rec,
switch_stream_handle_t istream 
)

Definition at line 341 of file switch_console.c.

References switch_api_execute(), SWITCH_CHANNEL_LOG, switch_console_execute(), switch_console_expand_alias(), SWITCH_LOG_CRIT, switch_log_printf(), switch_safe_free, switch_separate_string_string(), and SWITCH_STATUS_FALSE.

Referenced by switch_console_execute(), switch_console_process(), and switch_core_init_and_modload().

00342 {
00343         char *arg = NULL, *alias = NULL;
00344 
00345         char *delim = ";;";
00346         int argc;
00347         char *argv[128];
00348         int x;
00349         char *dup = strdup(xcmd);
00350         char *cmd;
00351 
00352         switch_status_t status = SWITCH_STATUS_FALSE;
00353 
00354 
00355         if (rec > 100) {
00356                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Too much recursion!\n");
00357                 goto end;
00358         }
00359 
00360         if (!strncasecmp(xcmd, "alias", 5)) {
00361                 argc = 1;
00362                 argv[0] = xcmd;
00363         } else {
00364                 argc = switch_separate_string_string(dup, delim, argv, 128);
00365         }
00366 
00367         for (x = 0; x < argc; x++) {
00368                 cmd = argv[x];
00369                 if ((arg = strchr(cmd, '\r')) != 0 || (arg = strchr(cmd, '\n')) != 0) {
00370                         *arg = '\0';
00371                         arg = NULL;
00372                 }
00373                 if ((arg = strchr(cmd, ' ')) != 0) {
00374                         *arg++ = '\0';
00375                 }
00376 
00377                 if ((alias = switch_console_expand_alias(cmd, arg)) && alias != cmd) {
00378                         istream->write_function(istream, "\nExpand Alias [%s]->[%s]\n\n", cmd, alias);
00379                         status = switch_console_execute(alias, ++rec, istream);
00380                         free(alias);
00381                         continue;
00382                 }
00383 
00384 
00385                 status = switch_api_execute(cmd, arg, NULL, istream);
00386         }
00387 
00388   end:
00389 
00390         switch_safe_free(dup);
00391 
00392         return status;
00393 }

char* switch_console_expand_alias ( char *  cmd,
char *  arg 
)

Definition at line 234 of file switch_console.c.

References alias_callback(), SCDB_TYPE_CORE_DB, SCF_USE_SQL, switch_cache_db_execute_sql_callback(), switch_cache_db_get_type(), switch_cache_db_release_db_handle(), SWITCH_CHANNEL_LOG, switch_core_db_handle, switch_core_flags(), SWITCH_LOG_ERROR, switch_log_printf(), switch_mprintf(), switch_safe_free, and SWITCH_STATUS_SUCCESS.

Referenced by switch_console_execute().

00235 {
00236         char *errmsg = NULL;
00237         char *r = NULL;
00238         char *sql = NULL;
00239         char *exp = NULL;
00240         switch_cache_db_handle_t *db = NULL;
00241         switch_core_flag_t cflags = switch_core_flags();
00242         int full = 0;
00243 
00244         
00245         if (!(cflags & SCF_USE_SQL)) {
00246                 return NULL;
00247         }
00248 
00249         if (switch_core_db_handle(&db) != SWITCH_STATUS_SUCCESS) {
00250                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Database Error\n");
00251                 return NULL;
00252         }
00253 
00254 
00255         if (switch_cache_db_get_type(db) == SCDB_TYPE_CORE_DB) {
00256                 sql = switch_mprintf("select command from aliases where alias='%q'", cmd);
00257         } else {
00258                 sql = switch_mprintf("select command from aliases where alias='%w'", cmd);
00259         }
00260 
00261         switch_cache_db_execute_sql_callback(db, sql, alias_callback, &r, &errmsg);
00262 
00263         if (errmsg) {
00264                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg);
00265                 free(errmsg);
00266         }
00267 
00268         switch_safe_free(sql);
00269 
00270         if (!r) {
00271                 if (switch_cache_db_get_type(db) == SCDB_TYPE_CORE_DB) {
00272                         sql = switch_mprintf("select command from aliases where alias='%q %q'", cmd, arg);
00273                 } else {
00274                         sql = switch_mprintf("select command from aliases where alias='%w %w'", cmd, arg);
00275                 }
00276 
00277                 switch_cache_db_execute_sql_callback(db, sql, alias_callback, &r, &errmsg);
00278 
00279                 if (errmsg) {
00280                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg);
00281                         free(errmsg);
00282                 }
00283                 if (r) {
00284                         full++;
00285                 }
00286         }
00287 
00288         switch_safe_free(sql);
00289 
00290         if (r) {
00291                 if (arg && !full) {
00292                         exp = switch_mprintf("%s %s", r, arg);
00293                         free(r);
00294                 } else {
00295                         exp = r;
00296                 }
00297         } else {
00298                 exp = cmd;
00299         }
00300 
00301         switch_cache_db_release_db_handle(&db);
00302 
00303         return exp;
00304 }

void switch_console_free_matches ( switch_console_callback_match_t **  matches  ) 

Definition at line 1639 of file switch_console.c.

References switch_console_callback_match::dynamic, switch_console_callback_match::head, switch_console_callback_match_node::next, and switch_console_callback_match_node::val.

Referenced by comp_callback().

01640 {
01641         switch_console_callback_match_t *my_match = *matches;
01642         switch_console_callback_match_node_t *m, *cur;
01643 
01644         /* Don't play with matches */
01645         *matches = NULL;
01646 
01647         m = my_match->head;
01648         while (m) {
01649                 cur = m;
01650                 m = m->next;
01651                 free(cur->val);
01652                 free(cur);
01653         }
01654 
01655         if (my_match->dynamic) {
01656                 free(my_match);
01657         }
01658 }

switch_status_t switch_console_init ( switch_memory_pool_t pool  ) 

Definition at line 1602 of file switch_console.c.

References globals, switch_console_add_complete_func(), switch_console_list_available_modules(), switch_console_list_loaded_modules(), switch_console_list_uuid(), switch_core_hash_init, switch_mutex_init(), SWITCH_MUTEX_NESTED, and SWITCH_STATUS_SUCCESS.

Referenced by switch_core_init().

void switch_console_loop ( void   ) 

A simple comand loop that reads input from the terminal.

Definition at line 1494 of file switch_console.c.

References CMD_BUFLEN, console_xml_config(), SCSC_CHECK_RUNNING, SWITCH_CHANNEL_LOG_CLEAN, switch_console_process(), switch_core_get_switchname(), switch_core_session_ctl(), SWITCH_LOG_CONSOLE, and switch_log_printf().

Referenced by fs_console_loop(), and switch_core_runtime_loop().

01495 {
01496         char cmd[CMD_BUFLEN + 1] = "";
01497         int32_t activity = 1;
01498 #ifndef _MSC_VER
01499         int x = 0;
01500 #else
01501         char keys[CMD_BUFLEN];
01502 #endif
01503 
01504         /* Load/Init the config first */
01505         console_xml_config();
01506 
01507 #ifdef _MSC_VER
01508         sprintf(cmd, "\nfreeswitch@%s> ", switch_core_get_switchname());
01509         console_bufferInput(0, 0, cmd, PROMPT_OP);
01510         memset(cmd, 0, sizeof(cmd));
01511 #endif
01512 
01513         while (running) {
01514                 int32_t arg;
01515 #ifdef _MSC_VER
01516                 int read, key;
01517                 HANDLE stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
01518 #else
01519                 fd_set rfds, efds;
01520                 struct timeval tv = { 0, 20000 };
01521 #endif
01522 
01523                 switch_core_session_ctl(SCSC_CHECK_RUNNING, &arg);
01524                 if (!arg) {
01525                         break;
01526                 }
01527 
01528                 if (activity) {
01529                         switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_CONSOLE, "\nfreeswitch@%s> ", switch_core_get_switchname());
01530                 }
01531 #ifdef _MSC_VER
01532                 activity = 0;
01533                 if (console_readConsole(stdinHandle, keys, (int) sizeof(keys), &read, &key)) {
01534                         if (console_bufferInput(keys, read, cmd, key)) {
01535                                 if (!strcmp(cmd, "Empty")) {
01536                                         cmd[0] = 0;
01537                                 }
01538                                 activity = 1;
01539                                 if (cmd[0]) {
01540                                         running = switch_console_process(cmd);
01541                                 }
01542                                 memset(cmd, 0, sizeof(cmd));
01543                         }
01544                 }
01545                 Sleep(20);
01546 #else
01547                 FD_ZERO(&rfds);
01548                 FD_ZERO(&efds);
01549                 FD_SET(fileno(stdin), &rfds);
01550                 FD_SET(fileno(stdin), &efds);
01551                 if ((activity = select(fileno(stdin) + 1, &rfds, NULL, &efds, &tv)) < 0) {
01552                         break;
01553                 }
01554 
01555                 if (FD_ISSET(fileno(stdin), &efds)) {
01556                         continue;
01557                 }
01558 
01559                 if (!FD_ISSET(fileno(stdin), &rfds)) {
01560                         activity = 0;
01561                 }
01562 
01563                 if (activity == 0) {
01564                         fflush(stdout);
01565                         continue;
01566                 }
01567 
01568                 memset(&cmd, 0, sizeof(cmd));
01569                 for (x = 0; x < (sizeof(cmd) - 1); x++) {
01570                         int c = getchar();
01571                         if (c < 0) {
01572                                 int y = read(fileno(stdin), cmd, sizeof(cmd) - 1);
01573                                 cmd[y - 1] = '\0';
01574                                 break;
01575                         }
01576 
01577                         cmd[x] = (char) c;
01578 
01579                         if (cmd[x] == '\n') {
01580                                 cmd[x] = '\0';
01581                                 break;
01582                         }
01583                 }
01584 
01585                 if (cmd[0]) {
01586                         running = switch_console_process(cmd);
01587                 }
01588 #endif
01589         }
01590 }

void switch_console_printf ( switch_text_channel_t  channel,
const char *  file,
const char *  func,
int  line,
const char *  fmt,
  ... 
)

A method akin to printf that allows you to redirect output to a specific console "channel".

void switch_console_push_match ( switch_console_callback_match_t **  matches,
const char *  new_val 
)

Definition at line 1718 of file switch_console.c.

References switch_zmalloc.

Referenced by modulename_callback(), and uuid_callback().

01719 {
01720         switch_console_callback_match_node_t *match;
01721 
01722         if (!*matches) {
01723                 switch_zmalloc(*matches, sizeof(**matches));
01724                 (*matches)->dynamic = 1;
01725         }
01726 
01727         switch_zmalloc(match, sizeof(*match));
01728         match->val = strdup(new_val);
01729 
01730         if ((*matches)->head) {
01731                 (*matches)->end->next = match;
01732         } else {
01733                 (*matches)->head = match;
01734         }
01735 
01736         (*matches)->count++;
01737 
01738         (*matches)->end = match;
01739 }

switch_status_t switch_console_run_complete_func ( const char *  func,
const char *  line,
const char *  last_word,
switch_console_callback_match_t **  matches 
)

Definition at line 1741 of file switch_console.c.

References globals, switch_console_sort_matches(), switch_core_hash_find(), switch_mutex_lock(), switch_mutex_unlock(), SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by comp_callback().

01743 {
01744         switch_console_complete_callback_t cb;
01745         switch_status_t status = SWITCH_STATUS_FALSE;
01746 
01747         switch_mutex_lock(globals.func_mutex);
01748         if ((cb = (switch_console_complete_callback_t) (intptr_t) switch_core_hash_find(globals.func_hash, func))) {
01749                 if ((status = cb(line, last_word, matches)) == SWITCH_STATUS_SUCCESS) {
01750                         switch_console_sort_matches(*matches);
01751                 }
01752         }
01753         switch_mutex_unlock(globals.func_mutex);
01754 
01755         return status;
01756 }

void switch_console_save_history ( void   ) 

Definition at line 936 of file switch_console.c.

References SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, and switch_log_printf().

Referenced by switch_core_session_ctl().

00937 {
00938 #ifdef SWITCH_HAVE_LIBEDIT
00939         history(myhistory, &ev, H_SAVE, hfile);
00940 #else
00941         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "NOT IMPLEMENTED!\n");
00942 #endif
00943 }

switch_status_t switch_console_shutdown ( void   ) 

Definition at line 1612 of file switch_console.c.

References globals, and switch_core_hash_destroy().

Referenced by switch_core_destroy().

01613 {
01614         return switch_core_hash_destroy(&globals.func_hash);
01615 }

void switch_console_sort_matches ( switch_console_callback_match_t matches  ) 

Definition at line 1660 of file switch_console.c.

References switch_console_callback_match_node::next, and switch_assert.

Referenced by switch_console_run_complete_func().

01661 {
01662         switch_console_callback_match_node_t *p = NULL, *sort[4] = { 0 };
01663         int i, j;
01664 
01665         switch_assert(matches);
01666 
01667         if (matches->count < 2) {
01668                 return;
01669         }
01670 
01671         for (i = 1; i < matches->count; i++) {
01672                 sort[0] = NULL;
01673                 sort[1] = matches->head;
01674                 sort[2] = sort[1] ? sort[1]->next : NULL;
01675                 sort[3] = sort[2] ? sort[2]->next : NULL;
01676 
01677                 for (j = 1; j <= (matches->count - i); j++) {
01678                         switch_assert(sort[1] && sort[2]);
01679                         if (strcmp(sort[1]->val, sort[2]->val) > 0) {
01680                                 sort[1]->next = sort[3];
01681                                 sort[2]->next = sort[1];
01682 
01683                                 if (sort[0])
01684                                         sort[0]->next = sort[2];
01685                                 if (sort[1] == matches->head)
01686                                         matches->head = sort[2];
01687 
01688 
01689 
01690 
01691                                 sort[0] = sort[2];
01692                                 sort[2] = sort[1]->next;
01693                                 if (sort[3] && sort[3]->next)
01694                                         sort[3] = sort[3]->next;
01695 
01696                         } else {
01697                                 sort[0] = sort[1];
01698                                 sort[1] = sort[2];
01699                                 sort[2] = sort[3];
01700                                 if (sort[3] && sort[3]->next)
01701                                         sort[3] = sort[3]->next;
01702                         }
01703 
01704                 }
01705         }
01706 
01707         p = matches->head;
01708 
01709         for (i = 1; i < matches->count; i++)
01710                 p = p->next;
01711 
01712         if (p) {
01713                 p->next = NULL;
01714                 matches->end = p;
01715         }
01716 }

void switch_status_t switch_console_stream_raw_write ( switch_stream_handle_t handle,
uint8_t *  data,
switch_size_t  datalen 
)

Definition at line 123 of file switch_console.c.

References SWITCH_STATUS_MEMERR, and SWITCH_STATUS_SUCCESS.

00124 {
00125         switch_size_t need = handle->data_len + datalen;
00126 
00127         if (need >= handle->data_size) {
00128                 void *new_data;
00129                 need += handle->alloc_chunk;
00130 
00131                 if (!(new_data = realloc(handle->data, need))) {
00132                         return SWITCH_STATUS_MEMERR;
00133                 }
00134 
00135                 handle->data = new_data;
00136                 handle->data_size = need;
00137         }
00138 
00139         memcpy((uint8_t *) (handle->data) + handle->data_len, data, datalen);
00140         handle->data_len += datalen;
00141         handle->end = (uint8_t *) (handle->data) + handle->data_len;
00142         *(uint8_t *) handle->end = '\0';
00143 
00144         return SWITCH_STATUS_SUCCESS;
00145 }

switch_status_t switch_console_stream_write ( switch_stream_handle_t handle,
const char *  fmt,
  ... 
)

A method akin to printf for dealing with api streams.

switch_status_t switch_status_t switch_stream_write_file_contents ( switch_stream_handle_t stream,
const char *  path 
)

Definition at line 203 of file switch_console.c.

References buf, switch_directories::conf_dir, switch_fd_read_line(), SWITCH_GLOBAL_dirs, switch_is_file_path(), switch_mprintf(), SWITCH_PATH_SEPARATOR, switch_safe_free, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

00204 {
00205         char *dpath = NULL;
00206         int fd;
00207         switch_status_t status = SWITCH_STATUS_FALSE;
00208 
00209         if (!switch_is_file_path(path)) {
00210                 dpath = switch_mprintf("%s%s%s", SWITCH_GLOBAL_dirs.conf_dir, SWITCH_PATH_SEPARATOR, path);
00211                 path = dpath;
00212         }
00213 
00214         if ((fd = open(path, O_RDONLY)) > -1) {
00215                 char buf[2048] = { 0 };
00216                 while (switch_fd_read_line(fd, buf, sizeof(buf))) {
00217                         stream->write_function(stream, "%s", buf);
00218                 }
00219                 close(fd);
00220                 status = SWITCH_STATUS_SUCCESS;
00221         }
00222 
00223         switch_safe_free(dpath);
00224         return status;
00225 }


Generated on Sun May 20 04:00:08 2012 for FreeSWITCH API Documentation by  doxygen 1.4.7