{"thread":{"id":"7177","subject":"[PATCH 3/8] Start defining a more sophisticated run_command","startedAt":"2007-03-10T08:28:00Z","lastAt":"2007-03-10T08:28:00Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":8},"messages":[{"id":"36735","messageId":"20070310082800.GC4133@spearce.org","threadId":"7177","inReplyTo":null,"subject":"[PATCH 3/8] Start defining a more sophisticated run_command","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-03-10T08:28:00Z","receivedAt":"2007-03-10T08:28:00Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"There are a number of places where we do some variation of\nfork()+exec() but we also need to setup redirection in the process,\nmuch like what run_command does for us already with its option flags.\n\nIt would be nice to reuse more of the run_command logic, especially\nas that non-fork API helps us to port to odd platforms like Win32.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n run-command.c |   25 ++++++++++++++++++-------\n run-command.h |    9 +++++++++\n 2 files changed, 27 insertions(+), 7 deletions(-)\n\ndiff --git a/run-command.c b/run-command.c\nindex 94ace50..1fb7fa5 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -2,26 +2,26 @@\n #include \"run-command.h\"\n #include \"exec_cmd.h\"\n \n-int run_command_v_opt(const char **argv, int flags)\n+int run_command(struct child_process *cmd)\n {\n \tpid_t pid = fork();\n \n \tif (pid < 0)\n \t\treturn -ERR_RUN_COMMAND_FORK;\n \tif (!pid) {\n-\t\tif (flags & RUN_COMMAND_NO_STDIN) {\n+\t\tif (cmd->no_stdin) {\n \t\t\tint fd = open(\"/dev/null\", O_RDWR);\n \t\t\tdup2(fd, 0);\n \t\t\tclose(fd);\n \t\t}\n-\t\tif (flags & RUN_COMMAND_STDOUT_TO_STDERR)\n+\t\tif (cmd->stdout_to_stderr)\n \t\t\tdup2(2, 1);\n-\t\tif (flags & RUN_GIT_CMD) {\n-\t\t\texecv_git_cmd(argv);\n+\t\tif (cmd->git_cmd) {\n+\t\t\texecv_git_cmd(cmd->argv);\n \t\t} else {\n-\t\t\texecvp(argv[0], (char *const*) argv);\n+\t\t\texecvp(cmd->argv[0], (char *const*) cmd->argv);\n \t\t}\n-\t\tdie(\"exec %s failed.\", argv[0]);\n+\t\tdie(\"exec %s failed.\", cmd->argv[0]);\n \t}\n \tfor (;;) {\n \t\tint status, code;\n@@ -46,3 +46,14 @@ int run_command_v_opt(const char **argv, int flags)\n \t\treturn 0;\n \t}\n }\n+\n+int run_command_v_opt(const char **argv, int opt)\n+{\n+\tstruct child_process cmd;\n+\tmemset(&cmd, 0, sizeof(cmd));\n+\tcmd.argv = argv;\n+\tcmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;\n+\tcmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;\n+\tcmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;\n+\treturn run_command(&cmd);\n+}\ndiff --git a/run-command.h b/run-command.h\nindex 2646d38..f9db2a7 100644\n--- a/run-command.h\n+++ b/run-command.h\n@@ -10,6 +10,15 @@ enum {\n \tERR_RUN_COMMAND_WAITPID_NOEXIT,\n };\n \n+struct child_process {\n+\tconst char **argv;\n+\tunsigned no_stdin:1;\n+\tunsigned git_cmd:1; /* if this is to be git sub-command */\n+\tunsigned stdout_to_stderr:1;\n+};\n+\n+int run_command(struct child_process *);\n+\n #define RUN_COMMAND_NO_STDIN 1\n #define RUN_GIT_CMD\t     2\t/*If this is to be git sub-command */\n #define RUN_COMMAND_STDOUT_TO_STDERR 4\n-- \n1.5.0.3.942.g299f\n"}]}