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