Merge branch 'master' into ChaiScript_5_0_CPP_11

Fix for issue #94

Conflicts:
	src/main.cpp
This commit is contained in:
Jason Turner
2014-01-05 15:12:54 -07:00
2 changed files with 84 additions and 1 deletions

View File

@@ -23,6 +23,85 @@ char *mystrdup (const char *s) {
return d; // Return the new string
}
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;
@@ -30,6 +109,7 @@ char* readline(const char* p)
std::getline(std::cin, retval);
return std::cin.eof() ? NULL : mystrdup(retval.c_str());
}
void add_history(const char*){}
void using_history(){}
#endif
@@ -174,6 +254,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)
{