]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cliparser/cliparser.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2017 Merlok
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Command line parser core commands
9 //-----------------------------------------------------------------------------
11 #include "cliparser.h"
15 void **argtable
= NULL
;
16 size_t argtableLen
= 0;
17 char *programName
= NULL
;
18 char *programHint
= NULL
;
19 char *programHelp
= NULL
;
22 int CLIParserInit(char *vprogramName
, char *vprogramHint
, char *vprogramHelp
) {
25 programName
= vprogramName
;
26 programHint
= vprogramHint
;
27 programHelp
= vprogramHelp
;
28 memset(buf
, 0x00, 500);
33 int CLIParserParseArg(int argc
, char **argv
, void* vargtable
[], size_t vargtableLen
, bool allowEmptyExec
) {
37 argtableLen
= vargtableLen
;
39 /* verify the argtable[] entries were allocated sucessfully */
40 if (arg_nullcheck(argtable
) != 0) {
41 /* NULL entries were detected, some allocations must have failed */
42 printf("ERROR: Insufficient memory\n");
45 /* Parse the command line as defined by argtable[] */
46 nerrors
= arg_parse(argc
, argv
, argtable
);
48 /* special case: '--help' takes precedence over error reporting */
49 if ((argc
< 2 && !allowEmptyExec
) ||((struct arg_lit
*)argtable
[0])->count
> 0) { // help must be the first record
50 printf("Usage: %s", programName
);
51 arg_print_syntaxv(stdout
, argtable
, "\n");
53 printf("%s\n\n", programHint
);
54 arg_print_glossary(stdout
, argtable
, " %-20s %s\n");
57 printf("%s \n", programHelp
);
62 /* If the parser returned any errors then display them and exit */
64 /* Display the error details contained in the arg_end struct.*/
65 arg_print_errors(stdout
, ((struct arg_end
*)argtable
[vargtableLen
- 1]), programName
);
66 printf("Try '%s --help' for more information.\n", programName
);
80 #define isSpace(c)(c == ' ' || c == '\t')
82 int CLIParserParseString(const char* str
, void* vargtable
[], size_t vargtableLen
, bool allowEmptyExec
) {
84 char *argv
[200] = {NULL
};
86 int len
= strlen(str
);
88 char *spaceptr
= NULL
;
89 enum ParserState state
= PS_FIRST
;
91 argv
[argc
++] = bufptr
;
92 // param0 = program name
93 memcpy(buf
, programName
, strlen(programName
) + 1); // with 0x00
94 bufptr
+= strlen(programName
) + 1;
96 argv
[argc
++] = bufptr
;
99 for (int i
= 0; i
< len
; i
++) {
101 case PS_FIRST
: // first char
102 if (str
[i
] == '-'){ // first char before space is '-' - next element - option
109 argv
[argc
++] = bufptr
;
114 if (state
== PS_FIRST
)
116 if (isSpace(str
[i
])) {
124 if (isSpace(str
[i
])){
129 argv
[argc
++] = bufptr
;
139 return CLIParserParseArg(argc
, argv
, vargtable
, vargtableLen
, allowEmptyExec
);
142 void CLIParserFree() {
143 arg_freetable(argtable
, argtableLen
);
150 int CLIParamHexToBuf(struct arg_str
*argstr
, uint8_t *data
, int maxdatalen
, int *datalen
) {
152 if (!strlen(argstr
->sval
[0]))
155 switch(param_gethex_to_eol(argstr
->sval
[0], 0, data
, maxdatalen
, datalen
)) {
157 printf("Parameter error: Invalid HEX value.\n");
160 printf("Parameter error: parameter too large.\n");
163 printf("Parameter error: Hex string must have even number of digits.\n");