From: Pierre Habouzit Date: Sat, 10 Mar 2007 01:58:45 +0000 (+0100) Subject: missing add X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=6141042cd0f8e305182da8dd77d21b40d5aa8b4f;p=apps%2Fmadmutt.git missing add Signed-off-by: Pierre Habouzit --- diff --git a/lib-lua/runtime.c b/lib-lua/runtime.c new file mode 100644 index 0000000..f36af4d --- /dev/null +++ b/lib-lua/runtime.c @@ -0,0 +1,132 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * Copyright © 2007 Pierre Habouzit + */ + +#include + +#include +#include +#include + +#include "lib-lua.h" + +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 + { NULL, NULL } +}; + +void mlua_initialize(void) +{ + int i; + + L = lua_open(); + for (i = 0; i < countof(lualibs); i++) { + lua_pushcfunction(L, lualibs[i].func); + 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++) { + char buffer[STRING]; + + snprintf(buffer, sizeof(buffer), "madmutt.%s = \"%s\";", + madmutt_module_vars[i].key, + madmutt_module_vars[i].value); + (void)luaL_dostring(L, buffer); + } +} + +void mlua_shutdown(void) +{ + lua_close(L); +} + + +int mlua_dofile(const char *filename) +{ + return luaL_dofile(L, filename); +} + +int mlua_wrap(void (*errfun)(const char *fmt, ...), int status) +{ + if (status) { + (*errfun)("-[lua]-: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + } + return status; +} +