aboutsummaryrefslogtreecommitdiff
path: root/a8xd.c
diff options
context:
space:
mode:
Diffstat (limited to 'a8xd.c')
-rw-r--r--a8xd.c76
1 files changed, 64 insertions, 12 deletions
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);
}