From 5dca71f775e408b5e32b18ac733569897eec6303 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, 15 Oct 2025 23:59:19 +0200 Subject: [PATCH] work in progess --- libxtools/clcomp.c | 15 +++++++++------ libxtools/clcomp.h | 4 +++- libxtools/clconfig.c | 14 ++++++++++++-- libxtools/clconfig.h | 2 ++ libxtools/clconfig_test.c | 4 ++++ 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/libxtools/clcomp.c b/libxtools/clcomp.c index 2ff8eb5..7903b60 100644 --- a/libxtools/clcomp.c +++ b/libxtools/clcomp.c @@ -25,14 +25,18 @@ void clcomp_init(clcomp_t* comp, vmapper_t* vmapper, char** argv, int argc) { comp->argc = argc; comp->argn = 1; comp->pos = 0; + comp->errstr = NULL; comp->vmapper = vmapper; if (comp->argc > comp->argn) { cllexer_init(&(comp->lexer), comp->argv[comp->argn]); } } -int clcomp_gettok(clcomp_t* comp, char* token) { +char* clcomp_geterr(clcomp_t* comp) { + return comp->errstr; +} +int clcomp_gettok(clcomp_t* comp, char* token) { if (comp->argn > comp->argc) { strcpy(token, "EOF"); return TOKEN_ENDF; @@ -49,14 +53,12 @@ int clcomp_gettok(clcomp_t* comp, char* token) { } int clcomp_parse(clcomp_t* comp) { - char token[1024]; int toktype = TOKEN_NULL; int i = 0; char* key = NULL; char* val = NULL; - while (toktype != TOKEN_ENDF) { toktype = clcomp_gettok(comp, token); i++; @@ -66,10 +68,8 @@ int clcomp_parse(clcomp_t* comp) { if (toktype == TOKEN_PREF) { comp->pos = 1; printf("pos %d: TOKEN_PREF %s\n", comp->pos, token); - continue; - } else { - continue; } + continue; } case 1: { if (toktype == TOKEN_WORD) { @@ -78,6 +78,7 @@ int clcomp_parse(clcomp_t* comp) { printf("pos %d: TOKEN_WORD %s\n", comp->pos, token); continue; } else { + comp->errstr = "Unknown key token"; goto error; } } @@ -87,6 +88,7 @@ int clcomp_parse(clcomp_t* comp) { printf("pos %d: TOKEN_DELIM %s\n", comp->pos, token); continue; } else { + comp->errstr = "Unknown delimeter token"; goto error; } } @@ -101,6 +103,7 @@ int clcomp_parse(clcomp_t* comp) { free(val); continue; } else { + comp->errstr = "Unknown key token"; goto error; } } diff --git a/libxtools/clcomp.h b/libxtools/clcomp.h index 30137e7..4ccfd4e 100644 --- a/libxtools/clcomp.h +++ b/libxtools/clcomp.h @@ -16,11 +16,13 @@ typedef struct { int argn; int pos; cllexer_t lexer; - vmapper_t* vmapper; + vmapper_t* vmapper; + char* errstr; } clcomp_t; void clcomp_init(clcomp_t* clcomp, vmapper_t* vmapper, char** argv, int argc); int clcomp_gettok(clcomp_t* clcomp, char* token); int clcomp_parse(clcomp_t* clcomp); +char* clcomp_geterr(clcomp_t* clcomp); void clcomp_destroy(clcomp_t* clcomp); #endif diff --git a/libxtools/clconfig.c b/libxtools/clconfig.c index 6ec479e..ef889e6 100644 --- a/libxtools/clconfig.c +++ b/libxtools/clconfig.c @@ -11,6 +11,7 @@ #include void clconfig_init(clconfig_t* clconfig, int argc, char **argv) { + clconfig->errstr = NULL; vmapper_init(&(clconfig->vmapper)); clcomp_init(&(clconfig->comp), &(clconfig->vmapper), argv, argc); } @@ -21,8 +22,17 @@ int clconfig_bind(clconfig_t* clconfig, int type, char* name, void* ptr) { } int clconfig_parse(clconfig_t* clconfig) { - clcomp_parse(&(clconfig->comp)); - return 0; + int res = clcomp_parse(&(clconfig->comp)); + if (res < 0) { + if ((clconfig->errstr = clcomp_geterr(&(clconfig->comp))) == NULL) { + clconfig->errstr = "Undefined command line error"; + } + } + return res; +} + +char* clconfig_geterr(clconfig_t* clconfig) { + return clconfig->errstr; } void clconfig_destroy(clconfig_t* clconfig) { diff --git a/libxtools/clconfig.h b/libxtools/clconfig.h index 086fdfc..88c2d81 100644 --- a/libxtools/clconfig.h +++ b/libxtools/clconfig.h @@ -15,6 +15,7 @@ typedef struct { vmapper_t vmapper; int argc; char ** argv; + char* errstr; } clconfig_t; #define GCONF_STR MAPPER_STR @@ -24,6 +25,7 @@ typedef struct { void clconfig_init(clconfig_t* clconfig, int argc, char **argv); int clconfig_bind(clconfig_t* clconfig, int type, char* name, void* ptr); int clconfig_parse(clconfig_t* clconfig); +char* clconfig_geterr(clconfig_t* clconfig); void clconfig_destroy(clconfig_t* clconfig); #endif diff --git a/libxtools/clconfig_test.c b/libxtools/clconfig_test.c index 532b0cc..02cd284 100644 --- a/libxtools/clconfig_test.c +++ b/libxtools/clconfig_test.c @@ -26,6 +26,10 @@ 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)); + } + MASSERT(res == 0); clconfig_destroy(&clconfig);