mirror of
https://github.com/jarun/nnn.git
synced 2025-01-15 13:26:37 +00:00
Revert using UTIL_SH_EXEC
This commit is contained in:
parent
6b94911bc9
commit
8d21e5e832
126
src/nnn.c
126
src/nnn.c
|
@ -4425,92 +4425,106 @@ static void set_smart_ctx(int ctx, char *nextpath, char **path, char *file, char
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Gets only a single line (that's what we need for now) or shows full command output in pager.
|
* This function does one of the following depending on the values of `fdout` and `page`:
|
||||||
* Uses g_buf internally.
|
* 1) fdout == -1 && !page: Write up to CMD_LEN_MAX bytes of command output into g_buf
|
||||||
|
* 2) fdout == -1 && page: Create a temp file, write full command output into it and show in pager.
|
||||||
|
* 3) fdout != -1 && !page: Write full command output into the provided file.
|
||||||
|
* 4) fdout != -1 && page: Don't use! Returns FASLE.
|
||||||
|
*
|
||||||
|
* g_buf is modified only in case 1.
|
||||||
|
* g_tmpfpath is modified only in case 2.
|
||||||
*/
|
*/
|
||||||
static bool get_output(char *file, char *arg1, char *arg2, int fdout, bool multi, bool page)
|
static bool get_output(char *file, char *arg1, char *arg2, int fdout, bool page)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int pipefd[2];
|
int pipefd[2];
|
||||||
int index = 0, flags;
|
int index = 0, flags;
|
||||||
bool ret = FALSE;
|
bool ret = FALSE;
|
||||||
bool tmpfile = ((fdout == -1) && page);
|
bool have_file = fdout != -1;
|
||||||
char *argv[EXEC_ARGS_MAX] = {0};
|
int cmd_in_fd = -1;
|
||||||
char *cmd = NULL;
|
int cmd_out_fd = -1;
|
||||||
int fd = -1;
|
|
||||||
ssize_t len;
|
ssize_t len;
|
||||||
|
|
||||||
if (tmpfile) {
|
/*
|
||||||
|
* In this case the logic of the function dictates that we should write the output of the command
|
||||||
|
* to `fd` and show it in the pager. But since we didn't open the file descriptor we have no right
|
||||||
|
* to close it, the caller must do it. We don't even know the path to pass to the pager and
|
||||||
|
* it's a real hassle to get it. In general this just invites problems so we are blocking it.
|
||||||
|
*/
|
||||||
|
if (have_file && page)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
/* Setup file descriptors for child command */
|
||||||
|
if (!have_file && page) {
|
||||||
|
// Case 2
|
||||||
fdout = create_tmp_file();
|
fdout = create_tmp_file();
|
||||||
if (fdout == -1)
|
if (fdout == -1)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
|
||||||
|
|
||||||
if (multi) {
|
cmd_in_fd = STDIN_FILENO;
|
||||||
cmd = parseargs(file, argv, &index);
|
cmd_out_fd = fdout;
|
||||||
if (!cmd)
|
} else if (have_file) {
|
||||||
return FALSE;
|
// Case 3
|
||||||
} else
|
cmd_in_fd = STDIN_FILENO;
|
||||||
argv[index++] = file;
|
cmd_out_fd = fdout;
|
||||||
|
} else {
|
||||||
|
// Case 1
|
||||||
|
if (pipe(pipefd) == -1)
|
||||||
|
errexit();
|
||||||
|
|
||||||
argv[index] = arg1;
|
for (index = 0; index < 2; ++index) {
|
||||||
argv[++index] = arg2;
|
/* Get previous flags */
|
||||||
|
flags = fcntl(pipefd[index], F_GETFL, 0);
|
||||||
|
|
||||||
if (pipe(pipefd) == -1) {
|
/* Set bit for non-blocking flag */
|
||||||
free(cmd);
|
flags |= O_NONBLOCK;
|
||||||
errexit();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (index = 0; index < 2; ++index) {
|
/* Change flags on fd */
|
||||||
/* Get previous flags */
|
fcntl(pipefd[index], F_SETFL, flags);
|
||||||
flags = fcntl(pipefd[index], F_GETFL, 0);
|
}
|
||||||
|
|
||||||
/* Set bit for non-blocking flag */
|
cmd_in_fd = pipefd[0];
|
||||||
flags |= O_NONBLOCK;
|
cmd_out_fd = pipefd[1];
|
||||||
|
|
||||||
/* Change flags on fd */
|
|
||||||
fcntl(pipefd[index], F_SETFL, flags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
/* In child */
|
/* In child */
|
||||||
close(pipefd[0]);
|
close(cmd_in_fd);
|
||||||
dup2(pipefd[1], STDOUT_FILENO);
|
dup2(cmd_out_fd, STDOUT_FILENO);
|
||||||
dup2(pipefd[1], STDERR_FILENO);
|
dup2(cmd_out_fd, STDERR_FILENO);
|
||||||
close(pipefd[1]);
|
close(cmd_out_fd);
|
||||||
execvp(*argv, argv);
|
|
||||||
|
spawn(file, arg1, arg2, NULL, F_MULTI);
|
||||||
_exit(EXIT_SUCCESS);
|
_exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* In parent */
|
/* In parent */
|
||||||
waitpid(pid, NULL, 0);
|
waitpid(pid, NULL, 0);
|
||||||
close(pipefd[1]);
|
|
||||||
free(cmd);
|
|
||||||
|
|
||||||
while ((len = read(pipefd[0], g_buf, CMD_LEN_MAX - 1)) > 0) {
|
/* Do what each case should do */
|
||||||
ret = TRUE;
|
if (!have_file && page) {
|
||||||
if (fdout == -1) /* Read only the first line of output to buffer */
|
// Case 2
|
||||||
break;
|
close(fdout);
|
||||||
if (write(fdout, g_buf, len) != len)
|
|
||||||
break;
|
spawn(pager, g_tmpfpath, NULL, NULL, F_CLI | F_TTY);
|
||||||
|
|
||||||
|
unlink(g_tmpfpath);
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (have_file)
|
||||||
|
// Case 3
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
|
// Case 1
|
||||||
|
len = read(pipefd[0], g_buf, CMD_LEN_MAX - 1);
|
||||||
|
if (len > 0)
|
||||||
|
ret = TRUE;
|
||||||
|
|
||||||
close(pipefd[0]);
|
close(pipefd[0]);
|
||||||
if (!page)
|
close(pipefd[1]);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
if (tmpfile) {
|
|
||||||
close(fdout);
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
spawn(pager, g_tmpfpath, NULL, NULL, F_CLI | F_TTY);
|
|
||||||
|
|
||||||
if (tmpfile)
|
|
||||||
unlink(g_tmpfpath);
|
|
||||||
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in a new issue