shell.c 21 KB

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