Move the mainline into Lua.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 17 Dec 2013 22:36:55 -0500
changeset 1 0919d17b13f9
parent 0 d7ee4e2ed49d
child 2 16a2d269fd41
Move the mainline into Lua.
1pass.c
1pass.lua
--- a/1pass.c	Tue Dec 17 22:16:27 2013 -0500
+++ b/1pass.c	Tue Dec 17 22:36:55 2013 -0500
@@ -16,21 +16,8 @@
 static const uint8_t zero16[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
 static const char saltprefix[] = { 'S', 'a', 'l', 't', 'e', 'd', '_', '_' };
 
-
-static inline int retvalString(lua_State *L, const char *str)
-{
-    if (str != NULL)
-        lua_pushstring(L, str);
-    else
-        lua_pushnil(L);
-    return 1;
-} // retvalString
-
-
 static inline int retvalStringBytes(lua_State *L, const uint8_t *str, size_t len)
 {
-//size_t i; printf("{\n"); for (i = 0; i < len; i++) { printf(" 0x%X\n", (unsigned int) str[i]); } printf(" }\n\n");
-
     if (str != NULL)
         lua_pushlstring(L, (const char *) str, len);
     else
@@ -260,7 +247,7 @@
 } // initLua
 
 
-void deinitLua(void)
+static void deinitLua(void)
 {
     if (luaState != NULL)
     {
@@ -270,52 +257,16 @@
 } // deinitLua
 
 
-
-static char *loadKey(const char *baseDir, const char *level, const char *password)
-{
-    char *retval = NULL;
-    lua_getglobal(luaState, "loadKey");
-    lua_pushstring(luaState, baseDir);
-    lua_pushstring(luaState, level);
-    lua_pushstring(luaState, password);
-    lua_call(luaState, 3, 1);
-    const char *str = lua_tostring(luaState, -1);
-    if (str)
-        retval = strdup(str);
-    lua_pop(luaState, 1);
-    return retval;
-} // luafunc_loadKey
-
-
-static char *sl5 = NULL;
-
 int main(int argc, char **argv)
 {
-    const char *basedir = "1Password/1Password.agilekeychain/data/default";  // !!! FIXME
+    atexit(deinitLua);
 
-    char *password = NULL;
-    size_t pwlen = 0;
-    printf("password: "); fflush(stdout);
-    const ssize_t rc = getline(&password, &pwlen, stdin);
-    if (rc == -1)
-        return 1;
-    else if (password[rc-1] == '\n')
-        password[rc-1] = 0;
-
-    if (!initLua())
+    if (!initLua())  // this will move control to 1pass.lua
     {
         fprintf(stderr, "uhoh\n");
         return 1;
     } // if
 
-    sl5 = loadKey(basedir, "SL5", password);
-    if (!sl5)
-    {
-        fprintf(stderr, "wrong password?\n");
-        return 1;
-    } // if
-
-    free(sl5);
     return 0;
 } // main
 
--- a/1pass.lua	Tue Dec 17 22:16:27 2013 -0500
+++ b/1pass.lua	Tue Dec 17 22:36:55 2013 -0500
@@ -88,7 +88,7 @@
     return nil
 end
 
-function showHint(basedir)
+local function showHint(basedir)
     local f = io.open(basedir .. "/.password.hint", "r")
     if (f == nil) then
         return
@@ -100,5 +100,22 @@
     print("(hint is '" .. str .. "').")
 end
 
+
+-- Mainline!
+
+local basedir = "1Password/1Password.agilekeychain/data/default"  -- !!! FIXME
+
+showHint(basedir)
+io.write("password: ")
+local password = io.read("*l")
+
+local sl5 = loadKey(basedir, "SL5", password)
+if sl5 == nil then
+    print("wrong password?\n")
+    os.exit(1)
+end
+
+
+
 -- end of 1pass.lua ...