7 #if defined(__GNUC__) || defined(__WATCOMC__) || defined(__WIN32__)
14 #if defined(__unix__) || defined(__APPLE__)
18 #if defined(__FreeBSD__)
19 #include <sys/sysctl.h>
24 #if defined(__WIN32__)
25 #define WIN32_LEAN_AND_MEAN
35 typedef unsigned char byte;
36 typedef unsigned short uint16;
37 typedef unsigned int uint;
38 typedef unsigned int FileSize;
39 typedef unsigned long long uint64;
44 #define null ((void *)0)
46 #define MAX_LOCATION 797
47 #define MAX_FILENAME 274
48 #define MAX_EXTENSION 17
50 FILE *eC_stdin(void) { return stdin; }
51 FILE *eC_stdout(void) { return stdout; }
52 FILE *eC_stderr(void) { return stderr; }
54 void __ecereNameSpace__ecere__com__eSystem_Delete(void * memory);
55 void * __ecereNameSpace__ecere__com__eSystem_New0(unsigned int size);
56 void * __ecereNameSpace__ecere__com__eSystem_Renew(void * memory, unsigned int size);
57 void * __ecereNameSpace__ecere__com__eSystem_Renew0(void * memory, unsigned int size);
58 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16(char * source, int * wordCount);
59 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(char * source, uint16 * dest, int max);
60 char * __ecereNameSpace__ecere__sys__UTF16toUTF8(uint16 * source);
61 char * __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(uint16 * source, byte * dest, int max);
63 #define eSystem_Delete __ecereNameSpace__ecere__com__eSystem_Delete
64 #define eSystem_New0 __ecereNameSpace__ecere__com__eSystem_New0
65 #define eSystem_Renew __ecereNameSpace__ecere__com__eSystem_Renew
66 #define eSystem_Renew0 __ecereNameSpace__ecere__com__eSystem_Renew0
67 #define UTF8toUTF16 __ecereNameSpace__ecere__sys__UTF8toUTF16
68 #define UTF8toUTF16Buffer __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer
69 #define UTF16toUTF8 __ecereNameSpace__ecere__sys__UTF16toUTF8
70 #define UTF16toUTF8Buffer __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer
73 static char exeLocation[MAX_LOCATION];
76 int __ecereNameSpace__ecere__sys__Tokenize(char * string, int maxTokens, char* tokens[], bool escapeBackSlashes);
77 char * __ecereNameSpace__ecere__sys__RSearchString(char * buffer, char * subStr, int maxLen, bool matchCase, bool matchWord);
78 char * __ecereNameSpace__ecere__sys__GetLastDirectory(char * string, char * output);
79 char * __ecereNameSpace__ecere__sys__PathCat(char * string, char * addedPath);
80 char * __ecereNameSpace__ecere__sys__GetExtension(char * string, char * output);
82 #define Tokenize __ecereNameSpace__ecere__sys__Tokenize
83 #define RSearchString __ecereNameSpace__ecere__sys__RSearchString
84 #define GetLastDirectory __ecereNameSpace__ecere__sys__GetLastDirectory
85 #define PathCat __ecereNameSpace__ecere__sys__PathCat
86 #define GetExtension __ecereNameSpace__ecere__sys__GetExtension
88 extern struct __ecereNameSpace__ecere__com__Instance * __thisModule;
90 typedef enum { unknown, win32, tux, apple } Platform;
92 #if defined(__WIN32__)
93 Platform runtimePlatform = win32;
94 #elif defined(__APPLE__)
95 Platform runtimePlatform = apple;
96 #else //if defined(__linux__)
97 Platform runtimePlatform = tux;
99 Platform runtimePlatform = unknown; // 'linux' is used as a catch all UNIX platform for now
103 #if !defined(ECERE_NOFILE) && defined(__unix__)
105 typedef struct _DualPipe _DualPipe;
107 void DualPipe_Destructor(_DualPipe * dp);
108 bool DualPipe_Getc(_DualPipe * dp, char * ch);
109 bool DualPipe_Eof(_DualPipe * dp);
110 void DualPipe_Wait(_DualPipe * dp);
112 _DualPipe * _DualPipeOpen(int mode, char * commandLine, char * env, void ** inputPtr, void ** outputPtr);
114 static bool DualPipe_GetLine(FILE * p, char *s, int max)
126 if((ch = fgetc(p)) == EOF)
138 return result || c > 1;
143 bool Instance_LocateModule(char * name, char * fileName)
145 #if defined(__WIN32__)
146 HMODULE hModule = null;
149 uint16 _wmoduleName[MAX_LOCATION];
150 UTF8toUTF16Buffer(name, _wmoduleName, MAX_LOCATION);
151 hModule = GetModuleHandle(_wmoduleName);
154 wcscat(_wmoduleName, L".exe");
155 hModule = GetModuleHandle(_wmoduleName);
159 uint16 _wfileName[MAX_LOCATION];
160 GetModuleFileNameW(hModule, _wfileName, MAX_LOCATION);
161 UTF16toUTF8Buffer(_wfileName, (byte *)fileName, MAX_LOCATION);
167 uint16 _wfileName[MAX_LOCATION];
168 GetModuleFileNameW(null, _wfileName, MAX_LOCATION);
169 UTF16toUTF8Buffer(_wfileName, (byte *)fileName, MAX_LOCATION);
172 #elif defined(__APPLE__)
175 int imageCount = _dyld_image_count();
177 int nameLen = strlen(name);
178 for(c = 0; c<imageCount; c++)
180 struct mach_header * header = _dyld_get_image_header(c);
181 char * path = _dyld_get_image_name(c);
182 int pathLen = strlen(path);
183 char * subStr = RSearchString(path, name, pathLen, false, false);
186 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
187 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".dylib", 6)))
189 strcpy(fileName, path);
197 int size = MAX_LOCATION;
198 _NSGetExecutablePath(fileName, &size);
201 #elif defined(__unix__)
202 //File f = FileOpen("/proc/self/maps", read);
204 char exeName[MAX_FILENAME];
206 #if defined(__linux__)
207 f = fopen("/proc/self/status", "r");
209 f = fopen("/proc/curproc/status", "r");
214 while(fgets(line, sizeof(line), f))
216 char * name = strstr(line, "Name:\t");
221 nameLen = strlen(name);
223 strcpy(exeName, name);
229 #if defined(__linux__)
230 f = fopen("/proc/self/maps", "r");
232 f = fopen("/proc/curproc/map", "r");
237 //while(f.GetLine(line, sizeof(line)))
238 while(fgets(line, sizeof(line), f))
240 char * path = strstr(line, "/");
243 int pathLen = strlen(path);
247 int nameLen = strlen(name);
249 subStr = RSearchString(path, name, pathLen, false, false);
252 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
253 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
255 char * space = strchr(path, ' ');
256 if(space) *space = 0;
257 strcpy(fileName, path);
265 char name[MAX_FILENAME];
266 GetLastDirectory(path, name);
267 if(!exeName[0] || !strcmp(name, exeName))
269 char * space = strchr(path, ' ');
270 if(space) *space = 0;
271 strcpy(fileName, path);
280 #if !defined(ECERE_NOFILE) && !defined(__linux__)
283 // Couldn't locate libraries with /proc/curmap/map, attempt with ldd
284 FILE * in = null, * out = null;
286 char command[MAX_LOCATION];
287 snprintf(command, sizeof(command), "ldd /proc/%d/file", (int)getpid());
288 p = _DualPipeOpen(1, command, null, &in, &out);
293 int nameLen = strlen(name);
294 while(DualPipe_GetLine(in, line, sizeof(line)))
296 char * path = strstr(line, "/");
299 int pathLen = strlen(path);
302 subStr = RSearchString(path, name, pathLen, false, false);
305 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
306 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
308 char * space = strchr(path, ' ');
309 if(space) *space = 0;
310 strcpy(fileName, path);
318 DualPipe_Destructor(p);
323 if(!name || !name[0])
326 #if defined(__FreeBSD__)
328 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
329 size_t cb = MAX_LOCATION;
331 sysctl(mib, 4, fileName, &cb, null, 0);
336 #if !defined(__linux__)
337 if(!access("/proc/curproc/file", F_OK))
339 strcpy(fileName, "/proc/curproc/file");
342 if((env = getenv("_")))
344 strcpy(fileName, env);
348 strcpy(fileName, exeLocation);
355 void Instance_COM_Initialize(int argc, char ** argv, char ** parsedCommand, int * argcPtr, char *** argvPtr)
357 #if !defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
358 // Disable stdout buffering on Unix
359 setvbuf(stdout, null, _IONBF, 0);
361 #if defined(__WIN32__)
362 *parsedCommand = UTF16toUTF8(GetCommandLineW());
363 *argvPtr = eSystem_New0(sizeof(char *) * 512);
364 *argcPtr = Tokenize(*parsedCommand, 512,(void*)(char **)(*argvPtr), false);
369 #if defined(__unix__)
370 if(!__thisModule && argv)
372 getcwd(exeLocation, MAX_LOCATION);
373 PathCat(exeLocation, argv[0]);
378 void * Instance_Module_Load(const char * libLocation, const char * name, void ** Load, void ** Unload)
380 char fileName[MAX_LOCATION];
381 char extension[MAX_EXTENSION];
382 void * library = null;
383 #if defined(__unix__) || defined(__APPLE__)
385 char * paths[] = { null, "/usr/lib/ec/lib", "/usr/lib32/ec/lib" };
391 #if defined(__WIN32__)
392 strcpy(fileName, name);
393 GetExtension(fileName, extension);
395 strcat(fileName, ".dll");
398 uint16 _wfileName[MAX_LOCATION];
399 UTF8toUTF16Buffer(fileName, _wfileName, MAX_LOCATION);
400 library = LoadLibraryW(_wfileName);
405 *Load = (void *)GetProcAddress(library, "__ecereDll_Load");
406 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload");
408 *Load = (void *)GetProcAddress(library, "__ecereDll_Load@4");
409 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload@4");
412 FreeLibrary(library);
414 #elif defined(__unix__) || defined(__APPLE__)
416 strcpy(fileName, libLocation);
418 strcpy(fileName, "lib");
419 strcat(fileName, name);
420 GetExtension(fileName, extension);
422 #if defined(__APPLE__)
423 strcat(fileName, ".dylib");
425 strcat(fileName, ".so");
428 library = dlopen(fileName, RTLD_LAZY);
429 while(!library && attempts < sizeof(paths)/sizeof(paths[0]))
432 strcpy(fileName, paths[attempts++]);
436 #ifdef DEB_HOST_MULTIARCH
437 strcpy(fileName, DEB_HOST_MULTIARCH);
438 strcat(fileName, "/ec/lib");
443 strcat(fileName, name);
444 GetExtension(fileName, extension);
446 #if defined(__APPLE__)
447 strcat(fileName, ".dylib");
449 strcat(fileName, ".so");
451 library = dlopen(fileName, RTLD_LAZY);
456 *Load = dlsym(library, "__ecereDll_Load");
457 *Unload = dlsym(library, "__ecereDll_Unload");
461 #elif defined(__APPLE__)
462 strcpy(fileName, "lib");
463 strcat(fileName, name);
464 GetExtension(fileName, extension);
466 strcat(fileName, ".dylib");
468 NSObjectFileImage *fileImage;
469 NSObjectFileImageReturnCode returnCode = NSCreateObjectFileImageFromFile(fileName, &fileImage);
471 if(returnCode == NSObjectFileImageSuccess)
473 printf("NSObjectFileImageSuccess!\n");
474 library = NSLinkModule(fileImage,fileName,
475 NSLINKMODULE_OPTION_RETURN_ON_ERROR
476 | NSLINKMODULE_OPTION_PRIVATE);
477 // NSDestroyObjectFileImage(fileImage);
480 *Load = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Load"));
481 *Unload = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Unload"));
484 NSUnLinkModule(library, 0);
487 printf("Found Load!\n");
491 printf("No Success :(\n");
497 void Instance_Module_Free(void * library)
499 #if defined(__WIN32__)
501 FreeLibrary(library);
502 #elif defined(__unix__) || defined(__APPLE__)
509 void InternalModuleLoadBreakpoint()