Chapter 2 complete
This commit is contained in:
parent
73cf53b14d
commit
c65168b7cd
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
kilo
|
||||
2
Makefile
Normal file
2
Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
kilo: kilo.c
|
||||
$(CC) kilo.c -o kilo -Wall -Wextra -pedantic -std=c99
|
||||
60
kilo.c
Normal file
60
kilo.c
Normal file
@ -0,0 +1,60 @@
|
||||
/*** includes ***/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
|
||||
/*** data ***/
|
||||
|
||||
struct termios orig_termios;
|
||||
|
||||
/*** terminal ***/
|
||||
|
||||
void die(const char *s) {
|
||||
perror(s);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void disableRawMode() {
|
||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == 1)
|
||||
die("tcsetattr");
|
||||
}
|
||||
|
||||
void enableRawMode() {
|
||||
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1) die("tcgetattr");
|
||||
atexit(disableRawMode);
|
||||
|
||||
struct termios raw = orig_termios;
|
||||
|
||||
raw.c_iflag &= ~(IXON | ICRNL | INPCK | ISTRIP | BRKINT);
|
||||
raw.c_oflag &= ~(OPOST);
|
||||
raw.c_cflag |= (CS8);
|
||||
raw.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
|
||||
raw.c_cc[VMIN] = 0;
|
||||
raw.c_cc[VTIME] = 1;
|
||||
|
||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr");
|
||||
}
|
||||
|
||||
/*** init ***/
|
||||
|
||||
int main() {
|
||||
enableRawMode();
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user