scroll.c 3.5 KB

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