From 143b1e3469f7161bcb38687a5d6ded28d9f95c3e Mon Sep 17 00:00:00 2001 From: lethosor Date: Tue, 10 Jun 2014 13:41:01 -0400 Subject: [PATCH] Lua runCommand improvements * Return error codes (e.g. CR_FAILURE) when a command fails instead of output * Make dfhack.runCommand() take a list of arguments as well --- library/LuaApi.cpp | 8 +++++++- library/lua/dfhack.lua | 24 ++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 91baf5fbb..d64551bab 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2230,10 +2230,16 @@ static int internal_getDir(lua_State *L) static int internal_runCommand(lua_State *L) { + CoreSuspender suspend; luaL_checktype(L, 1, LUA_TSTRING); std::string command = lua_tostring(L, 1); buffered_color_ostream out; - Core::getInstance().runCommand(out, command); + command_result res = Core::getInstance().runCommand(out, command); + if (res != CR_OK) + { + lua_pushinteger(L, (int)res); + return 1; + } auto fragments = out.fragments(); lua_newtable(L); int i = 1; diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index 10af8b43e..b2f456532 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -10,6 +10,14 @@ local dfhack = dfhack local base_env = dfhack.BASE_G local _ENV = base_env +CR_LINK_FAILURE = -3 +CR_NEEDS_CONSOLE = -2 +CR_NOT_IMPLEMENTED = -1 +CR_OK = 0 +CR_FAILURE = 1 +CR_WRONG_USAGE = 2 +CR_NOT_FOUND = 3 + -- Console color constants COLOR_RESET = -1 @@ -358,11 +366,19 @@ function dfhack.run_script(name,...) return f(...) end -function dfhack.run_command(command, ...) - command = command .. ' ' .. table.concat({...}, ' ') - fragments = internal.runCommand(command) +function dfhack.run_command(...) + args = {...} + if type(args[1]) == 'table' then + command = table.concat(args[1], ' ') + else + command = table.concat(args, ' ') + end + result = internal.runCommand(command) + if type(result) == 'number' then + return result + end output = "" - for i, f in pairs(fragments) do + for i, f in pairs(result) do output = output .. f[2] end return output