FreeSWITCH API Documentation  1.7.0
Data Structures | Macros | Functions | Variables
switch_console.c File Reference
#include <switch.h>
#include <switch_console.h>
#include <switch_private.h>
+ Include dependency graph for switch_console.c:

Go to the source code of this file.

Data Structures

struct  helper
 
struct  match_helper
 

Macros

#define CMD_BUFLEN   1024
 
#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
 

Functions

static switch_status_t console_xml_config (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,...)
 
switch_status_t switch_stream_write_file_contents (switch_stream_handle_t *stream, const char *path)
 
static int alias_callback (void *pArg, int argc, char **argv, char **columnNames)
 
char * switch_console_expand_alias (char *cmd, char *arg)
 
static int switch_console_process (char *xcmd)
 
switch_status_t switch_console_execute (char *xcmd, int rec, switch_stream_handle_t *istream)
 
void switch_console_printf (switch_text_channel_t channel, const char *file, const char *func, int line, const char *fmt,...)
 
static int comp_callback (void *pArg, int argc, char **argv, char **columnNames)
 
static int modulename_callback (void *pArg, const char *module_name)
 
switch_status_t switch_console_list_available_modules (const char *line, const char *cursor, switch_console_callback_match_t **matches)
 
switch_status_t switch_console_list_loaded_modules (const char *line, const char *cursor, switch_console_callback_match_t **matches)
 
switch_status_t switch_console_list_interfaces (const char *line, const char *cursor, switch_console_callback_match_t **matches)
 
static int uuid_callback (void *pArg, int argc, char **argv, char **columnNames)
 
switch_status_t switch_console_list_uuid (const char *line, const char *cursor, switch_console_callback_match_t **matches)
 
unsigned char switch_console_complete (const char *line, const char *cursor, FILE *console_out, switch_stream_handle_t *stream, switch_xml_t xml)
 
void switch_console_save_history (void)
 
void switch_console_loop (void)
 A simple comand loop that reads input from the terminal. More...
 
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)
 
void switch_console_free_matches (switch_console_callback_match_t **matches)
 
void switch_console_sort_matches (switch_console_callback_match_t *matches)
 
void switch_console_push_match_unique (switch_console_callback_match_t **matches, const char *new_val)
 
void switch_console_push_match (switch_console_callback_match_t **matches, const char *new_val)
 
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_status_t switch_console_set_complete (const char *string)
 
switch_status_t switch_console_set_alias (const char *string)
 

Variables

static char * console_fnkeys [12]
 
static int32_t running = 1
 
struct {
   switch_hash_t *   func_hash
 
   switch_mutex_t *   func_mutex
 
globals
 

Macro Definition Documentation

#define CC_ARGHACK   3

Definition at line 53 of file switch_console.c.

#define CC_CURSOR   5

Definition at line 55 of file switch_console.c.

#define CC_EOF   2

Definition at line 52 of file switch_console.c.

#define CC_ERROR   6

Definition at line 56 of file switch_console.c.

Referenced by switch_console_complete().

#define CC_FATAL   7

Definition at line 57 of file switch_console.c.

#define CC_NEWLINE   1

Definition at line 51 of file switch_console.c.

#define CC_NORM   0

Definition at line 50 of file switch_console.c.

#define CC_REDISPLAY   8

Definition at line 58 of file switch_console.c.

Referenced by switch_console_complete().

#define CC_REFRESH   4

Definition at line 54 of file switch_console.c.

#define CC_REFRESH_BEEP   9

Definition at line 59 of file switch_console.c.

#define CMD_BUFLEN   1024

Definition at line 38 of file switch_console.c.

Referenced by switch_console_loop().

Function Documentation

static int alias_callback ( void *  pArg,
int  argc,
char **  argv,
char **  columnNames 
)
static

Definition at line 233 of file switch_console.c.

Referenced by switch_console_expand_alias().

234 {
235  char **r = (char **) pArg;
236  *r = strdup(argv[0]);
237  return -1;
238 }
static int comp_callback ( void *  pArg,
int  argc,
char **  argv,
char **  columnNames 
)
static

Definition at line 466 of file switch_console.c.

References cur, switch_console_callback_match::head, helper::hits, helper::last, switch_console_callback_match_node::next, helper::out, helper::partial, helper::stream, switch_console_free_matches(), switch_console_run_complete_func(), switch_copy_string(), switch_safe_free, switch_separate_string(), SWITCH_STATUS_SUCCESS, switch_xml_add_child_d, switch_xml_set_txt_d, switch_console_callback_match_node::val, switch_stream_handle::write_function, helper::xml, helper::xml_off, and zstr.

Referenced by switch_console_complete().

467 {
468  struct helper *h = (struct helper *) pArg;
469  char *target = NULL, *str = NULL, *cur = NULL;
470  switch_size_t x, y, i;
471 
472 
473  if (argc > 0)
474  target = argv[0];
475  if (argc > 1)
476  str = argv[1];
477  if (argc > 2)
478  cur = argv[2];
479 
480  if (cur) {
481  while (*cur == ' ')
482  cur++;
483  }
484 
485  if (zstr(cur))
486  cur = NULL;
487  if (zstr(str))
488  str = NULL;
489 
490  if (!target) {
491  return -1;
492  }
493 
494  if (!zstr(target) && *target == ':' && *(target + 1) == ':' && *(target + 2) == '[') {
495  char *p = target + 3, *list = NULL;
496 
497  if (p) {
498  char *s_argv[100] = { 0 };
499  char *r_argv[1] = { 0 }, *r_cols[1] = {0};
500  list = strdup(p);
501 
502  argc = switch_separate_string(list, ':', s_argv, (sizeof(s_argv) / sizeof(s_argv[0])));
503 
504  for (i = 0; (int)i < argc; i++) {
505  if (!cur || !strncmp(s_argv[i], cur, strlen(cur))) {
506  r_argv[0] = s_argv[i];
507  comp_callback(h, 1, r_argv, r_cols);
508  }
509  }
510  switch_safe_free(list);
511  }
512  return 0;
513  }
514 
515  if (!zstr(target) && *target == ':' && *(target + 1) == ':') {
516  char *r_argv[1] = { 0 }, *r_cols[1] = {0};
518  if (switch_console_run_complete_func(target, str, cur, &matches) == SWITCH_STATUS_SUCCESS) {
520  for (m = matches->head; m; m = m->next) {
521  if (!cur || !strncmp(m->val, cur, strlen(cur))) {
522  r_argv[0] = m->val;
523  comp_callback(h, 1, r_argv, r_cols);
524  }
525  }
526  switch_console_free_matches(&matches);
527  }
528  return 0;
529  }
530 
531  if (!zstr(target)) {
532  if (h->out) {
533  fprintf(h->out, "[%20s]\t", target);
534  }
535  if (h->stream) {
536  h->stream->write_function(h->stream, "[%20s]\t", target);
537  }
538  if (h->xml) {
539  switch_xml_set_txt_d(switch_xml_add_child_d(h->xml, "match", h->xml_off++), target);
540  }
541 
542  switch_copy_string(h->last, target, sizeof(h->last));
543  h->hits++;
544  }
545 
546  x = strlen(h->last);
547  y = strlen(h->partial);
548 
549  if (h->hits > 1) {
550  for (i = 0; i < x && i < y; i++) {
551  if (h->last[i] != h->partial[i]) {
552  h->partial[i] = '\0';
553  break;
554  }
555  }
556  } else if (h->hits == 1) {
557  switch_copy_string(h->partial, target, sizeof(h->last));
558  }
559 
560  if (!zstr(target)) {
561 #ifdef _MSC_VER
562  if ((h->hits % 3) == 0) {
563 #else
564  if ((h->hits % 4) == 0) {
565 #endif
566  if (h->out) {
567  fprintf(h->out, "\n");
568  }
569  if (h->stream) {
570  h->stream->write_function(h->stream, "\n");
571  }
572  }
573  }
574 
575  return 0;
576 }
static int comp_callback(void *pArg, int argc, char **argv, char **columnNames)
#define switch_xml_add_child_d(xml, name, off)
wrapper for switch_xml_add_child() that strdup()s name
Definition: switch_xml.h:269
struct switch_console_callback_match_node * head
FILE * out
void switch_console_free_matches(switch_console_callback_match_t **matches)
switch_status_t switch_console_run_complete_func(const char *func, const char *line, const char *last_word, switch_console_callback_match_t **matches)
pack cur
#define zstr(x)
Definition: switch_utils.h:281
unsigned int switch_separate_string(_In_ char *buf, char delim, _Post_count_(return) char **array, unsigned int arraylen)
Separate a string into an array based on a character delimiter.
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
switch_stream_handle_t * stream
uintptr_t switch_size_t
char * switch_copy_string(_Out_z_cap_(dst_size) char *dst, _In_z_ const char *src, _In_ switch_size_t dst_size)
char partial[512]
switch_stream_handle_write_function_t write_function
#define switch_xml_set_txt_d(xml, txt)
wrapper for switch_xml_set_txt() that strdup()s txt \ sets the character content for the given tag an...
Definition: switch_xml.h:283
char last[512]
switch_xml_t xml
struct switch_console_callback_match_node * next
static switch_status_t console_xml_config ( void  )
static

Definition at line 90 of file switch_console.c.

References console_fnkeys, switch_xml::next, SWITCH_CHANNEL_LOG, switch_core_permanent_strdup, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_SUCCESS, SWITCH_STATUS_TERM, switch_xml_attr_soft(), switch_xml_child(), switch_xml_free(), and switch_xml_open_cfg().

Referenced by switch_console_loop().

91 {
92  char *cf = "switch.conf";
93  switch_xml_t cfg, xml, settings, param;
94 
95  /* clear the keybind array */
96  int i;
97 
98  for (i = 0; i < 12; i++) {
99  console_fnkeys[i] = NULL;
100  }
101 
102  if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
103  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf);
104  return SWITCH_STATUS_TERM;
105  }
106 
107  if ((settings = switch_xml_child(cfg, "cli-keybindings"))) {
108  for (param = switch_xml_child(settings, "key"); param; param = param->next) {
109  char *var = (char *) switch_xml_attr_soft(param, "name");
110  char *val = (char *) switch_xml_attr_soft(param, "value");
111  i = atoi(var);
112  if ((i < 1) || (i > 12)) {
113  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Keybind %s is invalid, range is from 1 to 12\n", var);
114  } else {
115  /* Add the command to the fnkey array */
117  }
118  }
119  }
120 
121  switch_xml_free(xml);
122 
123  return SWITCH_STATUS_SUCCESS;
124 }
#define switch_core_permanent_strdup(_todup)
Copy a string using permanent memory allocation.
Definition: switch_core.h:705
void switch_xml_free(_In_opt_ switch_xml_t xml)
frees the memory allocated for an switch_xml structure
const char * switch_xml_attr_soft(_In_ switch_xml_t xml, _In_z_ const char *attr)
returns the value of the requested tag attribute, or "" if not found
#define SWITCH_CHANNEL_LOG
A representation of an XML tree.
Definition: switch_xml.h:76
switch_xml_t next
Definition: switch_xml.h:88
static char * console_fnkeys[12]
switch_xml_t switch_xml_open_cfg(_In_z_ const char *file_path, _Out_ switch_xml_t *node, _In_opt_ switch_event_t *params)
open a config in the core registry
switch_xml_t switch_xml_child(_In_ switch_xml_t xml, _In_z_ const char *name)
returns the first child tag (one level deeper) with the given name or NULL \ if not found ...
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_xml_t xml
static int modulename_callback ( void *  pArg,
const char *  module_name 
)
static

Definition at line 584 of file switch_console.c.

References match_helper::my_matches, and switch_console_push_match().

Referenced by switch_console_list_available_modules(), and switch_console_list_loaded_modules().

585 {
586  struct match_helper *h = (struct match_helper *) pArg;
587 
588  switch_console_push_match(&h->my_matches, module_name);
589  return 0;
590 }
switch_console_callback_match_t * my_matches
void switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val)
switch_status_t switch_console_add_complete_func ( const char *  name,
switch_console_complete_callback_t  cb 
)

Definition at line 1676 of file switch_console.c.

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

Referenced by switch_console_init().

1677 {
1678  switch_status_t status;
1679 
1680  switch_mutex_lock(globals.func_mutex);
1681  status = switch_core_hash_insert(globals.func_hash, name, (void *) (intptr_t) cb);
1682  switch_mutex_unlock(globals.func_mutex);
1683 
1684  return status;
1685 }
static struct @1 globals
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_status_t
Common return values.
#define switch_core_hash_insert(_h, _k, _d)
Definition: switch_core.h:1410
unsigned char switch_console_complete ( const char *  line,
const char *  cursor,
FILE *  console_out,
switch_stream_handle_t stream,
switch_xml_t  xml 
)

Definition at line 699 of file switch_console.c.

References buf, CC_ERROR, CC_REDISPLAY, comp_callback(), switch_stream_handle::data, end_of_p, helper::hits, helper::last, helper::len, helper::out, helper::partial, SCDB_TYPE_CORE_DB, helper::stream, switch_assert, 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_get_hostname(), SWITCH_LOG_ERROR, switch_log_printf(), switch_mprintf(), switch_safe_free, switch_separate_string(), SWITCH_STANDARD_STREAM, SWITCH_STATUS_SUCCESS, switch_str_nil, switch_xml_add_child_d, switch_xml_set_attr_d_buf, switch_xml_set_txt_d, helper::words, switch_stream_handle::write_function, helper::xml, helper::xml_off, and zstr.

701 {
702  switch_cache_db_handle_t *db = NULL;
703  char *sql = NULL;
704  char *dup = strdup(line);
705  char *buf = dup;
706  char *p, *lp = NULL;
707  char *errmsg = NULL;
708  struct helper h = { 0 };
709  unsigned char ret = CC_REDISPLAY;
710  int pos = 0;
711  int sc = 0;
712 
713 #ifndef HAVE_LIBEDIT
714 #ifndef _MSC_VER
715  if (!stream) {
716  return CC_ERROR;
717  }
718 #endif
719 #endif
720 
723  return CC_ERROR;
724  }
725 
726  if (!zstr(cursor) && !zstr(line)) {
727  pos = (int)(cursor - line);
728  }
729 
730  h.out = console_out;
731  h.stream = stream;
732  h.xml = xml;
733 
734  if (pos > 0) {
735  *(buf + pos) = '\0';
736  }
737 
738  if ((p = strchr(buf, '\r')) || (p = strchr(buf, '\n'))) {
739  *p = '\0';
740  }
741 
742  while (*buf == ' ') {
743  sc++;
744  buf++;
745  }
746 
747  if (!*buf) {
748 #ifdef HAVE_LIBEDIT
749  if (h.out && sc) {
750  el_deletestr(el, sc);
751  }
752 #endif
753  }
754 
755  sc = 0;
756  p = end_of_p(buf);
757  while (p >= buf && *p == ' ') {
758  sc++;
759  p--;
760  }
761 
762  if (sc > 1) {
763 #ifdef HAVE_LIBEDIT
764  if (h.out) {
765  el_deletestr(el, sc - 1);
766  }
767 #endif
768  *(p + 2) = '\0';
769  }
770 
771  for (p = buf; p && *p; p++) {
772  if (*p == ' ') {
773  lp = p;
774  h.words++;
775  while (*p == ' ')
776  p++;
777  if (!*p)
778  break;
779  }
780  }
781 
782  if (lp) {
783  buf = lp + 1;
784  }
785 
786  h.len = (int)strlen(buf);
787 
788  if (h.out) {
789  fprintf(h.out, "\n\n");
790  }
791 
792  if (h.stream) {
793  h.stream->write_function(h.stream, "\n\n");
794  }
795 
796 
797 
798  if (h.words == 0) {
799  sql = switch_mprintf("select distinct name from interfaces where type='api' and name like '%q%%' and hostname='%q' order by name",
800  buf, switch_core_get_hostname());
801  }
802 
803  if (sql) {
805 
806  if (errmsg) {
807  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg);
808  free(errmsg);
809  ret = CC_ERROR;
810  goto end;
811  }
812  free(sql);
813  sql = NULL;
814  }
815 
816  if (h.hits != -1) {
817  char *dupdup = strdup(dup);
818  int x, argc = 0;
819  char *argv[10] = { 0 };
820  switch_stream_handle_t stream = { 0 };
821  SWITCH_STANDARD_STREAM(stream);
822  switch_assert(dupdup);
823 
824  argc = switch_separate_string(dupdup, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
825 
826  if (h.words == 0) {
827  stream.write_function(&stream, "select distinct a1 from complete where " "a1 not in (select name from interfaces where hostname='%s') %s ",
828  switch_core_get_hostname(), argc ? "and" : "");
829  } else {
831  stream.write_function(&stream, "select distinct a%d,'%q','%q' from complete where ", h.words + 1, switch_str_nil(dup), switch_str_nil(lp));
832  } else {
833  stream.write_function(&stream, "select distinct a%d,'%q','%w' from complete where ", h.words + 1, switch_str_nil(dup), switch_str_nil(lp));
834  }
835  }
836 
837  for (x = 0; x < argc && x < 11; x++) {
838  if (h.words + 1 > argc) {
840  stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d = '%q')%q",
841  x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
842  } else {
843  stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d = '%w')%w",
844  x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
845  }
846  } else {
848  stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d like '%q%%')%q",
849  x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
850  } else {
851  stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d like '%w%%')%w",
852  x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
853  }
854  }
855  }
856 
857  stream.write_function(&stream, " and hostname='%s' order by a%d", switch_core_get_hostname(), h.words + 1);
858 
859  switch_cache_db_execute_sql_callback(db, stream.data, comp_callback, &h, &errmsg);
860 
861  if (errmsg) {
862  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", (char *) stream.data, errmsg);
863  free(errmsg);
864  ret = CC_ERROR;
865  }
866 
867  switch_safe_free(dupdup);
868  switch_safe_free(stream.data);
869 
870  if (ret == CC_ERROR) {
871  goto end;
872  }
873  }
874 
875  if (h.out) {
876  fprintf(h.out, "\n\n");
877  }
878 
879  if (h.stream) {
880  h.stream->write_function(h.stream, "\n\n");
881  if (h.hits == 1 && !zstr(h.last)) {
882  h.stream->write_function(h.stream, "write=%d:%s ", h.len, h.last);
883  } else if (h.hits > 1 && !zstr(h.partial)) {
884  h.stream->write_function(h.stream, "write=%d:%s", h.len, h.partial);
885  }
886  }
887 
888  if (h.xml) {
889  switch_xml_t x_write = switch_xml_add_child_d(h.xml, "write", h.xml_off++);
890  char buf[32];
891 
892  snprintf(buf, sizeof(buf), "%d", h.len);
893  switch_xml_set_attr_d_buf(x_write, "length", buf);
894 
895  if (h.hits == 1 && !zstr(h.last)) {
896  switch_xml_set_txt_d(x_write, h.last);
897  } else if (h.hits > 1 && !zstr(h.partial)) {
898  switch_xml_set_txt_d(x_write, h.partial);
899  }
900  }
901 #ifdef HAVE_LIBEDIT
902  if (h.out) {
903  if (h.hits == 1 && !zstr(h.last)) {
904  el_deletestr(el, h.len);
905  el_insertstr(el, h.last);
906  el_insertstr(el, " ");
907  } else if (h.hits > 1 && !zstr(h.partial)) {
908  el_deletestr(el, h.len);
909  el_insertstr(el, h.partial);
910  }
911  }
912 #else
913 #ifdef _MSC_VER
914  if (h.out) {
915  if (h.hits == 1 && !zstr(h.last)) {
916  console_bufferInput(0, h.len, (char *) line, DELETE_REFRESH_OP);
917  console_bufferInput(h.last, (int) strlen(h.last), (char *) line, 0);
918  console_bufferInput(" ", 1, (char *) line, 0);
919  } else if (h.hits > 1 && !zstr(h.partial)) {
920  console_bufferInput(0, h.len, (char *) line, DELETE_REFRESH_OP);
921  console_bufferInput(h.partial, (int) strlen(h.partial), (char *) line, 0);
922  } else {
923  console_bufferInput(0, 0, (char *) line, DELETE_REFRESH_OP);
924  }
925  }
926 #endif
927 #endif
928 
929  end:
930 
931  if (h.out) {
932  fflush(h.out);
933  }
934 
935  switch_safe_free(sql);
936  switch_safe_free(dup);
937 
939 
940  return (ret);
941 }
#define CC_ERROR
#define CC_REDISPLAY
#define SWITCH_CHANNEL_LOG
static int comp_callback(void *pArg, int argc, char **argv, char **columnNames)
#define switch_xml_add_child_d(xml, name, off)
wrapper for switch_xml_add_child() that strdup()s name
Definition: switch_xml.h:269
FILE * out
A representation of an XML tree.
Definition: switch_xml.h:76
switch_status_t switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err)
Executes the sql and uses callback for row-by-row processing.
#define end_of_p(_s)
Definition: switch_utils.h:617
#define zstr(x)
Definition: switch_utils.h:281
void switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh)
Returns the handle to the pool, handle is NOT available to other threads until the allocating thread ...
switch_byte_t switch_byte_t * buf
unsigned int switch_separate_string(_In_ char *buf, char delim, _Post_count_(return) char **array, unsigned int arraylen)
Separate a string into an array based on a character delimiter.
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
switch_stream_handle_t * stream
#define SWITCH_STANDARD_STREAM(s)
#define switch_core_db_handle(_a)
Definition: switch_core.h:2606
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:903
#define switch_xml_set_attr_d_buf(xml, name, value)
Definition: switch_xml.h:302
char partial[512]
switch_stream_handle_write_function_t write_function
switch_cache_db_handle_type_t switch_cache_db_get_type(switch_cache_db_handle_t *dbh)
const char * switch_core_get_hostname(void)
Definition: switch_core.c:344
#define switch_xml_set_txt_d(xml, txt)
wrapper for switch_xml_set_txt() that strdup()s txt \ sets the character content for the given tag an...
Definition: switch_xml.h:283
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
char last[512]
switch_xml_t xml
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
switch_status_t switch_console_del_complete_func ( const char *  name)

Definition at line 1687 of file switch_console.c.

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

1688 {
1689  switch_status_t status;
1690 
1691  switch_mutex_lock(globals.func_mutex);
1692  status = switch_core_hash_insert(globals.func_hash, name, NULL);
1693  switch_mutex_unlock(globals.func_mutex);
1694 
1695  return status;
1696 }
static struct @1 globals
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_status_t
Common return values.
#define switch_core_hash_insert(_h, _k, _d)
Definition: switch_core.h:1410
switch_status_t switch_console_execute ( char *  xcmd,
int  rec,
switch_stream_handle_t istream 
)

Definition at line 347 of file switch_console.c.

References switch_api_execute(), SWITCH_CHANNEL_LOG, 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_process(), and switch_core_init_and_modload().

348 {
349  char *arg = NULL, *alias = NULL;
350 
351  char *delim = ";;";
352  int argc;
353  char *argv[128];
354  int x;
355  char *dup = strdup(xcmd);
356  char *cmd;
357 
359 
360 
361  if (rec > 100) {
362  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Too much recursion!\n");
363  goto end;
364  }
365 
366  if (!strncasecmp(xcmd, "alias", 5)) {
367  argc = 1;
368  argv[0] = xcmd;
369  } else {
370  argc = switch_separate_string_string(dup, delim, argv, 128);
371  }
372 
373  for (x = 0; x < argc; x++) {
374  cmd = argv[x];
375  if ((arg = strchr(cmd, '\r')) != 0 || (arg = strchr(cmd, '\n')) != 0) {
376  *arg = '\0';
377  arg = NULL;
378  }
379  if ((arg = strchr(cmd, ' ')) != 0) {
380  *arg++ = '\0';
381  }
382 
383  if ((alias = switch_console_expand_alias(cmd, arg)) && alias != cmd) {
384  istream->write_function(istream, "\nExpand Alias [%s]->[%s]\n\n", cmd, alias);
385  status = switch_console_execute(alias, ++rec, istream);
386  free(alias);
387  continue;
388  }
389 
390 
391  status = switch_api_execute(cmd, arg, NULL, istream);
392  }
393 
394  end:
395 
396  switch_safe_free(dup);
397 
398  return status;
399 }
#define SWITCH_CHANNEL_LOG
switch_status_t switch_console_execute(char *xcmd, int rec, switch_stream_handle_t *istream)
unsigned int switch_separate_string_string(char *buf, char *delim, _Post_count_(return) char **array, unsigned int arraylen)
switch_status_t switch_api_execute(const char *cmd, const char *arg, switch_core_session_t *session, switch_stream_handle_t *stream)
Execute a registered API command.
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
switch_stream_handle_write_function_t write_function
switch_status_t
Common return values.
char * switch_console_expand_alias(char *cmd, char *arg)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
char* switch_console_expand_alias ( char *  cmd,
char *  arg 
)

Definition at line 240 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().

241 {
242  char *errmsg = NULL;
243  char *r = NULL;
244  char *sql = NULL;
245  char *exp = NULL;
246  switch_cache_db_handle_t *db = NULL;
248  int full = 0;
249 
250 
251  if (!(cflags & SCF_USE_SQL)) {
252  return NULL;
253  }
254 
257  return NULL;
258  }
259 
260 
262  sql = switch_mprintf("select command from aliases where alias='%q'", cmd);
263  } else {
264  sql = switch_mprintf("select command from aliases where alias='%w'", cmd);
265  }
266 
268 
269  if (errmsg) {
270  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg);
271  free(errmsg);
272  }
273 
274  switch_safe_free(sql);
275 
276  if (!r) {
278  sql = switch_mprintf("select command from aliases where alias='%q %q'", cmd, arg);
279  } else {
280  sql = switch_mprintf("select command from aliases where alias='%w %w'", cmd, arg);
281  }
282 
284 
285  if (errmsg) {
286  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg);
287  free(errmsg);
288  }
289  if (r) {
290  full++;
291  }
292  }
293 
294  switch_safe_free(sql);
295 
296  if (r) {
297  if (arg && !full) {
298  exp = switch_mprintf("%s %s", r, arg);
299  free(r);
300  } else {
301  exp = r;
302  }
303  } else {
304  exp = cmd;
305  }
306 
308 
309  return exp;
310 }
#define SWITCH_CHANNEL_LOG
uint32_t switch_core_flag_t
Definition: switch_types.h:376
switch_status_t switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err)
Executes the sql and uses callback for row-by-row processing.
void switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh)
Returns the handle to the pool, handle is NOT available to other threads until the allocating thread ...
switch_core_flag_t switch_core_flags(void)
return core flags
Definition: switch_core.c:2852
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
static int alias_callback(void *pArg, int argc, char **argv, char **columnNames)
#define switch_core_db_handle(_a)
Definition: switch_core.h:2606
switch_cache_db_handle_type_t switch_cache_db_get_type(switch_cache_db_handle_t *dbh)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
void switch_console_free_matches ( switch_console_callback_match_t **  matches)

Definition at line 1698 of file switch_console.c.

References cur, 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().

1699 {
1700  switch_console_callback_match_t *my_match = *matches;
1702 
1703  /* Don't play with matches */
1704  *matches = NULL;
1705 
1706  m = my_match->head;
1707  while (m) {
1708  cur = m;
1709  m = m->next;
1710  free(cur->val);
1711  free(cur);
1712  }
1713 
1714  if (my_match->dynamic) {
1715  free(my_match);
1716  }
1717 }
struct switch_console_callback_match_node * head
pack cur
struct switch_console_callback_match_node * next
switch_status_t switch_console_init ( switch_memory_pool_t pool)

Definition at line 1660 of file switch_console.c.

References globals, switch_console_add_complete_func(), switch_console_list_available_modules(), switch_console_list_interfaces(), 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().

1661 {
1663  switch_core_hash_init(&globals.func_hash);
1668  return SWITCH_STATUS_SUCCESS;
1669 }
#define switch_core_hash_init(_hash)
Definition: switch_core.h:1390
switch_status_t switch_console_add_complete_func(const char *name, switch_console_complete_callback_t cb)
switch_status_t switch_console_list_uuid(const char *line, const char *cursor, switch_console_callback_match_t **matches)
switch_status_t(* switch_console_complete_callback_t)(const char *, const char *, switch_console_callback_match_t **matches)
switch_memory_pool_t * pool
static struct @1 globals
switch_status_t switch_console_list_interfaces(const char *line, const char *cursor, switch_console_callback_match_t **matches)
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:270
switch_status_t switch_console_list_available_modules(const char *line, const char *cursor, switch_console_callback_match_t **matches)
switch_status_t switch_console_list_loaded_modules(const char *line, const char *cursor, switch_console_callback_match_t **matches)
switch_status_t switch_console_list_available_modules ( const char *  line,
const char *  cursor,
switch_console_callback_match_t **  matches 
)

Definition at line 592 of file switch_console.c.

References switch_directories::mod_dir, modulename_callback(), match_helper::my_matches, SWITCH_GLOBAL_dirs, switch_loadable_module_enumerate_available(), SWITCH_STATUS_FALSE, SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

Referenced by switch_console_init().

593 {
594  struct match_helper h = { 0 };
595 
597  return SWITCH_STATUS_GENERR;
598  }
599 
600  if (h.my_matches) {
601  *matches = h.my_matches;
602  return SWITCH_STATUS_SUCCESS;
603  }
604 
605  return SWITCH_STATUS_FALSE;
606 }
switch_console_callback_match_t * my_matches
switch_status_t switch_loadable_module_enumerate_available(const char *dir_path, switch_modulename_callback_func_t callback, void *user_data)
Enumerates a list of all modules discovered in a directory.
static int modulename_callback(void *pArg, const char *module_name)
switch_directories SWITCH_GLOBAL_dirs
Definition: switch_core.c:60
switch_status_t switch_console_list_interfaces ( const char *  line,
const char *  cursor,
switch_console_callback_match_t **  matches 
)

Definition at line 648 of file switch_console.c.

References SWITCH_STATUS_FALSE.

Referenced by switch_console_init().

649 {
650  return SWITCH_STATUS_FALSE;
651 }
switch_status_t switch_console_list_loaded_modules ( const char *  line,
const char *  cursor,
switch_console_callback_match_t **  matches 
)

Definition at line 608 of file switch_console.c.

References modulename_callback(), match_helper::my_matches, switch_loadable_module_enumerate_loaded(), SWITCH_STATUS_FALSE, SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

Referenced by switch_console_init().

609 {
610  struct match_helper h = { 0 };
611 
613  return SWITCH_STATUS_GENERR;
614  }
615 
616  if (h.my_matches) {
617  *matches = h.my_matches;
618  return SWITCH_STATUS_SUCCESS;
619  }
620 
621  return SWITCH_STATUS_FALSE;
622 }
switch_console_callback_match_t * my_matches
static int modulename_callback(void *pArg, const char *module_name)
switch_status_t switch_loadable_module_enumerate_loaded(switch_modulename_callback_func_t callback, void *user_data)
Enumerates a list of all currently loaded modules.
switch_status_t switch_console_list_uuid ( const char *  line,
const char *  cursor,
switch_console_callback_match_t **  matches 
)

Definition at line 663 of file switch_console.c.

References match_helper::my_matches, switch_cache_db_execute_sql_callback(), switch_cache_db_release_db_handle(), SWITCH_CHANNEL_LOG, switch_core_db_handle, switch_core_get_switchname(), SWITCH_LOG_ERROR, switch_log_printf(), switch_mprintf(), SWITCH_STATUS_FALSE, SWITCH_STATUS_GENERR, SWITCH_STATUS_SUCCESS, uuid_callback(), and zstr.

Referenced by switch_console_init().

664 {
665  char *sql;
666  struct match_helper h = { 0 };
667  switch_cache_db_handle_t *db = NULL;
669  char *errmsg;
670 
671 
674  return SWITCH_STATUS_GENERR;
675  }
676 
677  if (!zstr(cursor)) {
678  sql = switch_mprintf("select distinct uuid from channels where uuid like '%q%%' and hostname='%q' order by uuid",
679  cursor, switch_core_get_switchname());
680  } else {
681  sql = switch_mprintf("select distinct uuid from channels where hostname='%q' order by uuid", switch_core_get_switchname());
682  }
683 
685  free(sql);
686 
688 
689  if (h.my_matches) {
690  *matches = h.my_matches;
691  status = SWITCH_STATUS_SUCCESS;
692  }
693 
694 
695  return status;
696 }
const char * switch_core_get_switchname(void)
Definition: switch_core.c:349
#define SWITCH_CHANNEL_LOG
switch_console_callback_match_t * my_matches
switch_status_t switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh, const char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err)
Executes the sql and uses callback for row-by-row processing.
#define zstr(x)
Definition: switch_utils.h:281
void switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh)
Returns the handle to the pool, handle is NOT available to other threads until the allocating thread ...
static int uuid_callback(void *pArg, int argc, char **argv, char **columnNames)
#define switch_core_db_handle(_a)
Definition: switch_core.h:2606
switch_status_t
Common return values.
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
void switch_console_loop ( void  )

A simple comand loop that reads input from the terminal.

Definition at line 1552 of file switch_console.c.

References CMD_BUFLEN, console_xml_config(), memset(), running, 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().

1553 {
1554  char cmd[CMD_BUFLEN + 1] = "";
1555  int32_t activity = 1;
1556 #ifndef _MSC_VER
1557  int x = 0;
1558 #else
1559  char keys[CMD_BUFLEN];
1560 #endif
1561 
1562  /* Load/Init the config first */
1564 
1565 #ifdef _MSC_VER
1566  sprintf(cmd, "\nfreeswitch@%s> ", switch_core_get_switchname());
1567  console_bufferInput(0, 0, cmd, PROMPT_OP);
1568  memset(cmd, 0, sizeof(cmd));
1569 #endif
1570 
1571  while (running) {
1572  int32_t arg;
1573 #ifdef _MSC_VER
1574  int read, key;
1575  HANDLE stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
1576 #else
1577  fd_set rfds, efds;
1578  struct timeval tv = { 0, 20000 };
1579 #endif
1580 
1582  if (!arg) {
1583  break;
1584  }
1585 
1586  if (activity) {
1588  }
1589 #ifdef _MSC_VER
1590  activity = 0;
1591  if (console_readConsole(stdinHandle, keys, (int) sizeof(keys), &read, &key)) {
1592  if (console_bufferInput(keys, read, cmd, key)) {
1593  if (!strcmp(cmd, "Empty")) {
1594  cmd[0] = 0;
1595  }
1596  activity = 1;
1597  if (cmd[0]) {
1599  }
1600  memset(cmd, 0, sizeof(cmd));
1601  }
1602  }
1603  Sleep(20);
1604 #else
1605  FD_ZERO(&rfds);
1606  FD_ZERO(&efds);
1607  FD_SET(fileno(stdin), &rfds);
1608  FD_SET(fileno(stdin), &efds);
1609  if ((activity = select(fileno(stdin) + 1, &rfds, NULL, &efds, &tv)) < 0) {
1610  break;
1611  }
1612 
1613  if (FD_ISSET(fileno(stdin), &efds)) {
1614  continue;
1615  }
1616 
1617  if (!FD_ISSET(fileno(stdin), &rfds)) {
1618  activity = 0;
1619  }
1620 
1621  if (activity == 0) {
1622  fflush(stdout);
1623  continue;
1624  }
1625 
1626  memset(&cmd, 0, sizeof(cmd));
1627  for (x = 0; x < (sizeof(cmd) - 1); x++) {
1628  int c = getchar();
1629  if (c < 0) {
1630  int y = read(fileno(stdin), cmd, sizeof(cmd) - 1);
1631  cmd[y - 1] = '\0';
1632  break;
1633  }
1634 
1635  cmd[x] = (char) c;
1636 
1637  if (cmd[x] == '\n') {
1638  cmd[x] = '\0';
1639  break;
1640  }
1641  }
1642 
1643  if (cmd[0]) {
1645  }
1646 #endif
1647  }
1648 }
const char * switch_core_get_switchname(void)
Definition: switch_core.c:349
#define SWITCH_CHANNEL_LOG_CLEAN
static int32_t running
static switch_status_t console_xml_config(void)
static int switch_console_process(char *xcmd)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define CMD_BUFLEN
int32_t switch_core_session_ctl(switch_session_ctl_t cmd, void *val)
send a control message to the core
Definition: switch_core.c:2528
memset(buf, 0, buflen)
void switch_console_printf ( switch_text_channel_t  channel,
const char *  file,
const char *  func,
int  line,
const char *  fmt,
  ... 
)

Definition at line 401 of file switch_console.c.

References SWITCH_CHANNEL_ID_EVENT, SWITCH_CHANNEL_ID_LOG, SWITCH_CHANNEL_ID_LOG_CLEAN, switch_core_data_channel(), switch_cut_path(), switch_event_add_header(), switch_event_add_header_string(), switch_event_create, switch_event_fire, SWITCH_EVENT_LOG, switch_event_running(), switch_micro_time_now(), SWITCH_STACK_BOTTOM, SWITCH_STATUS_SUCCESS, switch_strftime_nocheck(), switch_time_exp_lt(), and switch_vasprintf().

402 {
403  char *data = NULL;
404  int ret = 0;
405  va_list ap;
406  FILE *handle = switch_core_data_channel(channel);
407  const char *filep = switch_cut_path(file);
408  char date[80] = "";
409  switch_size_t retsize;
411  switch_event_t *event;
412 
413  va_start(ap, fmt);
414  ret = switch_vasprintf(&data, fmt, ap);
415  va_end(ap);
416 
417  if (ret == -1) {
418  fprintf(stderr, "Memory Error\n");
419  goto done;
420  }
421 
422  if (channel == SWITCH_CHANNEL_ID_LOG_CLEAN) {
423  fprintf(handle, "%s", data);
424  goto done;
425  }
426 
428  switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
429 
430  if (channel == SWITCH_CHANNEL_ID_LOG) {
431  fprintf(handle, "[%d] %s %s:%d %s() %s", (int) getpid(), date, filep, line, func, data);
432  goto done;
433  }
434 
435  if (channel == SWITCH_CHANNEL_ID_EVENT &&
437 
438  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Data", data);
439  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-File", filep);
440  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Function", func);
441  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Line", "%d", line);
442  switch_event_fire(&event);
443  }
444 
445  done:
446  if (data) {
447  free(data);
448  }
449  fflush(handle);
450 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:310
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:412
switch_xml_t switch_status_t switch_event_running(void)
Determine if the event system has been initialized.
Definition: switch_event.c:417
switch_status_t switch_event_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *fmt,...) PRINTF_FUNCTION(4
Add a header to an event.
Representation of an event.
Definition: switch_event.h:80
switch_status_t switch_time_exp_lt(switch_time_exp_t *result, switch_time_t input)
Definition: switch_apr.c:323
switch_status_t switch_strftime_nocheck(char *s, switch_size_t *retsize, switch_size_t max, const char *format, switch_time_exp_t *tm)
Definition: switch_apr.c:179
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
uintptr_t switch_size_t
int switch_vasprintf(_Out_opt_ char **buf, _In_z_ _Printf_format_string_ const char *format, _In_ va_list ap)
FILE * switch_core_data_channel(switch_text_channel_t channel)
Retrieve a FILE stream of a given text channel name.
Definition: switch_core.c:263
#define switch_event_create(event, id)
Create a new event assuming it will not be custom event and therefore hiding the unused parameters...
Definition: switch_event.h:383
const char * switch_cut_path(const char *in)
Create a pointer to the file name in a given file path eliminating the directory name.
static int switch_console_process ( char *  xcmd)
static

Definition at line 313 of file switch_console.c.

References switch_stream_handle::data, switch_assert, switch_console_execute(), switch_core_get_console(), switch_safe_free, SWITCH_STANDARD_STREAM, and SWITCH_STATUS_SUCCESS.

Referenced by switch_console_loop().

314 {
315  switch_stream_handle_t stream = { 0 };
316  switch_status_t status;
317  FILE *handle = switch_core_get_console();
318  int r = 1;
319 
320  SWITCH_STANDARD_STREAM(stream);
321  switch_assert(stream.data);
322 
323  status = switch_console_execute(xcmd, 0, &stream);
324 
325  if (status == SWITCH_STATUS_SUCCESS) {
326  if (handle) {
327  fprintf(handle, "\n%s\n", (char *) stream.data);
328  fflush(handle);
329  }
330  } else {
331  if (!strcasecmp(xcmd, "...") || !strcasecmp(xcmd, "shutdown")) {
332  r = 0;
333  }
334  if (handle) {
335  fprintf(handle, "Unknown Command: %s\n", xcmd);
336  fflush(handle);
337  }
338  }
339 
340  switch_safe_free(stream.data);
341 
342  return r;
343 
344 }
switch_status_t switch_console_execute(char *xcmd, int rec, switch_stream_handle_t *istream)
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
#define SWITCH_STANDARD_STREAM(s)
switch_status_t
Common return values.
FILE * switch_core_get_console(void)
Get the output console.
Definition: switch_core.c:230
#define switch_assert(expr)
void switch_console_push_match ( switch_console_callback_match_t **  matches,
const char *  new_val 
)

Definition at line 1791 of file switch_console.c.

References switch_zmalloc, and switch_console_callback_match_node::val.

Referenced by modulename_callback(), switch_console_push_match_unique(), switch_core_session_findall(), switch_core_session_findall_matching_var(), switch_event_get_custom_events(), and uuid_callback().

1792 {
1794 
1795  if (!*matches) {
1796  switch_zmalloc(*matches, sizeof(**matches));
1797  (*matches)->dynamic = 1;
1798  }
1799 
1800  switch_zmalloc(match, sizeof(*match));
1801  match->val = strdup(new_val);
1802 
1803  if ((*matches)->head) {
1804  (*matches)->end->next = match;
1805  } else {
1806  (*matches)->head = match;
1807  }
1808 
1809  (*matches)->count++;
1810 
1811  (*matches)->end = match;
1812 }
#define switch_zmalloc(ptr, len)
void switch_console_push_match_unique ( switch_console_callback_match_t **  matches,
const char *  new_val 
)

Definition at line 1777 of file switch_console.c.

References switch_console_callback_match_node::next, switch_console_push_match(), and switch_console_callback_match_node::val.

1778 {
1779  /* Ignore the entry if it is already in the list */
1780  if (*matches) {
1782 
1783  for(node = (*matches)->head; node; node = node->next) {
1784  if (!strcasecmp(node->val, new_val)) return;
1785  }
1786  }
1787 
1788  switch_console_push_match(matches, new_val);
1789 }
void switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val)
struct switch_console_callback_match_node * next
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 1814 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().

1816 {
1819 
1820  switch_mutex_lock(globals.func_mutex);
1821  if ((cb = (switch_console_complete_callback_t) (intptr_t) switch_core_hash_find(globals.func_hash, func))) {
1822  if ((status = cb(line, last_word, matches)) == SWITCH_STATUS_SUCCESS) {
1823  switch_console_sort_matches(*matches);
1824  }
1825  }
1826  switch_mutex_unlock(globals.func_mutex);
1827 
1828  return status;
1829 }
void * switch_core_hash_find(_In_ switch_hash_t *hash, _In_z_ const char *key)
Retrieve data from a given hash.
void switch_console_sort_matches(switch_console_callback_match_t *matches)
switch_status_t(* switch_console_complete_callback_t)(const char *, const char *, switch_console_callback_match_t **matches)
static struct @1 globals
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_status_t
Common return values.
void switch_console_save_history ( void  )

Definition at line 972 of file switch_console.c.

References SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, and switch_log_printf().

Referenced by switch_core_session_ctl().

973 {
974 #ifdef HAVE_LIBEDIT
975  history(myhistory, &ev, H_SAVE, hfile);
976 #else
978 #endif
979 }
#define SWITCH_CHANNEL_LOG
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_console_shutdown ( void  )

Definition at line 1671 of file switch_console.c.

References globals, and switch_core_hash_destroy().

Referenced by switch_core_destroy().

1672 {
1673  return switch_core_hash_destroy(&globals.func_hash);
1674 }
switch_status_t switch_core_hash_destroy(_Inout_ switch_hash_t **hash)
Destroy an existing hash table.
static struct @1 globals
void switch_console_sort_matches ( switch_console_callback_match_t matches)

Definition at line 1719 of file switch_console.c.

References if(), switch_console_callback_match_node::next, and switch_assert.

Referenced by switch_console_run_complete_func().

1720 {
1721  switch_console_callback_match_node_t *p = NULL, *sort[4] = { 0 };
1722  int i, j;
1723 
1724  switch_assert(matches);
1725 
1726  if (matches->count < 2) {
1727  return;
1728  }
1729 
1730  for (i = 1; i < matches->count; i++) {
1731  sort[0] = NULL;
1732  sort[1] = matches->head;
1733  sort[2] = sort[1] ? sort[1]->next : NULL;
1734  sort[3] = sort[2] ? sort[2]->next : NULL;
1735 
1736  for (j = 1; j <= (matches->count - i); j++) {
1737  switch_assert(sort[1] && sort[2]);
1738  if (strcmp(sort[1]->val, sort[2]->val) > 0) {
1739  sort[1]->next = sort[3];
1740  sort[2]->next = sort[1];
1741 
1742  if (sort[0])
1743  sort[0]->next = sort[2];
1744  if (sort[1] == matches->head)
1745  matches->head = sort[2];
1746 
1747 
1748 
1749 
1750  sort[0] = sort[2];
1751  sort[2] = sort[1]->next;
1752  if (sort[3] && sort[3]->next)
1753  sort[3] = sort[3]->next;
1754 
1755  } else {
1756  sort[0] = sort[1];
1757  sort[1] = sort[2];
1758  sort[2] = sort[3];
1759  if (sort[3] && sort[3]->next)
1760  sort[3] = sort[3]->next;
1761  }
1762 
1763  }
1764  }
1765 
1766  p = matches->head;
1767 
1768  for (i = 1; i < matches->count; i++)
1769  p = p->next;
1770 
1771  if (p) {
1772  p->next = NULL;
1773  matches->end = p;
1774  }
1775 }
struct switch_console_callback_match_node * head
struct switch_console_callback_match_node * end
if((uint32_t)(unpack->cur-unpack->buf) > unpack->buflen)
#define switch_assert(expr)
struct switch_console_callback_match_node * next
switch_status_t switch_console_stream_raw_write ( switch_stream_handle_t handle,
uint8_t *  data,
switch_size_t  datalen 
)

Definition at line 126 of file switch_console.c.

References SWITCH_STATUS_MEMERR, and SWITCH_STATUS_SUCCESS.

127 {
128  switch_size_t need = handle->data_len + datalen;
129 
130  if (need >= handle->data_size) {
131  void *new_data;
132  need += handle->alloc_chunk;
133 
134  if (!(new_data = realloc(handle->data, need))) {
135  return SWITCH_STATUS_MEMERR;
136  }
137 
138  handle->data = new_data;
139  handle->data_size = need;
140  }
141 
142  memcpy((uint8_t *) (handle->data) + handle->data_len, data, datalen);
143  handle->data_len += datalen;
144  handle->end = (uint8_t *) (handle->data) + handle->data_len;
145  *(uint8_t *) handle->end = '\0';
146 
147  return SWITCH_STATUS_SUCCESS;
148 }
uintptr_t switch_size_t
switch_status_t switch_console_stream_write ( switch_stream_handle_t handle,
const char *  fmt,
  ... 
)

Definition at line 150 of file switch_console.c.

References buf, SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, switch_log_printf(), switch_snprintf(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_vmprintf().

151 {
152  va_list ap;
153  char *buf = handle->data;
154  char *end = handle->end;
155  int ret = 0;
156  char *data = NULL;
157 
158  if (handle->data_len >= handle->data_size) {
159  return SWITCH_STATUS_FALSE;
160  }
161 
162  va_start(ap, fmt);
163  //ret = switch_vasprintf(&data, fmt, ap);
164  if (!(data = switch_vmprintf(fmt, ap))) {
165  ret = -1;
166  }
167  va_end(ap);
168 
169  if (data) {
170  switch_size_t remaining = handle->data_size - handle->data_len;
171  switch_size_t need = strlen(data) + 1;
172 
173  if ((remaining < need) && handle->alloc_len) {
174  switch_size_t new_len;
175  void *new_data;
176 
177  new_len = handle->data_size + need + handle->alloc_chunk;
178  if ((new_data = realloc(handle->data, new_len))) {
179  handle->data_size = handle->alloc_len = new_len;
180  handle->data = new_data;
181  buf = handle->data;
182  remaining = handle->data_size - handle->data_len;
183  handle->end = (uint8_t *) (handle->data) + handle->data_len;
184  end = handle->end;
185  } else {
187  free(data);
188  return SWITCH_STATUS_FALSE;
189  }
190  }
191 
192  if (remaining < need) {
193  ret = -1;
194  } else {
195  ret = 0;
196  switch_snprintf(end, remaining, "%s", data);
197  handle->data_len = strlen(buf);
198  handle->end = (uint8_t *) (handle->data) + handle->data_len;
199  }
200  free(data);
201  }
202 
204 }
#define SWITCH_CHANNEL_LOG
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
switch_byte_t switch_byte_t * buf
uintptr_t switch_size_t
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
char * switch_vmprintf(const char *zFormat, va_list ap)
switch_status_t switch_stream_write_file_contents ( switch_stream_handle_t stream,
const char *  path 
)

Definition at line 206 of file switch_console.c.

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

207 {
208  char *dpath = NULL;
209  FILE *fd = NULL;
211 
212  if (!switch_is_file_path(path)) {
214  path = dpath;
215  }
216 
217  if ((fd = fopen(path, "r"))) {
218  char *line_buf = NULL;
219  switch_size_t llen = 0;
220 
221  while (switch_fp_read_dline(fd, &line_buf, &llen)) {
222  stream->write_function(stream, "%s", line_buf);
223  }
224  fclose(fd);
225  switch_safe_free(line_buf);
226  status = SWITCH_STATUS_SUCCESS;
227  }
228 
229  switch_safe_free(dpath);
230  return status;
231 }
#define SWITCH_PATH_SEPARATOR
Definition: switch_types.h:122
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
uintptr_t switch_size_t
switch_size_t switch_fp_read_dline(FILE *fd, char **buf, switch_size_t *len)
Definition: switch_utils.c:757
switch_directories SWITCH_GLOBAL_dirs
Definition: switch_core.c:60
switch_stream_handle_write_function_t write_function
switch_status_t
Common return values.
static switch_bool_t switch_is_file_path(const char *file)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
static int uuid_callback ( void *  pArg,
int  argc,
char **  argv,
char **  columnNames 
)
static

Definition at line 654 of file switch_console.c.

References match_helper::my_matches, and switch_console_push_match().

Referenced by switch_console_list_uuid().

655 {
656  struct match_helper *h = (struct match_helper *) pArg;
657 
659  return 0;
660 
661 }
switch_console_callback_match_t * my_matches
void switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val)

Variable Documentation

char* console_fnkeys[12]
static

Definition at line 82 of file switch_console.c.

Referenced by console_xml_config().

switch_hash_t* func_hash

Definition at line 1656 of file switch_console.c.

switch_mutex_t* func_mutex

Definition at line 1657 of file switch_console.c.

struct { ... } globals
int32_t running = 1
static