This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Trivial patch to make ExtUtils::Install more -w clean
[perl5.git] / handy.h
1 /*    handy.h
2  *
3  *    Copyright (c) 1991-1994, Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 #if !defined(__STDC__)
11 #ifdef NULL
12 #undef NULL
13 #endif
14 #ifndef I286
15 #  define NULL 0
16 #else
17 #  define NULL 0L
18 #endif
19 #endif
20
21 #define Null(type) ((type)NULL)
22 #define Nullch Null(char*)
23 #define Nullfp Null(PerlIO*)
24 #define Nullsv Null(SV*)
25
26 #ifdef TRUE
27 #undef TRUE
28 #endif
29 #ifdef FALSE
30 #undef FALSE
31 #endif
32 #define TRUE (1)
33 #define FALSE (0)
34
35
36 /* XXX Configure ought to have a test for a boolean type, if I can
37    just figure out all the headers such a test needs.
38    Andy Dougherty       August 1996
39 */
40 /* bool is built-in for g++-2.6.3, which might be used for an extension.
41    If the extension includes <_G_config.h> before this file then
42    _G_HAVE_BOOL will be properly set.  If, however, the extension includes
43    this file first, then you will have to manually set -DHAS_BOOL in 
44    your command line to avoid a conflict.
45 */
46 #ifdef _G_HAVE_BOOL
47 # if _G_HAVE_BOOL
48 #  ifndef HAS_BOOL
49 #   define HAS_BOOL 1
50 #  endif
51 # endif
52 #endif
53
54 /* The NeXT dynamic loader headers will not build with the bool macro
55    So declare them now to clear confusion.
56 */
57 #ifdef NeXT
58 # undef FALSE
59 # undef TRUE
60   typedef enum bool { FALSE = 0, TRUE = 1 } bool;
61 # define ENUM_BOOL 1
62 # ifndef HAS_BOOL
63 #  define HAS_BOOL 1
64 # endif /* !HAS_BOOL */
65 #endif /* NeXT */
66
67 #ifndef HAS_BOOL
68 # ifdef UTS
69 #  define bool int
70 # else
71 #  define bool char
72 # endif
73 #endif
74
75 /* XXX A note on the perl source internal type system.  The
76    original intent was that I32 be *exactly* 32 bits.
77
78    Currently, we only guarantee that I32 is *at least* 32 bits.
79    Specifically, if int is 64 bits, then so is I32.  (This is the case
80    for the Cray.)  This has the advantage of meshing nicely with
81    standard library calls (where we pass an I32 and the library is
82    expecting an int), but the disadvantage that an I32 is not 32 bits.
83    Andy Dougherty       August 1996
84
85    In the future, we may perhaps want to think about something like
86     #if INTSIZE == 4
87         typedef I32 int;
88     #else
89     #  if LONGSIZE == 4
90             typedef I32 long;
91     #  else
92     #    if SHORTSIZE == 4
93             typedef I32 short;
94     #    else
95             typedef I32 int;
96     #    endif
97     #  endif
98     #endif
99    For the moment, these are mentioned here so metaconfig will
100    construct Configure to figure out the various sizes.
101 */
102
103 typedef char            I8;
104 typedef unsigned char   U8;
105 /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
106    Please search CHAR_MAX in perl.h for further details. */
107 #define U8_MAX PERL_UCHAR_MAX
108 #define U8_MIN PERL_UCHAR_MIN
109
110 typedef short           I16;
111 typedef unsigned short  U16;
112 #define I16_MAX PERL_SHORT_MAX
113 #define I16_MIN PERL_SHORT_MIN
114 #define U16_MAX PERL_USHORT_MAX
115 #define U16_MIN PERL_USHORT_MIN
116
117 #if BYTEORDER > 0x4321
118   typedef int           I32;
119   typedef unsigned int  U32;
120 # define I32_MAX PERL_INT_MAX
121 # define I32_MIN PERL_INT_MIN
122 # define U32_MAX PERL_UINT_MAX
123 # define U32_MIN PERL_UINT_MIN
124 #else
125   typedef long          I32;
126   typedef unsigned long U32;
127 # define I32_MAX PERL_LONG_MAX
128 # define I32_MIN PERL_LONG_MIN
129 # define U32_MAX PERL_ULONG_MAX
130 # define U32_MIN PERL_ULONG_MIN
131 #endif
132
133 #define Ctl(ch) ((ch) & 037)
134
135 #define strNE(s1,s2) (strcmp(s1,s2))
136 #define strEQ(s1,s2) (!strcmp(s1,s2))
137 #define strLT(s1,s2) (strcmp(s1,s2) < 0)
138 #define strLE(s1,s2) (strcmp(s1,s2) <= 0)
139 #define strGT(s1,s2) (strcmp(s1,s2) > 0)
140 #define strGE(s1,s2) (strcmp(s1,s2) >= 0)
141 #define strnNE(s1,s2,l) (strncmp(s1,s2,l))
142 #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
143
144 #ifdef HAS_MEMCMP
145 #  define memNE(s1,s2,l) (memcmp(s1,s2,l))
146 #  define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
147 #else
148 #  define memNE(s1,s2,l) (bcmp(s1,s2,l))
149 #  define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
150 #endif
151
152 /*
153  * Character classes.
154  *
155  * Unfortunately, the introduction of locales means that we
156  * can't trust isupper(), etc. to tell the truth.  And when
157  * it comes to /\w+/ with tainting enabled, we *must* be able
158  * to trust our character classes.
159  *
160  * Therefore, the default tests in the text of Perl will be
161  * independent of locale.  Any code that wants to depend on
162  * the current locale will use the tests that begin with "lc".
163  */
164
165 #ifdef HAS_SETLOCALE  /* XXX Is there a better test for this? */
166 #  ifndef CTYPE256
167 #    define CTYPE256
168 #  endif
169 #endif
170
171 #define isALNUM(c)      (isALPHA(c) || isDIGIT(c) || (c) == '_')
172 #define isIDFIRST(c)    (isALPHA(c) || (c) == '_')
173 #define isALPHA(c)      (isUPPER(c) || isLOWER(c))
174 #define isSPACE(c) \
175         ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
176 #define isDIGIT(c)      ((c) >= '0' && (c) <= '9')
177 #define isUPPER(c)      ((c) >= 'A' && (c) <= 'Z')
178 #define isLOWER(c)      ((c) >= 'a' && (c) <= 'z')
179 #define isPRINT(c)      (((c) > 32 && (c) < 127) || isSPACE(c))
180 #define toUPPER(c)      (isLOWER(c) ? (c) - ('a' - 'A') : (c))
181 #define toLOWER(c)      (isUPPER(c) ? (c) + ('a' - 'A') : (c))
182
183 #ifdef USE_NEXT_CTYPE
184
185 #  define isALNUM_LC(c) \
186         (NXIsAlpha((unsigned int)(c)) || NXIsDigit((unsigned int)(c)) || \
187          (char)(c) == '_')
188 #  define isIDFIRST_LC(c) \
189         (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
190 #  define isALPHA_LC(c)         NXIsAlpha((unsigned int)(c))
191 #  define isSPACE_LC(c)         NXIsSpace((unsigned int)(c))
192 #  define isDIGIT_LC(c)         NXIsDigit((unsigned int)(c))
193 #  define isUPPER_LC(c)         NXIsUpper((unsigned int)(c))
194 #  define isLOWER_LC(c)         NXIsLower((unsigned int)(c))
195 #  define isPRINT_LC(c)         NXIsPrint((unsigned int)(c))
196 #  define toUPPER_LC(c)         NXToUpper((unsigned int)(c))
197 #  define toLOWER_LC(c)         NXToLower((unsigned int)(c))
198
199 #else /* !USE_NEXT_CTYPE */
200 #  if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
201
202 #    define isALNUM_LC(c) \
203         (isalpha((unsigned char)(c)) || \
204          isdigit((unsigned char)(c)) || (char)(c) == '_')
205 #    define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
206 #    define isALPHA_LC(c)       isalpha((unsigned char)(c))
207 #    define isSPACE_LC(c)       isspace((unsigned char)(c))
208 #    define isDIGIT_LC(c)       isdigit((unsigned char)(c))
209 #    define isUPPER_LC(c)       isupper((unsigned char)(c))
210 #    define isLOWER_LC(c)       islower((unsigned char)(c))
211 #    define isPRINT_LC(c)       isprint((unsigned char)(c))
212 #    define toUPPER_LC(c)       toupper((unsigned char)(c))
213 #    define toLOWER_LC(c)       tolower((unsigned char)(c))
214
215 #  else
216
217 #    define isALNUM_LC(c) \
218         (isascii(c) && (isalpha(c) || isdigit(c) || (c) == '_'))
219 #    define isIDFIRST_LC(c)     (isascii(c) && (isalpha(c) || (c) == '_'))
220 #    define isALPHA_LC(c)       (isascii(c) && isalpha(c))
221 #    define isSPACE_LC(c)       (isascii(c) && isspace(c))
222 #    define isDIGIT_LC(c)       (isascii(c) && isdigit(c))
223 #    define isUPPER_LC(c)       (isascii(c) && isupper(c))
224 #    define isLOWER_LC(c)       (isascii(c) && islower(c))
225 #    define isPRINT_LC(c)       (isascii(c) && isprint(c))
226 #    define toUPPER_LC(c)       toupper(c)
227 #    define toLOWER_LC(c)       tolower(c)
228
229 #  endif
230 #endif /* USE_NEXT_CTYPE */
231
232 /* This conversion works both ways, strangely enough. */
233 #define toCTRL(c)    (toUPPER(c) ^ 64)
234
235 /* Line numbers are unsigned, 16 bits. */
236 typedef U16 line_t;
237 #ifdef lint
238 #define NOLINE ((line_t)0)
239 #else
240 #define NOLINE ((line_t) 65535)
241 #endif
242
243 /* XXX LEAKTEST doesn't really work in perl5.  There are direct calls to
244    safemalloc() in the source, so LEAKTEST won't pick them up.
245    Further, if you try LEAKTEST, you'll also end up calling
246    Safefree, which might call safexfree() on some things that weren't
247    malloced with safexmalloc.  The correct "fix" to this, if anyone
248    is interested, is to ensure that all calls go through the New and
249    Renew macros.
250         --Andy Dougherty                August 1996
251 */
252
253 #ifndef lint
254 #ifndef LEAKTEST
255
256 #define New(x,v,n,t)    (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
257 #define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
258 #define Newz(x,v,n,t)   (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \
259                         memzero((char*)(v), (n)*sizeof(t))
260 #define Renew(v,n,t) \
261           (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
262 #define Renewc(v,n,t,c) \
263           (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
264 #define Safefree(d)     safefree((Malloc_t)(d))
265 #define NEWSV(x,len)    newSV(len)
266
267 #else /* LEAKTEST */
268
269 #define New(x,v,n,t)    (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
270 #define Newc(x,v,n,t,c) (v = (c*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
271 #define Newz(x,v,n,t)   (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))), \
272                          memzero((char*)(v), (n)*sizeof(t))
273 #define Renew(v,n,t) \
274           (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
275 #define Renewc(v,n,t,c) \
276           (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
277 #define Safefree(d)     safexfree((Malloc_t)d)
278 #define NEWSV(x,len)    newSV(x,len)
279
280 #define MAXXCOUNT 1200
281 long xcount[MAXXCOUNT];
282 long lastxcount[MAXXCOUNT];
283
284 #endif /* LEAKTEST */
285
286 #define Move(s,d,n,t)   (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
287 #define Copy(s,d,n,t)   (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
288 #define Zero(d,n,t)     (void)memzero((char*)(d), (n) * sizeof(t))
289
290 #else /* lint */
291
292 #define New(x,v,n,s)    (v = Null(s *))
293 #define Newc(x,v,n,s,c) (v = Null(s *))
294 #define Newz(x,v,n,s)   (v = Null(s *))
295 #define Renew(v,n,s)    (v = Null(s *))
296 #define Move(s,d,n,t)
297 #define Copy(s,d,n,t)
298 #define Zero(d,n,t)
299 #define Safefree(d)     (d) = (d)
300
301 #endif /* lint */
302
303 #ifdef USE_STRUCT_COPY
304 #define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
305 #else
306 #define StructCopy(s,d,t) Copy(s,d,1,t)
307 #endif