shell.c 21 KB

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