]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdparser.c
chg: moved to header 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
719000b7 18void CmdsHelp(const command_t Commands[]) {
19 if (Commands[0].Name == NULL) return;
20 int i = 0;
21 while (Commands[i].Name) {
22 if (!offline || Commands[i].Offline)
23 PrintAndLog("%-16s %s", Commands[i].Name, Commands[i].Help);
24 ++i;
25 }
7fe9b0b7 26}
27
f445df40 28
cc3c0a51 29int CmdsParse(const command_t Commands[], const char *Cmd)
7fe9b0b7 30{
57c69556 31 if(strcmp( Cmd, "XX_internal_command_dump_XX") == 0)
dec8e8bd 32 {// Help dump children
719000b7 33 dumpCommandsRecursive(Commands, 0);
34 return 0;
dec8e8bd
PT
35 }
36 if(strcmp( Cmd, "XX_internal_command_dump_markdown_XX") == 0)
37 {// Markdown help dump children
719000b7 38 dumpCommandsRecursive(Commands, 1);
39 return 0;
57c69556 40 }
7fe9b0b7 41 char cmd_name[32];
42 int len = 0;
aacb96d7 43 memset(cmd_name, 0, sizeof(cmd_name));
7fe9b0b7 44 sscanf(Cmd, "%31s%n", cmd_name, &len);
45 int i = 0;
46 while (Commands[i].Name && strcmp(Commands[i].Name, cmd_name))
47 ++i;
5d5311a2 48
49 /* try to find exactly one prefix-match */
50 if(!Commands[i].Name) {
51 int last_match = 0;
52 int matches = 0;
53
54 for(i=0;Commands[i].Name;i++) {
83819845 55 if( !strncmp(Commands[i].Name, cmd_name, strlen(cmd_name)) ) {
56 last_match = i;
57 matches++;
58 }
5d5311a2 59 }
60 if(matches == 1) i=last_match;
61 }
62
035303ac 63 if (Commands[i].Name) {
64 while (Cmd[len] == ' ')
65 ++len;
cc3c0a51 66 return Commands[i].Parse(Cmd + len);
035303ac 67 } else {
fcdfc43e 68 // show help for selected hierarchy or if command not recognised
040a7baa 69 CmdsHelp(Commands);
035303ac 70 }
cc3c0a51 71
72 return 0;
7fe9b0b7 73}
57c69556
MHS
74
75char pparent[512] = {0};
76char *parent = pparent;
77
dec8e8bd 78void dumpCommandsRecursive(const command_t cmds[], int markdown)
57c69556 79{
dec8e8bd 80 if (cmds[0].Name == NULL)
57c69556
MHS
81 return;
82
83 int i = 0;
dec8e8bd
PT
84 int w_cmd=25;
85 int w_off=8;
57c69556
MHS
86 // First, dump all single commands, which are not a container for
87 // other commands
dec8e8bd 88 if (markdown) {
19e2a10d
PT
89 printf("|%-*s|%-*s|%s\n",w_cmd,"command",w_off,"offline","description");
90 printf("|%-*s|%-*s|%s\n",w_cmd,"-------",w_off,"-------","-----------");
dec8e8bd
PT
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 }
57c69556
MHS
95
96 while (cmds[i].Name)
97 {
dec8e8bd 98 char* cmd_offline = "N";
57c69556
MHS
99 if(cmds[i].Help[0] == '{' && ++i) continue;
100
dec8e8bd
PT
101 if ( cmds[i].Offline) cmd_offline = "Y";
102 if (markdown)
19e2a10d 103 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
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);
57c69556
MHS
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
dec8e8bd 116 printf("### %s%s\n\n %s\n\n", parent, cmds[i].Name, cmds[i].Help);
57c69556
MHS
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;
57c69556
MHS
122 // This is what causes the recursion, since commands Parse-implementation
123 // in turn calls the CmdsParse above.
dec8e8bd
PT
124 if (markdown)
125 cmds[i].Parse("XX_internal_command_dump_markdown_XX");
126 else
127 cmds[i].Parse("XX_internal_command_dump_XX");
57c69556
MHS
128 parent = old_parent;
129 ++i;
130 }
131
132}
Impressum, Datenschutz