3 * Copyright (c) 1991-1999, Larry Wall
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.
10 #if !defined(__STDC__)
21 #define Null(type) ((type)NULL)
22 #define Nullch Null(char*)
23 #define Nullfp Null(PerlIO*)
24 #define Nullsv Null(SV*)
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
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.
54 /* The NeXT dynamic loader headers will not build with the bool macro
55 So declare them now to clear confusion.
57 #if defined(NeXT) || defined(__NeXT__)
60 typedef enum bool { FALSE = 0, TRUE = 1 } bool;
64 # endif /* !HAS_BOOL */
65 #endif /* NeXT || __NeXT__ */
68 # if defined(UTS) || defined(VMS)
75 /* XXX A note on the perl source internal type system. The
76 original intent was that I32 be *exactly* 32 bits.
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
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.
89 Similarly, there is no guarantee that I16 and U16 have exactly 16
92 For dealing with issues that may arise from various 32/64-bit
93 systems, we will ask Configure to check out
95 SHORTSIZE == sizeof(short)
96 INTSIZE == sizeof(int)
97 LONGSIZE == sizeof(long)
98 LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
99 PTRSIZE == sizeof(void *)
100 DOUBLESIZE == sizeof(double)
101 LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
113 # if QUADKIND == QUAD_IS_INT64_T
114 # include <sys/types.h>
115 # ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
116 # include <inttypes.h>
122 #endif /* PERL_CORE */
124 /* Mention I8SIZE, U8SIZE, I16SIZE, U16SIZE, I32SIZE, U32SIZE,
125 I64SIZE, and U64SIZE here so that metaconfig pulls them in. */
127 #if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
129 /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
130 Please search CHAR_MAX in perl.h for further details. */
131 #define U8_MAX UINT8_MAX
132 #define U8_MIN UINT8_MIN
134 #define I16_MAX INT16_MAX
135 #define I16_MIN INT16_MIN
136 #define U16_MAX UINT16_MAX
137 #define U16_MIN UINT16_MIN
139 #define I32_MAX INT32_MAX
140 #define I32_MIN INT32_MIN
141 #define U32_MAX UINT32_MAX
142 #define U32_MIN UINT32_MIN
146 /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
147 Please search CHAR_MAX in perl.h for further details. */
148 #define U8_MAX PERL_UCHAR_MAX
149 #define U8_MIN PERL_UCHAR_MIN
151 #define I16_MAX PERL_SHORT_MAX
152 #define I16_MIN PERL_SHORT_MIN
153 #define U16_MAX PERL_USHORT_MAX
154 #define U16_MIN PERL_USHORT_MIN
157 # define I32_MAX PERL_INT_MAX
158 # define I32_MIN PERL_INT_MIN
159 # define U32_MAX PERL_UINT_MAX
160 # define U32_MIN PERL_UINT_MIN
162 # define I32_MAX PERL_LONG_MAX
163 # define I32_MIN PERL_LONG_MIN
164 # define U32_MAX PERL_ULONG_MAX
165 # define U32_MIN PERL_ULONG_MIN
170 #define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
171 #define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8)
172 #define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */
174 #define Ctl(ch) ((ch) & 037)
176 #define strNE(s1,s2) (strcmp(s1,s2))
177 #define strEQ(s1,s2) (!strcmp(s1,s2))
178 #define strLT(s1,s2) (strcmp(s1,s2) < 0)
179 #define strLE(s1,s2) (strcmp(s1,s2) <= 0)
180 #define strGT(s1,s2) (strcmp(s1,s2) > 0)
181 #define strGE(s1,s2) (strcmp(s1,s2) >= 0)
182 #define strnNE(s1,s2,l) (strncmp(s1,s2,l))
183 #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
186 # define memNE(s1,s2,l) (memcmp(s1,s2,l))
187 # define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
189 # define memNE(s1,s2,l) (bcmp(s1,s2,l))
190 # define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
196 * Unfortunately, the introduction of locales means that we
197 * can't trust isupper(), etc. to tell the truth. And when
198 * it comes to /\w+/ with tainting enabled, we *must* be able
199 * to trust our character classes.
201 * Therefore, the default tests in the text of Perl will be
202 * independent of locale. Any code that wants to depend on
203 * the current locale will use the tests that begin with "lc".
206 #ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
212 #define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_')
213 #define isIDFIRST(c) (isALPHA(c) || (c) == '_')
214 #define isALPHA(c) (isUPPER(c) || isLOWER(c))
216 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
217 #define isDIGIT(c) ((c) >= '0' && (c) <= '9')
219 /* In EBCDIC we do not do locales: therefore() isupper() is fine. */
220 # define isUPPER(c) isupper(c)
221 # define isLOWER(c) islower(c)
222 # define isALNUMC(c) isalnum(c)
223 # define isASCII(c) isascii(c)
224 # define isCNTRL(c) iscntrl(c)
225 # define isGRAPH(c) isgraph(c)
226 # define isPRINT(c) isprint(c)
227 # define isPUNCT(c) ispunct(c)
228 # define isXDIGIT(c) isxdigit(c)
229 # define toUPPER(c) toupper(c)
230 # define toLOWER(c) tolower(c)
232 # define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
233 # define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
234 # define isALNUMC(c) (isALPHA(c) || isDIGIT(c))
235 # define isASCII(c) ((c) <= 127)
236 # define isCNTRL(c) ((c) < ' ')
237 # define isGRAPH(c) (isALNUM(c) || isPUNCT(c))
238 # define isPRINT(c) (((c) > 32 && (c) < 127) || isSPACE(c))
239 # define isPUNCT(c) (((c) >= 33 && (c) <= 47) || ((c) >= 58 && (c) <= 64) || ((c) >= 91 && (c) <= 96) || ((c) >= 123 && (c) <= 126))
240 # define isXDIGIT(c) (isdigit(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
241 # define toUPPER(c) (isLOWER(c) ? (c) - ('a' - 'A') : (c))
242 # define toLOWER(c) (isUPPER(c) ? (c) + ('a' - 'A') : (c))
245 #ifdef USE_NEXT_CTYPE
247 # define isALNUM_LC(c) \
248 (NXIsAlNum((unsigned int)(c)) || (char)(c) == '_')
249 # define isIDFIRST_LC(c) \
250 (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
251 # define isALPHA_LC(c) NXIsAlpha((unsigned int)(c))
252 # define isSPACE_LC(c) NXIsSpace((unsigned int)(c))
253 # define isDIGIT_LC(c) NXIsDigit((unsigned int)(c))
254 # define isUPPER_LC(c) NXIsUpper((unsigned int)(c))
255 # define isLOWER_LC(c) NXIsLower((unsigned int)(c))
256 # define isALNUMC_LC(c) NXIsAlNum((unsigned int)(c))
257 # define isCNTRL_LC(c) NXIsCntrl((unsigned int)(c))
258 # define isGRAPH_LC(c) NXIsGraph((unsigned int)(c))
259 # define isPRINT_LC(c) NXIsPrint((unsigned int)(c))
260 # define isPUNCT_LC(c) NXIsPunct((unsigned int)(c))
261 # define toUPPER_LC(c) NXToUpper((unsigned int)(c))
262 # define toLOWER_LC(c) NXToLower((unsigned int)(c))
264 #else /* !USE_NEXT_CTYPE */
266 # if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
268 # define isALNUM_LC(c) (isalnum((unsigned char)(c)) || (char)(c) == '_')
269 # define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
270 # define isALPHA_LC(c) isalpha((unsigned char)(c))
271 # define isSPACE_LC(c) isspace((unsigned char)(c))
272 # define isDIGIT_LC(c) isdigit((unsigned char)(c))
273 # define isUPPER_LC(c) isupper((unsigned char)(c))
274 # define isLOWER_LC(c) islower((unsigned char)(c))
275 # define isALNUMC_LC(c) isalnum((unsigned char)(c))
276 # define isCNTRL_LC(c) iscntrl((unsigned char)(c))
277 # define isGRAPH_LC(c) isgraph((unsigned char)(c))
278 # define isPRINT_LC(c) isprint((unsigned char)(c))
279 # define isPUNCT_LC(c) ispunct((unsigned char)(c))
280 # define toUPPER_LC(c) toupper((unsigned char)(c))
281 # define toLOWER_LC(c) tolower((unsigned char)(c))
285 # define isALNUM_LC(c) (isascii(c) && (isalnum(c) || (c) == '_'))
286 # define isIDFIRST_LC(c) (isascii(c) && (isalpha(c) || (c) == '_'))
287 # define isALPHA_LC(c) (isascii(c) && isalpha(c))
288 # define isSPACE_LC(c) (isascii(c) && isspace(c))
289 # define isDIGIT_LC(c) (isascii(c) && isdigit(c))
290 # define isUPPER_LC(c) (isascii(c) && isupper(c))
291 # define isLOWER_LC(c) (isascii(c) && islower(c))
292 # define isALNUMC_LC(c) (isascii(c) && isalnum(c))
293 # define isCNTRL_LC(c) (isascii(c) && iscntrl(c))
294 # define isGRAPH_LC(c) (isascii(c) && isgraph(c))
295 # define isPRINT_LC(c) (isascii(c) && isprint(c))
296 # define isPUNCT_LC(c) (isascii(c) && ispunct(c))
297 # define toUPPER_LC(c) toupper(c)
298 # define toLOWER_LC(c) tolower(c)
301 #endif /* USE_NEXT_CTYPE */
303 #define isALNUM_uni(c) is_uni_alnum(c)
304 #define isIDFIRST_uni(c) is_uni_idfirst(c)
305 #define isALPHA_uni(c) is_uni_alpha(c)
306 #define isSPACE_uni(c) is_uni_space(c)
307 #define isDIGIT_uni(c) is_uni_digit(c)
308 #define isUPPER_uni(c) is_uni_upper(c)
309 #define isLOWER_uni(c) is_uni_lower(c)
310 #define isALNUMC_uni(c) is_uni_alnumc(c)
311 #define isASCII_uni(c) is_uni_ascii(c)
312 #define isCNTRL_uni(c) is_uni_cntrl(c)
313 #define isGRAPH_uni(c) is_uni_graph(c)
314 #define isPRINT_uni(c) is_uni_print(c)
315 #define isPUNCT_uni(c) is_uni_punct(c)
316 #define isXDIGIT_uni(c) is_uni_xdigit(c)
317 #define toUPPER_uni(c) to_uni_upper(c)
318 #define toTITLE_uni(c) to_uni_title(c)
319 #define toLOWER_uni(c) to_uni_lower(c)
321 #define isALNUM_LC_uni(c) (c < 256 ? isALNUM_LC(c) : is_uni_alnum_lc(c))
322 #define isIDFIRST_LC_uni(c) (c < 256 ? isIDFIRST_LC(c) : is_uni_idfirst_lc(c))
323 #define isALPHA_LC_uni(c) (c < 256 ? isALPHA_LC(c) : is_uni_alpha_lc(c))
324 #define isSPACE_LC_uni(c) (c < 256 ? isSPACE_LC(c) : is_uni_space_lc(c))
325 #define isDIGIT_LC_uni(c) (c < 256 ? isDIGIT_LC(c) : is_uni_digit_lc(c))
326 #define isUPPER_LC_uni(c) (c < 256 ? isUPPER_LC(c) : is_uni_upper_lc(c))
327 #define isLOWER_LC_uni(c) (c < 256 ? isLOWER_LC(c) : is_uni_lower_lc(c))
328 #define isALNUMC_LC_uni(c) (c < 256 ? isALNUMC_LC(c) : is_uni_alnumc_lc(c))
329 #define isCNTRL_LC_uni(c) (c < 256 ? isCNTRL_LC(c) : is_uni_cntrl_lc(c))
330 #define isGRAPH_LC_uni(c) (c < 256 ? isGRAPH_LC(c) : is_uni_graph_lc(c))
331 #define isPRINT_LC_uni(c) (c < 256 ? isPRINT_LC(c) : is_uni_print_lc(c))
332 #define isPUNCT_LC_uni(c) (c < 256 ? isPUNCT_LC(c) : is_uni_punct_lc(c))
333 #define toUPPER_LC_uni(c) (c < 256 ? toUPPER_LC(c) : to_uni_upper_lc(c))
334 #define toTITLE_LC_uni(c) (c < 256 ? toUPPER_LC(c) : to_uni_title_lc(c))
335 #define toLOWER_LC_uni(c) (c < 256 ? toLOWER_LC(c) : to_uni_lower_lc(c))
337 #define isALNUM_utf8(p) is_utf8_alnum(p)
338 #define isIDFIRST_utf8(p) is_utf8_idfirst(p)
339 #define isALPHA_utf8(p) is_utf8_alpha(p)
340 #define isSPACE_utf8(p) is_utf8_space(p)
341 #define isDIGIT_utf8(p) is_utf8_digit(p)
342 #define isUPPER_utf8(p) is_utf8_upper(p)
343 #define isLOWER_utf8(p) is_utf8_lower(p)
344 #define isALNUMC_utf8(p) is_utf8_alnumc(p)
345 #define isASCII_utf8(p) is_utf8_ascii(p)
346 #define isCNTRL_utf8(p) is_utf8_cntrl(p)
347 #define isGRAPH_utf8(p) is_utf8_graph(p)
348 #define isPRINT_utf8(p) is_utf8_print(p)
349 #define isPUNCT_utf8(p) is_utf8_punct(p)
350 #define isXDIGIT_utf8(p) is_utf8_xdigit(p)
351 #define toUPPER_utf8(p) to_utf8_upper(p)
352 #define toTITLE_utf8(p) to_utf8_title(p)
353 #define toLOWER_utf8(p) to_utf8_lower(p)
355 #define isALNUM_LC_utf8(p) isALNUM_LC_uni(utf8_to_uv(p, 0))
356 #define isIDFIRST_LC_utf8(p) isIDFIRST_LC_uni(utf8_to_uv(p, 0))
357 #define isALPHA_LC_utf8(p) isALPHA_LC_uni(utf8_to_uv(p, 0))
358 #define isSPACE_LC_utf8(p) isSPACE_LC_uni(utf8_to_uv(p, 0))
359 #define isDIGIT_LC_utf8(p) isDIGIT_LC_uni(utf8_to_uv(p, 0))
360 #define isUPPER_LC_utf8(p) isUPPER_LC_uni(utf8_to_uv(p, 0))
361 #define isLOWER_LC_utf8(p) isLOWER_LC_uni(utf8_to_uv(p, 0))
362 #define isALNUMC_LC_utf8(p) isALNUMC_LC_uni(utf8_to_uv(p, 0))
363 #define isCNTRL_LC_utf8(p) isCNTRL_LC_uni(utf8_to_uv(p, 0))
364 #define isGRAPH_LC_utf8(p) isGRAPH_LC_uni(utf8_to_uv(p, 0))
365 #define isPRINT_LC_utf8(p) isPRINT_LC_uni(utf8_to_uv(p, 0))
366 #define isPUNCT_LC_utf8(p) isPUNCT_LC_uni(utf8_to_uv(p, 0))
367 #define toUPPER_LC_utf8(p) toUPPER_LC_uni(utf8_to_uv(p, 0))
368 #define toTITLE_LC_utf8(p) toTITLE_LC_uni(utf8_to_uv(p, 0))
369 #define toLOWER_LC_utf8(p) toLOWER_LC_uni(utf8_to_uv(p, 0))
372 EXT int ebcdic_control (int);
373 # define toCTRL(c) ebcdic_control(c)
375 /* This conversion works both ways, strangely enough. */
376 # define toCTRL(c) (toUPPER(c) ^ 64)
379 /* Line numbers are unsigned, 16 bits. */
382 #define NOLINE ((line_t)0)
384 #define NOLINE ((line_t) 65535)
388 /* This looks obsolete (IZ):
390 XXX LEAKTEST doesn't really work in perl5. There are direct calls to
391 safemalloc() in the source, so LEAKTEST won't pick them up.
392 Further, if you try LEAKTEST, you'll also end up calling
393 Safefree, which might call safexfree() on some things that weren't
394 malloced with safexmalloc. The correct "fix" to this, if anyone
395 is interested, is to ensure that all calls go through the New and
397 --Andy Dougherty August 1996
402 #define NEWSV(x,len) newSV(len)
406 #define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
407 #define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
408 #define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \
409 memzero((char*)(v), (n)*sizeof(t))
410 #define Renew(v,n,t) \
411 (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
412 #define Renewc(v,n,t,c) \
413 (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
414 #define Safefree(d) safefree((Malloc_t)(d))
418 #define New(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
419 #define Newc(x,v,n,t,c) (v = (c*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
420 #define Newz(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))), \
421 memzero((char*)(v), (n)*sizeof(t))
422 #define Renew(v,n,t) \
423 (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
424 #define Renewc(v,n,t,c) \
425 (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
426 #define Safefree(d) safexfree((Malloc_t)(d))
428 #define MAXXCOUNT 1400
430 #define MAXYCOUNT 16 /* (MAXY_SIZE/4 + 1) */
431 extern long xcount[MAXXCOUNT];
432 extern long lastxcount[MAXXCOUNT];
433 extern long xycount[MAXXCOUNT][MAXYCOUNT];
434 extern long lastxycount[MAXXCOUNT][MAXYCOUNT];
436 #endif /* LEAKTEST */
438 #define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
439 #define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
440 #define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t))
444 #define New(x,v,n,s) (v = Null(s *))
445 #define Newc(x,v,n,s,c) (v = Null(s *))
446 #define Newz(x,v,n,s) (v = Null(s *))
447 #define Renew(v,n,s) (v = Null(s *))
448 #define Move(s,d,n,t)
449 #define Copy(s,d,n,t)
451 #define Safefree(d) (d) = (d)
455 #ifdef USE_STRUCT_COPY
456 #define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
458 #define StructCopy(s,d,t) Copy(s,d,1,t)