From f16b35b4b6c11695ae9f066a02e2fdb999c5585a Mon Sep 17 00:00:00 2001 From: Neffi42 <114996380+Neffi42@users.noreply.github.com> Date: Wed, 15 Oct 2025 11:12:57 +0200 Subject: [PATCH 1/4] add tty id --- kernel/include/tty.h | 1 + kernel/src/tty/tty.c | 1 + 2 files changed, 2 insertions(+) diff --git a/kernel/include/tty.h b/kernel/include/tty.h index e1b6616..b619959 100644 --- a/kernel/include/tty.h +++ b/kernel/include/tty.h @@ -15,6 +15,7 @@ typedef struct { size_t column; uint8_t color; uint16_t buf[VGA_BUFFER_SIZE]; + uint8_t id; } tty_t; typedef enum vga_color { diff --git a/kernel/src/tty/tty.c b/kernel/src/tty/tty.c index 0a6ade3..cae3cd7 100644 --- a/kernel/src/tty/tty.c +++ b/kernel/src/tty/tty.c @@ -35,6 +35,7 @@ void tty_init() { ttys[i].row = 0; ttys[i].column = 0; ttys[i].color = curr_tty->color; + ttys[i].id = i + 1; memcpy(ttys[i].buf, vga_buf, VGA_BUFFER_SIZE); } } From 635c6379929e0e20c62afccb9cd1bf7654c90298 Mon Sep 17 00:00:00 2001 From: Neffi42 <114996380+Neffi42@users.noreply.github.com> Date: Wed, 15 Oct 2025 11:14:06 +0200 Subject: [PATCH 2/4] update tty_print_pompt --- kernel/src/tty/tty.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kernel/src/tty/tty.c b/kernel/src/tty/tty.c index cae3cd7..af81e3d 100644 --- a/kernel/src/tty/tty.c +++ b/kernel/src/tty/tty.c @@ -7,11 +7,11 @@ #include #include -#define TTY_PROMPT ">" static tty_t ttys[4]; static tty_t* curr_tty = &ttys[0]; static uint16_t* vga_buf = (uint16_t*)VGA_MEMORY_BASE; +static size_t prompt_len = 0; static void putendl(size_t y) { for (size_t x = 0; x < VGA_WIDTH; x++) @@ -108,7 +108,12 @@ void tty_change_screen(size_t screen_number) { void tty_print_prompt() { if (curr_tty->column != 0) return; - tty_putstr(TTY_PROMPT); + uint8_t fg = vga_get_color_fg(curr_tty->color); + uint8_t bg = vga_get_color_bg(curr_tty->color); + + tty_set_color(VGA_COLOR_LIGHT_GREEN, bg); + prompt_len = printf("[%d] > ", curr_tty->id); + tty_set_color(fg, bg); } static void build_command(char* buf) { From 2aa2762c2f24107455369f929e160719ea381f5d Mon Sep 17 00:00:00 2001 From: Neffi42 <114996380+Neffi42@users.noreply.github.com> Date: Wed, 15 Oct 2025 11:14:41 +0200 Subject: [PATCH 3/4] add tty.input_start to correct BOF in try_build_command --- kernel/include/tty.h | 2 ++ kernel/src/tty/tty.c | 28 ++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/kernel/include/tty.h b/kernel/include/tty.h index b619959..88336ee 100644 --- a/kernel/include/tty.h +++ b/kernel/include/tty.h @@ -5,6 +5,7 @@ #include #define TTY_MAX_SCREEN 4 + #define VGA_MEMORY_BASE 0xB8000 #define VGA_WIDTH 80 #define VGA_HEIGHT 25 @@ -16,6 +17,7 @@ typedef struct { uint8_t color; uint16_t buf[VGA_BUFFER_SIZE]; uint8_t id; + size_t input_start; } tty_t; typedef enum vga_color { diff --git a/kernel/src/tty/tty.c b/kernel/src/tty/tty.c index af81e3d..8e24a6b 100644 --- a/kernel/src/tty/tty.c +++ b/kernel/src/tty/tty.c @@ -2,12 +2,12 @@ #include "command.h" #include "io.h" #include "keyboard.h" +#include #include #include #include #include - static tty_t ttys[4]; static tty_t* curr_tty = &ttys[0]; static uint16_t* vga_buf = (uint16_t*)VGA_MEMORY_BASE; @@ -36,6 +36,7 @@ void tty_init() { ttys[i].column = 0; ttys[i].color = curr_tty->color; ttys[i].id = i + 1; + ttys[i].input_start = 0; memcpy(ttys[i].buf, vga_buf, VGA_BUFFER_SIZE); } } @@ -54,6 +55,7 @@ void tty_putchar(unsigned char c) { tty_putchar_at(c, curr_tty->column, curr_tty->row); if (++curr_tty->column == VGA_WIDTH || c == '\n') { curr_tty->column = 0; + curr_tty->input_start = 0; if (++curr_tty->row == VGA_HEIGHT) { scroll(); curr_tty->row -= 1; @@ -78,7 +80,7 @@ void tty_clear() { } void tty_backspace() { - if (curr_tty->column <= strlen(TTY_PROMPT)) { + if (curr_tty->column <= curr_tty->input_start) { return; } else { curr_tty->column--; @@ -108,27 +110,37 @@ void tty_change_screen(size_t screen_number) { void tty_print_prompt() { if (curr_tty->column != 0) return; + uint8_t fg = vga_get_color_fg(curr_tty->color); uint8_t bg = vga_get_color_bg(curr_tty->color); tty_set_color(VGA_COLOR_LIGHT_GREEN, bg); prompt_len = printf("[%d] > ", curr_tty->id); + curr_tty->input_start = prompt_len; tty_set_color(fg, bg); } -static void build_command(char* buf) { - size_t prompt_len = strlen(TTY_PROMPT); - const uint16_t* command = &vga_buf[curr_tty->row * VGA_WIDTH + prompt_len]; - for (size_t i = 0; i < curr_tty->column - prompt_len; i++) +static bool try_build_command(char* buf) { + if (curr_tty->column <= curr_tty->input_start) + return false; + + const uint16_t* command = + &vga_buf[curr_tty->row * VGA_WIDTH + curr_tty->input_start]; + + for (size_t i = 0; i < curr_tty->column - curr_tty->input_start; i++) buf[i] = vga_get_char(command[i]); + + return true; } void tty_handle_entry(char c) { if (c == KC_ENTER) { char buf[VGA_WIDTH] = {0}; - build_command(buf); + bool valid_command = try_build_command(buf); tty_putchar('\n'); - cmd_handle(buf); + if (valid_command) + cmd_handle(buf); + tty_print_prompt(); return; } From c93116be0ea847a102ec23c251b172adafa4301c Mon Sep 17 00:00:00 2001 From: Neffi42 <114996380+Neffi42@users.noreply.github.com> Date: Wed, 15 Oct 2025 13:13:41 +0200 Subject: [PATCH 4/4] correct mistakes --- kernel/src/tty/tty.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/kernel/src/tty/tty.c b/kernel/src/tty/tty.c index 8e24a6b..0087356 100644 --- a/kernel/src/tty/tty.c +++ b/kernel/src/tty/tty.c @@ -11,7 +11,6 @@ static tty_t ttys[4]; static tty_t* curr_tty = &ttys[0]; static uint16_t* vga_buf = (uint16_t*)VGA_MEMORY_BASE; -static size_t prompt_len = 0; static void putendl(size_t y) { for (size_t x = 0; x < VGA_WIDTH; x++) @@ -115,8 +114,7 @@ void tty_print_prompt() { uint8_t bg = vga_get_color_bg(curr_tty->color); tty_set_color(VGA_COLOR_LIGHT_GREEN, bg); - prompt_len = printf("[%d] > ", curr_tty->id); - curr_tty->input_start = prompt_len; + printf("[%u] > ", curr_tty->id); tty_set_color(fg, bg); }