From 282af378f21e609f68d4a3e2dda05b992072580d Mon Sep 17 00:00:00 2001 From: Hadeed Ahmad Date: Wed, 23 Aug 2023 02:32:16 +0500 Subject: [PATCH] Finish chapter 2 --- .gitignore | 2 ++ Makefile | 4 ++++ kilo.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 kilo.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03b7da1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.nvim.lua +kilo diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d262ca6 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +CCFLAGS := -Wall -Wextra -Wpedantic -Werror -std=c99 + +kilo: kilo.c + $(CC) $(CCFLAGS) kilo.c -o kilo diff --git a/kilo.c b/kilo.c new file mode 100644 index 0000000..3af5156 --- /dev/null +++ b/kilo.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include + + +struct termios orig_termios; + + +void die(const char *s) { + perror(s); + exit(1); +} + +void disable_raw_mode() { + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) + die("tcsetattr"); +} + +void enable_raw_mode() { + if (tcgetattr(STDIN_FILENO, &orig_termios) == -1) die("tcgetattr"); + atexit(disable_raw_mode); + + struct termios raw = orig_termios; + raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); + raw.c_oflag &= ~(OPOST); + raw.c_cflag |= (CS8); + raw.c_lflag &= ~(ECHO | ICANON | ISIG); + raw.c_cc[VMIN] = 0; + raw.c_cc[VTIME] = 1; + + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr"); +} + + +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; +}