From dd08a04b9d40222063a6ceb223c614c7197f8dfc Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Wed, 19 Feb 2025 11:39:53 +0100 Subject: huge step in the code redesign got the core of the code working, now whats left is to work on small little features and then fine tune everything --- main.c | 70 ++++-------------------------------------------------------------- 1 file changed, 4 insertions(+), 66 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 4304a92..5ee11f6 100644 --- a/main.c +++ b/main.c @@ -5,7 +5,7 @@ #include #define MAX_HISTORY 100 -#define MAX_COMMAND_LENGTH 255 // not setting a macro for the argument because they are included in the command) +#define MAX_COMMAND_LENGTH 255 // not setting a macro for the argument because they are included in the command #define MAX_PATH_LENGTH 1024 struct command { @@ -15,44 +15,15 @@ struct command { struct exec_command { const char *cmd; + void (*func)(const char *); }; - // code prototype functions struct command split_command(char *input); void exec_command(char *input); -// code functions -struct command split_command(char *input) { - struct command NEW_COMMAND = {}; - - char *command = strtok(input, " "); - char *argument = strtok(NULL, " "); - - if (command != NULL) { - NEW_COMMAND.com = strdup(command); - } else { - printf("failed, null pointer detected, no initial command has been inserted"); - free(command); - } - - if (argument != NULL) { - NEW_COMMAND.arg = strdup(argument); - } else { - free(argument); - } - return NEW_COMMAND; -} - -void exec_command(char *input) { - struct command NEW_COMMAND = {}; - - // cycle through the command array and find the matching command and execute it -} - - // shell commands functions prototypes void list(const char *path); @@ -189,15 +160,7 @@ void print_cdirectory(const char *arg) { return; } char *current_working_directory = getcwd(NULL, 0); - /* getcwd() command already allocates malloc for the path that should be returned, - to do: should I free it when finished or not?/ - so what I understand from the documentation, you should only free it when actually setting an exact size - but when keeping the max size to 0 and relying on the malloc there is no need for it - not sure though - should rely on further debugging - */ - - return current_working_directory; + printf("%s", current_working_directory); } void change_directory(const char *path) { @@ -214,31 +177,6 @@ int main(void) { char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH); fgets(input, MAX_COMMAND_LENGTH, stdin); - - const struct command new_input = split_command(input); - new_input.com[strcspn(new_input.com, "\n")] = '\0'; - - if (new_input.arg != NULL) { - new_input.arg[strcspn(new_input.arg, "\n")] = '\0'; - } - - if (strcmp(new_input.com, "exit") == 0) { - free(input); - return 0; - } - if (strcmp(new_input.com, "ls") == 0) { - ls(new_input.arg); - free(new_input.com); - if (new_input.arg != NULL) { - free(new_input.arg); - } - } - if (strcmp(new_input.com, "echo") == 0) { - printf("%s", new_input.arg); - free(new_input.arg); - } - if (strcmp(new_input.com, "clear") == 0) { - clear(); - } + exec_command(input); } } -- cgit v1.2.3-70-g09d2