]>
Commit | Line | Data |
---|---|---|
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> |
12 | #include <string.h> | |
13 | #include "ui.h" | |
14 | #include "cmdparser.h" | |
15 | ||
16 | void CmdsHelp(const command_t Commands[]) | |
17 | { | |
18 | if (Commands[0].Name == NULL) | |
19 | return; | |
20 | int i = 0; | |
21 | while (Commands[i].Name) | |
22 | { | |
23 | if (!offline || Commands[i].Offline) | |
24 | PrintAndLog("%-16s %s", Commands[i].Name, Commands[i].Help); | |
25 | ++i; | |
26 | } | |
27 | } | |
28 | ||
9cb00f30 | 29 | void CmdsParse(const command_t Commands[], const char *Cmd) |
7fe9b0b7 | 30 | { |
31 | char cmd_name[32]; | |
32 | int len = 0; | |
33 | memset(cmd_name, 0, 32); | |
34 | sscanf(Cmd, "%31s%n", cmd_name, &len); | |
35 | int i = 0; | |
36 | while (Commands[i].Name && strcmp(Commands[i].Name, cmd_name)) | |
37 | ++i; | |
5d5311a2 | 38 | |
39 | /* try to find exactly one prefix-match */ | |
40 | if(!Commands[i].Name) { | |
41 | int last_match = 0; | |
42 | int matches = 0; | |
43 | ||
44 | for(i=0;Commands[i].Name;i++) { | |
83819845 | 45 | if( !strncmp(Commands[i].Name, cmd_name, strlen(cmd_name)) ) { |
46 | last_match = i; | |
47 | matches++; | |
48 | } | |
5d5311a2 | 49 | } |
50 | if(matches == 1) i=last_match; | |
51 | } | |
52 | ||
035303ac | 53 | if (Commands[i].Name) { |
54 | while (Cmd[len] == ' ') | |
55 | ++len; | |
7fe9b0b7 | 56 | Commands[i].Parse(Cmd + len); |
035303ac | 57 | } else { |
fcdfc43e | 58 | // show help for selected hierarchy or if command not recognised |
040a7baa | 59 | CmdsHelp(Commands); |
035303ac | 60 | } |
7fe9b0b7 | 61 | } |