From ca5f7a6e46fd75810b63f7899ecd26263f155986 Mon Sep 17 00:00:00 2001 From: czapek1337 Date: Fri, 28 Jan 2022 23:10:44 +0100 Subject: [PATCH 1/7] menu: Handle uppercase inputs for menu hotkeys --- stage23/menu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stage23/menu.c b/stage23/menu.c index b0f6b60c..6430b0e6 100644 --- a/stage23/menu.c +++ b/stage23/menu.c @@ -847,7 +847,8 @@ timeout_aborted: reset_term(); } boot(selected_menu_entry->body); - case 'e': { + case 'e': + case 'E': { if (editor_enabled) { if (selected_menu_entry->sub != NULL) goto refresh; @@ -860,7 +861,8 @@ timeout_aborted: } break; } - case 'c': { + case 'c': + case 'C': { reset_term(); console(); goto refresh; From 0c677d093eaf349e11fd74fc09cebd16d8909920 Mon Sep 17 00:00:00 2001 From: czapek1337 Date: Fri, 28 Jan 2022 23:51:34 +0100 Subject: [PATCH 2/7] console,editor: Filter out non-printable characters --- stage23/lib/libc.h | 4 ++++ stage23/lib/libc.s2.c | 8 ++++++++ stage23/lib/readline.c | 2 +- stage23/menu.c | 12 +++++++----- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/stage23/lib/libc.h b/stage23/lib/libc.h index 7f7d91bb..7e8add9d 100644 --- a/stage23/lib/libc.h +++ b/stage23/lib/libc.h @@ -2,6 +2,10 @@ #define __LIB__LIBC_H__ #include +#include + +bool isprint(int c); +bool isspace(int c); int toupper(int c); int tolower(int c); diff --git a/stage23/lib/libc.s2.c b/stage23/lib/libc.s2.c index 24af2bec..1b818135 100644 --- a/stage23/lib/libc.s2.c +++ b/stage23/lib/libc.s2.c @@ -4,6 +4,14 @@ #include #include +bool isprint(int c) { + return c >= ' ' && c <= '~'; +} + +bool isspace(int c) { + return (c >= '\t' && c <= 0xD) || c == ' '; +} + int toupper(int c) { if (c >= 'a' && c <= 'z') { return c - 0x20; diff --git a/stage23/lib/readline.c b/stage23/lib/readline.c index c810dab8..22415716 100644 --- a/stage23/lib/readline.c +++ b/stage23/lib/readline.c @@ -317,7 +317,7 @@ void readline(const char *orig_str, char *buf, size_t limit) { term_write((uintptr_t)"\n", 1); goto out; default: { - if (strlen(buf) < limit - 1) { + if (strlen(buf) < limit - 1 && isprint(c)) { for (size_t j = strlen(buf); ; j--) { buf[j+1] = buf[j]; if (j == i) diff --git a/stage23/menu.c b/stage23/menu.c index 6430b0e6..f4663340 100644 --- a/stage23/menu.c +++ b/stage23/menu.c @@ -463,12 +463,14 @@ refresh: return NULL; default: if (strlen(buffer) < EDITOR_MAX_BUFFER_SIZE - 1) { - for (size_t i = strlen(buffer); ; i--) { - buffer[i+1] = buffer[i]; - if (i == cursor_offset) - break; + if (isprint(c) || c == '\n') { + for (size_t i = strlen(buffer); ; i--) { + buffer[i+1] = buffer[i]; + if (i == cursor_offset) + break; + } + buffer[cursor_offset++] = c; } - buffer[cursor_offset++] = c; } else { display_overflow_error = true; } From 2c1604089c93526acae9f073d4af7c89e9f25c2c Mon Sep 17 00:00:00 2001 From: czapek1337 Date: Fri, 28 Jan 2022 23:51:56 +0100 Subject: [PATCH 3/7] readline: Handle END/HOME keys --- stage23/lib/readline.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/stage23/lib/readline.c b/stage23/lib/readline.c index 22415716..06667405 100644 --- a/stage23/lib/readline.c +++ b/stage23/lib/readline.c @@ -316,6 +316,18 @@ void readline(const char *orig_str, char *buf, size_t limit) { case '\n': term_write((uintptr_t)"\n", 1); goto out; + case GETCHAR_END: + for (size_t j = 0; j < strlen(buf) - i; j++) { + cursor_fwd(); + } + i = strlen(buf); + continue; + case GETCHAR_HOME: + for (size_t j = 0; j < i; j++) { + cursor_back(); + } + i = 0; + continue; default: { if (strlen(buf) < limit - 1 && isprint(c)) { for (size_t j = strlen(buf); ; j--) { From 53e515e6421421a861968e0741bd14e6729cb58d Mon Sep 17 00:00:00 2001 From: czapek1337 Date: Fri, 28 Jan 2022 23:55:52 +0100 Subject: [PATCH 4/7] readline: Zero out the buffer before using it --- stage23/lib/readline.c | 1 + 1 file changed, 1 insertion(+) diff --git a/stage23/lib/readline.c b/stage23/lib/readline.c index 06667405..28673ab3 100644 --- a/stage23/lib/readline.c +++ b/stage23/lib/readline.c @@ -272,6 +272,7 @@ void readline(const char *orig_str, char *buf, size_t limit) { term_autoflush = false; size_t orig_str_len = strlen(orig_str); + memset(buf, 0, limit); memmove(buf, orig_str, orig_str_len); buf[orig_str_len] = 0; From 077341d1029e566e0bbc3a00adaf72e6ba829ef1 Mon Sep 17 00:00:00 2001 From: czapek1337 Date: Fri, 28 Jan 2022 23:56:10 +0100 Subject: [PATCH 5/7] console: Add a clear command --- stage23/console.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stage23/console.c b/stage23/console.c index 49a165b0..f810d196 100644 --- a/stage23/console.c +++ b/stage23/console.c @@ -13,6 +13,7 @@ static void console_help(void) { print( "Available commands:\n" "exit -- Exit Limine console.\n" + "clear -- Clears the console.\n" "editor -- Open an empty boot entry editor.\n" "version -- Print version.\n" "copyright -- Print copyright.\n" @@ -33,6 +34,8 @@ void console(void) { console_help(); } else if (strcmp(prompt, "exit") == 0) { break; + } else if (strcmp(prompt, "clear") == 0) { + reset_term(); } else if (strcmp(prompt, "editor") == 0) { char *new_entry = config_entry_editor("New Entry", ""); if (new_entry != NULL) { From 77ed9d23ca0260a3b63f1e0404a1ccebacf3fbee Mon Sep 17 00:00:00 2001 From: czapek1337 Date: Sat, 29 Jan 2022 07:52:44 +0100 Subject: [PATCH 6/7] console: Use escape sequence instead of reset_term() --- stage23/console.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stage23/console.c b/stage23/console.c index f810d196..46fb14ad 100644 --- a/stage23/console.c +++ b/stage23/console.c @@ -35,7 +35,7 @@ void console(void) { } else if (strcmp(prompt, "exit") == 0) { break; } else if (strcmp(prompt, "clear") == 0) { - reset_term(); + print("\e[2J\e[H"); } else if (strcmp(prompt, "editor") == 0) { char *new_entry = config_entry_editor("New Entry", ""); if (new_entry != NULL) { From a6f6958b41e97590b84b9f221d380c34ef804bd8 Mon Sep 17 00:00:00 2001 From: czapek1337 Date: Sat, 29 Jan 2022 07:54:49 +0100 Subject: [PATCH 7/7] readline: Don't delete input beyond a null terminator --- stage23/lib/readline.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stage23/lib/readline.c b/stage23/lib/readline.c index 28673ab3..f13347b7 100644 --- a/stage23/lib/readline.c +++ b/stage23/lib/readline.c @@ -272,7 +272,6 @@ void readline(const char *orig_str, char *buf, size_t limit) { term_autoflush = false; size_t orig_str_len = strlen(orig_str); - memset(buf, 0, limit); memmove(buf, orig_str, orig_str_len); buf[orig_str_len] = 0; @@ -303,6 +302,9 @@ void readline(const char *orig_str, char *buf, size_t limit) { cursor_back(); case GETCHAR_DELETE:; size_t j; + if (buf[i] == 0) { + continue; + } for (j = i; ; j++) { buf[j] = buf[j+1]; if (!buf[j]) {