2008-12-19 19:35:09 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <esl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2008-12-22 13:53:09 -05:00
|
|
|
#ifdef WIN32
|
|
|
|
#define strdup(src) _strdup(src)
|
|
|
|
#define usleep(time) Sleep(time/1000)
|
2008-12-23 17:52:52 -05:00
|
|
|
#define fileno _fileno
|
|
|
|
#define read _read
|
|
|
|
#include <io.h>
|
2008-12-22 13:53:09 -05:00
|
|
|
#else
|
|
|
|
#include <sys/select.h>
|
2008-12-19 19:35:09 -05:00
|
|
|
#include <histedit.h>
|
2008-12-22 13:53:09 -05:00
|
|
|
#define HAVE_EDITLINE
|
|
|
|
#endif
|
2008-12-21 16:54:43 -05:00
|
|
|
|
2008-12-24 10:29:38 -05:00
|
|
|
#include <getopt.h>
|
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
static char prompt_str[512] = "";
|
|
|
|
|
2008-12-22 13:53:09 -05:00
|
|
|
#ifdef HAVE_EDITLINE
|
2008-12-21 16:54:43 -05:00
|
|
|
static char *prompt(EditLine * e)
|
2008-12-19 19:35:09 -05:00
|
|
|
{
|
|
|
|
return prompt_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static EditLine *el;
|
|
|
|
static History *myhistory;
|
|
|
|
static HistEvent ev;
|
2008-12-22 13:53:09 -05:00
|
|
|
#endif
|
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
static int running = 1;
|
|
|
|
static int thread_running = 0;
|
2008-12-20 13:34:34 -05:00
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
static void handle_SIGINT(int sig)
|
|
|
|
{
|
|
|
|
if (sig);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-23 15:25:10 -05:00
|
|
|
|
2008-12-26 12:07:05 -05:00
|
|
|
#ifdef WIN32
|
|
|
|
static HANDLE hStdout;
|
|
|
|
static WORD wOldColorAttrs;
|
|
|
|
static CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
|
|
|
|
|
|
|
static WORD
|
|
|
|
#else
|
|
|
|
static const char*
|
|
|
|
#endif
|
2008-12-23 19:00:45 -05:00
|
|
|
COLORS[] = { ESL_SEQ_DEFAULT_COLOR, ESL_SEQ_FRED, ESL_SEQ_FRED,
|
|
|
|
ESL_SEQ_FRED, ESL_SEQ_FMAGEN, ESL_SEQ_FCYAN, ESL_SEQ_FGREEN, ESL_SEQ_FYELLOW };
|
2008-12-19 19:35:09 -05:00
|
|
|
|
2008-12-26 13:41:59 -05:00
|
|
|
static int usage(char *name){
|
|
|
|
printf("Usage: %s [-H <host>] [-P <port>] [-p <secret>] [-d <level>] [-x command] [profile]\n\n", name);
|
|
|
|
printf(" -?,-h --help Usage Information\n");
|
|
|
|
printf(" -H, --host=hostname Host to connect\n");
|
2008-12-26 13:43:54 -05:00
|
|
|
printf(" -P, --port=port Port to connect (1 - 65535)\n");
|
2008-12-26 13:41:59 -05:00
|
|
|
printf(" -p, --password=FILENAME Password\n");
|
2008-12-26 14:25:36 -05:00
|
|
|
printf(" -x, --execute=command Execute Command and Exit\n");
|
2008-12-26 13:43:54 -05:00
|
|
|
printf(" -d, --debug=level Debug Level (0 - 7)\n\n");
|
2008-12-26 13:41:59 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
static void *msg_thread_run(esl_thread_t *me, void *obj)
|
|
|
|
{
|
|
|
|
|
|
|
|
esl_handle_t *handle = (esl_handle_t *) obj;
|
|
|
|
|
|
|
|
thread_running = 1;
|
|
|
|
|
|
|
|
while(thread_running && handle->connected) {
|
2008-12-20 13:34:34 -05:00
|
|
|
esl_status_t status = esl_recv_timed(handle, 10);
|
2008-12-19 19:35:09 -05:00
|
|
|
|
2008-12-20 13:34:34 -05:00
|
|
|
if (status == ESL_FAIL) {
|
|
|
|
esl_log(ESL_LOG_WARNING, "Disconnected.\n");
|
|
|
|
running = thread_running = 0;
|
|
|
|
} else if (status == ESL_SUCCESS) {
|
2008-12-19 19:35:09 -05:00
|
|
|
if (handle->last_event) {
|
|
|
|
const char *type = esl_event_get_header(handle->last_event, "content-type");
|
2008-12-20 09:56:08 -05:00
|
|
|
int known = 0;
|
|
|
|
|
|
|
|
if (!esl_strlen_zero(type)) {
|
|
|
|
if (!strcasecmp(type, "log/data")) {
|
2008-12-29 19:31:15 -05:00
|
|
|
int level = 0;
|
2008-12-20 09:56:08 -05:00
|
|
|
const char *lname = esl_event_get_header(handle->last_event, "log-level");
|
2008-12-29 19:31:15 -05:00
|
|
|
#ifdef WIN32
|
|
|
|
DWORD len = (DWORD) strlen(handle->last_event->body);
|
|
|
|
DWORD outbytes = 0;
|
|
|
|
#endif
|
2008-12-20 09:56:08 -05:00
|
|
|
if (lname) {
|
|
|
|
level = atoi(lname);
|
|
|
|
}
|
2008-12-29 13:40:40 -05:00
|
|
|
|
2008-12-29 19:31:15 -05:00
|
|
|
|
2008-12-23 19:00:45 -05:00
|
|
|
#ifdef WIN32
|
2008-12-29 19:31:15 -05:00
|
|
|
|
|
|
|
SetConsoleTextAttribute(hStdout, COLORS[level]);
|
|
|
|
WriteFile(hStdout, handle->last_event->body, len, &outbytes, NULL);
|
|
|
|
SetConsoleTextAttribute(hStdout, wOldColorAttrs);
|
2008-12-23 19:00:45 -05:00
|
|
|
#else
|
2008-12-29 19:31:15 -05:00
|
|
|
printf("%s%s%s", COLORS[level], handle->last_event->body, ESL_SEQ_DEFAULT_COLOR);
|
2008-12-23 19:00:45 -05:00
|
|
|
#endif
|
2008-12-29 19:31:15 -05:00
|
|
|
|
2008-12-20 09:56:08 -05:00
|
|
|
known++;
|
|
|
|
} else if (!strcasecmp(type, "text/disconnect-notice")) {
|
|
|
|
running = thread_running = 0;
|
|
|
|
known++;
|
2008-12-19 19:35:09 -05:00
|
|
|
}
|
2008-12-20 09:56:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!known) {
|
2008-12-20 00:24:51 -05:00
|
|
|
printf("INCOMING DATA [%s]\n%s", type, handle->last_event->body);
|
2008-12-19 19:35:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
usleep(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_running = 0;
|
2008-12-20 09:56:08 -05:00
|
|
|
esl_log(ESL_LOG_DEBUG, "Thread Done\n");
|
2008-12-19 19:35:09 -05:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int process_command(esl_handle_t *handle, const char *cmd)
|
|
|
|
{
|
2008-12-20 00:24:51 -05:00
|
|
|
if (
|
|
|
|
!strcasecmp(cmd, "exit") ||
|
|
|
|
!strcasecmp(cmd, "quit") ||
|
|
|
|
!strcasecmp(cmd, "bye")
|
|
|
|
) {
|
2008-12-19 19:35:09 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-20 00:24:51 -05:00
|
|
|
if (
|
|
|
|
!strncasecmp(cmd, "event", 5) ||
|
|
|
|
!strncasecmp(cmd, "noevent", 7) ||
|
|
|
|
!strncasecmp(cmd, "nixevent", 8) ||
|
|
|
|
!strncasecmp(cmd, "log", 3) ||
|
|
|
|
!strncasecmp(cmd, "nolog", 5) ||
|
|
|
|
!strncasecmp(cmd, "filter", 6)
|
|
|
|
) {
|
2008-12-20 13:34:34 -05:00
|
|
|
|
2008-12-26 13:11:31 -05:00
|
|
|
esl_send_recv(handle, cmd);
|
2008-12-26 13:07:36 -05:00
|
|
|
|
2008-12-20 13:34:34 -05:00
|
|
|
printf("%s\n", handle->last_sr_reply);
|
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
goto end;
|
|
|
|
}
|
2008-12-20 00:24:51 -05:00
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
printf("Unknown command [%s]\n", cmd);
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-12-20 00:24:51 -05:00
|
|
|
typedef struct {
|
2008-12-21 16:54:43 -05:00
|
|
|
char name[128];
|
2008-12-20 00:24:51 -05:00
|
|
|
char host[128];
|
|
|
|
esl_port_t port;
|
2008-12-21 16:54:43 -05:00
|
|
|
char pass[128];
|
2008-12-26 15:47:53 -05:00
|
|
|
int debug;
|
2008-12-20 00:24:51 -05:00
|
|
|
} cli_profile_t;
|
|
|
|
|
2008-12-21 16:54:43 -05:00
|
|
|
static cli_profile_t profiles[128] = {{{0}}};
|
2008-12-20 00:24:51 -05:00
|
|
|
static int pcount;
|
|
|
|
|
2008-12-21 16:54:43 -05:00
|
|
|
|
|
|
|
static int get_profile(const char *name, cli_profile_t **profile)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
|
|
|
|
for (x = 0; x < pcount; x++) {
|
|
|
|
if (!strcmp(profiles[x].name, name)) {
|
|
|
|
*profile = &profiles[x];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-23 17:15:39 -05:00
|
|
|
#ifndef HAVE_EDITLINE
|
|
|
|
static char command_buf[2048] = "";
|
|
|
|
|
|
|
|
static const char *basic_gets(int *cnt)
|
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
printf("%s", prompt_str);
|
|
|
|
|
|
|
|
memset(&command_buf, 0, sizeof(command_buf));
|
|
|
|
for (x = 0; x < (sizeof(command_buf) - 1); x++) {
|
|
|
|
int c = getchar();
|
|
|
|
if (c < 0) {
|
|
|
|
int y = read(fileno(stdin), command_buf, sizeof(command_buf) - 1);
|
|
|
|
command_buf[y - 1] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_buf[x] = (char) c;
|
|
|
|
|
|
|
|
if (command_buf[x] == '\n') {
|
|
|
|
command_buf[x] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*cnt = x;
|
|
|
|
|
|
|
|
return command_buf;
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-12-20 00:24:51 -05:00
|
|
|
int main(int argc, char *argv[])
|
2008-12-19 19:35:09 -05:00
|
|
|
{
|
2008-12-21 16:54:43 -05:00
|
|
|
esl_handle_t handle = {{0}};
|
2008-12-22 13:53:09 -05:00
|
|
|
int count = 0;
|
|
|
|
const char *line = NULL;
|
2008-12-19 19:35:09 -05:00
|
|
|
char cmd_str[1024] = "";
|
2008-12-20 00:24:51 -05:00
|
|
|
esl_config_t cfg;
|
|
|
|
cli_profile_t *profile = &profiles[0];
|
2008-12-21 16:54:43 -05:00
|
|
|
int cur = 0;
|
2008-12-23 17:52:52 -05:00
|
|
|
#ifndef WIN32
|
|
|
|
char hfile[512] = "/tmp/fs_cli_history";
|
|
|
|
char cfile[512] = "/tmp/fs_cli_config";
|
|
|
|
char *home = getenv("HOME");
|
|
|
|
#else
|
2008-12-26 15:39:03 -05:00
|
|
|
char hfile[512] = "fs_cli_history";
|
|
|
|
char cfile[512] = "fs_cli_config";
|
|
|
|
char *home = getenv("HOME");
|
2008-12-23 17:52:52 -05:00
|
|
|
#endif
|
2008-12-24 10:29:38 -05:00
|
|
|
/* Vars for optargs */
|
|
|
|
int opt;
|
|
|
|
static struct option options[] = {
|
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{"host", 1, 0, 'H'},
|
|
|
|
{"port", 1, 0, 'P'},
|
|
|
|
{"password", 1, 0, 'p'},
|
2008-12-26 13:14:00 -05:00
|
|
|
{"debug", 1, 0, 'd'},
|
|
|
|
{"execute", 1, 0, 'x'},
|
2008-12-24 10:29:38 -05:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
2008-12-26 12:07:05 -05:00
|
|
|
|
2008-12-26 13:14:00 -05:00
|
|
|
char temp_host[128];
|
|
|
|
int argv_host = 0;
|
|
|
|
char temp_pass[128];
|
|
|
|
int argv_pass = 0 ;
|
2008-12-26 13:41:59 -05:00
|
|
|
int temp_port = 0;
|
2008-12-26 13:14:00 -05:00
|
|
|
int argv_port = 0;
|
2008-12-26 15:47:53 -05:00
|
|
|
int temp_log = -1;
|
2008-12-26 13:41:59 -05:00
|
|
|
int argv_error = 0;
|
2008-12-26 14:24:45 -05:00
|
|
|
int argv_exec = 0;
|
|
|
|
char argv_command[256] = "";
|
|
|
|
|
2008-12-26 13:14:00 -05:00
|
|
|
|
2008-12-26 12:07:05 -05:00
|
|
|
strncpy(profiles[0].host, "127.0.0.1", sizeof(profiles[0].host));
|
|
|
|
strncpy(profiles[0].pass, "ClueCon", sizeof(profiles[0].pass));
|
|
|
|
strncpy(profiles[0].name, "default", sizeof(profiles[0].name));
|
|
|
|
profiles[0].port = 8021;
|
|
|
|
pcount++;
|
2008-12-20 00:24:51 -05:00
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
if (home) {
|
|
|
|
snprintf(hfile, sizeof(hfile), "%s/.fs_cli_history", home);
|
2008-12-20 00:24:51 -05:00
|
|
|
snprintf(cfile, sizeof(cfile), "%s/.fs_cli_config", home);
|
2008-12-19 19:35:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
signal(SIGINT, handle_SIGINT);
|
|
|
|
|
2008-12-20 09:57:31 -05:00
|
|
|
handle.debug = 0;
|
2008-12-29 20:55:59 -05:00
|
|
|
esl_global_set_default_logger(3); /* default debug level to 3 (error) */
|
2008-12-20 00:24:51 -05:00
|
|
|
|
2008-12-26 13:14:00 -05:00
|
|
|
for(;;) {
|
|
|
|
int option_index = 0;
|
|
|
|
opt = getopt_long(argc, argv, "H:U:P:S:p:d:x:h?", options, &option_index);
|
|
|
|
if (opt == -1) break;
|
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 'H':
|
|
|
|
esl_set_string(temp_host, optarg);
|
|
|
|
argv_host = 1;
|
|
|
|
break;
|
|
|
|
case 'P':
|
2008-12-26 13:41:59 -05:00
|
|
|
temp_port= atoi(optarg);
|
|
|
|
if (temp_port > 0 && temp_port < 65536){
|
|
|
|
argv_port = 1;
|
|
|
|
} else {
|
|
|
|
printf("ERROR: Port must be in range 1 - 65535\n");
|
|
|
|
argv_error = 1;
|
|
|
|
}
|
2008-12-26 13:14:00 -05:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
esl_set_string(temp_pass, optarg);
|
|
|
|
argv_pass = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2008-12-26 13:41:59 -05:00
|
|
|
temp_log=atoi(optarg);
|
|
|
|
if (temp_log < 0 || temp_log > 7){
|
|
|
|
printf("ERROR: Debug level should be 0 - 7.\n");
|
|
|
|
argv_error = 1;
|
|
|
|
} else {
|
|
|
|
esl_global_set_default_logger(temp_log);
|
|
|
|
}
|
2008-12-26 13:14:00 -05:00
|
|
|
break;
|
|
|
|
case 'x':
|
2008-12-26 14:24:45 -05:00
|
|
|
argv_exec = 1;
|
|
|
|
esl_set_string(argv_command, optarg);
|
|
|
|
break;
|
2008-12-26 13:14:00 -05:00
|
|
|
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
2008-12-26 13:41:59 -05:00
|
|
|
usage(argv[0]);
|
2008-12-26 13:14:00 -05:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
opt = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-12-26 13:41:59 -05:00
|
|
|
|
2008-12-26 16:24:34 -05:00
|
|
|
if (argv_error){
|
2008-12-26 13:41:59 -05:00
|
|
|
printf("\n");
|
2008-12-26 13:45:49 -05:00
|
|
|
return usage(argv[0]);
|
2008-12-26 13:41:59 -05:00
|
|
|
}
|
2008-12-26 13:14:00 -05:00
|
|
|
|
2008-12-20 00:24:51 -05:00
|
|
|
if (esl_config_open_file(&cfg, cfile)) {
|
|
|
|
char *var, *val;
|
2008-12-21 16:54:43 -05:00
|
|
|
char cur_cat[128] = "";
|
|
|
|
|
2008-12-20 00:24:51 -05:00
|
|
|
while (esl_config_next_pair(&cfg, &var, &val)) {
|
2008-12-21 16:54:43 -05:00
|
|
|
if (strcmp(cur_cat, cfg.category)) {
|
|
|
|
cur++;
|
|
|
|
esl_set_string(cur_cat, cfg.category);
|
|
|
|
esl_set_string(profiles[cur].name, cur_cat);
|
|
|
|
esl_set_string(profiles[cur].host, "localhost");
|
|
|
|
esl_set_string(profiles[cur].pass, "ClueCon");
|
|
|
|
profiles[cur].port = 8021;
|
|
|
|
esl_log(ESL_LOG_INFO, "Found Profile [%s]\n", profiles[cur].name);
|
|
|
|
pcount++;
|
|
|
|
}
|
2008-12-20 00:24:51 -05:00
|
|
|
|
2008-12-21 16:54:43 -05:00
|
|
|
if (!strcasecmp(var, "host")) {
|
|
|
|
esl_set_string(profiles[cur].host, val);
|
|
|
|
} else if (!strcasecmp(var, "password")) {
|
|
|
|
esl_set_string(profiles[cur].pass, val);
|
|
|
|
} else if (!strcasecmp(var, "port")) {
|
|
|
|
int pt = atoi(val);
|
|
|
|
if (pt > 0) {
|
2008-12-22 13:53:09 -05:00
|
|
|
profiles[cur].port = (esl_port_t)pt;
|
2008-12-21 16:54:43 -05:00
|
|
|
}
|
2008-12-26 15:47:53 -05:00
|
|
|
} else if (!strcasecmp(var, "debug")) {
|
|
|
|
int dt = atoi(val);
|
|
|
|
if (dt > -1 && dt < 8){
|
|
|
|
profiles[cur].debug = dt;
|
|
|
|
}
|
|
|
|
}
|
2008-12-20 00:24:51 -05:00
|
|
|
}
|
|
|
|
esl_config_close_file(&cfg);
|
|
|
|
}
|
2008-12-23 15:25:10 -05:00
|
|
|
|
|
|
|
if (optind < argc) {
|
|
|
|
if (get_profile(argv[optind], &profile)) {
|
|
|
|
esl_log(ESL_LOG_INFO, "Chosen profile %s does not exist using builtin default\n", argv[optind]);
|
2008-12-21 16:54:43 -05:00
|
|
|
profile = &profiles[0];
|
|
|
|
} else {
|
|
|
|
esl_log(ESL_LOG_INFO, "Chosen profile %s\n", profile->name);
|
2008-12-26 15:47:53 -05:00
|
|
|
if (temp_log < 0 ) {
|
|
|
|
esl_global_set_default_logger(profile->debug);
|
|
|
|
}
|
2008-12-21 16:54:43 -05:00
|
|
|
}
|
2008-12-19 19:35:09 -05:00
|
|
|
}
|
2008-12-23 15:25:10 -05:00
|
|
|
|
2008-12-26 16:10:40 -05:00
|
|
|
if (argv_host) {
|
|
|
|
esl_set_string(profile->host, temp_host);
|
|
|
|
}
|
|
|
|
if (argv_port) {
|
|
|
|
profile->port = (esl_port_t)temp_port;
|
|
|
|
}
|
|
|
|
if (argv_pass) {
|
|
|
|
esl_set_string(profile->pass, temp_pass);
|
|
|
|
}
|
|
|
|
|
2008-12-26 15:39:03 -05:00
|
|
|
esl_log(ESL_LOG_INFO, "Using profile %s [%s]\n", profile->name, profile->host);
|
2008-12-21 16:54:43 -05:00
|
|
|
|
2008-12-26 15:39:03 -05:00
|
|
|
if (argv_host) {
|
2008-12-26 16:20:58 -05:00
|
|
|
if (argv_port && profile->port != 8021) {
|
|
|
|
snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s:%u@%s> ", profile->host, profile->port, profile->name);
|
|
|
|
} else {
|
|
|
|
snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s@%s> ", profile->host, profile->name);
|
|
|
|
}
|
2008-12-26 15:39:03 -05:00
|
|
|
} else {
|
|
|
|
snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", profile->name);
|
|
|
|
}
|
|
|
|
|
2008-12-21 16:54:43 -05:00
|
|
|
if (esl_connect(&handle, profile->host, profile->port, profile->pass)) {
|
|
|
|
esl_log(ESL_LOG_ERROR, "Error Connecting [%s]\n", handle.err);
|
|
|
|
return -1;
|
2008-12-20 00:24:51 -05:00
|
|
|
}
|
2008-12-26 14:24:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
if (argv_exec){
|
|
|
|
snprintf(cmd_str, sizeof(cmd_str), "api %s\n\n", argv_command);
|
|
|
|
esl_send_recv(&handle, cmd_str);
|
|
|
|
if (handle.last_sr_event && handle.last_sr_event->body) {
|
|
|
|
printf("%s\n", handle.last_sr_event->body);
|
|
|
|
}
|
|
|
|
esl_disconnect(&handle);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
esl_thread_create_detached(msg_thread_run, &handle);
|
2008-12-22 13:53:09 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_EDITLINE
|
2008-12-19 19:35:09 -05:00
|
|
|
el = el_init(__FILE__, stdout, stdout, stdout);
|
|
|
|
el_set(el, EL_PROMPT, &prompt);
|
|
|
|
el_set(el, EL_EDITOR, "emacs");
|
|
|
|
myhistory = history_init();
|
|
|
|
|
|
|
|
if (myhistory == 0) {
|
2008-12-21 16:54:43 -05:00
|
|
|
esl_log(ESL_LOG_ERROR, "history could not be initialized\n");
|
2008-12-19 19:35:09 -05:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
history(myhistory, &ev, H_SETSIZE, 800);
|
|
|
|
el_set(el, EL_HIST, history, myhistory);
|
|
|
|
history(myhistory, &ev, H_LOAD, hfile);
|
2008-12-22 13:53:09 -05:00
|
|
|
#endif
|
2008-12-23 19:00:45 -05:00
|
|
|
#ifdef WIN32
|
2008-12-26 12:07:05 -05:00
|
|
|
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if (hStdout != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) {
|
|
|
|
wOldColorAttrs = csbiInfo.wAttributes;
|
|
|
|
}
|
2008-12-23 19:00:45 -05:00
|
|
|
#endif
|
2008-12-22 13:53:09 -05:00
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
snprintf(cmd_str, sizeof(cmd_str), "log info\n\n");
|
|
|
|
esl_send_recv(&handle, cmd_str);
|
|
|
|
|
2008-12-21 16:54:43 -05:00
|
|
|
esl_log(ESL_LOG_INFO, "FS CLI Ready.\n");
|
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
while (running) {
|
|
|
|
|
2008-12-22 13:53:09 -05:00
|
|
|
#ifdef HAVE_EDITLINE
|
2008-12-19 19:35:09 -05:00
|
|
|
line = el_gets(el, &count);
|
2008-12-23 17:15:39 -05:00
|
|
|
#else
|
|
|
|
line = basic_gets(&count);
|
2008-12-22 13:53:09 -05:00
|
|
|
#endif
|
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
if (count > 1) {
|
|
|
|
if (!esl_strlen_zero(line)) {
|
|
|
|
char *cmd = strdup(line);
|
|
|
|
char *p;
|
2008-12-22 13:53:09 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_EDITLINE
|
2008-12-19 19:35:09 -05:00
|
|
|
const LineInfo *lf = el_line(el);
|
|
|
|
char *foo = (char *) lf->buffer;
|
2008-12-22 13:53:09 -05:00
|
|
|
#endif
|
|
|
|
|
2008-12-19 19:35:09 -05:00
|
|
|
if ((p = strrchr(cmd, '\r')) || (p = strrchr(cmd, '\n'))) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
assert(cmd != NULL);
|
2008-12-22 13:53:09 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_EDITLINE
|
2008-12-19 19:35:09 -05:00
|
|
|
history(myhistory, &ev, H_ENTER, line);
|
2008-12-22 13:53:09 -05:00
|
|
|
#endif
|
2008-12-19 19:35:09 -05:00
|
|
|
|
|
|
|
if (!strncasecmp(cmd, "...", 3)) {
|
|
|
|
goto done;
|
|
|
|
} else if (*cmd == '/') {
|
|
|
|
if (process_command(&handle, cmd + 1)) {
|
|
|
|
running = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
snprintf(cmd_str, sizeof(cmd_str), "api %s\n\n", cmd);
|
|
|
|
esl_send_recv(&handle, cmd_str);
|
2008-12-26 12:54:30 -05:00
|
|
|
if (handle.last_sr_event && handle.last_sr_event->body) {
|
2008-12-20 13:34:34 -05:00
|
|
|
printf("%s\n", handle.last_sr_event->body);
|
|
|
|
}
|
2008-12-19 19:35:09 -05:00
|
|
|
}
|
|
|
|
|
2008-12-22 13:53:09 -05:00
|
|
|
#ifdef HAVE_EDITLINE
|
2008-12-19 19:35:09 -05:00
|
|
|
el_deletestr(el, strlen(foo) + 1);
|
|
|
|
memset(foo, 0, strlen(foo));
|
2008-12-22 13:53:09 -05:00
|
|
|
#endif
|
2008-12-19 19:35:09 -05:00
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
usleep(1000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
2008-12-22 13:53:09 -05:00
|
|
|
#ifdef HAVE_EDITLINE
|
2008-12-19 19:35:09 -05:00
|
|
|
history(myhistory, &ev, H_SAVE, hfile);
|
|
|
|
|
|
|
|
/* Clean up our memory */
|
|
|
|
history_end(myhistory);
|
|
|
|
el_end(el);
|
2008-12-22 13:53:09 -05:00
|
|
|
#endif
|
2008-12-19 19:35:09 -05:00
|
|
|
|
|
|
|
esl_disconnect(&handle);
|
|
|
|
|
|
|
|
thread_running = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|