msleep, hints, auto, now uses cliparser

This commit is contained in:
iceman1001 2020-11-19 20:44:44 +01:00
commit 381a87c138

View file

@ -38,47 +38,10 @@
#include "util_posix.h"
#include "commonutil.h" // ARRAYLEN
#include "preferences.h"
#include "cliparser.h"
static int CmdHelp(const char *Cmd);
static int usage_hints(void) {
PrintAndLogEx(NORMAL, "Turn on/off hints");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: hints [h] <0|1>");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h This help");
PrintAndLogEx(NORMAL, " <0|1> off or on");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, _YELLOW_(" hints 1"));
return PM3_SUCCESS;
}
static int usage_msleep(void) {
PrintAndLogEx(NORMAL, "Sleep for given amount of milliseconds");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: msleep <ms>");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h This help");
PrintAndLogEx(NORMAL, " <ms> time in milliseconds");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, _YELLOW_(" msleep 100"));
return PM3_SUCCESS;
}
static int usage_auto(void) {
PrintAndLogEx(NORMAL, "Run LF SEARCH / HF SEARCH / DATA PLOT / DATA SAVE ");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: auto <ms>");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h This help");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, _YELLOW_(" auto"));
return PM3_SUCCESS;
}
static void AppendDate(char *s, size_t slen, const char *fmt) {
struct tm *ct, tm_buf;
time_t now = time(NULL);
@ -157,18 +120,31 @@ static int lf_search_plus(const char *Cmd) {
return retval;
}
static int CmdAuto(const char *Cmd) {
char ctmp = tolower(param_getchar(Cmd, 0));
if (ctmp == 'h') return usage_auto();
static int CmdAuto(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "auto",
"Run LF SEARCH / HF SEARCH / DATA PLOT / DATA SAVE",
"auto"
);
void *argtable[] = {
arg_param_begin,
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
CLIParserFree(ctx);
PrintAndLogEx(INFO, "lf search");
int ret = CmdLFfind("");
if (ret == PM3_SUCCESS)
if (ret == PM3_SUCCESS)
return ret;
PrintAndLogEx(INFO, "hf search");
ret = CmdHFSearch("");
if (ret == PM3_SUCCESS)
return ret;
PrintAndLogEx(INFO, "lf search - unknown");
ret = lf_search_plus("");
if (ret == PM3_SUCCESS)
return ret;
@ -193,26 +169,35 @@ int CmdRem(const char *Cmd) {
}
static int CmdHints(const char *Cmd) {
uint32_t ms = 0;
char ctmp = tolower(param_getchar(Cmd, 0));
if (ctmp == 'h') return usage_hints();
if (strlen(Cmd) > 1) {
str_lower((char *)Cmd);
if (str_startswith(Cmd, "of")) {
session.show_hints = false;
} else {
session.show_hints = true;
}
} else if (strlen(Cmd) == 1) {
if (param_getchar(Cmd, 0) != 0x00) {
ms = param_get32ex(Cmd, 0, 0, 10);
if (ms == 0) {
session.show_hints = false;
} else {
session.show_hints = true;
}
}
CLIParserContext *ctx;
CLIParserInit(&ctx, "hints",
"Turn on/off hints",
"hints --on\n"
"hints -1\n"
);
void *argtable[] = {
arg_param_begin,
arg_lit0("1", "on", "turn on hints"),
arg_lit0("0", "off", "turn off hints"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
bool turn_on = arg_get_lit(ctx, 1);
bool turn_off = arg_get_lit(ctx, 2);
CLIParserFree(ctx);
if (turn_on && turn_off) {
PrintAndLogEx(ERR, "you can't turn off and on at the same time");
return PM3_EINVARG;
}
if (turn_off) {
session.show_hints = false;
} else if (turn_on) {
session.show_hints = true;
}
PrintAndLogEx(INFO, "Hints are %s", (session.show_hints) ? "ON" : "OFF");
@ -220,14 +205,26 @@ static int CmdHints(const char *Cmd) {
}
static int CmdMsleep(const char *Cmd) {
uint32_t ms = 0;
char ctmp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) < 1 || ctmp == 'h') return usage_msleep();
if (param_getchar(Cmd, 0) != 0x00) {
ms = param_get32ex(Cmd, 0, 0, 10);
if (ms == 0)
return usage_msleep();
CLIParserContext *ctx;
CLIParserInit(&ctx, "msleep",
"Sleep for given amount of milliseconds",
"msleep 100"
);
void *argtable[] = {
arg_param_begin,
arg_int0("t", "ms", "<ms>", "time in milliseconds"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
uint32_t ms = arg_get_u32_def(ctx, 1, 0);
CLIParserFree(ctx);
if (ms == 0) {
PrintAndLogEx(ERR, "Specified invalid input. Can't be zero");
return PM3_EINVARG;
}
msleep(ms);
return PM3_SUCCESS;
}