summary refs log tree commit diff
path: root/bin/ptee.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-10-18 14:07:35 -0400
committerJune McEnroe <june@causal.agency>2021-10-18 14:07:35 -0400
commita7bab29e11080b9e365c814ef8fc3aa6dee80189 (patch)
tree286e70343bb8e0096c80c20ea651f50a3c489c0e /bin/ptee.c
parentSwap functions of ^Q and ^S in ptee (diff)
downloadsrc-a7bab29e11080b9e365c814ef8fc3aa6dee80189.tar.gz
src-a7bab29e11080b9e365c814ef8fc3aa6dee80189.zip
Add ptee -t to output media copy at an interval
Diffstat (limited to '')
-rw-r--r--bin/ptee.c49
1 files changed, 42 insertions, 7 deletions
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 <err.h>
+#include <errno.h>
 #include <poll.h>
+#include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/ioctl.h>
+#include <sys/time.h>
 #include <sys/wait.h>
 #include <sysexits.h>
 #include <termios.h>
@@ -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");
 }