From ff5f5656193e4b7cbd3693137b3c94f52c6bc6e9 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 31 Aug 2020 11:36:35 +0200 Subject: [PATCH] redirect LUA print to Proxmark PrintAndLogEx --- client/src/scripting.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/client/src/scripting.c b/client/src/scripting.c index 416ef2c77..091e951bc 100644 --- a/client/src/scripting.c +++ b/client/src/scripting.c @@ -1129,9 +1129,7 @@ static int l_remark(lua_State *L) { } size_t size; - // data const char *s = luaL_checklstring(L, 1, &size); - int res = CmdRem(s); lua_pushinteger(L, res); return 1; @@ -1199,6 +1197,19 @@ static int l_cwd(lua_State *L) { return 1; } +// ref: https://github.com/RfidResearchGroup/proxmark3/issues/891 +// redirect LUA's print to Proxmark3 PrintAndLogEx +static int l_printandlogex(lua_State* L) { + + int n = lua_gettop(L); + for (int i = 1; i <= n; i++) { + if (lua_isstring(L, i)) { + PrintAndLogEx(NORMAL, "%s", lua_tostring(L, i)); + } + } + return 0; +} + /** * @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. @@ -1269,21 +1280,24 @@ int set_pm3_libraries(lua_State *L) { lua_pushglobaltable(L); // Core library is in this table. Contains ' - //this is 'pm3' table + // this is 'pm3' table lua_newtable(L); - //Put the function into the hash table. + // put the function into the hash table. for (int i = 0; libs[i].name; i++) { lua_pushcfunction(L, libs[i].func); lua_setfield(L, -2, libs[i].name);//set the name, pop stack } - //Name of 'core' + // Name of 'core' lua_setfield(L, -2, "core"); - //-- remove the global environment table from the stack + // remove the global environment table from the stack lua_pop(L, 1); + + // print redirect here + lua_register(L, "print", l_printandlogex); - //--add to the LUA_PATH (package.path in lua) + // add to the LUA_PATH (package.path in lua) // so we can load scripts from various places: const char *exec_path = get_my_executable_directory(); if (exec_path != NULL) {