Skip to content

Commit

Permalink
Merge pull request #13 from Ryand1234/command_executor
Browse files Browse the repository at this point in the history
added basic commands
  • Loading branch information
Ryand1234 authored Aug 5, 2024
2 parents 30fa561 + e95c4b2 commit c57efbf
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 6 deletions.
4 changes: 2 additions & 2 deletions kernel/arch/devices/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ void keyboard_handler(struct regs *r){

if(scancode == 0x2a || scancode == 0x36){shift = 1;return;}
if(scancode == 0x3a){ caps = ~caps; return;}
if(scancode == 0x14 || scancode == 0xE0) { ctrl = 1; return;}
if(scancode == 0x1c){terminal_new_line();return;}
if(scancode == 0xE0) { ctrl = 1; return;}
if(scancode == 0x1c){terminal_process_command();return;}
if(scancode == 0x0e){
terminal_backspace();
return;
Expand Down
2 changes: 1 addition & 1 deletion kernel/arch/i386/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ struct kernel_time rtc_time() {

void show_time() {
struct kernel_time time = rtc_time();
printf("current time: %d:%d:%d %d:%d:%d%d\n", time.hour, time.minute, time.second, time.day, time.month, time.century, time.year);
printf("current time: %d:%d:%d %d:%d:%d%d", time.hour, time.minute, time.second, time.day, time.month, time.century, time.year);
}
84 changes: 83 additions & 1 deletion kernel/arch/i386/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
#include<stddef.h>
#include<stdint.h>
#include<string.h>

#include<mmu/alloc.h>
#include<arch/i386/time.h>
#include<kernel/tty.h>
#include "vga.h"
#include <string.h>

#define PROMPT "cheaky>"
#define PROMPT_LENGTH 7 // Length of "cheaky>"

static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 100;
Expand Down Expand Up @@ -121,3 +126,80 @@ void terminal_write(const char* data, size_t size) {
void terminal_writestring(const char* data) {
terminal_write(data, strlen(data));
}



char* parse_command(size_t y) {
if (y >= VGA_HEIGHT) {
return NULL; // Invalid row number
}
// Calculate the starting index for the row
size_t start_index = y * VGA_WIDTH;

// Check if the row starts with the prompt
for (size_t i = 0; i < PROMPT_LENGTH; i++) {
printf(terminal_buffer[start_index + i]);
printf(vga_entry(PROMPT[i], terminal_color));
if (terminal_buffer[start_index + i] != vga_entry(PROMPT[i], terminal_color)) {
return NULL; // Prompt not found at the start of the row
}
}
// Allocate memory for the string (max length is VGA_WIDTH - PROMPT_LENGTH)
char* result = (char*)kmalloc(VGA_WIDTH - PROMPT_LENGTH + 1);
if (result == NULL) {
return NULL; // Memory allocation failed
}
// Extract characters after the prompt
size_t j = 0;
for (size_t i = start_index + PROMPT_LENGTH; i < (y + 1) * VGA_WIDTH; i++) {
char c = terminal_buffer[i] & 0xFF; // Extract character from VGA entry
if (c == '\0' || c == ' ') {
break; // Stop at null terminator or space
}
result[j++] = c;
}
result[j] = '\0'; // Null-terminate the string
return result;
}



enum Command {
CMD_VERSION,
CMD_REBOOT,
CMD_WHOAMI,
CMD_TIME,
CMD_UNKNOWN
// ... other commands ...
};

enum Command get_command(const char *str) {
if (strcmp(str, "version")) return CMD_VERSION;
if (strcmp(str, "reboot")) return CMD_REBOOT;
if (strcmp(str, "whoami")) return CMD_WHOAMI;
if (strcmp(str, "time")) return CMD_TIME;
return CMD_UNKNOWN;
}

void terminal_process_command() {
char *command = parse_command(terminal_row);
terminal_new_line();
switch (get_command(command)) {
case CMD_VERSION:
terminal_writestring("Kernel version 1.0");
break;
case CMD_REBOOT:
terminal_writestring("Rebooting system...");
// Add reboot logic here
break;
case CMD_WHOAMI:
terminal_writestring("Revan");
break;
case CMD_TIME:
show_time(); break;
default:
terminal_writestring("bash: command '"); printf(command); terminal_writestring("' not recognised!!!");
}

terminal_new_line(); terminal_writestring("cheaky>");
}
2 changes: 1 addition & 1 deletion kernel/mmu/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void *ksbrk(uint16_t n)
realsize = KMALLOC_MINSIZE;
}

printf("Size required: %d\n", realsize);
// printf("Size required: %d\n", realsize);
struct kmalloc_header *chunk, *other;
chunk = (struct kmalloc_header*) &end;
while(chunk->used || chunk->size < realsize)
Expand Down
4 changes: 3 additions & 1 deletion libc/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ extern "C" {
#endif

int memcmp(const void*, const void*, size_t);
void* memcpy(void* __restrict, const void* __restrict, size_t);
int strcmp(const void*, const void*);
void* memcpy(void* __restrict, const void* __restrict, size_t);;
void* memmove(void*, const void*, size_t);
void* memset(void*, int, size_t);
size_t strlen(const char*);

#ifdef _cplusplus
}
Expand Down
15 changes: 15 additions & 0 deletions libc/string/memcmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,19 @@ int memcmp(const void* aptr, const void* bptr, size_t size) {
return 1;
}
return 0;
}

int strcmp(const void* aptr, const void* bptr) {
size_t alen = strlen(aptr);
size_t blen = strlen(bptr);
if(alen != blen)
return 0;
const unsigned char* a = (const unsigned char*) aptr;
const unsigned char* b = (const unsigned char*) bptr;
for(size_t i = 0; i < alen; i++)
{
if(a[i] != b[i])
return 0;
}
return 1;
}

0 comments on commit c57efbf

Please sign in to comment.