/* -------------------------------------------------------------------------- */
/* --- 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));
#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
/* 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");
+}