:: commit 290bcd1c7961d3811baea1d76822951e6a5fa5f6

czapek1337 <czapek1337@gmail.com> — 2022-01-28 22:51

parents: 41835870f7

console,editor: Filter out non-printable characters

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 <stddef.h>
+#include <stdbool.h>
+
+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 <stdbool.h>
 #include <lib/blib.h>
 
+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;
             }
tab: 248 wrap: offon