shell.c 19 KB

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