#include "lua_console.h"
#include "lua_objs.h"
#include "log.h"
#include <stdlib.h>
#include <lua5.1/lauxlib.h>
static void lua_console_on_line (const char *line, void *arg)
{
struct lua_console *lc = arg;
lua_State *L = lc->lua->st;
int ret;
// ignore empty lines and EOF
if (!line || !(*line))
return;
// XXX: move to nexus_lua
// load the line as a lua function
if ((ret = luaL_loadstring(L, line)))
goto error;
// execute it
if ((ret = lua_pcall(L, 0, 0, 0)))
goto error;
// XXX: display results?
error:
if (ret) {
const char *error = lua_tostring(L, -1);
switch (ret) {
case LUA_ERRSYNTAX:
log_error("syntax error: %s", error);
break;
case LUA_ERRRUN:
log_error("runtime error: %s", error);
break;
case LUA_ERRMEM:
log_error("memory allocation error: %s", error);
break;
case LUA_ERRERR:
log_error("error handling error: %s", error);
break;
default:
log_error("unknown error: %s", error);
break;
};
lua_pop(L, 1);
}
}
static struct console_callbacks _console_callbacks = {
.on_line = &lua_console_on_line,
};
err_t lua_console_create (struct lua_console **lc_ptr, struct console *console, struct nexus_lua *lua, struct error_info *err)
{
struct lua_console *lc;
// allocate
if ((lc = calloc(1, sizeof(*lc))) == NULL)
return SET_ERROR(err, ERR_CALLOC);
// store
lc->console = console;
lc->lua = lua;
// set our console callbacks
console_set_callbacks(console, &_console_callbacks, lc);
// ok
*lc_ptr = lc;
return SUCCESS;
}
void lua_console_destroy (struct lua_console *lc)
{
// the console
console_destroy(lc->console);
free(lc);
}