Subversion Repositories Tewi

Rev

Rev 25 | Rev 159 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 nishi 1
/* $Id: module.c 156 2024-09-25 12:28:10Z nishi $ */
2
 
18 nishi 3
#define SOURCE
4
 
17 nishi 5
#include "tw_module.h"
6
 
7
#include "tw_config.h"
8
 
9
#include <cm_string.h>
10
#include <cm_log.h>
11
 
156 nishi 12
#include <string.h>
17 nishi 13
#include <unistd.h>
14
#include <stdlib.h>
15
 
16
#ifdef __MINGW32__
17
#include <windows.h>
18
#else
19
#include <dlfcn.h>
20
#endif
21
 
22
extern struct tw_config config;
23
 
24
void* tw_module_load(const char* path) {
25
	char* p = getcwd(NULL, 0);
26
	chdir(config.server_root);
27
	void* lib;
28
#ifdef __MINGW32__
29
	lib = LoadLibraryA(path);
30
#else
25 nishi 31
	lib = dlopen(path, RTLD_LAZY);
17 nishi 32
#endif
33
	if(lib == NULL) {
34
		cm_log("Module", "Could not load %s", path);
35
	}
36
	chdir(p);
37
	free(p);
38
	return lib;
39
}
40
 
41
void* tw_module_symbol(void* mod, const char* sym) {
42
#ifdef __MINGW32__
43
	return GetProcAddress(mod, sym);
44
#else
45
	return dlsym(mod, sym);
46
#endif
47
}
48
 
18 nishi 49
void tw_add_version(const char* string) {
50
	if(config.extension == NULL) {
51
		config.extension = cm_strcat(" ", string);
52
	} else {
53
		char* tmp = config.extension;
54
		config.extension = cm_strcat3(tmp, " ", string);
55
		free(tmp);
56
	}
57
}
17 nishi 58
 
156 nishi 59
void tw_add_define(const char* string) {
60
	int i;
61
	for(i = 0; config.defined[i] != NULL; i++)
62
		;
63
	config.defined[i] = cm_strdup(string);
64
	config.defined[i + 1] = NULL;
65
}
66
 
67
void tw_delete_define(const char* string) {
68
	int i;
69
	for(i = 0; config.defined[i] != NULL; i++) {
70
		if(strcmp(config.defined[i], string) == 0) {
71
			free(config.defined[i]);
72
			for(; config.defined[i] != NULL; i++) {
73
				config.defined[i] = config.defined[i + 1];
74
			}
75
		}
76
	}
77
}
78
 
18 nishi 79
void tw_init_tools(struct tw_tool* tools) {
80
	tools->log = cm_log;
81
	tools->add_version = tw_add_version;
156 nishi 82
	tools->add_define = tw_add_define;
18 nishi 83
}
84
 
17 nishi 85
int tw_module_init(void* mod) {
86
	tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
87
	if(mod_init == NULL) {
88
		cm_log("Module", "Could not init a module");
89
		return 1;
90
	} else {
91
		struct tw_tool tools;
92
		tw_init_tools(&tools);
93
		return mod_init(&config, &tools);
94
	}
95
}