This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Decommission the symlink test.
[perl5.git] / handy.h
1 /*    handy.h
2  *
3  *    Copyright (c) 1991-1997, 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 # if defined(UTS) || defined(VMS)
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    There is no guarantee that there is *any* integral type with
86    exactly 32 bits.  It is perfectly legal for a system to have
87    sizeof(short) == sizeof(int) == sizeof(long) == 8.
88
89    Similarly, there is no guarantee that I16 and U16 have exactly 16
90    bits.
91
92    For dealing with issues that may arise from various 32/64-bit 
93    systems, we will ask Configure to check out 
94         SHORTSIZE == sizeof(short)
95         INTSIZE == sizeof(int)
96         LONGSIZE == sizeof(long)
97         LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
98         PTRSIZE == sizeof(void *)
99         DOUBLESIZE == sizeof(double)
100         LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
101     Most of these are currently unused, but they are mentioned here so
102     metaconfig will include the appropriate tests in Configure and
103     we can then start to consider how best to deal with long long
104     variables.
105    Andy Dougherty       April 1998
106 */
107
108 #if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
109
110 typedef int8_t          I8;
111 typedef uint8_t         U8;
112 /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
113    Please search CHAR_MAX in perl.h for further details. */
114 #define U8_MAX UINT8_MAX
115 #define U8_MIN UINT8_MIN
116
117 typedef int16_t         I16;
118 typedef uint16_t        U16;
119 #define I16_MAX INT16_MAX
120 #define I16_MIN INT16_MIN
121 #define U16_MAX UINT16_MAX
122 #define U16_MIN UINT16_MIN
123
124 typedef int32_t         I32;
125 typedef uint32_t        U32;
126 #define I32_MAX INT32_MAX
127 #define I32_MIN INT32_MIN
128 #define U32_MAX UINT32_MAX
129 #define U32_MIN UINT32_MIN
130
131 #else
132
133 typedef char            I8;
134 typedef unsigned char   U8;
135 /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
136    Please search CHAR_MAX in perl.h for further details. */
137 #define U8_MAX PERL_UCHAR_MAX
138 #define U8_MIN PERL_UCHAR_MIN
139
140 /* Beware.  SHORTSIZE > 2 in Cray C90ties. */
141 typedef short           I16;
142 typedef unsigned short  U16;
143 #define I16_MAX PERL_SHORT_MAX
144 #define I16_MIN PERL_SHORT_MIN
145 #define U16_MAX PERL_USHORT_MAX
146 #define U16_MIN PERL_USHORT_MIN
147
148 #if LONGSIZE > 4
149   typedef int           I32;
150   typedef unsigned int  U32;
151 # define I32_MAX PERL_INT_MAX
152 # define I32_MIN PERL_INT_MIN
153 # define U32_MAX PERL_UINT_MAX
154 # define U32_MIN PERL_UINT_MIN
155 #else
156   typedef long          I32;
157   typedef unsigned long U32;
158 # define I32_MAX PERL_LONG_MAX
159 # define I32_MIN PERL_LONG_MIN
160 # define U32_MAX PERL_ULONG_MAX
161 # define U32_MIN PERL_ULONG_MIN
162 #endif
163
164 #endif
165
166 #define BIT_DIGITS(N)   (((N)*146)/485 + 1)  /* log2(10) =~ 146/485 */
167 #define TYPE_DIGITS(T)  BIT_DIGITS(sizeof(T) * 8)
168 #define TYPE_CHARS(T)   (TYPE_DIGITS(T) + 2) /* sign, NUL */
169
170 #define Ctl(ch) ((ch) & 037)
171
172 #define strNE(s1,s2) (strcmp(s1,s2))
173 #define strEQ(s1,s2) (!strcmp(s1,s2))
174 #define strLT(s1,s2) (strcmp(s1,s2) < 0)
175 #define strLE(s1,s2) (strcmp(s1,s2) <= 0)
176 #define strGT(s1,s2) (strcmp(s1,s2) > 0)
177 #define strGE(s1,s2) (strcmp(s1,s2) >= 0)
178 #define strnNE(s1,s2,l) (strncmp(s1,s2,l))
179 #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
180
181 #ifdef HAS_MEMCMP
182 #  define memNE(s1,s2,l) (memcmp(s1,s2,l))
183 #  define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
184 #else
185 #  define memNE(s1,s2,l) (bcmp(s1,s2,l))
186 #  define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
187 #endif
188
189 /*
190  * Character classes.
191  *
192  * Unfortunately, the introduction of locales means that we
193  * can't trust isupper(), etc. to tell the truth.  And when
194  * it comes to /\w+/ with tainting enabled, we *must* be able
195  * to trust our character classes.
196  *
197  * Therefore, the default tests in the text of Perl will be
198  * independent of locale.  Any code that wants to depend on
199  * the current locale will use the tests that begin with "lc".
200  */
201
202 #ifdef HAS_SETLOCALE  /* XXX Is there a better test for this? */
203 #  ifndef CTYPE256
204 #    define CTYPE256
205 #  endif
206 #endif
207
208 #define isALNUM(c)      (isALPHA(c) || isDIGIT(c) || (c) == '_')
209 #define isIDFIRST(c)    (isALPHA(c) || (c) == '_')
210 #define isALPHA(c)      (isUPPER(c) || isLOWER(c))
211 #define isSPACE(c) \
212         ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
213 #define isDIGIT(c)      ((c) >= '0' && (c) <= '9')
214 #ifdef EBCDIC
215     /* In EBCDIC we do not do locales: therefore() isupper() is fine. */
216 #   define isUPPER(c)   isupper(c)
217 #   define isLOWER(c)   islower(c)
218 #   define isPRINT(c)   isprint(c)
219 #   define toUPPER(c)   toupper(c)
220 #   define toLOWER(c)   tolower(c)
221 #else
222 #   define isUPPER(c)   ((c) >= 'A' && (c) <= 'Z')
223 #   define isLOWER(c)   ((c) >= 'a' && (c) <= 'z')
224 #   define isPRINT(c)   (((c) > 32 && (c) < 127) || isSPACE(c))
225 #   define toUPPER(c)   (isLOWER(c) ? (c) - ('a' - 'A') : (c))
226 #   define toLOWER(c)   (isUPPER(c) ? (c) + ('a' - 'A') : (c))
227 #endif
228
229 #ifdef USE_NEXT_CTYPE
230
231 #  define isALNUM_LC(c) \
232         (NXIsAlpha((unsigned int)(c)) || NXIsDigit((unsigned int)(c)) || \
233          (char)(c) == '_')
234 #  define isIDFIRST_LC(c) \
235         (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
236 #  define isALPHA_LC(c)         NXIsAlpha((unsigned int)(c))
237 #  define isSPACE_LC(c)         NXIsSpace((unsigned int)(c))
238 #  define isDIGIT_LC(c)         NXIsDigit((unsigned int)(c))
239 #  define isUPPER_LC(c)         NXIsUpper((unsigned int)(c))
240 #  define isLOWER_LC(c)         NXIsLower((unsigned int)(c))
241 #  define isPRINT_LC(c)         NXIsPrint((unsigned int)(c))
242 #  define toUPPER_LC(c)         NXToUpper((unsigned int)(c))
243 #  define toLOWER_LC(c)         NXToLower((unsigned int)(c))
244
245 #else /* !USE_NEXT_CTYPE */
246 #  if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
247
248 #    define isALNUM_LC(c) \
249         (isalpha((unsigned char)(c)) || \
250          isdigit((unsigned char)(c)) || (char)(c) == '_')
251 #    define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
252 #    define isALPHA_LC(c)       isalpha((unsigned char)(c))
253 #    define isSPACE_LC(c)       isspace((unsigned char)(c))
254 #    define isDIGIT_LC(c)       isdigit((unsigned char)(c))
255 #    define isUPPER_LC(c)       isupper((unsigned char)(c))
256 #    define isLOWER_LC(c)       islower((unsigned char)(c))
257 #    define isPRINT_LC(c)       isprint((unsigned char)(c))
258 #    define toUPPER_LC(c)       toupper((unsigned char)(c))
259 #    define toLOWER_LC(c)       tolower((unsigned char)(c))
260
261 #  else
262
263 #    define isALNUM_LC(c) \
264         (isascii(c) && (isalpha(c) || isdigit(c) || (c) == '_'))
265 #    define isIDFIRST_LC(c)     (isascii(c) && (isalpha(c) || (c) == '_'))
266 #    define isALPHA_LC(c)       (isascii(c) && isalpha(c))
267 #    define isSPACE_LC(c)       (isascii(c) && isspace(c))
268 #    define isDIGIT_LC(c)       (isascii(c) && isdigit(c))
269 #    define isUPPER_LC(c)       (isascii(c) && isupper(c))
270 #    define isLOWER_LC(c)       (isascii(c) && islower(c))
271 #    define isPRINT_LC(c)       (isascii(c) && isprint(c))
272 #    define toUPPER_LC(c)       toupper(c)
273 #    define toLOWER_LC(c)       tolower(c)
274
275 #  endif
276 #endif /* USE_NEXT_CTYPE */
277
278 #define isALNUM_uni(c)          is_uni_alnum(c)
279 #define isIDFIRST_uni(c)        is_uni_idfirst(c)
280 #define isALPHA_uni(c)          is_uni_alpha(c)
281 #define isSPACE_uni(c)          is_uni_space(c)
282 #define isDIGIT_uni(c)          is_uni_digit(c)
283 #define isUPPER_uni(c)          is_uni_upper(c)
284 #define isLOWER_uni(c)          is_uni_lower(c)
285 #define isPRINT_uni(c)          is_uni_print(c)
286 #define toUPPER_uni(c)          to_uni_upper(c)
287 #define toTITLE_uni(c)          to_uni_title(c)
288 #define toLOWER_uni(c)          to_uni_lower(c)
289
290 #define isALNUM_LC_uni(c)       (c < 256 ? isALNUM_LC(c) : is_uni_alnum_lc(c))
291 #define isIDFIRST_LC_uni(c)     (c < 256 ? isIDFIRST_LC(c) : is_uni_idfirst_lc(c))
292 #define isALPHA_LC_uni(c)       (c < 256 ? isALPHA_LC(c) : is_uni_alpha_lc(c))
293 #define isSPACE_LC_uni(c)       (c < 256 ? isSPACE_LC(c) : is_uni_space_lc(c))
294 #define isDIGIT_LC_uni(c)       (c < 256 ? isDIGIT_LC(c) : is_uni_digit_lc(c))
295 #define isUPPER_LC_uni(c)       (c < 256 ? isUPPER_LC(c) : is_uni_upper_lc(c))
296 #define isLOWER_LC_uni(c)       (c < 256 ? isLOWER_LC(c) : is_uni_lower_lc(c))
297 #define isPRINT_LC_uni(c)       (c < 256 ? isPRINT_LC(c) : is_uni_print_lc(c))
298 #define toUPPER_LC_uni(c)       (c < 256 ? toUPPER_LC(c) : to_uni_upper_lc(c))
299 #define toTITLE_LC_uni(c)       (c < 256 ? toUPPER_LC(c) : to_uni_title_lc(c))
300 #define toLOWER_LC_uni(c)       (c < 256 ? toLOWER_LC(c) : to_uni_lower_lc(c))
301
302 #define isALNUM_utf8(p)         is_utf8_alnum(p)
303 #define isIDFIRST_utf8(p)       is_utf8_idfirst(p)
304 #define isALPHA_utf8(p)         is_utf8_alpha(p)
305 #define isSPACE_utf8(p)         is_utf8_space(p)
306 #define isDIGIT_utf8(p)         is_utf8_digit(p)
307 #define isUPPER_utf8(p)         is_utf8_upper(p)
308 #define isLOWER_utf8(p)         is_utf8_lower(p)
309 #define isPRINT_utf8(p)         is_utf8_print(p)
310 #define toUPPER_utf8(p)         to_utf8_upper(p)
311 #define toTITLE_utf8(p)         to_utf8_title(p)
312 #define toLOWER_utf8(p)         to_utf8_lower(p)
313
314 #define isALNUM_LC_utf8(p)      isALNUM_LC_uni(utf8_to_uv(p, 0))
315 #define isIDFIRST_LC_utf8(p)    isIDFIRST_LC_uni(utf8_to_uv(p, 0))
316 #define isALPHA_LC_utf8(p)      isALPHA_LC_uni(utf8_to_uv(p, 0))
317 #define isSPACE_LC_utf8(p)      isSPACE_LC_uni(utf8_to_uv(p, 0))
318 #define isDIGIT_LC_utf8(p)      isDIGIT_LC_uni(utf8_to_uv(p, 0))
319 #define isUPPER_LC_utf8(p)      isUPPER_LC_uni(utf8_to_uv(p, 0))
320 #define isLOWER_LC_utf8(p)      isLOWER_LC_uni(utf8_to_uv(p, 0))
321 #define isPRINT_LC_utf8(p)      isPRINT_LC_uni(utf8_to_uv(p, 0))
322 #define toUPPER_LC_utf8(p)      toUPPER_LC_uni(utf8_to_uv(p, 0))
323 #define toTITLE_LC_utf8(p)      toTITLE_LC_uni(utf8_to_uv(p, 0))
324 #define toLOWER_LC_utf8(p)      toLOWER_LC_uni(utf8_to_uv(p, 0))
325
326 #ifdef EBCDIC
327 EXT int ebcdic_control _((int));
328 #  define toCTRL(c)     ebcdic_control(c)
329 #else
330   /* This conversion works both ways, strangely enough. */
331 #  define toCTRL(c)    (toUPPER(c) ^ 64)
332 #endif
333
334 /* Line numbers are unsigned, 16 bits. */
335 typedef U16 line_t;
336 #ifdef lint
337 #define NOLINE ((line_t)0)
338 #else
339 #define NOLINE ((line_t) 65535)
340 #endif
341
342
343 /* This looks obsolete (IZ):
344
345    XXX LEAKTEST doesn't really work in perl5.  There are direct calls to
346    safemalloc() in the source, so LEAKTEST won't pick them up.
347    Further, if you try LEAKTEST, you'll also end up calling
348    Safefree, which might call safexfree() on some things that weren't
349    malloced with safexmalloc.  The correct "fix" to this, if anyone
350    is interested, is to ensure that all calls go through the New and
351    Renew macros.
352         --Andy Dougherty                August 1996
353 */
354
355 #ifndef lint
356
357 #define NEWSV(x,len)    newSV(len)
358
359 #ifndef LEAKTEST
360
361 #define New(x,v,n,t)    (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
362 #define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
363 #define Newz(x,v,n,t)   (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \
364                         memzero((char*)(v), (n)*sizeof(t))
365 #define Renew(v,n,t) \
366           (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
367 #define Renewc(v,n,t,c) \
368           (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
369 #define Safefree(d)     safefree((Malloc_t)(d))
370
371 #else /* LEAKTEST */
372
373 #define New(x,v,n,t)    (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
374 #define Newc(x,v,n,t,c) (v = (c*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
375 #define Newz(x,v,n,t)   (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))), \
376                          memzero((char*)(v), (n)*sizeof(t))
377 #define Renew(v,n,t) \
378           (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
379 #define Renewc(v,n,t,c) \
380           (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
381 #define Safefree(d)     safexfree((Malloc_t)(d))
382
383 #define MAXXCOUNT 1400
384 #define MAXY_SIZE 80
385 #define MAXYCOUNT 16                    /* (MAXY_SIZE/4 + 1) */
386 extern long xcount[MAXXCOUNT];
387 extern long lastxcount[MAXXCOUNT];
388 extern long xycount[MAXXCOUNT][MAXYCOUNT];
389 extern long lastxycount[MAXXCOUNT][MAXYCOUNT];
390
391 #endif /* LEAKTEST */
392
393 #define Move(s,d,n,t)   (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
394 #define Copy(s,d,n,t)   (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
395 #define Zero(d,n,t)     (void)memzero((char*)(d), (n) * sizeof(t))
396
397 #else /* lint */
398
399 #define New(x,v,n,s)    (v = Null(s *))
400 #define Newc(x,v,n,s,c) (v = Null(s *))
401 #define Newz(x,v,n,s)   (v = Null(s *))
402 #define Renew(v,n,s)    (v = Null(s *))
403 #define Move(s,d,n,t)
404 #define Copy(s,d,n,t)
405 #define Zero(d,n,t)
406 #define Safefree(d)     (d) = (d)
407
408 #endif /* lint */
409
410 #ifdef USE_STRUCT_COPY
411 #define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
412 #else
413 #define StructCopy(s,d,t) Copy(s,d,1,t)
414 #endif