git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Junio C Hamano <junkio@cox.net>
Cc: git@vger.kernel.org
Subject: [PATCH 3/8] Start defining a more sophisticated run_command
Date: Sat, 10 Mar 2007 03:28:00 -0500	[thread overview]
Message-ID: <20070310082800.GC4133@spearce.org> (raw)

There are a number of places where we do some variation of
fork()+exec() but we also need to setup redirection in the process,
much like what run_command does for us already with its option flags.

It would be nice to reuse more of the run_command logic, especially
as that non-fork API helps us to port to odd platforms like Win32.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 run-command.c |   25 ++++++++++++++++++-------
 run-command.h |    9 +++++++++
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/run-command.c b/run-command.c
index 94ace50..1fb7fa5 100644
--- a/run-command.c
+++ b/run-command.c
@@ -2,26 +2,26 @@
 #include "run-command.h"
 #include "exec_cmd.h"
 
-int run_command_v_opt(const char **argv, int flags)
+int run_command(struct child_process *cmd)
 {
 	pid_t pid = fork();
 
 	if (pid < 0)
 		return -ERR_RUN_COMMAND_FORK;
 	if (!pid) {
-		if (flags & RUN_COMMAND_NO_STDIN) {
+		if (cmd->no_stdin) {
 			int fd = open("/dev/null", O_RDWR);
 			dup2(fd, 0);
 			close(fd);
 		}
-		if (flags & RUN_COMMAND_STDOUT_TO_STDERR)
+		if (cmd->stdout_to_stderr)
 			dup2(2, 1);
-		if (flags & RUN_GIT_CMD) {
-			execv_git_cmd(argv);
+		if (cmd->git_cmd) {
+			execv_git_cmd(cmd->argv);
 		} else {
-			execvp(argv[0], (char *const*) argv);
+			execvp(cmd->argv[0], (char *const*) cmd->argv);
 		}
-		die("exec %s failed.", argv[0]);
+		die("exec %s failed.", cmd->argv[0]);
 	}
 	for (;;) {
 		int status, code;
@@ -46,3 +46,14 @@ int run_command_v_opt(const char **argv, int flags)
 		return 0;
 	}
 }
+
+int run_command_v_opt(const char **argv, int opt)
+{
+	struct child_process cmd;
+	memset(&cmd, 0, sizeof(cmd));
+	cmd.argv = argv;
+	cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
+	cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
+	cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
+	return run_command(&cmd);
+}
diff --git a/run-command.h b/run-command.h
index 2646d38..f9db2a7 100644
--- a/run-command.h
+++ b/run-command.h
@@ -10,6 +10,15 @@ enum {
 	ERR_RUN_COMMAND_WAITPID_NOEXIT,
 };
 
+struct child_process {
+	const char **argv;
+	unsigned no_stdin:1;
+	unsigned git_cmd:1; /* if this is to be git sub-command */
+	unsigned stdout_to_stderr:1;
+};
+
+int run_command(struct child_process *);
+
 #define RUN_COMMAND_NO_STDIN 1
 #define RUN_GIT_CMD	     2	/*If this is to be git sub-command */
 #define RUN_COMMAND_STDOUT_TO_STDERR 4
-- 
1.5.0.3.942.g299f

                 reply	other threads:[~2007-03-10  8:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070310082800.GC4133@spearce.org \
    --to=spearce@spearce.org \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).