cmd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. #include "cmd.h"
  2. #include "cmd_help.h"
  3. #include "cmd_test.h"
  4. #include <dirent.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <libsystem/syscall.h>
  8. #include <signal.h>
  9. #include <stddef.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/stat.h>
  14. #include <sys/wait.h>
  15. #include <unistd.h>
  16. // 当前工作目录(在main_loop中初始化)
  17. char *shell_current_path = NULL;
  18. /**
  19. * @brief shell 内建函数的主命令与处理函数的映射表
  20. *
  21. */
  22. struct built_in_cmd_t shell_cmds[] = {
  23. {"cd", shell_cmd_cd}, {"cat", shell_cmd_cat}, {"exec", shell_cmd_exec}, {"ls", shell_cmd_ls},
  24. {"mkdir", shell_cmd_mkdir}, {"pwd", shell_cmd_pwd}, {"rm", shell_cmd_rm}, {"rmdir", shell_cmd_rmdir},
  25. {"reboot", shell_cmd_reboot}, {"touch", shell_cmd_touch}, {"about", shell_cmd_about}, {"free", shell_cmd_free},
  26. {"help", shell_help}, {"pipe", shell_pipe_test}, {"kill", shell_cmd_kill},
  27. };
  28. // 总共的内建命令数量
  29. const static int total_built_in_cmd_num = sizeof(shell_cmds) / sizeof(struct built_in_cmd_t);
  30. /**
  31. * @brief 将cwd与文件名进行拼接,得到最终的文件绝对路径
  32. *
  33. * @param filename 文件名
  34. * @param result_path_len 结果字符串的大小
  35. * @return char* 结果字符串
  36. */
  37. static char *get_target_filepath(const char *filename, int *result_path_len)
  38. {
  39. char *file_path = NULL;
  40. if (filename[0] != '/')
  41. {
  42. int cwd_len = strlen(shell_current_path);
  43. // 计算文件完整路径的长度
  44. *result_path_len = cwd_len + strlen(filename);
  45. file_path = (char *)malloc(*result_path_len + 2);
  46. memset(file_path, 0, *result_path_len + 2);
  47. strncpy(file_path, shell_current_path, cwd_len);
  48. // 在文件路径中加入斜杠
  49. if (cwd_len > 1)
  50. file_path[cwd_len] = '/';
  51. // 拼接完整路径
  52. strcat(file_path, filename);
  53. }
  54. else
  55. {
  56. *result_path_len = strlen(filename);
  57. file_path = (char *)malloc(*result_path_len + 2);
  58. memset(file_path, 0, *result_path_len + 2);
  59. strncpy(file_path, filename, *result_path_len);
  60. if (filename[(*result_path_len) - 1] != '/')
  61. file_path[*result_path_len] = '/';
  62. }
  63. return file_path;
  64. }
  65. /**
  66. * @brief 寻找对应的主命令编号
  67. *
  68. * @param main_cmd 主命令
  69. * @return int 成功:主命令编号
  70. * 失败: -1
  71. */
  72. int shell_find_cmd(char *main_cmd)
  73. {
  74. for (int i = 0; i < total_built_in_cmd_num; ++i)
  75. {
  76. if (strcmp(main_cmd, shell_cmds[i].name) == 0) // 找到对应的命令号
  77. return i;
  78. }
  79. // 找不到该命令
  80. return -1;
  81. }
  82. /**
  83. * @brief 运行shell内建的命令
  84. *
  85. * @param index 主命令编号
  86. * @param argc 参数数量
  87. * @param argv 参数列表
  88. */
  89. void shell_run_built_in_command(int index, int argc, char **argv)
  90. {
  91. if (index >= total_built_in_cmd_num)
  92. return;
  93. // printf("run built-in command : %s\n", shell_cmds[index].name);
  94. shell_cmds[index].func(argc, argv);
  95. }
  96. /**
  97. * @brief cd命令:进入文件夹
  98. *
  99. * @param argc
  100. * @param argv
  101. * @return int
  102. */
  103. int shell_cmd_cd(int argc, char **argv)
  104. {
  105. int current_dir_len = strlen(shell_current_path);
  106. if (argc < 2)
  107. {
  108. shell_help_cd();
  109. goto done;
  110. }
  111. // 进入当前文件夹
  112. if (!strcmp(".", argv[1]))
  113. goto done;
  114. // 进入父目录
  115. if (!strcmp("..", argv[1]))
  116. {
  117. // 当前已经是根目录
  118. if (!strcmp("/", shell_current_path))
  119. goto done;
  120. // 返回到父目录
  121. int index = current_dir_len - 1;
  122. for (; index > 1; --index)
  123. {
  124. if (shell_current_path[index] == '/')
  125. break;
  126. }
  127. shell_current_path[index] = '\0';
  128. // printf("switch to \" %s \"\n", shell_current_path);
  129. goto done;
  130. }
  131. int dest_len = strlen(argv[1]);
  132. // 路径过长
  133. if (dest_len >= SHELL_CWD_MAX_SIZE - 1)
  134. {
  135. printf("ERROR: Path too long!\n");
  136. goto fail;
  137. }
  138. if (argv[1][0] == '/')
  139. {
  140. // ======进入绝对路径=====
  141. int ec = chdir(argv[1]);
  142. if (ec == -1)
  143. ec = errno;
  144. if (ec == 0)
  145. {
  146. // 获取新的路径字符串
  147. char *new_path = (char *)malloc(dest_len + 2);
  148. memset(new_path, 0, dest_len + 2);
  149. strncpy(new_path, argv[1], dest_len);
  150. // 释放原有的路径字符串的内存空间
  151. free(shell_current_path);
  152. shell_current_path = new_path;
  153. shell_current_path[dest_len] = '\0';
  154. return 0;
  155. }
  156. else
  157. goto fail;
  158. ; // 出错则直接忽略
  159. }
  160. else // ======进入相对路径=====
  161. {
  162. int dest_offset = 0;
  163. if (dest_len > 2)
  164. {
  165. if (argv[1][0] == '.' && argv[1][1] == '/') // 相对路径
  166. dest_offset = 2;
  167. }
  168. int new_len = current_dir_len + dest_len - dest_offset;
  169. if (new_len >= SHELL_CWD_MAX_SIZE - 1)
  170. {
  171. printf("ERROR: Path too long!\n");
  172. goto fail;
  173. }
  174. // 拼接出新的字符串
  175. char *new_path = (char *)malloc(new_len + 2);
  176. memset(new_path, 0, sizeof(new_path));
  177. strncpy(new_path, shell_current_path, current_dir_len);
  178. if (current_dir_len > 1)
  179. new_path[current_dir_len] = '/';
  180. strcat(new_path, argv[1] + dest_offset);
  181. int x = chdir(new_path);
  182. if (x == 0) // 成功切换目录
  183. {
  184. free(shell_current_path);
  185. // printf("new_path=%s, newlen= %d\n", new_path, new_len);
  186. new_path[new_len + 1] = '\0';
  187. shell_current_path = new_path;
  188. goto done;
  189. }
  190. else
  191. {
  192. free(new_path);
  193. printf("ERROR: Cannot switch to directory: %s\n", new_path);
  194. goto fail;
  195. }
  196. }
  197. fail:;
  198. done:;
  199. // 释放参数所占的内存
  200. free(argv);
  201. return 0;
  202. }
  203. /**
  204. * @brief 查看文件夹下的文件列表
  205. *
  206. * @param argc
  207. * @param argv
  208. * @return int
  209. */
  210. int shell_cmd_ls(int argc, char **argv)
  211. {
  212. struct DIR *dir = opendir(shell_current_path);
  213. if (dir == NULL)
  214. return -1;
  215. struct dirent *buf = NULL;
  216. // printf("dir=%#018lx\n", dir);
  217. while (1)
  218. {
  219. buf = readdir(dir);
  220. if (buf == NULL)
  221. break;
  222. int color = COLOR_WHITE;
  223. if (buf->d_type == DT_DIR)
  224. color = COLOR_YELLOW;
  225. else if (buf->d_type == DT_REG)
  226. color = COLOR_INDIGO;
  227. else if (buf->d_type == DT_BLK || buf->d_type == DT_CHR)
  228. color = COLOR_GREEN;
  229. char output_buf[256] = {0};
  230. sprintf(output_buf, "%s ", buf->d_name);
  231. put_string(output_buf, color, COLOR_BLACK);
  232. }
  233. printf("\n");
  234. closedir(dir);
  235. if (argv != NULL)
  236. free(argv);
  237. return 0;
  238. }
  239. /**
  240. * @brief 显示当前工作目录的命令
  241. *
  242. * @param argc
  243. * @param argv
  244. * @return int
  245. */
  246. int shell_cmd_pwd(int argc, char **argv)
  247. {
  248. if (shell_current_path)
  249. printf("%s\n", shell_current_path);
  250. if (argv != NULL)
  251. free(argv);
  252. return 0;
  253. }
  254. /**
  255. * @brief 查看文件内容的命令
  256. *
  257. * @param argc
  258. * @param argv
  259. * @return int
  260. */
  261. int shell_cmd_cat(int argc, char **argv)
  262. {
  263. int path_len = 0;
  264. char *file_path = get_target_filepath(argv[1], &path_len);
  265. // 打开文件
  266. int fd = open(file_path, 0);
  267. if (fd <= 0)
  268. {
  269. printf("ERROR: Cannot open file: %s, fd=%d\n", file_path, fd);
  270. return -1;
  271. }
  272. // 获取文件总大小
  273. int file_size = lseek(fd, 0, SEEK_END);
  274. // 将文件指针切换回文件起始位置
  275. lseek(fd, 0, SEEK_SET);
  276. char *buf = (char *)malloc(512);
  277. while (file_size > 0)
  278. {
  279. memset(buf, 0, 512);
  280. int l = read(fd, buf, 511);
  281. if (l < 0)
  282. {
  283. printf("ERROR: Cannot read file: %s\n", file_path);
  284. return -1;
  285. }
  286. buf[l] = '\0';
  287. file_size -= l;
  288. printf("%s", buf);
  289. }
  290. close(fd);
  291. free(buf);
  292. free(file_path);
  293. if (argv != NULL)
  294. free(argv);
  295. return 0;
  296. }
  297. /**
  298. * @brief 创建空文件的命令
  299. *
  300. * @param argc
  301. * @param argv
  302. * @return int
  303. */
  304. int shell_cmd_touch(int argc, char **argv)
  305. {
  306. int path_len = 0;
  307. char *file_path;
  308. bool alloc_full_path = false;
  309. if (argv[1][0] == '/')
  310. file_path = argv[1];
  311. else
  312. {
  313. file_path = get_target_filepath(argv[1], &path_len);
  314. alloc_full_path = true;
  315. }
  316. // 打开文件
  317. int fd = open(file_path, O_CREAT);
  318. switch (fd)
  319. {
  320. case -ENOENT:
  321. put_string("Parent dir not exists.\n", COLOR_RED, COLOR_BLACK);
  322. break;
  323. default:
  324. break;
  325. }
  326. close(fd);
  327. if (argv != NULL)
  328. free(argv);
  329. if (alloc_full_path)
  330. free(file_path);
  331. return 0;
  332. }
  333. /**
  334. * @brief 创建文件夹的命令
  335. *
  336. * @param argc
  337. * @param argv
  338. * @return int
  339. */
  340. int shell_cmd_mkdir(int argc, char **argv)
  341. {
  342. int result_path_len = -1;
  343. char *full_path = NULL;
  344. bool alloc_full_path = false;
  345. if (argv[1][0] == '/')
  346. full_path = argv[1];
  347. else
  348. {
  349. full_path = get_target_filepath(argv[1], &result_path_len);
  350. alloc_full_path = true;
  351. }
  352. // printf("mkdir: full_path = %s\n", full_path);
  353. int retval = mkdir(full_path, 0);
  354. if (argv != NULL)
  355. free(argv);
  356. if (alloc_full_path)
  357. free(full_path);
  358. return retval;
  359. }
  360. /**
  361. * @brief 删除文件夹的命令
  362. *
  363. * @param argc
  364. * @param argv
  365. * @return int
  366. */
  367. int shell_cmd_rmdir(int argc, char **argv)
  368. {
  369. char *full_path = NULL;
  370. int result_path_len = -1;
  371. bool alloc_full_path = false;
  372. if (argv[1][0] == '/')
  373. full_path = argv[1];
  374. else
  375. {
  376. full_path = get_target_filepath(argv[1], &result_path_len);
  377. alloc_full_path = true;
  378. }
  379. int retval = rmdir(full_path);
  380. if (retval != 0)
  381. printf("Failed to remove %s, retval=%d\n", full_path, retval);
  382. // printf("rmdir: path=%s, retval=%d\n", full_path, retval);
  383. if (argv != NULL)
  384. free(argv);
  385. if (alloc_full_path)
  386. free(full_path);
  387. return retval;
  388. }
  389. /**
  390. * @brief 删除文件的命令
  391. *
  392. * @param argc
  393. * @param argv
  394. * @return int
  395. */
  396. int shell_cmd_rm(int argc, char **argv)
  397. {
  398. char *full_path = NULL;
  399. int result_path_len = -1;
  400. int retval = 0;
  401. bool alloc_full_path = false;
  402. if (argv[1][0] == '/')
  403. full_path = argv[1];
  404. else
  405. {
  406. full_path = get_target_filepath(argv[1], &result_path_len);
  407. alloc_full_path = true;
  408. }
  409. retval = rm(full_path);
  410. // printf("rmdir: path=%s, retval=%d\n", full_path, retval);
  411. if (retval != 0)
  412. printf("Failed to remove %s, retval=%d\n", full_path, retval);
  413. if (alloc_full_path)
  414. free(full_path);
  415. if (argv != NULL)
  416. free(argv);
  417. return retval;
  418. }
  419. /**
  420. * @brief 执行新的程序的命令
  421. *
  422. * @param argc
  423. * @param argv
  424. * @return int
  425. */
  426. int shell_cmd_exec(int argc, char **argv)
  427. {
  428. pid_t pid = fork();
  429. int retval = 0;
  430. // printf(" pid=%d \n",pid);
  431. if (pid == 0)
  432. {
  433. // 子进程
  434. int path_len = 0;
  435. char *file_path = get_target_filepath(argv[1], &path_len);
  436. // printf("before execv, path=%s, argc=%d\n", file_path, argc);
  437. execv(file_path, argv);
  438. // printf("after execv, path=%s, argc=%d\n", file_path, argc);
  439. free(argv);
  440. free(file_path);
  441. exit(-1);
  442. }
  443. else
  444. {
  445. // 如果不指定后台运行,则等待退出
  446. if (strcmp(argv[argc - 1], "&") != 0)
  447. waitpid(pid, &retval, 0);
  448. else
  449. printf("[1] %d\n", pid); // 输出子进程的pid
  450. free(argv);
  451. }
  452. }
  453. int shell_cmd_about(int argc, char **argv)
  454. {
  455. if (argv != NULL)
  456. free(argv);
  457. int aac = 0;
  458. char **aav;
  459. unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0};
  460. strcpy(input_buffer, "exec /bin/about.elf\0");
  461. parse_command(input_buffer, &aac, &aav);
  462. return shell_cmd_exec(aac, aav);
  463. }
  464. int shell_cmd_kill(int argc, char **argv)
  465. {
  466. int retval = 0;
  467. if (argc < 2)
  468. {
  469. printf("Usage: Kill <pid>\n");
  470. retval = -EINVAL;
  471. goto out;
  472. }
  473. retval = kill(atoi(argv[1]), SIGKILL);
  474. out:;
  475. free(argv);
  476. return retval;
  477. }
  478. /**
  479. * @brief 重启命令
  480. *
  481. * @param argc
  482. * @param argv
  483. * @return int
  484. */
  485. int shell_cmd_reboot(int argc, char **argv)
  486. {
  487. return syscall_invoke(SYS_REBOOT, 0, 0, 0, 0, 0, 0, 0, 0);
  488. }
  489. int shell_cmd_free(int argc, char **argv)
  490. {
  491. int retval = 0;
  492. if (argc == 2 && strcmp("-m", argv[1]) != 0)
  493. {
  494. retval = -EINVAL;
  495. printf("Invalid argument: %s\n", argv[1]);
  496. goto done;
  497. }
  498. struct mstat_t mst = {0};
  499. retval = mstat(&mst);
  500. if (retval != 0)
  501. {
  502. printf("Failed: retval=%d", retval);
  503. goto done;
  504. }
  505. printf("\ttotal\tused\tfree\tshared\tcache\tavailable\n");
  506. printf("Mem:\t");
  507. if (argc == 1) // 按照kb显示
  508. {
  509. printf("%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t\n", mst.total >> 10, mst.used >> 10, mst.free >> 10, mst.shared >> 10,
  510. mst.cache_used >> 10, mst.available >> 10);
  511. }
  512. else // 按照MB显示
  513. {
  514. printf("%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t\n", mst.total >> 20, mst.used >> 20, mst.free >> 20, mst.shared >> 20,
  515. mst.cache_used >> 20, mst.available >> 20);
  516. }
  517. done:;
  518. if (argv != NULL)
  519. free(argv);
  520. return retval;
  521. }
  522. /**
  523. * @brief 解析shell命令
  524. *
  525. * @param buf 输入缓冲区
  526. * @param argc 返回值:参数数量
  527. * @param argv 返回值:参数列表
  528. * @return int 主命令的编号,小于零为无效命令
  529. */
  530. int parse_command(char *buf, int *argc, char ***argv)
  531. {
  532. // printf("parse command\n");
  533. int index = 0; // 当前访问的是buf的第几位
  534. // 去除命令前导的空格
  535. while (index < INPUT_BUFFER_SIZE && buf[index] == ' ')
  536. ++index;
  537. // 如果去除前导空格后第一项为0x00,则归为空命令
  538. if(!buf[index])
  539. return -1;
  540. // 计算参数数量
  541. for (int i = index; i < (INPUT_BUFFER_SIZE - 1); ++i)
  542. {
  543. // 到达了字符串末尾
  544. if (!buf[i])
  545. break;
  546. if (buf[i] != ' ' && (buf[i + 1] == ' ' || buf[i + 1] == '\0'))
  547. ++(*argc);
  548. }
  549. // printf("\nargc=%d\n", *argc);
  550. // 为指向每个指令的指针分配空间
  551. *argv = (char **)malloc(sizeof(char **) * (*argc));
  552. memset(*argv, 0, sizeof(char **) * (*argc));
  553. // 将每个命令都单独提取出来
  554. for (int i = 0; i < *argc && index < INPUT_BUFFER_SIZE; ++i)
  555. {
  556. // 提取出命令,以空格作为分割
  557. *((*argv) + i) = &buf[index];
  558. while (index < (INPUT_BUFFER_SIZE - 1) && buf[index] && buf[index] != ' ')
  559. ++index;
  560. buf[index++] = '\0';
  561. // 删除命令间多余的空格
  562. while (index < INPUT_BUFFER_SIZE && buf[index] == ' ')
  563. ++index;
  564. // printf("%s\n", (*argv)[i]);
  565. }
  566. // 以第一个命令作为主命令,查找其在命令表中的编号
  567. return shell_find_cmd(**argv);
  568. }