shell.c 12 KB

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