feat: basic ksh
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// Copyright (c) 2025 0xKarinyash
|
||||
|
||||
#include <core/string.h>
|
||||
|
||||
i32 strcmp(const char* s1, const char* s2) {
|
||||
while (*s1 && (*s1 == *s2)) {
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
|
||||
}
|
||||
@@ -43,6 +43,10 @@ void console_init(SG_Context *ctx) {
|
||||
s_font.base = (const unsigned char*)font8x16;
|
||||
}
|
||||
|
||||
u64 console_get_colors() {
|
||||
return ((u64) s_color << 32) | s_bg_color;
|
||||
}
|
||||
|
||||
void console_clear(u32 color) {
|
||||
if (!ctx_ptr) return;
|
||||
|
||||
|
||||
+6
-8
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "bootinfo.h"
|
||||
#include "../data/logo.h"
|
||||
#include "shell/ksh.h"
|
||||
|
||||
#include <types.h>
|
||||
|
||||
@@ -10,6 +11,8 @@
|
||||
#include <drivers/serial.h>
|
||||
#include <drivers/console.h>
|
||||
|
||||
#include <core/panic.h>
|
||||
|
||||
#include <gdt.h>
|
||||
#include <idt.h>
|
||||
#include <pic.h>
|
||||
@@ -49,12 +52,7 @@ void kmain(Bootinfo* info) {
|
||||
SG_Point text_normal_point = {0, 120}; // not nice to hardcode nums like that but we have what we have
|
||||
console_set_cursor_pos(&text_normal_point);
|
||||
|
||||
kprintf("ksh_> ");
|
||||
char buff[32];
|
||||
kgets(buff, 32);
|
||||
kprintf("You typed: %s", buff);
|
||||
|
||||
__asm__ volatile ("sti");
|
||||
|
||||
while (1) { __asm__("hlt"); }
|
||||
ksh();
|
||||
|
||||
panic("Kernel main loop exited");
|
||||
}
|
||||
@@ -1,3 +1,58 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// Copyright (c) 2025 0xKarinyash
|
||||
#include <shell/ksh.h>
|
||||
#include <drivers/console.h>
|
||||
#include <core/string.h>
|
||||
|
||||
typedef enum {
|
||||
TOKEN_NULL,
|
||||
TOKEN_ILLEGAL,
|
||||
|
||||
TOKEN_HELP,
|
||||
|
||||
TOKEN_QUIT,
|
||||
TOKEN_CLEAR
|
||||
} ksh_token;
|
||||
|
||||
typedef struct {
|
||||
char* str;
|
||||
ksh_token token;
|
||||
} ksh_command_map;
|
||||
|
||||
static const ksh_command_map token_map[] = {
|
||||
{"q", TOKEN_QUIT},
|
||||
{"quit", TOKEN_QUIT},
|
||||
{"exit", TOKEN_QUIT},
|
||||
|
||||
{"cls", TOKEN_CLEAR},
|
||||
{"clear", TOKEN_CLEAR},
|
||||
|
||||
{"help", TOKEN_HELP},
|
||||
{nullptr, TOKEN_NULL}
|
||||
};
|
||||
|
||||
ksh_token char2token(char* token) {
|
||||
for (i32 i = 0; token_map[i].str != nullptr; i++) {
|
||||
if (strcmp(token, token_map[i].str) == 0) return token_map[i].token;
|
||||
}
|
||||
return TOKEN_ILLEGAL;
|
||||
}
|
||||
|
||||
static void print_help() {
|
||||
kprintf("\tWelcome to ^ptermOS^0's kernel shell!\n");
|
||||
kprintf("\tIt can almost nothing! yet.");
|
||||
}
|
||||
|
||||
void ksh() {
|
||||
while (true) {
|
||||
kprintf("\nksh_> ");
|
||||
char cmdbuff[256];
|
||||
kgets(cmdbuff, 256);
|
||||
switch(char2token(cmdbuff)) {
|
||||
case TOKEN_QUIT: return; // that'll cause panic lol
|
||||
case TOKEN_CLEAR: console_clear((u32) console_get_colors() & 0xFFFFFFFF); break;
|
||||
case TOKEN_HELP: print_help(); break;
|
||||
default: kprintf("Unknown command!!"); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user