fs_cli: make some stylistic improvements

This commit is contained in:
Travis Cross 2011-09-22 02:55:59 +00:00
parent a78ec2588a
commit a511dcee94

View File

@ -6,17 +6,19 @@
#include <stdlib.h> #include <stdlib.h>
#include <esl.h> #include <esl.h>
#include <signal.h> #include <signal.h>
#include <getopt.h>
#define CMD_BUFLEN 1024 #define CMD_BUFLEN 1024
static int WARN_STOP = 0;
#ifdef WIN32 #ifndef WIN32
#include <sys/select.h>
#include <unistd.h>
#else
#define strdup(src) _strdup(src) #define strdup(src) _strdup(src)
#define usleep(time) Sleep(time/1000) #define usleep(time) Sleep(time/1000)
#define fileno _fileno #define fileno _fileno
#define read _read #define read _read
#include <io.h> #include <io.h>
#define CC_NORM 0 #define CC_NORM 0
#define CC_NEWLINE 1 #define CC_NEWLINE 1
#define CC_EOF 2 #define CC_EOF 2
@ -27,7 +29,6 @@ static int WARN_STOP = 0;
#define CC_FATAL 7 #define CC_FATAL 7
#define CC_REDISPLAY 8 #define CC_REDISPLAY 8
#define CC_REFRESH_BEEP 9 #define CC_REFRESH_BEEP 9
#define HISTLEN 10 #define HISTLEN 10
#define KEY_UP 1 #define KEY_UP 1
#define KEY_DOWN 2 #define KEY_DOWN 2
@ -38,23 +39,14 @@ static int WARN_STOP = 0;
#define KEY_RIGHT 7 #define KEY_RIGHT 7
#define KEY_INSERT 8 #define KEY_INSERT 8
#define PROMPT_OP 9 #define PROMPT_OP 9
static int console_bufferInput (char *buf, int len, char *cmd, int key); static int console_bufferInput (char *buf, int len, char *cmd, int key);
static unsigned char esl_console_complete(const char *buffer, const char *cursor); static unsigned char esl_console_complete(const char *buffer, const char *cursor);
#else
#include <sys/select.h>
#include <unistd.h>
#endif #endif
#include <getopt.h>
#ifdef HAVE_EDITLINE #ifdef HAVE_EDITLINE
#include <histedit.h> #include <histedit.h>
#endif #endif
static char prompt_str[512] = "";
static int CONNECTED = 0;
typedef struct { typedef struct {
char name[128]; char name[128];
char host[128]; char host[128];
@ -67,18 +59,22 @@ typedef struct {
int quiet; int quiet;
} cli_profile_t; } cli_profile_t;
static int warn_stop = 0;
static int connected = 0;
static char prompt_str[512] = "";
static cli_profile_t profiles[128] = {{{0}}}; static cli_profile_t profiles[128] = {{{0}}};
static cli_profile_t internal_profile = {{ 0 }}; static cli_profile_t internal_profile = {{ 0 }};
static int pcount = 0; static int pcount = 0;
static esl_handle_t *global_handle; static esl_handle_t *global_handle;
static cli_profile_t *global_profile; static cli_profile_t *global_profile;
static int process_command(esl_handle_t *handle, const char *cmd);
static int running = 1; static int running = 1;
static int thread_running = 0; static int thread_running = 0;
static char *filter_uuid; static char *filter_uuid;
static EditLine *el;
static History *myhistory;
static HistEvent ev;
static int process_command(esl_handle_t *handle, const char *cmd);
/* If a fnkey is configured then process the command */ /* If a fnkey is configured then process the command */
static unsigned char console_fnkey_pressed(int i) static unsigned char console_fnkey_pressed(int i)
@ -101,11 +97,6 @@ static unsigned char console_fnkey_pressed(int i)
#ifdef HAVE_EDITLINE #ifdef HAVE_EDITLINE
static char *prompt(EditLine *e) { return prompt_str; } static char *prompt(EditLine *e) { return prompt_str; }
static EditLine *el;
static History *myhistory;
static HistEvent ev;
static unsigned char console_f1key(EditLine *el, int ch) { return console_fnkey_pressed(1); } static unsigned char console_f1key(EditLine *el, int ch) { return console_fnkey_pressed(1); }
static unsigned char console_f2key(EditLine *el, int ch) { return console_fnkey_pressed(2); } static unsigned char console_f2key(EditLine *el, int ch) { return console_fnkey_pressed(2); }
static unsigned char console_f3key(EditLine *el, int ch) { return console_fnkey_pressed(3); } static unsigned char console_f3key(EditLine *el, int ch) { return console_fnkey_pressed(3); }
@ -472,12 +463,11 @@ static BOOL console_readConsole(HANDLE conIn, char *buf, int len, int *pRed, int
static void handle_SIGINT(int sig) static void handle_SIGINT(int sig)
{ {
if (sig); if (!connected) {
if (!CONNECTED) {
fprintf(stdout, "Interrupted.\n"); fprintf(stdout, "Interrupted.\n");
exit(1); exit(1);
} }
WARN_STOP = 1; warn_stop = 1;
signal(SIGINT, handle_SIGINT); signal(SIGINT, handle_SIGINT);
#ifdef SIGTSTP #ifdef SIGTSTP
signal(SIGTSTP, handle_SIGINT); signal(SIGTSTP, handle_SIGINT);
@ -495,28 +485,36 @@ static void handle_SIGQUIT(int sig)
static HANDLE hStdout; static HANDLE hStdout;
static WORD wOldColorAttrs; static WORD wOldColorAttrs;
static CONSOLE_SCREEN_BUFFER_INFO csbiInfo; static CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
static WORD
#else
static const char*
#endif #endif
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 }; #ifdef WIN32
static WORD colors[]
#else
static const char *colors[]
#endif
= {
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
};
static const char *usage_str =
"Usage: %s [-H <host>] [-P <port>] [-p <secret>] [-d <level>] [-x command] [-t <timeout_ms>] [profile]\n\n"
" -?,-h --help Usage Information\n"
" -H, --host=hostname Host to connect\n"
" -P, --port=port Port to connect (1 - 65535)\n"
" -u, --user=user@domain user@domain\n"
" -p, --password=password Password\n"
" -i, --interrupt Allow Control-c to interrupt\n"
" -x, --execute=command Execute Command and Exit\n"
" -l, --loglevel=command Log Level\n"
" -q, --quiet Disable logging\n"
" -r, --retry Retry connection on failure\n"
" -R, --reconnect Reconnect if disconnected\n"
" -d, --debug=level Debug Level (0 - 7)\n"
" -t, --timeout Timeout for API commands (in miliseconds)\n\n";
static int usage(char *name){ static int usage(char *name){
printf("Usage: %s [-H <host>] [-P <port>] [-p <secret>] [-d <level>] [-x command] [-t <timeout_ms>] [profile]\n\n", name); printf(usage_str, name);
printf(" -?,-h --help Usage Information\n");
printf(" -H, --host=hostname Host to connect\n");
printf(" -P, --port=port Port to connect (1 - 65535)\n");
printf(" -u, --user=user@domain user@domain\n");
printf(" -p, --password=password Password\n");
printf(" -i, --interrupt Allow Control-c to interrupt\n");
printf(" -x, --execute=command Execute Command and Exit\n");
printf(" -l, --loglevel=command Log Level\n");
printf(" -q, --quiet Disable logging\n");
printf(" -r, --retry Retry connection on failure\n");
printf(" -R, --reconnect Reconnect if disconnected\n");
printf(" -d, --debug=level Debug Level (0 - 7)\n");
printf(" -t, --timeout Timeout for API commands (in miliseconds)\n\n");
return 1; return 1;
} }
@ -525,17 +523,16 @@ static void *msg_thread_run(esl_thread_t *me, void *obj)
esl_handle_t *handle = (esl_handle_t *) obj; esl_handle_t *handle = (esl_handle_t *) obj;
thread_running = 1; thread_running = 1;
while(thread_running && handle->connected) { while(thread_running && handle->connected) {
esl_status_t status = esl_recv_event_timed(handle, 10, 1, NULL);
int aok = 1; int aok = 1;
esl_status_t status = esl_recv_event_timed(handle, 10, 1, NULL);
if (status == ESL_FAIL) { if (status == ESL_FAIL) {
if (aok) esl_log(ESL_LOG_WARNING, "Disconnected.\n"); if (aok) esl_log(ESL_LOG_WARNING, "Disconnected.\n");
running = -1; thread_running = 0; running = -1; thread_running = 0;
} else if (status == ESL_SUCCESS) { } else if (status == ESL_SUCCESS) {
#ifndef WIN32 #ifndef WIN32
fd_set can_write; fd_set can_write;
int fd; int fd = fileno(stdout);
struct timeval to; struct timeval to;
fd = fileno(stdout);
memset(&to, 0, sizeof(to)); memset(&to, 0, sizeof(to));
FD_ZERO(&can_write); FD_ZERO(&can_write);
FD_SET(fd, &can_write); FD_SET(fd, &can_write);
@ -548,8 +545,8 @@ static void *msg_thread_run(esl_thread_t *me, void *obj)
} }
#endif #endif
if (handle->last_event) { if (handle->last_event) {
int known = 1;
const char *type = esl_event_get_header(handle->last_event, "content-type"); const char *type = esl_event_get_header(handle->last_event, "content-type");
int known = 0;
if (!esl_strlen_zero(type)) { if (!esl_strlen_zero(type)) {
if (aok && !strcasecmp(type, "log/data")) { if (aok && !strcasecmp(type, "log/data")) {
const char *userdata = esl_event_get_header(handle->last_event, "user-data"); const char *userdata = esl_event_get_header(handle->last_event, "user-data");
@ -563,38 +560,37 @@ static void *msg_thread_run(esl_thread_t *me, void *obj)
if (lname) { if (lname) {
level = atoi(lname); level = atoi(lname);
} }
#ifdef WIN32 #ifndef WIN32
SetConsoleTextAttribute(hStdout, COLORS[level]); printf("%s%s%s", colors[level], handle->last_event->body, ESL_SEQ_DEFAULT_COLOR);
#else
SetConsoleTextAttribute(hStdout, colors[level]);
WriteFile(hStdout, handle->last_event->body, len, &outbytes, NULL); WriteFile(hStdout, handle->last_event->body, len, &outbytes, NULL);
SetConsoleTextAttribute(hStdout, wOldColorAttrs); SetConsoleTextAttribute(hStdout, wOldColorAttrs);
#else
printf("%s%s%s", COLORS[level], handle->last_event->body, ESL_SEQ_DEFAULT_COLOR);
#endif #endif
} }
known++;
} else if (!strcasecmp(type, "text/disconnect-notice")) { } else if (!strcasecmp(type, "text/disconnect-notice")) {
running = -1; thread_running = 0; running = -1; thread_running = 0;
known++;
} else if (aok && !strcasecmp(type, "text/event-plain")) { } else if (aok && !strcasecmp(type, "text/event-plain")) {
char *foo; char *s;
esl_event_serialize(handle->last_ievent, &foo, ESL_FALSE); esl_event_serialize(handle->last_ievent, &s, ESL_FALSE);
printf("RECV EVENT\n%s\n", foo); printf("RECV EVENT\n%s\n", s);
free(foo); free(s);
known++; } else {
known = 0;
} }
} }
if (aok && !known) { if (aok && !known) {
char *foo; char *s;
printf("INCOMING DATA [%s]\n%s\n", type, handle->last_event->body ? handle->last_event->body : ""); printf("INCOMING DATA [%s]\n%s\n", type, handle->last_event->body ? handle->last_event->body : "");
esl_event_serialize(handle->last_event, &foo, ESL_FALSE); esl_event_serialize(handle->last_event, &s, ESL_FALSE);
printf("RECV EVENT\n%s\n", foo); printf("RECV EVENT\n%s\n", s);
free(foo); free(s);
} }
} }
} }
if (WARN_STOP) { if (warn_stop) {
if (aok) printf("Type control-D or /exit or /quit or /bye to exit.\n\n"); if (aok) printf("Type control-D or /exit or /quit or /bye to exit.\n\n");
WARN_STOP = 0; warn_stop = 0;
} }
usleep(1000); usleep(1000);
} }
@ -603,11 +599,7 @@ static void *msg_thread_run(esl_thread_t *me, void *obj)
return NULL; return NULL;
} }
static int process_command(esl_handle_t *handle, const char *cmd) static const char *cli_usage =
{
if ((*cmd == '/' && cmd++) || !strncasecmp(cmd, "...", 3)) {
if (!strcasecmp(cmd, "help")) {
printf(
"Command \tDescription\n" "Command \tDescription\n"
"-----------------------------------------------\n" "-----------------------------------------------\n"
"/help \tHelp\n" "/help \tHelp\n"
@ -617,8 +609,13 @@ static int process_command(esl_handle_t *handle, const char *cmd)
"/uuid \tFilter logs for a single call uuid\n" "/uuid \tFilter logs for a single call uuid\n"
"/filter \tFilter commands.\n" "/filter \tFilter commands.\n"
"/debug [0-7] \tSet debug level.\n" "/debug [0-7] \tSet debug level.\n"
"\n" "\n";
);
static int process_command(esl_handle_t *handle, const char *cmd)
{
if ((*cmd == '/' && cmd++) || !strncasecmp(cmd, "...", 3)) {
if (!strcasecmp(cmd, "help")) {
printf("%s", cli_usage);
goto end; goto end;
} }
if (!strcasecmp(cmd, "exit") || if (!strcasecmp(cmd, "exit") ||
@ -736,9 +733,7 @@ static const char *basic_gets(int *cnt)
} }
#endif #endif
static void print_banner(FILE *stream) static const char *banner =
{
fprintf(stream,
" _____ ____ ____ _ ___ \n" " _____ ____ ____ _ ___ \n"
" | ___/ ___| / ___| | |_ _| \n" " | ___/ ___| / ___| | |_ _| \n"
" | |_ \\___ \\ | | | | | | \n" " | |_ \\___ \\ | | | | | | \n"
@ -752,9 +747,9 @@ static void print_banner(FILE *stream)
"* Brought to you by ClueCon http://www.cluecon.com/ *\n" "* Brought to you by ClueCon http://www.cluecon.com/ *\n"
"*******************************************************\n" "*******************************************************\n"
"\n" "\n"
"Type /help <enter> to see a list of commands\n\n\n" "Type /help <enter> to see a list of commands\n\n\n";
);
} static void print_banner(FILE *stream) { fprintf(stream,banner); }
static void set_fn_keys(cli_profile_t *profile) static void set_fn_keys(cli_profile_t *profile)
{ {
@ -772,7 +767,7 @@ static void set_fn_keys(cli_profile_t *profile)
profile->console_fnkeys[11] = "version"; profile->console_fnkeys[11] = "version";
} }
#define end_of_p(_s) (*_s == '\0' ? _s : _s + strlen(_s) - 1) static char* end_of_str(char *s) { return (*s == '\0' ? s : s + strlen(s) - 1); }
static unsigned char esl_console_complete(const char *buffer, const char *cursor) static unsigned char esl_console_complete(const char *buffer, const char *cursor)
{ {
@ -798,10 +793,10 @@ static unsigned char esl_console_complete(const char *buffer, const char *cursor
#ifdef HAVE_EDITLINE #ifdef HAVE_EDITLINE
if (!*buf && sc) { if (!*buf && sc) {
el_deletestr(el, sc); el_deletestr(el, sc);
sc = 0;
} }
#endif #endif
sc = 0; p = end_of_str(buf);
p = end_of_p(buf);
while(p >= buf && *p == ' ') { while(p >= buf && *p == ' ') {
sc++; sc++;
p--; p--;
@ -869,7 +864,6 @@ int main(int argc, char *argv[])
char cmd_str[1024] = ""; char cmd_str[1024] = "";
esl_config_t cfg; esl_config_t cfg;
cli_profile_t *profile = NULL; cli_profile_t *profile = NULL;
int rv = 0;
#ifndef WIN32 #ifndef WIN32
char hfile[512] = "/etc/fs_cli_history"; char hfile[512] = "/etc/fs_cli_history";
char cfile[512] = "/etc/fs_cli.conf"; char cfile[512] = "/etc/fs_cli.conf";
@ -1002,10 +996,8 @@ int main(int argc, char *argv[])
printf("\n"); printf("\n");
return usage(argv[0]); return usage(argv[0]);
} }
if (!(rv = esl_config_open_file(&cfg, cfile))) { if (esl_config_open_file(&cfg, cfile) ||
rv = esl_config_open_file(&cfg, dft_cfile); esl_config_open_file(&cfg, dft_cfile)) {
}
if (rv) {
char *var, *val; char *var, *val;
char cur_cat[128] = ""; char cur_cat[128] = "";
while (esl_config_next_pair(&cfg, &var, &val)) { while (esl_config_next_pair(&cfg, &var, &val)) {
@ -1090,7 +1082,7 @@ int main(int argc, char *argv[])
snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", profile->name); snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", profile->name);
} }
connect: connect:
CONNECTED = 0; connected = 0;
while (--loops > 0) { while (--loops > 0) {
memset(&handle, 0, sizeof(handle)); memset(&handle, 0, sizeof(handle));
if (esl_connect(&handle, profile->host, profile->port, profile->user, profile->pass)) { if (esl_connect(&handle, profile->host, profile->port, profile->user, profile->pass)) {
@ -1109,7 +1101,7 @@ int main(int argc, char *argv[])
} }
} else { } else {
if (!ctl_c) { if (!ctl_c) {
CONNECTED = 1; connected = 1;
} }
if (temp_log < 0 ) { if (temp_log < 0 ) {
esl_global_set_default_logger(profile->debug); esl_global_set_default_logger(profile->debug);
@ -1151,8 +1143,6 @@ int main(int argc, char *argv[])
el_set(el, EL_PROMPT, &prompt); el_set(el, EL_PROMPT, &prompt);
el_set(el, EL_EDITOR, "emacs"); el_set(el, EL_EDITOR, "emacs");
myhistory = history_init();
el_set(el, EL_ADDFN, "f1-key", "F1 KEY PRESS", console_f1key); el_set(el, EL_ADDFN, "f1-key", "F1 KEY PRESS", console_f1key);
el_set(el, EL_ADDFN, "f2-key", "F2 KEY PRESS", console_f2key); el_set(el, EL_ADDFN, "f2-key", "F2 KEY PRESS", console_f2key);
el_set(el, EL_ADDFN, "f3-key", "F3 KEY PRESS", console_f3key); el_set(el, EL_ADDFN, "f3-key", "F3 KEY PRESS", console_f3key);
@ -1190,11 +1180,10 @@ int main(int argc, char *argv[])
el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete); el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
el_set(el, EL_BIND, "^I", "ed-complete", NULL); el_set(el, EL_BIND, "^I", "ed-complete", NULL);
if (myhistory == 0) { if (!(myhistory = history_init())) {
esl_log(ESL_LOG_ERROR, "history could not be initialized\n"); esl_log(ESL_LOG_ERROR, "history could not be initialized\n");
goto done; goto done;
} }
history(myhistory, &ev, H_SETSIZE, 800); history(myhistory, &ev, H_SETSIZE, 800);
el_set(el, EL_HIST, history, myhistory); el_set(el, EL_HIST, history, myhistory);
history(myhistory, &ev, H_LOAD, hfile); history(myhistory, &ev, H_LOAD, hfile);
@ -1220,30 +1209,27 @@ int main(int argc, char *argv[])
#else #else
line = basic_gets(&count); line = basic_gets(&count);
#endif #endif
if (count > 1) { if (count > 1 && !esl_strlen_zero(line)) {
if (!esl_strlen_zero(line)) { char *p, *cmd = strdup(line);
char *cmd = strdup(line);
char *p;
#ifdef HAVE_EDITLINE
const LineInfo *lf = el_line(el);
char *foo = (char *) lf->buffer;
#endif
if ((p = strrchr(cmd, '\r')) || (p = strrchr(cmd, '\n'))) { if ((p = strrchr(cmd, '\r')) || (p = strrchr(cmd, '\n'))) {
*p = '\0'; *p = '\0';
} }
assert(cmd != NULL); assert(cmd);
#ifdef HAVE_EDITLINE #ifdef HAVE_EDITLINE
history(myhistory, &ev, H_ENTER, line); history(myhistory, &ev, H_ENTER, line);
#endif #endif
if ((r = process_command(&handle, cmd))) { if ((r = process_command(&handle, cmd))) {
running = r; running = r;
} }
#ifdef HAVE_EDITLINE
el_deletestr(el, strlen(foo) + 1);
memset(foo, 0, strlen(foo));
#endif
free(cmd); free(cmd);
#ifdef HAVE_EDITLINE
{
const LineInfo *lf = el_line(el);
char *s = (char *) lf->buffer;
el_deletestr(el, strlen(s) + 1);
memset(s, 0, strlen(s));
} }
#endif
} }
usleep(1000); usleep(1000);
} }
@ -1255,7 +1241,6 @@ int main(int argc, char *argv[])
#ifdef HAVE_EDITLINE #ifdef HAVE_EDITLINE
done: done:
history(myhistory, &ev, H_SAVE, hfile); history(myhistory, &ev, H_SAVE, hfile);
/* Clean up our memory */
history_end(myhistory); history_end(myhistory);
el_end(el); el_end(el);
#endif #endif