kilo.c 887 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <termios.h>
  5. #include <unistd.h>
  6. struct termios orig_termios;
  7. void die(const char *s) {
  8. perror(s);
  9. exit(1);
  10. }
  11. void disableRawMode() {
  12. tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
  13. }
  14. void enableRawMode() {
  15. tcgetattr(STDIN_FILENO, &orig_termios);
  16. atexit(disableRawMode);
  17. struct termios raw = orig_termios;
  18. raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  19. raw.c_oflag &= ~(OPOST);
  20. raw.c_cflag |= (CS8);
  21. raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  22. tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
  23. raw.c_cc[VMIN] = 0;
  24. raw.c_cc[VTIME] = 1;
  25. }
  26. int main() {
  27. enableRawMode();
  28. while (1) {
  29. char c = '\0';
  30. read(STDIN_FILENO, &c, 1);
  31. if (iscntrl(c)) {
  32. printf("%d\r\n", c);
  33. } else {
  34. printf("%d ('%c')\r\n", c, c);
  35. }
  36. if (c == 'q') break;
  37. }
  38. return 0;
  39. }