shell.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. */
  20. #include <rtthread.h>
  21. #include <rthw.h>
  22. #include "finsh.h"
  23. #include "shell.h"
  24. /* finsh thread */
  25. static struct rt_thread finsh_thread;
  26. ALIGN(RT_ALIGN_SIZE)
  27. static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE];
  28. struct finsh_shell* shell;
  29. #if !defined (RT_USING_NEWLIB) && !defined (RT_USING_MINILIBC)
  30. int strcmp (const char *s1, const char *s2)
  31. {
  32. while (*s1 && *s1 == *s2) s1++, s2++;
  33. return (*s1 - *s2);
  34. }
  35. #ifdef RT_USING_HEAP
  36. char *strdup(const char *s)
  37. {
  38. size_t len = strlen(s) + 1;
  39. char *tmp = (char *)rt_malloc(len);
  40. if(!tmp) return NULL;
  41. rt_memcpy(tmp, s, len);
  42. return tmp;
  43. }
  44. #endif
  45. #if !defined(__CC_ARM) && !defined(__IAR_SYSTEMS_ICC__)
  46. int isalpha( int ch )
  47. {
  48. return (unsigned int)((ch | 0x20) - 'a') < 26u;
  49. }
  50. int atoi(const char* s)
  51. {
  52. long int v=0;
  53. int sign=1;
  54. while ( *s == ' ' || (unsigned int)(*s - 9) < 5u) s++;
  55. switch (*s)
  56. {
  57. case '-': sign=-1;
  58. case '+': ++s;
  59. }
  60. while ((unsigned int) (*s - '0') < 10u)
  61. {
  62. v=v*10+*s-'0'; ++s;
  63. }
  64. return sign==-1?-v:v;
  65. }
  66. int isprint(unsigned char ch)
  67. {
  68. return (unsigned int)(ch - ' ') < 127u - ' ';
  69. }
  70. #endif
  71. #endif
  72. static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
  73. {
  74. RT_ASSERT(shell != RT_NULL);
  75. /* release semaphore to let finsh thread rx data */
  76. rt_sem_release(&shell->rx_sem);
  77. return RT_EOK;
  78. }
  79. void finsh_set_device(const char* device_name)
  80. {
  81. rt_device_t dev = RT_NULL;
  82. RT_ASSERT(shell != RT_NULL);
  83. dev = rt_device_find(device_name);
  84. if (dev != RT_NULL && rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) == RT_EOK)
  85. {
  86. if (shell->device != RT_NULL)
  87. {
  88. /* close old finsh device */
  89. rt_device_close(shell->device);
  90. }
  91. shell->device = dev;
  92. rt_device_set_rx_indicate(dev, finsh_rx_ind);
  93. }
  94. else
  95. {
  96. rt_kprintf("finsh: can not find device:%s\n", device_name);
  97. }
  98. }
  99. const char* finsh_get_device()
  100. {
  101. RT_ASSERT(shell != RT_NULL);
  102. return shell->device->parent.name;
  103. }
  104. void finsh_set_echo(rt_uint32_t enable)
  105. {
  106. RT_ASSERT(shell != RT_NULL);
  107. shell->echo_mode = enable;
  108. }
  109. rt_uint32_t finsh_get_echo()
  110. {
  111. RT_ASSERT(shell != RT_NULL);
  112. return shell->echo_mode;
  113. }
  114. void finsh_auto_complete(char* prefix)
  115. {
  116. extern void list_prefix(char* prefix);
  117. rt_kprintf("\n");
  118. list_prefix(prefix);
  119. rt_kprintf("finsh>>%s", prefix);
  120. }
  121. void finsh_run_line(struct finsh_parser* parser, const char *line)
  122. {
  123. const char* err_str;
  124. rt_kprintf("\n");
  125. finsh_parser_run(parser, (unsigned char*)line);
  126. /* compile node root */
  127. if (finsh_errno() == 0)
  128. {
  129. finsh_compiler_run(parser->root);
  130. }
  131. else
  132. {
  133. err_str = finsh_error_string(finsh_errno());
  134. rt_kprintf("%s\n", err_str);
  135. }
  136. /* run virtual machine */
  137. if (finsh_errno() == 0)
  138. {
  139. char ch;
  140. finsh_vm_run();
  141. ch = (unsigned char)finsh_stack_bottom();
  142. if (ch > 0x20 && ch < 0x7e)
  143. {
  144. rt_kprintf("\t'%c', %d, 0x%08x\n",
  145. (unsigned char)finsh_stack_bottom(),
  146. (unsigned int)finsh_stack_bottom(),
  147. (unsigned int)finsh_stack_bottom());
  148. }
  149. else
  150. {
  151. rt_kprintf("\t%d, 0x%08x\n",
  152. (unsigned int)finsh_stack_bottom(),
  153. (unsigned int)finsh_stack_bottom());
  154. }
  155. }
  156. finsh_flush(parser);
  157. }
  158. #ifdef FINSH_USING_HISTORY
  159. rt_bool_t finsh_handle_history(struct finsh_shell* shell, char ch)
  160. {
  161. /*
  162. * handle up and down key
  163. * up key : 0x1b 0x5b 0x41
  164. * down key: 0x1b 0x5b 0x42
  165. */
  166. if (ch == 0x1b)
  167. {
  168. shell->stat = WAIT_SPEC_KEY;
  169. return RT_TRUE;
  170. }
  171. if ((shell->stat == WAIT_SPEC_KEY))
  172. {
  173. if (ch == 0x5b)
  174. {
  175. shell->stat = WAIT_FUNC_KEY;
  176. return RT_TRUE;
  177. }
  178. shell->stat = WAIT_NORMAL;
  179. return RT_FALSE;
  180. }
  181. if (shell->stat == WAIT_FUNC_KEY)
  182. {
  183. shell->stat = WAIT_NORMAL;
  184. if (ch == 0x41) /* up key */
  185. {
  186. /* prev history */
  187. if (shell->current_history > 0)shell->current_history --;
  188. else
  189. {
  190. shell->current_history = 0;
  191. return RT_TRUE;
  192. }
  193. /* copy the history command */
  194. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  195. FINSH_CMD_SIZE);
  196. shell->line_position = strlen(shell->line);
  197. shell->use_history = 1;
  198. }
  199. else if (ch == 0x42) /* down key */
  200. {
  201. /* next history */
  202. if (shell->current_history < shell->history_count - 1)
  203. shell->current_history ++;
  204. else
  205. {
  206. /* set to the end of history */
  207. if (shell->history_count != 0)
  208. {
  209. shell->current_history = shell->history_count - 1;
  210. }
  211. else return RT_TRUE;
  212. }
  213. memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
  214. FINSH_CMD_SIZE);
  215. shell->line_position = strlen(shell->line);
  216. shell->use_history = 1;
  217. }
  218. if (shell->use_history)
  219. {
  220. rt_kprintf("\033[2K\r");
  221. rt_kprintf("%s%s", FINSH_PROMPT, shell->line);
  222. return RT_TRUE;;
  223. }
  224. }
  225. return RT_FALSE;
  226. }
  227. void finsh_push_history(struct finsh_shell* shell)
  228. {
  229. if ((shell->use_history == 0) && (shell->line_position != 0))
  230. {
  231. /* push history */
  232. if (shell->history_count >= FINSH_HISTORY_LINES)
  233. {
  234. /* move history */
  235. int index;
  236. for (index = 0; index < FINSH_HISTORY_LINES - 1; index ++)
  237. {
  238. memcpy(&shell->cmd_history[index][0],
  239. &shell->cmd_history[index + 1][0], FINSH_CMD_SIZE);
  240. }
  241. memset(&shell->cmd_history[index][0], 0, FINSH_CMD_SIZE);
  242. memcpy(&shell->cmd_history[index][0], shell->line, shell->line_position);
  243. /* it's the maximum history */
  244. shell->history_count = FINSH_HISTORY_LINES;
  245. }
  246. else
  247. {
  248. memset(&shell->cmd_history[shell->history_count][0], 0, FINSH_CMD_SIZE);
  249. memcpy(&shell->cmd_history[shell->history_count][0], shell->line, shell->line_position);
  250. /* increase count and set current history position */
  251. shell->history_count ++;
  252. }
  253. }
  254. shell->current_history = shell->history_count;
  255. }
  256. #endif
  257. #ifndef RT_USING_HEAP
  258. struct finsh_shell _shell;
  259. #endif
  260. void finsh_thread_entry(void* parameter)
  261. {
  262. char ch;
  263. /* normal is echo mode */
  264. shell->echo_mode = 1;
  265. finsh_init(&shell->parser);
  266. rt_kprintf(FINSH_PROMPT);
  267. while (1)
  268. {
  269. /* wait receive */
  270. if (rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue;
  271. /* read one character from device */
  272. while (rt_device_read(shell->device, 0, &ch, 1) == 1)
  273. {
  274. /* handle history key */
  275. #ifdef FINSH_USING_HISTORY
  276. if (finsh_handle_history(shell, ch) == RT_TRUE) continue;
  277. #endif
  278. /* handle CR key */
  279. if (ch == '\r')
  280. {
  281. char next;
  282. if (rt_device_read(shell->device, 0, &next, 1) == 1)
  283. ch = next;
  284. else ch = '\r';
  285. }
  286. /* handle tab key */
  287. else if (ch == '\t')
  288. {
  289. /* auto complete */
  290. finsh_auto_complete(&shell->line[0]);
  291. /* re-calculate position */
  292. shell->line_position = strlen(shell->line);
  293. continue;
  294. }
  295. /* handle backspace key */
  296. else if (ch == 0x7f || ch == 0x08)
  297. {
  298. if (shell->line_position != 0)
  299. {
  300. rt_kprintf("%c %c", ch, ch);
  301. }
  302. if (shell->line_position <= 0) shell->line_position = 0;
  303. else shell->line_position --;
  304. shell->line[shell->line_position] = 0;
  305. continue;
  306. }
  307. /* handle end of line, break */
  308. if (ch == '\r' || ch == '\n')
  309. {
  310. /* change to ';' and break */
  311. shell->line[shell->line_position] = ';';
  312. #ifdef FINSH_USING_HISTORY
  313. finsh_push_history(shell);
  314. #endif
  315. if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
  316. else rt_kprintf("\n");
  317. rt_kprintf(FINSH_PROMPT);
  318. memset(shell->line, 0, sizeof(shell->line));
  319. shell->line_position = 0;
  320. break;
  321. }
  322. /* it's a large line, discard it */
  323. if (shell->line_position >= FINSH_CMD_SIZE) shell->line_position = 0;
  324. /* normal character */
  325. shell->line[shell->line_position] = ch; ch = 0;
  326. if (shell->echo_mode) rt_kprintf("%c", shell->line[shell->line_position]);
  327. shell->line_position ++;
  328. shell->use_history = 0; /* it's a new command */
  329. } /* end of device read */
  330. }
  331. }
  332. void finsh_system_function_init(void* begin, void* end)
  333. {
  334. _syscall_table_begin = (struct finsh_syscall*) begin;
  335. _syscall_table_end = (struct finsh_syscall*) end;
  336. }
  337. void finsh_system_var_init(void* begin, void* end)
  338. {
  339. _sysvar_table_begin = (struct finsh_sysvar*) begin;
  340. _sysvar_table_end = (struct finsh_sysvar*) end;
  341. }
  342. #if defined(__ICCARM__) /* for IAR compiler */
  343. #ifdef FINSH_USING_SYMTAB
  344. #pragma section="FSymTab"
  345. #pragma section="VSymTab"
  346. #endif
  347. #endif
  348. /* init finsh */
  349. void finsh_system_init(void)
  350. {
  351. rt_err_t result;
  352. #ifdef FINSH_USING_SYMTAB
  353. #ifdef __CC_ARM /* ARM C Compiler */
  354. extern int FSymTab$$Base;
  355. extern int FSymTab$$Limit;
  356. extern int VSymTab$$Base;
  357. extern int VSymTab$$Limit;
  358. finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
  359. finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
  360. #elif defined (__ICCARM__) /* for IAR Compiler */
  361. finsh_system_function_init(__section_begin("FSymTab"),
  362. __section_end("FSymTab"));
  363. finsh_system_var_init(__section_begin("VSymTab"),
  364. __section_end("VSymTab"));
  365. #elif defined (__GNUC__) /* GNU GCC Compiler */
  366. extern int __fsymtab_start;
  367. extern int __fsymtab_end;
  368. extern int __vsymtab_start;
  369. extern int __vsymtab_end;
  370. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  371. finsh_system_var_init(&__vsymtab_start, &__vsymtab_start);
  372. #endif
  373. #endif
  374. /* create or set shell structure */
  375. #ifdef RT_USING_HEAP
  376. shell = (struct finsh_shell*)rt_malloc(sizeof(struct finsh_shell));
  377. #else
  378. shell = &_shell;
  379. #endif
  380. if (shell == RT_NULL)
  381. {
  382. rt_kprintf("no memory for shell\n");
  383. return;
  384. }
  385. memset(shell, 0, sizeof(struct finsh_shell));
  386. rt_sem_init(&(shell->rx_sem), "shrx", 0, 0);
  387. result = rt_thread_init(&finsh_thread,
  388. "tshell",
  389. finsh_thread_entry, RT_NULL,
  390. &finsh_thread_stack[0], sizeof(finsh_thread_stack),
  391. FINSH_THREAD_PRIORITY, 10);
  392. if (result == RT_EOK)
  393. rt_thread_startup(&finsh_thread);
  394. }