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