msh.c 23 KB

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