textual
[RRG-proxmark3.git] / client / src / cmdparser.c
blobf67e18608cc9466ce2ccc5744c4be445e4c0c382
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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 parser
9 //-----------------------------------------------------------------------------
11 #include "cmdparser.h"
13 #include <stdio.h>
14 #include <string.h>
16 #include "ui.h"
17 #include "comms.h"
19 bool AlwaysAvailable(void) {
20 return true;
23 bool IfPm3Present(void) {
24 if (session.help_dump_mode)
25 return false;
26 return session.pm3_present;
29 bool IfPm3Rdv4Fw(void) {
30 if (!IfPm3Present())
31 return false;
32 return (pm3_capabilities.compiled_with_flash) || (pm3_capabilities.compiled_with_smartcard);
35 bool IfPm3Flash(void) {
36 if (!IfPm3Present())
37 return false;
38 if (!pm3_capabilities.compiled_with_flash)
39 return false;
40 return pm3_capabilities.hw_available_flash;
43 bool IfPm3Smartcard(void) {
44 if (!IfPm3Present())
45 return false;
46 if (!pm3_capabilities.compiled_with_smartcard)
47 return false;
48 return pm3_capabilities.hw_available_smartcard;
51 bool IfPm3FpcUsart(void) {
52 if (!IfPm3Present())
53 return false;
54 return pm3_capabilities.compiled_with_fpc_usart;
57 bool IfPm3FpcUsartHost(void) {
58 if (!IfPm3Present())
59 return false;
60 return pm3_capabilities.compiled_with_fpc_usart_host;
63 bool IfPm3FpcUsartHostFromUsb(void) {
64 // true if FPC USART Host support and if talking from USB-CDC interface
65 if (!IfPm3Present())
66 return false;
67 if (!pm3_capabilities.compiled_with_fpc_usart_host)
68 return false;
69 return !conn.send_via_fpc_usart;
72 bool IfPm3FpcUsartDevFromUsb(void) {
73 // true if FPC USART developer support and if talking from USB-CDC interface
74 if (!IfPm3Present())
75 return false;
76 if (!pm3_capabilities.compiled_with_fpc_usart_dev)
77 return false;
78 return !conn.send_via_fpc_usart;
81 bool IfPm3FpcUsartFromUsb(void) {
82 // true if FPC USART Host or developer support and if talking from USB-CDC interface
83 return IfPm3FpcUsartHostFromUsb() || IfPm3FpcUsartDevFromUsb();
86 bool IfPm3Lf(void) {
87 if (!IfPm3Present())
88 return false;
89 return pm3_capabilities.compiled_with_lf;
92 bool IfPm3Hitag(void) {
93 if (!IfPm3Present())
94 return false;
95 return pm3_capabilities.compiled_with_hitag;
98 bool IfPm3EM4x50(void) {
99 if (!IfPm3Present())
100 return false;
101 return pm3_capabilities.compiled_with_em4x50;
104 bool IfPm3EM4x70(void) {
106 if (!IfPm3Present())
107 return false;
108 return pm3_capabilities.compiled_with_em4x70;
111 bool IfPm3Hfsniff(void) {
112 if (!IfPm3Present())
113 return false;
114 return pm3_capabilities.compiled_with_hfsniff;
117 bool IfPm3Hfplot(void) {
118 if (!IfPm3Present())
119 return false;
120 return pm3_capabilities.compiled_with_hfplot;
123 bool IfPm3Iso14443a(void) {
124 if (!IfPm3Present())
125 return false;
126 return pm3_capabilities.compiled_with_iso14443a;
129 bool IfPm3Iso14443b(void) {
130 if (!IfPm3Present())
131 return false;
132 return pm3_capabilities.compiled_with_iso14443b;
135 bool IfPm3Iso14443(void) {
136 if (!IfPm3Present())
137 return false;
138 return pm3_capabilities.compiled_with_iso14443a || pm3_capabilities.compiled_with_iso14443b;
141 bool IfPm3Iso15693(void) {
142 if (!IfPm3Present())
143 return false;
144 return pm3_capabilities.compiled_with_iso15693;
147 bool IfPm3Felica(void) {
148 if (!IfPm3Present())
149 return false;
150 return pm3_capabilities.compiled_with_felica;
153 bool IfPm3Legicrf(void) {
154 if (!IfPm3Present())
155 return false;
156 return pm3_capabilities.compiled_with_legicrf;
159 bool IfPm3Iclass(void) {
160 if (!IfPm3Present())
161 return false;
162 return pm3_capabilities.compiled_with_iclass;
165 bool IfPm3NfcBarcode(void) {
166 if (!IfPm3Present())
167 return false;
168 return pm3_capabilities.compiled_with_nfcbarcode;
171 bool IfPm3Lcd(void) {
172 if (!IfPm3Present())
173 return false;
174 return pm3_capabilities.compiled_with_lcd;
178 void CmdsHelp(const command_t Commands[]) {
179 if (Commands[0].Name == NULL) return;
180 int i = 0;
181 while (Commands[i].Name) {
182 if (Commands[i].IsAvailable()) {
183 g_printAndLog = PRINTANDLOG_PRINT;
184 if (Commands[i].Name[0] == '-' || Commands[i].Name[0] == ' ') {
185 PrintAndLogEx(NORMAL, "%-16s %s", Commands[i].Name, Commands[i].Help);
186 } else {
187 PrintAndLogEx(NORMAL, _GREEN_("%-16s")" %s", Commands[i].Name, Commands[i].Help);
189 g_printAndLog = PRINTANDLOG_PRINT | PRINTANDLOG_LOG;
191 ++i;
195 int CmdsParse(const command_t Commands[], const char *Cmd) {
196 // Help dump children
197 if (strcmp(Cmd, "XX_internal_command_dump_XX") == 0) {
198 dumpCommandsRecursive(Commands, 0, false);
199 return PM3_SUCCESS;
201 // Help dump children with help
202 if (strcmp(Cmd, "XX_internal_command_dump_full_XX") == 0) {
203 dumpCommandsRecursive(Commands, 0, true);
204 return PM3_SUCCESS;
206 // Markdown help dump children
207 if (strcmp(Cmd, "XX_internal_command_dump_markdown_XX") == 0) {
208 dumpCommandsRecursive(Commands, 1, false);
209 return PM3_SUCCESS;
211 // Markdown help dump children with help
212 if (strcmp(Cmd, "XX_internal_command_dump_markdown_help_XX") == 0) {
213 dumpCommandsRecursive(Commands, 1, true);
214 return PM3_SUCCESS;
217 if (strcmp(Cmd, "coffee") == 0) {
218 PrintAndLogEx(NORMAL, "");
219 PrintAndLogEx(NORMAL, " ((\n ))\n" _YELLOW_(" .______.\n | |]\n \\ /\n `----ยด\n\n"));
220 return PM3_SUCCESS;
223 if (strcmp(Cmd, "star") == 0) {
224 PrintAndLogEx(NORMAL, "");
225 PrintAndLogEx(NORMAL, " \\o o/");
226 PrintAndLogEx(NORMAL, " v\\ /v");
227 PrintAndLogEx(NORMAL, " <\\ />");
228 PrintAndLogEx(NORMAL, " |\\o/|");
229 PrintAndLogEx(NORMAL, " _\\__o | o__/");
230 PrintAndLogEx(NORMAL, " |/ \\|");
231 PrintAndLogEx(NORMAL, " o/ \\o");
232 PrintAndLogEx(NORMAL, " /v v\\");
233 PrintAndLogEx(NORMAL, " /> <\\");
234 PrintAndLogEx(NORMAL, "");
235 return PM3_SUCCESS;
239 char cmd_name[128];
240 memset(cmd_name, 0, sizeof(cmd_name));
242 int len = 0;
243 // %n == receives an integer of value equal to the number of chars read so far.
244 // len = max 127
245 sscanf(Cmd, "%127s%n", cmd_name, &len);
247 str_lower(cmd_name);
249 // Comment
250 if (cmd_name[0] == '#')
251 return PM3_SUCCESS;
253 // find args, check for -h / --help
254 int tmplen = len;
255 while (Cmd[tmplen] == ' ')
256 ++tmplen;
257 bool request_help = (strcmp(Cmd + tmplen, "-h") == 0) || (strcmp(Cmd + tmplen, "--help") == 0);
259 int i = 0;
260 while (Commands[i].Name) {
261 if (0 == strcmp(Commands[i].Name, cmd_name)) {
262 if ((Commands[i].Help[0] == '{') || // always allow parsing categories
263 request_help || // always allow requesting help
264 Commands[i].IsAvailable()) {
265 break;
266 } else {
267 PrintAndLogEx(WARNING, "This command is " _YELLOW_("not available") " in this mode");
268 return PM3_ENOTIMPL;
271 ++i;
274 /* try to find exactly one prefix-match */
275 if (!Commands[i].Name) {
276 int last_match = 0;
277 int matches = 0;
279 for (i = 0; Commands[i].Name; i++) {
280 if (!strncmp(Commands[i].Name, cmd_name, strlen(cmd_name)) && Commands[i].IsAvailable()) {
281 last_match = i;
282 matches++;
285 if (matches == 1) i = last_match;
288 if (Commands[i].Name) {
289 while (Cmd[len] == ' ')
290 ++len;
291 return Commands[i].Parse(Cmd + len);
292 } else {
293 // show help for selected hierarchy or if command not recognised
294 CmdsHelp(Commands);
297 return PM3_SUCCESS;
300 static char pparent[512] = {0};
301 static char *parent = pparent;
303 void dumpCommandsRecursive(const command_t cmds[], int markdown, bool full_help) {
304 if (cmds[0].Name == NULL) return;
306 int i = 0;
307 int w_cmd = 25;
308 int w_off = 8;
309 // First, dump all single commands, which are not a container for
310 // other commands
311 if (markdown) {
312 PrintAndLogEx(NORMAL, "|%-*s|%-*s|%s", w_cmd, "command", w_off, "offline", "description");
313 PrintAndLogEx(NORMAL, "|%-*s|%-*s|%s", w_cmd, "-------", w_off, "-------", "-----------");
314 } else if (! full_help) {
315 PrintAndLogEx(NORMAL, "%-*s|%-*s|%s", w_cmd, "command", w_off, "offline", "description");
316 PrintAndLogEx(NORMAL, "%-*s|%-*s|%s", w_cmd, "-------", w_off, "-------", "-----------");
319 while (cmds[i].Name) {
321 if ((cmds[i].Name[0] == '-' || strlen(cmds[i].Name) == 0) && ++i) continue;
322 if (cmds[i].Help[0] == '{' && ++i) continue;
324 const char *cmd_offline = "N";
326 if (cmds[i].IsAvailable())
327 cmd_offline = "Y";
328 if (markdown)
329 PrintAndLogEx(NORMAL, "|`%s%-*s`|%-*s|`%s`", parent, w_cmd - (int)strlen(parent) - 2, cmds[i].Name, w_off, cmd_offline, cmds[i].Help);
330 else if (full_help) {
331 PrintAndLogEx(NORMAL, "---------------------------------------------------------------------------------------");
332 PrintAndLogEx(NORMAL, _RED_("%s%-*s\n") "available offline: %s", parent, w_cmd - (int)strlen(parent), cmds[i].Name, cmds[i].IsAvailable() ? _GREEN_("yes") : _RED_("no"));
333 cmds[i].Parse("--help");
334 } else {
335 PrintAndLogEx(NORMAL, "%s%-*s|%-*s|%s", parent, w_cmd - (int)strlen(parent), cmds[i].Name, w_off, cmd_offline, cmds[i].Help);
337 ++i;
339 PrintAndLogEx(NORMAL, "\n");
340 i = 0;
342 // Then, print the categories. These will go into subsections with their own tables
343 while (cmds[i].Name) {
345 if ((cmds[i].Name[0] == '-' || strlen(cmds[i].Name) == 0) && ++i) continue;
346 if (cmds[i].Help[0] != '{' && ++i) continue;
348 if (full_help) {
349 PrintAndLogEx(NORMAL, "=======================================================================================");
350 PrintAndLogEx(NORMAL, _RED_("%s%s\n\n ")_CYAN_("%s\n"), parent, cmds[i].Name, cmds[i].Help);
351 } else {
352 PrintAndLogEx(NORMAL, "### %s%s\n\n %s\n", parent, cmds[i].Name, cmds[i].Help);
354 char currentparent[512] = {0};
355 snprintf(currentparent, sizeof currentparent, "%s%s ", parent, cmds[i].Name);
356 char *old_parent = parent;
357 parent = currentparent;
358 // This is what causes the recursion, since commands Parse-implementation
359 // in turn calls the CmdsParse above.
360 if (markdown)
361 cmds[i].Parse("XX_internal_command_dump_markdown_XX");
362 else if (full_help) {
363 cmds[i].Parse("XX_internal_command_dump_full_XX");
364 } else {
365 cmds[i].Parse("XX_internal_command_dump_XX");
368 parent = old_parent;
369 ++i;