]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/scripting.c
fixed some more compiler warnings
[proxmark3-svn] / client / scripting.c
index edaa926c73a0a5d4b7077143af0b0e42773a1b09..ae11d7caacbea057bb3ab4999e131d3a861db96a 100644 (file)
@@ -51,7 +51,7 @@ static int l_SendCommand(lua_State *L){
     }
 
 //    UsbCommand c = (*data);
-    SendCommand(data);
+    SendCommand((UsbCommand* )data);
     return 0; // no return values
 }
 /**
@@ -93,7 +93,7 @@ static int l_WaitForResponseTimeout(lua_State *L){
     if(WaitForResponseTimeout(cmd, &response, ms_timeout))
     {
         //Push it as a string
-         lua_pushlstring(L,&response,sizeof(UsbCommand));
+         lua_pushlstring(L,(const char *)&response,sizeof(UsbCommand));
 
         return 1;// return 1 to signal one return value
     }else{
@@ -102,10 +102,11 @@ static int l_WaitForResponseTimeout(lua_State *L){
         return 1;// one return value
     }
 }
-static int l_nonce2key(lua_State *L){ return CmdHF14AMfRdSc(luaL_checkstring(L, 1));}
-static int l_PrintAndLog(lua_State *L){ return CmdHF14AMfDump(luaL_checkstring(L, 1));}
+//static int l_nonce2key(lua_State *L){ return CmdHF14AMfRdSc(luaL_checkstring(L, 1));}
+//static int l_PrintAndLog(lua_State *L){ return CmdHF14AMfDump(luaL_checkstring(L, 1));}
 static int l_clearCommandBuffer(lua_State *L){
     clearCommandBuffer();
+    return 0;
 }
 /**
  * @brief l_foobar is a dummy function to test lua-integration with
@@ -121,7 +122,7 @@ static int l_foobar(lua_State *L)
     printf("Arguments discarded, stack now contains %d elements", lua_gettop(L));
     UsbCommand response =  {CMD_MIFARE_READBL, {1337, 1338, 1339}};
     printf("Now returning a UsbCommand as a string");
-    lua_pushlstring(L,&response,sizeof(UsbCommand));
+    lua_pushlstring(L,(const char *)&response,sizeof(UsbCommand));
     return 1;
 }
 
@@ -136,13 +137,38 @@ static int l_ukbhit(lua_State *L)
     return 1;
 }
 
+/**
+ * @brief Sets the lua path to include "./lualibs/?.lua", in order for a script to be
+ * able to do "require('foobar')" if foobar.lua is within lualibs folder.
+ * Taken from http://stackoverflow.com/questions/4125971/setting-the-global-lua-path-variable-from-c-c
+ * @param L
+ * @param path
+ * @return
+ */
+int setLuaPath( lua_State* L, const char* path )
+{
+    lua_getglobal( L, "package" );
+    lua_getfield( L, -1, "path" ); // get field "path" from table at top of stack (-1)
+    const char* cur_path = lua_tostring( L, -1 ); // grab path string from top of stack
+    int requiredLength = strlen(cur_path)+ strlen(path)+10; //A few bytes too many, whatever we can afford it
+    char * buf = malloc(requiredLength);
+    snprintf(buf, requiredLength, "%s;%s", cur_path, path);
+    lua_pop( L, 1 ); // get rid of the string on the stack we just pushed on line 5
+    lua_pushstring( L, buf ); // push the new one
+    lua_setfield( L, -2, "path" ); // set the field "path" in table at -2 with value at top of stack
+    lua_pop( L, 1 ); // get rid of package table from top of stack
+    return 0; // all done!
+}
+
+
 int set_pm3_libraries(lua_State *L)
 {
+
     static const luaL_Reg libs[] = {
         {"SendCommand",                 l_SendCommand},
         {"WaitForResponseTimeout",      l_WaitForResponseTimeout},
-        {"nonce2key",                   l_nonce2key},
-        {"PrintAndLog",                 l_PrintAndLog},
+        //{"nonce2key",                   l_nonce2key},
+        //{"PrintAndLog",                 l_PrintAndLog},
         {"foobar",                      l_foobar},
         {"ukbhit",                      l_ukbhit},
         {"clearCommandBuffer",          l_clearCommandBuffer},
@@ -164,5 +190,10 @@ int set_pm3_libraries(lua_State *L)
 
     //-- remove the global environment table from the stack
     lua_pop(L, 1);
+
+    //-- Last but not least, add to the LUA_PATH (package.path in lua)
+    // so we can load libraries from the ./lualib/ - directory
+    setLuaPath(L,"./lualibs/?.lua");
+
     return 1;
 }
Impressum, Datenschutz