]>
Commit | Line | Data |
---|---|---|
a553f267 | 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 | //----------------------------------------------------------------------------- | |
10 | ||
7fe9b0b7 | 11 | #include <stdio.h> |
57c69556 | 12 | #include <stdlib.h> |
7fe9b0b7 | 13 | #include <string.h> |
14 | #include "ui.h" | |
15 | #include "cmdparser.h" | |
902cb3c0 | 16 | #include "proxmark3.h" |
7fe9b0b7 | 17 | |
18 | void CmdsHelp(const command_t Commands[]) | |
19 | { | |
20 | if (Commands[0].Name == NULL) | |
21 | return; | |
22 | int i = 0; | |
23 | while (Commands[i].Name) | |
24 | { | |
57c69556 MHS |
25 | if(Commands[i].Offline) |
26 | { | |
27 | PrintAndLog("%-16s \t%s", Commands[i].Name, Commands[i].Help); | |
28 | }else | |
29 | { | |
30 | PrintAndLog("%-16s @\t%s", Commands[i].Name, Commands[i].Help); | |
31 | } | |
7fe9b0b7 | 32 | ++i; |
33 | } | |
34 | } | |
35 | ||
9cb00f30 | 36 | void CmdsParse(const command_t Commands[], const char *Cmd) |
7fe9b0b7 | 37 | { |
57c69556 MHS |
38 | if(strcmp( Cmd, "XX_internal_command_dump_XX") == 0) |
39 | {// Markdown dump children | |
40 | dumpCommandsRecursive(Commands); | |
41 | return; | |
42 | } | |
43 | ||
44 | ||
7fe9b0b7 | 45 | char cmd_name[32]; |
46 | int len = 0; | |
47 | memset(cmd_name, 0, 32); | |
48 | sscanf(Cmd, "%31s%n", cmd_name, &len); | |
49 | int i = 0; | |
50 | while (Commands[i].Name && strcmp(Commands[i].Name, cmd_name)) | |
51 | ++i; | |
5d5311a2 | 52 | |
53 | /* try to find exactly one prefix-match */ | |
54 | if(!Commands[i].Name) { | |
55 | int last_match = 0; | |
56 | int matches = 0; | |
57 | ||
58 | for(i=0;Commands[i].Name;i++) { | |
83819845 | 59 | if( !strncmp(Commands[i].Name, cmd_name, strlen(cmd_name)) ) { |
60 | last_match = i; | |
61 | matches++; | |
62 | } | |
5d5311a2 | 63 | } |
64 | if(matches == 1) i=last_match; | |
65 | } | |
66 | ||
035303ac | 67 | if (Commands[i].Name) { |
68 | while (Cmd[len] == ' ') | |
69 | ++len; | |
7fe9b0b7 | 70 | Commands[i].Parse(Cmd + len); |
035303ac | 71 | } else { |
fcdfc43e | 72 | // show help for selected hierarchy or if command not recognised |
040a7baa | 73 | CmdsHelp(Commands); |
035303ac | 74 | } |
7fe9b0b7 | 75 | } |
57c69556 MHS |
76 | //static int tablevel = 0; |
77 | ||
78 | char pparent[512] = {0}; | |
79 | char *parent = pparent; | |
80 | ||
81 | void dumpCommandsRecursive(const command_t cmds[]) | |
82 | { | |
83 | if (cmds[0].Name == NULL) | |
84 | return; | |
85 | ||
86 | int i = 0; | |
87 | char* tabulation = "###"; | |
88 | char* offline = "N"; | |
89 | // First, dump all single commands, which are not a container for | |
90 | // other commands | |
91 | printf("command|offline|description\n"); | |
92 | printf("-------|-------|-----------\n"); | |
93 | ||
94 | while (cmds[i].Name) | |
95 | { | |
96 | if(cmds[i].Help[0] == '{' && ++i) continue; | |
97 | ||
98 | if ( cmds[i].Offline) offline = "Y"; | |
99 | printf("|`%s%s`|%s|`%s`|\n", parent, cmds[i].Name,offline, cmds[i].Help); | |
100 | ++i; | |
101 | } | |
102 | printf("\n\n"); | |
103 | i=0; | |
104 | // Then, print the categories. These will go into subsections with their own tables | |
105 | ||
106 | while (cmds[i].Name) | |
107 | { | |
108 | if(cmds[i].Help[0] != '{' && ++i) continue; | |
109 | ||
110 | printf("%s %s%s\n\n %s\n\n", tabulation, parent, cmds[i].Name, cmds[i].Help); | |
111 | ||
112 | char currentparent[512] = {0}; | |
113 | snprintf(currentparent, sizeof currentparent, "%s%s ", parent, cmds[i].Name); | |
114 | char *old_parent = parent; | |
115 | parent = currentparent; | |
116 | // tablevel++; | |
117 | // This is what causes the recursion, since commands Parse-implementation | |
118 | // in turn calls the CmdsParse above. | |
119 | cmds[i].Parse("XX_internal_command_dump_XX"); | |
120 | // tablevel--; | |
121 | parent = old_parent; | |
122 | ++i; | |
123 | } | |
124 | ||
125 | } |