msh.c 14 KB

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