added error descr storing into tccomp

This commit is contained in:
Олег Бородин
2025-10-18 16:31:44 +02:00
parent bd89591945
commit a9541628be
15 changed files with 95 additions and 27 deletions

4
.gitignore vendored
View File

@@ -1,5 +1,7 @@
*.o
*.~
*.a
*.S
*.log
*_test
*~

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -1,7 +1,5 @@
# example
logpath = /var/log/helmet.log
sockpath = /var/run/helmet.run
#EOF

View File

@@ -11,12 +11,8 @@
#include <cllexer.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) {
size_t srcsize = strlen(src) + 1;

View File

@@ -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));
}

View File

@@ -4,6 +4,7 @@
*
*/
#define DEBUG 1
#include <stdint.h>
#include <stdlib.h>
@@ -15,15 +16,11 @@
#include <tclexer.h>
#include <tccomp.h>
#include <vmapper.h>
#include <xdebug.h>
#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);

View File

@@ -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);

View File

@@ -12,6 +12,7 @@
#include <tconfig.h>
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) {

View File

@@ -12,6 +12,7 @@
#include <bstream.h>
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

View File

@@ -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;
}

10
libxtools/xdebug.h Normal file
View 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

View File

@@ -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);

View File

@@ -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;
}