shell.c 9.3 KB

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