]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cliparser/cliparser.c
Merge branch 'master' into emv_argtable
[proxmark3-svn] / client / cliparser / cliparser.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2017 Merlok
3 //
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
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Command line parser core commands
9 //-----------------------------------------------------------------------------
10
11 #include "cliparser.h"
12 #include <stdio.h>
13 #include <string.h>
14
15 void **argtable = NULL;
16 size_t argtableLen = 0;
17 char *programName = NULL;
18 char *programHint = NULL;
19 char *programHelp = NULL;
20 char buf[500] = {0};
21
22 int CLIParserInit(char *vprogramName, char *vprogramHint, char *vprogramHelp) {
23 argtable = NULL;
24 argtableLen = 0;
25 programName = vprogramName;
26 programHint = vprogramHint;
27 programHelp = vprogramHelp;
28 memset(buf, 0x00, 500);
29
30 return 0;
31 }
32
33 int CLIParserParseArg(int argc, char **argv, void* vargtable[], size_t vargtableLen, bool allowEmptyExec) {
34 int nerrors;
35
36 argtable = vargtable;
37 argtableLen = vargtableLen;
38
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");
43 return 2;
44 }
45 /* Parse the command line as defined by argtable[] */
46 nerrors = arg_parse(argc, argv, argtable);
47
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");
52 if (programHint)
53 printf("%s\n\n", programHint);
54 arg_print_glossary(stdout, argtable, " %-20s %s\n");
55 printf("\n");
56 if (programHelp)
57 printf("%s \n", programHelp);
58
59 return 1;
60 }
61
62 /* If the parser returned any errors then display them and exit */
63 if (nerrors > 0) {
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);
67
68 return 3;
69 }
70
71 return 0;
72 }
73
74 enum ParserState {
75 PS_FIRST,
76 PS_ARGUMENT,
77 PS_OPTION,
78 };
79
80 #define isSpace(c)(c == ' ' || c == '\t')
81
82 int CLIParserParseString(const char* str, void* vargtable[], size_t vargtableLen, bool allowEmptyExec) {
83 int argc = 0;
84 char *argv[200] = {NULL};
85
86 int len = strlen(str);
87 char *bufptr = buf;
88 char *spaceptr = NULL;
89 enum ParserState state = PS_FIRST;
90
91 argv[argc++] = bufptr;
92 // param0 = program name
93 memcpy(buf, programName, strlen(programName) + 1); // with 0x00
94 bufptr += strlen(programName) + 1;
95 if (len)
96 argv[argc++] = bufptr;
97
98 // parse params
99 for (int i = 0; i < len; i++) {
100 switch(state){
101 case PS_FIRST: // first char
102 if (str[i] == '-'){ // first char before space is '-' - next element - option
103 state = PS_OPTION;
104
105 if (spaceptr) {
106 bufptr = spaceptr;
107 *bufptr = 0x00;
108 bufptr++;
109 argv[argc++] = bufptr;
110 }
111 }
112 spaceptr = NULL;
113 case PS_ARGUMENT:
114 if (state == PS_FIRST)
115 state = PS_ARGUMENT;
116 if (isSpace(str[i])) {
117 spaceptr = bufptr;
118 state = PS_FIRST;
119 }
120 *bufptr = str[i];
121 bufptr++;
122 break;
123 case PS_OPTION:
124 if (isSpace(str[i])){
125 state = PS_FIRST;
126
127 *bufptr = 0x00;
128 bufptr++;
129 argv[argc++] = bufptr;
130 break;
131 }
132
133 *bufptr = str[i];
134 bufptr++;
135 break;
136 }
137 }
138
139 return CLIParserParseArg(argc, argv, vargtable, vargtableLen, allowEmptyExec);
140 }
141
142 void CLIParserFree() {
143 arg_freetable(argtable, argtableLen);
144 argtable = NULL;
145
146 return;
147 }
148
149 // convertors
150 int CLIParamHexToBuf(struct arg_str *argstr, uint8_t *data, int maxdatalen, int *datalen) {
151 *datalen = 0;
152 if (!strlen(argstr->sval[0]))
153 return 0;
154
155 switch(param_gethex_to_eol(argstr->sval[0], 0, data, maxdatalen, datalen)) {
156 case 1:
157 printf("Parameter error: Invalid HEX value.\n");
158 return 1;
159 case 2:
160 printf("Parameter error: parameter too large.\n");
161 return 2;
162 case 3:
163 printf("Parameter error: Hex string must have even number of digits.\n");
164 return 3;
165 }
166
167 return 0;
168 }
169
170
171
Impressum, Datenschutz