index-pack: be careful after fixing up the header/footer
[git/dscho.git] / Documentation / technical / api-run-command.txt
blob75aa5d49234ec36857a7c8d2f3900001af5cbcde
1 run-command API
2 ===============
4 The run-command API offers a versatile tool to run sub-processes with
5 redirected input and output as well as with a modified environment
6 and an alternate current directory.
8 A similar API offers the capability to run a function asynchronously,
9 which is primarily used to capture the output that the function
10 produces in the caller in order to process it.
13 Functions
14 ---------
16 `start_command`::
18         Start a sub-process. Takes a pointer to a `struct child_process`
19         that specifies the details and returns pipe FDs (if requested).
20         See below for details.
22 `finish_command`::
24         Wait for the completion of a sub-process that was started with
25         start_command().
27 `run_command`::
29         A convenience function that encapsulates a sequence of
30         start_command() followed by finish_command(). Takes a pointer
31         to a `struct child_process` that specifies the details.
33 `run_command_v_opt`, `run_command_v_opt_cd`, `run_command_v_opt_cd_env`::
35         Convenience functions that encapsulate a sequence of
36         start_command() followed by finish_command(). The argument argv
37         specifies the program and its arguments. The argument opt is zero
38         or more of the flags `RUN_COMMAND_NO_STDIN`, `RUN_GIT_CMD`, or
39         `RUN_COMMAND_STDOUT_TO_STDERR` that correspond to the members
40         .no_stdin, .git_cmd, .stdout_to_stderr of `struct child_process`.
41         The argument dir corresponds the member .dir. The argument env
42         corresponds to the member .env.
44 `start_async`::
46         Run a function asynchronously. Takes a pointer to a `struct
47         async` that specifies the details and returns a pipe FD
48         from which the caller reads. See below for details.
50 `finish_async`::
52         Wait for the completion of an asynchronous function that was
53         started with start_async().
56 Data structures
57 ---------------
59 * `struct child_process`
61 This describes the arguments, redirections, and environment of a
62 command to run in a sub-process.
64 The caller:
66 1. allocates and clears (memset(&chld, 0, sizeof(chld));) a
67    struct child_process variable;
68 2. initializes the members;
69 3. calls start_command();
70 4. processes the data;
71 5. closes file descriptors (if necessary; see below);
72 6. calls finish_command().
74 The .argv member is set up as an array of string pointers (NULL
75 terminated), of which .argv[0] is the program name to run (usually
76 without a path). If the command to run is a git command, set argv[0] to
77 the command name without the 'git-' prefix and set .git_cmd = 1.
79 The members .in, .out, .err are used to redirect stdin, stdout,
80 stderr as follows:
82 . Specify 0 to request no special redirection. No new file descriptor
83   is allocated. The child process simply inherits the channel from the
84   parent.
86 . Specify -1 to have a pipe allocated; start_command() replaces -1
87   by the pipe FD in the following way:
89         .in: Returns the writable pipe end into which the caller writes;
90                 the readable end of the pipe becomes the child's stdin.
92         .out, .err: Returns the readable pipe end from which the caller
93                 reads; the writable end of the pipe end becomes child's
94                 stdout/stderr.
96   The caller of start_command() must close the so returned FDs
97   after it has completed reading from/writing to it!
99 . Specify a file descriptor > 0 to be used by the child:
101         .in: The FD must be readable; it becomes child's stdin.
102         .out: The FD must be writable; it becomes child's stdout.
103         .err > 0 is not supported.
105   The specified FD is closed by start_command(), even if it fails to
106   run the sub-process!
108 . Special forms of redirection are available by setting these members
109   to 1:
111         .no_stdin, .no_stdout, .no_stderr: The respective channel is
112                 redirected to /dev/null.
114         .stdout_to_stderr: stdout of the child is redirected to its
115                 stderr. This happens after stderr is itself redirected.
116                 So stdout will follow stderr to wherever it is
117                 redirected.
119 To modify the environment of the sub-process, specify an array of
120 string pointers (NULL terminated) in .env:
122 . If the string is of the form "VAR=value", i.e. it contains '='
123   the variable is added to the child process's environment.
125 . If the string does not contain '=', it names an environment
126   variable that will be removed from the child process's environment.
128 To specify a new initial working directory for the sub-process,
129 specify it in the .dir member.
132 * `struct async`
134 This describes a function to run asynchronously, whose purpose is
135 to produce output that the caller reads.
137 The caller:
139 1. allocates and clears (memset(&asy, 0, sizeof(asy));) a
140    struct async variable;
141 2. initializes .proc and .data;
142 3. calls start_async();
143 4. processes the data by reading from the fd in .out;
144 5. closes .out;
145 6. calls finish_async().
147 The function pointer in .proc has the following signature:
149         int proc(int fd, void *data);
151 . fd specifies a writable file descriptor to which the function must
152   write the data that it produces. The function *must* close this
153   descriptor before it returns.
155 . data is the value that the caller has specified in the .data member
156   of struct async.
158 . The return value of the function is 0 on success and non-zero
159   on failure. If the function indicates failure, finish_async() will
160   report failure as well.
163 There are serious restrictions on what the asynchronous function can do
164 because this facility is implemented by a pipe to a forked process on
165 UNIX, but by a thread in the same address space on Windows:
167 . It cannot change the program's state (global variables, environment,
168   etc.) in a way that the caller notices; in other words, .out is the
169   only communication channel to the caller.
171 . It must not change the program's state that the caller of the
172   facility also uses.