shell.c 21 KB

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