From d0ca5c32cd0b3890725df7dd75a807217b8e1810 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Mon, 16 Dec 2024 16:36:12 -0500 Subject: add -1 option, error checking for parse_number() --- uxd.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'uxd.c') diff --git a/uxd.c b/uxd.c index 3aea465..95a91bd 100644 --- a/uxd.c +++ b/uxd.c @@ -66,7 +66,7 @@ int bad_color = RED; int special_color = PURPLE; /* toggles between 0 and 1 for each normal character */ -int cur_normal_hilite = 1; +int cur_normal_hilite = 0; /* highlight types */ #define HL_NORMAL 0 @@ -110,6 +110,7 @@ char * const special_symbols[] = { }; /* options */ +int alternate_colors = 1; /* -1 */ int print_info = 0; /* -i */ int bold = 0; /* -b */ int hilite_multi = 0; /* -r */ @@ -192,8 +193,18 @@ void parse_colors(char *arg) { bad_color = arg[3] - '0'; } -long parse_number(const char *s) { - return strtol(s, NULL, 0); /* TODO: error checking */ +long parse_number(int opt, const char *s) { + char *e; + long result; + + result = strtol(s, &e, 0); + + if(*e) { + fprintf(stderr, "%s: invalid number for -%c option.\n", self, opt); + exit(1); + } + + return result; } void parse_args(int argc, char **argv) { @@ -206,8 +217,10 @@ void parse_args(int argc, char **argv) { version(); } - while((opt = my_getopt(argc, argv, "ic:nbl:rmo:S:s:uhv")) != -1) { + while((opt = my_getopt(argc, argv, "1ic:nbl:rmo:S:s:uhv")) != -1) { switch(opt) { + case '1': + alternate_colors = 0; break; case 'i': print_info = 1; break; case 'c': @@ -217,18 +230,18 @@ void parse_args(int argc, char **argv) { case 'b': bold = 1; break; case 'l': - limit = parse_number(optarg); break; + limit = parse_number(opt, optarg); break; case 'r': hilite_multi = 1; break; case 'm': mono = 1; break; case 'o': - display_offset = parse_number(optarg); break; + display_offset = parse_number(opt, optarg); break; case 'S': seek_offset_zero = 1; /* fall thru */ case 's': - seekpos = parse_number(optarg); + seekpos = parse_number(opt, optarg); break; case 'u': hex_byte_fmt = "%02X"; hex_word_fmt = "%04X: "; break; @@ -330,7 +343,8 @@ void print_line(void) { } void next_normal_hilite() { - cur_normal_hilite = !cur_normal_hilite; + if(alternate_colors) + cur_normal_hilite = !cur_normal_hilite; } void append_color(char *buf, int hl_type) { @@ -557,7 +571,6 @@ int dump_utf8_char(void) { } else { hl_type = HL_NORMAL; printable = (char *)bytes; - next_normal_hilite(); } /* human-readable (right) column: */ @@ -572,6 +585,9 @@ int dump_utf8_char(void) { append_left(bytes[i], (i != cont_count), hl_type); } + if(hl_type == HL_NORMAL || hl_type == HL_NORM_INV) + next_normal_hilite(); + return 1; } -- cgit v1.2.3