msh.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. #include <errno.h>
  15. #ifdef RT_USING_FINSH
  16. #ifndef FINSH_ARG_MAX
  17. #define FINSH_ARG_MAX 8
  18. #endif /* FINSH_ARG_MAX */
  19. #include "msh.h"
  20. #include "shell.h"
  21. #ifdef DFS_USING_POSIX
  22. #include <dfs_file.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #endif /* DFS_USING_POSIX */
  26. #ifdef RT_USING_MODULE
  27. #include <dlmodule.h>
  28. #endif /* RT_USING_MODULE */
  29. typedef int (*cmd_function_t)(int argc, char **argv);
  30. int msh_help(int argc, char **argv)
  31. {
  32. rt_kprintf("RT-Thread shell commands:\n");
  33. {
  34. struct finsh_syscall *index;
  35. for (index = _syscall_table_begin;
  36. index < _syscall_table_end;
  37. FINSH_NEXT_SYSCALL(index))
  38. {
  39. #if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
  40. rt_kprintf("%-16s - %s\n", index->name, index->desc);
  41. #else
  42. rt_kprintf("%s ", index->name);
  43. #endif
  44. }
  45. }
  46. rt_kprintf("\n");
  47. return 0;
  48. }
  49. MSH_CMD_EXPORT_ALIAS(msh_help, help, RT-Thread shell help.);
  50. #ifdef MSH_USING_BUILT_IN_COMMANDS
  51. int cmd_ps(int argc, char **argv)
  52. {
  53. extern long list_thread(void);
  54. extern int list_module(void);
  55. #ifdef RT_USING_MODULE
  56. if ((argc == 2) && (strcmp(argv[1], "-m") == 0))
  57. list_module();
  58. else
  59. #endif
  60. list_thread();
  61. return 0;
  62. }
  63. MSH_CMD_EXPORT_ALIAS(cmd_ps, ps, List threads in the system.);
  64. #ifdef RT_USING_HEAP
  65. int cmd_free(int argc, char **argv)
  66. {
  67. #ifdef RT_USING_MEMHEAP_AS_HEAP
  68. extern void list_memheap(void);
  69. list_memheap();
  70. #else
  71. rt_size_t total = 0, used = 0, max_used = 0;
  72. rt_memory_info(&total, &used, &max_used);
  73. rt_kprintf("total : %d\n", total);
  74. rt_kprintf("used : %d\n", used);
  75. rt_kprintf("maximum : %d\n", max_used);
  76. rt_kprintf("available: %d\n", total - used);
  77. #endif
  78. return 0;
  79. }
  80. MSH_CMD_EXPORT_ALIAS(cmd_free, free, Show the memory usage in the system.);
  81. #endif /* RT_USING_HEAP */
  82. #endif /* MSH_USING_BUILT_IN_COMMANDS */
  83. static int msh_split(char *cmd, rt_size_t length, char *argv[FINSH_ARG_MAX])
  84. {
  85. char *ptr;
  86. rt_size_t position;
  87. rt_size_t argc;
  88. rt_size_t i;
  89. ptr = cmd;
  90. position = 0;
  91. argc = 0;
  92. while (position < length)
  93. {
  94. /* strip bank and tab */
  95. while ((*ptr == ' ' || *ptr == '\t') && position < length)
  96. {
  97. *ptr = '\0';
  98. ptr ++;
  99. position ++;
  100. }
  101. if (argc >= FINSH_ARG_MAX)
  102. {
  103. rt_kprintf("Too many args ! We only Use:\n");
  104. for (i = 0; i < argc; i++)
  105. {
  106. rt_kprintf("%s ", argv[i]);
  107. }
  108. rt_kprintf("\n");
  109. break;
  110. }
  111. if (position >= length) break;
  112. /* handle string */
  113. if (*ptr == '"')
  114. {
  115. ptr ++;
  116. position ++;
  117. argv[argc] = ptr;
  118. argc ++;
  119. /* skip this string */
  120. while (*ptr != '"' && position < length)
  121. {
  122. if (*ptr == '\\')
  123. {
  124. if (*(ptr + 1) == '"')
  125. {
  126. ptr ++;
  127. position ++;
  128. }
  129. }
  130. ptr ++;
  131. position ++;
  132. }
  133. if (position >= length) break;
  134. /* skip '"' */
  135. *ptr = '\0';
  136. ptr ++;
  137. position ++;
  138. }
  139. else
  140. {
  141. argv[argc] = ptr;
  142. argc ++;
  143. while ((*ptr != ' ' && *ptr != '\t') && position < length)
  144. {
  145. ptr ++;
  146. position ++;
  147. }
  148. if (position >= length) break;
  149. }
  150. }
  151. return argc;
  152. }
  153. static cmd_function_t msh_get_cmd(char *cmd, int size)
  154. {
  155. struct finsh_syscall *index;
  156. cmd_function_t cmd_func = RT_NULL;
  157. for (index = _syscall_table_begin;
  158. index < _syscall_table_end;
  159. FINSH_NEXT_SYSCALL(index))
  160. {
  161. if (strncmp(index->name, cmd, size) == 0 &&
  162. index->name[size] == '\0')
  163. {
  164. cmd_func = (cmd_function_t)index->func;
  165. break;
  166. }
  167. }
  168. return cmd_func;
  169. }
  170. #if defined(RT_USING_MODULE) && defined(DFS_USING_POSIX)
  171. /* Return 0 on module executed. Other value indicate error.
  172. */
  173. int msh_exec_module(const char *cmd_line, int size)
  174. {
  175. int ret;
  176. int fd = -1;
  177. char *pg_name;
  178. int length, cmd_length = 0;
  179. if (size == 0)
  180. return -RT_ERROR;
  181. /* get the length of command0 */
  182. while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
  183. cmd_length ++;
  184. /* get name length */
  185. length = cmd_length + 32;
  186. /* allocate program name memory */
  187. pg_name = (char *) rt_malloc(length + 3);
  188. if (pg_name == RT_NULL)
  189. return -RT_ENOMEM;
  190. /* copy command0 */
  191. rt_memcpy(pg_name, cmd_line, cmd_length);
  192. pg_name[cmd_length] = '\0';
  193. if (strstr(pg_name, ".mo") != RT_NULL || strstr(pg_name, ".MO") != RT_NULL)
  194. {
  195. /* try to open program */
  196. fd = open(pg_name, O_RDONLY, 0);
  197. /* search in /bin path */
  198. if (fd < 0)
  199. {
  200. rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
  201. fd = open(pg_name, O_RDONLY, 0);
  202. }
  203. }
  204. else
  205. {
  206. /* add .mo and open program */
  207. /* try to open program */
  208. strcat(pg_name, ".mo");
  209. fd = open(pg_name, O_RDONLY, 0);
  210. /* search in /bin path */
  211. if (fd < 0)
  212. {
  213. rt_snprintf(pg_name, length - 1, "/bin/%.*s.mo", cmd_length, cmd_line);
  214. fd = open(pg_name, O_RDONLY, 0);
  215. }
  216. }
  217. if (fd >= 0)
  218. {
  219. /* found program */
  220. close(fd);
  221. dlmodule_exec(pg_name, cmd_line, size);
  222. ret = 0;
  223. }
  224. else
  225. {
  226. ret = -1;
  227. }
  228. rt_free(pg_name);
  229. return ret;
  230. }
  231. #endif
  232. static int _msh_exec_cmd(char *cmd, rt_size_t length, int *retp)
  233. {
  234. int argc;
  235. rt_size_t cmd0_size = 0;
  236. cmd_function_t cmd_func;
  237. char *argv[FINSH_ARG_MAX];
  238. RT_ASSERT(cmd);
  239. RT_ASSERT(retp);
  240. /* find the size of first command */
  241. while (cmd0_size < length && (cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t'))
  242. cmd0_size ++;
  243. if (cmd0_size == 0)
  244. return -RT_ERROR;
  245. cmd_func = msh_get_cmd(cmd, cmd0_size);
  246. if (cmd_func == RT_NULL)
  247. return -RT_ERROR;
  248. /* split arguments */
  249. rt_memset(argv, 0x00, sizeof(argv));
  250. argc = msh_split(cmd, length, argv);
  251. if (argc == 0)
  252. return -RT_ERROR;
  253. /* exec this command */
  254. *retp = cmd_func(argc, argv);
  255. return 0;
  256. }
  257. #if defined(RT_USING_SMART) && defined(DFS_USING_POSIX)
  258. pid_t exec(char*, int, int, char**);
  259. /* check whether a file of the given path exits */
  260. static rt_bool_t _msh_lwp_cmd_exists(const char *path)
  261. {
  262. int fd = -1;
  263. fd = open(path, O_RDONLY, 0);
  264. if (fd < 0)
  265. {
  266. return RT_FALSE;
  267. }
  268. close(fd);
  269. return RT_TRUE;
  270. }
  271. /*
  272. * search for the file named "pg_name" or "pg_name.elf" at the given directory,
  273. * and return its path. return NULL when not found.
  274. */
  275. static char *_msh_exec_search_path(const char *path, const char *pg_name)
  276. {
  277. char *path_buffer = RT_NULL;
  278. ssize_t pg_len = strlen(pg_name);
  279. ssize_t base_len = 0;
  280. if (path)
  281. {
  282. base_len = strlen(path);
  283. }
  284. path_buffer = rt_malloc(base_len + pg_len + 6);
  285. if (path_buffer == RT_NULL)
  286. {
  287. return RT_NULL; /* no mem */
  288. }
  289. if (base_len > 0)
  290. {
  291. memcpy(path_buffer, path, base_len);
  292. path_buffer[base_len] = '/';
  293. path_buffer[base_len + 1] = '\0';
  294. }
  295. else
  296. {
  297. *path_buffer = '\0';
  298. }
  299. strcat(path_buffer, pg_name);
  300. if (_msh_lwp_cmd_exists(path_buffer))
  301. {
  302. return path_buffer;
  303. }
  304. if (strstr(path_buffer, ".elf") != NULL)
  305. {
  306. goto not_found;
  307. }
  308. strcat(path_buffer, ".elf");
  309. if (_msh_lwp_cmd_exists(path_buffer))
  310. {
  311. return path_buffer;
  312. }
  313. not_found:
  314. rt_free(path_buffer);
  315. return RT_NULL;
  316. }
  317. /*
  318. * search for the file named "pg_name" or "pg_name.elf" at each env path,
  319. * and return its path. return NULL when not found.
  320. */
  321. static char *_msh_exec_search_env(const char *pg_name)
  322. {
  323. char *result = RT_NULL;
  324. char *exec_path = RT_NULL;
  325. char *search_path = RT_NULL;
  326. char *pos = RT_NULL;
  327. char tmp_ch = '\0';
  328. if (!(exec_path = getenv("PATH")))
  329. {
  330. return RT_NULL;
  331. }
  332. /* exec path may need to be modified */
  333. if (!(exec_path = strdup(exec_path)))
  334. {
  335. return RT_NULL;
  336. }
  337. pos = exec_path;
  338. search_path = exec_path;
  339. /* walk through the entire exec_path until finding the program wanted
  340. or hitting its end */
  341. while (1)
  342. {
  343. /* env paths are seperated by ':' */
  344. if (*pos == ':' || *pos == '\0')
  345. {
  346. tmp_ch = *pos;
  347. *pos = '\0';
  348. result = _msh_exec_search_path(search_path, pg_name);
  349. if (result || tmp_ch == '\0')
  350. {
  351. goto ret;
  352. }
  353. pos++;
  354. search_path = pos;
  355. continue;
  356. }
  357. pos++;
  358. }
  359. /* release the duplicated exec_path and return */
  360. ret:
  361. rt_free(exec_path);
  362. return result;
  363. }
  364. int _msh_exec_lwp(int debug, char *cmd, rt_size_t length)
  365. {
  366. int argc;
  367. int cmd0_size = 0;
  368. char *argv[FINSH_ARG_MAX];
  369. char *pg_name;
  370. int ret;
  371. /* find the size of first command */
  372. while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
  373. cmd0_size ++;
  374. if (cmd0_size == 0)
  375. return -1;
  376. /* split arguments */
  377. rt_memset(argv, 0x00, sizeof(argv));
  378. argc = msh_split(cmd, length, argv);
  379. if (argc == 0)
  380. return -1;
  381. /* try to find program in working directory */
  382. pg_name = _msh_exec_search_path("", argv[0]);
  383. if (pg_name)
  384. {
  385. goto found_program;
  386. }
  387. /* only check these paths when the first argument doesn't contain path
  388. seperator */
  389. if (strstr(argv[0], "/"))
  390. {
  391. return -1;
  392. }
  393. /* try to find program in /bin */
  394. pg_name = _msh_exec_search_path("/bin", argv[0]);
  395. if (pg_name)
  396. {
  397. goto found_program;
  398. }
  399. /* try to find program in dirs registered to env path */
  400. pg_name = _msh_exec_search_env(argv[0]);
  401. if (pg_name)
  402. {
  403. goto found_program;
  404. }
  405. /* not found in anywhere */
  406. return -1;
  407. /* found program */
  408. found_program:
  409. ret = exec(pg_name, debug, argc, argv);
  410. rt_free(pg_name);
  411. return ret;
  412. }
  413. #endif
  414. int msh_exec(char *cmd, rt_size_t length)
  415. {
  416. int cmd_ret = 0;
  417. /* strim the beginning of command */
  418. while ((length > 0) && (*cmd == ' ' || *cmd == '\t'))
  419. {
  420. cmd++;
  421. length--;
  422. }
  423. if (length == 0)
  424. return 0;
  425. /* Exec sequence:
  426. * 1. built-in command
  427. * 2. module(if enabled)
  428. */
  429. if (_msh_exec_cmd(cmd, length, &cmd_ret) == 0)
  430. {
  431. return cmd_ret;
  432. }
  433. #ifdef DFS_USING_POSIX
  434. #ifdef DFS_USING_WORKDIR
  435. if (msh_exec_script(cmd, length) == 0)
  436. {
  437. return 0;
  438. }
  439. #endif
  440. #ifdef RT_USING_MODULE
  441. if (msh_exec_module(cmd, length) == 0)
  442. {
  443. return 0;
  444. }
  445. #endif /* RT_USING_MODULE */
  446. #ifdef RT_USING_SMART
  447. /* exec from msh_exec , debug = 0*/
  448. /* _msh_exec_lwp return is pid , <= 0 means failed */
  449. cmd_ret = _msh_exec_lwp(0, cmd, length);
  450. if (cmd_ret > 0)
  451. {
  452. return 0;
  453. }
  454. #endif /* RT_USING_SMART */
  455. #endif /* DFS_USING_POSIX */
  456. /* truncate the cmd at the first space. */
  457. {
  458. char *tcmd;
  459. tcmd = cmd;
  460. while (*tcmd != ' ' && *tcmd != '\0')
  461. {
  462. tcmd++;
  463. }
  464. *tcmd = '\0';
  465. }
  466. #ifdef RT_USING_SMART
  467. if (cmd_ret == -EACCES)
  468. {
  469. rt_kprintf("%s: Permission denied.\n", cmd);
  470. }
  471. else
  472. #endif
  473. {
  474. rt_kprintf("%s: command not found.\n", cmd);
  475. }
  476. return -1;
  477. }
  478. static int str_common(const char *str1, const char *str2)
  479. {
  480. const char *str = str1;
  481. while ((*str != 0) && (*str2 != 0) && (*str == *str2))
  482. {
  483. str ++;
  484. str2 ++;
  485. }
  486. return (str - str1);
  487. }
  488. #ifdef DFS_USING_POSIX
  489. void msh_auto_complete_path(char *path)
  490. {
  491. DIR *dir = RT_NULL;
  492. struct dirent *dirent = RT_NULL;
  493. char *full_path, *ptr, *index;
  494. if (!path)
  495. return;
  496. full_path = (char *)rt_malloc(256);
  497. if (full_path == RT_NULL) return; /* out of memory */
  498. if (*path != '/')
  499. {
  500. getcwd(full_path, 256);
  501. if (full_path[rt_strlen(full_path) - 1] != '/')
  502. strcat(full_path, "/");
  503. }
  504. else *full_path = '\0';
  505. index = RT_NULL;
  506. ptr = path;
  507. for (;;)
  508. {
  509. if (*ptr == '/') index = ptr + 1;
  510. if (!*ptr) break;
  511. ptr ++;
  512. }
  513. if (index == RT_NULL) index = path;
  514. if (index != RT_NULL)
  515. {
  516. char *dest = index;
  517. /* fill the parent path */
  518. ptr = full_path;
  519. while (*ptr) ptr ++;
  520. for (index = path; index != dest;)
  521. *ptr++ = *index++;
  522. *ptr = '\0';
  523. dir = opendir(full_path);
  524. if (dir == RT_NULL) /* open directory failed! */
  525. {
  526. rt_free(full_path);
  527. return;
  528. }
  529. /* restore the index position */
  530. index = dest;
  531. }
  532. /* auto complete the file or directory name */
  533. if (*index == '\0') /* display all of files and directories */
  534. {
  535. for (;;)
  536. {
  537. dirent = readdir(dir);
  538. if (dirent == RT_NULL) break;
  539. rt_kprintf("%s\n", dirent->d_name);
  540. }
  541. }
  542. else
  543. {
  544. int multi = 0;
  545. rt_size_t length, min_length;
  546. min_length = 0;
  547. for (;;)
  548. {
  549. dirent = readdir(dir);
  550. if (dirent == RT_NULL) break;
  551. /* matched the prefix string */
  552. if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
  553. {
  554. multi ++;
  555. if (min_length == 0)
  556. {
  557. min_length = rt_strlen(dirent->d_name);
  558. /* save dirent name */
  559. strcpy(full_path, dirent->d_name);
  560. }
  561. length = str_common(dirent->d_name, full_path);
  562. if (length < min_length)
  563. {
  564. min_length = length;
  565. }
  566. }
  567. }
  568. if (min_length)
  569. {
  570. if (multi > 1)
  571. {
  572. /* list the candidate */
  573. rewinddir(dir);
  574. for (;;)
  575. {
  576. dirent = readdir(dir);
  577. if (dirent == RT_NULL) break;
  578. if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
  579. rt_kprintf("%s\n", dirent->d_name);
  580. }
  581. }
  582. length = index - path;
  583. rt_memcpy(index, full_path, min_length);
  584. path[length + min_length] = '\0';
  585. /* try to locate folder */
  586. if (multi == 1)
  587. {
  588. struct stat buffer = {0};
  589. if ((stat(path, &buffer) == 0))
  590. {
  591. if (S_ISDIR(buffer.st_mode))
  592. {
  593. strcat(path, "/");
  594. }
  595. else if (S_ISLNK(buffer.st_mode))
  596. {
  597. DIR *dir = opendir(path);
  598. if (dir)
  599. {
  600. closedir(dir);
  601. strcat(path, "/");
  602. }
  603. }
  604. }
  605. }
  606. }
  607. }
  608. closedir(dir);
  609. rt_free(full_path);
  610. }
  611. #endif /* DFS_USING_POSIX */
  612. void msh_auto_complete(char *prefix)
  613. {
  614. int length, min_length;
  615. const char *name_ptr, *cmd_name;
  616. struct finsh_syscall *index;
  617. min_length = 0;
  618. name_ptr = RT_NULL;
  619. if (*prefix == '\0')
  620. {
  621. msh_help(0, RT_NULL);
  622. return;
  623. }
  624. #ifdef DFS_USING_POSIX
  625. /* check whether a spare in the command */
  626. {
  627. char *ptr;
  628. ptr = prefix + rt_strlen(prefix);
  629. while (ptr != prefix)
  630. {
  631. if (*ptr == ' ')
  632. {
  633. msh_auto_complete_path(ptr + 1);
  634. break;
  635. }
  636. ptr --;
  637. }
  638. #if defined(RT_USING_MODULE) || defined(RT_USING_SMART)
  639. /* There is a chance that the user want to run the module directly. So
  640. * try to complete the file names. If the completed path is not a
  641. * module, the system won't crash anyway. */
  642. if (ptr == prefix)
  643. {
  644. msh_auto_complete_path(ptr);
  645. }
  646. #endif /* RT_USING_MODULE */
  647. }
  648. #endif /* DFS_USING_POSIX */
  649. /* checks in internal command */
  650. {
  651. for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
  652. {
  653. /* skip finsh shell function */
  654. cmd_name = (const char *) index->name;
  655. if (strncmp(prefix, cmd_name, strlen(prefix)) == 0)
  656. {
  657. if (min_length == 0)
  658. {
  659. /* set name_ptr */
  660. name_ptr = cmd_name;
  661. /* set initial length */
  662. min_length = strlen(name_ptr);
  663. }
  664. length = str_common(name_ptr, cmd_name);
  665. if (length < min_length)
  666. min_length = length;
  667. rt_kprintf("%s\n", cmd_name);
  668. }
  669. }
  670. }
  671. /* auto complete string */
  672. if (name_ptr != NULL)
  673. {
  674. rt_strncpy(prefix, name_ptr, min_length);
  675. }
  676. return ;
  677. }
  678. #ifdef FINSH_USING_OPTION_COMPLETION
  679. static msh_cmd_opt_t *msh_get_cmd_opt(char *opt_str)
  680. {
  681. struct finsh_syscall *index;
  682. msh_cmd_opt_t *opt = RT_NULL;
  683. char *ptr;
  684. int len;
  685. if ((ptr = strchr(opt_str, ' ')))
  686. {
  687. len = ptr - opt_str;
  688. }
  689. else
  690. {
  691. len = strlen(opt_str);
  692. }
  693. for (index = _syscall_table_begin;
  694. index < _syscall_table_end;
  695. FINSH_NEXT_SYSCALL(index))
  696. {
  697. if (strncmp(index->name, opt_str, len) == 0 && index->name[len] == '\0')
  698. {
  699. opt = index->opt;
  700. break;
  701. }
  702. }
  703. return opt;
  704. }
  705. static int msh_get_argc(char *prefix, char **last_argv)
  706. {
  707. int argc = 0;
  708. char *ch = prefix;
  709. while (*ch)
  710. {
  711. if ((*ch == ' ') && *(ch + 1) && (*(ch + 1) != ' '))
  712. {
  713. *last_argv = ch + 1;
  714. argc++;
  715. }
  716. ch++;
  717. }
  718. return argc;
  719. }
  720. static void msh_opt_complete(char *opts_str, struct msh_cmd_opt *cmd_opt)
  721. {
  722. struct msh_cmd_opt *opt = cmd_opt;
  723. const char *name_ptr = RT_NULL;
  724. int min_length = 0, length, opts_str_len;
  725. opts_str_len = strlen(opts_str);
  726. for (opt = cmd_opt; opt->id; opt++)
  727. {
  728. if (!strncmp(opt->name, opts_str, opts_str_len))
  729. {
  730. if (min_length == 0)
  731. {
  732. /* set name_ptr */
  733. name_ptr = opt->name;
  734. /* set initial length */
  735. min_length = strlen(name_ptr);
  736. }
  737. length = str_common(name_ptr, opt->name);
  738. if (length < min_length)
  739. {
  740. min_length = length;
  741. }
  742. rt_kprintf("%s\n", opt->name);
  743. }
  744. }
  745. rt_kprintf("\n");
  746. if (name_ptr != NULL)
  747. {
  748. strncpy(opts_str, name_ptr, min_length);
  749. }
  750. }
  751. static void msh_opt_help(msh_cmd_opt_t *cmd_opt)
  752. {
  753. msh_cmd_opt_t *opt = cmd_opt;
  754. for (; opt->id; opt++)
  755. {
  756. rt_kprintf("%-16s - %s\n", opt->name, opt->des);
  757. }
  758. rt_kprintf("\n");
  759. }
  760. void msh_opt_auto_complete(char *prefix)
  761. {
  762. int argc;
  763. char *opt_str = RT_NULL;
  764. msh_cmd_opt_t *opt = RT_NULL;
  765. if ((argc = msh_get_argc(prefix, &opt_str)))
  766. {
  767. opt = msh_get_cmd_opt(prefix);
  768. }
  769. else if (!msh_get_cmd(prefix, strlen(prefix)) && (' ' == prefix[strlen(prefix) - 1]))
  770. {
  771. opt = msh_get_cmd_opt(prefix);
  772. }
  773. if (opt && opt->id)
  774. {
  775. switch (argc)
  776. {
  777. case 0:
  778. msh_opt_help(opt);
  779. break;
  780. case 1:
  781. msh_opt_complete(opt_str, opt);
  782. break;
  783. default:
  784. break;
  785. }
  786. }
  787. }
  788. int msh_cmd_opt_id_get(int argc, char *argv[], void *options)
  789. {
  790. msh_cmd_opt_t *opt = (msh_cmd_opt_t *) options;
  791. int opt_id;
  792. for (opt_id = 0; (argc >= 2) && opt && opt->id; opt++)
  793. {
  794. if (!strcmp(opt->name, argv[1]))
  795. {
  796. opt_id = opt->id;
  797. break;
  798. }
  799. }
  800. return opt_id;
  801. }
  802. void msh_opt_list_dump(void *options)
  803. {
  804. msh_cmd_opt_t *opt = (msh_cmd_opt_t *) options;
  805. for (; opt && opt->id; opt++)
  806. {
  807. rt_kprintf(" %-16s - %s\n", opt->name, opt->des);
  808. }
  809. }
  810. #endif /* FINSH_USING_OPTION_COMPLETION */
  811. #endif /* RT_USING_FINSH */