kilo.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. abAppend(ab, welcome, welcomelen);
  87. } else {
  88. abAppend(ab, "~", 1);
  89. }
  90. abAppend(ab, "\x1b[K", 3);
  91. if (y < E.screenrows - 1) {
  92. abAppend(ab, "\r\n", 2);
  93. }
  94. }
  95. }
  96. void editorRefreshScreen() {
  97. struct abuf ab = ABUF_INIT;
  98. abAppend(&ab, "\x1b[?25l", 6);
  99. abAppend(&ab, "\x1b[H", 3);
  100. editorDrawRows(&ab);
  101. abAppend(&ab, "\x1b[H", 3);
  102. abAppend(&ab, "\x1b[?25h", 6);
  103. write(STDOUT_FILENO, ab.b, ab.len);
  104. abFree(&ab);
  105. }
  106. /*** input ***/
  107. void editorProcessKeypress() {
  108. char c = editorReadKey();
  109. switch (c) {
  110. case CTRL_KEY('q'):
  111. exit(0);
  112. break;
  113. }
  114. }
  115. /*** init ***/
  116. void initEditor() {
  117. if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
  118. }
  119. int main() {
  120. enableRawMode();
  121. initEditor();
  122. while (1) {
  123. editorRefreshScreen();
  124. editorProcessKeypress();
  125. }
  126. return 0;
  127. }