1
0

msh.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Copyright (c) 2006-2022, 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 + 3);
  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
  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_SMART) && defined(DFS_USING_POSIX)
  257. pid_t exec(char*, int, int, char**);
  258. /* check whether a file of the given path exits */
  259. static rt_bool_t _msh_lwp_cmd_exists(const char *path)
  260. {
  261. int fd = -1;
  262. fd = open(path, O_RDONLY, 0);
  263. if (fd < 0)
  264. {
  265. return RT_FALSE;
  266. }
  267. close(fd);
  268. return RT_TRUE;
  269. }
  270. /*
  271. * search for the file named "pg_name" or "pg_name.elf" at the given directory,
  272. * and return its path. return NULL when not found.
  273. */
  274. static char *_msh_exec_search_path(const char *path, const char *pg_name)
  275. {
  276. char *path_buffer = RT_NULL;
  277. ssize_t pg_len = strlen(pg_name);
  278. ssize_t base_len = 0;
  279. if (path)
  280. {
  281. base_len = strlen(path);
  282. }
  283. path_buffer = rt_malloc(base_len + pg_len + 6);
  284. if (path_buffer == RT_NULL)
  285. {
  286. return RT_NULL; /* no mem */
  287. }
  288. if (base_len > 0)
  289. {
  290. memcpy(path_buffer, path, base_len);
  291. path_buffer[base_len] = '/';
  292. path_buffer[base_len + 1] = '\0';
  293. }
  294. else
  295. {
  296. *path_buffer = '\0';
  297. }
  298. strcat(path_buffer, pg_name);
  299. if (_msh_lwp_cmd_exists(path_buffer))
  300. {
  301. return path_buffer;
  302. }
  303. if (strstr(path_buffer, ".elf") != NULL)
  304. {
  305. goto not_found;
  306. }
  307. strcat(path_buffer, ".elf");
  308. if (_msh_lwp_cmd_exists(path_buffer))
  309. {
  310. return path_buffer;
  311. }
  312. not_found:
  313. rt_free(path_buffer);
  314. return RT_NULL;
  315. }
  316. /*
  317. * search for the file named "pg_name" or "pg_name.elf" at each env path,
  318. * and return its path. return NULL when not found.
  319. */
  320. static char *_msh_exec_search_env(const char *pg_name)
  321. {
  322. char *result = RT_NULL;
  323. char *exec_path = RT_NULL;
  324. char *search_path = RT_NULL;
  325. char *pos = RT_NULL;
  326. char tmp_ch = '\0';
  327. if (!(exec_path = getenv("PATH")))
  328. {
  329. return RT_NULL;
  330. }
  331. /* exec path may need to be modified */
  332. if (!(exec_path = strdup(exec_path)))
  333. {
  334. return RT_NULL;
  335. }
  336. pos = exec_path;
  337. search_path = exec_path;
  338. /* walk through the entire exec_path until finding the program wanted
  339. or hitting its end */
  340. while (1)
  341. {
  342. /* env paths are seperated by ':' */
  343. if (*pos == ':' || *pos == '\0')
  344. {
  345. tmp_ch = *pos;
  346. *pos = '\0';
  347. result = _msh_exec_search_path(search_path, pg_name);
  348. if (result || tmp_ch == '\0')
  349. {
  350. goto ret;
  351. }
  352. pos++;
  353. search_path = pos;
  354. continue;
  355. }
  356. pos++;
  357. }
  358. /* release the duplicated exec_path and return */
  359. ret:
  360. rt_free(exec_path);
  361. return result;
  362. }
  363. int _msh_exec_lwp(int debug, char *cmd, rt_size_t length)
  364. {
  365. int argc;
  366. int cmd0_size = 0;
  367. char *argv[FINSH_ARG_MAX];
  368. char *pg_name;
  369. int ret;
  370. /* find the size of first command */
  371. while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
  372. cmd0_size ++;
  373. if (cmd0_size == 0)
  374. return -1;
  375. /* split arguments */
  376. rt_memset(argv, 0x00, sizeof(argv));
  377. argc = msh_split(cmd, length, argv);
  378. if (argc == 0)
  379. return -1;
  380. /* try to find program in working directory */
  381. pg_name = _msh_exec_search_path("", argv[0]);
  382. if (pg_name)
  383. {
  384. goto found_program;
  385. }
  386. /* only check these paths when the first argument doesn't contain path
  387. seperator */
  388. if (strstr(argv[0], "/"))
  389. {
  390. return -1;
  391. }
  392. /* try to find program in /bin */
  393. pg_name = _msh_exec_search_path("/bin", argv[0]);
  394. if (pg_name)
  395. {
  396. goto found_program;
  397. }
  398. /* try to find program in dirs registered to env path */
  399. pg_name = _msh_exec_search_env(argv[0]);
  400. if (pg_name)
  401. {
  402. goto found_program;
  403. }
  404. /* not found in anywhere */
  405. return -1;
  406. /* found program */
  407. found_program:
  408. ret = exec(pg_name, debug, argc, argv);
  409. rt_free(pg_name);
  410. return ret;
  411. }
  412. #endif
  413. int msh_exec(char *cmd, rt_size_t length)
  414. {
  415. int cmd_ret;
  416. /* strim the beginning of command */
  417. while ((length > 0) && (*cmd == ' ' || *cmd == '\t'))
  418. {
  419. cmd++;
  420. length--;
  421. }
  422. if (length == 0)
  423. return 0;
  424. /* Exec sequence:
  425. * 1. built-in command
  426. * 2. module(if enabled)
  427. */
  428. if (_msh_exec_cmd(cmd, length, &cmd_ret) == 0)
  429. {
  430. return cmd_ret;
  431. }
  432. #ifdef DFS_USING_POSIX
  433. #ifdef DFS_USING_WORKDIR
  434. if (msh_exec_script(cmd, length) == 0)
  435. {
  436. return 0;
  437. }
  438. #endif
  439. #ifdef RT_USING_MODULE
  440. if (msh_exec_module(cmd, length) == 0)
  441. {
  442. return 0;
  443. }
  444. #endif /* RT_USING_MODULE */
  445. #ifdef RT_USING_SMART
  446. /* exec from msh_exec , debug = 0*/
  447. /* _msh_exec_lwp return is pid , <= 0 means failed */
  448. if (_msh_exec_lwp(0, cmd, length) > 0)
  449. {
  450. return 0;
  451. }
  452. #endif /* RT_USING_SMART */
  453. #endif /* DFS_USING_POSIX */
  454. /* truncate the cmd at the first space. */
  455. {
  456. char *tcmd;
  457. tcmd = cmd;
  458. while (*tcmd != ' ' && *tcmd != '\0')
  459. {
  460. tcmd++;
  461. }
  462. *tcmd = '\0';
  463. }
  464. rt_kprintf("%s: command not found.\n", cmd);
  465. return -1;
  466. }
  467. static int str_common(const char *str1, const char *str2)
  468. {
  469. const char *str = str1;
  470. while ((*str != 0) && (*str2 != 0) && (*str == *str2))
  471. {
  472. str ++;
  473. str2 ++;
  474. }
  475. return (str - str1);
  476. }
  477. #ifdef DFS_USING_POSIX
  478. void msh_auto_complete_path(char *path)
  479. {
  480. DIR *dir = RT_NULL;
  481. struct dirent *dirent = RT_NULL;
  482. char *full_path, *ptr, *index;
  483. if (!path)
  484. return;
  485. full_path = (char *)rt_malloc(256);
  486. if (full_path == RT_NULL) return; /* out of memory */
  487. if (*path != '/')
  488. {
  489. getcwd(full_path, 256);
  490. if (full_path[rt_strlen(full_path) - 1] != '/')
  491. strcat(full_path, "/");
  492. }
  493. else *full_path = '\0';
  494. index = RT_NULL;
  495. ptr = path;
  496. for (;;)
  497. {
  498. if (*ptr == '/') index = ptr + 1;
  499. if (!*ptr) break;
  500. ptr ++;
  501. }
  502. if (index == RT_NULL) index = path;
  503. if (index != RT_NULL)
  504. {
  505. char *dest = index;
  506. /* fill the parent path */
  507. ptr = full_path;
  508. while (*ptr) ptr ++;
  509. for (index = path; index != dest;)
  510. *ptr++ = *index++;
  511. *ptr = '\0';
  512. dir = opendir(full_path);
  513. if (dir == RT_NULL) /* open directory failed! */
  514. {
  515. rt_free(full_path);
  516. return;
  517. }
  518. /* restore the index position */
  519. index = dest;
  520. }
  521. /* auto complete the file or directory name */
  522. if (*index == '\0') /* display all of files and directories */
  523. {
  524. for (;;)
  525. {
  526. dirent = readdir(dir);
  527. if (dirent == RT_NULL) break;
  528. rt_kprintf("%s\n", dirent->d_name);
  529. }
  530. }
  531. else
  532. {
  533. int multi = 0;
  534. rt_size_t length, min_length;
  535. min_length = 0;
  536. for (;;)
  537. {
  538. dirent = readdir(dir);
  539. if (dirent == RT_NULL) break;
  540. /* matched the prefix string */
  541. if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
  542. {
  543. multi ++;
  544. if (min_length == 0)
  545. {
  546. min_length = rt_strlen(dirent->d_name);
  547. /* save dirent name */
  548. strcpy(full_path, dirent->d_name);
  549. }
  550. length = str_common(dirent->d_name, full_path);
  551. if (length < min_length)
  552. {
  553. min_length = length;
  554. }
  555. }
  556. }
  557. if (min_length)
  558. {
  559. if (multi > 1)
  560. {
  561. /* list the candidate */
  562. rewinddir(dir);
  563. for (;;)
  564. {
  565. dirent = readdir(dir);
  566. if (dirent == RT_NULL) break;
  567. if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
  568. rt_kprintf("%s\n", dirent->d_name);
  569. }
  570. }
  571. length = index - path;
  572. rt_memcpy(index, full_path, min_length);
  573. path[length + min_length] = '\0';
  574. /* try to locate folder */
  575. if (multi == 1)
  576. {
  577. struct stat buffer = {0};
  578. if ((stat(path, &buffer) == 0) && (S_ISDIR(buffer.st_mode)))
  579. {
  580. strcat(path, "/");
  581. }
  582. }
  583. }
  584. }
  585. closedir(dir);
  586. rt_free(full_path);
  587. }
  588. #endif /* DFS_USING_POSIX */
  589. void msh_auto_complete(char *prefix)
  590. {
  591. int length, min_length;
  592. const char *name_ptr, *cmd_name;
  593. struct finsh_syscall *index;
  594. min_length = 0;
  595. name_ptr = RT_NULL;
  596. if (*prefix == '\0')
  597. {
  598. msh_help(0, RT_NULL);
  599. return;
  600. }
  601. #ifdef DFS_USING_POSIX
  602. /* check whether a spare in the command */
  603. {
  604. char *ptr;
  605. ptr = prefix + rt_strlen(prefix);
  606. while (ptr != prefix)
  607. {
  608. if (*ptr == ' ')
  609. {
  610. msh_auto_complete_path(ptr + 1);
  611. break;
  612. }
  613. ptr --;
  614. }
  615. #if defined(RT_USING_MODULE) || defined(RT_USING_SMART)
  616. /* There is a chance that the user want to run the module directly. So
  617. * try to complete the file names. If the completed path is not a
  618. * module, the system won't crash anyway. */
  619. if (ptr == prefix)
  620. {
  621. msh_auto_complete_path(ptr);
  622. }
  623. #endif /* RT_USING_MODULE */
  624. }
  625. #endif /* DFS_USING_POSIX */
  626. /* checks in internal command */
  627. {
  628. for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
  629. {
  630. /* skip finsh shell function */
  631. cmd_name = (const char *) index->name;
  632. if (strncmp(prefix, cmd_name, strlen(prefix)) == 0)
  633. {
  634. if (min_length == 0)
  635. {
  636. /* set name_ptr */
  637. name_ptr = cmd_name;
  638. /* set initial length */
  639. min_length = strlen(name_ptr);
  640. }
  641. length = str_common(name_ptr, cmd_name);
  642. if (length < min_length)
  643. min_length = length;
  644. rt_kprintf("%s\n", cmd_name);
  645. }
  646. }
  647. }
  648. /* auto complete string */
  649. if (name_ptr != NULL)
  650. {
  651. rt_strncpy(prefix, name_ptr, min_length);
  652. }
  653. return ;
  654. }
  655. #endif /* RT_USING_FINSH */