diff options
-rw-r--r-- | listamsb.c | 66 |
1 files changed, 23 insertions, 43 deletions
@@ -1,6 +1,7 @@ #include <stdio.h> #include <stdlib.h> #include <unistd.h> +#include <errno.h> #include <string.h> #include <stdarg.h> @@ -116,7 +117,7 @@ void finish(int rv); } VA_FUNC(die, "error", finish(2)); -VA_FUNC(os_err, "error", exit(1)); +VA_FUNC(os_err, "fatal", exit(1)); VA_FUNC(warn, "warning", warnings++); void set_self(const char *argv0) { @@ -147,7 +148,7 @@ void finish(int rv) { verbose(1, "file size matches proglen"); } else { warn("actual program size %d doesn't match program size %d in header,", - self, progsize, proglen); + progsize, proglen); fputs(" ", stderr); if(proglen > progsize) { fputs("AMSB will give #136 ERROR and fail to LOAD this file\n", stderr); @@ -228,7 +229,7 @@ int read_prog_word(void) { void read_header(void) { /* $00 for plain, $01 for SAVE with LOCK */ locked = read_byte(); - if(locked > 1) die("not an AMSB file: first byte not $00 or $01"); + if(locked > 1) die("not an AMSB file: first byte is $%02x, not $00 or $01", locked); if(locked) verbose(1, "program is locked, decrypting"); @@ -237,14 +238,14 @@ void read_header(void) { verbose(1, "proglen == %d (%04x)", proglen, proglen); if(proglen > MAX_PROGLEN) { - die("%s: not an AMSB file: too big (%d bytes), won't fit in Atari memory\n", proglen); + die("not an AMSB file: too big (%d bytes), won't fit in Atari memory", proglen); } if(proglen == EMPTY_PROGLEN) { - warn("program length is 2, no code in file (SAVE after NEW)\n", self); + warn("program length is 2, no code in file (SAVE after NEW)"); } else { if(proglen < MIN_PROGLEN) { - die("%s: not an AMSB file: program size too small (%d). Atari BASIC file?\n", self, proglen); + die("not an AMSB file: program size too small (%d). Atari BASIC file?", proglen); } } @@ -283,33 +284,23 @@ int next_line(void) { printing = (lineno >= startline) && (lineno <= endline); if(ptr < MIN_PTR) { - fprintf(stderr, "%s: line %d: EOL address $%04x too low (<$%04x)\n", - self, lineno, ptr, MIN_PTR); - warnings++; + warn("line %d: EOL address $%04x too low (<$%04x)", lineno, ptr, MIN_PTR); } else if(ptr >= MAX_PTR) { - fprintf(stderr, "%s: line %d: EOL address $%04x too high (>$%04x)\n", - self, lineno, ptr, MAX_PTR); - warnings++; + warn("line %d: EOL address $%04x too high (>$%04x)", lineno, ptr, MAX_PTR); } if(last_ptr != -1) { if(ptr <= last_ptr) { - fprintf(stderr, "%s: line %d: EOL address $%04x <= previous $%04x\n", - self, lineno, ptr, last_ptr); - warnings++; + warn("line %d: EOL address $%04x <= previous $%04x", lineno, ptr, last_ptr); } } if(lineno <= last_lineno) { - fprintf(stderr, "%s: line number out of order (%d <= %d)\n", - self, lineno, last_lineno); - warnings++; + warn("line number out of order (%d <= %d)", lineno, last_lineno); } if(lineno > MAX_LINENO) { - fprintf(stderr, "%s: line number out range (%d > %d)\n", - self, lineno, MAX_LINENO); - warnings++; + warn("line number out range (%d > %d)", lineno, MAX_LINENO); } /* AMSB always prints a blank line when it LISTs, so we do too. */ @@ -395,10 +386,8 @@ int next_line(void) { if(!byte) break; if(byte < 0x20) { /* ATASCII graphics outside of a string */ - fprintf(stderr, "%s: line %d has character %d outside of a string, " - "maybe Atari BASIC?\n", - self, lineno, byte); - warnings++; + warn("line %d has character %d outside of a string, maybe Atari BASIC?", + lineno, byte); } if(printing) fputc(byte, outfile); } @@ -410,8 +399,7 @@ int next_line(void) { if(len > MAX_LINE_LEN) { int hard = len > MAX_LINE_LEN_HARD; - fprintf(stderr, "%s: line %d is %s long (length %d > %d)\n", - self, + warn("line %d is %s long (length %d > %d)", lineno, hard ? "impossibly" : "supiciously", len, @@ -421,9 +409,7 @@ int next_line(void) { if(last_ptr != -1) { int plen = ptr - last_ptr; if(len != plen) { - fprintf(stderr, "%s: line %d: EOL address doesn't match actual line length %d\n", - self, lineno, len); - warnings++; + warn("line %d: EOL address doesn't match actual line length %d", lineno, len); } } @@ -442,6 +428,7 @@ int next_line(void) { void unlock_program(void) { int c; + /* do not convert this to warn() (it ain't a warning) */ fprintf(stderr, "%s: program is %slocked, output will be %slocked\n", self, locked ? "" : "un", locked ? "un" : ""); @@ -488,8 +475,7 @@ void get_line_range(const char *arg) { val *= 10; val += *p - '0'; if(val > MAX_LINENO) { - fprintf(stderr, "invalid line number for -r (range is 0-%d)\n", MAX_LINENO); - exit(1); + os_err("invalid line number for -r (range is 0-%d)", MAX_LINENO); } } else if(*p == ',' || *p == '-') { if(comma) os_err("invalid argument for -r (too many commas)"); @@ -549,18 +535,15 @@ void parse_args(int argc, char **argv) { if(optind >= argc) { if(isatty(fileno(stdin))) { - fprintf(stderr, "%s: can't read binary data from a terminal\n", self); print_help(); - exit(1); + os_err("can't read binary data from a terminal"); } freopen(NULL, "rb", stdin); infile = stdin; } else { infile = fopen(argv[optind], "rb"); if(!infile) { - fprintf(stderr, "%s: ", self); - perror(argv[optind]); - exit(1); + os_err("%s: %s", argv[optind], strerror(errno)); } } } @@ -569,16 +552,13 @@ void open_output() { if(check_only) { outfile = freopen("/dev/null", "wb", stdout); if(!outfile) { - fprintf(stderr, "%s: ", self); - perror("/dev/null"); - exit(1); + os_err("/dev/null: %s", strerror(errno)); } verbose(1, "using /dev/null for output (check_only)"); } else if(raw_output || unlock_mode) { if(isatty(fileno(stdout))) { - fprintf(stderr, "%s: refusing to write %s to a terminal\n", - self, (unlock_mode ? "tokenized BASIC" : "raw ATASCII")); - exit(1); + os_err("refusing to write %s to a terminal", + (unlock_mode ? "tokenized BASIC" : "raw ATASCII")); } outfile = stdout; verbose(1, "using stdout for output"); |