scroll.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Based on an example code from Roberto E. Vargas Caballero.
  3. *
  4. * Copyright (c) 2020 Jan Klemkow <j.klemkow@wemelug.de>
  5. * Copyright (c) 2020 Jochen Sprickerhof <git@jochen.sprickerhof.de>
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #include <sys/types.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/wait.h>
  22. #include <sys/queue.h>
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <poll.h>
  27. #include <signal.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <termios.h>
  33. #include <unistd.h>
  34. #if defined(__linux)
  35. #include <pty.h>
  36. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  37. #include <util.h>
  38. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  39. #include <libutil.h>
  40. #endif
  41. TAILQ_HEAD(tailhead, line) head;
  42. struct line {
  43. TAILQ_ENTRY(line) entries;
  44. size_t size;
  45. size_t len;
  46. char *buf;
  47. } *bottom;
  48. pid_t child;
  49. int mfd;
  50. struct termios dfl;
  51. struct winsize ws;
  52. void
  53. die(const char *fmt, ...)
  54. {
  55. va_list ap;
  56. va_start(ap, fmt);
  57. vfprintf(stderr, fmt, ap);
  58. va_end(ap);
  59. if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
  60. fputc(' ', stderr);
  61. perror(NULL);
  62. } else {
  63. fputc('\n', stderr);
  64. }
  65. exit(EXIT_FAILURE);
  66. }
  67. void
  68. sigchld(int sig)
  69. {
  70. pid_t pid;
  71. int status;
  72. assert(sig == SIGCHLD);
  73. while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
  74. if (pid == child)
  75. exit(WEXITSTATUS(status));
  76. }
  77. void
  78. sigwinch(int sig)
  79. {
  80. assert(sig == SIGWINCH);
  81. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
  82. die("ioctl:");
  83. if (ioctl(mfd, TIOCSWINSZ, &ws) == -1)
  84. die("ioctl:");
  85. kill(-child, SIGWINCH);
  86. }
  87. void
  88. reset(void)
  89. {
  90. if (tcsetattr(STDIN_FILENO, TCSANOW, &dfl) == -1)
  91. die("tcsetattr:");
  92. }
  93. /* Count string length w/o ansi esc sequences. */
  94. size_t
  95. strelen(const char *buf, size_t size)
  96. {
  97. enum {CHAR, BREK, ESC} state = CHAR;
  98. size_t len = 0;
  99. for (size_t i = 0; i < size; i++) {
  100. char c = buf[i];
  101. switch (state) {
  102. case CHAR:
  103. if (c == '\033')
  104. state = BREK;
  105. else
  106. len++;
  107. break;
  108. case BREK:
  109. if (c == '[') {
  110. state = ESC;
  111. } else {
  112. state = CHAR;
  113. len++;
  114. }
  115. break;
  116. case ESC:
  117. if (c >= 64 && c <= 126)
  118. state = CHAR;
  119. break;
  120. }
  121. }
  122. return len;
  123. }
  124. void
  125. addline(char *buf, size_t size)
  126. {
  127. struct line *line = malloc(sizeof *line);
  128. if (line == NULL)
  129. die("malloc:");
  130. line->size = size;
  131. line->len = strelen(buf, size);
  132. line->buf = malloc(size);
  133. if (line->buf == NULL)
  134. die("malloc:");
  135. memcpy(line->buf, buf, size);
  136. bottom = line;
  137. TAILQ_INSERT_HEAD(&head, line, entries);
  138. }
  139. void
  140. scrollup(void)
  141. {
  142. int rows;
  143. int first = 0;
  144. /* move the text in terminal n lines down */
  145. dprintf(STDOUT_FILENO, "\033[%dS", ws.ws_row);
  146. /* set cursor position */
  147. write(STDOUT_FILENO, "\033[0;0H", 6);
  148. if (TAILQ_FIRST(&head) == bottom)
  149. first = 1;
  150. for (rows = 0; bottom != NULL && rows < 2 * ws.ws_row; rows++)
  151. bottom = TAILQ_NEXT(bottom, entries);
  152. for (; rows > ws.ws_row - first;) {
  153. if ((bottom = TAILQ_PREV(bottom, tailhead, entries)) == NULL)
  154. break;
  155. if (--rows > ws.ws_row - first)
  156. write(STDOUT_FILENO, bottom->buf, bottom->size);
  157. else /* last line w/o "/r/n" */
  158. write(STDOUT_FILENO, bottom->buf, bottom->size - 2);
  159. }
  160. }
  161. int
  162. main(int argc, char *argv[])
  163. {
  164. TAILQ_INIT(&head);
  165. if (isatty(STDIN_FILENO) == 0)
  166. die("stdin it not a tty");
  167. if (isatty(STDOUT_FILENO) == 0)
  168. die("stdout it not a tty");
  169. if (argc <= 1)
  170. die("usage: scroll <program>");
  171. if (tcgetattr(STDIN_FILENO, &dfl) == -1)
  172. die("tcgetattr:");
  173. if (atexit(reset))
  174. die("atexit:");
  175. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
  176. die("ioctl:");
  177. child = forkpty(&mfd, NULL, &dfl, &ws);
  178. if (child == -1)
  179. die("forkpty:");
  180. if (child == 0) { /* child */
  181. execvp(argv[1], argv + 1);
  182. perror("execvp");
  183. _exit(127);
  184. }
  185. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  186. die("signal:");
  187. if (signal(SIGWINCH, sigwinch) == SIG_ERR)
  188. die("signal:");
  189. int f;
  190. if ((f = fcntl(mfd, F_GETFL)) == -1)
  191. die("fcntl:");
  192. if (fcntl(mfd, F_SETFL, f /*| O_NONBLOCK*/) == -1)
  193. die("fcntl:");
  194. struct termios new = dfl;
  195. cfmakeraw(&new);
  196. new.c_cc[VMIN ] = 1;
  197. new.c_cc[VTIME] = 0;
  198. if (tcsetattr(STDIN_FILENO, TCSANOW, &new) == -1)
  199. die("tcsetattr:");
  200. size_t size = BUFSIZ, pos = 0;
  201. char *buf = calloc(size, sizeof *buf);
  202. if (buf == NULL)
  203. die("calloc:");
  204. struct pollfd pfd[2] = {
  205. {STDIN_FILENO, POLLIN, 0},
  206. {mfd, POLLIN, 0}
  207. };
  208. for (;;) {
  209. char c;
  210. if (poll(pfd, 2, 0) == -1 && errno != EINTR)
  211. die("poll:");
  212. if (pfd[0].revents & POLLIN) {
  213. if (read(STDIN_FILENO, &c, 1) <= 0 && errno != EINTR)
  214. die("read:");
  215. if (c == 17) /* ^Q */
  216. scrollup();
  217. else if (write(mfd, &c, 1) == -1)
  218. die("write:");
  219. }
  220. if (pfd[1].revents & POLLIN) {
  221. ssize_t n = read(mfd, &c, 1);
  222. if (n == -1 && errno != EINTR)
  223. die("read:");
  224. buf[pos++] = c;
  225. if (pos == size) {
  226. size *= 2;
  227. buf = realloc(buf, size);
  228. if (buf == NULL)
  229. die("realloc:");
  230. }
  231. if (c == '\n') {
  232. addline(buf, pos);
  233. memset(buf, 0, size);
  234. pos = 0;
  235. }
  236. if (write(STDOUT_FILENO, &c, 1) == -1)
  237. die("write:");
  238. }
  239. }
  240. return EXIT_SUCCESS;
  241. }