shell.c 11 KB

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