2006-05-11 Michael Matz <matz@suse.de>
[deliverable/binutils-gdb.git] / libiberty / pexecute.txh
CommitLineData
3db2e6dd 1@c -*- mode: texinfo -*-
f13c9bea 2@deftypefn Extension {struct pex_obj *} pex_init (int @var{flags}, const char *@var{pname}, const char *@var{tempbase})
5a17353c 3
b109e79a
ILT
4Prepare to execute one or more programs, with standard output of each
5program fed to standard input of the next. This is a system
6independent interface to execute a pipeline.
5a17353c 7
b109e79a 8@var{flags} is a bitwise combination of the following:
5a17353c 9
b109e79a 10@table @code
5a17353c 11
b109e79a
ILT
12@vindex PEX_RECORD_TIMES
13@item PEX_RECORD_TIMES
14Record subprocess times if possible.
5a17353c 15
b109e79a
ILT
16@vindex PEX_USE_PIPES
17@item PEX_USE_PIPES
18Use pipes for communication between processes, if possible.
5a17353c 19
b109e79a
ILT
20@vindex PEX_SAVE_TEMPS
21@item PEX_SAVE_TEMPS
22Don't delete temporary files used for communication between
23processes.
5a17353c 24
b109e79a 25@end table
5a17353c 26
b109e79a
ILT
27@var{pname} is the name of program to be executed, used in error
28messages. @var{tempbase} is a base name to use for any required
29temporary files; it may be @code{NULL} to use a randomly chosen name.
5a17353c
DD
30
31@end deftypefn
32
f13c9bea 33@deftypefn Extension {const char *} pex_run (struct pex_obj *@var{obj}, int @var{flags}, const char *@var{executable}, char * const *@var{argv}, const char *@var{outname}, const char *@var{errname}, int *@var{err})
b109e79a
ILT
34
35Execute one program in a pipeline. On success this returns
36@code{NULL}. On failure it returns an error message, a statically
37allocated string.
38
39@var{obj} is returned by a previous call to @code{pex_init}.
40
41@var{flags} is a bitwise combination of the following:
42
43@table @code
44
45@vindex PEX_LAST
46@item PEX_LAST
47This must be set on the last program in the pipeline. In particular,
48it should be set when executing a single program. The standard output
49of the program will be sent to @var{outname}, or, if @var{outname} is
f13c9bea
DD
50@code{NULL}, to the standard output of the calling program. Do @emph{not}
51set this bit if you want to call @code{pex_read_output}
b109e79a
ILT
52(described below). After a call to @code{pex_run} with this bit set,
53@var{pex_run} may no longer be called with the same @var{obj}.
54
55@vindex PEX_SEARCH
56@item PEX_SEARCH
57Search for the program using the user's executable search path.
58
59@vindex PEX_SUFFIX
60@item PEX_SUFFIX
61@var{outname} is a suffix. See the description of @var{outname},
62below.
63
64@vindex PEX_STDERR_TO_STDOUT
65@item PEX_STDERR_TO_STDOUT
66Send the program's standard error to standard output, if possible.
67
68@vindex PEX_BINARY_INPUT
69@vindex PEX_BINARY_OUTPUT
70@item PEX_BINARY_INPUT
71@itemx PEX_BINARY_OUTPUT
72The standard input (output) of the program should be read (written) in
73binary mode rather than text mode. These flags are ignored on systems
74which do not distinguish binary mode and text mode, such as Unix. For
f13c9bea 75proper behavior these flags should match appropriately---a call to
b109e79a
ILT
76@code{pex_run} using @code{PEX_BINARY_OUTPUT} should be followed by a
77call using @code{PEX_BINARY_INPUT}.
78@end table
79
80@var{executable} is the program to execute. @var{argv} is the set of
81arguments to pass to the program; normally @code{@var{argv}[0]} will
82be a copy of @var{executable}.
83
84@var{outname} is used to set the name of the file to use for standard
f13c9bea
DD
85output. There are two cases in which no output file will be used:
86
87@enumerate
88@item
b109e79a 89if @code{PEX_LAST} is not set in @var{flags}, and @code{PEX_USE_PIPES}
f13c9bea
DD
90was set in the call to @code{pex_init}, and the system supports pipes
91
92@item
93if @code{PEX_LAST} is set in @var{flags}, and @var{outname} is
94@code{NULL}
95@end enumerate
96
97@noindent
98Otherwise the code will use a file to hold standard
b109e79a
ILT
99output. If @code{PEX_LAST} is not set, this file is considered to be
100a temporary file, and it will be removed when no longer needed, unless
101@code{PEX_SAVE_TEMPS} was set in the call to @code{pex_init}.
102
103There are two cases to consider when setting the name of the file to
104hold standard output.
105
f13c9bea
DD
106@enumerate
107@item
108@code{PEX_SUFFIX} is set in @var{flags}. In this case
b109e79a
ILT
109@var{outname} may not be @code{NULL}. If the @var{tempbase} parameter
110to @code{pex_init} was not @code{NULL}, then the output file name is
111the concatenation of @var{tempbase} and @var{outname}. If
112@var{tempbase} was @code{NULL}, then the output file name is a random
113file name ending in @var{outname}.
114
f13c9bea
DD
115@item
116@code{PEX_SUFFIX} was not set in @var{flags}. In this
b109e79a
ILT
117case, if @var{outname} is not @code{NULL}, it is used as the output
118file name. If @var{outname} is @code{NULL}, and @var{tempbase} was
119not NULL, the output file name is randomly chosen using
120@var{tempbase}. Otherwise the output file name is chosen completely
121at random.
f13c9bea 122@end enumerate
b109e79a
ILT
123
124@var{errname} is the file name to use for standard error output. If
f13c9bea 125it is @code{NULL}, standard error is the same as the caller's.
b109e79a
ILT
126Otherwise, standard error is written to the named file.
127
128On an error return, the code sets @code{*@var{err}} to an @code{errno}
129value, or to 0 if there is no relevant @code{errno}.
130
131@end deftypefn
132
3db2e6dd
DD
133@deftypefn Extension {FILE *} pex_input_file (struct pex_obj *@var{obj}, int @var{flags}, const char *@var{in_name})
134
135Return a stream for a temporary file to pass to the first program in
136the pipeline as input.
137
138The name of the input file is chosen according to the same rules
139@code{pex_run} uses to choose output file names, based on
140@var{in_name}, @var{obj} and the @code{PEX_SUFFIX} bit in @var{flags}.
141
142Don't call @code{fclose} on the returned stream; the first call to
143@code{pex_run} closes it automatically.
144
145If @var{flags} includes @code{PEX_BINARY_OUTPUT}, open the stream in
146binary mode; otherwise, open it in the default mode. Including
147@code{PEX_BINARY_OUTPUT} in @var{flags} has no effect on Unix.
148@end deftypefn
149
150@deftypefn Extension {FILE *} pex_input_pipe (struct pex_obj *@var{obj}, int @var{binary})
151
152Return a stream @var{fp} for a pipe connected to the standard input of
153the first program in the pipeline; @var{fp} is opened for writing.
154You must have passed @code{PEX_USE_PIPES} to the @code{pex_init} call
155that returned @var{obj}.
156
157You must close @var{fp} using @code{fclose} yourself when you have
158finished writing data to the pipeline.
159
160The file descriptor underlying @var{fp} is marked not to be inherited
161by child processes.
162
163On systems that do not support pipes, this function returns
164@code{NULL}, and sets @code{errno} to @code{EINVAL}. If you would
165like to write code that is portable to all systems the @code{pex}
166functions support, consider using @code{pex_input_file} instead.
167
168There are two opportunities for deadlock using
169@code{pex_input_pipe}:
170
171@itemize @bullet
172@item
173Most systems' pipes can buffer only a fixed amount of data; a process
174that writes to a full pipe blocks. Thus, if you write to @file{fp}
175before starting the first process, you run the risk of blocking when
176there is no child process yet to read the data and allow you to
177continue. @code{pex_input_pipe} makes no promises about the
178size of the pipe's buffer, so if you need to write any data at all
179before starting the first process in the pipeline, consider using
180@code{pex_input_file} instead.
181
182@item
183Using @code{pex_input_pipe} and @code{pex_read_output} together
184may also cause deadlock. If the output pipe fills up, so that each
185program in the pipeline is waiting for the next to read more data, and
186you fill the input pipe by writing more data to @var{fp}, then there
187is no way to make progress: the only process that could read data from
188the output pipe is you, but you are blocked on the input pipe.
189
190@end itemize
191
192@end deftypefn
193
f13c9bea 194@deftypefn Extension {FILE *} pex_read_output (struct pex_obj *@var{obj}, int @var{binary})
b109e79a
ILT
195
196Returns a @code{FILE} pointer which may be used to read the standard
197output of the last program in the pipeline. When this is used,
198@code{PEX_LAST} should not be used in a call to @code{pex_run}. After
199this is called, @code{pex_run} may no longer be called with the same
200@var{obj}. @var{binary} should be non-zero if the file should be
201opened in binary mode. Don't call @code{fclose} on the returned file;
202it will be closed by @code{pex_free}.
203
204@end deftypefn
205
206@deftypefn Extension int pex_get_status (struct pex_obj *@var{obj}, int @var{count}, int *@var{vector})
207
208Returns the exit status of all programs run using @var{obj}.
209@var{count} is the number of results expected. The results will be
210placed into @var{vector}. The results are in the order of the calls
211to @code{pex_run}. Returns 0 on error, 1 on success.
212
213@end deftypefn
214
215@deftypefn Extension int pex_get_times (struct pex_obj *@var{obj}, int @var{count}, struct pex_time *@var{vector})
5a17353c 216
b109e79a
ILT
217Returns the process execution times of all programs run using
218@var{obj}. @var{count} is the number of results expected. The
219results will be placed into @var{vector}. The results are in the
220order of the calls to @code{pex_run}. Returns 0 on error, 1 on
221success.
5a17353c 222
f13c9bea
DD
223@code{struct pex_time} has the following fields of the type
224@code{unsigned long}: @code{user_seconds},
b109e79a
ILT
225@code{user_microseconds}, @code{system_seconds},
226@code{system_microseconds}. On systems which do not support reporting
227process times, all the fields will be set to @code{0}.
5a17353c 228
b109e79a
ILT
229@end deftypefn
230
231@deftypefn Extension void pex_free (struct pex_obj @var{obj})
5a17353c 232
b109e79a 233Clean up and free all data associated with @var{obj}.
5a17353c
DD
234
235@end deftypefn
236
f13c9bea 237@deftypefn Extension {const char *} pex_one (int @var{flags}, const char *@var{executable}, char * const *@var{argv}, const char *@var{pname}, const char *@var{outname}, const char *@var{errname}, int *@var{status}, int *@var{err})
b109e79a 238
f13c9bea 239An interface to permit the easy execution of a
b109e79a
ILT
240single program. The return value and most of the parameters are as
241for a call to @code{pex_run}. @var{flags} is restricted to a
242combination of @code{PEX_SEARCH}, @code{PEX_STDERR_TO_STDOUT}, and
243@code{PEX_BINARY_OUTPUT}. @var{outname} is interpreted as if
f13c9bea 244@code{PEX_LAST} were set. On a successful return, @code{*@var{status}} will
b109e79a
ILT
245be set to the exit status of the program.
246
247@end deftypefn
248
249@deftypefn Extension int pexecute (const char *@var{program}, char * const *@var{argv}, const char *@var{this_pname}, const char *@var{temp_base}, char **@var{errmsg_fmt}, char **@var{errmsg_arg}, int flags)
5a17353c 250
b109e79a
ILT
251This is the old interface to execute one or more programs. It is
252still supported for compatibility purposes, but is no longer
253documented.
5a17353c 254
b109e79a
ILT
255@end deftypefn
256
257@deftypefn Extension int pwait (int @var{pid}, int *@var{status}, int @var{flags})
258
259Another part of the old execution interface.
260
261@end deftypefn
This page took 0.189789 seconds and 4 git commands to generate.