scroll.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <sys/types.h>
  2. #include <sys/ioctl.h>
  3. #include <sys/select.h>
  4. #include <sys/wait.h>
  5. #include <sys/queue.h>
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <signal.h>
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <termios.h>
  15. #include <unistd.h>
  16. #if defined(__linux)
  17. #include <pty.h>
  18. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  19. #include <util.h>
  20. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  21. #include <libutil.h>
  22. #endif
  23. TAILQ_HEAD(tailhead, line) head;
  24. struct line {
  25. TAILQ_ENTRY(line) entries;
  26. size_t size;
  27. size_t len;
  28. char *buf;
  29. } *bottom;
  30. pid_t child;
  31. int mfd;
  32. struct termios dfl;
  33. struct winsize ws;
  34. void
  35. die(const char *fmt, ...)
  36. {
  37. va_list ap;
  38. va_start(ap, fmt);
  39. vfprintf(stderr, fmt, ap);
  40. va_end(ap);
  41. if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
  42. fputc(' ', stderr);
  43. perror(NULL);
  44. } else {
  45. fputc('\n', stderr);
  46. }
  47. exit(1);
  48. }
  49. void
  50. sigchld(int sig)
  51. {
  52. assert(sig == SIGCHLD);
  53. pid_t pid;
  54. while ((pid = waitpid(-1, NULL, WNOHANG)) > 0)
  55. if (pid == child)
  56. die("child died");
  57. }
  58. void
  59. sigwinch(int sig)
  60. {
  61. assert(sig == SIGWINCH);
  62. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
  63. die("ioctl:");
  64. if (ioctl(mfd, TIOCSWINSZ, &ws) == -1)
  65. die("ioctl:");
  66. kill(-child, SIGWINCH);
  67. }
  68. void
  69. reset(void)
  70. {
  71. if (tcsetattr(STDIN_FILENO, TCSANOW, &dfl) == -1)
  72. die("tcsetattr:");
  73. }
  74. size_t
  75. strelen(const char *buf, size_t size)
  76. {
  77. enum {CHAR, BREK, ESC} state = CHAR;
  78. size_t len = 0;
  79. for (size_t i = 0; i < size; i++) {
  80. char c = buf[i];
  81. switch (state) {
  82. case CHAR:
  83. if (c == '\033')
  84. state = BREK;
  85. else
  86. len++;
  87. break;
  88. case BREK:
  89. if (c == '[') {
  90. state = ESC;
  91. } else {
  92. state = CHAR;
  93. len++;
  94. }
  95. break;
  96. case ESC:
  97. if (c >= 64 && c <= 126)
  98. state = CHAR;
  99. break;
  100. }
  101. }
  102. return len;
  103. }
  104. void
  105. addline(char *buf, size_t size)
  106. {
  107. struct line *line = malloc(sizeof *line);
  108. if (line == NULL)
  109. die("malloc:");
  110. line->size = size;
  111. line->len = strelen(buf, size);
  112. line->buf = malloc(size);
  113. if (line->buf == NULL)
  114. die("malloc:");
  115. memcpy(line->buf, buf, size);
  116. bottom = line;
  117. TAILQ_INSERT_HEAD(&head, line, entries);
  118. }
  119. void
  120. scrollup(void)
  121. {
  122. struct line *line;
  123. int rows = ws.ws_row-1;
  124. int cols = ws.ws_col;
  125. if (bottom == NULL || (bottom = TAILQ_NEXT(bottom, entries)) == NULL)
  126. return;
  127. /* TODO: save cursor position */
  128. /* scroll one line UP */
  129. //write(STDOUT_FILENO, "\033[1S", 4);
  130. /* scroll one line DOWN */
  131. write(STDOUT_FILENO, "\033[1T", 4);
  132. /* set cursor position */
  133. /* Esc[Line;ColumnH */
  134. write(STDOUT_FILENO, "\033[0;0H", 6);
  135. for (line = bottom; line != NULL && rows > 0; line = TAILQ_NEXT(line, entries)) {
  136. rows -= line->len / cols + 1;
  137. //printf("rows: %d\n", rows);
  138. }
  139. if (line == NULL)
  140. return;
  141. write(STDOUT_FILENO, line->buf, line->size);
  142. return;
  143. }
  144. int
  145. main(int argc, char *argv[])
  146. {
  147. TAILQ_INIT(&head);
  148. if (isatty(STDIN_FILENO) == 0)
  149. die("stdin it not a tty");
  150. if (isatty(STDOUT_FILENO) == 0)
  151. die("stdout it not a tty");
  152. if (argc <= 1)
  153. die("usage: scroll <program>");
  154. if (tcgetattr(STDIN_FILENO, &dfl) == -1)
  155. die("tcgetattr:");
  156. if (atexit(reset))
  157. die("atexit:");
  158. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
  159. die("ioctl:");
  160. child = forkpty(&mfd, NULL, &dfl, &ws);
  161. if (child == -1)
  162. die("forkpty:");
  163. if (child == 0) { /* child */
  164. execvp(argv[1], argv + 1);
  165. perror("execvp");
  166. _exit(127);
  167. }
  168. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  169. die("signal:");
  170. if (signal(SIGWINCH, sigwinch) == SIG_ERR)
  171. die("signal:");
  172. int f;
  173. if ((f = fcntl(mfd, F_GETFL)) == -1)
  174. die("fcntl:");
  175. if (fcntl(mfd, F_SETFL, f /*| O_NONBLOCK*/) == -1)
  176. die("fcntl:");
  177. struct termios new = dfl;
  178. cfmakeraw(&new);
  179. new.c_cc[VMIN ] = 1;
  180. new.c_cc[VTIME] = 0;
  181. if (tcsetattr(STDIN_FILENO, TCSANOW, &new) == -1)
  182. die("tcsetattr:");
  183. fd_set rd;
  184. size_t size = BUFSIZ, pos = 0;
  185. char *buf = calloc(size, sizeof *buf);
  186. if (buf == NULL)
  187. die("calloc:");
  188. for (;;) {
  189. char c;
  190. FD_ZERO(&rd);
  191. FD_SET(STDIN_FILENO, &rd);
  192. FD_SET(mfd, &rd);
  193. if (select(mfd + 1, &rd, NULL, NULL, NULL) < 0 && errno != EINTR)
  194. die("select:");
  195. if (FD_ISSET(STDIN_FILENO, &rd)) {
  196. if (read(STDIN_FILENO, &c, 1) <= 0 && errno != EINTR)
  197. die("read:");
  198. if (c == 17) /* ^Q */
  199. scrollup();
  200. else if (write(mfd, &c, 1) == -1)
  201. die("write:");
  202. }
  203. if (FD_ISSET(mfd, &rd)) {
  204. if (read(mfd, &c, 1) <= 0 && errno != EINTR)
  205. die("read:");
  206. buf[pos++] = c;
  207. if (pos == size) {
  208. size *= 2;
  209. buf = realloc(buf, size);
  210. if (buf == NULL)
  211. die("realloc:");
  212. }
  213. if (c == '\n') {
  214. addline(buf, pos);
  215. memset(buf, 0, size);
  216. pos = 0;
  217. }
  218. if (write(STDOUT_FILENO, &c, 1) == -1)
  219. die("write:");
  220. }
  221. }
  222. return 0;
  223. }