Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

msh.c 14KB

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