Print error if exit code is not 127

This commit is contained in:
Nikita Ivanov 2022-05-24 05:46:34 +05:00
parent f37e3a1885
commit 82a82e4a61
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
3 changed files with 49 additions and 16 deletions

View File

@ -5,6 +5,8 @@
#include "error.h" #include "error.h"
#include "preview.h" #include "preview.h"
#define FAILED_PREVIEW_EC 127
#define PREVP_SIZE sizeof(Preview *) #define PREVP_SIZE sizeof(Preview *)
static char shell[] = "sh"; static char shell[] = "sh";
@ -111,13 +113,37 @@ static void check_init_previews(void)
} }
} }
#define CMD_ERR_BUF 256
static int run(Preview *p, int *exitcode) static int run(Preview *p, int *exitcode)
{ {
int pipe_fd[2];
ERRCHK_RET(pipe(pipe_fd) == -1, FUNCFAILED("pipe"), ERRNOS);
int fd = STDERR_FILENO;
int *sp_arg[] = { pipe_fd, &fd };
char *args[] = { shell, "-c", p->script, shell, NULL }; char *args[] = { shell, "-c", p->script, shell, NULL };
int ret = spawn(args, NULL, exitcode, spawn_redirect, sp_arg);
int *fds[] = { (int[]){ STDOUT_FILENO, STDERR_FILENO }, NULL }; close(pipe_fd[1]);
return spawn(args, NULL, exitcode, fds); if (*exitcode != FAILED_PREVIEW_EC) {
char buf[CMD_ERR_BUF];
int len;
while ((len = read(pipe_fd[0], buf, CMD_ERR_BUF)) > 0) {
write(STDOUT_FILENO, buf, len);
}
if (len == -1) {
PRINTINTERR(FUNCFAILED("read"), ERRNOS);
ret = ERR;
}
}
close(pipe_fd[0]);
return ret;
} }
#define SET_PENV(n, v) \ #define SET_PENV(n, v) \
@ -152,7 +178,7 @@ run:
} }
ERRCHK_RET_OK(run(p, &exitcode)); ERRCHK_RET_OK(run(p, &exitcode));
if (exitcode == 127) { if (exitcode == FAILED_PREVIEW_EC) {
i++; i++;
goto run; goto run;
} }

27
utils.c
View File

@ -8,16 +8,28 @@
char *program; char *program;
int spawn_redirect(const void *arg)
{
int **fds = (int **)arg;
int *pipe = fds[0];
int *fd = fds[1];
ERRCHK_RET(close(pipe[0]) == -1, FUNCFAILED("close"), ERRNOS);
ERRCHK_RET(dup2(pipe[1], *fd) == -1, FUNCFAILED("dup2"), ERRNOS);
return OK;
}
/* /*
* Call command * Call command
* *
* If cpid is NULL, wait for the command to finish executing; * If cpid is NULL, wait for the command to finish executing;
* otherwise store pid in cpid * otherwise store pid in cpid
* *
* fd is a NULL-terminated array of pairs of file descriptors * cfunc is a function to call when child process is created
* to pass to dup2()
*/ */
int spawn(char *args[], pid_t *cpid, int *exitcode, int *fds[2]) int spawn(char *args[], pid_t *cpid, int *exitcode, int (*cfunc)(const void *),
const void *carg)
{ {
if (exitcode) if (exitcode)
*exitcode = -1; *exitcode = -1;
@ -27,15 +39,8 @@ int spawn(char *args[], pid_t *cpid, int *exitcode, int *fds[2])
/* Child process */ /* Child process */
if (pid == 0) { if (pid == 0) {
if (fds) { if (cfunc && (cfunc(carg) != OK))
while (*fds) {
if (dup2((*fds)[0], (*fds)[1]) == -1) {
print_errorf("dup2() failed: %s", strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
fds = &fds[1];
}
}
execvp(args[0], args); execvp(args[0], args);
PRINTINTERR(FUNCFAILED("exec"), ERRNOS); PRINTINTERR(FUNCFAILED("exec"), ERRNOS);

View File

@ -26,7 +26,9 @@ typedef struct {
extern char *program; extern char *program;
int spawn(char *args[], pid_t *cpid, int *exitcode, int *fds[2]); int spawn_redirect(const void *arg);
int spawn(char *args[], pid_t *cpid, int *exitcode, int (*cfunc)(const void *),
const void *carg);
int strcmpnull(char const *s1, char const *s2); int strcmpnull(char const *s1, char const *s2);