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