shell.c 23 KB

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