Skip to content
Browse files

Add module search path relative to current exe #94

  • Loading branch information...
1 parent 36466a4 commit b872a50accf239b2a780acce52dbb1c474551586 @lefticus lefticus committed Jan 5, 2014
Showing with 83 additions and 0 deletions.
  1. +1 −0 CMakeLists.txt
  2. +82 −0 src/main.cpp
View
1 CMakeLists.txt
@@ -242,6 +242,7 @@ if(BUILD_TESTING)
endif(BUILD_TESTING)
install(TARGETS chai ${MODULES} RUNTIME DESTINATION bin LIBRARY DESTINATION lib/chaiscript )
+
install(DIRECTORY include/chaiscript DESTINATION include
PATTERN "*.hpp"
PATTERN "*/.svn*" EXCLUDE
View
82 src/main.cpp
@@ -15,6 +15,86 @@
#include <readline/readline.h>
#include <readline/history.h>
#else
+
+void *cast_module_symbol(std::string (*t_path)())
+{
+ union cast_union
+ {
+ std::string (*in_ptr)();
+ void *out_ptr;
+ };
+
+ cast_union c;
+ c.in_ptr = t_path;
+ return c.out_ptr;
+}
+
+std::string default_search_path()
+{
+#ifdef CHAISCRIPT_WINDOWS
+ TCHAR path[2048];
+ int size = GetModuleFileName(0, path, sizeof(path)-1);
+
+ std::string exepath(path, size);
+
+ size_t secondtolastslash = exepath.rfind('\\', exepath.rfind('\\') - 1);
+ if (secondtolastslash != std::string::npos)
+ {
+ return exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\";
+ } else {
+ return "";
+ }
+
+
+#else
+ std::string exepath;
+
+ std::vector<char> buf(2048);
+ ssize_t size = -1;
+
+ if ((size = readlink("/proc/self/exe", &buf.front(), buf.size())) != -1)
+ {
+ exepath = std::string(&buf.front(), size);
+ }
+
+ if (exepath.empty())
+ {
+ if ((size = readlink("/proc/curproc/file", &buf.front(), buf.size())) != -1)
+ {
+ exepath = std::string(&buf.front(), size);
+ }
+ }
+
+ if (exepath.empty())
+ {
+ if ((size = readlink("/proc/self/path/a.out", &buf.front(), buf.size())) != -1)
+ {
+ exepath = std::string(&buf.front(), size);
+ }
+ }
+
+ if (exepath.empty())
+ {
+ Dl_info rInfo;
+ memset( &rInfo, 0, sizeof(rInfo) );
+ if ( !dladdr(cast_module_symbol(&default_search_path), &rInfo) || !rInfo.dli_fname ) {
+ return "";
+ }
+
+ exepath = std::string(rInfo.dli_fname);
+ }
+
+ size_t secondtolastslash = exepath.rfind('/', exepath.rfind('/') - 1);
+ if (secondtolastslash != std::string::npos)
+ {
+ return exepath.substr(0, secondtolastslash) + "/lib/chaiscript/";
+ } else {
+ return "";
+ }
+#endif
+}
+
+
char* readline(const char* p)
{
std::string retval;
@@ -167,6 +247,8 @@ int main(int argc, char *argv[])
usepaths.push_back(usepath);
}
+ std::string searchpath = default_search_path();
+ modulepaths.push_back(searchpath);
modulepaths.push_back("");
if (modulepath)
{

0 comments on commit b872a50

Please sign in to comment.
Something went wrong with that request. Please try again.