Move history and logfile to ~/.proxmark3

This commit is contained in:
Philippe Teuwen 2019-08-28 21:15:11 +02:00
commit d2d58db985
5 changed files with 74 additions and 13 deletions

View file

@ -896,6 +896,8 @@ out:
} }
int searchFile(char **foundpath, const char *pm3dir, const char *searchname, const char *suffix) { int searchFile(char **foundpath, const char *pm3dir, const char *searchname, const char *suffix) {
if (foundpath == NULL)
return PM3_EINVARG;
char *filename = filenamemcopy(searchname, suffix); char *filename = filenamemcopy(searchname, suffix);
if (filename == NULL) return PM3_EMALLOC; if (filename == NULL) return PM3_EMALLOC;
int res = searchFinalFile(foundpath, pm3dir, filename); int res = searchFinalFile(foundpath, pm3dir, filename);

View file

@ -102,8 +102,13 @@ main_loop(char *script_cmds_file, char *script_cmd, bool stayInCommandLoop) {
PrintAndLogEx(ERR, "could not open " _YELLOW_("%s") "...", script_cmds_file); PrintAndLogEx(ERR, "could not open " _YELLOW_("%s") "...", script_cmds_file);
} }
read_history(".history"); char *my_history_path = NULL;
if (searchHomeFilePath(&my_history_path, PROXHISTORY, true) != PM3_SUCCESS) {
PrintAndLogEx(ERR, "could not create $HOME/" PROXHISTORY ", no history will be recorded");
my_history_path = NULL;
} else {
read_history(my_history_path);
}
// loops every time enter is pressed... // loops every time enter is pressed...
while (1) { while (1) {
bool printprompt = false; bool printprompt = false;
@ -220,8 +225,10 @@ main_loop(char *script_cmds_file, char *script_cmd, bool stayInCommandLoop) {
if (sf) if (sf)
fclose(sf); fclose(sf);
write_history(".history"); if (my_history_path) {
write_history(my_history_path);
free(my_history_path);
}
if (cmd) { if (cmd) {
free(cmd); free(cmd);
cmd = NULL; cmd = NULL;

View file

@ -18,6 +18,8 @@
#define PROXPROMPT_USB "[usb] pm3 --> " #define PROXPROMPT_USB "[usb] pm3 --> "
#define PROXPROMPT_FPC "[fpc] pm3 --> " #define PROXPROMPT_FPC "[fpc] pm3 --> "
#define PROXPROMPT_OFFLINE "[offline] pm3 --> " #define PROXPROMPT_OFFLINE "[offline] pm3 --> "
#define PROXHISTORY "history.txt"
#define PROXLOG "log_%Y%m%d.txt"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View file

@ -24,6 +24,9 @@
#include <readline/readline.h> #include <readline/readline.h>
#include <complex.h> #include <complex.h>
#include "util.h" #include "util.h"
#include "proxmark3.h" // PROXLOG
#include "fileutils.h"
#include "pm3_cmd.h"
session_arg_t session; session_arg_t session;
@ -36,9 +39,45 @@ bool GridLocked = false;
bool showDemod = true; bool showDemod = true;
pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
static const char *logfilename = "proxmark3.log";
static void fPrintAndLog(FILE *stream, const char *fmt, ...); static void fPrintAndLog(FILE *stream, const char *fmt, ...);
// needed by flasher, so let's put it here instead of fileutils.c
int searchHomeFilePath(char **foundpath, const char *filename, bool create_home) {
if (foundpath == NULL)
return PM3_EINVARG;
char *user_path = getenv("HOME");
if (user_path == NULL)
return PM3_EFILE;
char *path = calloc(strlen(user_path) + strlen(PM3_USER_DIRECTORY) + 1, sizeof(char));
if (path == NULL)
return PM3_EMALLOC;
strcpy(path, user_path);
strcat(path, PM3_USER_DIRECTORY);
#ifdef _WIN32
struct _stat st;
int result = _stat(path, &st);
#else
struct stat st;
int result = stat(path, &st);
#endif
if ((result != 0) && create_home) {
if (mkdir(path, 0700)) {
free(path);
return PM3_EFILE;
}
}
if (filename == NULL) {
*foundpath = path;
return PM3_SUCCESS;
}
path = realloc(path, (strlen(user_path) + strlen(PM3_USER_DIRECTORY) + strlen(filename) + 1) * sizeof(char));
strcat(path, filename);
*foundpath = path;
return PM3_SUCCESS;
}
void PrintAndLogOptions(const char *str[][2], size_t size, size_t space) { void PrintAndLogOptions(const char *str[][2], size_t size, size_t space) {
char buff[2000] = "Options:\n"; char buff[2000] = "Options:\n";
char format[2000] = ""; char format[2000] = "";
@ -166,10 +205,24 @@ static void fPrintAndLog(FILE *stream, const char *fmt, ...) {
pthread_mutex_lock(&print_lock); pthread_mutex_lock(&print_lock);
if (logging && !logfile) { if (logging && !logfile) {
logfile = fopen(logfilename, "a"); char *my_logfile_path = NULL;
if (!logfile) { char filename[40];
fprintf(stderr, "Can't open logfile, logging disabled!\n"); struct tm *timenow;
time_t now = time(NULL);
timenow = gmtime(&now);
strftime(filename, sizeof(filename), PROXLOG, timenow);
if (searchHomeFilePath(&my_logfile_path, filename, true) != PM3_SUCCESS) {
fprintf(stderr, "Could not create $HOME/.proxmark3/%s, no log will be recorded\n", filename);
my_logfile_path = NULL;
logging = 0; logging = 0;
} else {
logfile = fopen(my_logfile_path, "a");
if (logfile == NULL) {
fprintf(stderr, "Can't open logfile %s, logging disabled!\n", my_logfile_path);
logging = 0;
}
printf("Session is logged into %s\n", my_logfile_path);
free(my_logfile_path);
} }
} }
@ -228,10 +281,6 @@ static void fPrintAndLog(FILE *stream, const char *fmt, ...) {
pthread_mutex_unlock(&print_lock); pthread_mutex_unlock(&print_lock);
} }
void SetLogFilename(char *fn) {
logfilename = fn;
}
void SetFlushAfterWrite(bool value) { void SetFlushAfterWrite(bool value) {
flushAfterWrite = value; flushAfterWrite = value;
} }

View file

@ -39,7 +39,6 @@ void ShowGraphWindow(void);
void RepaintGraphWindow(void); void RepaintGraphWindow(void);
void PrintAndLogOptions(const char *str[][2], size_t size, size_t space); void PrintAndLogOptions(const char *str[][2], size_t size, size_t space);
void PrintAndLogEx(logLevel_t level, const char *fmt, ...); void PrintAndLogEx(logLevel_t level, const char *fmt, ...);
void SetLogFilename(char *fn);
void SetFlushAfterWrite(bool value); void SetFlushAfterWrite(bool value);
void memcpy_filter_ansi(void *dest, const void *src, size_t n, bool filter); void memcpy_filter_ansi(void *dest, const void *src, size_t n, bool filter);
@ -49,6 +48,8 @@ extern uint32_t CursorCPos, CursorDPos;
extern bool GridLocked; extern bool GridLocked;
extern bool showDemod; extern bool showDemod;
int searchHomeFilePath(char **foundpath, const char *filename, bool create_home);
extern pthread_mutex_t print_lock; extern pthread_mutex_t print_lock;
void iceIIR_Butterworth(int *data, const size_t len); void iceIIR_Butterworth(int *data, const size_t len);