From a7bab29e11080b9e365c814ef8fc3aa6dee80189 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Mon, 18 Oct 2021 14:07:35 -0400 Subject: Add ptee -t to output media copy at an interval --- bin/ptee.c | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) (limited to 'bin/ptee.c') diff --git a/bin/ptee.c b/bin/ptee.c index f2ea9609..8374bd8f 100644 --- a/bin/ptee.c +++ b/bin/ptee.c @@ -15,11 +15,14 @@ */ #include +#include #include +#include #include #include #include #include +#include #include #include #include @@ -40,8 +43,22 @@ static void restoreTerm(void) { tcsetattr(STDIN_FILENO, TCSADRAIN, &saveTerm); } +static void handler(int sig) { + (void)sig; +} + int main(int argc, char *argv[]) { - if (argc < 2) return EX_USAGE; + int timer = 0; + for (int opt; 0 < (opt = getopt(argc, argv, "t:"));) { + switch (opt) { + break; case 't': timer = atoi(optarg); + break; default: return EX_USAGE; + } + } + argc -= optind; + argv += optind; + + if (argc < 1) return EX_USAGE; if (isatty(STDOUT_FILENO)) errx(EX_USAGE, "stdout is not redirected"); int error = tcgetattr(STDIN_FILENO, &saveTerm); @@ -62,10 +79,21 @@ int main(int argc, char *argv[]) { if (pid < 0) err(EX_OSERR, "forkpty"); if (!pid) { - execvp(argv[1], &argv[1]); - err(EX_NOINPUT, "%s", argv[1]); + execvp(argv[0], argv); + err(EX_NOINPUT, "%s", argv[0]); } + if (timer) { + signal(SIGALRM, handler); + struct timeval tv = { + .tv_sec = timer / 1000, + .tv_usec = timer % 1000 * 1000, + }; + struct itimerval itv = { tv, tv }; + setitimer(ITIMER_REAL, &itv, NULL); + } + + char mc[] = "\x1B[10i"; bool stop = false; byte buf[4096]; @@ -73,7 +101,16 @@ int main(int argc, char *argv[]) { { .events = POLLIN, .fd = STDIN_FILENO }, { .events = POLLIN, .fd = pty }, }; - while (0 < poll(fds, 2, -1)) { + for (;;) { + int nfds = poll(fds, 2, -1); + if (nfds < 0 && errno != EINTR) err(EX_IOERR, "poll"); + + if (nfds < 0) { + ssize_t wlen = write(STDOUT_FILENO, mc, sizeof(mc) - 1); + if (wlen < 0) err(EX_IOERR, "write"); + continue; + } + if (fds[0].revents & POLLIN) { ssize_t rlen = read(STDIN_FILENO, buf, sizeof(buf)); if (rlen < 0) err(EX_IOERR, "read"); @@ -84,8 +121,7 @@ int main(int argc, char *argv[]) { } if (rlen == 1 && buf[0] == CTRL('S')) { - char dump[] = "\x1B[10i"; - ssize_t wlen = write(STDOUT_FILENO, dump, sizeof(dump) - 1); + ssize_t wlen = write(STDOUT_FILENO, mc, sizeof(mc) - 1); if (wlen < 0) err(EX_IOERR, "write"); continue; } @@ -112,5 +148,4 @@ int main(int argc, char *argv[]) { if (dead < 0) err(EX_OSERR, "waitpid"); if (dead) return WIFEXITED(status) ? WEXITSTATUS(status) : EX_SOFTWARE; } - err(EX_IOERR, "poll"); } -- cgit 1.4.1