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