2021-01-23 16:10:29 -05:00
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "luastack.hpp"
|
|
|
|
|
#include "util.hpp"
|
|
|
|
|
#include "viewer.hpp"
|
|
|
|
|
#include "traceback.hpp"
|
|
|
|
|
#include "textgame.hpp"
|
|
|
|
|
#include "luaconsole.hpp"
|
|
|
|
|
|
|
|
|
|
// Add another error status.
|
|
|
|
|
|
|
|
|
|
static lua_State *globalL = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void lstop(lua_State *L, lua_Debug *ar)
|
|
|
|
|
{
|
|
|
|
|
(void)ar; /* unused arg. */
|
|
|
|
|
lua_sethook(L, NULL, 0, 0);
|
|
|
|
|
/* Avoid luaL_error -- a C hook doesn't add an extra frame. */
|
|
|
|
|
luaL_where(L, 0);
|
|
|
|
|
lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));
|
|
|
|
|
lua_error(L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void laction(int i)
|
|
|
|
|
{
|
|
|
|
|
signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
|
|
|
|
|
terminate process (default action) */
|
|
|
|
|
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l_message(const char *msg)
|
|
|
|
|
{
|
|
|
|
|
fputs(msg, stderr);
|
|
|
|
|
fputc('\n', stderr);
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int report(lua_State *L, int status)
|
|
|
|
|
{
|
|
|
|
|
if (status && !lua_isnil(L, -1))
|
|
|
|
|
{
|
|
|
|
|
const char *msg = lua_tostring(L, -1);
|
|
|
|
|
if (msg == NULL)
|
|
|
|
|
msg = "(error object is not a string)";
|
|
|
|
|
l_message(msg);
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
}
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-23 16:31:16 -05:00
|
|
|
static void doexp(lua_State *L, const std::string &exp)
|
2021-01-23 16:10:29 -05:00
|
|
|
{
|
2021-01-23 16:31:16 -05:00
|
|
|
int status = luaL_loadbuffer(L, exp.c_str(), exp.size(), "=stdin");
|
|
|
|
|
assert(status == LUA_OK);
|
|
|
|
|
globalL = L;
|
2021-01-23 16:10:29 -05:00
|
|
|
signal(SIGINT, laction);
|
2021-01-23 16:31:16 -05:00
|
|
|
status = traceback_pcall(L, 0, LUA_MULTRET);
|
2021-01-23 16:10:29 -05:00
|
|
|
signal(SIGINT, SIG_DFL);
|
2021-01-23 16:31:16 -05:00
|
|
|
if (status != LUA_OK) {
|
|
|
|
|
report(L, status);
|
2021-01-23 16:10:29 -05:00
|
|
|
lua_gc(L, LUA_GCCOLLECT, 0);
|
2021-01-23 16:31:16 -05:00
|
|
|
}
|
|
|
|
|
if (status == LUA_OK && lua_gettop(L) > 0) {
|
|
|
|
|
lua_getglobal(L, "pprint");
|
|
|
|
|
if (lua_isnil(L, -1)) {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_getglobal(L, "print");
|
|
|
|
|
}
|
|
|
|
|
lua_insert(L, 1);
|
|
|
|
|
if (lua_pcall(L, lua_gettop(L) - 1, 0, 0) != 0) {
|
|
|
|
|
l_message(
|
|
|
|
|
lua_pushfstring(L, "error calling 'print' (%s)",
|
|
|
|
|
lua_tostring(L, -1)));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-23 16:10:29 -05:00
|
|
|
}
|
|
|
|
|
|
2021-01-23 16:31:16 -05:00
|
|
|
void TextGame::run()
|
2021-01-23 16:10:29 -05:00
|
|
|
{
|
|
|
|
|
LuaConsole console;
|
|
|
|
|
while (true) {
|
2021-01-23 16:31:16 -05:00
|
|
|
console.add_stdin();
|
2021-01-23 16:10:29 -05:00
|
|
|
switch (console.state()) {
|
|
|
|
|
case LuaConsole::STATE_INCOMPLETE:
|
|
|
|
|
break;
|
2021-01-23 16:31:16 -05:00
|
|
|
case LuaConsole::STATE_COMPLETE:
|
|
|
|
|
doexp(viewer_.get_lua_state(), console.expression());
|
2021-01-23 16:10:29 -05:00
|
|
|
console.clear();
|
|
|
|
|
break;
|
|
|
|
|
case LuaConsole::STATE_SLASHCOMMAND:
|
|
|
|
|
std::cerr << "Slash commands not supported yet." << std::endl;
|
|
|
|
|
console.clear();
|
|
|
|
|
break;
|
|
|
|
|
case LuaConsole::STATE_SYNTAX:
|
|
|
|
|
std::cerr << console.syntax() << std::endl;
|
|
|
|
|
console.clear();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|