msh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. * 2013-03-30 Bernard the first verion for finsh
  9. * 2014-01-03 Bernard msh can execute module.
  10. * 2017-07-19 Aubr.Cool limit argc to RT_FINSH_ARG_MAX
  11. */
  12. #include <rtthread.h>
  13. #ifdef FINSH_USING_MSH
  14. #include "msh.h"
  15. #include <finsh.h>
  16. #include <shell.h>
  17. #ifdef RT_USING_DFS
  18. #include <dfs_posix.h>
  19. #endif
  20. #ifdef RT_USING_MODULE
  21. #include <dlmodule.h>
  22. #endif
  23. #ifndef FINSH_ARG_MAX
  24. #define FINSH_ARG_MAX 8
  25. #endif
  26. typedef int (*cmd_function_t)(int argc, char **argv);
  27. #ifdef FINSH_USING_MSH
  28. #ifdef FINSH_USING_MSH_ONLY
  29. rt_bool_t msh_is_used(void)
  30. {
  31. return RT_TRUE;
  32. }
  33. #else
  34. #ifdef FINSH_USING_MSH_DEFAULT
  35. static rt_bool_t __msh_state = RT_TRUE;
  36. #else
  37. static rt_bool_t __msh_state = RT_FALSE;
  38. #endif
  39. rt_bool_t msh_is_used(void)
  40. {
  41. return __msh_state;
  42. }
  43. static int msh_exit(int argc, char **argv)
  44. {
  45. /* return to finsh shell mode */
  46. __msh_state = RT_FALSE;
  47. return 0;
  48. }
  49. FINSH_FUNCTION_EXPORT_ALIAS(msh_exit, __cmd_exit, return to RT-Thread shell mode.);
  50. static int msh_enter(void)
  51. {
  52. /* enter module shell mode */
  53. __msh_state = RT_TRUE;
  54. return 0;
  55. }
  56. FINSH_FUNCTION_EXPORT_ALIAS(msh_enter, msh, use module shell);
  57. #endif
  58. int msh_help(int argc, char **argv)
  59. {
  60. rt_kprintf("RT-Thread shell commands:\n");
  61. {
  62. struct finsh_syscall *index;
  63. for (index = _syscall_table_begin;
  64. index < _syscall_table_end;
  65. FINSH_NEXT_SYSCALL(index))
  66. {
  67. if (strncmp(index->name, "__cmd_", 6) != 0) continue;
  68. #if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
  69. rt_kprintf("%-16s - %s\n", &index->name[6], index->desc);
  70. #else
  71. rt_kprintf("%s ", &index->name[6]);
  72. #endif
  73. }
  74. }
  75. rt_kprintf("\n");
  76. return 0;
  77. }
  78. FINSH_FUNCTION_EXPORT_ALIAS(msh_help, __cmd_help, RT-Thread shell help.);
  79. int cmd_ps(int argc, char **argv)
  80. {
  81. extern long list_thread(void);
  82. extern int list_module(void);
  83. #ifdef RT_USING_MODULE
  84. if ((argc == 2) && (strcmp(argv[1], "-m") == 0))
  85. list_module();
  86. else
  87. #endif
  88. list_thread();
  89. return 0;
  90. }
  91. FINSH_FUNCTION_EXPORT_ALIAS(cmd_ps, __cmd_ps, List threads in the system.);
  92. #ifdef RT_USING_HEAP
  93. int cmd_free(int argc, char **argv)
  94. {
  95. extern void list_mem(void);
  96. extern void list_memheap(void);
  97. #ifdef RT_USING_MEMHEAP_AS_HEAP
  98. list_memheap();
  99. #else
  100. list_mem();
  101. #endif
  102. return 0;
  103. }
  104. FINSH_FUNCTION_EXPORT_ALIAS(cmd_free, __cmd_free, Show the memory usage in the system.);
  105. #endif
  106. static int msh_split(char *cmd, rt_size_t length, char *argv[FINSH_ARG_MAX])
  107. {
  108. char *ptr;
  109. rt_size_t position;
  110. rt_size_t argc;
  111. rt_size_t i;
  112. ptr = cmd;
  113. position = 0; argc = 0;
  114. while (position < length)
  115. {
  116. /* strip bank and tab */
  117. while ((*ptr == ' ' || *ptr == '\t') && position < length)
  118. {
  119. *ptr = '\0';
  120. ptr ++; position ++;
  121. }
  122. if(argc >= FINSH_ARG_MAX)
  123. {
  124. rt_kprintf("Too many args ! We only Use:\n");
  125. for(i = 0; i < argc; i++)
  126. {
  127. rt_kprintf("%s ", argv[i]);
  128. }
  129. rt_kprintf("\n");
  130. break;
  131. }
  132. if (position >= length) break;
  133. /* handle string */
  134. if (*ptr == '"')
  135. {
  136. ptr ++; position ++;
  137. argv[argc] = ptr; argc ++;
  138. /* skip this string */
  139. while (*ptr != '"' && position < length)
  140. {
  141. if (*ptr == '\\')
  142. {
  143. if (*(ptr + 1) == '"')
  144. {
  145. ptr ++; position ++;
  146. }
  147. }
  148. ptr ++; position ++;
  149. }
  150. if (position >= length) break;
  151. /* skip '"' */
  152. *ptr = '\0'; ptr ++; position ++;
  153. }
  154. else
  155. {
  156. argv[argc] = ptr;
  157. argc ++;
  158. while ((*ptr != ' ' && *ptr != '\t') && position < length)
  159. {
  160. ptr ++; position ++;
  161. }
  162. if (position >= length) break;
  163. }
  164. }
  165. return argc;
  166. }
  167. static cmd_function_t msh_get_cmd(char *cmd, int size)
  168. {
  169. struct finsh_syscall *index;
  170. cmd_function_t cmd_func = RT_NULL;
  171. for (index = _syscall_table_begin;
  172. index < _syscall_table_end;
  173. FINSH_NEXT_SYSCALL(index))
  174. {
  175. if (strncmp(index->name, "__cmd_", 6) != 0) continue;
  176. if (strncmp(&index->name[6], cmd, size) == 0 &&
  177. index->name[6 + size] == '\0')
  178. {
  179. cmd_func = (cmd_function_t)index->func;
  180. break;
  181. }
  182. }
  183. return cmd_func;
  184. }
  185. #if defined(RT_USING_MODULE) && defined(RT_USING_DFS)
  186. /* Return 0 on module executed. Other value indicate error.
  187. */
  188. int msh_exec_module(const char *cmd_line, int size)
  189. {
  190. int ret;
  191. int fd = -1;
  192. char *pg_name;
  193. int length, cmd_length = 0;
  194. if (size == 0)
  195. return -RT_ERROR;
  196. /* get the length of command0 */
  197. while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
  198. cmd_length ++;
  199. /* get name length */
  200. length = cmd_length + 32;
  201. /* allocate program name memory */
  202. pg_name = (char *) rt_malloc(length);
  203. if (pg_name == RT_NULL)
  204. return -RT_ENOMEM;
  205. /* copy command0 */
  206. memcpy(pg_name, cmd_line, cmd_length);
  207. pg_name[cmd_length] = '\0';
  208. if (strstr(pg_name, ".mo") != RT_NULL || strstr(pg_name, ".MO") != RT_NULL)
  209. {
  210. /* try to open program */
  211. fd = open(pg_name, O_RDONLY, 0);
  212. /* search in /bin path */
  213. if (fd < 0)
  214. {
  215. rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
  216. fd = open(pg_name, O_RDONLY, 0);
  217. }
  218. }
  219. else
  220. {
  221. /* add .mo and open program */
  222. /* try to open program */
  223. strcat(pg_name, ".mo");
  224. fd = open(pg_name, O_RDONLY, 0);
  225. /* search in /bin path */
  226. if (fd < 0)
  227. {
  228. rt_snprintf(pg_name, length - 1, "/bin/%.*s.mo", cmd_length, cmd_line);
  229. fd = open(pg_name, O_RDONLY, 0);
  230. }
  231. }
  232. if (fd >= 0)
  233. {
  234. /* found program */
  235. close(fd);
  236. dlmodule_exec(pg_name, cmd_line, size);
  237. ret = 0;
  238. }
  239. else
  240. {
  241. ret = -1;
  242. }
  243. rt_free(pg_name);
  244. return ret;
  245. }
  246. int system(const char *command)
  247. {
  248. int ret = -RT_ENOMEM;
  249. char *cmd = rt_strdup(command);
  250. if (cmd)
  251. {
  252. ret = msh_exec(cmd, rt_strlen(cmd));
  253. rt_free(cmd);
  254. }
  255. return ret;
  256. }
  257. #endif
  258. static int _msh_exec_cmd(char *cmd, rt_size_t length, int *retp)
  259. {
  260. int argc;
  261. rt_size_t cmd0_size = 0;
  262. cmd_function_t cmd_func;
  263. char *argv[FINSH_ARG_MAX];
  264. RT_ASSERT(cmd);
  265. RT_ASSERT(retp);
  266. /* find the size of first command */
  267. while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
  268. cmd0_size ++;
  269. if (cmd0_size == 0)
  270. return -RT_ERROR;
  271. cmd_func = msh_get_cmd(cmd, cmd0_size);
  272. if (cmd_func == RT_NULL)
  273. return -RT_ERROR;
  274. /* split arguments */
  275. memset(argv, 0x00, sizeof(argv));
  276. argc = msh_split(cmd, length, argv);
  277. if (argc == 0)
  278. return -RT_ERROR;
  279. /* exec this command */
  280. *retp = cmd_func(argc, argv);
  281. return 0;
  282. }
  283. #if defined(RT_USING_LWP) && defined(RT_USING_DFS)
  284. static int _msh_exec_lwp(char *cmd, rt_size_t length)
  285. {
  286. int argc;
  287. int cmd0_size = 0;
  288. char *argv[FINSH_ARG_MAX];
  289. int fd = -1;
  290. char *pg_name;
  291. extern int exec(char*, int, char**);
  292. /* find the size of first command */
  293. while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
  294. cmd0_size ++;
  295. if (cmd0_size == 0)
  296. return -1;
  297. /* split arguments */
  298. rt_memset(argv, 0x00, sizeof(argv));
  299. argc = msh_split(cmd, length, argv);
  300. if (argc == 0)
  301. return -1;
  302. pg_name = argv[0];
  303. /* try to open program */
  304. fd = open(pg_name, O_RDONLY, 0);
  305. if (fd < 0)
  306. return -1;
  307. /* found program */
  308. close(fd);
  309. exec(pg_name, argc, argv);
  310. return 0;
  311. }
  312. #endif
  313. int msh_exec(char *cmd, rt_size_t length)
  314. {
  315. int cmd_ret;
  316. /* strim the beginning of command */
  317. while ((length > 0) && (*cmd == ' ' || *cmd == '\t'))
  318. {
  319. cmd++;
  320. length--;
  321. }
  322. if (length == 0)
  323. return 0;
  324. /* Exec sequence:
  325. * 1. built-in command
  326. * 2. module(if enabled)
  327. */
  328. if (_msh_exec_cmd(cmd, length, &cmd_ret) == 0)
  329. {
  330. return cmd_ret;
  331. }
  332. #ifdef RT_USING_DFS
  333. #ifdef DFS_USING_WORKDIR
  334. if (msh_exec_script(cmd, length) == 0)
  335. {
  336. return 0;
  337. }
  338. #endif
  339. #ifdef RT_USING_MODULE
  340. if (msh_exec_module(cmd, length) == 0)
  341. {
  342. return 0;
  343. }
  344. #endif
  345. #ifdef RT_USING_LWP
  346. if (_msh_exec_lwp(cmd, length) == 0)
  347. {
  348. return 0;
  349. }
  350. #endif
  351. #endif
  352. /* truncate the cmd at the first space. */
  353. {
  354. char *tcmd;
  355. tcmd = cmd;
  356. while (*tcmd != ' ' && *tcmd != '\0')
  357. {
  358. tcmd++;
  359. }
  360. *tcmd = '\0';
  361. }
  362. rt_kprintf("%s: command not found.\n", cmd);
  363. return -1;
  364. }
  365. static int str_common(const char *str1, const char *str2)
  366. {
  367. const char *str = str1;
  368. while ((*str != 0) && (*str2 != 0) && (*str == *str2))
  369. {
  370. str ++;
  371. str2 ++;
  372. }
  373. return (str - str1);
  374. }
  375. #ifdef RT_USING_DFS
  376. void msh_auto_complete_path(char *path)
  377. {
  378. DIR *dir = RT_NULL;
  379. struct dirent *dirent = RT_NULL;
  380. char *full_path, *ptr, *index;
  381. if (!path)
  382. return;
  383. full_path = (char *)rt_malloc(256);
  384. if (full_path == RT_NULL) return; /* out of memory */
  385. if (*path != '/')
  386. {
  387. getcwd(full_path, 256);
  388. if (full_path[rt_strlen(full_path) - 1] != '/')
  389. strcat(full_path, "/");
  390. }
  391. else *full_path = '\0';
  392. index = RT_NULL;
  393. ptr = path;
  394. for (;;)
  395. {
  396. if (*ptr == '/') index = ptr + 1;
  397. if (!*ptr) break;
  398. ptr ++;
  399. }
  400. if (index == RT_NULL) index = path;
  401. if (index != RT_NULL)
  402. {
  403. char *dest = index;
  404. /* fill the parent path */
  405. ptr = full_path;
  406. while (*ptr) ptr ++;
  407. for (index = path; index != dest;)
  408. *ptr++ = *index++;
  409. *ptr = '\0';
  410. dir = opendir(full_path);
  411. if (dir == RT_NULL) /* open directory failed! */
  412. {
  413. rt_free(full_path);
  414. return;
  415. }
  416. /* restore the index position */
  417. index = dest;
  418. }
  419. /* auto complete the file or directory name */
  420. if (*index == '\0') /* display all of files and directories */
  421. {
  422. for (;;)
  423. {
  424. dirent = readdir(dir);
  425. if (dirent == RT_NULL) break;
  426. rt_kprintf("%s\n", dirent->d_name);
  427. }
  428. }
  429. else
  430. {
  431. rt_size_t length, min_length;
  432. min_length = 0;
  433. for (;;)
  434. {
  435. dirent = readdir(dir);
  436. if (dirent == RT_NULL) break;
  437. /* matched the prefix string */
  438. if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
  439. {
  440. if (min_length == 0)
  441. {
  442. min_length = rt_strlen(dirent->d_name);
  443. /* save dirent name */
  444. strcpy(full_path, dirent->d_name);
  445. }
  446. length = str_common(dirent->d_name, full_path);
  447. if (length < min_length)
  448. {
  449. min_length = length;
  450. }
  451. }
  452. }
  453. if (min_length)
  454. {
  455. if (min_length < rt_strlen(full_path))
  456. {
  457. /* list the candidate */
  458. rewinddir(dir);
  459. for (;;)
  460. {
  461. dirent = readdir(dir);
  462. if (dirent == RT_NULL) break;
  463. if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
  464. rt_kprintf("%s\n", dirent->d_name);
  465. }
  466. }
  467. length = index - path;
  468. memcpy(index, full_path, min_length);
  469. path[length + min_length] = '\0';
  470. }
  471. }
  472. closedir(dir);
  473. rt_free(full_path);
  474. }
  475. #endif
  476. void msh_auto_complete(char *prefix)
  477. {
  478. int length, min_length;
  479. const char *name_ptr, *cmd_name;
  480. struct finsh_syscall *index;
  481. min_length = 0;
  482. name_ptr = RT_NULL;
  483. if (*prefix == '\0')
  484. {
  485. msh_help(0, RT_NULL);
  486. return;
  487. }
  488. #ifdef RT_USING_DFS
  489. /* check whether a spare in the command */
  490. {
  491. char *ptr;
  492. ptr = prefix + rt_strlen(prefix);
  493. while (ptr != prefix)
  494. {
  495. if (*ptr == ' ')
  496. {
  497. msh_auto_complete_path(ptr + 1);
  498. break;
  499. }
  500. ptr --;
  501. }
  502. #ifdef RT_USING_MODULE
  503. /* There is a chance that the user want to run the module directly. So
  504. * try to complete the file names. If the completed path is not a
  505. * module, the system won't crash anyway. */
  506. if (ptr == prefix)
  507. {
  508. msh_auto_complete_path(ptr);
  509. }
  510. #endif
  511. }
  512. #endif
  513. /* checks in internal command */
  514. {
  515. for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
  516. {
  517. /* skip finsh shell function */
  518. if (strncmp(index->name, "__cmd_", 6) != 0) continue;
  519. cmd_name = (const char *) &index->name[6];
  520. if (strncmp(prefix, cmd_name, strlen(prefix)) == 0)
  521. {
  522. if (min_length == 0)
  523. {
  524. /* set name_ptr */
  525. name_ptr = cmd_name;
  526. /* set initial length */
  527. min_length = strlen(name_ptr);
  528. }
  529. length = str_common(name_ptr, cmd_name);
  530. if (length < min_length)
  531. min_length = length;
  532. rt_kprintf("%s\n", cmd_name);
  533. }
  534. }
  535. }
  536. /* auto complete string */
  537. if (name_ptr != NULL)
  538. {
  539. rt_strncpy(prefix, name_ptr, min_length);
  540. }
  541. return ;
  542. }
  543. #endif
  544. #endif /* FINSH_USING_MSH */