kilo.c 3.1 KB

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