msh.c 14 KB

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