shell.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. #ifndef FINSH_ECHO_DISABLE_DEFAULT
  368. shell->echo_mode = 1;
  369. #else
  370. shell->echo_mode = 0;
  371. #endif
  372. #ifndef FINSH_USING_MSH_ONLY
  373. finsh_init(&shell->parser);
  374. #endif
  375. #ifndef RT_USING_POSIX
  376. /* set console device as shell device */
  377. if (shell->device == RT_NULL)
  378. {
  379. rt_device_t console = rt_console_get_device();
  380. if (console)
  381. {
  382. finsh_set_device(console->parent.name);
  383. }
  384. }
  385. #endif
  386. #ifdef FINSH_USING_AUTH
  387. /* set the default password when the password isn't setting */
  388. if (rt_strlen(finsh_get_password()) == 0)
  389. {
  390. if (finsh_set_password(FINSH_DEFAULT_PASSWORD) != RT_EOK)
  391. {
  392. rt_kprintf("Finsh password set failed.\n");
  393. }
  394. }
  395. /* waiting authenticate success */
  396. finsh_wait_auth();
  397. #endif
  398. rt_kprintf(FINSH_PROMPT);
  399. while (1)
  400. {
  401. ch = finsh_getchar();
  402. /*
  403. * handle control key
  404. * up key : 0x1b 0x5b 0x41
  405. * down key: 0x1b 0x5b 0x42
  406. * right key:0x1b 0x5b 0x43
  407. * left key: 0x1b 0x5b 0x44
  408. */
  409. if (ch == 0x1b)
  410. {
  411. shell->stat = WAIT_SPEC_KEY;
  412. continue;
  413. }
  414. else if (shell->stat == WAIT_SPEC_KEY)
  415. {
  416. if (ch == 0x5b)
  417. {
  418. shell->stat = WAIT_FUNC_KEY;
  419. continue;
  420. }
  421. shell->stat = WAIT_NORMAL;
  422. }
  423. else if (shell->stat == WAIT_FUNC_KEY)
  424. {
  425. shell->stat = WAIT_NORMAL;
  426. if (ch == 0x41) /* up key */
  427. {
  428. #ifdef FINSH_USING_HISTORY
  429. /* prev history */
  430. if (shell->current_history > 0)
  431. shell->current_history --;
  432. else
  433. {
  434. shell->current_history = 0;
  435. continue;
  436. }
  437. /* copy the history command */
  438. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  439. FINSH_CMD_SIZE);
  440. shell->line_curpos = shell->line_position = strlen(shell->line);
  441. shell_handle_history(shell);
  442. #endif
  443. continue;
  444. }
  445. else if (ch == 0x42) /* down key */
  446. {
  447. #ifdef FINSH_USING_HISTORY
  448. /* next history */
  449. if (shell->current_history < shell->history_count - 1)
  450. shell->current_history ++;
  451. else
  452. {
  453. /* set to the end of history */
  454. if (shell->history_count != 0)
  455. shell->current_history = shell->history_count - 1;
  456. else
  457. continue;
  458. }
  459. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  460. FINSH_CMD_SIZE);
  461. shell->line_curpos = shell->line_position = strlen(shell->line);
  462. shell_handle_history(shell);
  463. #endif
  464. continue;
  465. }
  466. else if (ch == 0x44) /* left key */
  467. {
  468. if (shell->line_curpos)
  469. {
  470. rt_kprintf("\b");
  471. shell->line_curpos --;
  472. }
  473. continue;
  474. }
  475. else if (ch == 0x43) /* right key */
  476. {
  477. if (shell->line_curpos < shell->line_position)
  478. {
  479. rt_kprintf("%c", shell->line[shell->line_curpos]);
  480. shell->line_curpos ++;
  481. }
  482. continue;
  483. }
  484. }
  485. /* received null or error */
  486. if (ch == '\0' || ch == 0xFF) continue;
  487. /* handle tab key */
  488. else if (ch == '\t')
  489. {
  490. int i;
  491. /* move the cursor to the beginning of line */
  492. for (i = 0; i < shell->line_curpos; i++)
  493. rt_kprintf("\b");
  494. /* auto complete */
  495. shell_auto_complete(&shell->line[0]);
  496. /* re-calculate position */
  497. shell->line_curpos = shell->line_position = strlen(shell->line);
  498. continue;
  499. }
  500. /* handle backspace key */
  501. else if (ch == 0x7f || ch == 0x08)
  502. {
  503. /* note that shell->line_curpos >= 0 */
  504. if (shell->line_curpos == 0)
  505. continue;
  506. shell->line_position--;
  507. shell->line_curpos--;
  508. if (shell->line_position > shell->line_curpos)
  509. {
  510. int i;
  511. rt_memmove(&shell->line[shell->line_curpos],
  512. &shell->line[shell->line_curpos + 1],
  513. shell->line_position - shell->line_curpos);
  514. shell->line[shell->line_position] = 0;
  515. rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]);
  516. /* move the cursor to the origin position */
  517. for (i = shell->line_curpos; i <= shell->line_position; i++)
  518. rt_kprintf("\b");
  519. }
  520. else
  521. {
  522. rt_kprintf("\b \b");
  523. shell->line[shell->line_position] = 0;
  524. }
  525. continue;
  526. }
  527. /* handle end of line, break */
  528. if (ch == '\r' || ch == '\n')
  529. {
  530. #ifdef FINSH_USING_HISTORY
  531. shell_push_history(shell);
  532. #endif
  533. #ifdef FINSH_USING_MSH
  534. if (msh_is_used() == RT_TRUE)
  535. {
  536. if (shell->echo_mode)
  537. rt_kprintf("\n");
  538. msh_exec(shell->line, shell->line_position);
  539. }
  540. else
  541. #endif
  542. {
  543. #ifndef FINSH_USING_MSH_ONLY
  544. /* add ';' and run the command line */
  545. shell->line[shell->line_position] = ';';
  546. if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
  547. else
  548. if (shell->echo_mode) rt_kprintf("\n");
  549. #endif
  550. }
  551. rt_kprintf(FINSH_PROMPT);
  552. memset(shell->line, 0, sizeof(shell->line));
  553. shell->line_curpos = shell->line_position = 0;
  554. continue;
  555. }
  556. /* it's a large line, discard it */
  557. if (shell->line_position >= FINSH_CMD_SIZE)
  558. shell->line_position = 0;
  559. /* normal character */
  560. if (shell->line_curpos < shell->line_position)
  561. {
  562. int i;
  563. rt_memmove(&shell->line[shell->line_curpos + 1],
  564. &shell->line[shell->line_curpos],
  565. shell->line_position - shell->line_curpos);
  566. shell->line[shell->line_curpos] = ch;
  567. if (shell->echo_mode)
  568. rt_kprintf("%s", &shell->line[shell->line_curpos]);
  569. /* move the cursor to new position */
  570. for (i = shell->line_curpos; i < shell->line_position; i++)
  571. rt_kprintf("\b");
  572. }
  573. else
  574. {
  575. shell->line[shell->line_position] = ch;
  576. if (shell->echo_mode)
  577. rt_kprintf("%c", ch);
  578. }
  579. ch = 0;
  580. shell->line_position ++;
  581. shell->line_curpos++;
  582. if (shell->line_position >= FINSH_CMD_SIZE)
  583. {
  584. /* clear command line */
  585. shell->line_position = 0;
  586. shell->line_curpos = 0;
  587. }
  588. } /* end of device read */
  589. }
  590. void finsh_system_function_init(const void *begin, const void *end)
  591. {
  592. _syscall_table_begin = (struct finsh_syscall *) begin;
  593. _syscall_table_end = (struct finsh_syscall *) end;
  594. }
  595. void finsh_system_var_init(const void *begin, const void *end)
  596. {
  597. _sysvar_table_begin = (struct finsh_sysvar *) begin;
  598. _sysvar_table_end = (struct finsh_sysvar *) end;
  599. }
  600. #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
  601. #ifdef FINSH_USING_SYMTAB
  602. #pragma section="FSymTab"
  603. #pragma section="VSymTab"
  604. #endif
  605. #elif defined(__ADSPBLACKFIN__) /* for VisaulDSP++ Compiler*/
  606. #ifdef FINSH_USING_SYMTAB
  607. extern "asm" int __fsymtab_start;
  608. extern "asm" int __fsymtab_end;
  609. extern "asm" int __vsymtab_start;
  610. extern "asm" int __vsymtab_end;
  611. #endif
  612. #elif defined(_MSC_VER)
  613. #pragma section("FSymTab$a", read)
  614. const char __fsym_begin_name[] = "__start";
  615. const char __fsym_begin_desc[] = "begin of finsh";
  616. __declspec(allocate("FSymTab$a")) const struct finsh_syscall __fsym_begin =
  617. {
  618. __fsym_begin_name,
  619. __fsym_begin_desc,
  620. NULL
  621. };
  622. #pragma section("FSymTab$z", read)
  623. const char __fsym_end_name[] = "__end";
  624. const char __fsym_end_desc[] = "end of finsh";
  625. __declspec(allocate("FSymTab$z")) const struct finsh_syscall __fsym_end =
  626. {
  627. __fsym_end_name,
  628. __fsym_end_desc,
  629. NULL
  630. };
  631. #endif
  632. /*
  633. * @ingroup finsh
  634. *
  635. * This function will initialize finsh shell
  636. */
  637. int finsh_system_init(void)
  638. {
  639. rt_err_t result;
  640. #ifdef FINSH_USING_SYMTAB
  641. #ifdef __CC_ARM /* ARM C Compiler */
  642. extern const int FSymTab$$Base;
  643. extern const int FSymTab$$Limit;
  644. extern const int VSymTab$$Base;
  645. extern const int VSymTab$$Limit;
  646. finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
  647. #ifndef FINSH_USING_MSH_ONLY
  648. finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
  649. #endif
  650. #elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
  651. finsh_system_function_init(__section_begin("FSymTab"),
  652. __section_end("FSymTab"));
  653. finsh_system_var_init(__section_begin("VSymTab"),
  654. __section_end("VSymTab"));
  655. #elif defined (__GNUC__) || defined(__TI_COMPILER_VERSION__)
  656. /* GNU GCC Compiler and TI CCS */
  657. extern const int __fsymtab_start;
  658. extern const int __fsymtab_end;
  659. extern const int __vsymtab_start;
  660. extern const int __vsymtab_end;
  661. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  662. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  663. #elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
  664. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  665. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  666. #elif defined(_MSC_VER)
  667. unsigned int *ptr_begin, *ptr_end;
  668. ptr_begin = (unsigned int *)&__fsym_begin;
  669. ptr_begin += (sizeof(struct finsh_syscall) / sizeof(unsigned int));
  670. while (*ptr_begin == 0) ptr_begin ++;
  671. ptr_end = (unsigned int *) &__fsym_end;
  672. ptr_end --;
  673. while (*ptr_end == 0) ptr_end --;
  674. finsh_system_function_init(ptr_begin, ptr_end);
  675. #endif
  676. #endif
  677. /* create or set shell structure */
  678. #ifdef RT_USING_HEAP
  679. shell = (struct finsh_shell *)rt_malloc(sizeof(struct finsh_shell));
  680. if (shell == RT_NULL)
  681. {
  682. rt_kprintf("no memory for shell\n");
  683. return -1;
  684. }
  685. #else
  686. shell = &_shell;
  687. #endif
  688. memset(shell, 0, sizeof(struct finsh_shell));
  689. rt_sem_init(&(shell->rx_sem), "shrx", 0, 0);
  690. result = rt_thread_init(&finsh_thread,
  691. FINSH_THREAD_NAME,
  692. finsh_thread_entry, RT_NULL,
  693. &finsh_thread_stack[0], sizeof(finsh_thread_stack),
  694. FINSH_THREAD_PRIORITY, 10);
  695. if (result == RT_EOK)
  696. rt_thread_startup(&finsh_thread);
  697. return 0;
  698. }
  699. INIT_APP_EXPORT(finsh_system_init);