amiro-os / core / src / aos_shell.c @ cacd990c
History | View | Annotate | Download (73.613 KB)
1 | e545e620 | Thomas Schöpping | /*
|
---|---|---|---|
2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
3 | 96621a83 | Thomas Schöpping | Copyright (C) 2016..2020 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 | ab5cad1b | Thomas Schöpping | * @brief Size of the escape sequence buffer.
|
39 | */
|
||
40 | #if !defined(AOS_SHELL_ESCSEQUENCE_LENGTH) || defined(__DOXYGEN__)
|
||
41 | #define AOS_SHELL_ESCSEQUENCE_LENGTH 8 |
||
42 | #endif
|
||
43 | |||
44 | /**
|
||
45 | 697dba3c | Thomas Schöpping | * @brief The character the input buffer is initialized with.
|
46 | */
|
||
47 | #define INBUF_INIT_CHAR '\x07' |
||
48 | |||
49 | /**
|
||
50 | ba516b61 | Thomas Schöpping | * @brief Event mask to be set on OS related events.
|
51 | */
|
||
52 | 697dba3c | Thomas Schöpping | #define EVENTMASK_OS EVENT_MASK(0) |
53 | ba516b61 | Thomas Schöpping | |
54 | /**
|
||
55 | * @brief Event mask to be set on a input event.
|
||
56 | */
|
||
57 | 697dba3c | Thomas Schöpping | #define EVENTMASK_INPUT EVENT_MASK(1) |
58 | |||
59 | /**
|
||
60 | * @brief String that defines the INSERT key as specified by VT100.
|
||
61 | */
|
||
62 | #define KEYSTRING_INSERT "\x1B\x5B\x32\x7E" |
||
63 | |||
64 | /**
|
||
65 | * @brief String that defines the DEL key as specified by VT100.
|
||
66 | */
|
||
67 | #define KEYSTRING_DELETE "\x1B\x5B\x33\x7E" |
||
68 | |||
69 | /**
|
||
70 | * @brief String that defines the HOME key as specified by VT100.
|
||
71 | */
|
||
72 | #define KEYSTRING_HOME "\x1B\x5B\x48" |
||
73 | |||
74 | /**
|
||
75 | * @brief String that defines the END key as specified by VT100.
|
||
76 | */
|
||
77 | #define KEYSTRING_END "\x1B\x5B\x46" |
||
78 | |||
79 | /**
|
||
80 | * @brief String that defines the PGUP key as specified by VT100.
|
||
81 | */
|
||
82 | #define KEYSTRING_PAGEUP "\x1B\x5B\x35\x7E" |
||
83 | |||
84 | /**
|
||
85 | * @brief String that defines the PGUP key as specified by VT100.
|
||
86 | */
|
||
87 | #define KEYSTRING_PAGEDOWN "\x1B\x5B\x36\x7E" |
||
88 | |||
89 | /**
|
||
90 | * @brief String that defines the 'arrow down' key as specified by VT100.
|
||
91 | */
|
||
92 | #define KEYSTRING_ARROWUP "\x1B\x5B\x41" |
||
93 | |||
94 | /**
|
||
95 | * @brief String that defines the 'arrow up' key as specified by VT100.
|
||
96 | */
|
||
97 | #define KEYSTRING_ARROWDOWN "\x1B\x5B\x42" |
||
98 | |||
99 | /**
|
||
100 | * @brief String that defines the 'arrow left' key as specified by VT100.
|
||
101 | */
|
||
102 | #define KEYSTRING_ARROWLEFT "\x1B\x5B\x44" |
||
103 | |||
104 | /**
|
||
105 | * @brief String that defines the 'arrow right' key as specified by VT100.
|
||
106 | */
|
||
107 | #define KEYSTRING_ARROWRIGHT "\x1B\x5B\x43" |
||
108 | |||
109 | /**
|
||
110 | * @brief String that defines the CRTL + 'arrow up' key combination as specified by VT100.
|
||
111 | */
|
||
112 | #define KEYSTRING_CTRL_ARROWUP "\x1B\x5B\x31\x3B\x35\x41" |
||
113 | |||
114 | /**
|
||
115 | * @brief String that defines the CRTL + 'arrow down' key combination as specified by VT100.
|
||
116 | */
|
||
117 | #define KEYSTRING_CTRL_ARROWDOWN "\x1B\x5B\x31\x3B\x35\x42" |
||
118 | |||
119 | /**
|
||
120 | * @brief String that defines the CRTL + 'arrow left' key combination as specified by VT100.
|
||
121 | */
|
||
122 | #define KEYSTRING_CTRL_ARROWLEFT "\x1B\x5B\x31\x3B\x35\x44" |
||
123 | |||
124 | /**
|
||
125 | * @brief String that defines the CRTL + 'arrow right' key combination as specified by VT100.
|
||
126 | */
|
||
127 | #define KEYSTRING_CTRL_ARROWRIGHT "\x1B\x5B\x31\x3B\x35\x43" |
||
128 | ba516b61 | Thomas Schöpping | |
129 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
130 | /* EXPORTED VARIABLES */
|
||
131 | /******************************************************************************/
|
||
132 | |||
133 | /******************************************************************************/
|
||
134 | /* LOCAL TYPES */
|
||
135 | /******************************************************************************/
|
||
136 | |||
137 | /*
|
||
138 | * forward declarations
|
||
139 | */
|
||
140 | static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n); |
||
141 | static size_t _channelread(void *instance, uint8_t *bp, size_t n); |
||
142 | static msg_t _channelput(void *instance, uint8_t b); |
||
143 | static msg_t _channelget(void *instance); |
||
144 | static msg_t _channelputt(void *instance, uint8_t b, sysinterval_t time); |
||
145 | static msg_t _channelgett(void *instance, sysinterval_t time); |
||
146 | static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, sysinterval_t time); |
||
147 | static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, sysinterval_t time); |
||
148 | static msg_t _channelctl(void *instance, unsigned int operation, void *arg); |
||
149 | static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n); |
||
150 | static size_t _stremread(void *instance, uint8_t *bp, size_t n); |
||
151 | static msg_t _streamput(void *instance, uint8_t b); |
||
152 | static msg_t _streamget(void *instance); |
||
153 | |||
154 | static const struct AosShellChannelVMT _channelvmt = { |
||
155 | (size_t) 0,
|
||
156 | _channelwrite, |
||
157 | _channelread, |
||
158 | _channelput, |
||
159 | _channelget, |
||
160 | _channelputt, |
||
161 | _channelgett, |
||
162 | _channelwritet, |
||
163 | _channelreadt, |
||
164 | _channelctl, |
||
165 | }; |
||
166 | |||
167 | static const struct AosShellStreamVMT _streamvmt = { |
||
168 | (size_t) 0,
|
||
169 | _streamwrite, |
||
170 | _stremread, |
||
171 | _streamput, |
||
172 | _streamget, |
||
173 | }; |
||
174 | |||
175 | /**
|
||
176 | * @brief Enumerator of special keyboard keys.
|
||
177 | */
|
||
178 | typedef enum special_key { |
||
179 | cc33217b | Thomas Schöpping | KEY_UNKNOWN, /**< any/unknow key */
|
180 | KEY_AMBIGUOUS, /**< key is ambiguous */
|
||
181 | KEY_TAB, /**< tabulator key */
|
||
182 | KEY_BACKSPACE, /**< backspace key */
|
||
183 | KEY_INSERT, /**< insert key */
|
||
184 | KEY_DELETE, /**< delete key */
|
||
185 | 697dba3c | Thomas Schöpping | KEY_ESCAPE, /**< escape key */
|
186 | cc33217b | Thomas Schöpping | KEY_HOME, /**< home key */
|
187 | KEY_END, /**< end key */
|
||
188 | 697dba3c | Thomas Schöpping | KEY_PAGEUP, /**< page up key */
|
189 | KEY_PAGEDOWN, /**< page down key */
|
||
190 | KEY_ARROWUP, /**< arrow up key */
|
||
191 | KEY_ARROWDOWN, /**< arrow down key */
|
||
192 | KEY_ARROWLEFT, /**< arrow left key */
|
||
193 | KEY_ARROWRIGHT, /**< arrow right key */
|
||
194 | KEY_CTRL_ARROWUP, /**< CTRL + arrow up key */
|
||
195 | KEY_CTRL_ARROWDOWN, /**< CTRL + arrow down key */
|
||
196 | KEY_CTRL_ARROWLEFT, /**< CTRL + arrow left key */
|
||
197 | KEY_CTRL_ARROWRIGHT, /**< CTRL + arrow right key */
|
||
198 | KEY_CTRL_C, /**< CTRL + C key */
|
||
199 | f3ac1c96 | Thomas Schöpping | } special_key_t; |
200 | |||
201 | /**
|
||
202 | * @brief Enumerator for case (in)sensitive character matching.
|
||
203 | */
|
||
204 | typedef enum charmatch { |
||
205 | CHAR_MATCH_NOT = 0, /**< Characters do not match at all. */ |
||
206 | CHAR_MATCH_NCASE = 1, /**< Characters would match case insensitive. */ |
||
207 | CHAR_MATCH_CASE = 2, /**< Characters do match with case. */ |
||
208 | } charmatch_t; |
||
209 | |||
210 | 697dba3c | Thomas Schöpping | /**
|
211 | * @brief Enumerator to encode shell actions.
|
||
212 | */
|
||
213 | typedef enum aos_shellaction { |
||
214 | ACTION_NONE, /**< No action at all. */
|
||
215 | ACTION_READCHAR, /**< Read a printable character. */
|
||
216 | ACTION_AUTOCOMPLETE, /**< Automatically comlete input by using available command. */
|
||
217 | ACTION_SUGGEST, /**< Suggest matching available commands. */
|
||
218 | ACTION_EXECUTE, /**< Execute input. */
|
||
219 | ACTION_DELETEBACKWARD, /**< Delete a single character backwards. */
|
||
220 | ACTION_DELETEFORWARD, /**< Delete a single character forwards. */
|
||
221 | ACTION_CLEAR, /**< Clear the input. */
|
||
222 | ACTION_RECALLPREVIOUS, /**< Recall the previous (older) entry in the history. */
|
||
223 | ACTION_RECALLNEXT, /**< Recall the next (more recent) entry in the history. */
|
||
224 | ACTION_RECALLOLDEST, /**< Recall the oldest entry in the history. */
|
||
225 | ACTION_RECALLCURRENT, /**< Recall the current input. */
|
||
226 | ACTION_CURSORLEFT, /**< Move cursor one character to the left. */
|
||
227 | ACTION_CURSORRIGHT, /**< Move cursor one character to the right. */
|
||
228 | ACTION_CURSORWORDLEFT, /**< Move cursor one word to the left. */
|
||
229 | ACTION_CURSORWORDRIGHT, /**< Move cursor one word to the right. */
|
||
230 | ACTION_CURSOR2END, /**< Move cursor to the very right. */
|
||
231 | ACTION_CURSOR2START, /**< Move cursor to the very left. */
|
||
232 | ACTION_RESET, /**< Reset the current input. */
|
||
233 | ACTION_INSERTTOGGLE, /**< Toggle insertion mode. */
|
||
234 | ACTION_ESCSTART, /**< Start an escape sequence (special keys). */
|
||
235 | ACTION_PRINTUNKNOWNSEQUENCE, /**< Print an unknown escape sequence. */
|
||
236 | } action_t; |
||
237 | |||
238 | /**
|
||
239 | * @brief Struct that holds most important runtime data for the shell.
|
||
240 | * @details The structure is to be used by the shell thread main function as some kind of structured stack, which can be easily passed to other functions.
|
||
241 | */
|
||
242 | typedef struct runtimedata { |
||
243 | /**
|
||
244 | * @brief Data related to the current input.
|
||
245 | */
|
||
246 | struct {
|
||
247 | /**
|
||
248 | * @brief Length of the input.
|
||
249 | */
|
||
250 | size_t length; |
||
251 | |||
252 | /**
|
||
253 | * @brief Current position of the cursor in the input line.
|
||
254 | */
|
||
255 | size_t cursorpos; |
||
256 | |||
257 | /**
|
||
258 | * @brief Buffer to store escape sequences, which describe special characters.
|
||
259 | */
|
||
260 | char escseq[AOS_SHELL_ESCSEQUENCE_LENGTH];
|
||
261 | } input; |
||
262 | |||
263 | /**
|
||
264 | * @brief Data related to the entry or history buffer.
|
||
265 | */
|
||
266 | struct {
|
||
267 | /**
|
||
268 | * @brief Current entry to be filled and executed.
|
||
269 | */
|
||
270 | size_t current; |
||
271 | |||
272 | /**
|
||
273 | * @brief Selected entry in the 'history' as preview.
|
||
274 | * @details A value of 0 indicates, that the line is cleared as a preview.
|
||
275 | * A value of 1 indicates, that the current entry is selected.
|
||
276 | * A value of t>1 indicates, that the entry t-1 in the past is selected.
|
||
277 | * The value must never be greater than the number of entries available, of course.
|
||
278 | */
|
||
279 | size_t selected; |
||
280 | |||
281 | /**
|
||
282 | * @brief Selected entry in the 'history' that has been edited by the user.
|
||
283 | * A value of 0 indicates, that there was no modification by the user yet (i.e. charcters, deletions or autofill).
|
||
284 | * A value of 1 indicates, that the current entry was edited.
|
||
285 | * A value of t>1 indicated, that a history entry was recalled and then edited.
|
||
286 | */
|
||
287 | size_t edited; |
||
288 | } buffer; |
||
289 | |||
290 | /**
|
||
291 | * @brief The last action executed by the shell.
|
||
292 | */
|
||
293 | action_t lastaction; |
||
294 | } runtimedata_t; |
||
295 | |||
296 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
297 | /* LOCAL VARIABLES */
|
||
298 | /******************************************************************************/
|
||
299 | |||
300 | /******************************************************************************/
|
||
301 | /* LOCAL FUNCTIONS */
|
||
302 | /******************************************************************************/
|
||
303 | |||
304 | ba516b61 | Thomas Schöpping | /**
|
305 | * @brief Implementation of the BaseAsynchronous write() method (inherited from BaseSequentialStream).
|
||
306 | */
|
||
307 | static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n) |
||
308 | { |
||
309 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
310 | dd8738ea | Thomas Schöpping | return streamWrite(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
311 | ba516b61 | Thomas Schöpping | } else {
|
312 | return 0; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | /**
|
||
317 | * @brief Implementation of the BaseAsynchronous read() method (inherited from BaseSequentialStream).
|
||
318 | */
|
||
319 | static size_t _channelread(void *instance, uint8_t *bp, size_t n) |
||
320 | { |
||
321 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
322 | return streamRead(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
||
323 | } else {
|
||
324 | return 0; |
||
325 | } |
||
326 | ba516b61 | Thomas Schöpping | } |
327 | |||
328 | /**
|
||
329 | * @brief Implementation of the BaseAsynchronous put() method (inherited from BaseSequentialStream).
|
||
330 | */
|
||
331 | static msg_t _channelput(void *instance, uint8_t b) |
||
332 | { |
||
333 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
334 | dd8738ea | Thomas Schöpping | return streamPut(((AosShellChannel*)instance)->asyncchannel, b);
|
335 | ba516b61 | Thomas Schöpping | } else {
|
336 | return MSG_RESET;
|
||
337 | } |
||
338 | } |
||
339 | |||
340 | /**
|
||
341 | * @brief Implementation of the BaseAsynchronous get() method (inherited from BaseSequentialStream).
|
||
342 | */
|
||
343 | static msg_t _channelget(void *instance) |
||
344 | { |
||
345 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
346 | return streamGet(((AosShellChannel*)instance)->asyncchannel);
|
||
347 | } else {
|
||
348 | return MSG_RESET;
|
||
349 | } |
||
350 | ba516b61 | Thomas Schöpping | } |
351 | |||
352 | /**
|
||
353 | * @brief Implementation of the BaseAsynchronous putt() method.
|
||
354 | */
|
||
355 | 2c99037f | Marc Rothmann | static msg_t _channelputt(void *instance, uint8_t b, sysinterval_t time) |
356 | ba516b61 | Thomas Schöpping | { |
357 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
358 | dd8738ea | Thomas Schöpping | return chnPutTimeout(((AosShellChannel*)instance)->asyncchannel, b, time);
|
359 | ba516b61 | Thomas Schöpping | } else {
|
360 | return MSG_RESET;
|
||
361 | } |
||
362 | } |
||
363 | |||
364 | /**
|
||
365 | * @brief Implementation of the BaseAsynchronous gett() method.
|
||
366 | */
|
||
367 | 2c99037f | Marc Rothmann | static msg_t _channelgett(void *instance, sysinterval_t time) |
368 | ba516b61 | Thomas Schöpping | { |
369 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
370 | return chnGetTimeout(((AosShellChannel*)instance)->asyncchannel, time);
|
||
371 | } else {
|
||
372 | return MSG_RESET;
|
||
373 | } |
||
374 | ba516b61 | Thomas Schöpping | } |
375 | |||
376 | /**
|
||
377 | * @brief Implementation of the BaseAsynchronous writet() method.
|
||
378 | */
|
||
379 | 2c99037f | Marc Rothmann | static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, sysinterval_t time) |
380 | ba516b61 | Thomas Schöpping | { |
381 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
382 | dd8738ea | Thomas Schöpping | return chnWriteTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
383 | ba516b61 | Thomas Schöpping | } else {
|
384 | return 0; |
||
385 | } |
||
386 | } |
||
387 | |||
388 | /**
|
||
389 | * @brief Implementation of the BaseAsynchronous readt() method.
|
||
390 | */
|
||
391 | 2c99037f | Marc Rothmann | static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, sysinterval_t time) |
392 | ba516b61 | Thomas Schöpping | { |
393 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
394 | return chnReadTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
||
395 | } else {
|
||
396 | return 0; |
||
397 | } |
||
398 | ba516b61 | Thomas Schöpping | } |
399 | |||
400 | 2c99037f | Marc Rothmann | /**
|
401 | * @brief Implementation of the BaseAsynchronousChannel ctl() method.
|
||
402 | */
|
||
403 | f3ac1c96 | Thomas Schöpping | static msg_t _channelctl(void *instance, unsigned int operation, void *arg) |
404 | { |
||
405 | 2c99037f | Marc Rothmann | (void) instance;
|
406 | |||
407 | switch (operation) {
|
||
408 | case CHN_CTL_NOP:
|
||
409 | osalDbgCheck(arg == NULL);
|
||
410 | break;
|
||
411 | case CHN_CTL_INVALID:
|
||
412 | osalDbgAssert(false, "invalid CTL operation"); |
||
413 | break;
|
||
414 | default:
|
||
415 | break;
|
||
416 | } |
||
417 | return MSG_OK;
|
||
418 | } |
||
419 | |||
420 | 697dba3c | Thomas Schöpping | /**
|
421 | * @brief Implementation of the BaseSequentialStream write() method.
|
||
422 | */
|
||
423 | ba516b61 | Thomas Schöpping | static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n) |
424 | { |
||
425 | aosDbgCheck(instance != NULL);
|
||
426 | |||
427 | // local variables
|
||
428 | AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
||
429 | size_t bytes; |
||
430 | size_t maxbytes = 0;
|
||
431 | |||
432 | // iterate through the list of channels
|
||
433 | while (channel != NULL) { |
||
434 | bytes = streamWrite(channel, bp, n); |
||
435 | maxbytes = (bytes > maxbytes) ? bytes : maxbytes; |
||
436 | channel = channel->next; |
||
437 | } |
||
438 | |||
439 | return maxbytes;
|
||
440 | } |
||
441 | |||
442 | 697dba3c | Thomas Schöpping | /**
|
443 | * @brief Implementation of the BaseSequentialStream read() method.
|
||
444 | */
|
||
445 | ba516b61 | Thomas Schöpping | static size_t _stremread(void *instance, uint8_t *bp, size_t n) |
446 | { |
||
447 | (void)instance;
|
||
448 | (void)bp;
|
||
449 | (void)n;
|
||
450 | |||
451 | return 0; |
||
452 | } |
||
453 | |||
454 | 697dba3c | Thomas Schöpping | /**
|
455 | * @brief Implementation of the BaseSequentialStream put() method.
|
||
456 | */
|
||
457 | ba516b61 | Thomas Schöpping | static msg_t _streamput(void *instance, uint8_t b) |
458 | { |
||
459 | aosDbgCheck(instance != NULL);
|
||
460 | |||
461 | // local variables
|
||
462 | AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
||
463 | dd8738ea | Thomas Schöpping | msg_t ret = MSG_OK; |
464 | ba516b61 | Thomas Schöpping | |
465 | // iterate through the list of channels
|
||
466 | while (channel != NULL) { |
||
467 | dd8738ea | Thomas Schöpping | msg_t ret_ = streamPut(channel, b); |
468 | ret = (ret_ < ret) ? ret_ : ret; |
||
469 | ba516b61 | Thomas Schöpping | channel = channel->next; |
470 | } |
||
471 | |||
472 | dd8738ea | Thomas Schöpping | return ret;
|
473 | ba516b61 | Thomas Schöpping | } |
474 | |||
475 | 697dba3c | Thomas Schöpping | /**
|
476 | * @brief Implementation of the BaseSequentialStream get() method.
|
||
477 | */
|
||
478 | ba516b61 | Thomas Schöpping | static msg_t _streamget(void *instance) |
479 | { |
||
480 | (void)instance;
|
||
481 | |||
482 | return 0; |
||
483 | } |
||
484 | |||
485 | e545e620 | Thomas Schöpping | /**
|
486 | a93a1019 | Thomas Schöpping | * @brief Retrieve a pointer to the string buffer of a specified entry in the input buffer.
|
487 | 697dba3c | Thomas Schöpping | *
|
488 | * @param[in] shell Pointer to a shell object.
|
||
489 | * @param[in] entry Entry to be retrieved.
|
||
490 | *
|
||
491 | * @return Pointer to the entry in the input buffer.
|
||
492 | */
|
||
493 | static inline char* _getAbsoluteEntry(const aos_shell_t* shell, size_t entry) |
||
494 | { |
||
495 | aosDbgCheck(shell != NULL);
|
||
496 | aosDbgCheck(entry < shell->input.nentries); |
||
497 | |||
498 | return &(shell->input.buffer[entry * shell->input.linewidth * sizeof(char)]); |
||
499 | } |
||
500 | |||
501 | /**
|
||
502 | * @brief Calculate absolute entry from history offset.
|
||
503 | *
|
||
504 | * @param[in] shell Pointer to a shell object.
|
||
505 | * @param[in] rdata Pointer to a runtime data object.
|
||
506 | a93a1019 | Thomas Schöpping | * @param[in] offset Relative offset of the entry to be retrieved.
|
507 | 697dba3c | Thomas Schöpping | *
|
508 | * @return Absolute index of the historic entry.
|
||
509 | */
|
||
510 | static inline size_t _historyOffset2EntryIndex(const aos_shell_t* shell, const runtimedata_t* rdata, size_t offset) |
||
511 | { |
||
512 | aosDbgCheck(shell != NULL);
|
||
513 | aosDbgCheck(rdata != NULL);
|
||
514 | aosDbgCheck(offset < shell->input.nentries); |
||
515 | |||
516 | return ((shell->input.nentries + rdata->buffer.current - offset) % shell->input.nentries);
|
||
517 | } |
||
518 | |||
519 | /**
|
||
520 | a93a1019 | Thomas Schöpping | * @brief Retrieve a pointer to the string buffer of a historic entry in the input buffer.
|
521 | 697dba3c | Thomas Schöpping | *
|
522 | * @param[in] shell Pointer to a shell object.
|
||
523 | * @param[in] rdata Pointer to a runtime data object.
|
||
524 | a93a1019 | Thomas Schöpping | * @param[in] offset Relative offset of the entry to be retrieved.
|
525 | 697dba3c | Thomas Schöpping | *
|
526 | * @return Pointer to the entry in the input buffer.
|
||
527 | */
|
||
528 | static inline char* _getRelativeEntry(const aos_shell_t* shell, const runtimedata_t* rdata, size_t offset) |
||
529 | { |
||
530 | aosDbgCheck(shell != NULL);
|
||
531 | aosDbgCheck(rdata != NULL);
|
||
532 | aosDbgCheck(offset < shell->input.nentries); |
||
533 | |||
534 | return _getAbsoluteEntry(shell, _historyOffset2EntryIndex(shell, rdata, offset));
|
||
535 | } |
||
536 | |||
537 | /**
|
||
538 | a93a1019 | Thomas Schöpping | * @brief Retrieve a pointer to the current entry string in the input buffer.
|
539 | 697dba3c | Thomas Schöpping | *
|
540 | * @param[in] shell Pointer to a shell object.
|
||
541 | * @param[in] rdata Pointer to a runtime data object.
|
||
542 | *
|
||
543 | * @return Pointer to the string of the current entry in the input buffer.
|
||
544 | */
|
||
545 | static inline char* _getCurrentEntry(const aos_shell_t* shell, const runtimedata_t* rdata) |
||
546 | { |
||
547 | aosDbgCheck(shell != NULL);
|
||
548 | aosDbgCheck(rdata != NULL);
|
||
549 | |||
550 | return _getAbsoluteEntry(shell, rdata->buffer.current);
|
||
551 | } |
||
552 | |||
553 | /**
|
||
554 | a93a1019 | Thomas Schöpping | * @brief Retrieve a pointer to the currently selected entry.
|
555 | 697dba3c | Thomas Schöpping | *
|
556 | * @param[in] shell Pointer to a shell object.
|
||
557 | * @param[in] rdata Pointer to a runtime data object.
|
||
558 | *
|
||
559 | * @return Pointer to the currently selected entry or NULL if no entry is selected (cleared preview).
|
||
560 | */
|
||
561 | static inline char* _getSelectedEntry(const aos_shell_t* shell, const runtimedata_t* rdata) |
||
562 | { |
||
563 | aosDbgCheck(shell != NULL);
|
||
564 | aosDbgCheck(rdata != NULL);
|
||
565 | |||
566 | if (rdata->buffer.selected > 0) { |
||
567 | return _getRelativeEntry(shell, rdata, rdata->buffer.selected - 1); |
||
568 | } else {
|
||
569 | return NULL; |
||
570 | } |
||
571 | } |
||
572 | |||
573 | /**
|
||
574 | a93a1019 | Thomas Schöpping | * @brief Retrieve the currently visualized entry.
|
575 | 697dba3c | Thomas Schöpping | *
|
576 | * @param[in] shell Pointer to a shell object.
|
||
577 | * @param[in] rdata Pointer to a runtime data object.
|
||
578 | *
|
||
579 | * @return Pointer to the currently visualized entry or NULL if the input has been cleared (cleared preview).
|
||
580 | */
|
||
581 | static inline char* _getVisualisedEntry(const aos_shell_t* shell, const runtimedata_t* rdata) |
||
582 | { |
||
583 | aosDbgCheck(shell != NULL);
|
||
584 | aosDbgCheck(rdata != NULL);
|
||
585 | |||
586 | if (rdata->buffer.selected == 0) { |
||
587 | // cleared preview, nothing visualized
|
||
588 | return NULL; |
||
589 | } else {
|
||
590 | if (rdata->buffer.selected == 1 || rdata->buffer.selected == rdata->buffer.edited) { |
||
591 | // the current or a modified entry is selected
|
||
592 | return _getCurrentEntry(shell, rdata);
|
||
593 | } else {
|
||
594 | // a historic, unmodified entry is selected
|
||
595 | return _getRelativeEntry(shell, rdata, rdata->buffer.selected - 1); |
||
596 | } |
||
597 | } |
||
598 | } |
||
599 | |||
600 | /**
|
||
601 | e545e620 | Thomas Schöpping | * @brief Print the shell prompt
|
602 | * @details Depending on the configuration flags, the system uptime is printed before the prompt string.
|
||
603 | *
|
||
604 | * @param[in] shell Pointer to the shell object.
|
||
605 | */
|
||
606 | static void _printPrompt(aos_shell_t* shell) |
||
607 | { |
||
608 | aosDbgCheck(shell != NULL);
|
||
609 | |||
610 | 8399aeae | Thomas Schöpping | // print some time informattion before prompt if configured
|
611 | if (shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) {
|
||
612 | // printf the system uptime
|
||
613 | if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_UPTIME) {
|
||
614 | // get current system uptime
|
||
615 | aos_timestamp_t uptime; |
||
616 | aosSysGetUptime(&uptime); |
||
617 | |||
618 | chprintf((BaseSequentialStream*)&shell->stream, "[%01u:%02u:%02u:%02u:%03u:%03u] ",
|
||
619 | (uint32_t)(uptime / MICROSECONDS_PER_DAY), |
||
620 | (uint8_t)(uptime % MICROSECONDS_PER_DAY / MICROSECONDS_PER_HOUR), |
||
621 | (uint8_t)(uptime % MICROSECONDS_PER_HOUR / MICROSECONDS_PER_MINUTE), |
||
622 | (uint8_t)(uptime % MICROSECONDS_PER_MINUTE / MICROSECONDS_PER_SECOND), |
||
623 | (uint16_t)(uptime % MICROSECONDS_PER_SECOND / MICROSECONDS_PER_MILLISECOND), |
||
624 | (uint16_t)(uptime % MICROSECONDS_PER_MILLISECOND / MICROSECONDS_PER_MICROSECOND)); |
||
625 | } |
||
626 | 23437e98 | Thomas Schöpping | #if (HAL_USE_RTC == TRUE)
|
627 | 8399aeae | Thomas Schöpping | else if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_DATETIME) { |
628 | // get current RTC time
|
||
629 | struct tm dt;
|
||
630 | aosSysGetDateTime(&dt); |
||
631 | chprintf((BaseSequentialStream*)&shell->stream, "[%02u-%02u-%04u|%02u:%02u:%02u] ",
|
||
632 | dt.tm_mday, |
||
633 | dt.tm_mon + 1,
|
||
634 | dt.tm_year + 1900,
|
||
635 | dt.tm_hour, |
||
636 | dt.tm_min, |
||
637 | dt.tm_sec); |
||
638 | } |
||
639 | 7de0cc90 | Thomas Schöpping | #endif /* (HAL_USE_RTC == TRUE) */ |
640 | 8399aeae | Thomas Schöpping | else {
|
641 | aosDbgAssert(false);
|
||
642 | } |
||
643 | e545e620 | Thomas Schöpping | } |
644 | |||
645 | // print the actual prompt string
|
||
646 | if (shell->prompt && !(shell->config & AOS_SHELL_CONFIG_PROMPT_MINIMAL)) {
|
||
647 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "%s$ ", shell->prompt);
|
648 | e545e620 | Thomas Schöpping | } else {
|
649 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "%>$ ");
|
650 | e545e620 | Thomas Schöpping | } |
651 | |||
652 | return;
|
||
653 | } |
||
654 | |||
655 | /**
|
||
656 | * @brief Interprete a escape sequence
|
||
657 | d96ce104 | Thomas Schöpping | * @details This function interpretes escape sequences (starting with ASCII
|
658 | * "Escape" character 0x1B) according to the VT100 / VT52 ANSI escape
|
||
659 | * sequence definitions.
|
||
660 | * @note Only the most important escape sequences are implemented yet.
|
||
661 | e545e620 | Thomas Schöpping | *
|
662 | * @param[in] seq Character sequence to interprete.
|
||
663 | * Must be terminated by NUL byte.
|
||
664 | *
|
||
665 | * @return A @p special_key value.
|
||
666 | */
|
||
667 | static special_key_t _interpreteEscapeSequence(const char seq[]) |
||
668 | { |
||
669 | // local variables
|
||
670 | cc33217b | Thomas Schöpping | unsigned long strl = 0; |
671 | const unsigned long seql = strlen(seq); |
||
672 | e545e620 | Thomas Schöpping | bool ambiguous = false; |
673 | |||
674 | // TAB
|
||
675 | 697dba3c | Thomas Schöpping | /* not supported yet; use '\x09' instead */
|
676 | e545e620 | Thomas Schöpping | |
677 | // BACKSPACE
|
||
678 | 697dba3c | Thomas Schöpping | /* not supported yet; use '\x08' instead */
|
679 | e545e620 | Thomas Schöpping | |
680 | // ESCAPE
|
||
681 | 697dba3c | Thomas Schöpping | /* not supported yes; use '\x1B' instead */
|
682 | |||
683 | // CTRL + C
|
||
684 | /* not defined yet; use '\x03' instead */
|
||
685 | e545e620 | Thomas Schöpping | |
686 | // INSERT
|
||
687 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_INSERT, seql) == 0) { |
688 | strl = strlen(KEYSTRING_INSERT); |
||
689 | if (seql == strl) {
|
||
690 | return KEY_INSERT;
|
||
691 | } else if (seql < strl) { |
||
692 | ambiguous = true;
|
||
693 | } |
||
694 | e545e620 | Thomas Schöpping | } |
695 | |||
696 | // DELETE
|
||
697 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_DELETE, seql) == 0) { |
698 | strl = strlen(KEYSTRING_DELETE); |
||
699 | if (seql == strl) {
|
||
700 | return KEY_DELETE;
|
||
701 | } else if (seql < strl) { |
||
702 | ambiguous = true;
|
||
703 | } |
||
704 | e545e620 | Thomas Schöpping | } |
705 | |||
706 | // HOME
|
||
707 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_HOME, seql) == 0) { |
708 | strl = strlen(KEYSTRING_HOME); |
||
709 | if (seql == strl) {
|
||
710 | return KEY_HOME;
|
||
711 | } else if (seql < strl) { |
||
712 | ambiguous = true;
|
||
713 | } |
||
714 | e545e620 | Thomas Schöpping | } |
715 | |||
716 | // END
|
||
717 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_END, seql) == 0) { |
718 | strl = strlen(KEYSTRING_END); |
||
719 | if (seql == strl) {
|
||
720 | return KEY_END;
|
||
721 | } else if (seql < strl) { |
||
722 | ambiguous = true;
|
||
723 | } |
||
724 | e545e620 | Thomas Schöpping | } |
725 | |||
726 | // PAGE UP
|
||
727 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_PAGEUP, seql) == 0) { |
728 | strl = strlen(KEYSTRING_PAGEUP); |
||
729 | if (seql == strl) {
|
||
730 | return KEY_PAGEUP;
|
||
731 | } else if (seql < strl) { |
||
732 | ambiguous = true;
|
||
733 | } |
||
734 | e545e620 | Thomas Schöpping | } |
735 | |||
736 | // PAGE DOWN
|
||
737 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_PAGEDOWN, seql) == 0) { |
738 | strl = strlen(KEYSTRING_PAGEDOWN); |
||
739 | if (seql == strl) {
|
||
740 | return KEY_PAGEDOWN;
|
||
741 | } else if (seql < strl) { |
||
742 | ambiguous = true;
|
||
743 | } |
||
744 | e545e620 | Thomas Schöpping | } |
745 | |||
746 | // ARROW UP
|
||
747 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_ARROWUP, seql) == 0) { |
748 | strl = strlen(KEYSTRING_ARROWUP); |
||
749 | if (seql == strl) {
|
||
750 | return KEY_ARROWUP;
|
||
751 | } else if (seql < strl) { |
||
752 | ambiguous = true;
|
||
753 | } |
||
754 | e545e620 | Thomas Schöpping | } |
755 | |||
756 | // ARROW DOWN
|
||
757 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_ARROWDOWN, seql) == 0) { |
758 | strl = strlen(KEYSTRING_ARROWDOWN); |
||
759 | if (seql == strl) {
|
||
760 | return KEY_ARROWDOWN;
|
||
761 | } else if (seql < strl) { |
||
762 | ambiguous = true;
|
||
763 | } |
||
764 | e545e620 | Thomas Schöpping | } |
765 | |||
766 | // ARROW LEFT
|
||
767 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_ARROWLEFT, seql) == 0) { |
768 | strl = strlen(KEYSTRING_ARROWLEFT); |
||
769 | if (seql == strl) {
|
||
770 | return KEY_ARROWLEFT;
|
||
771 | } else if (seql < strl) { |
||
772 | ambiguous = true;
|
||
773 | } |
||
774 | e545e620 | Thomas Schöpping | } |
775 | |||
776 | // ARROW RIGHT
|
||
777 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_ARROWRIGHT, seql) == 0) { |
778 | strl = strlen(KEYSTRING_ARROWRIGHT); |
||
779 | if (seql == strl) {
|
||
780 | return KEY_ARROWRIGHT;
|
||
781 | } else if (seql < strl) { |
||
782 | ambiguous = true;
|
||
783 | } |
||
784 | cc33217b | Thomas Schöpping | } |
785 | |||
786 | // CTRL + ARROW UP
|
||
787 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_CTRL_ARROWUP, seql) == 0) { |
788 | strl = strlen(KEYSTRING_CTRL_ARROWUP); |
||
789 | if (seql == strl) {
|
||
790 | return KEY_CTRL_ARROWUP;
|
||
791 | } else if (seql < strl) { |
||
792 | ambiguous = true;
|
||
793 | } |
||
794 | cc33217b | Thomas Schöpping | } |
795 | |||
796 | // CTRL + ARROW DOWN
|
||
797 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_CTRL_ARROWDOWN, seql) == 0) { |
798 | strl = strlen(KEYSTRING_CTRL_ARROWDOWN); |
||
799 | if (seql == strl) {
|
||
800 | return KEY_CTRL_ARROWDOWN;
|
||
801 | } else if (seql < strl) { |
||
802 | ambiguous = true;
|
||
803 | } |
||
804 | cc33217b | Thomas Schöpping | } |
805 | |||
806 | // CTRL + ARROW LEFT
|
||
807 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_CTRL_ARROWLEFT, seql) == 0) { |
808 | strl = strlen(KEYSTRING_CTRL_ARROWLEFT); |
||
809 | if (seql == strl) {
|
||
810 | return KEY_CTRL_ARROWLEFT;
|
||
811 | } else if (seql < strl) { |
||
812 | ambiguous = true;
|
||
813 | } |
||
814 | cc33217b | Thomas Schöpping | } |
815 | |||
816 | // CTRL + ARROW RIGHT
|
||
817 | 697dba3c | Thomas Schöpping | if (strncmp(seq, KEYSTRING_CTRL_ARROWRIGHT, seql) == 0) { |
818 | strl = strlen(KEYSTRING_CTRL_ARROWRIGHT); |
||
819 | if (seql == strl) {
|
||
820 | return KEY_CTRL_ARROWRIGHT;
|
||
821 | } else if (seql < strl) { |
||
822 | ambiguous = true;
|
||
823 | } |
||
824 | e545e620 | Thomas Schöpping | } |
825 | |||
826 | return ambiguous ? KEY_AMBIGUOUS : KEY_UNKNOWN;
|
||
827 | } |
||
828 | |||
829 | /**
|
||
830 | 697dba3c | Thomas Schöpping | * @brief Move the cursor in the terminal.
|
831 | e545e620 | Thomas Schöpping | *
|
832 | * @param[in] shell Pointer to the shell object.
|
||
833 | 697dba3c | Thomas Schöpping | * @param[in] line Pointer to the current content of the line.
|
834 | e545e620 | Thomas Schöpping | * @param[in] from Starting position of the cursor.
|
835 | * @param[in] to Target position to move the cursor to.
|
||
836 | *
|
||
837 | * @return The number of positions moved.
|
||
838 | */
|
||
839 | 697dba3c | Thomas Schöpping | static int _moveCursor(aos_shell_t* shell, const char* line, size_t from, size_t to) |
840 | e545e620 | Thomas Schöpping | { |
841 | aosDbgCheck(shell != NULL);
|
||
842 | 697dba3c | Thomas Schöpping | aosDbgCheck(line != NULL || from >= to);
|
843 | aosDbgCheck(from <= shell->input.linewidth); |
||
844 | aosDbgCheck(to <= shell->input.linewidth); |
||
845 | e545e620 | Thomas Schöpping | |
846 | // local variables
|
||
847 | size_t pos = from; |
||
848 | |||
849 | // move cursor left by printing backspaces
|
||
850 | while (pos > to) {
|
||
851 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, '\b');
|
852 | e545e620 | Thomas Schöpping | --pos; |
853 | } |
||
854 | |||
855 | // move cursor right by printing line content
|
||
856 | while (pos < to) {
|
||
857 | 697dba3c | Thomas Schöpping | streamPut(&shell->stream, (uint8_t)line[pos]); |
858 | e545e620 | Thomas Schöpping | ++pos; |
859 | } |
||
860 | |||
861 | return (int)pos - (int)from; |
||
862 | } |
||
863 | |||
864 | /**
|
||
865 | 697dba3c | Thomas Schöpping | * @brief Print content of a given string to the shell output stream.
|
866 | e545e620 | Thomas Schöpping | *
|
867 | * @param[in] shell Pointer to the shell object.
|
||
868 | 697dba3c | Thomas Schöpping | * @param[in] line Pointer to the line to be printed.
|
869 | e545e620 | Thomas Schöpping | * @param[in] from First position to start printing from.
|
870 | * @param[in] to Position after the last character to print.
|
||
871 | *
|
||
872 | * @return Number of characters printed.
|
||
873 | */
|
||
874 | 697dba3c | Thomas Schöpping | static size_t _printString(aos_shell_t* shell, const char* line, size_t from, size_t to) |
875 | e545e620 | Thomas Schöpping | { |
876 | aosDbgCheck(shell != NULL);
|
||
877 | 697dba3c | Thomas Schöpping | aosDbgCheck(line != NULL || from >= to);
|
878 | aosDbgCheck(from < shell->input.linewidth); |
||
879 | aosDbgCheck(to <= shell->input.linewidth); |
||
880 | e545e620 | Thomas Schöpping | |
881 | // local variables
|
||
882 | size_t cnt; |
||
883 | |||
884 | for (cnt = 0; from + cnt < to; ++cnt) { |
||
885 | 697dba3c | Thomas Schöpping | streamPut(&shell->stream, (uint8_t)line[from + cnt]); |
886 | e545e620 | Thomas Schöpping | } |
887 | |||
888 | return cnt;
|
||
889 | } |
||
890 | |||
891 | 697dba3c | Thomas Schöpping | /**
|
892 | * @brief Print a single character to the input buffer and to the output stream.
|
||
893 | *
|
||
894 | * @param[in] shell Pointer to the shell object.
|
||
895 | * @param[in] rdata Pointer to the runtim data object.
|
||
896 | * @param[in] c Character to print.
|
||
897 | *
|
||
898 | * @return Number of successfully handled characters.
|
||
899 | * The return value can be interpreted as boolean (1 = sucess; 0 = error).
|
||
900 | */
|
||
901 | static int _printChar(aos_shell_t* shell, runtimedata_t* rdata, char c) |
||
902 | { |
||
903 | cc33217b | Thomas Schöpping | aosDbgCheck(shell != NULL);
|
904 | 697dba3c | Thomas Schöpping | aosDbgCheck(rdata != NULL);
|
905 | cc33217b | Thomas Schöpping | |
906 | // check whether input line is already full
|
||
907 | 697dba3c | Thomas Schöpping | if (rdata->input.length + 1 >= shell->input.linewidth) { |
908 | cc33217b | Thomas Schöpping | return 0; |
909 | 697dba3c | Thomas Schöpping | } |
910 | |||
911 | a93a1019 | Thomas Schöpping | // retrieve entry in the input buffer
|
912 | 697dba3c | Thomas Schöpping | char* line = _getCurrentEntry(shell, rdata);
|
913 | |||
914 | // overwrite content
|
||
915 | if (shell->config & AOS_SHELL_CONFIG_INPUT_OVERWRITE) {
|
||
916 | line[rdata->input.cursorpos] = c; |
||
917 | ++rdata->input.cursorpos; |
||
918 | rdata->input.length = (rdata->input.cursorpos > rdata->input.length) ? rdata->input.cursorpos : rdata->input.length; |
||
919 | streamPut(&shell->stream, (uint8_t)c); |
||
920 | return 1; |
||
921 | } |
||
922 | // insert character
|
||
923 | else {
|
||
924 | memmove(&line[rdata->input.cursorpos + 1], &line[rdata->input.cursorpos], rdata->input.length - rdata->input.cursorpos);
|
||
925 | line[rdata->input.cursorpos] = c; |
||
926 | ++rdata->input.length; |
||
927 | _printString(shell, line, rdata->input.cursorpos, rdata->input.length); |
||
928 | ++rdata->input.cursorpos; |
||
929 | _moveCursor(shell, line, rdata->input.length, rdata->input.cursorpos); |
||
930 | cc33217b | Thomas Schöpping | return 1; |
931 | } |
||
932 | } |
||
933 | |||
934 | e545e620 | Thomas Schöpping | /**
|
935 | 697dba3c | Thomas Schöpping | * @brief Overwrite the current output with a given line.
|
936 | * @details If the current output is longer than the string, the additional characters are cleared.
|
||
937 | *
|
||
938 | * @param[in] shell Pointer to a shell object.
|
||
939 | * @param[in] rdata Pointer to a runtime data object.
|
||
940 | * @param[in] line The line to be printed.
|
||
941 | */
|
||
942 | static void _overwriteOutput(aos_shell_t* shell, runtimedata_t* rdata, const char* line) |
||
943 | { |
||
944 | aosDbgCheck(shell != NULL);
|
||
945 | aosDbgCheck(rdata != NULL);
|
||
946 | aosDbgCheck(line != NULL);
|
||
947 | |||
948 | // local variables
|
||
949 | const size_t oldlength = rdata->input.length;
|
||
950 | |||
951 | // print line (overwrite current output)
|
||
952 | _moveCursor(shell, line, rdata->input.cursorpos, 0);
|
||
953 | rdata->input.length = strlen(line); |
||
954 | _printString(shell, line, 0, rdata->input.length);
|
||
955 | |||
956 | // clear any remaining symbols
|
||
957 | if (oldlength > rdata->input.length) {
|
||
958 | for (rdata->input.cursorpos = rdata->input.length; rdata->input.cursorpos < oldlength; ++rdata->input.cursorpos) {
|
||
959 | streamPut(&shell->stream, ' ');
|
||
960 | } |
||
961 | _moveCursor(shell, line, oldlength, rdata->input.length); |
||
962 | } |
||
963 | |||
964 | rdata->input.cursorpos = rdata->input.length; |
||
965 | |||
966 | return;
|
||
967 | } |
||
968 | |||
969 | /**
|
||
970 | e545e620 | Thomas Schöpping | * @brief Compare two characters.
|
971 | *
|
||
972 | * @param[in] lhs First character to compare.
|
||
973 | * @param[in] rhs Second character to compare.
|
||
974 | *
|
||
975 | * @return How well the characters match.
|
||
976 | */
|
||
977 | static inline charmatch_t _charcmp(char lhs, char rhs) |
||
978 | { |
||
979 | // if lhs is a upper case letter and rhs is a lower case letter
|
||
980 | if (lhs >= 'A' && lhs <= 'Z' && rhs >= 'a' && rhs <= 'z') { |
||
981 | return (lhs == (rhs - 'a' + 'A')) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
||
982 | } |
||
983 | // if lhs is a lower case letter and rhs is a upper case letter
|
||
984 | else if (lhs >= 'a' && lhs <= 'z' && rhs >= 'A' && rhs <= 'Z') { |
||
985 | return ((lhs - 'a' + 'A') == rhs) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
||
986 | } |
||
987 | // default
|
||
988 | else {
|
||
989 | return (lhs == rhs) ? CHAR_MATCH_CASE : CHAR_MATCH_NOT;
|
||
990 | } |
||
991 | } |
||
992 | |||
993 | /**
|
||
994 | * @brief Maps an character from ASCII to a modified custom encoding.
|
||
995 | * @details The custom character encoding is very similar to ASCII and has the following structure:
|
||
996 | * 0x00=NULL ... 0x40='@' (identically to ASCII)
|
||
997 | * 0x4A='a'; 0x4B='A'; 0x4C='b'; 0x4D='B' ... 0x73='z'; 0x74='Z' (custom letter order)
|
||
998 | * 0x75='[' ... 0x7A='`' (0x5B..0x60 is ASCII)
|
||
999 | * 0x7B='{' ... 0x7F=DEL (identically to ASCII)
|
||
1000 | *
|
||
1001 | * @param[in] c Character to map to the custom encoding.
|
||
1002 | *
|
||
1003 | * @return The customly encoded character.
|
||
1004 | */
|
||
1005 | 697dba3c | Thomas Schöpping | static inline char _mapAscii2Custom(char c) |
1006 | e545e620 | Thomas Schöpping | { |
1007 | if (c >= 'A' && c <= 'Z') { |
||
1008 | return ((c - 'A') * 2) + 'A' + 1; |
||
1009 | } else if (c > 'Z' && c < 'a') { |
||
1010 | return c + ('z' - 'a') + 1; |
||
1011 | } else if (c >= 'a' && c <= 'z') { |
||
1012 | return ((c - 'a') * 2) + 'A'; |
||
1013 | } else {
|
||
1014 | return c;
|
||
1015 | } |
||
1016 | } |
||
1017 | |||
1018 | /**
|
||
1019 | * @brief Compares two strings wrt letter case.
|
||
1020 | * @details Comparisson uses a custom character encoding or mapping.
|
||
1021 | * See @p _mapAscii2Custom for details.
|
||
1022 | *
|
||
1023 | * @param[in] str1 First string to compare.
|
||
1024 | * @param[in] str2 Second string to compare.
|
||
1025 | * @param[in] cs Flag indicating whether comparison shall be case sensitive.
|
||
1026 | * @param[in,out] n Maximum number of character to compare (in) and number of matching characters (out).
|
||
1027 | * If a null pointer is specified, this parameter is ignored.
|
||
1028 | * If the value pointed to is zero, comarison will not be limited.
|
||
1029 | * @param[out] m Optional indicator whether there was at least one case mismatch.
|
||
1030 | *
|
||
1031 | * @return Integer value indicating the relationship between the strings.
|
||
1032 | * @retval <0 The first character that does not match has a lower value in str1 than in str2.
|
||
1033 | * @retval 0 The contents of both strings are equal.
|
||
1034 | * @retval >0 The first character that does not match has a greater value in str1 than in str2.
|
||
1035 | */
|
||
1036 | static int _strccmp(const char *str1, const char *str2, bool cs, size_t* n, charmatch_t* m) |
||
1037 | { |
||
1038 | aosDbgCheck(str1 != NULL);
|
||
1039 | aosDbgCheck(str2 != NULL);
|
||
1040 | |||
1041 | // initialize variables
|
||
1042 | if (m) {
|
||
1043 | *m = CHAR_MATCH_NOT; |
||
1044 | } |
||
1045 | size_t i = 0;
|
||
1046 | |||
1047 | // iterate through the strings
|
||
1048 | while ((n == NULL) || (*n == 0) || (*n > 0 && i < *n)) { |
||
1049 | // break on NUL
|
||
1050 | if (str1[i] == '\0' || str2[i] == '\0') { |
||
1051 | if (n) {
|
||
1052 | *n = i; |
||
1053 | } |
||
1054 | break;
|
||
1055 | } |
||
1056 | // compare character
|
||
1057 | const charmatch_t match = _charcmp(str1[i], str2[i]);
|
||
1058 | if ((match == CHAR_MATCH_CASE) || (!cs && match == CHAR_MATCH_NCASE)) {
|
||
1059 | if (m != NULL && *m != CHAR_MATCH_NCASE) { |
||
1060 | *m = match; |
||
1061 | } |
||
1062 | ++i; |
||
1063 | } else {
|
||
1064 | if (n) {
|
||
1065 | *n = i; |
||
1066 | } |
||
1067 | break;
|
||
1068 | } |
||
1069 | } |
||
1070 | |||
1071 | return _mapAscii2Custom(str1[i]) - _mapAscii2Custom(str2[i]);
|
||
1072 | } |
||
1073 | |||
1074 | 27286ba5 | Thomas Schöpping | /**
|
1075 | 697dba3c | Thomas Schöpping | * @brief Alters all intermediate NUL bytes in a string to spaces.
|
1076 | *
|
||
1077 | * @param[in] string The string to be handled.
|
||
1078 | * @param[in] length Length of the string.
|
||
1079 | *
|
||
1080 | * @return Detected Length of the actual content of the string.
|
||
1081 | */
|
||
1082 | static size_t _restoreWhitespace(char* string, size_t length) |
||
1083 | { |
||
1084 | aosDbgCheck(string != NULL || length == 0); |
||
1085 | |||
1086 | // local variables
|
||
1087 | size_t c = length; |
||
1088 | |||
1089 | // seach for first non-NUL byte from the back
|
||
1090 | while (c > 0) { |
||
1091 | --c; |
||
1092 | if (string[c] != '\0') { |
||
1093 | // store the detected length of the content
|
||
1094 | length = ++c; |
||
1095 | break;
|
||
1096 | } |
||
1097 | } |
||
1098 | |||
1099 | // iterate further and replace all encountered NUL bytes by spaces
|
||
1100 | while (c > 0) { |
||
1101 | --c; |
||
1102 | if (string[c] == '\0') { |
||
1103 | string[c] = ' ';
|
||
1104 | } |
||
1105 | } |
||
1106 | |||
1107 | return length;
|
||
1108 | } |
||
1109 | |||
1110 | /**
|
||
1111 | * @brief Performs required actions before an imminent modiifcation (character input, deletion or autofill).
|
||
1112 | * @details This functions checks the current status and clears or copies entries in the input buffer as required.
|
||
1113 | * Status information (runtime data) is altered accordingly as well.
|
||
1114 | *
|
||
1115 | * @param[in] shell Pointer to a shell object.
|
||
1116 | * @param[in] rdata Pointer to a runtime data object.
|
||
1117 | *
|
||
1118 | * @return Pointer to the current entry in the input buffer.
|
||
1119 | */
|
||
1120 | static char* _prepare4Modification(aos_shell_t* shell, runtimedata_t* rdata) |
||
1121 | { |
||
1122 | aosDbgCheck(shell != NULL);
|
||
1123 | aosDbgCheck(rdata != NULL);
|
||
1124 | |||
1125 | char* line = _getCurrentEntry(shell, rdata);
|
||
1126 | |||
1127 | // cleared preview
|
||
1128 | if (rdata->buffer.selected == 0) { |
||
1129 | // reset the current entry if required
|
||
1130 | if (rdata->buffer.edited != 0) { |
||
1131 | memset(line, '\0', shell->input.linewidth * sizeof(char)); |
||
1132 | } |
||
1133 | // set the current entry as the selected one and mark it as edited
|
||
1134 | rdata->buffer.selected = 1;
|
||
1135 | rdata->buffer.edited = 1;
|
||
1136 | } |
||
1137 | // current entry
|
||
1138 | else if (rdata->buffer.selected == 1) { |
||
1139 | // mark current entry as edited
|
||
1140 | rdata->buffer.edited = 1;
|
||
1141 | } |
||
1142 | // preview of historic entry
|
||
1143 | else if (rdata->buffer.selected > 1) { |
||
1144 | // copy the selected entry before modification if required
|
||
1145 | if (rdata->buffer.selected!= rdata->buffer.edited) {
|
||
1146 | memcpy(line, _getSelectedEntry(shell, rdata), shell->input.linewidth * sizeof(char)); |
||
1147 | } |
||
1148 | // mark the selected entry as edited
|
||
1149 | rdata->buffer.edited = rdata->buffer.selected; |
||
1150 | } |
||
1151 | |||
1152 | return line;
|
||
1153 | } |
||
1154 | |||
1155 | /**
|
||
1156 | 27286ba5 | Thomas Schöpping | * @brief Read input from a channel as long as there is data available.
|
1157 | *
|
||
1158 | 697dba3c | Thomas Schöpping | * @param[in] shell Pointer to the shell object.
|
1159 | * @param[in,out] rdata Pointer to a runtime data object.
|
||
1160 | * @param[in] channel The channel to read from.
|
||
1161 | 27286ba5 | Thomas Schöpping | *
|
1162 | 697dba3c | Thomas Schöpping | * @return Number of characters read.
|
1163 | 27286ba5 | Thomas Schöpping | */
|
1164 | 697dba3c | Thomas Schöpping | static size_t _readChannel(aos_shell_t* shell, runtimedata_t* rdata, AosShellChannel* channel)
|
1165 | e545e620 | Thomas Schöpping | { |
1166 | aosDbgCheck(shell != NULL);
|
||
1167 | 697dba3c | Thomas Schöpping | aosDbgCheck(rdata != NULL);
|
1168 | ba516b61 | Thomas Schöpping | aosDbgCheck(channel != NULL);
|
1169 | e545e620 | Thomas Schöpping | |
1170 | // local variables
|
||
1171 | 697dba3c | Thomas Schöpping | size_t bytes = 0;
|
1172 | e545e620 | Thomas Schöpping | char c;
|
1173 | dd8738ea | Thomas Schöpping | special_key_t key; |
1174 | 697dba3c | Thomas Schöpping | action_t action; |
1175 | ba516b61 | Thomas Schöpping | |
1176 | // read character by character from the channel
|
||
1177 | while (chnReadTimeout(channel, (uint8_t*)&c, 1, TIME_IMMEDIATE)) { |
||
1178 | 697dba3c | Thomas Schöpping | // increment byte counter
|
1179 | ++bytes; |
||
1180 | e545e620 | Thomas Schöpping | |
1181 | 697dba3c | Thomas Schöpping | // drop any further input after an execution request was detected
|
1182 | if (rdata->lastaction == ACTION_EXECUTE && bytes > 1) { |
||
1183 | c18a848e | Thomas Schöpping | continue;
|
1184 | } |
||
1185 | |||
1186 | 697dba3c | Thomas Schöpping | // try to interprete escape sequence
|
1187 | { |
||
1188 | // set default
|
||
1189 | key = KEY_UNKNOWN; |
||
1190 | // only interprete, if there is an escape sequence at all
|
||
1191 | const size_t escl = strlen(rdata->input.escseq);
|
||
1192 | if (escl > 0) { |
||
1193 | // append and 'consume' character
|
||
1194 | rdata->input.escseq[escl] = c; |
||
1195 | c = '\0';
|
||
1196 | // try to interprete sequence
|
||
1197 | key = _interpreteEscapeSequence(rdata->input.escseq); |
||
1198 | switch (key) {
|
||
1199 | // ambiguous key due to incomplete sequence
|
||
1200 | case KEY_AMBIGUOUS:
|
||
1201 | // read next byte to resolve ambiguity
|
||
1202 | continue;
|
||
1203 | // an unknown sequence has been encountered
|
||
1204 | case KEY_UNKNOWN:
|
||
1205 | // increment number of inputs but handle this unknown sequence below
|
||
1206 | break;
|
||
1207 | // a key was identified successfully
|
||
1208 | default:
|
||
1209 | // reset the sequence buffer
|
||
1210 | memset(rdata->input.escseq, '\0', AOS_SHELL_ESCSEQUENCE_LENGTH * sizeof(char)); |
||
1211 | break;
|
||
1212 | } |
||
1213 | e545e620 | Thomas Schöpping | } |
1214 | } |
||
1215 | |||
1216 | 697dba3c | Thomas Schöpping | /*
|
1217 | * Derive action to be executed from keypress.
|
||
1218 | * This step handles all sanity checks, so any required prerequisites for the selected action are fulfilled.
|
||
1219 | */
|
||
1220 | // set default
|
||
1221 | action = ACTION_NONE; |
||
1222 | // if there is no escape sequence pending
|
||
1223 | if (rdata->input.escseq[0] == '\0') { |
||
1224 | ba516b61 | Thomas Schöpping | |
1225 | // printable character
|
||
1226 | 697dba3c | Thomas Schöpping | if (c >= '\x20' && c <= '\x7E') { |
1227 | action = ACTION_READCHAR; |
||
1228 | ba516b61 | Thomas Schöpping | } |
1229 | |||
1230 | // tab key or character
|
||
1231 | 697dba3c | Thomas Schöpping | else if (c == '\x09' || key == KEY_TAB) { |
1232 | ba516b61 | Thomas Schöpping | /*
|
1233 | * pressing tab once applies auto fill
|
||
1234 | 697dba3c | Thomas Schöpping | * pressing tab a second time (or more) prints suggestions
|
1235 | ba516b61 | Thomas Schöpping | */
|
1236 | 697dba3c | Thomas Schöpping | if (rdata->lastaction == ACTION_AUTOCOMPLETE || rdata->lastaction == ACTION_SUGGEST) {
|
1237 | action = ACTION_SUGGEST; |
||
1238 | e545e620 | Thomas Schöpping | } else {
|
1239 | 697dba3c | Thomas Schöpping | action = ACTION_AUTOCOMPLETE; |
1240 | e545e620 | Thomas Schöpping | } |
1241 | ba516b61 | Thomas Schöpping | } |
1242 | |||
1243 | 697dba3c | Thomas Schöpping | // carriage return ('\r') or line feed ('\n') character
|
1244 | else if (c == '\x0D' || c == '\x0A') { |
||
1245 | action = ACTION_EXECUTE; |
||
1246 | ba516b61 | Thomas Schöpping | } |
1247 | |||
1248 | // backspace key or character
|
||
1249 | 697dba3c | Thomas Schöpping | else if (c == '\x08' || key == KEY_BACKSPACE) { |
1250 | ba516b61 | Thomas Schöpping | // ignore if cursor is at very left
|
1251 | 697dba3c | Thomas Schöpping | if (rdata->input.cursorpos > 0) { |
1252 | action = ACTION_DELETEBACKWARD; |
||
1253 | e545e620 | Thomas Schöpping | } |
1254 | ba516b61 | Thomas Schöpping | } |
1255 | |||
1256 | 697dba3c | Thomas Schöpping | // DEL key or character
|
1257 | else if (c == '\x7F' || key == KEY_DELETE) { |
||
1258 | // ignore if cursor is at very right
|
||
1259 | if (rdata->input.cursorpos < rdata->input.length) {
|
||
1260 | action = ACTION_DELETEFORWARD; |
||
1261 | e545e620 | Thomas Schöpping | } |
1262 | ba516b61 | Thomas Schöpping | } |
1263 | |||
1264 | 697dba3c | Thomas Schöpping | // 'arrow up' key
|
1265 | else if (key == KEY_ARROWUP) { |
||
1266 | // recall previous input from history only if
|
||
1267 | // not the oldest entry is already selected and
|
||
1268 | // the previous entry has been set.
|
||
1269 | if (rdata->buffer.selected < shell->input.nentries &&
|
||
1270 | (_getRelativeEntry(shell, rdata, rdata->buffer.selected))[0] != INBUF_INIT_CHAR) {
|
||
1271 | action = ACTION_RECALLPREVIOUS; |
||
1272 | e545e620 | Thomas Schöpping | } |
1273 | ba516b61 | Thomas Schöpping | } |
1274 | |||
1275 | 697dba3c | Thomas Schöpping | // 'arrow down' key
|
1276 | else if (key == KEY_ARROWDOWN) { |
||
1277 | // clear the line if
|
||
1278 | // no historic entry is selected or
|
||
1279 | // the most recent entry is selected, but the current one is occupied by a moodfied version of a historic entry
|
||
1280 | if ((rdata->buffer.selected == 1) || |
||
1281 | (rdata->buffer.selected == 2 && rdata->buffer.edited > 1)) { |
||
1282 | action = ACTION_CLEAR; |
||
1283 | e545e620 | Thomas Schöpping | } |
1284 | 697dba3c | Thomas Schöpping | // if a historic entry is selected, recall the next input from history
|
1285 | else if (rdata->buffer.selected > 1) { |
||
1286 | action = ACTION_RECALLNEXT; |
||
1287 | e545e620 | Thomas Schöpping | } |
1288 | ba516b61 | Thomas Schöpping | } |
1289 | |||
1290 | // 'arrow left' key
|
||
1291 | 697dba3c | Thomas Schöpping | else if (key == KEY_ARROWLEFT) { |
1292 | e545e620 | Thomas Schöpping | // ignore if cursor is very left
|
1293 | 697dba3c | Thomas Schöpping | if (rdata->input.cursorpos > 0) { |
1294 | action = ACTION_CURSORLEFT; |
||
1295 | e545e620 | Thomas Schöpping | } |
1296 | ba516b61 | Thomas Schöpping | } |
1297 | |||
1298 | // 'arrow right' key
|
||
1299 | 697dba3c | Thomas Schöpping | else if (key == KEY_ARROWRIGHT) { |
1300 | cc33217b | Thomas Schöpping | // ignore if cursor is very right
|
1301 | 697dba3c | Thomas Schöpping | if (rdata->input.cursorpos < rdata->input.length) {
|
1302 | action = ACTION_CURSORRIGHT; |
||
1303 | } |
||
1304 | } |
||
1305 | |||
1306 | // CTRL + 'arrow up' key combination or 'page up' key
|
||
1307 | else if (key == KEY_CTRL_ARROWUP || key == KEY_PAGEUP) { |
||
1308 | // recall oldest input from history only if
|
||
1309 | // not the oldest entry is already selected and
|
||
1310 | // there is at least one history entry set
|
||
1311 | if (rdata->buffer.selected < shell->input.nentries &&
|
||
1312 | (_getRelativeEntry(shell, rdata, (rdata->buffer.selected > 0) ? 1 : 0))[0] != INBUF_INIT_CHAR) { |
||
1313 | action = ACTION_RECALLOLDEST; |
||
1314 | } |
||
1315 | } |
||
1316 | |||
1317 | // CTRL + 'arrow down' key combination or 'page down' key
|
||
1318 | else if (key == KEY_CTRL_ARROWDOWN || key == KEY_PAGEDOWN) { |
||
1319 | // clear the line if
|
||
1320 | // no historic entry is selected or
|
||
1321 | // the most recent entry is selected, but the current one is occupied by a moodfied version of a historic entry
|
||
1322 | if ((rdata->buffer.selected == 1) || |
||
1323 | (rdata->buffer.selected > 1 && rdata->buffer.edited > 1)) { |
||
1324 | action = ACTION_CLEAR; |
||
1325 | } |
||
1326 | // if a historic entry is selected, reset to the current input
|
||
1327 | else if (rdata->buffer.selected > 1) { |
||
1328 | action = ACTION_RECALLCURRENT; |
||
1329 | e545e620 | Thomas Schöpping | } |
1330 | ba516b61 | Thomas Schöpping | } |
1331 | |||
1332 | cc33217b | Thomas Schöpping | // CTRL + 'arrow left' key combination
|
1333 | 697dba3c | Thomas Schöpping | else if (key == KEY_CTRL_ARROWLEFT) { |
1334 | cc33217b | Thomas Schöpping | // ignore if cursor is very left
|
1335 | 697dba3c | Thomas Schöpping | if (rdata->input.cursorpos > 0) { |
1336 | action = ACTION_CURSORWORDLEFT; |
||
1337 | cc33217b | Thomas Schöpping | } |
1338 | } |
||
1339 | |||
1340 | // CTRL + 'arrow right' key combination
|
||
1341 | 697dba3c | Thomas Schöpping | else if (key == KEY_CTRL_ARROWRIGHT) { |
1342 | cc33217b | Thomas Schöpping | // ignore if cursor is very right
|
1343 | 697dba3c | Thomas Schöpping | if (rdata->input.cursorpos < rdata->input.length) {
|
1344 | action = ACTION_CURSORWORDRIGHT; |
||
1345 | cc33217b | Thomas Schöpping | } |
1346 | } |
||
1347 | |||
1348 | 697dba3c | Thomas Schöpping | // 'end' key
|
1349 | else if (key == KEY_END) { |
||
1350 | // ignore if cursos is very right
|
||
1351 | if (rdata->input.cursorpos < rdata->input.length) {
|
||
1352 | action = ACTION_CURSOR2END; |
||
1353 | } |
||
1354 | ba516b61 | Thomas Schöpping | } |
1355 | |||
1356 | 697dba3c | Thomas Schöpping | // 'home' key
|
1357 | else if (key == KEY_HOME) { |
||
1358 | // ignore if cursor is very left
|
||
1359 | if (rdata->input.cursorpos > 0) { |
||
1360 | action = ACTION_CURSOR2START; |
||
1361 | } |
||
1362 | e545e620 | Thomas Schöpping | } |
1363 | cc33217b | Thomas Schöpping | |
1364 | 697dba3c | Thomas Schöpping | // CTRL + C key combination
|
1365 | else if (c == '\x03' || key == KEY_CTRL_C) { |
||
1366 | action = ACTION_RESET; |
||
1367 | } |
||
1368 | |||
1369 | // INS key
|
||
1370 | else if (key == KEY_INSERT) { |
||
1371 | action = ACTION_INSERTTOGGLE; |
||
1372 | } |
||
1373 | |||
1374 | // ESC key or [ESCAPE] character
|
||
1375 | else if (c == '\x1B' || key == KEY_ESCAPE) { |
||
1376 | action = ACTION_ESCSTART; |
||
1377 | cc33217b | Thomas Schöpping | } |
1378 | e545e620 | Thomas Schöpping | } |
1379 | 697dba3c | Thomas Schöpping | // ongoing escape sequence or interpretation failed
|
1380 | else /* if (rdata->input.escseq[0] != '\0') */ { |
||
1381 | // unknown escape sequence (interpretation failed)
|
||
1382 | if (key == KEY_UNKNOWN) {
|
||
1383 | action = ACTION_PRINTUNKNOWNSEQUENCE; |
||
1384 | } |
||
1385 | } /* end of action selection */
|
||
1386 | e545e620 | Thomas Schöpping | |
1387 | 697dba3c | Thomas Schöpping | /*
|
1388 | * execute action
|
||
1389 | */
|
||
1390 | ba516b61 | Thomas Schöpping | switch (action) {
|
1391 | 697dba3c | Thomas Schöpping | case ACTION_NONE:
|
1392 | { |
||
1393 | // do nothing (ignore input) and read next byte
|
||
1394 | break;
|
||
1395 | } |
||
1396 | |||
1397 | case ACTION_READCHAR:
|
||
1398 | ba516b61 | Thomas Schöpping | { |
1399 | 697dba3c | Thomas Schöpping | char* line = _prepare4Modification(shell, rdata);
|
1400 | if (_printChar(shell, rdata, c) == 0) { |
||
1401 | cc33217b | Thomas Schöpping | // line is full
|
1402 | 697dba3c | Thomas Schöpping | _moveCursor(shell, line, rdata->input.cursorpos, rdata->input.length); |
1403 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "\n\tmaximum line width reached\n");
|
1404 | e545e620 | Thomas Schöpping | _printPrompt(shell); |
1405 | 697dba3c | Thomas Schöpping | _printString(shell, line, 0, rdata->input.length);
|
1406 | _moveCursor(shell, line, rdata->input.length, rdata->input.cursorpos); |
||
1407 | e545e620 | Thomas Schöpping | } |
1408 | break;
|
||
1409 | ba516b61 | Thomas Schöpping | } |
1410 | e545e620 | Thomas Schöpping | |
1411 | 697dba3c | Thomas Schöpping | case ACTION_AUTOCOMPLETE:
|
1412 | e545e620 | Thomas Schöpping | { |
1413 | 697dba3c | Thomas Schöpping | // local variables
|
1414 | char* line = _getVisualisedEntry(shell, rdata);
|
||
1415 | const char* fill = line; |
||
1416 | size_t cmatch = rdata->input.cursorpos; |
||
1417 | e545e620 | Thomas Schöpping | charmatch_t matchlevel = CHAR_MATCH_NOT; |
1418 | size_t n; |
||
1419 | 697dba3c | Thomas Schöpping | |
1420 | // only execute autofill if the line is valid
|
||
1421 | if (line) {
|
||
1422 | // iterate through command list
|
||
1423 | for (aos_shellcommand_t* cmd = shell->commands; cmd != NULL; cmd = cmd->next) { |
||
1424 | // compare current match with command
|
||
1425 | n = cmatch; |
||
1426 | charmatch_t mlvl = CHAR_MATCH_NOT; |
||
1427 | _strccmp(fill, cmd->name, shell->config & AOS_SHELL_CONFIG_MATCH_CASE, (n == 0) ? NULL : &n, &mlvl); |
||
1428 | const int cmp = (n < cmatch) ? |
||
1429 | ((int)n - (int)cmatch) : |
||
1430 | (cmd->name[n] != '\0') ?
|
||
1431 | (int)strlen(cmd->name) - (int)n : |
||
1432 | 0;
|
||
1433 | // if an exact match was found
|
||
1434 | if ((size_t)((int)cmatch + cmp) == rdata->input.cursorpos) { |
||
1435 | cmatch = rdata->input.cursorpos; |
||
1436 | e545e620 | Thomas Schöpping | fill = cmd->name; |
1437 | 697dba3c | Thomas Schöpping | // break the loop only if there are no case mismatches with the input
|
1438 | n = rdata->input.cursorpos; |
||
1439 | _strccmp(fill, line, false, &n, &mlvl);
|
||
1440 | if (mlvl == CHAR_MATCH_CASE) {
|
||
1441 | break;
|
||
1442 | } |
||
1443 | e545e620 | Thomas Schöpping | } |
1444 | 697dba3c | Thomas Schöpping | // if a not exact match was found
|
1445 | else if ((size_t)((int)cmatch + cmp) > rdata->input.cursorpos) { |
||
1446 | // if this is the first one
|
||
1447 | if (fill == line) {
|
||
1448 | cmatch = (size_t)((int)cmatch + cmp);
|
||
1449 | fill = cmd->name; |
||
1450 | } |
||
1451 | // if this is a worse one
|
||
1452 | else if ((cmp < 0) || (cmp == 0 && mlvl == CHAR_MATCH_CASE)) { |
||
1453 | cmatch = (size_t)((int)cmatch + cmp);
|
||
1454 | } |
||
1455 | e545e620 | Thomas Schöpping | } |
1456 | 697dba3c | Thomas Schöpping | // non matching commands are ignored
|
1457 | else {}
|
||
1458 | e545e620 | Thomas Schöpping | } |
1459 | 697dba3c | Thomas Schöpping | |
1460 | // evaluate if there are case mismatches
|
||
1461 | n = cmatch; |
||
1462 | _strccmp(line, fill, shell->config & AOS_SHELL_CONFIG_MATCH_CASE, &n, &matchlevel); |
||
1463 | // print the auto fill if any
|
||
1464 | ab5cad1b | Thomas Schöpping | if ((cmatch > rdata->input.cursorpos) ||
|
1465 | (cmatch == rdata->input.cursorpos && matchlevel == CHAR_MATCH_NCASE && strlen(fill) == rdata->input.cursorpos)) { |
||
1466 | 474a0731 | Thomas Schöpping | line = _prepare4Modification(shell, rdata); |
1467 | 697dba3c | Thomas Schöpping | // limit auto fill so it will not overflow the line width
|
1468 | if (rdata->input.length + (cmatch - rdata->input.cursorpos) > shell->input.linewidth) {
|
||
1469 | cmatch = shell->input.linewidth - rdata->input.length + rdata->input.cursorpos; |
||
1470 | } |
||
1471 | // move trailing memory further in the line
|
||
1472 | memmove(&line[cmatch], &line[rdata->input.cursorpos], (rdata->input.length - rdata->input.cursorpos) * sizeof(char)); |
||
1473 | rdata->input.length += cmatch - rdata->input.cursorpos; |
||
1474 | // if there was no incorrect case when matching
|
||
1475 | if (matchlevel == CHAR_MATCH_CASE) {
|
||
1476 | // insert fill command name to line
|
||
1477 | memcpy(&line[rdata->input.cursorpos], &fill[rdata->input.cursorpos], (cmatch - rdata->input.cursorpos) * sizeof(char)); |
||
1478 | // print the output
|
||
1479 | _printString(shell, line, rdata->input.cursorpos, rdata->input.length); |
||
1480 | } else {
|
||
1481 | // overwrite line with fill command name
|
||
1482 | memcpy(line, fill, cmatch * sizeof(char)); |
||
1483 | // reprint the whole line
|
||
1484 | _moveCursor(shell, line, rdata->input.cursorpos, 0);
|
||
1485 | _printString(shell, line, 0, rdata->input.length);
|
||
1486 | } |
||
1487 | // move cursor to the end of the matching sequence
|
||
1488 | rdata->input.cursorpos = cmatch; |
||
1489 | _moveCursor(shell, line, rdata->input.length, rdata->input.cursorpos); |
||
1490 | e545e620 | Thomas Schöpping | } |
1491 | } |
||
1492 | break;
|
||
1493 | } |
||
1494 | |||
1495 | 697dba3c | Thomas Schöpping | case ACTION_SUGGEST:
|
1496 | e545e620 | Thomas Schöpping | { |
1497 | 697dba3c | Thomas Schöpping | // local variables
|
1498 | const char* line = _getVisualisedEntry(shell, rdata); |
||
1499 | e545e620 | Thomas Schöpping | unsigned int matches = 0; |
1500 | 697dba3c | Thomas Schöpping | |
1501 | e545e620 | Thomas Schöpping | // iterate through command list
|
1502 | for (aos_shellcommand_t* cmd = shell->commands; cmd != NULL; cmd = cmd->next) { |
||
1503 | 697dba3c | Thomas Schöpping | // compare line content with command, except if cursorpos is 0
|
1504 | size_t i = rdata->input.cursorpos; |
||
1505 | if (rdata->input.cursorpos > 0) { |
||
1506 | ab5cad1b | Thomas Schöpping | _strccmp(line, cmd->name, shell->config & AOS_SHELL_CONFIG_MATCH_CASE, &i, NULL);
|
1507 | e545e620 | Thomas Schöpping | } |
1508 | 697dba3c | Thomas Schöpping | const int cmp = (i < rdata->input.cursorpos) ? |
1509 | ((int)i - (int)rdata->input.cursorpos) : |
||
1510 | (cmd->name[i] != '\0') ?
|
||
1511 | (int)strlen(cmd->name) - (int)i : |
||
1512 | 0;
|
||
1513 | e545e620 | Thomas Schöpping | // if a match was found
|
1514 | if (cmp > 0) { |
||
1515 | // if this is the first one
|
||
1516 | if (matches == 0) { |
||
1517 | 697dba3c | Thomas Schöpping | _moveCursor(shell, line, rdata->input.cursorpos, rdata->input.length); |
1518 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, '\n');
|
1519 | e545e620 | Thomas Schöpping | } |
1520 | // print the command
|
||
1521 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "\t%s\n", cmd->name);
|
1522 | e545e620 | Thomas Schöpping | ++matches; |
1523 | } |
||
1524 | } |
||
1525 | // reprint the prompt and line if any matches have been found
|
||
1526 | if (matches > 0) { |
||
1527 | _printPrompt(shell); |
||
1528 | 697dba3c | Thomas Schöpping | _printString(shell, line, 0, rdata->input.length);
|
1529 | _moveCursor(shell, line, rdata->input.length, rdata->input.cursorpos); |
||
1530 | e545e620 | Thomas Schöpping | } |
1531 | break;
|
||
1532 | } |
||
1533 | |||
1534 | 697dba3c | Thomas Schöpping | case ACTION_EXECUTE:
|
1535 | ba516b61 | Thomas Schöpping | { |
1536 | 697dba3c | Thomas Schöpping | // if the input buffer can hold historic entries
|
1537 | if (shell->input.nentries > 1) { |
||
1538 | _prepare4Modification(shell, rdata); |
||
1539 | e545e620 | Thomas Schöpping | } |
1540 | break;
|
||
1541 | ba516b61 | Thomas Schöpping | } |
1542 | e545e620 | Thomas Schöpping | |
1543 | 697dba3c | Thomas Schöpping | case ACTION_DELETEBACKWARD:
|
1544 | ba516b61 | Thomas Schöpping | { |
1545 | 697dba3c | Thomas Schöpping | char* line = _prepare4Modification(shell, rdata);
|
1546 | --rdata->input.cursorpos; |
||
1547 | memmove(&line[rdata->input.cursorpos], &line[rdata->input.cursorpos + 1], (rdata->input.length - rdata->input.cursorpos) * sizeof(char)); |
||
1548 | --rdata->input.length; |
||
1549 | line[rdata->input.length] = '\0';
|
||
1550 | _moveCursor(shell, line, rdata->input.cursorpos + 1, rdata->input.cursorpos);
|
||
1551 | _printString(shell, line, rdata->input.cursorpos, rdata->input.length); |
||
1552 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, ' ');
|
1553 | 697dba3c | Thomas Schöpping | _moveCursor(shell, line, rdata->input.length + 1, rdata->input.cursorpos);
|
1554 | e545e620 | Thomas Schöpping | break;
|
1555 | ba516b61 | Thomas Schöpping | } |
1556 | e545e620 | Thomas Schöpping | |
1557 | 697dba3c | Thomas Schöpping | case ACTION_DELETEFORWARD:
|
1558 | ba516b61 | Thomas Schöpping | { |
1559 | 697dba3c | Thomas Schöpping | char* line = _prepare4Modification(shell, rdata);
|
1560 | --rdata->input.length; |
||
1561 | memmove(&line[rdata->input.cursorpos], &line[rdata->input.cursorpos + 1], (rdata->input.length - rdata->input.cursorpos) * sizeof(char)); |
||
1562 | _printString(shell, line, rdata->input.cursorpos, rdata->input.length); |
||
1563 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, ' ');
|
1564 | 697dba3c | Thomas Schöpping | _moveCursor(shell, line, rdata->input.length + 1, rdata->input.cursorpos);
|
1565 | e545e620 | Thomas Schöpping | break;
|
1566 | ba516b61 | Thomas Schöpping | } |
1567 | e545e620 | Thomas Schöpping | |
1568 | 697dba3c | Thomas Schöpping | case ACTION_CLEAR:
|
1569 | e545e620 | Thomas Schöpping | { |
1570 | 697dba3c | Thomas Schöpping | // clear visualization
|
1571 | _moveCursor(shell, NULL, rdata->input.cursorpos, 0); |
||
1572 | for (size_t cpos = 0; cpos < rdata->input.length; ++cpos) { |
||
1573 | streamPut(&shell->stream, ' ');
|
||
1574 | e545e620 | Thomas Schöpping | } |
1575 | 697dba3c | Thomas Schöpping | _moveCursor(shell, NULL, rdata->input.length, 0); |
1576 | |||
1577 | // update metadata
|
||
1578 | rdata->input.cursorpos = 0;
|
||
1579 | rdata->input.length = 0;
|
||
1580 | rdata->buffer.selected = 0;
|
||
1581 | |||
1582 | e545e620 | Thomas Schöpping | break;
|
1583 | } |
||
1584 | |||
1585 | 697dba3c | Thomas Schöpping | case ACTION_RECALLPREVIOUS:
|
1586 | ba516b61 | Thomas Schöpping | { |
1587 | 697dba3c | Thomas Schöpping | // if the input was cleared but the current entry is occupied by a modified copy of a history entry, skip the current entry
|
1588 | 4b8243d7 | Thomas Schöpping | if (shell->input.nentries > 1 && rdata->buffer.selected == 0 && rdata->buffer.edited != 1) { |
1589 | 697dba3c | Thomas Schöpping | rdata->buffer.selected += 2;
|
1590 | } else {
|
||
1591 | ++rdata->buffer.selected; |
||
1592 | e545e620 | Thomas Schöpping | } |
1593 | 697dba3c | Thomas Schöpping | _overwriteOutput(shell, rdata, _getVisualisedEntry(shell, rdata)); |
1594 | break;
|
||
1595 | } |
||
1596 | |||
1597 | case ACTION_RECALLNEXT:
|
||
1598 | { |
||
1599 | --rdata->buffer.selected; |
||
1600 | _overwriteOutput(shell, rdata, _getVisualisedEntry(shell, rdata)); |
||
1601 | e545e620 | Thomas Schöpping | break;
|
1602 | ba516b61 | Thomas Schöpping | } |
1603 | e545e620 | Thomas Schöpping | |
1604 | 697dba3c | Thomas Schöpping | case ACTION_RECALLOLDEST:
|
1605 | ba516b61 | Thomas Schöpping | { |
1606 | 697dba3c | Thomas Schöpping | // find oldest valid entry
|
1607 | rdata->buffer.selected = shell->input.nentries; |
||
1608 | while (_getSelectedEntry(shell, rdata)[0] == INBUF_INIT_CHAR) { |
||
1609 | --rdata->buffer.selected; |
||
1610 | } |
||
1611 | |||
1612 | _overwriteOutput(shell, rdata, _getVisualisedEntry(shell, rdata)); |
||
1613 | e545e620 | Thomas Schöpping | break;
|
1614 | ba516b61 | Thomas Schöpping | } |
1615 | e545e620 | Thomas Schöpping | |
1616 | 697dba3c | Thomas Schöpping | case ACTION_RECALLCURRENT:
|
1617 | ba516b61 | Thomas Schöpping | { |
1618 | 697dba3c | Thomas Schöpping | rdata->buffer.selected = 1;
|
1619 | _overwriteOutput(shell, rdata, _getVisualisedEntry(shell, rdata)); |
||
1620 | e545e620 | Thomas Schöpping | break;
|
1621 | ba516b61 | Thomas Schöpping | } |
1622 | e545e620 | Thomas Schöpping | |
1623 | 697dba3c | Thomas Schöpping | case ACTION_CURSORLEFT:
|
1624 | ba516b61 | Thomas Schöpping | { |
1625 | 697dba3c | Thomas Schöpping | _moveCursor(shell, NULL, rdata->input.cursorpos, rdata->input.cursorpos - 1); |
1626 | --rdata->input.cursorpos; |
||
1627 | e545e620 | Thomas Schöpping | break;
|
1628 | ba516b61 | Thomas Schöpping | } |
1629 | e545e620 | Thomas Schöpping | |
1630 | 697dba3c | Thomas Schöpping | case ACTION_CURSORRIGHT:
|
1631 | ba516b61 | Thomas Schöpping | { |
1632 | 697dba3c | Thomas Schöpping | _moveCursor(shell, _getVisualisedEntry(shell, rdata), rdata->input.cursorpos, rdata->input.cursorpos + 1);
|
1633 | ++rdata->input.cursorpos; |
||
1634 | e545e620 | Thomas Schöpping | break;
|
1635 | ba516b61 | Thomas Schöpping | } |
1636 | e545e620 | Thomas Schöpping | |
1637 | 697dba3c | Thomas Schöpping | case ACTION_CURSORWORDLEFT:
|
1638 | cc33217b | Thomas Schöpping | { |
1639 | 697dba3c | Thomas Schöpping | // local variables
|
1640 | const char* line = _getVisualisedEntry(shell, rdata); |
||
1641 | size_t cpos = rdata->input.cursorpos; |
||
1642 | |||
1643 | // skip spaces
|
||
1644 | while (cpos > 0) { |
||
1645 | cc33217b | Thomas Schöpping | --cpos; |
1646 | 697dba3c | Thomas Schöpping | if (line[cpos] != ' ') { |
1647 | break;
|
||
1648 | } |
||
1649 | cc33217b | Thomas Schöpping | } |
1650 | 697dba3c | Thomas Schöpping | |
1651 | // search for beginning of the word
|
||
1652 | while (cpos > 0) { |
||
1653 | cc33217b | Thomas Schöpping | --cpos; |
1654 | 697dba3c | Thomas Schöpping | if (line[cpos] == ' ') { |
1655 | // go back to first character of the word
|
||
1656 | ++cpos; |
||
1657 | break;
|
||
1658 | } |
||
1659 | cc33217b | Thomas Schöpping | } |
1660 | 697dba3c | Thomas Schöpping | |
1661 | // move the cursor and set metadata
|
||
1662 | _moveCursor(shell, line, rdata->input.cursorpos, cpos); |
||
1663 | rdata->input.cursorpos = cpos; |
||
1664 | |||
1665 | cc33217b | Thomas Schöpping | break;
|
1666 | } |
||
1667 | |||
1668 | 697dba3c | Thomas Schöpping | case ACTION_CURSORWORDRIGHT:
|
1669 | cc33217b | Thomas Schöpping | { |
1670 | 697dba3c | Thomas Schöpping | // local variables
|
1671 | const char* line = _getVisualisedEntry(shell, rdata); |
||
1672 | size_t cpos = rdata->input.cursorpos; |
||
1673 | |||
1674 | // skip spaces
|
||
1675 | while (cpos < rdata->input.length && line[cpos] == ' ') { |
||
1676 | cc33217b | Thomas Schöpping | ++cpos; |
1677 | } |
||
1678 | 697dba3c | Thomas Schöpping | |
1679 | // search for end of the word
|
||
1680 | while (cpos < rdata->input.length && line[cpos] != ' ') { |
||
1681 | cc33217b | Thomas Schöpping | ++cpos; |
1682 | } |
||
1683 | 697dba3c | Thomas Schöpping | |
1684 | // move cursor and set metadata
|
||
1685 | _moveCursor(shell, line, rdata->input.cursorpos, cpos); |
||
1686 | rdata->input.cursorpos = cpos; |
||
1687 | |||
1688 | cc33217b | Thomas Schöpping | break;
|
1689 | } |
||
1690 | |||
1691 | 697dba3c | Thomas Schöpping | case ACTION_CURSOR2END:
|
1692 | ba516b61 | Thomas Schöpping | { |
1693 | 697dba3c | Thomas Schöpping | _moveCursor(shell, _getVisualisedEntry(shell, rdata), rdata->input.cursorpos, rdata->input.length); |
1694 | rdata->input.cursorpos = rdata->input.length; |
||
1695 | c18a848e | Thomas Schöpping | break;
|
1696 | ba516b61 | Thomas Schöpping | } |
1697 | e545e620 | Thomas Schöpping | |
1698 | 697dba3c | Thomas Schöpping | case ACTION_CURSOR2START:
|
1699 | ba516b61 | Thomas Schöpping | { |
1700 | 697dba3c | Thomas Schöpping | _moveCursor(shell, _getVisualisedEntry(shell, rdata), rdata->input.cursorpos, 0);
|
1701 | rdata->input.cursorpos = 0;
|
||
1702 | cc33217b | Thomas Schöpping | break;
|
1703 | } |
||
1704 | |||
1705 | 697dba3c | Thomas Schöpping | case ACTION_RESET:
|
1706 | cc33217b | Thomas Schöpping | { |
1707 | 697dba3c | Thomas Schöpping | // print reset indicator
|
1708 | chprintf((BaseSequentialStream*)&shell->stream, "^C");
|
||
1709 | if (!(shell->config & AOS_SHELL_CONFIG_INPUT_OVERWRITE)) {
|
||
1710 | _printString(shell, _getVisualisedEntry(shell, rdata), rdata->input.cursorpos, rdata->input.length); |
||
1711 | cc33217b | Thomas Schöpping | } |
1712 | 697dba3c | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "\n");
|
1713 | |||
1714 | // reset buffers and metadata
|
||
1715 | if (rdata->buffer.edited != 0) { |
||
1716 | memset(_getCurrentEntry(shell, rdata), '\0', shell->input.linewidth * sizeof(char)); |
||
1717 | } |
||
1718 | rdata->input.length = 0;
|
||
1719 | rdata->input.cursorpos = 0;
|
||
1720 | rdata->buffer.selected = (shell->input.nentries > 1) ? 1 : 0; |
||
1721 | rdata->buffer.edited = 0;
|
||
1722 | |||
1723 | // print a new prompt
|
||
1724 | _printPrompt(shell); |
||
1725 | |||
1726 | e545e620 | Thomas Schöpping | break;
|
1727 | ba516b61 | Thomas Schöpping | } |
1728 | e545e620 | Thomas Schöpping | |
1729 | 697dba3c | Thomas Schöpping | case ACTION_INSERTTOGGLE:
|
1730 | ba516b61 | Thomas Schöpping | { |
1731 | 697dba3c | Thomas Schöpping | if (shell->config & AOS_SHELL_CONFIG_INPUT_OVERWRITE) {
|
1732 | shell->config &= ~AOS_SHELL_CONFIG_INPUT_OVERWRITE; |
||
1733 | } else {
|
||
1734 | shell->config |= AOS_SHELL_CONFIG_INPUT_OVERWRITE; |
||
1735 | } |
||
1736 | break;
|
||
1737 | ba516b61 | Thomas Schöpping | } |
1738 | e545e620 | Thomas Schöpping | |
1739 | 697dba3c | Thomas Schöpping | case ACTION_ESCSTART:
|
1740 | { |
||
1741 | rdata->input.escseq[0] = c;
|
||
1742 | break;
|
||
1743 | } |
||
1744 | e545e620 | Thomas Schöpping | |
1745 | 697dba3c | Thomas Schöpping | case ACTION_PRINTUNKNOWNSEQUENCE:
|
1746 | { |
||
1747 | _prepare4Modification(shell, rdata); |
||
1748 | for (size_t seqc = 1; rdata->input.escseq[seqc] != '\0'; ++seqc) { |
||
1749 | // element 0 would be unprintable ESC character
|
||
1750 | _printChar(shell, rdata, rdata->input.escseq[seqc]); |
||
1751 | } |
||
1752 | memset(rdata->input.escseq, '\0', AOS_SHELL_ESCSEQUENCE_LENGTH * sizeof(char)); |
||
1753 | break;
|
||
1754 | } |
||
1755 | } |
||
1756 | c18a848e | Thomas Schöpping | |
1757 | 697dba3c | Thomas Schöpping | // update runtime data
|
1758 | rdata->lastaction = (action != ACTION_NONE) ? action : rdata->lastaction; |
||
1759 | } /* end of while */
|
||
1760 | |||
1761 | return bytes;
|
||
1762 | e545e620 | Thomas Schöpping | } |
1763 | |||
1764 | /**
|
||
1765 | 697dba3c | Thomas Schöpping | * @brief Parses the content of the given string to separate arguments.
|
1766 | e545e620 | Thomas Schöpping | *
|
1767 | 10fd7ac9 | Thomas Schöpping | * @param[in] shell Pointer to the shell object.
|
1768 | 697dba3c | Thomas Schöpping | * @param[in] str String to be parsed.
|
1769 | 10fd7ac9 | Thomas Schöpping | * @param[out] argbuf Buffer to store argument pointers to.
|
1770 | e545e620 | Thomas Schöpping | *
|
1771 | * @return Number of arguments found.
|
||
1772 | */
|
||
1773 | 697dba3c | Thomas Schöpping | static size_t _parseArguments(aos_shell_t* shell, char* str, char** argbuf) |
1774 | e545e620 | Thomas Schöpping | { |
1775 | aosDbgCheck(shell != NULL);
|
||
1776 | 697dba3c | Thomas Schöpping | aosDbgCheck(str != NULL);
|
1777 | 10fd7ac9 | Thomas Schöpping | aosDbgCheck(argbuf != NULL);
|
1778 | e545e620 | Thomas Schöpping | |
1779 | /*
|
||
1780 | * States for a very small FSM.
|
||
1781 | */
|
||
1782 | typedef enum { |
||
1783 | START, |
||
1784 | SPACE, |
||
1785 | TEXT, |
||
1786 | END, |
||
1787 | } state_t; |
||
1788 | |||
1789 | // local variables
|
||
1790 | state_t state = START; |
||
1791 | 10fd7ac9 | Thomas Schöpping | size_t nargs = 0;
|
1792 | e545e620 | Thomas Schöpping | |
1793 | // iterate through the line
|
||
1794 | 697dba3c | Thomas Schöpping | for (size_t c = 0; c < shell->input.linewidth; ++c) { |
1795 | e545e620 | Thomas Schöpping | // terminate at first NUL byte
|
1796 | 697dba3c | Thomas Schöpping | if (str[c] == '\0') { |
1797 | e545e620 | Thomas Schöpping | state = END; |
1798 | break;
|
||
1799 | } |
||
1800 | // spaces become NUL bytes
|
||
1801 | 697dba3c | Thomas Schöpping | else if (str[c] == ' ') { |
1802 | str[c] = '\0';
|
||
1803 | e545e620 | Thomas Schöpping | state = SPACE; |
1804 | } |
||
1805 | // handle non-NUL bytes
|
||
1806 | else {
|
||
1807 | switch (state) {
|
||
1808 | case START:
|
||
1809 | case SPACE:
|
||
1810 | // ignore too many arguments
|
||
1811 | 10fd7ac9 | Thomas Schöpping | if (nargs < shell->input.nargs) {
|
1812 | 697dba3c | Thomas Schöpping | argbuf[nargs] = &str[c]; |
1813 | e545e620 | Thomas Schöpping | } |
1814 | 10fd7ac9 | Thomas Schöpping | ++nargs; |
1815 | e545e620 | Thomas Schöpping | break;
|
1816 | case TEXT:
|
||
1817 | case END:
|
||
1818 | break;
|
||
1819 | } |
||
1820 | state = TEXT; |
||
1821 | } |
||
1822 | } |
||
1823 | |||
1824 | // set all remaining argument pointers to NULL
|
||
1825 | 10fd7ac9 | Thomas Schöpping | for (size_t a = nargs; a < shell->input.nargs; ++a) {
|
1826 | argbuf[a] = NULL;
|
||
1827 | e545e620 | Thomas Schöpping | } |
1828 | |||
1829 | 10fd7ac9 | Thomas Schöpping | return nargs;
|
1830 | e545e620 | Thomas Schöpping | } |
1831 | |||
1832 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
1833 | /* EXPORTED FUNCTIONS */
|
||
1834 | /******************************************************************************/
|
||
1835 | |||
1836 | e545e620 | Thomas Schöpping | /**
|
1837 | * @brief Initializes a shell object with the specified parameters.
|
||
1838 | *
|
||
1839 | 697dba3c | Thomas Schöpping | * @param[in,out] shell Pointer to the shell object to be initialized.
|
1840 | 3da12676 | Thomas Schöpping | * @param[in] name Name of the shell thread (may be NULL).
|
1841 | 697dba3c | Thomas Schöpping | * @param[in] prompt Prompt line to print (NULL = use default prompt).
|
1842 | * @param[in] inbuf Two dimensional input buffer.
|
||
1843 | * @param[in] entries Number of entries in the input buffer (1st dimension).
|
||
1844 | * @param[in] linewidth Length of each entry in the input buffer (2nd dimension).
|
||
1845 | * @param[in] numargs Maximum number of arguments (defines size of internal buffer).
|
||
1846 | */
|
||
1847 | 3da12676 | Thomas Schöpping | void aosShellInit(aos_shell_t* shell, const char* name, const char* prompt, char inbuf[], size_t entries, size_t linewidth, size_t numargs) |
1848 | e545e620 | Thomas Schöpping | { |
1849 | aosDbgCheck(shell != NULL);
|
||
1850 | 697dba3c | Thomas Schöpping | aosDbgCheck(inbuf != NULL);
|
1851 | aosDbgCheck(entries > 0);
|
||
1852 | aosDbgCheck(linewidth > 0);
|
||
1853 | aosDbgCheck(numargs > 0);
|
||
1854 | e545e620 | Thomas Schöpping | |
1855 | // set parameters
|
||
1856 | shell->thread = NULL;
|
||
1857 | 3da12676 | Thomas Schöpping | #if (CH_CFG_USE_REGISTRY == TRUE)
|
1858 | shell->name = name; |
||
1859 | #else /* (CH_CFG_USE_REGISTRY == TRUE) */ |
||
1860 | (void)name;
|
||
1861 | #endif /* (CH_CFG_USE_REGISTRY == TRUE) */ |
||
1862 | e545e620 | Thomas Schöpping | chEvtObjectInit(&shell->eventSource); |
1863 | ba516b61 | Thomas Schöpping | aosShellStreamInit(&shell->stream); |
1864 | e545e620 | Thomas Schöpping | shell->prompt = prompt; |
1865 | shell->commands = NULL;
|
||
1866 | shell->execstatus.command = NULL;
|
||
1867 | shell->execstatus.retval = 0;
|
||
1868 | 697dba3c | Thomas Schöpping | shell->input.buffer = inbuf; |
1869 | shell->input.nentries = entries; |
||
1870 | shell->input.linewidth = linewidth; |
||
1871 | shell->input.nargs = numargs; |
||
1872 | e545e620 | Thomas Schöpping | shell->config = 0x00;
|
1873 | |||
1874 | cc33217b | Thomas Schöpping | // initialize buffers
|
1875 | 697dba3c | Thomas Schöpping | memset(shell->input.buffer, INBUF_INIT_CHAR, shell->input.nentries * shell->input.linewidth * sizeof(char)); |
1876 | e545e620 | Thomas Schöpping | |
1877 | return;
|
||
1878 | } |
||
1879 | |||
1880 | /**
|
||
1881 | ba516b61 | Thomas Schöpping | * @brief Initialize an AosShellStream object.
|
1882 | *
|
||
1883 | * @param[in] stream The AosShellStrem to initialize.
|
||
1884 | */
|
||
1885 | void aosShellStreamInit(AosShellStream* stream)
|
||
1886 | { |
||
1887 | aosDbgCheck(stream != NULL);
|
||
1888 | |||
1889 | stream->vmt = &_streamvmt; |
||
1890 | stream->channel = NULL;
|
||
1891 | |||
1892 | return;
|
||
1893 | } |
||
1894 | |||
1895 | /**
|
||
1896 | * @brief Initialize an AosShellChannel object with the specified parameters.
|
||
1897 | *
|
||
1898 | dd8738ea | Thomas Schöpping | * @param[in] channel The AosShellChannel to initialize.
|
1899 | * @param[in] asyncchannel An BaseAsynchronousChannel this AosShellChannel is associated with.
|
||
1900 | ba516b61 | Thomas Schöpping | */
|
1901 | dd8738ea | Thomas Schöpping | void aosShellChannelInit(AosShellChannel* channel, BaseAsynchronousChannel* asyncchannel)
|
1902 | ba516b61 | Thomas Schöpping | { |
1903 | aosDbgCheck(channel != NULL);
|
||
1904 | dd8738ea | Thomas Schöpping | aosDbgCheck(asyncchannel != NULL);
|
1905 | ba516b61 | Thomas Schöpping | |
1906 | channel->vmt = &_channelvmt; |
||
1907 | dd8738ea | Thomas Schöpping | channel->asyncchannel = asyncchannel; |
1908 | channel->listener.wflags = 0;
|
||
1909 | ba516b61 | Thomas Schöpping | channel->next = NULL;
|
1910 | channel->flags = 0;
|
||
1911 | |||
1912 | return;
|
||
1913 | } |
||
1914 | |||
1915 | /**
|
||
1916 | e545e620 | Thomas Schöpping | * @brief Inserts a command to the shells list of commands.
|
1917 | *
|
||
1918 | * @param[in] shell Pointer to the shell object.
|
||
1919 | * @param[in] cmd Pointer to the command to add.
|
||
1920 | *
|
||
1921 | * @return A status value.
|
||
1922 | * @retval AOS_SUCCESS The command was added successfully.
|
||
1923 | * @retval AOS_ERROR Another command with identical name already exists.
|
||
1924 | */
|
||
1925 | aos_status_t aosShellAddCommand(aos_shell_t *shell, aos_shellcommand_t *cmd) |
||
1926 | { |
||
1927 | aosDbgCheck(shell != NULL);
|
||
1928 | aosDbgCheck(cmd != NULL);
|
||
1929 | aosDbgCheck(cmd->name != NULL && strlen(cmd->name) > 0 && strchr(cmd->name, ' ') == NULL && strchr(cmd->name, '\t') == NULL); |
||
1930 | aosDbgCheck(cmd->callback != NULL);
|
||
1931 | aosDbgCheck(cmd->next == NULL);
|
||
1932 | |||
1933 | aos_shellcommand_t* prev = NULL;
|
||
1934 | aos_shellcommand_t** curr = &(shell->commands); |
||
1935 | |||
1936 | // insert the command to the list wrt lexographical order (exception: lower case characters preceed upper their uppercase counterparts)
|
||
1937 | ba516b61 | Thomas Schöpping | while (*curr != NULL) { |
1938 | // iterate through the list as long as the command names are 'smaller'
|
||
1939 | const int cmp = _strccmp((*curr)->name, cmd->name, true, NULL, NULL); |
||
1940 | if (cmp < 0) { |
||
1941 | prev = *curr; |
||
1942 | curr = &((*curr)->next); |
||
1943 | continue;
|
||
1944 | } |
||
1945 | // error if the command already exists
|
||
1946 | else if (cmp == 0) { |
||
1947 | return AOS_ERROR;
|
||
1948 | } |
||
1949 | // insert the command as soon as a 'larger' name was found
|
||
1950 | else /* if (cmpval > 0) */ { |
||
1951 | cmd->next = *curr; |
||
1952 | // special case: the first command is larger
|
||
1953 | if (prev == NULL) { |
||
1954 | shell->commands = cmd; |
||
1955 | } else {
|
||
1956 | prev->next = cmd; |
||
1957 | e545e620 | Thomas Schöpping | } |
1958 | ba516b61 | Thomas Schöpping | return AOS_SUCCESS;
|
1959 | e545e620 | Thomas Schöpping | } |
1960 | } |
||
1961 | ba516b61 | Thomas Schöpping | // the end of the list has been reached
|
1962 | |||
1963 | // append the command
|
||
1964 | *curr = cmd; |
||
1965 | return AOS_SUCCESS;
|
||
1966 | e545e620 | Thomas Schöpping | } |
1967 | |||
1968 | /**
|
||
1969 | * @brief Removes a command from the shells list of commands.
|
||
1970 | *
|
||
1971 | * @param[in] shell Pointer to the shell object.
|
||
1972 | * @param[in] cmd Name of the command to removde.
|
||
1973 | * @param[out] removed Optional pointer to the command that was removed.
|
||
1974 | *
|
||
1975 | * @return A status value.
|
||
1976 | * @retval AOS_SUCCESS The command was removed successfully.
|
||
1977 | * @retval AOS_ERROR The command name was not found.
|
||
1978 | */
|
||
1979 | aos_status_t aosShellRemoveCommand(aos_shell_t *shell, char *cmd, aos_shellcommand_t **removed)
|
||
1980 | { |
||
1981 | aosDbgCheck(shell != NULL);
|
||
1982 | aosDbgCheck(cmd != NULL && strlen(cmd) > 0); |
||
1983 | |||
1984 | aos_shellcommand_t* prev = NULL;
|
||
1985 | aos_shellcommand_t** curr = &(shell->commands); |
||
1986 | |||
1987 | // iterate through the list and seach for the specified command name
|
||
1988 | while (curr != NULL) { |
||
1989 | const int cmpval = strcmp((*curr)->name, cmd); |
||
1990 | // iterate through the list as long as the command names are 'smaller'
|
||
1991 | if (cmpval < 0) { |
||
1992 | prev = *curr; |
||
1993 | curr = &((*curr)->next); |
||
1994 | continue;
|
||
1995 | } |
||
1996 | // remove the command when found
|
||
1997 | else if (cmpval == 0) { |
||
1998 | // special case: the first command matches
|
||
1999 | if (prev == NULL) { |
||
2000 | shell->commands = (*curr)->next; |
||
2001 | } else {
|
||
2002 | prev->next = (*curr)->next; |
||
2003 | } |
||
2004 | (*curr)->next = NULL;
|
||
2005 | // set the optional output argument
|
||
2006 | if (removed != NULL) { |
||
2007 | *removed = *curr; |
||
2008 | } |
||
2009 | return AOS_SUCCESS;
|
||
2010 | } |
||
2011 | // break the loop if the command names are 'larger'
|
||
2012 | else /* if (cmpval > 0) */ { |
||
2013 | break;
|
||
2014 | } |
||
2015 | } |
||
2016 | |||
2017 | // if the command was not found, return an error
|
||
2018 | return AOS_ERROR;
|
||
2019 | } |
||
2020 | |||
2021 | /**
|
||
2022 | aed3754b | Thomas Schöpping | * @brief Count the number of commands assigned to the shell.
|
2023 | *
|
||
2024 | * @param[in] shell The shell to count the commands for.
|
||
2025 | *
|
||
2026 | * @return The number of commands associated to the shell.
|
||
2027 | */
|
||
2028 | unsigned int aosShellCountCommands(aos_shell_t* shell) |
||
2029 | { |
||
2030 | aosDbgCheck(shell != NULL);
|
||
2031 | |||
2032 | unsigned int count = 0; |
||
2033 | aos_shellcommand_t* cmd = shell->commands; |
||
2034 | while (cmd != NULL) { |
||
2035 | ++count; |
||
2036 | cmd = cmd->next; |
||
2037 | } |
||
2038 | |||
2039 | return count;
|
||
2040 | } |
||
2041 | |||
2042 | /**
|
||
2043 | ba516b61 | Thomas Schöpping | * @brief Add a channel to a AosShellStream.
|
2044 | *
|
||
2045 | * @param[in] stream The AosShellStream to extend.
|
||
2046 | * @param[in] channel The channel to be added to the stream.
|
||
2047 | */
|
||
2048 | void aosShellStreamAddChannel(AosShellStream* stream, AosShellChannel* channel)
|
||
2049 | { |
||
2050 | aosDbgCheck(stream != NULL);
|
||
2051 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL && channel->next == NULL && (channel->flags & AOS_SHELLCHANNEL_ATTACHED) == 0); |
2052 | ba516b61 | Thomas Schöpping | |
2053 | // prepend the new channel
|
||
2054 | chSysLock(); |
||
2055 | channel->flags |= AOS_SHELLCHANNEL_ATTACHED; |
||
2056 | channel->next = stream->channel; |
||
2057 | stream->channel = channel; |
||
2058 | chSysUnlock(); |
||
2059 | |||
2060 | return;
|
||
2061 | } |
||
2062 | |||
2063 | /**
|
||
2064 | * @brief Remove a channel from an AosShellStream.
|
||
2065 | *
|
||
2066 | * @param[in] stream The AosShellStream to modify.
|
||
2067 | * @param[in] channel The channel to remove.
|
||
2068 | aed3754b | Thomas Schöpping | *
|
2069 | * @return A status value.
|
||
2070 | * @retval AOS_SUCCESS The channel was removed successfully.
|
||
2071 | * @retval AOS_ERROR The specified channel was not found to be associated with the shell.
|
||
2072 | ba516b61 | Thomas Schöpping | */
|
2073 | aos_status_t aosShellStreamRemoveChannel(AosShellStream* stream, AosShellChannel* channel) |
||
2074 | { |
||
2075 | aosDbgCheck(stream != NULL);
|
||
2076 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL && channel->flags & AOS_SHELLCHANNEL_ATTACHED); |
2077 | ba516b61 | Thomas Schöpping | |
2078 | // local varibales
|
||
2079 | AosShellChannel* prev = NULL;
|
||
2080 | AosShellChannel* curr = stream->channel; |
||
2081 | |||
2082 | // iterate through the list and search for the specified channel
|
||
2083 | while (curr != NULL) { |
||
2084 | // if the channel was found
|
||
2085 | if (curr == channel) {
|
||
2086 | chSysLock(); |
||
2087 | // special case: the first channel matches (prev is NULL)
|
||
2088 | if (prev == NULL) { |
||
2089 | stream->channel = curr->next; |
||
2090 | } else {
|
||
2091 | prev->next = channel->next; |
||
2092 | } |
||
2093 | curr->next = NULL;
|
||
2094 | curr->flags &= ~AOS_SHELLCHANNEL_ATTACHED; |
||
2095 | chSysUnlock(); |
||
2096 | return AOS_SUCCESS;
|
||
2097 | } |
||
2098 | } |
||
2099 | |||
2100 | // if the channel was not found, return an error
|
||
2101 | return AOS_ERROR;
|
||
2102 | } |
||
2103 | |||
2104 | /**
|
||
2105 | 243fb4e2 | Thomas Schöpping | * @brief Enable a AosShellChannel as input.
|
2106 | ba516b61 | Thomas Schöpping | *
|
2107 | * @param[in] channel The channel to enable as input.
|
||
2108 | */
|
||
2109 | void aosShellChannelInputEnable(AosShellChannel* channel)
|
||
2110 | { |
||
2111 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
2112 | ba516b61 | Thomas Schöpping | |
2113 | chSysLock(); |
||
2114 | channel->listener.wflags |= CHN_INPUT_AVAILABLE; |
||
2115 | channel->flags |= AOS_SHELLCHANNEL_INPUT_ENABLED; |
||
2116 | chSysUnlock(); |
||
2117 | |||
2118 | return;
|
||
2119 | } |
||
2120 | |||
2121 | /**
|
||
2122 | 243fb4e2 | Thomas Schöpping | * @brief Disable a AosShellChannel as input.
|
2123 | ba516b61 | Thomas Schöpping | *
|
2124 | * @param[in] channel The channel to disable as input.
|
||
2125 | */
|
||
2126 | void aosShellChannelInputDisable( AosShellChannel* channel)
|
||
2127 | { |
||
2128 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
2129 | ba516b61 | Thomas Schöpping | |
2130 | chSysLock(); |
||
2131 | channel->listener.wflags &= ~CHN_INPUT_AVAILABLE; |
||
2132 | channel->flags &= ~AOS_SHELLCHANNEL_INPUT_ENABLED; |
||
2133 | chSysUnlock(); |
||
2134 | |||
2135 | return;
|
||
2136 | } |
||
2137 | |||
2138 | /**
|
||
2139 | 243fb4e2 | Thomas Schöpping | * @brief Enable a AosShellChannel as output.
|
2140 | ba516b61 | Thomas Schöpping | *
|
2141 | * @param[in] channel The channel to enable as output.
|
||
2142 | */
|
||
2143 | void aosShellChannelOutputEnable(AosShellChannel* channel)
|
||
2144 | { |
||
2145 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
2146 | ba516b61 | Thomas Schöpping | |
2147 | channel->flags |= AOS_SHELLCHANNEL_OUTPUT_ENABLED; |
||
2148 | |||
2149 | return;
|
||
2150 | } |
||
2151 | |||
2152 | /**
|
||
2153 | 243fb4e2 | Thomas Schöpping | * @brief Disable a AosShellChannel as output.
|
2154 | ba516b61 | Thomas Schöpping | *
|
2155 | * @param[in] channel The channel to disable as output.
|
||
2156 | */
|
||
2157 | void aosShellChannelOutputDisable(AosShellChannel* channel)
|
||
2158 | { |
||
2159 | dd8738ea | Thomas Schöpping | aosDbgCheck(channel != NULL && channel->asyncchannel != NULL); |
2160 | ba516b61 | Thomas Schöpping | |
2161 | channel->flags &= ~AOS_SHELLCHANNEL_OUTPUT_ENABLED; |
||
2162 | |||
2163 | return;
|
||
2164 | } |
||
2165 | |||
2166 | /**
|
||
2167 | e545e620 | Thomas Schöpping | * @brief Thread main function.
|
2168 | *
|
||
2169 | * @param[in] aosShellThread Name of the function;
|
||
2170 | * @param[in] shell Pointer to the shell object.
|
||
2171 | */
|
||
2172 | af4fd4a2 | Thomas Schöpping | void aosShellThread(void* shell) |
2173 | e545e620 | Thomas Schöpping | { |
2174 | aosDbgCheck(shell != NULL);
|
||
2175 | 697dba3c | Thomas Schöpping | aosDbgCheck(((aos_shell_t*)shell)->input.nentries > 0);
|
2176 | e545e620 | Thomas Schöpping | |
2177 | // local variables
|
||
2178 | ba516b61 | Thomas Schöpping | eventmask_t eventmask; |
2179 | eventflags_t eventflags; |
||
2180 | AosShellChannel* channel; |
||
2181 | 697dba3c | Thomas Schöpping | runtimedata_t rdata; |
2182 | 10fd7ac9 | Thomas Schöpping | char* args[((aos_shell_t*)shell)->input.nargs];
|
2183 | e545e620 | Thomas Schöpping | size_t nargs = 0;
|
2184 | ba516b61 | Thomas Schöpping | aos_shellcommand_t* cmd; |
2185 | |||
2186 | 697dba3c | Thomas Schöpping | // initialize variables and buffers
|
2187 | 3da12676 | Thomas Schöpping | #if (CH_CFG_USE_REGISTRY == TRUE)
|
2188 | currp->name = ((aos_shell_t*)shell)->name; |
||
2189 | #endif /* (CH_CFG_USE_REGISTRY == TRUE) */ |
||
2190 | 697dba3c | Thomas Schöpping | rdata.input.length = 0;
|
2191 | rdata.input.cursorpos = 0;
|
||
2192 | memset(rdata.input.escseq, '\0', AOS_SHELL_ESCSEQUENCE_LENGTH * sizeof(char)); |
||
2193 | rdata.buffer.current = 0;
|
||
2194 | rdata.buffer.selected = (((aos_shell_t*)shell)->input.nentries > 1) ? 1 : 0; |
||
2195 | rdata.buffer.edited = 0;
|
||
2196 | rdata.lastaction = ACTION_NONE; |
||
2197 | 10fd7ac9 | Thomas Schöpping | for (size_t arg = 0; arg < ((aos_shell_t*)shell)->input.nargs; ++arg) { |
2198 | args[arg] = NULL;
|
||
2199 | } |
||
2200 | 697dba3c | Thomas Schöpping | memset(_getCurrentEntry((aos_shell_t*)shell, &rdata), '\0', ((aos_shell_t*)shell)->input.linewidth * sizeof(char)); |
2201 | ba516b61 | Thomas Schöpping | |
2202 | // register OS related events
|
||
2203 | 697dba3c | Thomas Schöpping | chEvtRegisterMask(&aos.events.os, &(((aos_shell_t*)shell)->osEventListener), EVENTMASK_OS); |
2204 | ba516b61 | Thomas Schöpping | // register events to all input channels
|
2205 | for (channel = ((aos_shell_t*)shell)->stream.channel; channel != NULL; channel = channel->next) { |
||
2206 | 697dba3c | Thomas Schöpping | chEvtRegisterMaskWithFlags(&(channel->asyncchannel->event), &(channel->listener), EVENTMASK_INPUT, channel->listener.wflags); |
2207 | ba516b61 | Thomas Schöpping | } |
2208 | e545e620 | Thomas Schöpping | |
2209 | // fire start event
|
||
2210 | chEvtBroadcastFlags(&(((aos_shell_t*)shell)->eventSource), AOS_SHELL_EVTFLAG_START); |
||
2211 | |||
2212 | ba516b61 | Thomas Schöpping | // print the prompt for the first time
|
2213 | _printPrompt((aos_shell_t*)shell); |
||
2214 | |||
2215 | e545e620 | Thomas Schöpping | // enter thread loop
|
2216 | while (!chThdShouldTerminateX()) {
|
||
2217 | ba516b61 | Thomas Schöpping | // wait for event and handle it accordingly
|
2218 | eventmask = chEvtWaitOne(ALL_EVENTS); |
||
2219 | |||
2220 | // handle event
|
||
2221 | switch (eventmask) {
|
||
2222 | |||
2223 | // OS related events
|
||
2224 | 697dba3c | Thomas Schöpping | case EVENTMASK_OS:
|
2225 | ba516b61 | Thomas Schöpping | { |
2226 | 697dba3c | Thomas Schöpping | eventflags = chEvtGetAndClearFlags(&((aos_shell_t*)shell)->osEventListener); |
2227 | ba516b61 | Thomas Schöpping | // handle shutdown/restart events
|
2228 | cda14729 | Thomas Schöpping | if (eventflags & AOS_SYSTEM_EVENTFLAGS_SHUTDOWN_MASK) {
|
2229 | ba516b61 | Thomas Schöpping | chThdTerminate(((aos_shell_t*)shell)->thread); |
2230 | } else {
|
||
2231 | // print an error message
|
||
2232 | chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "\nERROR: unknown OS event received (0x%08X)\n", eventflags);
|
||
2233 | } |
||
2234 | e545e620 | Thomas Schöpping | break;
|
2235 | } |
||
2236 | |||
2237 | ba516b61 | Thomas Schöpping | // input events
|
2238 | 697dba3c | Thomas Schöpping | case EVENTMASK_INPUT:
|
2239 | ba516b61 | Thomas Schöpping | { |
2240 | // check and handle all channels
|
||
2241 | channel = ((aos_shell_t*)shell)->stream.channel; |
||
2242 | while (channel != NULL) { |
||
2243 | eventflags = chEvtGetAndClearFlags(&channel->listener); |
||
2244 | ae8211ea | Thomas Schöpping | // if there is new input and a command shall be executed
|
2245 | c18a848e | Thomas Schöpping | if (eventflags & CHN_INPUT_AVAILABLE) {
|
2246 | 697dba3c | Thomas Schöpping | _readChannel(shell, &rdata, channel); |
2247 | |||
2248 | // if an execution request was detected
|
||
2249 | if (rdata.lastaction == ACTION_EXECUTE) {
|
||
2250 | streamPut(&((aos_shell_t*)shell)->stream, '\n');
|
||
2251 | char* line = _getVisualisedEntry((aos_shell_t*)shell, &rdata);
|
||
2252 | |||
2253 | // skip, if there is nothing to be executed
|
||
2254 | if (line) {
|
||
2255 | // parse arguments
|
||
2256 | nargs = _parseArguments(shell, line, args); |
||
2257 | // check number of arguments found
|
||
2258 | if (nargs > ((aos_shell_t*)shell)->input.nargs) {
|
||
2259 | chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "\ttoo many arguments\n");
|
||
2260 | } else if (nargs > 0) { |
||
2261 | // search command list for arg[0] and execute callback
|
||
2262 | cmd = ((aos_shell_t*)shell)->commands; |
||
2263 | while (cmd != NULL) { |
||
2264 | // if the requested command has been found
|
||
2265 | if (strcmp(args[0], cmd->name) == 0) { |
||
2266 | ((aos_shell_t*)shell)->execstatus.command = cmd; |
||
2267 | chEvtBroadcastFlags(&((aos_shell_t*)shell)->eventSource, AOS_SHELL_EVTFLAG_EXECUTE); |
||
2268 | ((aos_shell_t*)shell)->execstatus.retval = cmd->callback((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, (int)nargs, args);
|
||
2269 | chEvtBroadcastFlags(&((aos_shell_t*)shell)->eventSource, AOS_SHELL_EVTFLAG_DONE); |
||
2270 | // notify user if execution of the command was not successful
|
||
2271 | if (((aos_shell_t*)shell)->execstatus.retval != 0) { |
||
2272 | chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "command returned exit status %d\n", ((aos_shell_t*)shell)->execstatus.retval);
|
||
2273 | } |
||
2274 | break;
|
||
2275 | } |
||
2276 | // keep searching
|
||
2277 | else {
|
||
2278 | cmd = cmd->next; |
||
2279 | c18a848e | Thomas Schöpping | } |
2280 | 697dba3c | Thomas Schöpping | } /* end of while */
|
2281 | |||
2282 | // if no matching command was found, print a message
|
||
2283 | if (cmd == NULL) { |
||
2284 | chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "\tcommand '%s' not found\n", args[0]); |
||
2285 | ba516b61 | Thomas Schöpping | } |
2286 | 697dba3c | Thomas Schöpping | } |
2287 | e545e620 | Thomas Schöpping | |
2288 | 697dba3c | Thomas Schöpping | // restore spaces in the current entry, which have been modified to NUL bytes due to argument parsing
|
2289 | _restoreWhitespace(line, rdata.input.length); |
||
2290 | |||
2291 | // update internat variables
|
||
2292 | { |
||
2293 | rdata.input.length = 0;
|
||
2294 | rdata.input.cursorpos = 0;
|
||
2295 | rdata.buffer.edited = 0;
|
||
2296 | |||
2297 | // if the input buffer can hold historic entries
|
||
2298 | if (((aos_shell_t*)shell)->input.nentries > 1) { |
||
2299 | // iterate in the history only if
|
||
2300 | // there was some valid input and
|
||
2301 | // the user did not execute the exact previous command again
|
||
2302 | if (nargs > 0 && |
||
2303 | strcmp(line, _getRelativeEntry((aos_shell_t*)shell, &rdata, 1)) != 0) { |
||
2304 | rdata.buffer.current = (rdata.buffer.current + 1) % ((aos_shell_t*)shell)->input.nentries;
|
||
2305 | } |
||
2306 | // clear and select next (now current) entry
|
||
2307 | memset(_getCurrentEntry((aos_shell_t*)shell, &rdata), '\0', ((aos_shell_t*)shell)->input.linewidth * sizeof(char)); |
||
2308 | rdata.buffer.selected = 1;
|
||
2309 | } |
||
2310 | // if there is only a single entry in the input buffer
|
||
2311 | else {
|
||
2312 | // do not clear/reset the entry, but start off with a cleared preview
|
||
2313 | rdata.buffer.selected = 0;
|
||
2314 | } |
||
2315 | c18a848e | Thomas Schöpping | } |
2316 | ba516b61 | Thomas Schöpping | } |
2317 | dd8738ea | Thomas Schöpping | |
2318 | 697dba3c | Thomas Schöpping | // print a new prompt
|
2319 | if (!chThdShouldTerminateX()) {
|
||
2320 | _printPrompt((aos_shell_t*)shell); |
||
2321 | } |
||
2322 | ba516b61 | Thomas Schöpping | } |
2323 | e545e620 | Thomas Schöpping | } |
2324 | ba516b61 | Thomas Schöpping | |
2325 | // iterate to next channel
|
||
2326 | channel = channel->next; |
||
2327 | e545e620 | Thomas Schöpping | } |
2328 | ba516b61 | Thomas Schöpping | break;
|
2329 | e545e620 | Thomas Schöpping | } |
2330 | ba516b61 | Thomas Schöpping | |
2331 | // other events
|
||
2332 | default:
|
||
2333 | { |
||
2334 | // print an error message
|
||
2335 | 1e5f7648 | Thomas Schöpping | chprintf((BaseSequentialStream*)&((aos_shell_t*)shell)->stream, "\nSHELL: ERROR: unknown event received (0x%08X)\n", eventmask);
|
2336 | ba516b61 | Thomas Schöpping | break;
|
2337 | e545e620 | Thomas Schöpping | } |
2338 | |||
2339 | ba516b61 | Thomas Schöpping | } /* end of switch */
|
2340 | |||
2341 | } /* end of while */
|
||
2342 | e545e620 | Thomas Schöpping | |
2343 | // fire event and exit the thread
|
||
2344 | chSysLock(); |
||
2345 | chEvtBroadcastFlagsI(&(((aos_shell_t*)shell)->eventSource), AOS_SHELL_EVTFLAG_EXIT); |
||
2346 | chThdExitS(MSG_OK); |
||
2347 | // no chSysUnlock() required since the thread has been terminated an all waiting threads have been woken up
|
||
2348 | } |
||
2349 | ba516b61 | Thomas Schöpping | |
2350 | cda14729 | Thomas Schöpping | #endif /* (AMIROOS_CFG_SHELL_ENABLE == true) */ |
2351 | 53710ca3 | Marc Rothmann | |
2352 | /** @} */ |