Changeset b7ed6ac in nscp for modules/LUAScript/script_wrapper.hpp
- Timestamp:
- 03/21/08 18:15:51 (5 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2, stable
- Children:
- 367bf20
- Parents:
- dd02c15
- File:
-
- 1 edited
-
modules/LUAScript/script_wrapper.hpp (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
modules/LUAScript/script_wrapper.hpp
rd76af81 rb7ed6ac 45 45 return strEx::string_to_wstring(s); 46 46 } 47 48 49 50 51 class Account { 52 lua_Number m_balance; 53 public: 54 static const char className[]; 55 static Luna<Account>::RegType methods[]; 56 57 Account(lua_State *L) { m_balance = luaL_checknumber(L, 1); } 58 int inject(lua_State *L) { 59 m_balance += luaL_checknumber(L, 1); return 0; 60 } 61 int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; } 62 int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; } 63 ~Account() { printf("deleted Account (%p)\n", this); } 64 }; 65 66 const char Account::className[] = "Account"; 67 68 #define method(class, name) {#name, &class::name} 69 70 Luna<Account>::RegType Account::methods[] = { 71 method(Account, inject), 72 method(Account, withdraw), 73 method(Account, balance), 74 {0,0} 75 }; 47 typedef std::pair<std::wstring,int> where_type; 48 where_type where(lua_State *L, int level = 1) { 49 lua_Debug ar; 50 if (lua_getstack(L, level, &ar)) { /* check function at level */ 51 lua_getinfo(L, "Sl", &ar); /* get info about it */ 52 if (ar.currentline > 0) { /* is there info? */ 53 return where_type(s2w(ar.short_src), ar.currentline); 54 } 55 } 56 return where_type(_T("unknown"),0); 57 } 76 58 std::wstring extract_string(lua_State *L) { 77 59 return strEx::string_to_wstring(lua_tostring( L, lua_gettop( L ) )); … … 120 102 lua_pushstring(L, strEx::wstring_to_string(_T("unknown")).c_str()); 121 103 } 122 123 static int inject(lua_State *L) { 124 int nargs = lua_gettop( L ); 125 unsigned int argLen = nargs-1; 126 arrayBuffer::arrayBuffer arguments = arrayBuffer::createArrayBuffer(argLen); 127 for (unsigned int i=argLen;i>0;i--) { 128 std::wstring arg = extract_string(L); 129 arrayBuffer::set(arguments, argLen, i-1, arg); 130 lua_pop(L, 1); 131 } 132 std::wstring command = extract_string(L); 133 lua_pop(L, 1); 134 135 std::wstring msg; 136 std::wstring perf; 137 NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectCommand(command.c_str(), argLen, arguments, msg, perf); 138 push_code(L, ret); 139 lua_pushstring(L, strEx::wstring_to_string(msg).c_str()); 140 lua_pushstring(L, strEx::wstring_to_string(perf).c_str()); 141 return 3; 104 void push_string(lua_State *L, std::wstring s) { 105 lua_pushstring(L, strEx::wstring_to_string(s).c_str()); 142 106 } 143 107 … … 198 162 199 163 }; 164 class nsclient_wrapper { 165 public: 166 167 static int execute (lua_State *L) { 168 try { 169 int nargs = lua_gettop( L ); 170 if (nargs == 0) { 171 return luaL_error(L, "nscp.execute requires atleast 1 argument!"); 172 } 173 unsigned int argLen = nargs-1; 174 arrayBuffer::arrayBuffer arguments = arrayBuffer::createArrayBuffer(argLen); 175 for (unsigned int i=argLen;i>0;i--) { 176 std::wstring arg = extract_string(L); 177 arrayBuffer::set(arguments, argLen, i-1, arg); 178 lua_pop(L, 1); 179 } 180 std::wstring command = extract_string(L); 181 lua_pop(L, 1); 182 std::wstring msg; 183 std::wstring perf; 184 NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectCommand(command.c_str(), argLen, arguments, msg, perf); 185 push_code(L, ret); 186 lua_pushstring(L, strEx::wstring_to_string(msg).c_str()); 187 lua_pushstring(L, strEx::wstring_to_string(perf).c_str()); 188 return 3; 189 } catch (...) { 190 return luaL_error(L, "Unknown exception in: nscp.execute"); 191 } 192 } 193 194 static int register_command(lua_State *L) { 195 try { 196 lua_handler *handler = lua_manager::get_handler(L); 197 lua_script *script = lua_manager::get_script(L); 198 int nargs = lua_gettop( L ); 199 if (nargs != 2) 200 return luaL_error(L, "Incorrect syntax: nscp.register(<key>, <function>);"); 201 handler->register_command(script, pop_string(L), pop_string(L)); 202 return 0; 203 } catch (LUAException e) { 204 return luaL_error(L, std::string("Error in nscp.register: " + w2s(e.getMessage())).c_str()); 205 } catch (...) { 206 return luaL_error(L, "Unknown exception in: nscp.register"); 207 } 208 } 209 210 static int getSetting (lua_State *L) { 211 int nargs = lua_gettop( L ); 212 if (nargs < 2 || nargs > 3) 213 return luaL_error(L, "Incorrect syntax: nscp.getSetting(<section>, <key>[, <default value>]);"); 214 std::wstring v; 215 if (nargs > 2) 216 v = pop_string(L); 217 std::wstring k = pop_string(L); 218 std::wstring s = pop_string(L); 219 push_string(L, NSCModuleHelper::getSettingsString(s, k, v)); 220 return 1; 221 } 222 static int getSection (lua_State *L) { 223 NSC_DEBUG_MSG_STD(_T("LUA::setSettings")); 224 return 0; 225 } 226 static int info (lua_State *L) { 227 return log_any(L, NSCAPI::log); 228 } 229 static int error (lua_State *L) { 230 return log_any(L, NSCAPI::error); 231 } 232 static int log_any(lua_State *L, int mode) { 233 where_type w = where(L); 234 int nargs = lua_gettop( L ); 235 std::wstring str; 236 for (int i=0;i<nargs;i++) { 237 str += pop_string(L); 238 } 239 NSCModuleHelper::Message(mode, w.first, w.second, str); 240 return 0; 241 } 242 243 static const luaL_Reg my_funcs[]; 244 245 static int luaopen(lua_State *L) { 246 luaL_register(L, "nscp", my_funcs); 247 return 1; 248 } 249 250 251 }; 252 const luaL_Reg nsclient_wrapper::my_funcs[] = { 253 {"execute", execute}, 254 {"info", info}, 255 {"print", info}, 256 {"error", error}, 257 {"register", register_command}, 258 {"getSetting", getSetting}, 259 {"getSection", getSection}, 260 {NULL, NULL} 261 }; 262 200 263 lua_manager::handler_type lua_manager::handlers; 201 264 lua_manager::script_type lua_manager::scripts; 202 265 double lua_manager::last_value = 0; 203 266 char lua_manager::handler_key[] = "registry.key.handler"; 204 char lua_manager::script_key[] = "registry.key.sctrip"; 205 206 static int register_command(lua_State *L) { 207 try { 208 lua_handler *handler = lua_manager::get_handler(L); 209 lua_script *script = lua_manager::get_script(L); 210 int nargs = lua_gettop( L ); 211 if (nargs < 2) { 212 return luaL_error(L, "Missing argument for register_command! usage: register_command(<key>, <function>);"); 213 } 214 if (nargs > 2) { 215 return luaL_error(L, "To many arguments for register_command! usage: register_command(<key>, <function>);"); 216 } 217 handler->register_command(script, pop_string(L), pop_string(L)); 218 return 0; 219 } catch (LUAException e) { 220 return luaL_error(L, std::string("Error: " + w2s(e.getMessage())).c_str()); 221 } catch (...) { 222 return luaL_error(L, "Unknown exception in: register_command"); 223 } 224 } 267 char lua_manager::script_key[] = "registry.key.script"; 268 225 269 class lua_script { 226 270 Lua_State L; … … 232 276 void load() { 233 277 luaL_openlibs(L); 278 nsclient_wrapper::luaopen(L); 234 279 //Luna<Account>::Register(L); 235 lua_register(L, "inject", inject); 236 lua_register(L, "register_command", register_command); 280 //lua_register(L, "register_command", register_command); 237 281 238 282 if (luaL_loadfile(L, strEx::wstring_to_string(script_).c_str()) != 0) {
Note: See TracChangeset
for help on using the changeset viewer.








