-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLineEngine.cpp
233 lines (191 loc) · 5.07 KB
/
CommandLineEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "defs.h"
#include "spark_wiring_print.h"
#include "spark_wiring_string.h"
#include "Tokens.h"
#include "CommandLineEngine.h"
using namespace CommandEngine;
class NullPrinter : public Print {
public:
size_t write(uint8_t) { return 0; }
size_t write(const char *str) { return 0; }
size_t write(const uint8_t *buffer, size_t size) { return 0; }
};
NullPrinter nullprinter;
void CommandLineEngine::registerFunction( String funcName, CommandLineEngine::UserFunction func )
{
UserFunctions::iterator it = findFunction(funcName);
if( it == mUserFunctions.end() )
{
UserFunctionDef uf = { funcName, func };
mUserFunctions.push_back(uf);
}
else{
it->func = func;
}
}
void CommandLineEngine::registerMacro( String macroName, String value )
{
UserMacros::iterator it = findMacro(macroName);
if( it == mUserMacros.end() ) {
UserMacroDef um = {macroName, value};
mUserMacros.push_back(um);
}
else {
it->value = value;
}
}
#define quickprint(x) if(printer) { printer->println(x); }
bool CommandLineEngine::execute( String cmdLine, Print * printer )
{
if( cmdLine.length() <= 0 )
return false;
Tokens tk;
if( Tokens::Tokenize(tk,cmdLine).isEmpty() )
return false;
// Replace the macros now
// doMacros uses external iterator so the internal iterator of tk is untouched
doMacros( tk );
// Now find the function and call it
// But first check if it is one of the built in functions
String func;
if (!tk.next(func))
return false;
// Remove trailing weird space issues
func.trim();
if( func.equalsIgnoreCase("set") ) {
// Built in function
set( printer, tk, FF_Normal );
}
else if ( func.equalsIgnoreCase("help") ) {
// Built in function
help( printer, tk, FF_Normal );
}
else {
bool foundfunction = false;
// Now Search for the function and call the user function if
// we can find it
for( UserFunctions::iterator it = mUserFunctions.begin(); it != mUserFunctions.end(); ++it )
{
if( func.equalsIgnoreCase( it->name) ) {
foundfunction = true;
it->func( printer ? printer : &nullprinter,
tk,
FF_Normal);
break;
}
}
if( !foundfunction ) {
if(printer) {
printer->print("Unable to find the specified command: ");
printer->println( func );
}
return false;
}
}
return true;
}
void CommandLineEngine::doMacros(Tokens & tk)
{
for( Tokens::iterator it = tk.begin(); it != tk.end(); ++it)
{
String & v = it;
if( v[0] == '$' ) {
String macro = v.substring(1);
UserMacros::iterator it = findMacro(macro);
if( it == mUserMacros.end() )
continue;
else
v = it->value;
}
}
}
void CommandLineEngine::help(Print * printer, Tokens & tk, FunctionFlags ff)
{
if (!printer)
return;
UserFunctions::iterator it;
String arg;
if( tk.next(arg) )
{
if( arg.equalsIgnoreCase("help") )
{
printer->println("Type help without any arguments to learn more.");
return;
}
else if( arg.equalsIgnoreCase("set") )
{
set(printer, tk, FF_HelpText);
return;
}
it = findFunction(arg);
if( it == mUserFunctions.end() )
{
printer->print("Unable to locate the function: ");
printer->println(arg);
return;
}
// Call the function and tell it to display its help text
it->func(printer, tk, FF_HelpText);
return;
}
else
{
printer->println("Function Name: help");
printer->println("Type help <function name> to learn more about that function.");
printer->println("The following functions are defined:");
printer->println("help");
printer->println("set");
// List all the functions
for( it = mUserFunctions.begin(); it != mUserFunctions.end(); ++it)
{
printer->println(it->name);
}
}
}
void CommandLineEngine::set(Print * printer, Tokens & tk, FunctionFlags ff)
{
if( ff == FF_HelpText ) {
if( printer ) {
printer->println("Function Name: Set");
printer->println("Description: Use it to set a macro. When using set do not include the $ sign.");
printer->println("To use a macro type the $macro name. For instance the following commands.");
printer->println("set redpin D7");
printer->println("pin $redpin high");
printer->println("The set function syntax is as follows:");
printer->println("set [macroname] [value]");
}
return;
}
String arg, val;
if( !tk.next(arg) || !tk.next(val) )
quickprint("Invalid syntax. Type help set")
UserMacros::iterator it = findMacro(arg);
if( it == mUserMacros.end() ) {
UserMacroDef um = { arg, val};
mUserMacros.push_back( um );
}
else{
it->value = val;
}
}
// Helper Functinos for the vectors
CommandLineEngine::UserMacros::iterator CommandLineEngine::findMacro(String txt)
{
// Searches for the string exactly as it is seen
for( UserMacros::iterator it = mUserMacros.begin(); it != mUserMacros.end(); ++it)
{
if( txt.equalsIgnoreCase( it->name ))
return it;
}
return mUserMacros.end();
}
CommandLineEngine::UserFunctions::iterator CommandLineEngine::findFunction(String txt)
{
for( UserFunctions::iterator it = mUserFunctions.begin(); it != mUserFunctions.end(); ++it)
{
if( txt.equalsIgnoreCase( it->name ))
return it;
}
return mUserFunctions.end();
}
#undef quickprint