7 #if defined(__GNUC__) || defined(__WATCOMC__) || defined(__WIN32__)
14 #if defined(__unix__) || defined(__APPLE__)
17 #if !defined(__pnacl__)
20 #if defined(__APPLE__)
21 #include <mach-o/dyld.h>
23 #if defined(__FreeBSD__)
24 #include <sys/sysctl.h>
29 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
30 #define WIN32_LEAN_AND_MEAN
40 typedef unsigned char byte;
41 typedef unsigned short uint16;
42 typedef unsigned int uint;
43 typedef unsigned int FileSize;
44 typedef unsigned long long uint64;
49 #define null ((void *)0)
51 #define MAX_LOCATION 797
52 #define MAX_FILENAME 274
53 #define MAX_EXTENSION 17
55 #if defined(__MINGW32__) && !defined(_W64) && __GNUC__ < 4
56 __declspec(dllexport) int isblank(int c) { return c == '\t' || c == ' '; }
59 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
60 intptr_t stdinHandle, stdoutHandle;
61 int osfStdin, osfStdout;
62 FILE * fStdIn, * fStdOut;
65 FILE *eC_stdin(void) {
66 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
69 stdinHandle = (intptr_t)GetStdHandle(STD_INPUT_HANDLE);
70 osfStdin = _open_osfhandle(stdinHandle, _O_TEXT);
72 fStdIn = _fdopen( osfStdin, "rb");
75 setvbuf( fStdIn, NULL, _IONBF, 0 );
85 #if 0 //defined(__WIN32__)
88 stdoutHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
89 osfStdout = _open_osfhandle(stdoutHandle, _O_TEXT);
91 fStdOut = _fdopen( osfStdout, "wb");
94 setvbuf( fStdOut, NULL, _IONBF, 0 );
101 FILE *eC_stderr(void) { return stderr; }
103 void __ecereNameSpace__ecere__com__eSystem_Delete(void * memory);
104 void * __ecereNameSpace__ecere__com__eSystem_New0(unsigned int size);
105 void * __ecereNameSpace__ecere__com__eSystem_Renew(void * memory, unsigned int size);
106 void * __ecereNameSpace__ecere__com__eSystem_Renew0(void * memory, unsigned int size);
107 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16(const char * source, int * wordCount);
108 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(const char * source, uint16 * dest, int max);
109 char * __ecereNameSpace__ecere__sys__UTF16toUTF8(uint16 * source);
110 char * __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(uint16 * source, char * dest, int max);
112 #define eSystem_Delete __ecereNameSpace__ecere__com__eSystem_Delete
113 #define eSystem_New0 __ecereNameSpace__ecere__com__eSystem_New0
114 #define eSystem_Renew __ecereNameSpace__ecere__com__eSystem_Renew
115 #define eSystem_Renew0 __ecereNameSpace__ecere__com__eSystem_Renew0
116 #define UTF8toUTF16 __ecereNameSpace__ecere__sys__UTF8toUTF16
117 #define UTF8toUTF16Buffer __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer
118 #define UTF16toUTF8 __ecereNameSpace__ecere__sys__UTF16toUTF8
119 #define UTF16toUTF8Buffer __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer
121 #if defined(__unix__)
122 static char exeLocation[MAX_LOCATION];
125 #define forArgsPassing 2
126 int __ecereNameSpace__ecere__sys__Tokenize(char * string, int maxTokens, char* tokens[], int esc);
127 char * __ecereNameSpace__ecere__sys__RSearchString(const char * buffer, const char * subStr, int maxLen, bool matchCase, bool matchWord);
128 char * __ecereNameSpace__ecere__sys__GetLastDirectory(const char * string, char * output);
129 char * __ecereNameSpace__ecere__sys__PathCat(char * string, const char * addedPath);
130 char * __ecereNameSpace__ecere__sys__GetExtension(const char * string, char * output);
132 #define Tokenize __ecereNameSpace__ecere__sys__Tokenize
133 #define RSearchString __ecereNameSpace__ecere__sys__RSearchString
134 #define GetLastDirectory __ecereNameSpace__ecere__sys__GetLastDirectory
135 #define PathCat __ecereNameSpace__ecere__sys__PathCat
136 #define GetExtension __ecereNameSpace__ecere__sys__GetExtension
138 extern struct __ecereNameSpace__ecere__com__Instance * __thisModule;
140 typedef enum { unknown, win32, tux, apple } Platform;
142 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
143 Platform runtimePlatform = win32;
144 #elif defined(__APPLE__)
145 Platform runtimePlatform = apple;
146 #else //if defined(__linux__)
147 Platform runtimePlatform = tux;
149 Platform runtimePlatform = unknown; // 'linux' is used as a catch all UNIX platform for now
153 #if !defined(ECERE_NOFILE) && defined(__unix__) && !defined(__linux__)
155 typedef struct _DualPipe _DualPipe;
157 void DualPipe_Destructor(_DualPipe * dp);
158 bool DualPipe_Getc(_DualPipe * dp, char * ch);
159 bool DualPipe_Eof(_DualPipe * dp);
160 void DualPipe_Wait(_DualPipe * dp);
162 _DualPipe * _DualPipeOpen(int mode, char * commandLine, char * env, void ** inputPtr, void ** outputPtr);
164 static bool DualPipe_GetLine(FILE * p, char *s, int max)
176 if((ch = fgetc(p)) == EOF)
188 return result || c > 1;
193 bool Instance_LocateModule(const char * name, char * fileName)
195 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
196 HMODULE hModule = null;
199 uint16 _wmoduleName[MAX_LOCATION];
200 UTF8toUTF16Buffer(name, _wmoduleName, MAX_LOCATION);
201 hModule = GetModuleHandle(_wmoduleName);
204 wcscat(_wmoduleName, L".exe");
205 hModule = GetModuleHandle(_wmoduleName);
209 uint16 _wfileName[MAX_LOCATION];
210 GetModuleFileNameW(hModule, _wfileName, MAX_LOCATION);
211 UTF16toUTF8Buffer(_wfileName, fileName, MAX_LOCATION);
217 uint16 _wfileName[MAX_LOCATION];
218 GetModuleFileNameW(null, _wfileName, MAX_LOCATION);
219 UTF16toUTF8Buffer(_wfileName, fileName, MAX_LOCATION);
222 #elif defined(__APPLE__)
225 int imageCount = _dyld_image_count();
227 int nameLen = strlen(name);
228 for(c = 0; c<imageCount; c++)
230 struct mach_header * header = _dyld_get_image_header(c);
231 char * path = _dyld_get_image_name(c);
232 int pathLen = strlen(path);
233 char * subStr = RSearchString(path, name, pathLen, false, false);
236 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
237 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".dylib", 6)))
239 strcpy(fileName, path);
247 int size = MAX_LOCATION;
248 _NSGetExecutablePath(fileName, &size);
251 #elif defined(__unix__)
252 //File f = FileOpen("/proc/self/maps", read);
254 char exeName[MAX_FILENAME];
256 #if defined(__linux__)
257 f = fopen("/proc/self/status", "r");
259 f = fopen("/proc/curproc/status", "r");
264 while(fgets(line, sizeof(line), f))
266 char * name = strstr(line, "Name:\t");
271 nameLen = strlen(name);
273 strcpy(exeName, name);
279 #if defined(__linux__)
280 f = fopen("/proc/self/maps", "r");
282 f = fopen("/proc/curproc/map", "r");
287 //while(f.GetLine(line, sizeof(line)))
288 while(fgets(line, sizeof(line), f))
290 char * path = strstr(line, "/");
293 int pathLen = strlen(path);
297 int nameLen = strlen(name);
299 subStr = RSearchString(path, name, pathLen, false, false);
302 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
303 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
305 char * space = strchr(path, ' ');
306 if(space) *space = 0;
307 strcpy(fileName, path);
315 char name[MAX_FILENAME];
316 GetLastDirectory(path, name);
317 if(!exeName[0] || !strcmp(name, exeName))
319 char * space = strchr(path, ' ');
320 if(space) *space = 0;
321 strcpy(fileName, path);
330 #if !defined(ECERE_NOFILE) && !defined(__linux__) && !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
333 // Couldn't locate libraries with /proc/curmap/map, attempt with ldd
334 FILE * in = null, * out = null;
336 char command[MAX_LOCATION];
337 snprintf(command, sizeof(command), "ldd /proc/%d/file", (int)getpid());
338 p = _DualPipeOpen(1, command, null, &in, &out);
343 int nameLen = strlen(name);
344 while(DualPipe_GetLine(in, line, sizeof(line)))
346 char * path = strstr(line, "/");
349 int pathLen = strlen(path);
352 subStr = RSearchString(path, name, pathLen, false, false);
355 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
356 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
358 char * space = strchr(path, ' ');
359 if(space) *space = 0;
360 strcpy(fileName, path);
368 DualPipe_Destructor(p);
373 if(!name || !name[0])
375 #if defined(__FreeBSD__)
377 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
378 size_t cb = MAX_LOCATION;
380 sysctl(mib, 4, fileName, &cb, null, 0);
385 #if !defined(__linux__)
388 if(!access("/proc/curproc/file", F_OK))
390 strcpy(fileName, "/proc/curproc/file");
393 if((env = getenv("_")))
395 strcpy(fileName, env);
400 strcpy(fileName, exeLocation);
407 void Instance_COM_Initialize(int argc, char ** argv, char ** parsedCommand, int * argcPtr, const char *** argvPtr)
409 #if !defined(__WIN32__) && !defined(__EMSCRIPTEN__) && !defined(ECERE_BOOTSTRAP) && !defined(__pnacl__)
410 // Disable stdout buffering on Unix
411 setvbuf(stdout, null, _IONBF, 0);
413 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
414 *parsedCommand = UTF16toUTF8(GetCommandLineW());
415 *argvPtr = eSystem_New0(sizeof(char *) * 512);
416 *argcPtr = Tokenize(*parsedCommand, 512,(void*)(char **)(*argvPtr), forArgsPassing);
419 *argvPtr = (const char **)argv;
421 #if defined(__unix__)
422 if(!__thisModule && argv)
424 if(!getcwd(exeLocation, MAX_LOCATION))
426 PathCat(exeLocation, argv[0]);
431 void * Instance_Module_Load(const char * libLocation, const char * name, void ** Load, void ** Unload)
433 char fileName[MAX_LOCATION];
434 char extension[MAX_EXTENSION];
435 void * library = null;
436 #if defined(__unix__) || defined(__APPLE__)
438 char * paths[] = { null, "/usr/lib/ec/lib", "/usr/lib32/ec/lib" };
444 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
445 strcpy(fileName, name);
446 GetExtension(fileName, extension);
448 strcat(fileName, ".dll");
451 uint16 _wfileName[MAX_LOCATION];
452 UTF8toUTF16Buffer(fileName, _wfileName, MAX_LOCATION);
453 library = LoadLibraryW(_wfileName);
458 *Load = (void *)GetProcAddress(library, "__ecereDll_Load");
459 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload");
461 *Load = (void *)GetProcAddress(library, "__ecereDll_Load@4");
462 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload@4");
465 FreeLibrary(library);
467 #elif defined(__unix__) || defined(__APPLE__)
468 if(libLocation || strchr(name, '/'))
469 strcpy(fileName, libLocation ? libLocation : "");
471 strcpy(fileName, "lib");
472 strcat(fileName, name);
473 GetExtension(fileName, extension);
475 #if defined(__APPLE__)
476 strcat(fileName, ".dylib");
478 strcat(fileName, ".so");
481 #if !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
482 library = dlopen(fileName, RTLD_LAZY);
484 while(!library && attempts < sizeof(paths)/sizeof(paths[0]))
487 strcpy(fileName, paths[attempts++]);
491 #ifdef DEB_HOST_MULTIARCH
492 strcpy(fileName, DEB_HOST_MULTIARCH);
493 strcat(fileName, "/ec/lib");
498 strcat(fileName, name);
499 GetExtension(fileName, extension);
501 #if defined(__APPLE__)
502 strcat(fileName, ".dylib");
504 strcat(fileName, ".so");
506 #if !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
507 library = dlopen(fileName, RTLD_LAZY);
513 *Load = dlsym(library, "__ecereDll_Load");
514 *Unload = dlsym(library, "__ecereDll_Unload");
515 #if !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
520 #elif defined(__APPLE__)
521 if(libLocation || strchr(name, '/'))
522 strcpy(fileName, libLocation ? libLocation : "");
524 strcpy(fileName, "lib");
525 strcat(fileName, name);
526 GetExtension(fileName, extension);
528 strcat(fileName, ".dylib");
530 NSObjectFileImage *fileImage;
531 NSObjectFileImageReturnCode returnCode = NSCreateObjectFileImageFromFile(fileName, &fileImage);
533 if(returnCode == NSObjectFileImageSuccess)
535 printf("NSObjectFileImageSuccess!\n");
536 library = NSLinkModule(fileImage,fileName,
537 NSLINKMODULE_OPTION_RETURN_ON_ERROR
538 | NSLINKMODULE_OPTION_PRIVATE);
539 // NSDestroyObjectFileImage(fileImage);
542 *Load = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Load"));
543 *Unload = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Unload"));
546 NSUnLinkModule(library, 0);
549 printf("Found Load!\n");
553 printf("No Success :(\n");
559 void Instance_Module_Free(void * library)
561 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
563 FreeLibrary(library);
564 #elif (defined(__unix__) || defined(__APPLE__)) && !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
571 void InternalModuleLoadBreakpoint()
578 bool Float_isNan(float n) { return isnan(n); }
579 bool Float_isInf(float n) { return isinf(n); }
580 int Float_signBit(float n) { return signbit(n); }
581 float Float_nan(void) { return NAN; }
582 float Float_inf(void) { return INFINITY; }
584 bool Double_isNan(double n) { return isnan(n); }
585 bool Double_isInf(double n) { return isinf(n); }
586 int Double_signBit(double n) { return signbit(n); }
587 double Double_nan(void) { return NAN; }
588 double Double_inf(void) { return INFINITY; }