From ac14dd1755c1408996faed3a19f84570af804b50 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Sun, 15 Dec 2024 15:48:14 -0500 Subject: implement UXD_OPTS and NO_COLOR environment vars, add -n option --- uxd.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 13 deletions(-) (limited to 'uxd.c') diff --git a/uxd.c b/uxd.c index de6501a..3fc0bbb 100644 --- a/uxd.c +++ b/uxd.c @@ -46,6 +46,10 @@ extern int optind; #define BUFSIZ 4096 #endif +#define NO_COLOR "NO_COLOR" +#define ENV_OPTS "UXD_OPTS" +#define MAX_ARGS 64 + /* ANSI colors */ #define BLACK 0 /* don't use (could be the background color) */ #define RED 1 @@ -76,6 +80,18 @@ char right_buf[4096]; int dump_column = 0; int filepos = 0; +/* Unicode control character printable equivalents. For 0, use + the "empty set" symbol. It's a lot more readable than the "nul" + symbol, ␀. Escape, tab, newline, space are what urxvt uses in + its "keycap picture" mode. The rest of there are hard to read at + normal font sizes, but it's still better than using a dot for + everything like xxd does. */ +char * const special_symbols[] = { + "∅", "␁", "␂", "␃", "␄", "␅", "␆", "␇", "␈", "⇥", "↵", "␋", "␌", "␍", "␎", "␏", + "␐", "␑", "␒", "␓", "␔", "␕", "␖", "␗", "␘", "␙", "␚", "⎋", "␜", "␝", "␞", "␟", + "␣", +}; + /* options */ int bold = 0; /* -b */ int hilite_multi = 0; /* -r */ @@ -116,7 +132,7 @@ long parse_number(const char *s) { return strtol(s, NULL, 0); /* TODO: error checking */ } -void parse_options(int argc, char **argv) { +void parse_args(int argc, char **argv) { int opt; if(argc > 1) { @@ -126,8 +142,10 @@ void parse_options(int argc, char **argv) { version(); } - while((opt = my_getopt(argc, argv, "bl:rmo:S:s:uhv")) != -1) { + while((opt = my_getopt(argc, argv, "nbl:rmo:S:s:uhv")) != -1) { switch(opt) { + case 'n': + break; case 'b': bold = 1; break; case 'l': @@ -162,17 +180,52 @@ void parse_options(int argc, char **argv) { open_input(argv[optind]); } -/* Unicode control character printable equivalents. For 0, use - the "empty set" symbol. It's a lot more readable than the "nul" - symbol, ␀. Escape, tab, newline, space are what urxvt uses in - its "keycap picture" mode. The rest of there are hard to read at - normal font sizes, but it's still better than using a dot for - everything like xxd does. */ -char * const special_symbols[] = { - "∅", "␁", "␂", "␃", "␄", "␅", "␆", "␇", "␈", "⇥", "↵", "␋", "␌", "␍", "␎", "␏", - "␐", "␑", "␒", "␓", "␔", "␕", "␖", "␗", "␘", "␙", "␚", "⎋", "␜", "␝", "␞", "␟", - "␣", -}; +/* read options from the environment and the command line, create a + new argv/argc that has all the options from both, with the + environment ones first. */ +void parse_options(int argc, char **argv) { + int nargc; + char **real_argv = argv; + char *nargv[MAX_ARGS + 1]; + char *env, *p; + + if(getenv(NO_COLOR)) + mono = 1; + + env = getenv(ENV_OPTS); + if(!env) { + /* nothing in the env, use regular args as-is */ + parse_args(argc, argv); + return; + } + + nargv[0] = (char *)self; + nargv[1] = env; + nargc = 2; + + for(p = env; *p; p++) { + if(*p == ' ' || *p == '\t') { + *p = '\0'; + if(nargc == MAX_ARGS) break; + nargv[nargc++] = p + 1; + } + } + + argv++; /* skip exe name */ + while(*argv) { + /* have to check for the -n option here */ + if(argv[0][0] == '-' && argv[0][1] == 'n') { + parse_args(argc, real_argv); + return; + } + if(nargc == MAX_ARGS) break; + nargv[nargc++] = *argv; + argv++; + } + + nargv[nargc] = NULL; + parse_args(nargc, nargv); +} char *get_special(unsigned char c) { if(c == 0x7f) return "⌦"; /* tab */ -- cgit v1.2.3