From d7966dbe2d82e4fd5fe582db4bc05d55a99ff20b Mon Sep 17 00:00:00 2001 From: Lefteris Chatzimparmpas Date: Sun, 18 Nov 2012 23:09:29 +0100 Subject: [PATCH] Print stack traceback in case of error. --- src/lua.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lua.c b/src/lua.c index 706f4fc..1bbf9a6 100644 --- a/src/lua.c +++ b/src/lua.c @@ -16,6 +16,7 @@ extern struct sessionhead sessions; static lua_State *lua; /* Lua interpreter state. */ +static int traceback_handler(lua_State *lua); void init_options(void); void interactive_mode(void); @@ -77,8 +78,11 @@ start_lua() "=") || lua_pcall(lua, 0, LUA_MULTRET, 0)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); } else { - if (luaL_loadfile(lua, opts.config) || - lua_pcall(lua, 0, LUA_MULTRET, 0)) + if (luaL_loadfile(lua, opts.config)) + fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); + lua_pushcfunction(lua, traceback_handler); + lua_insert(lua, 1); + if (lua_pcall(lua, 0, LUA_MULTRET, -2)) fatal(ERROR_CONFIG, "%s\n", lua_tostring(lua, -1)); } @@ -87,6 +91,18 @@ start_lua() } +/* + * Add a stack traceback to the error message. + */ +static int +traceback_handler(lua_State *lua) +{ + + luaL_traceback(lua, lua, lua_tostring(lua, 1), 0); + return 1; +} + + /* * Stop the Lua interpreter. */