From 5c15179a9e2f91a9cacf28192ee0e8074cee18f7 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Tue, 25 Feb 2025 05:02:58 -0500 Subject: listamsb: warn on non-sequential or out-of-range line numbers. --- listamsb.c | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'listamsb.c') diff --git a/listamsb.c b/listamsb.c index a8a3e86..ee0d9a1 100644 --- a/listamsb.c +++ b/listamsb.c @@ -15,6 +15,10 @@ #define BUFSIZ 4096 #endif +#define EOL 0x9b + +#define MAX_LINENO 63999 + const char *self; char pipe_command[BUFSIZ + 1] = { "a8cat" }; @@ -84,7 +88,7 @@ void unknown_token(int lineno, unsigned char byte, int ext) { } int next_line(void) { - int ptr, lineno, was_ff, in_string; + int ptr, lineno, was_ff, in_string, last_lineno = -1; unsigned char byte; /* pointer to last token on the line, offset by whatever MEMLO @@ -97,12 +101,23 @@ int next_line(void) { return -1; } - /* TODO: sanity check line number: it must be < 64000, and greater than - the line before it (if there was one). */ lineno = read_word(); if(verbose) fprintf(stderr, "found line number %d\n", lineno); + if(lineno <= last_lineno) { + fprintf(stderr, "%s: line number out of order (%d <= %d)\n", + self, lineno, last_lineno); + } + + if(lineno > MAX_LINENO) { + fprintf(stderr, "%s: line number out range (%d > %d)\n", + self, lineno, MAX_LINENO); + } + + last_lineno = lineno; + + /* note that AMSB always puts a space after the line number in LIST */ fprintf(outfile, "%d ", lineno); was_ff = 0; @@ -133,6 +148,7 @@ int next_line(void) { if( !(next == 0x9a || next == 0x9b) ) putc(byte, outfile); ungetc(next, infile); + bytes_read--; } else if(byte == '"') { /* strings start but *don't end* with a double-quote */ in_string = 1; @@ -156,7 +172,7 @@ int next_line(void) { } } - putc(0x9b, outfile); + putc(EOL, outfile); return lineno; } @@ -224,7 +240,7 @@ void open_output() { } int main(int argc, char **argv) { - int proglen, lineno; + int proglen, lineno, rv = 0; set_self(argv[0]); @@ -240,16 +256,22 @@ int main(int argc, char **argv) { if(verbose) fprintf(stderr, "line %d read OK\n", lineno); - /* TODO: sanity check proglen vs. file position */ if(verbose) fprintf(stderr, "read %d bytes\n", bytes_read);; - if(need_pclose) pclose(outfile); + if(proglen == (bytes_read - 3)) { + if(verbose) + fprintf(stderr, "file size matches proglen\n"); + } else { + fprintf(stderr, "%s: trailing garbage at EOF\n", self); + } if(unknowns) { fprintf(stderr, "%s: input has %d unknown tokens\n", self, unknowns);; - exit(2); + rv = 2; } - exit(0); + if(need_pclose) pclose(outfile); + + exit(rv); } -- cgit v1.2.3