kilo.c 676 B

1234567891011121314151617181920212223242526272829303132
  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_iflag &= ~(IXON);
  15. raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  16. tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
  17. }
  18. int main() {
  19. enableRawMode();
  20. char c;
  21. while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') {
  22. if (iscntrl(c)) {
  23. printf("%d\n", c);
  24. } else {
  25. printf("%d ('%c')\n", c, c);
  26. }
  27. }
  28. return 0;
  29. }