msh.c 23 KB

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