shell.c 8.8 KB

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