Moved command lines into Lua.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 19 Dec 2013 09:27:25 -0500
changeset 7 682d7ea1e7f3
parent 6 b7f19e0599db
child 8 eada72719d17
Moved command lines into Lua.
1pass.c
1pass.lua
--- a/1pass.c	Wed Dec 18 00:24:01 2013 -0500
+++ b/1pass.c	Thu Dec 19 09:27:25 2013 -0500
@@ -168,6 +168,7 @@
 } // decryptBase64UsingKey
 
 
+
 static void *luaAlloc(void *ud, void *ptr, size_t osize, size_t nsize)
 {
     if (nsize == 0)
@@ -195,7 +196,7 @@
 } // luaFatal
 
 
-static int initLua(void)
+static int initLua(const int argc, char **argv)
 {
     assert(luaState == NULL);
     luaState = lua_newstate(luaAlloc, NULL);
@@ -208,6 +209,17 @@
     luaSetCFunc(luaState, decryptUsingPBKDF2, "decryptUsingPBKDF2");
     luaSetCFunc(luaState, decryptBase64UsingKey, "decryptBase64UsingKey");
 
+    // Set up argv table...
+    lua_newtable(luaState);
+    int i;
+    for (i = 0; i < argc; i++)
+    {
+        lua_pushinteger(luaState, i+1);
+        lua_pushstring(luaState, argv[i]);
+        lua_settable(luaState, -3);
+    } // for
+    lua_setglobal(luaState, "argv");
+
     // Transfer control to Lua...
     if (luaL_dofile(luaState, "1pass.lua") != 0)
     {
@@ -235,7 +247,7 @@
 {
     atexit(deinitLua);
 
-    if (!initLua())  // this will move control to 1pass.lua
+    if (!initLua(argc, argv))  // this will move control to 1pass.lua
         return 1;
 
     return 0;
--- a/1pass.lua	Wed Dec 18 00:24:01 2013 -0500
+++ b/1pass.lua	Thu Dec 19 09:27:25 2013 -0500
@@ -118,11 +118,18 @@
 
 -- Mainline!
 
+--for i,v in ipairs(argv) do
+--    print("argv[" .. i .. "] = " .. v)
+--end
+
 local basedir = "1Password/1Password.agilekeychain/data/default"  -- !!! FIXME
 
-showHint(basedir)
-io.write("password: ")
-local password = io.read("*l")
+local password = argv[3]
+if password == nil then
+    showHint(basedir)
+    io.write("password: ")
+    password = io.read("*l")
+end
 
 if loadKey(basedir, "SL5", password) == nil then
     print("wrong password?\n")