From 9e43df64ff894e8d878b81300b8b36a0bb371701 Mon Sep 17 00:00:00 2001 From: dschmenk Date: Wed, 30 Nov 2016 10:37:05 -0800 Subject: [PATCH] Allow a file to be passed in before going interactive --- src/a2term.c | 101 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 34 deletions(-) diff --git a/src/a2term.c b/src/a2term.c index c282896..3231256 100755 --- a/src/a2term.c +++ b/src/a2term.c @@ -1,5 +1,6 @@ -#include "a2lib.c" +#include #include +#include "a2lib.c" #define RUN 0 #define STOP 1 @@ -16,10 +17,36 @@ int main(int argc, char **argv) fd_set readset, openset; unsigned char iopkt[2]; int state = RUN, cin = 0; - int pifd = a2open(argc > 1 ? argv[1] : "127.0.0.1"); - if (pifd < 0) + int pifd, echofd = -1; + char *a2host = "127.0.0.1"; + if (!isatty(STDIN_FILENO)) + echofd = STDIN_FILENO; + /* + * Parse arguments. + */ + while (argc > 1) { - perror("Unable to connect to Apple II Pi"); + if (argc > 2 && argv[1][0] == '-' && argv[1][1] == 'f') + { + echofd = open(argv[2], O_RDONLY); + argc -= 2; + argv += 2; + } + else if (argv[1][0] != '-') + { + a2host = argv[1]; + argc--; + argv++; + } + else + { + fprintf(stderr, "Invalid option: %s\n", argv[1]); + exit(EXIT_FAILURE); + } + } + if ((pifd = a2open(a2host)) < 0) + { + fprintf(stderr, "Unable to connect to Apple II Pi\n"); exit(EXIT_FAILURE); } /* @@ -27,6 +54,42 @@ int main(int argc, char **argv) */ iopkt[0] = 0x98; write(pifd, iopkt, 1); + if (echofd >= 0) + { + /* + * Echo input file making sure to not drop characters. + */ + while (read(echofd, &cin, 1) == 1) + { + if (cin == '\n') + cin = 0x0D; + cin |= 0x80; + a2cin(pifd, cin); + while (read(pifd, iopkt, 2) == 2) + { + if (iopkt[0] == 0x98) + { + /* + * Echo output. + */ + putchar(iopkt[1] == 0x8D ? '\n' : iopkt[1] & 0x7F); + } + else if (iopkt[0] == 0x9E) + { + if (iopkt[1] == cin) + break; + else + fprintf(stderr, "\nInput character mismatch!\n"); + } + } + } + if (echofd != STDIN_FILENO) + { + close(echofd); + putchar('\n'); + } + fflush(stdout); + } /* * Are we running interactively? */ @@ -156,36 +219,6 @@ int main(int argc, char **argv) } } } - else - { - /* - * Copy STDIN making sure to not drop characters. - */ - while (read(STDIN_FILENO, &cin, 1) == 1) - { - if (cin == '\n') - cin = 0x0D; - cin |= 0x80; - a2cin(pifd, cin); - while (read(pifd, iopkt, 2) == 2) - { - if (iopkt[0] == 0x98) - { - /* - * Echo output. - */ - putchar(iopkt[1] == 0x8D ? '\n' : iopkt[1] & 0x7F); - } - else if (iopkt[0] == 0x9E) - { - if (iopkt[1] == cin) - break; - else - fprintf(stderr, "\nInput character mismatch!\n"); - } - } - } - } tcsetattr(STDIN_FILENO, TCSANOW, &oldtio); a2close(pifd); fprintf(stderr, "\n");