scroll.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <sys/types.h>
  2. #include <sys/ioctl.h>
  3. #include <sys/select.h>
  4. #include <sys/wait.h>
  5. #include <assert.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <signal.h>
  9. #include <stdarg.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <termios.h>
  14. #include <unistd.h>
  15. #if defined(__linux)
  16. #include <pty.h>
  17. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  18. #include <util.h>
  19. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  20. #include <libutil.h>
  21. #endif
  22. typedef struct Line Line;
  23. struct Line {
  24. Line *next;
  25. Line *prev;
  26. size_t len;
  27. char str[];
  28. };
  29. pid_t child;
  30. int mfd;
  31. struct termios dfl;
  32. struct winsize ws;
  33. Line *lines, *bottom;
  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. void
  75. addline(char *str)
  76. {
  77. size_t len = strchr(str, '\n') - str + 1;
  78. Line *lp = malloc(sizeof(*lp) + len * sizeof(*lp->str));
  79. if (!lp)
  80. die("malloc:");
  81. memcpy(lp->str, str, len);
  82. lp->len = len;
  83. if (lines)
  84. lines->next = lp;
  85. lp->prev = lines;
  86. lp->next = NULL;
  87. bottom = lines = lp;
  88. }
  89. void
  90. scrollup(void)
  91. {
  92. Line *lp;
  93. int rows = ws.ws_row-1;
  94. int cols = ws.ws_col;
  95. if (!bottom || !(bottom = bottom->prev))
  96. return;
  97. for (lp = bottom; lp && rows > 0; lp = lp->prev)
  98. rows -= lp->len / cols + 1;
  99. if (rows < 0) {
  100. write(STDOUT_FILENO, lp->str + -rows * cols, lp->len - -rows * cols);
  101. rows = 0;
  102. lp = lp->next;
  103. }
  104. for (; lp && lp != bottom->next; lp = lp->next)
  105. write(STDOUT_FILENO, lp->str, lp->len);
  106. }
  107. int
  108. main(int argc, char *argv[])
  109. {
  110. if (isatty(STDIN_FILENO) == 0)
  111. die("stdin it not a tty");
  112. if (isatty(STDOUT_FILENO) == 0)
  113. die("stdout it not a tty");
  114. if (argc <= 1)
  115. die("usage: scroll <program>");
  116. if (tcgetattr(STDIN_FILENO, &dfl) == -1)
  117. die("tcgetattr:");
  118. if (atexit(reset))
  119. die("atexit:");
  120. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
  121. die("ioctl:");
  122. child = forkpty(&mfd, NULL, &dfl, &ws);
  123. if (child == -1)
  124. die("forkpty:");
  125. if (child == 0) { /* child */
  126. execvp(argv[1], argv + 1);
  127. perror("execvp");
  128. _exit(127);
  129. }
  130. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  131. die("signal:");
  132. if (signal(SIGWINCH, sigwinch) == SIG_ERR)
  133. die("signal:");
  134. int f;
  135. if ((f = fcntl(mfd, F_GETFL)) == -1)
  136. die("fcntl:");
  137. if (fcntl(mfd, F_SETFL, f /*| O_NONBLOCK*/) == -1)
  138. die("fcntl:");
  139. struct termios new = dfl;
  140. cfmakeraw(&new);
  141. new.c_cc[VMIN ] = 1;
  142. new.c_cc[VTIME] = 0;
  143. if (tcsetattr(STDIN_FILENO, TCSANOW, &new) == -1)
  144. die("tcsetattr:");
  145. fd_set rd;
  146. char buf[10000], *p = buf;
  147. for (;;) {
  148. char c;
  149. FD_ZERO(&rd);
  150. FD_SET(STDIN_FILENO, &rd);
  151. FD_SET(mfd, &rd);
  152. if (select(mfd + 1, &rd, NULL, NULL, NULL) < 0 && errno != EINTR)
  153. die("select:");
  154. if (FD_ISSET(STDIN_FILENO, &rd)) {
  155. if (read(STDIN_FILENO, &c, 1) <= 0 && errno != EINTR)
  156. die("read:");
  157. if (c == 17) /* ^Q */
  158. scrollup();
  159. else if (write(mfd, &c, 1) == -1)
  160. die("write:");
  161. }
  162. if (FD_ISSET(mfd, &rd)) {
  163. if (read(mfd, &c, 1) <= 0 && errno != EINTR)
  164. die("read:");
  165. *p++ = c;
  166. if (c == '\n') {
  167. p = buf;
  168. addline(buf);
  169. }
  170. if (write(STDOUT_FILENO, &c, 1) == -1)
  171. die("write:");
  172. }
  173. }
  174. return 0;
  175. }