FreeSWITCH API Documentation  1.7.0
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

#define SWITCH_CMD_CHUNK_LEN   1024

Definition at line 45 of file switch_console.h.

#define SWITCH_STANDARD_STREAM (   s)
Value:
memset(&s, 0, sizeof(s)); s.data = malloc(SWITCH_CMD_CHUNK_LEN); \
switch_assert(s.data); \
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.
#define switch_assert(expr)
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_callback(), 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_event_build_param_string(), switch_event_expand_headers_check(), switch_ivr_record_file(), switch_nat_republish(), switch_nat_status(), and switch_say_file_handle_create().

Function Documentation

switch_status_t switch_console_add_complete_func ( const char *  name,
switch_console_complete_callback_t  cb 
)

Definition at line 1676 of file switch_console.c.

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

Referenced by switch_console_init().

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

Definition at line 699 of file switch_console.c.

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

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

Definition at line 1687 of file switch_console.c.

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

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

Definition at line 347 of file switch_console.c.

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

Referenced by switch_console_process(), and switch_core_init_and_modload().

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

Definition at line 240 of file switch_console.c.

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

Referenced by switch_console_execute().

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

Definition at line 1698 of file switch_console.c.

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

Referenced by comp_callback().

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

Definition at line 1660 of file switch_console.c.

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

Referenced by switch_core_init().

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

A simple comand loop that reads input from the terminal.

Definition at line 1552 of file switch_console.c.

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

Referenced by fs_console_loop(), and switch_core_runtime_loop().

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

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

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

Definition at line 1791 of file switch_console.c.

References switch_zmalloc, and switch_console_callback_match_node::val.

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

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

Definition at line 1777 of file switch_console.c.

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

1778 {
1779  /* Ignore the entry if it is already in the list */
1780  if (*matches) {
1782 
1783  for(node = (*matches)->head; node; node = node->next) {
1784  if (!strcasecmp(node->val, new_val)) return;
1785  }
1786  }
1787 
1788  switch_console_push_match(matches, new_val);
1789 }
void switch_console_push_match(switch_console_callback_match_t **matches, const char *new_val)
struct switch_console_callback_match_node * next
switch_status_t switch_console_run_complete_func ( const char *  func,
const char *  line,
const char *  last_word,
switch_console_callback_match_t **  matches 
)

Definition at line 1814 of file switch_console.c.

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

Referenced by comp_callback().

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

Definition at line 972 of file switch_console.c.

References SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, and switch_log_printf().

Referenced by switch_core_session_ctl().

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

Definition at line 1671 of file switch_console.c.

References globals, and switch_core_hash_destroy().

Referenced by switch_core_destroy().

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

Definition at line 1719 of file switch_console.c.

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

Referenced by switch_console_run_complete_func().

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

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

A method akin to printf for dealing with api streams.

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

Definition at line 206 of file switch_console.c.

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

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