msh.c 12 KB

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