scroll.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. int rows;
  123. printf("\033[%dS", ws.ws_row);
  124. fflush(stdout);
  125. /* set cursor position */
  126. write(STDOUT_FILENO, "\033[0;0H", 6);
  127. for (rows = 0;bottom != NULL && rows < 2 * ws.ws_row; rows++) {
  128. bottom = TAILQ_NEXT(bottom, entries);
  129. }
  130. for (; bottom != NULL && rows > ws.ws_row; rows--) {
  131. bottom = TAILQ_PREV(bottom, tailhead, entries);
  132. write(STDOUT_FILENO, bottom->buf, bottom->len);
  133. }
  134. }
  135. int
  136. main(int argc, char *argv[])
  137. {
  138. TAILQ_INIT(&head);
  139. if (isatty(STDIN_FILENO) == 0)
  140. die("stdin it not a tty");
  141. if (isatty(STDOUT_FILENO) == 0)
  142. die("stdout it not a tty");
  143. if (argc <= 1)
  144. die("usage: scroll <program>");
  145. if (tcgetattr(STDIN_FILENO, &dfl) == -1)
  146. die("tcgetattr:");
  147. if (atexit(reset))
  148. die("atexit:");
  149. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
  150. die("ioctl:");
  151. child = forkpty(&mfd, NULL, &dfl, &ws);
  152. if (child == -1)
  153. die("forkpty:");
  154. if (child == 0) { /* child */
  155. execvp(argv[1], argv + 1);
  156. perror("execvp");
  157. _exit(127);
  158. }
  159. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  160. die("signal:");
  161. if (signal(SIGWINCH, sigwinch) == SIG_ERR)
  162. die("signal:");
  163. int f;
  164. if ((f = fcntl(mfd, F_GETFL)) == -1)
  165. die("fcntl:");
  166. if (fcntl(mfd, F_SETFL, f /*| O_NONBLOCK*/) == -1)
  167. die("fcntl:");
  168. struct termios new = dfl;
  169. cfmakeraw(&new);
  170. new.c_cc[VMIN ] = 1;
  171. new.c_cc[VTIME] = 0;
  172. if (tcsetattr(STDIN_FILENO, TCSANOW, &new) == -1)
  173. die("tcsetattr:");
  174. fd_set rd;
  175. size_t size = BUFSIZ, pos = 0;
  176. char *buf = calloc(size, sizeof *buf);
  177. if (buf == NULL)
  178. die("calloc:");
  179. for (;;) {
  180. char c;
  181. FD_ZERO(&rd);
  182. FD_SET(STDIN_FILENO, &rd);
  183. FD_SET(mfd, &rd);
  184. if (select(mfd + 1, &rd, NULL, NULL, NULL) < 0 && errno != EINTR)
  185. die("select:");
  186. if (FD_ISSET(STDIN_FILENO, &rd)) {
  187. if (read(STDIN_FILENO, &c, 1) <= 0 && errno != EINTR)
  188. die("read:");
  189. if (c == 17) /* ^Q */
  190. scrollup();
  191. else if (write(mfd, &c, 1) == -1)
  192. die("write:");
  193. }
  194. if (FD_ISSET(mfd, &rd)) {
  195. if (read(mfd, &c, 1) <= 0 && errno != EINTR)
  196. die("read:");
  197. buf[pos++] = c;
  198. if (pos == size) {
  199. size *= 2;
  200. buf = realloc(buf, size);
  201. if (buf == NULL)
  202. die("realloc:");
  203. }
  204. if (c == '\n') {
  205. addline(buf, pos);
  206. memset(buf, 0, size);
  207. pos = 0;
  208. }
  209. if (write(STDOUT_FILENO, &c, 1) == -1)
  210. die("write:");
  211. }
  212. }
  213. return 0;
  214. }