]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdparser.c
ADD: @piwi's fixes to .history
[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
7fe9b0b7 11#include <stdio.h>
57c69556 12#include <stdlib.h>
7fe9b0b7 13#include <string.h>
14#include "ui.h"
15#include "cmdparser.h"
902cb3c0 16#include "proxmark3.h"
7fe9b0b7 17
18void CmdsHelp(const command_t Commands[])
19{
20 if (Commands[0].Name == NULL)
21 return;
22 int i = 0;
23 while (Commands[i].Name)
24 {
46782176
PT
25 if (!offline || Commands[i].Offline)
26 PrintAndLog("%-16s %s", Commands[i].Name, Commands[i].Help);
7fe9b0b7 27 ++i;
28 }
29}
30
f445df40 31void CmdsLS(const command_t Commands[])
32{
33 if (Commands[0].Name == NULL) return;
34 int i = 0;
35 while (Commands[i].Name)
36 {
37 if (!offline || Commands[i].Offline)
38 PrintAndLog("%-16s", Commands[i].Name);
39 ++i;
40 }
41}
42
cc3c0a51 43int CmdsParse(const command_t Commands[], const char *Cmd)
7fe9b0b7 44{
57c69556 45 if(strcmp( Cmd, "XX_internal_command_dump_XX") == 0)
dec8e8bd
PT
46 {// Help dump children
47 dumpCommandsRecursive(Commands, 0);
cc3c0a51 48 return 0;
dec8e8bd
PT
49 }
50 if(strcmp( Cmd, "XX_internal_command_dump_markdown_XX") == 0)
51 {// Markdown help dump children
52 dumpCommandsRecursive(Commands, 1);
cc3c0a51 53 return 0;
57c69556 54 }
7fe9b0b7 55 char cmd_name[32];
56 int len = 0;
57 memset(cmd_name, 0, 32);
58 sscanf(Cmd, "%31s%n", cmd_name, &len);
59 int i = 0;
60 while (Commands[i].Name && strcmp(Commands[i].Name, cmd_name))
61 ++i;
5d5311a2 62
63 /* try to find exactly one prefix-match */
64 if(!Commands[i].Name) {
65 int last_match = 0;
66 int matches = 0;
67
68 for(i=0;Commands[i].Name;i++) {
83819845 69 if( !strncmp(Commands[i].Name, cmd_name, strlen(cmd_name)) ) {
70 last_match = i;
71 matches++;
72 }
5d5311a2 73 }
74 if(matches == 1) i=last_match;
75 }
76
035303ac 77 if (Commands[i].Name) {
78 while (Cmd[len] == ' ')
79 ++len;
cc3c0a51 80 return Commands[i].Parse(Cmd + len);
035303ac 81 } else {
fcdfc43e 82 // show help for selected hierarchy or if command not recognised
040a7baa 83 CmdsHelp(Commands);
035303ac 84 }
cc3c0a51 85
86 return 0;
7fe9b0b7 87}
57c69556
MHS
88
89char pparent[512] = {0};
90char *parent = pparent;
91
dec8e8bd 92void dumpCommandsRecursive(const command_t cmds[], int markdown)
57c69556 93{
dec8e8bd 94 if (cmds[0].Name == NULL)
57c69556
MHS
95 return;
96
97 int i = 0;
dec8e8bd
PT
98 int w_cmd=25;
99 int w_off=8;
57c69556
MHS
100 // First, dump all single commands, which are not a container for
101 // other commands
dec8e8bd 102 if (markdown) {
19e2a10d
PT
103 printf("|%-*s|%-*s|%s\n",w_cmd,"command",w_off,"offline","description");
104 printf("|%-*s|%-*s|%s\n",w_cmd,"-------",w_off,"-------","-----------");
dec8e8bd
PT
105 } else {
106 printf("%-*s|%-*s|%s\n",w_cmd,"command",w_off,"offline","description");
107 printf("%-*s|%-*s|%s\n",w_cmd,"-------",w_off,"-------","-----------");
108 }
57c69556
MHS
109
110 while (cmds[i].Name)
111 {
dec8e8bd 112 char* cmd_offline = "N";
57c69556
MHS
113 if(cmds[i].Help[0] == '{' && ++i) continue;
114
dec8e8bd
PT
115 if ( cmds[i].Offline) cmd_offline = "Y";
116 if (markdown)
19e2a10d 117 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
118 else
119 printf("%s%-*s|%-*s|%s\n", parent, w_cmd-(int)strlen(parent), cmds[i].Name, w_off, cmd_offline, cmds[i].Help);
57c69556
MHS
120 ++i;
121 }
122 printf("\n\n");
123 i=0;
124 // Then, print the categories. These will go into subsections with their own tables
125
126 while (cmds[i].Name)
127 {
128 if(cmds[i].Help[0] != '{' && ++i) continue;
129
dec8e8bd 130 printf("### %s%s\n\n %s\n\n", parent, cmds[i].Name, cmds[i].Help);
57c69556
MHS
131
132 char currentparent[512] = {0};
133 snprintf(currentparent, sizeof currentparent, "%s%s ", parent, cmds[i].Name);
134 char *old_parent = parent;
135 parent = currentparent;
57c69556
MHS
136 // This is what causes the recursion, since commands Parse-implementation
137 // in turn calls the CmdsParse above.
dec8e8bd
PT
138 if (markdown)
139 cmds[i].Parse("XX_internal_command_dump_markdown_XX");
140 else
141 cmds[i].Parse("XX_internal_command_dump_XX");
57c69556
MHS
142 parent = old_parent;
143 ++i;
144 }
145
146}
Impressum, Datenschutz