shell.c 24 KB

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