From c9e479515fa95dd77b6430331dc53ba2174308a5 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Sat, 29 Jun 2024 20:31:11 -0400 Subject: a8xd: adding new options (WIP). --- a8xd.1 | 13 ++++------- a8xd.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- a8xd.rst | 13 ++++------- 3 files changed, 72 insertions(+), 30 deletions(-) diff --git a/a8xd.1 b/a8xd.1 index 0e73cd6..385983e 100644 --- a/a8xd.1 +++ b/a8xd.1 @@ -73,7 +73,7 @@ Print XL/XE International Character Set conversions instead of ATASCII. .INDENT 0.0 .TP .B \-l \fIlen\fP -Stop after dumping \fIlen\fP bytes. \fIlen\fP may be given in decimal or hex (with +\fB*\fP Stop after dumping \fIlen\fP bytes. \fIlen\fP may be given in decimal or hex (with leading \fI0x\fP or \fI$\fP). .UNINDENT .INDENT 0.0 @@ -81,16 +81,11 @@ leading \fI0x\fP or \fI$\fP). .B \-m Monochrome mode. Disables color, but ATASCII characters with the high bit set are still displayed in inverse. -.TP -.B \-n -\fB*\fP Narrow dump mode. Displays 8 bytes per line. Maximum line length is -less than 40 columns. Use this if your terminal is less than 75 columns -wide. .UNINDENT .INDENT 0.0 .TP .B \-o \fIoffset\fP -\fB*\fP Add \fIoffset\fP to displayed file position. \fIoffset\fP can be given in decimal +Add \fIoffset\fP to displayed file position. \fIoffset\fP can be given in decimal or hex (with leading \fI0x\fP or \fI$\fP). .TP .B \-s \fI[\-]seek\fP @@ -102,7 +97,7 @@ reading from standard input. \fIseek\fP may be given in decimal or hex .INDENT 0.0 .TP .B \-u -\fB*\fP Use uppercase letters; the default is lowercase. +Use uppercase letters for hex digits; the default is lowercase. .TP .B \-v Verbose. Currently, this option is accepted, but it doesn\(aqt do anything yet. @@ -135,7 +130,7 @@ missing are: .IP \(bu 2 \fB\-p\fP (PostScript/continuous dump). .IP \(bu 2 -\fB\-cols\fP (\fBa8xd\fP only supports 8 or 16 column dumps). +\fB\-cols\fP (\fBa8xd\fP only supports 16 column dumps). .IP \(bu 2 \fB\-b\fP (bits mode). .UNINDENT diff --git a/a8xd.c b/a8xd.c index 52a8092..c322624 100644 --- a/a8xd.c +++ b/a8xd.c @@ -11,8 +11,11 @@ const char **table = ata2utf; const char *inverse_on = "\x1b[7m"; const char *inverse_off = "\x1b[0m"; +const char *word_format = "%04x"; +const char *byte_format = "%02x"; -int verbose = 0, color = 1; +int verbose = 0, color = 1, disp_offset = 0, maxlen = 0; +int seek_whence = 0, seekpos = 0, filepos = 0, limit = 0; const char *self; @@ -33,6 +36,50 @@ void print_help(void) { printf("Usage: %s [-v] [file ...]\n", self); } +int parse_num_arg(const char *arg) { + int got; + + if(sscanf(arg, "0x%x", &got) != 1) + if(sscanf(arg, "$%x", &got) != 1) + if(sscanf(arg, "%d", &got) != 1) { + fprintf(stderr, "%s: invalid numeric argument '%s'.", self, arg); + exit(1); + } + + return got; +} + +void parse_seek_arg(const char *arg) { + seek_whence = SEEK_SET; + + if(*arg == '-') { + seek_whence = SEEK_END; + arg++; + } + + seekpos = parse_num_arg(arg); +} + +int parse_offset_arg(const char *arg) { + int got; + + got = parse_num_arg(arg); + if(got < 0) + die("negative arguments to -o (offset) are not supported."); + + return got; +} + +int parse_limit_arg(const char *arg) { + int got; + + got = parse_num_arg(arg); + if(got < 0) + die("negative arguments to -l (limit) are not supported."); + + return got; +} + FILE *open_input(const char *file) { FILE *input; @@ -83,16 +130,17 @@ char *get_color(unsigned char c) { void dump_line(const unsigned char *buf, int len) { char hex[1024], asc[1024]; - static int filepos = 0; int hpos = 0, apos = 0, count = len; memset(hex, 0, sizeof(hex)); memset(asc, 0, sizeof(asc)); - printf("%04x: ", filepos); + printf(word_format, filepos + disp_offset); + fputs(": ", stdout); + while(len) { if(color) hpos += sprintf(hex + hpos, "%s", get_color(*buf)); - hpos += sprintf(hex + hpos, "%02x", *buf); + hpos += sprintf(hex + hpos, byte_format, *buf); if(color) hpos += sprintf(hex + hpos, "%s", color_off); hex[hpos++] = ' '; if(count - len == 7) hex[hpos++] = ' '; @@ -151,22 +199,26 @@ int main(int argc, char **argv) { exit(0); } - while( (opt = getopt(argc, argv, "vim")) != -1) { + while( (opt = getopt(argc, argv, "vimus:o:l")) != -1) { switch(opt) { case 'v': verbose = 1; break; case 'i': table = ics2utf; break; case 'm': color = 0; break; + case 'u': word_format = "%04X"; byte_format = "%02X"; break; + case 's': parse_seek_arg(optarg); break; + case 'o': disp_offset = parse_offset_arg(optarg); break; + case 'l': disp_offset = parse_limit_arg(optarg); break; default: print_help(); exit(1); break; } } - if(optind >= argc) { + if(optind < argc - 1) + die("Only one filename is supported."); + + if(optind >= argc) result = a8xd("-"); - } else { - while(optind < argc) { - result += a8xd(argv[optind]); - optind++; - } - } + else + result = a8xd(argv[optind]); + exit(result); } diff --git a/a8xd.rst b/a8xd.rst index 9d6d00d..ebe3364 100644 --- a/a8xd.rst +++ b/a8xd.rst @@ -47,20 +47,15 @@ Options marked with **\*** are not yet implemented. Print XL/XE International Character Set conversions instead of ATASCII. -l *len* - Stop after dumping *len* bytes. *len* may be given in decimal or hex (with + **\*** Stop after dumping *len* bytes. *len* may be given in decimal or hex (with leading *0x* or *$*). -m Monochrome mode. Disables color, but ATASCII characters with the high bit set are still displayed in inverse. --n - **\*** Narrow dump mode. Displays 8 bytes per line. Maximum line length is - less than 40 columns. Use this if your terminal is less than 75 columns - wide. - -o *offset* - **\*** Add *offset* to displayed file position. *offset* can be given in decimal + Add *offset* to displayed file position. *offset* can be given in decimal or hex (with leading *0x* or *$*). -s *[-]seek* @@ -70,7 +65,7 @@ Options marked with **\*** are not yet implemented. (with leading *0x* or *$*). -u - **\*** Use uppercase letters; the default is lowercase. + Use uppercase letters for hex digits; the default is lowercase. -v Verbose. Currently, this option is accepted, but it doesn't do anything yet. @@ -101,7 +96,7 @@ missing are: - **-p** (PostScript/continuous dump). -- **-cols** (**a8xd** only supports 8 or 16 column dumps). +- **-cols** (**a8xd** only supports 16 column dumps). - **-b** (bits mode). -- cgit v1.2.3