From: nishi Date: Sat, 13 Apr 2024 02:09:00 +0000 (+0000) Subject: syslog works X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=5a9e027d70fc8c4dc29dd85744ab605352d73fcb;p=mandshurica.git syslog works git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@7 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- diff --git a/DevForge/config.c b/DevForge/config.c index 974e54c..5d843eb 100644 --- a/DevForge/config.c +++ b/DevForge/config.c @@ -28,24 +28,97 @@ /* -------------------------------------------------------------------------- */ /* --- END LICENSE --- */ +#define DF_CONFIG_SRC #include "df_config.h" #include "devforge.h" #include "df_log.h" #include "df_util.h" +#include #include +#include #include #include #include +void devforge_add_mod(void* lib){ + void** old_mods = loaded_mods; + int i; + for(i = 0; old_mods[i] != NULL; i++); + loaded_mods = malloc(sizeof(*loaded_mods) * (i + 2)); + for(i = 0; old_mods[i] != NULL; i++){ + loaded_mods[i] = old_mods[i]; + } + loaded_mods[i] = lib; + loaded_mods[i + 1] = NULL; + free(old_mods); +} + int devforge_load_config(const char* path) { + if(server_root == NULL){ + server_root = devforge_strdup(PREFIX); + } + + if(loaded_mods == NULL){ + loaded_mods = malloc(sizeof(*loaded_mods)); + loaded_mods[0] = NULL; + } + char* str = devforge_strcat3("Loading the config `", path, "`"); devforge_log(DF_LOG, str); free(str); FILE* f = fopen(path, "r"); if(f != NULL) { + char cbuf[2]; + cbuf[1] = 0; + char* str = malloc(1); + str[0] = 0; + while(true) { + fread(cbuf, 1, 1, f); + if(cbuf[0] == '\n' || feof(f)) { + if(strlen(str) == 0) { + } else if(str[0] == '#') { + } else { + int i; + for(i = 0; str[i] != 0; i++){ + if(str[i] == ' ' || str[i] == '\t'){ + for(; str[i] != 0 && (str[i] == ' ' || str[i] == '\t'); i++) str[i] = 0; + char* key = str; + char* value = str + i; + if(strcmp(key, "ServerRoot") == 0){ + free(server_root); + server_root = devforge_strdup(value); + }else if(strcmp(key, "LoadModule") == 0){ + char* path; + if(value[0] == '/'){ + path = devforge_strdup(value); + }else{ + path = devforge_strcat3(server_root, "/", value); + } + void* lib = dlopen(path, RTLD_LAZY); + if(lib != NULL){ + void(*init_func)(void(*)(const char*, const char*)) = (void(*)(void(*)(const char*, const char*)))dlsym(lib, "mod_init"); + if(init_func != NULL) init_func(devforge_log); + devforge_add_mod(lib); + } + free(path); + } + } + } + } + free(str); + str = malloc(1); + str[0] = 0; + if(feof(f)) break; + } else { + char* tmp = str; + str = devforge_strcat(tmp, cbuf); + free(tmp); + } + } + free(str); fclose(f); } else { devforge_log(DF_ERROR, strerror(errno)); diff --git a/DevForge/devforge.h b/DevForge/devforge.h index 52a8dbc..72a91a5 100644 --- a/DevForge/devforge.h +++ b/DevForge/devforge.h @@ -33,4 +33,6 @@ #define DEVFORGE_VERSION "0.0" +#define DF_MOD_LOG "LOG" + #endif diff --git a/DevForge/df_config.h b/DevForge/df_config.h index ab35e5f..aab73ee 100644 --- a/DevForge/df_config.h +++ b/DevForge/df_config.h @@ -31,7 +31,27 @@ #ifndef __DEVFORGE_DF_CONFIG_H__ #define __DEVFORGE_DF_CONFIG_H__ +#include + int devforge_load_config(const char* path); int devforge_create_config(const char* path); +#ifdef DF_CONFIG_SRC +#define DF_CONFIG_PREFIX +#else +#define DF_CONFIG_PREFIX extern +#endif + +DF_CONFIG_PREFIX char* server_root +#ifdef DF_CONFIG_SRC + = NULL +#endif +; + +DF_CONFIG_PREFIX void** loaded_mods +#ifdef DF_CONFIG_SRC + = NULL +#endif +; + #endif diff --git a/DevForge/df_util.h b/DevForge/df_util.h index 226f869..136469b 100644 --- a/DevForge/df_util.h +++ b/DevForge/df_util.h @@ -33,5 +33,6 @@ char* devforge_strcat(const char* str1, const char* str2); char* devforge_strcat3(const char* str1, const char* str2, const char* str3); +char* devforge_strdup(const char* str); #endif diff --git a/DevForge/util.c b/DevForge/util.c index 59dab3c..9d15723 100644 --- a/DevForge/util.c +++ b/DevForge/util.c @@ -21,3 +21,10 @@ char* devforge_strcat3(const char* str1, const char* str2, const char* str3) { free(tmp); return r; } + +char* devforge_strdup(const char* str){ + char* r = malloc(strlen(str) + 1); + memcpy(r, str, strlen(str)); + r[strlen(str)] = 0; + return r; +} diff --git a/Module/syslog.c b/Module/syslog.c index 98aac2a..cc4e42c 100644 --- a/Module/syslog.c +++ b/Module/syslog.c @@ -27,3 +27,15 @@ /* OF SUCH DAMAGE. */ /* -------------------------------------------------------------------------- */ /* --- END LICENSE --- */ + +#include "../DevForge/devforge.h" +#include "../DevForge/df_log.h" + +const char type[] = DF_MOD_LOG; + +void(*putlog)(const char* name, const char* log); + +void mod_init(void(*_putlog)(const char* name, const char* log)){ + putlog = _putlog; + putlog(DF_INFO, "Syslog Module init"); +}