scroll.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. } *bottom;
  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, size_t size)
  75. {
  76. struct line *line = malloc(sizeof *line);
  77. if (line == NULL)
  78. die("malloc:");
  79. line->len = size;
  80. line->str = malloc(size);
  81. if (line->str == NULL)
  82. die("malloc:");
  83. memcpy(line->str, str, size);
  84. bottom = line;
  85. TAILQ_INSERT_HEAD(&head, line, entries);
  86. }
  87. void
  88. scrollup(void)
  89. {
  90. struct line *line;
  91. int rows = ws.ws_row-1;
  92. int cols = ws.ws_col;
  93. if (bottom == NULL || (bottom = TAILQ_NEXT(bottom, entries)) == NULL)
  94. return;
  95. /* TODO: save cursor position */
  96. /* scroll one line UP */
  97. //write(STDOUT_FILENO, "\033[1S", 4);
  98. /* scroll one line DOWN */
  99. write(STDOUT_FILENO, "\033[1T", 4);
  100. /* set cursor position */
  101. /* Esc[Line;ColumnH */
  102. write(STDOUT_FILENO, "\033[0;0H", 6);
  103. for (line = bottom; line != NULL && rows > 0; line = TAILQ_NEXT(line, entries)) {
  104. rows -= line->len / cols + 1;
  105. //printf("rows: %d\n", rows);
  106. }
  107. if (line == NULL)
  108. return;
  109. write(STDOUT_FILENO, line->str, line->len);
  110. return;
  111. }
  112. int
  113. main(int argc, char *argv[])
  114. {
  115. TAILQ_INIT(&head);
  116. if (isatty(STDIN_FILENO) == 0)
  117. die("stdin it not a tty");
  118. if (isatty(STDOUT_FILENO) == 0)
  119. die("stdout it not a tty");
  120. if (argc <= 1)
  121. die("usage: scroll <program>");
  122. if (tcgetattr(STDIN_FILENO, &dfl) == -1)
  123. die("tcgetattr:");
  124. if (atexit(reset))
  125. die("atexit:");
  126. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
  127. die("ioctl:");
  128. child = forkpty(&mfd, NULL, &dfl, &ws);
  129. if (child == -1)
  130. die("forkpty:");
  131. if (child == 0) { /* child */
  132. execvp(argv[1], argv + 1);
  133. perror("execvp");
  134. _exit(127);
  135. }
  136. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  137. die("signal:");
  138. if (signal(SIGWINCH, sigwinch) == SIG_ERR)
  139. die("signal:");
  140. int f;
  141. if ((f = fcntl(mfd, F_GETFL)) == -1)
  142. die("fcntl:");
  143. if (fcntl(mfd, F_SETFL, f /*| O_NONBLOCK*/) == -1)
  144. die("fcntl:");
  145. struct termios new = dfl;
  146. cfmakeraw(&new);
  147. new.c_cc[VMIN ] = 1;
  148. new.c_cc[VTIME] = 0;
  149. if (tcsetattr(STDIN_FILENO, TCSANOW, &new) == -1)
  150. die("tcsetattr:");
  151. fd_set rd;
  152. size_t size = BUFSIZ, pos = 0;
  153. char *buf = calloc(size, sizeof *buf);
  154. if (buf == NULL)
  155. die("calloc:");
  156. memset(buf, 0, size);
  157. for (;;) {
  158. char c;
  159. FD_ZERO(&rd);
  160. FD_SET(STDIN_FILENO, &rd);
  161. FD_SET(mfd, &rd);
  162. if (select(mfd + 1, &rd, NULL, NULL, NULL) < 0 && errno != EINTR)
  163. die("select:");
  164. if (FD_ISSET(STDIN_FILENO, &rd)) {
  165. if (read(STDIN_FILENO, &c, 1) <= 0 && errno != EINTR)
  166. die("read:");
  167. if (c == 17) /* ^Q */
  168. scrollup();
  169. else if (write(mfd, &c, 1) == -1)
  170. die("write:");
  171. }
  172. if (FD_ISSET(mfd, &rd)) {
  173. if (read(mfd, &c, 1) <= 0 && errno != EINTR)
  174. die("read:");
  175. buf[pos++] = c;
  176. if (pos == size) {
  177. size *= 2;
  178. buf = realloc(buf, size);
  179. if (buf == NULL)
  180. die("realloc:");
  181. }
  182. if (c == '\n') {
  183. addline(buf, pos);
  184. memset(buf, 0, size);
  185. pos = 0;
  186. }
  187. if (write(STDOUT_FILENO, &c, 1) == -1)
  188. die("write:");
  189. }
  190. }
  191. return 0;
  192. }