1
0

shell.c 22 KB

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