X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lua%2Fruntime.c;h=923222bde7e2a97ad1dff82097d48cc7d9c860f0;hp=7a351ee802129030b95d4ac5b580066aec3db75c;hb=1681dff7a1b69b0dad31d515c466439548b71f94;hpb=0dd5b492f18e549ef95b0ac6765329473e426633 diff --git a/lib-lua/runtime.c b/lib-lua/runtime.c index 7a351ee..923222b 100644 --- a/lib-lua/runtime.c +++ b/lib-lua/runtime.c @@ -18,25 +18,29 @@ */ #include -#include "lib-lua_priv.h" +#include + +#include "../alias.h" +#include "../mutt.h" static lua_State *L; -static reg_entry registry[LTK_count]; - -static const luaL_Reg lualibs[] = { - {"", luaopen_base}, - {LUA_OSLIBNAME, luaopen_os}, - {LUA_LOADLIBNAME, luaopen_package}, - {LUA_TABLIBNAME, luaopen_table}, - {LUA_IOLIBNAME, luaopen_io}, - {LUA_STRLIBNAME, luaopen_string}, - {LUA_MATHLIBNAME, luaopen_math}, - {LUA_DBLIBNAME, luaopen_debug}, - {LUA_MADMUTT, luaopen_madmutt}, -}; - -void mlua_initialize(void) + +void luaM_initialize(void) { + static const luaL_Reg lualibs[] = { + {"", luaopen_base}, + {LUA_OSLIBNAME, luaopen_os}, + {LUA_LOADLIBNAME, luaopen_package}, + {LUA_TABLIBNAME, luaopen_table}, + {LUA_IOLIBNAME, luaopen_io}, + {LUA_STRLIBNAME, luaopen_string}, + {LUA_MATHLIBNAME, luaopen_math}, + {LUA_DBLIBNAME, luaopen_debug}, + {"MCore", luaopen_MCore}, + {"MTransport", luaopen_MTransport}, + {"MAlias", luaopen_MAlias}, + }; + int i; L = lua_open(); @@ -47,18 +51,18 @@ void mlua_initialize(void) } } -void mlua_shutdown(void) +void luaM_shutdown(void) { lua_close(L); } -int mlua_dofile(const char *filename) +int luaM_dofile(const char *filename) { return luaL_dofile(L, filename); } -int mlua_wrap(void (*errfun)(const char *fmt, ...), int status) +int luaM_wrap(void (*errfun)(const char *fmt, ...), int status) { if (status) { (*errfun)("-[lua]-: %s\n", lua_tostring(L, -1)); @@ -67,30 +71,69 @@ int mlua_wrap(void (*errfun)(const char *fmt, ...), int status) return status; } -const char *mlua_reggets(int tk) + +quadopt_t luaM_checkquadopt(lua_State *Ls, int narg) { - if (registry[tk].type != REG_STR) - return NULL; - return registry[tk].s; + const char *s; + int i; + + if (lua_type(Ls, narg) == LUA_TSTRING) { + s = lua_tostring(Ls, narg); + switch (mlua_which_token(s, -1)) { + case LTK_YES: return M_YES; + case LTK_NO: return M_NO; + case LTK_ASK_YES: return M_ASKYES; + case LTK_ASK_NO: return M_ASKNO; + default: + break; + } + } + + i = luaL_checkinteger(Ls, narg); + if (i & ~3) { + s = lua_pushfstring(Ls, "int in [0-3] expected, got %d", i); + return luaL_argerror(Ls, narg, s); + } + return i; } -quadopt_t mlua_reggetq(int tk) +void luaM_pushquadopt(lua_State *Ls, int val) { - if (registry[tk].type != REG_QUAD) - return -1; - return registry[tk].i; + switch (val) { + case M_YES: return lua_pushstring(Ls, "yes"); + case M_NO: return lua_pushstring(Ls, "no"); + case M_ASKYES: return lua_pushstring(Ls, "ask-yes"); + case M_ASKNO: return lua_pushstring(Ls, "ask-no"); + default: return lua_pushnil(Ls); + } +} + +const char *luaM_checkrx(lua_State *Ls, int narg) +{ + const char *s = luaL_checkstring(Ls, narg); + char buf[STRING]; + + if (rx_validate(s, buf, ssizeof(buf))) { + s = lua_pushfstring(Ls, "invalid regexp: `%s'", buf); + luaL_argerror(Ls, narg, s); + return NULL; + } + + return s; } -void mlua_regsets(int tk, const char *s) +char *luaM_pathnew(const char *val) { - reg_entry_wipe(registry + tk); - registry[tk].type = REG_STR; - registry[tk].s = m_strdup(s); + char path[PATH_MAX]; + _mutt_expand_path(path, sizeof(path), val, 0); + return m_strdup(path); } -void mlua_regsetq(int tk, quadopt_t q) +rx_t *luaM_rxnew(const char *val) { - reg_entry_wipe(registry + tk); - registry[tk].type = REG_QUAD; - registry[tk].i = q; + if (m_strisempty(val)) + val = "."; + + return rx_compile(val, mutt_which_case(val)); } +