use switch case for NNN_SCRIPT

This commit is contained in:
Arun Prakash Jana 2018-12-16 08:33:40 +05:30
parent 94ae45b391
commit cc38369540
No known key found for this signature in database
GPG key ID: A75979F35C080412

View file

@ -295,6 +295,7 @@ static char *copier;
static char *editor, *editor_arg; static char *editor, *editor_arg;
static char *pager, *pager_arg; static char *pager, *pager_arg;
static char *shell, *shell_arg; static char *shell, *shell_arg;
static char *runpath;
static blkcnt_t ent_blocks; static blkcnt_t ent_blocks;
static blkcnt_t dir_blocks; static blkcnt_t dir_blocks;
static ulong num_files; static ulong num_files;
@ -2105,8 +2106,8 @@ static bool show_help(char *path)
dprintf(fd, "NNN_COPIER: %s\n", copier); dprintf(fd, "NNN_COPIER: %s\n", copier);
else if (g_cppath[0]) else if (g_cppath[0])
dprintf(fd, "copy file: %s\n", g_cppath); dprintf(fd, "copy file: %s\n", g_cppath);
if (getenv("NNN_SCRIPT")) if (runpath)
dprintf(fd, "NNN_SCRIPT: %s\n", getenv("NNN_SCRIPT")); dprintf(fd, "NNN_SCRIPT: %s\n", runpath);
if (getenv("NNN_SHOW_HIDDEN")) if (getenv("NNN_SHOW_HIDDEN"))
dprintf(fd, "NNN_SHOW_HIDDEN: 1\n"); dprintf(fd, "NNN_SHOW_HIDDEN: 1\n");
if (getenv("NNN_NO_AUTOSELECT")) if (getenv("NNN_NO_AUTOSELECT"))
@ -2695,7 +2696,8 @@ nochange:
if (cfg.runctx != cfg.curctx) if (cfg.runctx != cfg.curctx)
continue; continue;
if (!getenv("NNN_SCRIPT") || strcmp(path, getenv("NNN_SCRIPT")) != 0) /* Must be in script directory to select script */
if (strcmp(path, runpath) != 0)
continue; continue;
mkpath(path, dents[cur].name, newpath, PATH_MAX); mkpath(path, dents[cur].name, newpath, PATH_MAX);
@ -3393,50 +3395,49 @@ nochange:
xstrlcpy(lastname, tmp, NAME_MAX + 1); xstrlcpy(lastname, tmp, NAME_MAX + 1);
goto begin; goto begin;
} }
case SEL_EXEC: case SEL_EXEC: // fallthrough
if (!ndents)
goto nochange; // fallthrough
case SEL_SHELL: // fallthrough case SEL_SHELL: // fallthrough
case SEL_SCRIPT: // fallthrough case SEL_SCRIPT: // fallthrough
case SEL_RUNCMD: case SEL_RUNCMD:
if (sel == SEL_EXEC) { switch (sel) {
case SEL_EXEC:
if (!ndents)
goto nochange;
/* Check if this is a directory */ /* Check if this is a directory */
if (S_ISDIR(dents[cur].mode)) { if (!S_ISREG(dents[cur].mode)) {
printmsg("directory"); printmsg("not a regular file");
goto nochange; goto nochange;
} }
/* Check if file is executable */ /* Check if file is executable */
if (!(dents[cur].mode & 0100)) { if (!(dents[cur].mode & 0100)) {
printmsg("Permission denied"); printmsg("permission denied");
goto nochange; goto nochange;
} }
mkpath(path, dents[cur].name, newpath, PATH_MAX); mkpath(path, dents[cur].name, newpath, PATH_MAX);
spawn(newpath, NULL, NULL, path, F_NORMAL | F_SIGINT); spawn(newpath, NULL, NULL, path, F_NORMAL | F_SIGINT);
} else if (sel == SEL_SCRIPT) { break;
dir = getenv("NNN_SCRIPT"); case SEL_SHELL:
if (!dir) { spawn(shell, shell_arg, NULL, path, F_NORMAL | F_MARKER);
break;
case SEL_SCRIPT:
if (!runpath) {
printmsg("set NNN_SCRIPT"); printmsg("set NNN_SCRIPT");
goto nochange; goto nochange;
} }
if (stat(dir, &sb) == -1) { if (stat(runpath, &sb) == -1) {
printwarn(); printwarn();
goto nochange; goto nochange;
} }
if (!S_ISDIR(sb.st_mode)) { if (S_ISDIR(sb.st_mode)) {
if (ndents)
tmp = dents[cur].name;
else
tmp = NULL;
spawn(shell, dir, tmp, path, F_NORMAL | F_SIGINT);
} else {
cfg.runscript ^= 1; cfg.runscript ^= 1;
if (!cfg.runscript && rundir[0]) { if (!cfg.runscript && rundir[0]) {
/* If reset, switch to original dir */ /* If toggled, switch to original directory */
if (strcmp(path, getenv("NNN_SCRIPT")) == 0) { if (strcmp(path, runpath) == 0) {
xstrlcpy(path, rundir, PATH_MAX); xstrlcpy(path, rundir, PATH_MAX);
xstrlcpy(lastname, runfile, NAME_MAX); xstrlcpy(lastname, runfile, NAME_MAX);
rundir[0] = '\0'; rundir[0] = '\0';
@ -3448,11 +3449,11 @@ nochange:
} }
/* Check if directory is accessible */ /* Check if directory is accessible */
if (!xdiraccess(dir)) if (!xdiraccess(runpath))
goto nochange; goto nochange;
xstrlcpy(rundir, path, PATH_MAX); xstrlcpy(rundir, path, PATH_MAX);
xstrlcpy(path, dir, PATH_MAX); xstrlcpy(path, runpath, PATH_MAX);
if (ndents) if (ndents)
xstrlcpy(runfile, dents[cur].name, NAME_MAX); xstrlcpy(runfile, dents[cur].name, NAME_MAX);
cfg.runctx = cfg.curctx; cfg.runctx = cfg.curctx;
@ -3460,12 +3461,23 @@ nochange:
setdirwatch(); setdirwatch();
goto begin; goto begin;
} }
} else if (sel == SEL_RUNCMD) {
if (S_ISREG(sb.st_mode)) {
if (ndents)
tmp = dents[cur].name;
else
tmp = NULL;
spawn(shell, runpath, tmp, path, F_NORMAL | F_SIGINT);
} else {
printmsg("unsupported file");
goto nochange;
}
break;
default: /* SEL_RUNCMD */
tmp = xreadline(NULL, "> "); tmp = xreadline(NULL, "> ");
if (tmp && tmp[0]) if (tmp && tmp[0])
spawn(shell, "-c", tmp, path, F_NORMAL | F_SIGINT); spawn(shell, "-c", tmp, path, F_NORMAL | F_SIGINT);
} else }
spawn(shell, shell_arg, NULL, path, F_NORMAL | F_MARKER);
/* Continue in navigate-as-you-type mode, if enabled */ /* Continue in navigate-as-you-type mode, if enabled */
if (cfg.filtermode) if (cfg.filtermode)
@ -3705,6 +3717,9 @@ int main(int argc, char *argv[])
shell = xgetenv("SHELL", "sh"); shell = xgetenv("SHELL", "sh");
getprogarg(shell, &shell_arg); getprogarg(shell, &shell_arg);
/* Setup script execution */
runpath = getenv("NNN_SCRIPT");
#ifdef LINUX_INOTIFY #ifdef LINUX_INOTIFY
/* Initialize inotify */ /* Initialize inotify */
inotify_fd = inotify_init1(IN_NONBLOCK); inotify_fd = inotify_init1(IN_NONBLOCK);