shell.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * File : shell.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-04-30 Bernard the first verion for FinSH
  13. * 2006-05-08 Bernard change finsh thread stack to 2048
  14. * 2006-06-03 Bernard add support for skyeye
  15. * 2006-09-24 Bernard remove the code related with hardware
  16. * 2010-01-18 Bernard fix down then up key bug.
  17. * 2010-03-19 Bernard fix backspace issue and fix device read in shell.
  18. * 2010-04-01 Bernard add prompt output when start and remove the empty history
  19. * 2011-02-23 Bernard fix variable section end issue of finsh shell
  20. * initialization when use GNU GCC compiler.
  21. */
  22. #include <rtthread.h>
  23. #include <rthw.h>
  24. #include "finsh.h"
  25. #include "shell.h"
  26. #ifdef _WIN32
  27. #include <stdio.h> /* for putchar */
  28. #endif
  29. /* finsh thread */
  30. static struct rt_thread finsh_thread;
  31. ALIGN(RT_ALIGN_SIZE)
  32. static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE];
  33. struct finsh_shell* shell;
  34. #if !defined (RT_USING_NEWLIB) && !defined (RT_USING_MINILIBC)
  35. int strcmp (const char *s1, const char *s2)
  36. {
  37. while (*s1 && *s1 == *s2) s1++, s2++;
  38. return (*s1 - *s2);
  39. }
  40. #ifdef RT_USING_HEAP
  41. char *strdup(const char *s)
  42. {
  43. size_t len = strlen(s) + 1;
  44. char *tmp = (char *)rt_malloc(len);
  45. if(!tmp) return NULL;
  46. rt_memcpy(tmp, s, len);
  47. return tmp;
  48. }
  49. #endif
  50. #if !defined(__CC_ARM) && !defined(__IAR_SYSTEMS_ICC__) && !defined(__ADSPBLACKFIN__) && !defined(_MSC_VER)
  51. int isalpha( int ch )
  52. {
  53. return (unsigned int)((ch | 0x20) - 'a') < 26u;
  54. }
  55. int atoi(const char* s)
  56. {
  57. long int v=0;
  58. int sign=1;
  59. while ( *s == ' ' || (unsigned int)(*s - 9) < 5u) s++;
  60. switch (*s)
  61. {
  62. case '-': sign=-1;
  63. case '+': ++s;
  64. }
  65. while ((unsigned int) (*s - '0') < 10u)
  66. {
  67. v=v*10+*s-'0'; ++s;
  68. }
  69. return sign==-1?-v:v;
  70. }
  71. int isprint(unsigned char ch)
  72. {
  73. return (unsigned int)(ch - ' ') < 127u - ' ';
  74. }
  75. #endif
  76. #endif
  77. #if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)
  78. #include <dfs_posix.h>
  79. const char* finsh_get_prompt()
  80. {
  81. #define _PROMPT "finsh "
  82. static char finsh_prompt[RT_CONSOLEBUF_SIZE + 1] = {_PROMPT};
  83. /* get current working directory */
  84. getcwd(&finsh_prompt[6], RT_CONSOLEBUF_SIZE - 8);
  85. strcat(finsh_prompt, ">");
  86. return finsh_prompt;
  87. }
  88. #endif
  89. static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
  90. {
  91. RT_ASSERT(shell != RT_NULL);
  92. /* release semaphore to let finsh thread rx data */
  93. rt_sem_release(&shell->rx_sem);
  94. return RT_EOK;
  95. }
  96. /**
  97. * @ingroup finsh
  98. *
  99. * This function sets the input device of finsh shell.
  100. *
  101. * @param device_name the name of new input device.
  102. */
  103. void finsh_set_device(const char* device_name)
  104. {
  105. rt_device_t dev = RT_NULL;
  106. RT_ASSERT(shell != RT_NULL);
  107. dev = rt_device_find(device_name);
  108. if (dev != RT_NULL && 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. }
  115. shell->device = dev;
  116. rt_device_set_rx_indicate(dev, finsh_rx_ind);
  117. }
  118. else
  119. {
  120. rt_kprintf("finsh: can not find device:%s\n", device_name);
  121. }
  122. }
  123. /**
  124. * @ingroup finsh
  125. *
  126. * This function returns current finsh shell input device.
  127. *
  128. * @return the finsh shell input device name is returned.
  129. */
  130. const char* finsh_get_device()
  131. {
  132. RT_ASSERT(shell != RT_NULL);
  133. return shell->device->parent.name;
  134. }
  135. /**
  136. * @ingroup finsh
  137. *
  138. * This function set the echo mode of finsh shell.
  139. *
  140. * FINSH_OPTION_ECHO=0x01 is echo mode, other values are none-echo mode.
  141. *
  142. * @param echo the echo mode
  143. */
  144. void finsh_set_echo(rt_uint32_t echo)
  145. {
  146. RT_ASSERT(shell != RT_NULL);
  147. shell->echo_mode = (rt_uint8_t)echo;
  148. }
  149. /**
  150. * @ingroup finsh
  151. *
  152. * This function gets the echo mode of finsh shell.
  153. *
  154. * @return the echo mode
  155. */
  156. rt_uint32_t finsh_get_echo()
  157. {
  158. RT_ASSERT(shell != RT_NULL);
  159. return shell->echo_mode;
  160. }
  161. void finsh_auto_complete(char* prefix)
  162. {
  163. extern void list_prefix(char* prefix);
  164. rt_kprintf("\n");
  165. list_prefix(prefix);
  166. rt_kprintf("%s%s", FINSH_PROMPT, prefix);
  167. }
  168. void finsh_run_line(struct finsh_parser* parser, const char *line)
  169. {
  170. const char* err_str;
  171. rt_kprintf("\n");
  172. finsh_parser_run(parser, (unsigned char*)line);
  173. /* compile node root */
  174. if (finsh_errno() == 0)
  175. {
  176. finsh_compiler_run(parser->root);
  177. }
  178. else
  179. {
  180. err_str = finsh_error_string(finsh_errno());
  181. rt_kprintf("%s\n", err_str);
  182. }
  183. /* run virtual machine */
  184. if (finsh_errno() == 0)
  185. {
  186. char ch;
  187. finsh_vm_run();
  188. ch = (unsigned char)finsh_stack_bottom();
  189. if (ch > 0x20 && ch < 0x7e)
  190. {
  191. rt_kprintf("\t'%c', %d, 0x%08x\n",
  192. (unsigned char)finsh_stack_bottom(),
  193. (unsigned int)finsh_stack_bottom(),
  194. (unsigned int)finsh_stack_bottom());
  195. }
  196. else
  197. {
  198. rt_kprintf("\t%d, 0x%08x\n",
  199. (unsigned int)finsh_stack_bottom(),
  200. (unsigned int)finsh_stack_bottom());
  201. }
  202. }
  203. finsh_flush(parser);
  204. }
  205. #ifdef FINSH_USING_HISTORY
  206. rt_bool_t finsh_handle_history(struct finsh_shell* shell, char ch)
  207. {
  208. /*
  209. * handle up and down key
  210. * up key : 0x1b 0x5b 0x41
  211. * down key: 0x1b 0x5b 0x42
  212. */
  213. if (ch == 0x1b)
  214. {
  215. shell->stat = WAIT_SPEC_KEY;
  216. return RT_TRUE;
  217. }
  218. if ((shell->stat == WAIT_SPEC_KEY))
  219. {
  220. if (ch == 0x5b)
  221. {
  222. shell->stat = WAIT_FUNC_KEY;
  223. return RT_TRUE;
  224. }
  225. shell->stat = WAIT_NORMAL;
  226. return RT_FALSE;
  227. }
  228. if (shell->stat == WAIT_FUNC_KEY)
  229. {
  230. shell->stat = WAIT_NORMAL;
  231. if (ch == 0x41) /* up key */
  232. {
  233. /* prev history */
  234. if (shell->current_history > 0)shell->current_history --;
  235. else
  236. {
  237. shell->current_history = 0;
  238. return RT_TRUE;
  239. }
  240. /* copy the history command */
  241. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  242. FINSH_CMD_SIZE);
  243. shell->line_position = strlen(shell->line);
  244. shell->use_history = 1;
  245. }
  246. else if (ch == 0x42) /* down key */
  247. {
  248. /* next history */
  249. if (shell->current_history < shell->history_count - 1)
  250. shell->current_history ++;
  251. else
  252. {
  253. /* set to the end of history */
  254. if (shell->history_count != 0)
  255. {
  256. shell->current_history = shell->history_count - 1;
  257. }
  258. else return RT_TRUE;
  259. }
  260. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  261. FINSH_CMD_SIZE);
  262. shell->line_position = strlen(shell->line);
  263. shell->use_history = 1;
  264. }
  265. if (shell->use_history)
  266. {
  267. #if defined(_WIN32)
  268. int i;
  269. rt_kprintf("\r");
  270. for(i=0; i<= 60; i++)
  271. putchar(' ');
  272. rt_kprintf("\r");
  273. #else
  274. rt_kprintf("\033[2K\r");
  275. #endif
  276. rt_kprintf("%s%s", FINSH_PROMPT, shell->line);
  277. return RT_TRUE;;
  278. }
  279. }
  280. return RT_FALSE;
  281. }
  282. void finsh_push_history(struct finsh_shell* shell)
  283. {
  284. if ((shell->use_history == 0) && (shell->line_position != 0))
  285. {
  286. /* push history */
  287. if (shell->history_count >= FINSH_HISTORY_LINES)
  288. {
  289. /* move history */
  290. int index;
  291. for (index = 0; index < FINSH_HISTORY_LINES - 1; index ++)
  292. {
  293. memcpy(&shell->cmd_history[index][0],
  294. &shell->cmd_history[index + 1][0], FINSH_CMD_SIZE);
  295. }
  296. memset(&shell->cmd_history[index][0], 0, FINSH_CMD_SIZE);
  297. memcpy(&shell->cmd_history[index][0], shell->line, shell->line_position);
  298. /* it's the maximum history */
  299. shell->history_count = FINSH_HISTORY_LINES;
  300. }
  301. else
  302. {
  303. memset(&shell->cmd_history[shell->history_count][0], 0, FINSH_CMD_SIZE);
  304. memcpy(&shell->cmd_history[shell->history_count][0], shell->line, shell->line_position);
  305. /* increase count and set current history position */
  306. shell->history_count ++;
  307. }
  308. }
  309. shell->current_history = shell->history_count;
  310. }
  311. #endif
  312. #ifndef RT_USING_HEAP
  313. struct finsh_shell _shell;
  314. #endif
  315. void finsh_thread_entry(void* parameter)
  316. {
  317. char ch;
  318. /* normal is echo mode */
  319. shell->echo_mode = 1;
  320. finsh_init(&shell->parser);
  321. rt_kprintf(FINSH_PROMPT);
  322. while (1)
  323. {
  324. /* wait receive */
  325. if (rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue;
  326. /* read one character from device */
  327. while (rt_device_read(shell->device, 0, &ch, 1) == 1)
  328. {
  329. /* handle history key */
  330. #ifdef FINSH_USING_HISTORY
  331. if (finsh_handle_history(shell, ch) == RT_TRUE) continue;
  332. #endif
  333. /* handle CR key */
  334. if (ch == '\r')
  335. {
  336. char next;
  337. if (rt_device_read(shell->device, 0, &next, 1) == 1)
  338. ch = next;
  339. else ch = '\r';
  340. }
  341. /* handle tab key */
  342. else if (ch == '\t')
  343. {
  344. /* auto complete */
  345. finsh_auto_complete(&shell->line[0]);
  346. /* re-calculate position */
  347. shell->line_position = strlen(shell->line);
  348. continue;
  349. }
  350. /* handle backspace key */
  351. else if (ch == 0x7f || ch == 0x08)
  352. {
  353. if (shell->line_position != 0)
  354. {
  355. rt_kprintf("%c %c", ch, ch);
  356. }
  357. if (shell->line_position <= 0) shell->line_position = 0;
  358. else shell->line_position --;
  359. shell->line[shell->line_position] = 0;
  360. continue;
  361. }
  362. /* handle end of line, break */
  363. if (ch == '\r' || ch == '\n')
  364. {
  365. /* change to ';' and break */
  366. shell->line[shell->line_position] = ';';
  367. #ifdef FINSH_USING_HISTORY
  368. finsh_push_history(shell);
  369. #endif
  370. if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
  371. else rt_kprintf("\n");
  372. rt_kprintf(FINSH_PROMPT);
  373. memset(shell->line, 0, sizeof(shell->line));
  374. shell->line_position = 0;
  375. break;
  376. }
  377. /* it's a large line, discard it */
  378. if (shell->line_position >= FINSH_CMD_SIZE) shell->line_position = 0;
  379. /* normal character */
  380. shell->line[shell->line_position] = ch; ch = 0;
  381. if (shell->echo_mode) rt_kprintf("%c", shell->line[shell->line_position]);
  382. shell->line_position ++;
  383. shell->use_history = 0; /* it's a new command */
  384. } /* end of device read */
  385. }
  386. }
  387. void finsh_system_function_init(const void* begin, const void* end)
  388. {
  389. _syscall_table_begin = (struct finsh_syscall*) begin;
  390. _syscall_table_end = (struct finsh_syscall*) end;
  391. }
  392. void finsh_system_var_init(const void* begin, const void* end)
  393. {
  394. _sysvar_table_begin = (struct finsh_sysvar*) begin;
  395. _sysvar_table_end = (struct finsh_sysvar*) end;
  396. }
  397. #if defined(__ICCARM__) /* for IAR compiler */
  398. #ifdef FINSH_USING_SYMTAB
  399. #pragma section="FSymTab"
  400. #pragma section="VSymTab"
  401. #endif
  402. #elif defined(__ADSPBLACKFIN__) /* for VisaulDSP++ Compiler*/
  403. #ifdef FINSH_USING_SYMTAB
  404. extern "asm" int __fsymtab_start;
  405. extern "asm" int __fsymtab_end;
  406. extern "asm" int __vsymtab_start;
  407. extern "asm" int __vsymtab_end;
  408. #endif
  409. #elif defined(_MSC_VER)
  410. #pragma section("FSymTab$a", read)
  411. const char __fsym_begin_name[] = "__start";
  412. const char __fsym_begin_desc[] = "begin of finsh";
  413. __declspec(allocate("FSymTab$a")) const struct finsh_syscall __fsym_begin =
  414. {
  415. __fsym_begin_name,
  416. __fsym_begin_desc,
  417. NULL
  418. };
  419. #pragma section("FSymTab$z", read)
  420. const char __fsym_end_name[] = "__end";
  421. const char __fsym_end_desc[] = "end of finsh";
  422. __declspec(allocate("FSymTab$z")) const struct finsh_syscall __fsym_end =
  423. {
  424. __fsym_end_name,
  425. __fsym_end_desc,
  426. NULL
  427. };
  428. #endif
  429. /*
  430. * @ingroup finsh
  431. *
  432. * This function will initialize finsh shell
  433. */
  434. void finsh_system_init(void)
  435. {
  436. rt_err_t result;
  437. #ifdef FINSH_USING_SYMTAB
  438. #ifdef __CC_ARM /* ARM C Compiler */
  439. extern const int FSymTab$$Base;
  440. extern const int FSymTab$$Limit;
  441. extern const int VSymTab$$Base;
  442. extern const int VSymTab$$Limit;
  443. finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
  444. finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
  445. #elif defined (__ICCARM__) /* for IAR Compiler */
  446. finsh_system_function_init(__section_begin("FSymTab"),
  447. __section_end("FSymTab"));
  448. finsh_system_var_init(__section_begin("VSymTab"),
  449. __section_end("VSymTab"));
  450. #elif defined (__GNUC__) /* GNU GCC Compiler */
  451. extern const int __fsymtab_start;
  452. extern const int __fsymtab_end;
  453. extern const int __vsymtab_start;
  454. extern const int __vsymtab_end;
  455. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  456. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  457. #elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
  458. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  459. finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
  460. #elif defined(_MSC_VER)
  461. unsigned int *ptr_begin, *ptr_end;
  462. ptr_begin = (unsigned int*)&__fsym_begin; ptr_begin += (sizeof(struct finsh_syscall)/sizeof(unsigned int));
  463. while (*ptr_begin == 0) ptr_begin ++;
  464. ptr_end = (unsigned int*) &__fsym_end; ptr_end --;
  465. while (*ptr_end == 0) ptr_end --;
  466. finsh_system_function_init(ptr_begin, ptr_end);
  467. #endif
  468. #endif
  469. /* create or set shell structure */
  470. #ifdef RT_USING_HEAP
  471. shell = (struct finsh_shell*)rt_malloc(sizeof(struct finsh_shell));
  472. #else
  473. shell = &_shell;
  474. #endif
  475. if (shell == RT_NULL)
  476. {
  477. rt_kprintf("no memory for shell\n");
  478. return;
  479. }
  480. memset(shell, 0, sizeof(struct finsh_shell));
  481. rt_sem_init(&(shell->rx_sem), "shrx", 0, 0);
  482. result = rt_thread_init(&finsh_thread,
  483. "tshell",
  484. finsh_thread_entry, RT_NULL,
  485. &finsh_thread_stack[0], sizeof(finsh_thread_stack),
  486. FINSH_THREAD_PRIORITY, 10);
  487. if (result == RT_EOK)
  488. rt_thread_startup(&finsh_thread);
  489. }