]>
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 //-----------------------------------------------------------------------------
11 #include "cmdparser.h"
17 #include "proxmark3.h"
21 void CmdsHelp(const command_t Commands
[])
23 if (Commands
[0].Name
== NULL
)
26 while (Commands
[i
].Name
)
28 if (!IsOffline() || Commands
[i
].Offline
)
29 PrintAndLog("%-16s %s", Commands
[i
].Name
, Commands
[i
].Help
);
35 int CmdsParse(const command_t Commands
[], const char *Cmd
)
37 if(strcmp( Cmd
, "XX_internal_command_dump_XX") == 0)
38 {// Help dump children
39 dumpCommandsRecursive(Commands
, 0);
42 if(strcmp( Cmd
, "XX_internal_command_dump_markdown_XX") == 0)
43 {// Markdown help dump children
44 dumpCommandsRecursive(Commands
, 1);
49 memset(cmd_name
, 0, 32);
50 sscanf(Cmd
, "%31s%n", cmd_name
, &len
);
52 while (Commands
[i
].Name
&& strcmp(Commands
[i
].Name
, cmd_name
))
55 /* try to find exactly one prefix-match */
56 if(!Commands
[i
].Name
) {
60 for(i
=0;Commands
[i
].Name
;i
++) {
61 if( !strncmp(Commands
[i
].Name
, cmd_name
, strlen(cmd_name
)) ) {
66 if(matches
== 1) i
=last_match
;
69 if (Commands
[i
].Name
) {
70 while (Cmd
[len
] == ' ')
72 return Commands
[i
].Parse(Cmd
+ len
);
74 // show help for selected hierarchy or if command not recognised
81 char pparent
[512] = {0};
82 char *parent
= pparent
;
84 void dumpCommandsRecursive(const command_t cmds
[], int markdown
)
86 if (cmds
[0].Name
== NULL
)
92 // First, dump all single commands, which are not a container for
95 printf("|%-*s|%-*s|%s\n",w_cmd
,"command",w_off
,"offline","description");
96 printf("|%-*s|%-*s|%s\n",w_cmd
,"-------",w_off
,"-------","-----------");
98 printf("%-*s|%-*s|%s\n",w_cmd
,"command",w_off
,"offline","description");
99 printf("%-*s|%-*s|%s\n",w_cmd
,"-------",w_off
,"-------","-----------");
104 char* cmd_offline
= "N";
105 if(cmds
[i
].Help
[0] == '{' && ++i
) continue;
107 if ( cmds
[i
].Offline
) cmd_offline
= "Y";
109 printf("|`%s%-*s`|%-*s|`%s`\n", parent
, w_cmd
-(int)strlen(parent
)-2, cmds
[i
].Name
, w_off
, cmd_offline
, cmds
[i
].Help
);
111 printf("%s%-*s|%-*s|%s\n", parent
, w_cmd
-(int)strlen(parent
), cmds
[i
].Name
, w_off
, cmd_offline
, cmds
[i
].Help
);
116 // Then, print the categories. These will go into subsections with their own tables
120 if(cmds
[i
].Help
[0] != '{' && ++i
) continue;
122 printf("### %s%s\n\n %s\n\n", parent
, cmds
[i
].Name
, cmds
[i
].Help
);
124 char currentparent
[512] = {0};
125 snprintf(currentparent
, sizeof currentparent
, "%s%s ", parent
, cmds
[i
].Name
);
126 char *old_parent
= parent
;
127 parent
= currentparent
;
128 // This is what causes the recursion, since commands Parse-implementation
129 // in turn calls the CmdsParse above.
131 cmds
[i
].Parse("XX_internal_command_dump_markdown_XX");
133 cmds
[i
].Parse("XX_internal_command_dump_XX");