shell.c 24 KB

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