This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Namespace cleanup: Does SDBM need binary compatibility?
[perl5.git] / handy.h
CommitLineData
a0d0e21e 1/* handy.h
a687059c 2 *
a0d0e21e 3 * Copyright (c) 1991-1994, Larry Wall
a687059c 4 *
6e21c824
LW
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.
8d063cd8 7 *
8d063cd8
LW
8 */
9
85e6fe83 10#if !defined(__STDC__)
378cc40b
LW
11#ifdef NULL
12#undef NULL
13#endif
a687059c
LW
14#ifndef I286
15# define NULL 0
16#else
17# define NULL 0L
18#endif
85e6fe83
LW
19#endif
20
378cc40b 21#define Null(type) ((type)NULL)
8d063cd8 22#define Nullch Null(char*)
760ac839 23#define Nullfp Null(PerlIO*)
79072805 24#define Nullsv Null(SV*)
8d063cd8 25
641d3f0b
PP
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
27d4fb96
PP
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*/
232e078e 40/* bool is built-in for g++-2.6.3, which might be used for an extension.
5d94fbed
AD
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
641d3f0b
PP
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
5d94fbed
AD
67#ifndef HAS_BOOL
68# ifdef UTS
69# define bool int
70# else
71# define bool char
72# endif
a687059c 73#endif
0d3e774c 74
27d4fb96
PP
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
79072805
LW
86typedef char I8;
87typedef unsigned char U8;
5c9fa16e
KA
88/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
89 Please search CHAR_MAX in perl.h for further details. */
27d4fb96
PP
90#define U8_MAX PERL_UCHAR_MAX
91#define U8_MIN PERL_UCHAR_MIN
79072805
LW
92
93typedef short I16;
94typedef unsigned short U16;
27d4fb96
PP
95#define I16_MAX PERL_SHORT_MAX
96#define I16_MIN PERL_SHORT_MIN
97#define U16_MAX PERL_USHORT_MAX
98#define U16_MIN PERL_USHORT_MIN
79072805 99
a0d0e21e 100#if BYTEORDER > 0x4321
85e6fe83
LW
101 typedef int I32;
102 typedef unsigned int U32;
27d4fb96
PP
103# define I32_MAX PERL_INT_MAX
104# define I32_MIN PERL_INT_MIN
105# define U32_MAX PERL_UINT_MAX
106# define U32_MIN PERL_UINT_MIN
79072805 107#else
85e6fe83
LW
108 typedef long I32;
109 typedef unsigned long U32;
27d4fb96
PP
110# define I32_MAX PERL_LONG_MAX
111# define I32_MIN PERL_LONG_MIN
112# define U32_MAX PERL_ULONG_MAX
113# define U32_MIN PERL_ULONG_MIN
79072805
LW
114#endif
115
ff68c719 116#define Ctl(ch) ((ch) & 037)
8d063cd8
LW
117
118#define strNE(s1,s2) (strcmp(s1,s2))
119#define strEQ(s1,s2) (!strcmp(s1,s2))
120#define strLT(s1,s2) (strcmp(s1,s2) < 0)
121#define strLE(s1,s2) (strcmp(s1,s2) <= 0)
122#define strGT(s1,s2) (strcmp(s1,s2) > 0)
123#define strGE(s1,s2) (strcmp(s1,s2) >= 0)
124#define strnNE(s1,s2,l) (strncmp(s1,s2,l))
125#define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
378cc40b 126
bbce6d69
PP
127/*
128 * Character classes.
129 *
130 * Unfortunately, the introduction of locales means that we
131 * can't trust isupper(), etc. to tell the truth. And when
132 * it comes to /\w+/ with tainting enabled, we *must* be able
133 * to trust our character classes.
134 *
135 * Therefore, the default tests in the text of Perl will be
136 * independent of locale. Any code that wants to depend on
137 * the current locale will use the tests that begin with "lc".
138 */
139
2304df62
AD
140#ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
141# ifndef CTYPE256
142# define CTYPE256
143# endif
144#endif
145
bbce6d69
PP
146#define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_')
147#define isIDFIRST(c) (isALPHA(c) || (c) == '_')
148#define isALPHA(c) (isUPPER(c) || isLOWER(c))
149#define isSPACE(c) \
150 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
151#define isDIGIT(c) ((c) >= '0' && (c) <= '9')
152#define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
153#define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
154#define isPRINT(c) (((c) > 32 && (c) < 127) || isSPACE(c))
155#define toUPPER(c) (isLOWER(c) ? (c) - ('a' - 'A') : (c))
156#define toLOWER(c) (isUPPER(c) ? (c) + ('a' - 'A') : (c))
157
158#ifdef USE_NEXT_CTYPE
159
160# define isALNUM_LC(c) \
ff68c719
PP
161 (NXIsAlpha((unsigned int)(c)) || NXIsDigit((unsigned int)(c)) || \
162 (char)(c) == '_')
163# define isIDFIRST_LC(c) \
164 (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
165# define isALPHA_LC(c) NXIsAlpha((unsigned int)(c))
166# define isSPACE_LC(c) NXIsSpace((unsigned int)(c))
167# define isDIGIT_LC(c) NXIsDigit((unsigned int)(c))
168# define isUPPER_LC(c) NXIsUpper((unsigned int)(c))
169# define isLOWER_LC(c) NXIsLower((unsigned int)(c))
170# define isPRINT_LC(c) NXIsPrint((unsigned int)(c))
171# define toUPPER_LC(c) NXToUpper((unsigned int)(c))
172# define toLOWER_LC(c) NXToLower((unsigned int)(c))
bbce6d69
PP
173
174#else /* !USE_NEXT_CTYPE */
175# if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
176
177# define isALNUM_LC(c) \
178 (isalpha((unsigned char)(c)) || \
ff68c719
PP
179 isdigit((unsigned char)(c)) || (char)(c) == '_')
180# define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
bbce6d69
PP
181# define isALPHA_LC(c) isalpha((unsigned char)(c))
182# define isSPACE_LC(c) isspace((unsigned char)(c))
183# define isDIGIT_LC(c) isdigit((unsigned char)(c))
184# define isUPPER_LC(c) isupper((unsigned char)(c))
185# define isLOWER_LC(c) islower((unsigned char)(c))
186# define isPRINT_LC(c) isprint((unsigned char)(c))
187# define toUPPER_LC(c) toupper((unsigned char)(c))
188# define toLOWER_LC(c) tolower((unsigned char)(c))
189
190# else
191
192# define isALNUM_LC(c) \
ff68c719 193 (isascii(c) && (isalpha(c) || isdigit(c) || (c) == '_'))
bbce6d69
PP
194# define isIDFIRST_LC(c) (isascii(c) && (isalpha(c) || (c) == '_'))
195# define isALPHA_LC(c) (isascii(c) && isalpha(c))
196# define isSPACE_LC(c) (isascii(c) && isspace(c))
197# define isDIGIT_LC(c) (isascii(c) && isdigit(c))
198# define isUPPER_LC(c) (isascii(c) && isupper(c))
199# define isLOWER_LC(c) (isascii(c) && islower(c))
200# define isPRINT_LC(c) (isascii(c) && isprint(c))
201# define toUPPER_LC(c) toupper(c)
202# define toLOWER_LC(c) tolower(c)
203
204# endif
a0d0e21e 205#endif /* USE_NEXT_CTYPE */
55204971 206
bbce6d69
PP
207/* This conversion works both ways, strangely enough. */
208#define toCTRL(c) (toUPPER(c) ^ 64)
209
378cc40b 210/* Line numbers are unsigned, 16 bits. */
79072805 211typedef U16 line_t;
378cc40b
LW
212#ifdef lint
213#define NOLINE ((line_t)0)
214#else
215#define NOLINE ((line_t) 65535)
216#endif
217
27d4fb96
PP
218/* XXX LEAKTEST doesn't really work in perl5. There are direct calls to
219 safemalloc() in the source, so LEAKTEST won't pick them up.
220 Further, if you try LEAKTEST, you'll also end up calling
221 Safefree, which might call safexfree() on some things that weren't
222 malloced with safexmalloc. The correct "fix" to this, if anyone
223 is interested, is to ensure that all calls go through the New and
224 Renew macros.
225 --Andy Dougherty August 1996
226*/
55497cff 227
a687059c
LW
228#ifndef lint
229#ifndef LEAKTEST
598a3d64 230
ff68c719
PP
231#define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
232#define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
233#define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \
234 memzero((char*)(v), (n)*sizeof(t))
235#define Renew(v,n,t) \
236 (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
237#define Renewc(v,n,t,c) \
238 (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
239#define Safefree(d) safefree((Malloc_t)(d))
240#define NEWSV(x,len) newSV(len)
55497cff 241
a687059c 242#else /* LEAKTEST */
55497cff 243
ff68c719
PP
244#define New(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
245#define Newc(x,v,n,t,c) (v = (c*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
246#define Newz(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))), \
247 memzero((char*)(v), (n)*sizeof(t))
248#define Renew(v,n,t) \
249 (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
250#define Renewc(v,n,t,c) \
251 (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
252#define Safefree(d) safexfree((Malloc_t)d)
253#define NEWSV(x,len) newSV(x,len)
254
a687059c
LW
255#define MAXXCOUNT 1200
256long xcount[MAXXCOUNT];
257long lastxcount[MAXXCOUNT];
55497cff 258
a687059c 259#endif /* LEAKTEST */
55497cff 260
ff68c719
PP
261#define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
262#define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
263#define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t))
55497cff 264
a687059c 265#else /* lint */
55497cff 266
ff68c719
PP
267#define New(x,v,n,s) (v = Null(s *))
268#define Newc(x,v,n,s,c) (v = Null(s *))
269#define Newz(x,v,n,s) (v = Null(s *))
270#define Renew(v,n,s) (v = Null(s *))
bee1dbe2 271#define Move(s,d,n,t)
a687059c
LW
272#define Copy(s,d,n,t)
273#define Zero(d,n,t)
ff68c719 274#define Safefree(d) (d) = (d)
55497cff 275
a687059c 276#endif /* lint */
bee1dbe2 277
2304df62 278#ifdef USE_STRUCT_COPY
ff68c719 279#define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
bee1dbe2
LW
280#else
281#define StructCopy(s,d,t) Copy(s,d,1,t)
282#endif