# # # patch "lua.cc" # from [b24e1956c4529dc205ac3168de9119d2048cf481] # to [987982ec6fae322b0fe836aaff25ccc1fdc4f973] # # patch "lua.hh" # from [a133cf4720fdd0213f3fad854be18d28f6828646] # to [94050b961bd34d473e74a066e0b4c956546c863c] # # patch "lua_hooks.cc" # from [757749282b8c1f336a466c55e7c0d9d7a874aa41] # to [0044b4eafa513561907184fd83a7e134337be697] # # patch "tester.cc" # from [f6d1c48885f8231c79e40e3bd0b0041d84c983f9] # to [d1ad2241f29ecf11fca85ae573fc8bff4d942a87] # ============================================================ --- lua.cc b24e1956c4529dc205ac3168de9119d2048cf481 +++ lua.cc 987982ec6fae322b0fe836aaff25ccc1fdc4f973 @@ -395,11 +395,11 @@ Lua & } Lua & -Lua::loadstring(string const & str, string const & identity) +Lua::loadstring(char const * str, char const * identity) { if (!failed) { - if (luaL_loadbuffer(st, str.c_str(), str.size(), identity.c_str())) + if (luaL_loadbuffer(st, str, strlen(str), identity)) { report_error(); } @@ -408,11 +408,11 @@ Lua & } Lua & -Lua::loadfile(string const & filename) +Lua::loadfile(char const * filename) { if (!failed) { - if (luaL_loadfile(st, filename.c_str())) + if (luaL_loadfile(st, filename)) { report_error(); } @@ -468,10 +468,7 @@ LUAEXT(include, ) const char *path = luaL_checkstring(L, -1); N(path, F("%s called with an invalid parameter") % "Include"); - bool res =Lua(L) - .loadfile(string(path, lua_strlen(L, -1))) - .call(0,1) - .ok(); + bool res = run_file(L, path); lua_pushboolean(L, res); return 1; @@ -482,30 +479,7 @@ LUAEXT(includedir, ) const char *pathstr = luaL_checkstring(L, -1); N(pathstr, F("%s called with an invalid parameter") % "IncludeDir"); - fs::path locpath(pathstr, fs::native); - N(fs::exists(locpath), F("Directory '%s' does not exist") % pathstr); - N(fs::is_directory(locpath), F("'%s' is not a directory") % pathstr); - - // directory, iterate over it, skipping subdirs, taking every filename, - // sorting them and loading in sorted order - fs::directory_iterator it(locpath); - vector arr; - while (it != fs::directory_iterator()) - { - if (!fs::is_directory(*it)) - arr.push_back(*it); - ++it; - } - sort(arr.begin(), arr.end()); - for (vector::iterator i= arr.begin(); i != arr.end(); ++i) - { - bool res =Lua(L) - .loadfile(i->string()) - .call(0,1) - .ok(); - N(res, F("lua error while loading rcfile '%s'") % i->string()); - } - + run_directory(L, pathstr, "*"); lua_pushboolean(L, true); return 1; } @@ -517,32 +491,7 @@ LUAEXT(includedirpattern, ) N(pathstr && pattern, F("%s called with an invalid parameter") % "IncludeDirPattern"); - fs::path locpath(pathstr, fs::native); - N(fs::exists(locpath), F("Directory '%s' does not exist") % pathstr); - N(fs::is_directory(locpath), F("'%s' is not a directory") % pathstr); - - // directory, iterate over it, skipping subdirs, taking every filename - // matching the pattern, sorting them and loading in sorted order - fs::directory_iterator it(locpath); - globish r = globish(pattern); - globish_matcher glob(r, globish()); - vector arr; - while (it != fs::directory_iterator()) - { - if (!fs::is_directory(*it) && glob(it->string())) - arr.push_back(*it); - ++it; - } - sort(arr.begin(), arr.end()); - for (vector::iterator i= arr.begin(); i != arr.end(); ++i) - { - bool res =Lua(L) - .loadfile(i->string()) - .call(0,1) - .ok(); - N(res, F("lua error while loading rcfile '%s'") % i->string()); - } - + run_directory(L, pathstr, pattern); lua_pushboolean(L, true); return 1; } @@ -573,7 +522,7 @@ bool } bool -run_string(lua_State * st, string const &str, string const & identity) +run_string(lua_State * st, char const * str, char const * identity) { I(st); return @@ -584,7 +533,7 @@ bool } bool -run_file(lua_State * st, string const &filename) +run_file(lua_State * st, char const * filename) { I(st); return @@ -594,6 +543,38 @@ run_file(lua_State * st, string const &f .ok(); } +void +run_directory(lua_State * st, char const * pathstr, char const * pattern) +{ + fs::path locpath(pathstr, fs::native); + N(fs::exists(locpath), F("Directory '%s' does not exist") % pathstr); + N(fs::is_directory(locpath), F("'%s' is not a directory") % pathstr); + + // directory, iterate over it, skipping subdirs, taking every filename + // matching the pattern, sorting them and loading in sorted order + fs::directory_iterator it(locpath); + globish r = globish(pattern); + globish_matcher glob(r, globish()); + vector arr; + while (it != fs::directory_iterator()) + { + if (!fs::is_directory(*it) && glob(it->string())) + arr.push_back(*it); + ++it; + } + sort(arr.begin(), arr.end()); + for (vector::iterator i= arr.begin(); i != arr.end(); ++i) + { + L(FL("opening rcfile '%s'") % i->string()); + bool res = Lua(st) + .loadfile(i->string().c_str()) + .call(0,1) + .ok(); + N(res, F("lua error while loading rcfile '%s'") % i->string()); + L(FL("'%s' is ok") % i->string()); + } +} + // Local Variables: // mode: C++ // fill-column: 76 ============================================================ --- lua.hh a133cf4720fdd0213f3fad854be18d28f6828646 +++ lua.hh 94050b961bd34d473e74a066e0b4c956546c863c @@ -62,14 +62,13 @@ Lua Lua & call(int in, int out); Lua & pop(int count = 1); Lua & func(std::string const & fname); - Lua & loadstring(std::string const & str, std::string const & identity); - Lua & loadfile(std::string const & filename); + Lua & loadstring(char const * str, char const * identity); + Lua & loadfile(char const * filename); }; -bool run_string(lua_State * st, - std::string const &str, - std::string const & identity); -bool run_file(lua_State * st, std::string const &filename); +bool run_string(lua_State * st, char const * str, char const * identity); +bool run_file(lua_State * st, char const * filename); +void run_directory(lua_State * st, char const * dirname, char const * pattern); void add_functions(lua_State * st); namespace luaext ============================================================ --- lua_hooks.cc 757749282b8c1f336a466c55e7c0d9d7a874aa41 +++ lua_hooks.cc 0044b4eafa513561907184fd83a7e134337be697 @@ -13,10 +13,6 @@ #include "lualib.h" #include "lauxlib.h" -#include -#include -#include - #include #include #include @@ -38,8 +34,6 @@ extern char const test_hooks_constant[]; extern char const test_hooks_constant[]; #endif -namespace fs = boost::filesystem; - using std::make_pair; using std::map; using std::pair; @@ -48,8 +42,6 @@ using std::vector; using std::string; using std::vector; -using boost::lexical_cast; - static int panic_thrower(lua_State * st) { throw oops("lua panic"); @@ -95,12 +87,16 @@ lua_hooks::lua_hooks() // Disable any functions we don't want. This is easiest // to do just by running a lua string. - if (!run_string(st, - // "os.unsafeexecute = os.execute " - // "io.unsafepopen = io.popen " - "os.execute = function(c) error(\"os.execute disabled for security reasons. Try spawn().\") end " - "io.popen = function(c,t) error(\"io.popen disabled for security reasons. Try spawn_pipe().\") end ", - string(""))) + static char const disable_dangerous[] = + "os.execute = function(c) " + " error(\"os.execute disabled for security reasons. Try spawn().\") " + "end " + "io.popen = function(c,t) " + " error(\"io.popen disabled for security reasons. Try spawn_pipe().\") " + "end "; + + if (!run_string(st, disable_dangerous, + "")) throw oops("lua error while disabling existing functions"); } @@ -125,7 +121,7 @@ lua_hooks::add_test_hooks() void lua_hooks::add_test_hooks() { - if (!run_string(st, test_hooks_constant, string(""))) + if (!run_string(st, test_hooks_constant, "")) throw oops("lua error while setting up testing hooks"); } #endif @@ -133,7 +129,7 @@ lua_hooks::add_std_hooks() void lua_hooks::add_std_hooks() { - if (!run_string(st, std_hooks_constant, string(""))) + if (!run_string(st, std_hooks_constant, "")) throw oops("lua error while setting up standard hooks"); } @@ -156,35 +152,17 @@ lua_hooks::load_rcfile(utf8 const & rc) lua_hooks::load_rcfile(utf8 const & rc) { I(st); - if (rc() != "-") + if (rc() != "-" && directory_exists(system_path(rc))) + run_directory(st, system_path(rc).as_external().c_str(), "*"); + else { - fs::path locpath(system_path(rc).as_external(), fs::native); - if (fs::exists(locpath) && fs::is_directory(locpath)) - { - // directory, iterate over it, skipping subdirs, taking every filename, - // sorting them and loading in sorted order - fs::directory_iterator it(locpath); - vector arr; - while (it != fs::directory_iterator()) - { - if (!fs::is_directory(*it)) - arr.push_back(*it); - ++it; - } - sort(arr.begin(), arr.end()); - for (vector::iterator i= arr.begin(); i != arr.end(); ++i) - { - load_rcfile(system_path(i->native_directory_string()), true); - } - return; // directory read, skip the rest ... - } + data dat; + L(FL("opening rcfile '%s'") % rc); + read_data_for_command_line(rc, dat); + N(run_string(st, dat().c_str(), rc().c_str()), + F("lua error while loading rcfile '%s'") % rc); + L(FL("'%s' is ok") % rc); } - data dat; - L(FL("opening rcfile '%s'") % rc); - read_data_for_command_line(rc, dat); - N(run_string(st, dat(), rc().c_str()), - F("lua error while loading rcfile '%s'") % rc); - L(FL("'%s' is ok") % rc); } void @@ -194,7 +172,7 @@ lua_hooks::load_rcfile(any_path const & if (path_exists(rc)) { L(FL("opening rcfile '%s'") % rc); - N(run_file(st, rc.as_external()), + N(run_file(st, rc.as_external().c_str()), F("lua error while loading '%s'") % rc); L(FL("'%s' is ok") % rc); } ============================================================ --- tester.cc f6d1c48885f8231c79e40e3bd0b0041d84c983f9 +++ tester.cc d1ad2241f29ecf11fca85ae573fc8bff4d942a87 @@ -14,7 +14,6 @@ #include #include #include -#include #include @@ -32,7 +31,6 @@ using std::make_pair; using std::getenv; using std::exit; using std::make_pair; -using boost::lexical_cast; // Lua uses the c i/o functions, so we need to too. struct tester_sanity : public sanity @@ -544,8 +542,7 @@ int main(int argc, char **argv) try { run_string(st, testlib_constant, "tester builtin functions"); - //printf("Loading test file %s\n", testfile.c_str()); - run_file(st, testfile); + run_file(st, testfile.c_str()); Lua ll(st); ll.func("run_tests"); ll.push_table();