kilo.c 641 B

12345678910111213141516171819202122232425262728293031
  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 disableRawMode() {
  8. tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
  9. }
  10. void enableRawMode() {
  11. tcgetattr(STDIN_FILENO, &orig_termios);
  12. atexit(disableRawMode);
  13. struct termios raw = orig_termios;
  14. raw.c_lflag &= ~(ECHO | ICANON | ISIG);
  15. tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
  16. }
  17. int main() {
  18. enableRawMode();
  19. char c;
  20. while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') {
  21. if (iscntrl(c)) {
  22. printf("%d\n", c);
  23. } else {
  24. printf("%d ('%c')\n", c, c);
  25. }
  26. }
  27. return 0;
  28. }