kilo.c 2.6 KB

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