shell.c 11 KB

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