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