提交 6ac7c18b authored 作者: Anthony Minessale's avatar Anthony Minessale

work in progress

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@10892 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 40760653
......@@ -4,7 +4,7 @@ LIBEDIT_DIR=../../libs/libedit
CFLAGS=$(INCS) -g -ggdb -I$(LIBEDIT_DIR)/src/
MYLIB=libesl.a
OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o
OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o src/esl_config.o
all: $(MYLIB) fs_cli
......
......@@ -61,14 +61,18 @@ static void *msg_thread_run(esl_thread_t *me, void *obj)
esl_mutex_unlock(global_mutex);
goto done;
}
if (activity && FD_ISSET(handle->sock, &rfds)) {
esl_recv(handle);
if (esl_recv(handle)) {
running = thread_running = 0;
esl_mutex_unlock(global_mutex);
esl_log(ESL_LOG_WARNING, "Disconnected.\n");
goto done;
}
if (handle->last_event) {
const char *type = esl_event_get_header(handle->last_event, "content-type");
if (!strcasecmp(type, "log/data")) {
int level;
int level = 0;
if (strstr(handle->last_event->body, "[CONSOLE]")) {
level = 0;
} else if (strstr(handle->last_event->body, "[ALERT]")) {
......@@ -88,6 +92,10 @@ static void *msg_thread_run(esl_thread_t *me, void *obj)
}
printf("%s%s%s", COLORS[level], handle->last_event->body, ESL_SEQ_DEFAULT_COLOR);
} else if (0 && !strcasecmp(type, "text/disconnect-notice")) {
running = thread_running = 0;
} else {
printf("INCOMING DATA [%s]\n%s", type, handle->last_event->body);
}
}
......@@ -106,28 +114,29 @@ static void *msg_thread_run(esl_thread_t *me, void *obj)
static int process_command(esl_handle_t *handle, const char *cmd)
{
if (!strcasecmp(cmd, "exit")) {
if (
!strcasecmp(cmd, "exit") ||
!strcasecmp(cmd, "quit") ||
!strcasecmp(cmd, "bye")
) {
return -1;
}
if (!strncasecmp(cmd, "loglevel", 8)) {
const char *level = cmd + 8;
while(*level == ' ') level++;
if (!esl_strlen_zero(level)) {
char cb[128] = "";
snprintf(cb, sizeof(cb), "log %s\n\n", level);
esl_mutex_lock(global_mutex);
esl_send_recv(handle, cb);
printf("%s\n", handle->last_reply);
esl_mutex_unlock(global_mutex);
}
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)
) {
esl_mutex_lock(global_mutex);
esl_send_recv(handle, cmd);
printf("%s\n", handle->last_reply);
esl_mutex_unlock(global_mutex);
goto end;
}
printf("Unknown command [%s]\n", cmd);
end:
......@@ -136,17 +145,35 @@ static int process_command(esl_handle_t *handle, const char *cmd)
}
int main(void)
typedef struct {
char host[128];
char pass[128];
esl_port_t port;
} cli_profile_t;
static cli_profile_t profiles[128] = { 0 };
static int pcount;
int main(int argc, char *argv[])
{
esl_handle_t handle = {0};
int count;
const char *line;
char cmd_str[1024] = "";
char hfile[512] = "/tmp/fs_cli_history";
char cfile[512] = "/tmp/fs_cli_config";
char *home = getenv("HOME");
esl_config_t cfg;
cli_profile_t *profile = &profiles[0];
strncpy(profiles[0].host, "localhost", sizeof(profiles[0].host));
strncpy(profiles[0].pass, "ClueCon", sizeof(profiles[0].pass));
profiles[0].port = 8021;
pcount = 1;
if (home) {
snprintf(hfile, sizeof(hfile), "%s/.fs_cli_history", home);
snprintf(cfile, sizeof(cfile), "%s/.fs_cli_config", home);
}
esl_mutex_create(&global_mutex);
......@@ -155,15 +182,27 @@ int main(void)
gethostname(hostname, sizeof(hostname));
handle.debug = 0;
// um ya add some command line parsing for host port and pass
if (esl_config_open_file(&cfg, cfile)) {
char *var, *val;
while (esl_config_next_pair(&cfg, &var, &val)) {
}
esl_config_close_file(&cfg);
}
if (esl_connect(&handle, "localhost", 8021, "ClueCon")) {
if (esl_connect(&handle, profile->host, profile->port, profile->pass)) {
printf("Error Connecting [%s]\n", handle.err);
goto done;
return -1;
}
if (handle.debug) {
esl_global_set_default_logger(7);
}
esl_thread_create_detached(msg_thread_run, &handle);
el = el_init(__FILE__, stdout, stdout, stdout);
......
差异被折叠。
/*
* Copyright (c) 2007, Anthony Minessale II
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "esl.h"
#include "esl_config.h"
int esl_config_open_file(esl_config_t *cfg, const char *file_path)
{
FILE *f;
const char *path = NULL;
char path_buf[1024];
if (file_path[0] == '/') {
path = file_path;
} else {
snprintf(path_buf, sizeof(path_buf), "%s%s%s", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR, file_path);
path = path_buf;
}
if (!path) {
return 0;
}
memset(cfg, 0, sizeof(*cfg));
cfg->lockto = -1;
esl_log(ESL_LOG_DEBUG, "Configuration file is %s.\n", path);
f = fopen(path, "r");
if (!f) {
if (file_path[0] != '/') {
int last = -1;
char *var, *val;
snprintf(path_buf, sizeof(path_buf), "%s%sopenesl.conf", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR);
path = path_buf;
if ((f = fopen(path, "r")) == 0) {
return 0;
}
cfg->file = f;
esl_set_string(cfg->path, path);
while (esl_config_next_pair(cfg, &var, &val)) {
if ((cfg->sectno != last) && !strcmp(cfg->section, file_path)) {
cfg->lockto = cfg->sectno;
return 1;
}
}
esl_config_close_file(cfg);
memset(cfg, 0, sizeof(*cfg));
return 0;
}
return 0;
} else {
cfg->file = f;
esl_set_string(cfg->path, path);
return 1;
}
}
void esl_config_close_file(esl_config_t *cfg)
{
if (cfg->file) {
fclose(cfg->file);
}
memset(cfg, 0, sizeof(*cfg));
}
int esl_config_next_pair(esl_config_t *cfg, char **var, char **val)
{
int ret = 0;
char *p, *end;
*var = *val = NULL;
if (!cfg->path) {
return 0;
}
for (;;) {
cfg->lineno++;
if (!fgets(cfg->buf, sizeof(cfg->buf), cfg->file)) {
ret = 0;
break;
}
*var = cfg->buf;
if (**var == '[' && (end = strchr(*var, ']')) != 0) {
*end = '\0';
(*var)++;
if (**var == '+') {
(*var)++;
esl_copy_string(cfg->section, *var, sizeof(cfg->section));
cfg->sectno++;
if (cfg->lockto > -1 && cfg->sectno != cfg->lockto) {
break;
}
cfg->catno = 0;
cfg->lineno = 0;
*var = (char *) "";
*val = (char *) "";
return 1;
} else {
esl_copy_string(cfg->category, *var, sizeof(cfg->category));
cfg->catno++;
}
continue;
}
if (**var == '#' || **var == ';' || **var == '\n' || **var == '\r') {
continue;
}
if (!strncmp(*var, "__END__", 7)) {
break;
}
if ((end = strchr(*var, ';')) && *(end+1) == *end) {
*end = '\0';
end--;
} else if ((end = strchr(*var, '\n')) != 0) {
if (*(end - 1) == '\r') {
end--;
}
*end = '\0';
}
p = *var;
while ((*p == ' ' || *p == '\t') && p != end) {
*p = '\0';
p++;
}
*var = p;
if ((*val = strchr(*var, '=')) == 0) {
ret = -1;
/* log_printf(0, server.log, "Invalid syntax on %s: line %d\n", cfg->path, cfg->lineno); */
continue;
} else {
p = *val - 1;
*(*val) = '\0';
(*val)++;
if (*(*val) == '>') {
*(*val) = '\0';
(*val)++;
}
while ((*p == ' ' || *p == '\t') && p != *var) {
*p = '\0';
p--;
}
p = *val;
while ((*p == ' ' || *p == '\t') && p != end) {
*p = '\0';
p++;
}
*val = p;
ret = 1;
break;
}
}
return ret;
}
int esl_config_get_cas_bits(char *strvalue, unsigned char *outbits)
{
char cas_bits[5];
unsigned char bit = 0x8;
char *double_colon = strchr(strvalue, ':');
if (!double_colon) {
esl_log(ESL_LOG_ERROR, "No CAS bits specified: %s, :xxxx definition expected, where x is 1 or 0\n", double_colon);
return -1;
}
double_colon++;
*outbits = 0;
cas_bits[4] = 0;
if (sscanf(double_colon, "%c%c%c%c", &cas_bits[0], &cas_bits[1], &cas_bits[2], &cas_bits[3]) != 4) {
esl_log(ESL_LOG_ERROR, "Invalid CAS bits specified: %s, :xxxx definition expected, where x is 1 or 0\n", double_colon);
return -1;
}
esl_log(ESL_LOG_DEBUG, "CAS bits specification found: %s\n", cas_bits);
int x = 0;
for (; cas_bits[x]; x++) {
if ('1' == cas_bits[x]) {
*outbits |= bit;
} else if ('0' != cas_bits[x]) {
esl_log(ESL_LOG_ERROR, "Invalid CAS pattern specified: %s, just 0 or 1 allowed for each bit\n");
return -1;
}
bit >>= 1;
}
return 0;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
*/
......@@ -34,6 +34,9 @@
#ifndef _ESL_H_
#define _ESL_H_
#define esl_copy_string(_x, _y, _z) strncpy(_x, _y, _z - 1)
#define esl_set_string(_x, _y) esl_copy_string(_x, _y, sizeof(_x))
typedef struct esl_event_header esl_event_header_t;
typedef struct esl_event esl_event_t;
......@@ -204,6 +207,8 @@ typedef struct {
char header_buf[4196];
char last_reply[1024];
esl_event_t *last_event;
esl_event_t *last_ievent;
esl_event_t *info_event;
int debug;
int connected;
} esl_handle_t;
......@@ -213,12 +218,44 @@ typedef enum {
ESL_FALSE = 0
} esl_bool_t;
#ifndef __FUNCTION__
#define __FUNCTION__ (const char *)__func__
#endif
#define ESL_PRE __FILE__, __FUNCTION__, __LINE__
#define ESL_LOG_LEVEL_DEBUG 7
#define ESL_LOG_LEVEL_INFO 6
#define ESL_LOG_LEVEL_NOTICE 5
#define ESL_LOG_LEVEL_WARNING 4
#define ESL_LOG_LEVEL_ERROR 3
#define ESL_LOG_LEVEL_CRIT 2
#define ESL_LOG_LEVEL_ALERT 1
#define ESL_LOG_LEVEL_EMERG 0
#define ESL_LOG_DEBUG ESL_PRE, ESL_LOG_LEVEL_DEBUG
#define ESL_LOG_INFO ESL_PRE, ESL_LOG_LEVEL_INFO
#define ESL_LOG_NOTICE ESL_PRE, ESL_LOG_LEVEL_NOTICE
#define ESL_LOG_WARNING ESL_PRE, ESL_LOG_LEVEL_WARNING
#define ESL_LOG_ERROR ESL_PRE, ESL_LOG_LEVEL_ERROR
#define ESL_LOG_CRIT ESL_PRE, ESL_LOG_LEVEL_CRIT
#define ESL_LOG_ALERT ESL_PRE, ESL_LOG_LEVEL_ALERT
#define ESL_LOG_EMERG ESL_PRE, ESL_LOG_LEVEL_EMERG
typedef void (*esl_logger_t)(const char *file, const char *func, int line, int level, const char *fmt, ...);
extern esl_logger_t esl_log;
void esl_global_set_logger(esl_logger_t logger);
void esl_global_set_default_logger(int level);
#include "esl_event.h"
#include "esl_threadmutex.h"
#include "esl_config.h"
size_t esl_url_encode(const char *url, char *buf, size_t len);
char *esl_url_decode(char *s);
int esl_toupper(int c);
int esl_tolower(int c);
esl_status_t esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password);
esl_status_t esl_disconnect(esl_handle_t *handle);
......@@ -226,7 +263,6 @@ esl_status_t esl_send(esl_handle_t *handle, const char *cmd);
esl_status_t esl_recv(esl_handle_t *handle);
esl_status_t esl_send_recv(esl_handle_t *handle, const char *cmd);
#endif
......
/*
* Copyright (c) 2007, Anthony Minessale II
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup config Config File Parser
* @ingroup config
* This module implements a basic interface and file format parser
*
* <pre>
*
* EXAMPLE
*
* [category1]
* var1 => val1
* var2 => val2
* \# lines that begin with \# are comments
* \#var3 => val3
* </pre>
* @{
*/
#ifndef ESL_CONFIG_H
#define ESL_CONFIG_H
#include "esl.h"
#define ESL_URL_SEPARATOR "://"
#ifdef WIN32
#define ESL_PATH_SEPARATOR "\\"
#ifndef ESL_CONFIG_DIR
#define ESL_CONFIG_DIR "c:\\openesl"
#endif
#define esl_is_file_path(file) (*(file +1) == ':' || *file == '/' || strstr(file, SWITCH_URL_SEPARATOR))
#else
#define ESL_PATH_SEPARATOR "/"
#ifndef ESL_CONFIG_DIR
#define ESL_CONFIG_DIR "/etc/openesl"
#endif
#define esl_is_file_path(file) ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR))
#endif
typedef struct esl_config esl_config_t;
/*! \brief A simple file handle representing an open configuration file **/
struct esl_config {
/*! FILE stream buffer to the opened file */
FILE *file;
/*! path to the file */
char path[512];
/*! current category */
char category[256];
/*! current section */
char section[256];
/*! buffer of current line being read */
char buf[1024];
/*! current line number in file */
int lineno;
/*! current category number in file */
int catno;
/*! current section number in file */
int sectno;
int lockto;
};
/*!
\brief Open a configuration file
\param cfg (esl_config_t *) config handle to use
\param file_path path to the file
\return 1 (true) on success 0 (false) on failure
*/
int esl_config_open_file(esl_config_t * cfg, const char *file_path);
/*!
\brief Close a previously opened configuration file
\param cfg (esl_config_t *) config handle to use
*/
void esl_config_close_file(esl_config_t * cfg);
/*!
\brief Retrieve next name/value pair from configuration file
\param cfg (esl_config_t *) config handle to use
\param var pointer to aim at the new variable name
\param val pointer to aim at the new value
*/
int esl_config_next_pair(esl_config_t * cfg, char **var, char **val);
/*!
\brief Retrieve the CAS bits from a configuration string value
\param strvalue pointer to the configuration string value (expected to be in format whatever:xxxx)
\param outbits pointer to aim at the CAS bits
*/
int esl_config_get_cas_bits(char *strvalue, unsigned char *outbits);
/** @} */
#endif
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
*/
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论