msh.c 13 KB

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