From b6e952c80154be7ad8942ea7b139f31af64ac804 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Mon, 18 Oct 2021 14:07:35 -0400 Subject: Add ptee -t to output media copy at an interval --- bin/man1/ptee.1 | 11 +++++++++++ bin/ptee.c | 49 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 7 deletions(-) (limited to 'bin') diff --git a/bin/man1/ptee.1 b/bin/man1/ptee.1 index ba615f27..bb381ecb 100644 --- a/bin/man1/ptee.1 +++ b/bin/man1/ptee.1 @@ -8,6 +8,7 @@ . .Sh SYNOPSIS .Nm +.Op Fl t Ar ms .Ar command ... .Cm > .Ar file @@ -32,6 +33,16 @@ Type .Ic ^Q to toggle writing to standard output. . +.Pp +The arguments are as follows: +.Bl -tag -width Ds +.It Fl t Ar ms +Write a media copy sequence +to standard output every +.Ar ms +milliseconds. +.El +. .Sh SEE ALSO .Xr script 1 . diff --git a/bin/ptee.c b/bin/ptee.c index 335e0e67..52350a21 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 td>Reimplement text macrosJune McEnroe 2022-02-19Factor out input handling to input.cJune McEnroe 2022-02-19Factor out window management to window.cJune McEnroe 2022-02-19Enable -Wmissing-prototypesJune McEnroe 2022-02-19Fix edit.[ch] license notice additional permissionsJune McEnroe 2022-02-19Run line editing testsJune McEnroe 2022-02-18Implement new line editing "library"June McEnroe 2022-02-18Simplify cursor positioning in inputJune McEnroe 2022-02-18Fix M-f orderingJune McEnroe 2022-02-12Move sandman build to scripts/MakefileJune McEnroe 2022-02-12Use compat_readpassphrase.c on LinuxJune McEnroe 2022-02-12Copy RPP defines from oconfigureJune McEnroe