Subversion Repositories Tewi

Rev

Rev 366 | 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 402 2024-11-03 10:39:59Z 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>
212 nishi 13
#include <stdlib.h>
215 nishi 14
#if !defined(_MSC_VER) && !defined(__BORLANDC__)
17 nishi 15
#include <unistd.h>
212 nishi 16
#endif
17 nishi 17
 
182 nishi 18
extern struct tw_config config;
19
 
402 nishi 20
#if defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(__NeXT__) || defined(__DOS__) || defined(__amiga__)
182 nishi 21
void* tw_module_load(const char* path) { return NULL; }
22
 
23
void* tw_module_symbol(void* mod, const char* sym) { return NULL; }
24
 
25
int tw_module_init(void* mod) { return 1; }
26
 
27
#else
28
 
219 nishi 29
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
312 nishi 30
#ifdef __OS2__
31
#define INCL_DOSMODULEMGR
313 nishi 32
#define INCL_DOSERRORS
312 nishi 33
#include <os2.h>
315 nishi 34
#elif defined(__NETWARE__)
349 nishi 35
#include <nwadv.h>
36
#include <nwthread.h>
312 nishi 37
#else
17 nishi 38
#include <windows.h>
217 nishi 39
#include <direct.h>
312 nishi 40
#endif
17 nishi 41
#else
42
#include <dlfcn.h>
43
#endif
44
 
45
void* tw_module_load(const char* path) {
46
	char* p = getcwd(NULL, 0);
212 nishi 47
	void* lib;
312 nishi 48
	char tmp[512];
313 nishi 49
#ifdef __OS2__
50
	HMODULE mod;
315 nishi 51
#elif defined(__NETWARE__)
52
	unsigned int* hnd = malloc(sizeof(*hnd));
313 nishi 53
#endif
17 nishi 54
	chdir(config.server_root);
219 nishi 55
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
312 nishi 56
#ifdef __OS2__
315 nishi 57
	if(DosLoadModule(tmp, 512, path, &mod) != NO_ERROR) {
313 nishi 58
		return NULL;
59
	}
60
	lib = (void*)mod;
315 nishi 61
#elif defined(__NETWARE__)
349 nishi 62
	*hnd = FindNLMHandle(path);
366 nishi 63
	if(*hnd == 0){
64
		free(hnd);
65
		hnd = NULL;
66
	}
349 nishi 67
	lib = (void*)hnd;
312 nishi 68
#else
17 nishi 69
	lib = LoadLibraryA(path);
312 nishi 70
#endif
17 nishi 71
#else
25 nishi 72
	lib = dlopen(path, RTLD_LAZY);
17 nishi 73
#endif
74
	if(lib == NULL) {
75
		cm_log("Module", "Could not load %s", path);
76
	}
77
	chdir(p);
78
	free(p);
79
	return lib;
80
}
81
 
82
void* tw_module_symbol(void* mod, const char* sym) {
219 nishi 83
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
312 nishi 84
#ifdef __OS2__
85
	void* ret;
313 nishi 86
	APIRET rc;
315 nishi 87
	if((rc = DosQueryProcAddr((HMODULE)mod, 0, sym, (PFN*)&ret)) != NO_ERROR) {
313 nishi 88
		cm_log("Module", "OS/2 error %d", (int)rc);
89
		return NULL;
90
	}
312 nishi 91
	return ret;
315 nishi 92
#elif defined(__NETWARE__)
349 nishi 93
	return ImportSymbol(*(unsigned int*)mod, sym);
312 nishi 94
#else
17 nishi 95
	return GetProcAddress(mod, sym);
312 nishi 96
#endif
17 nishi 97
#else
98
	return dlsym(mod, sym);
99
#endif
100
}
101
 
182 nishi 102
int tw_module_init(void* mod) {
103
	tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
104
	if(mod_init == NULL) {
313 nishi 105
		cm_log("Module", "Could not find a init call");
182 nishi 106
		return 1;
107
	} else {
108
		struct tw_tool tools;
109
		tw_init_tools(&tools);
110
		return mod_init(&config, &tools);
111
	}
112
}
113
#endif
114
 
18 nishi 115
void tw_add_version(const char* string) {
116
	if(config.extension == NULL) {
117
		config.extension = cm_strcat(" ", string);
118
	} else {
119
		char* tmp = config.extension;
120
		config.extension = cm_strcat3(tmp, " ", string);
121
		free(tmp);
122
	}
123
}
17 nishi 124
 
156 nishi 125
void tw_add_define(const char* string) {
126
	int i;
159 nishi 127
	for(i = 0; config.defined[i] != NULL; i++) {
128
		if(strcmp(config.defined[i], string) == 0) {
129
			return;
130
		}
131
	}
156 nishi 132
	for(i = 0; config.defined[i] != NULL; i++)
133
		;
134
	config.defined[i] = cm_strdup(string);
135
	config.defined[i + 1] = NULL;
136
}
137
 
138
void tw_delete_define(const char* string) {
139
	int i;
140
	for(i = 0; config.defined[i] != NULL; i++) {
141
		if(strcmp(config.defined[i], string) == 0) {
142
			free(config.defined[i]);
143
			for(; config.defined[i] != NULL; i++) {
144
				config.defined[i] = config.defined[i + 1];
145
			}
159 nishi 146
			break;
156 nishi 147
		}
148
	}
149
}
150
 
18 nishi 151
void tw_init_tools(struct tw_tool* tools) {
152
	tools->log = cm_log;
153
	tools->add_version = tw_add_version;
156 nishi 154
	tools->add_define = tw_add_define;
18 nishi 155
}