console,editor: Filter out non-printable characters

This commit is contained in:
czapek1337 2022-01-28 23:51:34 +01:00
parent ca5f7a6e46
commit 0c677d093e
4 changed files with 20 additions and 6 deletions

View File

@ -2,6 +2,10 @@
#define __LIB__LIBC_H__ #define __LIB__LIBC_H__
#include <stddef.h> #include <stddef.h>
#include <stdbool.h>
bool isprint(int c);
bool isspace(int c);
int toupper(int c); int toupper(int c);
int tolower(int c); int tolower(int c);

View File

@ -4,6 +4,14 @@
#include <stdbool.h> #include <stdbool.h>
#include <lib/blib.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) { int toupper(int c) {
if (c >= 'a' && c <= 'z') { if (c >= 'a' && c <= 'z') {
return c - 0x20; return c - 0x20;

View File

@ -317,7 +317,7 @@ void readline(const char *orig_str, char *buf, size_t limit) {
term_write((uintptr_t)"\n", 1); term_write((uintptr_t)"\n", 1);
goto out; goto out;
default: { default: {
if (strlen(buf) < limit - 1) { if (strlen(buf) < limit - 1 && isprint(c)) {
for (size_t j = strlen(buf); ; j--) { for (size_t j = strlen(buf); ; j--) {
buf[j+1] = buf[j]; buf[j+1] = buf[j];
if (j == i) if (j == i)

View File

@ -463,12 +463,14 @@ refresh:
return NULL; return NULL;
default: default:
if (strlen(buffer) < EDITOR_MAX_BUFFER_SIZE - 1) { if (strlen(buffer) < EDITOR_MAX_BUFFER_SIZE - 1) {
for (size_t i = strlen(buffer); ; i--) { if (isprint(c) || c == '\n') {
buffer[i+1] = buffer[i]; for (size_t i = strlen(buffer); ; i--) {
if (i == cursor_offset) buffer[i+1] = buffer[i];
break; if (i == cursor_offset)
break;
}
buffer[cursor_offset++] = c;
} }
buffer[cursor_offset++] = c;
} else { } else {
display_overflow_error = true; display_overflow_error = true;
} }