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, emscripten, pnacl } Platform;
142 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
143 Platform runtimePlatform = win32;
144 #elif defined(__APPLE__)
145 Platform runtimePlatform = apple;
146 #elif defined(__EMSCRIPTEN__) // (EMSCRIPTEN)
147 Platform runtimePlatform = emscripten;
148 #else //elif defined(__linux__)
149 Platform runtimePlatform = tux;
151 Platform runtimePlatform = unknown; // 'linux' is used as a catch all UNIX platform for now
155 #if !defined(ECERE_NOFILE) && defined(__unix__) && !defined(__linux__)
157 typedef struct _DualPipe _DualPipe;
159 void DualPipe_Destructor(_DualPipe * dp);
160 bool DualPipe_Getc(_DualPipe * dp, char * ch);
161 bool DualPipe_Eof(_DualPipe * dp);
162 void DualPipe_Wait(_DualPipe * dp);
164 _DualPipe * _DualPipeOpen(int mode, char * commandLine, char * env, void ** inputPtr, void ** outputPtr);
166 static bool DualPipe_GetLine(FILE * p, char *s, int max)
178 if((ch = fgetc(p)) == EOF)
190 return result || c > 1;
195 bool Instance_LocateModule(const char * name, char * fileName)
197 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
198 HMODULE hModule = null;
201 uint16 _wmoduleName[MAX_LOCATION];
202 UTF8toUTF16Buffer(name, _wmoduleName, MAX_LOCATION);
203 hModule = GetModuleHandle(_wmoduleName);
206 wcscat(_wmoduleName, L".exe");
207 hModule = GetModuleHandle(_wmoduleName);
211 uint16 _wfileName[MAX_LOCATION];
212 GetModuleFileNameW(hModule, _wfileName, MAX_LOCATION);
213 UTF16toUTF8Buffer(_wfileName, fileName, MAX_LOCATION);
219 uint16 _wfileName[MAX_LOCATION];
220 GetModuleFileNameW(null, _wfileName, MAX_LOCATION);
221 UTF16toUTF8Buffer(_wfileName, fileName, MAX_LOCATION);
224 #elif defined(__APPLE__)
227 int imageCount = _dyld_image_count();
229 int nameLen = strlen(name);
230 for(c = 0; c<imageCount; c++)
232 struct mach_header * header = _dyld_get_image_header(c);
233 char * path = _dyld_get_image_name(c);
234 int pathLen = strlen(path);
235 char * subStr = RSearchString(path, name, pathLen, false, false);
238 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
239 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".dylib", 6)))
241 strcpy(fileName, path);
249 int size = MAX_LOCATION;
250 _NSGetExecutablePath(fileName, &size);
253 #elif defined(__unix__)
254 //File f = FileOpen("/proc/self/maps", read);
256 char exeName[MAX_FILENAME];
258 #if defined(__linux__)
259 f = fopen("/proc/self/status", "r");
261 f = fopen("/proc/curproc/status", "r");
266 while(fgets(line, sizeof(line), f))
268 char * name = strstr(line, "Name:\t");
273 nameLen = strlen(name);
275 strcpy(exeName, name);
281 #if defined(__linux__)
282 f = fopen("/proc/self/maps", "r");
284 f = fopen("/proc/curproc/map", "r");
289 //while(f.GetLine(line, sizeof(line)))
290 while(fgets(line, sizeof(line), f))
292 char * path = strstr(line, "/");
295 int pathLen = strlen(path);
299 int nameLen = strlen(name);
301 subStr = RSearchString(path, name, pathLen, false, false);
304 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
305 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
307 char * space = strchr(path, ' ');
308 if(space) *space = 0;
309 strcpy(fileName, path);
317 char name[MAX_FILENAME];
318 GetLastDirectory(path, name);
319 if(!exeName[0] || !strcmp(name, exeName))
321 char * space = strchr(path, ' ');
322 if(space) *space = 0;
323 strcpy(fileName, path);
332 #if !defined(ECERE_NOFILE) && !defined(__linux__) && !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
335 // Couldn't locate libraries with /proc/curmap/map, attempt with ldd
336 FILE * in = null, * out = null;
338 char command[MAX_LOCATION];
339 snprintf(command, sizeof(command), "ldd /proc/%d/file", (int)getpid());
340 p = _DualPipeOpen(1, command, null, &in, &out);
345 int nameLen = strlen(name);
346 while(DualPipe_GetLine(in, line, sizeof(line)))
348 char * path = strstr(line, "/");
351 int pathLen = strlen(path);
354 subStr = RSearchString(path, name, pathLen, false, false);
357 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
358 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
360 char * space = strchr(path, ' ');
361 if(space) *space = 0;
362 strcpy(fileName, path);
370 DualPipe_Destructor(p);
375 if(!name || !name[0])
377 #if defined(__FreeBSD__)
379 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
380 size_t cb = MAX_LOCATION;
382 sysctl(mib, 4, fileName, &cb, null, 0);
387 #if !defined(__linux__)
390 if(!access("/proc/curproc/file", F_OK))
392 strcpy(fileName, "/proc/curproc/file");
395 if((env = getenv("_")))
397 strcpy(fileName, env);
402 strcpy(fileName, exeLocation);
409 void Instance_COM_Initialize(int argc, char ** argv, char ** parsedCommand, int * argcPtr, const char *** argvPtr)
411 #if !defined(__WIN32__) && !defined(__EMSCRIPTEN__) && !defined(ECERE_BOOTSTRAP) && !defined(__pnacl__)
412 // Disable stdout buffering on Unix
413 setvbuf(stdout, null, _IONBF, 0);
415 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
416 *parsedCommand = UTF16toUTF8(GetCommandLineW());
417 *argvPtr = eSystem_New0(sizeof(char *) * 512);
418 *argcPtr = Tokenize(*parsedCommand, 512,(void*)(char **)(*argvPtr), forArgsPassing);
421 *argvPtr = (const char **)argv;
423 #if defined(__unix__)
424 if(!__thisModule && argv)
426 if(!getcwd(exeLocation, MAX_LOCATION))
428 PathCat(exeLocation, argv[0]);
433 void * Instance_Module_Load(const char * libLocation, const char * name, void ** Load, void ** Unload)
435 char fileName[MAX_LOCATION];
436 char extension[MAX_EXTENSION];
437 void * library = null;
438 #if defined(__unix__) || defined(__APPLE__)
440 char * paths[] = { null, "/usr/lib/ec/lib", "/usr/lib32/ec/lib" };
446 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
447 strcpy(fileName, name);
448 GetExtension(fileName, extension);
450 strcat(fileName, ".dll");
453 uint16 _wfileName[MAX_LOCATION];
454 UTF8toUTF16Buffer(fileName, _wfileName, MAX_LOCATION);
455 library = LoadLibraryW(_wfileName);
460 *Load = (void *)GetProcAddress(library, "__ecereDll_Load");
461 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload");
463 *Load = (void *)GetProcAddress(library, "__ecereDll_Load@4");
464 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload@4");
467 FreeLibrary(library);
469 #elif defined(__unix__) || defined(__APPLE__)
470 if(libLocation || strchr(name, '/'))
471 strcpy(fileName, libLocation ? libLocation : "");
473 strcpy(fileName, "lib");
474 strcat(fileName, name);
475 GetExtension(fileName, extension);
477 #if defined(__APPLE__)
478 strcat(fileName, ".dylib");
480 strcat(fileName, ".so");
483 #if !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
484 library = dlopen(fileName, RTLD_LAZY);
486 while(!library && attempts < sizeof(paths)/sizeof(paths[0]))
489 strcpy(fileName, paths[attempts++]);
493 #ifdef DEB_HOST_MULTIARCH
494 strcpy(fileName, DEB_HOST_MULTIARCH);
495 strcat(fileName, "/ec/lib");
500 strcat(fileName, name);
501 GetExtension(fileName, extension);
503 #if defined(__APPLE__)
504 strcat(fileName, ".dylib");
506 strcat(fileName, ".so");
508 #if !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
509 library = dlopen(fileName, RTLD_LAZY);
515 *Load = dlsym(library, "__ecereDll_Load");
516 *Unload = dlsym(library, "__ecereDll_Unload");
517 #if !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
522 #elif defined(__APPLE__)
523 if(libLocation || strchr(name, '/'))
524 strcpy(fileName, libLocation ? libLocation : "");
526 strcpy(fileName, "lib");
527 strcat(fileName, name);
528 GetExtension(fileName, extension);
530 strcat(fileName, ".dylib");
532 NSObjectFileImage *fileImage;
533 NSObjectFileImageReturnCode returnCode = NSCreateObjectFileImageFromFile(fileName, &fileImage);
535 if(returnCode == NSObjectFileImageSuccess)
537 printf("NSObjectFileImageSuccess!\n");
538 library = NSLinkModule(fileImage,fileName,
539 NSLINKMODULE_OPTION_RETURN_ON_ERROR
540 | NSLINKMODULE_OPTION_PRIVATE);
541 // NSDestroyObjectFileImage(fileImage);
544 *Load = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Load"));
545 *Unload = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Unload"));
548 NSUnLinkModule(library, 0);
551 printf("Found Load!\n");
555 printf("No Success :(\n");
561 void Instance_Module_Free(void * library)
563 #if defined(__WIN32__) && !defined(__EMSCRIPTEN__)
565 FreeLibrary(library);
566 #elif (defined(__unix__) || defined(__APPLE__)) && !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
573 void InternalModuleLoadBreakpoint()
580 bool Float_isNan(float n) { return isnan(n); }
581 bool Float_isInf(float n) { return isinf(n); }
582 int Float_signBit(float n) { return signbit(n); }
583 float Float_nan(void) { return NAN; }
584 float Float_inf(void) { return INFINITY; }
586 bool Double_isNan(double n) { return isnan(n); }
587 bool Double_isInf(double n) { return isinf(n); }
588 int Double_signBit(double n) { return signbit(n); }
589 double Double_nan(void) { return NAN; }
590 double Double_inf(void) { return INFINITY; }