kilo.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*** includes ***/
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/ioctl.h>
  7. #include <termios.h>
  8. #include <unistd.h>
  9. /*** defines ***/
  10. #define CTRL_KEY(k) ((k) & 0x1f)
  11. /*** data ***/
  12. struct editorConfig {
  13. int screenrows;
  14. int screencols;
  15. struct termios orig_termios;
  16. };
  17. struct editorConfig E;
  18. /*** terminal ***/
  19. void die(const char *s) {
  20. write(STDOUT_FILENO, "\x1b[2J", 4);
  21. write(STDOUT_FILENO, "\x1b[H", 3);
  22. perror(s);
  23. exit(1);
  24. }
  25. void disableRawMode() {
  26. if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1)
  27. die("tcsetattr");
  28. }
  29. void enableRawMode() {
  30. if (tcgetattr(STDIN_FILENO, &E.orig_termios) == -1) die("tcgetattr");
  31. atexit(disableRawMode);
  32. struct termios raw = E.orig_termios;
  33. raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  34. raw.c_oflag &= ~(OPOST);
  35. raw.c_cflag |= (CS8);
  36. raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  37. raw.c_cc[VMIN] = 1;
  38. raw.c_cc[VTIME] = 0;
  39. if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr");
  40. }
  41. char editorReadKey() {
  42. int nread;
  43. char c;
  44. while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
  45. if (nread == -1 && errno != EAGAIN) die("read");
  46. }
  47. return c;
  48. }
  49. int getCursorPosition(int *rows, int *cols) {
  50. char buf[32];
  51. unsigned int i = 0;
  52. if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4) return -1;
  53. while (i < sizeof(buf) - 1) {
  54. if (read(STDIN_FILENO, &buf[i], 1) != 1) break;
  55. if (buf[i] == 'R') break;
  56. i++;
  57. }
  58. buf[i] = '\0';
  59. if (buf[0] != '\x1b' || buf[1] != '[') return -1;
  60. if (sscanf(&buf[2], "%d;%d", rows, cols) != 2) return -1;
  61. return 0;
  62. }
  63. // int getWindowSize(int *rows, int *cols) {
  64. // struct winsize ws;
  65. // if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
  66. // return -1;
  67. // } else {
  68. // *cols = ws.ws_col;
  69. // *rows = ws.ws_row;
  70. // return 0;
  71. // }
  72. // }
  73. int getWindowSize(int *rows, int *cols) {
  74. struct winsize ws;
  75. if (1 || ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
  76. if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) return -1;
  77. return getCursorPosition(rows, cols);
  78. } else {
  79. *cols = ws.ws_col;
  80. *rows = ws.ws_row;
  81. return 0;
  82. }
  83. }
  84. /*** output ***/
  85. void editorDrawRows() {
  86. int y;
  87. for (y = 0; y < E.screenrows; y++) {
  88. write(STDOUT_FILENO, "~\r\n", 3);
  89. }
  90. }
  91. void editorRefreshScreen() {
  92. write(STDOUT_FILENO, "\x1b[2J", 4);
  93. write(STDOUT_FILENO, "\x1b[H", 3);
  94. editorDrawRows();
  95. write(STDOUT_FILENO, "\x1b[H", 3);
  96. }
  97. /*** input ***/
  98. void editorProcessKeypress() {
  99. char c = editorReadKey();
  100. switch (c) {
  101. case CTRL_KEY('q'):
  102. write(STDOUT_FILENO, "\x1b[2J", 4);
  103. write(STDOUT_FILENO, "\x1b[H", 3);
  104. exit(0);
  105. break;
  106. }
  107. }
  108. /*** init ***/
  109. void initEditor() {
  110. if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
  111. }
  112. int main() {
  113. enableRawMode();
  114. initEditor();
  115. while (1) {
  116. editorRefreshScreen();
  117. editorProcessKeypress();
  118. }
  119. return 0;
  120. }