diff --git a/RunCPM/abstraction_vstudio.h b/RunCPM/abstraction_vstudio.h index 7f53eee..fd55ad0 100644 --- a/RunCPM/abstraction_vstudio.h +++ b/RunCPM/abstraction_vstudio.h @@ -486,10 +486,92 @@ uint32 _HardwareIn(const uint32 Port) { /* Host initialization functions */ /*===============================================================================*/ -void _host_init(int argc, char* argv[]) { +#ifdef STREAMIO +static void _abort_if_kbd_eof() { +} +static void _file_failure_exit(char *argv[], char* fmt, char* filename) +{ + fprintf(stderr, "%s: ", argv[0]); + fprintf(stderr, fmt, filename); + if (errno) { + fprintf(stderr, ": %s", strerror(errno)); + } + fprintf(stderr, "\n"); + exit(EXIT_FAILURE); +} + +static void _usage(char *argv[]) { + fprintf(stderr, + "RunCPM - an emulator to run CP/M programs on modern hosts\n" + "usage: %s [-i input_file] [-o output_file] [-s]\n", argv[0]); + fprintf(stderr, + " -i input_file: console input will be read from the file " + "with the\n given name. " + "After input file's EOF, further console input\n will be read " + "from the keyboard.\n"); + fprintf(stderr, + " -o output_file: console output will be written to the file " + "with the\n given name, in addition to the screen.\n"); + fprintf(stderr, + " -s: console input and output is connected directly to " + "stdin and stdout.\n"); +} + +#define SET_OPTARG ++i; if (i >= argc) {++errflg; break;} optarg = argv[i]; + +static void _parse_options(int argc, char *argv[]) { + int errflg = 0; + char *optarg; + for (int i = 1; i < argc && errflg == 0; ++i) { + if (strcmp("-i", argv[i]) == 0) { + /* ++i; + if (i >= argc) { + ++errflg; + break; + } + optarg = argv[i]; */ + SET_OPTARG + streamInputFile = fopen(optarg, "r"); + if (NULL == streamInputFile) { + _file_failure_exit(argv, + "error opening console input file %s", optarg); + } + streamInputActive = TRUE; + continue; + } + if (strcmp("-o", argv[i]) == 0) { + SET_OPTARG + streamOutputFile = fopen(optarg, "w"); + if (NULL == streamOutputFile) { + _file_failure_exit(argv, + "error opening console output file %s", optarg); + } + continue; + } + if (strcmp("-s", argv[i]) == 0) { + streamInputFile = stdin; + streamOutputFile = stdout; + streamInputActive = TRUE; + consoleOutputActive = FALSE; + continue; + } + fprintf(stderr, + "Unrecognized option: '%s'\n", argv[i]); + errflg++; + } + if (errflg) { + _usage(argv); + exit(EXIT_FAILURE); + } } +#endif +void _host_init(int argc, char* argv[]) { +#ifdef STREAMIO + _parse_options(argc, argv); +#endif +} /* Console abstraction functions */ /*===============================================================================*/ @@ -512,11 +594,6 @@ void _console_init(void) { void _console_reset(void) { } -#ifdef STREAMIO -static void _abort_if_kbd_eof() { - -} -#endif /* Implemented by conio.h int _kbhit(void)