shell.c 23 KB

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