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