From c04418d725604f001d3c328a4c780c78c1c58430 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: Wed, 1 Oct 2025 12:35:52 +0200 Subject: [PATCH] import --- Makefile | 10 ++-- Makefile.am | 8 ++- Makefile.in | 10 ++-- helmetctl.c | 1 + libxtools/bstream.c | 4 +- libxtools/gconfig.c | 53 ++++++++++++++++++ libxtools/gconfig.h | 26 +++++++++ libxtools/tccomp.c~ | 125 ------------------------------------------- libxtools/tconfig.h~ | 27 ---------- logger.h | 2 + server.c | 11 +++- 11 files changed, 112 insertions(+), 165 deletions(-) create mode 100644 libxtools/gconfig.c create mode 100644 libxtools/gconfig.h delete mode 100644 libxtools/tccomp.c~ delete mode 100644 libxtools/tconfig.h~ diff --git a/Makefile b/Makefile index 47ed310..dc76b35 100644 --- a/Makefile +++ b/Makefile @@ -121,7 +121,7 @@ am_libxtools_a_OBJECTS = libxtools/bstream.$(OBJEXT) \ libxtools/galexer.$(OBJEXT) libxtools/gclexer.$(OBJEXT) \ libxtools/massert.$(OBJEXT) libxtools/tccomp.$(OBJEXT) \ libxtools/tclexer.$(OBJEXT) libxtools/tconfig.$(OBJEXT) \ - libxtools/vmapper.$(OBJEXT) + libxtools/vmapper.$(OBJEXT) libxtools/gconfig.$(OBJEXT) libxtools_a_OBJECTS = $(am_libxtools_a_OBJECTS) am_helmetctl_OBJECTS = helmetctl.$(OBJEXT) logger.$(OBJEXT) helmetctl_OBJECTS = $(am_helmetctl_OBJECTS) @@ -322,7 +322,9 @@ libxtools_a_SOURCES = \ libxtools/tconfig.c \ libxtools/tconfig.h \ libxtools/vmapper.c \ - libxtools/vmapper.h + libxtools/vmapper.h \ + libxtools/gconfig.c \ + libxtools/gconfig.h libxasync_a_SOURCES = \ libxasync/waitgroup.c \ @@ -472,6 +474,7 @@ libxtools/tccomp.$(OBJEXT): libxtools/$(am__dirstamp) libxtools/tclexer.$(OBJEXT): libxtools/$(am__dirstamp) libxtools/tconfig.$(OBJEXT): libxtools/$(am__dirstamp) libxtools/vmapper.$(OBJEXT): libxtools/$(am__dirstamp) +libxtools/gconfig.$(OBJEXT): libxtools/$(am__dirstamp) libxtools.a: $(libxtools_a_OBJECTS) $(libxtools_a_DEPENDENCIES) $(EXTRA_libxtools_a_DEPENDENCIES) $(AM_V_at)-rm -f libxtools.a @@ -867,7 +870,8 @@ test: helmetctl ./helmetctl clean-local: - rm -f *~ + rm -f *~ + rm -f */*~ rm -rf autom4te.cache # Tell versions [3.59,3.63) of GNU make to not export all variables. diff --git a/Makefile.am b/Makefile.am index 76dbf90..91d846c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -38,7 +38,10 @@ libxtools_a_SOURCES = \ libxtools/tconfig.c \ libxtools/tconfig.h \ libxtools/vmapper.c \ - libxtools/vmapper.h + libxtools/vmapper.h \ + libxtools/gconfig.c \ + libxtools/gconfig.h + libxasync_a_SOURCES = \ libxasync/waitgroup.c \ @@ -48,5 +51,6 @@ test: helmetctl ./helmetctl clean-local: - rm -f *~ + rm -f *~ + rm -f */*~ rm -rf autom4te.cache diff --git a/Makefile.in b/Makefile.in index 8445c68..0cdc542 100644 --- a/Makefile.in +++ b/Makefile.in @@ -121,7 +121,7 @@ am_libxtools_a_OBJECTS = libxtools/bstream.$(OBJEXT) \ libxtools/galexer.$(OBJEXT) libxtools/gclexer.$(OBJEXT) \ libxtools/massert.$(OBJEXT) libxtools/tccomp.$(OBJEXT) \ libxtools/tclexer.$(OBJEXT) libxtools/tconfig.$(OBJEXT) \ - libxtools/vmapper.$(OBJEXT) + libxtools/vmapper.$(OBJEXT) libxtools/gconfig.$(OBJEXT) libxtools_a_OBJECTS = $(am_libxtools_a_OBJECTS) am_helmetctl_OBJECTS = helmetctl.$(OBJEXT) logger.$(OBJEXT) helmetctl_OBJECTS = $(am_helmetctl_OBJECTS) @@ -322,7 +322,9 @@ libxtools_a_SOURCES = \ libxtools/tconfig.c \ libxtools/tconfig.h \ libxtools/vmapper.c \ - libxtools/vmapper.h + libxtools/vmapper.h \ + libxtools/gconfig.c \ + libxtools/gconfig.h libxasync_a_SOURCES = \ libxasync/waitgroup.c \ @@ -472,6 +474,7 @@ libxtools/tccomp.$(OBJEXT): libxtools/$(am__dirstamp) libxtools/tclexer.$(OBJEXT): libxtools/$(am__dirstamp) libxtools/tconfig.$(OBJEXT): libxtools/$(am__dirstamp) libxtools/vmapper.$(OBJEXT): libxtools/$(am__dirstamp) +libxtools/gconfig.$(OBJEXT): libxtools/$(am__dirstamp) libxtools.a: $(libxtools_a_OBJECTS) $(libxtools_a_DEPENDENCIES) $(EXTRA_libxtools_a_DEPENDENCIES) $(AM_V_at)-rm -f libxtools.a @@ -867,7 +870,8 @@ test: helmetctl ./helmetctl clean-local: - rm -f *~ + rm -f *~ + rm -f */*~ rm -rf autom4te.cache # Tell versions [3.59,3.63) of GNU make to not export all variables. diff --git a/helmetctl.c b/helmetctl.c index ed30bdf..46d1dcb 100644 --- a/helmetctl.c +++ b/helmetctl.c @@ -12,6 +12,7 @@ #include + int main(void) { int res = 0; diff --git a/libxtools/bstream.c b/libxtools/bstream.c index 5646337..52fede6 100644 --- a/libxtools/bstream.c +++ b/libxtools/bstream.c @@ -81,9 +81,7 @@ ssize_t bstream_fread(bstream_t * stream, char* filename) { stream->data = realloc(stream->data, (size_t)newcapa); stream->capa = newcapa; } - if (buf != NULL) { - memcpy(&(stream->data[stream->wpos]), buf, (size_t)size); - } + memcpy(&(stream->data[stream->wpos]), buf, (size_t)size); stream->wpos += size; rsize += size; } diff --git a/libxtools/gconfig.c b/libxtools/gconfig.c new file mode 100644 index 0000000..dccc9d1 --- /dev/null +++ b/libxtools/gconfig.c @@ -0,0 +1,53 @@ +/* + * Copyright 2023 Oleg Borodin + */ + + +#include + +#include +#include +#include +#include + +void gconfig_init(gconfig_t* gconfig, int argc, char **argv) { + gclexer_init(&(gconfig->lex), argv, argc); + vmapper_init(&(gconfig->mapper)); +} + +int gconfig_bind(gconfig_t* gconfig, int type, char* name, void* ptr) { + vmapper_t* vmapper = &(gconfig->mapper); + return vmapper_bind(vmapper, type, name, ptr); +} + +int gconfig_parse(gconfig_t* gconfig) { +// return tccomp_parse(&(gconfig->comp)); + return 0; +} + +void gconfig_destroy(gconfig_t* gconfig) { + //gclexer_destroy(&(gconfig->lexer)); + vmapper_destroy(&(gconfig->mapper)); +} + +int __main(int argc, char **argv) { + (void)argc; + (void)argv; + + char*_argv[] = { argv[0], "--qwerty=-num12345", "--foo=-bar" }; + int _argc = 2; + + gclexer_t lex; + gclexer_init(&lex, _argv, _argc); + + char token[1024]; + int toktype = TOKEN_NULL; + int i = 0; + while (toktype != TOKEN_ENDF) { + toktype = gclexer_gettok(&lex, token); + printf("%d: %d [%s]\n", i, toktype, token); + i++; + } + + return 0; +} diff --git a/libxtools/gconfig.h b/libxtools/gconfig.h new file mode 100644 index 0000000..66576af --- /dev/null +++ b/libxtools/gconfig.h @@ -0,0 +1,26 @@ +/* + * Copyright 2023 Oleg Borodin + */ + +#ifndef GCONFIG_H_QWERTY +#define GCONFIG_H_QWERTY + +#include + +#include + +typedef struct { + gclexer_t lex; + vmapper_t mapper; +} gconfig_t; + +#define GCONF_STR MAPPER_STR +#define GCONF_INT MAPPER_INT +#define GCONF_BOOL MAPPER_BOOL + +void gconfig_init(gconfig_t* gconfig, int argc, char **argv); +int gconfig_bind(gconfig_t* gconfig, int type, char* name, void* ptr); +int gconfig_parse(gconfig_t* gconfig); +void gconfig_destroy(gconfig_t* gconfig); + +#endif diff --git a/libxtools/tccomp.c~ b/libxtools/tccomp.c~ deleted file mode 100644 index 755081f..0000000 --- a/libxtools/tccomp.c~ +++ /dev/null @@ -1,125 +0,0 @@ -/* - * - * Copyright 2023 Oleg Borodin - * - */ - - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#define RES_OK 0 -#define RES_ERR -1 - -#define POS1TYPE TOKEN_WORD -#define POS2TYPE TOKEN_OPER -#define POS3TYPE TOKEN_WORD -#define POS4TYPE TOKEN_COMM - -static char* strcopy(char* src) { - size_t srcsize = strlen(src) + 1; - char* dst = malloc(srcsize); - memset(dst, '\0', srcsize); - strcpy(dst, src); - return dst; -} - -tccomp_t * new_tccomp(tclexer_t * lexer, vmapper_t* vmapper) { - tccomp_t *comp = malloc(sizeof(tccomp_t)); - if (comp == NULL) return NULL; - comp->lexer = lexer; - comp->vmapper = vmapper; - comp->pos = 0; - comp->lnum = 0; - return comp; -} - - -void tccomp_init(tccomp_t * comp, tclexer_t * lexer, vmapper_t* vmapper) { - comp->lexer = lexer; - comp->vmapper = vmapper; - comp->pos = 0; - comp->lnum = 0; -} - -int tccomp_parse(tccomp_t * comp) { - char token[MAX_TOK_SIZE]; - int toktype = -1; - tclexer_t* lexer = comp->lexer; - char* key = NULL; - char* val = NULL; - - while (true) { - toktype = tclexer_get_token(lexer, token, MAX_TOK_SIZE); - if (toktype == TOKEN_SPACE) { - continue; - } - if (toktype == TOKEN_COMM) { - continue; - } - //printf("tok=%d pos=%d line=%d [%s]\n", toktype, comp->pos, comp->lnum, token); - - if (toktype == TOKEN_NEWLN) { - comp->lnum++; - } - - switch (comp->pos) { - case 0: { - if (toktype == TOKEN_NEWLN) { - comp->pos = 0; - break; - } - if (toktype != TOKEN_WORD) { - return -1; - } - comp->pos++; - key = strcopy(token); - break; - } - case 1: { - if (toktype != TOKEN_OPER) { - return -1; - } - comp->pos++; - break; - } - case 2: { - if (toktype != TOKEN_WORD) { - return -1; - } - comp->pos++; - val = strcopy(token); - break; - } - case 3: { - if (toktype != TOKEN_NEWLN && toktype != TOKEN_ENDFL) { - return -1; - } - comp->pos = 0; - printf("keyval = [%s], [%s]\n", key, val); - vmapper_set(comp->vmapper, key, val); - free(key); - free(val); - break; - } - } - if (toktype == TOKEN_ENDFL) break; - } - return 0; -} - -void tccomp_destroy(tccomp_t* comp) { - (void)comp; -} - -void tccomp_free(tccomp_t* comp) { - free(comp); -} diff --git a/libxtools/tconfig.h~ b/libxtools/tconfig.h~ deleted file mode 100644 index 778823c..0000000 --- a/libxtools/tconfig.h~ +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2023 Oleg Borodin - */ - - -#include - -#include -#include -#include - -typedef struct { - bstream_t stream; - tclexer_t lexer; - tccomp_t comp; - vmapper_t mapper; -} tconfig_t; - -#define TCONF_STR MAPPER_STR -#define TCONF_INT MAPPER_INT -#define TCONF_BOOL MAPPER_BOOL - -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); -void tconfig_destroy(tconfig_t* tconfig); diff --git a/logger.h b/logger.h index 3e1d527..ec31fb3 100644 --- a/logger.h +++ b/logger.h @@ -4,5 +4,7 @@ int logger_init(void); void logger_dprintf(char* message, ...); +void logger_wprintf(char* message, ...); +void logger_iprintf(char* message, ...); #endif diff --git a/server.c b/server.c index 0d48d2c..21ac5e2 100644 --- a/server.c +++ b/server.c @@ -13,6 +13,8 @@ int server_init(void) { return 0; } +#define SERVER_CONFIG_ERROR -1 + int server_config(void) { logger_dprintf("server config"); @@ -21,10 +23,15 @@ int server_config(void) { char* logpath = NULL; tconfig_bind(&tconfig, TCONF_STR, "logpath", &logpath); - ssize_t rsize = tconfig_read(&tconfig, "helmet.conf"); + ssize_t rsize = tconfig_read(&tconfig, "helmet.conf"); + if (rsize < 0) { + return SERVER_CONFIG_ERROR; + } int res = tconfig_parse(&tconfig); - MASSERT(res == -1); + if (res < 0) { + return SERVER_CONFIG_ERROR; + } tconfig_destroy(&tconfig); logger_dprintf("logpath = %s", logpath);