aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2024-12-15 16:24:26 -0500
committerB. Watson <urchlay@slackware.uk>2024-12-15 16:24:26 -0500
commit49d5ffdd4abe4e0425f60cf1f40aec5d157bfe50 (patch)
tree3d217c0788a40db00beae885c6016c64004bae1c
parentac14dd1755c1408996faed3a19f84570af804b50 (diff)
downloaduxd-49d5ffdd4abe4e0425f60cf1f40aec5d157bfe50.tar.gz
implement -c (colors) option.
-rw-r--r--uxd.11
-rw-r--r--uxd.c46
-rw-r--r--uxd.rst1
3 files changed, 40 insertions, 8 deletions
diff --git a/uxd.1 b/uxd.1
index 4d102b5..493002f 100644
--- a/uxd.1
+++ b/uxd.1
@@ -72,7 +72,6 @@ normal characters, the 3rd digit (optional) is the color for non\-printable
and space characters, and the 4th (optional) is for invalid UTF\-8
sequences. Default: \fB2351\fP\&. This option also disables a prior \fB\-m\fP
option.
-NOT IMPLEMENTED YET.
.TP
.B \-h\fP,\fB \-\-help
Print built\-in usage message and exit.
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 {
diff --git a/uxd.rst b/uxd.rst
index db230ab..2acd120 100644
--- a/uxd.rst
+++ b/uxd.rst
@@ -61,7 +61,6 @@ by itself.
and space characters, and the 4th (optional) is for invalid UTF-8
sequences. Default: **2351**. This option also disables a prior **-m**
option.
- NOT IMPLEMENTED YET.
-h, --help
Print built-in usage message and exit.