]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdparser.c
fix 'hf iclass sim':
[proxmark3-svn] / client / cmdparser.c
CommitLineData
a553f267 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
f5ecd97b 11#include "cmdparser.h"
12
7fe9b0b7 13#include <stdio.h>
57c69556 14#include <stdlib.h>
7fe9b0b7 15#include <string.h>
16#include "ui.h"
902cb3c0 17#include "proxmark3.h"
f5ecd97b 18#include "comms.h"
7fe9b0b7 19
2487dfeb 20
7fe9b0b7 21void CmdsHelp(const command_t Commands[])
22{
23 if (Commands[0].Name == NULL)
24 return;
25 int i = 0;
26 while (Commands[i].Name)
27 {
61aaee35 28 if (!IsOffline() || Commands[i].Offline)
46782176 29 PrintAndLog("%-16s %s", Commands[i].Name, Commands[i].Help);
7fe9b0b7 30 ++i;
31 }
32}
33
2487dfeb 34
35int CmdsParse(const command_t Commands[], const char *Cmd)
7fe9b0b7 36{
2487dfeb 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;
5d5311a2 54
2487dfeb 55 /* try to find exactly one prefix-match */
56 if(!Commands[i].Name) {
57 int last_match = 0;
58 int matches = 0;
5d5311a2 59
2487dfeb 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 }
5d5311a2 68
2487dfeb 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;
7fe9b0b7 79}
57c69556
MHS
80
81char pparent[512] = {0};
82char *parent = pparent;
83
dec8e8bd 84void dumpCommandsRecursive(const command_t cmds[], int markdown)
57c69556 85{
dec8e8bd 86 if (cmds[0].Name == NULL)
57c69556
MHS
87 return;
88
89 int i = 0;
dec8e8bd
PT
90 int w_cmd=25;
91 int w_off=8;
57c69556
MHS
92 // First, dump all single commands, which are not a container for
93 // other commands
dec8e8bd 94 if (markdown) {
19e2a10d
PT
95 printf("|%-*s|%-*s|%s\n",w_cmd,"command",w_off,"offline","description");
96 printf("|%-*s|%-*s|%s\n",w_cmd,"-------",w_off,"-------","-----------");
dec8e8bd
PT
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 }
57c69556
MHS
101
102 while (cmds[i].Name)
103 {
dec8e8bd 104 char* cmd_offline = "N";
57c69556
MHS
105 if(cmds[i].Help[0] == '{' && ++i) continue;
106
dec8e8bd
PT
107 if ( cmds[i].Offline) cmd_offline = "Y";
108 if (markdown)
19e2a10d 109 printf("|`%s%-*s`|%-*s|`%s`\n", parent, w_cmd-(int)strlen(parent)-2, cmds[i].Name, w_off, cmd_offline, cmds[i].Help);
dec8e8bd
PT
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);
57c69556
MHS
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
dec8e8bd 122 printf("### %s%s\n\n %s\n\n", parent, cmds[i].Name, cmds[i].Help);
57c69556
MHS
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;
57c69556
MHS
128 // This is what causes the recursion, since commands Parse-implementation
129 // in turn calls the CmdsParse above.
dec8e8bd
PT
130 if (markdown)
131 cmds[i].Parse("XX_internal_command_dump_markdown_XX");
132 else
133 cmds[i].Parse("XX_internal_command_dump_XX");
57c69556
MHS
134 parent = old_parent;
135 ++i;
136 }
137
138}
Impressum, Datenschutz