shell.c 9.0 KB

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