Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

shell.c 24KB

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