123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 |
- #include "cmd.h"
- #include <libc/string.h>
- #include <libc/stdio.h>
- #include <libc/stddef.h>
- #include <libsystem/syscall.h>
- #include <libc/string.h>
- #include <libc/errno.h>
- #include <libc/unistd.h>
- #include <libc/stdlib.h>
- #include <libc/fcntl.h>
- #include <libc/dirent.h>
- #include <libc/sys/wait.h>
- #include <libc/sys/stat.h>
- #include "cmd_help.h"
- #include "cmd_test.h"
- char *shell_current_path = NULL;
- struct built_in_cmd_t shell_cmds[] =
- {
- {"cd", shell_cmd_cd},
- {"cat", shell_cmd_cat},
- {"exec", shell_cmd_exec},
- {"ls", shell_cmd_ls},
- {"mkdir", shell_cmd_mkdir},
- {"pwd", shell_cmd_pwd},
- {"rm", shell_cmd_rm},
- {"rmdir", shell_cmd_rmdir},
- {"reboot", shell_cmd_reboot},
- {"touch", shell_cmd_touch},
- {"about", shell_cmd_about},
- {"free", shell_cmd_free},
- {"help", shell_help},
- {"pipe", shell_pipe_test},
- };
- const static int total_built_in_cmd_num = sizeof(shell_cmds) / sizeof(struct built_in_cmd_t);
- static char *get_target_filepath(const char *filename, int *result_path_len)
- {
- int cwd_len = strlen(shell_current_path);
-
- *result_path_len = cwd_len + strlen(filename);
- char *file_path = (char *)malloc(*result_path_len + 2);
- memset(file_path, 0, *result_path_len + 2);
- strcpy(file_path, shell_current_path);
-
- if (cwd_len > 1)
- file_path[cwd_len] = '/';
-
- if (filename[0] == '/')
- strcat(file_path, filename + 1);
- else
- strcat(file_path, filename);
- return file_path;
- }
- int shell_find_cmd(char *main_cmd)
- {
- for (int i = 0; i < total_built_in_cmd_num; ++i)
- {
- if (strcmp(main_cmd, shell_cmds[i].name) == 0)
- return i;
- }
-
- return -1;
- }
- void shell_run_built_in_command(int index, int argc, char **argv)
- {
- if (index >= total_built_in_cmd_num)
- return;
-
- shell_cmds[index].func(argc, argv);
- }
- int shell_cmd_cd(int argc, char **argv)
- {
- int current_dir_len = strlen(shell_current_path);
- if (argc < 2)
- {
- shell_help_cd();
- goto done;
- }
-
- if (!strcmp(".", argv[1]))
- goto done;
-
- if (!strcmp("..", argv[1]))
- {
-
- if (!strcmp("/", shell_current_path))
- goto done;
-
- int index = current_dir_len - 1;
- for (; index > 1; --index)
- {
- if (shell_current_path[index] == '/')
- break;
- }
- shell_current_path[index] = '\0';
-
- goto done;
- }
- int dest_len = strlen(argv[1]);
-
- if (dest_len >= SHELL_CWD_MAX_SIZE - 1)
- {
- printf("ERROR: Path too long!\n");
- goto fail;
- }
- if (argv[1][0] == '/')
- {
-
- int ec = chdir(argv[1]);
- if (ec == -1)
- ec = errno;
- if (ec == 0)
- {
-
- char *new_path = (char *)malloc(dest_len + 2);
- memset(new_path, 0, dest_len + 2);
- strncpy(new_path, argv[1], dest_len);
-
- free(shell_current_path);
- shell_current_path = new_path;
- shell_current_path[dest_len] = '\0';
- return 0;
- }
- else
- goto fail;
- ;
- }
- else
- {
- int dest_offset = 0;
- if (dest_len > 2)
- {
- if (argv[1][0] == '.' && argv[1][1] == '/')
- dest_offset = 2;
- }
- int new_len = current_dir_len + dest_len - dest_offset;
- if (new_len >= SHELL_CWD_MAX_SIZE - 1)
- {
- printf("ERROR: Path too long!\n");
- goto fail;
- }
-
- char *new_path = (char *)malloc(new_len + 2);
- memset(new_path, 0, sizeof(new_path));
- strncpy(new_path, shell_current_path, current_dir_len);
- if (current_dir_len > 1)
- new_path[current_dir_len] = '/';
- strcat(new_path, argv[1] + dest_offset);
- int x = chdir(new_path);
- if (x == 0)
- {
- free(shell_current_path);
-
- new_path[new_len + 1] = '\0';
- shell_current_path = new_path;
- goto done;
- }
- else
- {
- printf("ERROR: Cannot switch to directory: %s\n", new_path);
- goto fail;
- }
- }
- fail:;
- done:;
-
- free(argv);
- return 0;
- }
- int shell_cmd_ls(int argc, char **argv)
- {
- struct DIR *dir = opendir(shell_current_path);
- if (dir == NULL)
- return -1;
- struct dirent *buf = NULL;
-
- while (1)
- {
- buf = readdir(dir);
- if (buf == NULL)
- break;
- int color = COLOR_WHITE;
- if (buf->d_type & VFS_IF_DIR)
- color = COLOR_YELLOW;
- else if (buf->d_type & VFS_IF_FILE)
- color = COLOR_INDIGO;
- else if (buf->d_type & VFS_IF_DEVICE)
- color = COLOR_GREEN;
- char output_buf[256] = {0};
- sprintf(output_buf, "%s ", buf->d_name);
- put_string(output_buf, color, COLOR_BLACK);
- }
- printf("\n");
- closedir(dir);
- if (argv != NULL)
- free(argv);
- return 0;
- }
- int shell_cmd_pwd(int argc, char **argv)
- {
- if (shell_current_path)
- printf("%s\n", shell_current_path);
- if (argv != NULL)
- free(argv);
- return 0;
- }
- int shell_cmd_cat(int argc, char **argv)
- {
- int path_len = 0;
- char *file_path = get_target_filepath(argv[1], &path_len);
-
- int fd = open(file_path, 0);
-
- int file_size = lseek(fd, 0, SEEK_END);
-
- lseek(fd, 0, SEEK_SET);
- char *buf = (char *)malloc(512);
- memset(buf, 0, 512);
- while (file_size > 0)
- {
- int l = read(fd, buf, 511);
- buf[l] = '\0';
- file_size -= l;
- printf("%s", buf);
- }
- close(fd);
- free(buf);
- if (argv != NULL)
- free(argv);
- return 0;
- }
- int shell_cmd_touch(int argc, char **argv)
- {
- int path_len = 0;
- char *file_path;
- if (argv[1][0] == '/')
- file_path = argv[1];
- else
- file_path = get_target_filepath(argv[1], &path_len);
-
- int fd = open(file_path, O_CREAT);
- switch (fd)
- {
- case -ENOENT:
- put_string("Parent dir not exists.\n", COLOR_RED, COLOR_BLACK);
- break;
- default:
- break;
- }
- close(fd);
- if (argv != NULL)
- free(argv);
- return 0;
- }
- int shell_cmd_rm(int argc, char **argv) {return 0;}
- int shell_cmd_mkdir(int argc, char **argv)
- {
- int result_path_len = -1;
- const char *full_path = NULL;
- if (argv[1][0] == '/')
- full_path = argv[1];
- else
- {
- full_path = get_target_filepath(argv[1], &result_path_len);
- }
-
- int retval = mkdir(full_path, 0);
- if (argv != NULL)
- free(argv);
- return retval;
- }
- int shell_cmd_rmdir(int argc, char **argv)
- {
- const char *full_path = NULL;
- int result_path_len = -1;
- if (argv[1][0] == '/')
- full_path = argv[1];
- else
- full_path = get_target_filepath(argv[1], &result_path_len);
- int retval = rmdir(full_path);
-
- if (argv != NULL)
- free(argv);
- return retval;
- }
- int shell_cmd_exec(int argc, char **argv)
- {
- pid_t pid = fork();
- int retval = 0;
-
- if (pid == 0)
- {
-
-
- int path_len = 0;
- char *file_path = get_target_filepath(argv[1], &path_len);
-
- execv(file_path, argv);
- free(argv);
- while (1)
- ;
- exit(0);
- }
- else
- {
-
- waitpid(pid, &retval, 0);
-
- free(argv);
- }
- }
- int shell_cmd_about(int argc, char **argv)
- {
- if (argv != NULL)
- free(argv);
- int aac = 0;
- char **aav;
- unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0};
- strcpy(input_buffer, "exec /about.elf\0");
- parse_command(input_buffer, &aac, &aav);
- shell_cmd_exec(aac, aav);
- }
- int shell_cmd_reboot(int argc, char **argv)
- {
- return syscall_invoke(SYS_REBOOT, 0, 0, 0, 0, 0, 0, 0, 0);
- }
- int shell_cmd_free(int argc, char **argv)
- {
- int retval = 0;
- if (argc == 2 && strcmp("-m", argv[1]) != 0)
- {
- retval = -EINVAL;
- printf("Invalid argument: %s\n", argv[1]);
- goto done;
- }
- struct mstat_t mst = {0};
- retval = mstat(&mst);
- if (retval != 0)
- {
- printf("Failed: retval=%d", retval);
- goto done;
- }
- printf("\ttotal\tused\tfree\tshared\tcache\tavailable\n");
- printf("Mem:\t");
- if (argc == 1)
- {
- 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, mst.cache_used >> 10, mst.available >> 10);
- }
- else
- {
- 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, mst.cache_used >> 20, mst.available >> 20);
- }
- done:;
- if (argv != NULL)
- free(argv);
- return retval;
- }
- int parse_command(char *buf, int *argc, char ***argv)
- {
-
- int index = 0;
-
- while (index < INPUT_BUFFER_SIZE && buf[index] == ' ')
- ++index;
-
- for (int i = index; i < (INPUT_BUFFER_SIZE - 1); ++i)
- {
-
- if (!buf[i])
- break;
- if (buf[i] != ' ' && (buf[i + 1] == ' ' || buf[i + 1] == '\0'))
- ++(*argc);
- }
-
-
- *argv = (char **)malloc(sizeof(char **) * (*argc));
- memset(*argv, 0, sizeof(char **) * (*argc));
-
- for (int i = 0; i < *argc && index < INPUT_BUFFER_SIZE; ++i)
- {
-
- *((*argv) + i) = &buf[index];
- while (index < (INPUT_BUFFER_SIZE - 1) && buf[index] && buf[index] != ' ')
- ++index;
- buf[index++] = '\0';
-
- while (index < INPUT_BUFFER_SIZE && buf[index] == ' ')
- ++index;
-
- }
-
- return shell_find_cmd(**argv);
- }
|