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