import
This commit is contained in:
10
Makefile
10
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.
|
||||
|
||||
@@ -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
|
||||
|
||||
10
Makefile.in
10
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.
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <massert.h>
|
||||
|
||||
|
||||
|
||||
int main(void) {
|
||||
int res = 0;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
53
libxtools/gconfig.c
Normal file
53
libxtools/gconfig.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <galexer.h>
|
||||
#include <gclexer.h>
|
||||
#include <vmapper.h>
|
||||
#include <gconfig.h>
|
||||
|
||||
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;
|
||||
}
|
||||
26
libxtools/gconfig.h
Normal file
26
libxtools/gconfig.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
|
||||
*/
|
||||
|
||||
#ifndef GCONFIG_H_QWERTY
|
||||
#define GCONFIG_H_QWERTY
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <gclexer.h>
|
||||
|
||||
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
|
||||
@@ -1,125 +0,0 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <tclexer.h>
|
||||
#include <tccomp.h>
|
||||
#include <vmapper.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <tclexer.h>
|
||||
#include <tccomp.h>
|
||||
#include <bstream.h>
|
||||
|
||||
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);
|
||||
2
logger.h
2
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
|
||||
|
||||
11
server.c
11
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);
|
||||
|
||||
Reference in New Issue
Block a user