From 7b78ed45aec218a904ae4f5ff995705340dc12ed Mon Sep 17 00:00:00 2001 From: GAMINGNOOBdev Date: Tue, 14 Jan 2025 22:17:07 +0100 Subject: [PATCH] Fixed username not showing in in shell+added clear command --- source/includes/sh_util.h | 5 ++-- source/kernel/C/kernel.c | 2 +- source/kernel/C/shell/commands/login.c | 20 ++++++------- source/kernel/C/shell/sh.c | 40 +++++++++++++++++--------- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/source/includes/sh_util.h b/source/includes/sh_util.h index 3bbf3ff..a38435d 100644 --- a/source/includes/sh_util.h +++ b/source/includes/sh_util.h @@ -12,7 +12,7 @@ #include #include -#define BUFFER_SIZE 1024 +#define BUFFER_SIZE 10 typedef struct command_list_entry { @@ -48,5 +48,6 @@ void dispose_command_list(command_list* lst); * * @param lst Pointer to command list object * @param value Command string (will be copied to a new pointer) + * @param length Command string length */ -void push_command_to_list(command_list* lst, const char* value); +void push_command_to_list(command_list* lst, const char* value, size_t length); diff --git a/source/kernel/C/kernel.c b/source/kernel/C/kernel.c index 7708c3e..b6cc753 100755 --- a/source/kernel/C/kernel.c +++ b/source/kernel/C/kernel.c @@ -413,7 +413,7 @@ void main(void) { char* username = login_request(); - if(username != ""){ + if(username != NULL){ int argc = 1; char* dummy_argv[] = {username, null}; shell_main(argc, dummy_argv); diff --git a/source/kernel/C/shell/commands/login.c b/source/kernel/C/shell/commands/login.c index 797c759..94fa56a 100644 --- a/source/kernel/C/shell/commands/login.c +++ b/source/kernel/C/shell/commands/login.c @@ -12,8 +12,10 @@ #include char* login_request(){ - char username[21]; // +1 for null terminator - char password[21]; + static char username[21]; // +1 for null terminator + static char password[21]; + memset(username, 0, 21); + memset(password, 0, 21); char temp; int i; @@ -21,8 +23,6 @@ char* login_request(){ for(int i = 0; i < 30; i++) __putc('='); __putc('\n'); - - warn("DON'T USE BACKSPACE!!", __FILE__); print("Username: "); i = 0; @@ -68,11 +68,9 @@ char* login_request(){ __putc('\n'); - if (strcmp(username, "root") == 0 && strcmp(password, "prad") == 0) { - memset(username, 0, 21); - memset(password, 0, 21); - return username; // I know this won't give the username.. need to fix! - } else { - return ""; - } + if (strcmp(username, "root") == 0 && strcmp(password, "prad") == 0) + return &username[0]; + else + return NULL; + } \ No newline at end of file diff --git a/source/kernel/C/shell/sh.c b/source/kernel/C/shell/sh.c index b571d7b..639b8ba 100644 --- a/source/kernel/C/shell/sh.c +++ b/source/kernel/C/shell/sh.c @@ -9,6 +9,7 @@ * */ +#include #include #include #include @@ -41,20 +42,21 @@ void dispose_command_list(command_list* lst) } } -void push_command_to_list(command_list* lst, const char* value) +void push_command_to_list(command_list* lst, const char* value, size_t length) { - if (lst == NULL || value == NULL) + if (lst == NULL || value == NULL || length == 0) return; command_list_entry* entry = malloc(sizeof(command_list_entry)); - memset(entry, 0, sizeof(command_list_entry)); + assert(entry != NULL, __FILE__, __LINE__); if (entry == NULL) return; - size_t commandSize = strlen_(value); - entry->command = malloc(commandSize+1); - memset(entry->command, 0, commandSize+1); - memcpy(entry->command, value, commandSize); + memset(entry, 0, sizeof(command_list_entry)); + entry->length = length; + entry->command = malloc(length+1); + memset(entry->command, 0, length+1); + memcpy(entry->command, value, length); if (lst->start == NULL) { @@ -98,13 +100,16 @@ int shell_main(int argc, char** argv){ print("\x1b[2J\x1b[H"); welcome_message(); + + command_list commandHistory; + init_command_list(&commandHistory); putc('\n'); print(argv[0]); - putc(' '); - putc('%'); - putc(' '); + print(" % "); + uint8_t commandPulledFromHistory = 0; + command_list_entry* entry = NULL; char c; while (running) { @@ -118,15 +123,14 @@ int shell_main(int argc, char** argv){ command[cursor] = '\0'; // Null-terminate the string putc('\n'); execute(command, argc, argv); + // push_command_to_list(&commandHistory, command, cursor); // <-- doesn't work because of malloc i assume cursor = 0; commandSize = 0; memset(command, 0, commandBufferSize); if(running){ print(argv[0]); - putc(' '); - putc('%'); - putc(' '); + print(" % "); } continue; } @@ -134,6 +138,7 @@ int shell_main(int argc, char** argv){ { if (cursor > 0) cursor--; + else continue; } else { @@ -152,14 +157,23 @@ int shell_main(int argc, char** argv){ } free(command); + dispose_command_list(&commandHistory); + return 0; } void execute(const char* buffer, int argc, char** argv) { + if (buffer == NULL) + return; + if (strlen_(buffer) == 0) + return; + if(strcmp(buffer, "exit") == 0){ print("\x1b[2J\x1b[H"); running = false; + } else if(strcmp(buffer, "clear") == 0){ + print("\x1b[2J\x1b[H"); } else if(strcmp(buffer, "shutdown") == 0){ info("Goodbye from Frosted Shell...", __FILE__); shutdown();