Subversion Repositories Tewi

Rev

Rev 402 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 nishi 1
/* $Id: module.c 416 2024-11-19 07:54:58Z 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
416 nishi 54
#ifndef WINCE
17 nishi 55
	chdir(config.server_root);
416 nishi 56
#endif
219 nishi 57
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
312 nishi 58
#ifdef __OS2__
315 nishi 59
	if(DosLoadModule(tmp, 512, path, &mod) != NO_ERROR) {
313 nishi 60
		return NULL;
61
	}
62
	lib = (void*)mod;
315 nishi 63
#elif defined(__NETWARE__)
349 nishi 64
	*hnd = FindNLMHandle(path);
366 nishi 65
	if(*hnd == 0){
66
		free(hnd);
67
		hnd = NULL;
68
	}
349 nishi 69
	lib = (void*)hnd;
312 nishi 70
#else
416 nishi 71
	lib = LoadLibrary(path);
312 nishi 72
#endif
17 nishi 73
#else
25 nishi 74
	lib = dlopen(path, RTLD_LAZY);
17 nishi 75
#endif
76
	if(lib == NULL) {
77
		cm_log("Module", "Could not load %s", path);
78
	}
416 nishi 79
#ifndef WINCE
17 nishi 80
	chdir(p);
416 nishi 81
#endif
17 nishi 82
	free(p);
83
	return lib;
84
}
85
 
86
void* tw_module_symbol(void* mod, const char* sym) {
219 nishi 87
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
312 nishi 88
#ifdef __OS2__
89
	void* ret;
313 nishi 90
	APIRET rc;
315 nishi 91
	if((rc = DosQueryProcAddr((HMODULE)mod, 0, sym, (PFN*)&ret)) != NO_ERROR) {
313 nishi 92
		cm_log("Module", "OS/2 error %d", (int)rc);
93
		return NULL;
94
	}
312 nishi 95
	return ret;
315 nishi 96
#elif defined(__NETWARE__)
349 nishi 97
	return ImportSymbol(*(unsigned int*)mod, sym);
416 nishi 98
#elif defined(WINCE)
99
	return GetProcAddressW(mod, sym);
312 nishi 100
#else
17 nishi 101
	return GetProcAddress(mod, sym);
312 nishi 102
#endif
17 nishi 103
#else
104
	return dlsym(mod, sym);
105
#endif
106
}
107
 
182 nishi 108
int tw_module_init(void* mod) {
109
	tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
110
	if(mod_init == NULL) {
313 nishi 111
		cm_log("Module", "Could not find a init call");
182 nishi 112
		return 1;
113
	} else {
114
		struct tw_tool tools;
115
		tw_init_tools(&tools);
116
		return mod_init(&config, &tools);
117
	}
118
}
119
#endif
120
 
18 nishi 121
void tw_add_version(const char* string) {
122
	if(config.extension == NULL) {
123
		config.extension = cm_strcat(" ", string);
124
	} else {
125
		char* tmp = config.extension;
126
		config.extension = cm_strcat3(tmp, " ", string);
127
		free(tmp);
128
	}
129
}
17 nishi 130
 
156 nishi 131
void tw_add_define(const char* string) {
132
	int i;
159 nishi 133
	for(i = 0; config.defined[i] != NULL; i++) {
134
		if(strcmp(config.defined[i], string) == 0) {
135
			return;
136
		}
137
	}
156 nishi 138
	for(i = 0; config.defined[i] != NULL; i++)
139
		;
140
	config.defined[i] = cm_strdup(string);
141
	config.defined[i + 1] = NULL;
142
}
143
 
144
void tw_delete_define(const char* string) {
145
	int i;
146
	for(i = 0; config.defined[i] != NULL; i++) {
147
		if(strcmp(config.defined[i], string) == 0) {
148
			free(config.defined[i]);
149
			for(; config.defined[i] != NULL; i++) {
150
				config.defined[i] = config.defined[i + 1];
151
			}
159 nishi 152
			break;
156 nishi 153
		}
154
	}
155
}
156
 
18 nishi 157
void tw_init_tools(struct tw_tool* tools) {
158
	tools->log = cm_log;
159
	tools->add_version = tw_add_version;
156 nishi 160
	tools->add_define = tw_add_define;
18 nishi 161
}