shell.c 9.7 KB

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