shell.c 10 KB

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