Subversion Repositories Tewi

Rev

Rev 156 | Rev 182 | 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 159 2024-09-25 12:56:45Z 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;
159 nishi 61
	for(i = 0; config.defined[i] != NULL; i++) {
62
		if(strcmp(config.defined[i], string) == 0) {
63
			return;
64
		}
65
	}
156 nishi 66
	for(i = 0; config.defined[i] != NULL; i++)
67
		;
68
	config.defined[i] = cm_strdup(string);
69
	config.defined[i + 1] = NULL;
70
}
71
 
72
void tw_delete_define(const char* string) {
73
	int i;
74
	for(i = 0; config.defined[i] != NULL; i++) {
75
		if(strcmp(config.defined[i], string) == 0) {
76
			free(config.defined[i]);
77
			for(; config.defined[i] != NULL; i++) {
78
				config.defined[i] = config.defined[i + 1];
79
			}
159 nishi 80
			break;
156 nishi 81
		}
82
	}
83
}
84
 
18 nishi 85
void tw_init_tools(struct tw_tool* tools) {
86
	tools->log = cm_log;
87
	tools->add_version = tw_add_version;
156 nishi 88
	tools->add_define = tw_add_define;
18 nishi 89
}
90
 
17 nishi 91
int tw_module_init(void* mod) {
92
	tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
93
	if(mod_init == NULL) {
94
		cm_log("Module", "Could not init a module");
95
		return 1;
96
	} else {
97
		struct tw_tool tools;
98
		tw_init_tools(&tools);
99
		return mod_init(&config, &tools);
100
	}
101
}