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