amiro-os / core / src / aos_shell.c @ f3ac1c96
History | View | Annotate | Download (49.5 KB)
1 | e545e620 | Thomas Schöpping | /*
|
---|---|---|---|
2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
3 | 84f0ce9e | Thomas Schöpping | Copyright (C) 2016..2019 Thomas Schöpping et al.
|
4 | e545e620 | Thomas Schöpping | |
5 | This program is free software: you can redistribute it and/or modify
|
||
6 | it under the terms of the GNU General Public License as published by
|
||
7 | the Free Software Foundation, either version 3 of the License, or
|
||
8 | (at your option) any later version.
|
||
9 | |||
10 | This program is distributed in the hope that it will be useful,
|
||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | GNU General Public License for more details.
|
||
14 | |||
15 | You should have received a copy of the GNU General Public License
|
||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
17 | */
|
||
18 | |||
19 | 53710ca3 | Marc Rothmann | /**
|
20 | * @file aos_shell.c
|
||
21 | * @brief Shell code.
|
||
22 | * @details Shell code as well as shell related channels and streams.
|
||
23 | *
|
||
24 | * @addtogroup aos_shell
|
||
25 | * @{
|
||
26 | */
|
||
27 | |||
28 | e545e620 | Thomas Schöpping | #include <aos_shell.h> |
29 | |||
30 | 2dd2e257 | Thomas Schöpping | #if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) |
31 | e545e620 | Thomas Schöpping | #include <aos_debug.h> |
32 | #include <aos_time.h> |
||
33 | #include <aos_system.h> |
||
34 | #include <string.h> |
||
35 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
36 | /* LOCAL DEFINITIONS */
|
||
37 | /******************************************************************************/
|
||
38 | ba516b61 | Thomas Schöpping | |
39 | /**
|
||
40 | * @brief Event mask to be set on OS related events.
|
||
41 | */
|
||
42 | #define AOS_SHELL_EVENTMASK_OS EVENT_MASK(0) |
||
43 | |||
44 | /**
|
||
45 | * @brief Event mask to be set on a input event.
|
||
46 | */
|
||
47 | #define AOS_SHELL_EVENTMASK_INPUT EVENT_MASK(1) |
||
48 | |||
49 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
50 | /* EXPORTED VARIABLES */
|
||
51 | /******************************************************************************/
|
||
52 | |||
53 | /******************************************************************************/
|
||
54 | /* LOCAL TYPES */
|
||
55 | /******************************************************************************/
|
||
56 | |||
57 | /*
|
||
58 | * forward declarations
|
||
59 | */
|
||
60 | static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n); |
||
61 | static size_t _channelread(void *instance, uint8_t *bp, size_t n); |
||
62 | static msg_t _channelput(void *instance, uint8_t b); |
||
63 | static msg_t _channelget(void *instance); |
||
64 | static msg_t _channelputt(void *instance, uint8_t b, sysinterval_t time); |
||
65 | static msg_t _channelgett(void *instance, sysinterval_t time); |
||
66 | static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, sysinterval_t time); |
||
67 | static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, sysinterval_t time); |
||
68 | static msg_t _channelctl(void *instance, unsigned int operation, void *arg); |
||
69 | static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n); |
||
70 | static size_t _stremread(void *instance, uint8_t *bp, size_t n); |
||
71 | static msg_t _streamput(void *instance, uint8_t b); |
||
72 | static msg_t _streamget(void *instance); |
||
73 | |||
74 | static const struct AosShellChannelVMT _channelvmt = { |
||
75 | (size_t) 0,
|
||
76 | _channelwrite, |
||
77 | _channelread, |
||
78 | _channelput, |
||
79 | _channelget, |
||
80 | _channelputt, |
||
81 | _channelgett, |
||
82 | _channelwritet, |
||
83 | _channelreadt, |
||
84 | _channelctl, |
||
85 | }; |
||
86 | |||
87 | static const struct AosShellStreamVMT _streamvmt = { |
||
88 | (size_t) 0,
|
||
89 | _streamwrite, |
||
90 | _stremread, |
||
91 | _streamput, |
||
92 | _streamget, |
||
93 | }; |
||
94 | |||
95 | /**
|
||
96 | * @brief Enumerator of special keyboard keys.
|
||
97 | */
|
||
98 | typedef enum special_key { |
||
99 | KEY_UNKNOWN, /**< any/unknow key */
|
||
100 | KEY_AMBIGUOUS, /**< key is ambiguous */
|
||
101 | KEY_TAB, /**< tabulator key */
|
||
102 | KEY_ESCAPE, /**< escape key */
|
||
103 | KEY_BACKSPACE, /**< backspace key */
|
||
104 | KEY_INSERT, /**< insert key */
|
||
105 | KEY_DELETE, /**< delete key */
|
||
106 | KEY_HOME, /**< home key */
|
||
107 | KEY_END, /**< end key */
|
||
108 | KEY_PAGE_UP, /**< page up key */
|
||
109 | KEY_PAGE_DOWN, /**< page down key */
|
||
110 | KEY_ARROW_UP, /**< arrow up key */
|
||
111 | KEY_ARROW_DOWN, /**< arrow down key */
|
||
112 | KEY_ARROW_LEFT, /**< arrow left key */
|
||
113 | KEY_ARROW_RIGHT, /**< arrow right key */
|
||
114 | } special_key_t; |
||
115 | |||
116 | /**
|
||
117 | * @brief Enumerator for case (in)sensitive character matching.
|
||
118 | */
|
||
119 | typedef enum charmatch { |
||
120 | CHAR_MATCH_NOT = 0, /**< Characters do not match at all. */ |
||
121 | CHAR_MATCH_NCASE = 1, /**< Characters would match case insensitive. */ |
||
122 | CHAR_MATCH_CASE = 2, /**< Characters do match with case. */ |
||
123 | } charmatch_t; |
||
124 | |||
125 | /******************************************************************************/
|
||
126 | /* LOCAL VARIABLES */
|
||
127 | /******************************************************************************/
|
||
128 | |||
129 | /******************************************************************************/
|
||
130 | /* LOCAL FUNCTIONS */
|
||
131 | /******************************************************************************/
|
||
132 | |||
133 | ba516b61 | Thomas Schöpping | /**
|
134 | * @brief Implementation of the BaseAsynchronous write() method (inherited from BaseSequentialStream).
|
||
135 | */
|
||
136 | static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n) |
||
137 | { |
||
138 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
139 | dd8738ea | Thomas Schöpping | return streamWrite(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
140 | ba516b61 | Thomas Schöpping | } else {
|
141 | return 0; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | /**
|
||
146 | * @brief Implementation of the BaseAsynchronous read() method (inherited from BaseSequentialStream).
|
||
147 | */
|
||
148 | static size_t _channelread(void *instance, uint8_t *bp, size_t n) |
||
149 | { |
||
150 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
151 | return streamRead(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
||
152 | } else {
|
||
153 | return 0; |
||
154 | } |
||
155 | ba516b61 | Thomas Schöpping | } |
156 | |||
157 | /**
|
||
158 | * @brief Implementation of the BaseAsynchronous put() method (inherited from BaseSequentialStream).
|
||
159 | */
|
||
160 | static msg_t _channelput(void *instance, uint8_t b) |
||
161 | { |
||
162 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
163 | dd8738ea | Thomas Schöpping | return streamPut(((AosShellChannel*)instance)->asyncchannel, b);
|
164 | ba516b61 | Thomas Schöpping | } else {
|
165 | return MSG_RESET;
|
||
166 | } |
||
167 | } |
||
168 | |||
169 | /**
|
||
170 | * @brief Implementation of the BaseAsynchronous get() method (inherited from BaseSequentialStream).
|
||
171 | */
|
||
172 | static msg_t _channelget(void *instance) |
||
173 | { |
||
174 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
175 | return streamGet(((AosShellChannel*)instance)->asyncchannel);
|
||
176 | } else {
|
||
177 | return MSG_RESET;
|
||
178 | } |
||
179 | ba516b61 | Thomas Schöpping | } |
180 | |||
181 | /**
|
||
182 | * @brief Implementation of the BaseAsynchronous putt() method.
|
||
183 | */
|
||
184 | 2c99037f | Marc Rothmann | static msg_t _channelputt(void *instance, uint8_t b, sysinterval_t time) |
185 | ba516b61 | Thomas Schöpping | { |
186 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
187 | dd8738ea | Thomas Schöpping | return chnPutTimeout(((AosShellChannel*)instance)->asyncchannel, b, time);
|
188 | ba516b61 | Thomas Schöpping | } else {
|
189 | return MSG_RESET;
|
||
190 | } |
||
191 | } |
||
192 | |||
193 | /**
|
||
194 | * @brief Implementation of the BaseAsynchronous gett() method.
|
||
195 | */
|
||
196 | 2c99037f | Marc Rothmann | static msg_t _channelgett(void *instance, sysinterval_t time) |
197 | ba516b61 | Thomas Schöpping | { |
198 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
199 | return chnGetTimeout(((AosShellChannel*)instance)->asyncchannel, time);
|
||
200 | } else {
|
||
201 | return MSG_RESET;
|
||
202 | } |
||
203 | ba516b61 | Thomas Schöpping | } |
204 | |||
205 | /**
|
||
206 | * @brief Implementation of the BaseAsynchronous writet() method.
|
||
207 | */
|
||
208 | 2c99037f | Marc Rothmann | static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, sysinterval_t time) |
209 | ba516b61 | Thomas Schöpping | { |
210 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
211 | dd8738ea | Thomas Schöpping | return chnWriteTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
212 | ba516b61 | Thomas Schöpping | } else {
|
213 | return 0; |
||
214 | } |
||
215 | } |
||
216 | |||
217 | /**
|
||
218 | * @brief Implementation of the BaseAsynchronous readt() method.
|
||
219 | */
|
||
220 | 2c99037f | Marc Rothmann | static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, sysinterval_t time) |
221 | ba516b61 | Thomas Schöpping | { |
222 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
223 | return chnReadTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
||
224 | } else {
|
||
225 | return 0; |
||
226 | } |
||
227 | ba516b61 | Thomas Schöpping | } |
228 | |||
229 | 2c99037f | Marc Rothmann | /**
|
230 | * @brief Implementation of the BaseAsynchronousChannel ctl() method.
|
||
231 | */
|
||
232 | f3ac1c96 | Thomas Schöpping | static msg_t _channelctl(void *instance, unsigned int operation, void *arg) |
233 | { |
||
234 | 2c99037f | Marc Rothmann | (void) instance;
|
235 | |||
236 | switch (operation) {
|
||
237 | case CHN_CTL_NOP:
|
||
238 | osalDbgCheck(arg == NULL);
|
||
239 | break;
|
||
240 | case CHN_CTL_INVALID:
|
||
241 | osalDbgAssert(false, "invalid CTL operation"); |
||
242 | break;
|
||
243 | default:
|
||
244 | break;
|
||
245 | } |
||
246 | return MSG_OK;
|
||
247 | } |
||
248 | |||
249 | ba516b61 | Thomas Schöpping | static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n) |
250 | { |
||
251 | aosDbgCheck(instance != NULL);
|
||
252 | |||
253 | // local variables
|
||
254 | AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
||
255 | size_t bytes; |
||
256 | size_t maxbytes = 0;
|
||
257 | |||
258 | // iterate through the list of channels
|
||
259 | while (channel != NULL) { |
||
260 | bytes = streamWrite(channel, bp, n); |
||
261 | maxbytes = (bytes > maxbytes) ? bytes : maxbytes; |
||
262 | channel = channel->next; |
||
263 | } |
||
264 | |||
265 | return maxbytes;
|
||
266 | } |
||
267 | |||
268 | static size_t _stremread(void *instance, uint8_t *bp, size_t n) |
||
269 | { |
||
270 | (void)instance;
|
||
271 | (void)bp;
|
||
272 | (void)n;
|
||
273 | |||
274 | return 0; |
||
275 | } |
||
276 | |||
277 | static msg_t _streamput(void *instance, uint8_t b) |
||
278 | { |
||
279 | aosDbgCheck(instance != NULL);
|
||
280 | |||
281 | // local variables
|
||
282 | AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
||
283 | dd8738ea | Thomas Schöpping | msg_t ret = MSG_OK; |
284 | ba516b61 | Thomas Schöpping | |
285 | // iterate through the list of channels
|
||
286 | while (channel != NULL) { |
||
287 | dd8738ea | Thomas Schöpping | msg_t ret_ = streamPut(channel, b); |
288 | ret = (ret_ < ret) ? ret_ : ret; |
||
289 | ba516b61 | Thomas Schöpping | channel = channel->next; |
290 | } |
||
291 | |||
292 | dd8738ea | Thomas Schöpping | return ret;
|
293 | ba516b61 | Thomas Schöpping | } |
294 | |||
295 | static msg_t _streamget(void *instance) |
||
296 | { |
||
297 | (void)instance;
|
||
298 | |||
299 | return 0; |
||
300 | } |
||
301 | |||
302 | e545e620 | Thomas Schöpping | /**
|
303 | * @brief Print the shell prompt
|
||
304 | * @details Depending on the configuration flags, the system uptime is printed before the prompt string.
|
||
305 | *
|
||
306 | * @param[in] shell Pointer to the shell object.
|
||
307 | */
|
||
308 | static void _printPrompt(aos_shell_t* shell) |
||
309 | { |
||
310 | aosDbgCheck(shell != NULL);
|
||
311 | |||
312 | 8399aeae | Thomas Schöpping | // print some time informattion before prompt if configured
|
313 | if (shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) {
|
||
314 | // printf the system uptime
|
||
315 | if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_UPTIME) {
|
||
316 | // get current system uptime
|
||
317 | aos_timestamp_t uptime; |
||
318 | aosSysGetUptime(&uptime); |
||
319 | |||
320 | chprintf((BaseSequentialStream*)&shell->stream, "[%01u:%02u:%02u:%02u:%03u:%03u] ",
|
||
321 | (uint32_t)(uptime / MICROSECONDS_PER_DAY), |
||
322 | (uint8_t)(uptime % MICROSECONDS_PER_DAY / MICROSECONDS_PER_HOUR), |
||
323 | (uint8_t)(uptime % MICROSECONDS_PER_HOUR / MICROSECONDS_PER_MINUTE), |
||
324 | (uint8_t)(uptime % MICROSECONDS_PER_MINUTE / MICROSECONDS_PER_SECOND), |
||
325 | (uint16_t)(uptime % MICROSECONDS_PER_SECOND / MICROSECONDS_PER_MILLISECOND), |
||
326 | (uint16_t)(uptime % MICROSECONDS_PER_MILLISECOND / MICROSECONDS_PER_MICROSECOND)); |
||
327 | } |
||
328 | else if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_DATETIME) { |
||
329 | // get current RTC time
|
||
330 | struct tm dt;
|
||
331 | aosSysGetDateTime(&dt); |
||
332 | chprintf((BaseSequentialStream*)&shell->stream, "[%02u-%02u-%04u|%02u:%02u:%02u] ",
|
||
333 | dt.tm_mday, |
||
334 | dt.tm_mon + 1,
|
||
335 | dt.tm_year + 1900,
|
||
336 | dt.tm_hour, |
||
337 | dt.tm_min, |
||
338 | dt.tm_sec); |
||
339 | } |
||
340 | else {
|
||
341 | aosDbgAssert(false);
|
||
342 | } |
||
343 | e545e620 | Thomas Schöpping | } |
344 | |||
345 | // print the actual prompt string
|
||
346 | if (shell->prompt && !(shell->config & AOS_SHELL_CONFIG_PROMPT_MINIMAL)) {
|
||
347 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "%s$ ", shell->prompt);
|
348 | e545e620 | Thomas Schöpping | } else {
|
349 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "%>$ ");
|
350 | e545e620 | Thomas Schöpping | } |
351 | |||
352 | return;
|
||
353 | } |
||
354 | |||
355 | /**
|
||
356 | * @brief Interprete a escape sequence
|
||
357 | *
|
||
358 | * @param[in] seq Character sequence to interprete.
|
||
359 | * Must be terminated by NUL byte.
|
||
360 | *
|
||
361 | * @return A @p special_key value.
|
||
362 | */
|
||
363 | static special_key_t _interpreteEscapeSequence(const char seq[]) |
||
364 | { |
||
365 | // local variables
|
||
366 | bool ambiguous = false; |
||
367 | int cmp = 0; |
||
368 | |||
369 | // TAB
|
||
370 | /* not supported yet; use "\x09" instead */
|
||
371 | |||
372 | // BACKSPACE
|
||
373 | /* not supported yet; use "\x08" instead */
|
||
374 | |||
375 | // ESCAPE
|
||
376 | cmp = strcmp(seq, "\x1B");
|
||
377 | if (cmp == 0) { |
||
378 | return KEY_ESCAPE;
|
||
379 | } else {
|
||
380 | ambiguous |= (cmp < 0);
|
||
381 | } |
||
382 | |||
383 | // INSERT
|
||
384 | cmp = strcmp(seq, "\x1B\x5B\x32\x7E");
|
||
385 | if (cmp == 0) { |
||
386 | return KEY_INSERT;
|
||
387 | } else {
|
||
388 | ambiguous |= (cmp < 0);
|
||
389 | } |
||
390 | |||
391 | // DELETE
|
||
392 | cmp = strcmp(seq, "\x1B\x5B\x33\x7E");
|
||
393 | if (cmp == 0) { |
||
394 | return KEY_DELETE;
|
||
395 | } else {
|
||
396 | ambiguous |= (cmp < 0);
|
||
397 | } |
||
398 | |||
399 | // HOME
|
||
400 | cmp = strcmp(seq, "\x1B\x4F\x48");
|
||
401 | if (cmp == 0) { |
||
402 | return KEY_HOME;
|
||
403 | } else {
|
||
404 | ambiguous |= (cmp < 0);
|
||
405 | } |
||
406 | |||
407 | // END
|
||
408 | cmp = strcmp(seq, "\x1B\x4F\x46");
|
||
409 | if (cmp == 0) { |
||
410 | return KEY_END;
|
||
411 | } else {
|
||
412 | ambiguous |= (cmp < 0);
|
||
413 | } |
||
414 | |||
415 | // PAGE UP
|
||
416 | cmp = strcmp(seq, "\x1B\x5B\x35\x7E");
|
||
417 | if (cmp == 0) { |
||
418 | return KEY_PAGE_UP;
|
||
419 | } else {
|
||
420 | ambiguous |= (cmp < 0);
|
||
421 | } |
||
422 | |||
423 | // PAGE DOWN
|
||
424 | cmp = strcmp(seq, "\x1B\x5B\x36\x7E");
|
||
425 | if (cmp == 0) { |
||
426 | return KEY_PAGE_DOWN;
|
||
427 | } else {
|
||
428 | ambiguous |= (cmp < 0);
|
||
429 | } |
||
430 | |||
431 | // ARROW UP
|
||
432 | cmp = strcmp(seq, "\x1B\x5B\x41");
|
||
433 | if (cmp == 0) { |
||
434 | return KEY_ARROW_UP;
|
||
435 | } else {
|
||
436 | ambiguous |= (cmp < 0);
|
||
437 | } |
||
438 | |||
439 | // ARROW DOWN
|
||
440 | cmp = strcmp(seq, "\x1B\x5B\x42");
|
||
441 | if (cmp == 0) { |
||
442 | return KEY_ARROW_DOWN;
|
||
443 | } else {
|
||
444 | ambiguous |= (cmp < 0);
|
||
445 | } |
||
446 | |||
447 | // ARROW LEFT
|
||
448 | cmp = strcmp(seq, "\x1B\x5B\x44");
|
||
449 | if (cmp == 0) { |
||
450 | return KEY_ARROW_LEFT;
|
||
451 | } else {
|
||
452 | ambiguous |= (cmp < 0);
|
||
453 | } |
||
454 | |||
455 | // ARROW RIGHT
|
||
456 | cmp = strcmp(seq, "\x1B\x5B\x43");
|
||
457 | if (cmp == 0) { |
||
458 | return KEY_ARROW_RIGHT;
|
||
459 | } else {
|
||
460 | ambiguous |= (cmp < 0);
|
||
461 | } |
||
462 | |||
463 | return ambiguous ? KEY_AMBIGUOUS : KEY_UNKNOWN;
|
||
464 | } |
||
465 | |||
466 | /**
|
||
467 | * @brief Move the cursor in the terminal
|
||
468 | *
|
||
469 | * @param[in] shell Pointer to the shell object.
|
||
470 | * @param[in] from Starting position of the cursor.
|
||
471 | * @param[in] to Target position to move the cursor to.
|
||
472 | *
|
||
473 | * @return The number of positions moved.
|
||
474 | */
|
||
475 | static int _moveCursor(aos_shell_t* shell, const size_t from, const size_t to) |
||
476 | { |
||
477 | aosDbgCheck(shell != NULL);
|
||
478 | |||
479 | // local variables
|
||
480 | size_t pos = from; |
||
481 | |||
482 | // move cursor left by printing backspaces
|
||
483 | while (pos > to) {
|
||
484 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, '\b');
|
485 | e545e620 | Thomas Schöpping | --pos; |
486 | } |
||
487 | |||
488 | // move cursor right by printing line content
|
||
489 | while (pos < to) {
|
||
490 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, shell->line[pos]); |
491 | e545e620 | Thomas Schöpping | ++pos; |
492 | } |
||
493 | |||
494 | return (int)pos - (int)from; |
||
495 | } |
||
496 | |||
497 | /**
|
||
498 | * @brief Print content of the shell line
|
||
499 | *
|
||
500 | * @param[in] shell Pointer to the shell object.
|
||
501 | * @param[in] from First position to start printing from.
|
||
502 | * @param[in] to Position after the last character to print.
|
||
503 | *
|
||
504 | * @return Number of characters printed.
|
||
505 | */
|
||
506 | static inline size_t _printLine(aos_shell_t* shell, const size_t from, const size_t to) |
||
507 | { |
||
508 | aosDbgCheck(shell != NULL);
|
||
509 | |||
510 | // local variables
|
||
511 | size_t cnt; |
||
512 | |||
513 | for (cnt = 0; from + cnt < to; ++cnt) { |
||
514 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, shell->line[from + cnt]); |
515 | e545e620 | Thomas Schöpping | } |
516 | |||
517 | return cnt;
|
||
518 | } |
||
519 | |||
520 | /**
|
||
521 | * @brief Compare two characters.
|
||
522 | *
|
||
523 | * @param[in] lhs First character to compare.
|
||
524 | * @param[in] rhs Second character to compare.
|
||
525 | *
|
||
526 | * @return How well the characters match.
|
||
527 | */
|
||
528 | static inline charmatch_t _charcmp(char lhs, char rhs) |
||
529 | { |
||
530 | // if lhs is a upper case letter and rhs is a lower case letter
|
||
531 | if (lhs >= 'A' && lhs <= 'Z' && rhs >= 'a' && rhs <= 'z') { |
||
532 | return (lhs == (rhs - 'a' + 'A')) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
||
533 | } |
||
534 | // if lhs is a lower case letter and rhs is a upper case letter
|
||
535 | else if (lhs >= 'a' && lhs <= 'z' && rhs >= 'A' && rhs <= 'Z') { |
||
536 | return ((lhs - 'a' + 'A') == rhs) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
||
537 | } |
||
538 | // default
|
||
539 | else {
|
||
540 | return (lhs == rhs) ? CHAR_MATCH_CASE : CHAR_MATCH_NOT;
|
||
541 | } |
||
542 | } |
||
543 | |||
544 | /**
|
||
545 | * @brief Maps an character from ASCII to a modified custom encoding.
|
||
546 | * @details The custom character encoding is very similar to ASCII and has the following structure:
|
||
547 | * 0x00=NULL ... 0x40='@' (identically to ASCII)
|
||
548 | * 0x4A='a'; 0x4B='A'; 0x4C='b'; 0x4D='B' ... 0x73='z'; 0x74='Z' (custom letter order)
|
||
549 | * 0x75='[' ... 0x7A='`' (0x5B..0x60 is ASCII)
|
||
550 | * 0x7B='{' ... 0x7F=DEL (identically to ASCII)
|
||
551 | *
|
||
552 | * @param[in] c Character to map to the custom encoding.
|
||
553 | *
|
||
554 | * @return The customly encoded character.
|
||
555 | */
|
||
556 | static inline char _mapAscii2Custom(const char c) |
||
557 | { |
||
558 | if (c >= 'A' && c <= 'Z') { |
||
559 | return ((c - 'A') * 2) + 'A' + 1; |
||
560 | } else if (c > 'Z' && c < 'a') { |
||
561 | return c + ('z' - 'a') + 1; |
||
562 | } else if (c >= 'a' && c <= 'z') { |
||
563 | return ((c - 'a') * 2) + 'A'; |
||
564 | } else {
|
||
565 | return c;
|
||
566 | } |
||
567 | } |
||
568 | |||
569 | /**
|
||
570 | * @brief Compares two strings wrt letter case.
|
||
571 | * @details Comparisson uses a custom character encoding or mapping.
|
||
572 | * See @p _mapAscii2Custom for details.
|
||
573 | *
|
||
574 | * @param[in] str1 First string to compare.
|
||
575 | * @param[in] str2 Second string to compare.
|
||
576 | * @param[in] cs Flag indicating whether comparison shall be case sensitive.
|
||
577 | * @param[in,out] n Maximum number of character to compare (in) and number of matching characters (out).
|
||
578 | * If a null pointer is specified, this parameter is ignored.
|
||
579 | * If the value pointed to is zero, comarison will not be limited.
|
||
580 | * @param[out] m Optional indicator whether there was at least one case mismatch.
|
||
581 | *
|
||
582 | * @return Integer value indicating the relationship between the strings.
|
||
583 | * @retval <0 The first character that does not match has a lower value in str1 than in str2.
|
||
584 | * @retval 0 The contents of both strings are equal.
|
||
585 | * @retval >0 The first character that does not match has a greater value in str1 than in str2.
|
||
586 | */
|
||
587 | static int _strccmp(const char *str1, const char *str2, bool cs, size_t* n, charmatch_t* m) |
||
588 | { |
||
589 | aosDbgCheck(str1 != NULL);
|
||
590 | aosDbgCheck(str2 != NULL);
|
||
591 | |||
592 | // initialize variables
|
||
593 | if (m) {
|
||
594 | *m = CHAR_MATCH_NOT; |
||
595 | } |
||
596 | size_t i = 0;
|
||
597 | |||
598 | // iterate through the strings
|
||
599 | while ((n == NULL) || (*n == 0) || (*n > 0 && i < *n)) { |
||
600 | // break on NUL
|
||
601 | if (str1[i] == '\0' || str2[i] == '\0') { |
||
602 | if (n) {
|
||
603 | *n = i; |
||
604 | } |
||
605 | break;
|
||
606 | } |
||
607 | // compare character
|
||
608 | const charmatch_t match = _charcmp(str1[i], str2[i]);
|
||
609 | if ((match == CHAR_MATCH_CASE) || (!cs && match == CHAR_MATCH_NCASE)) {
|
||
610 | if (m != NULL && *m != CHAR_MATCH_NCASE) { |
||
611 | *m = match; |
||
612 | } |
||
613 | ++i; |
||
614 | } else {
|
||
615 | if (n) {
|
||
616 | *n = i; |
||
617 | } |
||
618 | break;
|
||
619 | } |
||
620 | } |
||
621 | |||
622 | return _mapAscii2Custom(str1[i]) - _mapAscii2Custom(str2[i]);
|
||
623 | } |
||
624 | |||
625 | 27286ba5 | Thomas Schöpping | /**
|
626 | * @brief Read input from a channel as long as there is data available.
|
||
627 | *
|
||
628 | * @param[in] shell Pointer to the shell object.
|
||
629 | * @param[in] channel The channel to read from.
|
||
630 | * @param[out] n Pointer to a variable to store the number of read characters to.
|
||
631 | *
|
||
632 | * @return
|
||
633 | */
|
||
634 | ba516b61 | Thomas Schöpping | static aos_status_t _readChannel(aos_shell_t* shell, AosShellChannel* channel, size_t* n)
|
635 | e545e620 | Thomas Schöpping | { |
636 | aosDbgCheck(shell != NULL);
|
||
637 | ba516b61 | Thomas Schöpping | aosDbgCheck(channel != NULL);
|
638 | aosDbgCheck(n != NULL);
|
||
639 | e545e620 | Thomas Schöpping | |
640 | // local variables
|
||
641 | ba516b61 | Thomas Schöpping | aos_shellaction_t action = AOS_SHELL_ACTION_NONE; |
642 | e545e620 | Thomas Schöpping | char c;
|
643 | dd8738ea | Thomas Schöpping | special_key_t key; |
644 | e545e620 | Thomas Schöpping | |
645 | ba516b61 | Thomas Schöpping | // initialize output variables
|
646 | *n = 0;
|
||
647 | |||
648 | // read character by character from the channel
|
||
649 | while (chnReadTimeout(channel, (uint8_t*)&c, 1, TIME_IMMEDIATE)) { |
||
650 | dd8738ea | Thomas Schöpping | key = KEY_UNKNOWN; |
651 | e545e620 | Thomas Schöpping | |
652 | // parse escape sequence
|
||
653 | ba516b61 | Thomas Schöpping | if (shell->inputdata.escp > 0) { |
654 | shell->inputdata.escseq[shell->inputdata.escp] = c; |
||
655 | ++shell->inputdata.escp; |
||
656 | key = _interpreteEscapeSequence(shell->inputdata.escseq); |
||
657 | e545e620 | Thomas Schöpping | if (key == KEY_AMBIGUOUS) {
|
658 | // read next byte to resolve ambiguity
|
||
659 | continue;
|
||
660 | } else {
|
||
661 | ba516b61 | Thomas Schöpping | /*
|
662 | * If the escape sequence could either be parsed sucessfully
|
||
663 | * or there is no match (KEY_UNKNOWN),
|
||
664 | * reset the sequence variable and interprete key/character
|
||
665 | */
|
||
666 | shell->inputdata.escp = 0;
|
||
667 | memset(shell->inputdata.escseq, '\0', sizeof(shell->inputdata.escseq)*sizeof(shell->inputdata.escseq[0])); |
||
668 | e545e620 | Thomas Schöpping | } |
669 | } |
||
670 | |||
671 | ba516b61 | Thomas Schöpping | /* interprete keys or character */
|
672 | e545e620 | Thomas Schöpping | { |
673 | ba516b61 | Thomas Schöpping | // default
|
674 | action = AOS_SHELL_ACTION_NONE; |
||
675 | |||
676 | // printable character
|
||
677 | if (key == KEY_UNKNOWN && c >= '\x20' && c <= '\x7E') { |
||
678 | action = AOS_SHELL_ACTION_READCHAR; |
||
679 | } |
||
680 | |||
681 | // tab key or character
|
||
682 | else if (key == KEY_TAB || c == '\x09') { |
||
683 | /*
|
||
684 | * pressing tab once applies auto fill
|
||
685 | * pressing tab a second time prints suggestions
|
||
686 | */
|
||
687 | if (shell->inputdata.lastaction == AOS_SHELL_ACTION_AUTOFILL || shell->inputdata.lastaction == AOS_SHELL_ACTION_SUGGEST) {
|
||
688 | action = AOS_SHELL_ACTION_SUGGEST; |
||
689 | e545e620 | Thomas Schöpping | } else {
|
690 | ba516b61 | Thomas Schöpping | action = AOS_SHELL_ACTION_AUTOFILL; |
691 | e545e620 | Thomas Schöpping | } |
692 | ba516b61 | Thomas Schöpping | } |
693 | |||
694 | // INS key
|
||
695 | else if (key == KEY_INSERT) { |
||
696 | action = AOS_SHELL_ACTION_INSERTTOGGLE; |
||
697 | } |
||
698 | |||
699 | // DEL key or character
|
||
700 | else if (key == KEY_DELETE || c == '\x7F') { |
||
701 | // ignore if cursor is at very right
|
||
702 | if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
||
703 | action = AOS_SHELL_ACTION_DELETEFORWARD; |
||
704 | e545e620 | Thomas Schöpping | } |
705 | ba516b61 | Thomas Schöpping | } |
706 | |||
707 | // backspace key or character
|
||
708 | else if (key == KEY_BACKSPACE || c == '\x08') { |
||
709 | // ignore if cursor is at very left
|
||
710 | if (shell->inputdata.cursorpos > 0) { |
||
711 | action = AOS_SHELL_ACTION_DELETEBACKWARD; |
||
712 | e545e620 | Thomas Schöpping | } |
713 | ba516b61 | Thomas Schöpping | } |
714 | |||
715 | // 'page up' of 'arrow up' key
|
||
716 | else if (key == KEY_PAGE_UP || key == KEY_ARROW_UP) { |
||
717 | e545e620 | Thomas Schöpping | // ignore if there was some input
|
718 | ba516b61 | Thomas Schöpping | if (shell->inputdata.noinput) {
|
719 | action = AOS_SHELL_ACTION_RECALLLAST; |
||
720 | e545e620 | Thomas Schöpping | } |
721 | ba516b61 | Thomas Schöpping | } |
722 | |||
723 | // 'page down' key, 'arrow done' key, 'end of test' character or 'end of transmission' character
|
||
724 | else if (key == KEY_PAGE_DOWN || key == KEY_ARROW_DOWN || c == '\x03' || c == '\x03') { |
||
725 | e545e620 | Thomas Schöpping | // ignore if line is empty
|
726 | ba516b61 | Thomas Schöpping | if (shell->inputdata.lineend > 0) { |
727 | action = AOS_SHELL_ACTION_CLEAR; |
||
728 | e545e620 | Thomas Schöpping | } |
729 | ba516b61 | Thomas Schöpping | } |
730 | |||
731 | // 'home' key
|
||
732 | else if (key == KEY_HOME) { |
||
733 | e545e620 | Thomas Schöpping | // ignore if cursor is very left
|
734 | ba516b61 | Thomas Schöpping | if (shell->inputdata.cursorpos > 0) { |
735 | action = AOS_SHELL_ACTION_CURSOR2START; |
||
736 | e545e620 | Thomas Schöpping | } |
737 | ba516b61 | Thomas Schöpping | } |
738 | |||
739 | // 'end' key
|
||
740 | else if (key == KEY_END) { |
||
741 | // ignore if cursos is very right
|
||
742 | if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
||
743 | action = AOS_SHELL_ACTION_CURSOR2END; |
||
744 | e545e620 | Thomas Schöpping | } |
745 | ba516b61 | Thomas Schöpping | } |
746 | |||
747 | // 'arrow left' key
|
||
748 | else if (key == KEY_ARROW_LEFT) { |
||
749 | e545e620 | Thomas Schöpping | // ignore if cursor is very left
|
750 | ba516b61 | Thomas Schöpping | if (shell->inputdata.cursorpos > 0) { |
751 | action = AOS_SHELL_ACTION_CURSORLEFT; |
||
752 | e545e620 | Thomas Schöpping | } |
753 | ba516b61 | Thomas Schöpping | } |
754 | |||
755 | // 'arrow right' key
|
||
756 | else if (key == KEY_ARROW_RIGHT) { |
||
757 | // irgnore if cursor is very right
|
||
758 | if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
||
759 | action = AOS_SHELL_ACTION_CURSORRIGHT; |
||
760 | e545e620 | Thomas Schöpping | } |
761 | ba516b61 | Thomas Schöpping | } |
762 | |||
763 | // carriage return ('\r') or line feed ('\n') character
|
||
764 | else if (c == '\x0D' || c == '\x0A') { |
||
765 | action = AOS_SHELL_ACTION_EXECUTE; |
||
766 | } |
||
767 | |||
768 | // ESC key or [ESCAPE] character
|
||
769 | else if (key == KEY_ESCAPE || c == '\x1B') { |
||
770 | action = AOS_SHELL_ACTION_ESCSTART; |
||
771 | e545e620 | Thomas Schöpping | } |
772 | } |
||
773 | |||
774 | /* handle function */
|
||
775 | ba516b61 | Thomas Schöpping | switch (action) {
|
776 | case AOS_SHELL_ACTION_READCHAR:
|
||
777 | { |
||
778 | e545e620 | Thomas Schöpping | // line is full
|
779 | ba516b61 | Thomas Schöpping | if (shell->inputdata.lineend + 1 >= shell->linesize) { |
780 | _moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
||
781 | chprintf((BaseSequentialStream*)&shell->stream, "\n\tmaximum line width reached\n");
|
||
782 | e545e620 | Thomas Schöpping | _printPrompt(shell); |
783 | ba516b61 | Thomas Schöpping | _printLine(shell, 0, shell->inputdata.lineend);
|
784 | _moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
||
785 | e545e620 | Thomas Schöpping | } |
786 | // read character
|
||
787 | else {
|
||
788 | // clear old line content on first input
|
||
789 | ba516b61 | Thomas Schöpping | if (shell->inputdata.noinput) {
|
790 | e545e620 | Thomas Schöpping | memset(shell->line, '\0', shell->linesize);
|
791 | ba516b61 | Thomas Schöpping | shell->inputdata.noinput = false;
|
792 | e545e620 | Thomas Schöpping | } |
793 | // overwrite content
|
||
794 | if (shell->config & AOS_SHELL_CONFIG_INPUT_OVERWRITE) {
|
||
795 | ba516b61 | Thomas Schöpping | shell->line[shell->inputdata.cursorpos] = c; |
796 | ++shell->inputdata.cursorpos; |
||
797 | shell->inputdata.lineend = (shell->inputdata.cursorpos > shell->inputdata.lineend) ? shell->inputdata.cursorpos : shell->inputdata.lineend; |
||
798 | streamPut(&shell->stream, (uint8_t)c); |
||
799 | e545e620 | Thomas Schöpping | } |
800 | // insert character
|
||
801 | else {
|
||
802 | ba516b61 | Thomas Schöpping | memmove(&(shell->line[shell->inputdata.cursorpos+1]), &(shell->line[shell->inputdata.cursorpos]), shell->inputdata.lineend - shell->inputdata.cursorpos);
|
803 | shell->line[shell->inputdata.cursorpos] = c; |
||
804 | ++shell->inputdata.lineend; |
||
805 | _printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
||
806 | ++shell->inputdata.cursorpos; |
||
807 | _moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
||
808 | e545e620 | Thomas Schöpping | } |
809 | } |
||
810 | break;
|
||
811 | ba516b61 | Thomas Schöpping | } |
812 | e545e620 | Thomas Schöpping | |
813 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_AUTOFILL:
|
814 | e545e620 | Thomas Schöpping | { |
815 | const char* fill = shell->line; |
||
816 | ba516b61 | Thomas Schöpping | size_t cmatch = shell->inputdata.cursorpos; |
817 | e545e620 | Thomas Schöpping | charmatch_t matchlevel = CHAR_MATCH_NOT; |
818 | size_t n; |
||
819 | // iterate through command list
|
||
820 | for (aos_shellcommand_t* cmd = shell->commands; cmd != NULL; cmd = cmd->next) { |
||
821 | // compare current match with command
|
||
822 | n = cmatch; |
||
823 | charmatch_t mlvl = CHAR_MATCH_NOT; |
||
824 | _strccmp(fill, cmd->name, shell->config & AOS_SHELL_CONFIG_MATCH_CASE, (n == 0) ? NULL : &n, &mlvl); |
||
825 | const int cmp = (n < cmatch) ? |
||
826 | (n - cmatch) : |
||
827 | (cmd->name[n] != '\0') ?
|
||
828 | strlen(cmd->name) - n : |
||
829 | 0;
|
||
830 | // if an exact match was found
|
||
831 | ba516b61 | Thomas Schöpping | if (cmatch + cmp == shell->inputdata.cursorpos) {
|
832 | cmatch = shell->inputdata.cursorpos; |
||
833 | e545e620 | Thomas Schöpping | fill = cmd->name; |
834 | // break the loop only if there are no case mismatches with the input
|
||
835 | ba516b61 | Thomas Schöpping | n = shell->inputdata.cursorpos; |
836 | e545e620 | Thomas Schöpping | _strccmp(fill, shell->line, false, &n, &mlvl);
|
837 | if (mlvl == CHAR_MATCH_CASE) {
|
||
838 | break;
|
||
839 | } |
||
840 | } |
||
841 | // if a not exact match was found
|
||
842 | ba516b61 | Thomas Schöpping | else if (cmatch + cmp > shell->inputdata.cursorpos) { |
843 | e545e620 | Thomas Schöpping | // if this is the first one
|
844 | if (fill == shell->line) {
|
||
845 | cmatch += cmp; |
||
846 | fill = cmd->name; |
||
847 | } |
||
848 | // if this is a worse one
|
||
849 | else if ((cmp < 0) || (cmp == 0 && mlvl == CHAR_MATCH_CASE)) { |
||
850 | cmatch += cmp; |
||
851 | } |
||
852 | } |
||
853 | // non matching commands are ignored
|
||
854 | else {}
|
||
855 | } |
||
856 | // evaluate if there are case mismatches
|
||
857 | n = cmatch; |
||
858 | _strccmp(shell->line, fill, shell->config & AOS_SHELL_CONFIG_MATCH_CASE, &n, &matchlevel); |
||
859 | // print the auto fill if any
|
||
860 | ba516b61 | Thomas Schöpping | if (cmatch > shell->inputdata.cursorpos || (cmatch == shell->inputdata.cursorpos && matchlevel == CHAR_MATCH_NCASE)) {
|
861 | shell->inputdata.noinput = false;
|
||
862 | e545e620 | Thomas Schöpping | // limit auto fill so it will not overflow the line width
|
863 | ba516b61 | Thomas Schöpping | if (shell->inputdata.lineend + (cmatch - shell->inputdata.cursorpos) > shell->linesize) {
|
864 | cmatch = shell->linesize - shell->inputdata.lineend + shell->inputdata.cursorpos; |
||
865 | e545e620 | Thomas Schöpping | } |
866 | // move trailing memory further in the line
|
||
867 | ba516b61 | Thomas Schöpping | memmove(&(shell->line[cmatch]), &(shell->line[shell->inputdata.cursorpos]), shell->inputdata.lineend - shell->inputdata.cursorpos); |
868 | shell->inputdata.lineend += cmatch - shell->inputdata.cursorpos; |
||
869 | e545e620 | Thomas Schöpping | // if there was no incorrect case when matching
|
870 | if (matchlevel == CHAR_MATCH_CASE) {
|
||
871 | // insert fill command name to line
|
||
872 | ba516b61 | Thomas Schöpping | memcpy(&(shell->line[shell->inputdata.cursorpos]), &(fill[shell->inputdata.cursorpos]), cmatch - shell->inputdata.cursorpos); |
873 | e545e620 | Thomas Schöpping | // print the output
|
874 | ba516b61 | Thomas Schöpping | _printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
875 | e545e620 | Thomas Schöpping | } else {
|
876 | // overwrite line with fill command name
|
||
877 | memcpy(shell->line, fill, cmatch); |
||
878 | // reprint the whole line
|
||
879 | ba516b61 | Thomas Schöpping | _moveCursor(shell, shell->inputdata.cursorpos, 0);
|
880 | _printLine(shell, 0, shell->inputdata.lineend);
|
||
881 | e545e620 | Thomas Schöpping | } |
882 | // move cursor to the end of the matching sequence
|
||
883 | ba516b61 | Thomas Schöpping | shell->inputdata.cursorpos = cmatch; |
884 | _moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
||
885 | e545e620 | Thomas Schöpping | } |
886 | break;
|
||
887 | } |
||
888 | |||
889 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_SUGGEST:
|
890 | e545e620 | Thomas Schöpping | { |
891 | unsigned int matches = 0; |
||
892 | // iterate through command list
|
||
893 | for (aos_shellcommand_t* cmd = shell->commands; cmd != NULL; cmd = cmd->next) { |
||
894 | // compare line content with command, excpet if cursorpos=0
|
||
895 | ba516b61 | Thomas Schöpping | size_t i = shell->inputdata.cursorpos; |
896 | if (shell->inputdata.cursorpos > 0) { |
||
897 | e545e620 | Thomas Schöpping | _strccmp(shell->line, cmd->name, true, &i, NULL); |
898 | } |
||
899 | ba516b61 | Thomas Schöpping | const int cmp = (i < shell->inputdata.cursorpos) ? |
900 | (i - shell->inputdata.cursorpos) : |
||
901 | e545e620 | Thomas Schöpping | (cmd->name[i] != '\0') ?
|
902 | strlen(cmd->name) - i : |
||
903 | 0;
|
||
904 | // if a match was found
|
||
905 | if (cmp > 0) { |
||
906 | // if this is the first one
|
||
907 | if (matches == 0) { |
||
908 | ba516b61 | Thomas Schöpping | _moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
909 | streamPut(&shell->stream, '\n');
|
||
910 | e545e620 | Thomas Schöpping | } |
911 | // print the command
|
||
912 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "\t%s\n", cmd->name);
|
913 | e545e620 | Thomas Schöpping | ++matches; |
914 | } |
||
915 | } |
||
916 | // reprint the prompt and line if any matches have been found
|
||
917 | if (matches > 0) { |
||
918 | _printPrompt(shell); |
||
919 | ba516b61 | Thomas Schöpping | _printLine(shell, 0, shell->inputdata.lineend);
|
920 | _moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
||
921 | shell->inputdata.noinput = false;
|
||
922 | e545e620 | Thomas Schöpping | } |
923 | break;
|
||
924 | } |
||
925 | |||
926 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_INSERTTOGGLE:
|
927 | { |
||
928 | e545e620 | Thomas Schöpping | if (shell->config & AOS_SHELL_CONFIG_INPUT_OVERWRITE) {
|
929 | shell->config &= ~AOS_SHELL_CONFIG_INPUT_OVERWRITE; |
||
930 | } else {
|
||
931 | shell->config |= AOS_SHELL_CONFIG_INPUT_OVERWRITE; |
||
932 | } |
||
933 | break;
|
||
934 | ba516b61 | Thomas Schöpping | } |
935 | e545e620 | Thomas Schöpping | |
936 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_DELETEFORWARD:
|
937 | { |
||
938 | --shell->inputdata.lineend; |
||
939 | memmove(&(shell->line[shell->inputdata.cursorpos]), &(shell->line[shell->inputdata.cursorpos+1]), shell->inputdata.lineend - shell->inputdata.cursorpos);
|
||
940 | _printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
||
941 | streamPut(&shell->stream, ' ');
|
||
942 | _moveCursor(shell, shell->inputdata.lineend + 1, shell->inputdata.cursorpos);
|
||
943 | e545e620 | Thomas Schöpping | break;
|
944 | ba516b61 | Thomas Schöpping | } |
945 | e545e620 | Thomas Schöpping | |
946 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_DELETEBACKWARD:
|
947 | { |
||
948 | --shell->inputdata.cursorpos; |
||
949 | memmove(&(shell->line[shell->inputdata.cursorpos]), &(shell->line[shell->inputdata.cursorpos+1]), shell->inputdata.lineend - shell->inputdata.cursorpos);
|
||
950 | --shell->inputdata.lineend; |
||
951 | shell->line[shell->inputdata.lineend] = '\0';
|
||
952 | _moveCursor(shell, shell->inputdata.cursorpos + 1, shell->inputdata.cursorpos);
|
||
953 | _printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
||
954 | streamPut(&shell->stream, ' ');
|
||
955 | _moveCursor(shell, shell->inputdata.lineend+1, shell->inputdata.cursorpos);
|
||
956 | e545e620 | Thomas Schöpping | break;
|
957 | ba516b61 | Thomas Schöpping | } |
958 | e545e620 | Thomas Schöpping | |
959 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_RECALLLAST:
|
960 | e545e620 | Thomas Schöpping | { |
961 | // replace any intermediate NUL bytes with spaces
|
||
962 | ba516b61 | Thomas Schöpping | shell->inputdata.lineend = 0;
|
963 | e545e620 | Thomas Schöpping | size_t nul_start = 0;
|
964 | size_t nul_end = 0;
|
||
965 | // search line for a NUL byte
|
||
966 | while (nul_start < shell->linesize) {
|
||
967 | if (shell->line[nul_start] == '\0') { |
||
968 | nul_end = nul_start + 1;
|
||
969 | // keep searcjing for a byte that is not NUL
|
||
970 | while (nul_end < shell->linesize) {
|
||
971 | if (shell->line[nul_end] != '\0') { |
||
972 | // an intermediate NUL sequence was found
|
||
973 | memset(&(shell->line[nul_start]), ' ', nul_end - nul_start);
|
||
974 | ba516b61 | Thomas Schöpping | shell->inputdata.lineend = nul_end + 1;
|
975 | e545e620 | Thomas Schöpping | break;
|
976 | } else {
|
||
977 | ++nul_end; |
||
978 | } |
||
979 | } |
||
980 | nul_start = nul_end + 1;
|
||
981 | } else {
|
||
982 | ba516b61 | Thomas Schöpping | ++shell->inputdata.lineend; |
983 | e545e620 | Thomas Schöpping | ++nul_start; |
984 | } |
||
985 | } |
||
986 | ba516b61 | Thomas Schöpping | shell->inputdata.cursorpos = shell->inputdata.lineend; |
987 | e545e620 | Thomas Schöpping | // print the line
|
988 | ba516b61 | Thomas Schöpping | shell->inputdata.noinput = _printLine(shell, 0, shell->inputdata.lineend) == 0; |
989 | e545e620 | Thomas Schöpping | break;
|
990 | } |
||
991 | |||
992 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_CLEAR:
|
993 | { |
||
994 | e545e620 | Thomas Schöpping | // clear output
|
995 | ba516b61 | Thomas Schöpping | _moveCursor(shell, shell->inputdata.cursorpos, 0);
|
996 | for (shell->inputdata.cursorpos = 0; shell->inputdata.cursorpos < shell->inputdata.lineend; ++shell->inputdata.cursorpos) { |
||
997 | streamPut(&shell->stream, ' ');
|
||
998 | e545e620 | Thomas Schöpping | } |
999 | ba516b61 | Thomas Schöpping | _moveCursor(shell, shell->inputdata.lineend, 0);
|
1000 | shell->inputdata.cursorpos = 0;
|
||
1001 | shell->inputdata.lineend = 0;
|
||
1002 | shell->inputdata.noinput = true;
|
||
1003 | e545e620 | Thomas Schöpping | break;
|
1004 | ba516b61 | Thomas Schöpping | } |
1005 | e545e620 | Thomas Schöpping | |
1006 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_CURSOR2START:
|
1007 | { |
||
1008 | _moveCursor(shell, shell->inputdata.cursorpos, 0);
|
||
1009 | shell->inputdata.cursorpos = 0;
|
||
1010 | e545e620 | Thomas Schöpping | break;
|
1011 | ba516b61 | Thomas Schöpping | } |
1012 | e545e620 | Thomas Schöpping | |
1013 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_CURSOR2END:
|
1014 | { |
||
1015 | _moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
||
1016 | shell->inputdata.cursorpos = shell->inputdata.lineend; |
||
1017 | e545e620 | Thomas Schöpping | break;
|
1018 | ba516b61 | Thomas Schöpping | } |
1019 | e545e620 | Thomas Schöpping | |
1020 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_CURSORLEFT:
|
1021 | { |
||
1022 | _moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.cursorpos-1);
|
||
1023 | --shell->inputdata.cursorpos; |
||
1024 | e545e620 | Thomas Schöpping | break;
|
1025 | ba516b61 | Thomas Schöpping | } |
1026 | e545e620 | Thomas Schöpping | |
1027 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_CURSORRIGHT:
|
1028 | { |
||
1029 | _moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.cursorpos+1);
|
||
1030 | ++shell->inputdata.cursorpos; |
||
1031 | e545e620 | Thomas Schöpping | break;
|
1032 | ba516b61 | Thomas Schöpping | } |
1033 | e545e620 | Thomas Schöpping | |
1034 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_EXECUTE:
|
1035 | { |
||
1036 | streamPut(&shell->stream, '\n');
|
||
1037 | // set the number of read bytes and return
|
||
1038 | if (!shell->inputdata.noinput) {
|
||
1039 | *n = shell->linesize - shell->inputdata.lineend; |
||
1040 | e545e620 | Thomas Schöpping | // fill the remainder of the line with NUL bytes
|
1041 | ba516b61 | Thomas Schöpping | memset(&(shell->line[shell->inputdata.lineend]), '\0', *n);
|
1042 | // reset static variables
|
||
1043 | shell->inputdata.noinput = true;
|
||
1044 | e545e620 | Thomas Schöpping | } |
1045 | ba516b61 | Thomas Schöpping | return AOS_SUCCESS;
|
1046 | } |
||
1047 | e545e620 | Thomas Schöpping | |
1048 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_ESCSTART:
|
1049 | { |
||
1050 | shell->inputdata.escseq[0] = c;
|
||
1051 | ++shell->inputdata.escp; |
||
1052 | e545e620 | Thomas Schöpping | break;
|
1053 | ba516b61 | Thomas Schöpping | } |
1054 | e545e620 | Thomas Schöpping | |
1055 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_NONE:
|
1056 | e545e620 | Thomas Schöpping | default:
|
1057 | ba516b61 | Thomas Schöpping | { |
1058 | e545e620 | Thomas Schöpping | // do nothing (ignore input) and read next byte
|
1059 | continue;
|
||
1060 | ba516b61 | Thomas Schöpping | } |
1061 | } /* end of switch */
|
||
1062 | e545e620 | Thomas Schöpping | |
1063 | ba516b61 | Thomas Schöpping | shell->inputdata.lastaction = action; |
1064 | e545e620 | Thomas Schöpping | } /* end of while */
|
1065 | |||
1066 | ba516b61 | Thomas Schöpping | // no more data could be read from the channel
|
1067 | return AOS_WARNING;
|
||
1068 | e545e620 | Thomas Schöpping | } |
1069 | |||
1070 | /**
|
||
1071 | * @brief Parses the content of the input buffer (line) to separate arguments.
|
||
1072 | *
|
||
1073 | * @param[in] shell Pointer to the shell object.
|
||
1074 | *
|
||
1075 | * @return Number of arguments found.
|
||
1076 | */
|
||
1077 | static size_t _parseArguments(aos_shell_t* shell)
|
||
1078 | { |
||
1079 | aosDbgCheck(shell != NULL);
|
||
1080 | |||
1081 | /*
|
||
1082 | * States for a very small FSM.
|
||
1083 | */
|
||
1084 | typedef enum { |
||
1085 | START, |
||
1086 | SPACE, |
||
1087 | TEXT, |
||
1088 | END, |
||
1089 | } state_t; |
||
1090 | |||
1091 | // local variables
|
||
1092 | state_t state = START; |
||
1093 | size_t arg = 0;
|
||
1094 | |||
1095 | // iterate through the line
|
||
1096 | for (char* c = shell->line; c < shell->line + shell->linesize; ++c) { |
||
1097 | // terminate at first NUL byte
|
||
1098 | if (*c == '\0') { |
||
1099 | state = END; |
||
1100 | break;
|
||
1101 | } |
||
1102 | // spaces become NUL bytes
|
||
1103 | else if (*c == ' ') { |
||
1104 | *c = '\0';
|
||
1105 | state = SPACE; |
||
1106 | } |
||
1107 | // handle non-NUL bytes
|
||
1108 | else {
|
||
1109 | switch (state) {
|
||
1110 | case START:
|
||
1111 | case SPACE:
|
||
1112 | // ignore too many arguments
|
||
1113 | if (arg < shell->arglistsize) {
|
||
1114 | shell->arglist[arg] = c; |
||
1115 | } |
||
1116 | ++arg; |
||
1117 | break;
|
||
1118 | case TEXT:
|
||
1119 | case END:
|
||
1120 | default:
|
||
1121 | break;
|
||
1122 | } |
||
1123 | state = TEXT; |
||
1124 | } |
||
1125 | } |
||
1126 | |||
1127 | // set all remaining argument pointers to NULL
|
||
1128 | for (size_t a = arg; a < shell->arglistsize; ++a) {
|
||
1129 | shell->arglist[a] = NULL;
|
||
1130 | } |
||
1131 | |||
1132 | return arg;
|
||
1133 | } |
||
1134 | |||
1135 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
1136 | /* EXPORTED FUNCTIONS */
|
||
1137 | /******************************************************************************/
|
||
1138 | |||
1139 | e545e620 | Thomas Schöpping | /**
|
1140 | * @brief Initializes a shell object with the specified parameters.
|
||
1141 | *
|
||
1142 | * @param[in] shell Pointer to the shell object.
|
||
1143 | * @param[in] stream I/O stream to use.
|
||
1144 | * @param[in] prompt Prompt line to print (NULL = use default prompt).
|
||
1145 | * @param[in] line Pointer to the input buffer.
|
||
1146 | * @param[in] linesize Size of the input buffer.
|
||
1147 | * @param[in] arglist Pointer to the argument buffer.
|
||
1148 | * @param[in] arglistsize Size of te argument buffer.
|
||
1149 | */
|
||
1150 | ba516b61 | Thomas Schöpping | void aosShellInit(aos_shell_t* shell, event_source_t* oseventsource, const char* prompt, char* line, size_t linesize, char** arglist, size_t arglistsize) |
1151 | e545e620 | Thomas Schöpping | { |
1152 | aosDbgCheck(shell != NULL);
|
||
1153 | ba516b61 | Thomas Schöpping | aosDbgCheck(oseventsource != NULL);
|
1154 | e545e620 | Thomas Schöpping | aosDbgCheck(line != NULL);
|
1155 | aosDbgCheck(arglist != NULL);
|
||
1156 | |||
1157 | // set parameters
|
||
1158 | shell->thread = NULL;
|
||
1159 | chEvtObjectInit(&shell->eventSource); |
||
1160 | ba516b61 | Thomas Schöpping | shell->os.eventSource = oseventsource; |
1161 | aosShellStreamInit(&shell->stream); |
||
1162 | e545e620 | Thomas Schöpping | shell->prompt = prompt; |
1163 | shell->commands = NULL;
|
||
1164 | shell->execstatus.command = NULL;
|
||
1165 | shell->execstatus.retval = 0;
|
||
1166 | shell->line = line; |
||
1167 | shell->linesize = linesize; |
||
1168 | ba516b61 | Thomas Schöpping | shell->inputdata.lastaction = AOS_SHELL_ACTION_NONE; |
1169 | shell->inputdata.escp = 0;
|
||
1170 | memset(shell->inputdata.escseq, '\0', sizeof(shell->inputdata.escseq)*sizeof(shell->inputdata.escseq[0])); |
||
1171 | shell->inputdata.cursorpos = 0;
|
||
1172 | shell->inputdata.lineend = 0;
|
||
1173 | shell->inputdata.noinput = true;
|
||
1174 | e545e620 | Thomas Schöpping | shell->arglist = arglist; |
1175 | shell->arglistsize = arglistsize; |
||
1176 | shell->config = 0x00;
|
||
1177 | |||
1178 | // initialize arrays
|
||
1179 | memset(shell->line, '\0', shell->linesize);
|
||
1180 | for (size_t a = 0; a < shell->arglistsize; ++a) { |
||
1181 | shell->arglist[a] = NULL;
|
||
1182 | } |
||
1183 | |||
1184 | return;
|
||
1185 | } |
||
1186 | |||
1187 | /**
|
||
1188 | ba516b61 | Thomas Schöpping | * @brief Initialize an AosShellStream object.
|
1189 | *
|
||
1190 | * @param[in] stream The AosShellStrem to initialize.
|
||
1191 | */
|
||
1192 | void aosShellStreamInit(AosShellStream* stream)
|
||
1193 | { |
||
1194 | aosDbgCheck(stream != NULL);
|
||
1195 | |||
1196 | stream->vmt = &_streamvmt; |
||
1197 | stream->channel = NULL;
|
||
1198 | |||
1199 | return;
|
||
1200 | } |
||
1201 | |||
1202 | /**
|
||
1203 | * @brief Initialize an AosShellChannel object with the specified parameters.
|
||
1204 | *
|
||
1205 | dd8738ea | Thomas Schöpping | * @param[in] channel The AosShellChannel to initialize.
|
1206 | * @param[in] asyncchannel An BaseAsynchronousChannel this AosShellChannel is associated with.
|
||
1207 | ba516b61 | Thomas Schöpping | */
|
1208 | dd8738ea | Thomas Schöpping | void aosShellChannelInit(AosShellChannel* channel, BaseAsynchronousChannel* asyncchannel)
|
1209 | ba516b61 | Thomas Schöpping | { |
1210 | aosDbgCheck(channel != NULL);
|
||
1211 | dd8738ea | Thomas Schöpping | aosDbgCheck(asyncchannel != NULL);
|
1212 | ba516b61 | Thomas Schöpping | |
1213 | channel->vmt = &_channelvmt; |
||
1214 | dd8738ea | Thomas Schöpping | channel->asyncchannel = asyncchannel; |
1215 | channel->listener.wflags = 0;
|
||
1216 | ba516b61 | Thomas Schöpping | channel->next = NULL;
|
1217 | channel->flags = 0;
|
||
1218 | |||
1219 | return;
|
||
1220 | } |
||
1221 | |||
1222 | /**
|
||
1223 | e545e620 | Thomas Schöpping | * @brief Inserts a command to the shells list of commands.
|
1224 | *
|
||
1225 | * @param[in] shell Pointer to the shell object.
|
||
1226 | * @param[in] cmd Pointer to the command to add.
|
||
1227 | *
|
||
1228 | * @return A status value.
|
||
1229 | * @retval AOS_SUCCESS The command was added successfully.
|
||
1230 | * @retval AOS_ERROR Another command with identical name already exists.
|
||
1231 | */
|
||
1232 | aos_status_t aosShellAddCommand(aos_shell_t *shell, aos_shellcommand_t *cmd) |
||
1233 | { |
||
1234 | aosDbgCheck(shell != NULL);
|
||
1235 | aosDbgCheck(cmd != NULL);
|
||
1236 | aosDbgCheck(cmd->name != NULL && strlen(cmd->name) > 0 && strchr(cmd->name, ' ') == NULL && strchr(cmd->name, '\t') == NULL); |
||
1237 | aosDbgCheck(cmd->callback != NULL);
|
||
1238 | aosDbgCheck(cmd->next == NULL);
|
||
1239 | |||
1240 | aos_shellcommand_t* prev = NULL;
|
||
1241 | aos_shellcommand_t** curr = &(shell->commands); |
||
1242 | |||
1243 | // insert the command to the list wrt lexographical order (exception: lower case characters preceed upper their uppercase counterparts)
|
||
1244 | ba516b61 | Thomas Schöpping | while (*curr != NULL) { |
1245 | // iterate through the list as long as the command names are 'smaller'
|
||
1246 | const int cmp = _strccmp((*curr)->name, cmd->name, true, NULL, NULL); |
||
1247 | if (cmp < 0) { |
||
1248 | prev = *curr; |
||
1249 | curr = &((*curr)->next); |
||
1250 | continue;
|
||
1251 | } |
||
1252 | // error if the command already exists
|
||
1253 | else if (cmp == 0) { |
||
1254 | return AOS_ERROR;
|
||
1255 | } |
||
1256 | // insert the command as soon as a 'larger' name was found
|
||
1257 | else /* if (cmpval > 0) */ { |
||
1258 | cmd->next = *curr; |
||
1259 | // special case: the first command is larger
|
||
1260 | if (prev == NULL) { |
||
1261 | shell->commands = cmd; |
||
1262 | } else {
|
||
1263 | prev->next = cmd; |
||
1264 | e545e620 | Thomas Schöpping | } |
1265 | ba516b61 | Thomas Schöpping | return AOS_SUCCESS;
|
1266 | e545e620 | Thomas Schöpping | } |
1267 | } |
||
1268 | ba516b61 | Thomas Schöpping | // the end of the list has been reached
|
1269 | |||
1270 | // append the command
|
||
1271 | *curr = cmd; |
||
1272 | return AOS_SUCCESS;
|
||
1273 | e545e620 | Thomas Schöpping | } |
1274 | |||
1275 | /**
|
||
1276 | * @brief Removes a command from the shells list of commands.
|
||
1277 | *
|
||
1278 | * @param[in] shell Pointer to the shell object.
|
||
1279 | * @param[in] cmd Name of the command to removde.
|
||
1280 | * @param[out] removed Optional pointer to the command that was removed.
|
||
1281 | *
|
||
1282 | * @return A status value.
|
||
1283 | * @retval AOS_SUCCESS The command was removed successfully.
|
||
1284 | * @retval AOS_ERROR The command name was not found.
|
||
1285 | */
|
||
1286 | aos_status_t aosShellRemoveCommand(aos_shell_t *shell, char *cmd, aos_shellcommand_t **removed)
|
||
1287 | { |
||
1288 | aosDbgCheck(shell != NULL);
|
||
1289 | aosDbgCheck(cmd != NULL && strlen(cmd) > 0); |
||
1290 | |||
1291 | aos_shellcommand_t* prev = NULL;
|
||
1292 | aos_shellcommand_t** curr = &(shell->commands); |
||
1293 | |||
1294 | // iterate through the list and seach for the specified command name
|
||
1295 | while (curr != NULL) { |
||
1296 | const int cmpval = strcmp((*curr)->name, cmd); |
||
1297 | // iterate through the list as long as the command names are 'smaller'
|
||
1298 | if (cmpval < 0) { |
||
1299 | prev = *curr; |
||
1300 | curr = &((*curr)->next); |
||
1301 | continue;
|
||
1302 | } |
||
1303 | // remove the command when found
|
||
1304 | else if (cmpval == 0) { |
||
1305 | // special case: the first command matches
|
||
1306 | if (prev == NULL) { |
||
1307 | shell->commands = (*curr)->next; |
||
1308 | } else {
|
||
1309 | prev->next = (*curr)->next; |
||
1310 | } |
||
1311 | (*curr)->next = NULL;
|
||
1312 | // set the optional output argument
|
||
1313 | if (removed != NULL) { |
||
1314 | *removed = *curr; |
||
1315 | } |
||
1316 | return AOS_SUCCESS;
|
||
1317 | } |
||
1318 | // break the loop if the command names are 'larger'
|
||
1319 | else /* if (cmpval > 0) */ { |
||
1320 | break;
|
||
1321 | } |
||
1322 | } |
||
1323 | |||
1324 | // if the command was not found, return an error
|
||
1325 | return AOS_ERROR;
|
||
1326 | } |
||
1327 | |||
1328 | /**
|
||
1329 | aed3754b | Thomas Schöpping | * @brief Count the number of commands assigned to the shell.
|
1330 | *
|
||
1331 | * @param[in] shell The shell to count the commands for.
|
||
1332 | *
|
||
1333 | * @return The number of commands associated to the shell.
|
||
1334 | */
|
||
1335 | unsigned int aosShellCountCommands(aos_shell_t* shell) |
||
1336 | { |
||
1337 | aosDbgCheck(shell != NULL);
|
||
1338 | |||
1339 | unsigned int count = 0; |
||
1340 | aos_shellcommand_t* cmd = shell->commands; |
||
1341 | while (cmd != NULL) { |
||
1342 | ++count; |
||
1343 | cmd = cmd->next; |
||
1344 | } |
||
1345 | |||
1346 | return count;
|
||
1347 | } |
||
1348 | |||
1349 | /**
|
||
1350 | ba516b61 | Thomas Schöpping | * @brief Add a channel to a AosShellStream.
|
1351 | *
|
||
1352 | * @param[in] stream The AosShellStream to extend.
|
||
1353 | * @param[in] channel The channel to be added to the stream.
|
||
1354 | */
|
||
1355 | void aosShellStreamAddChannel(AosShellStream* stream, AosShellChannel* channel)
|
||
1356 | { |
||
1357 | aosDbgCheck(stream != NULL);
|
||
1358 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL && channel->next == NULL && (channel->flags & AOS_SHELLCHANNEL_ATTACHED) == 0); |
1359 | ba516b61 | Thomas Schöpping | |
1360 | // prepend the new channel
|
||
1361 | chSysLock(); |
||
1362 | channel->flags |= AOS_SHELLCHANNEL_ATTACHED; |
||
1363 | channel->next = stream->channel; |
||
1364 | stream->channel = channel; |
||
1365 | chSysUnlock(); |
||
1366 | |||
1367 | return;
|
||
1368 | } |
||
1369 | |||
1370 | /**
|
||
1371 | * @brief Remove a channel from an AosShellStream.
|
||
1372 | *
|
||
1373 | * @param[in] stream The AosShellStream to modify.
|
||
1374 | * @param[in] channel The channel to remove.
|
||
1375 | aed3754b | Thomas Schöpping | *
|
1376 | * @return A status value.
|
||
1377 | * @retval AOS_SUCCESS The channel was removed successfully.
|
||
1378 | * @retval AOS_ERROR The specified channel was not found to be associated with the shell.
|
||
1379 | ba516b61 | Thomas Schöpping | */
|
1380 | aos_status_t aosShellStreamRemoveChannel(AosShellStream* stream, AosShellChannel* channel) |
||
1381 | { |
||
1382 | aosDbgCheck(stream != NULL);
|
||
1383 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL && channel->flags & AOS_SHELLCHANNEL_ATTACHED); |
1384 | ba516b61 | Thomas Schöpping | |
1385 | // local varibales
|
||
1386 | AosShellChannel* prev = NULL;
|
||
1387 | AosShellChannel* curr = stream->channel; |
||
1388 | |||
1389 | // iterate through the list and search for the specified channel
|
||
1390 | while (curr != NULL) { |
||
1391 | // if the channel was found
|
||
1392 | if (curr == channel) {
|
||
1393 | chSysLock(); |
||
1394 | // special case: the first channel matches (prev is NULL)
|
||
1395 | if (prev == NULL) { |
||
1396 | stream->channel = curr->next; |
||
1397 | } else {
|
||
1398 | prev->next = channel->next; |
||
1399 | } |
||
1400 | curr->next = NULL;
|
||
1401 | curr->flags &= ~AOS_SHELLCHANNEL_ATTACHED; |
||
1402 | chSysUnlock(); |
||
1403 | return AOS_SUCCESS;
|
||
1404 | } |
||
1405 | } |
||
1406 | |||
1407 | // if the channel was not found, return an error
|
||
1408 | return AOS_ERROR;
|
||
1409 | } |
||
1410 | |||
1411 | /**
|
||
1412 | * @brief Enable a AosSheööChannel as input.
|
||
1413 | *
|
||
1414 | * @param[in] channel The channel to enable as input.
|
||
1415 | */
|
||
1416 | void aosShellChannelInputEnable(AosShellChannel* channel)
|
||
1417 | { |
||
1418 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
1419 | ba516b61 | Thomas Schöpping | |
1420 | chSysLock(); |
||
1421 | channel->listener.wflags |= CHN_INPUT_AVAILABLE; |
||
1422 | channel->flags |= AOS_SHELLCHANNEL_INPUT_ENABLED; |
||
1423 | chSysUnlock(); |
||
1424 | |||
1425 | return;
|
||
1426 | } |
||
1427 | |||
1428 | /**
|
||
1429 | * @brief Disable a AosSheööChannel as input.
|
||
1430 | *
|
||
1431 | * @param[in] channel The channel to disable as input.
|
||
1432 | */
|
||
1433 | void aosShellChannelInputDisable( AosShellChannel* channel)
|
||
1434 | { |
||
1435 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
1436 | ba516b61 | Thomas Schöpping | |
1437 | chSysLock(); |
||
1438 | channel->listener.wflags &= ~CHN_INPUT_AVAILABLE; |
||
1439 | channel->flags &= ~AOS_SHELLCHANNEL_INPUT_ENABLED; |
||
1440 | chSysUnlock(); |
||
1441 | |||
1442 | return;
|
||
1443 | } |
||
1444 | |||
1445 | /**
|
||
1446 | * @brief Enable a AosSheööChannel as output.
|
||
1447 | *
|
||
1448 | * @param[in] channel The channel to enable as output.
|
||
1449 | */
|
||
1450 | void aosShellChannelOutputEnable(AosShellChannel* channel)
|
||
1451 | { |
||
1452 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
1453 | ba516b61 | Thomas Schöpping | |
1454 | channel->flags |= AOS_SHELLCHANNEL_OUTPUT_ENABLED; |
||
1455 | |||
1456 | return;
|
||
1457 | } |
||
1458 | |||
1459 | /**
|
||
1460 | * @brief Disable a AosSheööChannel as output.
|
||
1461 | *
|
||
1462 | * @param[in] channel The channel to disable as output.
|
||
1463 | */
|
||
1464 | void aosShellChannelOutputDisable(AosShellChannel* channel)
|
||
1465 | { |
||
1466 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
1467 | ba516b61 | Thomas Schöpping | |
1468 | channel->flags &= ~AOS_SHELLCHANNEL_OUTPUT_ENABLED; |
||
1469 | |||
1470 | return;
|
||
1471 | } |
||
1472 | |||
1473 | /**
|
||
1474 | e545e620 | Thomas Schöpping | * @brief Thread main function.
|
1475 | *
|
||
1476 | * @param[in] aosShellThread Name of the function;
|
||
1477 | * @param[in] shell Pointer to the shell object.
|
||
1478 | */
|
||
1479 | af4fd4a2 | Thomas Schöpping | void aosShellThread(void* shell) |
1480 | e545e620 | Thomas Schöpping | { |
1481 | aosDbgCheck(shell != NULL);
|
||
1482 | |||
1483 | // local variables
|
||
1484 | ba516b61 | Thomas Schöpping | eventmask_t eventmask; |
1485 | eventflags_t eventflags; |
||
1486 | AosShellChannel* channel; |
||
1487 | aos_status_t readeval; |
||
1488 | size_t nchars = 0;
|
||
1489 | e545e620 | Thomas Schöpping | size_t nargs = 0;
|
1490 | ba516b61 | Thomas Schöpping | aos_shellcommand_t* cmd; |
1491 | |||
1492 | |||
1493 | // register OS related events
|
||
1494 | chEvtRegisterMask(((aos_shell_t*)shell)->os.eventSource, &(((aos_shell_t*)shell)->os.eventListener), AOS_SHELL_EVENTMASK_OS); |
||
1495 | // register events to all input channels
|
||
1496 | for (channel = ((aos_shell_t*)shell)->stream.channel; channel != NULL; channel = channel->next) { |
||
1497 | dd8738ea | Thomas Schöpping | chEvtRegisterMaskWithFlags(&(channel->asyncchannel->event), &(channel->listener), AOS_SHELL_EVENTMASK_INPUT, channel->listener.wflags); |
1498 | ba516b61 | Thomas Schöpping | } |
1499 | e545e620 | Thomas Schöpping | |
1500 | // fire start event
|
||
1501 | chEvtBroadcastFlags(&(((aos_shell_t*)shell)->eventSource), AOS_SHELL_EVTFLAG_START); |
||
1502 | |||
1503 | ba516b61 | Thomas Schöpping | // print the prompt for the first time
|
1504 | _printPrompt((aos_shell_t*)shell); |
||
1505 | |||
1506 | e545e620 | Thomas Schöpping | // enter thread loop
|
1507 | while (!chThdShouldTerminateX()) {
|
||
1508 | ba516b61 | Thomas Schöpping | // wait for event and handle it accordingly
|
1509 | eventmask = chEvtWaitOne(ALL_EVENTS); |
||
1510 | |||
1511 | // handle event
|
||
1512 | switch (eventmask) {
|
||
1513 | |||
1514 | // OS related events
|
||
1515 | case AOS_SHELL_EVENTMASK_OS:
|
||
1516 | { |
||
1517 | eventflags = chEvtGetAndClearFlags(&((aos_shell_t*)shell)->os.eventListener); |
||
1518 | // handle shutdown/restart events
|
||
1519 | if (eventflags & AOS_SYSTEM_EVENTFLAGS_SHUTDOWN) {
|
||
1520 | chThdTerminate(((aos_shell_t*)shell)->thread); |
||
1521 | } else {
|
||
1522 | // print an error message
|
||
1523 | chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "\nERROR: unknown OS event received (0x%08X)\n", eventflags);
|
||
1524 | } |
||
1525 | e545e620 | Thomas Schöpping | break;
|
1526 | } |
||
1527 | |||
1528 | ba516b61 | Thomas Schöpping | // input events
|
1529 | case AOS_SHELL_EVENTMASK_INPUT:
|
||
1530 | { |
||
1531 | // check and handle all channels
|
||
1532 | channel = ((aos_shell_t*)shell)->stream.channel; |
||
1533 | while (channel != NULL) { |
||
1534 | eventflags = chEvtGetAndClearFlags(&channel->listener); |
||
1535 | // if there is new input
|
||
1536 | if (eventflags & CHN_INPUT_AVAILABLE) {
|
||
1537 | dd8738ea | Thomas Schöpping | // read input from channel
|
1538 | readeval = _readChannel((aos_shell_t*)shell, channel, &nchars); |
||
1539 | // parse input line to argument list only if the input shall be executed
|
||
1540 | nargs = (readeval == AOS_SUCCESS && nchars > 0) ? _parseArguments((aos_shell_t*)shell) : 0; |
||
1541 | // check number of arguments
|
||
1542 | if (nargs > ((aos_shell_t*)shell)->arglistsize) {
|
||
1543 | // error too many arguments
|
||
1544 | chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "\ttoo many arguments\n");
|
||
1545 | } else if (nargs > 0) { |
||
1546 | // search command list for arg[0] and execute callback
|
||
1547 | cmd = ((aos_shell_t*)shell)->commands; |
||
1548 | while (cmd != NULL) { |
||
1549 | if (strcmp(((aos_shell_t*)shell)->arglist[0], cmd->name) == 0) { |
||
1550 | ((aos_shell_t*)shell)->execstatus.command = cmd; |
||
1551 | chEvtBroadcastFlags(&(((aos_shell_t*)shell)->eventSource), AOS_SHELL_EVTFLAG_EXEC); |
||
1552 | ((aos_shell_t*)shell)->execstatus.retval = cmd->callback((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, nargs, ((aos_shell_t*)shell)->arglist); |
||
1553 | chEvtBroadcastFlags(&(((aos_shell_t*)shell)->eventSource), AOS_SHELL_EVTFLAG_DONE); |
||
1554 | // notify if the command was not successful
|
||
1555 | if (((aos_shell_t*)shell)->execstatus.retval != 0) { |
||
1556 | chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "command returned exit status %d\n", ((aos_shell_t*)shell)->execstatus.retval);
|
||
1557 | ba516b61 | Thomas Schöpping | } |
1558 | dd8738ea | Thomas Schöpping | break;
|
1559 | ba516b61 | Thomas Schöpping | } |
1560 | dd8738ea | Thomas Schöpping | cmd = cmd->next; |
1561 | } /* end of while */
|
||
1562 | e545e620 | Thomas Schöpping | |
1563 | dd8738ea | Thomas Schöpping | // if no matching command was found, print an error
|
1564 | if (cmd == NULL) { |
||
1565 | chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "%s: command not found\n", ((aos_shell_t*)shell)->arglist[0]); |
||
1566 | ba516b61 | Thomas Schöpping | } |
1567 | } |
||
1568 | dd8738ea | Thomas Schöpping | |
1569 | // reset some internal variables and eprint a new prompt
|
||
1570 | if (readeval == AOS_SUCCESS && !chThdShouldTerminateX()) {
|
||
1571 | ((aos_shell_t*)shell)->inputdata.cursorpos = 0;
|
||
1572 | ((aos_shell_t*)shell)->inputdata.lineend = 0;
|
||
1573 | _printPrompt((aos_shell_t*)shell); |
||
1574 | ba516b61 | Thomas Schöpping | } |
1575 | e545e620 | Thomas Schöpping | } |
1576 | ba516b61 | Thomas Schöpping | |
1577 | // iterate to next channel
|
||
1578 | channel = channel->next; |
||
1579 | e545e620 | Thomas Schöpping | } |
1580 | ba516b61 | Thomas Schöpping | break;
|
1581 | e545e620 | Thomas Schöpping | } |
1582 | ba516b61 | Thomas Schöpping | |
1583 | // other events
|
||
1584 | default:
|
||
1585 | { |
||
1586 | // print an error message
|
||
1587 | 1e5f7648 | Thomas Schöpping | chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "\nSHELL: ERROR: unknown event received (0x%08X)\n", eventmask);
|
1588 | ba516b61 | Thomas Schöpping | break;
|
1589 | e545e620 | Thomas Schöpping | } |
1590 | |||
1591 | ba516b61 | Thomas Schöpping | } /* end of switch */
|
1592 | |||
1593 | } /* end of while */
|
||
1594 | e545e620 | Thomas Schöpping | |
1595 | // fire event and exit the thread
|
||
1596 | chSysLock(); |
||
1597 | chEvtBroadcastFlagsI(&(((aos_shell_t*)shell)->eventSource), AOS_SHELL_EVTFLAG_EXIT); |
||
1598 | chThdExitS(MSG_OK); |
||
1599 | // no chSysUnlock() required since the thread has been terminated an all waiting threads have been woken up
|
||
1600 | } |
||
1601 | ba516b61 | Thomas Schöpping | |
1602 | 2dd2e257 | Thomas Schöpping | #endif /* (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true)*/ |
1603 | 53710ca3 | Marc Rothmann | |
1604 | /** @} */ |