repos/msh

simple shell implementation
Commits Files Refs README LICENSE
commit 35216f5faf7650bf850689a08745f01c66417a76
parent 26ea86ba3aca1f176e6cd862385dfea91e991a5d
Author: mjkloeckner <martinjkloeckner@gmail.com>
Date:   Sun, 21 May 2023 22:37:00 -0300

only print "printable" characters

avoid printing non-printable characters, for example control sequences,
escape sequences, etc. which aren't printed anyway but are appended to
the buffer.

Diffstat:
Mmsh.c | 27+++++++++++++++------------
1 file changed, 15 insertions(+), 12 deletions(-)
diff --git a/msh.c b/msh.c
@@ -175,21 +175,24 @@ char *editor_read_line(char *s) {
             }
             s = aux;
         }
-        if (cursor_pos < line_len) {
-            memmove(s + cursor_pos + 1, s + cursor_pos, line_len - cursor_pos);
 
-            putchar(c);
-            s[cursor_pos++] = c;
-            line_len += 1;
+        /* only print printable caracters */
+        if (isprint(c)) {
+            if (cursor_pos < line_len) {
+                memmove(s + cursor_pos + 1, s + cursor_pos, line_len - cursor_pos);
+                putchar(c);
+                s[cursor_pos++] = c;
+                line_len += 1;
 
-            buffer_print_slice(s, cursor_pos, line_len);
+                buffer_print_slice(s, cursor_pos, line_len);
 
-            /* move left the amount buffer_print_slice moved the cursor */
-            printf("\033[%ldD", line_len - cursor_pos);
-        } else {
-            putchar(c);
-            s[line_len++] = c;
-            cursor_pos++;
+                /* move left the amount buffer_print_slice moved the cursor */
+                printf("\033[%ldD", line_len - cursor_pos);
+            } else {
+                putchar(c);
+                s[line_len++] = c;
+                cursor_pos++;
+            }
         }
     }
     printf("\r\n");