shell.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-04-30 Bernard the first version for FinSH
  9. * 2006-05-08 Bernard change finsh thread stack to 2048
  10. * 2006-06-03 Bernard add support for skyeye
  11. * 2006-09-24 Bernard remove the code related with hardware
  12. * 2010-01-18 Bernard fix down then up key bug.
  13. * 2010-03-19 Bernard fix backspace issue and fix device read in shell.
  14. * 2010-04-01 Bernard add prompt output when start and remove the empty history
  15. * 2011-02-23 Bernard fix variable section end issue of finsh shell
  16. * initialization when use GNU GCC compiler.
  17. * 2016-11-26 armink add password authentication
  18. * 2018-07-02 aozima add custom prompt support.
  19. */
  20. #include <rthw.h>
  21. #ifdef RT_USING_FINSH
  22. #include "finsh.h"
  23. #include "shell.h"
  24. #ifdef FINSH_USING_MSH
  25. #include "msh.h"
  26. #endif
  27. #ifdef _WIN32
  28. #include <stdio.h> /* for putchar */
  29. #endif
  30. /* finsh thread */
  31. #ifndef RT_USING_HEAP
  32. static struct rt_thread finsh_thread;
  33. ALIGN(RT_ALIGN_SIZE)
  34. static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE];
  35. struct finsh_shell _shell;
  36. #endif
  37. /* finsh symtab */
  38. #ifdef FINSH_USING_SYMTAB
  39. struct finsh_syscall *_syscall_table_begin = NULL;
  40. struct finsh_syscall *_syscall_table_end = NULL;
  41. struct finsh_sysvar *_sysvar_table_begin = NULL;
  42. struct finsh_sysvar *_sysvar_table_end = NULL;
  43. #endif
  44. struct finsh_shell *shell;
  45. static char *finsh_prompt_custom = RT_NULL;
  46. #if defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__))
  47. struct finsh_syscall* finsh_syscall_next(struct finsh_syscall* call)
  48. {
  49. unsigned int *ptr;
  50. ptr = (unsigned int*) (call + 1);
  51. while ((*ptr == 0) && ((unsigned int*)ptr < (unsigned int*) _syscall_table_end))
  52. ptr ++;
  53. return (struct finsh_syscall*)ptr;
  54. }
  55. struct finsh_sysvar* finsh_sysvar_next(struct finsh_sysvar* call)
  56. {
  57. unsigned int *ptr;
  58. ptr = (unsigned int*) (call + 1);
  59. while ((*ptr == 0) && ((unsigned int*)ptr < (unsigned int*) _sysvar_table_end))
  60. ptr ++;
  61. return (struct finsh_sysvar*)ptr;
  62. }
  63. #endif /* defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__)) */
  64. #ifdef RT_USING_HEAP
  65. int finsh_set_prompt(const char * prompt)
  66. {
  67. if(finsh_prompt_custom)
  68. {
  69. rt_free(finsh_prompt_custom);
  70. finsh_prompt_custom = RT_NULL;
  71. }
  72. /* strdup */
  73. if(prompt)
  74. {
  75. finsh_prompt_custom = (char *)rt_malloc(strlen(prompt)+1);
  76. if(finsh_prompt_custom)
  77. {
  78. strcpy(finsh_prompt_custom, prompt);
  79. }
  80. }
  81. return 0;
  82. }
  83. #endif /* RT_USING_HEAP */
  84. #if defined(RT_USING_DFS)
  85. #include <dfs_posix.h>
  86. #endif /* RT_USING_DFS */
  87. const char *finsh_get_prompt(void)
  88. {
  89. #define _MSH_PROMPT "msh "
  90. #define _PROMPT "finsh "
  91. static char finsh_prompt[RT_CONSOLEBUF_SIZE + 1] = {0};
  92. /* check prompt mode */
  93. if (!shell->prompt_mode)
  94. {
  95. finsh_prompt[0] = '\0';
  96. return finsh_prompt;
  97. }
  98. if(finsh_prompt_custom)
  99. {
  100. strncpy(finsh_prompt, finsh_prompt_custom, sizeof(finsh_prompt)-1);
  101. return finsh_prompt;
  102. }
  103. #ifdef FINSH_USING_MSH
  104. if (msh_is_used()) strcpy(finsh_prompt, _MSH_PROMPT);
  105. else
  106. #endif
  107. strcpy(finsh_prompt, _PROMPT);
  108. #if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)
  109. /* get current working directory */
  110. getcwd(&finsh_prompt[rt_strlen(finsh_prompt)], RT_CONSOLEBUF_SIZE - rt_strlen(finsh_prompt));
  111. #endif
  112. strcat(finsh_prompt, ">");
  113. return finsh_prompt;
  114. }
  115. /**
  116. * @ingroup finsh
  117. *
  118. * This function get the prompt mode of finsh shell.
  119. *
  120. * @return prompt the prompt mode, 0 disable prompt mode, other values enable prompt mode.
  121. */
  122. rt_uint32_t finsh_get_prompt_mode(void)
  123. {
  124. RT_ASSERT(shell != RT_NULL);
  125. return shell->prompt_mode;
  126. }
  127. /**
  128. * @ingroup finsh
  129. *
  130. * This function set the prompt mode of finsh shell.
  131. *
  132. * The parameter 0 disable prompt mode, other values enable prompt mode.
  133. *
  134. * @param prompt the prompt mode
  135. */
  136. void finsh_set_prompt_mode(rt_uint32_t prompt_mode)
  137. {
  138. RT_ASSERT(shell != RT_NULL);
  139. shell->prompt_mode = prompt_mode;
  140. }
  141. static int finsh_getchar(void)
  142. {
  143. #ifdef RT_USING_DEVICE
  144. #ifdef RT_USING_POSIX
  145. return getchar();
  146. #else
  147. char ch = 0;
  148. RT_ASSERT(shell != RT_NULL);
  149. while (rt_device_read(shell->device, -1, &ch, 1) != 1)
  150. rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER);
  151. return (int)ch;
  152. #endif
  153. #else
  154. extern char rt_hw_console_getchar(void);
  155. return rt_hw_console_getchar();
  156. #endif
  157. }
  158. #if !defined(RT_USING_POSIX) && defined(RT_USING_DEVICE)
  159. static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
  160. {
  161. RT_ASSERT(shell != RT_NULL);
  162. /* release semaphore to let finsh thread rx data */
  163. rt_sem_release(&shell->rx_sem);
  164. return RT_EOK;
  165. }
  166. /**
  167. * @ingroup finsh
  168. *
  169. * This function sets the input device of finsh shell.
  170. *
  171. * @param device_name the name of new input device.
  172. */
  173. void finsh_set_device(const char *device_name)
  174. {
  175. rt_device_t dev = RT_NULL;
  176. RT_ASSERT(shell != RT_NULL);
  177. dev = rt_device_find(device_name);
  178. if (dev == RT_NULL)
  179. {
  180. rt_kprintf("finsh: can not find device: %s\n", device_name);
  181. return;
  182. }
  183. /* check whether it's a same device */
  184. if (dev == shell->device) return;
  185. /* open this device and set the new device in finsh shell */
  186. if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR |
  187. //RT_DEVICE_FLAG_INT_RX |
  188. RT_DEVICE_FLAG_STREAM) == RT_EOK)
  189. {
  190. if (shell->device != RT_NULL)
  191. {
  192. /* close old finsh device */
  193. rt_device_close(shell->device);
  194. rt_device_set_rx_indicate(shell->device, RT_NULL);
  195. }
  196. /* clear line buffer before switch to new device */
  197. memset(shell->line, 0, sizeof(shell->line));
  198. shell->line_curpos = shell->line_position = 0;
  199. shell->device = dev;
  200. rt_device_set_rx_indicate(dev, finsh_rx_ind);
  201. }
  202. }
  203. /**
  204. * @ingroup finsh
  205. *
  206. * This function returns current finsh shell input device.
  207. *
  208. * @return the finsh shell input device name is returned.
  209. */
  210. const char *finsh_get_device()
  211. {
  212. RT_ASSERT(shell != RT_NULL);
  213. return shell->device->parent.name;
  214. }
  215. #endif
  216. /**
  217. * @ingroup finsh
  218. *
  219. * This function set the echo mode of finsh shell.
  220. *
  221. * FINSH_OPTION_ECHO=0x01 is echo mode, other values are none-echo mode.
  222. *
  223. * @param echo the echo mode
  224. */
  225. void finsh_set_echo(rt_uint32_t echo)
  226. {
  227. RT_ASSERT(shell != RT_NULL);
  228. shell->echo_mode = (rt_uint8_t)echo;
  229. }
  230. /**
  231. * @ingroup finsh
  232. *
  233. * This function gets the echo mode of finsh shell.
  234. *
  235. * @return the echo mode
  236. */
  237. rt_uint32_t finsh_get_echo()
  238. {
  239. RT_ASSERT(shell != RT_NULL);
  240. return shell->echo_mode;
  241. }
  242. #ifdef FINSH_USING_AUTH
  243. /**
  244. * set a new password for finsh
  245. *
  246. * @param password new password
  247. *
  248. * @return result, RT_EOK on OK, -RT_ERROR on the new password length is less than
  249. * FINSH_PASSWORD_MIN or greater than FINSH_PASSWORD_MAX
  250. */
  251. rt_err_t finsh_set_password(const char *password) {
  252. rt_ubase_t level;
  253. rt_size_t pw_len = rt_strlen(password);
  254. if (pw_len < FINSH_PASSWORD_MIN || pw_len > FINSH_PASSWORD_MAX)
  255. return -RT_ERROR;
  256. level = rt_hw_interrupt_disable();
  257. rt_strncpy(shell->password, password, FINSH_PASSWORD_MAX);
  258. rt_hw_interrupt_enable(level);
  259. return RT_EOK;
  260. }
  261. /**
  262. * get the finsh password
  263. *
  264. * @return password
  265. */
  266. const char *finsh_get_password(void)
  267. {
  268. return shell->password;
  269. }
  270. static void finsh_wait_auth(void)
  271. {
  272. int ch;
  273. rt_bool_t input_finish = RT_FALSE;
  274. char password[FINSH_PASSWORD_MAX] = { 0 };
  275. rt_size_t cur_pos = 0;
  276. /* password not set */
  277. if (rt_strlen(finsh_get_password()) == 0) return;
  278. while (1)
  279. {
  280. rt_kprintf("Password for login: ");
  281. while (!input_finish)
  282. {
  283. while (1)
  284. {
  285. /* read one character from device */
  286. ch = finsh_getchar();
  287. if (ch < 0)
  288. {
  289. continue;
  290. }
  291. if (ch >= ' ' && ch <= '~' && cur_pos < FINSH_PASSWORD_MAX)
  292. {
  293. /* change the printable characters to '*' */
  294. rt_kprintf("*");
  295. password[cur_pos++] = ch;
  296. }
  297. else if (ch == '\b' && cur_pos > 0)
  298. {
  299. /* backspace */
  300. cur_pos--;
  301. password[cur_pos] = '\0';
  302. rt_kprintf("\b \b");
  303. }
  304. else if (ch == '\r' || ch == '\n')
  305. {
  306. rt_kprintf("\n");
  307. input_finish = RT_TRUE;
  308. break;
  309. }
  310. }
  311. }
  312. if (!rt_strncmp(shell->password, password, FINSH_PASSWORD_MAX)) return;
  313. else
  314. {
  315. /* authentication failed, delay 2S for retry */
  316. rt_thread_delay(2 * RT_TICK_PER_SECOND);
  317. rt_kprintf("Sorry, try again.\n");
  318. cur_pos = 0;
  319. input_finish = RT_FALSE;
  320. rt_memset(password, '\0', FINSH_PASSWORD_MAX);
  321. }
  322. }
  323. }
  324. #endif /* FINSH_USING_AUTH */
  325. static void shell_auto_complete(char *prefix)
  326. {
  327. rt_kprintf("\n");
  328. #ifdef FINSH_USING_MSH
  329. if (msh_is_used() == RT_TRUE)
  330. {
  331. msh_auto_complete(prefix);
  332. }
  333. else
  334. #endif
  335. {
  336. #ifndef FINSH_USING_MSH_ONLY
  337. extern void list_prefix(char * prefix);
  338. list_prefix(prefix);
  339. #endif
  340. }
  341. rt_kprintf("%s%s", FINSH_PROMPT, prefix);
  342. }
  343. #ifndef FINSH_USING_MSH_ONLY
  344. void finsh_run_line(struct finsh_parser *parser, const char *line)
  345. {
  346. const char *err_str;
  347. if(shell->echo_mode)
  348. rt_kprintf("\n");
  349. finsh_parser_run(parser, (unsigned char *)line);
  350. /* compile node root */
  351. if (finsh_errno() == 0)
  352. {
  353. finsh_compiler_run(parser->root);
  354. }
  355. else
  356. {
  357. err_str = finsh_error_string(finsh_errno());
  358. rt_kprintf("%s\n", err_str);
  359. }
  360. /* run virtual machine */
  361. if (finsh_errno() == 0)
  362. {
  363. char ch;
  364. finsh_vm_run();
  365. ch = (unsigned char)finsh_stack_bottom();
  366. if (ch > 0x20 && ch < 0x7e)
  367. {
  368. rt_kprintf("\t'%c', %d, 0x%08x\n",
  369. (unsigned char)finsh_stack_bottom(),
  370. (unsigned int)finsh_stack_bottom(),
  371. (unsigned int)finsh_stack_bottom());
  372. }
  373. else
  374. {
  375. rt_kprintf("\t%d, 0x%08x\n",
  376. (unsigned int)finsh_stack_bottom(),
  377. (unsigned int)finsh_stack_bottom());
  378. }
  379. }
  380. finsh_flush(parser);
  381. }
  382. #endif
  383. #ifdef FINSH_USING_HISTORY
  384. static rt_bool_t shell_handle_history(struct finsh_shell *shell)
  385. {
  386. #if defined(_WIN32)
  387. int i;
  388. rt_kprintf("\r");
  389. for (i = 0; i <= 60; i++)
  390. putchar(' ');
  391. rt_kprintf("\r");
  392. #else
  393. rt_kprintf("\033[2K\r");
  394. #endif
  395. rt_kprintf("%s%s", FINSH_PROMPT, shell->line);
  396. return RT_FALSE;
  397. }
  398. static void shell_push_history(struct finsh_shell *shell)
  399. {
  400. if (shell->line_position != 0)
  401. {
  402. /* push history */
  403. if (shell->history_count >= FINSH_HISTORY_LINES)
  404. {
  405. /* if current cmd is same as last cmd, don't push */
  406. if (memcmp(&shell->cmd_history[FINSH_HISTORY_LINES - 1], shell->line, FINSH_CMD_SIZE))
  407. {
  408. /* move history */
  409. int index;
  410. for (index = 0; index < FINSH_HISTORY_LINES - 1; index ++)
  411. {
  412. memcpy(&shell->cmd_history[index][0],
  413. &shell->cmd_history[index + 1][0], FINSH_CMD_SIZE);
  414. }
  415. memset(&shell->cmd_history[index][0], 0, FINSH_CMD_SIZE);
  416. memcpy(&shell->cmd_history[index][0], shell->line, shell->line_position);
  417. /* it's the maximum history */
  418. shell->history_count = FINSH_HISTORY_LINES;
  419. }
  420. }
  421. else
  422. {
  423. /* if current cmd is same as last cmd, don't push */
  424. if (shell->history_count == 0 || memcmp(&shell->cmd_history[shell->history_count - 1], shell->line, FINSH_CMD_SIZE))
  425. {
  426. shell->current_history = shell->history_count;
  427. memset(&shell->cmd_history[shell->history_count][0], 0, FINSH_CMD_SIZE);
  428. memcpy(&shell->cmd_history[shell->history_count][0], shell->line, shell->line_position);
  429. /* increase count and set current history position */
  430. shell->history_count ++;
  431. }
  432. }
  433. }
  434. shell->current_history = shell->history_count;
  435. }
  436. #endif
  437. void finsh_thread_entry(void *parameter)
  438. {
  439. int ch;
  440. /* normal is echo mode */
  441. #ifndef FINSH_ECHO_DISABLE_DEFAULT
  442. shell->echo_mode = 1;
  443. #else
  444. shell->echo_mode = 0;
  445. #endif
  446. #ifndef FINSH_USING_MSH_ONLY
  447. finsh_init(&shell->parser);
  448. #endif
  449. #if !defined(RT_USING_POSIX) && defined(RT_USING_DEVICE)
  450. /* set console device as shell device */
  451. if (shell->device == RT_NULL)
  452. {
  453. rt_device_t console = rt_console_get_device();
  454. if (console)
  455. {
  456. finsh_set_device(console->parent.name);
  457. }
  458. }
  459. #endif
  460. #ifdef FINSH_USING_AUTH
  461. /* set the default password when the password isn't setting */
  462. if (rt_strlen(finsh_get_password()) == 0)
  463. {
  464. if (finsh_set_password(FINSH_DEFAULT_PASSWORD) != RT_EOK)
  465. {
  466. rt_kprintf("Finsh password set failed.\n");
  467. }
  468. }
  469. /* waiting authenticate success */
  470. finsh_wait_auth();
  471. #endif
  472. rt_kprintf(FINSH_PROMPT);
  473. while (1)
  474. {
  475. ch = finsh_getchar();
  476. if (ch < 0)
  477. {
  478. continue;
  479. }
  480. /*
  481. * handle control key
  482. * up key : 0x1b 0x5b 0x41
  483. * down key: 0x1b 0x5b 0x42
  484. * right key:0x1b 0x5b 0x43
  485. * left key: 0x1b 0x5b 0x44
  486. */
  487. if (ch == 0x1b)
  488. {
  489. shell->stat = WAIT_SPEC_KEY;
  490. continue;
  491. }
  492. else if (shell->stat == WAIT_SPEC_KEY)
  493. {
  494. if (ch == 0x5b)
  495. {
  496. shell->stat = WAIT_FUNC_KEY;
  497. continue;
  498. }
  499. shell->stat = WAIT_NORMAL;
  500. }
  501. else if (shell->stat == WAIT_FUNC_KEY)
  502. {
  503. shell->stat = WAIT_NORMAL;
  504. if (ch == 0x41) /* up key */
  505. {
  506. #ifdef FINSH_USING_HISTORY
  507. /* prev history */
  508. if (shell->current_history > 0)
  509. shell->current_history --;
  510. else
  511. {
  512. shell->current_history = 0;
  513. continue;
  514. }
  515. /* copy the history command */
  516. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  517. FINSH_CMD_SIZE);
  518. shell->line_curpos = shell->line_position = strlen(shell->line);
  519. shell_handle_history(shell);
  520. #endif
  521. continue;
  522. }
  523. else if (ch == 0x42) /* down key */
  524. {
  525. #ifdef FINSH_USING_HISTORY
  526. /* next history */
  527. if (shell->current_history < shell->history_count - 1)
  528. shell->current_history ++;
  529. else
  530. {
  531. /* set to the end of history */
  532. if (shell->history_count != 0)
  533. shell->current_history = shell->history_count - 1;
  534. else
  535. continue;
  536. }
  537. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  538. FINSH_CMD_SIZE);
  539. shell->line_curpos = shell->line_position = strlen(shell->line);
  540. shell_handle_history(shell);
  541. #endif
  542. continue;
  543. }
  544. else if (ch == 0x44) /* left key */
  545. {
  546. if (shell->line_curpos)
  547. {
  548. rt_kprintf("\b");
  549. shell->line_curpos --;
  550. }
  551. continue;
  552. }
  553. else if (ch == 0x43) /* right key */
  554. {
  555. if (shell->line_curpos < shell->line_position)
  556. {
  557. rt_kprintf("%c", shell->line[shell->line_curpos]);
  558. shell->line_curpos ++;
  559. }
  560. continue;
  561. }
  562. }
  563. /* received null or error */
  564. if (ch == '\0' || ch == 0xFF) continue;
  565. /* handle tab key */
  566. else if (ch == '\t')
  567. {
  568. int i;
  569. /* move the cursor to the beginning of line */
  570. for (i = 0; i < shell->line_curpos; i++)
  571. rt_kprintf("\b");
  572. /* auto complete */
  573. shell_auto_complete(&shell->line[0]);
  574. /* re-calculate position */
  575. shell->line_curpos = shell->line_position = strlen(shell->line);
  576. continue;
  577. }
  578. /* handle backspace key */
  579. else if (ch == 0x7f || ch == 0x08)
  580. {
  581. /* note that shell->line_curpos >= 0 */
  582. if (shell->line_curpos == 0)
  583. continue;
  584. shell->line_position--;
  585. shell->line_curpos--;
  586. if (shell->line_position > shell->line_curpos)
  587. {
  588. int i;
  589. rt_memmove(&shell->line[shell->line_curpos],
  590. &shell->line[shell->line_curpos + 1],
  591. shell->line_position - shell->line_curpos);
  592. shell->line[shell->line_position] = 0;
  593. rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]);
  594. /* move the cursor to the origin position */
  595. for (i = shell->line_curpos; i <= shell->line_position; i++)
  596. rt_kprintf("\b");
  597. }
  598. else
  599. {
  600. rt_kprintf("\b \b");
  601. shell->line[shell->line_position] = 0;
  602. }
  603. continue;
  604. }
  605. /* handle end of line, break */
  606. if (ch == '\r' || ch == '\n')
  607. {
  608. #ifdef FINSH_USING_HISTORY
  609. shell_push_history(shell);
  610. #endif
  611. #ifdef FINSH_USING_MSH
  612. if (msh_is_used() == RT_TRUE)
  613. {
  614. if (shell->echo_mode)
  615. rt_kprintf("\n");
  616. msh_exec(shell->line, shell->line_position);
  617. }
  618. else
  619. #endif
  620. {
  621. #ifndef FINSH_USING_MSH_ONLY
  622. /* add ';' and run the command line */
  623. shell->line[shell->line_position] = ';';
  624. if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
  625. else
  626. if (shell->echo_mode) rt_kprintf("\n");
  627. #endif
  628. }
  629. rt_kprintf(FINSH_PROMPT);
  630. memset(shell->line, 0, sizeof(shell->line));
  631. shell->line_curpos = shell->line_position = 0;
  632. continue;
  633. }
  634. /* it's a large line, discard it */
  635. if (shell->line_position >= FINSH_CMD_SIZE)
  636. shell->line_position = 0;
  637. /* normal character */
  638. if (shell->line_curpos < shell->line_position)
  639. {
  640. int i;
  641. rt_memmove(&shell->line[shell->line_curpos + 1],
  642. &shell->line[shell->line_curpos],
  643. shell->line_position - shell->line_curpos);
  644. shell->line[shell->line_curpos] = ch;
  645. if (shell->echo_mode)
  646. rt_kprintf("%s", &shell->line[shell->line_curpos]);
  647. /* move the cursor to new position */
  648. for (i = shell->line_curpos; i < shell->line_position; i++)
  649. rt_kprintf("\b");
  650. }
  651. else
  652. {
  653. shell->line[shell->line_position] = ch;
  654. if (shell->echo_mode)
  655. rt_kprintf("%c", ch);
  656. }
  657. ch = 0;
  658. shell->line_position ++;
  659. shell->line_curpos++;
  660. if (shell->line_position >= FINSH_CMD_SIZE)
  661. {
  662. /* clear command line */
  663. shell->line_position = 0;
  664. shell->line_curpos = 0;
  665. }
  666. } /* end of device read */
  667. }
  668. void finsh_system_function_init(const void *begin, const void *end)
  669. {
  670. _syscall_table_begin = (struct finsh_syscall *) begin;
  671. _syscall_table_end = (struct finsh_syscall *) end;
  672. }
  673. void finsh_system_var_init(const void *begin, const void *end)
  674. {
  675. _sysvar_table_begin = (struct finsh_sysvar *) begin;
  676. _sysvar_table_end = (struct finsh_sysvar *) end;
  677. }
  678. #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
  679. #ifdef FINSH_USING_SYMTAB
  680. #pragma section="FSymTab"
  681. #pragma section="VSymTab"
  682. #endif
  683. #elif defined(__ADSPBLACKFIN__) /* for VisaulDSP++ Compiler*/
  684. #ifdef FINSH_USING_SYMTAB
  685. extern "asm" int __fsymtab_start;
  686. extern "asm" int __fsymtab_end;
  687. extern "asm" int __vsymtab_start;
  688. extern "asm" int __vsymtab_end;
  689. #endif
  690. #elif defined(_MSC_VER)
  691. #pragma section("FSymTab$a", read)
  692. const char __fsym_begin_name[] = "__start";
  693. const char __fsym_begin_desc[] = "begin of finsh";
  694. __declspec(allocate("FSymTab$a")) const struct finsh_syscall __fsym_begin =
  695. {
  696. __fsym_begin_name,
  697. __fsym_begin_desc,
  698. NULL
  699. };
  700. #pragma section("FSymTab$z", read)
  701. const char __fsym_end_name[] = "__end";
  702. const char __fsym_end_desc[] = "end of finsh";
  703. __declspec(allocate("FSymTab$z")) const struct finsh_syscall __fsym_end =
  704. {
  705. __fsym_end_name,
  706. __fsym_end_desc,
  707. NULL
  708. };
  709. #endif
  710. /*
  711. * @ingroup finsh
  712. *
  713. * This function will initialize finsh shell
  714. */
  715. int finsh_system_init(void)
  716. {
  717. rt_err_t result = RT_EOK;
  718. rt_thread_t tid;
  719. #ifdef FINSH_USING_SYMTAB
  720. #if defined(__CC_ARM) || defined(__CLANG_ARM) /* ARM C Compiler */
  721. extern const int FSymTab$$Base;
  722. extern const int FSymTab$$Limit;
  723. extern const int VSymTab$$Base;
  724. extern const int VSymTab$$Limit;
  725. finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
  726. #ifndef FINSH_USING_MSH_ONLY
  727. finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
  728. #endif
  729. #elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
  730. finsh_system_function_init(__section_begin("FSymTab"),
  731. __section_end("FSymTab"));
  732. finsh_system_var_init(__section_begin("VSymTab"),
  733. __section_end("VSymTab"));
  734. #elif defined (__GNUC__) || defined(__TI_COMPILER_VERSION__)
  735. /* GNU GCC Compiler and TI CCS */
  736. extern const int __fsymtab_start;
  737. extern const int __fsymtab_end;
  738. extern const int __vsymtab_start;
  739. extern const int __vsymtab_end;
  740. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  741. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  742. #elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
  743. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  744. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  745. #elif defined(_MSC_VER)
  746. unsigned int *ptr_begin, *ptr_end;
  747. if(shell)
  748. {
  749. rt_kprintf("finsh shell already init.\n");
  750. return RT_EOK;
  751. }
  752. ptr_begin = (unsigned int *)&__fsym_begin;
  753. ptr_begin += (sizeof(struct finsh_syscall) / sizeof(unsigned int));
  754. while (*ptr_begin == 0) ptr_begin ++;
  755. ptr_end = (unsigned int *) &__fsym_end;
  756. ptr_end --;
  757. while (*ptr_end == 0) ptr_end --;
  758. finsh_system_function_init(ptr_begin, ptr_end);
  759. #endif
  760. #endif
  761. #ifdef RT_USING_HEAP
  762. /* create or set shell structure */
  763. shell = (struct finsh_shell *)rt_calloc(1, sizeof(struct finsh_shell));
  764. if (shell == RT_NULL)
  765. {
  766. rt_kprintf("no memory for shell\n");
  767. return -1;
  768. }
  769. tid = rt_thread_create(FINSH_THREAD_NAME,
  770. finsh_thread_entry, RT_NULL,
  771. FINSH_THREAD_STACK_SIZE, FINSH_THREAD_PRIORITY, 10);
  772. #else
  773. shell = &_shell;
  774. tid = &finsh_thread;
  775. result = rt_thread_init(&finsh_thread,
  776. FINSH_THREAD_NAME,
  777. finsh_thread_entry, RT_NULL,
  778. &finsh_thread_stack[0], sizeof(finsh_thread_stack),
  779. FINSH_THREAD_PRIORITY, 10);
  780. #endif /* RT_USING_HEAP */
  781. rt_sem_init(&(shell->rx_sem), "shrx", 0, 0);
  782. finsh_set_prompt_mode(1);
  783. if (tid != NULL && result == RT_EOK)
  784. rt_thread_startup(tid);
  785. return 0;
  786. }
  787. INIT_APP_EXPORT(finsh_system_init);
  788. #endif /* RT_USING_FINSH */