X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lua%2Fruntime.c;h=b90d460ec61f6be23b3d872241672ea21c7144f1;hp=becd6de67cc99fcda63e47292f08367c6b892bdb;hb=16be8d10413bbcf822577afd4019d65aba7459e0;hpb=84dadd88f9d900c76451ac97b74c9187fab0dd09 diff --git a/lib-lua/runtime.c b/lib-lua/runtime.c index becd6de..b90d460 100644 --- a/lib-lua/runtime.c +++ b/lib-lua/runtime.c @@ -18,77 +18,40 @@ */ #include +#include -#include -#include -#include - -#include "lib-lua.h" +#include "../mutt.h" +#include "../alias.li" +#include "../buffy.li" +#include "../charset.li" +#include "../crypt.li" +#include "../score.li" +#include static lua_State *L; -static const luaL_Reg lualibs[] = { - {"", luaopen_base}, - {LUA_LOADLIBNAME, luaopen_package}, - {LUA_TABLIBNAME, luaopen_table}, - {LUA_IOLIBNAME, luaopen_io}, - {LUA_STRLIBNAME, luaopen_string}, - {LUA_MATHLIBNAME, luaopen_math}, - {LUA_DBLIBNAME, luaopen_debug}, -}; - -static const struct luaL_Reg madmutt_module_funcs[] = { - {NULL, NULL} -}; - -static const struct { - const char *key; - const char *value; -} madmutt_module_vars[] = { - { "version", VERSION }, - /* - ** .pp - ** \fIThis is a read-only system property and specifies madmutt's - ** version string.\fP - */ - { "sysconfdir", SYSCONFDIR }, - /* - ** .pp - ** \fIThis is a read-only system property and specifies madmutt's - ** subversion revision string.\fP - */ - { "bindir", BINDIR }, - /* - ** .pp - ** \fIThis is a read-only system property and specifies the - ** directory containing the madmutt binary.\fP - */ - { "docdir", PKGDOCDIR }, - /* - ** .pp - ** \fIThis is a read-only system property and specifies the - ** directory containing the madmutt documentation.\fP - */ -#ifdef USE_HCACHE -#if defined(HAVE_QDBM) - {"hcache_backend", "qdbm" }, -#elif defined(HAVE_GDBM) - {"hcache_backend", "gdbm" }, -#elif defined(HAVE_DB4) - {"hcache_backend", "db4" }, -#else - {"hcache_backend", "unknown" }, -#endif - /* - ** .pp - ** \fIThis is a read-only system property and specifies the - ** header chaching's database backend.\fP - */ -#endif -}; - -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}, + {"mod_core", luaopen_mod_core}, + {"MTransport", luaopen_MTransport}, + {"MAlias", luaopen_MAlias}, + {"mod_cset", luaopen_mod_cset}, + {"mod_mime", luaopen_mod_mime}, + {"mod_buffy", luaopen_mod_buffy}, + {"mod_ssl", luaopen_mod_ssl}, + {"mod_crypt", luaopen_mod_crypt}, + {"mod_score", luaopen_mod_score}, + }; + int i; L = lua_open(); @@ -97,26 +60,24 @@ void mlua_initialize(void) lua_pushstring(L, lualibs[i].name); lua_call(L, 1, 0); } - luaL_register(L, "madmutt", madmutt_module_funcs); - - 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); - } } -void mlua_shutdown(void) +void luaM_shutdown(void) { lua_close(L); } +lua_State *luaM_getruntime(void) +{ + return 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)); @@ -125,3 +86,76 @@ int mlua_wrap(void (*errfun)(const char *fmt, ...), int status) return status; } + +quadopt_t luaM_checkquadopt(lua_State *Ls, int narg) +{ + 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; +} + +void luaM_pushquadopt(lua_State *Ls, int val) +{ + 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; +} + +rx_t *luaM_rxnew(const char *val) +{ + if (m_strisempty(val)) + val = "."; + + return rx_compile(val, mutt_which_case(val)); +} + +char *luaM_pathnew(const char *val) +{ + char path[_POSIX_PATH_MAX]; + _mutt_expand_path(path, sizeof(path), val, 0); + return m_strdup(path); +} + + +void luaM_pushaddr(lua_State *Ls, address_t *addr) +{ + char s[HUGE_STRING] = ""; + rfc822_addrcat(s, sizeof(s), addr, 0); + lua_pushstring(Ls, s); +}