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