shell.c 19 KB

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