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