shell.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. */
  37. #include <rthw.h>
  38. #include "finsh.h"
  39. #include "shell.h"
  40. #ifdef FINSH_USING_MSH
  41. #include "msh.h"
  42. #endif
  43. #ifdef _WIN32
  44. #include <stdio.h> /* for putchar */
  45. #endif
  46. /* finsh thread */
  47. static struct rt_thread finsh_thread;
  48. ALIGN(RT_ALIGN_SIZE)
  49. static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE];
  50. struct finsh_shell *shell;
  51. #if defined(FINSH_USING_MSH) || (defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR))
  52. #if defined(RT_USING_DFS)
  53. #include <dfs_posix.h>
  54. #endif
  55. const char *finsh_get_prompt()
  56. {
  57. #define _MSH_PROMPT "msh "
  58. #define _PROMPT "finsh "
  59. static char finsh_prompt[RT_CONSOLEBUF_SIZE + 1] = {0};
  60. #ifdef FINSH_USING_MSH
  61. if (msh_is_used()) strcpy(finsh_prompt, _MSH_PROMPT);
  62. else
  63. #endif
  64. strcpy(finsh_prompt, _PROMPT);
  65. #if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)
  66. /* get current working directory */
  67. getcwd(&finsh_prompt[rt_strlen(finsh_prompt)], RT_CONSOLEBUF_SIZE - rt_strlen(finsh_prompt));
  68. #endif
  69. strcat(finsh_prompt, ">");
  70. return finsh_prompt;
  71. }
  72. #endif
  73. static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
  74. {
  75. RT_ASSERT(shell != RT_NULL);
  76. /* release semaphore to let finsh thread rx data */
  77. rt_sem_release(&shell->rx_sem);
  78. return RT_EOK;
  79. }
  80. /**
  81. * @ingroup finsh
  82. *
  83. * This function sets the input device of finsh shell.
  84. *
  85. * @param device_name the name of new input device.
  86. */
  87. void finsh_set_device(const char *device_name)
  88. {
  89. rt_device_t dev = RT_NULL;
  90. RT_ASSERT(shell != RT_NULL);
  91. dev = rt_device_find(device_name);
  92. if (dev == RT_NULL)
  93. {
  94. rt_kprintf("finsh: can not find device: %s\n", device_name);
  95. return;
  96. }
  97. /* check whether it's a same device */
  98. if (dev == shell->device) return;
  99. /* open this device and set the new device in finsh shell */
  100. if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | \
  101. RT_DEVICE_FLAG_STREAM) == RT_EOK)
  102. {
  103. if (shell->device != RT_NULL)
  104. {
  105. /* close old finsh device */
  106. rt_device_close(shell->device);
  107. rt_device_set_rx_indicate(shell->device, RT_NULL);
  108. }
  109. /* clear line buffer before switch to new device */
  110. memset(shell->line, 0, sizeof(shell->line));
  111. shell->line_curpos = shell->line_position = 0;
  112. shell->device = dev;
  113. rt_device_set_rx_indicate(dev, finsh_rx_ind);
  114. }
  115. }
  116. /**
  117. * @ingroup finsh
  118. *
  119. * This function returns current finsh shell input device.
  120. *
  121. * @return the finsh shell input device name is returned.
  122. */
  123. const char *finsh_get_device()
  124. {
  125. RT_ASSERT(shell != RT_NULL);
  126. return shell->device->parent.name;
  127. }
  128. /**
  129. * @ingroup finsh
  130. *
  131. * This function set the echo mode of finsh shell.
  132. *
  133. * FINSH_OPTION_ECHO=0x01 is echo mode, other values are none-echo mode.
  134. *
  135. * @param echo the echo mode
  136. */
  137. void finsh_set_echo(rt_uint32_t echo)
  138. {
  139. RT_ASSERT(shell != RT_NULL);
  140. shell->echo_mode = (rt_uint8_t)echo;
  141. }
  142. /**
  143. * @ingroup finsh
  144. *
  145. * This function gets the echo mode of finsh shell.
  146. *
  147. * @return the echo mode
  148. */
  149. rt_uint32_t finsh_get_echo()
  150. {
  151. RT_ASSERT(shell != RT_NULL);
  152. return shell->echo_mode;
  153. }
  154. static void shell_auto_complete(char *prefix)
  155. {
  156. rt_kprintf("\n");
  157. #ifdef FINSH_USING_MSH
  158. if (msh_is_used() == RT_TRUE)
  159. {
  160. msh_auto_complete(prefix);
  161. }
  162. else
  163. #endif
  164. {
  165. #ifndef FINSH_USING_MSH_ONLY
  166. extern void list_prefix(char * prefix);
  167. list_prefix(prefix);
  168. #endif
  169. }
  170. rt_kprintf("%s%s", FINSH_PROMPT, prefix);
  171. }
  172. #ifndef FINSH_USING_MSH_ONLY
  173. void finsh_run_line(struct finsh_parser *parser, const char *line)
  174. {
  175. const char *err_str;
  176. rt_kprintf("\n");
  177. finsh_parser_run(parser, (unsigned char *)line);
  178. /* compile node root */
  179. if (finsh_errno() == 0)
  180. {
  181. finsh_compiler_run(parser->root);
  182. }
  183. else
  184. {
  185. err_str = finsh_error_string(finsh_errno());
  186. rt_kprintf("%s\n", err_str);
  187. }
  188. /* run virtual machine */
  189. if (finsh_errno() == 0)
  190. {
  191. char ch;
  192. finsh_vm_run();
  193. ch = (unsigned char)finsh_stack_bottom();
  194. if (ch > 0x20 && ch < 0x7e)
  195. {
  196. rt_kprintf("\t'%c', %d, 0x%08x\n",
  197. (unsigned char)finsh_stack_bottom(),
  198. (unsigned int)finsh_stack_bottom(),
  199. (unsigned int)finsh_stack_bottom());
  200. }
  201. else
  202. {
  203. rt_kprintf("\t%d, 0x%08x\n",
  204. (unsigned int)finsh_stack_bottom(),
  205. (unsigned int)finsh_stack_bottom());
  206. }
  207. }
  208. finsh_flush(parser);
  209. }
  210. #endif
  211. #ifdef FINSH_USING_HISTORY
  212. static rt_bool_t shell_handle_history(struct finsh_shell *shell)
  213. {
  214. #if defined(_WIN32)
  215. int i;
  216. rt_kprintf("\r");
  217. for (i = 0; i <= 60; i++)
  218. putchar(' ');
  219. rt_kprintf("\r");
  220. #else
  221. rt_kprintf("\033[2K\r");
  222. #endif
  223. rt_kprintf("%s%s", FINSH_PROMPT, shell->line);
  224. return RT_FALSE;
  225. }
  226. static void shell_push_history(struct finsh_shell *shell)
  227. {
  228. if (shell->line_position != 0)
  229. {
  230. /* push history */
  231. if (shell->history_count >= FINSH_HISTORY_LINES)
  232. {
  233. /* move history */
  234. int index;
  235. for (index = 0; index < FINSH_HISTORY_LINES - 1; index ++)
  236. {
  237. memcpy(&shell->cmd_history[index][0],
  238. &shell->cmd_history[index + 1][0], FINSH_CMD_SIZE);
  239. }
  240. memset(&shell->cmd_history[index][0], 0, FINSH_CMD_SIZE);
  241. memcpy(&shell->cmd_history[index][0], shell->line, shell->line_position);
  242. /* it's the maximum history */
  243. shell->history_count = FINSH_HISTORY_LINES;
  244. }
  245. else
  246. {
  247. memset(&shell->cmd_history[shell->history_count][0], 0, FINSH_CMD_SIZE);
  248. memcpy(&shell->cmd_history[shell->history_count][0], shell->line, shell->line_position);
  249. /* increase count and set current history position */
  250. shell->history_count ++;
  251. }
  252. }
  253. shell->current_history = shell->history_count;
  254. }
  255. #endif
  256. #ifndef RT_USING_HEAP
  257. struct finsh_shell _shell;
  258. #endif
  259. void finsh_thread_entry(void *parameter)
  260. {
  261. char ch;
  262. /* normal is echo mode */
  263. shell->echo_mode = 1;
  264. #ifndef FINSH_USING_MSH_ONLY
  265. finsh_init(&shell->parser);
  266. #endif
  267. rt_kprintf(FINSH_PROMPT);
  268. /* set console device as shell device */
  269. if (shell->device == RT_NULL)
  270. {
  271. #ifdef RT_USING_CONSOLE
  272. shell->device = rt_console_get_device();
  273. RT_ASSERT(shell->device);
  274. rt_device_set_rx_indicate(shell->device, finsh_rx_ind);
  275. rt_device_open(shell->device, (RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX));
  276. #else
  277. RT_ASSERT(shell->device);
  278. #endif
  279. }
  280. while (1)
  281. {
  282. /* wait receive */
  283. if (rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue;
  284. /* read one character from device */
  285. while (rt_device_read(shell->device, 0, &ch, 1) == 1)
  286. {
  287. /*
  288. * handle control key
  289. * up key : 0x1b 0x5b 0x41
  290. * down key: 0x1b 0x5b 0x42
  291. * right key:0x1b 0x5b 0x43
  292. * left key: 0x1b 0x5b 0x44
  293. */
  294. if (ch == 0x1b)
  295. {
  296. shell->stat = WAIT_SPEC_KEY;
  297. continue;
  298. }
  299. else if (shell->stat == WAIT_SPEC_KEY)
  300. {
  301. if (ch == 0x5b)
  302. {
  303. shell->stat = WAIT_FUNC_KEY;
  304. continue;
  305. }
  306. shell->stat = WAIT_NORMAL;
  307. }
  308. else if (shell->stat == WAIT_FUNC_KEY)
  309. {
  310. shell->stat = WAIT_NORMAL;
  311. if (ch == 0x41) /* up key */
  312. {
  313. #ifdef FINSH_USING_HISTORY
  314. /* prev history */
  315. if (shell->current_history > 0)
  316. shell->current_history --;
  317. else
  318. {
  319. shell->current_history = 0;
  320. continue;
  321. }
  322. /* copy the history command */
  323. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  324. FINSH_CMD_SIZE);
  325. shell->line_curpos = shell->line_position = strlen(shell->line);
  326. shell_handle_history(shell);
  327. #endif
  328. continue;
  329. }
  330. else if (ch == 0x42) /* down key */
  331. {
  332. #ifdef FINSH_USING_HISTORY
  333. /* next history */
  334. if (shell->current_history < shell->history_count - 1)
  335. shell->current_history ++;
  336. else
  337. {
  338. /* set to the end of history */
  339. if (shell->history_count != 0)
  340. shell->current_history = shell->history_count - 1;
  341. else
  342. continue;
  343. }
  344. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  345. FINSH_CMD_SIZE);
  346. shell->line_curpos = shell->line_position = strlen(shell->line);
  347. shell_handle_history(shell);
  348. #endif
  349. continue;
  350. }
  351. else if (ch == 0x44) /* left key */
  352. {
  353. if (shell->line_curpos)
  354. {
  355. rt_kprintf("\b");
  356. shell->line_curpos --;
  357. }
  358. continue;
  359. }
  360. else if (ch == 0x43) /* right key */
  361. {
  362. if (shell->line_curpos < shell->line_position)
  363. {
  364. rt_kprintf("%c", shell->line[shell->line_curpos]);
  365. shell->line_curpos ++;
  366. }
  367. continue;
  368. }
  369. }
  370. /* handle CR key */
  371. if (ch == '\r')
  372. {
  373. char next;
  374. if (rt_device_read(shell->device, 0, &next, 1) == 1)
  375. {
  376. if (next == '\0') ch = '\r'; /* linux telnet will issue '\0' */
  377. else ch = next;
  378. }
  379. else ch = '\r';
  380. }
  381. /* handle tab key */
  382. else if (ch == '\t')
  383. {
  384. int i;
  385. /* move the cursor to the beginning of line */
  386. for (i = 0; i < shell->line_curpos; i++)
  387. rt_kprintf("\b");
  388. /* auto complete */
  389. shell_auto_complete(&shell->line[0]);
  390. /* re-calculate position */
  391. shell->line_curpos = shell->line_position = strlen(shell->line);
  392. continue;
  393. }
  394. /* handle backspace key */
  395. else if (ch == 0x7f || ch == 0x08)
  396. {
  397. /* note that shell->line_curpos >= 0 */
  398. if (shell->line_curpos == 0)
  399. continue;
  400. shell->line_position--;
  401. shell->line_curpos--;
  402. if (shell->line_position > shell->line_curpos)
  403. {
  404. int i;
  405. rt_memmove(&shell->line[shell->line_curpos],
  406. &shell->line[shell->line_curpos + 1],
  407. shell->line_position - shell->line_curpos);
  408. shell->line[shell->line_position] = 0;
  409. rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]);
  410. /* move the cursor to the origin position */
  411. for (i = shell->line_curpos; i <= shell->line_position; i++)
  412. rt_kprintf("\b");
  413. }
  414. else
  415. {
  416. rt_kprintf("\b \b");
  417. shell->line[shell->line_position] = 0;
  418. }
  419. continue;
  420. }
  421. /* handle end of line, break */
  422. if (ch == '\r' || ch == '\n')
  423. {
  424. #ifdef FINSH_USING_HISTORY
  425. shell_push_history(shell);
  426. #endif
  427. #ifdef FINSH_USING_MSH
  428. if (msh_is_used() == RT_TRUE)
  429. {
  430. rt_kprintf("\n");
  431. msh_exec(shell->line, shell->line_position);
  432. }
  433. else
  434. #endif
  435. {
  436. #ifndef FINSH_USING_MSH_ONLY
  437. /* add ';' and run the command line */
  438. shell->line[shell->line_position] = ';';
  439. if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
  440. else rt_kprintf("\n");
  441. #endif
  442. }
  443. rt_kprintf(FINSH_PROMPT);
  444. memset(shell->line, 0, sizeof(shell->line));
  445. shell->line_curpos = shell->line_position = 0;
  446. break;
  447. }
  448. /* it's a large line, discard it */
  449. if (shell->line_position >= FINSH_CMD_SIZE)
  450. shell->line_position = 0;
  451. /* normal character */
  452. if (shell->line_curpos < shell->line_position)
  453. {
  454. int i;
  455. rt_memmove(&shell->line[shell->line_curpos + 1],
  456. &shell->line[shell->line_curpos],
  457. shell->line_position - shell->line_curpos);
  458. shell->line[shell->line_curpos] = ch;
  459. if (shell->echo_mode)
  460. rt_kprintf("%s", &shell->line[shell->line_curpos]);
  461. /* move the cursor to new position */
  462. for (i = shell->line_curpos; i < shell->line_position; i++)
  463. rt_kprintf("\b");
  464. }
  465. else
  466. {
  467. shell->line[shell->line_position] = ch;
  468. if (shell->echo_mode)
  469. rt_kprintf("%c", ch);
  470. }
  471. ch = 0;
  472. shell->line_position ++;
  473. shell->line_curpos++;
  474. if (shell->line_position >= 80)
  475. {
  476. /* clear command line */
  477. shell->line_position = 0;
  478. shell->line_curpos = 0;
  479. }
  480. } /* end of device read */
  481. }
  482. }
  483. void finsh_system_function_init(const void *begin, const void *end)
  484. {
  485. _syscall_table_begin = (struct finsh_syscall *) begin;
  486. _syscall_table_end = (struct finsh_syscall *) end;
  487. }
  488. void finsh_system_var_init(const void *begin, const void *end)
  489. {
  490. _sysvar_table_begin = (struct finsh_sysvar *) begin;
  491. _sysvar_table_end = (struct finsh_sysvar *) end;
  492. }
  493. #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
  494. #ifdef FINSH_USING_SYMTAB
  495. #pragma section="FSymTab"
  496. #pragma section="VSymTab"
  497. #endif
  498. #elif defined(__ADSPBLACKFIN__) /* for VisaulDSP++ Compiler*/
  499. #ifdef FINSH_USING_SYMTAB
  500. extern "asm" int __fsymtab_start;
  501. extern "asm" int __fsymtab_end;
  502. extern "asm" int __vsymtab_start;
  503. extern "asm" int __vsymtab_end;
  504. #endif
  505. #elif defined(_MSC_VER)
  506. #pragma section("FSymTab$a", read)
  507. const char __fsym_begin_name[] = "__start";
  508. const char __fsym_begin_desc[] = "begin of finsh";
  509. __declspec(allocate("FSymTab$a")) const struct finsh_syscall __fsym_begin =
  510. {
  511. __fsym_begin_name,
  512. __fsym_begin_desc,
  513. NULL
  514. };
  515. #pragma section("FSymTab$z", read)
  516. const char __fsym_end_name[] = "__end";
  517. const char __fsym_end_desc[] = "end of finsh";
  518. __declspec(allocate("FSymTab$z")) const struct finsh_syscall __fsym_end =
  519. {
  520. __fsym_end_name,
  521. __fsym_end_desc,
  522. NULL
  523. };
  524. #endif
  525. /*
  526. * @ingroup finsh
  527. *
  528. * This function will initialize finsh shell
  529. */
  530. int finsh_system_init(void)
  531. {
  532. rt_err_t result;
  533. #ifdef FINSH_USING_SYMTAB
  534. #ifdef __CC_ARM /* ARM C Compiler */
  535. extern const int FSymTab$$Base;
  536. extern const int FSymTab$$Limit;
  537. extern const int VSymTab$$Base;
  538. extern const int VSymTab$$Limit;
  539. finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
  540. #ifndef FINSH_USING_MSH_ONLY
  541. finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
  542. #endif
  543. #elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
  544. finsh_system_function_init(__section_begin("FSymTab"),
  545. __section_end("FSymTab"));
  546. finsh_system_var_init(__section_begin("VSymTab"),
  547. __section_end("VSymTab"));
  548. #elif defined (__GNUC__) || defined(__TI_COMPILER_VERSION__)
  549. /* GNU GCC Compiler and TI CCS */
  550. extern const int __fsymtab_start;
  551. extern const int __fsymtab_end;
  552. extern const int __vsymtab_start;
  553. extern const int __vsymtab_end;
  554. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  555. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  556. #elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
  557. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  558. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  559. #elif defined(_MSC_VER)
  560. unsigned int *ptr_begin, *ptr_end;
  561. ptr_begin = (unsigned int *)&__fsym_begin;
  562. ptr_begin += (sizeof(struct finsh_syscall) / sizeof(unsigned int));
  563. while (*ptr_begin == 0) ptr_begin ++;
  564. ptr_end = (unsigned int *) &__fsym_end;
  565. ptr_end --;
  566. while (*ptr_end == 0) ptr_end --;
  567. finsh_system_function_init(ptr_begin, ptr_end);
  568. #endif
  569. #endif
  570. /* create or set shell structure */
  571. #ifdef RT_USING_HEAP
  572. shell = (struct finsh_shell *)rt_malloc(sizeof(struct finsh_shell));
  573. if (shell == RT_NULL)
  574. {
  575. rt_kprintf("no memory for shell\n");
  576. return -1;
  577. }
  578. #else
  579. shell = &_shell;
  580. #endif
  581. memset(shell, 0, sizeof(struct finsh_shell));
  582. rt_sem_init(&(shell->rx_sem), "shrx", 0, 0);
  583. result = rt_thread_init(&finsh_thread,
  584. "tshell",
  585. finsh_thread_entry, RT_NULL,
  586. &finsh_thread_stack[0], sizeof(finsh_thread_stack),
  587. FINSH_THREAD_PRIORITY, 10);
  588. if (result == RT_EOK)
  589. rt_thread_startup(&finsh_thread);
  590. return 0;
  591. }
  592. INIT_COMPONENT_EXPORT(finsh_system_init);