From 966bcc0d2825ae3f411dacb84f8a2f08a20d24f7 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 19 May 2020 09:13:31 +0200 Subject: [PATCH] chg: adding execute/home/current working directory functions to lua --- client/luascripts/tracetest.lua | 14 +++++----- client/src/preferences.c | 9 ------- client/src/proxmark3.c | 8 ------ client/src/proxmark3.h | 9 +++++++ client/src/scripting.c | 48 ++++++++++++++++++++++++++++++++- 5 files changed, 64 insertions(+), 24 deletions(-) diff --git a/client/luascripts/tracetest.lua b/client/luascripts/tracetest.lua index c1917b401..e45d1ceba 100644 --- a/client/luascripts/tracetest.lua +++ b/client/luascripts/tracetest.lua @@ -7,15 +7,15 @@ local ansicolors = require('ansicolors') copyright = '' author = 'Iceman' -version = 'v1.0.2' +version = 'v1.0.3' desc = [[ -This script will load several traces files in ../traces/ folder and do +This script will load several traces files in current working directory/traces/ folder and do "data load" "lf search 1 u" The following tracefiles will be loaded: em*.pm3 - m*.pm3 + modulation*.pm3 ]] example = [[ 1. script run tracetest @@ -78,8 +78,10 @@ local function main(args) print( string.rep('--',20) ) local cmdDataLoad = 'data load %s'; - local tracesEM = "find '../traces/' -iname 'em*.pm3' -type f" - local tracesMOD = "find '../traces/' -iname 'm*.pm3' -type f" + local cwd = core.cwd(); + + local tracesEM = "find '"..cwd.."/traces/ ' -iname 'em*.pm3' -type f" + local tracesMOD = "find '"..cwd.."/traces/' -iname 'modulation*.pm3' -type f" local write2File = false local outputTemplate = os.date('testtest_%Y-%m-%d_%H%M%S') @@ -100,7 +102,7 @@ local function main(args) end p.close(); - -- Find a set of traces staring with MOD + -- Find a set of traces staring with MODULATION p = assert( io.popen(tracesMOD) ) for file in p:lines() do table.insert(files, file) diff --git a/client/src/preferences.c b/client/src/preferences.c index 0ef715075..4c8fefb77 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -29,15 +29,6 @@ static int CmdHelp(const char *Cmd); static int setCmdHelp(const char *Cmd); -// Load all settings into memory (struct) -#ifdef _WIN32 -#include -#define GetCurrentDir _getcwd -#else -#include -#define GetCurrentDir getcwd -#endif - static char *prefGetFilename(void) { char *path; diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index abf014e08..07d777a2c 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -29,14 +29,6 @@ #include "flash.h" #include "preferences.h" -#ifdef _WIN32 -#include -#define GetCurrentDir _getcwd -#else -#include -#define GetCurrentDir getcwd -#endif - // Used to enable/disable use of preferences json file #define USE_PREFERENCE_FILE diff --git a/client/src/proxmark3.h b/client/src/proxmark3.h index 6402baea9..6d7e9dfb4 100644 --- a/client/src/proxmark3.h +++ b/client/src/proxmark3.h @@ -12,6 +12,7 @@ #ifndef PROXMARK3_H__ #define PROXMARK3_H__ +#include #include "common.h" #define PROXPROMPT_MAX_SIZE 255 @@ -36,6 +37,14 @@ extern "C" { #endif +// Load all settings into memory (struct) +#ifdef _WIN32 +#include +#define GetCurrentDir _getcwd +#else +#define GetCurrentDir getcwd +#endif + int push_cmdscriptfile(char *path, bool stayafter); const char *get_my_executable_path(void); const char *get_my_executable_directory(void); diff --git a/client/src/scripting.c b/client/src/scripting.c index 25bda6322..4a3bab49c 100644 --- a/client/src/scripting.c +++ b/client/src/scripting.c @@ -12,9 +12,11 @@ #include #include +#include #include "lauxlib.h" #include "cmdmain.h" +#include "proxmark3.h" #include "comms.h" #include "mifare/mifarehost.h" #include "crc.h" @@ -28,7 +30,7 @@ #include "mifare/ndef.h" // ndef parsing #include "commonutil.h" #include "ui.h" -#include "proxmark3.h" + #include "crc16.h" #include "protocols.h" #include "fileutils.h" // searchfile @@ -912,6 +914,12 @@ static int l_detect_prng(lua_State *L) { * @return */ static int l_keygen_algoD(lua_State *L) { + //Check number of arguments + int n = lua_gettop(L); + if (n != 1) { + return returnToLuaWithError(L, "Only UID"); + } + size_t size; uint32_t tmp; const char *p_uid = luaL_checklstring(L, 1, &size); @@ -1129,6 +1137,9 @@ static int l_remark(lua_State *L) { return 1; } +// 1. filename +// 2. extension +// output: full search path to file static int l_searchfile(lua_State *L) { //Check number of arguments int n = lua_gettop(L); @@ -1154,6 +1165,38 @@ static int l_searchfile(lua_State *L) { return 1; } +static int l_ud(lua_State *L) { + const char *ud = get_my_user_directory(); + lua_pushstring(L, ud); + return 1; +} +static int l_ewd(lua_State *L) { + const char *ewd = get_my_executable_directory(); + lua_pushstring(L, ewd); + return 1; +} +static int l_cwd(lua_State *L) { + + uint16_t path_len = FILENAME_MAX; // should be a good starting point + bool error = false; + char *cwd = NULL; + cwd = (char *)calloc(path_len, sizeof(uint8_t)); + + while (!error && (GetCurrentDir(cwd, path_len) == NULL)) { + if (errno == ERANGE) { // Need bigger buffer + path_len += 10; // if buffer was too small add 10 characters and try again + cwd = realloc(cwd, path_len); + } else { + error = true; + free(cwd); + return returnToLuaWithError(L, "Failed to get current working directory"); + } + } + lua_pushstring(L, cwd); + free(cwd); + 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. @@ -1215,6 +1258,9 @@ int set_pm3_libraries(lua_State *L) { {"ndefparse", l_ndefparse}, {"fast_push_mode", l_fast_push_mode}, {"search_file", l_searchfile}, + {"cwd", l_cwd}, + {"ewd", l_ewd}, + {"ud", l_ud}, {"rem", l_remark}, {NULL, NULL} };