shell.c 19 KB

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