mirror of
https://github.com/dankamongmen/notcurses.git
synced 2024-11-04 06:00:30 +00:00
[subprocs] factor out ncexecvpe() #2194
This commit is contained in:
parent
a5397ea19a
commit
5553919579
50
src/lib/fd.c
50
src/lib/fd.c
@ -311,9 +311,10 @@ ncsubproc_launch(ncplane* n, ncsubproc* ret, const ncsubproc_options* opts, int
|
|||||||
return ret->nfp;
|
return ret->nfp;
|
||||||
}
|
}
|
||||||
|
|
||||||
ncsubproc* ncsubproc_createv(ncplane* n, const ncsubproc_options* opts,
|
static ncsubproc*
|
||||||
const char* bin, char* const arg[],
|
ncexecvpe(ncplane* n, const ncsubproc_options* opts, unsigned usepath,
|
||||||
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn){
|
const char* bin, char* const arg[],
|
||||||
|
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn){
|
||||||
ncsubproc_options zeroed = {};
|
ncsubproc_options zeroed = {};
|
||||||
if(!opts){
|
if(!opts){
|
||||||
opts = &zeroed;
|
opts = &zeroed;
|
||||||
@ -332,7 +333,11 @@ ncsubproc* ncsubproc_createv(ncplane* n, const ncsubproc_options* opts,
|
|||||||
memset(ret, 0, sizeof(*ret));
|
memset(ret, 0, sizeof(*ret));
|
||||||
ret->pid = launch_pipe_process(&fd, &ret->pidfd);
|
ret->pid = launch_pipe_process(&fd, &ret->pidfd);
|
||||||
if(ret->pid == 0){
|
if(ret->pid == 0){
|
||||||
execv(bin, arg);
|
if(usepath){
|
||||||
|
execvp(bin, arg);
|
||||||
|
}else{
|
||||||
|
execv(bin, arg);
|
||||||
|
}
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}else if(ret->pid < 0){
|
}else if(ret->pid < 0){
|
||||||
free(ret);
|
free(ret);
|
||||||
@ -346,39 +351,16 @@ ncsubproc* ncsubproc_createv(ncplane* n, const ncsubproc_options* opts,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ncsubproc* ncsubproc_createv(ncplane* n, const ncsubproc_options* opts,
|
||||||
|
const char* bin, char* const arg[],
|
||||||
|
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn){
|
||||||
|
return ncexecvpe(n, opts, 0, bin, arg, cbfxn, donecbfxn);
|
||||||
|
}
|
||||||
|
|
||||||
ncsubproc* ncsubproc_createvp(ncplane* n, const ncsubproc_options* opts,
|
ncsubproc* ncsubproc_createvp(ncplane* n, const ncsubproc_options* opts,
|
||||||
const char* bin, char* const arg[],
|
const char* bin, char* const arg[],
|
||||||
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn){
|
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn){
|
||||||
ncsubproc_options zeroed = {};
|
return ncexecvpe(n, opts, 1, bin, arg, cbfxn, donecbfxn);
|
||||||
if(!opts){
|
|
||||||
opts = &zeroed;
|
|
||||||
}
|
|
||||||
if(!cbfxn || !donecbfxn){
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if(opts->flags > 0){
|
|
||||||
logwarn("Provided unsupported flags %016jx\n", (uintmax_t)opts->flags);
|
|
||||||
}
|
|
||||||
int fd = -1;
|
|
||||||
ncsubproc* ret = malloc(sizeof(*ret));
|
|
||||||
if(ret == NULL){
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
memset(ret, 0, sizeof(*ret));
|
|
||||||
ret->pid = launch_pipe_process(&fd, &ret->pidfd);
|
|
||||||
if(ret->pid == 0){
|
|
||||||
execvp(bin, arg);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}else if(ret->pid < 0){
|
|
||||||
free(ret);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if((ret->nfp = ncsubproc_launch(n, ret, opts, fd, cbfxn, donecbfxn)) == NULL){
|
|
||||||
kill_and_wait_subproc(ret->pid, ret->pidfd, NULL);
|
|
||||||
free(ret);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ncsubproc* ncsubproc_createvpe(ncplane* n, const ncsubproc_options* opts,
|
ncsubproc* ncsubproc_createvpe(ncplane* n, const ncsubproc_options* opts,
|
||||||
|
Loading…
Reference in New Issue
Block a user