shell.c 22 KB

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