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