[279] | 1 | /**
|
---|
| 2 | * @file ntshell.c
|
---|
| 3 | * @author CuBeatSystems
|
---|
| 4 | * @author Shinichiro Nakamura
|
---|
| 5 | * @copyright
|
---|
| 6 | * ===============================================================
|
---|
| 7 | * Natural Tiny Shell (NT-Shell) Version 0.3.1
|
---|
| 8 | * ===============================================================
|
---|
| 9 | * Copyright (c) 2010-2016 Shinichiro Nakamura
|
---|
| 10 | *
|
---|
| 11 | * Permission is hereby granted, free of charge, to any person
|
---|
| 12 | * obtaining a copy of this software and associated documentation
|
---|
| 13 | * files (the "Software"), to deal in the Software without
|
---|
| 14 | * restriction, including without limitation the rights to use,
|
---|
| 15 | * copy, modify, merge, publish, distribute, sublicense, and/or
|
---|
| 16 | * sell copies of the Software, and to permit persons to whom the
|
---|
| 17 | * Software is furnished to do so, subject to the following
|
---|
| 18 | * conditions:
|
---|
| 19 | *
|
---|
| 20 | * The above copyright notice and this permission notice shall be
|
---|
| 21 | * included in all copies or substantial portions of the Software.
|
---|
| 22 | *
|
---|
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
| 25 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
| 27 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
| 28 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
| 29 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
| 30 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | #include <string.h>
|
---|
| 34 | #include "ntshell.h"
|
---|
[331] | 35 | #include "core/ntlibc.h"
|
---|
[279] | 36 |
|
---|
| 37 | int ntshell_exit;
|
---|
| 38 |
|
---|
| 39 | #define VERSION_MAJOR (0) /**< Major number. */
|
---|
| 40 | #define VERSION_MINOR (3) /**< Minor number. */
|
---|
| 41 | #define VERSION_RELEASE (1) /**< Release number. */
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * @brief Initialization code.
|
---|
| 45 | */
|
---|
| 46 | #define INITCODE (0x4367)
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * @brief Unused variable wrapper.
|
---|
| 50 | *
|
---|
| 51 | * @param N A variable.
|
---|
| 52 | */
|
---|
| 53 | #define UNUSED_VARIABLE(N) do { (void)(N); } while (0)
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * @brief Index number of the suggestion.
|
---|
| 57 | *
|
---|
| 58 | * @param HANDLE A pointer of the handle.
|
---|
| 59 | */
|
---|
| 60 | #define SUGGEST_INDEX(HANDLE) ((HANDLE)->suggest_index)
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * @brief Source text string of the suggestion.
|
---|
| 64 | *
|
---|
| 65 | * @param HANDLE A pointer of the handle.
|
---|
| 66 | */
|
---|
| 67 | #define SUGGEST_SOURCE(HANDLE) ((HANDLE)->suggest_source)
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * @brief Get the text editor.
|
---|
| 71 | *
|
---|
| 72 | * @param HANDLE A pointer of the handle.
|
---|
| 73 | */
|
---|
| 74 | #define GET_EDITOR(HANDLE) (&((HANDLE)->editor))
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * @brief Get the text history.
|
---|
| 78 | *
|
---|
| 79 | * @param HANDLE A pointer of the handle.
|
---|
| 80 | */
|
---|
| 81 | #define GET_HISTORY(HANDLE) (&((HANDLE)->history))
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * @brief Read from the serial port.
|
---|
| 85 | *
|
---|
| 86 | * @param HANDLE A pointer of the handle.
|
---|
| 87 | * @param BUF A pointer to the buffer.
|
---|
| 88 | * @param CNT Read length.
|
---|
| 89 | *
|
---|
| 90 | * @return The number of bytes read.
|
---|
| 91 | */
|
---|
| 92 | #define SERIAL_READ(HANDLE, BUF, CNT) ((HANDLE)->func_read(BUF, CNT, (HANDLE)->extobj))
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * @brief Write to the serial port.
|
---|
| 96 | *
|
---|
| 97 | * @param HANDLE A pointer of the handle.
|
---|
| 98 | * @param BUF A pointer to the buffer.
|
---|
| 99 | * @param CNT Write length.
|
---|
| 100 | *
|
---|
| 101 | * @return The number of bytes written.
|
---|
| 102 | */
|
---|
| 103 | #define SERIAL_WRITE(HANDLE, BUF, CNT) ((HANDLE)->func_write(BUF, CNT, (HANDLE)->extobj))
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | * @brief Write the prompt to the serial port.
|
---|
| 107 | *
|
---|
| 108 | * @param HANDLE A pointer of the handle.
|
---|
| 109 | */
|
---|
[331] | 110 | #define PROMPT_WRITE(HANDLE) SERIAL_WRITE((HANDLE), (HANDLE)->prompt, ntlibc_strlen((HANDLE)->prompt))
|
---|
[279] | 111 |
|
---|
| 112 | /**
|
---|
| 113 | * @brief Write the newline to the serial port.
|
---|
| 114 | *
|
---|
| 115 | * @param HANDLE A pointer of the handle.
|
---|
| 116 | */
|
---|
[331] | 117 | #define PROMPT_NEWLINE(HANDLE) SERIAL_WRITE((HANDLE), NTSHELL_PROMPT_NEWLINE, ntlibc_strlen(NTSHELL_PROMPT_NEWLINE))
|
---|
[279] | 118 |
|
---|
| 119 | /**
|
---|
| 120 | * @brief Call the user callback function.
|
---|
| 121 | *
|
---|
| 122 | * @param HANDLE A pointer of the handle.
|
---|
| 123 | * @param TEXT A text string for the callback function.
|
---|
| 124 | */
|
---|
| 125 | #define CALLBACK(HANDLE, TEXT) ((HANDLE)->func_callback((TEXT), (HANDLE)->extobj))
|
---|
| 126 |
|
---|
| 127 | #define VTSEND_ERASE_LINE(HANDLE) (vtsend_erase_line(&((HANDLE)->vtsend)))
|
---|
| 128 | #define VTSEND_CURSOR_HEAD(HANDLE) (vtsend_cursor_backward(&((HANDLE)->vtsend), 80))
|
---|
| 129 | #define VTSEND_CURSOR_PREV(HANDLE) (vtsend_cursor_backward(&((HANDLE)->vtsend), 1))
|
---|
| 130 | #define VTSEND_CURSOR_PREVS(HANDLE,n) (vtsend_cursor_backward(&((HANDLE)->vtsend), n))
|
---|
| 131 | #define VTSEND_CURSOR_NEXT(HANDLE) (vtsend_cursor_forward(&((HANDLE)->vtsend), 1))
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * @brief Search a previous input text on the history module.
|
---|
| 135 | * @details This function change the state of the logical text editor and the view.
|
---|
| 136 | *
|
---|
| 137 | * @param ntshell A handler of the NT-Shell.
|
---|
| 138 | * @param action An action.
|
---|
| 139 | * @param ch A input character.
|
---|
| 140 | */
|
---|
| 141 | static void actfunc_history_prev(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
|
---|
| 142 | {
|
---|
| 143 | UNUSED_VARIABLE(action);
|
---|
| 144 | UNUSED_VARIABLE(ch);
|
---|
| 145 | if (text_history_read_point_prev(GET_HISTORY(ntshell))) {
|
---|
| 146 | char txt[TEXTHISTORY_MAXLEN];
|
---|
| 147 | int n = text_history_read(GET_HISTORY(ntshell), &txt[0], sizeof(txt));
|
---|
| 148 | if (0 < n) {
|
---|
| 149 | VTSEND_ERASE_LINE(ntshell);
|
---|
| 150 | VTSEND_CURSOR_HEAD(ntshell);
|
---|
| 151 | PROMPT_WRITE(ntshell);
|
---|
| 152 | SERIAL_WRITE(ntshell, txt, n);
|
---|
| 153 | text_editor_set_text(GET_EDITOR(ntshell), txt);
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /**
|
---|
| 159 | * @brief Search a next input text on the history module.
|
---|
| 160 | * @details This function change the state of the logical text editor and the view.
|
---|
| 161 | *
|
---|
| 162 | * @param ntshell A handler of the NT-Shell.
|
---|
| 163 | * @param action An action.
|
---|
| 164 | * @param ch A input character.
|
---|
| 165 | */
|
---|
| 166 | static void actfunc_history_next(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
|
---|
| 167 | {
|
---|
| 168 | UNUSED_VARIABLE(action);
|
---|
| 169 | UNUSED_VARIABLE(ch);
|
---|
| 170 | if (text_history_read_point_next(GET_HISTORY(ntshell))) {
|
---|
| 171 | char txt[TEXTHISTORY_MAXLEN];
|
---|
| 172 | int n = text_history_read(GET_HISTORY(ntshell), &txt[0], sizeof(txt));
|
---|
| 173 | if (0 < n) {
|
---|
| 174 | VTSEND_ERASE_LINE(ntshell);
|
---|
| 175 | VTSEND_CURSOR_HEAD(ntshell);
|
---|
| 176 | PROMPT_WRITE(ntshell);
|
---|
| 177 | SERIAL_WRITE(ntshell, txt, n);
|
---|
| 178 | text_editor_set_text(GET_EDITOR(ntshell), txt);
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /**
|
---|
| 184 | * @brief Move the cursor to left.
|
---|
| 185 | * @details This function change the state of the logical text editor and the view.
|
---|
| 186 | *
|
---|
| 187 | * @param ntshell A handler of the NT-Shell.
|
---|
| 188 | * @param action An action.
|
---|
| 189 | * @param ch A input character.
|
---|
| 190 | */
|
---|
| 191 | static void actfunc_cursor_left(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
|
---|
| 192 | {
|
---|
| 193 | UNUSED_VARIABLE(action);
|
---|
| 194 | UNUSED_VARIABLE(ch);
|
---|
| 195 | if (text_editor_cursor_left(GET_EDITOR(ntshell))) {
|
---|
| 196 | VTSEND_CURSOR_PREV(ntshell);
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /**
|
---|
| 201 | * @brief Move the cursor to right.
|
---|
| 202 | * @details This function change the state of the logical text editor and the view.
|
---|
| 203 | *
|
---|
| 204 | * @param ntshell A handler of the NT-Shell.
|
---|
| 205 | * @param action An action.
|
---|
| 206 | * @param ch A input character.
|
---|
| 207 | */
|
---|
| 208 | static void actfunc_cursor_right(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
|
---|
| 209 | {
|
---|
| 210 | UNUSED_VARIABLE(action);
|
---|
| 211 | UNUSED_VARIABLE(ch);
|
---|
| 212 | if (text_editor_cursor_right(GET_EDITOR(ntshell))) {
|
---|
| 213 | VTSEND_CURSOR_NEXT(ntshell);
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | /**
|
---|
| 218 | * @brief Process for the enter action.
|
---|
| 219 | * @details This function change the state of the logical text editor and the view.
|
---|
| 220 | *
|
---|
| 221 | * @param ntshell A handler of the NT-Shell.
|
---|
| 222 | * @param action An action.
|
---|
| 223 | * @param ch A input character.
|
---|
| 224 | */
|
---|
| 225 | static void actfunc_enter(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
|
---|
| 226 | {
|
---|
| 227 | char txt[TEXTEDITOR_MAXLEN];
|
---|
| 228 | UNUSED_VARIABLE(action);
|
---|
| 229 | UNUSED_VARIABLE(ch);
|
---|
| 230 | text_editor_get_text(GET_EDITOR(ntshell), &txt[0], sizeof(txt));
|
---|
| 231 | text_editor_clear(GET_EDITOR(ntshell));
|
---|
| 232 | text_history_write(GET_HISTORY(ntshell), txt);
|
---|
| 233 | PROMPT_NEWLINE(ntshell);
|
---|
| 234 | CALLBACK(ntshell, txt);
|
---|
| 235 | PROMPT_WRITE(ntshell);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | /**
|
---|
| 239 | * @brief Process for the cancel action.
|
---|
| 240 | * @details This function change the state of the logical text editor and the view.
|
---|
| 241 | *
|
---|
| 242 | * @note
|
---|
| 243 | * The CTRL+C operation in the general OS uses a signal.
|
---|
| 244 | * In this cancel action, It simulate only the view.
|
---|
| 245 | *
|
---|
| 246 | * @param ntshell A handler of the NT-Shell.
|
---|
| 247 | * @param action An action.
|
---|
| 248 | * @param ch A input character.
|
---|
| 249 | */
|
---|
| 250 | static void actfunc_cancel(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
|
---|
| 251 | {
|
---|
| 252 | UNUSED_VARIABLE(action);
|
---|
| 253 | UNUSED_VARIABLE(ch);
|
---|
| 254 | text_editor_clear(GET_EDITOR(ntshell));
|
---|
| 255 | SERIAL_WRITE(ntshell, "^C", 2);
|
---|
| 256 | PROMPT_NEWLINE(ntshell);
|
---|
| 257 | PROMPT_WRITE(ntshell);
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | /**
|
---|
| 261 | * @brief Process for the insert action.
|
---|
| 262 | * @details This function change the state of the logical text editor and the view.
|
---|
| 263 | *
|
---|
| 264 | * @param ntshell A handler of the NT-Shell.
|
---|
| 265 | * @param action An action.
|
---|
| 266 | * @param ch A input character.
|
---|
| 267 | */
|
---|
| 268 | static void actfunc_insert(ntshell_t *ntshell, vtrecv_action_t action, unsigned int ch)
|
---|
| 269 | {
|
---|
| 270 | char utf8[6];
|
---|
| 271 | int len8;
|
---|
| 272 |
|
---|
| 273 | UNUSED_VARIABLE(action);
|
---|
| 274 |
|
---|
| 275 | /*
|
---|
| 276 | * Reject the suggestion index number if an input action occurred.
|
---|
| 277 | */
|
---|
| 278 | SUGGEST_INDEX(ntshell) = -1;
|
---|
| 279 |
|
---|
| 280 | /*
|
---|
| 281 | * Insert the input character using the logical text editor.
|
---|
| 282 | * Update the view.
|
---|
| 283 | */
|
---|
| 284 | if ((len8 = text_editor_insert(GET_EDITOR(ntshell), ch, utf8)) > 0) {
|
---|
| 285 | char txt[TEXTEDITOR_MAXLEN];
|
---|
| 286 | int len = text_editor_get_text(GET_EDITOR(ntshell), &txt[0], sizeof(txt));
|
---|
| 287 | int pos = text_editor_cursor_get_position(GET_EDITOR(ntshell));
|
---|
| 288 | int n = len - pos;
|
---|
| 289 | SERIAL_WRITE(ntshell, utf8, len8);
|
---|
| 290 | if (n > 0) {
|
---|
| 291 | int i;
|
---|
| 292 | SERIAL_WRITE(ntshell, txt + pos, len - pos);
|
---|
| 293 | for (i = 0; i < n; i++) {
|
---|
| 294 | VTSEND_CURSOR_PREV(ntshell);
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | /**
|
---|
| 301 | * @brief Process for the backspace action.
|
---|
| 302 | * @details This function change the state of the logical text editor and the view.
|
---|
| 303 | *
|
---|
| 304 | * @param ntshell A handler of the NT-Shell.
|
---|
| 305 | * @param action An action.
|
---|
| 306 | * @param ch A input character.
|
---|
| 307 | */
|
---|
| 308 | static void actfunc_backspace(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
|
---|
| 309 | {
|
---|
| 310 | int bs_char_num;
|
---|
| 311 |
|
---|
| 312 | UNUSED_VARIABLE(action);
|
---|
| 313 | UNUSED_VARIABLE(ch);
|
---|
| 314 | bs_char_num = text_editor_backspace(GET_EDITOR(ntshell));
|
---|
| 315 | if (bs_char_num) {
|
---|
| 316 | char txt[TEXTEDITOR_MAXLEN];
|
---|
| 317 | int len = text_editor_get_text(GET_EDITOR(ntshell), &txt[0], sizeof(txt));
|
---|
| 318 | int pos = text_editor_cursor_get_position(GET_EDITOR(ntshell));
|
---|
| 319 | int n = len - pos;
|
---|
| 320 | VTSEND_CURSOR_PREV(ntshell);
|
---|
| 321 | if (n > 0) {
|
---|
| 322 | int i;
|
---|
| 323 | SERIAL_WRITE(ntshell, txt + pos, n);
|
---|
| 324 | SERIAL_WRITE(ntshell, " ", 1);
|
---|
| 325 | for (i = 0; i < n + 1; i++) {
|
---|
| 326 | VTSEND_CURSOR_PREV(ntshell);
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 | else {
|
---|
| 330 | SERIAL_WRITE(ntshell, " ", 1);
|
---|
| 331 | VTSEND_CURSOR_PREVS(ntshell, bs_char_num);
|
---|
| 332 | SERIAL_WRITE(ntshell, " ", 1);
|
---|
| 333 | VTSEND_CURSOR_PREV(ntshell);
|
---|
| 334 | }
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | /**
|
---|
| 339 | * @brief Process for the delete action.
|
---|
| 340 | * @details This function change the state of the logical text editor and the view.
|
---|
| 341 | *
|
---|
| 342 | * @param ntshell A handler of the NT-Shell.
|
---|
| 343 | * @param action An action.
|
---|
| 344 | * @param ch A input character.
|
---|
| 345 | */
|
---|
| 346 | static void actfunc_delete(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
|
---|
| 347 | {
|
---|
| 348 | UNUSED_VARIABLE(action);
|
---|
| 349 | UNUSED_VARIABLE(ch);
|
---|
| 350 | if (text_editor_delete(GET_EDITOR(ntshell))) {
|
---|
| 351 | char txt[TEXTEDITOR_MAXLEN];
|
---|
| 352 | int len = text_editor_get_text(GET_EDITOR(ntshell), &txt[0], sizeof(txt));
|
---|
| 353 | int pos = text_editor_cursor_get_position(GET_EDITOR(ntshell));
|
---|
| 354 | int n = len - pos;
|
---|
| 355 | if (n > 0) {
|
---|
| 356 | int i;
|
---|
| 357 | SERIAL_WRITE(ntshell, txt + pos, n);
|
---|
| 358 | SERIAL_WRITE(ntshell, " ", 1);
|
---|
| 359 | for (i = 0; i < n + 1; i++) {
|
---|
| 360 | VTSEND_CURSOR_PREV(ntshell);
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 | else {
|
---|
| 364 | SERIAL_WRITE(ntshell, " ", 1);
|
---|
| 365 | VTSEND_CURSOR_PREV(ntshell);
|
---|
| 366 | }
|
---|
| 367 | }
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | /**
|
---|
| 371 | * @brief Process for the suggestion action.
|
---|
| 372 | * @details This function change the state of the logical text editor and the view.
|
---|
| 373 | *
|
---|
| 374 | * @param ntshell A handler of the NT-Shell.
|
---|
| 375 | * @param action An action.
|
---|
| 376 | * @param ch A input character.
|
---|
| 377 | */
|
---|
| 378 | static void actfunc_suggest(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
|
---|
| 379 | {
|
---|
| 380 | char buf[TEXTEDITOR_MAXLEN];
|
---|
| 381 | UNUSED_VARIABLE(action);
|
---|
| 382 | UNUSED_VARIABLE(ch);
|
---|
| 383 | if (SUGGEST_INDEX(ntshell) < 0) {
|
---|
| 384 | /*
|
---|
| 385 | * Enter the input suggestion mode.
|
---|
| 386 | * Get the suggested text string with the current text string.
|
---|
| 387 | */
|
---|
| 388 | if (text_editor_get_text(
|
---|
| 389 | GET_EDITOR(ntshell),
|
---|
| 390 | SUGGEST_SOURCE(ntshell),
|
---|
| 391 | sizeof(SUGGEST_SOURCE(ntshell))) > 0) {
|
---|
| 392 | SUGGEST_INDEX(ntshell) = 0;
|
---|
| 393 | if (text_history_find(
|
---|
| 394 | GET_HISTORY(ntshell),
|
---|
| 395 | SUGGEST_INDEX(ntshell),
|
---|
| 396 | SUGGEST_SOURCE(ntshell),
|
---|
| 397 | buf,
|
---|
| 398 | sizeof(buf)) == 0) {
|
---|
| 399 | /*
|
---|
| 400 | * Found the suggestion.
|
---|
| 401 | */
|
---|
[331] | 402 | int n = ntlibc_strlen((const char *)buf);
|
---|
[279] | 403 | VTSEND_ERASE_LINE(ntshell);
|
---|
| 404 | VTSEND_CURSOR_HEAD(ntshell);
|
---|
| 405 | PROMPT_WRITE(ntshell);
|
---|
| 406 | SERIAL_WRITE(ntshell, buf, n);
|
---|
| 407 | text_editor_set_text(GET_EDITOR(ntshell), buf);
|
---|
| 408 | }
|
---|
| 409 | else {
|
---|
| 410 | /*
|
---|
| 411 | * Not found the suggestion.
|
---|
| 412 | */
|
---|
| 413 | SUGGEST_INDEX(ntshell) = -1;
|
---|
| 414 | }
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 | else {
|
---|
| 418 | /*
|
---|
| 419 | * Already the input suggestion mode.
|
---|
| 420 | * Search the next suggestion text string.
|
---|
| 421 | */
|
---|
| 422 | SUGGEST_INDEX(ntshell) = SUGGEST_INDEX(ntshell) + 1;
|
---|
| 423 | if (text_history_find(
|
---|
| 424 | GET_HISTORY(ntshell),
|
---|
| 425 | SUGGEST_INDEX(ntshell),
|
---|
| 426 | SUGGEST_SOURCE(ntshell),
|
---|
| 427 | buf,
|
---|
| 428 | sizeof(buf)) == 0) {
|
---|
| 429 | /*
|
---|
| 430 | * Found the suggestion.
|
---|
| 431 | */
|
---|
[331] | 432 | int n = ntlibc_strlen((const char *)buf);
|
---|
[279] | 433 | VTSEND_ERASE_LINE(ntshell);
|
---|
| 434 | VTSEND_CURSOR_HEAD(ntshell);
|
---|
| 435 | PROMPT_WRITE(ntshell);
|
---|
| 436 | SERIAL_WRITE(ntshell, buf, n);
|
---|
| 437 | text_editor_set_text(GET_EDITOR(ntshell), buf);
|
---|
| 438 | }
|
---|
| 439 | else {
|
---|
| 440 | /*
|
---|
| 441 | * Not found the suggestion.
|
---|
| 442 | * Recall the previous input text string.
|
---|
| 443 | */
|
---|
[331] | 444 | int n = ntlibc_strlen(SUGGEST_SOURCE(ntshell));
|
---|
[279] | 445 | VTSEND_ERASE_LINE(ntshell);
|
---|
| 446 | VTSEND_CURSOR_HEAD(ntshell);
|
---|
| 447 | PROMPT_WRITE(ntshell);
|
---|
| 448 | SERIAL_WRITE(ntshell, SUGGEST_SOURCE(ntshell), n);
|
---|
| 449 | text_editor_set_text(GET_EDITOR(ntshell), SUGGEST_SOURCE(ntshell));
|
---|
| 450 | SUGGEST_INDEX(ntshell) = -1;
|
---|
| 451 | }
|
---|
| 452 | }
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | /**
|
---|
| 456 | * @brief Move the cursor to the head of the line.
|
---|
| 457 | * @details This function change the state of the logical text editor and the view.
|
---|
| 458 | *
|
---|
| 459 | * @param ntshell A handler of the NT-Shell.
|
---|
| 460 | * @param action An action.
|
---|
| 461 | * @param ch A input character.
|
---|
| 462 | */
|
---|
| 463 | static void actfunc_cursor_head(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
|
---|
| 464 | {
|
---|
| 465 | UNUSED_VARIABLE(action);
|
---|
| 466 | UNUSED_VARIABLE(ch);
|
---|
| 467 | VTSEND_CURSOR_HEAD(ntshell);
|
---|
| 468 | PROMPT_WRITE(ntshell);
|
---|
| 469 | text_editor_cursor_head(GET_EDITOR(ntshell));
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | /**
|
---|
| 473 | * @brief Move the cursor to the tail of the line.
|
---|
| 474 | * @details This function change the state of the logical text editor and the view.
|
---|
| 475 | *
|
---|
| 476 | * @param ntshell A handler of the NT-Shell.
|
---|
| 477 | * @param action An action.
|
---|
| 478 | * @param ch A input character.
|
---|
| 479 | */
|
---|
| 480 | static void actfunc_cursor_tail(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch)
|
---|
| 481 | {
|
---|
| 482 | char buf[TEXTEDITOR_MAXLEN];
|
---|
| 483 | int len;
|
---|
| 484 | UNUSED_VARIABLE(action);
|
---|
| 485 | UNUSED_VARIABLE(ch);
|
---|
| 486 | text_editor_get_text(GET_EDITOR(ntshell), buf, sizeof(buf));
|
---|
[331] | 487 | len = ntlibc_strlen((const char *)buf);
|
---|
[279] | 488 | VTSEND_CURSOR_HEAD(ntshell);
|
---|
| 489 | PROMPT_WRITE(ntshell);
|
---|
| 490 | SERIAL_WRITE(ntshell, buf, len);
|
---|
| 491 | text_editor_cursor_tail(GET_EDITOR(ntshell));
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | /**
|
---|
| 495 | * @brief The data structure of the action table.
|
---|
| 496 | * @details
|
---|
| 497 | * The action consists from the state and the input character.
|
---|
| 498 | * This definition also have the callback function.
|
---|
| 499 | */
|
---|
| 500 | typedef struct {
|
---|
| 501 | vtrecv_action_t action;
|
---|
| 502 | unsigned char ch;
|
---|
| 503 | void(*func)(ntshell_t *ntshell, vtrecv_action_t action, unsigned char ch);
|
---|
| 504 | } ntshell_action_table_t;
|
---|
| 505 |
|
---|
| 506 | /**
|
---|
| 507 | * @brief Process function table for the actions.
|
---|
| 508 | * @details
|
---|
| 509 | * The action codes depends on the virtual terminals.
|
---|
| 510 | * So you should check some virtual terminal softwares and the environments.
|
---|
| 511 | *
|
---|
| 512 | * <table>
|
---|
| 513 | * <th>
|
---|
| 514 | * <td>Platform</td>
|
---|
| 515 | * <td>Tools</td>
|
---|
| 516 | * </th>
|
---|
| 517 | * <tr>
|
---|
| 518 | * <td>Windows</td>
|
---|
| 519 | * <td>Hyper Terminal, Poderossa, TeraTerm</td>
|
---|
| 520 | * </tr>
|
---|
| 521 | * <tr>
|
---|
| 522 | * <td>Linux</td>
|
---|
| 523 | * <td>minicom, screen, kermit</td>
|
---|
| 524 | * </tr>
|
---|
| 525 | * </table>
|
---|
| 526 | */
|
---|
| 527 | static const ntshell_action_table_t action_table[] = {
|
---|
| 528 | {VTRECV_ACTION_EXECUTE, 0x01, actfunc_cursor_head},
|
---|
| 529 | {VTRECV_ACTION_EXECUTE, 0x02, actfunc_cursor_left},
|
---|
| 530 | {VTRECV_ACTION_EXECUTE, 0x03, actfunc_cancel},
|
---|
| 531 | {VTRECV_ACTION_EXECUTE, 0x04, actfunc_delete},
|
---|
| 532 | {VTRECV_ACTION_EXECUTE, 0x05, actfunc_cursor_tail},
|
---|
| 533 | {VTRECV_ACTION_EXECUTE, 0x06, actfunc_cursor_right},
|
---|
| 534 | {VTRECV_ACTION_EXECUTE, 0x08, actfunc_backspace},
|
---|
| 535 | {VTRECV_ACTION_EXECUTE, 0x09, actfunc_suggest},
|
---|
| 536 | {VTRECV_ACTION_EXECUTE, 0x0d, actfunc_enter},
|
---|
| 537 | {VTRECV_ACTION_EXECUTE, 0x0e, actfunc_history_next},
|
---|
| 538 | {VTRECV_ACTION_EXECUTE, 0x10, actfunc_history_prev},
|
---|
| 539 | {VTRECV_ACTION_CSI_DISPATCH, 0x41, actfunc_history_prev},
|
---|
| 540 | {VTRECV_ACTION_CSI_DISPATCH, 0x42, actfunc_history_next},
|
---|
| 541 | {VTRECV_ACTION_CSI_DISPATCH, 0x43, actfunc_cursor_right},
|
---|
| 542 | {VTRECV_ACTION_CSI_DISPATCH, 0x44, actfunc_cursor_left},
|
---|
| 543 | {VTRECV_ACTION_CSI_DISPATCH, 0x7e, actfunc_delete},
|
---|
| 544 | {VTRECV_ACTION_PRINT, 0x7f, actfunc_backspace},
|
---|
| 545 | };
|
---|
| 546 |
|
---|
| 547 | /**
|
---|
| 548 | * @brief The callback function for the vtrecv module.
|
---|
| 549 | *
|
---|
| 550 | * @param vtrecv The vtrecv.
|
---|
| 551 | * @param action An action.
|
---|
| 552 | * @param ch A character.
|
---|
| 553 | */
|
---|
| 554 | void vtrecv_callback(vtrecv_t *vtrecv, vtrecv_action_t action, unsigned int ch)
|
---|
| 555 | {
|
---|
| 556 | ntshell_action_table_t *p;
|
---|
| 557 | int i;
|
---|
| 558 | const int ACTTBLSIZ = sizeof(action_table) / sizeof(action_table[0]);
|
---|
| 559 |
|
---|
| 560 | /*
|
---|
| 561 | * Search the process function for the control codes.
|
---|
| 562 | */
|
---|
| 563 | p = (ntshell_action_table_t *)action_table;
|
---|
| 564 | for (i = 0; i < ACTTBLSIZ; i++) {
|
---|
| 565 | if ((p->action == action) && (p->ch == ch)) {
|
---|
| 566 | p->func(vtrecv->user_data, action, ch);
|
---|
| 567 | return;
|
---|
| 568 | }
|
---|
| 569 | p++;
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | /*
|
---|
| 573 | * A general character is the input character.
|
---|
| 574 | */
|
---|
| 575 | if (VTRECV_ACTION_PRINT == action) {
|
---|
| 576 | actfunc_insert(vtrecv->user_data, action, ch);
|
---|
| 577 | return;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | /*
|
---|
| 581 | * Other cases, there is no defined process function for the input codes.
|
---|
| 582 | * If you need to support the input action, you should update the action table.
|
---|
| 583 | */
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | /**
|
---|
| 587 | * @brief Initialize the NT-Shell.
|
---|
| 588 | *
|
---|
| 589 | * @param p A pointer to the handler of NT-Shell.
|
---|
| 590 | * @param func_read Serial read function.
|
---|
| 591 | * @param func_write Serial write function.
|
---|
| 592 | * @param func_callback Callback function.
|
---|
| 593 | * @param extobj An external object for the callback function.
|
---|
| 594 | */
|
---|
| 595 | void ntshell_init(ntshell_t *p,
|
---|
| 596 | NTSHELL_SERIAL_READ func_read,
|
---|
| 597 | NTSHELL_SERIAL_WRITE func_write,
|
---|
| 598 | NTSHELL_USER_CALLBACK func_callback,
|
---|
| 599 | void *extobj)
|
---|
| 600 | {
|
---|
| 601 | /*
|
---|
| 602 | * The vtrecv module provides a pointer interface to an external object.
|
---|
| 603 | * NT-Shell uses the text editor, text history, read function, write function with the pointer interface.
|
---|
| 604 | */
|
---|
| 605 | p->func_read = func_read;
|
---|
| 606 | p->func_write = func_write;
|
---|
| 607 | p->func_callback = func_callback;
|
---|
| 608 | p->extobj = extobj;
|
---|
[331] | 609 | ntlibc_strcpy(p->prompt, NTSHELL_PROMPT_DEFAULT);
|
---|
[279] | 610 |
|
---|
| 611 | p->vtrecv.user_data = p;
|
---|
| 612 |
|
---|
| 613 | /*
|
---|
| 614 | * Initialize the modules.
|
---|
| 615 | */
|
---|
| 616 | vtsend_init(&(p->vtsend), func_write, extobj);
|
---|
| 617 | vtrecv_init(&(p->vtrecv), vtrecv_callback);
|
---|
| 618 | text_editor_init(GET_EDITOR(p));
|
---|
| 619 | text_history_init(GET_HISTORY(p));
|
---|
| 620 | SUGGEST_INDEX(p) = -1;
|
---|
| 621 |
|
---|
| 622 | /*
|
---|
| 623 | * Set the initialization code.
|
---|
| 624 | */
|
---|
| 625 | p->initcode = INITCODE;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | /**
|
---|
| 629 | * @brief Execute the NT-Shell.
|
---|
| 630 | * @details Never return from this function.
|
---|
| 631 | *
|
---|
| 632 | * @param p A pointer to the handler of the NT-Shell.
|
---|
| 633 | */
|
---|
| 634 | void ntshell_execute(ntshell_t *p)
|
---|
| 635 | {
|
---|
| 636 | /*
|
---|
| 637 | * Check the initialization code.
|
---|
| 638 | */
|
---|
| 639 | if (p->initcode != INITCODE) {
|
---|
| 640 | return;
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | /*
|
---|
| 644 | * User input loop.
|
---|
| 645 | */
|
---|
| 646 | PROMPT_WRITE(p);
|
---|
| 647 | while (ntshell_exit == 0) {
|
---|
| 648 | unsigned char ch;
|
---|
| 649 | SERIAL_READ(p, (char *)&ch, sizeof(ch));
|
---|
| 650 | vtrecv_execute(&(p->vtrecv), &ch, sizeof(ch));
|
---|
| 651 | }
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | /**
|
---|
| 655 | * @brief Set up the prompt of the NT-Shell.
|
---|
| 656 | *
|
---|
| 657 | * @param p A pointer to the handler of the NT-Shell.
|
---|
| 658 | * @param prompt A text string.
|
---|
| 659 | */
|
---|
| 660 | void ntshell_set_prompt(ntshell_t *p, const char *prompt)
|
---|
| 661 | {
|
---|
| 662 | /*
|
---|
| 663 | * Check the initialization code.
|
---|
| 664 | */
|
---|
| 665 | if (p->initcode != INITCODE) {
|
---|
| 666 | return;
|
---|
| 667 | }
|
---|
| 668 |
|
---|
[331] | 669 | ntlibc_strcpy(p->prompt, prompt);
|
---|
[279] | 670 | }
|
---|
| 671 |
|
---|
| 672 | /**
|
---|
| 673 | * @brief Get the version.
|
---|
| 674 | *
|
---|
| 675 | * @param major Major number.
|
---|
| 676 | * @param minor Minor number.
|
---|
| 677 | * @param release Release number.
|
---|
| 678 | */
|
---|
| 679 | void ntshell_version(int *major, int *minor, int *release)
|
---|
| 680 | {
|
---|
| 681 | *major = VERSION_MAJOR;
|
---|
| 682 | *minor = VERSION_MINOR;
|
---|
| 683 | *release = VERSION_RELEASE;
|
---|
| 684 | }
|
---|