shell.c 21 KB

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