mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-19 21:03:48 -07:00
Update cliparser.c
Adjusted to meet RRG format style and color.
This commit is contained in:
parent
3785adcb91
commit
45ca5c416d
1 changed files with 54 additions and 9 deletions
|
@ -11,10 +11,25 @@
|
||||||
#include "cliparser.h"
|
#include "cliparser.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <util.h> // Get color constants
|
||||||
|
#include <ui.h> // get PrintAndLogEx
|
||||||
|
|
||||||
#ifndef ARRAYLEN
|
#ifndef ARRAYLEN
|
||||||
# define ARRAYLEN(x) (sizeof(x)/sizeof((x)[0]))
|
# define ARRAYLEN(x) (sizeof(x)/sizeof((x)[0]))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Custom Colors
|
||||||
|
// To default the color return s
|
||||||
|
#define _SectionTagColor_(s) _GREEN_(s)
|
||||||
|
#define _ExampleColor_(s) _YELLOW_(s)
|
||||||
|
#define _CommandColor_(s) _RED_(s)
|
||||||
|
#define _DescriptionColor_(s) _CYAN_(s)
|
||||||
|
#define _ArgColor_(s) _WHITE_(s)
|
||||||
|
#define _ArgHelpColor_(s) _WHITE_(s)
|
||||||
|
// End Custom Colors
|
||||||
|
// Option width set to 30 to allow option descriptions to align. approx line 74
|
||||||
|
// Example width set to 50 to allow help descriptions to align. approx line 93
|
||||||
|
|
||||||
int CLIParserInit(CLIParserContext **ctx, const char *vprogramName, const char *vprogramHint, const char *vprogramHelp) {
|
int CLIParserInit(CLIParserContext **ctx, const char *vprogramName, const char *vprogramHint, const char *vprogramHelp) {
|
||||||
*ctx = malloc(sizeof(CLIParserContext));
|
*ctx = malloc(sizeof(CLIParserContext));
|
||||||
if (!*ctx) {
|
if (!*ctx) {
|
||||||
|
@ -40,7 +55,7 @@ int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargta
|
||||||
/* verify the argtable[] entries were allocated sucessfully */
|
/* verify the argtable[] entries were allocated sucessfully */
|
||||||
if (arg_nullcheck(ctx->argtable) != 0) {
|
if (arg_nullcheck(ctx->argtable) != 0) {
|
||||||
/* NULL entries were detected, some allocations must have failed */
|
/* NULL entries were detected, some allocations must have failed */
|
||||||
printf("ERROR: Insufficient memory\n");
|
PrintAndLogEx(ERR,"ERROR: Insufficient memory\n");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
@ -49,14 +64,43 @@ int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargta
|
||||||
|
|
||||||
/* special case: '--help' takes precedence over error reporting */
|
/* 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
|
if ((argc < 2 && !allowEmptyExec) || ((struct arg_lit *)(ctx->argtable)[0])->count > 0) { // help must be the first record
|
||||||
printf("Usage: %s", ctx->programName);
|
|
||||||
arg_print_syntaxv(stdout, ctx->argtable, "\n");
|
|
||||||
if (ctx->programHint)
|
if (ctx->programHint)
|
||||||
printf("%s\n\n", ctx->programHint);
|
PrintAndLogEx(NORMAL,"\n"_DescriptionColor_("%s"), ctx->programHint);
|
||||||
arg_print_glossary(stdout, ctx->argtable, " %-20s %s\n");
|
|
||||||
printf("\n");
|
PrintAndLogEx(NORMAL,"\n"_SectionTagColor_("usage:"));
|
||||||
if (ctx->programHelp)
|
PrintAndLogEx (NORMAL," "_CommandColor_("%s")NOLF, ctx->programName);
|
||||||
printf("%s \n", ctx->programHelp);
|
arg_print_syntaxv(stdout, ctx->argtable, "\n\n");
|
||||||
|
|
||||||
|
PrintAndLogEx(NORMAL,_SectionTagColor_("options:"));
|
||||||
|
|
||||||
|
arg_print_glossary(stdout, ctx->argtable, " "_ArgColor_("%-30s")" "_ArgHelpColor_("%s")"\n");
|
||||||
|
|
||||||
|
PrintAndLogEx(NORMAL,"");
|
||||||
|
if (ctx->programHelp) {
|
||||||
|
PrintAndLogEx(NORMAL,_SectionTagColor_("examples:"));
|
||||||
|
char *buf = NULL;
|
||||||
|
int idx = 0;
|
||||||
|
buf = realloc (buf,strlen (ctx->programHelp)+1); // more then enough as we are splitting
|
||||||
|
|
||||||
|
char *p2; // pointer to split example from comment.
|
||||||
|
for (int i = 0; i < strlen (ctx->programHelp); i++) {
|
||||||
|
buf[idx++] = ctx->programHelp[i];
|
||||||
|
if ((ctx->programHelp[i] == '\n') || (ctx->programHelp[i] == 0x00)) {
|
||||||
|
buf[idx-1] = 0x00;
|
||||||
|
p2 = strstr(buf,"->"); // See if the example has a comment.
|
||||||
|
if (p2 != NULL) {
|
||||||
|
*(p2-1) = 0x00;
|
||||||
|
PrintAndLogEx(NORMAL," "_ExampleColor_("%-50s")" %s",buf,p2);
|
||||||
|
} else {
|
||||||
|
PrintAndLogEx(NORMAL," "_ExampleColor_("%-50s"),buf);
|
||||||
|
}
|
||||||
|
idx = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintAndLogEx(NORMAL,"");
|
||||||
|
free (buf);
|
||||||
|
}
|
||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -66,7 +110,7 @@ int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargta
|
||||||
if (nerrors > 0) {
|
if (nerrors > 0) {
|
||||||
/* Display the error details contained in the arg_end struct.*/
|
/* Display the error details contained in the arg_end struct.*/
|
||||||
arg_print_errors(stdout, ((struct arg_end *)(ctx->argtable)[vargtableLen - 1]), ctx->programName);
|
arg_print_errors(stdout, ((struct arg_end *)(ctx->argtable)[vargtableLen - 1]), ctx->programName);
|
||||||
printf("Try '%s --help' for more information.\n", ctx->programName);
|
PrintAndLogEx(WARNING,"Try '%s --help' for more information.\n", ctx->programName);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
@ -74,6 +118,7 @@ int CLIParserParseArg(CLIParserContext *ctx, int argc, char **argv, void *vargta
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
enum ParserState {
|
enum ParserState {
|
||||||
PS_FIRST,
|
PS_FIRST,
|
||||||
PS_ARGUMENT,
|
PS_ARGUMENT,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue