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