Skip to content

Commit

Permalink
Fixed username not showing in in shell+added clear command
Browse files Browse the repository at this point in the history
  • Loading branch information
GAMINGNOOBdev committed Jan 14, 2025
1 parent 9fdc505 commit 7b78ed4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
5 changes: 3 additions & 2 deletions source/includes/sh_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <memory2.h>
#include <graphics.h>

#define BUFFER_SIZE 1024
#define BUFFER_SIZE 10

typedef struct command_list_entry
{
Expand Down Expand Up @@ -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);
2 changes: 1 addition & 1 deletion source/kernel/C/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 9 additions & 11 deletions source/kernel/C/shell/commands/login.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
#include <basics.h>

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;

for(int i = 0; i < 30; i++)
__putc('=');
__putc('\n');

warn("DON'T USE BACKSPACE!!", __FILE__);

print("Username: ");
i = 0;
Expand Down Expand Up @@ -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;

}
40 changes: 27 additions & 13 deletions source/kernel/C/shell/sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
*/

#include <basics.h>
#include <sh_util.h>
#include <memory2.h>
#include <keyboard.h>
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -118,22 +123,22 @@ 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;
}
else if (c == '\b')
{
if (cursor > 0)
cursor--;
else continue;
}
else
{
Expand All @@ -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();
Expand Down

0 comments on commit 7b78ed4

Please sign in to comment.