aboutsummaryrefslogtreecommitdiff
path: root/uxd.c
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2024-12-15 15:48:14 -0500
committerB. Watson <urchlay@slackware.uk>2024-12-15 15:48:14 -0500
commitac14dd1755c1408996faed3a19f84570af804b50 (patch)
tree06c659f4087032fd6d2ed5e4d3539d37aebeb66e /uxd.c
parent9e30d05957cef3e621240886746574a35900abf9 (diff)
downloaduxd-ac14dd1755c1408996faed3a19f84570af804b50.tar.gz
implement UXD_OPTS and NO_COLOR environment vars, add -n option
Diffstat (limited to 'uxd.c')
-rw-r--r--uxd.c79
1 files changed, 66 insertions, 13 deletions
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 */