amiro-os / os / core / src / aos_shell.c @ 2674917e
History | View | Annotate | Download (45.585 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 | #include <aos_shell.h> |
||
| 20 | |||
| 21 | ba516b61 | Thomas Schöpping | #if (AMIROOS_CFG_SHELL_ENABLE == true) |
| 22 | e545e620 | Thomas Schöpping | #include <aos_debug.h> |
| 23 | #include <aos_time.h> |
||
| 24 | #include <aos_system.h> |
||
| 25 | #include <string.h> |
||
| 26 | ba516b61 | Thomas Schöpping | |
| 27 | /**
|
||
| 28 | * @brief Event mask to be set on OS related events.
|
||
| 29 | */
|
||
| 30 | #define AOS_SHELL_EVENTMASK_OS EVENT_MASK(0) |
||
| 31 | |||
| 32 | /**
|
||
| 33 | * @brief Event mask to be set on a input event.
|
||
| 34 | */
|
||
| 35 | #define AOS_SHELL_EVENTMASK_INPUT EVENT_MASK(1) |
||
| 36 | |||
| 37 | /**
|
||
| 38 | * @brief Implementation of the BaseAsynchronous write() method (inherited from BaseSequentialStream).
|
||
| 39 | */
|
||
| 40 | static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n) |
||
| 41 | {
|
||
| 42 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 43 | dd8738ea | Thomas Schöpping | return streamWrite(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
| 44 | ba516b61 | Thomas Schöpping | } else {
|
| 45 | return 0; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /**
|
||
| 50 | * @brief Implementation of the BaseAsynchronous read() method (inherited from BaseSequentialStream).
|
||
| 51 | */
|
||
| 52 | static size_t _channelread(void *instance, uint8_t *bp, size_t n) |
||
| 53 | {
|
||
| 54 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 55 | return streamRead(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
||
| 56 | } else {
|
||
| 57 | return 0; |
||
| 58 | } |
||
| 59 | ba516b61 | Thomas Schöpping | } |
| 60 | |||
| 61 | /**
|
||
| 62 | * @brief Implementation of the BaseAsynchronous put() method (inherited from BaseSequentialStream).
|
||
| 63 | */
|
||
| 64 | static msg_t _channelput(void *instance, uint8_t b) |
||
| 65 | {
|
||
| 66 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 67 | dd8738ea | Thomas Schöpping | return streamPut(((AosShellChannel*)instance)->asyncchannel, b);
|
| 68 | ba516b61 | Thomas Schöpping | } else {
|
| 69 | return MSG_RESET;
|
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /**
|
||
| 74 | * @brief Implementation of the BaseAsynchronous get() method (inherited from BaseSequentialStream).
|
||
| 75 | */
|
||
| 76 | static msg_t _channelget(void *instance) |
||
| 77 | {
|
||
| 78 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 79 | return streamGet(((AosShellChannel*)instance)->asyncchannel);
|
||
| 80 | } else {
|
||
| 81 | return MSG_RESET;
|
||
| 82 | } |
||
| 83 | ba516b61 | Thomas Schöpping | } |
| 84 | |||
| 85 | /**
|
||
| 86 | * @brief Implementation of the BaseAsynchronous putt() method.
|
||
| 87 | */
|
||
| 88 | static msg_t _channelputt(void *instance, uint8_t b, systime_t time) |
||
| 89 | {
|
||
| 90 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 91 | dd8738ea | Thomas Schöpping | return chnPutTimeout(((AosShellChannel*)instance)->asyncchannel, b, time);
|
| 92 | ba516b61 | Thomas Schöpping | } else {
|
| 93 | return MSG_RESET;
|
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /**
|
||
| 98 | * @brief Implementation of the BaseAsynchronous gett() method.
|
||
| 99 | */
|
||
| 100 | static msg_t _channelgett(void *instance, systime_t time) |
||
| 101 | {
|
||
| 102 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 103 | return chnGetTimeout(((AosShellChannel*)instance)->asyncchannel, time);
|
||
| 104 | } else {
|
||
| 105 | return MSG_RESET;
|
||
| 106 | } |
||
| 107 | ba516b61 | Thomas Schöpping | } |
| 108 | |||
| 109 | /**
|
||
| 110 | * @brief Implementation of the BaseAsynchronous writet() method.
|
||
| 111 | */
|
||
| 112 | static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, systime_t time) |
||
| 113 | {
|
||
| 114 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 115 | dd8738ea | Thomas Schöpping | return chnWriteTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
| 116 | ba516b61 | Thomas Schöpping | } else {
|
| 117 | return 0; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /**
|
||
| 122 | * @brief Implementation of the BaseAsynchronous readt() method.
|
||
| 123 | */
|
||
| 124 | static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, systime_t time) |
||
| 125 | {
|
||
| 126 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 127 | return chnReadTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
||
| 128 | } else {
|
||
| 129 | return 0; |
||
| 130 | } |
||
| 131 | ba516b61 | Thomas Schöpping | } |
| 132 | |||
| 133 | static const struct AosShellChannelVMT _channelvmt = { |
||
| 134 | _channelwrite, |
||
| 135 | _channelread, |
||
| 136 | _channelput, |
||
| 137 | _channelget, |
||
| 138 | _channelputt, |
||
| 139 | _channelgett, |
||
| 140 | _channelwritet, |
||
| 141 | _channelreadt, |
||
| 142 | }; |
||
| 143 | |||
| 144 | static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n) |
||
| 145 | {
|
||
| 146 | aosDbgCheck(instance != NULL);
|
||
| 147 | |||
| 148 | // local variables
|
||
| 149 | AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
||
| 150 | size_t bytes; |
||
| 151 | size_t maxbytes = 0;
|
||
| 152 | |||
| 153 | // iterate through the list of channels
|
||
| 154 | while (channel != NULL) { |
||
| 155 | bytes = streamWrite(channel, bp, n); |
||
| 156 | maxbytes = (bytes > maxbytes) ? bytes : maxbytes; |
||
| 157 | channel = channel->next; |
||
| 158 | } |
||
| 159 | |||
| 160 | return maxbytes;
|
||
| 161 | } |
||
| 162 | |||
| 163 | static size_t _stremread(void *instance, uint8_t *bp, size_t n) |
||
| 164 | {
|
||
| 165 | (void)instance;
|
||
| 166 | (void)bp;
|
||
| 167 | (void)n;
|
||
| 168 | |||
| 169 | return 0; |
||
| 170 | } |
||
| 171 | |||
| 172 | static msg_t _streamput(void *instance, uint8_t b) |
||
| 173 | {
|
||
| 174 | aosDbgCheck(instance != NULL);
|
||
| 175 | |||
| 176 | // local variables
|
||
| 177 | AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
||
| 178 | dd8738ea | Thomas Schöpping | msg_t ret = MSG_OK; |
| 179 | ba516b61 | Thomas Schöpping | |
| 180 | // iterate through the list of channels
|
||
| 181 | while (channel != NULL) { |
||
| 182 | dd8738ea | Thomas Schöpping | msg_t ret_ = streamPut(channel, b); |
| 183 | ret = (ret_ < ret) ? ret_ : ret; |
||
| 184 | ba516b61 | Thomas Schöpping | channel = channel->next; |
| 185 | } |
||
| 186 | |||
| 187 | dd8738ea | Thomas Schöpping | return ret;
|
| 188 | ba516b61 | Thomas Schöpping | } |
| 189 | |||
| 190 | static msg_t _streamget(void *instance) |
||
| 191 | {
|
||
| 192 | (void)instance;
|
||
| 193 | |||
| 194 | return 0; |
||
| 195 | } |
||
| 196 | |||
| 197 | static const struct AosShellStreamVMT _streamvmt = { |
||
| 198 | _streamwrite, |
||
| 199 | _stremread, |
||
| 200 | _streamput, |
||
| 201 | _streamget, |
||
| 202 | }; |
||
| 203 | |||
| 204 | e545e620 | Thomas Schöpping | /**
|
| 205 | * @brief Enumerator of special keyboard keys.
|
||
| 206 | */
|
||
| 207 | typedef enum special_key { |
||
| 208 | KEY_UNKNOWN, /**< any/unknow key */
|
||
| 209 | KEY_AMBIGUOUS, /**< key is ambiguous */
|
||
| 210 | KEY_TAB, /**< tabulator key */
|
||
| 211 | KEY_ESCAPE, /**< escape key */
|
||
| 212 | KEY_BACKSPACE, /**< backspace key */
|
||
| 213 | KEY_INSERT, /**< insert key */
|
||
| 214 | KEY_DELETE, /**< delete key */
|
||
| 215 | KEY_HOME, /**< home key */
|
||
| 216 | KEY_END, /**< end key */
|
||
| 217 | KEY_PAGE_UP, /**< page up key */
|
||
| 218 | KEY_PAGE_DOWN, /**< page down key */
|
||
| 219 | KEY_ARROW_UP, /**< arrow up key */
|
||
| 220 | KEY_ARROW_DOWN, /**< arrow down key */
|
||
| 221 | KEY_ARROW_LEFT, /**< arrow left key */
|
||
| 222 | KEY_ARROW_RIGHT, /**< arrow right key */
|
||
| 223 | } special_key_t; |
||
| 224 | |||
| 225 | /**
|
||
| 226 | * @brief Enumerator for case (in)sensitive character matching.
|
||
| 227 | */
|
||
| 228 | typedef enum charmatch { |
||
| 229 | CHAR_MATCH_NOT = 0, /**< Characters do not match at all. */ |
||
| 230 | CHAR_MATCH_NCASE = 1, /**< Characters would match case insensitive. */ |
||
| 231 | CHAR_MATCH_CASE = 2, /**< Characters do match with case. */ |
||
| 232 | } charmatch_t; |
||
| 233 | |||
| 234 | /**
|
||
| 235 | * @brief Print the shell prompt
|
||
| 236 | * @details Depending on the configuration flags, the system uptime is printed before the prompt string.
|
||
| 237 | *
|
||
| 238 | * @param[in] shell Pointer to the shell object.
|
||
| 239 | */
|
||
| 240 | static void _printPrompt(aos_shell_t* shell) |
||
| 241 | {
|
||
| 242 | aosDbgCheck(shell != NULL);
|
||
| 243 | |||
| 244 | 8399aeae | Thomas Schöpping | // print some time informattion before prompt if configured
|
| 245 | if (shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) {
|
||
| 246 | // printf the system uptime
|
||
| 247 | if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_UPTIME) {
|
||
| 248 | // get current system uptime
|
||
| 249 | aos_timestamp_t uptime; |
||
| 250 | aosSysGetUptime(&uptime); |
||
| 251 | |||
| 252 | chprintf((BaseSequentialStream*)&shell->stream, "[%01u:%02u:%02u:%02u:%03u:%03u] ",
|
||
| 253 | (uint32_t)(uptime / MICROSECONDS_PER_DAY), |
||
| 254 | (uint8_t)(uptime % MICROSECONDS_PER_DAY / MICROSECONDS_PER_HOUR), |
||
| 255 | (uint8_t)(uptime % MICROSECONDS_PER_HOUR / MICROSECONDS_PER_MINUTE), |
||
| 256 | (uint8_t)(uptime % MICROSECONDS_PER_MINUTE / MICROSECONDS_PER_SECOND), |
||
| 257 | (uint16_t)(uptime % MICROSECONDS_PER_SECOND / MICROSECONDS_PER_MILLISECOND), |
||
| 258 | (uint16_t)(uptime % MICROSECONDS_PER_MILLISECOND / MICROSECONDS_PER_MICROSECOND)); |
||
| 259 | } |
||
| 260 | else if ((shell->config & (AOS_SHELL_CONFIG_PROMPT_UPTIME | AOS_SHELL_CONFIG_PROMPT_DATETIME)) == AOS_SHELL_CONFIG_PROMPT_DATETIME) { |
||
| 261 | // get current RTC time
|
||
| 262 | struct tm dt;
|
||
| 263 | aosSysGetDateTime(&dt); |
||
| 264 | chprintf((BaseSequentialStream*)&shell->stream, "[%02u-%02u-%04u|%02u:%02u:%02u] ",
|
||
| 265 | dt.tm_mday, |
||
| 266 | dt.tm_mon + 1,
|
||
| 267 | dt.tm_year + 1900,
|
||
| 268 | dt.tm_hour, |
||
| 269 | dt.tm_min, |
||
| 270 | dt.tm_sec); |
||
| 271 | } |
||
| 272 | else {
|
||
| 273 | aosDbgAssert(false);
|
||
| 274 | } |
||
| 275 | e545e620 | Thomas Schöpping | } |
| 276 | |||
| 277 | // print the actual prompt string
|
||
| 278 | if (shell->prompt && !(shell->config & AOS_SHELL_CONFIG_PROMPT_MINIMAL)) {
|
||
| 279 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "%s$ ", shell->prompt);
|
| 280 | e545e620 | Thomas Schöpping | } else {
|
| 281 | ba516b61 | Thomas Schöpping | chprintf((BaseSequentialStream*)&shell->stream, "%>$ ");
|
| 282 | e545e620 | Thomas Schöpping | } |
| 283 | |||
| 284 | return;
|
||
| 285 | } |
||
| 286 | |||
| 287 | /**
|
||
| 288 | * @brief Interprete a escape sequence
|
||
| 289 | *
|
||
| 290 | * @param[in] seq Character sequence to interprete.
|
||
| 291 | * Must be terminated by NUL byte.
|
||
| 292 | *
|
||
| 293 | * @return A @p special_key value.
|
||
| 294 | */
|
||
| 295 | static special_key_t _interpreteEscapeSequence(const char seq[]) |
||
| 296 | {
|
||
| 297 | // local variables
|
||
| 298 | bool ambiguous = false; |
||
| 299 | int cmp = 0; |
||
| 300 | |||
| 301 | // TAB
|
||
| 302 | /* not supported yet; use "\x09" instead */
|
||
| 303 | |||
| 304 | // BACKSPACE
|
||
| 305 | /* not supported yet; use "\x08" instead */
|
||
| 306 | |||
| 307 | // ESCAPE
|
||
| 308 | cmp = strcmp(seq, "\x1B");
|
||
| 309 | if (cmp == 0) { |
||
| 310 | return KEY_ESCAPE;
|
||
| 311 | } else {
|
||
| 312 | ambiguous |= (cmp < 0);
|
||
| 313 | } |
||
| 314 | |||
| 315 | // INSERT
|
||
| 316 | cmp = strcmp(seq, "\x1B\x5B\x32\x7E");
|
||
| 317 | if (cmp == 0) { |
||
| 318 | return KEY_INSERT;
|
||
| 319 | } else {
|
||
| 320 | ambiguous |= (cmp < 0);
|
||
| 321 | } |
||
| 322 | |||
| 323 | // DELETE
|
||
| 324 | cmp = strcmp(seq, "\x1B\x5B\x33\x7E");
|
||
| 325 | if (cmp == 0) { |
||
| 326 | return KEY_DELETE;
|
||
| 327 | } else {
|
||
| 328 | ambiguous |= (cmp < 0);
|
||
| 329 | } |
||
| 330 | |||
| 331 | // HOME
|
||
| 332 | cmp = strcmp(seq, "\x1B\x4F\x48");
|
||
| 333 | if (cmp == 0) { |
||
| 334 | return KEY_HOME;
|
||
| 335 | } else {
|
||
| 336 | ambiguous |= (cmp < 0);
|
||
| 337 | } |
||
| 338 | |||
| 339 | // END
|
||
| 340 | cmp = strcmp(seq, "\x1B\x4F\x46");
|
||
| 341 | if (cmp == 0) { |
||
| 342 | return KEY_END;
|
||
| 343 | } else {
|
||
| 344 | ambiguous |= (cmp < 0);
|
||
| 345 | } |
||
| 346 | |||
| 347 | // PAGE UP
|
||
| 348 | cmp = strcmp(seq, "\x1B\x5B\x35\x7E");
|
||
| 349 | if (cmp == 0) { |
||
| 350 | return KEY_PAGE_UP;
|
||
| 351 | } else {
|
||
| 352 | ambiguous |= (cmp < 0);
|
||
| 353 | } |
||
| 354 | |||
| 355 | // PAGE DOWN
|
||
| 356 | cmp = strcmp(seq, "\x1B\x5B\x36\x7E");
|
||
| 357 | if (cmp == 0) { |
||
| 358 | return KEY_PAGE_DOWN;
|
||
| 359 | } else {
|
||
| 360 | ambiguous |= (cmp < 0);
|
||
| 361 | } |
||
| 362 | |||
| 363 | // ARROW UP
|
||
| 364 | cmp = strcmp(seq, "\x1B\x5B\x41");
|
||
| 365 | if (cmp == 0) { |
||
| 366 | return KEY_ARROW_UP;
|
||
| 367 | } else {
|
||
| 368 | ambiguous |= (cmp < 0);
|
||
| 369 | } |
||
| 370 | |||
| 371 | // ARROW DOWN
|
||
| 372 | cmp = strcmp(seq, "\x1B\x5B\x42");
|
||
| 373 | if (cmp == 0) { |
||
| 374 | return KEY_ARROW_DOWN;
|
||
| 375 | } else {
|
||
| 376 | ambiguous |= (cmp < 0);
|
||
| 377 | } |
||
| 378 | |||
| 379 | // ARROW LEFT
|
||
| 380 | cmp = strcmp(seq, "\x1B\x5B\x44");
|
||
| 381 | if (cmp == 0) { |
||
| 382 | return KEY_ARROW_LEFT;
|
||
| 383 | } else {
|
||
| 384 | ambiguous |= (cmp < 0);
|
||
| 385 | } |
||
| 386 | |||
| 387 | // ARROW RIGHT
|
||
| 388 | cmp = strcmp(seq, "\x1B\x5B\x43");
|
||
| 389 | if (cmp == 0) { |
||
| 390 | return KEY_ARROW_RIGHT;
|
||
| 391 | } else {
|
||
| 392 | ambiguous |= (cmp < 0);
|
||
| 393 | } |
||
| 394 | |||
| 395 | return ambiguous ? KEY_AMBIGUOUS : KEY_UNKNOWN;
|
||
| 396 | } |
||
| 397 | |||
| 398 | /**
|
||
| 399 | * @brief Move the cursor in the terminal
|
||
| 400 | *
|
||
| 401 | * @param[in] shell Pointer to the shell object.
|
||
| 402 | * @param[in] from Starting position of the cursor.
|
||
| 403 | * @param[in] to Target position to move the cursor to.
|
||
| 404 | *
|
||
| 405 | * @return The number of positions moved.
|
||
| 406 | */
|
||
| 407 | static int _moveCursor(aos_shell_t* shell, const size_t from, const size_t to) |
||
| 408 | {
|
||
| 409 | aosDbgCheck(shell != NULL);
|
||
| 410 | |||
| 411 | // local variables
|
||
| 412 | size_t pos = from; |
||
| 413 | |||
| 414 | // move cursor left by printing backspaces
|
||
| 415 | while (pos > to) {
|
||
| 416 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, '\b');
|
| 417 | e545e620 | Thomas Schöpping | --pos; |
| 418 | } |
||
| 419 | |||
| 420 | // move cursor right by printing line content
|
||
| 421 | while (pos < to) {
|
||
| 422 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, shell->line[pos]); |
| 423 | e545e620 | Thomas Schöpping | ++pos; |
| 424 | } |
||
| 425 | |||
| 426 | return (int)pos - (int)from; |
||
| 427 | } |
||
| 428 | |||
| 429 | /**
|
||
| 430 | * @brief Print content of the shell line
|
||
| 431 | *
|
||
| 432 | * @param[in] shell Pointer to the shell object.
|
||
| 433 | * @param[in] from First position to start printing from.
|
||
| 434 | * @param[in] to Position after the last character to print.
|
||
| 435 | *
|
||
| 436 | * @return Number of characters printed.
|
||
| 437 | */
|
||
| 438 | static inline size_t _printLine(aos_shell_t* shell, const size_t from, const size_t to) |
||
| 439 | {
|
||
| 440 | aosDbgCheck(shell != NULL);
|
||
| 441 | |||
| 442 | // local variables
|
||
| 443 | size_t cnt; |
||
| 444 | |||
| 445 | for (cnt = 0; from + cnt < to; ++cnt) { |
||
| 446 | ba516b61 | Thomas Schöpping | streamPut(&shell->stream, shell->line[from + cnt]); |
| 447 | e545e620 | Thomas Schöpping | } |
| 448 | |||
| 449 | return cnt;
|
||
| 450 | } |
||
| 451 | |||
| 452 | /**
|
||
| 453 | * @brief Compare two characters.
|
||
| 454 | *
|
||
| 455 | * @param[in] lhs First character to compare.
|
||
| 456 | * @param[in] rhs Second character to compare.
|
||
| 457 | *
|
||
| 458 | * @return How well the characters match.
|
||
| 459 | */
|
||
| 460 | static inline charmatch_t _charcmp(char lhs, char rhs) |
||
| 461 | {
|
||
| 462 | // if lhs is a upper case letter and rhs is a lower case letter
|
||
| 463 | if (lhs >= 'A' && lhs <= 'Z' && rhs >= 'a' && rhs <= 'z') { |
||
| 464 | return (lhs == (rhs - 'a' + 'A')) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
||
| 465 | } |
||
| 466 | // if lhs is a lower case letter and rhs is a upper case letter
|
||
| 467 | else if (lhs >= 'a' && lhs <= 'z' && rhs >= 'A' && rhs <= 'Z') { |
||
| 468 | return ((lhs - 'a' + 'A') == rhs) ? CHAR_MATCH_NCASE : CHAR_MATCH_NOT; |
||
| 469 | } |
||
| 470 | // default
|
||
| 471 | else {
|
||
| 472 | return (lhs == rhs) ? CHAR_MATCH_CASE : CHAR_MATCH_NOT;
|
||
| 473 | } |
||
| 474 | } |
||
| 475 | |||
| 476 | /**
|
||
| 477 | * @brief Maps an character from ASCII to a modified custom encoding.
|
||
| 478 | * @details The custom character encoding is very similar to ASCII and has the following structure:
|
||
| 479 | * 0x00=NULL ... 0x40='@' (identically to ASCII)
|
||
| 480 | * 0x4A='a'; 0x4B='A'; 0x4C='b'; 0x4D='B' ... 0x73='z'; 0x74='Z' (custom letter order)
|
||
| 481 | * 0x75='[' ... 0x7A='`' (0x5B..0x60 is ASCII)
|
||
| 482 | * 0x7B='{' ... 0x7F=DEL (identically to ASCII)
|
||
| 483 | *
|
||
| 484 | * @param[in] c Character to map to the custom encoding.
|
||
| 485 | *
|
||
| 486 | * @return The customly encoded character.
|
||
| 487 | */
|
||
| 488 | static inline char _mapAscii2Custom(const char c) |
||
| 489 | {
|
||
| 490 | if (c >= 'A' && c <= 'Z') { |
||
| 491 | return ((c - 'A') * 2) + 'A' + 1; |
||
| 492 | } else if (c > 'Z' && c < 'a') { |
||
| 493 | return c + ('z' - 'a') + 1; |
||
| 494 | } else if (c >= 'a' && c <= 'z') { |
||
| 495 | return ((c - 'a') * 2) + 'A'; |
||
| 496 | } else {
|
||
| 497 | return c;
|
||
| 498 | } |
||
| 499 | } |
||
| 500 | |||
| 501 | /**
|
||
| 502 | * @brief Compares two strings wrt letter case.
|
||
| 503 | * @details Comparisson uses a custom character encoding or mapping.
|
||
| 504 | * See @p _mapAscii2Custom for details.
|
||
| 505 | *
|
||
| 506 | * @param[in] str1 First string to compare.
|
||
| 507 | * @param[in] str2 Second string to compare.
|
||
| 508 | * @param[in] cs Flag indicating whether comparison shall be case sensitive.
|
||
| 509 | * @param[in,out] n Maximum number of character to compare (in) and number of matching characters (out).
|
||
| 510 | * If a null pointer is specified, this parameter is ignored.
|
||
| 511 | * If the value pointed to is zero, comarison will not be limited.
|
||
| 512 | * @param[out] m Optional indicator whether there was at least one case mismatch.
|
||
| 513 | *
|
||
| 514 | * @return Integer value indicating the relationship between the strings.
|
||
| 515 | * @retval <0 The first character that does not match has a lower value in str1 than in str2.
|
||
| 516 | * @retval 0 The contents of both strings are equal.
|
||
| 517 | * @retval >0 The first character that does not match has a greater value in str1 than in str2.
|
||
| 518 | */
|
||
| 519 | static int _strccmp(const char *str1, const char *str2, bool cs, size_t* n, charmatch_t* m) |
||
| 520 | {
|
||
| 521 | aosDbgCheck(str1 != NULL);
|
||
| 522 | aosDbgCheck(str2 != NULL);
|
||
| 523 | |||
| 524 | // initialize variables
|
||
| 525 | if (m) {
|
||
| 526 | *m = CHAR_MATCH_NOT; |
||
| 527 | } |
||
| 528 | size_t i = 0;
|
||
| 529 | |||
| 530 | // iterate through the strings
|
||
| 531 | while ((n == NULL) || (*n == 0) || (*n > 0 && i < *n)) { |
||
| 532 | // break on NUL
|
||
| 533 | if (str1[i] == '\0' || str2[i] == '\0') { |
||
| 534 | if (n) {
|
||
| 535 | *n = i; |
||
| 536 | } |
||
| 537 | break;
|
||
| 538 | } |
||
| 539 | // compare character
|
||
| 540 | const charmatch_t match = _charcmp(str1[i], str2[i]);
|
||
| 541 | if ((match == CHAR_MATCH_CASE) || (!cs && match == CHAR_MATCH_NCASE)) {
|
||
| 542 | if (m != NULL && *m != CHAR_MATCH_NCASE) { |
||
| 543 | *m = match; |
||
| 544 | } |
||
| 545 | ++i; |
||
| 546 | } else {
|
||
| 547 | if (n) {
|
||
| 548 | *n = i; |
||
| 549 | } |
||
| 550 | break;
|
||
| 551 | } |
||
| 552 | } |
||
| 553 | |||
| 554 | return _mapAscii2Custom(str1[i]) - _mapAscii2Custom(str2[i]);
|
||
| 555 | } |
||
| 556 | |||
| 557 | ba516b61 | Thomas Schöpping | static aos_status_t _readChannel(aos_shell_t* shell, AosShellChannel* channel, size_t* n)
|
| 558 | e545e620 | Thomas Schöpping | {
|
| 559 | aosDbgCheck(shell != NULL);
|
||
| 560 | ba516b61 | Thomas Schöpping | aosDbgCheck(channel != NULL);
|
| 561 | aosDbgCheck(n != NULL);
|
||
| 562 | e545e620 | Thomas Schöpping | |
| 563 | // local variables
|
||
| 564 | ba516b61 | Thomas Schöpping | aos_shellaction_t action = AOS_SHELL_ACTION_NONE; |
| 565 | e545e620 | Thomas Schöpping | char c;
|
| 566 | dd8738ea | Thomas Schöpping | special_key_t key; |
| 567 | e545e620 | Thomas Schöpping | |
| 568 | ba516b61 | Thomas Schöpping | // initialize output variables
|
| 569 | *n = 0;
|
||
| 570 | |||
| 571 | // read character by character from the channel
|
||
| 572 | while (chnReadTimeout(channel, (uint8_t*)&c, 1, TIME_IMMEDIATE)) { |
||
| 573 | dd8738ea | Thomas Schöpping | key = KEY_UNKNOWN; |
| 574 | e545e620 | Thomas Schöpping | |
| 575 | // parse escape sequence
|
||
| 576 | ba516b61 | Thomas Schöpping | if (shell->inputdata.escp > 0) { |
| 577 | shell->inputdata.escseq[shell->inputdata.escp] = c; |
||
| 578 | ++shell->inputdata.escp; |
||
| 579 | key = _interpreteEscapeSequence(shell->inputdata.escseq); |
||
| 580 | e545e620 | Thomas Schöpping | if (key == KEY_AMBIGUOUS) {
|
| 581 | // read next byte to resolve ambiguity
|
||
| 582 | continue;
|
||
| 583 | } else {
|
||
| 584 | ba516b61 | Thomas Schöpping | /*
|
| 585 | * If the escape sequence could either be parsed sucessfully
|
||
| 586 | * or there is no match (KEY_UNKNOWN),
|
||
| 587 | * reset the sequence variable and interprete key/character
|
||
| 588 | */
|
||
| 589 | shell->inputdata.escp = 0;
|
||
| 590 | memset(shell->inputdata.escseq, '\0', sizeof(shell->inputdata.escseq)*sizeof(shell->inputdata.escseq[0])); |
||
| 591 | e545e620 | Thomas Schöpping | } |
| 592 | } |
||
| 593 | |||
| 594 | ba516b61 | Thomas Schöpping | /* interprete keys or character */
|
| 595 | e545e620 | Thomas Schöpping | {
|
| 596 | ba516b61 | Thomas Schöpping | // default
|
| 597 | action = AOS_SHELL_ACTION_NONE; |
||
| 598 | |||
| 599 | // printable character
|
||
| 600 | if (key == KEY_UNKNOWN && c >= '\x20' && c <= '\x7E') { |
||
| 601 | action = AOS_SHELL_ACTION_READCHAR; |
||
| 602 | } |
||
| 603 | |||
| 604 | // tab key or character
|
||
| 605 | else if (key == KEY_TAB || c == '\x09') { |
||
| 606 | /*
|
||
| 607 | * pressing tab once applies auto fill
|
||
| 608 | * pressing tab a second time prints suggestions
|
||
| 609 | */
|
||
| 610 | if (shell->inputdata.lastaction == AOS_SHELL_ACTION_AUTOFILL || shell->inputdata.lastaction == AOS_SHELL_ACTION_SUGGEST) {
|
||
| 611 | action = AOS_SHELL_ACTION_SUGGEST; |
||
| 612 | e545e620 | Thomas Schöpping | } else {
|
| 613 | ba516b61 | Thomas Schöpping | action = AOS_SHELL_ACTION_AUTOFILL; |
| 614 | e545e620 | Thomas Schöpping | } |
| 615 | ba516b61 | Thomas Schöpping | } |
| 616 | |||
| 617 | // INS key
|
||
| 618 | else if (key == KEY_INSERT) { |
||
| 619 | action = AOS_SHELL_ACTION_INSERTTOGGLE; |
||
| 620 | } |
||
| 621 | |||
| 622 | // DEL key or character
|
||
| 623 | else if (key == KEY_DELETE || c == '\x7F') { |
||
| 624 | // ignore if cursor is at very right
|
||
| 625 | if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
||
| 626 | action = AOS_SHELL_ACTION_DELETEFORWARD; |
||
| 627 | e545e620 | Thomas Schöpping | } |
| 628 | ba516b61 | Thomas Schöpping | } |
| 629 | |||
| 630 | // backspace key or character
|
||
| 631 | else if (key == KEY_BACKSPACE || c == '\x08') { |
||
| 632 | // ignore if cursor is at very left
|
||
| 633 | if (shell->inputdata.cursorpos > 0) { |
||
| 634 | action = AOS_SHELL_ACTION_DELETEBACKWARD; |
||
| 635 | e545e620 | Thomas Schöpping | } |
| 636 | ba516b61 | Thomas Schöpping | } |
| 637 | |||
| 638 | // 'page up' of 'arrow up' key
|
||
| 639 | else if (key == KEY_PAGE_UP || key == KEY_ARROW_UP) { |
||
| 640 | e545e620 | Thomas Schöpping | // ignore if there was some input
|
| 641 | ba516b61 | Thomas Schöpping | if (shell->inputdata.noinput) {
|
| 642 | action = AOS_SHELL_ACTION_RECALLLAST; |
||
| 643 | e545e620 | Thomas Schöpping | } |
| 644 | ba516b61 | Thomas Schöpping | } |
| 645 | |||
| 646 | // 'page down' key, 'arrow done' key, 'end of test' character or 'end of transmission' character
|
||
| 647 | else if (key == KEY_PAGE_DOWN || key == KEY_ARROW_DOWN || c == '\x03' || c == '\x03') { |
||
| 648 | e545e620 | Thomas Schöpping | // ignore if line is empty
|
| 649 | ba516b61 | Thomas Schöpping | if (shell->inputdata.lineend > 0) { |
| 650 | action = AOS_SHELL_ACTION_CLEAR; |
||
| 651 | e545e620 | Thomas Schöpping | } |
| 652 | ba516b61 | Thomas Schöpping | } |
| 653 | |||
| 654 | // 'home' key
|
||
| 655 | else if (key == KEY_HOME) { |
||
| 656 | e545e620 | Thomas Schöpping | // ignore if cursor is very left
|
| 657 | ba516b61 | Thomas Schöpping | if (shell->inputdata.cursorpos > 0) { |
| 658 | action = AOS_SHELL_ACTION_CURSOR2START; |
||
| 659 | e545e620 | Thomas Schöpping | } |
| 660 | ba516b61 | Thomas Schöpping | } |
| 661 | |||
| 662 | // 'end' key
|
||
| 663 | else if (key == KEY_END) { |
||
| 664 | // ignore if cursos is very right
|
||
| 665 | if (shell->inputdata.cursorpos < shell->inputdata.lineend) {
|
||
| 666 | action = AOS_SHELL_ACTION_CURSOR2END; |
||
| 667 | e545e620 | Thomas Schöpping | } |
| 668 | ba516b61 | Thomas Schöpping | } |