From f99bd691883d32ff62b4b1bc799f45de760a9a77 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Mon, 15 Sep 2025 03:34:43 -0400 Subject: kern: cons: Handle ASCII linefeed char Signed-off-by: Ian Moffett --- src/sys/io/cons/cons.c | 63 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/src/sys/io/cons/cons.c b/src/sys/io/cons/cons.c index f95bf0f..aab5778 100644 --- a/src/sys/io/cons/cons.c +++ b/src/sys/io/cons/cons.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,9 @@ struct cons_scr g_root_scr; +/* Forward declarations */ +static void fill_screen(struct cons_scr *scr, uint32_t bg); + /* * Get the index into the framebuffer with an x and y * position. @@ -59,6 +63,26 @@ fb_get_index(uint32_t pitch, uint32_t x, uint32_t y) return x + y * (pitch / 4); } +/* + * Write a newline onto the console and handle + * Y overflows + * + * @scr: Screen to write a newline onto + */ +static void +cons_newline(struct cons_scr *scr) +{ + scr->text_x = 0; + scr->text_y += FONT_HEIGHT; + + /* Handle console y overflow */ + if (scr->text_y >= scr->max_row - FONT_HEIGHT) { + scr->text_x = 0; + scr->text_y = 0; + fill_screen(scr, scr->scr_bg); + } +} + /* * Fill a screen with a desired background * color @@ -80,6 +104,31 @@ fill_screen(struct cons_scr *scr, uint32_t bg) memset(fbvars->io, bg, len); } +/* + * Handle special ASCII characters + * + * @scr: Screen we are writing on + * @c: Character to handle + * + * Returns the character on success, otherwis a less than + * zero value on [need retransmit] / error + */ +static int +cons_handle_spec(struct cons_scr *scr, int c) +{ + if (scr == NULL) { + return -EINVAL; + } + + switch (c) { + case ASCII_LF: + cons_newline(scr); + return c; + } + + return -1; +} + /* * Plot a single character onto the screen * @@ -136,20 +185,16 @@ cons_putstr(struct cons_scr *scr, const char *str, size_t len) spinlock_acquire(&scr->lock); for (size_t i = 0; i < len; ++i) { ch.c = str[i]; + if (cons_handle_spec(scr, ch.c) == ch.c) { + continue; + } + cons_putch(scr, &ch); scr->text_x += FONT_WIDTH; /* Handle console x overflow */ if (scr->text_x >= scr->max_col - FONT_WIDTH) { - scr->text_x = 0; - scr->text_y += FONT_HEIGHT; - } - - /* Handle console y overflow */ - if (scr->text_y >= scr->max_row - FONT_HEIGHT) { - scr->text_x = 0; - scr->text_y = 0; - fill_screen(scr, scr->scr_bg); + cons_newline(scr); } } -- cgit v1.2.3