aboutsummaryrefslogtreecommitdiffstats
path: root/kilo.c
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2021-01-10 18:36:03 +0100
committerivarlovlie <git@ivarlovlie.no>2021-01-10 18:36:03 +0100
commit8eeb38784aa4ab68c8bfbb9f8e6c5d191e4ac09d (patch)
treee823d35eb058e39a4cab55f6e4b3dfe9a01143ad /kilo.c
downloadkilo-8eeb38784aa4ab68c8bfbb9f8e6c5d191e4ac09d.tar.xz
kilo-8eeb38784aa4ab68c8bfbb9f8e6c5d191e4ac09d.zip
initial commit, done with chapter 2 (raw mode)
Diffstat (limited to 'kilo.c')
-rw-r--r--kilo.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/kilo.c b/kilo.c
new file mode 100644
index 0000000..836fb32
--- /dev/null
+++ b/kilo.c
@@ -0,0 +1,63 @@
+/*** includes ***/
+
+#include <stdlib.h>
+#include <termios.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <errno.h>
+
+/*** data ***/
+
+struct termios original_termios;
+
+/*** terminal ***/
+
+void die(const char *s) {
+ perror(s);
+ exit(1);
+}
+
+void disable_raw_mode() {
+ if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios) == -1) {
+ die("tcsetattr");
+ }
+}
+
+void enable_raw_mode() {
+ if (tcgetattr(STDIN_FILENO, &original_termios) == -1) {
+ die("tcgetattr");
+ }
+ atexit(disable_raw_mode);
+
+ struct termios raw = original_termios;
+ raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
+ raw.c_oflag &= ~(OPOST);
+ raw.c_cflag |= (CS8);
+ raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
+ raw.c_cc[VMIN] = 0;
+ raw.c_cc[VTIME] = 1;
+
+ if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) {
+ die("tcsetattr");
+ }
+}
+
+/*** init ***/
+
+int main() {
+ enable_raw_mode();
+ while (1) {
+ char c = '\0';
+ if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN) {
+ die("read");
+ }
+ if (iscntrl(c)) {
+ printf("%d\r\n", c);
+ } else {
+ printf("%d ('%c')\r\n", c, c);
+ }
+ if (c == 'q') break;
+ }
+ return 0;
+}