msh.c 14 KB

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