amiro-os / core / src / aos_shell.c @ 27d0378b
History | View | Annotate | Download (46.846 KB)
| 1 | e545e620 | Thomas Schöpping | /*
|
|---|---|---|---|
| 2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
| 3 | Copyright (C) 2016..2018 Thomas Schöpping et al.
|
||
| 4 | |||
| 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 | ba516b61 | Thomas Schöpping | #if (AMIROOS_CFG_SHELL_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 | 27d0378b | Simon Welzel | /** commented out by simon welzel
|
| 274 | e545e620 | Thomas Schöpping | aosDbgCheck(shell != NULL);
|
| 275 | |||
| 276 | 8399aeae | Thomas Schöpping | // print some time informattion before prompt if configured
|
| 277 | if (shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) {
|
||
| 278 | // printf the system uptime
|
||
| 279 | if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_UPTIME) {
|
||
| 280 | // get current system uptime
|
||
| 281 | aos_timestamp_t uptime;
|
||
| 282 | aosSysGetUptime(&uptime);
|
||
| 283 | |||
| 284 | chprintf((BaseSequentialStream*)&shell->stream, "[%01u:%02u:%02u:%02u:%03u:%03u] ",
|
||
| 285 | (uint32_t)(uptime / MICROSECONDS_PER_DAY),
|
||
| 286 | (uint8_t)(uptime % MICROSECONDS_PER_DAY / MICROSECONDS_PER_HOUR),
|
||
| 287 | (uint8_t)(uptime % MICROSECONDS_PER_HOUR / MICROSECONDS_PER_MINUTE),
|
||
| 288 | (uint8_t)(uptime % MICROSECONDS_PER_MINUTE / MICROSECONDS_PER_SECOND),
|
||
| 289 | (uint16_t)(uptime % MICROSECONDS_PER_SECOND / MICROSECONDS_PER_MILLISECOND),
|
||
| 290 | (uint16_t)(uptime % MICROSECONDS_PER_MILLISECOND / MICROSECONDS_PER_MICROSECOND));
|
||
| 291 | }
|
||
| 292 | else if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_DATETIME) {
|
||
| 293 | // get current RTC time
|
||
| 294 | struct tm dt;
|
||
| 295 | aosSysGetDateTime(&dt);
|
||
| 296 | chprintf((BaseSequentialStream*)&shell->stream, "[%02u-%02u-%04u|%02u:%02u:%02u] ",
|
||
| 297 | dt.tm_mday,
|
||
| 298 | dt.tm_mon + 1,
|
||
| 299 | dt.tm_year + 1900,
|
||
| 300 | dt.tm_hour,
|
||
| 301 | dt.tm_min,
|
||
| 302 | dt.tm_sec);
|
||
| 303 | }
|
||
| 304 | else {
|
||
| 305 | aosDbgAssert(false);
|
||
| 306 | }
|
||
| 307 | e545e620 | Thomas Schöpping | }
|
| 308 | |||
| 309 | // print the actual prompt string
|
||
| 310 | if (shell->prompt && !(shell->config & AOS_SHELL_CONFIG_PROMPT_MINIMAL)) {
|
||
| 311 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "%s$ ", shell->prompt);
|
| 312 | e545e620 | Thomas Schöpping | } else {
|
| 313 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "%>$ ");
|
| 314 | e545e620 | Thomas Schöpping | }
|
| 315 | 27d0378b | Simon Welzel | */
|
| 316 | e545e620 | Thomas Schöpping | return;
|
| 317 | } |
||
| 318 | |||
| 319 | /**
|
||
| 320 | * @brief Interprete a escape sequence
|
||
| 321 | *
|
||
| 322 | * @param[in] seq Character sequence to interprete.
|
||
| 323 | * Must be terminated by NUL byte.
|
||
| 324 | *
|
||
| 325 | * @return A @p special_key value.
|
||
| 326 | */
|
||
| 327 | static special_key_t _interpreteEscapeSequence(const char seq[]) |
||
| 328 | {
|
||
| 329 | // local variables
|
||
| 330 | bool ambiguous = false; |
||
| 331 | int cmp = 0; |
||
| 332 | |||
| 333 | // TAB
|
||
| 334 | /* not supported yet; use "\x09" instead */
|
||
| 335 | |||
| 336 | // BACKSPACE
|
||
| 337 | /* not supported yet; use "\x08" instead */
|
||
| 338 | |||
| 339 | // ESCAPE
|
||
| 340 | cmp = strcmp(seq, "\x1B");
|
||
| 341 | if (cmp == 0) { |
||
| 342 | return KEY_ESCAPE;
|
||
| 343 | } else {
|
||
| 344 | ambiguous |= (cmp < 0);
|
||
| 345 | } |
||
| 346 | |||
| 347 | // INSERT
|
||
| 348 | cmp = strcmp(seq, "\x1B\x5B\x32\x7E");
|
||
| 349 | if (cmp == 0) { |
||
| 350 | return KEY_INSERT;
|
||
| 351 | } else {
|
||
| 352 | ambiguous |= (cmp < 0);
|
||
| 353 | } |
||
| 354 | |||
| 355 | // DELETE
|
||
| 356 | cmp = strcmp(seq, "\x1B\x5B\x33\x7E");
|
||
| 357 | if (cmp == 0) { |
||
| 358 | return KEY_DELETE;
|
||
| 359 | } else {
|
||
| 360 | ambiguous |= (cmp < 0);
|
||
| 361 | } |
||
| 362 | |||
| 363 | // HOME
|
||
| 364 | cmp = strcmp(seq, "\x1B\x4F\x48");
|
||
| 365 | if (cmp == 0) { |
||
| 366 | return KEY_HOME;
|
||
| 367 | } else {
|
||
| 368 | ambiguous |= (cmp < 0);
|
||
| 369 | } |
||
| 370 | |||
| 371 | // END
|
||
| 372 | cmp = strcmp(seq, "\x1B\x4F\x46");
|
||
| 373 | if (cmp == 0) { |
||
| 374 | return KEY_END;
|
||
| 375 | } else {
|
||
| 376 | ambiguous |= (cmp < 0);
|
||
| 377 | } |
||
| 378 | |||
| 379 | // PAGE UP
|
||
| 380 | cmp = strcmp(seq, "\x1B\x5B\x35\x7E");
|
||
| 381 | if (cmp == 0) { |
||
| 382 | return KEY_PAGE_UP;
|
||
| 383 | } else {
|
||
| 384 | ambiguous |= (cmp < 0);
|
||
| 385 | } |
||
| 386 | |||
| 387 | // PAGE DOWN
|
||
| 388 | cmp = strcmp(seq, "\x1B\x5B\x36\x7E");
|
||
| 389 | if (cmp == 0) { |
||
| 390 | return KEY_PAGE_DOWN;
|
||
| 391 | } else {
|
||
| 392 | ambiguous |= (cmp < 0);
|
||
| 393 | } |
||
| 394 | |||
| 395 | // ARROW UP
|
||
| 396 | cmp = strcmp(seq, "\x1B\x5B\x41");
|
||
| 397 | if (cmp == 0) { |
||
| 398 | return KEY_ARROW_UP;
|
||
| 399 | } else {
|
||
| 400 | ambiguous |= (cmp < 0);
|
||
| 401 | } |
||
| 402 | |||
| 403 | // ARROW DOWN
|
||
| 404 | cmp = strcmp(seq, "\x1B\x5B\x42");
|
||
| 405 | if (cmp == 0) { |
||
| 406 | return KEY_ARROW_DOWN;
|
||
| 407 | } else {
|
||
| 408 | ambiguous |= (cmp < 0);
|
||
| 409 | } |
||
| 410 | |||
| 411 | // ARROW LEFT
|
||
| 412 | cmp = strcmp(seq, "\x1B\x5B\x44");
|
||
| 413 | if (cmp == 0) { |
||
| 414 | return KEY_ARROW_LEFT;
|
||
| 415 | } else {
|
||
| 416 | ambiguous |= (cmp < 0);
|
||
| 417 | } |
||
| 418 | |||
| 419 | // ARROW RIGHT
|
||
| 420 | cmp = strcmp(seq, "\x1B\x5B\x43");
|
||
| 421 | if (cmp == 0) { |
||
| 422 | return KEY_ARROW_RIGHT;
|
||
| 423 | } else {
|
||
| 424 | ambiguous |= (cmp < 0);
|
||
| 425 | } |
||
| 426 | |||
| 427 | return ambiguous ? KEY_AMBIGUOUS : KEY_UNKNOWN;
|
||
| 428 | } |
||
| 429 | |||
| 430 | /**
|
||
| 431 | * @brief Move the cursor in the terminal
|
||
| 432 | *
|
||
| 433 | * @param[in] shell Pointer to the shell object.
|
||
| 434 | * @param[in] from Starting position of the cursor.
|
||
| 435 | * @param[in] to Target position to move the cursor to.
|
||
| 436 | *
|
||
| 437 | * @return The number of positions moved.
|
||
| 438 | */
|
||
| 439 | static int _moveCursor(aos_shell_t* shell, const size_t from, const size_t to) |
||
| 440 | {
|
||
| 441 | aosDbgCheck(shell != NULL);
|
||
| 442 | |||
| 443 | // local variables
|
||
| 444 | size_t pos = from; |
||
| 445 | |||
| 446 | // move cursor left by printing backspaces
|
||
| 447 | while (pos > to) {
|
||
| 448 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, '\b');
|
| 449 | e545e620 | Thomas Schöpping | --pos; |
| 450 | } |
||
| 451 | |||
| 452 | // move cursor right by printing line content
|
||
| 453 | while (pos < to) {
|
||
| 454 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, shell->line[pos]); |
| 455 | e545e620 | Thomas Schöpping | ++pos; |
| 456 | } |
||
| 457 | |||
| 458 | return (int)pos - (int)from; |
||
| 459 | } |
||
| 460 | |||
| 461 | /**
|
||
| 462 | * @brief Print content of the shell line
|
||
| 463 | *
|
||
| 464 | * @param[in] shell Pointer to the shell object.
|
||
| 465 | * @param[in] from First position to start printing from.
|
||
| 466 | * @param[in] to Position after the last character to print.
|
||
| 467 | *
|
||
| 468 | * @return Number of characters printed.
|
||
| 469 | */
|
||
| 470 | static inline size_t _printLine(aos_shell_t* shell, const size_t from, const size_t to) |
||
| 471 | {
|
||
| 472 | aosDbgCheck(shell != NULL);
|
||
| 473 | |||
| 474 | // local variables
|
||
| 475 | size_t cnt; |
||
| 476 | |||
| 477 | for (cnt = 0; from + cnt < to; ++cnt) { |
||
| 478 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, shell->line[from + cnt]); |
| 479 | e545e620 | Thomas Schöpping | } |
| 480 | |||
| 481 | return cnt;
|
||
| 482 | } |
||
| 483 | |||
| 484 | /**
|
||
| 485 | * @brief Compare two characters.
|
||
| 486 | *
|
||
| 487 | * @param[in] lhs First character to compare.
|
||
| 488 | * @param[in] rhs Second character to compare.
|
||
| 489 | *
|
||
| 490 | * @return How well the characters match.
|
||
| 491 | */
|
||
| 492 | static inline charmatch_t _charcmp(char lhs, char rhs) |
||
| 493 | {
|
||
| 494 | // if lhs is a upper case letter and rhs is a lower case letter
|
||
| 495 | if (lhs >= 'A' && lhs <= 'Z' && rhs >= 'a' && rhs <= 'z') { |
||
| 496 | return (lhs == (rhs - 'a' + 'A')) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
||
| 497 | } |
||
| 498 | // if lhs is a lower case letter and rhs is a upper case letter
|
||
| 499 | else if (lhs >= 'a' && lhs <= 'z' && rhs >= 'A' && rhs <= 'Z') { |
||
| 500 | return ((lhs - 'a' + 'A') == rhs) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
||
| 501 | } |
||
| 502 | // default
|
||
| 503 | else {
|
||
| 504 | return (lhs == rhs) ? CHAR_MATCH_CASE : CHAR_MATCH_NOT;
|
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | /**
|
||
| 509 | * @brief Maps an character from ASCII to a modified custom encoding.
|
||
| 510 | * @details The custom character encoding is very similar to ASCII and has the following structure:
|
||
| 511 | * 0x00=NULL ... 0x40='@' (identically to ASCII)
|
||
| 512 | * 0x4A='a'; 0x4B='A'; 0x4C='b'; 0x4D='B' ... 0x73='z'; 0x74='Z' (custom letter order)
|
||
| 513 | * 0x75='[' ... 0x7A='`' (0x5B..0x60 is ASCII)
|
||
| 514 | * 0x7B='{' ... 0x7F=DEL (identically to ASCII)
|
||
| 515 | *
|
||
| 516 | * @param[in] c Character to map to the custom encoding.
|
||
| 517 | *
|
||
| 518 | * @return The customly encoded character.
|
||
| 519 | */
|
||
| 520 | static inline char _mapAscii2Custom(const char c) |
||
| 521 | {
|
||
| 522 | if (c >= 'A' && c <= 'Z') { |
||
| 523 | return ((c - 'A') * 2) + 'A' + 1; |
||
| 524 | } else if (c > 'Z' && c < 'a') { |
||
| 525 | return c + ('z' - 'a') + 1; |
||
| 526 | } else if (c >= 'a' && c <= 'z') { |
||
| 527 | return ((c - 'a') * 2) + 'A'; |
||
| 528 | } else {
|
||
| 529 | return c;
|
||
| 530 | } |
||
| 531 | } |
||
| 532 | |||
| 533 | /**
|
||
| 534 | * @brief Compares two strings wrt letter case.
|
||
| 535 | * @details Comparisson uses a custom character encoding or mapping.
|
||
| 536 | * See @p _mapAscii2Custom for details.
|
||
| 537 | *
|
||
| 538 | * @param[in] str1 First string to compare.
|
||
| 539 | * @param[in] str2 Second string to compare.
|
||
| 540 | * @param[in] cs Flag indicating whether comparison shall be case sensitive.
|
||
| 541 | * @param[in,out] n Maximum number of character to compare (in) and number of matching characters (out).
|
||
| 542 | * If a null pointer is specified, this parameter is ignored.
|
||
| 543 | * If the value pointed to is zero, comarison will not be limited.
|
||
| 544 | * @param[out] m Optional indicator whether there was at least one case mismatch.
|
||
| 545 | *
|
||
| 546 | * @return Integer value indicating the relationship between the strings.
|
||
| 547 | * @retval <0 The first character that does not match has a lower value in str1 than in str2.
|
||
| 548 | * @retval 0 The contents of both strings are equal.
|
||
| 549 | * @retval >0 The first character that does not match has a greater value in str1 than in str2.
|
||
| 550 | */
|
||
| 551 | static int _strccmp(const char *str1, const char *str2, bool cs, size_t* n, charmatch_t* m) |
||
| 552 | {
|
||
| 553 | aosDbgCheck(str1 != NULL);
|
||
| 554 | aosDbgCheck(str2 != NULL);
|
||
| 555 | |||
| 556 | // initialize variables
|
||
| 557 | if (m) {
|
||
| 558 | *m = CHAR_MATCH_NOT; |
||
| 559 | } |
||
| 560 | size_t i = 0;
|
||
| 561 | |||
| 562 | // iterate through the strings
|
||
| 563 | while ((n == NULL) || (*n == 0) || (*n > 0 && i < *n)) { |
||
| 564 | // break on NUL
|
||
| 565 | if (str1[i] == '\0' || str2[i] == '\0') { |
||
| 566 | if (n) {
|
||
| 567 | *n = i; |
||
| 568 | } |
||
| 569 | break;
|
||
| 570 | } |
||
| 571 | // compare character
|
||
| 572 | const charmatch_t match = _charcmp(str1[i], str2[i]);
|
||
| 573 | if ((match == CHAR_MATCH_CASE) || (!cs && match == CHAR_MATCH_NCASE)) {
|
||
| 574 | if (m != NULL && *m != CHAR_MATCH_NCASE) { |
||
| 575 | *m = match; |
||
| 576 | } |
||
| 577 | ++i; |
||
| 578 | } else {
|
||
| 579 | if (n) {
|
||
| 580 | *n = i; |
||
| 581 | } |
||
| 582 | break;
|
||
| 583 | } |
||
| 584 | } |
||
| 585 | |||
| 586 | return _mapAscii2Custom(str1[i]) - _mapAscii2Custom(str2[i]);
|
||
| 587 | } |
||
| 588 | |||
| 589 | ba516b61 | Thomas Schöpping | static aos_status_t _readChannel(aos_shell_t* shell, AosShellChannel* channel, size_t* n)
|
| 590 | e545e620 | Thomas Schöpping | {
|
| 591 | aosDbgCheck(shell != NULL);
|
||
| 592 | ba516b61 | Thomas Schöpping | aosDbgCheck(channel != NULL);
|
| 593 | aosDbgCheck(n != NULL);
|
||
| 594 | e545e620 | Thomas Schöpping | |
| 595 | // local variables
|
||
| 596 | ba516b61 | Thomas Schöpping | aos_shellaction_t action = AOS_SHELL_ACTION_NONE; |
| 597 | e545e620 | Thomas Schöpping | char c;
|
| 598 | dd8738ea | Thomas Schöpping | special_key_t key; |
| 599 | e545e620 | Thomas Schöpping | |
| 600 | ba516b61 | Thomas Schöpping | // initialize output variables
|
| 601 | *n = 0;
|
||
| 602 | |||
| 603 | // read character by character from the channel
|
||
| 604 | while (chnReadTimeout(channel, (uint8_t*)&c, 1, TIME_IMMEDIATE)) { |
||
| 605 | dd8738ea | Thomas Schöpping | key = KEY_UNKNOWN; |
| 606 | e545e620 | Thomas Schöpping | |
| 607 | // parse escape sequence
|
||
| 608 | ba516b61 | Thomas Schöpping | if (shell->inputdata.escp > 0) { |
| 609 | shell->inputdata.escseq[shell->inputdata.escp] = c; |
||
| 610 | ++shell->inputdata.escp; |
||
| 611 | key = _interpreteEscapeSequence(shell->inputdata.escseq); |
||
| 612 | e545e620 | Thomas Schöpping | if (key == KEY_AMBIGUOUS) {
|
| 613 | // read next byte to resolve ambiguity
|
||
| 614 | continue;
|
||
| 615 | } else {
|
||
| 616 | ba516b61 | Thomas Schöpping | /*
|
| 617 | * If the escape sequence could either be parsed sucessfully
|
||
| 618 | * or there is no match (KEY_UNKNOWN),
|
||
| 619 | * reset the sequence variable and interprete key/character
|
||
| 620 | */
|
||
| 621 | shell->inputdata.escp = 0;
|
||
| 622 | memset(shell->inputdata.escseq, '\0', sizeof(shell->inputdata.escseq)*sizeof(shell->inputdata.escseq[0])); |
||
| 623 | e545e620 | Thomas Schöpping | } |
| 624 | } |
||
| 625 | |||
| 626 | ba516b61 | Thomas Schöpping | /* interprete keys or character */
|
| 627 | e545e620 | Thomas Schöpping | {
|
| 628 | ba516b61 | Thomas Schöpping | // default
|
| 629 | action = AOS_SHELL_ACTION_NONE; |
||
| 630 | |||
| 631 | // printable character
|
||
| 632 | if (key == KEY_UNKNOWN && c >= '\x20' && c <= '\x7E') { |
||
| 633 | action = AOS_SHELL_ACTION_READCHAR; |
||
| 634 | } |
||
| 635 | |||
| 636 | // tab key or character
|
||
| 637 | else if (key == KEY_TAB || c == '\x09') { |
||
| 638 | /*
|
||
| 639 | * pressing tab once applies auto fill
|
||
| 640 | * pressing tab a second time prints suggestions
|
||
| 641 | */
|
||
| 642 | if (shell->inputdata.lastaction == AOS_SHELL_ACTION_AUTOFILL || shell->inputdata.lastaction == AOS_SHELL_ACTION_SUGGEST) {
|
||
| 643 | action = AOS_SHELL_ACTION_SUGGEST; |
||
| 644 | e545e620 | Thomas Schöpping | } else {
|
| 645 | ba516b61 | Thomas Schöpping | action = AOS_SHELL_ACTION_AUTOFILL; |
| 646 | e545e620 | Thomas Schöpping | } |
| 647 | ba516b61 | Thomas Schöpping | } |
| 648 | |||
| 649 | // INS key
|
||
| 650 | else if (key == KEY_INSERT) { |
||
| 651 | action = AOS_SHELL_ACTION_INSERTTOGGLE; |
||
| 652 | } |
||
| 653 | |||
| 654 | // DEL key or character
|
||
| 655 | else if (key == KEY_DELETE || c == '\x7F') { |
||
| 656 | // ignore if cursor is at very right
|
||
| 657 | if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
||
| 658 | action = AOS_SHELL_ACTION_DELETEFORWARD; |
||
| 659 | e545e620 | Thomas Schöpping | } |
| 660 | ba516b61 | Thomas Schöpping | } |
| 661 | |||
| 662 | // backspace key or character
|
||
| 663 | else if (key == KEY_BACKSPACE || c == '\x08') { |
||
| 664 | // ignore if cursor is at very left
|
||
| 665 | if (shell->inputdata.cursorpos > 0) { |
||
| 666 | action = AOS_SHELL_ACTION_DELETEBACKWARD; |
||
| 667 | e545e620 | Thomas Schöpping | } |
| 668 | ba516b61 | Thomas Schöpping | } |
| 669 | |||
| 670 | // 'page up' of 'arrow up' key
|
||
| 671 | else if (key == KEY_PAGE_UP || key == KEY_ARROW_UP) { |
||
| 672 | e545e620 | Thomas Schöpping | // ignore if there was some input
|
| 673 | ba516b61 | Thomas Schöpping | if (shell->inputdata.noinput) {
|
| 674 | action = AOS_SHELL_ACTION_RECALLLAST; |
||
| 675 | e545e620 | Thomas Schöpping | } |
| 676 | ba516b61 | Thomas Schöpping | } |
| 677 | |||
| 678 | // 'page down' key, 'arrow done' key, 'end of test' character or 'end of transmission' character
|
||
| 679 | else if (key == KEY_PAGE_DOWN || key == KEY_ARROW_DOWN || c == '\x03' || c == '\x03') { |
||
| 680 | e545e620 | Thomas Schöpping | // ignore if line is empty
|
| 681 | ba516b61 | Thomas Schöpping | if (shell->inputdata.lineend > 0) { |
| 682 | action = AOS_SHELL_ACTION_CLEAR; |
||
| 683 | e545e620 | Thomas Schöpping | } |
| 684 | ba516b61 | Thomas Schöpping | } |
| 685 | |||
| 686 | // 'home' key
|
||
| 687 |