]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/pexecute.txh
* MAINTAINERS: Update my email address.
[thirdparty/gcc.git] / libiberty / pexecute.txh
CommitLineData
8eff378c 1@c -*- mode: texinfo -*-
2b836651 2@deftypefn Extension {struct pex_obj *} pex_init (int @var{flags}, const char *@var{pname}, const char *@var{tempbase})
55d0e5e0 3
a584cf65
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.
55d0e5e0 7
a584cf65 8@var{flags} is a bitwise combination of the following:
55d0e5e0 9
a584cf65 10@table @code
55d0e5e0 11
a584cf65
ILT
12@vindex PEX_RECORD_TIMES
13@item PEX_RECORD_TIMES
14Record subprocess times if possible.
55d0e5e0 15
a584cf65
ILT
16@vindex PEX_USE_PIPES
17@item PEX_USE_PIPES
18Use pipes for communication between processes, if possible.
55d0e5e0 19
a584cf65
ILT
20@vindex PEX_SAVE_TEMPS
21@item PEX_SAVE_TEMPS
22Don't delete temporary files used for communication between
23processes.
55d0e5e0 24
a584cf65 25@end table
55d0e5e0 26
a584cf65
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.
55d0e5e0
ZW
30
31@end deftypefn
32
2b836651 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})
a584cf65
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
2b836651
EZ
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}
a584cf65
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
7cf4c53d 70@vindex PEX_BINARY_ERROR
a584cf65
ILT
71@item PEX_BINARY_INPUT
72@itemx PEX_BINARY_OUTPUT
7cf4c53d
VP
73@itemx PEX_BINARY_ERROR
74The standard input (output or error) of the program should be read (written) in
a584cf65
ILT
75binary mode rather than text mode. These flags are ignored on systems
76which do not distinguish binary mode and text mode, such as Unix. For
2b836651 77proper behavior these flags should match appropriately---a call to
a584cf65
ILT
78@code{pex_run} using @code{PEX_BINARY_OUTPUT} should be followed by a
79call using @code{PEX_BINARY_INPUT}.
7cf4c53d
VP
80
81@vindex PEX_STDERR_TO_PIPE
82@item PEX_STDERR_TO_PIPE
83Send the program's standard error to a pipe, if possible. This flag
84cannot be specified together with @code{PEX_STDERR_TO_STDOUT}. This
85flag can be specified only on the last program in pipeline.
86
a584cf65
ILT
87@end table
88
89@var{executable} is the program to execute. @var{argv} is the set of
90arguments to pass to the program; normally @code{@var{argv}[0]} will
91be a copy of @var{executable}.
92
93@var{outname} is used to set the name of the file to use for standard
2b836651
EZ
94output. There are two cases in which no output file will be used:
95
96@enumerate
97@item
a584cf65 98if @code{PEX_LAST} is not set in @var{flags}, and @code{PEX_USE_PIPES}
2b836651
EZ
99was set in the call to @code{pex_init}, and the system supports pipes
100
101@item
102if @code{PEX_LAST} is set in @var{flags}, and @var{outname} is
103@code{NULL}
104@end enumerate
105
106@noindent
107Otherwise the code will use a file to hold standard
a584cf65
ILT
108output. If @code{PEX_LAST} is not set, this file is considered to be
109a temporary file, and it will be removed when no longer needed, unless
110@code{PEX_SAVE_TEMPS} was set in the call to @code{pex_init}.
111
112There are two cases to consider when setting the name of the file to
113hold standard output.
114
2b836651
EZ
115@enumerate
116@item
117@code{PEX_SUFFIX} is set in @var{flags}. In this case
a584cf65
ILT
118@var{outname} may not be @code{NULL}. If the @var{tempbase} parameter
119to @code{pex_init} was not @code{NULL}, then the output file name is
120the concatenation of @var{tempbase} and @var{outname}. If
121@var{tempbase} was @code{NULL}, then the output file name is a random
122file name ending in @var{outname}.
123
2b836651
EZ
124@item
125@code{PEX_SUFFIX} was not set in @var{flags}. In this
a584cf65
ILT
126case, if @var{outname} is not @code{NULL}, it is used as the output
127file name. If @var{outname} is @code{NULL}, and @var{tempbase} was
128not NULL, the output file name is randomly chosen using
129@var{tempbase}. Otherwise the output file name is chosen completely
130at random.
2b836651 131@end enumerate
a584cf65
ILT
132
133@var{errname} is the file name to use for standard error output. If
2b836651 134it is @code{NULL}, standard error is the same as the caller's.
a584cf65
ILT
135Otherwise, standard error is written to the named file.
136
137On an error return, the code sets @code{*@var{err}} to an @code{errno}
138value, or to 0 if there is no relevant @code{errno}.
139
140@end deftypefn
141
ea60341e
MS
142@deftypefn Extension {const char *} pex_run_in_environment (struct pex_obj *@var{obj}, int @var{flags}, const char *@var{executable}, char * const *@var{argv}, char * const *@var{env}, int @var{env_size}, const char *@var{outname}, const char *@var{errname}, int *@var{err})
143
144Execute one program in a pipeline, permitting the environment for the
145program to be specified. Behaviour and parameters not listed below are
146as for @code{pex_run}.
147
148@var{env} is the environment for the child process, specified as an array of
149character pointers. Each element of the array should point to a string of the
150form @code{VAR=VALUE}, with the exception of the last element that must be
151@code{NULL}.
152
153@end deftypefn
154
8eff378c
JB
155@deftypefn Extension {FILE *} pex_input_file (struct pex_obj *@var{obj}, int @var{flags}, const char *@var{in_name})
156
157Return a stream for a temporary file to pass to the first program in
158the pipeline as input.
159
160The name of the input file is chosen according to the same rules
161@code{pex_run} uses to choose output file names, based on
162@var{in_name}, @var{obj} and the @code{PEX_SUFFIX} bit in @var{flags}.
163
164Don't call @code{fclose} on the returned stream; the first call to
165@code{pex_run} closes it automatically.
166
167If @var{flags} includes @code{PEX_BINARY_OUTPUT}, open the stream in
168binary mode; otherwise, open it in the default mode. Including
169@code{PEX_BINARY_OUTPUT} in @var{flags} has no effect on Unix.
170@end deftypefn
171
172@deftypefn Extension {FILE *} pex_input_pipe (struct pex_obj *@var{obj}, int @var{binary})
173
174Return a stream @var{fp} for a pipe connected to the standard input of
175the first program in the pipeline; @var{fp} is opened for writing.
176You must have passed @code{PEX_USE_PIPES} to the @code{pex_init} call
177that returned @var{obj}.
178
179You must close @var{fp} using @code{fclose} yourself when you have
180finished writing data to the pipeline.
181
182The file descriptor underlying @var{fp} is marked not to be inherited
183by child processes.
184
185On systems that do not support pipes, this function returns
186@code{NULL}, and sets @code{errno} to @code{EINVAL}. If you would
187like to write code that is portable to all systems the @code{pex}
188functions support, consider using @code{pex_input_file} instead.
189
190There are two opportunities for deadlock using
191@code{pex_input_pipe}:
192
193@itemize @bullet
194@item
195Most systems' pipes can buffer only a fixed amount of data; a process
196that writes to a full pipe blocks. Thus, if you write to @file{fp}
197before starting the first process, you run the risk of blocking when
198there is no child process yet to read the data and allow you to
199continue. @code{pex_input_pipe} makes no promises about the
200size of the pipe's buffer, so if you need to write any data at all
201before starting the first process in the pipeline, consider using
202@code{pex_input_file} instead.
203
204@item
205Using @code{pex_input_pipe} and @code{pex_read_output} together
206may also cause deadlock. If the output pipe fills up, so that each
207program in the pipeline is waiting for the next to read more data, and
208you fill the input pipe by writing more data to @var{fp}, then there
209is no way to make progress: the only process that could read data from
210the output pipe is you, but you are blocked on the input pipe.
211
212@end itemize
213
214@end deftypefn
215
2b836651 216@deftypefn Extension {FILE *} pex_read_output (struct pex_obj *@var{obj}, int @var{binary})
a584cf65
ILT
217
218Returns a @code{FILE} pointer which may be used to read the standard
219output of the last program in the pipeline. When this is used,
220@code{PEX_LAST} should not be used in a call to @code{pex_run}. After
221this is called, @code{pex_run} may no longer be called with the same
222@var{obj}. @var{binary} should be non-zero if the file should be
223opened in binary mode. Don't call @code{fclose} on the returned file;
224it will be closed by @code{pex_free}.
225
226@end deftypefn
227
7cf4c53d
VP
228@deftypefn Extension {FILE *} pex_read_err (struct pex_obj *@var{obj}, int @var{binary})
229
230Returns a @code{FILE} pointer which may be used to read the standard
231error of the last program in the pipeline. When this is used,
232@code{PEX_LAST} should not be used in a call to @code{pex_run}. After
233this is called, @code{pex_run} may no longer be called with the same
234@var{obj}. @var{binary} should be non-zero if the file should be
235opened in binary mode. Don't call @code{fclose} on the returned file;
236it will be closed by @code{pex_free}.
237
238@end deftypefn
239
240
a584cf65
ILT
241@deftypefn Extension int pex_get_status (struct pex_obj *@var{obj}, int @var{count}, int *@var{vector})
242
243Returns the exit status of all programs run using @var{obj}.
244@var{count} is the number of results expected. The results will be
245placed into @var{vector}. The results are in the order of the calls
246to @code{pex_run}. Returns 0 on error, 1 on success.
247
248@end deftypefn
249
250@deftypefn Extension int pex_get_times (struct pex_obj *@var{obj}, int @var{count}, struct pex_time *@var{vector})
55d0e5e0 251
a584cf65
ILT
252Returns the process execution times of all programs run using
253@var{obj}. @var{count} is the number of results expected. The
254results will be placed into @var{vector}. The results are in the
255order of the calls to @code{pex_run}. Returns 0 on error, 1 on
256success.
55d0e5e0 257
2b836651
EZ
258@code{struct pex_time} has the following fields of the type
259@code{unsigned long}: @code{user_seconds},
a584cf65
ILT
260@code{user_microseconds}, @code{system_seconds},
261@code{system_microseconds}. On systems which do not support reporting
262process times, all the fields will be set to @code{0}.
55d0e5e0 263
a584cf65
ILT
264@end deftypefn
265
266@deftypefn Extension void pex_free (struct pex_obj @var{obj})
55d0e5e0 267
48492bdf
TT
268Clean up and free all data associated with @var{obj}. If you have not
269yet called @code{pex_get_times} or @code{pex_get_status}, this will
270try to kill the subprocesses.
55d0e5e0
ZW
271
272@end deftypefn
273
2b836651 274@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})
a584cf65 275
2b836651 276An interface to permit the easy execution of a
a584cf65
ILT
277single program. The return value and most of the parameters are as
278for a call to @code{pex_run}. @var{flags} is restricted to a
279combination of @code{PEX_SEARCH}, @code{PEX_STDERR_TO_STDOUT}, and
280@code{PEX_BINARY_OUTPUT}. @var{outname} is interpreted as if
2b836651 281@code{PEX_LAST} were set. On a successful return, @code{*@var{status}} will
a584cf65
ILT
282be set to the exit status of the program.
283
284@end deftypefn
285
97977a91 286@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 @var{flags})
55d0e5e0 287
a584cf65
ILT
288This is the old interface to execute one or more programs. It is
289still supported for compatibility purposes, but is no longer
290documented.
55d0e5e0 291
a584cf65
ILT
292@end deftypefn
293
294@deftypefn Extension int pwait (int @var{pid}, int *@var{status}, int @var{flags})
295
296Another part of the old execution interface.
297
298@end deftypefn