Commit 542c540

mo khan <mo.khan@gmail.com>
2021-01-12 03:05:53
print characters and quit on q
1 parent bb57be5
Changed files (1)
kilo.c
@@ -1,3 +1,36 @@
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <termios.h>
+#include <unistd.h>
+
+struct termios orig_termios;
+
+void disable_raw_mode() {
+  tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
+}
+
+void enable_raw_mode() {
+  tcgetattr(STDIN_FILENO, &orig_termios);
+  atexit(disable_raw_mode);
+
+  struct termios raw = orig_termios;
+  raw.c_lflag &= ~(ECHO | ICANON);
+
+  tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
+}
+
 int main() {
+  enable_raw_mode();
+
+  char c;
+  while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q'){
+    if (iscntrl(c)) {
+      printf("%d\n", c);
+    } else {
+      printf("%d ('%c')\n", c, c);
+    }
+  }
+
   return 0;
 }