1
0

shell.c 24 KB

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