From a9541628be5191fa2a9a3cae89a981ae9f63cecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D0=B5=D0=B3=20=D0=91=D0=BE=D1=80=D0=BE=D0=B4?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Sat, 18 Oct 2025 16:31:44 +0200 Subject: [PATCH] added error descr storing into tccomp --- .gitignore | 4 +++- Makefile | 12 ++++++++++++ Makefile.am | 4 ++++ Makefile.in | 12 ++++++++++++ helmet.conf | 4 +--- libxtools/clcomp.c | 6 +----- libxtools/clconfig_test.c | 1 + libxtools/tccomp.c | 25 +++++++++++++++++-------- libxtools/tccomp.h | 2 ++ libxtools/tconfig.c | 17 +++++++++++++---- libxtools/tconfig.h | 2 ++ libxtools/tconfig_test.c | 10 +++++++--- libxtools/xdebug.h | 10 ++++++++++ server.c | 8 +++++--- server_test.c | 5 +++++ 15 files changed, 95 insertions(+), 27 deletions(-) create mode 100644 libxtools/xdebug.h diff --git a/.gitignore b/.gitignore index a863328..5ee2904 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ *.o -*.~ *.a *.S *.log +*_test +*~ + diff --git a/Makefile b/Makefile index c4310e2..62f4d7b 100644 --- a/Makefile +++ b/Makefile @@ -934,6 +934,18 @@ uninstall-am: uninstall-binPROGRAMS uninstall-sbinPROGRAMS .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 ./server_test diff --git a/Makefile.am b/Makefile.am index 6b4ec5d..26f3c93 100644 --- a/Makefile.am +++ b/Makefile.am @@ -72,6 +72,10 @@ libxtools_cllexer_test_LDADD = $(libxtools_NAME) #test:: libxtools/clconfig_test # cd libxtools && ./clconfig_test +test:: libxtools/tconfig_test + cd libxtools && ./tconfig_test + + noinst_PROGRAMS += server_test server_test_SOURCES = server_test.c server.c logger.c server_test_LDADD = $(libxtools_NAME) diff --git a/Makefile.in b/Makefile.in index ad8129d..c59ec74 100644 --- a/Makefile.in +++ b/Makefile.in @@ -934,6 +934,18 @@ uninstall-am: uninstall-binPROGRAMS uninstall-sbinPROGRAMS .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 ./server_test diff --git a/helmet.conf b/helmet.conf index bb76321..43b77b4 100644 --- a/helmet.conf +++ b/helmet.conf @@ -1,7 +1,5 @@ -# example + logpath = /var/log/helmet.log sockpath = /var/run/helmet.run -#EOF - diff --git a/libxtools/clcomp.c b/libxtools/clcomp.c index ce52637..65c3dd9 100644 --- a/libxtools/clcomp.c +++ b/libxtools/clcomp.c @@ -11,12 +11,8 @@ #include #include +#include -#ifdef DEBUG -#define DPRINTF(str, ...) printf(str, __VA_ARGS__) -#else -#define DPRINTF(str, ...) ((void)0) -#endif static char* strcopy(char* src) { size_t srcsize = strlen(src) + 1; diff --git a/libxtools/clconfig_test.c b/libxtools/clconfig_test.c index 450b748..759cd1c 100644 --- a/libxtools/clconfig_test.c +++ b/libxtools/clconfig_test.c @@ -27,6 +27,7 @@ int main(int argc, char** argv) { clconfig_bind(&clconfig, GCONF_STR, "strkey", &strkey); int res = clconfig_parse(&clconfig); + if (res < 0) { printf("error: %s\n", clconfig_geterr(&clconfig)); } diff --git a/libxtools/tccomp.c b/libxtools/tccomp.c index 788d22d..64cc950 100644 --- a/libxtools/tccomp.c +++ b/libxtools/tccomp.c @@ -4,6 +4,7 @@ * */ +#define DEBUG 1 #include #include @@ -15,15 +16,11 @@ #include #include #include +#include #define RES_OK 0 #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) { size_t srcsize = strlen(src) + 1; char* dst = malloc(srcsize); @@ -42,6 +39,7 @@ tccomp_t* new_tccomp(tclexer_t * lexer, vmapper_t * vmapper) { comp->vmapper = vmapper; comp->pos = 0; comp->lnum = 0; + comp->errstr = NULL; return comp; } @@ -51,6 +49,11 @@ void tccomp_init(tccomp_t * comp, tclexer_t * lexer, vmapper_t * vmapper) { comp->vmapper = vmapper; comp->pos = 0; comp->lnum = 0; + comp->errstr = NULL; +} + +char* tccomp_geterr(tccomp_t * comp) { + return comp->errstr; } int tccomp_parse(tccomp_t * comp) { @@ -68,12 +71,14 @@ int tccomp_parse(tccomp_t * comp) { if (toktype == TLEX_TOKEN_COMM) { 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) { comp->lnum++; } - switch (comp->pos) { case 0:{ if (toktype == TLEX_TOKEN_NEWLN) { @@ -81,6 +86,7 @@ int tccomp_parse(tccomp_t * comp) { break; } if (toktype != TLEX_TOKEN_WORD) { + comp->errstr = "A string token is expected as key"; return -1; } comp->pos++; @@ -89,6 +95,7 @@ int tccomp_parse(tccomp_t * comp) { } case 1:{ if (toktype != TLEX_TOKEN_OPER) { + comp->errstr = "A operator token is expected"; return -1; } comp->pos++; @@ -96,6 +103,7 @@ int tccomp_parse(tccomp_t * comp) { } case 2:{ if (toktype != TLEX_TOKEN_WORD) { + comp->errstr = "A string token is expected as value"; return -1; } comp->pos++; @@ -104,10 +112,11 @@ int tccomp_parse(tccomp_t * comp) { } case 3:{ if (toktype != TLEX_TOKEN_NEWLN && toktype != TLEX_TOKEN_ENDFL) { + comp->errstr = "A newline or EOF token is expected"; return -1; } comp->pos = 0; - //printf("keyval = [%s], [%s]\n", key, val); + DPRINTF("keyval = [%s], [%s]\n", key, val); vmapper_set(comp->vmapper, key, val); free(key); free(val); diff --git a/libxtools/tccomp.h b/libxtools/tccomp.h index da82e98..3d7dde8 100644 --- a/libxtools/tccomp.h +++ b/libxtools/tccomp.h @@ -13,11 +13,13 @@ typedef struct { vmapper_t* vmapper; int pos; int lnum; + char* errstr; } tccomp_t; tccomp_t* new_tccomp(tclexer_t * lexer, vmapper_t * vmapper); void tccomp_init(tccomp_t * comp, tclexer_t * lexer, vmapper_t * vmapper); int tccomp_parse(tccomp_t * comp); +char* tccomp_geterr(tccomp_t * comp); void tccomp_destroy(tccomp_t * comp); void tccomp_free(tccomp_t * comp); diff --git a/libxtools/tconfig.c b/libxtools/tconfig.c index e628daa..b0d16c2 100644 --- a/libxtools/tconfig.c +++ b/libxtools/tconfig.c @@ -12,6 +12,7 @@ #include void tconfig_init(tconfig_t * tconfig) { + tconfig->errstr = NULL; bstream_init(&(tconfig->stream)); vmapper_init(&(tconfig->mapper)); 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) { vmapper_t* vmapper = &(tconfig->mapper); - return vmapper_bind(vmapper, type, name, ptr); } ssize_t tconfig_read(tconfig_t * tconfig, char* filename) { bstream_t* stream = &(tconfig->stream); - 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) { diff --git a/libxtools/tconfig.h b/libxtools/tconfig.h index 2fe2aed..fa5755f 100644 --- a/libxtools/tconfig.h +++ b/libxtools/tconfig.h @@ -12,6 +12,7 @@ #include typedef struct { + char* errstr; bstream_t stream; tclexer_t lexer; 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); ssize_t tconfig_read(tconfig_t * tconfig, char* filename); int tconfig_parse(tconfig_t * tconfig); +char* tconfig_geterr(tconfig_t * tconfig); void tconfig_destroy(tconfig_t * tconfig); #endif diff --git a/libxtools/tconfig_test.c b/libxtools/tconfig_test.c index be8a579..35147f2 100644 --- a/libxtools/tconfig_test.c +++ b/libxtools/tconfig_test.c @@ -27,16 +27,20 @@ int main(int argc, char** argv) { ssize_t rsize = tconfig_read(&tconfig, "test.conf"); - printf("%ld\n", rsize); + printf("config size is %ld bites\n", rsize); MASSERT(rsize > 0); int res = tconfig_parse(&tconfig); - - MASSERT(res == -1); + if (res < 0) { + char* errstr = tconfig_geterr(&tconfig); + printf("parse error: %s\n", errstr); + } + MASSERT(res == 0); tconfig_destroy(&tconfig); printf("int key = %d\n", intkey); printf("str key = %s\n", strkey); + return 0; } diff --git a/libxtools/xdebug.h b/libxtools/xdebug.h new file mode 100644 index 0000000..7959136 --- /dev/null +++ b/libxtools/xdebug.h @@ -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 diff --git a/server.c b/server.c index 6d2c777..cfb8fab 100644 --- a/server.c +++ b/server.c @@ -14,7 +14,7 @@ typedef struct { } server_t; -static server_t server; +//static server_t server; int server_init(void) { logger_dprintf("server initialization"); @@ -37,11 +37,13 @@ int server_config(void) { } int res = tconfig_parse(&tconfig); if (res < 0) { + char* errstr = tconfig_geterr(&tconfig); + logger_dprintf("tconfig parsing error: %s", errstr); return SERVER_CONFIG_ERROR; } - tconfig_destroy(&tconfig); - logger_dprintf("logpath = %s", logpath); + //tconfig_destroy(&tconfig); + //logger_dprintf("logpath = %s", logpath); //clconfig_t clconfig; //clconfig_init(&clconfig); diff --git a/server_test.c b/server_test.c index 0b413e3..52b0672 100644 --- a/server_test.c +++ b/server_test.c @@ -17,6 +17,11 @@ int main(int argc, char** argv) { (void)argv; server_init(); + int res = 0; + if ((res = server_config()) < 0) { + return res; + } + return 0; }