sub-process: add subprocess_start_argv()

Add function to start a subprocess with an argv.

Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
This commit is contained in:
Jeff Hostetler 2019-09-18 10:45:58 -04:00 коммит произвёл Johannes Schindelin
Родитель 407eb741d5
Коммит dda0c94dcc
2 изменённых файлов: 53 добавлений и 0 удалений

Просмотреть файл

@ -4,6 +4,7 @@
#include "sub-process.h"
#include "sigchain.h"
#include "pkt-line.h"
#include "quote.h"
int cmd2process_cmp(const void *cmp_data UNUSED,
const struct hashmap_entry *eptr,
@ -118,6 +119,52 @@ int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, co
return 0;
}
int subprocess_start_strvec(struct hashmap *hashmap,
struct subprocess_entry *entry,
int is_git_cmd,
const struct strvec *argv,
subprocess_start_fn startfn)
{
int err;
int k;
struct child_process *process;
struct strbuf quoted = STRBUF_INIT;
process = &entry->process;
child_process_init(process);
for (k = 0; k < argv->nr; k++)
strvec_push(&process->args, argv->v[k]);
process->use_shell = 1;
process->in = -1;
process->out = -1;
process->git_cmd = is_git_cmd;
process->clean_on_exit = 1;
process->clean_on_exit_handler = subprocess_exit_handler;
process->trace2_child_class = "subprocess";
sq_quote_argv_pretty(&quoted, argv->v);
entry->cmd = strbuf_detach(&quoted, NULL);
err = start_command(process);
if (err) {
error("cannot fork to run subprocess '%s'", entry->cmd);
return err;
}
hashmap_entry_init(&entry->ent, strhash(entry->cmd));
err = startfn(entry);
if (err) {
error("initialization for subprocess '%s' failed", entry->cmd);
subprocess_stop(hashmap, entry);
return err;
}
hashmap_add(hashmap, &entry->ent);
return 0;
}
static int handshake_version(struct child_process *process,
const char *welcome_prefix, int *versions,
int *chosen_version)

Просмотреть файл

@ -57,6 +57,12 @@ typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
subprocess_start_fn startfn);
int subprocess_start_strvec(struct hashmap *hashmap,
struct subprocess_entry *entry,
int is_git_cmd,
const struct strvec *argv,
subprocess_start_fn startfn);
/* Kill a subprocess and remove it from the subprocess hashmap. */
void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry);