From 49d5ffdd4abe4e0425f60cf1f40aec5d157bfe50 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Sun, 15 Dec 2024 16:24:26 -0500 Subject: implement -c (colors) option. --- uxd.c | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) (limited to 'uxd.c') diff --git a/uxd.c b/uxd.c index 3fc0bbb..aebc224 100644 --- a/uxd.c +++ b/uxd.c @@ -63,11 +63,13 @@ extern int optind; #define SPECIAL PURPLE #define BAD_FG BLACK -#define BAD_BG RED +#define BAD_BG bad_color -const int normal_colors[] = { GREEN, YELLOW }; +int normal_colors[] = { GREEN, YELLOW }; int cur_normal_color = 0; int dump_color; +int bad_color = RED; +int special_color = SPECIAL; const char *self; FILE *input; @@ -128,6 +130,36 @@ void open_input(const char *arg) { } } +void color_error(void) { + fprintf(stderr, "%s: invalid -c colors (-h for help).\n", self); + exit(1); +} + +void check_color(char c) { + if(c < '0' || c > '7') color_error(); +} + +void parse_colors(char *arg) { + if(!arg[0]) return; /* should never happen anyway */ + + /* first 2 are required */ + check_color(arg[0]); + check_color(arg[1]); + + normal_colors[0] = arg[0] - '0'; + normal_colors[1] = arg[1] - '0'; + + /* optional 3rd color */ + if(!arg[2]) return; + check_color(arg[2]); + special_color = arg[2] - '0'; + + /* optional 4th color */ + if(!arg[3]) return; + check_color(arg[3]); + bad_color = arg[3] - '0'; +} + long parse_number(const char *s) { return strtol(s, NULL, 0); /* TODO: error checking */ } @@ -142,10 +174,12 @@ void parse_args(int argc, char **argv) { version(); } - while((opt = my_getopt(argc, argv, "nbl:rmo:S:s:uhv")) != -1) { + while((opt = my_getopt(argc, argv, "c:nbl:rmo:S:s:uhv")) != -1) { switch(opt) { + case 'c': + mono = 0; parse_colors(optarg); break; case 'n': - break; + break; /* already handled in parse_options() */ case 'b': bold = 1; break; case 'l': @@ -414,11 +448,11 @@ int dump_utf8_char(void) { /* replacement character � is U+FFFD */ printable = "�"; } else if(special) { - fg = SPECIAL; + fg = special_color; bg = 0; printable = get_special(bytes[0]); } else if(cont_count == 2 && is_bom(bytes)) { - fg = SPECIAL; + fg = special_color; bg = 0; printable = "B"; } else { -- cgit v1.2.3