1 | /* Function declarations for libiberty. |
---|
2 | |
---|
3 | Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
---|
4 | 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
---|
5 | |
---|
6 | Note - certain prototypes declared in this header file are for |
---|
7 | functions whoes implementation copyright does not belong to the |
---|
8 | FSF. Those prototypes are present in this file for reference |
---|
9 | purposes only and their presence in this file should not construed |
---|
10 | as an indication of ownership by the FSF of the implementation of |
---|
11 | those functions in any way or form whatsoever. |
---|
12 | |
---|
13 | This program is free software; you can redistribute it and/or modify |
---|
14 | it under the terms of the GNU General Public License as published by |
---|
15 | the Free Software Foundation; either version 2, or (at your option) |
---|
16 | any later version. |
---|
17 | |
---|
18 | This program is distributed in the hope that it will be useful, |
---|
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | GNU General Public License for more details. |
---|
22 | |
---|
23 | You should have received a copy of the GNU General Public License |
---|
24 | along with this program; if not, write to the Free Software |
---|
25 | Foundation, Inc., 51 Franklin Street - Fifth Floor, |
---|
26 | Boston, MA 02110-1301, USA. |
---|
27 | |
---|
28 | Written by Cygnus Support, 1994. |
---|
29 | |
---|
30 | The libiberty library provides a number of functions which are |
---|
31 | missing on some operating systems. We do not declare those here, |
---|
32 | to avoid conflicts with the system header files on operating |
---|
33 | systems that do support those functions. In this file we only |
---|
34 | declare those functions which are specific to libiberty. |
---|
35 | |
---|
36 | Hacked up libiberty.h file from the real one. |
---|
37 | */ |
---|
38 | |
---|
39 | #if !defined (_LIBIERTY_H_) |
---|
40 | #define _LIBIERTY_H_ |
---|
41 | |
---|
42 | #ifdef __cplusplus |
---|
43 | extern "C" { |
---|
44 | #endif |
---|
45 | |
---|
46 | #include <ansidecl.h> |
---|
47 | #include <stdlib.h> |
---|
48 | #include <stdio.h> |
---|
49 | |
---|
50 | #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) |
---|
51 | |
---|
52 | #define xstrdup strdup |
---|
53 | #define xstrerror strerror |
---|
54 | |
---|
55 | /* These macros provide a K&R/C89/C++-friendly way of allocating structures |
---|
56 | with nice encapsulation. The XDELETE*() macros are technically |
---|
57 | superfluous, but provided here for symmetry. Using them consistently |
---|
58 | makes it easier to update client code to use different allocators such |
---|
59 | as new/delete and new[]/delete[]. */ |
---|
60 | |
---|
61 | /* Scalar allocators. */ |
---|
62 | |
---|
63 | #define XALLOCA(T) ((T *) alloca (sizeof (T))) |
---|
64 | #define XNEW(T) ((T *) malloc (sizeof (T))) |
---|
65 | #define XCNEW(T) ((T *) calloc (1, sizeof (T))) |
---|
66 | #define XDUP(T, P) ((T *) memdup ((P), sizeof (T), sizeof (T))) |
---|
67 | #define XDELETE(P) free ((void*) (P)) |
---|
68 | |
---|
69 | /* Array allocators. */ |
---|
70 | |
---|
71 | #define XALLOCAVEC(T, N) ((T *) alloca (sizeof (T) * (N))) |
---|
72 | #define XNEWVEC(T, N) ((T *) malloc (sizeof (T) * (N))) |
---|
73 | #define XCNEWVEC(T, N) ((T *) calloc ((N), sizeof (T))) |
---|
74 | #define XDUPVEC(T, P, N) ((T *) memdup ((P), sizeof (T) * (N), sizeof (T) * (N))) |
---|
75 | #define XRESIZEVEC(T, P, N) ((T *) realloc ((void *) (P), sizeof (T) * (N))) |
---|
76 | #define XDELETEVEC(P) free ((void*) (P)) |
---|
77 | |
---|
78 | /* Allocators for variable-sized structures and raw buffers. */ |
---|
79 | |
---|
80 | #define XALLOCAVAR(T, S) ((T *) alloca ((S))) |
---|
81 | #define XNEWVAR(T, S) ((T *) malloc ((S))) |
---|
82 | #define XCNEWVAR(T, S) ((T *) calloc (1, (S))) |
---|
83 | #define XDUPVAR(T, P, S1, S2) ((T *) memdup ((P), (S1), (S2))) |
---|
84 | #define XRESIZEVAR(T, P, S) ((T *) realloc ((P), (S))) |
---|
85 | |
---|
86 | /* Concatenate an arbitrary number of strings. You must pass NULL as |
---|
87 | the last argument of this function, to terminate the list of |
---|
88 | strings. Allocates memory using xmalloc. */ |
---|
89 | |
---|
90 | extern char *concat (const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL; |
---|
91 | |
---|
92 | /* Concatenate an arbitrary number of strings. You must pass NULL as |
---|
93 | the last argument of this function, to terminate the list of |
---|
94 | strings. Allocates memory using xmalloc. The first argument is |
---|
95 | not one of the strings to be concatenated, but if not NULL is a |
---|
96 | pointer to be freed after the new string is created, similar to the |
---|
97 | way xrealloc works. */ |
---|
98 | |
---|
99 | extern char *reconcat (char *, const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL; |
---|
100 | |
---|
101 | /* Determine the length of concatenating an arbitrary number of |
---|
102 | strings. You must pass NULL as the last argument of this function, |
---|
103 | to terminate the list of strings. */ |
---|
104 | |
---|
105 | extern unsigned long concat_length (const char *, ...) ATTRIBUTE_SENTINEL; |
---|
106 | |
---|
107 | /* Concatenate an arbitrary number of strings into a SUPPLIED area of |
---|
108 | memory. You must pass NULL as the last argument of this function, |
---|
109 | to terminate the list of strings. The supplied memory is assumed |
---|
110 | to be large enough. */ |
---|
111 | |
---|
112 | extern char *concat_copy (char *, const char *, ...) ATTRIBUTE_SENTINEL; |
---|
113 | |
---|
114 | /* Concatenate an arbitrary number of strings into a GLOBAL area of |
---|
115 | memory. You must pass NULL as the last argument of this function, |
---|
116 | to terminate the list of strings. The supplied memory is assumed |
---|
117 | to be large enough. */ |
---|
118 | |
---|
119 | extern char *concat_copy2 (const char *, ...) ATTRIBUTE_SENTINEL; |
---|
120 | |
---|
121 | /* This is the global area used by concat_copy2. */ |
---|
122 | |
---|
123 | extern char *libiberty_concat_ptr; |
---|
124 | |
---|
125 | /* Return a temporary file name or NULL if unable to create one. */ |
---|
126 | |
---|
127 | extern char *make_temp_file (const char *) ATTRIBUTE_MALLOC; |
---|
128 | |
---|
129 | /* Flags for pex_init. These are bits to be or'ed together. */ |
---|
130 | |
---|
131 | /* Record subprocess times, if possible. */ |
---|
132 | #define PEX_RECORD_TIMES 0x1 |
---|
133 | |
---|
134 | /* Use pipes for communication between processes, if possible. */ |
---|
135 | #define PEX_USE_PIPES 0x2 |
---|
136 | |
---|
137 | /* Save files used for communication between processes. */ |
---|
138 | #define PEX_SAVE_TEMPS 0x4 |
---|
139 | |
---|
140 | /* Prepare to execute one or more programs, with standard output of |
---|
141 | each program fed to standard input of the next. |
---|
142 | FLAGS As above. |
---|
143 | PNAME The name of the program to report in error messages. |
---|
144 | TEMPBASE A base name to use for temporary files; may be NULL to |
---|
145 | use a random name. |
---|
146 | Returns NULL on error. */ |
---|
147 | |
---|
148 | extern struct pex_obj *pex_init (int flags, const char *pname, |
---|
149 | const char *tempbase); |
---|
150 | |
---|
151 | /* Flags for pex_run. These are bits to be or'ed together. */ |
---|
152 | |
---|
153 | /* Last program in pipeline. Standard output of program goes to |
---|
154 | OUTNAME, or, if OUTNAME is NULL, to standard output of caller. Do |
---|
155 | not set this if you want to call pex_read_output. After this is |
---|
156 | set, pex_run may no longer be called with the same struct |
---|
157 | pex_obj. */ |
---|
158 | #define PEX_LAST 0x1 |
---|
159 | |
---|
160 | /* Search for program in executable search path. */ |
---|
161 | #define PEX_SEARCH 0x2 |
---|
162 | |
---|
163 | /* OUTNAME is a suffix. */ |
---|
164 | #define PEX_SUFFIX 0x4 |
---|
165 | |
---|
166 | /* Send program's standard error to standard output. */ |
---|
167 | #define PEX_STDERR_TO_STDOUT 0x8 |
---|
168 | |
---|
169 | /* Input file should be opened in binary mode. This flag is ignored |
---|
170 | on Unix. */ |
---|
171 | #define PEX_BINARY_INPUT 0x10 |
---|
172 | |
---|
173 | /* Output file should be opened in binary mode. This flag is ignored |
---|
174 | on Unix. For proper behaviour PEX_BINARY_INPUT and |
---|
175 | PEX_BINARY_OUTPUT have to match appropriately--i.e., a call using |
---|
176 | PEX_BINARY_OUTPUT should be followed by a call using |
---|
177 | PEX_BINARY_INPUT. */ |
---|
178 | #define PEX_BINARY_OUTPUT 0x20 |
---|
179 | |
---|
180 | /* Capture stderr to a pipe. The output can be read by |
---|
181 | calling pex_read_err and reading from the returned |
---|
182 | FILE object. This flag may be specified only for |
---|
183 | the last program in a pipeline. |
---|
184 | |
---|
185 | This flag is supported only on Unix and Windows. */ |
---|
186 | #define PEX_STDERR_TO_PIPE 0x40 |
---|
187 | |
---|
188 | /* Capture stderr in binary mode. This flag is ignored |
---|
189 | on Unix. */ |
---|
190 | #define PEX_BINARY_ERROR 0x80 |
---|
191 | |
---|
192 | |
---|
193 | /* Execute one program. Returns NULL on success. On error returns an |
---|
194 | error string (typically just the name of a system call); the error |
---|
195 | string is statically allocated. |
---|
196 | |
---|
197 | OBJ Returned by pex_init. |
---|
198 | |
---|
199 | FLAGS As above. |
---|
200 | |
---|
201 | EXECUTABLE The program to execute. |
---|
202 | |
---|
203 | ARGV NULL terminated array of arguments to pass to the program. |
---|
204 | |
---|
205 | OUTNAME Sets the output file name as follows: |
---|
206 | |
---|
207 | PEX_SUFFIX set (OUTNAME may not be NULL): |
---|
208 | TEMPBASE parameter to pex_init not NULL: |
---|
209 | Output file name is the concatenation of TEMPBASE |
---|
210 | and OUTNAME. |
---|
211 | TEMPBASE is NULL: |
---|
212 | Output file name is a random file name ending in |
---|
213 | OUTNAME. |
---|
214 | PEX_SUFFIX not set: |
---|
215 | OUTNAME not NULL: |
---|
216 | Output file name is OUTNAME. |
---|
217 | OUTNAME NULL, TEMPBASE not NULL: |
---|
218 | Output file name is randomly chosen using |
---|
219 | TEMPBASE. |
---|
220 | OUTNAME NULL, TEMPBASE NULL: |
---|
221 | Output file name is randomly chosen. |
---|
222 | |
---|
223 | If PEX_LAST is not set, the output file name is the |
---|
224 | name to use for a temporary file holding stdout, if |
---|
225 | any (there will not be a file if PEX_USE_PIPES is set |
---|
226 | and the system supports pipes). If a file is used, it |
---|
227 | will be removed when no longer needed unless |
---|
228 | PEX_SAVE_TEMPS is set. |
---|
229 | |
---|
230 | If PEX_LAST is set, and OUTNAME is not NULL, standard |
---|
231 | output is written to the output file name. The file |
---|
232 | will not be removed. If PEX_LAST and PEX_SUFFIX are |
---|
233 | both set, TEMPBASE may not be NULL. |
---|
234 | |
---|
235 | ERRNAME If not NULL, this is the name of a file to which |
---|
236 | standard error is written. If NULL, standard error of |
---|
237 | the program is standard error of the caller. |
---|
238 | |
---|
239 | ERR On an error return, *ERR is set to an errno value, or |
---|
240 | to 0 if there is no relevant errno. |
---|
241 | */ |
---|
242 | |
---|
243 | extern const char *pex_run (struct pex_obj *obj, int flags, |
---|
244 | const char *executable, char * const *argv, |
---|
245 | const char *outname, const char *errname, |
---|
246 | int *err); |
---|
247 | |
---|
248 | /* As for pex_run (), but takes an extra parameter to enable the |
---|
249 | environment for the child process to be specified. |
---|
250 | |
---|
251 | ENV The environment for the child process, specified as |
---|
252 | an array of character pointers. Each element of the |
---|
253 | array should point to a string of the form VAR=VALUE, |
---|
254 | with the exception of the last element which must be |
---|
255 | a null pointer. |
---|
256 | */ |
---|
257 | |
---|
258 | extern const char *pex_run_in_environment (struct pex_obj *obj, int flags, |
---|
259 | const char *executable, |
---|
260 | char * const *argv, |
---|
261 | char * const *env, |
---|
262 | const char *outname, |
---|
263 | const char *errname, int *err); |
---|
264 | |
---|
265 | /* Return a stream for a temporary file to pass to the first program |
---|
266 | in the pipeline as input. The file name is chosen as for pex_run. |
---|
267 | pex_run closes the file automatically; don't close it yourself. */ |
---|
268 | |
---|
269 | extern FILE *pex_input_file (struct pex_obj *obj, int flags, |
---|
270 | const char *in_name); |
---|
271 | |
---|
272 | /* Return a stream for a pipe connected to the standard input of the |
---|
273 | first program in the pipeline. You must have passed |
---|
274 | `PEX_USE_PIPES' to `pex_init'. Close the returned stream |
---|
275 | yourself. */ |
---|
276 | |
---|
277 | extern FILE *pex_input_pipe (struct pex_obj *obj, int binary); |
---|
278 | |
---|
279 | /* Read the standard output of the last program to be executed. |
---|
280 | pex_run can not be called after this. BINARY should be non-zero if |
---|
281 | the file should be opened in binary mode; this is ignored on Unix. |
---|
282 | Returns NULL on error. Don't call fclose on the returned FILE; it |
---|
283 | will be closed by pex_free. */ |
---|
284 | |
---|
285 | extern FILE *pex_read_output (struct pex_obj *, int binary); |
---|
286 | |
---|
287 | /* Read the standard error of the last program to be executed. |
---|
288 | pex_run can not be called after this. BINARY should be non-zero if |
---|
289 | the file should be opened in binary mode; this is ignored on Unix. |
---|
290 | Returns NULL on error. Don't call fclose on the returned FILE; it |
---|
291 | will be closed by pex_free. */ |
---|
292 | |
---|
293 | extern FILE *pex_read_err (struct pex_obj *, int binary); |
---|
294 | |
---|
295 | /* Return exit status of all programs in VECTOR. COUNT indicates the |
---|
296 | size of VECTOR. The status codes in the vector are in the order of |
---|
297 | the calls to pex_run. Returns 0 on error, 1 on success. */ |
---|
298 | |
---|
299 | extern int pex_get_status (struct pex_obj *, int count, int *vector); |
---|
300 | |
---|
301 | /* Return times of all programs in VECTOR. COUNT indicates the size |
---|
302 | of VECTOR. struct pex_time is really just struct timeval, but that |
---|
303 | is not portable to all systems. Returns 0 on error, 1 on |
---|
304 | success. */ |
---|
305 | |
---|
306 | struct pex_time |
---|
307 | { |
---|
308 | unsigned long user_seconds; |
---|
309 | unsigned long user_microseconds; |
---|
310 | unsigned long system_seconds; |
---|
311 | unsigned long system_microseconds; |
---|
312 | }; |
---|
313 | |
---|
314 | extern int pex_get_times (struct pex_obj *, int count, |
---|
315 | struct pex_time *vector); |
---|
316 | |
---|
317 | /* Clean up a pex_obj. If you have not called pex_get_times or |
---|
318 | pex_get_status, this will try to kill the subprocesses. */ |
---|
319 | |
---|
320 | extern void pex_free (struct pex_obj *); |
---|
321 | |
---|
322 | /* Just execute one program. Return value is as for pex_run. |
---|
323 | FLAGS Combination of PEX_SEARCH and PEX_STDERR_TO_STDOUT. |
---|
324 | EXECUTABLE As for pex_run. |
---|
325 | ARGV As for pex_run. |
---|
326 | PNAME As for pex_init. |
---|
327 | OUTNAME As for pex_run when PEX_LAST is set. |
---|
328 | ERRNAME As for pex_run. |
---|
329 | STATUS Set to exit status on success. |
---|
330 | ERR As for pex_run. |
---|
331 | */ |
---|
332 | |
---|
333 | extern const char *pex_one (int flags, const char *executable, |
---|
334 | char * const *argv, const char *pname, |
---|
335 | const char *outname, const char *errname, |
---|
336 | int *status, int *err); |
---|
337 | |
---|
338 | #ifdef __cplusplus |
---|
339 | } |
---|
340 | #endif |
---|
341 | |
---|
342 | #endif |
---|