source: Main/trunk/Server/module.c

Last change on this file was 402, checked in by Nishi, on Nov 3, 2024 at 7:39:59 PM

amiga

  • Property svn:keywords set to Id
File size: 3.3 KB
RevLine 
[17]1/* $Id: module.c 402 2024-11-03 10:39:59Z nishi $ */
2
[18]3#define SOURCE
4
[17]5#include "tw_module.h"
6
7#include "tw_config.h"
8
9#include <cm_string.h>
10#include <cm_log.h>
11
[156]12#include <string.h>
[212]13#include <stdlib.h>
[215]14#if !defined(_MSC_VER) && !defined(__BORLANDC__)
[17]15#include <unistd.h>
[212]16#endif
[17]17
[182]18extern struct tw_config config;
19
[402]20#if defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(__NeXT__) || defined(__DOS__) || defined(__amiga__)
[182]21void* tw_module_load(const char* path) { return NULL; }
22
23void* tw_module_symbol(void* mod, const char* sym) { return NULL; }
24
25int tw_module_init(void* mod) { return 1; }
26
27#else
28
[219]29#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[312]30#ifdef __OS2__
31#define INCL_DOSMODULEMGR
[313]32#define INCL_DOSERRORS
[312]33#include <os2.h>
[315]34#elif defined(__NETWARE__)
[349]35#include <nwadv.h>
36#include <nwthread.h>
[312]37#else
[17]38#include <windows.h>
[217]39#include <direct.h>
[312]40#endif
[17]41#else
42#include <dlfcn.h>
43#endif
44
45void* tw_module_load(const char* path) {
46 char* p = getcwd(NULL, 0);
[212]47 void* lib;
[312]48 char tmp[512];
[313]49#ifdef __OS2__
50 HMODULE mod;
[315]51#elif defined(__NETWARE__)
52 unsigned int* hnd = malloc(sizeof(*hnd));
[313]53#endif
[17]54 chdir(config.server_root);
[219]55#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[312]56#ifdef __OS2__
[315]57 if(DosLoadModule(tmp, 512, path, &mod) != NO_ERROR) {
[313]58 return NULL;
59 }
60 lib = (void*)mod;
[315]61#elif defined(__NETWARE__)
[349]62 *hnd = FindNLMHandle(path);
[366]63 if(*hnd == 0){
64 free(hnd);
65 hnd = NULL;
66 }
[349]67 lib = (void*)hnd;
[312]68#else
[17]69 lib = LoadLibraryA(path);
[312]70#endif
[17]71#else
[25]72 lib = dlopen(path, RTLD_LAZY);
[17]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
82void* tw_module_symbol(void* mod, const char* sym) {
[219]83#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[312]84#ifdef __OS2__
85 void* ret;
[313]86 APIRET rc;
[315]87 if((rc = DosQueryProcAddr((HMODULE)mod, 0, sym, (PFN*)&ret)) != NO_ERROR) {
[313]88 cm_log("Module", "OS/2 error %d", (int)rc);
89 return NULL;
90 }
[312]91 return ret;
[315]92#elif defined(__NETWARE__)
[349]93 return ImportSymbol(*(unsigned int*)mod, sym);
[312]94#else
[17]95 return GetProcAddress(mod, sym);
[312]96#endif
[17]97#else
98 return dlsym(mod, sym);
99#endif
100}
101
[182]102int 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]105 cm_log("Module", "Could not find a init call");
[182]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]115void 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]124
[156]125void tw_add_define(const char* string) {
126 int i;
[159]127 for(i = 0; config.defined[i] != NULL; i++) {
128 if(strcmp(config.defined[i], string) == 0) {
129 return;
130 }
131 }
[156]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
138void 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]146 break;
[156]147 }
148 }
149}
150
[18]151void tw_init_tools(struct tw_tool* tools) {
152 tools->log = cm_log;
153 tools->add_version = tw_add_version;
[156]154 tools->add_define = tw_add_define;
[18]155}
Note: See TracBrowser for help on using the repository browser.