From b369e07e3c59f6b046d3e0f74803fe2f6a764f04 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Wed, 5 Mar 2025 02:05:54 -0500 Subject: listamsb: allow giving an output filename. --- listamsb.c | 16 +++++++++++++++- listamsb.rst | 28 ++++++++++++++-------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/listamsb.c b/listamsb.c index a289eca..88c465a 100644 --- a/listamsb.c +++ b/listamsb.c @@ -94,6 +94,8 @@ int printing = 0; FILE *infile; FILE *outfile; +const char *outname = NULL; + void verbose(int level, const char *fmt, ...) { va_list ap; @@ -472,7 +474,7 @@ void unlock_program(void) { void print_help(void) { printf("%s v" VERSION " - detokenize Atari Microsoft BASIC files\n", self); puts("By B. Watson , released under the WTFPL"); - printf("Usage: %s [[-l] | [-a] [-v] [-h] [-i] [-n] [-u] [-t] [-m] [-s] [-r *start,end*]] [file]\n", self); + printf("Usage: %s [[-l] | [-[avhinutms] ... ] [-r *start,end*]] \n", self); puts(" -a: raw ATASCII output"); puts(" -c: check only (no listing)"); puts(" -l: lock or unlock program"); @@ -483,6 +485,7 @@ void print_help(void) { puts(" --version: print version number and exit"); puts(" -i -u -t -m -s: passed to a8cat (try 'a8cat -h')"); puts("file must be a tokenized (SAVEd) AMSB file. if not given, reads from stdin."); + puts("if outfile not given, writes to stdout (via pipe to a8cat)"); } void version(void) { @@ -570,6 +573,17 @@ void parse_args(int argc, char **argv) { os_err("%s: %s", argv[optind], strerror(errno)); } } + + optind++; + if(optind < argc) { + /* we were passed an output file */ + outname = argv[optind]; + if(check_only) + os_err("can't use output filename %s with -c", outname); + if(!freopen(outname, "wb", stdout)) + os_err("%s: %s", outname, strerror(errno)); + verbose(1, "redirected stdout to %s", outname); + } } void open_output() { diff --git a/listamsb.rst b/listamsb.rst index ca1bf72..38b9ab9 100644 --- a/listamsb.rst +++ b/listamsb.rst @@ -11,7 +11,7 @@ List the source of a tokenized Atari Microsoft BASIC program SYNOPSIS ======== -listamsb [**-l**] | [[**-a**\] [**-v**\] [**-h**\] [**-i**\] [**-u**\] [**-t**\] [**-m**\] [**-n**\] [**-s**\] ...] [**-r** *line-range*] [**input-file**\] +listamsb [**-l**] | [[**-a**\] [**-c**] [**-v**\] [**-h**\] [**-i**\] [**-u**\] [**-t**\] [**-m**\] [**-n**\] [**-s**\] ...] [**-r** *line-range*] [**input-file**\] [**output-file**\] DESCRIPTION =========== @@ -29,25 +29,26 @@ disk-based version 1 and the cartridge-based version 2. The language is the same for both versions, so there's no need to specify it. **input-file** must be a tokenized AMSB program. If no **input-file** is given, -input is read from stdin. +input is read from **stdin**. Programs created with *SAVE "filename" LOCK* are autodetected, and will be listed normally. It's also possible to convert a LOCKed program to an unencrypted one, with the **-l** option. -Output is to stdout. Use shell redirection to save the output to a file. +If no **output-file** is given, output is to **stdout**. OPTIONS ======= **-a** - Output raw ATASCII. This option must be used with a pipe or redirection, as - **listamsb** will not write ATASCII to a terminal. **a8cat** is not used, - with this option. + Output raw ATASCII. This option must be used with an + **output-file**, a pipe, or redirection: **listamsb** will not write + ATASCII to a terminal. **a8cat** is not used, with this option. **-c** Check only. No output on stdout. Diagnostics are still printed on stderr, - and the exit status is unchanged. + and the exit status is unchanged. It's an error to give an **output-file** with + this option. **-r** *line-range* Show only part of the listing. *line-range* can be a single line, or @@ -61,20 +62,19 @@ OPTIONS "Lock" or "unlock" the program. Locked programs are created with *SAVE "filename" LOCK*. The "encryption" is a reciprocal cipher: locking and unlocking are the same operation (similar to ROT13). The output will - be the locked or unlocked tokenized program (rather than a listing), - and you must use redirection to save it to a file. Example:: + be the locked or unlocked tokenized program (rather than a listing). - listamsb -l LOCKED.AMB > UNLOCKED.AMB - - **a8cat** is not used with this option. None of the other options - have any effect with **-l**. + Output raw ATASCII. This option must be used with an + **output-file**, a pipe, or redirection: **listamsb** will not write + tokenized BASIC to a terminal. **a8cat** is not used, with this option. + None of the other options have any effect with **-l**. **-n** No empty line. By default, **listamsb** prints an empty line at the start of the output, to match AMSB's LIST command. **-v** - Verbose output, on stderr. May be given twice for extra verbosity, + Verbose output, on **stderr**. May be given twice for extra verbosity, which shows each line number, its offset, length, and end-of-line pointer. **-h**\, **--help** -- cgit v1.2.3