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