RTS API Documentation  1.10.11
Macros | Functions
switch_console.h File Reference

Simple Console. More...

#include <switch.h>
+ Include dependency graph for switch_console.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define SWITCH_CMD_CHUNK_LEN   1024
 
#define SWITCH_STANDARD_STREAM(s)
 

Functions

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

Detailed Description

Simple Console.

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

Definition in file switch_console.h.

Macro Definition Documentation

◆ SWITCH_CMD_CHUNK_LEN

#define SWITCH_CMD_CHUNK_LEN   1024

Definition at line 45 of file switch_console.h.

◆ SWITCH_STANDARD_STREAM

#define SWITCH_STANDARD_STREAM (   s)
Value:
memset(&s, 0, sizeof(s)); s.data = malloc(SWITCH_CMD_CHUNK_LEN); \
switch_assert(s.data); \
memset(s.data, 0, SWITCH_CMD_CHUNK_LEN); \
s.end = s.data; \
s.data_size = SWITCH_CMD_CHUNK_LEN; \
s.write_function = switch_console_stream_write; \
s.raw_write_function = switch_console_stream_raw_write; \
s.alloc_len = SWITCH_CMD_CHUNK_LEN; \
s.alloc_chunk = SWITCH_CMD_CHUNK_LEN
void switch_status_t switch_console_stream_raw_write(switch_stream_handle_t *handle, uint8_t *data, switch_size_t datalen)
switch_status_t switch_console_stream_write(switch_stream_handle_t *handle, const char *fmt,...) PRINTF_FUNCTION(2
A method akin to printf for dealing with api streams.
memset(buf, 0, buflen)
#define SWITCH_CMD_CHUNK_LEN

Definition at line 46 of file switch_console.h.

Referenced by api_hook(), do_api_on(), API::execute(), API::executeString(), parse_presence_data_cols(), preprocess_exec_set(), record_helper_post_process(), Stream::Stream(), switch_channel_build_param_string(), switch_channel_expand_variables_check(), switch_channel_get_cap_string(), switch_channel_get_flag_string(), switch_channel_set_timestamps(), switch_console_complete(), switch_console_process(), switch_console_set_complete(), switch_core_init_and_modload(), switch_core_perform_file_open(), switch_event_build_param_string(), switch_event_expand_headers_check(), switch_html_strip(), switch_ivr_parse_event(), switch_ivr_record_file_event(), switch_nat_republish(), switch_nat_status(), and switch_say_file_handle_create().

Function Documentation

◆ 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 *  last_word,
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_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,
  ... 
)

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

◆ 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()

void 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,
  ... 
)

A method akin to printf for dealing with api streams.

◆ switch_stream_write_file_contents()

switch_status_t 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,...)