From 2969891e182914fbd9818113465016633f5bb1df Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Fri, 28 Feb 2025 18:07:20 -0500 Subject: listamsb: stop using ungetc(). --- listamsb.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/listamsb.c b/listamsb.c index 17ca3fc..6092dfa 100644 --- a/listamsb.c +++ b/listamsb.c @@ -178,17 +178,13 @@ unsigned char unlock_byte(unsigned char b) { /* the "encryption" is the same (process is reversible) */ #define lock_byte(x) unlock_byte(x) +/* read and (if needed) decrypt a byte from the program. */ unsigned char read_prog_byte(void) { unsigned char b = read_byte(); return locked ? unlock_byte(b) : b; } -void unread_prog_byte(unsigned char b) { - if(locked) b = lock_byte(b); - ungetc(b, infile); - bytes_read--; -} - +/* read a word from the program, does not decrypt */ int read_word(void) { int w; @@ -198,6 +194,7 @@ int read_word(void) { return w; } +/* read and (if needed) decrypt a word from the program. */ int read_prog_word(void) { int w; @@ -250,7 +247,7 @@ void unknown_token(unsigned char byte, int ext) { int next_line(void) { static int last_lineno = -1; static int last_ptr = -1; - int ptr, lineno, was_ff, in_string, in_comment, offset, len; + int ptr, lineno, was_ff, was_colon, in_string, in_comment, offset, len; int printing; unsigned char byte; @@ -308,6 +305,7 @@ int next_line(void) { if(printing) fprintf(outfile, "%d ", lineno); was_ff = 0; + was_colon = 0; in_string = 0; in_comment = 0; @@ -333,16 +331,22 @@ int next_line(void) { if(byte == '"') putc(byte, outfile); } } + continue; } else if(in_comment) { /* null byte ends both the comment and the line of code. */ if(byte == 0x00) break; if(printing) putc(byte, outfile); - } else if(byte == ':') { + continue; + } + + if(was_colon && byte != TOK_SQUOTE && byte != TOK_BANG) { + if(printing) putc(':', outfile); + was_colon = 0; + } + + if(byte == ':') { /* don't print the colon if the next token is a ! or ' for a comment */ - unsigned char next = read_prog_byte(); - if( !(next == TOK_SQUOTE || next == TOK_BANG) ) - if(printing) putc(byte, outfile); - unread_prog_byte(next); + was_colon = 1; } else if(byte == '"') { /* strings start but *don't end* with a double-quote */ in_string = 1; -- cgit v1.2.3