7 #if defined(__GNUC__) || defined(__WATCOMC__) || defined(__WIN32__)
14 #if defined(__unix__) || defined(__APPLE__)
18 #if defined(__APPLE__)
19 #include <mach-o/dyld.h>
21 #if defined(__FreeBSD__)
22 #include <sys/sysctl.h>
27 #if defined(__WIN32__)
28 #define WIN32_LEAN_AND_MEAN
38 typedef unsigned char byte;
39 typedef unsigned short uint16;
40 typedef unsigned int uint;
41 typedef unsigned int FileSize;
42 typedef unsigned long long uint64;
47 #define null ((void *)0)
49 #define MAX_LOCATION 797
50 #define MAX_FILENAME 274
51 #define MAX_EXTENSION 17
53 FILE *eC_stdin(void) { return stdin; }
54 FILE *eC_stdout(void) { return stdout; }
55 FILE *eC_stderr(void) { return stderr; }
57 void __ecereNameSpace__ecere__com__eSystem_Delete(void * memory);
58 void * __ecereNameSpace__ecere__com__eSystem_New0(unsigned int size);
59 void * __ecereNameSpace__ecere__com__eSystem_Renew(void * memory, unsigned int size);
60 void * __ecereNameSpace__ecere__com__eSystem_Renew0(void * memory, unsigned int size);
61 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16(char * source, int * wordCount);
62 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(char * source, uint16 * dest, int max);
63 char * __ecereNameSpace__ecere__sys__UTF16toUTF8(uint16 * source);
64 char * __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(uint16 * source, byte * dest, int max);
66 #define eSystem_Delete __ecereNameSpace__ecere__com__eSystem_Delete
67 #define eSystem_New0 __ecereNameSpace__ecere__com__eSystem_New0
68 #define eSystem_Renew __ecereNameSpace__ecere__com__eSystem_Renew
69 #define eSystem_Renew0 __ecereNameSpace__ecere__com__eSystem_Renew0
70 #define UTF8toUTF16 __ecereNameSpace__ecere__sys__UTF8toUTF16
71 #define UTF8toUTF16Buffer __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer
72 #define UTF16toUTF8 __ecereNameSpace__ecere__sys__UTF16toUTF8
73 #define UTF16toUTF8Buffer __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer
76 static char exeLocation[MAX_LOCATION];
79 int __ecereNameSpace__ecere__sys__Tokenize(char * string, int maxTokens, char* tokens[], bool escapeBackSlashes);
80 char * __ecereNameSpace__ecere__sys__RSearchString(char * buffer, char * subStr, int maxLen, bool matchCase, bool matchWord);
81 char * __ecereNameSpace__ecere__sys__GetLastDirectory(char * string, char * output);
82 char * __ecereNameSpace__ecere__sys__PathCat(char * string, char * addedPath);
83 char * __ecereNameSpace__ecere__sys__GetExtension(char * string, char * output);
85 #define Tokenize __ecereNameSpace__ecere__sys__Tokenize
86 #define RSearchString __ecereNameSpace__ecere__sys__RSearchString
87 #define GetLastDirectory __ecereNameSpace__ecere__sys__GetLastDirectory
88 #define PathCat __ecereNameSpace__ecere__sys__PathCat
89 #define GetExtension __ecereNameSpace__ecere__sys__GetExtension
91 extern struct __ecereNameSpace__ecere__com__Instance * __thisModule;
93 typedef enum { unknown, win32, tux, apple } Platform;
95 #if defined(__WIN32__)
96 Platform runtimePlatform = win32;
97 #elif defined(__APPLE__)
98 Platform runtimePlatform = apple;
99 #else //if defined(__linux__)
100 Platform runtimePlatform = tux;
102 Platform runtimePlatform = unknown; // 'linux' is used as a catch all UNIX platform for now
106 #if !defined(ECERE_NOFILE) && defined(__unix__) && !defined(__linux__)
108 typedef struct _DualPipe _DualPipe;
110 void DualPipe_Destructor(_DualPipe * dp);
111 bool DualPipe_Getc(_DualPipe * dp, char * ch);
112 bool DualPipe_Eof(_DualPipe * dp);
113 void DualPipe_Wait(_DualPipe * dp);
115 _DualPipe * _DualPipeOpen(int mode, char * commandLine, char * env, void ** inputPtr, void ** outputPtr);
117 static bool DualPipe_GetLine(FILE * p, char *s, int max)
129 if((ch = fgetc(p)) == EOF)
141 return result || c > 1;
146 bool Instance_LocateModule(char * name, char * fileName)
148 #if defined(__WIN32__)
149 HMODULE hModule = null;
152 uint16 _wmoduleName[MAX_LOCATION];
153 UTF8toUTF16Buffer(name, _wmoduleName, MAX_LOCATION);
154 hModule = GetModuleHandle(_wmoduleName);
157 wcscat(_wmoduleName, L".exe");
158 hModule = GetModuleHandle(_wmoduleName);
162 uint16 _wfileName[MAX_LOCATION];
163 GetModuleFileNameW(hModule, _wfileName, MAX_LOCATION);
164 UTF16toUTF8Buffer(_wfileName, (byte *)fileName, MAX_LOCATION);
170 uint16 _wfileName[MAX_LOCATION];
171 GetModuleFileNameW(null, _wfileName, MAX_LOCATION);
172 UTF16toUTF8Buffer(_wfileName, (byte *)fileName, MAX_LOCATION);
175 #elif defined(__APPLE__)
178 int imageCount = _dyld_image_count();
180 int nameLen = strlen(name);
181 for(c = 0; c<imageCount; c++)
183 struct mach_header * header = _dyld_get_image_header(c);
184 char * path = _dyld_get_image_name(c);
185 int pathLen = strlen(path);
186 char * subStr = RSearchString(path, name, pathLen, false, false);
189 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
190 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".dylib", 6)))
192 strcpy(fileName, path);
200 int size = MAX_LOCATION;
201 _NSGetExecutablePath(fileName, &size);
204 #elif defined(__unix__)
205 //File f = FileOpen("/proc/self/maps", read);
207 char exeName[MAX_FILENAME];
209 #if defined(__linux__)
210 f = fopen("/proc/self/status", "r");
212 f = fopen("/proc/curproc/status", "r");
217 while(fgets(line, sizeof(line), f))
219 char * name = strstr(line, "Name:\t");
224 nameLen = strlen(name);
226 strcpy(exeName, name);
232 #if defined(__linux__)
233 f = fopen("/proc/self/maps", "r");
235 f = fopen("/proc/curproc/map", "r");
240 //while(f.GetLine(line, sizeof(line)))
241 while(fgets(line, sizeof(line), f))
243 char * path = strstr(line, "/");
246 int pathLen = strlen(path);
250 int nameLen = strlen(name);
252 subStr = RSearchString(path, name, pathLen, false, false);
255 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
256 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
258 char * space = strchr(path, ' ');
259 if(space) *space = 0;
260 strcpy(fileName, path);
268 char name[MAX_FILENAME];
269 GetLastDirectory(path, name);
270 if(!exeName[0] || !strcmp(name, exeName))
272 char * space = strchr(path, ' ');
273 if(space) *space = 0;
274 strcpy(fileName, path);
283 #if !defined(ECERE_NOFILE) && !defined(__linux__)
286 // Couldn't locate libraries with /proc/curmap/map, attempt with ldd
287 FILE * in = null, * out = null;
289 char command[MAX_LOCATION];
290 snprintf(command, sizeof(command), "ldd /proc/%d/file", (int)getpid());
291 p = _DualPipeOpen(1, command, null, &in, &out);
296 int nameLen = strlen(name);
297 while(DualPipe_GetLine(in, line, sizeof(line)))
299 char * path = strstr(line, "/");
302 int pathLen = strlen(path);
305 subStr = RSearchString(path, name, pathLen, false, false);
308 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
309 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
311 char * space = strchr(path, ' ');
312 if(space) *space = 0;
313 strcpy(fileName, path);
321 DualPipe_Destructor(p);
326 if(!name || !name[0])
328 #if defined(__FreeBSD__)
330 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
331 size_t cb = MAX_LOCATION;
333 sysctl(mib, 4, fileName, &cb, null, 0);
338 #if !defined(__linux__)
341 if(!access("/proc/curproc/file", F_OK))
343 strcpy(fileName, "/proc/curproc/file");
346 if((env = getenv("_")))
348 strcpy(fileName, env);
353 strcpy(fileName, exeLocation);
360 void Instance_COM_Initialize(int argc, char ** argv, char ** parsedCommand, int * argcPtr, char *** argvPtr)
362 #if !defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
363 // Disable stdout buffering on Unix
364 setvbuf(stdout, null, _IONBF, 0);
366 #if defined(__WIN32__)
367 *parsedCommand = UTF16toUTF8(GetCommandLineW());
368 *argvPtr = eSystem_New0(sizeof(char *) * 512);
369 *argcPtr = Tokenize(*parsedCommand, 512,(void*)(char **)(*argvPtr), false);
374 #if defined(__unix__)
375 if(!__thisModule && argv)
377 getcwd(exeLocation, MAX_LOCATION);
378 PathCat(exeLocation, argv[0]);
383 void * Instance_Module_Load(const char * libLocation, const char * name, void ** Load, void ** Unload)
385 char fileName[MAX_LOCATION];
386 char extension[MAX_EXTENSION];
387 void * library = null;
388 #if defined(__unix__) || defined(__APPLE__)
390 char * paths[] = { null, "/usr/lib/ec/lib", "/usr/lib32/ec/lib" };
396 #if defined(__WIN32__)
397 strcpy(fileName, name);
398 GetExtension(fileName, extension);
400 strcat(fileName, ".dll");
403 uint16 _wfileName[MAX_LOCATION];
404 UTF8toUTF16Buffer(fileName, _wfileName, MAX_LOCATION);
405 library = LoadLibraryW(_wfileName);
410 *Load = (void *)GetProcAddress(library, "__ecereDll_Load");
411 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload");
413 *Load = (void *)GetProcAddress(library, "__ecereDll_Load@4");
414 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload@4");
417 FreeLibrary(library);
419 #elif defined(__unix__) || defined(__APPLE__)
421 strcpy(fileName, libLocation);
423 strcpy(fileName, "lib");
424 strcat(fileName, name);
425 GetExtension(fileName, extension);
427 #if defined(__APPLE__)
428 strcat(fileName, ".dylib");
430 strcat(fileName, ".so");
433 library = dlopen(fileName, RTLD_LAZY);
434 while(!library && attempts < sizeof(paths)/sizeof(paths[0]))
437 strcpy(fileName, paths[attempts++]);
441 #ifdef DEB_HOST_MULTIARCH
442 strcpy(fileName, DEB_HOST_MULTIARCH);
443 strcat(fileName, "/ec/lib");
448 strcat(fileName, name);
449 GetExtension(fileName, extension);
451 #if defined(__APPLE__)
452 strcat(fileName, ".dylib");
454 strcat(fileName, ".so");
456 library = dlopen(fileName, RTLD_LAZY);
461 *Load = dlsym(library, "__ecereDll_Load");
462 *Unload = dlsym(library, "__ecereDll_Unload");
466 #elif defined(__APPLE__)
467 strcpy(fileName, "lib");
468 strcat(fileName, name);
469 GetExtension(fileName, extension);
471 strcat(fileName, ".dylib");
473 NSObjectFileImage *fileImage;
474 NSObjectFileImageReturnCode returnCode = NSCreateObjectFileImageFromFile(fileName, &fileImage);
476 if(returnCode == NSObjectFileImageSuccess)
478 printf("NSObjectFileImageSuccess!\n");
479 library = NSLinkModule(fileImage,fileName,
480 NSLINKMODULE_OPTION_RETURN_ON_ERROR
481 | NSLINKMODULE_OPTION_PRIVATE);
482 // NSDestroyObjectFileImage(fileImage);
485 *Load = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Load"));
486 *Unload = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Unload"));
489 NSUnLinkModule(library, 0);
492 printf("Found Load!\n");
496 printf("No Success :(\n");
502 void Instance_Module_Free(void * library)
504 #if defined(__WIN32__)
506 FreeLibrary(library);
507 #elif defined(__unix__) || defined(__APPLE__)
514 void InternalModuleLoadBreakpoint()