Commit 9f1ae97
Changed files (1)
kilo.c
@@ -42,20 +42,40 @@ void enable_raw_mode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr");
}
+char editor_read_key() {
+ int nread;
+ char c;
+ while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
+ if (nread == -1 && errno != EAGAIN) die("read");
+ }
+ return c;
+}
+
+/*** input ***/
+
+void editor_process_keypress() {
+ char c = editor_read_key();
+
+ switch(c) {
+ case CTRL_KEY('q'):
+ exit(0);
+ break;
+ default:
+ if (iscntrl(c))
+ printf("%d\r\n", c);
+ else
+ printf("%d ('%c')\r\n", c, c);
+ break;
+ }
+}
+
/*** 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 == CTRL_KEY('q')) break;
+ editor_process_keypress();
}
return 0;