kilo.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. if (y < E.screenrows - 1) {
  82. abAppend(ab, "\r\n", 2);
  83. }
  84. }
  85. }
  86. void editorRefreshScreen() {
  87. struct abuf ab = ABUF_INIT;
  88. abAppend(&ab, "\x1b[2J", 4);
  89. abAppend(&ab, "\x1b[H", 3);
  90. editorDrawRows(&ab);
  91. abAppend(&ab, "\x1b[H", 3);
  92. write(STDOUT_FILENO, ab.b, ab.len);
  93. abFree(&ab);
  94. }
  95. /*** input ***/
  96. void editorProcessKeypress() {
  97. char c = editorReadKey();
  98. switch (c) {
  99. case CTRL_KEY('q'):
  100. exit(0);
  101. break;
  102. }
  103. }
  104. /*** init ***/
  105. void initEditor() {
  106. if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
  107. }
  108. int main() {
  109. enableRawMode();
  110. initEditor();
  111. while (1) {
  112. editorRefreshScreen();
  113. editorProcessKeypress();
  114. }
  115. return 0;
  116. }