shell.c 23 KB

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