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