shell.c 22 KB

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