xref: /netbsd-src/external/gpl3/gdb/dist/include/libiberty.h (revision e663ba6e3a60083e70de702e9d54bf486a57b6a7)
198b9484cSchristos /* Function declarations for libiberty.
298b9484cSchristos 
3*e663ba6eSchristos    Copyright (C) 1997-2024 Free Software Foundation, Inc.
498b9484cSchristos 
598b9484cSchristos    Note - certain prototypes declared in this header file are for
698b9484cSchristos    functions whoes implementation copyright does not belong to the
798b9484cSchristos    FSF.  Those prototypes are present in this file for reference
898b9484cSchristos    purposes only and their presence in this file should not construed
998b9484cSchristos    as an indication of ownership by the FSF of the implementation of
1098b9484cSchristos    those functions in any way or form whatsoever.
1198b9484cSchristos 
1298b9484cSchristos    This program is free software; you can redistribute it and/or modify
1398b9484cSchristos    it under the terms of the GNU General Public License as published by
1498b9484cSchristos    the Free Software Foundation; either version 2, or (at your option)
1598b9484cSchristos    any later version.
1698b9484cSchristos 
1798b9484cSchristos    This program is distributed in the hope that it will be useful,
1898b9484cSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1998b9484cSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2098b9484cSchristos    GNU General Public License for more details.
2198b9484cSchristos 
2298b9484cSchristos    You should have received a copy of the GNU General Public License
2398b9484cSchristos    along with this program; if not, write to the Free Software
2498b9484cSchristos    Foundation, Inc., 51 Franklin Street - Fifth Floor,
2598b9484cSchristos    Boston, MA 02110-1301, USA.
2698b9484cSchristos 
2798b9484cSchristos    Written by Cygnus Support, 1994.
2898b9484cSchristos 
2998b9484cSchristos    The libiberty library provides a number of functions which are
3098b9484cSchristos    missing on some operating systems.  We do not declare those here,
3198b9484cSchristos    to avoid conflicts with the system header files on operating
3298b9484cSchristos    systems that do support those functions.  In this file we only
3398b9484cSchristos    declare those functions which are specific to libiberty.  */
3498b9484cSchristos 
3598b9484cSchristos #ifndef LIBIBERTY_H
3698b9484cSchristos #define LIBIBERTY_H
3798b9484cSchristos 
3898b9484cSchristos #ifdef __cplusplus
3998b9484cSchristos extern "C" {
4098b9484cSchristos #endif
4198b9484cSchristos 
4298b9484cSchristos #include "ansidecl.h"
4398b9484cSchristos 
4498b9484cSchristos /* Get a definition for size_t.  */
4598b9484cSchristos #include <stddef.h>
4698b9484cSchristos /* Get a definition for va_list.  */
4798b9484cSchristos #include <stdarg.h>
4898b9484cSchristos 
4998b9484cSchristos #include <stdio.h>
5098b9484cSchristos 
5198b9484cSchristos /* If the OS supports it, ensure that the supplied stream is setup to
5298b9484cSchristos    avoid any multi-threaded locking.  Otherwise leave the FILE pointer
5398b9484cSchristos    unchanged.  If the stream is NULL do nothing.  */
5498b9484cSchristos 
5598b9484cSchristos extern void unlock_stream (FILE *);
5698b9484cSchristos 
5798b9484cSchristos /* If the OS supports it, ensure that the standard I/O streams, stdin,
5898b9484cSchristos    stdout and stderr are setup to avoid any multi-threaded locking.
5998b9484cSchristos    Otherwise do nothing.  */
6098b9484cSchristos 
6198b9484cSchristos extern void unlock_std_streams (void);
6298b9484cSchristos 
6398b9484cSchristos /* Open and return a FILE pointer.  If the OS supports it, ensure that
6498b9484cSchristos    the stream is setup to avoid any multi-threaded locking.  Otherwise
6598b9484cSchristos    return the FILE pointer unchanged.  */
6698b9484cSchristos 
6798b9484cSchristos extern FILE *fopen_unlocked (const char *, const char *);
6898b9484cSchristos extern FILE *fdopen_unlocked (int, const char *);
6998b9484cSchristos extern FILE *freopen_unlocked (const char *, const char *, FILE *);
7098b9484cSchristos 
7198b9484cSchristos /* Build an argument vector from a string.  Allocates memory using
7298b9484cSchristos    malloc.  Use freeargv to free the vector.  */
7398b9484cSchristos 
7498b9484cSchristos extern char **buildargv (const char *) ATTRIBUTE_MALLOC;
7598b9484cSchristos 
7698b9484cSchristos /* Free a vector returned by buildargv.  */
7798b9484cSchristos 
7898b9484cSchristos extern void freeargv (char **);
7998b9484cSchristos 
8098b9484cSchristos /* Duplicate an argument vector. Allocates memory using malloc.  Use
8198b9484cSchristos    freeargv to free the vector.  */
8298b9484cSchristos 
83ba340e45Schristos extern char **dupargv (char * const *) ATTRIBUTE_MALLOC;
8498b9484cSchristos 
8598b9484cSchristos /* Expand "@file" arguments in argv.  */
8698b9484cSchristos 
87968cf8f2Schristos extern void expandargv (int *, char ***);
8898b9484cSchristos 
8998b9484cSchristos /* Write argv to an @-file, inserting necessary quoting.  */
9098b9484cSchristos 
91ba340e45Schristos extern int writeargv (char * const *, FILE *);
9298b9484cSchristos 
93a2e2270fSchristos /* Return the number of elements in argv.  */
94a2e2270fSchristos 
95ba340e45Schristos extern int countargv (char * const *);
96a2e2270fSchristos 
9798b9484cSchristos /* Return the last component of a path name.  Note that we can't use a
9898b9484cSchristos    prototype here because the parameter is declared inconsistently
9998b9484cSchristos    across different systems, sometimes as "char *" and sometimes as
10098b9484cSchristos    "const char *" */
10198b9484cSchristos 
10298b9484cSchristos /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1.  If it is
10398b9484cSchristos    undefined, we haven't run the autoconf check so provide the
10498b9484cSchristos    declaration without arguments.  If it is 0, we checked and failed
10598b9484cSchristos    to find the declaration so provide a fully prototyped one.  If it
10698b9484cSchristos    is 1, we found it so don't provide any declaration at all.  */
10798b9484cSchristos #if !HAVE_DECL_BASENAME
108968cf8f2Schristos #if defined (__GNU_LIBRARY__ ) || defined (__linux__) \
109968cf8f2Schristos  || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__NetBSD__) \
110968cf8f2Schristos  || defined (__CYGWIN__) || defined (__CYGWIN32__) || defined (__MINGW32__) \
111968cf8f2Schristos  || defined (__DragonFly__) || defined (HAVE_DECL_BASENAME)
11203467a24Schristos extern char *basename (const char *) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_NONNULL(1);
11398b9484cSchristos #else
11498b9484cSchristos /* Do not allow basename to be used if there is no prototype seen.  We
11598b9484cSchristos    either need to use the above prototype or have one from
11698b9484cSchristos    autoconf which would result in HAVE_DECL_BASENAME being set.  */
11798b9484cSchristos #define basename basename_cannot_be_used_without_a_prototype
11898b9484cSchristos #endif
11998b9484cSchristos #endif
12098b9484cSchristos 
12198b9484cSchristos /* A well-defined basename () that is always compiled in.  */
12298b9484cSchristos 
12303467a24Schristos extern const char *lbasename (const char *) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_NONNULL(1);
12498b9484cSchristos 
12598b9484cSchristos /* Same, but assumes DOS semantics (drive name, backslash is also a
12698b9484cSchristos    dir separator) regardless of host.  */
12798b9484cSchristos 
12803467a24Schristos extern const char *dos_lbasename (const char *) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_NONNULL(1);
12998b9484cSchristos 
13098b9484cSchristos /* Same, but assumes Unix semantics (absolute paths always start with
13198b9484cSchristos    a slash, only forward slash is accepted as dir separator)
13298b9484cSchristos    regardless of host.  */
13398b9484cSchristos 
13403467a24Schristos extern const char *unix_lbasename (const char *) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_NONNULL(1);
13598b9484cSchristos 
13698b9484cSchristos /* A well-defined realpath () that is always compiled in.  */
13798b9484cSchristos 
13898b9484cSchristos extern char *lrealpath (const char *);
13998b9484cSchristos 
1408dffb485Schristos /* Return true when FD file descriptor exists.  */
1418dffb485Schristos 
1428dffb485Schristos extern int is_valid_fd (int fd);
1438dffb485Schristos 
14498b9484cSchristos /* Concatenate an arbitrary number of strings.  You must pass NULL as
14598b9484cSchristos    the last argument of this function, to terminate the list of
14698b9484cSchristos    strings.  Allocates memory using xmalloc.  */
14798b9484cSchristos 
14803467a24Schristos extern char *concat (const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_SENTINEL;
14998b9484cSchristos 
15098b9484cSchristos /* Concatenate an arbitrary number of strings.  You must pass NULL as
15198b9484cSchristos    the last argument of this function, to terminate the list of
15298b9484cSchristos    strings.  Allocates memory using xmalloc.  The first argument is
15398b9484cSchristos    not one of the strings to be concatenated, but if not NULL is a
15498b9484cSchristos    pointer to be freed after the new string is created, similar to the
15598b9484cSchristos    way xrealloc works.  */
15698b9484cSchristos 
15703467a24Schristos extern char *reconcat (char *, const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_SENTINEL;
15898b9484cSchristos 
15998b9484cSchristos /* Determine the length of concatenating an arbitrary number of
16098b9484cSchristos    strings.  You must pass NULL as the last argument of this function,
16198b9484cSchristos    to terminate the list of strings.  */
16298b9484cSchristos 
16398b9484cSchristos extern unsigned long concat_length (const char *, ...) ATTRIBUTE_SENTINEL;
16498b9484cSchristos 
16598b9484cSchristos /* Concatenate an arbitrary number of strings into a SUPPLIED area of
16698b9484cSchristos    memory.  You must pass NULL as the last argument of this function,
16798b9484cSchristos    to terminate the list of strings.  The supplied memory is assumed
16898b9484cSchristos    to be large enough.  */
16998b9484cSchristos 
17003467a24Schristos extern char *concat_copy (char *, const char *, ...) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_NONNULL(1) ATTRIBUTE_SENTINEL;
17198b9484cSchristos 
17298b9484cSchristos /* Concatenate an arbitrary number of strings into a GLOBAL area of
17398b9484cSchristos    memory.  You must pass NULL as the last argument of this function,
17498b9484cSchristos    to terminate the list of strings.  The supplied memory is assumed
17598b9484cSchristos    to be large enough.  */
17698b9484cSchristos 
17703467a24Schristos extern char *concat_copy2 (const char *, ...) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_SENTINEL;
17898b9484cSchristos 
17998b9484cSchristos /* This is the global area used by concat_copy2.  */
18098b9484cSchristos 
18198b9484cSchristos extern char *libiberty_concat_ptr;
18298b9484cSchristos 
18398b9484cSchristos /* Concatenate an arbitrary number of strings.  You must pass NULL as
18498b9484cSchristos    the last argument of this function, to terminate the list of
18598b9484cSchristos    strings.  Allocates memory using alloca.  The arguments are
18698b9484cSchristos    evaluated twice!  */
18798b9484cSchristos #define ACONCAT(ACONCAT_PARAMS) \
18898b9484cSchristos   (libiberty_concat_ptr = (char *) alloca (concat_length ACONCAT_PARAMS + 1), \
18998b9484cSchristos    concat_copy2 ACONCAT_PARAMS)
19098b9484cSchristos 
19198b9484cSchristos /* Check whether two file descriptors refer to the same file.  */
19298b9484cSchristos 
19398b9484cSchristos extern int fdmatch (int fd1, int fd2);
19498b9484cSchristos 
19598b9484cSchristos /* Return the position of the first bit set in the argument.  */
19698b9484cSchristos /* Prototypes vary from system to system, so we only provide a
19798b9484cSchristos    prototype on systems where we know that we need it.  */
19898b9484cSchristos #if defined (HAVE_DECL_FFS) && !HAVE_DECL_FFS
19998b9484cSchristos extern int ffs(int);
20098b9484cSchristos #endif
20198b9484cSchristos 
20298b9484cSchristos /* Get the working directory.  The result is cached, so don't call
20398b9484cSchristos    chdir() between calls to getpwd().  */
20498b9484cSchristos 
20598b9484cSchristos extern char * getpwd (void);
20698b9484cSchristos 
20798b9484cSchristos /* Get the current time.  */
20898b9484cSchristos /* Prototypes vary from system to system, so we only provide a
20998b9484cSchristos    prototype on systems where we know that we need it.  */
21098b9484cSchristos #ifdef __MINGW32__
21198b9484cSchristos /* Forward declaration to avoid #include <sys/time.h>.   */
21298b9484cSchristos struct timeval;
21398b9484cSchristos extern int gettimeofday (struct timeval *, void *);
21498b9484cSchristos #endif
21598b9484cSchristos 
21698b9484cSchristos /* Get the amount of time the process has run, in microseconds.  */
21798b9484cSchristos 
21898b9484cSchristos extern long get_run_time (void);
21998b9484cSchristos 
22098b9484cSchristos /* Generate a relocated path to some installation directory.  Allocates
22198b9484cSchristos    return value using malloc.  */
22298b9484cSchristos 
22398b9484cSchristos extern char *make_relative_prefix (const char *, const char *,
22498b9484cSchristos                                    const char *) ATTRIBUTE_MALLOC;
22598b9484cSchristos 
22698b9484cSchristos /* Generate a relocated path to some installation directory without
22798b9484cSchristos    attempting to follow any soft links.  Allocates
22898b9484cSchristos    return value using malloc.  */
22998b9484cSchristos 
23098b9484cSchristos extern char *make_relative_prefix_ignore_links (const char *, const char *,
23198b9484cSchristos 						const char *) ATTRIBUTE_MALLOC;
23298b9484cSchristos 
233968cf8f2Schristos /* Returns a pointer to a directory path suitable for creating temporary
234968cf8f2Schristos    files in.  */
235968cf8f2Schristos 
236968cf8f2Schristos extern const char *choose_tmpdir (void) ATTRIBUTE_RETURNS_NONNULL;
237968cf8f2Schristos 
23898b9484cSchristos /* Choose a temporary directory to use for scratch files.  */
23998b9484cSchristos 
24003467a24Schristos extern char *choose_temp_base (void) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL;
24198b9484cSchristos 
24298b9484cSchristos /* Return a temporary file name or NULL if unable to create one.  */
24398b9484cSchristos 
24498b9484cSchristos extern char *make_temp_file (const char *) ATTRIBUTE_MALLOC;
24598b9484cSchristos 
2464559860eSchristos /* Return a temporary file name with given PREFIX and SUFFIX
2474559860eSchristos    or NULL if unable to create one.  */
2484559860eSchristos 
2494559860eSchristos extern char *make_temp_file_with_prefix (const char *, const char *) ATTRIBUTE_MALLOC;
2504559860eSchristos 
25198b9484cSchristos /* Remove a link to a file unless it is special. */
25298b9484cSchristos 
25398b9484cSchristos extern int unlink_if_ordinary (const char *);
25498b9484cSchristos 
25598b9484cSchristos /* Allocate memory filled with spaces.  Allocates using malloc.  */
25698b9484cSchristos 
25798b9484cSchristos extern const char *spaces (int count);
25898b9484cSchristos 
25998b9484cSchristos /* Return the maximum error number for which strerror will return a
26098b9484cSchristos    string.  */
26198b9484cSchristos 
26298b9484cSchristos extern int errno_max (void);
26398b9484cSchristos 
26498b9484cSchristos /* Return the name of an errno value (e.g., strerrno (EINVAL) returns
26598b9484cSchristos    "EINVAL").  */
26698b9484cSchristos 
26798b9484cSchristos extern const char *strerrno (int);
26898b9484cSchristos 
26998b9484cSchristos /* Given the name of an errno value, return the value.  */
27098b9484cSchristos 
27198b9484cSchristos extern int strtoerrno (const char *);
27298b9484cSchristos 
27398b9484cSchristos /* ANSI's strerror(), but more robust.  */
27498b9484cSchristos 
27503467a24Schristos extern char *xstrerror (int) ATTRIBUTE_RETURNS_NONNULL;
27698b9484cSchristos 
27798b9484cSchristos /* Return the maximum signal number for which strsignal will return a
27898b9484cSchristos    string.  */
27998b9484cSchristos 
28098b9484cSchristos extern int signo_max (void);
28198b9484cSchristos 
28298b9484cSchristos /* Return a signal message string for a signal number
28398b9484cSchristos    (e.g., strsignal (SIGHUP) returns something like "Hangup").  */
28498b9484cSchristos /* This is commented out as it can conflict with one in system headers.
28598b9484cSchristos    We still document its existence though.  */
28698b9484cSchristos 
28798b9484cSchristos /*extern const char *strsignal (int);*/
28898b9484cSchristos 
28998b9484cSchristos /* Return the name of a signal number (e.g., strsigno (SIGHUP) returns
29098b9484cSchristos    "SIGHUP").  */
29198b9484cSchristos 
29298b9484cSchristos extern const char *strsigno (int);
29398b9484cSchristos 
29498b9484cSchristos /* Given the name of a signal, return its number.  */
29598b9484cSchristos 
29698b9484cSchristos extern int strtosigno (const char *);
29798b9484cSchristos 
29898b9484cSchristos /* Register a function to be run by xexit.  Returns 0 on success.  */
29998b9484cSchristos 
30098b9484cSchristos extern int xatexit (void (*fn) (void));
30198b9484cSchristos 
30298b9484cSchristos /* Exit, calling all the functions registered with xatexit.  */
30398b9484cSchristos 
30498b9484cSchristos extern void xexit (int status) ATTRIBUTE_NORETURN;
30598b9484cSchristos 
30698b9484cSchristos /* Set the program name used by xmalloc.  */
30798b9484cSchristos 
30898b9484cSchristos extern void xmalloc_set_program_name (const char *);
30998b9484cSchristos 
31098b9484cSchristos /* Report an allocation failure.  */
31198b9484cSchristos extern void xmalloc_failed (size_t) ATTRIBUTE_NORETURN;
31298b9484cSchristos 
31398b9484cSchristos /* Allocate memory without fail.  If malloc fails, this will print a
31498b9484cSchristos    message to stderr (using the name set by xmalloc_set_program_name,
31598b9484cSchristos    if any) and then call xexit.  */
31698b9484cSchristos 
3178dffb485Schristos extern void *xmalloc (size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_1 ATTRIBUTE_WARN_UNUSED_RESULT;
31898b9484cSchristos 
31998b9484cSchristos /* Reallocate memory without fail.  This works like xmalloc.  Note,
32098b9484cSchristos    realloc type functions are not suitable for attribute malloc since
32198b9484cSchristos    they may return the same address across multiple calls. */
32298b9484cSchristos 
3238dffb485Schristos extern void *xrealloc (void *, size_t) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_2 ATTRIBUTE_WARN_UNUSED_RESULT;
32498b9484cSchristos 
32598b9484cSchristos /* Allocate memory without fail and set it to zero.  This works like
32698b9484cSchristos    xmalloc.  */
32798b9484cSchristos 
3288dffb485Schristos extern void *xcalloc (size_t, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_RESULT_SIZE_1_2 ATTRIBUTE_WARN_UNUSED_RESULT;
32998b9484cSchristos 
33098b9484cSchristos /* Copy a string into a memory buffer without fail.  */
33198b9484cSchristos 
3328dffb485Schristos extern char *xstrdup (const char *) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_WARN_UNUSED_RESULT;
33398b9484cSchristos 
33498b9484cSchristos /* Copy at most N characters from string into a buffer without fail.  */
33598b9484cSchristos 
3368dffb485Schristos extern char *xstrndup (const char *, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_WARN_UNUSED_RESULT;
33798b9484cSchristos 
33898b9484cSchristos /* Copy an existing memory buffer to a new memory buffer without fail.  */
33998b9484cSchristos 
3408dffb485Schristos extern void *xmemdup (const void *, size_t, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_WARN_UNUSED_RESULT;
34198b9484cSchristos 
34298b9484cSchristos /* Physical memory routines.  Return values are in BYTES.  */
34398b9484cSchristos extern double physmem_total (void);
34498b9484cSchristos extern double physmem_available (void);
34598b9484cSchristos 
34698b9484cSchristos /* Compute the 32-bit CRC of a block of memory.  */
34798b9484cSchristos extern unsigned int xcrc32 (const unsigned char *, int, unsigned int);
34898b9484cSchristos 
34998b9484cSchristos /* These macros provide a K&R/C89/C++-friendly way of allocating structures
35098b9484cSchristos    with nice encapsulation.  The XDELETE*() macros are technically
35198b9484cSchristos    superfluous, but provided here for symmetry.  Using them consistently
35298b9484cSchristos    makes it easier to update client code to use different allocators such
35398b9484cSchristos    as new/delete and new[]/delete[].  */
35498b9484cSchristos 
35598b9484cSchristos /* Scalar allocators.  */
35698b9484cSchristos 
35798b9484cSchristos #define XALLOCA(T)		((T *) alloca (sizeof (T)))
35898b9484cSchristos #define XNEW(T)			((T *) xmalloc (sizeof (T)))
35998b9484cSchristos #define XCNEW(T)		((T *) xcalloc (1, sizeof (T)))
36098b9484cSchristos #define XDUP(T, P)		((T *) xmemdup ((P), sizeof (T), sizeof (T)))
36198b9484cSchristos #define XDELETE(P)		free ((void*) (P))
36298b9484cSchristos 
36398b9484cSchristos /* Array allocators.  */
36498b9484cSchristos 
36598b9484cSchristos #define XALLOCAVEC(T, N)	((T *) alloca (sizeof (T) * (N)))
36698b9484cSchristos #define XNEWVEC(T, N)		((T *) xmalloc (sizeof (T) * (N)))
36798b9484cSchristos #define XCNEWVEC(T, N)		((T *) xcalloc ((N), sizeof (T)))
36898b9484cSchristos #define XDUPVEC(T, P, N)	((T *) xmemdup ((P), sizeof (T) * (N), sizeof (T) * (N)))
36998b9484cSchristos #define XRESIZEVEC(T, P, N)	((T *) xrealloc ((void *) (P), sizeof (T) * (N)))
37098b9484cSchristos #define XDELETEVEC(P)		free ((void*) (P))
37198b9484cSchristos 
37298b9484cSchristos /* Allocators for variable-sized structures and raw buffers.  */
37398b9484cSchristos 
37498b9484cSchristos #define XALLOCAVAR(T, S)	((T *) alloca ((S)))
37598b9484cSchristos #define XNEWVAR(T, S)		((T *) xmalloc ((S)))
37698b9484cSchristos #define XCNEWVAR(T, S)		((T *) xcalloc (1, (S)))
37798b9484cSchristos #define XDUPVAR(T, P, S1, S2)	((T *) xmemdup ((P), (S1), (S2)))
37898b9484cSchristos #define XRESIZEVAR(T, P, S)	((T *) xrealloc ((P), (S)))
37998b9484cSchristos 
38098b9484cSchristos /* Type-safe obstack allocator.  */
38198b9484cSchristos 
38298b9484cSchristos #define XOBNEW(O, T)		((T *) obstack_alloc ((O), sizeof (T)))
38398b9484cSchristos #define XOBNEWVEC(O, T, N)	((T *) obstack_alloc ((O), sizeof (T) * (N)))
38498b9484cSchristos #define XOBNEWVAR(O, T, S)	((T *) obstack_alloc ((O), (S)))
38598b9484cSchristos #define XOBFINISH(O, T)         ((T) obstack_finish ((O)))
38698b9484cSchristos 
38798b9484cSchristos /* hex character manipulation routines */
38898b9484cSchristos 
38998b9484cSchristos #define _hex_array_size 256
39098b9484cSchristos #define _hex_bad	99
39198b9484cSchristos extern const unsigned char _hex_value[_hex_array_size];
39298b9484cSchristos extern void hex_init (void);
39398b9484cSchristos #define hex_p(c)	(hex_value (c) != _hex_bad)
39498b9484cSchristos /* If you change this, note well: Some code relies on side effects in
39598b9484cSchristos    the argument being performed exactly once.  */
39698b9484cSchristos #define hex_value(c)	((unsigned int) _hex_value[(unsigned char) (c)])
39798b9484cSchristos 
39898b9484cSchristos /* Flags for pex_init.  These are bits to be or'ed together.  */
39998b9484cSchristos 
40098b9484cSchristos /* Record subprocess times, if possible.  */
40198b9484cSchristos #define PEX_RECORD_TIMES	0x1
40298b9484cSchristos 
40398b9484cSchristos /* Use pipes for communication between processes, if possible.  */
40498b9484cSchristos #define PEX_USE_PIPES		0x2
40598b9484cSchristos 
40698b9484cSchristos /* Save files used for communication between processes.  */
40798b9484cSchristos #define PEX_SAVE_TEMPS		0x4
40898b9484cSchristos 
409ba340e45Schristos /* Max number of alloca bytes per call before we must switch to malloc.
410ba340e45Schristos 
411ba340e45Schristos    ?? Swiped from gnulib's regex_internal.h header.  Is this actually
412ba340e45Schristos    the case?  This number seems arbitrary, though sane.
413ba340e45Schristos 
414ba340e45Schristos    The OS usually guarantees only one guard page at the bottom of the stack,
415ba340e45Schristos    and a page size can be as small as 4096 bytes.  So we cannot safely
416ba340e45Schristos    allocate anything larger than 4096 bytes.  Also care for the possibility
417ba340e45Schristos    of a few compiler-allocated temporary stack slots.  */
418ba340e45Schristos #define MAX_ALLOCA_SIZE	4032
419ba340e45Schristos 
42098b9484cSchristos /* Prepare to execute one or more programs, with standard output of
42198b9484cSchristos    each program fed to standard input of the next.
42298b9484cSchristos    FLAGS	As above.
42398b9484cSchristos    PNAME	The name of the program to report in error messages.
42498b9484cSchristos    TEMPBASE	A base name to use for temporary files; may be NULL to
42598b9484cSchristos    		use a random name.
42698b9484cSchristos    Returns NULL on error.  */
42798b9484cSchristos 
42898b9484cSchristos extern struct pex_obj *pex_init (int flags, const char *pname,
42903467a24Schristos 				 const char *tempbase) ATTRIBUTE_RETURNS_NONNULL;
43098b9484cSchristos 
43198b9484cSchristos /* Flags for pex_run.  These are bits to be or'ed together.  */
43298b9484cSchristos 
43398b9484cSchristos /* Last program in pipeline.  Standard output of program goes to
43498b9484cSchristos    OUTNAME, or, if OUTNAME is NULL, to standard output of caller.  Do
43598b9484cSchristos    not set this if you want to call pex_read_output.  After this is
43698b9484cSchristos    set, pex_run may no longer be called with the same struct
43798b9484cSchristos    pex_obj.  */
43898b9484cSchristos #define PEX_LAST		0x1
43998b9484cSchristos 
44098b9484cSchristos /* Search for program in executable search path.  */
44198b9484cSchristos #define PEX_SEARCH		0x2
44298b9484cSchristos 
44398b9484cSchristos /* OUTNAME is a suffix.  */
44498b9484cSchristos #define PEX_SUFFIX		0x4
44598b9484cSchristos 
44698b9484cSchristos /* Send program's standard error to standard output.  */
44798b9484cSchristos #define PEX_STDERR_TO_STDOUT	0x8
44898b9484cSchristos 
44998b9484cSchristos /* Input file should be opened in binary mode.  This flag is ignored
45098b9484cSchristos    on Unix.  */
45198b9484cSchristos #define PEX_BINARY_INPUT	0x10
45298b9484cSchristos 
45398b9484cSchristos /* Output file should be opened in binary mode.  This flag is ignored
45498b9484cSchristos    on Unix.  For proper behaviour PEX_BINARY_INPUT and
45598b9484cSchristos    PEX_BINARY_OUTPUT have to match appropriately--i.e., a call using
45698b9484cSchristos    PEX_BINARY_OUTPUT should be followed by a call using
45798b9484cSchristos    PEX_BINARY_INPUT.  */
45898b9484cSchristos #define PEX_BINARY_OUTPUT	0x20
45998b9484cSchristos 
46098b9484cSchristos /* Capture stderr to a pipe.  The output can be read by
46198b9484cSchristos    calling pex_read_err and reading from the returned
46298b9484cSchristos    FILE object.  This flag may be specified only for
46398b9484cSchristos    the last program in a pipeline.
46498b9484cSchristos 
46598b9484cSchristos    This flag is supported only on Unix and Windows.  */
46698b9484cSchristos #define PEX_STDERR_TO_PIPE	0x40
46798b9484cSchristos 
46898b9484cSchristos /* Capture stderr in binary mode.  This flag is ignored
46998b9484cSchristos    on Unix.  */
47098b9484cSchristos #define PEX_BINARY_ERROR	0x80
47198b9484cSchristos 
472968cf8f2Schristos /* Append stdout to existing file instead of truncating it.  */
473968cf8f2Schristos #define PEX_STDOUT_APPEND	0x100
474968cf8f2Schristos 
475968cf8f2Schristos /* Thes same as PEX_STDOUT_APPEND, but for STDERR.  */
476968cf8f2Schristos #define PEX_STDERR_APPEND	0x200
47798b9484cSchristos 
47898b9484cSchristos /* Execute one program.  Returns NULL on success.  On error returns an
47998b9484cSchristos    error string (typically just the name of a system call); the error
48098b9484cSchristos    string is statically allocated.
48198b9484cSchristos 
48298b9484cSchristos    OBJ		Returned by pex_init.
48398b9484cSchristos 
48498b9484cSchristos    FLAGS	As above.
48598b9484cSchristos 
48698b9484cSchristos    EXECUTABLE	The program to execute.
48798b9484cSchristos 
48898b9484cSchristos    ARGV		NULL terminated array of arguments to pass to the program.
48998b9484cSchristos 
49098b9484cSchristos    OUTNAME	Sets the output file name as follows:
49198b9484cSchristos 
49298b9484cSchristos 		PEX_SUFFIX set (OUTNAME may not be NULL):
49398b9484cSchristos 		  TEMPBASE parameter to pex_init not NULL:
49498b9484cSchristos 		    Output file name is the concatenation of TEMPBASE
49598b9484cSchristos 		    and OUTNAME.
49698b9484cSchristos 		  TEMPBASE is NULL:
49798b9484cSchristos 		    Output file name is a random file name ending in
49898b9484cSchristos 		    OUTNAME.
49998b9484cSchristos 		PEX_SUFFIX not set:
50098b9484cSchristos 		  OUTNAME not NULL:
50198b9484cSchristos 		    Output file name is OUTNAME.
50298b9484cSchristos 		  OUTNAME NULL, TEMPBASE not NULL:
50398b9484cSchristos 		    Output file name is randomly chosen using
50498b9484cSchristos 		    TEMPBASE.
50598b9484cSchristos 		  OUTNAME NULL, TEMPBASE NULL:
50698b9484cSchristos 		    Output file name is randomly chosen.
50798b9484cSchristos 
50898b9484cSchristos 		If PEX_LAST is not set, the output file name is the
50998b9484cSchristos    		name to use for a temporary file holding stdout, if
51098b9484cSchristos    		any (there will not be a file if PEX_USE_PIPES is set
51198b9484cSchristos    		and the system supports pipes).  If a file is used, it
51298b9484cSchristos    		will be removed when no longer needed unless
51398b9484cSchristos    		PEX_SAVE_TEMPS is set.
51498b9484cSchristos 
51598b9484cSchristos 		If PEX_LAST is set, and OUTNAME is not NULL, standard
51698b9484cSchristos    		output is written to the output file name.  The file
51798b9484cSchristos    		will not be removed.  If PEX_LAST and PEX_SUFFIX are
51898b9484cSchristos    		both set, TEMPBASE may not be NULL.
51998b9484cSchristos 
52098b9484cSchristos    ERRNAME	If not NULL, this is the name of a file to which
52198b9484cSchristos 		standard error is written.  If NULL, standard error of
52298b9484cSchristos 		the program is standard error of the caller.
52398b9484cSchristos 
52498b9484cSchristos    ERR		On an error return, *ERR is set to an errno value, or
52598b9484cSchristos    		to 0 if there is no relevant errno.
52698b9484cSchristos */
52798b9484cSchristos 
52898b9484cSchristos extern const char *pex_run (struct pex_obj *obj, int flags,
52998b9484cSchristos 			    const char *executable, char * const *argv,
53098b9484cSchristos 			    const char *outname, const char *errname,
53198b9484cSchristos 			    int *err);
53298b9484cSchristos 
53398b9484cSchristos /* As for pex_run (), but takes an extra parameter to enable the
53498b9484cSchristos    environment for the child process to be specified.
53598b9484cSchristos 
53698b9484cSchristos    ENV		The environment for the child process, specified as
53798b9484cSchristos 		an array of character pointers.  Each element of the
53898b9484cSchristos 		array should point to a string of the form VAR=VALUE,
53998b9484cSchristos                 with the exception of the last element which must be
54098b9484cSchristos                 a null pointer.
54198b9484cSchristos */
54298b9484cSchristos 
54398b9484cSchristos extern const char *pex_run_in_environment (struct pex_obj *obj, int flags,
54498b9484cSchristos 			                   const char *executable,
54598b9484cSchristos                                            char * const *argv,
54698b9484cSchristos                                            char * const *env,
54798b9484cSchristos               	          		   const char *outname,
54898b9484cSchristos 					   const char *errname, int *err);
54998b9484cSchristos 
55098b9484cSchristos /* Return a stream for a temporary file to pass to the first program
55198b9484cSchristos    in the pipeline as input.  The file name is chosen as for pex_run.
55298b9484cSchristos    pex_run closes the file automatically; don't close it yourself.  */
55398b9484cSchristos 
55498b9484cSchristos extern FILE *pex_input_file (struct pex_obj *obj, int flags,
55598b9484cSchristos                              const char *in_name);
55698b9484cSchristos 
55798b9484cSchristos /* Return a stream for a pipe connected to the standard input of the
55898b9484cSchristos    first program in the pipeline.  You must have passed
55998b9484cSchristos    `PEX_USE_PIPES' to `pex_init'.  Close the returned stream
56098b9484cSchristos    yourself.  */
56198b9484cSchristos 
56298b9484cSchristos extern FILE *pex_input_pipe (struct pex_obj *obj, int binary);
56398b9484cSchristos 
56498b9484cSchristos /* Read the standard output of the last program to be executed.
56598b9484cSchristos    pex_run cannot be called after this.  BINARY should be non-zero if
56698b9484cSchristos    the file should be opened in binary mode; this is ignored on Unix.
56798b9484cSchristos    Returns NULL on error.  Don't call fclose on the returned FILE; it
56898b9484cSchristos    will be closed by pex_free.  */
56998b9484cSchristos 
57098b9484cSchristos extern FILE *pex_read_output (struct pex_obj *, int binary);
57198b9484cSchristos 
57298b9484cSchristos /* Read the standard error of the last program to be executed.
57398b9484cSchristos    pex_run cannot be called after this.  BINARY should be non-zero if
57498b9484cSchristos    the file should be opened in binary mode; this is ignored on Unix.
57598b9484cSchristos    Returns NULL on error.  Don't call fclose on the returned FILE; it
57698b9484cSchristos    will be closed by pex_free.  */
57798b9484cSchristos 
57898b9484cSchristos extern FILE *pex_read_err (struct pex_obj *, int binary);
57998b9484cSchristos 
58098b9484cSchristos /* Return exit status of all programs in VECTOR.  COUNT indicates the
58198b9484cSchristos    size of VECTOR.  The status codes in the vector are in the order of
58298b9484cSchristos    the calls to pex_run.  Returns 0 on error, 1 on success.  */
58398b9484cSchristos 
58498b9484cSchristos extern int pex_get_status (struct pex_obj *, int count, int *vector);
58598b9484cSchristos 
58698b9484cSchristos /* Return times of all programs in VECTOR.  COUNT indicates the size
58798b9484cSchristos    of VECTOR.  struct pex_time is really just struct timeval, but that
58898b9484cSchristos    is not portable to all systems.  Returns 0 on error, 1 on
58998b9484cSchristos    success.  */
59098b9484cSchristos 
59198b9484cSchristos struct pex_time
59298b9484cSchristos {
59398b9484cSchristos   unsigned long user_seconds;
59498b9484cSchristos   unsigned long user_microseconds;
59598b9484cSchristos   unsigned long system_seconds;
59698b9484cSchristos   unsigned long system_microseconds;
59798b9484cSchristos };
59898b9484cSchristos 
59998b9484cSchristos extern int pex_get_times (struct pex_obj *, int count,
60098b9484cSchristos 			  struct pex_time *vector);
60198b9484cSchristos 
60298b9484cSchristos /* Clean up a pex_obj.  If you have not called pex_get_times or
60398b9484cSchristos    pex_get_status, this will try to kill the subprocesses.  */
60498b9484cSchristos 
60598b9484cSchristos extern void pex_free (struct pex_obj *);
60698b9484cSchristos 
60798b9484cSchristos /* Just execute one program.  Return value is as for pex_run.
60898b9484cSchristos    FLAGS	Combination of PEX_SEARCH and PEX_STDERR_TO_STDOUT.
60998b9484cSchristos    EXECUTABLE	As for pex_run.
61098b9484cSchristos    ARGV		As for pex_run.
61198b9484cSchristos    PNAME	As for pex_init.
61298b9484cSchristos    OUTNAME	As for pex_run when PEX_LAST is set.
61398b9484cSchristos    ERRNAME	As for pex_run.
61498b9484cSchristos    STATUS	Set to exit status on success.
61598b9484cSchristos    ERR		As for pex_run.
61698b9484cSchristos */
61798b9484cSchristos 
61898b9484cSchristos extern const char *pex_one (int flags, const char *executable,
61998b9484cSchristos 			    char * const *argv, const char *pname,
62098b9484cSchristos 			    const char *outname, const char *errname,
62198b9484cSchristos 			    int *status, int *err);
62298b9484cSchristos 
62398b9484cSchristos /* pexecute and pwait are the old pexecute interface, still here for
62498b9484cSchristos    backward compatibility.  Don't use these for new code.  Instead,
62598b9484cSchristos    use pex_init/pex_run/pex_get_status/pex_free, or pex_one.  */
62698b9484cSchristos 
62798b9484cSchristos /* Definitions used by the pexecute routine.  */
62898b9484cSchristos 
62998b9484cSchristos #define PEXECUTE_FIRST   1
63098b9484cSchristos #define PEXECUTE_LAST    2
63198b9484cSchristos #define PEXECUTE_ONE     (PEXECUTE_FIRST + PEXECUTE_LAST)
63298b9484cSchristos #define PEXECUTE_SEARCH  4
63398b9484cSchristos #define PEXECUTE_VERBOSE 8
63498b9484cSchristos 
63598b9484cSchristos /* Execute a program.  */
63698b9484cSchristos 
63798b9484cSchristos extern int pexecute (const char *, char * const *, const char *,
63898b9484cSchristos                      const char *, char **, char **, int);
63998b9484cSchristos 
64098b9484cSchristos /* Wait for pexecute to finish.  */
64198b9484cSchristos 
64298b9484cSchristos extern int pwait (int, int *, int);
64398b9484cSchristos 
6448dffb485Schristos /* Like bsearch, but takes and passes on an argument like qsort_r.  */
6458dffb485Schristos 
6468dffb485Schristos extern void *bsearch_r (const void *, const void *,
6478dffb485Schristos 			size_t, size_t,
6488dffb485Schristos 			int (*)(const void *, const void *, void *),
6498dffb485Schristos 			void *);
6508dffb485Schristos 
651212397c6Schristos #if defined(HAVE_DECL_ASPRINTF) && !HAVE_DECL_ASPRINTF
65298b9484cSchristos /* Like sprintf but provides a pointer to malloc'd storage, which must
65398b9484cSchristos    be freed by the caller.  */
65498b9484cSchristos 
65598b9484cSchristos extern int asprintf (char **, const char *, ...) ATTRIBUTE_PRINTF_2;
65698b9484cSchristos #endif
65798b9484cSchristos 
658968cf8f2Schristos /* Like asprintf but allocates memory without fail. This works like
659968cf8f2Schristos    xmalloc.  */
660968cf8f2Schristos 
661968cf8f2Schristos extern char *xasprintf (const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_PRINTF_1;
662968cf8f2Schristos 
6638dffb485Schristos #if defined(HAVE_DECL_VASPRINTF) && !HAVE_DECL_VASPRINTF
66498b9484cSchristos /* Like vsprintf but provides a pointer to malloc'd storage, which
66598b9484cSchristos    must be freed by the caller.  */
66698b9484cSchristos 
66798b9484cSchristos extern int vasprintf (char **, const char *, va_list) ATTRIBUTE_PRINTF(2,0);
66898b9484cSchristos #endif
66998b9484cSchristos 
670968cf8f2Schristos /* Like vasprintf but allocates memory without fail. This works like
671968cf8f2Schristos    xmalloc.  */
672968cf8f2Schristos 
673968cf8f2Schristos extern char *xvasprintf (const char *, va_list) ATTRIBUTE_MALLOC ATTRIBUTE_PRINTF(1,0);
674968cf8f2Schristos 
67598b9484cSchristos #if defined(HAVE_DECL_SNPRINTF) && !HAVE_DECL_SNPRINTF
67698b9484cSchristos /* Like sprintf but prints at most N characters.  */
67798b9484cSchristos extern int snprintf (char *, size_t, const char *, ...) ATTRIBUTE_PRINTF_3;
67898b9484cSchristos #endif
67998b9484cSchristos 
68098b9484cSchristos #if defined(HAVE_DECL_VSNPRINTF) && !HAVE_DECL_VSNPRINTF
68198b9484cSchristos /* Like vsprintf but prints at most N characters.  */
68298b9484cSchristos extern int vsnprintf (char *, size_t, const char *, va_list) ATTRIBUTE_PRINTF(3,0);
68398b9484cSchristos #endif
68498b9484cSchristos 
685968cf8f2Schristos #if defined (HAVE_DECL_STRNLEN) && !HAVE_DECL_STRNLEN
686968cf8f2Schristos extern size_t strnlen (const char *, size_t);
687968cf8f2Schristos #endif
688968cf8f2Schristos 
689968cf8f2Schristos #if defined(HAVE_DECL_STRVERSCMP) && !HAVE_DECL_STRVERSCMP
690968cf8f2Schristos /* Compare version strings.  */
691968cf8f2Schristos extern int strverscmp (const char *, const char *);
692968cf8f2Schristos #endif
693968cf8f2Schristos 
694968cf8f2Schristos #if defined(HAVE_DECL_STRTOL) && !HAVE_DECL_STRTOL
695968cf8f2Schristos extern long int strtol (const char *nptr,
696968cf8f2Schristos                         char **endptr, int base);
697968cf8f2Schristos #endif
698968cf8f2Schristos 
699968cf8f2Schristos #if defined(HAVE_DECL_STRTOUL) && !HAVE_DECL_STRTOUL
700968cf8f2Schristos extern unsigned long int strtoul (const char *nptr,
701968cf8f2Schristos                                   char **endptr, int base);
702968cf8f2Schristos #endif
703968cf8f2Schristos 
704968cf8f2Schristos #if defined(HAVE_LONG_LONG) && defined(HAVE_DECL_STRTOLL) && !HAVE_DECL_STRTOLL
705968cf8f2Schristos __extension__
706968cf8f2Schristos extern long long int strtoll (const char *nptr,
707968cf8f2Schristos                               char **endptr, int base);
708968cf8f2Schristos #endif
709968cf8f2Schristos 
710968cf8f2Schristos #if defined(HAVE_LONG_LONG) && defined(HAVE_DECL_STRTOULL) && !HAVE_DECL_STRTOULL
711968cf8f2Schristos __extension__
712968cf8f2Schristos extern unsigned long long int strtoull (const char *nptr,
713968cf8f2Schristos                                         char **endptr, int base);
714968cf8f2Schristos #endif
715968cf8f2Schristos 
71698b9484cSchristos /* Set the title of a process */
71798b9484cSchristos extern void setproctitle (const char *name, ...);
71898b9484cSchristos 
719a2e2270fSchristos /* Increase stack limit if possible.  */
720a2e2270fSchristos extern void stack_limit_increase (unsigned long);
721a2e2270fSchristos 
72298b9484cSchristos #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
72398b9484cSchristos 
72498b9484cSchristos /* Drastically simplified alloca configurator.  If we're using GCC,
72598b9484cSchristos    we use __builtin_alloca; otherwise we use the C alloca.  The C
72698b9484cSchristos    alloca is always available.  You can override GCC by defining
72798b9484cSchristos    USE_C_ALLOCA yourself.  The canonical autoconf macro C_ALLOCA is
72898b9484cSchristos    also set/unset as it is often used to indicate whether code needs
72998b9484cSchristos    to call alloca(0).  */
73098b9484cSchristos extern void *C_alloca (size_t) ATTRIBUTE_MALLOC;
73198b9484cSchristos #undef alloca
73298b9484cSchristos #if GCC_VERSION >= 2000 && !defined USE_C_ALLOCA
73398b9484cSchristos # define alloca(x) __builtin_alloca(x)
73498b9484cSchristos # undef C_ALLOCA
73598b9484cSchristos # define ASTRDUP(X) \
73698b9484cSchristos   (__extension__ ({ const char *const libiberty_optr = (X); \
73798b9484cSchristos    const unsigned long libiberty_len = strlen (libiberty_optr) + 1; \
7384559860eSchristos    char *const libiberty_nptr = (char *) alloca (libiberty_len); \
73998b9484cSchristos    (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len); }))
74098b9484cSchristos #else
74198b9484cSchristos # define alloca(x) C_alloca(x)
74298b9484cSchristos # undef USE_C_ALLOCA
74398b9484cSchristos # define USE_C_ALLOCA 1
74498b9484cSchristos # undef C_ALLOCA
74598b9484cSchristos # define C_ALLOCA 1
74698b9484cSchristos extern const char *libiberty_optr;
74798b9484cSchristos extern char *libiberty_nptr;
74898b9484cSchristos extern unsigned long libiberty_len;
74998b9484cSchristos # define ASTRDUP(X) \
75098b9484cSchristos   (libiberty_optr = (X), \
75198b9484cSchristos    libiberty_len = strlen (libiberty_optr) + 1, \
75298b9484cSchristos    libiberty_nptr = (char *) alloca (libiberty_len), \
75398b9484cSchristos    (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len))
75498b9484cSchristos #endif
75598b9484cSchristos 
75698b9484cSchristos #ifdef __cplusplus
75798b9484cSchristos }
75898b9484cSchristos #endif
75998b9484cSchristos 
76098b9484cSchristos 
76198b9484cSchristos #endif /* ! defined (LIBIBERTY_H) */
762