scroll.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /* Count string length w/o ansi esc sequences. */
  75. size_t
  76. strelen(const char *buf, size_t size)
  77. {
  78. enum {CHAR, BREK, ESC} state = CHAR;
  79. size_t len = 0;
  80. for (size_t i = 0; i < size; i++) {
  81. char c = buf[i];
  82. switch (state) {
  83. case CHAR:
  84. if (c == '\033')
  85. state = BREK;
  86. else
  87. len++;
  88. break;
  89. case BREK:
  90. if (c == '[') {
  91. state = ESC;
  92. } else {
  93. state = CHAR;
  94. len++;
  95. }
  96. break;
  97. case ESC:
  98. if (c >= 64 && c <= 126)
  99. state = CHAR;
  100. break;
  101. }
  102. }
  103. return len;
  104. }
  105. void
  106. addline(char *buf, size_t size)
  107. {
  108. struct line *line = malloc(sizeof *line);
  109. if (line == NULL)
  110. die("malloc:");
  111. line->size = size;
  112. line->len = strelen(buf, size);
  113. line->buf = malloc(size);
  114. if (line->buf == NULL)
  115. die("malloc:");
  116. memcpy(line->buf, buf, size);
  117. bottom = line;
  118. TAILQ_INSERT_HEAD(&head, line, entries);
  119. }
  120. void
  121. scrollup(void)
  122. {
  123. int rows;
  124. printf("\033[%dS", ws.ws_row);
  125. fflush(stdout);
  126. /* set cursor position */
  127. write(STDOUT_FILENO, "\033[0;0H", 6);
  128. for (rows = 0;bottom != NULL && rows < 2 * ws.ws_row; rows++) {
  129. bottom = TAILQ_NEXT(bottom, entries);
  130. }
  131. for (; bottom != NULL && rows > ws.ws_row; rows--) {
  132. bottom = TAILQ_PREV(bottom, tailhead, entries);
  133. write(STDOUT_FILENO, bottom->buf, bottom->len);
  134. }
  135. }
  136. int
  137. main(int argc, char *argv[])
  138. {
  139. TAILQ_INIT(&head);
  140. if (isatty(STDIN_FILENO) == 0)
  141. die("stdin it not a tty");
  142. if (isatty(STDOUT_FILENO) == 0)
  143. die("stdout it not a tty");
  144. if (argc <= 1)
  145. die("usage: scroll <program>");
  146. if (tcgetattr(STDIN_FILENO, &dfl) == -1)
  147. die("tcgetattr:");
  148. if (atexit(reset))
  149. die("atexit:");
  150. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
  151. die("ioctl:");
  152. child = forkpty(&mfd, NULL, &dfl, &ws);
  153. if (child == -1)
  154. die("forkpty:");
  155. if (child == 0) { /* child */
  156. execvp(argv[1], argv + 1);
  157. perror("execvp");
  158. _exit(127);
  159. }
  160. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  161. die("signal:");
  162. if (signal(SIGWINCH, sigwinch) == SIG_ERR)
  163. die("signal:");
  164. int f;
  165. if ((f = fcntl(mfd, F_GETFL)) == -1)
  166. die("fcntl:");
  167. if (fcntl(mfd, F_SETFL, f /*| O_NONBLOCK*/) == -1)
  168. die("fcntl:");
  169. struct termios new = dfl;
  170. cfmakeraw(&new);
  171. new.c_cc[VMIN ] = 1;
  172. new.c_cc[VTIME] = 0;
  173. if (tcsetattr(STDIN_FILENO, TCSANOW, &new) == -1)
  174. die("tcsetattr:");
  175. fd_set rd;
  176. size_t size = BUFSIZ, pos = 0;
  177. char *buf = calloc(size, sizeof *buf);
  178. if (buf == NULL)
  179. die("calloc:");
  180. for (;;) {
  181. char c;
  182. FD_ZERO(&rd);
  183. FD_SET(STDIN_FILENO, &rd);
  184. FD_SET(mfd, &rd);
  185. if (select(mfd + 1, &rd, NULL, NULL, NULL) < 0 && errno != EINTR)
  186. die("select:");
  187. if (FD_ISSET(STDIN_FILENO, &rd)) {
  188. if (read(STDIN_FILENO, &c, 1) <= 0 && errno != EINTR)
  189. die("read:");
  190. if (c == 17) /* ^Q */
  191. scrollup();
  192. else if (write(mfd, &c, 1) == -1)
  193. die("write:");
  194. }
  195. if (FD_ISSET(mfd, &rd)) {
  196. if (read(mfd, &c, 1) <= 0 && errno != EINTR)
  197. die("read:");
  198. buf[pos++] = c;
  199. if (pos == size) {
  200. size *= 2;
  201. buf = realloc(buf, size);
  202. if (buf == NULL)
  203. die("realloc:");
  204. }
  205. if (c == '\n') {
  206. addline(buf, pos);
  207. memset(buf, 0, size);
  208. pos = 0;
  209. }
  210. if (write(STDOUT_FILENO, &c, 1) == -1)
  211. die("write:");
  212. }
  213. }
  214. return 0;
  215. }