aboutsummaryrefslogtreecommitdiff
path: root/listamsb.c
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2025-02-26 15:12:11 -0500
committerB. Watson <urchlay@slackware.uk>2025-02-26 15:12:27 -0500
commit2797f43e4cef6bea12e37208d3425cc72bd5778d (patch)
treed5c239f35da3384baa2b47e68793ee3c45338476 /listamsb.c
parent920ca094b9acd1e2b147cdb323a472a0da65dcc2 (diff)
downloadbw-atari8-tools-2797f43e4cef6bea12e37208d3425cc72bd5778d.tar.gz
listamsb: add -r option; listbas: fix typo in -r error message.
Diffstat (limited to 'listamsb.c')
-rw-r--r--listamsb.c90
1 files changed, 72 insertions, 18 deletions
diff --git a/listamsb.c b/listamsb.c
index c0bf3e8..3111735 100644
--- a/listamsb.c
+++ b/listamsb.c
@@ -49,9 +49,12 @@ const char *self;
char pipe_command[BUFSIZ + 1] = { "a8cat" };
-int verbose = 0; /* -v */
-int raw_output = 0; /* -a */
-int check_only = 0; /* -c */
+int verbose = 0; /* -v */
+int raw_output = 0; /* -a */
+int check_only = 0; /* -c */
+int startline = 0; /* -r */
+int endline = MAX_LINENO; /* -r */
+
int need_pclose = 0;
int bytes_read = 0;
@@ -128,7 +131,6 @@ void read_header(void) {
}
void unknown_token(int lineno, unsigned char byte, int ext) {
- warnings++;
fprintf(outfile, "<unknown %stoken ", (ext ? "extended " : ""));
fprintf(outfile, "%s%02x>", (ext ? "$ff ": ""), byte);
}
@@ -137,6 +139,7 @@ int next_line(void) {
static int last_lineno = -1;
static int last_ptr = -1;
int ptr, lineno, was_ff, in_string, offset, len;
+ int printing;
unsigned char byte;
offset = bytes_read;
@@ -155,6 +158,8 @@ int next_line(void) {
if(verbose)
fprintf(stderr, "found line %d, offset %d, end-of-line %d\n", lineno, offset, ptr);
+ 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);
@@ -188,7 +193,7 @@ int next_line(void) {
last_lineno = lineno;
/* note that AMSB always puts a space after the line number in LIST */
- fprintf(outfile, "%d ", lineno);
+ if(printing) fprintf(outfile, "%d ", lineno);
was_ff = 0;
in_string = 0;
@@ -201,7 +206,7 @@ int next_line(void) {
if(byte == 0x00 || byte == '|') {
/* end of string */
in_string = 0;
- putc('"', outfile);
+ if(printing) putc('"', outfile);
/* if we read a null, that means the line ends with a string
that's missing its closing double-quote. */
@@ -211,31 +216,33 @@ int next_line(void) {
continue;
}
}
- putc(byte, outfile);
+ if(printing) putc(byte, outfile);
} else if(byte == ':') {
/* don't print the colon if the next token is a ! or ' for a comment */
unsigned char next = read_byte();
if( !(next == 0x9a || next == 0x9b) )
- putc(byte, outfile);
+ if(printing) putc(byte, outfile);
ungetc(next, infile);
bytes_read--;
} else if(byte == '"') {
/* strings start but *don't end* with a double-quote */
in_string = 1;
- putc(byte, outfile);
+ if(printing) putc(byte, outfile);
} else if(was_ff) {
if(byte >= MIN_EXT_TOK && byte <= MAX_EXT_TOK) {
- fprintf(outfile, "%s", ext_tokens[byte - MIN_EXT_TOK]);
+ if(printing) fprintf(outfile, "%s", ext_tokens[byte - MIN_EXT_TOK]);
} else {
- unknown_token(lineno, byte, 1);
+ if(printing) unknown_token(lineno, byte, 1);
+ warnings++;
}
was_ff = 0;
} else if(byte == 0xff) {
was_ff = 1;
} else if(byte >= MIN_STD_TOK && byte <= MAX_STD_TOK) {
- fprintf(outfile, "%s", std_tokens[byte - MIN_STD_TOK]);
+ if(printing) fprintf(outfile, "%s", std_tokens[byte - MIN_STD_TOK]);
} else if(byte >= 0x80) {
- unknown_token(lineno, byte, 0);
+ if(printing) unknown_token(lineno, byte, 0);
+ warnings++;
} else {
if(!byte) break;
if(byte < 0x20) {
@@ -245,7 +252,7 @@ int next_line(void) {
self, lineno, byte);
warnings++;
}
- putc(byte, outfile);
+ if(printing) putc(byte, outfile);
}
}
@@ -266,7 +273,7 @@ int next_line(void) {
last_ptr = ptr;
- putc(EOL, outfile);
+ if(printing) putc(EOL, outfile);
return 1;
}
@@ -278,21 +285,69 @@ void print_help(void) {
puts(" -a: raw ATASCII output");
puts(" -c: check only (no listing)");
puts(" -v: verbose");
- puts(" -h: show this help");
+ puts(" --help, -h: print this help and exit");
+ 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.");
}
+void version(void) {
+ printf("%s " VERSION "\n", self);
+}
+
+void get_line_range(const char *arg) {
+ int val = 0, comma = 0;
+ const char *p = arg;
+
+ while(*p) {
+ if(*p >= '0' && *p <= '9') {
+ 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);
+ }
+ } else if(*p == ',' || *p == '-') {
+ if(comma) die("invalid argument for -r (too many commas)");
+ comma++;
+ startline = val;
+ val = 0;
+ } else {
+ if(comma) die("invalid argument for -r (only digits and comma allowed)");
+ }
+ p++;
+ }
+
+ if(comma)
+ endline = val ? val : MAX_LINENO;
+ else
+ startline = endline = val;
+
+ if(endline < startline)
+ die("invalid argument for -r (start > end)");
+}
+
void parse_args(int argc, char **argv) {
char tmp[10];
int opt;
- while( (opt = getopt(argc, argv, "cvaiutmsh")) != -1) {
+ if(argc >= 2) {
+ if(strcmp(argv[1], "--help") == 0) {
+ print_help();
+ exit(0);
+ } else if(strcmp(argv[1], "--version") == 0) {
+ version();
+ exit(0);
+ }
+ }
+
+ while( (opt = getopt(argc, argv, "r:cvaiutmsh")) != -1) {
switch(opt) {
case 'c': check_only = 1; break;
case 'a': raw_output = 1; break;
case 'h': print_help(); exit(0);
case 'v': verbose = 1; break;
+ case 'r': get_line_range(optarg); break;
case 'i':
case 'u':
case 't':
@@ -303,7 +358,6 @@ void parse_args(int argc, char **argv) {
sprintf(tmp, " -%c", opt);
strcat(pipe_command, tmp);
break;
-
default: print_help(); exit(1);
}
}