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 #if defined(__WIN32__)
54 intptr_t stdinHandle, stdoutHandle;
55 int osfStdin, osfStdout;
56 FILE * fStdIn, * fStdOut;
59 FILE *eC_stdin(void) {
60 #if defined(__WIN32__)
63 stdinHandle = (intptr_t)GetStdHandle(STD_INPUT_HANDLE);
64 osfStdin = _open_osfhandle(stdinHandle, _O_TEXT);
66 fStdIn = _fdopen( osfStdin, "rb");
69 setvbuf( fStdIn, NULL, _IONBF, 0 );
79 #if 0 //defined(__WIN32__)
82 stdoutHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
83 osfStdout = _open_osfhandle(stdoutHandle, _O_TEXT);
85 fStdOut = _fdopen( osfStdout, "wb");
88 setvbuf( fStdOut, NULL, _IONBF, 0 );
95 FILE *eC_stderr(void) { return stderr; }
97 void __ecereNameSpace__ecere__com__eSystem_Delete(void * memory);
98 void * __ecereNameSpace__ecere__com__eSystem_New0(unsigned int size);
99 void * __ecereNameSpace__ecere__com__eSystem_Renew(void * memory, unsigned int size);
100 void * __ecereNameSpace__ecere__com__eSystem_Renew0(void * memory, unsigned int size);
101 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16(char * source, int * wordCount);
102 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(char * source, uint16 * dest, int max);
103 char * __ecereNameSpace__ecere__sys__UTF16toUTF8(uint16 * source);
104 char * __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(uint16 * source, byte * dest, int max);
106 #define eSystem_Delete __ecereNameSpace__ecere__com__eSystem_Delete
107 #define eSystem_New0 __ecereNameSpace__ecere__com__eSystem_New0
108 #define eSystem_Renew __ecereNameSpace__ecere__com__eSystem_Renew
109 #define eSystem_Renew0 __ecereNameSpace__ecere__com__eSystem_Renew0
110 #define UTF8toUTF16 __ecereNameSpace__ecere__sys__UTF8toUTF16
111 #define UTF8toUTF16Buffer __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer
112 #define UTF16toUTF8 __ecereNameSpace__ecere__sys__UTF16toUTF8
113 #define UTF16toUTF8Buffer __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer
115 #if defined(__unix__)
116 static char exeLocation[MAX_LOCATION];
119 #define forArgsPassing 2
120 int __ecereNameSpace__ecere__sys__Tokenize(char * string, int maxTokens, char* tokens[], int esc);
121 char * __ecereNameSpace__ecere__sys__RSearchString(char * buffer, char * subStr, int maxLen, bool matchCase, bool matchWord);
122 char * __ecereNameSpace__ecere__sys__GetLastDirectory(char * string, char * output);
123 char * __ecereNameSpace__ecere__sys__PathCat(char * string, char * addedPath);
124 char * __ecereNameSpace__ecere__sys__GetExtension(char * string, char * output);
126 #define Tokenize __ecereNameSpace__ecere__sys__Tokenize
127 #define RSearchString __ecereNameSpace__ecere__sys__RSearchString
128 #define GetLastDirectory __ecereNameSpace__ecere__sys__GetLastDirectory
129 #define PathCat __ecereNameSpace__ecere__sys__PathCat
130 #define GetExtension __ecereNameSpace__ecere__sys__GetExtension
132 extern struct __ecereNameSpace__ecere__com__Instance * __thisModule;
134 typedef enum { unknown, win32, tux, apple } Platform;
136 #if defined(__WIN32__)
137 Platform runtimePlatform = win32;
138 #elif defined(__APPLE__)
139 Platform runtimePlatform = apple;
140 #else //if defined(__linux__)
141 Platform runtimePlatform = tux;
143 Platform runtimePlatform = unknown; // 'linux' is used as a catch all UNIX platform for now
147 #if !defined(ECERE_NOFILE) && defined(__unix__) && !defined(__linux__)
149 typedef struct _DualPipe _DualPipe;
151 void DualPipe_Destructor(_DualPipe * dp);
152 bool DualPipe_Getc(_DualPipe * dp, char * ch);
153 bool DualPipe_Eof(_DualPipe * dp);
154 void DualPipe_Wait(_DualPipe * dp);
156 _DualPipe * _DualPipeOpen(int mode, char * commandLine, char * env, void ** inputPtr, void ** outputPtr);
158 static bool DualPipe_GetLine(FILE * p, char *s, int max)
170 if((ch = fgetc(p)) == EOF)
182 return result || c > 1;
187 bool Instance_LocateModule(char * name, char * fileName)
189 #if defined(__WIN32__)
190 HMODULE hModule = null;
193 uint16 _wmoduleName[MAX_LOCATION];
194 UTF8toUTF16Buffer(name, _wmoduleName, MAX_LOCATION);
195 hModule = GetModuleHandle(_wmoduleName);
198 wcscat(_wmoduleName, L".exe");
199 hModule = GetModuleHandle(_wmoduleName);
203 uint16 _wfileName[MAX_LOCATION];
204 GetModuleFileNameW(hModule, _wfileName, MAX_LOCATION);
205 UTF16toUTF8Buffer(_wfileName, (byte *)fileName, MAX_LOCATION);
211 uint16 _wfileName[MAX_LOCATION];
212 GetModuleFileNameW(null, _wfileName, MAX_LOCATION);
213 UTF16toUTF8Buffer(_wfileName, (byte *)fileName, MAX_LOCATION);
216 #elif defined(__APPLE__)
219 int imageCount = _dyld_image_count();
221 int nameLen = strlen(name);
222 for(c = 0; c<imageCount; c++)
224 struct mach_header * header = _dyld_get_image_header(c);
225 char * path = _dyld_get_image_name(c);
226 int pathLen = strlen(path);
227 char * subStr = RSearchString(path, name, pathLen, false, false);
230 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
231 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".dylib", 6)))
233 strcpy(fileName, path);
241 int size = MAX_LOCATION;
242 _NSGetExecutablePath(fileName, &size);
245 #elif defined(__unix__)
246 //File f = FileOpen("/proc/self/maps", read);
248 char exeName[MAX_FILENAME];
250 #if defined(__linux__)
251 f = fopen("/proc/self/status", "r");
253 f = fopen("/proc/curproc/status", "r");
258 while(fgets(line, sizeof(line), f))
260 char * name = strstr(line, "Name:\t");
265 nameLen = strlen(name);
267 strcpy(exeName, name);
273 #if defined(__linux__)
274 f = fopen("/proc/self/maps", "r");
276 f = fopen("/proc/curproc/map", "r");
281 //while(f.GetLine(line, sizeof(line)))
282 while(fgets(line, sizeof(line), f))
284 char * path = strstr(line, "/");
287 int pathLen = strlen(path);
291 int nameLen = strlen(name);
293 subStr = RSearchString(path, name, pathLen, false, false);
296 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
297 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
299 char * space = strchr(path, ' ');
300 if(space) *space = 0;
301 strcpy(fileName, path);
309 char name[MAX_FILENAME];
310 GetLastDirectory(path, name);
311 if(!exeName[0] || !strcmp(name, exeName))
313 char * space = strchr(path, ' ');
314 if(space) *space = 0;
315 strcpy(fileName, path);
324 #if !defined(ECERE_NOFILE) && !defined(__linux__)
327 // Couldn't locate libraries with /proc/curmap/map, attempt with ldd
328 FILE * in = null, * out = null;
330 char command[MAX_LOCATION];
331 snprintf(command, sizeof(command), "ldd /proc/%d/file", (int)getpid());
332 p = _DualPipeOpen(1, command, null, &in, &out);
337 int nameLen = strlen(name);
338 while(DualPipe_GetLine(in, line, sizeof(line)))
340 char * path = strstr(line, "/");
343 int pathLen = strlen(path);
346 subStr = RSearchString(path, name, pathLen, false, false);
349 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
350 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
352 char * space = strchr(path, ' ');
353 if(space) *space = 0;
354 strcpy(fileName, path);
362 DualPipe_Destructor(p);
367 if(!name || !name[0])
369 #if defined(__FreeBSD__)
371 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
372 size_t cb = MAX_LOCATION;
374 sysctl(mib, 4, fileName, &cb, null, 0);
379 #if !defined(__linux__)
382 if(!access("/proc/curproc/file", F_OK))
384 strcpy(fileName, "/proc/curproc/file");
387 if((env = getenv("_")))
389 strcpy(fileName, env);
394 strcpy(fileName, exeLocation);
401 void Instance_COM_Initialize(int argc, char ** argv, char ** parsedCommand, int * argcPtr, char *** argvPtr)
403 #if !defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
404 // Disable stdout buffering on Unix
405 setvbuf(stdout, null, _IONBF, 0);
407 #if defined(__WIN32__)
408 *parsedCommand = UTF16toUTF8(GetCommandLineW());
409 *argvPtr = eSystem_New0(sizeof(char *) * 512);
410 *argcPtr = Tokenize(*parsedCommand, 512,(void*)(char **)(*argvPtr), forArgsPassing);
415 #if defined(__unix__)
416 if(!__thisModule && argv)
418 getcwd(exeLocation, MAX_LOCATION);
419 PathCat(exeLocation, argv[0]);
424 void * Instance_Module_Load(const char * libLocation, const char * name, void ** Load, void ** Unload)
426 char fileName[MAX_LOCATION];
427 char extension[MAX_EXTENSION];
428 void * library = null;
429 #if defined(__unix__) || defined(__APPLE__)
431 char * paths[] = { null, "/usr/lib/ec/lib", "/usr/lib32/ec/lib" };
437 #if defined(__WIN32__)
438 strcpy(fileName, name);
439 GetExtension(fileName, extension);
441 strcat(fileName, ".dll");
444 uint16 _wfileName[MAX_LOCATION];
445 UTF8toUTF16Buffer(fileName, _wfileName, MAX_LOCATION);
446 library = LoadLibraryW(_wfileName);
451 *Load = (void *)GetProcAddress(library, "__ecereDll_Load");
452 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload");
454 *Load = (void *)GetProcAddress(library, "__ecereDll_Load@4");
455 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload@4");
458 FreeLibrary(library);
460 #elif defined(__unix__) || defined(__APPLE__)
462 strcpy(fileName, libLocation);
464 strcpy(fileName, "lib");
465 strcat(fileName, name);
466 GetExtension(fileName, extension);
468 #if defined(__APPLE__)
469 strcat(fileName, ".dylib");
471 strcat(fileName, ".so");
474 library = dlopen(fileName, RTLD_LAZY);
475 while(!library && attempts < sizeof(paths)/sizeof(paths[0]))
478 strcpy(fileName, paths[attempts++]);
482 #ifdef DEB_HOST_MULTIARCH
483 strcpy(fileName, DEB_HOST_MULTIARCH);
484 strcat(fileName, "/ec/lib");
489 strcat(fileName, name);
490 GetExtension(fileName, extension);
492 #if defined(__APPLE__)
493 strcat(fileName, ".dylib");
495 strcat(fileName, ".so");
497 library = dlopen(fileName, RTLD_LAZY);
502 *Load = dlsym(library, "__ecereDll_Load");
503 *Unload = dlsym(library, "__ecereDll_Unload");
507 #elif defined(__APPLE__)
508 strcpy(fileName, "lib");
509 strcat(fileName, name);
510 GetExtension(fileName, extension);
512 strcat(fileName, ".dylib");
514 NSObjectFileImage *fileImage;
515 NSObjectFileImageReturnCode returnCode = NSCreateObjectFileImageFromFile(fileName, &fileImage);
517 if(returnCode == NSObjectFileImageSuccess)
519 printf("NSObjectFileImageSuccess!\n");
520 library = NSLinkModule(fileImage,fileName,
521 NSLINKMODULE_OPTION_RETURN_ON_ERROR
522 | NSLINKMODULE_OPTION_PRIVATE);
523 // NSDestroyObjectFileImage(fileImage);
526 *Load = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Load"));
527 *Unload = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Unload"));
530 NSUnLinkModule(library, 0);
533 printf("Found Load!\n");
537 printf("No Success :(\n");
543 void Instance_Module_Free(void * library)
545 #if defined(__WIN32__)
547 FreeLibrary(library);
548 #elif defined(__unix__) || defined(__APPLE__)
555 void InternalModuleLoadBreakpoint()