msh.c 14 KB

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