chg: adding execute/home/current working directory functions to lua

This commit is contained in:
iceman1001 2020-05-19 09:13:31 +02:00
commit 966bcc0d28
5 changed files with 64 additions and 24 deletions

View file

@ -7,15 +7,15 @@ local ansicolors = require('ansicolors')
copyright = '' copyright = ''
author = 'Iceman' author = 'Iceman'
version = 'v1.0.2' version = 'v1.0.3'
desc = [[ 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" "data load"
"lf search 1 u" "lf search 1 u"
The following tracefiles will be loaded: The following tracefiles will be loaded:
em*.pm3 em*.pm3
m*.pm3 modulation*.pm3
]] ]]
example = [[ example = [[
1. script run tracetest 1. script run tracetest
@ -78,8 +78,10 @@ local function main(args)
print( string.rep('--',20) ) print( string.rep('--',20) )
local cmdDataLoad = 'data load %s'; local cmdDataLoad = 'data load %s';
local tracesEM = "find '../traces/' -iname 'em*.pm3' -type f" local cwd = core.cwd();
local tracesMOD = "find '../traces/' -iname 'm*.pm3' -type f"
local tracesEM = "find '"..cwd.."/traces/ ' -iname 'em*.pm3' -type f"
local tracesMOD = "find '"..cwd.."/traces/' -iname 'modulation*.pm3' -type f"
local write2File = false local write2File = false
local outputTemplate = os.date('testtest_%Y-%m-%d_%H%M%S') local outputTemplate = os.date('testtest_%Y-%m-%d_%H%M%S')
@ -100,7 +102,7 @@ local function main(args)
end end
p.close(); p.close();
-- Find a set of traces staring with MOD -- Find a set of traces staring with MODULATION
p = assert( io.popen(tracesMOD) ) p = assert( io.popen(tracesMOD) )
for file in p:lines() do for file in p:lines() do
table.insert(files, file) table.insert(files, file)

View file

@ -29,15 +29,6 @@
static int CmdHelp(const char *Cmd); static int CmdHelp(const char *Cmd);
static int setCmdHelp(const char *Cmd); static int setCmdHelp(const char *Cmd);
// Load all settings into memory (struct)
#ifdef _WIN32
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
static char *prefGetFilename(void) { static char *prefGetFilename(void) {
char *path; char *path;

View file

@ -29,14 +29,6 @@
#include "flash.h" #include "flash.h"
#include "preferences.h" #include "preferences.h"
#ifdef _WIN32
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
// Used to enable/disable use of preferences json file // Used to enable/disable use of preferences json file
#define USE_PREFERENCE_FILE #define USE_PREFERENCE_FILE

View file

@ -12,6 +12,7 @@
#ifndef PROXMARK3_H__ #ifndef PROXMARK3_H__
#define PROXMARK3_H__ #define PROXMARK3_H__
#include <unistd.h>
#include "common.h" #include "common.h"
#define PROXPROMPT_MAX_SIZE 255 #define PROXPROMPT_MAX_SIZE 255
@ -36,6 +37,14 @@
extern "C" { extern "C" {
#endif #endif
// Load all settings into memory (struct)
#ifdef _WIN32
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#define GetCurrentDir getcwd
#endif
int push_cmdscriptfile(char *path, bool stayafter); int push_cmdscriptfile(char *path, bool stayafter);
const char *get_my_executable_path(void); const char *get_my_executable_path(void);
const char *get_my_executable_directory(void); const char *get_my_executable_directory(void);

View file

@ -12,9 +12,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include "lauxlib.h" #include "lauxlib.h"
#include "cmdmain.h" #include "cmdmain.h"
#include "proxmark3.h"
#include "comms.h" #include "comms.h"
#include "mifare/mifarehost.h" #include "mifare/mifarehost.h"
#include "crc.h" #include "crc.h"
@ -28,7 +30,7 @@
#include "mifare/ndef.h" // ndef parsing #include "mifare/ndef.h" // ndef parsing
#include "commonutil.h" #include "commonutil.h"
#include "ui.h" #include "ui.h"
#include "proxmark3.h"
#include "crc16.h" #include "crc16.h"
#include "protocols.h" #include "protocols.h"
#include "fileutils.h" // searchfile #include "fileutils.h" // searchfile
@ -912,6 +914,12 @@ static int l_detect_prng(lua_State *L) {
* @return * @return
*/ */
static int l_keygen_algoD(lua_State *L) { 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; size_t size;
uint32_t tmp; uint32_t tmp;
const char *p_uid = luaL_checklstring(L, 1, &size); const char *p_uid = luaL_checklstring(L, 1, &size);
@ -1129,6 +1137,9 @@ static int l_remark(lua_State *L) {
return 1; return 1;
} }
// 1. filename
// 2. extension
// output: full search path to file
static int l_searchfile(lua_State *L) { static int l_searchfile(lua_State *L) {
//Check number of arguments //Check number of arguments
int n = lua_gettop(L); int n = lua_gettop(L);
@ -1154,6 +1165,38 @@ static int l_searchfile(lua_State *L) {
return 1; 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 * @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. * 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}, {"ndefparse", l_ndefparse},
{"fast_push_mode", l_fast_push_mode}, {"fast_push_mode", l_fast_push_mode},
{"search_file", l_searchfile}, {"search_file", l_searchfile},
{"cwd", l_cwd},
{"ewd", l_ewd},
{"ud", l_ud},
{"rem", l_remark}, {"rem", l_remark},
{NULL, NULL} {NULL, NULL}
}; };