From: Pierre Habouzit Date: Sun, 11 Mar 2007 15:29:28 +0000 (+0100) Subject: [lua] More madmutt package upgrades: X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=commitdiff_plain;h=88d22daa9c1316d5ccc4b4bb60d40cc96c767757 [lua] More madmutt package upgrades: * make madmutt be a table where we deal with __index to only allow a few updates. * add a lua-token thing, to recognize efficiently fields we often work with. Signed-off-by: Pierre Habouzit --- diff --git a/lib-lua/.gitignore b/lib-lua/.gitignore new file mode 100644 index 0000000..84af223 --- /dev/null +++ b/lib-lua/.gitignore @@ -0,0 +1 @@ +lua-token.[hc] diff --git a/lib-lua/Makefile.am b/lib-lua/Makefile.am index 02c29a4..78006a1 100644 --- a/lib-lua/Makefile.am +++ b/lib-lua/Makefile.am @@ -1,10 +1,17 @@ +BUILT_SOURCES = lua-token.h lua-token.c +DISTCLEANFILES = $(BUILT_SOURCES) + noinst_LIBRARIES = liblua.a liblua_a_SOURCES = lib-lua.h lib-lua_priv.h \ - runtime.c madmutt.c + runtime.c madmutt.c \ + $(BUILT_SOURCES) noinst_HEADERS = lib-lua.h lib-lua_priv.h +lua-token.c lua-token.h: lua-token.sh + sh $< $@ || (rm -f $@; exit 1) + DEFS=-DPKGDATADIR=\"$(pkgdatadir)\" -DSYSCONFDIR=\"$(sysconfdir)\" \ -DBINDIR=\"$(bindir)\" -DMUTTLOCALEDIR=\"$(datadir)/locale\" \ -DHAVE_CONFIG_H=1 -DPKGDOCDIR=\"$(docdir)\" diff --git a/lib-lua/lib-lua_priv.h b/lib-lua/lib-lua_priv.h index 1dbb7f4..ff68378 100644 --- a/lib-lua/lib-lua_priv.h +++ b/lib-lua/lib-lua_priv.h @@ -21,6 +21,7 @@ #define MUTT_LIB_LUA_LIB_LUA_PRIV_H #include "lib-lua.h" +#include "lua-token.h" #include #include diff --git a/lib-lua/lua-token.sh b/lib-lua/lua-token.sh new file mode 100644 index 0000000..4bf256f --- /dev/null +++ b/lib-lua/lua-token.sh @@ -0,0 +1,101 @@ +#! /bin/sh -e + +die() { + echo "$@" 1>&2 + exit 2 +} + +do_hdr() { + cat < +#include "lua-token.h" + +static const struct tok * +lua_which_token_aux(const char *str, unsigned int len); + +%} +struct tok { const char *name; int val; }; +%% +`awk '{print $0 ", " NR }'` +%% + +enum lua_token lua_which_token(const char *s, ssize_t len) +{ + if (len < 0) + len = m_strlen(s); + + if (len) { + const struct tok *res = lua_which_token_aux(s, len); + return res ? res->val : LTK_UNKNOWN; + } else { + return LTK_UNKNOWN; + } +} +EOF +} + +grep_self() { + grep '^## ' "$1" | cut -d' ' -f2 +} + +trap "rm -f $1" 1 2 3 15 + +rm -f $1 +case "$1" in + *.h) grep_self "$0" | do_h > $1;; + *.c) grep_self "$0" | do_c > $1;; + *) die "you must ask for the 'h' or 'c' generation";; +esac +chmod -w $1 + +exit 0 + +############ Put tokens here ############ +## shell +## sendmail diff --git a/lib-lua/madmutt.c b/lib-lua/madmutt.c index 351ecb5..31ee28a 100644 --- a/lib-lua/madmutt.c +++ b/lib-lua/madmutt.c @@ -52,6 +52,29 @@ static int madmutt_folder_name(lua_State *L) return 1; } +static int madmutt_assign(lua_State *L) +{ + const char *idx = luaL_checkstring(L, 2); + const char *val = luaL_checkstring(L, 3); + + switch (lua_which_token(idx, -1)) { + default: + luaL_error(L, "bad subscript to madmutt: %s", val); + return 0; + + case LTK_SENDMAIL: + case LTK_SHELL: + break; + } + + lua_getmetatable(L, 1); + lua_pushstring(L, idx); + lua_pushstring(L, val); + lua_rawset(L, -3); + + return 0; +} + static const struct luaL_Reg madmutt_module_funcs[] = { {"pwd", madmutt_pwd}, /* @@ -86,6 +109,8 @@ static const struct luaL_Reg madmutt_module_funcs[] = { ** Third and last, the result will be just the name if neither ** $$$folder nor a ``/'' were found in the name. */ + + {"__newindex", madmutt_assign}, {NULL, NULL} }; @@ -138,11 +163,23 @@ static const struct { int luaopen_madmutt(lua_State *L) { int i; - luaL_register(L, LUA_MADMUTT, madmutt_module_funcs); + + lua_newuserdata(L, sizeof(void*)); + luaL_newmetatable(L, "madmutt.core"); + + luaL_openlib(L, NULL, madmutt_module_funcs, 0); for (i = 0; i < countof(madmutt_module_vars); i++) { lua_pushstring(L, madmutt_module_vars[i].value); lua_setfield(L, -2, madmutt_module_vars[i].key); } + + lua_pushstring(L, "__index"); + lua_pushvalue(L, -2); + lua_settable(L, -3); + + lua_setmetatable(L, -2); + lua_setglobal(L, "madmutt"); + return 1; } diff --git a/lib-lua/runtime.c b/lib-lua/runtime.c index 60a6835..8788ed5 100644 --- a/lib-lua/runtime.c +++ b/lib-lua/runtime.c @@ -24,6 +24,7 @@ static lua_State *L; static const luaL_Reg lualibs[] = { {"", luaopen_base}, + {LUA_OSLIBNAME, luaopen_os}, {LUA_LOADLIBNAME, luaopen_package}, {LUA_TABLIBNAME, luaopen_table}, {LUA_IOLIBNAME, luaopen_io},