diff --git a/library/Core.cpp b/library/Core.cpp index b927d49cc4..b4fd3195b6 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -84,6 +84,7 @@ distribution. #include #include #include +#include #include #ifdef _WIN32 @@ -416,13 +417,12 @@ bool is_builtin(color_ostream &con, const std::string &command) { return lua_toboolean(L, -1); } -void get_commands(color_ostream &con, std::vector &commands) { +std::optional get_commands(color_ostream &con, std::vector &commands) { ConditionalCoreSuspender suspend{}; if (!suspend) { - con.printerr("Cannot acquire core lock in helpdb.get_commands\n"); commands.clear(); - return; + return "Cannot acquire core lock in helpdb.get_commands"; } auto L = DFHack::Core::getInstance().getLuaState(); @@ -430,22 +430,26 @@ void get_commands(color_ostream &con, std::vector &commands) { if (!lua_checkstack(L, 1) || !Lua::PushModulePublic(con, L, "helpdb", "get_commands")) { - con.printerr("Failed to load helpdb Lua code\n"); - return; + return "Failed to load helpdb Lua code"; } if (!Lua::SafeCall(con, L, 0, 1)) { - con.printerr("Failed Lua call to helpdb.get_commands.\n"); + return "Failed Lua call to helpdb.get_commands."; } Lua::GetVector(L, commands, top + 1); + return std::nullopt; } static bool try_autocomplete(color_ostream &con, const std::string &first, std::string &completed) { std::vector commands, possible; - get_commands(con, commands); + if (auto err = get_commands(con, commands)) { + con.printerr("%s\n", err->c_str()); + return false; + } + for (auto &command : commands) if (command.substr(0, first.size()) == first) possible.push_back(command);