From 58e4b443a53facb9bb69370c35b1f3b3a0215eb8 Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Wed, 24 Apr 2019 20:31:52 +0530 Subject: [PATCH] Check binary config values are 1 --- README.md | 2 +- src/nnn.c | 35 +++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 00467828..4327a397 100644 --- a/README.md +++ b/README.md @@ -375,7 +375,7 @@ The following indicators are used in the detail view: | `NNN_COPIER='/absolute/path/to/copier'` | system clipboard copier script [default: none] | | `NNN_NOTE=/home/user/Dropbox/notes` | path to note file [default: none] | | `NNN_TMPFILE=/tmp/nnn` | file to write current open dir path to for cd on quit | -| `NNN_USE_EDITOR=1` | Open text files in `$EDITOR` (`$VISUAL`, if defined; fallback vi) | +| `NNN_USE_EDITOR=1` | open text files in `$EDITOR` (`$VISUAL`, if defined; fallback vi) | | `NNN_NO_AUTOSELECT=1` | do not auto-select matching dir in _nav-as-you-type_ mode | | `NNN_RESTRICT_NAV_OPEN=1` | open files on , not or l | | `NNN_RESTRICT_0B=1` | do not open 0-byte files | diff --git a/src/nnn.c b/src/nnn.c index c0f351f0..eb361a14 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -1049,6 +1049,17 @@ static char *xgetenv(const char *name, char *fallback) return value && value[0] ? value : fallback; } +/* Checks if an env variable is set to 1 */ +static bool xgetenv_set(const char *name) +{ + char *value = getenv(name); + + if (value && value[0] == 1 && !value[1]) + return TRUE; + + return FALSE; +} + /* Check if a dir exists, IS a dir and is readable */ static bool xdiraccess(const char *path) { @@ -2559,12 +2570,8 @@ static bool show_help(const char *path) for (i = NNN_OPENER; i <= NNN_TRASH; ++i) { start = getenv(env_cfg[i]); - if (start) { - if (i < NNN_USE_EDITOR) + if (start) dprintf(fd, "%s: %s\n", env_cfg[i], start); - else - dprintf(fd, "%s: 1\n", env_cfg[i]); - } } if (g_cppath) @@ -4108,10 +4115,6 @@ static bool setup_config(void) xstrlcpy(cfgdir + r - 1, "/.config/nnn", len - r); DPRINTF_S(cfgdir); - /* TODO: remove in next release */ - if (access(cfgdir, F_OK) == -1) - fprintf(stdout, "WARNING: selection file is ~/.config/nnn/.selection (see CHANGELOG)\n"); - /* Create ~/.config/nnn */ if (!create_dir(cfgdir)) { xerror(); @@ -4284,7 +4287,7 @@ int main(int argc, char *argv[]) /* Get custom opener, if set */ opener = xgetenv(env_cfg[NNN_OPENER], utils[OPENER]); - if (getenv(env_cfg[NNN_OPENER_DETACH])) + if (xgetenv_set(env_cfg[NNN_OPENER_DETACH])) opener_flag |= F_NOWAIT; DPRINTF_S(opener); @@ -4335,7 +4338,7 @@ int main(int argc, char *argv[]) } /* Edit text in EDITOR, if opted */ - if (getenv(env_cfg[NNN_USE_EDITOR])) + if (xgetenv_set(env_cfg[NNN_USE_EDITOR])) cfg.useeditor = 1; /* Get VISUAL/EDITOR */ @@ -4377,7 +4380,7 @@ int main(int argc, char *argv[]) idletimeout = xatoi(getenv(env_cfg[NNN_IDLE_TIMEOUT])); DPRINTF_U(idletimeout); - if (getenv(env_cfg[NNN_TRASH])) + if (xgetenv_set(env_cfg[NNN_TRASH])) cfg.trash = 1; /* Prefix for temporary files */ @@ -4388,19 +4391,19 @@ int main(int argc, char *argv[]) copier = getenv(env_cfg[NNN_COPIER]); /* Disable auto-select if opted */ - if (getenv(env_cfg[NNN_NO_AUTOSELECT])) + if (xgetenv_set(env_cfg[NNN_NO_AUTOSELECT])) cfg.autoselect = 0; /* Disable opening files on right arrow and `l` */ - if (getenv(env_cfg[NNN_RESTRICT_NAV_OPEN])) + if (xgetenv_set(env_cfg[NNN_RESTRICT_NAV_OPEN])) cfg.nonavopen = 1; /* Restrict opening of 0-byte files */ - if (getenv(env_cfg[NNN_RESTRICT_0B])) + if (xgetenv_set(env_cfg[NNN_RESTRICT_0B])) cfg.restrict0b = 1; #ifdef __linux__ - if (!getenv(env_cfg[NNN_OPS_PROG])) { + if (!xgetenv_set(env_cfg[NNN_OPS_PROG])) { cp[5] = cp[4]; cp[2] = cp[4] = ' ';