make quadopts be consistently dealt with as strings.
[apps/madmutt.git] / lib-lua / runtime.c
index e4482f2..923222b 100644 (file)
  */
 
 #include <lib-lib/lib-lib.h>
  */
 
 #include <lib-lib/lib-lib.h>
-#include "lib-lua_priv.h"
+#include <lib-lua/lib-lua.h>
 
 
-static lua_State *L;
+#include "../alias.h"
+#include "../mutt.h"
 
 
+static lua_State *L;
 
 void luaM_initialize(void)
 {
 
 void luaM_initialize(void)
 {
@@ -34,7 +36,9 @@ void luaM_initialize(void)
         {LUA_STRLIBNAME,  luaopen_string},
         {LUA_MATHLIBNAME, luaopen_math},
         {LUA_DBLIBNAME,   luaopen_debug},
         {LUA_STRLIBNAME,  luaopen_string},
         {LUA_MATHLIBNAME, luaopen_math},
         {LUA_DBLIBNAME,   luaopen_debug},
-        {"madmutt",       luaopen_madmutt},
+        {"MCore",         luaopen_MCore},
+        {"MTransport",    luaopen_MTransport},
+        {"MAlias",        luaopen_MAlias},
     };
 
     int i;
     };
 
     int i;
@@ -71,8 +75,21 @@ int luaM_wrap(void (*errfun)(const char *fmt, ...), int status)
 quadopt_t luaM_checkquadopt(lua_State *Ls, int narg)
 {
     const char *s;
 quadopt_t luaM_checkquadopt(lua_State *Ls, int narg)
 {
     const char *s;
-    int i = luaL_checkinteger(Ls, narg);
+    int i;
 
 
+    if (lua_type(Ls, narg) == LUA_TSTRING) {
+        s = lua_tostring(Ls, narg);
+        switch (mlua_which_token(s, -1)) {
+          case LTK_YES:     return M_YES;
+          case LTK_NO:      return M_NO;
+          case LTK_ASK_YES: return M_ASKYES;
+          case LTK_ASK_NO:  return M_ASKNO;
+          default:
+            break;
+        }
+    }
+
+    i = luaL_checkinteger(Ls, narg);
     if (i & ~3) {
         s = lua_pushfstring(Ls, "int in [0-3] expected, got %d", i);
         return luaL_argerror(Ls, narg, s);
     if (i & ~3) {
         s = lua_pushfstring(Ls, "int in [0-3] expected, got %d", i);
         return luaL_argerror(Ls, narg, s);
@@ -80,3 +97,43 @@ quadopt_t luaM_checkquadopt(lua_State *Ls, int narg)
     return i;
 }
 
     return i;
 }
 
+void luaM_pushquadopt(lua_State *Ls, int val)
+{
+    switch (val) {
+      case M_YES:    return lua_pushstring(Ls, "yes");
+      case M_NO:     return lua_pushstring(Ls, "no");
+      case M_ASKYES: return lua_pushstring(Ls, "ask-yes");
+      case M_ASKNO:  return lua_pushstring(Ls, "ask-no");
+      default:       return lua_pushnil(Ls);
+    }
+}
+
+const char *luaM_checkrx(lua_State *Ls, int narg)
+{
+    const char *s = luaL_checkstring(Ls, narg);
+    char buf[STRING];
+
+    if (rx_validate(s, buf, ssizeof(buf))) {
+        s = lua_pushfstring(Ls, "invalid regexp: `%s'", buf);
+        luaL_argerror(Ls, narg, s);
+        return NULL;
+    }
+
+    return s;
+}
+
+char *luaM_pathnew(const char *val)
+{
+    char path[PATH_MAX];
+    _mutt_expand_path(path, sizeof(path), val, 0);
+    return m_strdup(path);
+}
+
+rx_t *luaM_rxnew(const char *val)
+{
+    if (m_strisempty(val))
+        val = ".";
+
+    return rx_compile(val, mutt_which_case(val));
+}
+