make madmutt build properly in a separate build directory.
[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 #include <lib-lua/lib-lua.h>
22
23 #include "../alias.h"
24 #include "../mutt.h"
25 #include "../charset.h"
26 #include "../buffy.h"
27 #include "../crypt.h"
28 #include <lib-sys/mutt_ssl.li>
29
30 static lua_State *L;
31
32 void luaM_initialize(void)
33 {
34     static const luaL_Reg lualibs[] = {
35         {"",              luaopen_base},
36         {LUA_OSLIBNAME,   luaopen_os},
37         {LUA_LOADLIBNAME, luaopen_package},
38         {LUA_TABLIBNAME,  luaopen_table},
39         {LUA_IOLIBNAME,   luaopen_io},
40         {LUA_STRLIBNAME,  luaopen_string},
41         {LUA_MATHLIBNAME, luaopen_math},
42         {LUA_DBLIBNAME,   luaopen_debug},
43         {"MCore",         luaopen_MCore},
44         {"MTransport",    luaopen_MTransport},
45         {"MAlias",        luaopen_MAlias},
46         {"MCharset",      luaopen_MCharset},
47         {"Mime",          luaopen_Mime},
48         {"Buffy",         luaopen_Buffy},
49         {"mod_ssl",       luaopen_mod_ssl},
50         {"mod_crypt",     luaopen_mod_crypt},
51     };
52
53     int i;
54
55     L = lua_open();
56     for (i = 0; i < countof(lualibs); i++) {
57         lua_pushcfunction(L, lualibs[i].func);
58         lua_pushstring(L, lualibs[i].name);
59         lua_call(L, 1, 0);
60     }
61 }
62
63 void luaM_shutdown(void)
64 {
65     lua_close(L);
66 }
67
68 lua_State *luaM_getruntime(void)
69 {
70     return L;
71 }
72
73 int luaM_dofile(const char *filename)
74 {
75     return luaL_dofile(L, filename);
76 }
77
78 int luaM_wrap(void (*errfun)(const char *fmt, ...), int status)
79 {
80     if (status) {
81         (*errfun)("-[lua]-: %s\n", lua_tostring(L, -1));
82         lua_pop(L, 1);
83     }
84     return status;
85 }
86
87
88 quadopt_t luaM_checkquadopt(lua_State *Ls, int narg)
89 {
90     const char *s;
91     int i;
92
93     if (lua_type(Ls, narg) == LUA_TSTRING) {
94         s = lua_tostring(Ls, narg);
95         switch (mlua_which_token(s, -1)) {
96           case LTK_YES:     return M_YES;
97           case LTK_NO:      return M_NO;
98           case LTK_ASK_YES: return M_ASKYES;
99           case LTK_ASK_NO:  return M_ASKNO;
100           default:
101             break;
102         }
103     }
104
105     i = luaL_checkinteger(Ls, narg);
106     if (i & ~3) {
107         s = lua_pushfstring(Ls, "int in [0-3] expected, got %d", i);
108         return luaL_argerror(Ls, narg, s);
109     }
110     return i;
111 }
112
113 void luaM_pushquadopt(lua_State *Ls, int val)
114 {
115     switch (val) {
116       case M_YES:    return lua_pushstring(Ls, "yes");
117       case M_NO:     return lua_pushstring(Ls, "no");
118       case M_ASKYES: return lua_pushstring(Ls, "ask-yes");
119       case M_ASKNO:  return lua_pushstring(Ls, "ask-no");
120       default:       return lua_pushnil(Ls);
121     }
122 }
123
124 const char *luaM_checkrx(lua_State *Ls, int narg)
125 {
126     const char *s = luaL_checkstring(Ls, narg);
127     char buf[STRING];
128
129     if (rx_validate(s, buf, ssizeof(buf))) {
130         s = lua_pushfstring(Ls, "invalid regexp: `%s'", buf);
131         luaL_argerror(Ls, narg, s);
132         return NULL;
133     }
134
135     return s;
136 }
137
138 rx_t *luaM_rxnew(const char *val)
139 {
140     if (m_strisempty(val))
141         val = ".";
142
143     return rx_compile(val, mutt_which_case(val));
144 }
145
146 char *luaM_pathnew(const char *val)
147 {
148     char path[PATH_MAX];
149     _mutt_expand_path(path, sizeof(path), val, 0);
150     return m_strdup(path);
151 }
152
153
154 void luaM_pushaddr(lua_State *Ls, address_t *addr)
155 {
156     char s[HUGE_STRING] = "";
157     rfc822_addrcat(s, sizeof(s), addr, 0);
158     lua_pushstring(Ls, s);
159 }