f36af4d41055a1d40be3593d12a641929ce2302b
[apps/madmutt.git] / lib-lua / runtime.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2007 Pierre Habouzit
18  */
19
20 #include <lib-lib/lib-lib.h>
21
22 #include <lua.h>
23 #include <lualib.h>
24 #include <lauxlib.h>
25
26 #include "lib-lua.h"
27
28 static lua_State *L;
29
30 static const luaL_Reg lualibs[] = {
31     {"",              luaopen_base},
32     {LUA_LOADLIBNAME, luaopen_package},
33     {LUA_TABLIBNAME,  luaopen_table},
34     {LUA_IOLIBNAME,   luaopen_io},
35     {LUA_STRLIBNAME,  luaopen_string},
36     {LUA_MATHLIBNAME, luaopen_math},
37     {LUA_DBLIBNAME,   luaopen_debug},
38 };
39
40 static const struct luaL_Reg madmutt_module_funcs[] = {
41     {NULL, NULL}
42 };
43
44 static const struct {
45     const char *key;
46     const char *value;
47 } madmutt_module_vars[] = {
48     { "version",    VERSION },
49     /*
50      ** .pp
51      ** \fIThis is a read-only system property and specifies madmutt's
52      ** version string.\fP
53      */
54     { "sysconfdir", SYSCONFDIR },
55     /*
56      ** .pp
57      ** \fIThis is a read-only system property and specifies madmutt's
58      ** subversion revision string.\fP
59      */
60     { "bindir",     BINDIR },
61     /*
62      ** .pp
63      ** \fIThis is a read-only system property and specifies the
64      ** directory containing the madmutt binary.\fP
65      */
66     { "docdir",     PKGDOCDIR },
67     /*
68      ** .pp
69      ** \fIThis is a read-only system property and specifies the
70      ** directory containing the madmutt documentation.\fP
71      */
72 #ifdef USE_HCACHE
73 #if defined(HAVE_QDBM)
74     {"hcache_backend", "qdbm" },
75 #elif defined(HAVE_GDBM)
76     {"hcache_backend", "gdbm" },
77 #elif defined(HAVE_DB4)
78     {"hcache_backend", "db4" },
79 #else
80     {"hcache_backend", "unknown" },
81 #endif
82     /*
83      ** .pp
84      ** \fIThis is a read-only system property and specifies the
85      ** header chaching's database backend.\fP
86      */
87 #endif
88     { NULL,         NULL }
89 };
90
91 void mlua_initialize(void)
92 {
93     int i;
94
95     L = lua_open();
96     for (i = 0; i < countof(lualibs); i++) {
97         lua_pushcfunction(L, lualibs[i].func);
98         lua_pushstring(L, lualibs[i].name);
99         lua_call(L, 1, 0);
100     }
101     luaL_register(L, "madmutt", madmutt_module_funcs);
102
103     for (i = 0; i < countof(madmutt_module_vars); i++) {
104         char buffer[STRING];
105
106         snprintf(buffer, sizeof(buffer), "madmutt.%s = \"%s\";",
107                  madmutt_module_vars[i].key,
108                  madmutt_module_vars[i].value);
109         (void)luaL_dostring(L, buffer);
110     }
111 }
112
113 void mlua_shutdown(void)
114 {
115     lua_close(L);
116 }
117
118
119 int mlua_dofile(const char *filename)
120 {
121     return luaL_dofile(L, filename);
122 }
123
124 int mlua_wrap(void (*errfun)(const char *fmt, ...), int status)
125 {
126     if (status) {
127         (*errfun)("-[lua]-: %s\n", lua_tostring(L, -1));
128         lua_pop(L, 1);
129     }
130     return status;
131 }
132