MCPcopy Create free account
hub / github.com/DFHack/dfhack / internal_runCommand

Function internal_runCommand

library/LuaApi.cpp:4032–4109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4030}
4031
4032static int internal_runCommand(lua_State *L)
4033{
4034 color_ostream *out = NULL;
4035 std::unique_ptr<buffered_color_ostream> out_buffer;
4036 command_result res;
4037 if (lua_gettop(L) == 0)
4038 {
4039 lua_pushstring(L, "");
4040 }
4041 int type_1 = lua_type(L, 1);
4042 bool use_console = lua_toboolean(L, 2);
4043 if (use_console)
4044 {
4045 out = Lua::GetOutput(L);
4046 if (!out)
4047 {
4048 out = &Core::getInstance().getConsole();
4049 }
4050 }
4051 else
4052 {
4053 out_buffer.reset(new buffered_color_ostream());
4054 out = out_buffer.get();
4055 }
4056
4057 if (type_1 == LUA_TTABLE)
4058 {
4059 string command = "";
4060 vector<string> args;
4061 lua_pushnil(L); // first key
4062 while (lua_next(L, 1) != 0)
4063 {
4064 if (command == "")
4065 command = lua_tostring(L, -1);
4066 else
4067 args.push_back(lua_tostring(L, -1));
4068 lua_pop(L, 1); // remove value, leave key
4069 }
4070 CoreSuspender suspend;
4071 res = Core::getInstance().runCommand(*out, command, args);
4072 }
4073 else if (type_1 == LUA_TSTRING)
4074 {
4075 string command = lua_tostring(L, 1);
4076 CoreSuspender suspend;
4077 res = Core::getInstance().runCommand(*out, command);
4078 }
4079 else
4080 {
4081 lua_pushnil(L);
4082 lua_pushfstring(L, "Expected table, got %s", lua_typename(L, type_1));
4083 return 2;
4084 }
4085
4086 lua_newtable(L);
4087 lua_pushinteger(L, (int)res);
4088 lua_setfield(L, -2, "status");
4089

Callers

nothing calls this directly

Calls 15

lua_gettopFunction · 0.85
lua_pushstringFunction · 0.85
lua_typeFunction · 0.85
lua_tobooleanFunction · 0.85
lua_pushnilFunction · 0.85
lua_nextFunction · 0.85
lua_pushfstringFunction · 0.85
lua_typenameFunction · 0.85
lua_pushintegerFunction · 0.85
lua_setfieldFunction · 0.85
lua_createtableFunction · 0.85
lua_rawsetiFunction · 0.85

Tested by

no test coverage detected