shell.c 23 KB

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