mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-14 18:48:13 -07:00
cliparser CLIGetOptionList
This commit is contained in:
parent
afb06ec72c
commit
afbd80b906
2 changed files with 72 additions and 0 deletions
|
@ -298,6 +298,64 @@ int CLIParamStrToBuf(struct arg_str *argstr, uint8_t *data, int maxdatalen, int
|
|||
return 0;
|
||||
}
|
||||
|
||||
int CLIGetOptionList(struct arg_str *argstr, const CLIParserOption *option_array, int *value) {
|
||||
char data[200] = {0};
|
||||
int datalen = 0;
|
||||
int res = CLIParamStrToBuf(argstr, (uint8_t *)data, sizeof(data), &datalen);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
// no data to check - we do not touch *value, just return
|
||||
if (datalen == 0)
|
||||
return 0;
|
||||
|
||||
str_lower(data);
|
||||
|
||||
int val = -1;
|
||||
int cntr = 0;
|
||||
for (int i = 0; i < CLI_MAX_OPTLIST_LEN; i++) {
|
||||
// end of array
|
||||
if (option_array[i].text == NULL)
|
||||
break;
|
||||
// exact match
|
||||
if (strcmp(option_array[i].text, data) == 0) {
|
||||
*value = option_array[i].code;
|
||||
return 0;
|
||||
}
|
||||
// partial match
|
||||
if (strncmp(option_array[i].text, data, datalen) == 0) {
|
||||
val = option_array[i].code;
|
||||
cntr++;
|
||||
}
|
||||
}
|
||||
|
||||
// check partial match
|
||||
if (cntr == 0) {
|
||||
PrintAndLogEx(ERR, "Parameter error: No similar option to `%s`. Valid options: %s\n", argstr->sval[0], argstr->hdr.datatype);
|
||||
return 20;
|
||||
}
|
||||
if (cntr > 1) {
|
||||
PrintAndLogEx(ERR, "Parameter error: Several options fit to `%s`. Valid options: %s\n", argstr->sval[0], argstr->hdr.datatype);
|
||||
return 21;
|
||||
}
|
||||
|
||||
*value = val;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *CLIGetOptionListStr(const CLIParserOption *option_array, int value) {
|
||||
static const char *errmsg = "n/a";
|
||||
|
||||
for (int i = 0; i < CLI_MAX_OPTLIST_LEN; i++) {
|
||||
if (option_array[i].text == NULL)
|
||||
break;
|
||||
if (option_array[i].code == value)
|
||||
return option_array[i].text;
|
||||
}
|
||||
return errmsg;
|
||||
}
|
||||
|
||||
|
||||
// hexstr -> u64, w optional len input and default value fallback.
|
||||
// 0 = failed
|
||||
// 1 = OK
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue