shell.c 21 KB

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