OSDN Git Service

include/
[pf3gnuchains/gcc-fork.git] / include / libiberty.h
1 /* Function declarations for libiberty.
2
3    Copyright 2001, 2002 Free Software Foundation, Inc.
4    
5    Note - certain prototypes declared in this header file are for
6    functions whoes implementation copyright does not belong to the
7    FSF.  Those prototypes are present in this file for reference
8    purposes only and their presence in this file should not construed
9    as an indication of ownership by the FSF of the implementation of
10    those functions in any way or form whatsoever.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2, or (at your option)
15    any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place - Suite 330,
25    Boston, MA 02111-1307, USA.
26    
27    Written by Cygnus Support, 1994.
28
29    The libiberty library provides a number of functions which are
30    missing on some operating systems.  We do not declare those here,
31    to avoid conflicts with the system header files on operating
32    systems that do support those functions.  In this file we only
33    declare those functions which are specific to libiberty.  */
34
35 #ifndef LIBIBERTY_H
36 #define LIBIBERTY_H
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 #include "ansidecl.h"
43
44 /* Get a definition for size_t.  */
45 #include <stddef.h>
46 /* Get a definition for va_list.  */
47 #include <stdarg.h>
48
49 /* Build an argument vector from a string.  Allocates memory using
50    malloc.  Use freeargv to free the vector.  */
51
52 extern char **buildargv (const char *) ATTRIBUTE_MALLOC;
53
54 /* Free a vector returned by buildargv.  */
55
56 extern void freeargv (char **);
57
58 /* Duplicate an argument vector. Allocates memory using malloc.  Use
59    freeargv to free the vector.  */
60
61 extern char **dupargv (char **) ATTRIBUTE_MALLOC;
62
63
64 /* Return the last component of a path name.  Note that we can't use a
65    prototype here because the parameter is declared inconsistently
66    across different systems, sometimes as "char *" and sometimes as
67    "const char *" */
68
69 /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1.  If it is
70    undefined, we haven't run the autoconf check so provide the
71    declaration without arguments.  If it is 0, we checked and failed
72    to find the declaration so provide a fully prototyped one.  If it
73    is 1, we found it so don't provide any declaration at all.  */
74 #if !HAVE_DECL_BASENAME
75 #if defined (__GNU_LIBRARY__ ) || defined (__linux__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__) || defined (__CYGWIN__) || defined (__CYGWIN32__) || defined (__MINGW32__) || defined (HAVE_DECL_BASENAME)
76 extern char *basename (const char *);
77 #else
78 extern char *basename ();
79 #endif
80 #endif
81
82 /* A well-defined basename () that is always compiled in.  */
83
84 extern const char *lbasename (const char *);
85
86 /* A well-defined realpath () that is always compiled in.  */
87
88 extern char *lrealpath (const char *);
89
90 /* Concatenate an arbitrary number of strings.  You must pass NULL as
91    the last argument of this function, to terminate the list of
92    strings.  Allocates memory using xmalloc.  */
93
94 extern char *concat (const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
95
96 /* Concatenate an arbitrary number of strings.  You must pass NULL as
97    the last argument of this function, to terminate the list of
98    strings.  Allocates memory using xmalloc.  The first argument is
99    not one of the strings to be concatenated, but if not NULL is a
100    pointer to be freed after the new string is created, similar to the
101    way xrealloc works.  */
102
103 extern char *reconcat (char *, const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
104
105 /* Determine the length of concatenating an arbitrary number of
106    strings.  You must pass NULL as the last argument of this function,
107    to terminate the list of strings.  */
108
109 extern unsigned long concat_length (const char *, ...) ATTRIBUTE_SENTINEL;
110
111 /* Concatenate an arbitrary number of strings into a SUPPLIED area of
112    memory.  You must pass NULL as the last argument of this function,
113    to terminate the list of strings.  The supplied memory is assumed
114    to be large enough.  */
115
116 extern char *concat_copy (char *, const char *, ...) ATTRIBUTE_SENTINEL;
117
118 /* Concatenate an arbitrary number of strings into a GLOBAL area of
119    memory.  You must pass NULL as the last argument of this function,
120    to terminate the list of strings.  The supplied memory is assumed
121    to be large enough.  */
122
123 extern char *concat_copy2 (const char *, ...) ATTRIBUTE_SENTINEL;
124
125 /* This is the global area used by concat_copy2.  */
126
127 extern char *libiberty_concat_ptr;
128
129 /* Concatenate an arbitrary number of strings.  You must pass NULL as
130    the last argument of this function, to terminate the list of
131    strings.  Allocates memory using alloca.  The arguments are
132    evaluated twice!  */
133 #define ACONCAT(ACONCAT_PARAMS) \
134   (libiberty_concat_ptr = alloca (concat_length ACONCAT_PARAMS + 1), \
135    concat_copy2 ACONCAT_PARAMS)
136
137 /* Check whether two file descriptors refer to the same file.  */
138
139 extern int fdmatch (int fd1, int fd2);
140
141 /* Get the working directory.  The result is cached, so don't call
142    chdir() between calls to getpwd().  */
143
144 extern char * getpwd (void);
145
146 /* Get the current time.  */
147 /* Prototypes vary from system to system, so we only provide a
148    prototype on systems where we know that we need it.  */
149 #ifdef __MINGW32__
150 /* Forward declaration to avoid #include <sys/time.h>.   */
151 struct timeval;
152 extern int gettimeofday (struct timeval *, void *); 
153 #endif
154
155 /* Get the amount of time the process has run, in microseconds.  */
156
157 extern long get_run_time (void);
158
159 /* Generate a relocated path to some installation directory.  Allocates
160    return value using malloc.  */
161
162 extern char *make_relative_prefix (const char *, const char *,
163                                    const char *) ATTRIBUTE_MALLOC;
164
165 /* Choose a temporary directory to use for scratch files.  */
166
167 extern char *choose_temp_base (void) ATTRIBUTE_MALLOC;
168
169 /* Return a temporary file name or NULL if unable to create one.  */
170
171 extern char *make_temp_file (const char *) ATTRIBUTE_MALLOC;
172
173 /* Remove a link to a file unless it is special. */
174
175 extern int unlink_if_ordinary (const char *);
176
177 /* Allocate memory filled with spaces.  Allocates using malloc.  */
178
179 extern const char *spaces (int count);
180
181 /* Return the maximum error number for which strerror will return a
182    string.  */
183
184 extern int errno_max (void);
185
186 /* Return the name of an errno value (e.g., strerrno (EINVAL) returns
187    "EINVAL").  */
188
189 extern const char *strerrno (int);
190
191 /* Given the name of an errno value, return the value.  */
192
193 extern int strtoerrno (const char *);
194
195 /* ANSI's strerror(), but more robust.  */
196
197 extern char *xstrerror (int);
198
199 /* Return the maximum signal number for which strsignal will return a
200    string.  */
201
202 extern int signo_max (void);
203
204 /* Return a signal message string for a signal number
205    (e.g., strsignal (SIGHUP) returns something like "Hangup").  */
206 /* This is commented out as it can conflict with one in system headers.
207    We still document its existence though.  */
208
209 /*extern const char *strsignal (int);*/
210
211 /* Return the name of a signal number (e.g., strsigno (SIGHUP) returns
212    "SIGHUP").  */
213
214 extern const char *strsigno (int);
215
216 /* Given the name of a signal, return its number.  */
217
218 extern int strtosigno (const char *);
219
220 /* Register a function to be run by xexit.  Returns 0 on success.  */
221
222 extern int xatexit (void (*fn) (void));
223
224 /* Exit, calling all the functions registered with xatexit.  */
225
226 extern void xexit (int status) ATTRIBUTE_NORETURN;
227
228 /* Set the program name used by xmalloc.  */
229
230 extern void xmalloc_set_program_name (const char *);
231
232 /* Report an allocation failure.  */
233 extern void xmalloc_failed (size_t) ATTRIBUTE_NORETURN;
234
235 /* Allocate memory without fail.  If malloc fails, this will print a
236    message to stderr (using the name set by xmalloc_set_program_name,
237    if any) and then call xexit.  */
238
239 extern PTR xmalloc (size_t) ATTRIBUTE_MALLOC;
240
241 /* Reallocate memory without fail.  This works like xmalloc.  Note,
242    realloc type functions are not suitable for attribute malloc since
243    they may return the same address across multiple calls. */
244
245 extern PTR xrealloc (PTR, size_t);
246
247 /* Allocate memory without fail and set it to zero.  This works like
248    xmalloc.  */
249
250 extern PTR xcalloc (size_t, size_t) ATTRIBUTE_MALLOC;
251
252 /* Copy a string into a memory buffer without fail.  */
253
254 extern char *xstrdup (const char *) ATTRIBUTE_MALLOC;
255
256 /* Copy at most N characters from string into a buffer without fail.  */
257
258 extern char *xstrndup (const char *, size_t) ATTRIBUTE_MALLOC;
259
260 /* Copy an existing memory buffer to a new memory buffer without fail.  */
261
262 extern PTR xmemdup (const PTR, size_t, size_t) ATTRIBUTE_MALLOC;
263
264 /* Physical memory routines.  Return values are in BYTES.  */
265 extern double physmem_total (void);
266 extern double physmem_available (void);
267
268
269 /* These macros provide a K&R/C89/C++-friendly way of allocating structures
270    with nice encapsulation.  The XDELETE*() macros are technically
271    superfluous, but provided here for symmetry.  Using them consistently
272    makes it easier to update client code to use different allocators such
273    as new/delete and new[]/delete[].  */
274
275 /* Scalar allocators.  */
276
277 #define XNEW(T)                 ((T *) xmalloc (sizeof (T)))
278 #define XCNEW(T)                ((T *) xcalloc (1, sizeof (T)))
279 #define XDELETE(P)              free ((void*) (P))
280
281 /* Array allocators.  */
282
283 #define XNEWVEC(T, N)           ((T *) xmalloc (sizeof (T) * (N)))
284 #define XCNEWVEC(T, N)          ((T *) xcalloc ((N), sizeof (T)))
285 #define XRESIZEVEC(T, P, N)     ((T *) xrealloc ((void *) (P), sizeof (T) * (N)))
286 #define XDELETEVEC(P)           free ((void*) (P))
287
288 /* Allocators for variable-sized structures and raw buffers.  */
289
290 #define XNEWVAR(T, S)           ((T *) xmalloc ((S)))
291 #define XCNEWVAR(T, S)          ((T *) xcalloc (1, (S)))
292 #define XRESIZEVAR(T, P, S)     ((T *) xrealloc ((P), (S)))
293
294 /* Type-safe obstack allocator.  */
295
296 #define XOBNEW(O, T)            ((T *) obstack_alloc ((O), sizeof (T)))
297
298
299 /* hex character manipulation routines */
300
301 #define _hex_array_size 256
302 #define _hex_bad        99
303 extern const unsigned char _hex_value[_hex_array_size];
304 extern void hex_init (void);
305 #define hex_p(c)        (hex_value (c) != _hex_bad)
306 /* If you change this, note well: Some code relies on side effects in
307    the argument being performed exactly once.  */
308 #define hex_value(c)    ((unsigned int) _hex_value[(unsigned char) (c)])
309
310 /* Definitions used by the pexecute routine.  */
311
312 #define PEXECUTE_FIRST   1
313 #define PEXECUTE_LAST    2
314 #define PEXECUTE_ONE     (PEXECUTE_FIRST + PEXECUTE_LAST)
315 #define PEXECUTE_SEARCH  4
316 #define PEXECUTE_VERBOSE 8
317
318 /* Execute a program.  */
319
320 extern int pexecute (const char *, char * const *, const char *,
321                      const char *, char **, char **, int);
322
323 /* Wait for pexecute to finish.  */
324
325 extern int pwait (int, int *, int);
326
327 #if !HAVE_DECL_ASPRINTF
328 /* Like sprintf but provides a pointer to malloc'd storage, which must
329    be freed by the caller.  */
330
331 extern int asprintf (char **, const char *, ...) ATTRIBUTE_PRINTF_2;
332 #endif
333
334 #if !HAVE_DECL_VASPRINTF
335 /* Like vsprintf but provides a pointer to malloc'd storage, which
336    must be freed by the caller.  */
337
338 extern int vasprintf (char **, const char *, va_list)
339   ATTRIBUTE_PRINTF(2,0);
340 #endif
341
342 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
343
344 /* Drastically simplified alloca configurator.  If we're using GCC,
345    we use __builtin_alloca; otherwise we use the C alloca.  The C
346    alloca is always available.  You can override GCC by defining
347    USE_C_ALLOCA yourself.  The canonical autoconf macro C_ALLOCA is
348    also set/unset as it is often used to indicate whether code needs
349    to call alloca(0).  */
350 extern PTR C_alloca (size_t) ATTRIBUTE_MALLOC;
351 #undef alloca
352 #if GCC_VERSION >= 2000 && !defined USE_C_ALLOCA
353 # define alloca(x) __builtin_alloca(x)
354 # undef C_ALLOCA
355 # define ASTRDUP(X) \
356   (__extension__ ({ const char *const libiberty_optr = (X); \
357    const unsigned long libiberty_len = strlen (libiberty_optr) + 1; \
358    char *const libiberty_nptr = (char *const) alloca (libiberty_len); \
359    (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len); }))
360 #else
361 # define alloca(x) C_alloca(x)
362 # undef USE_C_ALLOCA
363 # define USE_C_ALLOCA 1
364 # undef C_ALLOCA
365 # define C_ALLOCA 1
366 extern const char *libiberty_optr;
367 extern char *libiberty_nptr;
368 extern unsigned long libiberty_len;
369 # define ASTRDUP(X) \
370   (libiberty_optr = (X), \
371    libiberty_len = strlen (libiberty_optr) + 1, \
372    libiberty_nptr = (char *) alloca (libiberty_len), \
373    (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len))
374 #endif
375
376 #ifdef __cplusplus
377 }
378 #endif
379
380
381 #endif /* ! defined (LIBIBERTY_H) */