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