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 #define forArgsPassing 2
80 int __ecereNameSpace__ecere__sys__Tokenize(char * string, int maxTokens, char* tokens[], int esc);
81 char * __ecereNameSpace__ecere__sys__RSearchString(char * buffer, char * subStr, int maxLen, bool matchCase, bool matchWord);
82 char * __ecereNameSpace__ecere__sys__GetLastDirectory(char * string, char * output);
83 char * __ecereNameSpace__ecere__sys__PathCat(char * string, char * addedPath);
84 char * __ecereNameSpace__ecere__sys__GetExtension(char * string, char * output);
86 #define Tokenize __ecereNameSpace__ecere__sys__Tokenize
87 #define RSearchString __ecereNameSpace__ecere__sys__RSearchString
88 #define GetLastDirectory __ecereNameSpace__ecere__sys__GetLastDirectory
89 #define PathCat __ecereNameSpace__ecere__sys__PathCat
90 #define GetExtension __ecereNameSpace__ecere__sys__GetExtension
92 extern struct __ecereNameSpace__ecere__com__Instance * __thisModule;
94 typedef enum { unknown, win32, tux, apple } Platform;
96 #if defined(__WIN32__)
97 Platform runtimePlatform = win32;
98 #elif defined(__APPLE__)
99 Platform runtimePlatform = apple;
100 #else //if defined(__linux__)
101 Platform runtimePlatform = tux;
103 Platform runtimePlatform = unknown; // 'linux' is used as a catch all UNIX platform for now
107 #if !defined(ECERE_NOFILE) && defined(__unix__) && !defined(__linux__)
109 typedef struct _DualPipe _DualPipe;
111 void DualPipe_Destructor(_DualPipe * dp);
112 bool DualPipe_Getc(_DualPipe * dp, char * ch);
113 bool DualPipe_Eof(_DualPipe * dp);
114 void DualPipe_Wait(_DualPipe * dp);
116 _DualPipe * _DualPipeOpen(int mode, char * commandLine, char * env, void ** inputPtr, void ** outputPtr);
118 static bool DualPipe_GetLine(FILE * p, char *s, int max)
130 if((ch = fgetc(p)) == EOF)
142 return result || c > 1;
147 bool Instance_LocateModule(char * name, char * fileName)
149 #if defined(__WIN32__)
150 HMODULE hModule = null;
153 uint16 _wmoduleName[MAX_LOCATION];
154 UTF8toUTF16Buffer(name, _wmoduleName, MAX_LOCATION);
155 hModule = GetModuleHandle(_wmoduleName);
158 wcscat(_wmoduleName, L".exe");
159 hModule = GetModuleHandle(_wmoduleName);
163 uint16 _wfileName[MAX_LOCATION];
164 GetModuleFileNameW(hModule, _wfileName, MAX_LOCATION);
165 UTF16toUTF8Buffer(_wfileName, (byte *)fileName, MAX_LOCATION);
171 uint16 _wfileName[MAX_LOCATION];
172 GetModuleFileNameW(null, _wfileName, MAX_LOCATION);
173 UTF16toUTF8Buffer(_wfileName, (byte *)fileName, MAX_LOCATION);
176 #elif defined(__APPLE__)
179 int imageCount = _dyld_image_count();
181 int nameLen = strlen(name);
182 for(c = 0; c<imageCount; c++)
184 struct mach_header * header = _dyld_get_image_header(c);
185 char * path = _dyld_get_image_name(c);
186 int pathLen = strlen(path);
187 char * subStr = RSearchString(path, name, pathLen, false, false);
190 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
191 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".dylib", 6)))
193 strcpy(fileName, path);
201 int size = MAX_LOCATION;
202 _NSGetExecutablePath(fileName, &size);
205 #elif defined(__unix__)
206 //File f = FileOpen("/proc/self/maps", read);
208 char exeName[MAX_FILENAME];
210 #if defined(__linux__)
211 f = fopen("/proc/self/status", "r");
213 f = fopen("/proc/curproc/status", "r");
218 while(fgets(line, sizeof(line), f))
220 char * name = strstr(line, "Name:\t");
225 nameLen = strlen(name);
227 strcpy(exeName, name);
233 #if defined(__linux__)
234 f = fopen("/proc/self/maps", "r");
236 f = fopen("/proc/curproc/map", "r");
241 //while(f.GetLine(line, sizeof(line)))
242 while(fgets(line, sizeof(line), f))
244 char * path = strstr(line, "/");
247 int pathLen = strlen(path);
251 int nameLen = strlen(name);
253 subStr = RSearchString(path, name, pathLen, false, false);
256 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
257 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
259 char * space = strchr(path, ' ');
260 if(space) *space = 0;
261 strcpy(fileName, path);
269 char name[MAX_FILENAME];
270 GetLastDirectory(path, name);
271 if(!exeName[0] || !strcmp(name, exeName))
273 char * space = strchr(path, ' ');
274 if(space) *space = 0;
275 strcpy(fileName, path);
284 #if !defined(ECERE_NOFILE) && !defined(__linux__)
287 // Couldn't locate libraries with /proc/curmap/map, attempt with ldd
288 FILE * in = null, * out = null;
290 char command[MAX_LOCATION];
291 snprintf(command, sizeof(command), "ldd /proc/%d/file", (int)getpid());
292 p = _DualPipeOpen(1, command, null, &in, &out);
297 int nameLen = strlen(name);
298 while(DualPipe_GetLine(in, line, sizeof(line)))
300 char * path = strstr(line, "/");
303 int pathLen = strlen(path);
306 subStr = RSearchString(path, name, pathLen, false, false);
309 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
310 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
312 char * space = strchr(path, ' ');
313 if(space) *space = 0;
314 strcpy(fileName, path);
322 DualPipe_Destructor(p);
327 if(!name || !name[0])
329 #if defined(__FreeBSD__)
331 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
332 size_t cb = MAX_LOCATION;
334 sysctl(mib, 4, fileName, &cb, null, 0);
339 #if !defined(__linux__)
342 if(!access("/proc/curproc/file", F_OK))
344 strcpy(fileName, "/proc/curproc/file");
347 if((env = getenv("_")))
349 strcpy(fileName, env);
354 strcpy(fileName, exeLocation);
361 void Instance_COM_Initialize(int argc, char ** argv, char ** parsedCommand, int * argcPtr, char *** argvPtr)
363 #if !defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
364 // Disable stdout buffering on Unix
365 setvbuf(stdout, null, _IONBF, 0);
367 #if defined(__WIN32__)
368 *parsedCommand = UTF16toUTF8(GetCommandLineW());
369 *argvPtr = eSystem_New0(sizeof(char *) * 512);
370 *argcPtr = Tokenize(*parsedCommand, 512,(void*)(char **)(*argvPtr), forArgsPassing);
375 #if defined(__unix__)
376 if(!__thisModule && argv)
378 getcwd(exeLocation, MAX_LOCATION);
379 PathCat(exeLocation, argv[0]);
384 void * Instance_Module_Load(const char * libLocation, const char * name, void ** Load, void ** Unload)
386 char fileName[MAX_LOCATION];
387 char extension[MAX_EXTENSION];
388 void * library = null;
389 #if defined(__unix__) || defined(__APPLE__)
391 char * paths[] = { null, "/usr/lib/ec/lib", "/usr/lib32/ec/lib" };
397 #if defined(__WIN32__)
398 strcpy(fileName, name);
399 GetExtension(fileName, extension);
401 strcat(fileName, ".dll");
404 uint16 _wfileName[MAX_LOCATION];
405 UTF8toUTF16Buffer(fileName, _wfileName, MAX_LOCATION);
406 library = LoadLibraryW(_wfileName);
411 *Load = (void *)GetProcAddress(library, "__ecereDll_Load");
412 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload");
414 *Load = (void *)GetProcAddress(library, "__ecereDll_Load@4");
415 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload@4");
418 FreeLibrary(library);
420 #elif defined(__unix__) || defined(__APPLE__)
422 strcpy(fileName, libLocation);
424 strcpy(fileName, "lib");
425 strcat(fileName, name);
426 GetExtension(fileName, extension);
428 #if defined(__APPLE__)
429 strcat(fileName, ".dylib");
431 strcat(fileName, ".so");
434 library = dlopen(fileName, RTLD_LAZY);
435 while(!library && attempts < sizeof(paths)/sizeof(paths[0]))
438 strcpy(fileName, paths[attempts++]);
442 #ifdef DEB_HOST_MULTIARCH
443 strcpy(fileName, DEB_HOST_MULTIARCH);
444 strcat(fileName, "/ec/lib");
449 strcat(fileName, name);
450 GetExtension(fileName, extension);
452 #if defined(__APPLE__)
453 strcat(fileName, ".dylib");
455 strcat(fileName, ".so");
457 library = dlopen(fileName, RTLD_LAZY);
462 *Load = dlsym(library, "__ecereDll_Load");
463 *Unload = dlsym(library, "__ecereDll_Unload");
467 #elif defined(__APPLE__)
468 strcpy(fileName, "lib");
469 strcat(fileName, name);
470 GetExtension(fileName, extension);
472 strcat(fileName, ".dylib");
474 NSObjectFileImage *fileImage;
475 NSObjectFileImageReturnCode returnCode = NSCreateObjectFileImageFromFile(fileName, &fileImage);
477 if(returnCode == NSObjectFileImageSuccess)
479 printf("NSObjectFileImageSuccess!\n");
480 library = NSLinkModule(fileImage,fileName,
481 NSLINKMODULE_OPTION_RETURN_ON_ERROR
482 | NSLINKMODULE_OPTION_PRIVATE);
483 // NSDestroyObjectFileImage(fileImage);
486 *Load = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Load"));
487 *Unload = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Unload"));
490 NSUnLinkModule(library, 0);
493 printf("Found Load!\n");
497 printf("No Success :(\n");
503 void Instance_Module_Free(void * library)
505 #if defined(__WIN32__)
507 FreeLibrary(library);
508 #elif defined(__unix__) || defined(__APPLE__)
515 void InternalModuleLoadBreakpoint()