]> Nishi Git Mirror - mandshurica.git/commitdiff
syslog works
authornishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 13 Apr 2024 02:09:00 +0000 (02:09 +0000)
committernishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 13 Apr 2024 02:09:00 +0000 (02:09 +0000)
git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@7 f982e544-4a7d-3444-ad1a-fde59a2a69f1

DevForge/config.c
DevForge/devforge.h
DevForge/df_config.h
DevForge/df_util.h
DevForge/util.c
Module/syslog.c

index 974e54c0f76dc0d53da2895922e26da5fad30705..5d843eb1199e2faa54d9be0431557e33a2b08eea 100644 (file)
 /* -------------------------------------------------------------------------- */
 /* --- END LICENSE --- */
 
+#define DF_CONFIG_SRC
 #include "df_config.h"
 
 #include "devforge.h"
 #include "df_log.h"
 #include "df_util.h"
 
+#include <dlfcn.h>
 #include <errno.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+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));
index 52a8dbc804f720f2b51dc21deb96cfc4fa2de46d..72a91a54a3ba2f0d47b1d9546ccf90c22db317aa 100644 (file)
@@ -33,4 +33,6 @@
 
 #define DEVFORGE_VERSION "0.0"
 
+#define DF_MOD_LOG "LOG"
+
 #endif
index ab35e5f85b3527267de66c37da36de644f8f3aee..aab73ee072f17de17f2cd3aa147064d922c2868c 100644 (file)
 #ifndef __DEVFORGE_DF_CONFIG_H__
 #define __DEVFORGE_DF_CONFIG_H__
 
+#include <stddef.h>
+
 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
index 226f8697080711b784968051873c287df8f4f98f..136469b7e318c436192d4ae114eb27b3f305f4eb 100644 (file)
@@ -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
index 59dab3c076b30bbefe8eb8709b397eb8383f3783..9d1572340066786dca4211a276e45c701fe2a92b 100644 (file)
@@ -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;
+}
index 98aac2a925cd96e69c626159c93feb2c30e44f05..cc4e42cc5fe0531e10f55233cbcca3c95ed1a956 100644 (file)
 /* 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");
+}