shell.c 23 KB

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