]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cliparser/cliparser.c
954220398c655ce5d8009dbb69f8e75afdbbd08e
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
) {
83 return CLIParserParseStringEx(str
, vargtable
, vargtableLen
, allowEmptyExec
, false);
86 int CLIParserParseStringEx(const char* str
, void* vargtable
[], size_t vargtableLen
, bool allowEmptyExec
, bool clueData
) {
88 char *argv
[200] = {NULL
};
90 int len
= strlen(str
);
92 char *spaceptr
= NULL
;
93 enum ParserState state
= PS_FIRST
;
95 argv
[argc
++] = bufptr
;
96 // param0 = program name
97 memcpy(buf
, programName
, strlen(programName
) + 1); // with 0x00
98 bufptr
+= strlen(programName
) + 1;
100 argv
[argc
++] = bufptr
;
103 for (int i
= 0; i
< len
; i
++) {
105 case PS_FIRST
: // first char
106 if (!clueData
|| str
[i
] == '-'){ // first char before space is '-' - next element - option OR not "clueData" for not-option fields
113 argv
[argc
++] = bufptr
;
118 if (state
== PS_FIRST
)
120 if (isSpace(str
[i
])) {
128 if (isSpace(str
[i
])){
133 argv
[argc
++] = bufptr
;
143 return CLIParserParseArg(argc
, argv
, vargtable
, vargtableLen
, allowEmptyExec
);
146 void CLIParserFree() {
147 arg_freetable(argtable
, argtableLen
);
154 int CLIParamHexToBuf(struct arg_str
*argstr
, uint8_t *data
, int maxdatalen
, int *datalen
) {
158 uint8_t buf
[256] = {0};
159 int res
= CLIParamStrToBuf(argstr
, buf
, maxdatalen
* 2, &ibuf
); // *2 because here HEX
163 switch(param_gethex_to_eol((char *)buf
, 0, data
, maxdatalen
, datalen
)) {
165 printf("Parameter error: Invalid HEX value.\n");
168 printf("Parameter error: parameter too large.\n");
171 printf("Parameter error: Hex string must have even number of digits.\n");
178 int CLIParamStrToBuf(struct arg_str
*argstr
, uint8_t *data
, int maxdatalen
, int *datalen
) {
183 uint8_t buf
[256] = {0};
186 for (int i
= 0; i
< argstr
->count
; i
++) {
187 int len
= strlen(argstr
->sval
[i
]);
188 memcpy(&buf
[ibuf
], argstr
->sval
[i
], len
);
196 if (ibuf
> maxdatalen
)
199 memcpy(data
, buf
, ibuf
);