shell.c 21 KB

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