shell.c 23 KB

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