7 #if defined(__GNUC__) || defined(__WATCOMC__) || defined(__WIN32__)
14 #if defined(__unix__) || defined(__APPLE__)
21 #if defined(__WIN32__)
22 #define WIN32_LEAN_AND_MEAN
32 typedef unsigned char byte;
33 typedef unsigned short uint16;
34 typedef unsigned int uint;
35 typedef unsigned int FileSize;
36 typedef unsigned long long uint64;
41 #define null ((void *)0)
43 #define MAX_LOCATION 797
44 #define MAX_FILENAME 274
45 #define MAX_EXTENSION 17
47 FILE *eC_stdin(void) { return stdin; }
48 FILE *eC_stdout(void) { return stdout; }
49 FILE *eC_stderr(void) { return stderr; }
51 void __ecereNameSpace__ecere__com__eSystem_Delete(void * memory);
52 void * __ecereNameSpace__ecere__com__eSystem_New0(unsigned int size);
53 void * __ecereNameSpace__ecere__com__eSystem_Renew(void * memory, unsigned int size);
54 void * __ecereNameSpace__ecere__com__eSystem_Renew0(void * memory, unsigned int size);
55 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16(char * source, int * wordCount);
56 unsigned short * __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer(char * source, uint16 * dest, int max);
57 char * __ecereNameSpace__ecere__sys__UTF16toUTF8(uint16 * source);
58 char * __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer(uint16 * source, byte * dest, int max);
60 #define eSystem_Delete __ecereNameSpace__ecere__com__eSystem_Delete
61 #define eSystem_New0 __ecereNameSpace__ecere__com__eSystem_New0
62 #define eSystem_Renew __ecereNameSpace__ecere__com__eSystem_Renew
63 #define eSystem_Renew0 __ecereNameSpace__ecere__com__eSystem_Renew0
64 #define UTF8toUTF16 __ecereNameSpace__ecere__sys__UTF8toUTF16
65 #define UTF8toUTF16Buffer __ecereNameSpace__ecere__sys__UTF8toUTF16Buffer
66 #define UTF16toUTF8 __ecereNameSpace__ecere__sys__UTF16toUTF8
67 #define UTF16toUTF8Buffer __ecereNameSpace__ecere__sys__UTF16toUTF8Buffer
70 static char exeLocation[MAX_LOCATION];
73 int __ecereNameSpace__ecere__sys__Tokenize(char * string, int maxTokens, char* tokens[], bool escapeBackSlashes);
74 char * __ecereNameSpace__ecere__sys__RSearchString(char * buffer, char * subStr, int maxLen, bool matchCase, bool matchWord);
75 char * __ecereNameSpace__ecere__sys__GetLastDirectory(char * string, char * output);
76 char * __ecereNameSpace__ecere__sys__PathCat(char * string, char * addedPath);
77 char * __ecereNameSpace__ecere__sys__GetExtension(char * string, char * output);
79 #define Tokenize __ecereNameSpace__ecere__sys__Tokenize
80 #define RSearchString __ecereNameSpace__ecere__sys__RSearchString
81 #define GetLastDirectory __ecereNameSpace__ecere__sys__GetLastDirectory
82 #define PathCat __ecereNameSpace__ecere__sys__PathCat
83 #define GetExtension __ecereNameSpace__ecere__sys__GetExtension
85 extern struct __ecereNameSpace__ecere__com__Instance * __thisModule;
87 typedef enum { unknown, win32, tux, apple } Platform;
89 #if defined(__WIN32__)
90 Platform runtimePlatform = win32;
91 #elif defined(__APPLE__)
92 Platform runtimePlatform = apple;
93 #elif defined(__linux__)
94 Platform runtimePlatform = tux;
96 Platform runtimePlatform = unknown;
99 #if !defined(ECERE_NOFILE) && defined(__unix__)
101 typedef struct _DualPipe _DualPipe;
103 void DualPipe_Destructor(_DualPipe * dp);
104 bool DualPipe_Getc(_DualPipe * dp, char * ch);
105 bool DualPipe_Eof(_DualPipe * dp);
106 void DualPipe_Wait(_DualPipe * dp);
108 _DualPipe * _DualPipeOpen(int mode, char * commandLine, char * env, void ** inputPtr, void ** outputPtr);
110 static bool DualPipe_GetLine(FILE * p, char *s, int max)
122 if((ch = fgetc(p)) == EOF)
134 return result || c > 1;
139 bool Instance_LocateModule(char * name, char * fileName)
141 #if defined(__WIN32__)
142 HMODULE hModule = null;
145 uint16 _wmoduleName[MAX_LOCATION];
146 UTF8toUTF16Buffer(name, _wmoduleName, MAX_LOCATION);
147 hModule = GetModuleHandle(_wmoduleName);
150 wcscat(_wmoduleName, L".exe");
151 hModule = GetModuleHandle(_wmoduleName);
155 uint16 _wfileName[MAX_LOCATION];
156 GetModuleFileNameW(hModule, _wfileName, MAX_LOCATION);
157 UTF16toUTF8Buffer(_wfileName, (byte *)fileName, MAX_LOCATION);
163 uint16 _wfileName[MAX_LOCATION];
164 GetModuleFileNameW(null, _wfileName, MAX_LOCATION);
165 UTF16toUTF8Buffer(_wfileName, (byte *)fileName, MAX_LOCATION);
168 #elif defined(__APPLE__)
171 int imageCount = _dyld_image_count();
173 int nameLen = strlen(name);
174 for(c = 0; c<imageCount; c++)
176 struct mach_header * header = _dyld_get_image_header(c);
177 char * path = _dyld_get_image_name(c);
178 int pathLen = strlen(path);
179 char * subStr = RSearchString(path, name, pathLen, false, false);
182 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
183 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".dylib", 6)))
185 strcpy(fileName, path);
193 int size = MAX_LOCATION;
194 _NSGetExecutablePath(fileName, &size);
197 #elif defined(__unix__)
198 //File f = FileOpen("/proc/self/maps", read);
200 char exeName[MAX_FILENAME];
202 #if defined(__linux__)
203 f = fopen("/proc/self/status", "r");
205 f = fopen("/proc/curproc/status", "r");
210 while(fgets(line, sizeof(line), f))
212 char * name = strstr(line, "Name:\t");
217 nameLen = strlen(name);
219 strcpy(exeName, name);
225 #if defined(__linux__)
226 f = fopen("/proc/self/maps", "r");
228 f = fopen("/proc/curproc/map", "r");
233 //while(f.GetLine(line, sizeof(line)))
234 while(fgets(line, sizeof(line), f))
236 char * path = strstr(line, "/");
239 int pathLen = strlen(path);
243 int nameLen = strlen(name);
245 subStr = RSearchString(path, name, pathLen, false, false);
248 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
249 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
251 char * space = strchr(path, ' ');
252 if(space) *space = 0;
253 strcpy(fileName, path);
261 char name[MAX_FILENAME];
262 GetLastDirectory(path, name);
263 if(!exeName[0] || !strcmp(name, exeName))
265 char * space = strchr(path, ' ');
266 if(space) *space = 0;
267 strcpy(fileName, path);
276 #if !defined(ECERE_NOFILE) && !defined(__linux__)
279 // Couldn't locate libraries with /proc/curmap/map, attempt with ldd
280 FILE * in = null, * out = null;
282 char command[MAX_LOCATION];
283 snprintf(command, sizeof(command), "ldd /proc/%d/file", (int)getpid());
284 p = _DualPipeOpen(1, command, null, &in, &out);
289 int nameLen = strlen(name);
290 while(DualPipe_GetLine(in, line, sizeof(line)))
292 char * path = strstr(line, "/");
295 int pathLen = strlen(path);
298 subStr = RSearchString(path, name, pathLen, false, false);
301 if(( *(subStr-1) == '/' || !strncmp(subStr - 4, "/lib", 4)) &&
302 (!subStr[nameLen] || !strncmp(subStr + nameLen, ".so", 3)))
304 char * space = strchr(path, ' ');
305 if(space) *space = 0;
306 strcpy(fileName, path);
314 DualPipe_Destructor(p);
319 if(!name || !name[0])
321 #if !defined(__linux__)
322 if(!access("/proc/curproc/file", F_OK))
323 strcpy(fileName, "/proc/curproc/file");
326 strcpy(fileName, exeLocation);
333 void Instance_COM_Initialize(int argc, char ** argv, char ** parsedCommand, int * argcPtr, char *** argvPtr)
335 #if !defined(__WIN32__) && !defined(ECERE_BOOTSTRAP)
336 // Disable stdout buffering on Unix
337 setvbuf(stdout, null, _IONBF, 0);
339 #if defined(__WIN32__)
340 *parsedCommand = UTF16toUTF8(GetCommandLineW());
341 *argvPtr = eSystem_New0(sizeof(char *) * 512);
342 *argcPtr = Tokenize(*parsedCommand, 512,(void*)(char **)(*argvPtr), false);
347 #if defined(__unix__)
348 if(!__thisModule && argv)
350 getcwd(exeLocation, MAX_LOCATION);
351 PathCat(exeLocation, argv[0]);
356 void * Instance_Module_Load(const char * libLocation, const char * name, void ** Load, void ** Unload)
358 char fileName[MAX_LOCATION];
359 char extension[MAX_EXTENSION];
360 void * library = null;
361 #if defined(__unix__) || defined(__APPLE__)
363 char * paths[] = { null, "/usr/lib/ec/lib", "/usr/lib32/ec/lib" };
369 #if defined(__WIN32__)
370 strcpy(fileName, name);
371 GetExtension(fileName, extension);
373 strcat(fileName, ".dll");
376 uint16 _wfileName[MAX_LOCATION];
377 UTF8toUTF16Buffer(fileName, _wfileName, MAX_LOCATION);
378 library = LoadLibraryW(_wfileName);
383 *Load = (void *)GetProcAddress(library, "__ecereDll_Load");
384 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload");
386 *Load = (void *)GetProcAddress(library, "__ecereDll_Load@4");
387 *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload@4");
390 FreeLibrary(library);
392 #elif defined(__unix__) || defined(__APPLE__)
394 strcpy(fileName, libLocation);
396 strcpy(fileName, "lib");
397 strcat(fileName, name);
398 GetExtension(fileName, extension);
400 #if defined(__APPLE__)
401 strcat(fileName, ".dylib");
403 strcat(fileName, ".so");
406 library = dlopen(fileName, RTLD_LAZY);
407 while(!library && attempts < sizeof(paths)/sizeof(paths[0]))
410 strcpy(fileName, paths[attempts++]);
414 #ifdef DEB_HOST_MULTIARCH
415 strcpy(fileName, DEB_HOST_MULTIARCH);
416 strcat(fileName, "/ec/lib");
421 strcat(fileName, name);
422 GetExtension(fileName, extension);
424 #if defined(__APPLE__)
425 strcat(fileName, ".dylib");
427 strcat(fileName, ".so");
429 library = dlopen(fileName, RTLD_LAZY);
434 *Load = dlsym(library, "__ecereDll_Load");
435 *Unload = dlsym(library, "__ecereDll_Unload");
439 #elif defined(__APPLE__)
440 strcpy(fileName, "lib");
441 strcat(fileName, name);
442 GetExtension(fileName, extension);
444 strcat(fileName, ".dylib");
446 NSObjectFileImage *fileImage;
447 NSObjectFileImageReturnCode returnCode = NSCreateObjectFileImageFromFile(fileName, &fileImage);
449 if(returnCode == NSObjectFileImageSuccess)
451 printf("NSObjectFileImageSuccess!\n");
452 library = NSLinkModule(fileImage,fileName,
453 NSLINKMODULE_OPTION_RETURN_ON_ERROR
454 | NSLINKMODULE_OPTION_PRIVATE);
455 // NSDestroyObjectFileImage(fileImage);
458 *Load = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Load"));
459 *Unload = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Unload"));
462 NSUnLinkModule(library, 0);
465 printf("Found Load!\n");
469 printf("No Success :(\n");
475 void Instance_Module_Free(void * library)
477 #if defined(__WIN32__)
479 FreeLibrary(library);
480 #elif defined(__unix__) || defined(__APPLE__)
487 void InternalModuleLoadBreakpoint()