xref: /openbsd-src/gnu/usr.bin/binutils-2.17/include/libiberty.h (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* Function declarations for libiberty.
2*3d8817e4Smiod 
3*3d8817e4Smiod    Copyright 2001, 2002, 2005 Free Software Foundation, Inc.
4*3d8817e4Smiod 
5*3d8817e4Smiod    Note - certain prototypes declared in this header file are for
6*3d8817e4Smiod    functions whoes implementation copyright does not belong to the
7*3d8817e4Smiod    FSF.  Those prototypes are present in this file for reference
8*3d8817e4Smiod    purposes only and their presence in this file should not construed
9*3d8817e4Smiod    as an indication of ownership by the FSF of the implementation of
10*3d8817e4Smiod    those functions in any way or form whatsoever.
11*3d8817e4Smiod 
12*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
13*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
14*3d8817e4Smiod    the Free Software Foundation; either version 2, or (at your option)
15*3d8817e4Smiod    any later version.
16*3d8817e4Smiod 
17*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
18*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
19*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20*3d8817e4Smiod    GNU General Public License for more details.
21*3d8817e4Smiod 
22*3d8817e4Smiod    You should have received a copy of the GNU General Public License
23*3d8817e4Smiod    along with this program; if not, write to the Free Software
24*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor,
25*3d8817e4Smiod    Boston, MA 02110-1301, USA.
26*3d8817e4Smiod 
27*3d8817e4Smiod    Written by Cygnus Support, 1994.
28*3d8817e4Smiod 
29*3d8817e4Smiod    The libiberty library provides a number of functions which are
30*3d8817e4Smiod    missing on some operating systems.  We do not declare those here,
31*3d8817e4Smiod    to avoid conflicts with the system header files on operating
32*3d8817e4Smiod    systems that do support those functions.  In this file we only
33*3d8817e4Smiod    declare those functions which are specific to libiberty.  */
34*3d8817e4Smiod 
35*3d8817e4Smiod #ifndef LIBIBERTY_H
36*3d8817e4Smiod #define LIBIBERTY_H
37*3d8817e4Smiod 
38*3d8817e4Smiod #ifdef __cplusplus
39*3d8817e4Smiod extern "C" {
40*3d8817e4Smiod #endif
41*3d8817e4Smiod 
42*3d8817e4Smiod #include "ansidecl.h"
43*3d8817e4Smiod 
44*3d8817e4Smiod /* Get a definition for size_t.  */
45*3d8817e4Smiod #include <stddef.h>
46*3d8817e4Smiod /* Get a definition for va_list.  */
47*3d8817e4Smiod #include <stdarg.h>
48*3d8817e4Smiod 
49*3d8817e4Smiod #include <stdio.h>
50*3d8817e4Smiod 
51*3d8817e4Smiod /* If the OS supports it, ensure that the supplied stream is setup to
52*3d8817e4Smiod    avoid any multi-threaded locking.  Otherwise leave the FILE pointer
53*3d8817e4Smiod    unchanged.  If the stream is NULL do nothing.  */
54*3d8817e4Smiod 
55*3d8817e4Smiod extern void unlock_stream (FILE *);
56*3d8817e4Smiod 
57*3d8817e4Smiod /* If the OS supports it, ensure that the standard I/O streams, stdin,
58*3d8817e4Smiod    stdout and stderr are setup to avoid any multi-threaded locking.
59*3d8817e4Smiod    Otherwise do nothing.  */
60*3d8817e4Smiod 
61*3d8817e4Smiod extern void unlock_std_streams (void);
62*3d8817e4Smiod 
63*3d8817e4Smiod /* Open and return a FILE pointer.  If the OS supports it, ensure that
64*3d8817e4Smiod    the stream is setup to avoid any multi-threaded locking.  Otherwise
65*3d8817e4Smiod    return the FILE pointer unchanged.  */
66*3d8817e4Smiod 
67*3d8817e4Smiod extern FILE *fopen_unlocked (const char *, const char *);
68*3d8817e4Smiod extern FILE *fdopen_unlocked (int, const char *);
69*3d8817e4Smiod extern FILE *freopen_unlocked (const char *, const char *, FILE *);
70*3d8817e4Smiod 
71*3d8817e4Smiod /* Build an argument vector from a string.  Allocates memory using
72*3d8817e4Smiod    malloc.  Use freeargv to free the vector.  */
73*3d8817e4Smiod 
74*3d8817e4Smiod extern char **buildargv (const char *) ATTRIBUTE_MALLOC;
75*3d8817e4Smiod 
76*3d8817e4Smiod /* Free a vector returned by buildargv.  */
77*3d8817e4Smiod 
78*3d8817e4Smiod extern void freeargv (char **);
79*3d8817e4Smiod 
80*3d8817e4Smiod /* Duplicate an argument vector. Allocates memory using malloc.  Use
81*3d8817e4Smiod    freeargv to free the vector.  */
82*3d8817e4Smiod 
83*3d8817e4Smiod extern char **dupargv (char **) ATTRIBUTE_MALLOC;
84*3d8817e4Smiod 
85*3d8817e4Smiod /* Expand "@file" arguments in argv.  */
86*3d8817e4Smiod 
87*3d8817e4Smiod extern void expandargv PARAMS ((int *, char ***));
88*3d8817e4Smiod 
89*3d8817e4Smiod /* Return the last component of a path name.  Note that we can't use a
90*3d8817e4Smiod    prototype here because the parameter is declared inconsistently
91*3d8817e4Smiod    across different systems, sometimes as "char *" and sometimes as
92*3d8817e4Smiod    "const char *" */
93*3d8817e4Smiod 
94*3d8817e4Smiod /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1.  If it is
95*3d8817e4Smiod    undefined, we haven't run the autoconf check so provide the
96*3d8817e4Smiod    declaration without arguments.  If it is 0, we checked and failed
97*3d8817e4Smiod    to find the declaration so provide a fully prototyped one.  If it
98*3d8817e4Smiod    is 1, we found it so don't provide any declaration at all.  */
99*3d8817e4Smiod #if !HAVE_DECL_BASENAME
100*3d8817e4Smiod #if defined (__GNU_LIBRARY__ ) || defined (__linux__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__) || defined (__CYGWIN__) || defined (__CYGWIN32__) || defined (__MINGW32__) || defined (HAVE_DECL_BASENAME)
101*3d8817e4Smiod extern char *basename (const char *);
102*3d8817e4Smiod #else
103*3d8817e4Smiod /* Do not allow basename to be used if there is no prototype seen.  We
104*3d8817e4Smiod    either need to use the above prototype or have one from
105*3d8817e4Smiod    autoconf which would result in HAVE_DECL_BASENAME being set.  */
106*3d8817e4Smiod #define basename basename_cannot_be_used_without_a_prototype
107*3d8817e4Smiod #endif
108*3d8817e4Smiod #endif
109*3d8817e4Smiod 
110*3d8817e4Smiod /* A well-defined basename () that is always compiled in.  */
111*3d8817e4Smiod 
112*3d8817e4Smiod extern const char *lbasename (const char *);
113*3d8817e4Smiod 
114*3d8817e4Smiod /* A well-defined realpath () that is always compiled in.  */
115*3d8817e4Smiod 
116*3d8817e4Smiod extern char *lrealpath (const char *);
117*3d8817e4Smiod 
118*3d8817e4Smiod /* Concatenate an arbitrary number of strings.  You must pass NULL as
119*3d8817e4Smiod    the last argument of this function, to terminate the list of
120*3d8817e4Smiod    strings.  Allocates memory using xmalloc.  */
121*3d8817e4Smiod 
122*3d8817e4Smiod extern char *concat (const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
123*3d8817e4Smiod 
124*3d8817e4Smiod /* Concatenate an arbitrary number of strings.  You must pass NULL as
125*3d8817e4Smiod    the last argument of this function, to terminate the list of
126*3d8817e4Smiod    strings.  Allocates memory using xmalloc.  The first argument is
127*3d8817e4Smiod    not one of the strings to be concatenated, but if not NULL is a
128*3d8817e4Smiod    pointer to be freed after the new string is created, similar to the
129*3d8817e4Smiod    way xrealloc works.  */
130*3d8817e4Smiod 
131*3d8817e4Smiod extern char *reconcat (char *, const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
132*3d8817e4Smiod 
133*3d8817e4Smiod /* Determine the length of concatenating an arbitrary number of
134*3d8817e4Smiod    strings.  You must pass NULL as the last argument of this function,
135*3d8817e4Smiod    to terminate the list of strings.  */
136*3d8817e4Smiod 
137*3d8817e4Smiod extern unsigned long concat_length (const char *, ...) ATTRIBUTE_SENTINEL;
138*3d8817e4Smiod 
139*3d8817e4Smiod /* Concatenate an arbitrary number of strings into a SUPPLIED area of
140*3d8817e4Smiod    memory.  You must pass NULL as the last argument of this function,
141*3d8817e4Smiod    to terminate the list of strings.  The supplied memory is assumed
142*3d8817e4Smiod    to be large enough.  */
143*3d8817e4Smiod 
144*3d8817e4Smiod extern char *concat_copy (char *, const char *, ...) ATTRIBUTE_SENTINEL;
145*3d8817e4Smiod 
146*3d8817e4Smiod /* Concatenate an arbitrary number of strings into a GLOBAL area of
147*3d8817e4Smiod    memory.  You must pass NULL as the last argument of this function,
148*3d8817e4Smiod    to terminate the list of strings.  The supplied memory is assumed
149*3d8817e4Smiod    to be large enough.  */
150*3d8817e4Smiod 
151*3d8817e4Smiod extern char *concat_copy2 (const char *, ...) ATTRIBUTE_SENTINEL;
152*3d8817e4Smiod 
153*3d8817e4Smiod /* This is the global area used by concat_copy2.  */
154*3d8817e4Smiod 
155*3d8817e4Smiod extern char *libiberty_concat_ptr;
156*3d8817e4Smiod 
157*3d8817e4Smiod /* Concatenate an arbitrary number of strings.  You must pass NULL as
158*3d8817e4Smiod    the last argument of this function, to terminate the list of
159*3d8817e4Smiod    strings.  Allocates memory using alloca.  The arguments are
160*3d8817e4Smiod    evaluated twice!  */
161*3d8817e4Smiod #define ACONCAT(ACONCAT_PARAMS) \
162*3d8817e4Smiod   (libiberty_concat_ptr = (char *) alloca (concat_length ACONCAT_PARAMS + 1), \
163*3d8817e4Smiod    concat_copy2 ACONCAT_PARAMS)
164*3d8817e4Smiod 
165*3d8817e4Smiod /* Check whether two file descriptors refer to the same file.  */
166*3d8817e4Smiod 
167*3d8817e4Smiod extern int fdmatch (int fd1, int fd2);
168*3d8817e4Smiod 
169*3d8817e4Smiod /* Return the position of the first bit set in the argument.  */
170*3d8817e4Smiod /* Prototypes vary from system to system, so we only provide a
171*3d8817e4Smiod    prototype on systems where we know that we need it.  */
172*3d8817e4Smiod #if defined (HAVE_DECL_FFS) && !HAVE_DECL_FFS
173*3d8817e4Smiod extern int ffs(int);
174*3d8817e4Smiod #endif
175*3d8817e4Smiod 
176*3d8817e4Smiod /* Get the working directory.  The result is cached, so don't call
177*3d8817e4Smiod    chdir() between calls to getpwd().  */
178*3d8817e4Smiod 
179*3d8817e4Smiod extern char * getpwd (void);
180*3d8817e4Smiod 
181*3d8817e4Smiod /* Get the current time.  */
182*3d8817e4Smiod /* Prototypes vary from system to system, so we only provide a
183*3d8817e4Smiod    prototype on systems where we know that we need it.  */
184*3d8817e4Smiod #ifdef __MINGW32__
185*3d8817e4Smiod /* Forward declaration to avoid #include <sys/time.h>.   */
186*3d8817e4Smiod struct timeval;
187*3d8817e4Smiod extern int gettimeofday (struct timeval *, void *);
188*3d8817e4Smiod #endif
189*3d8817e4Smiod 
190*3d8817e4Smiod /* Get the amount of time the process has run, in microseconds.  */
191*3d8817e4Smiod 
192*3d8817e4Smiod extern long get_run_time (void);
193*3d8817e4Smiod 
194*3d8817e4Smiod /* Generate a relocated path to some installation directory.  Allocates
195*3d8817e4Smiod    return value using malloc.  */
196*3d8817e4Smiod 
197*3d8817e4Smiod extern char *make_relative_prefix (const char *, const char *,
198*3d8817e4Smiod                                    const char *) ATTRIBUTE_MALLOC;
199*3d8817e4Smiod 
200*3d8817e4Smiod /* Choose a temporary directory to use for scratch files.  */
201*3d8817e4Smiod 
202*3d8817e4Smiod extern char *choose_temp_base (void) ATTRIBUTE_MALLOC;
203*3d8817e4Smiod 
204*3d8817e4Smiod /* Return a temporary file name or NULL if unable to create one.  */
205*3d8817e4Smiod 
206*3d8817e4Smiod extern char *make_temp_file (const char *) ATTRIBUTE_MALLOC;
207*3d8817e4Smiod 
208*3d8817e4Smiod /* Remove a link to a file unless it is special. */
209*3d8817e4Smiod 
210*3d8817e4Smiod extern int unlink_if_ordinary (const char *);
211*3d8817e4Smiod 
212*3d8817e4Smiod /* Allocate memory filled with spaces.  Allocates using malloc.  */
213*3d8817e4Smiod 
214*3d8817e4Smiod extern const char *spaces (int count);
215*3d8817e4Smiod 
216*3d8817e4Smiod /* Return the maximum error number for which strerror will return a
217*3d8817e4Smiod    string.  */
218*3d8817e4Smiod 
219*3d8817e4Smiod extern int errno_max (void);
220*3d8817e4Smiod 
221*3d8817e4Smiod /* Return the name of an errno value (e.g., strerrno (EINVAL) returns
222*3d8817e4Smiod    "EINVAL").  */
223*3d8817e4Smiod 
224*3d8817e4Smiod extern const char *strerrno (int);
225*3d8817e4Smiod 
226*3d8817e4Smiod /* Given the name of an errno value, return the value.  */
227*3d8817e4Smiod 
228*3d8817e4Smiod extern int strtoerrno (const char *);
229*3d8817e4Smiod 
230*3d8817e4Smiod /* ANSI's strerror(), but more robust.  */
231*3d8817e4Smiod 
232*3d8817e4Smiod extern char *xstrerror (int);
233*3d8817e4Smiod 
234*3d8817e4Smiod /* Return the maximum signal number for which strsignal will return a
235*3d8817e4Smiod    string.  */
236*3d8817e4Smiod 
237*3d8817e4Smiod extern int signo_max (void);
238*3d8817e4Smiod 
239*3d8817e4Smiod /* Return a signal message string for a signal number
240*3d8817e4Smiod    (e.g., strsignal (SIGHUP) returns something like "Hangup").  */
241*3d8817e4Smiod /* This is commented out as it can conflict with one in system headers.
242*3d8817e4Smiod    We still document its existence though.  */
243*3d8817e4Smiod 
244*3d8817e4Smiod /*extern const char *strsignal (int);*/
245*3d8817e4Smiod 
246*3d8817e4Smiod /* Return the name of a signal number (e.g., strsigno (SIGHUP) returns
247*3d8817e4Smiod    "SIGHUP").  */
248*3d8817e4Smiod 
249*3d8817e4Smiod extern const char *strsigno (int);
250*3d8817e4Smiod 
251*3d8817e4Smiod /* Given the name of a signal, return its number.  */
252*3d8817e4Smiod 
253*3d8817e4Smiod extern int strtosigno (const char *);
254*3d8817e4Smiod 
255*3d8817e4Smiod /* Register a function to be run by xexit.  Returns 0 on success.  */
256*3d8817e4Smiod 
257*3d8817e4Smiod extern int xatexit (void (*fn) (void));
258*3d8817e4Smiod 
259*3d8817e4Smiod /* Exit, calling all the functions registered with xatexit.  */
260*3d8817e4Smiod 
261*3d8817e4Smiod extern void xexit (int status) ATTRIBUTE_NORETURN;
262*3d8817e4Smiod 
263*3d8817e4Smiod /* Set the program name used by xmalloc.  */
264*3d8817e4Smiod 
265*3d8817e4Smiod extern void xmalloc_set_program_name (const char *);
266*3d8817e4Smiod 
267*3d8817e4Smiod /* Report an allocation failure.  */
268*3d8817e4Smiod extern void xmalloc_failed (size_t) ATTRIBUTE_NORETURN;
269*3d8817e4Smiod 
270*3d8817e4Smiod /* Allocate memory without fail.  If malloc fails, this will print a
271*3d8817e4Smiod    message to stderr (using the name set by xmalloc_set_program_name,
272*3d8817e4Smiod    if any) and then call xexit.  */
273*3d8817e4Smiod 
274*3d8817e4Smiod extern void *xmalloc (size_t) ATTRIBUTE_MALLOC;
275*3d8817e4Smiod 
276*3d8817e4Smiod /* Reallocate memory without fail.  This works like xmalloc.  Note,
277*3d8817e4Smiod    realloc type functions are not suitable for attribute malloc since
278*3d8817e4Smiod    they may return the same address across multiple calls. */
279*3d8817e4Smiod 
280*3d8817e4Smiod extern void *xrealloc (void *, size_t);
281*3d8817e4Smiod 
282*3d8817e4Smiod /* Allocate memory without fail and set it to zero.  This works like
283*3d8817e4Smiod    xmalloc.  */
284*3d8817e4Smiod 
285*3d8817e4Smiod extern void *xcalloc (size_t, size_t) ATTRIBUTE_MALLOC;
286*3d8817e4Smiod 
287*3d8817e4Smiod /* Copy a string into a memory buffer without fail.  */
288*3d8817e4Smiod 
289*3d8817e4Smiod extern char *xstrdup (const char *) ATTRIBUTE_MALLOC;
290*3d8817e4Smiod 
291*3d8817e4Smiod /* Copy at most N characters from string into a buffer without fail.  */
292*3d8817e4Smiod 
293*3d8817e4Smiod extern char *xstrndup (const char *, size_t) ATTRIBUTE_MALLOC;
294*3d8817e4Smiod 
295*3d8817e4Smiod /* Copy an existing memory buffer to a new memory buffer without fail.  */
296*3d8817e4Smiod 
297*3d8817e4Smiod extern void *xmemdup (const void *, size_t, size_t) ATTRIBUTE_MALLOC;
298*3d8817e4Smiod 
299*3d8817e4Smiod /* Physical memory routines.  Return values are in BYTES.  */
300*3d8817e4Smiod extern double physmem_total (void);
301*3d8817e4Smiod extern double physmem_available (void);
302*3d8817e4Smiod 
303*3d8817e4Smiod 
304*3d8817e4Smiod /* These macros provide a K&R/C89/C++-friendly way of allocating structures
305*3d8817e4Smiod    with nice encapsulation.  The XDELETE*() macros are technically
306*3d8817e4Smiod    superfluous, but provided here for symmetry.  Using them consistently
307*3d8817e4Smiod    makes it easier to update client code to use different allocators such
308*3d8817e4Smiod    as new/delete and new[]/delete[].  */
309*3d8817e4Smiod 
310*3d8817e4Smiod /* Scalar allocators.  */
311*3d8817e4Smiod 
312*3d8817e4Smiod #define XNEW(T)			((T *) xmalloc (sizeof (T)))
313*3d8817e4Smiod #define XCNEW(T)		((T *) xcalloc (1, sizeof (T)))
314*3d8817e4Smiod #define XDELETE(P)		free ((void*) (P))
315*3d8817e4Smiod 
316*3d8817e4Smiod /* Array allocators.  */
317*3d8817e4Smiod 
318*3d8817e4Smiod #define XNEWVEC(T, N)		((T *) xmalloc (sizeof (T) * (N)))
319*3d8817e4Smiod #define XCNEWVEC(T, N)		((T *) xcalloc ((N), sizeof (T)))
320*3d8817e4Smiod #define XRESIZEVEC(T, P, N)	((T *) xrealloc ((void *) (P), sizeof (T) * (N)))
321*3d8817e4Smiod #define XDELETEVEC(P)		free ((void*) (P))
322*3d8817e4Smiod 
323*3d8817e4Smiod /* Allocators for variable-sized structures and raw buffers.  */
324*3d8817e4Smiod 
325*3d8817e4Smiod #define XNEWVAR(T, S)		((T *) xmalloc ((S)))
326*3d8817e4Smiod #define XCNEWVAR(T, S)		((T *) xcalloc (1, (S)))
327*3d8817e4Smiod #define XRESIZEVAR(T, P, S)	((T *) xrealloc ((P), (S)))
328*3d8817e4Smiod 
329*3d8817e4Smiod /* Type-safe obstack allocator.  */
330*3d8817e4Smiod 
331*3d8817e4Smiod #define XOBNEW(O, T)		((T *) obstack_alloc ((O), sizeof (T)))
332*3d8817e4Smiod #define XOBFINISH(O, T)         ((T) obstack_finish ((O)))
333*3d8817e4Smiod 
334*3d8817e4Smiod /* hex character manipulation routines */
335*3d8817e4Smiod 
336*3d8817e4Smiod #define _hex_array_size 256
337*3d8817e4Smiod #define _hex_bad	99
338*3d8817e4Smiod extern const unsigned char _hex_value[_hex_array_size];
339*3d8817e4Smiod extern void hex_init (void);
340*3d8817e4Smiod #define hex_p(c)	(hex_value (c) != _hex_bad)
341*3d8817e4Smiod /* If you change this, note well: Some code relies on side effects in
342*3d8817e4Smiod    the argument being performed exactly once.  */
343*3d8817e4Smiod #define hex_value(c)	((unsigned int) _hex_value[(unsigned char) (c)])
344*3d8817e4Smiod 
345*3d8817e4Smiod /* Flags for pex_init.  These are bits to be or'ed together.  */
346*3d8817e4Smiod 
347*3d8817e4Smiod /* Record subprocess times, if possible.  */
348*3d8817e4Smiod #define PEX_RECORD_TIMES	0x1
349*3d8817e4Smiod 
350*3d8817e4Smiod /* Use pipes for communication between processes, if possible.  */
351*3d8817e4Smiod #define PEX_USE_PIPES		0x2
352*3d8817e4Smiod 
353*3d8817e4Smiod /* Save files used for communication between processes.  */
354*3d8817e4Smiod #define PEX_SAVE_TEMPS		0x4
355*3d8817e4Smiod 
356*3d8817e4Smiod /* Prepare to execute one or more programs, with standard output of
357*3d8817e4Smiod    each program fed to standard input of the next.
358*3d8817e4Smiod    FLAGS	As above.
359*3d8817e4Smiod    PNAME	The name of the program to report in error messages.
360*3d8817e4Smiod    TEMPBASE	A base name to use for temporary files; may be NULL to
361*3d8817e4Smiod    		use a random name.
362*3d8817e4Smiod    Returns NULL on error.  */
363*3d8817e4Smiod 
364*3d8817e4Smiod extern struct pex_obj *pex_init (int flags, const char *pname,
365*3d8817e4Smiod 				 const char *tempbase);
366*3d8817e4Smiod 
367*3d8817e4Smiod /* Flags for pex_run.  These are bits to be or'ed together.  */
368*3d8817e4Smiod 
369*3d8817e4Smiod /* Last program in pipeline.  Standard output of program goes to
370*3d8817e4Smiod    OUTNAME, or, if OUTNAME is NULL, to standard output of caller.  Do
371*3d8817e4Smiod    not set this if you want to call pex_read_output.  After this is
372*3d8817e4Smiod    set, pex_run may no longer be called with the same struct
373*3d8817e4Smiod    pex_obj.  */
374*3d8817e4Smiod #define PEX_LAST		0x1
375*3d8817e4Smiod 
376*3d8817e4Smiod /* Search for program in executable search path.  */
377*3d8817e4Smiod #define PEX_SEARCH		0x2
378*3d8817e4Smiod 
379*3d8817e4Smiod /* OUTNAME is a suffix.  */
380*3d8817e4Smiod #define PEX_SUFFIX		0x4
381*3d8817e4Smiod 
382*3d8817e4Smiod /* Send program's standard error to standard output.  */
383*3d8817e4Smiod #define PEX_STDERR_TO_STDOUT	0x8
384*3d8817e4Smiod 
385*3d8817e4Smiod /* Input file should be opened in binary mode.  This flag is ignored
386*3d8817e4Smiod    on Unix.  */
387*3d8817e4Smiod #define PEX_BINARY_INPUT	0x10
388*3d8817e4Smiod 
389*3d8817e4Smiod /* Output file should be opened in binary mode.  This flag is ignored
390*3d8817e4Smiod    on Unix.  For proper behaviour PEX_BINARY_INPUT and
391*3d8817e4Smiod    PEX_BINARY_OUTPUT have to match appropriately--i.e., a call using
392*3d8817e4Smiod    PEX_BINARY_OUTPUT should be followed by a call using
393*3d8817e4Smiod    PEX_BINARY_INPUT.  */
394*3d8817e4Smiod #define PEX_BINARY_OUTPUT	0x20
395*3d8817e4Smiod 
396*3d8817e4Smiod /* Execute one program.  Returns NULL on success.  On error returns an
397*3d8817e4Smiod    error string (typically just the name of a system call); the error
398*3d8817e4Smiod    string is statically allocated.
399*3d8817e4Smiod 
400*3d8817e4Smiod    OBJ		Returned by pex_init.
401*3d8817e4Smiod 
402*3d8817e4Smiod    FLAGS	As above.
403*3d8817e4Smiod 
404*3d8817e4Smiod    EXECUTABLE	The program to execute.
405*3d8817e4Smiod 
406*3d8817e4Smiod    ARGV		NULL terminated array of arguments to pass to the program.
407*3d8817e4Smiod 
408*3d8817e4Smiod    OUTNAME	Sets the output file name as follows:
409*3d8817e4Smiod 
410*3d8817e4Smiod 		PEX_SUFFIX set (OUTNAME may not be NULL):
411*3d8817e4Smiod 		  TEMPBASE parameter to pex_init not NULL:
412*3d8817e4Smiod 		    Output file name is the concatenation of TEMPBASE
413*3d8817e4Smiod 		    and OUTNAME.
414*3d8817e4Smiod 		  TEMPBASE is NULL:
415*3d8817e4Smiod 		    Output file name is a random file name ending in
416*3d8817e4Smiod 		    OUTNAME.
417*3d8817e4Smiod 		PEX_SUFFIX not set:
418*3d8817e4Smiod 		  OUTNAME not NULL:
419*3d8817e4Smiod 		    Output file name is OUTNAME.
420*3d8817e4Smiod 		  OUTNAME NULL, TEMPBASE not NULL:
421*3d8817e4Smiod 		    Output file name is randomly chosen using
422*3d8817e4Smiod 		    TEMPBASE.
423*3d8817e4Smiod 		  OUTNAME NULL, TEMPBASE NULL:
424*3d8817e4Smiod 		    Output file name is randomly chosen.
425*3d8817e4Smiod 
426*3d8817e4Smiod 		If PEX_LAST is not set, the output file name is the
427*3d8817e4Smiod    		name to use for a temporary file holding stdout, if
428*3d8817e4Smiod    		any (there will not be a file if PEX_USE_PIPES is set
429*3d8817e4Smiod    		and the system supports pipes).  If a file is used, it
430*3d8817e4Smiod    		will be removed when no longer needed unless
431*3d8817e4Smiod    		PEX_SAVE_TEMPS is set.
432*3d8817e4Smiod 
433*3d8817e4Smiod 		If PEX_LAST is set, and OUTNAME is not NULL, standard
434*3d8817e4Smiod    		output is written to the output file name.  The file
435*3d8817e4Smiod    		will not be removed.  If PEX_LAST and PEX_SUFFIX are
436*3d8817e4Smiod    		both set, TEMPBASE may not be NULL.
437*3d8817e4Smiod 
438*3d8817e4Smiod    ERRNAME	If not NULL, this is the name of a file to which
439*3d8817e4Smiod 		standard error is written.  If NULL, standard error of
440*3d8817e4Smiod 		the program is standard error of the caller.
441*3d8817e4Smiod 
442*3d8817e4Smiod    ERR		On an error return, *ERR is set to an errno value, or
443*3d8817e4Smiod    		to 0 if there is no relevant errno.
444*3d8817e4Smiod */
445*3d8817e4Smiod 
446*3d8817e4Smiod extern const char *pex_run (struct pex_obj *obj, int flags,
447*3d8817e4Smiod 			    const char *executable, char * const *argv,
448*3d8817e4Smiod 			    const char *outname, const char *errname,
449*3d8817e4Smiod 			    int *err);
450*3d8817e4Smiod 
451*3d8817e4Smiod /* Return a `FILE' pointer FP for the standard input of the first
452*3d8817e4Smiod    program in the pipeline; FP is opened for writing.  You must have
453*3d8817e4Smiod    passed `PEX_USE_PIPES' to the `pex_init' call that returned OBJ.
454*3d8817e4Smiod    You must close FP yourself with `fclose' to indicate that the
455*3d8817e4Smiod    pipeline's input is complete.
456*3d8817e4Smiod 
457*3d8817e4Smiod    The file descriptor underlying FP is marked not to be inherited by
458*3d8817e4Smiod    child processes.
459*3d8817e4Smiod 
460*3d8817e4Smiod    This call is not supported on systems which do not support pipes;
461*3d8817e4Smiod    it returns with an error.  (We could implement it by writing a
462*3d8817e4Smiod    temporary file, but then you would need to write all your data and
463*3d8817e4Smiod    close FP before your first call to `pex_run' -- and that wouldn't
464*3d8817e4Smiod    work on systems that do support pipes: the pipe would fill up, and
465*3d8817e4Smiod    you would block.  So there isn't any easy way to conceal the
466*3d8817e4Smiod    differences between the two types of systems.)
467*3d8817e4Smiod 
468*3d8817e4Smiod    If you call both `pex_write_input' and `pex_read_output', be
469*3d8817e4Smiod    careful to avoid deadlock.  If the output pipe fills up, so that
470*3d8817e4Smiod    each program in the pipeline is waiting for the next to read more
471*3d8817e4Smiod    data, and you fill the input pipe by writing more data to FP, then
472*3d8817e4Smiod    there is no way to make progress: the only process that could read
473*3d8817e4Smiod    data from the output pipe is you, but you are blocked on the input
474*3d8817e4Smiod    pipe.  */
475*3d8817e4Smiod 
476*3d8817e4Smiod extern FILE *pex_write_input (struct pex_obj *obj, int binary);
477*3d8817e4Smiod 
478*3d8817e4Smiod /* Return a stream for a temporary file to pass to the first program
479*3d8817e4Smiod    in the pipeline as input.  The file name is chosen as for pex_run.
480*3d8817e4Smiod    pex_run closes the file automatically; don't close it yourself.  */
481*3d8817e4Smiod 
482*3d8817e4Smiod extern FILE *pex_input_file (struct pex_obj *obj, int flags,
483*3d8817e4Smiod                              const char *in_name);
484*3d8817e4Smiod 
485*3d8817e4Smiod /* Return a stream for a pipe connected to the standard input of the
486*3d8817e4Smiod    first program in the pipeline.  You must have passed
487*3d8817e4Smiod    `PEX_USE_PIPES' to `pex_init'.  Close the returned stream
488*3d8817e4Smiod    yourself.  */
489*3d8817e4Smiod 
490*3d8817e4Smiod extern FILE *pex_input_pipe (struct pex_obj *obj, int binary);
491*3d8817e4Smiod 
492*3d8817e4Smiod /* Read the standard output of the last program to be executed.
493*3d8817e4Smiod    pex_run can not be called after this.  BINARY should be non-zero if
494*3d8817e4Smiod    the file should be opened in binary mode; this is ignored on Unix.
495*3d8817e4Smiod    Returns NULL on error.  Don't call fclose on the returned FILE; it
496*3d8817e4Smiod    will be closed by pex_free.  */
497*3d8817e4Smiod 
498*3d8817e4Smiod extern FILE *pex_read_output (struct pex_obj *, int binary);
499*3d8817e4Smiod 
500*3d8817e4Smiod /* Return exit status of all programs in VECTOR.  COUNT indicates the
501*3d8817e4Smiod    size of VECTOR.  The status codes in the vector are in the order of
502*3d8817e4Smiod    the calls to pex_run.  Returns 0 on error, 1 on success.  */
503*3d8817e4Smiod 
504*3d8817e4Smiod extern int pex_get_status (struct pex_obj *, int count, int *vector);
505*3d8817e4Smiod 
506*3d8817e4Smiod /* Return times of all programs in VECTOR.  COUNT indicates the size
507*3d8817e4Smiod    of VECTOR.  struct pex_time is really just struct timeval, but that
508*3d8817e4Smiod    is not portable to all systems.  Returns 0 on error, 1 on
509*3d8817e4Smiod    success.  */
510*3d8817e4Smiod 
511*3d8817e4Smiod struct pex_time
512*3d8817e4Smiod {
513*3d8817e4Smiod   unsigned long user_seconds;
514*3d8817e4Smiod   unsigned long user_microseconds;
515*3d8817e4Smiod   unsigned long system_seconds;
516*3d8817e4Smiod   unsigned long system_microseconds;
517*3d8817e4Smiod };
518*3d8817e4Smiod 
519*3d8817e4Smiod extern int pex_get_times (struct pex_obj *, int count,
520*3d8817e4Smiod 			  struct pex_time *vector);
521*3d8817e4Smiod 
522*3d8817e4Smiod /* Clean up a pex_obj.  */
523*3d8817e4Smiod 
524*3d8817e4Smiod extern void pex_free (struct pex_obj *);
525*3d8817e4Smiod 
526*3d8817e4Smiod /* Just execute one program.  Return value is as for pex_run.
527*3d8817e4Smiod    FLAGS	Combination of PEX_SEARCH and PEX_STDERR_TO_STDOUT.
528*3d8817e4Smiod    EXECUTABLE	As for pex_run.
529*3d8817e4Smiod    ARGV		As for pex_run.
530*3d8817e4Smiod    PNAME	As for pex_init.
531*3d8817e4Smiod    OUTNAME	As for pex_run when PEX_LAST is set.
532*3d8817e4Smiod    ERRNAME	As for pex_run.
533*3d8817e4Smiod    STATUS	Set to exit status on success.
534*3d8817e4Smiod    ERR		As for pex_run.
535*3d8817e4Smiod */
536*3d8817e4Smiod 
537*3d8817e4Smiod extern const char *pex_one (int flags, const char *executable,
538*3d8817e4Smiod 			    char * const *argv, const char *pname,
539*3d8817e4Smiod 			    const char *outname, const char *errname,
540*3d8817e4Smiod 			    int *status, int *err);
541*3d8817e4Smiod 
542*3d8817e4Smiod /* pexecute and pwait are the old pexecute interface, still here for
543*3d8817e4Smiod    backward compatibility.  Don't use these for new code.  Instead,
544*3d8817e4Smiod    use pex_init/pex_run/pex_get_status/pex_free, or pex_one.  */
545*3d8817e4Smiod 
546*3d8817e4Smiod /* Definitions used by the pexecute routine.  */
547*3d8817e4Smiod 
548*3d8817e4Smiod #define PEXECUTE_FIRST   1
549*3d8817e4Smiod #define PEXECUTE_LAST    2
550*3d8817e4Smiod #define PEXECUTE_ONE     (PEXECUTE_FIRST + PEXECUTE_LAST)
551*3d8817e4Smiod #define PEXECUTE_SEARCH  4
552*3d8817e4Smiod #define PEXECUTE_VERBOSE 8
553*3d8817e4Smiod 
554*3d8817e4Smiod /* Execute a program.  */
555*3d8817e4Smiod 
556*3d8817e4Smiod extern int pexecute (const char *, char * const *, const char *,
557*3d8817e4Smiod                      const char *, char **, char **, int);
558*3d8817e4Smiod 
559*3d8817e4Smiod /* Wait for pexecute to finish.  */
560*3d8817e4Smiod 
561*3d8817e4Smiod extern int pwait (int, int *, int);
562*3d8817e4Smiod 
563*3d8817e4Smiod #if !HAVE_DECL_ASPRINTF
564*3d8817e4Smiod /* Like sprintf but provides a pointer to malloc'd storage, which must
565*3d8817e4Smiod    be freed by the caller.  */
566*3d8817e4Smiod 
567*3d8817e4Smiod extern int asprintf (char **, const char *, ...) ATTRIBUTE_PRINTF_2;
568*3d8817e4Smiod #endif
569*3d8817e4Smiod 
570*3d8817e4Smiod #if !HAVE_DECL_VASPRINTF
571*3d8817e4Smiod /* Like vsprintf but provides a pointer to malloc'd storage, which
572*3d8817e4Smiod    must be freed by the caller.  */
573*3d8817e4Smiod 
574*3d8817e4Smiod extern int vasprintf (char **, const char *, va_list) ATTRIBUTE_PRINTF(2,0);
575*3d8817e4Smiod #endif
576*3d8817e4Smiod 
577*3d8817e4Smiod #if defined(HAVE_DECL_SNPRINTF) && !HAVE_DECL_SNPRINTF
578*3d8817e4Smiod /* Like sprintf but prints at most N characters.  */
579*3d8817e4Smiod extern int snprintf (char *, size_t, const char *, ...) ATTRIBUTE_PRINTF_3;
580*3d8817e4Smiod #endif
581*3d8817e4Smiod 
582*3d8817e4Smiod #if defined(HAVE_DECL_VSNPRINTF) && !HAVE_DECL_VSNPRINTF
583*3d8817e4Smiod /* Like vsprintf but prints at most N characters.  */
584*3d8817e4Smiod extern int vsnprintf (char *, size_t, const char *, va_list) ATTRIBUTE_PRINTF(3,0);
585*3d8817e4Smiod #endif
586*3d8817e4Smiod 
587*3d8817e4Smiod #if defined(HAVE_DECL_STRVERSCMP) && !HAVE_DECL_STRVERSCMP
588*3d8817e4Smiod /* Compare version strings.  */
589*3d8817e4Smiod extern int strverscmp (const char *, const char *);
590*3d8817e4Smiod #endif
591*3d8817e4Smiod 
592*3d8817e4Smiod #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
593*3d8817e4Smiod 
594*3d8817e4Smiod /* Drastically simplified alloca configurator.  If we're using GCC,
595*3d8817e4Smiod    we use __builtin_alloca; otherwise we use the C alloca.  The C
596*3d8817e4Smiod    alloca is always available.  You can override GCC by defining
597*3d8817e4Smiod    USE_C_ALLOCA yourself.  The canonical autoconf macro C_ALLOCA is
598*3d8817e4Smiod    also set/unset as it is often used to indicate whether code needs
599*3d8817e4Smiod    to call alloca(0).  */
600*3d8817e4Smiod extern void *C_alloca (size_t) ATTRIBUTE_MALLOC;
601*3d8817e4Smiod #undef alloca
602*3d8817e4Smiod #if GCC_VERSION >= 2000 && !defined USE_C_ALLOCA
603*3d8817e4Smiod # define alloca(x) __builtin_alloca(x)
604*3d8817e4Smiod # undef C_ALLOCA
605*3d8817e4Smiod # define ASTRDUP(X) \
606*3d8817e4Smiod   (__extension__ ({ const char *const libiberty_optr = (X); \
607*3d8817e4Smiod    const unsigned long libiberty_len = strlen (libiberty_optr) + 1; \
608*3d8817e4Smiod    char *const libiberty_nptr = (char *const) alloca (libiberty_len); \
609*3d8817e4Smiod    (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len); }))
610*3d8817e4Smiod #else
611*3d8817e4Smiod # define alloca(x) C_alloca(x)
612*3d8817e4Smiod # undef USE_C_ALLOCA
613*3d8817e4Smiod # define USE_C_ALLOCA 1
614*3d8817e4Smiod # undef C_ALLOCA
615*3d8817e4Smiod # define C_ALLOCA 1
616*3d8817e4Smiod extern const char *libiberty_optr;
617*3d8817e4Smiod extern char *libiberty_nptr;
618*3d8817e4Smiod extern unsigned long libiberty_len;
619*3d8817e4Smiod # define ASTRDUP(X) \
620*3d8817e4Smiod   (libiberty_optr = (X), \
621*3d8817e4Smiod    libiberty_len = strlen (libiberty_optr) + 1, \
622*3d8817e4Smiod    libiberty_nptr = (char *) alloca (libiberty_len), \
623*3d8817e4Smiod    (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len))
624*3d8817e4Smiod #endif
625*3d8817e4Smiod 
626*3d8817e4Smiod #ifdef __cplusplus
627*3d8817e4Smiod }
628*3d8817e4Smiod #endif
629*3d8817e4Smiod 
630*3d8817e4Smiod 
631*3d8817e4Smiod #endif /* ! defined (LIBIBERTY_H) */
632