From 734994ddaf7522f9d763cacc0f7cde63b9c7188a Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Mon, 23 Dec 2024 14:50:24 -0500 Subject: improve -c parsing and doc --- usage.c | 2 +- uxd.1 | 12 ++++++++---- uxd.c | 46 ++++++++++++++++++++-------------------------- uxd.rst | 12 ++++++++---- 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/usage.c b/usage.c index c3c4b89..e547068 100644 --- a/usage.c +++ b/usage.c @@ -2,7 +2,7 @@ char *usage_opts[] = { " --: no more options.", " -1: don't alternate colors.", " -b: bold color output.", - " -c nnnnn: colors (2 to 5 digits, 0 to 7).", + " -c nnnnn: colors (1 to 5 digits, 0 to 7).", " -d data: dump this data instead of a file.", " -h, --help: print this help message.", " -i: print number of bytes/chars/ascii/multibyte/bad sequences.", diff --git a/uxd.1 b/uxd.1 index 049d649..d891194 100644 --- a/uxd.1 +++ b/uxd.1 @@ -93,7 +93,7 @@ terminal and its color settings. Ignored if \fB\-m\fP given. .INDENT 0.0 .TP .BI \-c \ nnnnn -Set the colors to use. Must be 2 to 5 digits, from 0 to 7. These are +Set the colors to use. Must be 1 to 5 digits, from 0 to 7. These are standard ANSI colors: .INDENT 7.0 .TP @@ -125,15 +125,19 @@ white The first 2 digits are the alternating colors for normal characters, the 3rd and 4th (optional) are the alternating colors for non\-printable and space characters, and the 5th (optional) is for invalid UTF\-8 -sequences. Default: \fB23561\fP\&. This option also disables a prior \fB\-m\fP -option. +sequences. +.sp +Default colors are \fB23561\fP\&. If fewer than 5 colors are supplied, +the remaining colors keep their default values. .sp Note that the default color set doesn\(aqt include white or black: usually one of these is the terminal\(aqs background color. Also, it avoids blue, because blue text is hard to read on many terminals. +.sp +This option also disables a prior \fB\-m\fP option. .UNINDENT -.\" colors (2 to 5 digits, 0 to 7). +.\" colors (1 to 5 digits, 0 to 7). . .INDENT 0.0 .TP diff --git a/uxd.c b/uxd.c index a91520c..109cbf8 100644 --- a/uxd.c +++ b/uxd.c @@ -204,36 +204,30 @@ void color_error(void) { exit(1); } -void check_color(char c) { +int num_to_color(char c) { if(c < '0' || c > '7') color_error(); + return c - '0'; } 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_colors[0] = arg[2] - '0'; - - /* optional 4th color */ - if(!arg[3]) return; - check_color(arg[3]); - special_colors[1] = arg[3] - '0'; - - /* optional 5th color */ - if(!arg[4]) return; - check_color(arg[4]); - bad_color = arg[4] - '0'; - - if(arg[5]) color_error(); + static int *colors[] = { + &normal_colors[0], + &normal_colors[1], + &special_colors[0], + &special_colors[1], + &bad_color + }; + int i, c; + + i = strlen(arg); + if(i < 1 || i > 5) + color_error(); + + for(i = 0; i < 5; i++) { + c = arg[i]; + if(!c) break; + *colors[i] = num_to_color(c); + } } void number_err(int opt) { diff --git a/uxd.rst b/uxd.rst index eddbd4f..5ef598b 100644 --- a/uxd.rst +++ b/uxd.rst @@ -76,7 +76,7 @@ as *K*, *M*, and *G* for power-of-10 based (e.g. *1K* is 1000 bytes). .. bold color output. -c nnnnn - Set the colors to use. Must be 2 to 5 digits, from 0 to 7. These are + Set the colors to use. Must be 1 to 5 digits, from 0 to 7. These are standard ANSI colors: 0 @@ -106,15 +106,19 @@ as *K*, *M*, and *G* for power-of-10 based (e.g. *1K* is 1000 bytes). The first 2 digits are the alternating colors for normal characters, the 3rd and 4th (optional) are the alternating colors for non-printable and space characters, and the 5th (optional) is for invalid UTF-8 - sequences. Default: **23561**. This option also disables a prior **-m** - option. + sequences. + + Default colors are **23561**. If fewer than 5 colors are supplied, + the remaining colors keep their default values. Note that the default color set doesn't include white or black: usually one of these is the terminal's background color. Also, it avoids blue, because blue text is hard to read on many terminals. -.. colors (2 to 5 digits, 0 to 7). + This option also disables a prior **-m** option. + +.. colors (1 to 5 digits, 0 to 7). -d data Dump this data, instead of reading from a file or stdin. If *data* -- cgit v1.2.3