shell.c 21 KB

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