amiro-os / core / src / aos_shell.c @ efbf7cb1
History | View | Annotate | Download (47.146 KB)
| 1 | e545e620 | Thomas Schöpping | /*
|
|---|---|---|---|
| 2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
| 3 | 84f0ce9e | Thomas Schöpping | Copyright (C) 2016..2019 Thomas Schöpping et al.
|
| 4 | e545e620 | Thomas Schöpping | |
| 5 | This program is free software: you can redistribute it and/or modify
|
||
| 6 | it under the terms of the GNU General Public License as published by
|
||
| 7 | the Free Software Foundation, either version 3 of the License, or
|
||
| 8 | (at your option) any later version.
|
||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful,
|
||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 13 | GNU General Public License for more details.
|
||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License
|
||
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
| 17 | */
|
||
| 18 | |||
| 19 | 53710ca3 | Marc Rothmann | /**
|
| 20 | * @file aos_shell.c
|
||
| 21 | * @brief Shell code.
|
||
| 22 | * @details Shell code as well as shell related channels and streams.
|
||
| 23 | *
|
||
| 24 | * @addtogroup aos_shell
|
||
| 25 | * @{
|
||
| 26 | */
|
||
| 27 | |||
| 28 | e545e620 | Thomas Schöpping | #include <aos_shell.h> |
| 29 | |||
| 30 | 2dd2e257 | Thomas Schöpping | #if (AMIROOS_CFG_SHELL_ENABLE == true) || (AMIROOS_CFG_TESTS_ENABLE == true) |
| 31 | e545e620 | Thomas Schöpping | #include <aos_debug.h> |
| 32 | #include <aos_time.h> |
||
| 33 | #include <aos_system.h> |
||
| 34 | #include <string.h> |
||
| 35 | ba516b61 | Thomas Schöpping | |
| 36 | /**
|
||
| 37 | * @brief Event mask to be set on OS related events.
|
||
| 38 | */
|
||
| 39 | #define AOS_SHELL_EVENTMASK_OS EVENT_MASK(0) |
||
| 40 | |||
| 41 | /**
|
||
| 42 | * @brief Event mask to be set on a input event.
|
||
| 43 | */
|
||
| 44 | #define AOS_SHELL_EVENTMASK_INPUT EVENT_MASK(1) |
||
| 45 | |||
| 46 | /**
|
||
| 47 | * @brief Implementation of the BaseAsynchronous write() method (inherited from BaseSequentialStream).
|
||
| 48 | */
|
||
| 49 | static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n) |
||
| 50 | {
|
||
| 51 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 52 | dd8738ea | Thomas Schöpping | return streamWrite(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
| 53 | ba516b61 | Thomas Schöpping | } else {
|
| 54 | return 0; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | /**
|
||
| 59 | * @brief Implementation of the BaseAsynchronous read() method (inherited from BaseSequentialStream).
|
||
| 60 | */
|
||
| 61 | static size_t _channelread(void *instance, uint8_t *bp, size_t n) |
||
| 62 | {
|
||
| 63 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 64 | return streamRead(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
||
| 65 | } else {
|
||
| 66 | return 0; |
||
| 67 | } |
||
| 68 | ba516b61 | Thomas Schöpping | } |
| 69 | |||
| 70 | /**
|
||
| 71 | * @brief Implementation of the BaseAsynchronous put() method (inherited from BaseSequentialStream).
|
||
| 72 | */
|
||
| 73 | static msg_t _channelput(void *instance, uint8_t b) |
||
| 74 | {
|
||
| 75 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 76 | dd8738ea | Thomas Schöpping | return streamPut(((AosShellChannel*)instance)->asyncchannel, b);
|
| 77 | ba516b61 | Thomas Schöpping | } else {
|
| 78 | return MSG_RESET;
|
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | /**
|
||
| 83 | * @brief Implementation of the BaseAsynchronous get() method (inherited from BaseSequentialStream).
|
||
| 84 | */
|
||
| 85 | static msg_t _channelget(void *instance) |
||
| 86 | {
|
||
| 87 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 88 | return streamGet(((AosShellChannel*)instance)->asyncchannel);
|
||
| 89 | } else {
|
||
| 90 | return MSG_RESET;
|
||
| 91 | } |
||
| 92 | ba516b61 | Thomas Schöpping | } |
| 93 | |||
| 94 | /**
|
||
| 95 | * @brief Implementation of the BaseAsynchronous putt() method.
|
||
| 96 | */
|
||
| 97 | 2c99037f | Marc Rothmann | static msg_t _channelputt(void *instance, uint8_t b, sysinterval_t time) |
| 98 | ba516b61 | Thomas Schöpping | {
|
| 99 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 100 | dd8738ea | Thomas Schöpping | return chnPutTimeout(((AosShellChannel*)instance)->asyncchannel, b, time);
|
| 101 | ba516b61 | Thomas Schöpping | } else {
|
| 102 | return MSG_RESET;
|
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /**
|
||
| 107 | * @brief Implementation of the BaseAsynchronous gett() method.
|
||
| 108 | */
|
||
| 109 | 2c99037f | Marc Rothmann | static msg_t _channelgett(void *instance, sysinterval_t time) |
| 110 | ba516b61 | Thomas Schöpping | {
|
| 111 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 112 | return chnGetTimeout(((AosShellChannel*)instance)->asyncchannel, time);
|
||
| 113 | } else {
|
||
| 114 | return MSG_RESET;
|
||
| 115 | } |
||
| 116 | ba516b61 | Thomas Schöpping | } |
| 117 | |||
| 118 | /**
|
||
| 119 | * @brief Implementation of the BaseAsynchronous writet() method.
|
||
| 120 | */
|
||
| 121 | 2c99037f | Marc Rothmann | static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, sysinterval_t time) |
| 122 | ba516b61 | Thomas Schöpping | {
|
| 123 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 124 | dd8738ea | Thomas Schöpping | return chnWriteTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
| 125 | ba516b61 | Thomas Schöpping | } else {
|
| 126 | return 0; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | /**
|
||
| 131 | * @brief Implementation of the BaseAsynchronous readt() method.
|
||
| 132 | */
|
||
| 133 | 2c99037f | Marc Rothmann | static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, sysinterval_t time) |
| 134 | ba516b61 | Thomas Schöpping | {
|
| 135 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 136 | return chnReadTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
||
| 137 | } else {
|
||
| 138 | return 0; |
||
| 139 | } |
||
| 140 | ba516b61 | Thomas Schöpping | } |
| 141 | |||
| 142 | 2c99037f | Marc Rothmann | /**
|
| 143 | * @brief Implementation of the BaseAsynchronousChannel ctl() method.
|
||
| 144 | */
|
||
| 145 | static msg_t _channelctl(void *instance, unsigned int operation, void *arg) { |
||
| 146 | (void) instance;
|
||
| 147 | |||
| 148 | switch (operation) {
|
||
| 149 | case CHN_CTL_NOP:
|
||
| 150 | osalDbgCheck(arg == NULL);
|
||
| 151 | break;
|
||
| 152 | case CHN_CTL_INVALID:
|
||
| 153 | osalDbgAssert(false, "invalid CTL operation"); |
||
| 154 | break;
|
||
| 155 | default:
|
||
| 156 | break;
|
||
| 157 | } |
||
| 158 | return MSG_OK;
|
||
| 159 | } |
||
| 160 | |||
| 161 | ba516b61 | Thomas Schöpping | static const struct AosShellChannelVMT _channelvmt = { |
| 162 | 0128be0f | Marc Rothmann | (size_t) 0,
|
| 163 | ba516b61 | Thomas Schöpping | _channelwrite, |
| 164 | _channelread, |
||
| 165 | _channelput, |
||
| 166 | _channelget, |
||
| 167 | _channelputt, |
||
| 168 | _channelgett, |
||
| 169 | _channelwritet, |
||
| 170 | _channelreadt, |
||
| 171 | 2c99037f | Marc Rothmann | _channelctl, |
| 172 | ba516b61 | Thomas Schöpping | }; |
| 173 | |||
| 174 | static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n) |
||
| 175 | {
|
||
| 176 | aosDbgCheck(instance != NULL);
|
||
| 177 | |||
| 178 | // local variables
|
||
| 179 | AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
||
| 180 | size_t bytes; |
||
| 181 | size_t maxbytes = 0;
|
||
| 182 | |||
| 183 | // iterate through the list of channels
|
||
| 184 | while (channel != NULL) { |
||
| 185 | bytes = streamWrite(channel, bp, n); |
||
| 186 | maxbytes = (bytes > maxbytes) ? bytes : maxbytes; |
||
| 187 | channel = channel->next; |
||
| 188 | } |
||
| 189 | |||
| 190 | return maxbytes;
|
||
| 191 | } |
||
| 192 | |||
| 193 | static size_t _stremread(void *instance, uint8_t *bp, size_t n) |
||
| 194 | {
|
||
| 195 | (void)instance;
|
||
| 196 | (void)bp;
|
||
| 197 | (void)n;
|
||
| 198 | |||
| 199 | return 0; |
||
| 200 | } |
||
| 201 | |||
| 202 | static msg_t _streamput(void *instance, uint8_t b) |
||
| 203 | {
|
||
| 204 | aosDbgCheck(instance != NULL);
|
||
| 205 | |||
| 206 | // local variables
|
||
| 207 | AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
||
| 208 | dd8738ea | Thomas Schöpping | msg_t ret = MSG_OK; |
| 209 | ba516b61 | Thomas Schöpping | |
| 210 | // iterate through the list of channels
|
||
| 211 | while (channel != NULL) { |
||
| 212 | dd8738ea | Thomas Schöpping | msg_t ret_ = streamPut(channel, b); |
| 213 | ret = (ret_ < ret) ? ret_ : ret; |
||
| 214 | ba516b61 | Thomas Schöpping | channel = channel->next; |
| 215 | } |
||
| 216 | |||
| 217 | dd8738ea | Thomas Schöpping | return ret;
|
| 218 | ba516b61 | Thomas Schöpping | } |
| 219 | |||
| 220 | static msg_t _streamget(void *instance) |
||
| 221 | {
|
||
| 222 | (void)instance;
|
||
| 223 | |||
| 224 | return 0; |
||
| 225 | } |
||
| 226 | |||
| 227 | static const struct AosShellStreamVMT _streamvmt = { |
||
| 228 | 0128be0f | Marc Rothmann | (size_t) 0,
|
| 229 | ba516b61 | Thomas Schöpping | _streamwrite, |
| 230 | _stremread, |
||
| 231 | _streamput, |
||
| 232 | _streamget, |
||
| 233 | }; |
||
| 234 | |||
| 235 | e545e620 | Thomas Schöpping | /**
|
| 236 | * @brief Enumerator of special keyboard keys.
|
||
| 237 | */
|
||
| 238 | typedef enum special_key { |
||
| 239 | KEY_UNKNOWN, /**< any/unknow key */
|
||
| 240 | KEY_AMBIGUOUS, /**< key is ambiguous */
|
||
| 241 | KEY_TAB, /**< tabulator key */
|
||
| 242 | KEY_ESCAPE, /**< escape key */
|
||
| 243 | KEY_BACKSPACE, /**< backspace key */
|
||
| 244 | KEY_INSERT, /**< insert key */
|
||
| 245 | KEY_DELETE, /**< delete key */
|
||
| 246 | KEY_HOME, /**< home key */
|
||
| 247 | KEY_END, /**< end key */
|
||
| 248 | KEY_PAGE_UP, /**< page up key */
|
||
| 249 | KEY_PAGE_DOWN, /**< page down key */
|
||
| 250 | KEY_ARROW_UP, /**< arrow up key */
|
||
| 251 | KEY_ARROW_DOWN, /**< arrow down key */
|
||
| 252 | KEY_ARROW_LEFT, /**< arrow left key */
|
||
| 253 | KEY_ARROW_RIGHT, /**< arrow right key */
|
||
| 254 | } special_key_t; |
||
| 255 | |||
| 256 | /**
|
||
| 257 | * @brief Enumerator for case (in)sensitive character matching.
|
||
| 258 | */
|
||
| 259 | typedef enum charmatch { |
||
| 260 | CHAR_MATCH_NOT = 0, /**< Characters do not match at all. */ |
||
| 261 | CHAR_MATCH_NCASE = 1, /**< Characters would match case insensitive. */ |
||
| 262 | CHAR_MATCH_CASE = 2, /**< Characters do match with case. */ |
||
| 263 | } charmatch_t; |
||
| 264 | |||
| 265 | /**
|
||
| 266 | * @brief Print the shell prompt
|
||
| 267 | * @details Depending on the configuration flags, the system uptime is printed before the prompt string.
|
||
| 268 | *
|
||
| 269 | * @param[in] shell Pointer to the shell object.
|
||
| 270 | */
|
||
| 271 | static void _printPrompt(aos_shell_t* shell) |
||
| 272 | {
|
||
| 273 | aosDbgCheck(shell != NULL);
|
||
| 274 | |||
| 275 | 8399aeae | Thomas Schöpping | // print some time informattion before prompt if configured
|
| 276 | if (shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) {
|
||
| 277 | // printf the system uptime
|
||
| 278 | if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_UPTIME) {
|
||
| 279 | // get current system uptime
|
||
| 280 | aos_timestamp_t uptime; |
||
| 281 | aosSysGetUptime(&uptime); |
||
| 282 | |||
| 283 | chprintf((BaseSequentialStream*)&shell->stream, "[%01u:%02u:%02u:%02u:%03u:%03u] ",
|
||
| 284 | (uint32_t)(uptime / MICROSECONDS_PER_DAY), |
||
| 285 | (uint8_t)(uptime % MICROSECONDS_PER_DAY / MICROSECONDS_PER_HOUR), |
||
| 286 | (uint8_t)(uptime % MICROSECONDS_PER_HOUR / MICROSECONDS_PER_MINUTE), |
||
| 287 | (uint8_t)(uptime % MICROSECONDS_PER_MINUTE / MICROSECONDS_PER_SECOND), |
||
| 288 | (uint16_t)(uptime % MICROSECONDS_PER_SECOND / MICROSECONDS_PER_MILLISECOND), |
||
| 289 | (uint16_t)(uptime % MICROSECONDS_PER_MILLISECOND / MICROSECONDS_PER_MICROSECOND)); |
||
| 290 | } |
||
| 291 | else if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_DATETIME) { |
||
| 292 | // get current RTC time
|
||
| 293 | struct tm dt;
|
||
| 294 | aosSysGetDateTime(&dt); |
||
| 295 | chprintf((BaseSequentialStream*)&shell->stream, "[%02u-%02u-%04u|%02u:%02u:%02u] ",
|
||
| 296 | dt.tm_mday, |
||
| 297 | dt.tm_mon + 1,
|
||
| 298 | dt.tm_year + 1900,
|
||
| 299 | dt.tm_hour, |
||
| 300 | dt.tm_min, |
||
| 301 | dt.tm_sec); |
||
| 302 | } |
||
| 303 | else {
|
||
| 304 | aosDbgAssert(false);
|
||
| 305 | } |
||
| 306 | e545e620 | Thomas Schöpping | } |
| 307 | |||
| 308 | // print the actual prompt string
|
||
| 309 | if (shell->prompt && !(shell->config & AOS_SHELL_CONFIG_PROMPT_MINIMAL)) {
|
||
| 310 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "%s$ ", shell->prompt);
|
| 311 | e545e620 | Thomas Schöpping | } else {
|
| 312 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "%>$ ");
|
| 313 | e545e620 | Thomas Schöpping | } |
| 314 | |||
| 315 | return;
|
||
| 316 | } |
||
| 317 | |||
| 318 | /**
|
||
| 319 | * @brief Interprete a escape sequence
|
||
| 320 | *
|
||
| 321 | * @param[in] seq Character sequence to interprete.
|
||
| 322 | * Must be terminated by NUL byte.
|
||
| 323 | *
|
||
| 324 | * @return A @p special_key value.
|
||
| 325 | */
|
||
| 326 | static special_key_t _interpreteEscapeSequence(const char seq[]) |
||
| 327 | {
|
||
| 328 | // local variables
|
||
| 329 | bool ambiguous = false; |
||
| 330 | int cmp = 0; |
||
| 331 | |||
| 332 | // TAB
|
||
| 333 | /* not supported yet; use "\x09" instead */
|
||
| 334 | |||
| 335 | // BACKSPACE
|
||
| 336 | /* not supported yet; use "\x08" instead */
|
||
| 337 | |||
| 338 | // ESCAPE
|
||
| 339 | cmp = strcmp(seq, "\x1B");
|
||
| 340 | if (cmp == 0) { |
||
| 341 | return KEY_ESCAPE;
|
||
| 342 | } else {
|
||
| 343 | ambiguous |= (cmp < 0);
|
||
| 344 | } |
||
| 345 | |||
| 346 | // INSERT
|
||
| 347 | cmp = strcmp(seq, "\x1B\x5B\x32\x7E");
|
||
| 348 | if (cmp == 0) { |
||
| 349 | return KEY_INSERT;
|
||
| 350 | } else {
|
||
| 351 | ambiguous |= (cmp < 0);
|
||
| 352 | } |
||
| 353 | |||
| 354 | // DELETE
|
||
| 355 | cmp = strcmp(seq, "\x1B\x5B\x33\x7E");
|
||
| 356 | if (cmp == 0) { |
||
| 357 | return KEY_DELETE;
|
||
| 358 | } else {
|
||
| 359 | ambiguous |= (cmp < 0);
|
||
| 360 | } |
||
| 361 | |||
| 362 | // HOME
|
||
| 363 | cmp = strcmp(seq, "\x1B\x4F\x48");
|
||
| 364 | if (cmp == 0) { |
||
| 365 | return KEY_HOME;
|
||
| 366 | } else {
|
||
| 367 | ambiguous |= (cmp < 0);
|
||
| 368 | } |
||
| 369 | |||
| 370 | // END
|
||
| 371 | cmp = strcmp(seq, "\x1B\x4F\x46");
|
||
| 372 | if (cmp == 0) { |
||
| 373 | return KEY_END;
|
||
| 374 | } else {
|
||
| 375 | ambiguous |= (cmp < 0);
|
||
| 376 | } |
||
| 377 | |||
| 378 | // PAGE UP
|
||
| 379 | cmp = strcmp(seq, "\x1B\x5B\x35\x7E");
|
||
| 380 | if (cmp == 0) { |
||
| 381 | return KEY_PAGE_UP;
|
||
| 382 | } else {
|
||
| 383 | ambiguous |= (cmp < 0);
|
||
| 384 | } |
||
| 385 | |||
| 386 | // PAGE DOWN
|
||
| 387 | cmp = strcmp(seq, "\x1B\x5B\x36\x7E");
|
||
| 388 | if (cmp == 0) { |
||
| 389 | return KEY_PAGE_DOWN;
|
||
| 390 | } else {
|
||
| 391 | ambiguous |= (cmp < 0);
|
||
| 392 | } |
||
| 393 | |||
| 394 | // ARROW UP
|
||
| 395 | cmp = strcmp(seq, "\x1B\x5B\x41");
|
||
| 396 | if (cmp == 0) { |
||
| 397 | return KEY_ARROW_UP;
|
||
| 398 | } else {
|
||
| 399 | ambiguous |= (cmp < 0);
|
||
| 400 | } |
||
| 401 | |||
| 402 | // ARROW DOWN
|
||
| 403 | cmp = strcmp(seq, "\x1B\x5B\x42");
|
||
| 404 | if (cmp == 0) { |
||
| 405 | return KEY_ARROW_DOWN;
|
||
| 406 | } else {
|
||
| 407 | ambiguous |= (cmp < 0);
|
||
| 408 | } |
||
| 409 | |||
| 410 | // ARROW LEFT
|
||
| 411 | cmp = strcmp(seq, "\x1B\x5B\x44");
|
||
| 412 | if (cmp == 0) { |
||
| 413 | return KEY_ARROW_LEFT;
|
||
| 414 | } else {
|
||
| 415 | ambiguous |= (cmp < 0);
|
||
| 416 | } |
||
| 417 | |||
| 418 | // ARROW RIGHT
|
||
| 419 | cmp = strcmp(seq, "\x1B\x5B\x43");
|
||
| 420 | if (cmp == 0) { |
||
| 421 | return KEY_ARROW_RIGHT;
|
||
| 422 | } else {
|
||
| 423 | ambiguous |= (cmp < 0);
|
||
| 424 | } |
||
| 425 | |||
| 426 | return ambiguous ? KEY_AMBIGUOUS : KEY_UNKNOWN;
|
||
| 427 | } |
||
| 428 | |||
| 429 | /**
|
||
| 430 | * @brief Move the cursor in the terminal
|
||
| 431 | *
|
||
| 432 | * @param[in] shell Pointer to the shell object.
|
||
| 433 | * @param[in] from Starting position of the cursor.
|
||
| 434 | * @param[in] to Target position to move the cursor to.
|
||
| 435 | *
|
||
| 436 | * @return The number of positions moved.
|
||
| 437 | */
|
||
| 438 | static int _moveCursor(aos_shell_t* shell, const size_t from, const size_t to) |
||
| 439 | {
|
||
| 440 | aosDbgCheck(shell != NULL);
|
||
| 441 | |||
| 442 | // local variables
|
||
| 443 | size_t pos = from; |
||
| 444 | |||
| 445 | // move cursor left by printing backspaces
|
||
| 446 | while (pos > to) {
|
||
| 447 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, '\b');
|
| 448 | e545e620 | Thomas Schöpping | --pos; |
| 449 | } |
||
| 450 | |||
| 451 | // move cursor right by printing line content
|
||
| 452 | while (pos < to) {
|
||
| 453 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, shell->line[pos]); |
| 454 | e545e620 | Thomas Schöpping | ++pos; |
| 455 | } |
||
| 456 | |||
| 457 | return (int)pos - (int)from; |
||
| 458 | } |
||
| 459 | |||
| 460 | /**
|
||
| 461 | * @brief Print content of the shell line
|
||
| 462 | *
|
||
| 463 | * @param[in] shell Pointer to the shell object.
|
||
| 464 | * @param[in] from First position to start printing from.
|
||
| 465 | * @param[in] to Position after the last character to print.
|
||
| 466 | *
|
||
| 467 | * @return Number of characters printed.
|
||
| 468 | */
|
||
| 469 | static inline size_t _printLine(aos_shell_t* shell, const size_t from, const size_t to) |
||
| 470 | {
|
||
| 471 | aosDbgCheck(shell != NULL);
|
||
| 472 | |||
| 473 | // local variables
|
||
| 474 | size_t cnt; |
||
| 475 | |||
| 476 | for (cnt = 0; from + cnt < to; ++cnt) { |
||
| 477 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, shell->line[from + cnt]); |
| 478 | e545e620 | Thomas Schöpping | } |
| 479 | |||
| 480 | return cnt;
|
||
| 481 | } |
||
| 482 | |||
| 483 | /**
|
||
| 484 | * @brief Compare two characters.
|
||
| 485 | *
|
||
| 486 | * @param[in] lhs First character to compare.
|
||
| 487 | * @param[in] rhs Second character to compare.
|
||
| 488 | *
|
||
| 489 | * @return How well the characters match.
|
||
| 490 | */
|
||
| 491 | static inline charmatch_t _charcmp(char lhs, char rhs) |
||
| 492 | {
|
||
| 493 | // if lhs is a upper case letter and rhs is a lower case letter
|
||
| 494 | if (lhs >= 'A' && lhs <= 'Z' && rhs >= 'a' && rhs <= 'z') { |
||
| 495 | return (lhs == (rhs - 'a' + 'A')) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
||
| 496 | } |
||
| 497 | // if lhs is a lower case letter and rhs is a upper case letter
|
||
| 498 | else if (lhs >= 'a' && lhs <= 'z' && rhs >= 'A' && rhs <= 'Z') { |
||
| 499 | return ((lhs - 'a' + 'A') == rhs) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
||
| 500 | } |
||
| 501 | // default
|
||
| 502 | else {
|
||
| 503 | return (lhs == rhs) ? CHAR_MATCH_CASE : CHAR_MATCH_NOT;
|
||
| 504 | } |
||
| 505 | } |
||
| 506 | |||
| 507 | /**
|
||
| 508 | * @brief Maps an character from ASCII to a modified custom encoding.
|
||
| 509 | * @details The custom character encoding is very similar to ASCII and has the following structure:
|
||
| 510 | * 0x00=NULL ... 0x40='@' (identically to ASCII)
|
||
| 511 | * 0x4A='a'; 0x4B='A'; 0x4C='b'; 0x4D='B' ... 0x73='z'; 0x74='Z' (custom letter order)
|
||
| 512 | * 0x75='[' ... 0x7A='`' (0x5B..0x60 is ASCII)
|
||
| 513 | * 0x7B='{' ... 0x7F=DEL (identically to ASCII)
|
||
| 514 | *
|
||
| 515 | * @param[in] c Character to map to the custom encoding.
|
||
| 516 | *
|
||
| 517 | * @return The customly encoded character.
|
||
| 518 | */
|
||
| 519 | static inline char _mapAscii2Custom(const char c) |
||
| 520 | {
|
||
| 521 | if (c >= 'A' && c <= 'Z') { |
||
| 522 | return ((c - 'A') * 2) + 'A' + 1; |
||
| 523 | } else if (c > 'Z' && c < 'a') { |
||
| 524 | return c + ('z' - 'a') + 1; |
||
| 525 | } else if (c >= 'a' && c <= 'z') { |
||
| 526 | return ((c - 'a') * 2) + 'A'; |
||
| 527 | } else {
|
||
| 528 | return c;
|
||
| 529 | } |
||
| 530 | } |
||
| 531 | |||
| 532 | /**
|
||
| 533 | * @brief Compares two strings wrt letter case.
|
||
| 534 | * @details Comparisson uses a custom character encoding or mapping.
|
||
| 535 | * See @p _mapAscii2Custom for details.
|
||
| 536 | *
|
||
| 537 | * @param[in] str1 First string to compare.
|
||
| 538 | * @param[in] str2 Second string to compare.
|
||
| 539 | * @param[in] cs Flag indicating whether comparison shall be case sensitive.
|
||
| 540 | * @param[in,out] n Maximum number of character to compare (in) and number of matching characters (out).
|
||
| 541 | * If a null pointer is specified, this parameter is ignored.
|
||
| 542 | * If the value pointed to is zero, comarison will not be limited.
|
||
| 543 | * @param[out] m Optional indicator whether there was at least one case mismatch.
|
||
| 544 | *
|
||
| 545 | * @return Integer value indicating the relationship between the strings.
|
||
| 546 | * @retval <0 The first character that does not match has a lower value in str1 than in str2.
|
||
| 547 | * @retval 0 The contents of both strings are equal.
|
||
| 548 | * @retval >0 The first character that does not match has a greater value in str1 than in str2.
|
||
| 549 | */
|
||
| 550 | static int _strccmp(const char *str1, const char *str2, bool cs, size_t* n, charmatch_t* m) |
||
| 551 | {
|
||
| 552 | aosDbgCheck(str1 != NULL);
|
||
| 553 | aosDbgCheck(str2 != NULL);
|
||
| 554 | |||
| 555 | // initialize variables
|
||
| 556 | if (m) {
|
||
| 557 | *m = CHAR_MATCH_NOT; |
||
| 558 | } |
||
| 559 | size_t i = 0;
|
||
| 560 | |||
| 561 | // iterate through the strings
|
||
| 562 | while ((n == NULL) || (*n == 0) || (*n > 0 && i < *n)) { |
||
| 563 | // break on NUL
|
||
| 564 | if (str1[i] == '\0' || str2[i] == '\0') { |
||
| 565 | if (n) {
|
||
| 566 | *n = i; |
||
| 567 | } |
||
| 568 | break;
|
||
| 569 | } |
||
| 570 | // compare character
|
||
| 571 | const charmatch_t match = _charcmp(str1[i], str2[i]);
|
||
| 572 | if ((match == CHAR_MATCH_CASE) || (!cs && match == CHAR_MATCH_NCASE)) {
|
||
| 573 | if (m != NULL && *m != CHAR_MATCH_NCASE) { |
||
| 574 | *m = match; |
||
| 575 | } |
||
| 576 | ++i; |
||
| 577 | } else {
|
||
| 578 | if (n) {
|
||
| 579 | *n = i; |
||
| 580 | } |
||
| 581 | break;
|
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | return _mapAscii2Custom(str1[i]) - _mapAscii2Custom(str2[i]);
|
||
| 586 | } |
||
| 587 | |||
| 588 | 27286ba5 | Thomas Schöpping | /**
|
| 589 | * @brief Read input from a channel as long as there is data available.
|
||
| 590 | *
|
||
| 591 | * @param[in] shell Pointer to the shell object.
|
||
| 592 | * @param[in] channel The channel to read from.
|
||
| 593 | * @param[out] n Pointer to a variable to store the number of read characters to.
|
||
| 594 | *
|
||
| 595 | * @return
|
||
| 596 | */
|
||
| 597 | ba516b61 | Thomas Schöpping | static aos_status_t _readChannel(aos_shell_t* shell, AosShellChannel* channel, size_t* n)
|
| 598 | e545e620 | Thomas Schöpping | {
|
| 599 | aosDbgCheck(shell != NULL);
|
||
| 600 | ba516b61 | Thomas Schöpping | aosDbgCheck(channel != NULL);
|
| 601 | aosDbgCheck(n != NULL);
|
||
| 602 | e545e620 | Thomas Schöpping | |
| 603 | // local variables
|
||
| 604 | ba516b61 | Thomas Schöpping | aos_shellaction_t action = AOS_SHELL_ACTION_NONE; |
| 605 | e545e620 | Thomas Schöpping | char c;
|
| 606 | dd8738ea | Thomas Schöpping | special_key_t key; |
| 607 | e545e620 | Thomas Schöpping | |
| 608 | ba516b61 | Thomas Schöpping | // initialize output variables
|
| 609 | *n = 0;
|
||
| 610 | |||
| 611 | // read character by character from the channel
|
||
| 612 | while (chnReadTimeout(channel, (uint8_t*)&c, 1, TIME_IMMEDIATE)) { |
||
| 613 | dd8738ea | Thomas Schöpping | key = KEY_UNKNOWN; |
| 614 | e545e620 | Thomas Schöpping | |
| 615 | // parse escape sequence
|
||
| 616 | ba516b61 | Thomas Schöpping | if (shell->inputdata.escp > 0) { |
| 617 | shell->inputdata.escseq[shell->inputdata.escp] = c; |
||
| 618 | ++shell->inputdata.escp; |
||
| 619 | key = _interpreteEscapeSequence(shell->inputdata.escseq); |
||
| 620 | e545e620 | Thomas Schöpping | if (key == KEY_AMBIGUOUS) {
|
| 621 | // read next byte to resolve ambiguity
|
||
| 622 | continue;
|
||
| 623 | } else {
|
||
| 624 | ba516b61 | Thomas Schöpping | /*
|
| 625 | * If the escape sequence could either be parsed sucessfully
|
||
| 626 | * or there is no match (KEY_UNKNOWN),
|
||
| 627 | * reset the sequence variable and interprete key/character
|
||
| 628 | */
|
||
| 629 | shell->inputdata.escp = 0;
|
||
| 630 | memset(shell->inputdata.escseq, '\0', sizeof(shell->inputdata.escseq)*sizeof(shell->inputdata.escseq[0])); |
||
| 631 | e545e620 | Thomas Schöpping | } |
| 632 | } |
||
| 633 | |||
| 634 | ba516b61 | Thomas Schöpping | /* interprete keys or character */
|
| 635 | e545e620 | Thomas Schöpping | {
|
| 636 | ba516b61 | Thomas Schöpping | // default
|
| 637 | action = AOS_SHELL_ACTION_NONE; |
||
| 638 | |||
| 639 | // printable character
|
||
| 640 | if (key == KEY_UNKNOWN && c >= '\x20' && c <= '\x7E') { |
||
| 641 | action = AOS_SHELL_ACTION_READCHAR; |
||
| 642 | } |
||
| 643 | |||
| 644 | // tab key or character
|
||
| 645 | else if (key == KEY_TAB || c == '\x09') { |
||
| 646 | /*
|
||
| 647 | * pressing tab once applies auto fill
|
||
| 648 | * pressing tab a second time prints suggestions
|
||
| 649 | */
|
||
| 650 | if (shell->inputdata.lastaction == AOS_SHELL_ACTION_AUTOFILL || shell->inputdata.lastaction == AOS_SHELL_ACTION_SUGGEST) {
|
||
| 651 | action = AOS_SHELL_ACTION_SUGGEST; |
||
| 652 | e545e620 | Thomas Schöpping | } else {
|
| 653 | ba516b61 | Thomas Schöpping | action = AOS_SHELL_ACTION_AUTOFILL; |
| 654 | e545e620 | Thomas Schöpping | } |
| 655 | ba516b61 | Thomas Schöpping | } |
| 656 | |||
| 657 | // INS key
|
||
| 658 | else if (key == KEY_INSERT) { |
||
| 659 | action = AOS_SHELL_ACTION_INSERTTOGGLE; |
||
| 660 | } |
||
| 661 | |||
| 662 | // DEL key or character
|
||
| 663 | else if (key == KEY_DELETE || c == '\x7F') { |
||
| 664 | // ignore if cursor is at very right
|
||
| 665 | if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
||
| 666 | action = AOS_SHELL_ACTION_DELETEFORWARD; |
||
| 667 | e545e620 | Thomas Schöpping | } |
| 668 | ba516b61 | Thomas Schöpping | } |
| 669 | |||
| 670 | // backspace key or character
|
||
| 671 | else if (key == KEY_BACKSPACE || c == '\x08') { |
||
| 672 | // ignore if cursor is at very left
|
||
| 673 | if (shell->inputdata.cursorpos > 0) { |
||
| 674 | action = AOS_SHELL_ACTION_DELETEBACKWARD; |
||
| 675 | e545e620 | Thomas Schöpping | } |
| 676 | ba516b61 | Thomas Schöpping | } |
| 677 | |||
| 678 | // 'page up' of 'arrow up' key
|
||
| 679 | else if (key == KEY_PAGE_UP || key == KEY_ARROW_UP) { |
||
| 680 | e545e620 | Thomas Schöpping | // ignore if there was some input
|
| 681 | ba516b61 | Thomas Schöpping | if (shell->inputdata.noinput) {
|
| 682 | action = AOS_SHELL_ACTION_RECALLLAST; |
||
| 683 | e545e620 | Thomas Schöpping | } |
| 684 | ba516b61 | Thomas Schöpping | } |
| 685 | |||
| 686 | // 'page down' key, 'arrow done' key, 'end of test' character or 'end of transmission' character
|
||
| 687 | else if (key == KEY_PAGE_DOWN || key == KEY_ARROW_DOWN || c == '\x03' || c == '\x03') { |
||
| 688 | e545e620 | Thomas Schöpping | // ignore if line is empty
|
| 689 | ba516b61 | Thomas Schöpping | if (shell->inputdata.lineend > 0) { |
| 690 | action = AOS_SHELL_ACTION_CLEAR; |
||
| 691 | e545e620 | Thomas Schöpping | } |
| 692 | ba516b61 | Thomas Schöpping | } |
| 693 | |||
| 694 | // 'home' key
|
||
| 695 | else if (key == KEY_HOME) { |
||
| 696 | e545e620 | Thomas Schöpping | // ignore if cursor is very left
|
| 697 | ba516b61 | Thomas Schöpping | if (shell->inputdata.cursorpos > 0) { |
| 698 | action = AOS_SHELL_ACTION_CURSOR2START; |
||
| 699 | e545e620 | Thomas Schöpping | } |
| 700 | ba516b61 | Thomas Schöpping | } |
| 701 | |||
| 702 | // 'end' key
|
||
| 703 | else if (key == KEY_END) { |
||
| 704 | // ignore if cursos is very right
|
||
| 705 | if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
||
| 706 | action = AOS_SHELL_ACTION_CURSOR2END; |
||
| 707 | e545e620 | Thomas Schöpping | } |
| 708 | ba516b61 | Thomas Schöpping | } |
| 709 | |||
| 710 | // 'arrow left' key
|
||
| 711 | else if (key == KEY_ARROW_LEFT) { |
||
| 712 | e545e620 | Thomas Schöpping | // ignore if cursor is very left
|
| 713 | ba516b61 | Thomas Schöpping | if (shell->inputdata.cursorpos > 0) { |
| 714 | action = AOS_SHELL_ACTION_CURSORLEFT; |
||
| 715 | e545e620 | Thomas Schöpping | } |
| 716 | ba516b61 | Thomas Schöpping | } |
| 717 | |||
| 718 | // 'arrow right' key
|
||
| 719 | else if (key == KEY_ARROW_RIGHT) { |
||
| 720 | // irgnore if cursor is very right
|
||
| 721 | if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
||
| 722 | action = AOS_SHELL_ACTION_CURSORRIGHT; |
||
| 723 | e545e620 | Thomas Schöpping | } |
| 724 | ba516b61 | Thomas Schöpping | } |
| 725 | |||
| 726 | // carriage return ('\r') or line feed ('\n') character
|
||
| 727 | else if (c == '\x0D' || c == '\x0A') { |
||
| 728 | action = AOS_SHELL_ACTION_EXECUTE; |
||
| 729 | } |
||
| 730 | |||
| 731 | // ESC key or [ESCAPE] character
|
||
| 732 | else if (key == KEY_ESCAPE || c == '\x1B') { |
||
| 733 | action = AOS_SHELL_ACTION_ESCSTART; |
||
| 734 | e545e620 | Thomas Schöpping | } |
| 735 | } |
||
| 736 | |||
| 737 | /* handle function */
|
||
| 738 | ba516b61 | Thomas Schöpping | switch (action) {
|
| 739 | case AOS_SHELL_ACTION_READCHAR:
|
||
| 740 | {
|
||
| 741 | e545e620 | Thomas Schöpping | // line is full
|
| 742 | ba516b61 | Thomas Schöpping | if (shell->inputdata.lineend + 1 >= shell->linesize) { |
| 743 | _moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
||
| 744 | chprintf((BaseSequentialStream*)&shell->stream, "\n\tmaximum line width reached\n");
|
||
| 745 | e545e620 | Thomas Schöpping | _printPrompt(shell); |
| 746 | ba516b61 | Thomas Schöpping | _printLine(shell, 0, shell->inputdata.lineend);
|
| 747 | _moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
||
| 748 | e545e620 | Thomas Schöpping | } |
| 749 | // read character
|
||
| 750 | else {
|
||
| 751 | // clear old line content on first input
|
||
| 752 | ba516b61 | Thomas Schöpping | if (shell->inputdata.noinput) {
|
| 753 | e545e620 | Thomas Schöpping | memset(shell->line, '\0', shell->linesize);
|
| 754 | ba516b61 | Thomas Schöpping | shell->inputdata.noinput = false;
|
| 755 | e545e620 | Thomas Schöpping | } |
| 756 | // overwrite content
|
||
| 757 | if (shell->config & AOS_SHELL_CONFIG_INPUT_OVERWRITE) {
|
||
| 758 | ba516b61 | Thomas Schöpping | shell->line[shell->inputdata.cursorpos] = c; |
| 759 | ++shell->inputdata.cursorpos; |
||
| 760 | shell->inputdata.lineend = (shell->inputdata.cursorpos > shell->inputdata.lineend) ? shell->inputdata.cursorpos : shell->inputdata.lineend; |
||
| 761 | streamPut(&shell->stream, (uint8_t)c); |
||
| 762 | e545e620 | Thomas Schöpping | } |
| 763 | // insert character
|
||
| 764 | else {
|
||
| 765 | ba516b61 | Thomas Schöpping | memmove(&(shell->line[shell->inputdata.cursorpos+1]), &(shell->line[shell->inputdata.cursorpos]), shell->inputdata.lineend - shell->inputdata.cursorpos);
|
| 766 | shell->line[shell->inputdata.cursorpos] = c; |
||
| 767 | ++shell->inputdata.lineend; |
||
| 768 | _printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
||
| 769 | ++shell->inputdata.cursorpos; |
||
| 770 | _moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
||
| 771 | e545e620 | Thomas Schöpping | } |
| 772 | } |
||
| 773 | break;
|
||
| 774 | ba516b61 | Thomas Schöpping | } |
| 775 | e545e620 | Thomas Schöpping | |
| 776 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_AUTOFILL:
|
| 777 | e545e620 | Thomas Schöpping | {
|
| 778 | const char* fill = shell->line; |
||
| 779 | ba516b61 | Thomas Schöpping | size_t cmatch = shell->inputdata.cursorpos; |
| 780 | e545e620 | Thomas Schöpping | charmatch_t matchlevel = CHAR_MATCH_NOT; |
| 781 | size_t n; |
||
| 782 | // iterate through command list
|
||
| 783 | for (aos_shellcommand_t* cmd = shell->commands; cmd != NULL; cmd = cmd->next) { |
||
| 784 | // compare current match with command
|
||
| 785 | n = cmatch; |
||
| 786 | charmatch_t mlvl = CHAR_MATCH_NOT; |
||
| 787 | _strccmp(fill, cmd->name, shell->config & AOS_SHELL_CONFIG_MATCH_CASE, (n == 0) ? NULL : &n, &mlvl); |
||
| 788 | const int cmp = (n < cmatch) ? |
||
| 789 | (n - cmatch) : |
||
| 790 | (cmd->name[n] != '\0') ?
|
||
| 791 | strlen(cmd->name) - n : |
||
| 792 | 0;
|
||
| 793 | // if an exact match was found
|
||
| 794 | ba516b61 | Thomas Schöpping | if (cmatch + cmp == shell->inputdata.cursorpos) {
|
| 795 | cmatch = shell->inputdata.cursorpos; |
||
| 796 | e545e620 | Thomas Schöpping | fill = cmd->name; |
| 797 | // break the loop only if there are no case mismatches with the input
|
||
| 798 | ba516b61 | Thomas Schöpping | n = shell->inputdata.cursorpos; |
| 799 | e545e620 | Thomas Schöpping | _strccmp(fill, shell->line, false, &n, &mlvl);
|
| 800 | if (mlvl == CHAR_MATCH_CASE) {
|
||
| 801 | break;
|
||
| 802 | } |
||
| 803 | } |
||
| 804 | // if a not exact match was found
|
||
| 805 | ba516b61 | Thomas Schöpping | else if (cmatch + cmp > shell->inputdata.cursorpos) { |
| 806 | e545e620 | Thomas Schöpping | // if this is the first one
|
| 807 | if (fill == shell->line) {
|
||
| 808 | cmatch += cmp; |
||
| 809 | fill = cmd->name; |
||
| 810 | } |
||
| 811 | // if this is a worse one
|
||
| 812 | else if ((cmp < 0) || (cmp == 0 && mlvl == CHAR_MATCH_CASE)) { |
||
| 813 | cmatch += cmp; |
||
| 814 | } |
||
| 815 | } |
||
| 816 | // non matching commands are ignored
|
||
| 817 | else {}
|
||
| 818 | } |
||
| 819 | // evaluate if there are case mismatches
|
||
| 820 | n = cmatch; |
||
| 821 | _strccmp(shell->line, fill, shell->config & AOS_SHELL_CONFIG_MATCH_CASE, &n, &matchlevel); |
||
| 822 | // print the auto fill if any
|
||
| 823 | ba516b61 | Thomas Schöpping | if (cmatch > shell->inputdata.cursorpos || (cmatch == shell->inputdata.cursorpos && matchlevel == CHAR_MATCH_NCASE)) {
|
| 824 | shell->inputdata.noinput = false;
|
||
| 825 | e545e620 | Thomas Schöpping | // limit auto fill so it will not overflow the line width
|
| 826 | ba516b61 | Thomas Schöpping | if (shell->inputdata.lineend + (cmatch - shell->inputdata.cursorpos) > shell->linesize) {
|
| 827 | cmatch = shell->linesize - shell->inputdata.lineend + shell->inputdata.cursorpos; |
||
| 828 | e545e620 | Thomas Schöpping | } |
| 829 | // move trailing memory further in the line
|
||
| 830 | ba516b61 | Thomas Schöpping | memmove(&(shell->line[cmatch]), &(shell->line[shell->inputdata.cursorpos]), shell->inputdata.lineend - shell->inputdata.cursorpos); |
| 831 | shell->inputdata.lineend += cmatch - shell->inputdata.cursorpos; |
||
| 832 | e545e620 | Thomas Schöpping | // if there was no incorrect case when matching
|
| 833 | if (matchlevel == CHAR_MATCH_CASE) {
|
||
| 834 | // insert fill command name to line
|
||
| 835 | ba516b61 | Thomas Schöpping | memcpy(&(shell->line[shell->inputdata.cursorpos]), &(fill[shell->inputdata.cursorpos]), cmatch - shell->inputdata.cursorpos); |
| 836 | e545e620 | Thomas Schöpping | // print the output
|
| 837 | ba516b61 | Thomas Schöpping | _printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
| 838 | e545e620 | Thomas Schöpping | } else {
|
| 839 | // overwrite line with fill command name
|
||
| 840 | memcpy(shell->line, fill, cmatch); |
||
| 841 | // reprint the whole line
|
||
| 842 | ba516b61 | Thomas Schöpping | _moveCursor(shell, shell->inputdata.cursorpos, 0);
|
| 843 | _printLine(shell, 0, shell->inputdata.lineend);
|
||
| 844 | e545e620 | Thomas Schöpping | } |
| 845 | // move cursor to the end of the matching sequence
|
||
| 846 | ba516b61 | Thomas Schöpping | shell->inputdata.cursorpos = cmatch; |
| 847 | _moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
||
| 848 | e545e620 | Thomas Schöpping | } |
| 849 | break;
|
||
| 850 | } |
||
| 851 | |||
| 852 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_SUGGEST:
|
| 853 | e545e620 | Thomas Schöpping | {
|
| 854 | unsigned int matches = 0; |
||
| 855 | // iterate through command list
|
||
| 856 | for (aos_shellcommand_t* cmd = shell->commands; cmd != NULL; cmd = cmd->next) { |
||
| 857 | // compare line content with command, excpet if cursorpos=0
|
||
| 858 | ba516b61 | Thomas Schöpping | size_t i = shell->inputdata.cursorpos; |
| 859 | if (shell->inputdata.cursorpos > 0) { |
||
| 860 | e545e620 | Thomas Schöpping | _strccmp(shell->line, cmd->name, true, &i, NULL); |
| 861 | } |
||
| 862 | ba516b61 | Thomas Schöpping | const int cmp = (i < shell->inputdata.cursorpos) ? |
| 863 | (i - shell->inputdata.cursorpos) : |
||
| 864 | e545e620 | Thomas Schöpping | (cmd->name[i] != '\0') ?
|
| 865 | strlen(cmd->name) - i : |
||
| 866 | 0;
|
||
| 867 | // if a match was found
|
||
| 868 | if (cmp > 0) { |
||
| 869 | // if this is the first one
|
||
| 870 | if (matches == 0) { |
||
| 871 | ba516b61 | Thomas Schöpping | _moveCursor(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
| 872 | streamPut(&shell->stream, '\n');
|
||
| 873 | e545e620 | Thomas Schöpping | } |
| 874 | // print the command
|
||
| 875 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "\t%s\n", cmd->name);
|
| 876 | e545e620 | Thomas Schöpping | ++matches; |
| 877 | } |
||
| 878 | } |
||
| 879 | // reprint the prompt and line if any matches have been found
|
||
| 880 | if (matches > 0) { |
||
| 881 | _printPrompt(shell); |
||
| 882 | ba516b61 | Thomas Schöpping | _printLine(shell, 0, shell->inputdata.lineend);
|
| 883 | _moveCursor(shell, shell->inputdata.lineend, shell->inputdata.cursorpos); |
||
| 884 | shell->inputdata.noinput = false;
|
||
| 885 | e545e620 | Thomas Schöpping | } |
| 886 | break;
|
||
| 887 | } |
||
| 888 | |||
| 889 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_INSERTTOGGLE:
|
| 890 | {
|
||
| 891 | e545e620 | Thomas Schöpping | if (shell->config & AOS_SHELL_CONFIG_INPUT_OVERWRITE) {
|
| 892 | shell->config &= ~AOS_SHELL_CONFIG_INPUT_OVERWRITE; |
||
| 893 | } else {
|
||
| 894 | shell->config |= AOS_SHELL_CONFIG_INPUT_OVERWRITE; |
||
| 895 | } |
||
| 896 | break;
|
||
| 897 | ba516b61 | Thomas Schöpping | } |
| 898 | e545e620 | Thomas Schöpping | |
| 899 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_DELETEFORWARD:
|
| 900 | {
|
||
| 901 | --shell->inputdata.lineend; |
||
| 902 | memmove(&(shell->line[shell->inputdata.cursorpos]), &(shell->line[shell->inputdata.cursorpos+1]), shell->inputdata.lineend - shell->inputdata.cursorpos);
|
||
| 903 | _printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
||
| 904 | streamPut(&shell->stream, ' ');
|
||
| 905 | _moveCursor(shell, shell->inputdata.lineend + 1, shell->inputdata.cursorpos);
|
||
| 906 | e545e620 | Thomas Schöpping | break;
|
| 907 | ba516b61 | Thomas Schöpping | } |
| 908 | e545e620 | Thomas Schöpping | |
| 909 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_DELETEBACKWARD:
|
| 910 | {
|
||
| 911 | --shell->inputdata.cursorpos; |
||
| 912 | memmove(&(shell->line[shell->inputdata.cursorpos]), &(shell->line[shell->inputdata.cursorpos+1]), shell->inputdata.lineend - shell->inputdata.cursorpos);
|
||
| 913 | --shell->inputdata.lineend; |
||
| 914 | shell->line[shell->inputdata.lineend] = '\0';
|
||
| 915 | _moveCursor(shell, shell->inputdata.cursorpos + 1, shell->inputdata.cursorpos);
|
||
| 916 | _printLine(shell, shell->inputdata.cursorpos, shell->inputdata.lineend); |
||
| 917 | streamPut(&shell->stream, ' ');
|
||
| 918 | _moveCursor(shell, shell->inputdata.lineend+1, shell->inputdata.cursorpos);
|
||
| 919 | e545e620 | Thomas Schöpping | break;
|
| 920 | ba516b61 | Thomas Schöpping | } |
| 921 | e545e620 | Thomas Schöpping | |
| 922 | ba516b61 | Thomas Schöpping | case AOS_SHELL_ACTION_RECALLLAST:
|
| 923 | e545e620 | Thomas Schöpping | {
|
| 924 | // replace any intermediate NUL bytes with spaces
|
||
| 925 | ba516b61 | Thomas Schöpping | shell->inputdata.lineend = 0;
|
| 926 | e545e620 | Thomas Schöpping | size_t nul_start = 0;
|
| 927 | size_t nul_end = 0;
|
||
| 928 | // search line for a NUL byte
|
||
| 929 | while (nul_start < shell->linesize) {
|
||
| 930 | if (shell->line[nul_start] == '\0') { |
||
| 931 | nul_end = nul_start + 1;
|
||
| 932 | // keep searcjing for a byte that is not NUL
|
||
| 933 | while (nul_end < shell->linesize) {
|
||
| 934 | if (shell->line[nul_end] != '\0') { |
||
| 935 | // an intermediate NUL sequence was found
|
||
| 936 | memset(&(shell->line[nul_start]), ' ', nul_end - nul_start);
|
||
| 937 | ba516b61 | Thomas Schöpping | shell->inputdata.lineend = nul_end + 1;
|
| 938 | e545e620 | Thomas Schöpping | break;
|
| 939 | } else {
|
||
| 940 | ++nul_end; |
||
| 941 | } |
||
| 942 | } |
||
| 943 | nul_start = nul_end + 1;
|
||
| 944 | } else {
|
||
| 945 | ba516b61 | Thomas Schöpping | ++shell->inputdata.lineend; |
| 946 | e545e620 | Thomas Schöpping | ++nul_start; |
| 947 | } |
||
| 948 | } |
||
| 949 | ba516b61 | Thomas Schöpping | shell->inputdata.cursorpos = shell->inputdata.lineend; |
| 950 | e545e620 | Thomas Schöpping | // print the line
|
| 951 | ba516b61 | Thomas Schöpping | shell->inputdata.noinput = _printLine(shell, 0, shell->inputdata.lineend) == 0; |
| 952 | e545e620 | Thomas Schöpping | break;
|
| 953 | } |
||
| 954 | |||
| 955 | ba516b61 |