shell.c 19 KB

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