Revert "Strip \r from the input."
[git/dscho.git] / run-command.c
blob5ed338c0cbdb8e4ff55f2d77871e62ce916075a4
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
5 static inline void close_pair(int fd[2])
7 close(fd[0]);
8 close(fd[1]);
11 static inline void dup_devnull(int to)
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, to);
15 close(fd);
18 int start_command(struct child_process *cmd)
20 int need_in, need_out, need_err;
21 int fdin[2], fdout[2], fderr[2];
23 need_in = !cmd->no_stdin && cmd->in < 0;
24 if (need_in) {
25 if (pipe(fdin) < 0)
26 return -ERR_RUN_COMMAND_PIPE;
27 cmd->in = fdin[1];
28 cmd->close_in = 1;
31 need_out = !cmd->no_stdout
32 && !cmd->stdout_to_stderr
33 && cmd->out < 0;
34 if (need_out) {
35 if (pipe(fdout) < 0) {
36 if (need_in)
37 close_pair(fdin);
38 return -ERR_RUN_COMMAND_PIPE;
40 cmd->out = fdout[0];
41 cmd->close_out = 1;
44 need_err = !cmd->no_stderr && cmd->err < 0;
45 if (need_err) {
46 if (pipe(fderr) < 0) {
47 if (need_in)
48 close_pair(fdin);
49 if (need_out)
50 close_pair(fdout);
51 return -ERR_RUN_COMMAND_PIPE;
53 cmd->err = fderr[0];
56 #ifndef __MINGW32__
57 cmd->pid = fork();
58 if (!cmd->pid) {
59 if (cmd->no_stdin)
60 dup_devnull(0);
61 else if (need_in) {
62 dup2(fdin[0], 0);
63 close_pair(fdin);
64 } else if (cmd->in) {
65 dup2(cmd->in, 0);
66 close(cmd->in);
69 if (cmd->no_stdout)
70 dup_devnull(1);
71 else if (cmd->stdout_to_stderr)
72 dup2(2, 1);
73 else if (need_out) {
74 dup2(fdout[1], 1);
75 close_pair(fdout);
76 } else if (cmd->out > 1) {
77 dup2(cmd->out, 1);
78 close(cmd->out);
81 if (cmd->no_stderr)
82 dup_devnull(2);
83 else if (need_err) {
84 dup2(fderr[1], 2);
85 close_pair(fderr);
88 if (cmd->dir && chdir(cmd->dir))
89 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
90 cmd->dir, strerror(errno));
91 if (cmd->env) {
92 for (; *cmd->env; cmd->env++) {
93 if (strchr(*cmd->env, '='))
94 putenv((char*)*cmd->env);
95 else
96 unsetenv(*cmd->env);
99 if (cmd->git_cmd) {
100 execv_git_cmd(cmd->argv);
101 } else {
102 execvp(cmd->argv[0], (char *const*) cmd->argv);
104 die("exec %s failed.", cmd->argv[0]);
106 #else
107 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
108 const char *sargv0 = cmd->argv[0];
109 char **env = environ;
110 struct strbuf git_cmd;
112 if (cmd->no_stdin) {
113 s0 = dup(0);
114 dup_devnull(0);
115 } else if (need_in) {
116 s0 = dup(0);
117 dup2(fdin[0], 0);
118 } else if (cmd->in) {
119 s0 = dup(0);
120 dup2(cmd->in, 0);
123 if (cmd->no_stdout) {
124 s1 = dup(1);
125 dup_devnull(1);
126 } else if (cmd->stdout_to_stderr) {
127 s1 = dup(1);
128 dup2(2, 1);
129 } else if (need_out) {
130 s1 = dup(1);
131 dup2(fdout[1], 1);
132 } else if (cmd->out > 1) {
133 s1 = dup(1);
134 dup2(cmd->out, 1);
137 if (cmd->no_stderr) {
138 s2 = dup(2);
139 dup_devnull(2);
140 } else if (need_err) {
141 s2 = dup(2);
142 dup2(fderr[1], 2);
145 if (cmd->dir)
146 die("chdir in start_command() not implemented");
147 if (cmd->env) {
148 env = copy_environ();
149 for (; *cmd->env; cmd->env++)
150 env = env_setenv(env, *cmd->env);
153 if (cmd->git_cmd) {
154 strbuf_init(&git_cmd, 0);
155 strbuf_addf(&git_cmd, "git-%s", cmd->argv[0]);
156 cmd->argv[0] = git_cmd.buf;
159 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
161 if (cmd->env)
162 free_environ(env);
163 if (cmd->git_cmd)
164 strbuf_release(&git_cmd);
166 cmd->argv[0] = sargv0;
167 if (s0 >= 0)
168 dup2(s0, 0), close(s0);
169 if (s1 >= 0)
170 dup2(s1, 1), close(s1);
171 if (s2 >= 0)
172 dup2(s2, 2), close(s2);
173 #endif
175 if (cmd->pid < 0) {
176 if (need_in)
177 close_pair(fdin);
178 if (need_out)
179 close_pair(fdout);
180 if (need_err)
181 close_pair(fderr);
182 return -ERR_RUN_COMMAND_FORK;
185 if (need_in)
186 close(fdin[0]);
187 else if (cmd->in)
188 close(cmd->in);
190 if (need_out)
191 close(fdout[1]);
192 else if (cmd->out > 1)
193 close(cmd->out);
195 if (need_err)
196 close(fderr[1]);
198 return 0;
201 static int wait_or_whine(pid_t pid)
203 for (;;) {
204 int status, code;
205 pid_t waiting = waitpid(pid, &status, 0);
207 if (waiting < 0) {
208 if (errno == EINTR)
209 continue;
210 error("waitpid failed (%s)", strerror(errno));
211 return -ERR_RUN_COMMAND_WAITPID;
213 if (waiting != pid)
214 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
215 if (WIFSIGNALED(status))
216 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
218 if (!WIFEXITED(status))
219 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
220 code = WEXITSTATUS(status);
221 if (code)
222 return -code;
223 return 0;
227 int finish_command(struct child_process *cmd)
229 if (cmd->close_in)
230 close(cmd->in);
231 if (cmd->close_out)
232 close(cmd->out);
233 return wait_or_whine(cmd->pid);
236 int run_command(struct child_process *cmd)
238 int code = start_command(cmd);
239 if (code)
240 return code;
241 return finish_command(cmd);
244 static void prepare_run_command_v_opt(struct child_process *cmd,
245 const char **argv,
246 int opt)
248 memset(cmd, 0, sizeof(*cmd));
249 cmd->argv = argv;
250 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
251 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
252 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
255 int run_command_v_opt(const char **argv, int opt)
257 struct child_process cmd;
258 prepare_run_command_v_opt(&cmd, argv, opt);
259 return run_command(&cmd);
262 int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
264 struct child_process cmd;
265 prepare_run_command_v_opt(&cmd, argv, opt);
266 cmd.dir = dir;
267 return run_command(&cmd);
270 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
272 struct child_process cmd;
273 prepare_run_command_v_opt(&cmd, argv, opt);
274 cmd.dir = dir;
275 cmd.env = env;
276 return run_command(&cmd);
279 #ifdef __MINGW32__
280 static __stdcall unsigned run_thread(void *data)
282 struct async *async = data;
283 return async->proc(async->fd_for_proc, async->data);
285 #endif
287 int start_async(struct async *async)
289 int pipe_out[2];
291 if (pipe(pipe_out) < 0)
292 return error("cannot create pipe: %s", strerror(errno));
293 async->out = pipe_out[0];
295 #ifndef __MINGW32__
296 async->pid = fork();
297 if (async->pid < 0) {
298 error("fork (async) failed: %s", strerror(errno));
299 close_pair(pipe_out);
300 return -1;
302 if (!async->pid) {
303 close(pipe_out[0]);
304 exit(!!async->proc(pipe_out[1], async->data));
306 close(pipe_out[1]);
307 #else
308 async->fd_for_proc = pipe_out[1];
309 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
310 if (!async->tid) {
311 error("cannot create thread: %s", strerror(errno));
312 close_pair(pipe_out);
313 return -1;
315 #endif
316 return 0;
319 int finish_async(struct async *async)
321 #ifndef __MINGW32__
322 int ret = 0;
324 if (wait_or_whine(async->pid))
325 ret = error("waitpid (async) failed");
326 #else
327 DWORD ret = 0;
328 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
329 ret = error("waiting for thread failed: %lu", GetLastError());
330 else if (!GetExitCodeThread(async->tid, &ret))
331 ret = error("cannot get thread exit code: %lu", GetLastError());
332 CloseHandle(async->tid);
333 #endif
334 return ret;