shell.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #elif defined(__GNUC__)
  42. #ifdef FINSH_USING_SYMTAB
  43. extern int __fsymtab_start;
  44. extern int __fsymtab_end;
  45. extern int __vsymtab_start;
  46. extern int __vsymtab_end;
  47. #endif
  48. #endif
  49. /* finsh thread */
  50. struct rt_thread finsh_thread;
  51. char finsh_thread_stack[2048];
  52. struct rt_semaphore uart_sem;
  53. #ifdef RT_USING_DEVICE
  54. rt_device_t finsh_device;
  55. #endif
  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. #ifdef RT_USING_DEVICE
  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(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. #else
  157. void finsh_notify()
  158. {
  159. rt_sem_release(&uart_sem);
  160. }
  161. #endif
  162. void finsh_auto_complete(char* prefix)
  163. {
  164. extern void list_prefix(char* prefix);
  165. rt_kprintf("\n");
  166. list_prefix(prefix);
  167. rt_kprintf("finsh>>%s", prefix);
  168. }
  169. void finsh_thread_entry(void* parameter)
  170. {
  171. struct finsh_parser parser;
  172. char line[256];
  173. int pos ;
  174. #ifdef FINSH_USING_HISTORY
  175. enum input_stat stat;
  176. unsigned short current_history, use_history;
  177. #endif
  178. stat = WAIT_NORMAL;
  179. current_history = 0;
  180. use_history = 0;
  181. finsh_init(&parser);
  182. while (1)
  183. {
  184. rt_kprintf("finsh>>");
  185. memset(line, 0, sizeof(line));
  186. pos = 0;
  187. while (1)
  188. {
  189. if (rt_sem_take(&uart_sem, -1) == RT_EOK)
  190. {
  191. /* read one character from serial */
  192. char ch;
  193. #ifndef RT_USING_DEVICE
  194. ch = rt_serial_getc();
  195. if (ch != 0)
  196. #else
  197. rt_err_t rx_result;
  198. rx_result = rt_device_read(finsh_device, 0, &ch, 1);
  199. if (ch != 0 && rx_result == 1)
  200. #endif
  201. {
  202. #ifdef FINSH_USING_HISTORY
  203. /*
  204. * handle up and down key
  205. * up key : 0x1b 0x5b 0x41
  206. * down key: 0x1b 0x5b 0x42
  207. */
  208. if (ch == 0x1b)
  209. {
  210. stat = WAIT_SPEC_KEY;
  211. continue;
  212. }
  213. if ((stat == WAIT_SPEC_KEY) && (ch == 0x5b))
  214. {
  215. if (ch == 0x5b)
  216. {
  217. stat = WAIT_FUNC_KEY;
  218. continue;
  219. }
  220. stat = WAIT_NORMAL;
  221. }
  222. if (stat == WAIT_FUNC_KEY)
  223. {
  224. stat = WAIT_NORMAL;
  225. if (ch == 0x41) /* up key */
  226. {
  227. /* prev history */
  228. if (current_history > 0)current_history --;
  229. else
  230. {
  231. current_history = 0;
  232. continue;
  233. }
  234. /* copy the history command */
  235. memcpy(line, &finsh_cmd_history[current_history][0],
  236. FINSH_CMD_SIZE);
  237. pos = strlen(line);
  238. use_history = 1;
  239. }
  240. else if (ch == 0x42) /* down key */
  241. {
  242. /* next history */
  243. if (current_history < finsh_history_count - 1)
  244. current_history ++;
  245. else
  246. {
  247. current_history = finsh_history_count - 1;
  248. continue;
  249. }
  250. memcpy(line, &finsh_cmd_history[current_history][0],
  251. FINSH_CMD_SIZE);
  252. pos = strlen(line);
  253. use_history = 1;
  254. }
  255. if (use_history)
  256. {
  257. rt_kprintf("\033[2K\r");
  258. rt_kprintf("finsh>>%s", line);
  259. continue;
  260. }
  261. }
  262. #endif
  263. /*
  264. * handle tab key
  265. */
  266. if (ch == '\t')
  267. {
  268. /* auto complete */
  269. finsh_auto_complete(&line[0]);
  270. /* re-calculate position */
  271. pos = strlen(line);
  272. continue;
  273. }
  274. /*
  275. * handle backspace key
  276. */
  277. if (ch == 0x7f || ch == 0x08)
  278. {
  279. if (pos != 0)rt_kprintf("%c", ch);
  280. line[pos--] = 0;
  281. if (pos < 0) pos = 0;
  282. continue;
  283. }
  284. line[pos] = ch;
  285. rt_kprintf("%c", line[pos]);
  286. /* if it's the end of line, break */
  287. if (line[pos] == 0xd || line[pos] == 0xa)
  288. {
  289. /* change to ';' and break */
  290. line[pos] = ';';
  291. break;
  292. }
  293. else use_history = 0; /* not "\n", it's a new command */
  294. pos ++;
  295. }
  296. }
  297. }
  298. if (pos == 0)
  299. {
  300. rt_kprintf("\n");
  301. continue;
  302. }
  303. #ifdef FINSH_USING_HISTORY
  304. if (use_history == 0)
  305. {
  306. /* push history */
  307. if (finsh_history_count >= FINSH_HISTORY_LINES)
  308. {
  309. /* move history */
  310. int index;
  311. for (index = 0; index < FINSH_HISTORY_LINES - 1; index ++)
  312. {
  313. memcpy(&finsh_cmd_history[index][0],
  314. &finsh_cmd_history[index + 1][0], FINSH_CMD_SIZE);
  315. }
  316. memset(&finsh_cmd_history[index][0], 0, FINSH_CMD_SIZE);
  317. memcpy(&finsh_cmd_history[index][0], line, pos);
  318. /* it's the maximum history */
  319. finsh_history_count = FINSH_HISTORY_LINES;
  320. }
  321. else
  322. {
  323. memset(&finsh_cmd_history[finsh_history_count][0], 0, FINSH_CMD_SIZE);
  324. memcpy(&finsh_cmd_history[finsh_history_count][0], line, pos);
  325. /* increase count and set current history position */
  326. finsh_history_count ++;
  327. }
  328. }
  329. current_history = finsh_history_count;
  330. #endif
  331. rt_kprintf("\n");
  332. finsh_parser_run(&parser, (unsigned char*)&line[0]);
  333. /* compile node root */
  334. if (finsh_errno() == 0)
  335. {
  336. finsh_compiler_run(parser.root);
  337. }
  338. else
  339. {
  340. rt_kprintf("%s\n", finsh_error_string(finsh_errno()));
  341. }
  342. /* run virtual machine */
  343. if (finsh_errno() == 0)
  344. {
  345. finsh_vm_run();
  346. if (isprint((unsigned char)finsh_stack_bottom()))
  347. {
  348. rt_kprintf("\t'%c', %d, 0x%08x\n",
  349. (unsigned char)finsh_stack_bottom(),
  350. (unsigned int)finsh_stack_bottom(),
  351. (unsigned int)finsh_stack_bottom());
  352. }
  353. else
  354. {
  355. rt_kprintf("\t%d, 0x%04x\n",
  356. (unsigned int)finsh_stack_bottom(),
  357. (unsigned int)finsh_stack_bottom());
  358. }
  359. }
  360. finsh_flush(&parser);
  361. }
  362. }
  363. void finsh_system_function_init(void* begin, void* end)
  364. {
  365. _syscall_table_begin = (struct finsh_syscall*) begin;
  366. _syscall_table_end = (struct finsh_syscall*) end;
  367. }
  368. void finsh_system_var_init(void* begin, void* end)
  369. {
  370. _sysvar_table_begin = (struct finsh_sysvar*) begin;
  371. _sysvar_table_end = (struct finsh_sysvar*) end;
  372. }
  373. /* init finsh */
  374. void finsh_system_init()
  375. {
  376. rt_sem_init(&uart_sem, "uart", 0, 0);
  377. #ifdef FINSH_USING_SYMTAB
  378. #ifdef __CC_ARM /* ARM C Compiler */
  379. finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
  380. finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
  381. #elif defined (__ICCARM__) /* for IAR Compiler */
  382. finsh_system_function_init(__section_begin("FSymTab"),
  383. __section_end("FSymTab"));
  384. finsh_system_var_init(__section_begin("VSymTab"),
  385. __section_end("VSymTab"));
  386. #elif defined (__GNUC__) /* GNU GCC Compiler */
  387. finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
  388. finsh_system_var_init(&__vsymtab_start, &__vsymtab_start);
  389. #endif
  390. #endif
  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, 100);
  396. rt_thread_startup(&finsh_thread);
  397. }