source: Main/trunk/Server/module.c@ 341

Last change on this file since 341 was 339, checked in by Nishi, on Oct 15, 2024 at 3:22:34 AM

trying to add nextstep support

  • Property svn:keywords set to Id
File size: 3.2 KB
RevLine 
[17]1/* $Id: module.c 339 2024-10-14 18:22:34Z 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
[339]20#if defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(__NeXT__)
[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__)
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__)
62 *hnd = FindNLMHandle(path);
63 lib = (void*)hnd;
[312]64#else
[17]65 lib = LoadLibraryA(path);
[312]66#endif
[17]67#else
[25]68 lib = dlopen(path, RTLD_LAZY);
[17]69#endif
70 if(lib == NULL) {
71 cm_log("Module", "Could not load %s", path);
72 }
73 chdir(p);
74 free(p);
75 return lib;
76}
77
78void* tw_module_symbol(void* mod, const char* sym) {
[219]79#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
[312]80#ifdef __OS2__
81 void* ret;
[313]82 APIRET rc;
[315]83 if((rc = DosQueryProcAddr((HMODULE)mod, 0, sym, (PFN*)&ret)) != NO_ERROR) {
[313]84 cm_log("Module", "OS/2 error %d", (int)rc);
85 return NULL;
86 }
[312]87 return ret;
[315]88#elif defined(__NETWARE__)
89 return ImportSymbol(*(unsigned int*)mod, sym);
[312]90#else
[17]91 return GetProcAddress(mod, sym);
[312]92#endif
[17]93#else
94 return dlsym(mod, sym);
95#endif
96}
97
[182]98int tw_module_init(void* mod) {
99 tw_mod_init_t mod_init = (tw_mod_init_t)tw_module_symbol(mod, "mod_init");
100 if(mod_init == NULL) {
[313]101 cm_log("Module", "Could not find a init call");
[182]102 return 1;
103 } else {
104 struct tw_tool tools;
105 tw_init_tools(&tools);
106 return mod_init(&config, &tools);
107 }
108}
109#endif
110
[18]111void tw_add_version(const char* string) {
112 if(config.extension == NULL) {
113 config.extension = cm_strcat(" ", string);
114 } else {
115 char* tmp = config.extension;
116 config.extension = cm_strcat3(tmp, " ", string);
117 free(tmp);
118 }
119}
[17]120
[156]121void tw_add_define(const char* string) {
122 int i;
[159]123 for(i = 0; config.defined[i] != NULL; i++) {
124 if(strcmp(config.defined[i], string) == 0) {
125 return;
126 }
127 }
[156]128 for(i = 0; config.defined[i] != NULL; i++)
129 ;
130 config.defined[i] = cm_strdup(string);
131 config.defined[i + 1] = NULL;
132}
133
134void tw_delete_define(const char* string) {
135 int i;
136 for(i = 0; config.defined[i] != NULL; i++) {
137 if(strcmp(config.defined[i], string) == 0) {
138 free(config.defined[i]);
139 for(; config.defined[i] != NULL; i++) {
140 config.defined[i] = config.defined[i + 1];
141 }
[159]142 break;
[156]143 }
144 }
145}
146
[18]147void tw_init_tools(struct tw_tool* tools) {
148 tools->log = cm_log;
149 tools->add_version = tw_add_version;
[156]150 tools->add_define = tw_add_define;
[18]151}
Note: See TracBrowser for help on using the repository browser.