shell.c 22 KB

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