mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-20 05:13:46 -07:00
fix for script run & cliparser
This commit is contained in:
parent
06d61cdb7f
commit
7fbe4063b2
3 changed files with 60 additions and 56 deletions
|
@ -47,24 +47,7 @@ int CLIParserInit(CLIParserContext **ctx, const char *vprogramName, const char *
|
|||
return 0;
|
||||
}
|
||||
|
||||
int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargtable[], size_t vargtableLen, bool allowEmptyExec) {
|
||||
int nerrors;
|
||||
|
||||
ctx->argtable = vargtable;
|
||||
ctx->argtableLen = vargtableLen;
|
||||
|
||||
/* verify the argtable[] entries were allocated sucessfully */
|
||||
if (arg_nullcheck(ctx->argtable) != 0) {
|
||||
/* NULL entries were detected, some allocations must have failed */
|
||||
PrintAndLogEx(ERR, "ERROR: Insufficient memory\n");
|
||||
fflush(stdout);
|
||||
return 2;
|
||||
}
|
||||
/* Parse the command line as defined by argtable[] */
|
||||
nerrors = arg_parse(argc, argv, ctx->argtable);
|
||||
|
||||
/* special case: '--help' takes precedence over error reporting */
|
||||
if ((argc < 2 && !allowEmptyExec) || ((struct arg_lit *)(ctx->argtable)[0])->count > 0) { // help must be the first record
|
||||
void CLIParserPrintHelp(CLIParserContext *ctx) {
|
||||
if (ctx->programHint)
|
||||
PrintAndLogEx(NORMAL, "\n"_DescriptionColor_("%s"), ctx->programHint);
|
||||
|
||||
|
@ -111,6 +94,27 @@ int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargta
|
|||
}
|
||||
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargtable[], size_t vargtableLen, bool allowEmptyExec) {
|
||||
int nerrors;
|
||||
|
||||
ctx->argtable = vargtable;
|
||||
ctx->argtableLen = vargtableLen;
|
||||
|
||||
/* verify the argtable[] entries were allocated sucessfully */
|
||||
if (arg_nullcheck(ctx->argtable) != 0) {
|
||||
/* NULL entries were detected, some allocations must have failed */
|
||||
PrintAndLogEx(ERR, "ERROR: Insufficient memory\n");
|
||||
fflush(stdout);
|
||||
return 2;
|
||||
}
|
||||
/* Parse the command line as defined by argtable[] */
|
||||
nerrors = arg_parse(argc, argv, ctx->argtable);
|
||||
|
||||
/* special case: '--help' takes precedence over error reporting */
|
||||
if ((argc < 2 && !allowEmptyExec) || ((struct arg_lit *)(ctx->argtable)[0])->count > 0) { // help must be the first record
|
||||
CLIParserPrintHelp(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -60,6 +60,7 @@ typedef struct {
|
|||
char buf[1024 + 60];
|
||||
} CLIParserContext;
|
||||
int CLIParserInit(CLIParserContext **ctx, const char *vprogramName, const char *vprogramHint, const char *vprogramHelp);
|
||||
void CLIParserPrintHelp(CLIParserContext *ctx);
|
||||
int CLIParserParseString(CLIParserContext *ctx, const char *str, void *vargtable[], size_t vargtableLen, bool allowEmptyExec);
|
||||
int CLIParserParseStringEx(CLIParserContext *ctx, const char *str, void *vargtable[], size_t vargtableLen, bool allowEmptyExec, bool clueData);
|
||||
int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargtable[], size_t vargtableLen, bool allowEmptyExec);
|
||||
|
|
|
@ -237,30 +237,29 @@ static int CmdScriptRun(const char *Cmd) {
|
|||
CLIParserContext *ctx;
|
||||
CLIParserInit(&ctx, "script run",
|
||||
"Run a Lua, Cmd or Python script",
|
||||
"script run my_script.lua -- --my_script_args"
|
||||
"script run my_script.lua --my_script_args"
|
||||
);
|
||||
void *argtable[] = {
|
||||
arg_param_begin,
|
||||
arg_file1(NULL, NULL, "<filepath>", "script to run"),
|
||||
arg_rem("--", "separator for script params"),
|
||||
arg_strx0(NULL, NULL, "<params>", "params for the script"),
|
||||
arg_param_end
|
||||
};
|
||||
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
||||
|
||||
char preferredName[128] = {0};
|
||||
char arguments[256] = {0};
|
||||
int name_len = 0;
|
||||
int arg_len = 0;
|
||||
static uint8_t luascriptfile_idx = 0;
|
||||
CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)preferredName, sizeof(preferredName), &name_len);
|
||||
// this one removes all spaces
|
||||
// CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)arguments, sizeof(arguments), &arg_len);
|
||||
sscanf(Cmd, "%127s%n %255[^\n\r]%n", preferredName, &name_len, arguments, &arg_len);
|
||||
if ((strcmp(preferredName, "-h") == 0) || (strcmp(preferredName, "--help") == 0)) {
|
||||
ctx->argtable = argtable;
|
||||
ctx->argtableLen = arg_getsize(argtable);
|
||||
CLIParserPrintHelp(ctx);
|
||||
CLIParserFree(ctx);
|
||||
sscanf(Cmd + name_len + 1, "-- %255[^\n\r]%n", arguments, &arg_len);
|
||||
if (arg_len == 0) {
|
||||
sscanf(Cmd + name_len + 1, "%255[^\n\r]%n", arguments, &arg_len);
|
||||
return PM3_ESOFT;
|
||||
}
|
||||
CLIParserFree(ctx);
|
||||
if (strlen(preferredName) == 0) {
|
||||
PrintAndLogEx(FAILED, "no script name provided");
|
||||
PrintAndLogEx(HINT, "Hint: try " _YELLOW_("`script list`") " to see available scripts");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue