scroll.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //#define _DEFAULT_SOURCE
  2. #include <sys/types.h>
  3. #include <sys/ioctl.h>
  4. #include <sys/select.h>
  5. #include <sys/wait.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. typedef struct Line Line;
  24. struct Line {
  25. Line *next;
  26. Line *prev;
  27. size_t len;
  28. char str[];
  29. };
  30. pid_t child;
  31. int mfd;
  32. struct termios dfl;
  33. struct winsize ws;
  34. Line *lines, *bottom;
  35. void
  36. die(const char *fmt, ...)
  37. {
  38. va_list ap;
  39. va_start(ap, fmt);
  40. vfprintf(stderr, fmt, ap);
  41. va_end(ap);
  42. if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
  43. fputc(' ', stderr);
  44. perror(NULL);
  45. } else {
  46. fputc('\n', stderr);
  47. }
  48. exit(1);
  49. }
  50. void
  51. sigchld(int sig)
  52. {
  53. assert(sig == SIGCHLD);
  54. pid_t pid;
  55. while ((pid = waitpid(-1, NULL, WNOHANG)) > 0)
  56. if (pid == child)
  57. die("child died");
  58. }
  59. void
  60. sigwinch(int sig)
  61. {
  62. assert(sig == SIGWINCH);
  63. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
  64. die("ioctl:");
  65. if (ioctl(mfd, TIOCSWINSZ, &ws) == -1)
  66. die("ioctl:");
  67. kill(-child, SIGWINCH);
  68. }
  69. void
  70. reset(void)
  71. {
  72. if (tcsetattr(STDIN_FILENO, TCSANOW, &dfl) == -1)
  73. die("tcsetattr:");
  74. }
  75. void
  76. addline(char *str)
  77. {
  78. size_t len = strchr(str, '\n') - str + 1;
  79. Line *lp = malloc(sizeof(*lp) + len * sizeof(*lp->str));
  80. if (!lp)
  81. die("malloc:");
  82. memcpy(lp->str, str, len);
  83. lp->len = len;
  84. if (lines)
  85. lines->next = lp;
  86. lp->prev = lines;
  87. lp->next = NULL;
  88. bottom = lines = lp;
  89. }
  90. void
  91. scrollup(void)
  92. {
  93. Line *lp;
  94. int rows = ws.ws_row-1;
  95. int cols = ws.ws_col;
  96. if (!bottom || !(bottom = bottom->prev))
  97. return;
  98. for (lp = bottom; lp && rows > 0; lp = lp->prev)
  99. rows -= lp->len / cols + 1;
  100. if (rows < 0) {
  101. write(STDOUT_FILENO, lp->str + -rows * cols, lp->len - -rows * cols);
  102. rows = 0;
  103. lp = lp->next;
  104. }
  105. for (; lp && lp != bottom->next; lp = lp->next)
  106. write(STDOUT_FILENO, lp->str, lp->len);
  107. }
  108. int
  109. main(int argc, char *argv[])
  110. {
  111. if (isatty(STDIN_FILENO) == 0)
  112. die("stdin it not a tty");
  113. if (isatty(STDOUT_FILENO) == 0)
  114. die("stdout it not a tty");
  115. if (argc <= 1)
  116. die("usage: scroll <program>");
  117. if (tcgetattr(STDIN_FILENO, &dfl) == -1)
  118. die("tcgetattr:");
  119. if (atexit(reset))
  120. die("atexit:");
  121. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
  122. die("ioctl:");
  123. child = forkpty(&mfd, NULL, &dfl, &ws);
  124. if (child == -1)
  125. die("forkpty:");
  126. if (child == 0) { /* child */
  127. execvp(argv[1], argv + 1);
  128. perror("execvp");
  129. _exit(127);
  130. }
  131. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  132. die("signal:");
  133. if (signal(SIGWINCH, sigwinch) == SIG_ERR)
  134. die("signal:");
  135. int f;
  136. if ((f = fcntl(mfd, F_GETFL)) == -1)
  137. die("fcntl:");
  138. if (fcntl(mfd, F_SETFL, f /*| O_NONBLOCK*/) == -1)
  139. die("fcntl:");
  140. struct termios new = dfl;
  141. cfmakeraw(&new);
  142. new.c_cc[VMIN ] = 1;
  143. new.c_cc[VTIME] = 0;
  144. if (tcsetattr(STDIN_FILENO, TCSANOW, &new) == -1)
  145. die("tcsetattr:");
  146. fd_set rd;
  147. char buf[10000], *p = buf;
  148. for (;;) {
  149. char c;
  150. FD_ZERO(&rd);
  151. FD_SET(STDIN_FILENO, &rd);
  152. FD_SET(mfd, &rd);
  153. if (select(mfd + 1, &rd, NULL, NULL, NULL) < 0 && errno != EINTR)
  154. die("select:");
  155. if (FD_ISSET(STDIN_FILENO, &rd)) {
  156. if (read(STDIN_FILENO, &c, 1) <= 0 && errno != EINTR)
  157. die("read:");
  158. if (c == 17) /* ^Q */
  159. scrollup();
  160. else if (write(mfd, &c, 1) == -1)
  161. die("write:");
  162. }
  163. if (FD_ISSET(mfd, &rd)) {
  164. if (read(mfd, &c, 1) <= 0 && errno != EINTR)
  165. die("read:");
  166. *p++ = c;
  167. if (c == '\n') {
  168. p = buf;
  169. addline(buf);
  170. }
  171. if (write(STDOUT_FILENO, &c, 1) == -1)
  172. die("write:");
  173. }
  174. }
  175. return 0;
  176. }