-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandCom.cpp
More file actions
307 lines (271 loc) · 8.53 KB
/
Copy pathCommandCom.cpp
File metadata and controls
307 lines (271 loc) · 8.53 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/***********************************************************************
*
* C O N S O L E F U N C T I O N S
* Implementation of the CLI interpreter for microcontrollers
*
***********************************************************************/
#include "arduino.h"
#include "CommandCom.h"
#include "string.h"
// --- Local helper function prototypes (file scope only) ---
static int is_quotes(int ch);
static int is_delimiter(int ch);
static int is_whitespaces(int ch);
static int get_token_(char *wp, char *quote);
static int get_argv(char *input_line, int argc_max, char *argv[]);
static int find_then_execute(void* exe_table, int argc, char *argv[]);
static int split_all(char *input_line, int argc_max, char *argv[], const char *delimiters);
static char *trim(char *instr);
static void command_com(char *buf, EXEC_TABLE *exe_files);
static void just_execute(char *buf, EXEC_TABLE *exe_files);
void CommandCom::interpret(char *buf)
{
command_com(buf, table);
}
void CommandCom::execute(char *buf)
{
just_execute(buf, table);
}
void CommandCom::prompt(char *buf)
{
Serial.println(buf);
Serial.println("");
Serial.print(">");
}
/**
* @brief Checks if a character is a quote symbol.
*/
static int is_quotes(int ch)
{
if(strchr("\'\"`", ch)) // Supports single, double, and backtick quotes
return 1;
return 0;
}
/**
* @brief Checks if a character is a special token delimiter.
*/
static int is_delimiter(int ch)
{
if(strchr(">=<|", ch))
return 1;
return 0;
}
/**
* @brief Checks if a character is a space or a horizontal tab.
*/
static int is_whitespaces(int ch)
{
if(strchr(" \t", ch))
return 1;
return 0;
}
/**
* @brief Extracts a single token (argument) from a string.
* Correctly handles quoted arguments, preserving spaces inside quotes
* as a single parameter.
*
* @param wp Pointer to the start of the current token.
* @param quote Reference to return the type of quote detected (0x00 if none).
* @return int Length of the processed string segment in bytes.
*/
static int get_token_(char *wp, char *quote)
{
char *p = wp;
*quote = 0x00;
if(is_quotes(*p)) // If the argument begins with a quote
{
*quote = *p++; // Remember the quote type and advance pointer
while(*p != *quote && *p != 0x00) // Look for the closing quote or end of string
p++;
*p++ = 0x00; // In-place replacement of closing quote with null-terminator
return p - wp; // Return token length including quotes
}
else // Regular argument without quotes
{
do
{
p++;
}
while(!is_whitespaces(*p) && *p != 0x00); // Walk until first whitespace or end of string
}
return p - wp;
}
/**
* @brief Fills the standard argv[] array from the raw input line.
* Performs in-place parsing (0 bytes dynamic memory allocation)
* by replacing whitespaces between arguments with null-terminators '\0'.
*
* @param input_line Raw input command string.
* @param argc_max Maximum allowed arguments (bounded by TOKENS_MAX).
* @param argv Array of pointers to store argument start addresses.
* @return int Total number of successfully extracted arguments (argc).
*/
static int get_argv(char *input_line, int argc_max, char *argv[])
{
char *w = input_line;
int cnt = 0;
char quote;
while(1)
{
// 1. Skip consecutive leading whitespaces before an argument
if(is_whitespaces(*w))
{
if(*w == 0) // End of string reached unexpectedly
return cnt;
*w++ = 0; // Replace whitespace with null-terminator
continue;
}
// 2. Found the start of a valid argument token
if(*w != 0)
{
argv[cnt++] = w; // Store argument start address in argv array
w += get_token_(w, "e); // Move pointer to the end of this token
if(quote) // If the argument was enclosed in quotes
{
++argv[cnt-1]; // Shift start pointer forward to skip the opening quote
quote = 0x00;
}
if(cnt >= argc_max) // Protection against argv array overflow
return cnt;
}
else
break; // Exit when end of string is reached
}
return cnt;
}
/**
* @brief Splits a string globally based on a custom list of delimiter characters.
*/
static int split_all(char *input_line, int argc_max, char *argv[], const char *delimiters)
{
char *w = input_line;
int cnt = 0;
argv[cnt++] = w;
while(argc_max)
{
if(strchr(delimiters, *w))
{
if(*w == 0)
return cnt;
*w++ = 0;
argv[cnt++] = w;
if(cnt >= argc_max)
return cnt;
continue;
}
w++;
}
return cnt;
}
/**
* @brief Cleans up a string from trailing/leading whitespace (Zero-allocation Trim).
* Replaces carriage return/line feed characters (\r, \n) with 0x00,
* and chops off leading spaces by advancing the return pointer.
*
* @param instr Raw input buffer.
* @return char* Pointer to the first non-space character of the string.
*/
static char *trim(char *instr)
{
int len = strlen(instr);
int i;
// Replace any \r or \n occurrences with the string terminator
for(i=0; (i<len) && (instr[i]!=0x0D) && (instr[i]!=0x0A); i++);
instr[i] = 0x00;
// Skip all leading space characters (code 0x20)
for(i=0; (i<len) && (instr[i]==0x20); i++);
return &instr[i];
}
/**
* @brief Looks up the command in the table and executes it.
* Iterates through the EXEC_TABLE. If a name match (argv[0]) is found,
* it invokes the handler function directly by address, passing argc and argv.
*
* @return int Returns the command function status, or 1 if command is not found.
*/
static int find_then_execute(void* exe_table, int argc, char *argv[])
{
int i;
EXE_FPTR func;
EXEC_TABLE *exe_files = (EXEC_TABLE*) exe_table;
// Loop runs until the command name in the table is empty (end of table marker)
for (i = 0; exe_files[i].fname[0]; i++)
{
if (0 == strcmp(exe_files[i].fname, argv[0])) // Check for name match
{
func = exe_files[i].func;
if (func)
{
return func(argc, argv); // Call function directly by its memory address
}
}
}
// If the entire table was scanned and no match was found
if(argc > 0)
{
Serial.println("");
Serial.print("Command ");
Serial.print(argv[0]);
Serial.println(" not found");
}
return 1;
}
/**
* @brief Internal implementation of the interactive mode.
* Includes automatic processing of the system "list" command.
*/
static void command_com(char *buf, EXEC_TABLE *exe_files)
{
char *myargv[TOKENS_MAX];
memset(myargv, 0, sizeof(myargv)); // Clear token pointer array
// Generate argc (cnt) and argv (myargv)
int cnt = get_argv(trim(buf), TOKENS_MAX, myargv);
if (cnt == 0) // If an empty line was entered (just pressed Enter)
{
Serial.println("");
Serial.print(">");
return;
}
// Intercept system "list" command at the core library level
if(0 == strcmp(myargv[0], "list"))
{
int i;
int len = 0;
Serial.println("Avalilable commands:");
Serial.print("list ");
// Automatically print names of all registered table commands
for (i = 0; exe_files[i].fname[0]; i++)
{
len += (strlen(exe_files[i].fname) + 1);
Serial.print(exe_files[i].fname);
Serial.print(" ");
if(len > 60) // Line break to keep terminal output within window limits
{
len = 0;
Serial.println("");
}
}
Serial.println("");
Serial.print(">");
return;
}
// For standard commands - look up and execute
Serial.println("");
find_then_execute(exe_files, cnt, myargv);
Serial.println("");
Serial.print(">"); // Print prompt for the next input
}
/**
* @brief Internal implementation of the script mode.
* Parses the line and silently invokes the command without adding UI noise.
*/
static void just_execute(char *buf, EXEC_TABLE *exe_files)
{
char *myargv[TOKENS_MAX];
memset(myargv, 0, sizeof(myargv));
int cnt = get_argv(trim(buf), TOKENS_MAX, myargv);
if (cnt > 0)
{
find_then_execute(exe_files, cnt, myargv);
}
}