msh.c 14 KB

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