1
0

msh.c 13 KB

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