added error descr storing into tccomp
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,5 +1,7 @@
|
|||||||
*.o
|
*.o
|
||||||
*.~
|
|
||||||
*.a
|
*.a
|
||||||
*.S
|
*.S
|
||||||
*.log
|
*.log
|
||||||
|
*_test
|
||||||
|
*~
|
||||||
|
|
||||||
|
|||||||
12
Makefile
12
Makefile
@@ -934,6 +934,18 @@ uninstall-am: uninstall-binPROGRAMS uninstall-sbinPROGRAMS
|
|||||||
.PRECIOUS: Makefile
|
.PRECIOUS: Makefile
|
||||||
|
|
||||||
|
|
||||||
|
#test:: libxtools/cllexer_test
|
||||||
|
# cd libxtools && ./cllexer_test
|
||||||
|
|
||||||
|
#test:: libxtools/clcomp_test
|
||||||
|
# cd libxtools && ./clcomp_test
|
||||||
|
|
||||||
|
#test:: libxtools/clconfig_test
|
||||||
|
# cd libxtools && ./clconfig_test
|
||||||
|
|
||||||
|
test:: libxtools/tconfig_test
|
||||||
|
cd libxtools && ./tconfig_test
|
||||||
|
|
||||||
test:: server_test
|
test:: server_test
|
||||||
./server_test
|
./server_test
|
||||||
|
|
||||||
|
|||||||
@@ -72,6 +72,10 @@ libxtools_cllexer_test_LDADD = $(libxtools_NAME)
|
|||||||
#test:: libxtools/clconfig_test
|
#test:: libxtools/clconfig_test
|
||||||
# cd libxtools && ./clconfig_test
|
# cd libxtools && ./clconfig_test
|
||||||
|
|
||||||
|
test:: libxtools/tconfig_test
|
||||||
|
cd libxtools && ./tconfig_test
|
||||||
|
|
||||||
|
|
||||||
noinst_PROGRAMS += server_test
|
noinst_PROGRAMS += server_test
|
||||||
server_test_SOURCES = server_test.c server.c logger.c
|
server_test_SOURCES = server_test.c server.c logger.c
|
||||||
server_test_LDADD = $(libxtools_NAME)
|
server_test_LDADD = $(libxtools_NAME)
|
||||||
|
|||||||
12
Makefile.in
12
Makefile.in
@@ -934,6 +934,18 @@ uninstall-am: uninstall-binPROGRAMS uninstall-sbinPROGRAMS
|
|||||||
.PRECIOUS: Makefile
|
.PRECIOUS: Makefile
|
||||||
|
|
||||||
|
|
||||||
|
#test:: libxtools/cllexer_test
|
||||||
|
# cd libxtools && ./cllexer_test
|
||||||
|
|
||||||
|
#test:: libxtools/clcomp_test
|
||||||
|
# cd libxtools && ./clcomp_test
|
||||||
|
|
||||||
|
#test:: libxtools/clconfig_test
|
||||||
|
# cd libxtools && ./clconfig_test
|
||||||
|
|
||||||
|
test:: libxtools/tconfig_test
|
||||||
|
cd libxtools && ./tconfig_test
|
||||||
|
|
||||||
test:: server_test
|
test:: server_test
|
||||||
./server_test
|
./server_test
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
# example
|
|
||||||
logpath = /var/log/helmet.log
|
logpath = /var/log/helmet.log
|
||||||
sockpath = /var/run/helmet.run
|
sockpath = /var/run/helmet.run
|
||||||
#EOF
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,12 +11,8 @@
|
|||||||
|
|
||||||
#include <cllexer.h>
|
#include <cllexer.h>
|
||||||
#include <clcomp.h>
|
#include <clcomp.h>
|
||||||
|
#include <xdebug.h>
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
#define DPRINTF(str, ...) printf(str, __VA_ARGS__)
|
|
||||||
#else
|
|
||||||
#define DPRINTF(str, ...) ((void)0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static char* strcopy(char* src) {
|
static char* strcopy(char* src) {
|
||||||
size_t srcsize = strlen(src) + 1;
|
size_t srcsize = strlen(src) + 1;
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ int main(int argc, char** argv) {
|
|||||||
clconfig_bind(&clconfig, GCONF_STR, "strkey", &strkey);
|
clconfig_bind(&clconfig, GCONF_STR, "strkey", &strkey);
|
||||||
|
|
||||||
int res = clconfig_parse(&clconfig);
|
int res = clconfig_parse(&clconfig);
|
||||||
|
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
printf("error: %s\n", clconfig_geterr(&clconfig));
|
printf("error: %s\n", clconfig_geterr(&clconfig));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define DEBUG 1
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -15,15 +16,11 @@
|
|||||||
#include <tclexer.h>
|
#include <tclexer.h>
|
||||||
#include <tccomp.h>
|
#include <tccomp.h>
|
||||||
#include <vmapper.h>
|
#include <vmapper.h>
|
||||||
|
#include <xdebug.h>
|
||||||
|
|
||||||
#define RES_OK 0
|
#define RES_OK 0
|
||||||
#define RES_ERR -1
|
#define RES_ERR -1
|
||||||
|
|
||||||
//#define POS1TYPE TLEX_TOKEN_WORD
|
|
||||||
//#define POS2TYPE TLEX_TOKEN_OPER
|
|
||||||
//#define POS3TYPE TLEX_TOKEN_WORD
|
|
||||||
//#define POS4TYPE TLEX_TOKEN_COMM
|
|
||||||
|
|
||||||
static char* strcopy(char* src) {
|
static char* strcopy(char* src) {
|
||||||
size_t srcsize = strlen(src) + 1;
|
size_t srcsize = strlen(src) + 1;
|
||||||
char* dst = malloc(srcsize);
|
char* dst = malloc(srcsize);
|
||||||
@@ -42,6 +39,7 @@ tccomp_t* new_tccomp(tclexer_t * lexer, vmapper_t * vmapper) {
|
|||||||
comp->vmapper = vmapper;
|
comp->vmapper = vmapper;
|
||||||
comp->pos = 0;
|
comp->pos = 0;
|
||||||
comp->lnum = 0;
|
comp->lnum = 0;
|
||||||
|
comp->errstr = NULL;
|
||||||
return comp;
|
return comp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,6 +49,11 @@ void tccomp_init(tccomp_t * comp, tclexer_t * lexer, vmapper_t * vmapper) {
|
|||||||
comp->vmapper = vmapper;
|
comp->vmapper = vmapper;
|
||||||
comp->pos = 0;
|
comp->pos = 0;
|
||||||
comp->lnum = 0;
|
comp->lnum = 0;
|
||||||
|
comp->errstr = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* tccomp_geterr(tccomp_t * comp) {
|
||||||
|
return comp->errstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tccomp_parse(tccomp_t * comp) {
|
int tccomp_parse(tccomp_t * comp) {
|
||||||
@@ -68,12 +71,14 @@ int tccomp_parse(tccomp_t * comp) {
|
|||||||
if (toktype == TLEX_TOKEN_COMM) {
|
if (toktype == TLEX_TOKEN_COMM) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//printf("tok=%d pos=%d line=%d [%s]\n", toktype, comp->pos, comp->lnum, token);
|
DPRINTF("tok=%d pos=%d line=%d [%s]\n", toktype, comp->pos, comp->lnum, token);
|
||||||
|
|
||||||
|
if (toktype == TLEX_TOKEN_ENDFL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
if (toktype == TLEX_TOKEN_NEWLN) {
|
if (toktype == TLEX_TOKEN_NEWLN) {
|
||||||
comp->lnum++;
|
comp->lnum++;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (comp->pos) {
|
switch (comp->pos) {
|
||||||
case 0:{
|
case 0:{
|
||||||
if (toktype == TLEX_TOKEN_NEWLN) {
|
if (toktype == TLEX_TOKEN_NEWLN) {
|
||||||
@@ -81,6 +86,7 @@ int tccomp_parse(tccomp_t * comp) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (toktype != TLEX_TOKEN_WORD) {
|
if (toktype != TLEX_TOKEN_WORD) {
|
||||||
|
comp->errstr = "A string token is expected as key";
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
comp->pos++;
|
comp->pos++;
|
||||||
@@ -89,6 +95,7 @@ int tccomp_parse(tccomp_t * comp) {
|
|||||||
}
|
}
|
||||||
case 1:{
|
case 1:{
|
||||||
if (toktype != TLEX_TOKEN_OPER) {
|
if (toktype != TLEX_TOKEN_OPER) {
|
||||||
|
comp->errstr = "A operator token is expected";
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
comp->pos++;
|
comp->pos++;
|
||||||
@@ -96,6 +103,7 @@ int tccomp_parse(tccomp_t * comp) {
|
|||||||
}
|
}
|
||||||
case 2:{
|
case 2:{
|
||||||
if (toktype != TLEX_TOKEN_WORD) {
|
if (toktype != TLEX_TOKEN_WORD) {
|
||||||
|
comp->errstr = "A string token is expected as value";
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
comp->pos++;
|
comp->pos++;
|
||||||
@@ -104,10 +112,11 @@ int tccomp_parse(tccomp_t * comp) {
|
|||||||
}
|
}
|
||||||
case 3:{
|
case 3:{
|
||||||
if (toktype != TLEX_TOKEN_NEWLN && toktype != TLEX_TOKEN_ENDFL) {
|
if (toktype != TLEX_TOKEN_NEWLN && toktype != TLEX_TOKEN_ENDFL) {
|
||||||
|
comp->errstr = "A newline or EOF token is expected";
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
comp->pos = 0;
|
comp->pos = 0;
|
||||||
//printf("keyval = [%s], [%s]\n", key, val);
|
DPRINTF("keyval = [%s], [%s]\n", key, val);
|
||||||
vmapper_set(comp->vmapper, key, val);
|
vmapper_set(comp->vmapper, key, val);
|
||||||
free(key);
|
free(key);
|
||||||
free(val);
|
free(val);
|
||||||
|
|||||||
@@ -13,11 +13,13 @@ typedef struct {
|
|||||||
vmapper_t* vmapper;
|
vmapper_t* vmapper;
|
||||||
int pos;
|
int pos;
|
||||||
int lnum;
|
int lnum;
|
||||||
|
char* errstr;
|
||||||
} tccomp_t;
|
} tccomp_t;
|
||||||
|
|
||||||
tccomp_t* new_tccomp(tclexer_t * lexer, vmapper_t * vmapper);
|
tccomp_t* new_tccomp(tclexer_t * lexer, vmapper_t * vmapper);
|
||||||
void tccomp_init(tccomp_t * comp, tclexer_t * lexer, vmapper_t * vmapper);
|
void tccomp_init(tccomp_t * comp, tclexer_t * lexer, vmapper_t * vmapper);
|
||||||
int tccomp_parse(tccomp_t * comp);
|
int tccomp_parse(tccomp_t * comp);
|
||||||
|
char* tccomp_geterr(tccomp_t * comp);
|
||||||
void tccomp_destroy(tccomp_t * comp);
|
void tccomp_destroy(tccomp_t * comp);
|
||||||
void tccomp_free(tccomp_t * comp);
|
void tccomp_free(tccomp_t * comp);
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include <tconfig.h>
|
#include <tconfig.h>
|
||||||
|
|
||||||
void tconfig_init(tconfig_t * tconfig) {
|
void tconfig_init(tconfig_t * tconfig) {
|
||||||
|
tconfig->errstr = NULL;
|
||||||
bstream_init(&(tconfig->stream));
|
bstream_init(&(tconfig->stream));
|
||||||
vmapper_init(&(tconfig->mapper));
|
vmapper_init(&(tconfig->mapper));
|
||||||
tclexer_init(&(tconfig->lexer), &(tconfig->stream));
|
tclexer_init(&(tconfig->lexer), &(tconfig->stream));
|
||||||
@@ -20,19 +21,27 @@ void tconfig_init(tconfig_t * tconfig) {
|
|||||||
|
|
||||||
int tconfig_bind(tconfig_t * tconfig, int type, char* name, void* ptr) {
|
int tconfig_bind(tconfig_t * tconfig, int type, char* name, void* ptr) {
|
||||||
vmapper_t* vmapper = &(tconfig->mapper);
|
vmapper_t* vmapper = &(tconfig->mapper);
|
||||||
|
|
||||||
return vmapper_bind(vmapper, type, name, ptr);
|
return vmapper_bind(vmapper, type, name, ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t tconfig_read(tconfig_t * tconfig, char* filename) {
|
ssize_t tconfig_read(tconfig_t * tconfig, char* filename) {
|
||||||
bstream_t* stream = &(tconfig->stream);
|
bstream_t* stream = &(tconfig->stream);
|
||||||
|
|
||||||
return bstream_fread(stream, filename);
|
return bstream_fread(stream, filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
int tconfig_parse(tconfig_t * tconfig) {
|
char* tconfig_geterr(tconfig_t * tconfig) {
|
||||||
|
return tconfig->errstr;
|
||||||
|
}
|
||||||
|
|
||||||
return tccomp_parse(&(tconfig->comp));
|
int tconfig_parse(tconfig_t * tconfig) {
|
||||||
|
int res = 0;
|
||||||
|
if ((res = tccomp_parse(&(tconfig->comp))) < 0) {
|
||||||
|
if ((tconfig->errstr = tccomp_geterr(&(tconfig->comp))) == NULL) {
|
||||||
|
tconfig->errstr = "Unknown config parsing errror";
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tconfig_destroy(tconfig_t * tconfig) {
|
void tconfig_destroy(tconfig_t * tconfig) {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include <bstream.h>
|
#include <bstream.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
char* errstr;
|
||||||
bstream_t stream;
|
bstream_t stream;
|
||||||
tclexer_t lexer;
|
tclexer_t lexer;
|
||||||
tccomp_t comp;
|
tccomp_t comp;
|
||||||
@@ -26,6 +27,7 @@ void tconfig_init(tconfig_t * tconfig);
|
|||||||
int tconfig_bind(tconfig_t * tconfig, int type, char* name, void* ptr);
|
int tconfig_bind(tconfig_t * tconfig, int type, char* name, void* ptr);
|
||||||
ssize_t tconfig_read(tconfig_t * tconfig, char* filename);
|
ssize_t tconfig_read(tconfig_t * tconfig, char* filename);
|
||||||
int tconfig_parse(tconfig_t * tconfig);
|
int tconfig_parse(tconfig_t * tconfig);
|
||||||
|
char* tconfig_geterr(tconfig_t * tconfig);
|
||||||
void tconfig_destroy(tconfig_t * tconfig);
|
void tconfig_destroy(tconfig_t * tconfig);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -27,16 +27,20 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
ssize_t rsize = tconfig_read(&tconfig, "test.conf");
|
ssize_t rsize = tconfig_read(&tconfig, "test.conf");
|
||||||
|
|
||||||
printf("%ld\n", rsize);
|
printf("config size is %ld bites\n", rsize);
|
||||||
MASSERT(rsize > 0);
|
MASSERT(rsize > 0);
|
||||||
|
|
||||||
int res = tconfig_parse(&tconfig);
|
int res = tconfig_parse(&tconfig);
|
||||||
|
if (res < 0) {
|
||||||
MASSERT(res == -1);
|
char* errstr = tconfig_geterr(&tconfig);
|
||||||
|
printf("parse error: %s\n", errstr);
|
||||||
|
}
|
||||||
|
MASSERT(res == 0);
|
||||||
|
|
||||||
tconfig_destroy(&tconfig);
|
tconfig_destroy(&tconfig);
|
||||||
|
|
||||||
printf("int key = %d\n", intkey);
|
printf("int key = %d\n", intkey);
|
||||||
printf("str key = %s\n", strkey);
|
printf("str key = %s\n", strkey);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
10
libxtools/xdebug.h
Normal file
10
libxtools/xdebug.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef XDEBUG_H_QWERTY
|
||||||
|
#define XDEBUG_H_QWERTY
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
#define DPRINTF(s, ...) printf(s, __VA_ARGS__)
|
||||||
|
#else
|
||||||
|
#define DPRINTF(s, ...) ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
8
server.c
8
server.c
@@ -14,7 +14,7 @@ typedef struct {
|
|||||||
} server_t;
|
} server_t;
|
||||||
|
|
||||||
|
|
||||||
static server_t server;
|
//static server_t server;
|
||||||
|
|
||||||
int server_init(void) {
|
int server_init(void) {
|
||||||
logger_dprintf("server initialization");
|
logger_dprintf("server initialization");
|
||||||
@@ -37,11 +37,13 @@ int server_config(void) {
|
|||||||
}
|
}
|
||||||
int res = tconfig_parse(&tconfig);
|
int res = tconfig_parse(&tconfig);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
|
char* errstr = tconfig_geterr(&tconfig);
|
||||||
|
logger_dprintf("tconfig parsing error: %s", errstr);
|
||||||
return SERVER_CONFIG_ERROR;
|
return SERVER_CONFIG_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
tconfig_destroy(&tconfig);
|
//tconfig_destroy(&tconfig);
|
||||||
logger_dprintf("logpath = %s", logpath);
|
//logger_dprintf("logpath = %s", logpath);
|
||||||
|
|
||||||
//clconfig_t clconfig;
|
//clconfig_t clconfig;
|
||||||
//clconfig_init(&clconfig);
|
//clconfig_init(&clconfig);
|
||||||
|
|||||||
@@ -17,6 +17,11 @@ int main(int argc, char** argv) {
|
|||||||
(void)argv;
|
(void)argv;
|
||||||
|
|
||||||
server_init();
|
server_init();
|
||||||
|
int res = 0;
|
||||||
|
if ((res = server_config()) < 0) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user