msh.c 14 KB

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