From efb073738a4a0b44e325f8b35f748d0e8b8e57cd Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 25 Dec 2009 14:49:09 +0000 Subject: moved to lua 5.1 cleaned up lua functions --- README | 6 ++--- src/anylike.c | 68 +++++++++++++++++++++++++++++++++++++++++++++---------- src/configure | 2 +- src/l_crypt.c | 10 ++++---- src/l_crypt.h | 4 ++-- src/l_log.c | 7 +++--- src/l_log.h | 6 ++--- src/main_loop.lua | 2 +- 8 files changed, 75 insertions(+), 30 deletions(-) diff --git a/README b/README index 1781a96..30ca019 100644 --- a/README +++ b/README @@ -13,15 +13,13 @@ using libgcrypt: build-essential libgcrypt11-dev - liblua50-dev - liblualib50-dev + liblua5.1-0-dev using ssl crypto library: build-essential libssl-dev - liblua50-dev - liblualib50-dev + liblua5.1-0-dev if you want to rebuild the manpage: diff --git a/src/anylike.c b/src/anylike.c index 09f488f..ef0669b 100644 --- a/src/anylike.c +++ b/src/anylike.c @@ -27,7 +27,9 @@ #include #include -#include +#include +#include +#include #include "datatypes.h" #include "options.h" @@ -69,19 +71,64 @@ int init_libgcrypt() } #endif + +#define LUA_MAIN_LOOP "main_loop.lua" + +static const luaL_Reg anylike_lualibs[] = { + {"", luaopen_base}, + {LUA_LOADLIBNAME, luaopen_package}, + {LUA_TABLIBNAME, luaopen_table}, + {LUA_STRLIBNAME, luaopen_string}, + {LUA_LOGLIBNAME, luaopen_log}, + {LUA_CRYPTLIBNAME, luaopen_crypt}, + {NULL, NULL} +}; + +void anylike_openlibs(lua_State *L) +{ + const luaL_Reg *lib = anylike_lualibs; + for (; lib->func; lib++) { + lua_pushcfunction(L, lib->func); + lua_pushstring(L, lib->name); + lua_call(L, 1, 0); + } +} + int main_loop(options_t* opt) { - lua_State *L; - L = lua_open(); - luaopen_base(L); - luaopen_string(L); - luaopen_log(L); - luaopen_crypt(L); + lua_State *L; + L = luaL_newstate(); + if(!L) { + log_printf(ERROR, "error creating lua state"); + return -1; + } - lua_dofile(L, "main_loop.lua"); + anylike_openlibs(L); - lua_close(L); + int ret = luaL_loadfile(L, LUA_MAIN_LOOP); + if(ret) { + switch(ret) { + case LUA_ERRSYNTAX: log_printf(ERROR, "luaL_loadfile(%s): syntax error during pre-compilation", LUA_MAIN_LOOP); break; + case LUA_ERRMEM: log_printf(ERROR, "luaL_loadfile(%s): memory allocation error", LUA_MAIN_LOOP); break; + case LUA_ERRFILE: log_printf(ERROR, "luaL_loadfile(%s): file access error", LUA_MAIN_LOOP); break; + default: log_printf(ERROR, "luaL_loadfile(%s): unknown error", LUA_MAIN_LOOP); break; + } + lua_close(L); + return -1; + } + + ret = lua_pcall(L, 0, LUA_MULTRET, 0); + if(ret) { + switch(ret) { + case LUA_ERRRUN: log_printf(ERROR, "lua_pcall(): runtime error"); break; + case LUA_ERRMEM: log_printf(ERROR, "lua_pcall(): memory allocation error"); break; + case LUA_ERRERR: log_printf(ERROR, "lua_pcall(): error while running the error handler function"); break; + } + lua_close(L); + return -1; + } + lua_close(L); return 0; } @@ -138,9 +185,6 @@ int main(int argc, char* argv[]) log_printf(NOTICE, "just started..."); options_parse_post(&opt); - - - priv_info_t priv; if(opt.username_) if(priv_init(&priv, opt.username_, opt.groupname_)) { diff --git a/src/configure b/src/configure index a0e07dc..239208d 100755 --- a/src/configure +++ b/src/configure @@ -27,7 +27,7 @@ TARGET=`uname -s` EBUILD_COMPAT=0 CFLAGS='-g -O2' -LDFLAGS='-g -Wall -O2 -ldl -llua50 -llualib50' +LDFLAGS='-g -Wall -O2 -ldl -llua5.1' CRYPTO_LIB='gcrypt' diff --git a/src/l_crypt.c b/src/l_crypt.c index bdccaf5..d2d551c 100644 --- a/src/l_crypt.c +++ b/src/l_crypt.c @@ -23,16 +23,18 @@ * along with anylike. If not, see . */ -#include +#include +#include #include "l_crypt.h" -static int l_crypt_test(lua_State *L) { +static int l_crypt_test(lua_State *L) +{ lua_pushstring(L, "crypt test called"); return 1; } -static const struct luaL_reg cryptlib [] = { +static const struct luaL_reg crypt_funcs [] = { { "test", l_crypt_test }, { NULL, NULL } }; @@ -40,6 +42,6 @@ static const struct luaL_reg cryptlib [] = { LUALIB_API int luaopen_crypt(lua_State *L) { - luaL_openlib(L, "crypt", cryptlib, 0); + luaL_register(L, LUA_CRYPTLIBNAME, crypt_funcs); return 1; } diff --git a/src/l_crypt.h b/src/l_crypt.h index 591bfe4..39784aa 100644 --- a/src/l_crypt.h +++ b/src/l_crypt.h @@ -26,9 +26,9 @@ #ifndef ANYLIKE_l_crypt_h_INCLUDED #define ANYLIKE_l_crypt_h_INCLUDED -#include -#include +#include +#define LUA_CRYPTLIBNAME "crypt" LUALIB_API int luaopen_crypt(lua_State *L); #endif diff --git a/src/l_log.c b/src/l_log.c index 8e95927..d1ab9c1 100644 --- a/src/l_log.c +++ b/src/l_log.c @@ -23,7 +23,8 @@ * along with anylike. If not, see . */ -#include +#include +#include #include #include "log.h" @@ -92,7 +93,7 @@ static int l_log_print(lua_State *L) /* void log_print_hex_dump(log_prio_t prio, const u_int8_t* buf, u_int32_t len); */ -static const struct luaL_reg loglib [] = { +static const struct luaL_reg log_funcs [] = { { "init", l_log_init }, { "close", l_log_close }, { "add_target", l_log_add_target }, @@ -103,6 +104,6 @@ static const struct luaL_reg loglib [] = { LUALIB_API int luaopen_log(lua_State *L) { - luaL_openlib(L, "log", loglib, 0); + luaL_register(L, LUA_LOGLIBNAME, log_funcs); return 1; } diff --git a/src/l_log.h b/src/l_log.h index 97d23c5..483e08b 100644 --- a/src/l_log.h +++ b/src/l_log.h @@ -26,9 +26,9 @@ #ifndef ANYLIKE_l_log_h_INCLUDED #define ANYLIKE_l_log_h_INCLUDED -#include -#include +#include -LUALIB_API int luaopen_log (lua_State *L); +#define LUA_LOGLIBNAME "log" +LUALIB_API int luaopen_log(lua_State *L); #endif diff --git a/src/main_loop.lua b/src/main_loop.lua index b8f7c01..3b025d6 100644 --- a/src/main_loop.lua +++ b/src/main_loop.lua @@ -29,4 +29,4 @@ log.print("DEBUG", "yet another message"); log.print("ERROR", "this is an error"); -log.print("NOTICE", crypt.test()); \ No newline at end of file +log.print("NOTICE", crypt.test()); -- cgit v1.2.3