shell.c 22 KB

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