shell.c 22 KB

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