]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdparser.c
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 //-----------------------------------------------------------------------------
15 #include "cmdparser.h"
16 #include "proxmark3.h"
18 void CmdsHelp(const command_t Commands
[]) {
19 if (Commands
[0].Name
== NULL
) return;
21 while (Commands
[i
].Name
) {
22 if (!offline
|| Commands
[i
].Offline
)
23 PrintAndLog("%-16s %s", Commands
[i
].Name
, Commands
[i
].Help
);
29 int CmdsParse(const command_t Commands
[], const char *Cmd
)
31 if(strcmp( Cmd
, "XX_internal_command_dump_XX") == 0)
32 {// Help dump children
33 dumpCommandsRecursive(Commands
, 0);
36 if(strcmp( Cmd
, "XX_internal_command_dump_markdown_XX") == 0)
37 {// Markdown help dump children
38 dumpCommandsRecursive(Commands
, 1);
43 memset(cmd_name
, 0, sizeof(cmd_name
));
44 sscanf(Cmd
, "%31s%n", cmd_name
, &len
);
46 while (Commands
[i
].Name
&& strcmp(Commands
[i
].Name
, cmd_name
))
49 /* try to find exactly one prefix-match */
50 if(!Commands
[i
].Name
) {
54 for(i
=0;Commands
[i
].Name
;i
++) {
55 if( !strncmp(Commands
[i
].Name
, cmd_name
, strlen(cmd_name
)) ) {
60 if(matches
== 1) i
=last_match
;
63 if (Commands
[i
].Name
) {
64 while (Cmd
[len
] == ' ')
66 return Commands
[i
].Parse(Cmd
+ len
);
68 // show help for selected hierarchy or if command not recognised
75 char pparent
[512] = {0};
76 char *parent
= pparent
;
78 void dumpCommandsRecursive(const command_t cmds
[], int markdown
)
80 if (cmds
[0].Name
== NULL
)
86 // First, dump all single commands, which are not a container for
89 printf("|%-*s|%-*s|%s\n",w_cmd
,"command",w_off
,"offline","description");
90 printf("|%-*s|%-*s|%s\n",w_cmd
,"-------",w_off
,"-------","-----------");
92 printf("%-*s|%-*s|%s\n",w_cmd
,"command",w_off
,"offline","description");
93 printf("%-*s|%-*s|%s\n",w_cmd
,"-------",w_off
,"-------","-----------");
98 char* cmd_offline
= "N";
99 if(cmds
[i
].Help
[0] == '{' && ++i
) continue;
101 if ( cmds
[i
].Offline
) cmd_offline
= "Y";
103 printf("|`%s%-*s`|%-*s|`%s`\n", parent
, w_cmd
-(int)strlen(parent
)-2, cmds
[i
].Name
, w_off
, cmd_offline
, cmds
[i
].Help
);
105 printf("%s%-*s|%-*s|%s\n", parent
, w_cmd
-(int)strlen(parent
), cmds
[i
].Name
, w_off
, cmd_offline
, cmds
[i
].Help
);
110 // Then, print the categories. These will go into subsections with their own tables
114 if(cmds
[i
].Help
[0] != '{' && ++i
) continue;
116 printf("### %s%s\n\n %s\n\n", parent
, cmds
[i
].Name
, cmds
[i
].Help
);
118 char currentparent
[512] = {0};
119 snprintf(currentparent
, sizeof currentparent
, "%s%s ", parent
, cmds
[i
].Name
);
120 char *old_parent
= parent
;
121 parent
= currentparent
;
122 // This is what causes the recursion, since commands Parse-implementation
123 // in turn calls the CmdsParse above.
125 cmds
[i
].Parse("XX_internal_command_dump_markdown_XX");
127 cmds
[i
].Parse("XX_internal_command_dump_XX");