1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
11 #include "cmdparser.h"
19 bool AlwaysAvailable(void) {
23 bool IfPm3Present(void) {
24 if (session
.help_dump_mode
)
26 return session
.pm3_present
;
29 bool IfPm3Rdv4Fw(void) {
32 return (pm3_capabilities
.compiled_with_flash
) || (pm3_capabilities
.compiled_with_smartcard
);
35 bool IfPm3Flash(void) {
38 if (!pm3_capabilities
.compiled_with_flash
)
40 return pm3_capabilities
.hw_available_flash
;
43 bool IfPm3Smartcard(void) {
46 if (!pm3_capabilities
.compiled_with_smartcard
)
48 return pm3_capabilities
.hw_available_smartcard
;
51 bool IfPm3FpcUsart(void) {
54 return pm3_capabilities
.compiled_with_fpc_usart
;
57 bool IfPm3FpcUsartHost(void) {
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
67 if (!pm3_capabilities
.compiled_with_fpc_usart_host
)
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
76 if (!pm3_capabilities
.compiled_with_fpc_usart_dev
)
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();
89 return pm3_capabilities
.compiled_with_lf
;
92 bool IfPm3Hitag(void) {
95 return pm3_capabilities
.compiled_with_hitag
;
98 bool IfPm3EM4x50(void) {
101 return pm3_capabilities
.compiled_with_em4x50
;
104 bool IfPm3EM4x70(void) {
108 return pm3_capabilities
.compiled_with_em4x70
;
111 bool IfPm3Hfsniff(void) {
114 return pm3_capabilities
.compiled_with_hfsniff
;
117 bool IfPm3Hfplot(void) {
120 return pm3_capabilities
.compiled_with_hfplot
;
123 bool IfPm3Iso14443a(void) {
126 return pm3_capabilities
.compiled_with_iso14443a
;
129 bool IfPm3Iso14443b(void) {
132 return pm3_capabilities
.compiled_with_iso14443b
;
135 bool IfPm3Iso14443(void) {
138 return pm3_capabilities
.compiled_with_iso14443a
|| pm3_capabilities
.compiled_with_iso14443b
;
141 bool IfPm3Iso15693(void) {
144 return pm3_capabilities
.compiled_with_iso15693
;
147 bool IfPm3Felica(void) {
150 return pm3_capabilities
.compiled_with_felica
;
153 bool IfPm3Legicrf(void) {
156 return pm3_capabilities
.compiled_with_legicrf
;
159 bool IfPm3Iclass(void) {
162 return pm3_capabilities
.compiled_with_iclass
;
165 bool IfPm3NfcBarcode(void) {
168 return pm3_capabilities
.compiled_with_nfcbarcode
;
171 bool IfPm3Lcd(void) {
174 return pm3_capabilities
.compiled_with_lcd
;
178 void CmdsHelp(const command_t Commands
[]) {
179 if (Commands
[0].Name
== NULL
) return;
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
);
187 PrintAndLogEx(NORMAL
, _GREEN_("%-16s")" %s", Commands
[i
].Name
, Commands
[i
].Help
);
189 g_printAndLog
= PRINTANDLOG_PRINT
| PRINTANDLOG_LOG
;
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);
201 // Help dump children with help
202 if (strcmp(Cmd
, "XX_internal_command_dump_full_XX") == 0) {
203 dumpCommandsRecursive(Commands
, 0, true);
206 // Markdown help dump children
207 if (strcmp(Cmd
, "XX_internal_command_dump_markdown_XX") == 0) {
208 dumpCommandsRecursive(Commands
, 1, false);
211 // Markdown help dump children with help
212 if (strcmp(Cmd
, "XX_internal_command_dump_markdown_help_XX") == 0) {
213 dumpCommandsRecursive(Commands
, 1, true);
217 if (strcmp(Cmd
, "coffee") == 0) {
218 PrintAndLogEx(NORMAL
, "");
219 PrintAndLogEx(NORMAL
, " ((\n ))\n" _YELLOW_(" .______.\n | |]\n \\ /\n `----ยด\n\n"));
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
, "");
240 memset(cmd_name
, 0, sizeof(cmd_name
));
243 // %n == receives an integer of value equal to the number of chars read so far.
245 sscanf(Cmd
, "%127s%n", cmd_name
, &len
);
250 if (cmd_name
[0] == '#')
253 // find args, check for -h / --help
255 while (Cmd
[tmplen
] == ' ')
257 bool request_help
= (strcmp(Cmd
+ tmplen
, "-h") == 0) || (strcmp(Cmd
+ tmplen
, "--help") == 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()) {
267 PrintAndLogEx(WARNING
, "This command is " _YELLOW_("not available") " in this mode");
274 /* try to find exactly one prefix-match */
275 if (!Commands
[i
].Name
) {
279 for (i
= 0; Commands
[i
].Name
; i
++) {
280 if (!strncmp(Commands
[i
].Name
, cmd_name
, strlen(cmd_name
)) && Commands
[i
].IsAvailable()) {
285 if (matches
== 1) i
= last_match
;
288 if (Commands
[i
].Name
) {
289 while (Cmd
[len
] == ' ')
291 return Commands
[i
].Parse(Cmd
+ len
);
293 // show help for selected hierarchy or if command not recognised
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;
309 // First, dump all single commands, which are not a container for
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())
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");
335 PrintAndLogEx(NORMAL
, "%s%-*s|%-*s|%s", parent
, w_cmd
- (int)strlen(parent
), cmds
[i
].Name
, w_off
, cmd_offline
, cmds
[i
].Help
);
339 PrintAndLogEx(NORMAL
, "\n");
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;
349 PrintAndLogEx(NORMAL
, "=======================================================================================");
350 PrintAndLogEx(NORMAL
, _RED_("%s%s\n\n ")_CYAN_("%s\n"), parent
, cmds
[i
].Name
, cmds
[i
].Help
);
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.
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");
365 cmds
[i
].Parse("XX_internal_command_dump_XX");