shell.c 19 KB

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