shell.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-04-30 Bernard the first version for FinSH
  9. * 2006-05-08 Bernard change finsh thread stack to 2048
  10. * 2006-06-03 Bernard add support for skyeye
  11. * 2006-09-24 Bernard remove the code related with hardware
  12. * 2010-01-18 Bernard fix down then up key bug.
  13. * 2010-03-19 Bernard fix backspace issue and fix device read in shell.
  14. * 2010-04-01 Bernard add prompt output when start and remove the empty history
  15. * 2011-02-23 Bernard fix variable section end issue of finsh shell
  16. * initialization when use GNU GCC compiler.
  17. * 2016-11-26 armink add password authentication
  18. * 2018-07-02 aozima add custome prompt support.
  19. */
  20. #include <rthw.h>
  21. #ifdef RT_USING_FINSH
  22. #include "finsh.h"
  23. #include "shell.h"
  24. #ifdef FINSH_USING_MSH
  25. #include "msh.h"
  26. #endif
  27. #ifdef _WIN32
  28. #include <stdio.h> /* for putchar */
  29. #endif
  30. /* finsh thread */
  31. #ifndef RT_USING_HEAP
  32. static struct rt_thread finsh_thread;
  33. ALIGN(RT_ALIGN_SIZE)
  34. static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE];
  35. struct finsh_shell _shell;
  36. #endif
  37. struct finsh_shell *shell;
  38. static char *finsh_prompt_custom = RT_NULL;
  39. #if defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__))
  40. struct finsh_syscall* finsh_syscall_next(struct finsh_syscall* call)
  41. {
  42. unsigned int *ptr;
  43. ptr = (unsigned int*) (call + 1);
  44. while ((*ptr == 0) && ((unsigned int*)ptr < (unsigned int*) _syscall_table_end))
  45. ptr ++;
  46. return (struct finsh_syscall*)ptr;
  47. }
  48. struct finsh_sysvar* finsh_sysvar_next(struct finsh_sysvar* call)
  49. {
  50. unsigned int *ptr;
  51. ptr = (unsigned int*) (call + 1);
  52. while ((*ptr == 0) && ((unsigned int*)ptr < (unsigned int*) _sysvar_table_end))
  53. ptr ++;
  54. return (struct finsh_sysvar*)ptr;
  55. }
  56. #endif /* defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__)) */
  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 = (char *)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 int finsh_getchar(void)
  135. {
  136. #ifdef RT_USING_POSIX
  137. return getchar();
  138. #else
  139. char ch = 0;
  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 (int)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. int 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 < 0)
  275. {
  276. continue;
  277. }
  278. if (ch >= ' ' && ch <= '~' && cur_pos < FINSH_PASSWORD_MAX)
  279. {
  280. /* change the printable characters to '*' */
  281. rt_kprintf("*");
  282. password[cur_pos++] = ch;
  283. }
  284. else if (ch == '\b' && cur_pos > 0)
  285. {
  286. /* backspace */
  287. cur_pos--;
  288. password[cur_pos] = '\0';
  289. rt_kprintf("\b \b");
  290. }
  291. else if (ch == '\r' || ch == '\n')
  292. {
  293. rt_kprintf("\n");
  294. input_finish = RT_TRUE;
  295. break;
  296. }
  297. }
  298. }
  299. if (!rt_strncmp(shell->password, password, FINSH_PASSWORD_MAX)) return;
  300. else
  301. {
  302. /* authentication failed, delay 2S for retry */
  303. rt_thread_delay(2 * RT_TICK_PER_SECOND);
  304. rt_kprintf("Sorry, try again.\n");
  305. cur_pos = 0;
  306. input_finish = RT_FALSE;
  307. rt_memset(password, '\0', FINSH_PASSWORD_MAX);
  308. }
  309. }
  310. }
  311. #endif /* FINSH_USING_AUTH */
  312. static void shell_auto_complete(char *prefix)
  313. {
  314. rt_kprintf("\n");
  315. #ifdef FINSH_USING_MSH
  316. if (msh_is_used() == RT_TRUE)
  317. {
  318. msh_auto_complete(prefix);
  319. }
  320. else
  321. #endif
  322. {
  323. #ifndef FINSH_USING_MSH_ONLY
  324. extern void list_prefix(char * prefix);
  325. list_prefix(prefix);
  326. #endif
  327. }
  328. rt_kprintf("%s%s", FINSH_PROMPT, prefix);
  329. }
  330. #ifndef FINSH_USING_MSH_ONLY
  331. void finsh_run_line(struct finsh_parser *parser, const char *line)
  332. {
  333. const char *err_str;
  334. if(shell->echo_mode)
  335. rt_kprintf("\n");
  336. finsh_parser_run(parser, (unsigned char *)line);
  337. /* compile node root */
  338. if (finsh_errno() == 0)
  339. {
  340. finsh_compiler_run(parser->root);
  341. }
  342. else
  343. {
  344. err_str = finsh_error_string(finsh_errno());
  345. rt_kprintf("%s\n", err_str);
  346. }
  347. /* run virtual machine */
  348. if (finsh_errno() == 0)
  349. {
  350. char ch;
  351. finsh_vm_run();
  352. ch = (unsigned char)finsh_stack_bottom();
  353. if (ch > 0x20 && ch < 0x7e)
  354. {
  355. rt_kprintf("\t'%c', %d, 0x%08x\n",
  356. (unsigned char)finsh_stack_bottom(),
  357. (unsigned int)finsh_stack_bottom(),
  358. (unsigned int)finsh_stack_bottom());
  359. }
  360. else
  361. {
  362. rt_kprintf("\t%d, 0x%08x\n",
  363. (unsigned int)finsh_stack_bottom(),
  364. (unsigned int)finsh_stack_bottom());
  365. }
  366. }
  367. finsh_flush(parser);
  368. }
  369. #endif
  370. #ifdef FINSH_USING_HISTORY
  371. static rt_bool_t shell_handle_history(struct finsh_shell *shell)
  372. {
  373. #if defined(_WIN32)
  374. int i;
  375. rt_kprintf("\r");
  376. for (i = 0; i <= 60; i++)
  377. putchar(' ');
  378. rt_kprintf("\r");
  379. #else
  380. rt_kprintf("\033[2K\r");
  381. #endif
  382. rt_kprintf("%s%s", FINSH_PROMPT, shell->line);
  383. return RT_FALSE;
  384. }
  385. static void shell_push_history(struct finsh_shell *shell)
  386. {
  387. if (shell->line_position != 0)
  388. {
  389. /* push history */
  390. if (shell->history_count >= FINSH_HISTORY_LINES)
  391. {
  392. /* if current cmd is same as last cmd, don't push */
  393. if (memcmp(&shell->cmd_history[FINSH_HISTORY_LINES - 1], shell->line, FINSH_CMD_SIZE))
  394. {
  395. /* move history */
  396. int index;
  397. for (index = 0; index < FINSH_HISTORY_LINES - 1; index ++)
  398. {
  399. memcpy(&shell->cmd_history[index][0],
  400. &shell->cmd_history[index + 1][0], FINSH_CMD_SIZE);
  401. }
  402. memset(&shell->cmd_history[index][0], 0, FINSH_CMD_SIZE);
  403. memcpy(&shell->cmd_history[index][0], shell->line, shell->line_position);
  404. /* it's the maximum history */
  405. shell->history_count = FINSH_HISTORY_LINES;
  406. }
  407. }
  408. else
  409. {
  410. /* if current cmd is same as last cmd, don't push */
  411. if (shell->history_count == 0 || memcmp(&shell->cmd_history[shell->history_count - 1], shell->line, FINSH_CMD_SIZE))
  412. {
  413. shell->current_history = shell->history_count;
  414. memset(&shell->cmd_history[shell->history_count][0], 0, FINSH_CMD_SIZE);
  415. memcpy(&shell->cmd_history[shell->history_count][0], shell->line, shell->line_position);
  416. /* increase count and set current history position */
  417. shell->history_count ++;
  418. }
  419. }
  420. }
  421. shell->current_history = shell->history_count;
  422. }
  423. #endif
  424. void finsh_thread_entry(void *parameter)
  425. {
  426. int ch;
  427. /* normal is echo mode */
  428. #ifndef FINSH_ECHO_DISABLE_DEFAULT
  429. shell->echo_mode = 1;
  430. #else
  431. shell->echo_mode = 0;
  432. #endif
  433. #ifndef FINSH_USING_MSH_ONLY
  434. finsh_init(&shell->parser);
  435. #endif
  436. #ifndef RT_USING_POSIX
  437. /* set console device as shell device */
  438. if (shell->device == RT_NULL)
  439. {
  440. rt_device_t console = rt_console_get_device();
  441. if (console)
  442. {
  443. finsh_set_device(console->parent.name);
  444. }
  445. }
  446. #endif
  447. #ifdef FINSH_USING_AUTH
  448. /* set the default password when the password isn't setting */
  449. if (rt_strlen(finsh_get_password()) == 0)
  450. {
  451. if (finsh_set_password(FINSH_DEFAULT_PASSWORD) != RT_EOK)
  452. {
  453. rt_kprintf("Finsh password set failed.\n");
  454. }
  455. }
  456. /* waiting authenticate success */
  457. finsh_wait_auth();
  458. #endif
  459. rt_kprintf(FINSH_PROMPT);
  460. while (1)
  461. {
  462. ch = finsh_getchar();
  463. if (ch < 0)
  464. {
  465. continue;
  466. }
  467. /*
  468. * handle control key
  469. * up key : 0x1b 0x5b 0x41
  470. * down key: 0x1b 0x5b 0x42
  471. * right key:0x1b 0x5b 0x43
  472. * left key: 0x1b 0x5b 0x44
  473. */
  474. if (ch == 0x1b)
  475. {
  476. shell->stat = WAIT_SPEC_KEY;
  477. continue;
  478. }
  479. else if (shell->stat == WAIT_SPEC_KEY)
  480. {
  481. if (ch == 0x5b)
  482. {
  483. shell->stat = WAIT_FUNC_KEY;
  484. continue;
  485. }
  486. shell->stat = WAIT_NORMAL;
  487. }
  488. else if (shell->stat == WAIT_FUNC_KEY)
  489. {
  490. shell->stat = WAIT_NORMAL;
  491. if (ch == 0x41) /* up key */
  492. {
  493. #ifdef FINSH_USING_HISTORY
  494. /* prev history */
  495. if (shell->current_history > 0)
  496. shell->current_history --;
  497. else
  498. {
  499. shell->current_history = 0;
  500. continue;
  501. }
  502. /* copy the history command */
  503. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  504. FINSH_CMD_SIZE);
  505. shell->line_curpos = shell->line_position = strlen(shell->line);
  506. shell_handle_history(shell);
  507. #endif
  508. continue;
  509. }
  510. else if (ch == 0x42) /* down key */
  511. {
  512. #ifdef FINSH_USING_HISTORY
  513. /* next history */
  514. if (shell->current_history < shell->history_count - 1)
  515. shell->current_history ++;
  516. else
  517. {
  518. /* set to the end of history */
  519. if (shell->history_count != 0)
  520. shell->current_history = shell->history_count - 1;
  521. else
  522. continue;
  523. }
  524. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  525. FINSH_CMD_SIZE);
  526. shell->line_curpos = shell->line_position = strlen(shell->line);
  527. shell_handle_history(shell);
  528. #endif
  529. continue;
  530. }
  531. else if (ch == 0x44) /* left key */
  532. {
  533. if (shell->line_curpos)
  534. {
  535. rt_kprintf("\b");
  536. shell->line_curpos --;
  537. }
  538. continue;
  539. }
  540. else if (ch == 0x43) /* right key */
  541. {
  542. if (shell->line_curpos < shell->line_position)
  543. {
  544. rt_kprintf("%c", shell->line[shell->line_curpos]);
  545. shell->line_curpos ++;
  546. }
  547. continue;
  548. }
  549. }
  550. /* received null or error */
  551. if (ch == '\0' || ch == 0xFF) continue;
  552. /* handle tab key */
  553. else if (ch == '\t')
  554. {
  555. int i;
  556. /* move the cursor to the beginning of line */
  557. for (i = 0; i < shell->line_curpos; i++)
  558. rt_kprintf("\b");
  559. /* auto complete */
  560. shell_auto_complete(&shell->line[0]);
  561. /* re-calculate position */
  562. shell->line_curpos = shell->line_position = strlen(shell->line);
  563. continue;
  564. }
  565. /* handle backspace key */
  566. else if (ch == 0x7f || ch == 0x08)
  567. {
  568. /* note that shell->line_curpos >= 0 */
  569. if (shell->line_curpos == 0)
  570. continue;
  571. shell->line_position--;
  572. shell->line_curpos--;
  573. if (shell->line_position > shell->line_curpos)
  574. {
  575. int i;
  576. rt_memmove(&shell->line[shell->line_curpos],
  577. &shell->line[shell->line_curpos + 1],
  578. shell->line_position - shell->line_curpos);
  579. shell->line[shell->line_position] = 0;
  580. rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]);
  581. /* move the cursor to the origin position */
  582. for (i = shell->line_curpos; i <= shell->line_position; i++)
  583. rt_kprintf("\b");
  584. }
  585. else
  586. {
  587. rt_kprintf("\b \b");
  588. shell->line[shell->line_position] = 0;
  589. }
  590. continue;
  591. }
  592. /* handle end of line, break */
  593. if (ch == '\r' || ch == '\n')
  594. {
  595. #ifdef FINSH_USING_HISTORY
  596. shell_push_history(shell);
  597. #endif
  598. #ifdef FINSH_USING_MSH
  599. if (msh_is_used() == RT_TRUE)
  600. {
  601. if (shell->echo_mode)
  602. rt_kprintf("\n");
  603. msh_exec(shell->line, shell->line_position);
  604. }
  605. else
  606. #endif
  607. {
  608. #ifndef FINSH_USING_MSH_ONLY
  609. /* add ';' and run the command line */
  610. shell->line[shell->line_position] = ';';
  611. if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
  612. else
  613. if (shell->echo_mode) rt_kprintf("\n");
  614. #endif
  615. }
  616. rt_kprintf(FINSH_PROMPT);
  617. memset(shell->line, 0, sizeof(shell->line));
  618. shell->line_curpos = shell->line_position = 0;
  619. continue;
  620. }
  621. /* it's a large line, discard it */
  622. if (shell->line_position >= FINSH_CMD_SIZE)
  623. shell->line_position = 0;
  624. /* normal character */
  625. if (shell->line_curpos < shell->line_position)
  626. {
  627. int i;
  628. rt_memmove(&shell->line[shell->line_curpos + 1],
  629. &shell->line[shell->line_curpos],
  630. shell->line_position - shell->line_curpos);
  631. shell->line[shell->line_curpos] = ch;
  632. if (shell->echo_mode)
  633. rt_kprintf("%s", &shell->line[shell->line_curpos]);
  634. /* move the cursor to new position */
  635. for (i = shell->line_curpos; i < shell->line_position; i++)
  636. rt_kprintf("\b");
  637. }
  638. else
  639. {
  640. shell->line[shell->line_position] = ch;
  641. if (shell->echo_mode)
  642. rt_kprintf("%c", ch);
  643. }
  644. ch = 0;
  645. shell->line_position ++;
  646. shell->line_curpos++;
  647. if (shell->line_position >= FINSH_CMD_SIZE)
  648. {
  649. /* clear command line */
  650. shell->line_position = 0;
  651. shell->line_curpos = 0;
  652. }
  653. } /* end of device read */
  654. }
  655. void finsh_system_function_init(const void *begin, const void *end)
  656. {
  657. _syscall_table_begin = (struct finsh_syscall *) begin;
  658. _syscall_table_end = (struct finsh_syscall *) end;
  659. }
  660. void finsh_system_var_init(const void *begin, const void *end)
  661. {
  662. _sysvar_table_begin = (struct finsh_sysvar *) begin;
  663. _sysvar_table_end = (struct finsh_sysvar *) end;
  664. }
  665. #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
  666. #ifdef FINSH_USING_SYMTAB
  667. #pragma section="FSymTab"
  668. #pragma section="VSymTab"
  669. #endif
  670. #elif defined(__ADSPBLACKFIN__) /* for VisaulDSP++ Compiler*/
  671. #ifdef FINSH_USING_SYMTAB
  672. extern "asm" int __fsymtab_start;
  673. extern "asm" int __fsymtab_end;
  674. extern "asm" int __vsymtab_start;
  675. extern "asm" int __vsymtab_end;
  676. #endif
  677. #elif defined(_MSC_VER)
  678. #pragma section("FSymTab$a", read)
  679. const char __fsym_begin_name[] = "__start";
  680. const char __fsym_begin_desc[] = "begin of finsh";
  681. __declspec(allocate("FSymTab$a")) const struct finsh_syscall __fsym_begin =
  682. {
  683. __fsym_begin_name,
  684. __fsym_begin_desc,
  685. NULL
  686. };
  687. #pragma section("FSymTab$z", read)
  688. const char __fsym_end_name[] = "__end";
  689. const char __fsym_end_desc[] = "end of finsh";
  690. __declspec(allocate("FSymTab$z")) const struct finsh_syscall __fsym_end =
  691. {
  692. __fsym_end_name,
  693. __fsym_end_desc,
  694. NULL
  695. };
  696. #endif
  697. /*
  698. * @ingroup finsh
  699. *
  700. * This function will initialize finsh shell
  701. */
  702. int finsh_system_init(void)
  703. {
  704. rt_err_t result = RT_EOK;
  705. rt_thread_t tid;
  706. #ifdef FINSH_USING_SYMTAB
  707. #if defined(__CC_ARM) || defined(__CLANG_ARM) /* ARM C Compiler */
  708. extern const int FSymTab$$Base;
  709. extern const int FSymTab$$Limit;
  710. extern const int VSymTab$$Base;
  711. extern const int VSymTab$$Limit;
  712. finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
  713. #ifndef FINSH_USING_MSH_ONLY
  714. finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
  715. #endif
  716. #elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
  717. finsh_system_function_init(__section_begin("FSymTab"),
  718. __section_end("FSymTab"));
  719. finsh_system_var_init(__section_begin("VSymTab"),
  720. __section_end("VSymTab"));
  721. #elif defined (__GNUC__) || defined(__TI_COMPILER_VERSION__)
  722. /* GNU GCC Compiler and TI CCS */
  723. extern const int __fsymtab_start;
  724. extern const int __fsymtab_end;
  725. extern const int __vsymtab_start;
  726. extern const int __vsymtab_end;
  727. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  728. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  729. #elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
  730. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  731. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  732. #elif defined(_MSC_VER)
  733. unsigned int *ptr_begin, *ptr_end;
  734. if(shell)
  735. {
  736. rt_kprintf("finsh shell already init.\n");
  737. return RT_EOK;
  738. }
  739. ptr_begin = (unsigned int *)&__fsym_begin;
  740. ptr_begin += (sizeof(struct finsh_syscall) / sizeof(unsigned int));
  741. while (*ptr_begin == 0) ptr_begin ++;
  742. ptr_end = (unsigned int *) &__fsym_end;
  743. ptr_end --;
  744. while (*ptr_end == 0) ptr_end --;
  745. finsh_system_function_init(ptr_begin, ptr_end);
  746. #endif
  747. #endif
  748. #ifdef RT_USING_HEAP
  749. /* create or set shell structure */
  750. shell = (struct finsh_shell *)rt_calloc(1, sizeof(struct finsh_shell));
  751. if (shell == RT_NULL)
  752. {
  753. rt_kprintf("no memory for shell\n");
  754. return -1;
  755. }
  756. tid = rt_thread_create(FINSH_THREAD_NAME,
  757. finsh_thread_entry, RT_NULL,
  758. FINSH_THREAD_STACK_SIZE, FINSH_THREAD_PRIORITY, 10);
  759. #else
  760. shell = &_shell;
  761. tid = &finsh_thread;
  762. result = rt_thread_init(&finsh_thread,
  763. FINSH_THREAD_NAME,
  764. finsh_thread_entry, RT_NULL,
  765. &finsh_thread_stack[0], sizeof(finsh_thread_stack),
  766. FINSH_THREAD_PRIORITY, 10);
  767. #endif /* RT_USING_HEAP */
  768. rt_sem_init(&(shell->rx_sem), "shrx", 0, 0);
  769. finsh_set_prompt_mode(1);
  770. if (tid != NULL && result == RT_EOK)
  771. rt_thread_startup(tid);
  772. return 0;
  773. }
  774. INIT_APP_EXPORT(finsh_system_init);
  775. #endif /* RT_USING_FINSH */