msh_file.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015-09-25 Bernard the first verion for FinSH
  9. * 2021-06-09 Meco Man implement tail command
  10. */
  11. #include <rtthread.h>
  12. #if defined(RT_USING_FINSH) && defined(DFS_USING_POSIX)
  13. #include <finsh.h>
  14. #include "msh.h"
  15. #include <dfs_file.h>
  16. #include <unistd.h>
  17. #include <sys/stat.h>
  18. #include <sys/statfs.h>
  19. static int msh_readline(int fd, char *line_buf, int size)
  20. {
  21. char ch;
  22. int index = 0;
  23. do
  24. {
  25. if (read(fd, &ch, 1) != 1)
  26. {
  27. /* nothing in this file */
  28. return 0;
  29. }
  30. }
  31. while (ch == '\n' || ch == '\r');
  32. /* set the first character */
  33. line_buf[index ++] = ch;
  34. while (index < size)
  35. {
  36. if (read(fd, &ch, 1) == 1)
  37. {
  38. if (ch == '\n' || ch == '\r')
  39. {
  40. line_buf[index] = '\0';
  41. break;
  42. }
  43. line_buf[index++] = ch;
  44. }
  45. else
  46. {
  47. line_buf[index] = '\0';
  48. break;
  49. }
  50. }
  51. return index;
  52. }
  53. int msh_exec_script(const char *cmd_line, int size)
  54. {
  55. int ret;
  56. int fd = -1;
  57. char *pg_name;
  58. int length, cmd_length = 0;
  59. if (size == 0) return -RT_ERROR;
  60. /* get the length of command0 */
  61. while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
  62. cmd_length ++;
  63. /* get name length */
  64. length = cmd_length + 32;
  65. /* allocate program name memory */
  66. pg_name = (char *) rt_malloc(length);
  67. if (pg_name == RT_NULL) return -RT_ENOMEM;
  68. /* copy command0 */
  69. rt_memcpy(pg_name, cmd_line, cmd_length);
  70. pg_name[cmd_length] = '\0';
  71. if (strstr(pg_name, ".sh") != RT_NULL || strstr(pg_name, ".SH") != RT_NULL)
  72. {
  73. /* try to open program */
  74. fd = open(pg_name, O_RDONLY, 0);
  75. /* search in /bin path */
  76. if (fd < 0)
  77. {
  78. rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
  79. fd = open(pg_name, O_RDONLY, 0);
  80. }
  81. }
  82. rt_free(pg_name);
  83. if (fd >= 0)
  84. {
  85. /* found script */
  86. char *line_buf;
  87. int length;
  88. line_buf = (char *) rt_malloc(RT_CONSOLEBUF_SIZE);
  89. if (line_buf == RT_NULL)
  90. {
  91. close(fd);
  92. return -RT_ENOMEM;
  93. }
  94. /* read line by line and then exec it */
  95. do
  96. {
  97. length = msh_readline(fd, line_buf, RT_CONSOLEBUF_SIZE);
  98. if (length > 0)
  99. {
  100. char ch = '\0';
  101. int index;
  102. for (index = 0; index < length; index ++)
  103. {
  104. ch = line_buf[index];
  105. if (ch == ' ' || ch == '\t') continue;
  106. else break;
  107. }
  108. if (ch != '#') /* not a comment */
  109. msh_exec(line_buf, length);
  110. }
  111. }
  112. while (length > 0);
  113. close(fd);
  114. rt_free(line_buf);
  115. ret = 0;
  116. }
  117. else
  118. {
  119. ret = -1;
  120. }
  121. return ret;
  122. }
  123. #ifdef DFS_USING_WORKDIR
  124. extern char working_directory[];
  125. #endif
  126. static int cmd_ls(int argc, char **argv)
  127. {
  128. extern void ls(const char *pathname);
  129. if (argc == 1)
  130. {
  131. #ifdef DFS_USING_WORKDIR
  132. ls(working_directory);
  133. #else
  134. ls("/");
  135. #endif
  136. }
  137. else
  138. {
  139. ls(argv[1]);
  140. }
  141. return 0;
  142. }
  143. MSH_CMD_EXPORT_ALIAS(cmd_ls, ls, List information about the FILEs.);
  144. static int cmd_cp(int argc, char **argv)
  145. {
  146. void copy(const char *src, const char *dst);
  147. if (argc != 3)
  148. {
  149. rt_kprintf("Usage: cp SOURCE DEST\n");
  150. rt_kprintf("Copy SOURCE to DEST.\n");
  151. }
  152. else
  153. {
  154. copy(argv[1], argv[2]);
  155. }
  156. return 0;
  157. }
  158. MSH_CMD_EXPORT_ALIAS(cmd_cp, cp, Copy SOURCE to DEST.);
  159. static int cmd_mv(int argc, char **argv)
  160. {
  161. if (argc != 3)
  162. {
  163. rt_kprintf("Usage: mv SOURCE DEST\n");
  164. rt_kprintf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n");
  165. }
  166. else
  167. {
  168. int fd;
  169. char *dest = RT_NULL;
  170. rt_kprintf("%s => %s\n", argv[1], argv[2]);
  171. fd = open(argv[2], O_DIRECTORY, 0);
  172. if (fd >= 0)
  173. {
  174. char *src;
  175. close(fd);
  176. /* it's a directory */
  177. dest = (char *)rt_malloc(DFS_PATH_MAX);
  178. if (dest == RT_NULL)
  179. {
  180. rt_kprintf("out of memory\n");
  181. return -RT_ENOMEM;
  182. }
  183. src = argv[1] + rt_strlen(argv[1]);
  184. while (src != argv[1])
  185. {
  186. if (*src == '/') break;
  187. src --;
  188. }
  189. rt_snprintf(dest, DFS_PATH_MAX - 1, "%s/%s", argv[2], src);
  190. }
  191. else
  192. {
  193. fd = open(argv[2], O_RDONLY, 0);
  194. if (fd >= 0)
  195. {
  196. close(fd);
  197. unlink(argv[2]);
  198. }
  199. dest = argv[2];
  200. }
  201. rename(argv[1], dest);
  202. if (dest != RT_NULL && dest != argv[2]) rt_free(dest);
  203. }
  204. return 0;
  205. }
  206. MSH_CMD_EXPORT_ALIAS(cmd_mv, mv, Rename SOURCE to DEST.);
  207. static int cmd_cat(int argc, char **argv)
  208. {
  209. int index;
  210. extern void cat(const char *filename);
  211. if (argc == 1)
  212. {
  213. rt_kprintf("Usage: cat [FILE]...\n");
  214. rt_kprintf("Concatenate FILE(s)\n");
  215. return 0;
  216. }
  217. for (index = 1; index < argc; index ++)
  218. {
  219. cat(argv[index]);
  220. }
  221. return 0;
  222. }
  223. MSH_CMD_EXPORT_ALIAS(cmd_cat, cat, Concatenate FILE(s));
  224. static void directory_delete_for_msh(const char *pathname, char f, char v)
  225. {
  226. DIR *dir = NULL;
  227. struct dirent *dirent = NULL;
  228. char *full_path;
  229. if (pathname == RT_NULL)
  230. return;
  231. full_path = (char *)rt_malloc(DFS_PATH_MAX);
  232. if (full_path == RT_NULL)
  233. return;
  234. dir = opendir(pathname);
  235. if (dir == RT_NULL)
  236. {
  237. if (f == 0)
  238. {
  239. rt_kprintf("cannot remove '%s'\n", pathname);
  240. }
  241. rt_free(full_path);
  242. return;
  243. }
  244. while (1)
  245. {
  246. dirent = readdir(dir);
  247. if (dirent == RT_NULL)
  248. break;
  249. if (rt_strcmp(".", dirent->d_name) != 0 &&
  250. rt_strcmp("..", dirent->d_name) != 0)
  251. {
  252. rt_sprintf(full_path, "%s/%s", pathname, dirent->d_name);
  253. if (dirent->d_type == DT_REG)
  254. {
  255. if (unlink(full_path) != 0)
  256. {
  257. if (f == 0)
  258. rt_kprintf("cannot remove '%s'\n", full_path);
  259. }
  260. else if (v)
  261. {
  262. rt_kprintf("removed '%s'\n", full_path);
  263. }
  264. }
  265. else if (dirent->d_type == DT_DIR)
  266. {
  267. directory_delete_for_msh(full_path, f, v);
  268. }
  269. }
  270. }
  271. closedir(dir);
  272. rt_free(full_path);
  273. if (unlink(pathname) != 0)
  274. {
  275. if (f == 0)
  276. rt_kprintf("cannot remove '%s'\n", pathname);
  277. }
  278. else if (v)
  279. {
  280. rt_kprintf("removed directory '%s'\n", pathname);
  281. }
  282. }
  283. static int cmd_rm(int argc, char **argv)
  284. {
  285. int index, n;
  286. char f = 0, r = 0, v = 0;
  287. if (argc == 1)
  288. {
  289. rt_kprintf("Usage: rm option(s) FILE...\n");
  290. rt_kprintf("Remove (unlink) the FILE(s).\n");
  291. return 0;
  292. }
  293. if (argv[1][0] == '-')
  294. {
  295. for (n = 0; argv[1][n]; n++)
  296. {
  297. switch (argv[1][n])
  298. {
  299. case 'f':
  300. f = 1;
  301. break;
  302. case 'r':
  303. r = 1;
  304. break;
  305. case 'v':
  306. v = 1;
  307. break;
  308. case '-':
  309. break;
  310. default:
  311. rt_kprintf("Error: Bad option: %c\n", argv[1][n]);
  312. return 0;
  313. }
  314. }
  315. argc -= 1;
  316. argv = argv + 1;
  317. }
  318. for (index = 1; index < argc; index ++)
  319. {
  320. struct stat s;
  321. if (stat(argv[index], &s) == 0)
  322. {
  323. if (s.st_mode & S_IFDIR)
  324. {
  325. if (r == 0)
  326. rt_kprintf("cannot remove '%s': Is a directory\n", argv[index]);
  327. else
  328. directory_delete_for_msh(argv[index], f, v);
  329. }
  330. else if (s.st_mode & S_IFREG)
  331. {
  332. if (unlink(argv[index]) != 0)
  333. {
  334. if (f == 0)
  335. rt_kprintf("cannot remove '%s'\n", argv[index]);
  336. }
  337. else if (v)
  338. {
  339. rt_kprintf("removed '%s'\n", argv[index]);
  340. }
  341. }
  342. }
  343. else if (f == 0)
  344. {
  345. rt_kprintf("cannot remove '%s': No such file or directory\n", argv[index]);
  346. }
  347. }
  348. return 0;
  349. }
  350. MSH_CMD_EXPORT_ALIAS(cmd_rm, rm, Remove(unlink) the FILE(s).);
  351. #ifdef DFS_USING_WORKDIR
  352. static int cmd_cd(int argc, char **argv)
  353. {
  354. if (argc == 1)
  355. {
  356. rt_kprintf("%s\n", working_directory);
  357. }
  358. else if (argc == 2)
  359. {
  360. if (chdir(argv[1]) != 0)
  361. {
  362. rt_kprintf("No such directory: %s\n", argv[1]);
  363. }
  364. }
  365. return 0;
  366. }
  367. MSH_CMD_EXPORT_ALIAS(cmd_cd, cd, Change the shell working directory.);
  368. static int cmd_pwd(int argc, char **argv)
  369. {
  370. rt_kprintf("%s\n", working_directory);
  371. return 0;
  372. }
  373. MSH_CMD_EXPORT_ALIAS(cmd_pwd, pwd, Print the name of the current working directory.);
  374. #endif
  375. static int cmd_mkdir(int argc, char **argv)
  376. {
  377. if (argc == 1)
  378. {
  379. rt_kprintf("Usage: mkdir [OPTION] DIRECTORY\n");
  380. rt_kprintf("Create the DIRECTORY, if they do not already exist.\n");
  381. }
  382. else
  383. {
  384. mkdir(argv[1], 0);
  385. }
  386. return 0;
  387. }
  388. MSH_CMD_EXPORT_ALIAS(cmd_mkdir, mkdir, Create the DIRECTORY.);
  389. static int cmd_mkfs(int argc, char **argv)
  390. {
  391. int result = 0;
  392. char *type = "elm"; /* use the default file system type as 'fatfs' */
  393. if (argc == 2)
  394. {
  395. result = dfs_mkfs(type, argv[1]);
  396. }
  397. else if (argc == 4)
  398. {
  399. if (strcmp(argv[1], "-t") == 0)
  400. {
  401. type = argv[2];
  402. result = dfs_mkfs(type, argv[3]);
  403. }
  404. }
  405. else
  406. {
  407. rt_kprintf("Usage: mkfs [-t type] device\n");
  408. return 0;
  409. }
  410. if (result != RT_EOK)
  411. {
  412. rt_kprintf("mkfs failed, result=%d\n", result);
  413. }
  414. return 0;
  415. }
  416. MSH_CMD_EXPORT_ALIAS(cmd_mkfs, mkfs, format disk with file system);
  417. extern struct dfs_filesystem filesystem_table[];
  418. static int cmd_mount(int argc, char **argv)
  419. {
  420. if (argc == 1)
  421. {
  422. struct dfs_filesystem *iter;
  423. /* display the mount history */
  424. rt_kprintf("filesystem device mountpoint\n");
  425. rt_kprintf("---------- ------ ----------\n");
  426. for (iter = &filesystem_table[0];
  427. iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
  428. {
  429. if ((iter != NULL) && (iter->path != NULL))
  430. {
  431. rt_kprintf("%-10s %-6s %-s\n",
  432. iter->ops->name, iter->dev_id->parent.name, iter->path);
  433. }
  434. }
  435. return 0;
  436. }
  437. else if (argc == 4)
  438. {
  439. char *device = argv[1];
  440. char *path = argv[2];
  441. char *fstype = argv[3];
  442. /* mount a filesystem to the specified directory */
  443. rt_kprintf("mount device %s(%s) onto %s ... ", device, fstype, path);
  444. if (dfs_mount(device, path, fstype, 0, 0) == 0)
  445. {
  446. rt_kprintf("succeed!\n");
  447. return 0;
  448. }
  449. else
  450. {
  451. rt_kprintf("failed!\n");
  452. return -1;
  453. }
  454. }
  455. else
  456. {
  457. rt_kprintf("Usage: mount <device> <mountpoint> <fstype>.\n");
  458. return -1;
  459. }
  460. }
  461. MSH_CMD_EXPORT_ALIAS(cmd_mount, mount, mount <device> <mountpoint> <fstype>);
  462. /* unmount the filesystem from the specified mountpoint */
  463. static int cmd_umount(int argc, char **argv)
  464. {
  465. char *path = argv[1];
  466. if (argc != 2)
  467. {
  468. rt_kprintf("Usage: unmount <mountpoint>.\n");
  469. return -1;
  470. }
  471. rt_kprintf("unmount %s ... ", path);
  472. if (dfs_unmount(path) < 0)
  473. {
  474. rt_kprintf("failed!\n");
  475. return -1;
  476. }
  477. else
  478. {
  479. rt_kprintf("succeed!\n");
  480. return 0;
  481. }
  482. }
  483. MSH_CMD_EXPORT_ALIAS(cmd_umount, umount, Unmount device from file system);
  484. extern int df(const char *path);
  485. static int cmd_df(int argc, char **argv)
  486. {
  487. if (argc != 2)
  488. {
  489. df("/");
  490. }
  491. else
  492. {
  493. if ((strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0))
  494. {
  495. rt_kprintf("df [path]\n");
  496. }
  497. else
  498. {
  499. df(argv[1]);
  500. }
  501. }
  502. return 0;
  503. }
  504. MSH_CMD_EXPORT_ALIAS(cmd_df, df, disk free);
  505. static int cmd_echo(int argc, char **argv)
  506. {
  507. if (argc == 2)
  508. {
  509. rt_kprintf("%s\n", argv[1]);
  510. }
  511. else if (argc == 3)
  512. {
  513. int fd;
  514. fd = open(argv[2], O_RDWR | O_APPEND | O_CREAT, 0);
  515. if (fd >= 0)
  516. {
  517. write(fd, argv[1], strlen(argv[1]));
  518. close(fd);
  519. }
  520. else
  521. {
  522. rt_kprintf("open file:%s failed!\n", argv[2]);
  523. }
  524. }
  525. else
  526. {
  527. rt_kprintf("Usage: echo \"string\" [filename]\n");
  528. }
  529. return 0;
  530. }
  531. MSH_CMD_EXPORT_ALIAS(cmd_echo, echo, echo string to file);
  532. static int cmd_tail(int argc, char **argv)
  533. {
  534. int fd;
  535. char c = RT_NULL;
  536. char *file_name = RT_NULL;
  537. rt_uint32_t total_lines = 0;
  538. rt_uint32_t target_line = 0;
  539. rt_uint32_t current_line = 0;
  540. rt_uint32_t required_lines = 0;
  541. rt_uint32_t start_line = 0;
  542. if (argc < 2)
  543. {
  544. rt_kprintf("Usage: tail [-n numbers] <filename>\n");
  545. return -1;
  546. }
  547. else if (argc == 2)
  548. {
  549. required_lines = 10; /* default: 10 lines from tail */
  550. file_name = argv[1];
  551. }
  552. else if (rt_strcmp(argv[1], "-n") == 0)
  553. {
  554. if (argv[2][0] != '+')
  555. {
  556. required_lines = atoi(argv[2]);
  557. }
  558. else
  559. {
  560. start_line = atoi(&argv[2][1]); /* eg: +100, to get the 100 */
  561. }
  562. file_name = argv[3];
  563. }
  564. else
  565. {
  566. rt_kprintf("Usage: tail [-n numbers] <filename>\n");
  567. return -1;
  568. }
  569. fd = open(file_name, O_RDONLY);
  570. if (fd < 0)
  571. {
  572. rt_kprintf("File doesn't exist\n");
  573. return -1;
  574. }
  575. while ((read(fd, &c, sizeof(char))) > 0)
  576. {
  577. if(total_lines == 0)
  578. {
  579. total_lines++;
  580. }
  581. if (c == '\n')
  582. {
  583. total_lines++;
  584. }
  585. }
  586. rt_kprintf("\nTotal Number of lines:%d\n", total_lines);
  587. if (start_line != 0)
  588. {
  589. if (total_lines >= start_line)
  590. {
  591. required_lines = total_lines - start_line + 1;
  592. }
  593. else
  594. {
  595. rt_kprintf("\nError:Required lines are more than total number of lines\n");
  596. close(fd);
  597. return -1;
  598. }
  599. }
  600. if (required_lines > total_lines)
  601. {
  602. rt_kprintf("\nError:Required lines are more than total number of lines\n");
  603. close(fd);
  604. return -1;
  605. }
  606. rt_kprintf("Required Number of lines:%d\n", required_lines);
  607. target_line = total_lines - required_lines;
  608. lseek(fd, 0, SEEK_SET); /* back to head */
  609. while ((read(fd, &c, sizeof(char))) > 0)
  610. {
  611. if (current_line >= target_line)
  612. {
  613. rt_kprintf("%c", c);
  614. }
  615. if (c == '\n')
  616. {
  617. current_line++;
  618. }
  619. }
  620. rt_kprintf("\n");
  621. close(fd);
  622. return 0;
  623. }
  624. MSH_CMD_EXPORT_ALIAS(cmd_tail, tail, print the last N - lines data of the given file);
  625. #endif /* defined(RT_USING_FINSH) && defined(DFS_USING_POSIX) */