From 4632b3c1575ec9f04d938f4816c1d97c318750e1 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sun, 2 Aug 2020 16:11:19 -0400 Subject: [PATCH] tools/exec: exec specified file instead of hardcoding on stdin Under OpenBSD, stdin is already used by the device itself because of the whole "stty has no memory" situation. --- tools/exec.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/tools/exec.c b/tools/exec.c index 7622b05..a020ada 100644 --- a/tools/exec.c +++ b/tools/exec.c @@ -2,33 +2,46 @@ #include #include #include +#include #include "common.h" -/* Execute code from stdin on the target machine. +/* Execute code from target file on the target machine. + fname can be "-" for stdin. */ int main(int argc, char **argv) { - if (argc != 2) { - fprintf(stderr, "Usage: ./exec device\n"); + if (argc != 3) { + fprintf(stderr, "Usage: ./exec device fname\n"); return 1; } + FILE *fp = stdin; + if (strcmp(argv[2], "-") != 0) { + fp = fopen(argv[2], "r"); + } int fd = ttyopen(argv[1]); if (fd < 0) { fprintf(stderr, "Could not open %s\n", argv[1]); return 1; } set_blocking(fd, 0); - int c = getchar(); + int c = getc(fp); while (c != EOF) { if (c == '\n') c = '\r'; write(fd, &c, 1); + usleep(1000); // let it breathe while (read(fd, &c, 1) == 1) { putchar(c); fflush(stdout); } - c = getchar(); + c = getc(fp); + } + if (fd > 0) { + close(fd); + } + if (strcmp(argv[2], "-") != 0) { + fclose(fp); } printf("Done!\n"); return 0;