/[tewi]/Server/module.c
ViewVC logotype

Contents of /Server/module.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations)
Tue Nov 19 07:55:13 2024 UTC (5 months, 1 week ago) by nishi
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +8 -2 lines
Content type: text/x-c
compiles for windows ce

1 /* $Id: module.c 416 2024-11-19 07:54:58Z nishi $ */
2
3 #define SOURCE
4
5 #include "tw_module.h"
6
7 #include "tw_config.h"
8
9 #include <cm_string.h>
10 #include <cm_log.h>
11
12 #include <string.h>
13 #include <stdlib.h>
14 #if !defined(_MSC_VER) && !defined(__BORLANDC__)
15 #include <unistd.h>
16 #endif
17
18 extern struct tw_config config;
19
20 #if defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(__NeXT__) || defined(__DOS__) || defined(__amiga__)
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
29 #if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
30 #ifdef __OS2__
31 #define INCL_DOSMODULEMGR
32 #define INCL_DOSERRORS
33 #include <os2.h>
34 #elif defined(__NETWARE__)
35 #include <nwadv.h>
36 #include <nwthread.h>
37 #else
38 #include <windows.h>
39 #include <direct.h>
40 #endif
41 #else
42 #include <dlfcn.h>
43 #endif
44
45 void* tw_module_load(const char* path) {
46 char* p = getcwd(NULL, 0);
47 void* lib;
48 char tmp[512];
49 #ifdef __OS2__
50 HMODULE mod;
51 #elif defined(__NETWARE__)
52 unsigned int* hnd = malloc(sizeof(*hnd));
53 #endif
54 #ifndef WINCE
55 chdir(config.server_root);
56 #endif
57 #if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
58 #ifdef __OS2__
59 if(DosLoadModule(tmp, 512, path, &mod) != NO_ERROR) {
60 return NULL;
61 }
62 lib = (void*)mod;
63 #elif defined(__NETWARE__)
64 *hnd = FindNLMHandle(path);
65 if(*hnd == 0){
66 free(hnd);
67 hnd = NULL;
68 }
69 lib = (void*)hnd;
70 #else
71 lib = LoadLibrary(path);
72 #endif
73 #else
74 lib = dlopen(path, RTLD_LAZY);
75 #endif
76 if(lib == NULL) {
77 cm_log("Module", "Could not load %s", path);
78 }
79 #ifndef WINCE
80 chdir(p);
81 #endif
82 free(p);
83 return lib;
84 }
85
86 void* tw_module_symbol(void* mod, const char* sym) {
87 #if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
88 #ifdef __OS2__
89 void* ret;
90 APIRET rc;
91 if((rc = DosQueryProcAddr((HMODULE)mod, 0, sym, (PFN*)&ret)) != NO_ERROR) {
92 cm_log("Module", "OS/2 error %d", (int)rc);
93 return NULL;
94 }
95 return ret;
96 #elif defined(__NETWARE__)
97 return ImportSymbol(*(unsigned int*)mod, sym);
98 #elif defined(WINCE)
99 return GetProcAddressW(mod, sym);
100 #else
101 return GetProcAddress(mod, sym);
102 #endif
103 #else
104 return dlsym(mod, sym);
105 #endif
106 }
107
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) {
111 cm_log("Module", "Could not find a init call");
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
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 }
130
131 void tw_add_define(const char* string) {
132 int i;
133 for(i = 0; config.defined[i] != NULL; i++) {
134 if(strcmp(config.defined[i], string) == 0) {
135 return;
136 }
137 }
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 }
152 break;
153 }
154 }
155 }
156
157 void tw_init_tools(struct tw_tool* tools) {
158 tools->log = cm_log;
159 tools->add_version = tw_add_version;
160 tools->add_define = tw_add_define;
161 }

nishi@yakumo.dev
ViewVC Help
Powered by ViewVC 1.3.0-dev