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