copy_this_file_shell.c 13 KB

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