summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2010-10-25 15:40:38 -0700
committerPixel <pixel@nobis-crew.org>2010-10-25 15:40:38 -0700
commit8ed0e40ddde1c8da6ddb6ca0ee8de174a7814280 (patch)
treef5b49dc7e7938655ee7cd1984f60a9d0d61a8022 /src
parentcd0c7dd3505591a04c62e2914dd38cd638a4df69 (diff)
Making it so lua-interface can really load from anywhere. Almost.
Diffstat (limited to 'src')
-rw-r--r--src/lua-interface-main.c97
1 files changed, 95 insertions, 2 deletions
diff --git a/src/lua-interface-main.c b/src/lua-interface-main.c
index 35579db..5c16ef5 100644
--- a/src/lua-interface-main.c
+++ b/src/lua-interface-main.c
@@ -1,5 +1,98 @@
-int dll_main(int argc, char ** argv);
+#define MAX_PATH 1024
+
+#include <stdio.h>
+#include <string.h>
+
+#if defined(_WIN32)
+#define SHARED_EXT "dll"
+#elif defined(__APPLE__)
+#define SHARED_EXT "dylib"
+#else
+#define SHARED_EXT "so"
+#endif
+
+typedef void(*dll_main_t)(int argc, char ** argv);
+
+#if defined(_WIN32)
+#include <windows.h>
+
+static void showError() {
+ DWORD dwErrCode = GetLastError();
+ LPVOID lpMsgBuf;
+ if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, GetLastError(),
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+ (LPTSTR) &lpMsgBuf, 0, NULL )) {
+ fprintf(stderr, "Error: %s\n", lpMsgBuf);
+ LocalFree(lpMsgBuf);
+ }
+}
+
+static HMODULE LoadLibraryExWrap(LPCTSTR lpFileName, HANDLE hFile, DWORD dwFlags) {
+ HMODULE r = LoadLibraryEx(lpFileName, hFile, dwFlags);
+ if (!r)
+ showError();
+ return r;
+}
+
+static dll_main_t LoadDllMain(const char * searchpath) {
+ HMODULE handle;
+ const char * fname = "lua-interface." SHARED_EXT;
+
+ if (!(handle = LoadLibraryExWrap(fname, NULL, LOAD_WITH_ALTERED_SEARCH_PATH)) &&
+ !(handle = LoadLibraryExWrap(fname, NULL, NULL))) {
+ LuaLoadPlugin(&Input(fname), L);
+ return NULL;
+ }
+
+ dll_main_t dll_main = (dll_main) GetProcAddress(handle, "dll_main");
+
+ if (!dll_main)
+ FreeLibrary(handle);
+
+ return dll_main;
+}
+
+#else
+#include <dlfcn.h>
+
+static dll_main_t LoadDllMain(const char * searchpath) {
+ const char * fname = "./lua-interface." SHARED_EXT;
+ void * handle = dlopen(fname, RTLD_NOW | RTLD_GLOBAL);
+
+ if (!handle) {
+ char full_fname[MAX_PATH];
+ strcpy(full_fname, searchpath);
+ strcat(full_fname, "lua-interface." SHARED_EXT);
+ handle = dlopen(full_fname, RTLD_NOW | RTLD_GLOBAL);
+ if (!handle) {
+ return NULL;;
+ }
+ }
+
+ dll_main_t dll_main = (dll_main_t) dlsym(handle, "dll_main");
+
+ if (!dll_main)
+ dlclose(handle);
+
+ return dll_main;
+}
+
+#endif
int main(int argc, char ** argv) {
- dll_main(argc, argv);
+ char searchpath[MAX_PATH];
+ char * slash;
+ strcpy(searchpath, argv[0]);
+ slash = strrchr(searchpath, '/');
+ if (slash) {
+ *slash = 0;
+ } else {
+ searchpath[0] = 0;
+ }
+ dll_main_t dll_main = LoadDllMain(searchpath);
+ if (dll_main)
+ dll_main(argc, argv);
}