shell.c 10 KB

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