RTS API Documentation  1.10.11
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

◆ CC_ARGHACK

#define CC_ARGHACK   3

Definition at line 53 of file switch_console.c.

◆ CC_CURSOR

#define CC_CURSOR   5

Definition at line 55 of file switch_console.c.

◆ CC_EOF

#define CC_EOF   2

Definition at line 52 of file switch_console.c.

◆ CC_ERROR

#define CC_ERROR   6

Definition at line 56 of file switch_console.c.

Referenced by switch_console_complete().

◆ CC_FATAL

#define CC_FATAL   7

Definition at line 57 of file switch_console.c.

◆ CC_NEWLINE

#define CC_NEWLINE   1

Definition at line 51 of file switch_console.c.

◆ CC_NORM

#define CC_NORM   0

Definition at line 50 of file switch_console.c.

◆ CC_REDISPLAY

#define CC_REDISPLAY   8

Definition at line 58 of file switch_console.c.

Referenced by switch_console_complete().

◆ CC_REFRESH

#define CC_REFRESH   4

Definition at line 54 of file switch_console.c.

◆ CC_REFRESH_BEEP

#define CC_REFRESH_BEEP   9

Definition at line 59 of file switch_console.c.

◆ CMD_BUFLEN

#define CMD_BUFLEN   1024

Definition at line 38 of file switch_console.c.

Referenced by switch_console_loop(), and switch_console_save_history().

Function Documentation

◆ alias_callback()

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

Definition at line 231 of file switch_console.c.

Referenced by switch_console_expand_alias().

232 {
233  char **r = (char **) pArg;
234  *r = strdup(argv[0]);
235  return -1;
236 }

◆ comp_callback()

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

Definition at line 468 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(), target, switch_console_callback_match_node::val, switch_stream_handle::write_function, helper::xml, helper::xml_off, and zstr.

Referenced by switch_console_complete().

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

◆ console_xml_config()

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(), and switch_console_save_history().

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:707
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:79
switch_xml_t next
Definition: switch_xml.h:91
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

◆ modulename_callback()

static int modulename_callback ( void *  pArg,
const char *  module_name 
)
static

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

587 {
588  struct match_helper *h = (struct match_helper *) pArg;
589 
590  switch_console_push_match(&h->my_matches, module_name);
591  return 0;
592 }
switch_console_callback_match_t * my_matches
void switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val)

◆ switch_console_add_complete_func()

switch_status_t switch_console_add_complete_func ( const char *  name,
switch_console_complete_callback_t  cb 
)

Definition at line 1679 of file switch_console.c.

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

Referenced by switch_console_init().

1680 {
1681  switch_status_t status;
1682 
1683  switch_mutex_lock(globals.func_mutex);
1684  status = switch_core_hash_insert(globals.func_hash, name, (void *) (intptr_t) cb);
1685  switch_mutex_unlock(globals.func_mutex);
1686 
1687  return status;
1688 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
static struct @2 globals
switch_status_t
Common return values.
#define switch_core_hash_insert(_h, _k, _d)
Definition: switch_core.h:1479
const char *const name
Definition: switch_cJSON.h:250

◆ switch_console_complete()

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 701 of file switch_console.c.

References buf, CC_ERROR, CC_REDISPLAY, comp_callback(), console_fnkeys, 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_CHANNEL_LOG_CLEAN, switch_console_process(), switch_core_db_handle, switch_core_get_hostname(), SWITCH_LOG_CONSOLE, 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.

Referenced by switch_console_save_history().

703 {
704  switch_cache_db_handle_t *db = NULL;
705  char *sql = NULL;
706  char *dup = strdup(line);
707  char *buf = dup;
708  char *p, *lp = NULL;
709  char *errmsg = NULL;
710  struct helper h = { 0 };
711  unsigned char ret = CC_REDISPLAY;
712  int pos = 0;
713  int sc = 0;
714 
715 #ifndef HAVE_LIBEDIT
716 #ifndef _MSC_VER
717  if (!stream) {
718  return CC_ERROR;
719  }
720 #endif
721 #endif
722 
725  switch_safe_free(dup);
726  return CC_ERROR;
727  }
728 
729  if (!zstr(cursor) && !zstr(line)) {
730  pos = (int)(cursor - line);
731  }
732 
733  h.out = console_out;
734  h.stream = stream;
735  h.xml = xml;
736 
737  if (pos > 0) {
738  *(buf + pos) = '\0';
739  }
740 
741  if ((p = strchr(buf, '\r')) || (p = strchr(buf, '\n'))) {
742  *p = '\0';
743  }
744 
745  while (*buf == ' ') {
746  sc++;
747  buf++;
748  }
749 
750  if (!*buf) {
751 #ifdef HAVE_LIBEDIT
752  if (h.out && sc) {
753  el_deletestr(el, sc);
754  }
755 #endif
756  }
757 
758  sc = 0;
759  p = end_of_p(buf);
760  while (p >= buf && *p == ' ') {
761  sc++;
762  p--;
763  }
764 
765  if (sc > 1) {
766 #ifdef HAVE_LIBEDIT
767  if (h.out) {
768  el_deletestr(el, sc - 1);
769  }
770 #endif
771  *(p + 2) = '\0';
772  }
773 
774  for (p = buf; p && *p; p++) {
775  if (*p == ' ') {
776  lp = p;
777  h.words++;
778  while (*p == ' ')
779  p++;
780  if (!*p)
781  break;
782  }
783  }
784 
785  if (lp) {
786  buf = lp + 1;
787  }
788 
789  h.len = (int)strlen(buf);
790 
791  if (h.out) {
792  fprintf(h.out, "\n\n");
793  }
794 
795  if (h.stream) {
796  h.stream->write_function(h.stream, "\n\n");
797  }
798 
799 
800 
801  if (h.words == 0) {
802  sql = switch_mprintf("select distinct name from interfaces where type='api' and name like '%q%%' and hostname='%q' order by name",
803  buf, switch_core_get_hostname());
804  }
805 
806  if (sql) {
808 
809  if (errmsg) {
810  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg);
811  free(errmsg);
812  ret = CC_ERROR;
813  goto end;
814  }
815  free(sql);
816  sql = NULL;
817  }
818 
819  if (h.hits != -1) {
820  char *dupdup = strdup(dup);
821  int x, argc = 0;
822  char *argv[10] = { 0 };
823  switch_stream_handle_t stream = { 0 };
824  SWITCH_STANDARD_STREAM(stream);
825  switch_assert(dupdup);
826 
827  argc = switch_separate_string(dupdup, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
828 
829  if (h.words == 0) {
830  stream.write_function(&stream, "select distinct a1 from complete where " "a1 not in (select name from interfaces where hostname='%q') %s ",
831  switch_core_get_hostname(), argc ? "and" : "");
832  } else {
834  stream.write_function(&stream, "select distinct a%d,'%q','%q' from complete where ", h.words + 1, switch_str_nil(dup), switch_str_nil(lp));
835  } else {
836  stream.write_function(&stream, "select distinct a%d,'%q','%w' from complete where ", h.words + 1, switch_str_nil(dup), switch_str_nil(lp));
837  }
838  }
839 
840  for (x = 0; x < argc; x++) {
841  if (h.words + 1 > argc) {
843  stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d = '%q')%q",
844  x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
845  } else {
846  stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d = '%w')%w",
847  x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
848  }
849  } else {
851  stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d like '%q%%')%q",
852  x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
853  } else {
854  stream.write_function(&stream, "(a%d like '::%%' or a%d = '' or a%d like '%w%%')%w",
855  x + 1, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? "" : " and ");
856  }
857  }
858  }
859 
860  stream.write_function(&stream, " and hostname='%s' order by a%d", switch_core_get_hostname(), h.words + 1);
861 
862  switch_cache_db_execute_sql_callback(db, stream.data, comp_callback, &h, &errmsg);
863 
864  if (errmsg) {
865  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", (char *) stream.data, errmsg);
866  free(errmsg);
867  ret = CC_ERROR;
868  }
869 
870  switch_safe_free(dupdup);
871  switch_safe_free(stream.data);
872 
873  if (ret == CC_ERROR) {
874  goto end;
875  }
876  }
877 
878  if (h.out) {
879  fprintf(h.out, "\n\n");
880  }
881 
882  if (h.stream) {
883  h.stream->write_function(h.stream, "\n\n");
884  if (h.hits == 1 && !zstr(h.last)) {
885  h.stream->write_function(h.stream, "write=%d:%s ", h.len, h.last);
886  } else if (h.hits > 1 && !zstr(h.partial)) {
887  h.stream->write_function(h.stream, "write=%d:%s", h.len, h.partial);
888  }
889  }
890 
891  if (h.xml) {
892  switch_xml_t x_write = switch_xml_add_child_d(h.xml, "write", h.xml_off++);
893  char buf[32];
894 
895  snprintf(buf, sizeof(buf), "%d", h.len);
896  switch_xml_set_attr_d_buf(x_write, "length", buf);
897 
898  if (h.hits == 1 && !zstr(h.last)) {
899  switch_xml_set_txt_d(x_write, h.last);
900  } else if (h.hits > 1 && !zstr(h.partial)) {
901  switch_xml_set_txt_d(x_write, h.partial);
902  }
903  }
904 #ifdef HAVE_LIBEDIT
905  if (h.out) {
906  if (h.hits == 1 && !zstr(h.last)) {
907  el_deletestr(el, h.len);
908  el_insertstr(el, h.last);
909  el_insertstr(el, " ");
910  } else if (h.hits > 1 && !zstr(h.partial)) {
911  el_deletestr(el, h.len);
912  el_insertstr(el, h.partial);
913  }
914  }
915 #else
916 #ifdef _MSC_VER
917  if (h.out) {
918  if (h.hits == 1 && !zstr(h.last)) {
919  console_bufferInput(0, h.len, (char *) line, DELETE_REFRESH_OP);
920  console_bufferInput(h.last, (int) strlen(h.last), (char *) line, 0);
921  console_bufferInput(" ", 1, (char *) line, 0);
922  } else if (h.hits > 1 && !zstr(h.partial)) {
923  console_bufferInput(0, h.len, (char *) line, DELETE_REFRESH_OP);
924  console_bufferInput(h.partial, (int) strlen(h.partial), (char *) line, 0);
925  } else {
926  console_bufferInput(0, 0, (char *) line, DELETE_REFRESH_OP);
927  }
928  }
929 #endif
930 #endif
931 
932  end:
933 
934  if (h.out) {
935  fflush(h.out);
936  }
937 
938  switch_safe_free(sql);
939  switch_safe_free(dup);
940 
942 
943  return (ret);
944 }
switch_xml_t switch_xml_set_attr_d_buf(switch_xml_t xml, const char *name, const char *value)
Wrapper for switch_xml_set_attr() that strdup()s name/value. Value cannot be NULL.
Definition: switch_xml.c:3085
switch_xml_t switch_xml_add_child_d(_In_ switch_xml_t xml, _In_z_ const char *name, _In_ switch_size_t off)
wrapper for switch_xml_add_child() that strdup()s name
#define CC_ERROR
#define CC_REDISPLAY
#define SWITCH_CHANNEL_LOG
static int comp_callback(void *pArg, int argc, char **argv, char **columnNames)
FILE * out
A representation of an XML tree.
Definition: switch_xml.h:79
switch_xml_t switch_xml_set_txt_d(switch_xml_t xml, const char *txt)
wrapper for switch_xml_set_txt() that strdup()s txt \ sets the character content for the given tag an...
Definition: switch_xml.c:3005
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:686
#define zstr(x)
Definition: switch_utils.h:314
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:885
switch_stream_handle_t * stream
#define SWITCH_STANDARD_STREAM(s)
#define switch_core_db_handle(_a)
Definition: switch_core.h:2729
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:993
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:356
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_console_del_complete_func()

switch_status_t switch_console_del_complete_func ( const char *  name)

Definition at line 1690 of file switch_console.c.

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

1691 {
1692  switch_status_t status;
1693 
1694  switch_mutex_lock(globals.func_mutex);
1695  status = switch_core_hash_insert(globals.func_hash, name, NULL);
1696  switch_mutex_unlock(globals.func_mutex);
1697 
1698  return status;
1699 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
static struct @2 globals
switch_status_t
Common return values.
#define switch_core_hash_insert(_h, _k, _d)
Definition: switch_core.h:1479
const char *const name
Definition: switch_cJSON.h:250

◆ switch_console_execute()

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

Definition at line 345 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(), SWITCH_STATUS_FALSE, and switch_stream_handle::write_function.

Referenced by switch_console_process(), and switch_core_init_and_modload().

346 {
347  char *arg = NULL, *alias = NULL;
348 
349  char *delim = ";;";
350  int argc;
351  char *argv[128];
352  int x;
353  char *dup = NULL;
354  char *cmd;
355 
357 
358 
359  if (rec > 100) {
360  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Too much recursion!\n");
361  goto end;
362  }
363 
364  while (*xcmd == ' ') xcmd++;
365 
366  dup = strdup(xcmd);
367 
368  if (!strncasecmp(xcmd, "alias", 5)) {
369  argc = 1;
370  argv[0] = xcmd;
371  } else {
372  argc = switch_separate_string_string(dup, delim, argv, 128);
373  }
374 
375  for (x = 0; x < argc; x++) {
376  cmd = argv[x];
377  if ((arg = strchr(cmd, '\r')) != 0 || (arg = strchr(cmd, '\n')) != 0) {
378  *arg = '\0';
379  arg = NULL;
380  }
381  if ((arg = strchr(cmd, ' ')) != 0) {
382  *arg++ = '\0';
383  }
384 
385  if ((alias = switch_console_expand_alias(cmd, arg)) && alias != cmd) {
386  istream->write_function(istream, "\nExpand Alias [%s]->[%s]\n\n", cmd, alias);
387  status = switch_console_execute(alias, ++rec, istream);
388  free(alias);
389  continue;
390  }
391 
392 
393  status = switch_api_execute(cmd, arg, NULL, istream);
394  }
395 
396  end:
397 
398  switch_safe_free(dup);
399 
400  return status;
401 }
#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:885
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.

◆ switch_console_expand_alias()

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

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

239 {
240  char *errmsg = NULL;
241  char *r = NULL;
242  char *sql = NULL;
243  char *exp = NULL;
244  switch_cache_db_handle_t *db = NULL;
246  int full = 0;
247 
248 
249  if (!(cflags & SCF_USE_SQL)) {
250  return NULL;
251  }
252 
255  return NULL;
256  }
257 
258 
260  sql = switch_mprintf("select command from aliases where alias='%q'", cmd);
261  } else {
262  sql = switch_mprintf("select command from aliases where alias='%w'", cmd);
263  }
264 
266 
267  if (errmsg) {
268  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg);
269  free(errmsg);
270  }
271 
272  switch_safe_free(sql);
273 
274  if (!r) {
276  sql = switch_mprintf("select command from aliases where alias='%q %q'", cmd, arg);
277  } else {
278  sql = switch_mprintf("select command from aliases where alias='%w %w'", cmd, arg);
279  }
280 
282 
283  if (errmsg) {
284  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg);
285  free(errmsg);
286  }
287  if (r) {
288  full++;
289  }
290  }
291 
292  switch_safe_free(sql);
293 
294  if (r) {
295  if (arg && !full) {
296  exp = switch_mprintf("%s %s", r, arg);
297  free(r);
298  } else {
299  exp = r;
300  }
301  } else {
302  exp = cmd;
303  }
304 
306 
307  return exp;
308 }
#define SWITCH_CHANNEL_LOG
uint32_t switch_core_flag_t
Definition: switch_types.h:395
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:2988
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
static int alias_callback(void *pArg, int argc, char **argv, char **columnNames)
#define switch_core_db_handle(_a)
Definition: switch_core.h:2729
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,...)

◆ switch_console_free_matches()

void switch_console_free_matches ( switch_console_callback_match_t **  matches)

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

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

◆ switch_console_init()

switch_status_t switch_console_init ( switch_memory_pool_t pool)

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

1664 {
1666  switch_core_hash_init(&globals.func_hash);
1671  return SWITCH_STATUS_SUCCESS;
1672 }
#define switch_core_hash_init(_hash)
Definition: switch_core.h:1431
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_memory_pool_t * pool
switch_status_t(* switch_console_complete_callback_t)(const char *, const char *, 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)
#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:293
static struct @2 globals
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_console_list_available_modules()

switch_status_t switch_console_list_available_modules ( const char *  line,
const char *  cursor,
switch_console_callback_match_t **  matches 
)

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

595 {
596  struct match_helper h = { 0 };
597 
599  return SWITCH_STATUS_GENERR;
600  }
601 
602  if (h.my_matches) {
603  *matches = h.my_matches;
604  return SWITCH_STATUS_SUCCESS;
605  }
606 
607  return SWITCH_STATUS_FALSE;
608 }
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:82

◆ switch_console_list_interfaces()

switch_status_t switch_console_list_interfaces ( const char *  line,
const char *  cursor,
switch_console_callback_match_t **  matches 
)

Definition at line 650 of file switch_console.c.

References SWITCH_STATUS_FALSE.

Referenced by switch_console_init(), and switch_console_list_loaded_modules().

651 {
652  return SWITCH_STATUS_FALSE;
653 }

◆ switch_console_list_loaded_modules()

switch_status_t switch_console_list_loaded_modules ( const char *  line,
const char *  cursor,
switch_console_callback_match_t **  matches 
)

Definition at line 610 of file switch_console.c.

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

Referenced by switch_console_init().

611 {
612  struct match_helper h = { 0 };
613 
615  return SWITCH_STATUS_GENERR;
616  }
617 
618  if (h.my_matches) {
619  *matches = h.my_matches;
620  return SWITCH_STATUS_SUCCESS;
621  }
622 
623  return SWITCH_STATUS_FALSE;
624 }
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_console_list_uuid()

switch_status_t switch_console_list_uuid ( const char *  line,
const char *  cursor,
switch_console_callback_match_t **  matches 
)

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

666 {
667  char *sql;
668  struct match_helper h = { 0 };
669  switch_cache_db_handle_t *db = NULL;
671  char *errmsg;
672 
673 
676  return SWITCH_STATUS_GENERR;
677  }
678 
679  if (!zstr(cursor)) {
680  sql = switch_mprintf("select distinct uuid from channels where uuid like '%q%%' and hostname='%q' order by uuid",
681  cursor, switch_core_get_switchname());
682  } else {
683  sql = switch_mprintf("select distinct uuid from channels where hostname='%q' order by uuid", switch_core_get_switchname());
684  }
685 
687  free(sql);
688 
690 
691  if (h.my_matches) {
692  *matches = h.my_matches;
693  status = SWITCH_STATUS_SUCCESS;
694  }
695 
696 
697  return status;
698 }
const char * switch_core_get_switchname(void)
Definition: switch_core.c:361
#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:314
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:2729
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,...)

◆ switch_console_loop()

void switch_console_loop ( void  )

A simple comand loop that reads input from the terminal.

Definition at line 1555 of file switch_console.c.

References CMD_BUFLEN, console_xml_config(), key, 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(), switch_console_save_history(), and switch_core_runtime_loop().

1556 {
1557  char cmd[CMD_BUFLEN + 1] = "";
1558  int32_t activity = 1;
1559 #ifndef _MSC_VER
1560  int x = 0;
1561 #else
1562  char keys[CMD_BUFLEN];
1563 #endif
1564 
1565  /* Load/Init the config first */
1567 
1568 #ifdef _MSC_VER
1569  sprintf(cmd, "\nfreeswitch@%s> ", switch_core_get_switchname());
1570  console_bufferInput(0, 0, cmd, PROMPT_OP);
1571  memset(cmd, 0, sizeof(cmd));
1572 #endif
1573 
1574  while (running) {
1575  int32_t arg;
1576 #ifdef _MSC_VER
1577  int read, key;
1578  HANDLE stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
1579 #else
1580  fd_set rfds, efds;
1581  struct timeval tv = { 0, 20000 };
1582 #endif
1583 
1585  if (!arg) {
1586  break;
1587  }
1588 
1589  if (activity) {
1591  }
1592 #ifdef _MSC_VER
1593  activity = 0;
1594  if (console_readConsole(stdinHandle, keys, (int) sizeof(keys), &read, &key)) {
1595  if (console_bufferInput(keys, read, cmd, key)) {
1596  if (!strcmp(cmd, "Empty")) {
1597  cmd[0] = 0;
1598  }
1599  activity = 1;
1600  if (cmd[0]) {
1602  }
1603  memset(cmd, 0, sizeof(cmd));
1604  }
1605  }
1606  Sleep(20);
1607 #else
1608  FD_ZERO(&rfds);
1609  FD_ZERO(&efds);
1610  FD_SET(fileno(stdin), &rfds);
1611  FD_SET(fileno(stdin), &efds);
1612  if ((activity = select(fileno(stdin) + 1, &rfds, NULL, &efds, &tv)) < 0) {
1613  break;
1614  }
1615 
1616  if (FD_ISSET(fileno(stdin), &efds)) {
1617  continue;
1618  }
1619 
1620  if (!FD_ISSET(fileno(stdin), &rfds)) {
1621  activity = 0;
1622  }
1623 
1624  if (activity == 0) {
1625  fflush(stdout);
1626  continue;
1627  }
1628 
1629  memset(&cmd, 0, sizeof(cmd));
1630  for (x = 0; x < (sizeof(cmd) - 1); x++) {
1631  int c = getchar();
1632  if (c < 0) {
1633  int y = read(fileno(stdin), cmd, sizeof(cmd) - 1);
1634  cmd[y - 1] = '\0';
1635  break;
1636  }
1637 
1638  cmd[x] = (char) c;
1639 
1640  if (cmd[x] == '\n') {
1641  cmd[x] = '\0';
1642  break;
1643  }
1644  }
1645 
1646  if (cmd[0]) {
1648  }
1649 #endif
1650  }
1651 }
const char * switch_core_get_switchname(void)
Definition: switch_core.c:361
#define SWITCH_CHANNEL_LOG_CLEAN
static int32_t running
static switch_status_t console_xml_config(void)
static int switch_console_process(char *xcmd)
char * key
Definition: switch_msrp.c:64
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:2651
memset(buf, 0, buflen)

◆ switch_console_printf()

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

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

404 {
405  char *data = NULL;
406  int ret = 0;
407  va_list ap;
408  FILE *handle = switch_core_data_channel(channel);
409  const char *filep = switch_cut_path(file);
410  char date[80] = "";
411  switch_size_t retsize;
413  switch_event_t *event;
414 
415  va_start(ap, fmt);
416  ret = switch_vasprintf(&data, fmt, ap);
417  va_end(ap);
418 
419  if (ret == -1) {
420  fprintf(stderr, "Memory Error\n");
421  goto done;
422  }
423 
424  if (channel == SWITCH_CHANNEL_ID_LOG_CLEAN) {
425  fprintf(handle, "%s", data);
426  goto done;
427  }
428 
430  switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
431 
432  if (channel == SWITCH_CHANNEL_ID_LOG) {
433  fprintf(handle, "[%d] %s %s:%d %s() %s", (int) getpid(), date, filep, line, func, data);
434  goto done;
435  }
436 
437  if (channel == SWITCH_CHANNEL_ID_EVENT &&
439 
440  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Data", data);
441  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-File", filep);
442  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Function", func);
443  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Line", "%d", line);
444  switch_event_fire(&event);
445  }
446 
447  done:
448  if (data) {
449  free(data);
450  }
451  fflush(handle);
452 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:413
switch_xml_t switch_status_t switch_event_running(void)
Determine if the event system has been initialized.
Definition: switch_event.c:425
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:346
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:202
int cJSON_bool fmt
Definition: switch_cJSON.h:150
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:286
#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:384
const char * switch_cut_path(const char *in)
Create a pointer to the file name in a given file path eliminating the directory name.

◆ switch_console_process()

static int switch_console_process ( char *  xcmd)
static

Definition at line 311 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_complete(), switch_console_loop(), and switch_console_save_history().

312 {
313  switch_stream_handle_t stream = { 0 };
314  switch_status_t status;
315  FILE *handle = switch_core_get_console();
316  int r = 1;
317 
318  SWITCH_STANDARD_STREAM(stream);
319  switch_assert(stream.data);
320 
321  status = switch_console_execute(xcmd, 0, &stream);
322 
323  if (status == SWITCH_STATUS_SUCCESS) {
324  if (handle) {
325  fprintf(handle, "\n%s\n", (char *) stream.data);
326  fflush(handle);
327  }
328  } else {
329  if (!strcasecmp(xcmd, "...") || !strcasecmp(xcmd, "shutdown")) {
330  r = 0;
331  }
332  if (handle) {
333  fprintf(handle, "Unknown Command: %s\n", xcmd);
334  fflush(handle);
335  }
336  }
337 
338  switch_safe_free(stream.data);
339 
340  return r;
341 
342 }
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:885
#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:253
#define switch_assert(expr)

◆ switch_console_push_match()

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

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

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

◆ switch_console_push_match_unique()

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

Definition at line 1780 of file switch_console.c.

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

Referenced by switch_console_list_loaded_modules().

1781 {
1782  /* Ignore the entry if it is already in the list */
1783  if (*matches) {
1785 
1786  for(node = (*matches)->head; node; node = node->next) {
1787  if (!strcasecmp(node->val, new_val)) return;
1788  }
1789  }
1790 
1791  switch_console_push_match(matches, new_val);
1792 }
void switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val)
struct switch_console_callback_match_node * next

◆ switch_console_run_complete_func()

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 1817 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().

1819 {
1822 
1823  switch_mutex_lock(globals.func_mutex);
1824  if ((cb = (switch_console_complete_callback_t) (intptr_t) switch_core_hash_find(globals.func_hash, func))) {
1825  if ((status = cb(line, last_word, matches)) == SWITCH_STATUS_SUCCESS) {
1826  switch_console_sort_matches(*matches);
1827  }
1828  }
1829  switch_mutex_unlock(globals.func_mutex);
1830 
1831  return status;
1832 }
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)
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
static struct @2 globals
switch_status_t
Common return values.

◆ switch_console_save_history()

void switch_console_save_history ( void  )

Definition at line 975 of file switch_console.c.

References buf, CMD_BUFLEN, console_xml_config(), count, FALSE, if(), key, helper::len, switch_directories::log_dir, memset(), pool, running, SCSC_CHECK_RUNNING, SWITCH_CHANNEL_LOG, switch_cond_next(), switch_console_complete(), switch_console_loop(), switch_console_process(), switch_core_destroy_memory_pool, switch_core_get_console(), switch_core_get_switchname(), switch_core_new_memory_pool, switch_core_session_ctl(), SWITCH_DECLARE, SWITCH_GLOBAL_dirs, SWITCH_LOG_CONSOLE, SWITCH_LOG_CRIT, SWITCH_LOG_DEBUG, switch_log_printf(), SWITCH_LOG_WARNING, switch_mprintf(), SWITCH_PATH_SEPARATOR, switch_snprintf(), SWITCH_STATUS_SUCCESS, switch_thread_create(), SWITCH_THREAD_FUNC, SWITCH_THREAD_STACKSIZE, switch_threadattr_create(), switch_threadattr_detach_set(), switch_threadattr_stacksize_set(), switch_yield, thread, TRUE, and zstr.

Referenced by switch_core_session_ctl().

976 {
977 #ifdef HAVE_LIBEDIT
978  history(myhistory, &ev, H_SAVE, hfile);
979 #else
981 #endif
982 }
#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_console_shutdown()

switch_status_t switch_console_shutdown ( void  )

Definition at line 1674 of file switch_console.c.

References globals, and switch_core_hash_destroy().

Referenced by switch_core_destroy().

1675 {
1676  return switch_core_hash_destroy(&globals.func_hash);
1677 }
switch_status_t switch_core_hash_destroy(_Inout_ switch_hash_t **hash)
Destroy an existing hash table.
static struct @2 globals

◆ switch_console_sort_matches()

void switch_console_sort_matches ( switch_console_callback_match_t matches)

Definition at line 1722 of file switch_console.c.

References switch_console_callback_match::count, switch_console_callback_match::end, switch_console_callback_match::head, if(), switch_console_callback_match_node::next, and switch_assert.

Referenced by switch_console_run_complete_func().

1723 {
1724  switch_console_callback_match_node_t *p = NULL, *sort[4] = { 0 };
1725  int i, j;
1726 
1727  switch_assert(matches);
1728 
1729  if (matches->count < 2) {
1730  return;
1731  }
1732 
1733  for (i = 1; i < matches->count; i++) {
1734  sort[0] = NULL;
1735  sort[1] = matches->head;
1736  sort[2] = sort[1] ? sort[1]->next : NULL;
1737  sort[3] = sort[2] ? sort[2]->next : NULL;
1738 
1739  for (j = 1; j <= (matches->count - i); j++) {
1740  switch_assert(sort[1] && sort[2]);
1741  if (strcmp(sort[1]->val, sort[2]->val) > 0) {
1742  sort[1]->next = sort[3];
1743  sort[2]->next = sort[1];
1744 
1745  if (sort[0])
1746  sort[0]->next = sort[2];
1747  if (sort[1] == matches->head)
1748  matches->head = sort[2];
1749 
1750 
1751 
1752 
1753  sort[0] = sort[2];
1754  sort[2] = sort[1]->next;
1755  if (sort[3] && sort[3]->next)
1756  sort[3] = sort[3]->next;
1757 
1758  } else {
1759  sort[0] = sort[1];
1760  sort[1] = sort[2];
1761  sort[2] = sort[3];
1762  if (sort[3] && sort[3]->next)
1763  sort[3] = sort[3]->next;
1764  }
1765 
1766  }
1767  }
1768 
1769  p = matches->head;
1770 
1771  for (i = 1; i < matches->count; i++)
1772  p = p->next;
1773 
1774  if (p) {
1775  p->next = NULL;
1776  matches->end = p;
1777  }
1778 }
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_console_stream_raw_write()

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_stream_handle::alloc_chunk, switch_stream_handle::data, switch_stream_handle::data_len, switch_stream_handle::data_size, switch_stream_handle::end, 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_console_stream_write()

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 switch_stream_handle::alloc_chunk, switch_stream_handle::alloc_len, switch_stream_handle::data, switch_stream_handle::data_len, switch_stream_handle::data_size, switch_stream_handle::end, 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 *end = handle->end;
154  int ret = 0;
155  char *data = NULL;
156 
157  if (handle->data_len >= handle->data_size) {
158  return SWITCH_STATUS_FALSE;
159  }
160 
161  va_start(ap, fmt);
162  //ret = switch_vasprintf(&data, fmt, ap);
163  if (!(data = switch_vmprintf(fmt, ap))) {
164  ret = -1;
165  }
166  va_end(ap);
167 
168  if (data) {
169  switch_size_t remaining = handle->data_size - handle->data_len;
170  switch_size_t need = strlen(data) + 1;
171 
172  if ((remaining < need) && handle->alloc_len) {
173  switch_size_t new_len;
174  void *new_data;
175 
176  new_len = handle->data_size + need + handle->alloc_chunk;
177  if ((new_data = realloc(handle->data, new_len))) {
178  handle->data_size = handle->alloc_len = new_len;
179  handle->data = new_data;
180  remaining = handle->data_size - handle->data_len;
181  handle->end = (uint8_t *) (handle->data) + handle->data_len;
182  end = handle->end;
183  } else {
185  free(data);
186  return SWITCH_STATUS_FALSE;
187  }
188  }
189 
190  if (remaining < need) {
191  ret = -1;
192  } else {
193  ret = 0;
194  switch_snprintf(end, remaining, "%s", data);
195  handle->data_len += strlen(data);
196  handle->end = (uint8_t *) (handle->data) + handle->data_len;
197  }
198  free(data);
199  }
200 
202 }
#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,...)
int cJSON_bool fmt
Definition: switch_cJSON.h:150
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_stream_write_file_contents()

switch_status_t switch_stream_write_file_contents ( switch_stream_handle_t stream,
const char *  path 
)

Definition at line 204 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, SWITCH_STATUS_SUCCESS, and switch_stream_handle::write_function.

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

◆ uuid_callback()

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

Definition at line 656 of file switch_console.c.

References match_helper::my_matches, and switch_console_push_match().

Referenced by switch_console_list_uuid().

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

Variable Documentation

◆ console_fnkeys

char* console_fnkeys[12]
static

Definition at line 82 of file switch_console.c.

Referenced by console_xml_config(), and switch_console_complete().

◆ func_hash

switch_hash_t* func_hash

Definition at line 1659 of file switch_console.c.

◆ func_mutex

switch_mutex_t* func_mutex

Definition at line 1660 of file switch_console.c.

◆ globals

struct { ... } globals

◆ running

int32_t running = 1
static