msh.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. int cmd_ps(int argc, char **argv)
  48. {
  49. extern long list_thread(void);
  50. extern int list_module(void);
  51. #ifdef RT_USING_MODULE
  52. if ((argc == 2) && (strcmp(argv[1], "-m") == 0))
  53. list_module();
  54. else
  55. #endif
  56. list_thread();
  57. return 0;
  58. }
  59. MSH_CMD_EXPORT_ALIAS(cmd_ps, ps, List threads in the system.);
  60. #ifdef RT_USING_HEAP
  61. int cmd_free(int argc, char **argv)
  62. {
  63. extern void list_mem(void);
  64. extern void list_memheap(void);
  65. #ifdef RT_USING_MEMHEAP_AS_HEAP
  66. list_memheap();
  67. #else
  68. list_mem();
  69. #endif
  70. return 0;
  71. }
  72. MSH_CMD_EXPORT_ALIAS(cmd_free, free, Show the memory usage in the system.);
  73. #endif
  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(RT_USING_DFS)
  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. int system(const char *command)
  223. {
  224. int ret = -RT_ENOMEM;
  225. char *cmd = rt_strdup(command);
  226. if (cmd)
  227. {
  228. ret = msh_exec(cmd, rt_strlen(cmd));
  229. rt_free(cmd);
  230. }
  231. return ret;
  232. }
  233. RTM_EXPORT(system);
  234. #endif
  235. static int _msh_exec_cmd(char *cmd, rt_size_t length, int *retp)
  236. {
  237. int argc;
  238. rt_size_t cmd0_size = 0;
  239. cmd_function_t cmd_func;
  240. char *argv[FINSH_ARG_MAX];
  241. RT_ASSERT(cmd);
  242. RT_ASSERT(retp);
  243. /* find the size of first command */
  244. while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
  245. cmd0_size ++;
  246. if (cmd0_size == 0)
  247. return -RT_ERROR;
  248. cmd_func = msh_get_cmd(cmd, cmd0_size);
  249. if (cmd_func == RT_NULL)
  250. return -RT_ERROR;
  251. /* split arguments */
  252. memset(argv, 0x00, sizeof(argv));
  253. argc = msh_split(cmd, length, argv);
  254. if (argc == 0)
  255. return -RT_ERROR;
  256. /* exec this command */
  257. *retp = cmd_func(argc, argv);
  258. return 0;
  259. }
  260. #if defined(RT_USING_LWP) && defined(RT_USING_DFS)
  261. static int _msh_exec_lwp(char *cmd, rt_size_t length)
  262. {
  263. int argc;
  264. int cmd0_size = 0;
  265. char *argv[FINSH_ARG_MAX];
  266. int fd = -1;
  267. char *pg_name;
  268. extern int exec(char *, int, char **);
  269. /* find the size of first command */
  270. while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
  271. cmd0_size ++;
  272. if (cmd0_size == 0)
  273. return -1;
  274. /* split arguments */
  275. rt_memset(argv, 0x00, sizeof(argv));
  276. argc = msh_split(cmd, length, argv);
  277. if (argc == 0)
  278. return -1;
  279. pg_name = argv[0];
  280. /* try to open program */
  281. fd = open(pg_name, O_RDONLY, 0);
  282. if (fd < 0)
  283. return -1;
  284. /* found program */
  285. close(fd);
  286. exec(pg_name, argc, argv);
  287. return 0;
  288. }
  289. #endif
  290. int msh_exec(char *cmd, rt_size_t length)
  291. {
  292. int cmd_ret;
  293. /* strim the beginning of command */
  294. while ((length > 0) && (*cmd == ' ' || *cmd == '\t'))
  295. {
  296. cmd++;
  297. length--;
  298. }
  299. if (length == 0)
  300. return 0;
  301. /* Exec sequence:
  302. * 1. built-in command
  303. * 2. module(if enabled)
  304. */
  305. if (_msh_exec_cmd(cmd, length, &cmd_ret) == 0)
  306. {
  307. return cmd_ret;
  308. }
  309. #ifdef RT_USING_DFS
  310. #ifdef DFS_USING_WORKDIR
  311. if (msh_exec_script(cmd, length) == 0)
  312. {
  313. return 0;
  314. }
  315. #endif
  316. #ifdef RT_USING_MODULE
  317. if (msh_exec_module(cmd, length) == 0)
  318. {
  319. return 0;
  320. }
  321. #endif
  322. #ifdef RT_USING_LWP
  323. if (_msh_exec_lwp(cmd, length) == 0)
  324. {
  325. return 0;
  326. }
  327. #endif
  328. #endif
  329. /* truncate the cmd at the first space. */
  330. {
  331. char *tcmd;
  332. tcmd = cmd;
  333. while (*tcmd != ' ' && *tcmd != '\0')
  334. {
  335. tcmd++;
  336. }
  337. *tcmd = '\0';
  338. }
  339. rt_kprintf("%s: command not found.\n", cmd);
  340. return -1;
  341. }
  342. static int str_common(const char *str1, const char *str2)
  343. {
  344. const char *str = str1;
  345. while ((*str != 0) && (*str2 != 0) && (*str == *str2))
  346. {
  347. str ++;
  348. str2 ++;
  349. }
  350. return (str - str1);
  351. }
  352. #ifdef RT_USING_DFS
  353. void msh_auto_complete_path(char *path)
  354. {
  355. DIR *dir = RT_NULL;
  356. struct dirent *dirent = RT_NULL;
  357. char *full_path, *ptr, *index;
  358. if (!path)
  359. return;
  360. full_path = (char *)rt_malloc(256);
  361. if (full_path == RT_NULL) return; /* out of memory */
  362. if (*path != '/')
  363. {
  364. getcwd(full_path, 256);
  365. if (full_path[rt_strlen(full_path) - 1] != '/')
  366. strcat(full_path, "/");
  367. }
  368. else *full_path = '\0';
  369. index = RT_NULL;
  370. ptr = path;
  371. for (;;)
  372. {
  373. if (*ptr == '/') index = ptr + 1;
  374. if (!*ptr) break;
  375. ptr ++;
  376. }
  377. if (index == RT_NULL) index = path;
  378. if (index != RT_NULL)
  379. {
  380. char *dest = index;
  381. /* fill the parent path */
  382. ptr = full_path;
  383. while (*ptr) ptr ++;
  384. for (index = path; index != dest;)
  385. *ptr++ = *index++;
  386. *ptr = '\0';
  387. dir = opendir(full_path);
  388. if (dir == RT_NULL) /* open directory failed! */
  389. {
  390. rt_free(full_path);
  391. return;
  392. }
  393. /* restore the index position */
  394. index = dest;
  395. }
  396. /* auto complete the file or directory name */
  397. if (*index == '\0') /* display all of files and directories */
  398. {
  399. for (;;)
  400. {
  401. dirent = readdir(dir);
  402. if (dirent == RT_NULL) break;
  403. rt_kprintf("%s\n", dirent->d_name);
  404. }
  405. }
  406. else
  407. {
  408. rt_size_t length, min_length;
  409. min_length = 0;
  410. for (;;)
  411. {
  412. dirent = readdir(dir);
  413. if (dirent == RT_NULL) break;
  414. /* matched the prefix string */
  415. if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
  416. {
  417. if (min_length == 0)
  418. {
  419. min_length = rt_strlen(dirent->d_name);
  420. /* save dirent name */
  421. strcpy(full_path, dirent->d_name);
  422. }
  423. length = str_common(dirent->d_name, full_path);
  424. if (length < min_length)
  425. {
  426. min_length = length;
  427. }
  428. }
  429. }
  430. if (min_length)
  431. {
  432. if (min_length < rt_strlen(full_path))
  433. {
  434. /* list the candidate */
  435. rewinddir(dir);
  436. for (;;)
  437. {
  438. dirent = readdir(dir);
  439. if (dirent == RT_NULL) break;
  440. if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
  441. rt_kprintf("%s\n", dirent->d_name);
  442. }
  443. }
  444. length = index - path;
  445. memcpy(index, full_path, min_length);
  446. path[length + min_length] = '\0';
  447. }
  448. }
  449. closedir(dir);
  450. rt_free(full_path);
  451. }
  452. #endif
  453. void msh_auto_complete(char *prefix)
  454. {
  455. int length, min_length;
  456. const char *name_ptr, *cmd_name;
  457. struct finsh_syscall *index;
  458. min_length = 0;
  459. name_ptr = RT_NULL;
  460. if (*prefix == '\0')
  461. {
  462. msh_help(0, RT_NULL);
  463. return;
  464. }
  465. #ifdef RT_USING_DFS
  466. /* check whether a spare in the command */
  467. {
  468. char *ptr;
  469. ptr = prefix + rt_strlen(prefix);
  470. while (ptr != prefix)
  471. {
  472. if (*ptr == ' ')
  473. {
  474. msh_auto_complete_path(ptr + 1);
  475. break;
  476. }
  477. ptr --;
  478. }
  479. #ifdef RT_USING_MODULE
  480. /* There is a chance that the user want to run the module directly. So
  481. * try to complete the file names. If the completed path is not a
  482. * module, the system won't crash anyway. */
  483. if (ptr == prefix)
  484. {
  485. msh_auto_complete_path(ptr);
  486. }
  487. #endif
  488. }
  489. #endif
  490. /* checks in internal command */
  491. {
  492. for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
  493. {
  494. /* skip finsh shell function */
  495. cmd_name = (const char *) index->name;
  496. if (strncmp(prefix, cmd_name, strlen(prefix)) == 0)
  497. {
  498. if (min_length == 0)
  499. {
  500. /* set name_ptr */
  501. name_ptr = cmd_name;
  502. /* set initial length */
  503. min_length = strlen(name_ptr);
  504. }
  505. length = str_common(name_ptr, cmd_name);
  506. if (length < min_length)
  507. min_length = length;
  508. rt_kprintf("%s\n", cmd_name);
  509. }
  510. }
  511. }
  512. /* auto complete string */
  513. if (name_ptr != NULL)
  514. {
  515. rt_strncpy(prefix, name_ptr, min_length);
  516. }
  517. return ;
  518. }
  519. #endif /* RT_USING_FINSH */