diff --git a/client/cliparser/cliparser.c b/client/cliparser/cliparser.c index 8d3e7969..09c8f189 100644 --- a/client/cliparser/cliparser.c +++ b/client/cliparser/cliparser.c @@ -28,7 +28,7 @@ int CLIParserInit(char *vprogramName, char *vprogramHint) { return 0; } -int CLIParserParseArg(int argc, char **argv, void* vargtable[], size_t vargtableLen) { +int CLIParserParseArg(int argc, char **argv, void* vargtable[], size_t vargtableLen, bool allowEmptyExec) { int nerrors; argtable = vargtable; @@ -44,7 +44,7 @@ int CLIParserParseArg(int argc, char **argv, void* vargtable[], size_t vargtable nerrors = arg_parse(argc, argv, argtable); /* special case: '--help' takes precedence over error reporting */ - if (argc < 2 ||((struct arg_lit *)argtable[0])->count > 0) { // help must be the first record + if ((argc < 2 && !allowEmptyExec) ||((struct arg_lit *)argtable[0])->count > 0) { // help must be the first record printf("Usage: %s", programName); arg_print_syntaxv(stdout, argtable, "\n"); if (programHint) @@ -75,7 +75,7 @@ enum ParserState { #define isSpace(c)(c == ' ' || c == '\t') -int CLIParserParseString(const char* str, void* vargtable[], size_t vargtableLen) { +int CLIParserParseString(const char* str, void* vargtable[], size_t vargtableLen, bool allowEmptyExec) { int argc = 0; char *argv[200] = {NULL}; @@ -132,7 +132,7 @@ int CLIParserParseString(const char* str, void* vargtable[], size_t vargtableLen } } - return CLIParserParseArg(argc, argv, vargtable, vargtableLen); + return CLIParserParseArg(argc, argv, vargtable, vargtableLen, allowEmptyExec); } void CLIParserFree() { diff --git a/client/cliparser/cliparser.h b/client/cliparser/cliparser.h index 2999fc74..fe68fb07 100644 --- a/client/cliparser/cliparser.h +++ b/client/cliparser/cliparser.h @@ -10,6 +10,7 @@ #include "argtable3.h" #include "util.h" +#include #define arg_param_begin arg_lit0("hH", "help", "print this help and exit") #define arg_param_end arg_end(20) @@ -18,8 +19,8 @@ #define arg_get_str(n)(((struct arg_str*)argtable[n])) extern int CLIParserInit(char *vprogramName, char *vprogramHint); -extern int CLIParserParseString(const char* str, void* argtable[], size_t vargtableLen); -extern int CLIParserParseArg(int argc, char **argv, void* argtable[], size_t vargtableLen); +extern int CLIParserParseString(const char* str, void* argtable[], size_t vargtableLen, bool allowEmptyExec); +extern int CLIParserParseArg(int argc, char **argv, void* argtable[], size_t vargtableLen, bool allowEmptyExec); extern void CLIParserFree(); extern int CLIParamHexToBuf(struct arg_str *argstr, uint8_t *data, int maxdatalen, int *datalen); diff --git a/client/cmdhf14a.c b/client/cmdhf14a.c index b1be639a..f598ee86 100644 --- a/client/cmdhf14a.c +++ b/client/cmdhf14a.c @@ -133,38 +133,32 @@ int CmdHF14AList(const char *Cmd) int CmdHF14AReader(const char *Cmd) { uint32_t cm = ISO14A_CONNECT; - bool disconnectAfter = true; + bool leaveSignalON = false; - int cmdp = 0; - while(param_getchar(Cmd, cmdp) != 0x00) { - switch(param_getchar(Cmd, cmdp)) { - case 'h': - case 'H': - PrintAndLog("Usage: hf 14a reader [k|x] [3]"); - PrintAndLog(" k keep the field active after command executed"); - PrintAndLog(" x just drop the signal field"); - PrintAndLog(" 3 ISO14443-3 select only (skip RATS)"); - return 0; - case '3': - cm |= ISO14A_NO_RATS; - break; - case 'k': - case 'K': - disconnectAfter = false; - break; - case 'x': - case 'X': - cm = cm - ISO14A_CONNECT; - break; - default: - PrintAndLog("Unknown command."); - return 1; - } - - cmdp++; + CLIParserInit("hf 14a reader", "Executes ISO1443A anticollision-select group of commands."); + void* argtable[] = { + arg_param_begin, + arg_lit0("kK", "keep", "keep the field active after command executed"), + arg_lit0("xX", "drop", "just drop the signal field"), + arg_lit0("3", NULL, "ISO14443-3 select only (skip RATS)"), + arg_param_end + }; + if (CLIParserParseString(Cmd, argtable, sizeof(argtable) / sizeof(argtable[0]), true)){ + CLIParserFree(); + return 0; } - - if (!disconnectAfter) + + leaveSignalON = arg_get_lit(1)->count; + if (arg_get_lit(2)->count) { + cm = cm - ISO14A_CONNECT; + } + if (arg_get_lit(3)->count) { + cm |= ISO14A_NO_RATS; + } + + CLIParserFree(); + + if (leaveSignalON) cm |= ISO14A_NO_DISCONNECT; UsbCommand c = {CMD_READER_ISO_14443a, {cm, 0, 0}}; @@ -196,12 +190,12 @@ int CmdHF14AReader(const char *Cmd) { if(card.ats_len >= 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes PrintAndLog(" ATS : %s", sprint_hex(card.ats, card.ats_len)); } - if (!disconnectAfter) { + if (leaveSignalON) { PrintAndLog("Card is selected. You can now start sending commands"); } } - if (disconnectAfter) { + if (!leaveSignalON) { PrintAndLog("Field dropped."); } @@ -744,7 +738,7 @@ int CmdHF14AAPDU(const char *cmd) { arg_str1(NULL, NULL, "", NULL), arg_param_end }; - if (CLIParserParseString(cmd, argtable, sizeof(argtable) / sizeof(argtable[0]))){ + if (CLIParserParseString(cmd, argtable, sizeof(argtable) / sizeof(argtable[0]), false)){ CLIParserFree(); return 0; }