shell.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 = 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. rt_kprintf("finsh getchar error\n");
  259. continue;
  260. }
  261. if (ch >= ' ' && ch <= '~' && cur_pos < FINSH_PASSWORD_MAX)
  262. {
  263. /* change the printable characters to '*' */
  264. rt_kprintf("*");
  265. password[cur_pos++] = ch;
  266. }
  267. else if (ch == '\b' && cur_pos > 0)
  268. {
  269. /* backspace */
  270. password[cur_pos] = '\0';
  271. cur_pos--;
  272. rt_kprintf("\b \b");
  273. }
  274. else if (ch == '\r' || ch == '\n')
  275. {
  276. rt_kprintf("\n");
  277. input_finish = RT_TRUE;
  278. break;
  279. }
  280. }
  281. }
  282. if (!rt_strncmp(shell->password, password, FINSH_PASSWORD_MAX)) return;
  283. else
  284. {
  285. /* authentication failed, delay 2S for retry */
  286. rt_thread_delay(2 * RT_TICK_PER_SECOND);
  287. rt_kprintf("Sorry, try again.\n");
  288. cur_pos = 0;
  289. input_finish = RT_FALSE;
  290. rt_memset(password, '\0', FINSH_PASSWORD_MAX);
  291. }
  292. }
  293. }
  294. #endif /* FINSH_USING_AUTH */
  295. static void shell_auto_complete(char *prefix)
  296. {
  297. rt_kprintf("\n");
  298. #ifdef FINSH_USING_MSH
  299. if (msh_is_used() == RT_TRUE)
  300. {
  301. msh_auto_complete(prefix);
  302. }
  303. else
  304. #endif
  305. {
  306. #ifndef FINSH_USING_MSH_ONLY
  307. extern void list_prefix(char * prefix);
  308. list_prefix(prefix);
  309. #endif
  310. }
  311. rt_kprintf("%s%s", FINSH_PROMPT, prefix);
  312. }
  313. #ifndef FINSH_USING_MSH_ONLY
  314. void finsh_run_line(struct finsh_parser *parser, const char *line)
  315. {
  316. const char *err_str;
  317. if(shell->echo_mode)
  318. rt_kprintf("\n");
  319. finsh_parser_run(parser, (unsigned char *)line);
  320. /* compile node root */
  321. if (finsh_errno() == 0)
  322. {
  323. finsh_compiler_run(parser->root);
  324. }
  325. else
  326. {
  327. err_str = finsh_error_string(finsh_errno());
  328. rt_kprintf("%s\n", err_str);
  329. }
  330. /* run virtual machine */
  331. if (finsh_errno() == 0)
  332. {
  333. char ch;
  334. finsh_vm_run();
  335. ch = (unsigned char)finsh_stack_bottom();
  336. if (ch > 0x20 && ch < 0x7e)
  337. {
  338. rt_kprintf("\t'%c', %d, 0x%08x\n",
  339. (unsigned char)finsh_stack_bottom(),
  340. (unsigned int)finsh_stack_bottom(),
  341. (unsigned int)finsh_stack_bottom());
  342. }
  343. else
  344. {
  345. rt_kprintf("\t%d, 0x%08x\n",
  346. (unsigned int)finsh_stack_bottom(),
  347. (unsigned int)finsh_stack_bottom());
  348. }
  349. }
  350. finsh_flush(parser);
  351. }
  352. #endif
  353. #ifdef FINSH_USING_HISTORY
  354. static rt_bool_t shell_handle_history(struct finsh_shell *shell)
  355. {
  356. #if defined(_WIN32)
  357. int i;
  358. rt_kprintf("\r");
  359. for (i = 0; i <= 60; i++)
  360. putchar(' ');
  361. rt_kprintf("\r");
  362. #else
  363. rt_kprintf("\033[2K\r");
  364. #endif
  365. rt_kprintf("%s%s", FINSH_PROMPT, shell->line);
  366. return RT_FALSE;
  367. }
  368. static void shell_push_history(struct finsh_shell *shell)
  369. {
  370. if (shell->line_position != 0)
  371. {
  372. /* push history */
  373. if (shell->history_count >= FINSH_HISTORY_LINES)
  374. {
  375. /* if current cmd is same as last cmd, don't push */
  376. if (memcmp(&shell->cmd_history[FINSH_HISTORY_LINES - 1], shell->line, FINSH_CMD_SIZE))
  377. {
  378. /* move history */
  379. int index;
  380. for (index = 0; index < FINSH_HISTORY_LINES - 1; index ++)
  381. {
  382. memcpy(&shell->cmd_history[index][0],
  383. &shell->cmd_history[index + 1][0], FINSH_CMD_SIZE);
  384. }
  385. memset(&shell->cmd_history[index][0], 0, FINSH_CMD_SIZE);
  386. memcpy(&shell->cmd_history[index][0], shell->line, shell->line_position);
  387. /* it's the maximum history */
  388. shell->history_count = FINSH_HISTORY_LINES;
  389. }
  390. }
  391. else
  392. {
  393. /* if current cmd is same as last cmd, don't push */
  394. if (shell->history_count == 0 || memcmp(&shell->cmd_history[shell->history_count - 1], shell->line, FINSH_CMD_SIZE))
  395. {
  396. shell->current_history = shell->history_count;
  397. memset(&shell->cmd_history[shell->history_count][0], 0, FINSH_CMD_SIZE);
  398. memcpy(&shell->cmd_history[shell->history_count][0], shell->line, shell->line_position);
  399. /* increase count and set current history position */
  400. shell->history_count ++;
  401. }
  402. }
  403. }
  404. shell->current_history = shell->history_count;
  405. }
  406. #endif
  407. void finsh_thread_entry(void *parameter)
  408. {
  409. int ch;
  410. /* normal is echo mode */
  411. #ifndef FINSH_ECHO_DISABLE_DEFAULT
  412. shell->echo_mode = 1;
  413. #else
  414. shell->echo_mode = 0;
  415. #endif
  416. #ifndef FINSH_USING_MSH_ONLY
  417. finsh_init(&shell->parser);
  418. #endif
  419. #ifndef RT_USING_POSIX
  420. /* set console device as shell device */
  421. if (shell->device == RT_NULL)
  422. {
  423. rt_device_t console = rt_console_get_device();
  424. if (console)
  425. {
  426. finsh_set_device(console->parent.name);
  427. }
  428. }
  429. #endif
  430. #ifdef FINSH_USING_AUTH
  431. /* set the default password when the password isn't setting */
  432. if (rt_strlen(finsh_get_password()) == 0)
  433. {
  434. if (finsh_set_password(FINSH_DEFAULT_PASSWORD) != RT_EOK)
  435. {
  436. rt_kprintf("Finsh password set failed.\n");
  437. }
  438. }
  439. /* waiting authenticate success */
  440. finsh_wait_auth();
  441. #endif
  442. rt_kprintf(FINSH_PROMPT);
  443. while (1)
  444. {
  445. ch = finsh_getchar();
  446. if (ch < 0)
  447. {
  448. rt_kprintf("finsh getchar error\n");
  449. continue;
  450. }
  451. /*
  452. * handle control key
  453. * up key : 0x1b 0x5b 0x41
  454. * down key: 0x1b 0x5b 0x42
  455. * right key:0x1b 0x5b 0x43
  456. * left key: 0x1b 0x5b 0x44
  457. */
  458. if (ch == 0x1b)
  459. {
  460. shell->stat = WAIT_SPEC_KEY;
  461. continue;
  462. }
  463. else if (shell->stat == WAIT_SPEC_KEY)
  464. {
  465. if (ch == 0x5b)
  466. {
  467. shell->stat = WAIT_FUNC_KEY;
  468. continue;
  469. }
  470. shell->stat = WAIT_NORMAL;
  471. }
  472. else if (shell->stat == WAIT_FUNC_KEY)
  473. {
  474. shell->stat = WAIT_NORMAL;
  475. if (ch == 0x41) /* up key */
  476. {
  477. #ifdef FINSH_USING_HISTORY
  478. /* prev history */
  479. if (shell->current_history > 0)
  480. shell->current_history --;
  481. else
  482. {
  483. shell->current_history = 0;
  484. continue;
  485. }
  486. /* copy the history command */
  487. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  488. FINSH_CMD_SIZE);
  489. shell->line_curpos = shell->line_position = strlen(shell->line);
  490. shell_handle_history(shell);
  491. #endif
  492. continue;
  493. }
  494. else if (ch == 0x42) /* down key */
  495. {
  496. #ifdef FINSH_USING_HISTORY
  497. /* next history */
  498. if (shell->current_history < shell->history_count - 1)
  499. shell->current_history ++;
  500. else
  501. {
  502. /* set to the end of history */
  503. if (shell->history_count != 0)
  504. shell->current_history = shell->history_count - 1;
  505. else
  506. continue;
  507. }
  508. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  509. FINSH_CMD_SIZE);
  510. shell->line_curpos = shell->line_position = strlen(shell->line);
  511. shell_handle_history(shell);
  512. #endif
  513. continue;
  514. }
  515. else if (ch == 0x44) /* left key */
  516. {
  517. if (shell->line_curpos)
  518. {
  519. rt_kprintf("\b");
  520. shell->line_curpos --;
  521. }
  522. continue;
  523. }
  524. else if (ch == 0x43) /* right key */
  525. {
  526. if (shell->line_curpos < shell->line_position)
  527. {
  528. rt_kprintf("%c", shell->line[shell->line_curpos]);
  529. shell->line_curpos ++;
  530. }
  531. continue;
  532. }
  533. }
  534. /* received null or error */
  535. if (ch == '\0' || ch == 0xFF) continue;
  536. /* handle tab key */
  537. else if (ch == '\t')
  538. {
  539. int i;
  540. /* move the cursor to the beginning of line */
  541. for (i = 0; i < shell->line_curpos; i++)
  542. rt_kprintf("\b");
  543. /* auto complete */
  544. shell_auto_complete(&shell->line[0]);
  545. /* re-calculate position */
  546. shell->line_curpos = shell->line_position = strlen(shell->line);
  547. continue;
  548. }
  549. /* handle backspace key */
  550. else if (ch == 0x7f || ch == 0x08)
  551. {
  552. /* note that shell->line_curpos >= 0 */
  553. if (shell->line_curpos == 0)
  554. continue;
  555. shell->line_position--;
  556. shell->line_curpos--;
  557. if (shell->line_position > shell->line_curpos)
  558. {
  559. int i;
  560. rt_memmove(&shell->line[shell->line_curpos],
  561. &shell->line[shell->line_curpos + 1],
  562. shell->line_position - shell->line_curpos);
  563. shell->line[shell->line_position] = 0;
  564. rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]);
  565. /* move the cursor to the origin position */
  566. for (i = shell->line_curpos; i <= shell->line_position; i++)
  567. rt_kprintf("\b");
  568. }
  569. else
  570. {
  571. rt_kprintf("\b \b");
  572. shell->line[shell->line_position] = 0;
  573. }
  574. continue;
  575. }
  576. /* handle end of line, break */
  577. if (ch == '\r' || ch == '\n')
  578. {
  579. #ifdef FINSH_USING_HISTORY
  580. shell_push_history(shell);
  581. #endif
  582. #ifdef FINSH_USING_MSH
  583. if (msh_is_used() == RT_TRUE)
  584. {
  585. if (shell->echo_mode)
  586. rt_kprintf("\n");
  587. msh_exec(shell->line, shell->line_position);
  588. }
  589. else
  590. #endif
  591. {
  592. #ifndef FINSH_USING_MSH_ONLY
  593. /* add ';' and run the command line */
  594. shell->line[shell->line_position] = ';';
  595. if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
  596. else
  597. if (shell->echo_mode) rt_kprintf("\n");
  598. #endif
  599. }
  600. rt_kprintf(FINSH_PROMPT);
  601. memset(shell->line, 0, sizeof(shell->line));
  602. shell->line_curpos = shell->line_position = 0;
  603. continue;
  604. }
  605. /* it's a large line, discard it */
  606. if (shell->line_position >= FINSH_CMD_SIZE)
  607. shell->line_position = 0;
  608. /* normal character */
  609. if (shell->line_curpos < shell->line_position)
  610. {
  611. int i;
  612. rt_memmove(&shell->line[shell->line_curpos + 1],
  613. &shell->line[shell->line_curpos],
  614. shell->line_position - shell->line_curpos);
  615. shell->line[shell->line_curpos] = ch;
  616. if (shell->echo_mode)
  617. rt_kprintf("%s", &shell->line[shell->line_curpos]);
  618. /* move the cursor to new position */
  619. for (i = shell->line_curpos; i < shell->line_position; i++)
  620. rt_kprintf("\b");
  621. }
  622. else
  623. {
  624. shell->line[shell->line_position] = ch;
  625. if (shell->echo_mode)
  626. rt_kprintf("%c", ch);
  627. }
  628. ch = 0;
  629. shell->line_position ++;
  630. shell->line_curpos++;
  631. if (shell->line_position >= FINSH_CMD_SIZE)
  632. {
  633. /* clear command line */
  634. shell->line_position = 0;
  635. shell->line_curpos = 0;
  636. }
  637. } /* end of device read */
  638. }
  639. void finsh_system_function_init(const void *begin, const void *end)
  640. {
  641. _syscall_table_begin = (struct finsh_syscall *) begin;
  642. _syscall_table_end = (struct finsh_syscall *) end;
  643. }
  644. void finsh_system_var_init(const void *begin, const void *end)
  645. {
  646. _sysvar_table_begin = (struct finsh_sysvar *) begin;
  647. _sysvar_table_end = (struct finsh_sysvar *) end;
  648. }
  649. #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
  650. #ifdef FINSH_USING_SYMTAB
  651. #pragma section="FSymTab"
  652. #pragma section="VSymTab"
  653. #endif
  654. #elif defined(__ADSPBLACKFIN__) /* for VisaulDSP++ Compiler*/
  655. #ifdef FINSH_USING_SYMTAB
  656. extern "asm" int __fsymtab_start;
  657. extern "asm" int __fsymtab_end;
  658. extern "asm" int __vsymtab_start;
  659. extern "asm" int __vsymtab_end;
  660. #endif
  661. #elif defined(_MSC_VER)
  662. #pragma section("FSymTab$a", read)
  663. const char __fsym_begin_name[] = "__start";
  664. const char __fsym_begin_desc[] = "begin of finsh";
  665. __declspec(allocate("FSymTab$a")) const struct finsh_syscall __fsym_begin =
  666. {
  667. __fsym_begin_name,
  668. __fsym_begin_desc,
  669. NULL
  670. };
  671. #pragma section("FSymTab$z", read)
  672. const char __fsym_end_name[] = "__end";
  673. const char __fsym_end_desc[] = "end of finsh";
  674. __declspec(allocate("FSymTab$z")) const struct finsh_syscall __fsym_end =
  675. {
  676. __fsym_end_name,
  677. __fsym_end_desc,
  678. NULL
  679. };
  680. #endif
  681. /*
  682. * @ingroup finsh
  683. *
  684. * This function will initialize finsh shell
  685. */
  686. int finsh_system_init(void)
  687. {
  688. rt_err_t result = RT_EOK;
  689. rt_thread_t tid;
  690. #ifdef FINSH_USING_SYMTAB
  691. #if defined(__CC_ARM) || defined(__CLANG_ARM) /* ARM C Compiler */
  692. extern const int FSymTab$$Base;
  693. extern const int FSymTab$$Limit;
  694. extern const int VSymTab$$Base;
  695. extern const int VSymTab$$Limit;
  696. finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
  697. #ifndef FINSH_USING_MSH_ONLY
  698. finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
  699. #endif
  700. #elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
  701. finsh_system_function_init(__section_begin("FSymTab"),
  702. __section_end("FSymTab"));
  703. finsh_system_var_init(__section_begin("VSymTab"),
  704. __section_end("VSymTab"));
  705. #elif defined (__GNUC__) || defined(__TI_COMPILER_VERSION__)
  706. /* GNU GCC Compiler and TI CCS */
  707. extern const int __fsymtab_start;
  708. extern const int __fsymtab_end;
  709. extern const int __vsymtab_start;
  710. extern const int __vsymtab_end;
  711. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  712. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  713. #elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
  714. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  715. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  716. #elif defined(_MSC_VER)
  717. unsigned int *ptr_begin, *ptr_end;
  718. if(shell)
  719. {
  720. rt_kprintf("finsh shell already init.\n");
  721. return RT_EOK;
  722. }
  723. ptr_begin = (unsigned int *)&__fsym_begin;
  724. ptr_begin += (sizeof(struct finsh_syscall) / sizeof(unsigned int));
  725. while (*ptr_begin == 0) ptr_begin ++;
  726. ptr_end = (unsigned int *) &__fsym_end;
  727. ptr_end --;
  728. while (*ptr_end == 0) ptr_end --;
  729. finsh_system_function_init(ptr_begin, ptr_end);
  730. #endif
  731. #endif
  732. #ifdef RT_USING_HEAP
  733. /* create or set shell structure */
  734. shell = (struct finsh_shell *)rt_calloc(1, sizeof(struct finsh_shell));
  735. if (shell == RT_NULL)
  736. {
  737. rt_kprintf("no memory for shell\n");
  738. return -1;
  739. }
  740. tid = rt_thread_create(FINSH_THREAD_NAME,
  741. finsh_thread_entry, RT_NULL,
  742. FINSH_THREAD_STACK_SIZE, FINSH_THREAD_PRIORITY, 10);
  743. #else
  744. shell = &_shell;
  745. tid = &finsh_thread;
  746. result = rt_thread_init(&finsh_thread,
  747. FINSH_THREAD_NAME,
  748. finsh_thread_entry, RT_NULL,
  749. &finsh_thread_stack[0], sizeof(finsh_thread_stack),
  750. FINSH_THREAD_PRIORITY, 10);
  751. #endif /* RT_USING_HEAP */
  752. rt_sem_init(&(shell->rx_sem), "shrx", 0, 0);
  753. finsh_set_prompt_mode(1);
  754. if (tid != NULL && result == RT_EOK)
  755. rt_thread_startup(tid);
  756. return 0;
  757. }
  758. INIT_APP_EXPORT(finsh_system_init);
  759. #endif /* RT_USING_FINSH */