shell.c 14 KB

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