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(__MINGW32__) && !defined(_W64) && __GNUC__ < 4
54 __declspec(dllexport) int isblank(int c) { return c == '\t' || c == ' '; }
57 #if defined(__WIN32__)
58 intptr_t stdinHandle, stdoutHandle;
59 int osfStdin, osfStdout;
60 FILE * fStdIn, * fStdOut;
63 FILE *eC_stdin(void) {
64 #if defined(__WIN32__)
67 stdinHandle = (intptr_t)GetStdHandle(STD_INPUT_HANDLE);
68 osfStdin = _open_osfhandle(stdinHandle, _O_TEXT);
70 fStdIn = _fdopen( osfStdin, "rb");
73 setvbuf( fStdIn, NULL, _IONBF, 0 );
83 #if 0 //defined(__WIN32__)
86 stdoutHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
87 osfStdout = _open_osfhandle(stdoutHandle, _O_TEXT);
89 fStdOut = _fdopen( osfStdout, "wb");
92 setvbuf( fStdOut, NULL, _IONBF, 0 );
99 FILE *eC_stderr(void) { return stderr; }
101 void __ecereNameSpace__ecere__com__eSystem_Delete(void * memory);
102 void * __ecereNameSpace__ecere__com__eSystem_New0(unsigned int size);
103 void * __ecereNameSpace__ecere__com__eSystem_Renew(void * memory, unsigned int size);
104 void * __ecereNameSpace__ecere__com__eSystem_Renew0(void * memory, unsigned int size);
105 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16(const char * source, int * wordCount);
106 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(const char * source, uint16 * dest, int max);
107 char * __ecereNameSpace__ecere__sys__UTF16toUTF8(uint16 * source);
108 char * __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(uint16 * source, char * dest, int max);
110 #define eSystem_Delete __ecereNameSpace__ecere__com__eSystem_Delete
111 #define eSystem_New0 __ecereNameSpace__ecere__com__eSystem_New0
112 #define eSystem_Renew __ecereNameSpace__ecere__com__eSystem_Renew
113 #define eSystem_Renew0 __ecereNameSpace__ecere__com__eSystem_Renew0
114 #define UTF8toUTF16 __ecereNameSpace__ecere__sys__UTF8toUTF16
115 #define UTF8toUTF16Buffer __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer
116 #define UTF16toUTF8 __ecereNameSpace__ecere__sys__UTF16toUTF8
117 #define UTF16toUTF8Buffer __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer
119 #if defined(__unix__)
120 static char exeLocation[MAX_LOCATION];
123 #define forArgsPassing 2
124 int __ecereNameSpace__ecere__sys__Tokenize(char * string, int maxTokens, char* tokens[], int esc);
125 char * __ecereNameSpace__ecere__sys__RSearchString(const char * buffer, const char * subStr, int maxLen, bool matchCase, bool matchWord);
126 char * __ecereNameSpace__ecere__sys__GetLastDirectory(const char * string, char * output);
127 char * __ecereNameSpace__ecere__sys__PathCat(char * string, const char * addedPath);
128 char * __ecereNameSpace__ecere__sys__GetExtension(const char * string, char * output);
130 #define Tokenize __ecereNameSpace__ecere__sys__Tokenize
131 #define RSearchString __ecereNameSpace__ecere__sys__RSearchString
132 #define GetLastDirectory __ecereNameSpace__ecere__sys__GetLastDirectory
133 #define PathCat __ecereNameSpace__ecere__sys__PathCat
134 #define GetExtension __ecereNameSpace__ecere__sys__GetExtension
136 extern struct __ecereNameSpace__ecere__com__Instance * __thisModule;
138 typedef enum { unknown, win32, tux, apple } Platform;
140 #if defined(__WIN32__)
141 Platform runtimePlatform = win32;
142 #elif defined(__APPLE__)
143 Platform runtimePlatform = apple;
144 #else //if defined(__linux__)
145 Platform runtimePlatform = tux;
147 Platform runtimePlatform = unknown; // 'linux' is used as a catch all UNIX platform for now
151 #if !defined(ECERE_NOFILE) && defined(__unix__) && !defined(__linux__)
153 typedef struct _DualPipe _DualPipe;
155 void DualPipe_Destructor(_DualPipe * dp);
156 bool DualPipe_Getc(_DualPipe * dp, char * ch);
157 bool DualPipe_Eof(_DualPipe * dp);
158 void DualPipe_Wait(_DualPipe * dp);
160 _DualPipe * _DualPipeOpen(int mode, char * commandLine, char * env, void ** inputPtr, void ** outputPtr);
162 static bool DualPipe_GetLine(FILE * p, char *s, int max)
174 if((ch = fgetc(p)) == EOF)
186 return result || c > 1;
191 bool Instance_LocateModule(const char * name, char * fileName)
193 #if defined(__WIN32__)
194 HMODULE hModule = null;
197 uint16 _wmoduleName[MAX_LOCATION];
198 UTF8toUTF16Buffer(name, _wmoduleName, MAX_LOCATION);
199 hModule = GetModuleHandle(_wmoduleName);
202 wcscat(_wmoduleName, L".exe");
203 hModule = GetModuleHandle(_wmoduleName);
207 uint16 _wfileName[MAX_LOCATION];
208 GetModuleFileNameW(hModule, _wfileName, MAX_LOCATION);
209 UTF16toUTF8Buffer(_wfileName, fileName, MAX_LOCATION);
215 uint16 _wfileName[MAX_LOCATION];
216 GetModuleFileNameW(null, _wfileName, MAX_LOCATION);
217 UTF16toUTF8Buffer(_wfileName, fileName, MAX_LOCATION);
220 #elif defined(__APPLE__)
223 int imageCount = _dyld_image_count();
225 int nameLen = strlen(name);
226 for(c = 0; c<imageCount; c++)
228 struct mach_header * header = _dyld_get_image_header(c);
229 char * path = _dyld_get_image_name(c);
230 int pathLen = strlen(path);
231 char * subStr = RSearchString(path, name, pathLen, false, false);
234 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
235 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".dylib", 6)))
237 strcpy(fileName, path);
245 int size = MAX_LOCATION;
246 _NSGetExecutablePath(fileName, &size);
249 #elif defined(__unix__)
250 //File f = FileOpen("/proc/self/maps", read);
252 char exeName[MAX_FILENAME];
254 #if defined(__linux__)
255 f = fopen("/proc/self/status", "r");
257 f = fopen("/proc/curproc/status", "r");
262 while(fgets(line, sizeof(line), f))
264 char * name = strstr(line, "Name:\t");
269 nameLen = strlen(name);
271 strcpy(exeName, name);
277 #if defined(__linux__)
278 f = fopen("/proc/self/maps", "r");
280 f = fopen("/proc/curproc/map", "r");
285 //while(f.GetLine(line, sizeof(line)))
286 while(fgets(line, sizeof(line), f))
288 char * path = strstr(line, "/");
291 int pathLen = strlen(path);
295 int nameLen = strlen(name);
297 subStr = RSearchString(path, name, pathLen, false, false);
300 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
301 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
303 char * space = strchr(path, ' ');
304 if(space) *space = 0;
305 strcpy(fileName, path);
313 char name[MAX_FILENAME];
314 GetLastDirectory(path, name);
315 if(!exeName[0] || !strcmp(name, exeName))
317 char * space = strchr(path, ' ');
318 if(space) *space = 0;
319 strcpy(fileName, path);
328 #if !defined(ECERE_NOFILE) && !defined(__linux__)
331 // Couldn't locate libraries with /proc/curmap/map, attempt with ldd
332 FILE * in = null, * out = null;
334 char command[MAX_LOCATION];
335 snprintf(command, sizeof(command), "ldd /proc/%d/file", (int)getpid());
336 p = _DualPipeOpen(1, command, null, &in, &out);
341 int nameLen = strlen(name);
342 while(DualPipe_GetLine(in, line, sizeof(line)))
344 char * path = strstr(line, "/");
347 int pathLen = strlen(path);
350 subStr = RSearchString(path, name, pathLen, false, false);
353 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
354 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
356 char * space = strchr(path, ' ');
357 if(space) *space = 0;
358 strcpy(fileName, path);
366 DualPipe_Destructor(p);
371 if(!name || !name[0])
373 #if defined(__FreeBSD__)
375 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
376 size_t cb = MAX_LOCATION;
378 sysctl(mib, 4, fileName, &cb, null, 0);
383 #if !defined(__linux__)
386 if(!access("/proc/curproc/file", F_OK))
388 strcpy(fileName, "/proc/curproc/file");
391 if((env = getenv("_")))
393 strcpy(fileName, env);
398 strcpy(fileName, exeLocation);
405 void Instance_COM_Initialize(int argc, char ** argv, char ** parsedCommand, int * argcPtr, const char *** argvPtr)
407 #if !defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
408 // Disable stdout buffering on Unix
409 setvbuf(stdout, null, _IONBF, 0);
411 #if defined(__WIN32__)
412 *parsedCommand = UTF16toUTF8(GetCommandLineW());
413 *argvPtr = eSystem_New0(sizeof(char *) * 512);
414 *argcPtr = Tokenize(*parsedCommand, 512,(void*)(char **)(*argvPtr), forArgsPassing);
417 *argvPtr = (const char **)argv;
419 #if defined(__unix__)
420 if(!__thisModule && argv)
422 if(!getcwd(exeLocation, MAX_LOCATION))
424 PathCat(exeLocation, argv[0]);
429 void * Instance_Module_Load(const char * libLocation, const char * name, void ** Load, void ** Unload)
431 char fileName[MAX_LOCATION];
432 char extension[MAX_EXTENSION];
433 void * library = null;
434 #if defined(__unix__) || defined(__APPLE__)
436 char * paths[] = { null, "/usr/lib/ec/lib", "/usr/lib32/ec/lib" };
442 #if defined(__WIN32__)
443 strcpy(fileName, name);
444 GetExtension(fileName, extension);
446 strcat(fileName, ".dll");
449 uint16 _wfileName[MAX_LOCATION];
450 UTF8toUTF16Buffer(fileName, _wfileName, MAX_LOCATION);
451 library = LoadLibraryW(_wfileName);
456 *Load = (void *)GetProcAddress(library, "__ecereDll_Load");
457 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload");
459 *Load = (void *)GetProcAddress(library, "__ecereDll_Load@4");
460 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload@4");
463 FreeLibrary(library);
465 #elif defined(__unix__) || defined(__APPLE__)
466 if(libLocation || strchr(name, '/'))
467 strcpy(fileName, libLocation ? libLocation : "");
469 strcpy(fileName, "lib");
470 strcat(fileName, name);
471 GetExtension(fileName, extension);
473 #if defined(__APPLE__)
474 strcat(fileName, ".dylib");
476 strcat(fileName, ".so");
479 library = dlopen(fileName, RTLD_LAZY);
480 while(!library && attempts < sizeof(paths)/sizeof(paths[0]))
483 strcpy(fileName, paths[attempts++]);
487 #ifdef DEB_HOST_MULTIARCH
488 strcpy(fileName, DEB_HOST_MULTIARCH);
489 strcat(fileName, "/ec/lib");
494 strcat(fileName, name);
495 GetExtension(fileName, extension);
497 #if defined(__APPLE__)
498 strcat(fileName, ".dylib");
500 strcat(fileName, ".so");
502 library = dlopen(fileName, RTLD_LAZY);
507 *Load = dlsym(library, "__ecereDll_Load");
508 *Unload = dlsym(library, "__ecereDll_Unload");
512 #elif defined(__APPLE__)
513 if(libLocation || strchr(name, '/'))
514 strcpy(fileName, libLocation ? libLocation : "");
516 strcpy(fileName, "lib");
517 strcat(fileName, name);
518 GetExtension(fileName, extension);
520 strcat(fileName, ".dylib");
522 NSObjectFileImage *fileImage;
523 NSObjectFileImageReturnCode returnCode = NSCreateObjectFileImageFromFile(fileName, &fileImage);
525 if(returnCode == NSObjectFileImageSuccess)
527 printf("NSObjectFileImageSuccess!\n");
528 library = NSLinkModule(fileImage,fileName,
529 NSLINKMODULE_OPTION_RETURN_ON_ERROR
530 | NSLINKMODULE_OPTION_PRIVATE);
531 // NSDestroyObjectFileImage(fileImage);
534 *Load = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Load"));
535 *Unload = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Unload"));
538 NSUnLinkModule(library, 0);
541 printf("Found Load!\n");
545 printf("No Success :(\n");
551 void Instance_Module_Free(void * library)
553 #if defined(__WIN32__)
555 FreeLibrary(library);
556 #elif defined(__unix__) || defined(__APPLE__)
563 void InternalModuleLoadBreakpoint()
570 bool Float_isNan(float n) { return isnan(n); }
571 bool Float_isInf(float n) { return isinf(n); }
572 int Float_signBit(float n) { return signbit(n); }
573 float Float_nan(void) { return NAN; }
574 float Float_inf(void) { return INFINITY; }
576 bool Double_isNan(double n) { return isnan(n); }
577 bool Double_isInf(double n) { return isinf(n); }
578 int Double_signBit(double n) { return signbit(n); }
579 double Double_nan(void) { return NAN; }
580 double Double_inf(void) { return INFINITY; }