scroll.c 3.9 KB

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