From 8c6bb8a236441f0c31c5127c478a0e5be01d41df Mon Sep 17 00:00:00 2001 From: Henri DF Date: Fri, 4 Mar 2016 17:53:39 -0800 Subject: [PATCH] Set Lua cpath along with path --- README.md | 2 -- userspace/digwatch/digwatch.cpp | 34 ++++++++++++++++++++++++++++++++- userspace/digwatch/rules.cpp | 20 +------------------ userspace/digwatch/rules.h | 3 +-- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index aea72ffc..da9fbd41 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,6 @@ $ make as a result, you should have a digwatch executable `build/userspace/digwatch/digwatch`. -Still not quite done yet! we need to install the [lpeg](http://www.inf.puc-rio.br/~roberto/lpeg/) Lua library. (This should be done automatically as part of the build of course...). To install it, just do: `luarocks install lpeg`. - ### Running diff --git a/userspace/digwatch/digwatch.cpp b/userspace/digwatch/digwatch.cpp index 98a22e7a..0a010c8a 100644 --- a/userspace/digwatch/digwatch.cpp +++ b/userspace/digwatch/digwatch.cpp @@ -125,6 +125,37 @@ void do_inspect(sinsp* inspector, } } +void add_lua_path(lua_State *ls, string path) +{ + string cpath = string(path); + path += "?.lua"; + cpath += "?.so"; + + lua_getglobal(ls, "package"); + + lua_getfield(ls, -1, "path"); + string cur_path = lua_tostring(ls, -1 ); + cur_path += ';'; + lua_pop(ls, 1); + + cur_path.append(path.c_str()); + + lua_pushstring(ls, cur_path.c_str()); + lua_setfield(ls, -2, "path"); + + lua_getfield(ls, -1, "cpath"); + string cur_cpath = lua_tostring(ls, -1 ); + cur_cpath += ';'; + lua_pop(ls, 1); + + cur_cpath.append(cpath.c_str()); + + lua_pushstring(ls, cur_cpath.c_str()); + lua_setfield(ls, -2, "cpath"); + + lua_pop(ls, 1); +} + // // ARGUMENT PARSING AND PROGRAM SETUP // @@ -241,8 +272,9 @@ int digwatch_init(int argc, char **argv) // Initialize Lua interpreter ls = lua_open(); luaL_openlibs(ls); + add_lua_path(ls, lua_dir); - rules = new digwatch_rules(inspector, ls, lua_main_filename, lua_dir); + rules = new digwatch_rules(inspector, ls, lua_main_filename); digwatch_formats::init(inspector, ls); digwatch_fields::init(inspector, ls); diff --git a/userspace/digwatch/rules.cpp b/userspace/digwatch/rules.cpp index 97fcda16..f93b875d 100644 --- a/userspace/digwatch/rules.cpp +++ b/userspace/digwatch/rules.cpp @@ -7,33 +7,15 @@ extern "C" { } -digwatch_rules::digwatch_rules(sinsp* inspector, lua_State *ls, string lua_main_filename, string lua_dir) +digwatch_rules::digwatch_rules(sinsp* inspector, lua_State *ls, string lua_main_filename) { m_ls = ls; m_lua_parser = new lua_parser(inspector, m_ls); - add_lua_path(lua_dir); load_compiler(lua_main_filename); } -void digwatch_rules::add_lua_path(string path) -{ - path += "?.lua"; - - lua_getglobal(m_ls, "package"); - lua_getfield(m_ls, -1, "path"); - - string cur_path = lua_tostring(m_ls, -1 ); - cur_path += ';'; - - cur_path.append(path.c_str()); - lua_pop(m_ls, 1); - - lua_pushstring(m_ls, cur_path.c_str()); - lua_setfield(m_ls, -2, "path"); - lua_pop(m_ls, 1); -} void digwatch_rules::load_compiler(string lua_main_filename) { diff --git a/userspace/digwatch/rules.h b/userspace/digwatch/rules.h index 91e7adb2..366a9c1e 100644 --- a/userspace/digwatch/rules.h +++ b/userspace/digwatch/rules.h @@ -6,13 +6,12 @@ class digwatch_rules { public: - digwatch_rules(sinsp* inspector, lua_State *ls, string lua_main_filename, string lua_dir); + digwatch_rules(sinsp* inspector, lua_State *ls, string lua_main_filename); ~digwatch_rules(); void load_rules(string rules_filename); sinsp_filter* get_filter(); private: - void add_lua_path(string path); void load_compiler(string lua_main_filename); lua_parser* m_lua_parser;