msh.c 22 KB

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