diff --git a/library/LuaTools.cpp b/library/LuaTools.cpp index 24d6917ff..8e05fbe22 100644 --- a/library/LuaTools.cpp +++ b/library/LuaTools.cpp @@ -283,9 +283,6 @@ static int lua_dfhack_is_interactive(lua_State *S) static int dfhack_lineedit_sync(lua_State *S, Console *pstream) { - if (!pstream) - return 2; - const char *prompt = luaL_optstring(S, 1, ">> "); const char *hfile = luaL_optstring(S, 2, NULL); @@ -299,7 +296,10 @@ static int dfhack_lineedit_sync(lua_State *S, Console *pstream) if (rv < 0) { lua_pushnil(S); - lua_pushstring(S, "input error"); + if (rv == -2) + lua_pushstring(S, "shutdown requested"); + else + lua_pushstring(S, "input error"); return 2; } else @@ -333,8 +333,11 @@ static int dfhack_lineedit(lua_State *S) lua_settop(S, 2); Console *pstream = get_console(S); - if (!pstream) + if (!pstream) { + lua_pushnil(S); + lua_pushstring(S, "no console"); return 2; + } lua_rawgetp(S, LUA_REGISTRYINDEX, &DFHACK_QUERY_COROTABLE_TOKEN); lua_rawgetp(S, -1, S); @@ -1058,7 +1061,11 @@ bool DFHack::Lua::RunCoreQueryLoop(color_ostream &out, lua_State *state, prompt = ">> "; std::string curline; - con.lineedit(prompt,curline,hist); + rv = con.lineedit(prompt,curline,hist); + if (rv < 0) { + rv = rv == -2 ? LUA_OK : LUA_ERRRUN; + break; + } hist.add(curline); { diff --git a/library/lua/utils.lua b/library/lua/utils.lua index a6007a08f..60830e82f 100644 --- a/library/lua/utils.lua +++ b/library/lua/utils.lua @@ -505,17 +505,17 @@ function prompt_yes_no(msg,default) prompt = prompt..' (y/n)[n]: ' end while true do - local rv = dfhack.lineedit(prompt) - if rv then - if string.match(rv,'^[Yy]') then - return true - elseif string.match(rv,'^[Nn]') then - return false - elseif rv == 'abort' then - qerror('User abort') - elseif rv == '' and default ~= nil then - return default - end + local rv,err = dfhack.lineedit(prompt) + if not rv then + qerror(err); + elseif string.match(rv,'^[Yy]') then + return true + elseif string.match(rv,'^[Nn]') then + return false + elseif rv == 'abort' then + qerror('User abort') + elseif rv == '' and default ~= nil then + return default end end end @@ -524,7 +524,10 @@ end function prompt_input(prompt,check,quit_str) quit_str = quit_str or '~~~' while true do - local rv = dfhack.lineedit(prompt) + local rv,err = dfhack.lineedit(prompt) + if not rv then + qerror(err); + end if rv == quit_str then qerror('User abort') end diff --git a/plugins/Brushes.h b/plugins/Brushes.h index 6095a03da..0ef9c147e 100644 --- a/plugins/Brushes.h +++ b/plugins/Brushes.h @@ -214,7 +214,7 @@ DFHack::command_result parseRectangle(DFHack::color_ostream & out, bool hasConsole = true) { using namespace DFHack; - int newWidth = 0, newHeight = 0, newZLevels = 0; + int newWidth = 0, newHeight = 0, newZLevels = 0, rv = 0; if (end > start + 1) { @@ -237,7 +237,8 @@ DFHack::command_result parseRectangle(DFHack::color_ostream & out, str.str(""); str << "Set range width <" << width << "> "; - con.lineedit(str.str(), command, hist); + if ((rv = con.lineedit(str.str(), command, hist)) < 0) + return rv == -2 ? CR_OK : CR_FAILURE; hist.add(command); newWidth = command.empty() ? width : atoi(command.c_str()); } else { @@ -251,7 +252,8 @@ DFHack::command_result parseRectangle(DFHack::color_ostream & out, str.str(""); str << "Set range height <" << height << "> "; - con.lineedit(str.str(), command, hist); + if ((rv = con.lineedit(str.str(), command, hist)) < 0) + return rv == -2 ? CR_OK : CR_FAILURE; hist.add(command); newHeight = command.empty() ? height : atoi(command.c_str()); } else { @@ -265,7 +267,8 @@ DFHack::command_result parseRectangle(DFHack::color_ostream & out, str.str(""); str << "Set range z-levels <" << zLevels << "> "; - con.lineedit(str.str(), command, hist); + if ((rv = con.lineedit(str.str(), command, hist)) < 0) + return rv == -2 ? CR_OK : CR_FAILURE; hist.add(command); newZLevels = command.empty() ? zLevels : atoi(command.c_str()); } else { diff --git a/plugins/liquids.cpp b/plugins/liquids.cpp index f0dcf1ce3..12e92a573 100644 --- a/plugins/liquids.cpp +++ b/plugins/liquids.cpp @@ -188,8 +188,9 @@ command_result df_liquids (color_ostream &out_, vector & parameters) std::stringstream str; print_prompt(str, cur_mode); str << "# "; - if(out.lineedit(str.str(),input,liquids_hist) == -1) - return CR_FAILURE; + int rv; + if((rv = out.lineedit(str.str(),input,liquids_hist)) < 0) + return rv == -2 ? CR_OK : CR_FAILURE; liquids_hist.add(input); commands.clear(); diff --git a/plugins/mode.cpp b/plugins/mode.cpp index 66fb488a6..5258e2007 100644 --- a/plugins/mode.cpp +++ b/plugins/mode.cpp @@ -96,6 +96,7 @@ command_result mode (color_ostream &out_, vector & parameters) string command = ""; bool set = false; bool abuse = false; + int rv = 0; t_gamemodes gm; for(auto iter = parameters.begin(); iter != parameters.end(); iter++) { @@ -139,9 +140,9 @@ command_result mode (color_ostream &out_, vector & parameters) string selected; input_again: CommandHistory hist; - out.lineedit("Enter new mode: ",selected, hist); - if(selected == "c") - return CR_OK; + rv = out.lineedit("Enter new mode: ",selected, hist); + if(rv < 0 || selected == "c") + return rv == -2 ? CR_OK : CR_FAILURE; const char * start = selected.c_str(); char * end = 0; select = strtol(start, &end, 10); @@ -178,14 +179,14 @@ command_result mode (color_ostream &out_, vector & parameters) { CommandHistory hist; string selected; - out.lineedit("Enter new game mode number (c for exit): ",selected, hist); - if(selected == "c") - return CR_OK; + rv = out.lineedit("Enter new game mode number (c for exit): ",selected, hist); + if(rv < 0 || selected == "c") + return rv == -2 ? CR_OK : CR_FAILURE; const char * start = selected.c_str(); gm.g_mode = (GameMode) strtol(start, 0, 10); - out.lineedit("Enter new game type number (c for exit): ",selected, hist); - if(selected == "c") - return CR_OK; + rv = out.lineedit("Enter new game type number (c for exit): ",selected, hist); + if(rv < 0 || selected == "c") + return rv == -2 ? CR_OK : CR_FAILURE; start = selected.c_str(); gm.g_type = (GameType) strtol(start, 0, 10); } diff --git a/plugins/tiletypes.cpp b/plugins/tiletypes.cpp index 74fcbe1b9..1e3362b0b 100644 --- a/plugins/tiletypes.cpp +++ b/plugins/tiletypes.cpp @@ -984,9 +984,10 @@ command_result df_tiletypes (color_ostream &out_, vector & parameters) printState(out); std::string input = ""; + int rv = 0; - if (out.lineedit("tiletypes> ",input,tiletypes_hist) == -1) - return CR_FAILURE; + if ((rv = out.lineedit("tiletypes> ",input,tiletypes_hist)) < 0) + return rv == -2 ? CR_OK : CR_FAILURE; tiletypes_hist.add(input); commands.clear();