This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don't attempt to use non-standard <memory.h>
[perl5.git] / perl.h
CommitLineData
a0d0e21e 1/* perl.h
a687059c 2 *
1129b882 3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
5cd3f21a 4 * 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
a687059c 5 *
352d5a3a
LW
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8d063cd8 8 *
8d063cd8 9 */
d6376244 10
85e6fe83
LW
11#ifndef H_PERL
12#define H_PERL 1
8d063cd8 13
760ac839
LW
14#ifdef PERL_FOR_X2P
15/*
d460ef45 16 * This file is being used for x2p stuff.
760ac839 17 * Above symbol is defined via -D in 'x2p/Makefile.SH'
d460ef45 18 * Decouple x2p stuff from some of perls more extreme eccentricities.
760ac839 19 */
55497cff 20#undef MULTIPLICITY
760ac839
LW
21#undef USE_STDIO
22#define USE_STDIO
23#endif /* PERL_FOR_X2P */
24
d460ef45 25#ifdef PERL_MICRO
12ae5dfc
JH
26# include "uconfig.h"
27#else
611cf957 28# include "config.h"
12ae5dfc 29#endif
0cb96387 30
70febf4f
CB
31/* this is used for functions which take a depth trailing
32 * argument under debugging */
33#ifdef DEBUGGING
34#define _pDEPTH ,U32 depth
35#define _aDEPTH ,depth
36#else
37#define _pDEPTH
38#define _aDEPTH
39#endif
40
7dc3eb73 41/* NOTE 1: that with gcc -std=c89 the __STDC_VERSION__ is *not* defined
9267464d 42 * because the __STDC_VERSION__ became a thing only with C90. Therefore,
7dc3eb73
JH
43 * with gcc, HAS_C99 will never become true as long as we use -std=c89.
44
45 * NOTE 2: headers lie. Do not expect that if HAS_C99 gets to be true,
46 * all the C99 features are there and are correct. */
9267464d 47#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
996959af 48 defined(_STDC_C99) || defined(__c99)
86a3cabf 49# define HAS_C99 1
86a3cabf
JH
50#endif
51
2c2d71f5
JH
52/* See L<perlguts/"The Perl API"> for detailed notes on
53 * PERL_IMPLICIT_CONTEXT and PERL_IMPLICIT_SYS */
54
ac6bedea
JH
55/* Note that from here --> to <-- the same logic is
56 * repeated in makedef.pl, so be certain to update
57 * both places when editing. */
58
6f4183fe 59#ifdef USE_ITHREADS
acfe0abc 60# if !defined(MULTIPLICITY)
6f4183fe
GS
61# define MULTIPLICITY
62# endif
63#endif
64
27da23d5
JH
65#ifdef PERL_GLOBAL_STRUCT_PRIVATE
66# ifndef PERL_GLOBAL_STRUCT
67# define PERL_GLOBAL_STRUCT
68# endif
69#endif
97aff369 70
27da23d5
JH
71#ifdef PERL_GLOBAL_STRUCT
72# ifndef MULTIPLICITY
73# define MULTIPLICITY
74# endif
75#endif
76
97aff369
JH
77#ifdef MULTIPLICITY
78# ifndef PERL_IMPLICIT_CONTEXT
79# define PERL_IMPLICIT_CONTEXT
80# endif
81#endif
82
026fd48b
GH
83/* undef WIN32 when building on Cygwin (for libwin32) - gph */
84#ifdef __CYGWIN__
85# undef WIN32
86# undef _WIN32
87#endif
88
27da23d5
JH
89#if defined(__SYMBIAN32__) || (defined(__VC32__) && defined(WINS))
90# ifndef SYMBIAN
91# define SYMBIAN
92# endif
93#endif
94
a0fd4948 95#ifdef __SYMBIAN32__
27da23d5
JH
96# include "symbian/symbian_proto.h"
97#endif
98
99/* Any stack-challenged places. The limit varies (and often
100 * is configurable), but using more than a kilobyte of stack
101 * is usually dubious in these systems. */
739a0b84
NC
102#if defined(__SYMBIAN32__)
103/* Symbian: need to work around the SDK features. *
27da23d5
JH
104 * On WINS: MS VC5 generates calls to _chkstk, *
105 * if a "large" stack frame is allocated. *
106 * gcc on MARM does not generate calls like these. */
107# define USE_HEAP_INSTEAD_OF_STACK
108#endif
109
dd134d2c 110/* Use the reentrant APIs like localtime_r and getpwent_r */
10bc17b6 111/* Win32 has naturally threadsafe libraries, no need to use any _r variants. */
14795193 112#if defined(USE_ITHREADS) && !defined(USE_REENTRANT_API) && !defined(NETWARE) && !defined(WIN32)
10bc17b6
JH
113# define USE_REENTRANT_API
114#endif
115
ac6bedea
JH
116/* <--- here ends the logic shared by perl.h and makedef.pl */
117
9ea40801
SH
118/* Microsoft Visual C++ 6.0 needs special treatment in numerous places */
119#if defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1200 && _MSC_VER < 1300
120# define USING_MSVC6
121#endif
122
cf5a8da6
MHM
123#undef START_EXTERN_C
124#undef END_EXTERN_C
125#undef EXTERN_C
126#ifdef __cplusplus
127# define START_EXTERN_C extern "C" {
128# define END_EXTERN_C }
129# define EXTERN_C extern "C"
130#else
131# define START_EXTERN_C
132# define END_EXTERN_C
133# define EXTERN_C extern
134#endif
135
17a6c8e3
AD
136/* Fallback definitions in case we don't have definitions from config.h.
137 This should only matter for systems that don't use Configure and
138 haven't been modified to define PERL_STATIC_INLINE yet.
139*/
140#if !defined(PERL_STATIC_INLINE)
141# ifdef HAS_STATIC_INLINE
142# define PERL_STATIC_INLINE static inline
143# else
144# define PERL_STATIC_INLINE static
145# endif
146#endif
147
da5fdda8 148#if defined(PERL_GLOBAL_STRUCT) && !defined(PERL_GET_VARS)
27da23d5 149# ifdef PERL_GLOBAL_STRUCT_PRIVATE
cf5a8da6 150 EXTERN_C struct perl_vars* Perl_GetVarsPrivate();
27da23d5 151# define PERL_GET_VARS() Perl_GetVarsPrivate() /* see miniperlmain.c */
27da23d5
JH
152# else
153# define PERL_GET_VARS() PL_VarsPtr
154# endif
27da23d5
JH
155#endif
156
f0e5c859
DD
157/* this used to be off by default, now its on, see perlio.h */
158#define PERLIO_FUNCS_CONST
159
5aaab254 160#define pVAR struct perl_vars* my_vars PERL_UNUSED_DECL
27da23d5
JH
161
162#ifdef PERL_GLOBAL_STRUCT
163# define dVAR pVAR = (struct perl_vars*)PERL_GET_VARS()
164#else
165# define dVAR dNOOP
166#endif
167
c5be433b 168#ifdef PERL_IMPLICIT_CONTEXT
3db8f154
MB
169# ifndef MULTIPLICITY
170# define MULTIPLICITY
c5be433b 171# endif
e8dda941 172# define tTHX PerlInterpreter*
5aaab254 173# define pTHX tTHX my_perl PERL_UNUSED_DECL
3db8f154 174# define aTHX my_perl
9399a70c 175# define aTHXa(a) aTHX = (tTHX)a
27da23d5 176# ifdef PERL_GLOBAL_STRUCT
e8dda941 177# define dTHXa(a) dVAR; pTHX = (tTHX)a
27da23d5 178# else
e8dda941 179# define dTHXa(a) pTHX = (tTHX)a
27da23d5
JH
180# endif
181# ifdef PERL_GLOBAL_STRUCT
182# define dTHX dVAR; pTHX = PERL_GET_THX
183# else
184# define dTHX pTHX = PERL_GET_THX
185# endif
c5be433b 186# define pTHX_ pTHX,
c5be433b 187# define aTHX_ aTHX,
4373e329 188# define pTHX_1 2
894356b3
GS
189# define pTHX_2 3
190# define pTHX_3 4
191# define pTHX_4 5
4373e329
AL
192# define pTHX_5 6
193# define pTHX_6 7
a3b680e6
AL
194# define pTHX_7 8
195# define pTHX_8 9
196# define pTHX_9 10
a6fc70e5 197# define pTHX_12 13
e8dda941
JD
198# if defined(DEBUGGING) && !defined(PERL_TRACK_MEMPOOL)
199# define PERL_TRACK_MEMPOOL
200# endif
201#else
202# undef PERL_TRACK_MEMPOOL
c5be433b
GS
203#endif
204
76e3520e 205#define STATIC static
16c91539
BM
206
207#ifndef PERL_CORE
208/* Do not use these macros. They were part of PERL_OBJECT, which was an
209 * implementation of multiplicity using C++ objects. They have been left
210 * here solely for the sake of XS code which has incorrectly
211 * cargo-culted them.
212 */
76e3520e 213#define CPERLscope(x) x
565764a8 214#define CPERLarg void
76e3520e 215#define CPERLarg_
e3b8966e 216#define _CPERLarg
1d583055
GS
217#define PERL_OBJECT_THIS
218#define _PERL_OBJECT_THIS
219#define PERL_OBJECT_THIS_
312caa8e 220#define CALL_FPTR(fptr) (*fptr)
ef69c8fc 221#define MEMBER_TO_FPTR(name) name
16c91539 222#endif /* !PERL_CORE */
76e3520e 223
16c91539 224#define CALLRUNOPS PL_runops
f9f4320a 225
3ab4a224 226#define CALLREGCOMP(sv, flags) Perl_pregcomp(aTHX_ (sv),(flags))
f9f4320a 227
16c91539 228#define CALLREGCOMP_ENG(prog, sv, flags) (prog)->comp(aTHX_ sv, flags)
a340edde 229#define CALLREGEXEC(prog,stringarg,strend,strbeg,minend,sv,data,flags) \
16c91539 230 RX_ENGINE(prog)->exec(aTHX_ (prog),(stringarg),(strend), \
a340edde 231 (strbeg),(minend),(sv),(data),(flags))
52a21eb3
DM
232#define CALLREG_INTUIT_START(prog,sv,strbeg,strpos,strend,flags,data) \
233 RX_ENGINE(prog)->intuit(aTHX_ (prog), (sv), (strbeg), (strpos), \
f9f4320a
YO
234 (strend),(flags),(data))
235#define CALLREG_INTUIT_STRING(prog) \
16c91539 236 RX_ENGINE(prog)->checkstr(aTHX_ (prog))
f8149455 237
f8149455
YO
238#define CALLREGFREE(prog) \
239 Perl_pregfree(aTHX_ (prog))
240
241#define CALLREGFREE_PVT(prog) \
fc6bde6f 242 if(prog) RX_ENGINE(prog)->rxfree(aTHX_ (prog))
f8149455 243
2fdbfb4d 244#define CALLREG_NUMBUF_FETCH(rx,paren,usesv) \
16c91539 245 RX_ENGINE(rx)->numbered_buff_FETCH(aTHX_ (rx),(paren),(usesv))
93b32b6d 246
2fdbfb4d 247#define CALLREG_NUMBUF_STORE(rx,paren,value) \
16c91539 248 RX_ENGINE(rx)->numbered_buff_STORE(aTHX_ (rx),(paren),(value))
2fdbfb4d
AB
249
250#define CALLREG_NUMBUF_LENGTH(rx,sv,paren) \
16c91539 251 RX_ENGINE(rx)->numbered_buff_LENGTH(aTHX_ (rx),(sv),(paren))
2fdbfb4d 252
192b9cd1 253#define CALLREG_NAMED_BUFF_FETCH(rx, key, flags) \
16c91539 254 RX_ENGINE(rx)->named_buff(aTHX_ (rx), (key), NULL, ((flags) | RXapif_FETCH))
192b9cd1
AB
255
256#define CALLREG_NAMED_BUFF_STORE(rx, key, value, flags) \
16c91539 257 RX_ENGINE(rx)->named_buff(aTHX_ (rx), (key), (value), ((flags) | RXapif_STORE))
192b9cd1
AB
258
259#define CALLREG_NAMED_BUFF_DELETE(rx, key, flags) \
16c91539 260 RX_ENGINE(rx)->named_buff(aTHX_ (rx),(key), NULL, ((flags) | RXapif_DELETE))
192b9cd1
AB
261
262#define CALLREG_NAMED_BUFF_CLEAR(rx, flags) \
16c91539 263 RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, ((flags) | RXapif_CLEAR))
192b9cd1
AB
264
265#define CALLREG_NAMED_BUFF_EXISTS(rx, key, flags) \
16c91539 266 RX_ENGINE(rx)->named_buff(aTHX_ (rx), (key), NULL, ((flags) | RXapif_EXISTS))
192b9cd1
AB
267
268#define CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags) \
16c91539 269 RX_ENGINE(rx)->named_buff_iter(aTHX_ (rx), NULL, ((flags) | RXapif_FIRSTKEY))
192b9cd1
AB
270
271#define CALLREG_NAMED_BUFF_NEXTKEY(rx, lastkey, flags) \
16c91539 272 RX_ENGINE(rx)->named_buff_iter(aTHX_ (rx), (lastkey), ((flags) | RXapif_NEXTKEY))
192b9cd1
AB
273
274#define CALLREG_NAMED_BUFF_SCALAR(rx, flags) \
16c91539 275 RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, ((flags) | RXapif_SCALAR))
192b9cd1
AB
276
277#define CALLREG_NAMED_BUFF_COUNT(rx) \
16c91539 278 RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, RXapif_REGNAMES_COUNT)
192b9cd1
AB
279
280#define CALLREG_NAMED_BUFF_ALL(rx, flags) \
16c91539 281 RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, flags)
93b32b6d 282
49d7dfbc 283#define CALLREG_PACKAGE(rx) \
16c91539 284 RX_ENGINE(rx)->qr_package(aTHX_ (rx))
93b32b6d 285
00f254e2 286#if defined(USE_ITHREADS)
f9f4320a 287#define CALLREGDUPE(prog,param) \
f8149455
YO
288 Perl_re_dup(aTHX_ (prog),(param))
289
290#define CALLREGDUPE_PVT(prog,param) \
16c91539 291 (prog ? RX_ENGINE(prog)->dupe(aTHX_ (prog),(param)) \
00f254e2 292 : (REGEXP *)NULL)
f9f4320a 293#endif
14dd3ad8 294
a20207d7 295
bcdf7404 296
a20207d7
YO
297
298
5ba4cab2
SH
299/*
300 * Because of backward compatibility reasons the PERL_UNUSED_DECL
301 * cannot be changed from postfix to PERL_UNUSED_DECL(x). Sigh.
302 *
303 * Note that there are C compilers such as MetroWerks CodeWarrior
1266ad8c 304 * which do not have an "inlined" way (like the gcc __attribute__) of
5ba4cab2
SH
305 * marking unused variables (they need e.g. a #pragma) and therefore
306 * cpp macros like PERL_UNUSED_DECL cannot work for this purpose, even
307 * if it were PERL_UNUSED_DECL(x), which it cannot be (see above).
308 *
309 */
1266ad8c 310
9969cdde 311#if defined(__SYMBIAN32__) && defined(__GNUC__)
5b2bd0a5 312# ifdef __cplusplus
5ba4cab2 313# define PERL_UNUSED_DECL
5b2bd0a5 314# else
5ba4cab2 315# define PERL_UNUSED_DECL __attribute__((unused))
5b2bd0a5
AMS
316# endif
317#endif
44dbb695 318
5b2bd0a5 319#ifndef PERL_UNUSED_DECL
f7a9299a 320# if defined(HASATTRIBUTE_UNUSED) && (!defined(__cplusplus) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
5ba4cab2 321# define PERL_UNUSED_DECL __attribute__unused__
b79c357a 322# else
5ba4cab2 323# define PERL_UNUSED_DECL
b79c357a 324# endif
5b2bd0a5 325#endif
00f254e2 326
349b520e
DM
327/* gcc -Wall:
328 * for silencing unused variables that are actually used most of the time,
a730e3f2
JH
329 * but we cannot quite get rid of, such as "ax" in PPCODE+noargs xsubs,
330 * or variables/arguments that are used only in certain configurations.
349b520e 331 */
53c1dcc0 332#ifndef PERL_UNUSED_ARG
c707756e 333# define PERL_UNUSED_ARG(x) ((void)sizeof(x))
ad73156c 334#endif
53c1dcc0 335#ifndef PERL_UNUSED_VAR
a730e3f2 336# define PERL_UNUSED_VAR(x) ((void)sizeof(x))
53c1dcc0 337#endif
ad73156c 338
23491f1d 339#if defined(USE_ITHREADS) || defined(PERL_GLOBAL_STRUCT)
96a5add6
AL
340# define PERL_UNUSED_CONTEXT PERL_UNUSED_ARG(my_perl)
341#else
342# define PERL_UNUSED_CONTEXT
343#endif
344
24e7ff4e
JH
345/* gcc (-ansi) -pedantic doesn't allow gcc statement expressions,
346 * g++ allows them but seems to have problems with them
347 * (insane errors ensue).
348 * g++ does not give insane errors now (RMB 2008-01-30, gcc 4.2.2).
349 */
350#if defined(PERL_GCC_PEDANTIC) || \
351 (defined(__GNUC__) && defined(__cplusplus) && \
352 ((__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 2))))
353# ifndef PERL_GCC_BRACE_GROUPS_FORBIDDEN
354# define PERL_GCC_BRACE_GROUPS_FORBIDDEN
355# endif
356#endif
357
ffea512f 358/* Use PERL_UNUSED_RESULT() to suppress the warnings about unused results
b469f1e0
JH
359 * of function calls, e.g. PERL_UNUSED_RESULT(foo(a, b)).
360 *
361 * The main reason for this is that the combination of gcc -Wunused-result
362 * (part of -Wall) and the __attribute__((warn_unused_result)) cannot
363 * be silenced with casting to void. This causes trouble when the system
364 * header files use the attribute.
365 *
366 * Use PERL_UNUSED_RESULT sparingly, though, since usually the warning
367 * is there for a good reason: you might lose success/failure information,
368 * or leak resources, or changes in resources.
ffea512f
JH
369 *
370 * But sometimes you just want to ignore the return value, e.g. on
b469f1e0
JH
371 * codepaths soon ending up in abort, or in "best effort" attempts,
372 * or in situations where there is no good way to handle failures.
ffea512f 373 *
b469f1e0
JH
374 * Sometimes PERL_UNUSED_RESULT might not be the most natural way:
375 * another possibility is that you can capture the return value
376 * and use PERL_UNUSED_VAR on that.
ffea512f 377 *
b469f1e0
JH
378 * The __typeof__() is used instead of typeof() since typeof() is not
379 * available under strict C89, and because of compilers masquerading
380 * as gcc (clang and icc), we want exactly the gcc extension
381 * __typeof__ and nothing else.
ffea512f
JH
382 */
383#ifndef PERL_UNUSED_RESULT
b469f1e0
JH
384# if defined(__GNUC__) && defined(HASATTRIBUTE_WARN_UNUSED_RESULT)
385# define PERL_UNUSED_RESULT(v) STMT_START { __typeof__(v) z = (v); (void)sizeof(z); } STMT_END
ffea512f
JH
386# else
387# define PERL_UNUSED_RESULT(v) ((void)(v))
388# endif
389#endif
390
b56aac20
DM
391/* on gcc (and clang), specify that a warning should be temporarily
392 * ignored; e.g.
393 *
394 * GCC_DIAG_IGNORE(-Wmultichar);
395 * char b = 'ab';
396 * GCC_DIAG_RESTORE;
397 *
398 * based on http://dbp-consulting.com/tutorials/SuppressingGCCWarnings.html
399 *
400 * Note that "pragma GCC diagnostic push/pop" was added in GCC 4.6, Mar 2011;
401 * clang only pretends to be GCC 4.2, but still supports push/pop.
c1d6452f
JH
402 *
403 * Note on usage: on non-gcc (or lookalike, like clang) compilers
95b872d5
LM
404 * one cannot use these with a semicolon at file (global) level without
405 * warnings since they are defined as empty, which leads into the terminating
c1d6452f
JH
406 * semicolon being left alone on a line:
407 * ;
408 * which makes compilers mildly cranky. Therefore at file level one
95b872d5 409 * should use the GCC_DIAG_IGNORE and GCC_DIAG_RESTORE macros *without*
6ab56f1e 410 * the semicolons.
c1d6452f
JH
411 *
412 * (A dead-on-arrival solution would be to try to define the macros as
413 * NOOP or dNOOP, those don't work both inside functions and outside.)
b56aac20
DM
414 */
415
57d4b8c5 416#if defined(__clang__) || defined(__clang) || \
d28ce8b4 417 (defined( __GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406)
c1d6452f
JH
418# define GCC_DIAG_PRAGMA(x) _Pragma (#x)
419/* clang has "clang diagnostic" pragmas, but also understands gcc. */
b56aac20 420# define GCC_DIAG_IGNORE(x) _Pragma("GCC diagnostic push") \
c1d6452f 421 GCC_DIAG_PRAGMA(GCC diagnostic ignored #x)
b56aac20
DM
422# define GCC_DIAG_RESTORE _Pragma("GCC diagnostic pop")
423#else
8ce6f80a
JH
424# define GCC_DIAG_IGNORE(w)
425# define GCC_DIAG_RESTORE
b56aac20 426#endif
337458b1
JH
427/* for clang specific pragmas */
428#if defined(__clang__) || defined(__clang)
429# define CLANG_DIAG_PRAGMA(x) _Pragma (#x)
430# define CLANG_DIAG_IGNORE(x) _Pragma("clang diagnostic push") \
431 CLANG_DIAG_PRAGMA(clang diagnostic ignored #x)
432# define CLANG_DIAG_RESTORE _Pragma("clang diagnostic pop")
433#else
434# define CLANG_DIAG_IGNORE(w)
435# define CLANG_DIAG_RESTORE
436#endif
b56aac20 437
6f207bd3 438#define NOOP /*EMPTY*/(void)0
bafdc25d
NC
439/* cea2e8a9dd23747f accidentally lost the comment originally from the first
440 check in of thread.h, explaining why we need dNOOP at all: */
441/* Rats: if dTHR is just blank then the subsequent ";" throws an error */
442/* Declaring a *function*, instead of a variable, ensures that we don't rely
443 on being able to suppress "unused" warnings. */
7db66e12
CB
444#ifdef __cplusplus
445#define dNOOP (void)0
446#else
64a8239e 447#define dNOOP extern int Perl___notused(void)
7db66e12 448#endif
71be2cbc 449
0cb96387 450#ifndef pTHX
a78adc84 451/* Don't bother defining tTHX ; using it outside
e8dda941
JD
452 * code guarded by PERL_IMPLICIT_CONTEXT is an error.
453 */
0cb96387
GS
454# define pTHX void
455# define pTHX_
0cb96387
GS
456# define aTHX
457# define aTHX_
9399a70c 458# define aTHXa(a) NOOP
0cb96387
GS
459# define dTHXa(a) dNOOP
460# define dTHX dNOOP
894356b3
GS
461# define pTHX_1 1
462# define pTHX_2 2
463# define pTHX_3 3
464# define pTHX_4 4
3d42dc86
RGS
465# define pTHX_5 5
466# define pTHX_6 6
a3b680e6
AL
467# define pTHX_7 7
468# define pTHX_8 8
469# define pTHX_9 9
a6fc70e5 470# define pTHX_12 12
0cb96387
GS
471#endif
472
27da23d5
JH
473#ifndef dVAR
474# define dVAR dNOOP
475#endif
476
b4f7f263
GS
477/* these are only defined for compatibility; should not be used internally */
478#if !defined(pTHXo) && !defined(PERL_CORE)
0cb96387
GS
479# define pTHXo pTHX
480# define pTHXo_ pTHX_
0cb96387
GS
481# define aTHXo aTHX
482# define aTHXo_ aTHX_
c5be433b 483# define dTHXo dTHX
71d280e3 484# define dTHXoa(x) dTHXa(x)
0cb96387
GS
485#endif
486
487#ifndef pTHXx
5aaab254 488# define pTHXx PerlInterpreter *my_perl
0cb96387 489# define pTHXx_ pTHXx,
0cb96387
GS
490# define aTHXx my_perl
491# define aTHXx_ aTHXx,
c5be433b 492# define dTHXx dTHX
22c35a8c 493#endif
71be2cbc 494
1de7c2ac
GS
495/* Under PERL_IMPLICIT_SYS (used in Windows for fork emulation)
496 * PerlIO_foo() expands to PL_StdIO->pFOO(PL_StdIO, ...).
497 * dTHXs is therefore needed for all functions using PerlIO_foo(). */
498#ifdef PERL_IMPLICIT_SYS
27da23d5
JH
499# ifdef PERL_GLOBAL_STRUCT_PRIVATE
500# define dTHXs dVAR; dTHX
501# else
502# define dTHXs dTHX
503# endif
1de7c2ac 504#else
27da23d5
JH
505# ifdef PERL_GLOBAL_STRUCT_PRIVATE
506# define dTHXs dVAR
507# else
508# define dTHXs dNOOP
509# endif
1de7c2ac
GS
510#endif
511
5b692037
JH
512#if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN) && !defined(__cplusplus)
513# ifndef PERL_USE_GCC_BRACE_GROUPS
514# define PERL_USE_GCC_BRACE_GROUPS
515# endif
516#endif
517
728e2803
PP
518/*
519 * STMT_START { statements; } STMT_END;
520 * can be used as a single statement, as in
521 * if (x) STMT_START { ... } STMT_END; else ...
522 *
523 * Trying to select a version that gives no warnings...
524 */
525#if !(defined(STMT_START) && defined(STMT_END))
5b692037 526# ifdef PERL_USE_GCC_BRACE_GROUPS
a0288114 527# define STMT_START (void)( /* gcc supports "({ STATEMENTS; })" */
728e2803
PP
528# define STMT_END )
529# else
728e2803
PP
530# define STMT_START do
531# define STMT_END while (0)
728e2803
PP
532# endif
533#endif
534
d6f9c181
JH
535#ifndef BYTEORDER /* Should never happen -- byteorder is in config.h */
536# define BYTEORDER 0x1234
79072805
LW
537#endif
538
539/* Overall memory policy? */
540#ifndef CONSERVATIVE
541# define LIBERAL 1
542#endif
543
8ada0baa
JH
544#if 'A' == 65 && 'I' == 73 && 'J' == 74 && 'Z' == 90
545#define ASCIIish
546#else
547#undef ASCIIish
548#endif
549
79072805
LW
550/*
551 * The following contortions are brought to you on behalf of all the
552 * standards, semi-standards, de facto standards, not-so-de-facto standards
553 * of the world, as well as all the other botches anyone ever thought of.
554 * The basic theory is that if we work hard enough here, the rest of the
555 * code can be a lot prettier. Well, so much for theory. Sorry, Henry...
556 */
ac58e20f 557
ee0007ab 558/* define this once if either system, instead of cluttering up the src */
7c458fae 559#if defined(MSDOS) || defined(WIN32) || defined(NETWARE)
ee0007ab
LW
560#define DOSISH 1
561#endif
562
3d97541c 563/* These exist only for back-compat with XS modules. */
8162b70e 564#ifndef PERL_CORE
516e10a9 565#define VOL volatile
3d97541c 566#define CAN_PROTOTYPE
8162b70e 567#endif
663a0e37 568
284167a5
SM
569/* By compiling a perl with -DNO_TAINT_SUPPORT or -DSILENT_NO_TAINT_SUPPORT,
570 * you get a perl without taint support, but doubtlessly with a lesser
571 * degree of support. Do not do so unless you know exactly what it means
572 * technically, have a good reason to do so, and know exactly how the
573 * perl will be used. perls with -DSILENT_NO_TAINT_SUPPORT are considered
574 * a potential security risk due to flat out ignoring the security-relevant
575 * taint flags. This being said, a perl without taint support compiled in
576 * has marginal run-time performance benefits.
577 * SILENT_NO_TAINT_SUPPORT implies NO_TAINT_SUPPORT.
578 * SILENT_NO_TAINT_SUPPORT is the same as NO_TAINT_SUPPORT except it
579 * silently ignores -t/-T instead of throwing an exception.
053a3618
SM
580 *
581 * DANGER! Using NO_TAINT_SUPPORT or SILENT_NO_TAINT_SUPPORT
6ecb40c7 582 * voids your nonexistent warranty!
284167a5 583 */
dc6d7f5c 584#if defined(SILENT_NO_TAINT_SUPPORT) && !defined(NO_TAINT_SUPPORT)
284167a5
SM
585# define NO_TAINT_SUPPORT 1
586#endif
587
588/* NO_TAINT_SUPPORT can be set to transform virtually all taint-related
589 * operations into no-ops for a very modest speed-up. Enable only if you
590 * know what you're doing: tests and CPAN modules' tests are bound to fail.
591 */
dc6d7f5c 592#ifdef NO_TAINT_SUPPORT
284167a5
SM
593# define TAINT NOOP
594# define TAINT_NOT NOOP
595# define TAINT_IF(c) NOOP
596# define TAINT_ENV() NOOP
597# define TAINT_PROPER(s) NOOP
598# define TAINT_set(s) NOOP
599# define TAINT_get 0
600# define TAINTING_get 0
601# define TAINTING_set(s) NOOP
602# define TAINT_WARN_get 0
603# define TAINT_WARN_set(s) NOOP
604#else
d48c660d 605# define TAINT (PL_tainted = PL_tainting)
284167a5 606# define TAINT_NOT (PL_tainted = FALSE)
d48c660d 607# define TAINT_IF(c) if (UNLIKELY(c)) { PL_tainted = PL_tainting; }
2439e033
SM
608# define TAINT_ENV() if (UNLIKELY(PL_tainting)) { taint_env(); }
609# define TAINT_PROPER(s) if (UNLIKELY(PL_tainting)) { taint_proper(NULL, s); }
284167a5
SM
610# define TAINT_set(s) (PL_tainted = (s))
611# define TAINT_get (PL_tainted)
612# define TAINTING_get (PL_tainting)
613# define TAINTING_set(s) (PL_tainting = (s))
614# define TAINT_WARN_get (PL_taint_warn)
615# define TAINT_WARN_set(s) (PL_taint_warn = (s))
616#endif
a687059c 617
20be6587
DM
618/* flags used internally only within pp_subst and pp_substcont */
619#ifdef PERL_CORE
620# define SUBST_TAINT_STR 1 /* string tainted */
621# define SUBST_TAINT_PAT 2 /* pattern tainted */
622# define SUBST_TAINT_REPL 4 /* replacement tainted */
623# define SUBST_TAINT_RETAINT 8 /* use re'taint' in scope */
624# define SUBST_TAINT_BOOLRET 16 /* return is boolean (don't taint) */
625#endif
626
d460ef45 627/* XXX All process group stuff is handled in pp_sys.c. Should these
a6e633de
PP
628 defines move there? If so, I could simplify this a lot. --AD 9/96.
629*/
630/* Process group stuff changed from traditional BSD to POSIX.
631 perlfunc.pod documents the traditional BSD-style syntax, so we'll
632 try to preserve that, if possible.
633*/
634#ifdef HAS_SETPGID
635# define BSD_SETPGRP(pid, pgrp) setpgid((pid), (pgrp))
da5fdda8
AC
636#elif defined(HAS_SETPGRP) && defined(USE_BSD_SETPGRP)
637# define BSD_SETPGRP(pid, pgrp) setpgrp((pid), (pgrp))
638#elif defined(HAS_SETPGRP2)
639# define BSD_SETPGRP(pid, pgrp) setpgrp2((pid), (pgrp))
a6e633de
PP
640#endif
641#if defined(BSD_SETPGRP) && !defined(HAS_SETPGRP)
642# define HAS_SETPGRP /* Well, effectively it does . . . */
643#endif
644
645/* getpgid isn't POSIX, but at least Solaris and Linux have it, and it makes
646 our life easier :-) so we'll try it.
647*/
648#ifdef HAS_GETPGID
649# define BSD_GETPGRP(pid) getpgid((pid))
da5fdda8
AC
650#elif defined(HAS_GETPGRP) && defined(USE_BSD_GETPGRP)
651# define BSD_GETPGRP(pid) getpgrp((pid))
652#elif defined(HAS_GETPGRP2)
653# define BSD_GETPGRP(pid) getpgrp2((pid))
a6e633de
PP
654#endif
655#if defined(BSD_GETPGRP) && !defined(HAS_GETPGRP)
656# define HAS_GETPGRP /* Well, effectively it does . . . */
657#endif
658
d460ef45 659/* These are not exact synonyms, since setpgrp() and getpgrp() may
a6e633de
PP
660 have different behaviors, but perl.h used to define USE_BSDPGRP
661 (prior to 5.003_05) so some extension might depend on it.
662*/
663#if defined(USE_BSD_SETPGRP) || defined(USE_BSD_GETPGRP)
664# ifndef USE_BSDPGRP
665# define USE_BSDPGRP
666# endif
663a0e37
LW
667#endif
668
486ec47a 669/* HP-UX 10.X CMA (Common Multithreaded Architecture) insists that
8ac5a1fe
MH
670 pthread.h must be included before all other header files.
671*/
3db8f154 672#if defined(USE_ITHREADS) && defined(PTHREAD_H_FIRST) && defined(I_PTHREAD)
8ac5a1fe
MH
673# include <pthread.h>
674#endif
675
58ab9e8b 676#include <sys/types.h>
663a0e37 677
c7925a5e
DD
678/* EVC 4 SDK headers includes a bad definition of MB_CUR_MAX in stdlib.h
679 which is included from stdarg.h. Bad definition not present in SD 2008
680 SDK headers. wince.h is not yet included, so we cant fix this from there
681 since by then MB_CUR_MAX will be defined from stdlib.h.
682 cewchar.h includes a correct definition of MB_CUR_MAX and it is copied here
683 since cewchar.h can't be included this early */
684#if defined(UNDER_CE) && (_MSC_VER < 1300)
685# define MB_CUR_MAX 1
686#endif
9d82a2b7
AC
687
688# include <stdarg.h>
760ac839 689
27db2737
JH
690#ifdef I_STDINT
691# include <stdint.h>
692#endif
693
fe14fcc3 694#include <ctype.h>
63b481b9
AC
695#include <float.h>
696#include <limits.h>
a0d0e21e
LW
697
698#ifdef METHOD /* Defined by OSF/1 v3.0 by ctype.h */
699#undef METHOD
a0d0e21e
LW
700#endif
701
12ae5dfc
JH
702#ifdef PERL_MICRO
703# define NO_LOCALE
704#endif
705
4633a7c4 706#ifdef I_LOCALE
36477c24 707# include <locale.h>
4633a7c4
LW
708#endif
709
6ebbc862
KW
710#ifdef I_XLOCALE
711# include <xlocale.h>
712#endif
713
36477c24
PP
714#if !defined(NO_LOCALE) && defined(HAS_SETLOCALE)
715# define USE_LOCALE
ccd65d51
KW
716# define HAS_SKIP_LOCALE_INIT /* Solely for XS code to test for this
717 capability */
36477c24
PP
718# if !defined(NO_LOCALE_COLLATE) && defined(LC_COLLATE) \
719 && defined(HAS_STRXFRM)
720# define USE_LOCALE_COLLATE
721# endif
722# if !defined(NO_LOCALE_CTYPE) && defined(LC_CTYPE)
723# define USE_LOCALE_CTYPE
724# endif
725# if !defined(NO_LOCALE_NUMERIC) && defined(LC_NUMERIC)
726# define USE_LOCALE_NUMERIC
727# endif
fa9b773e
KW
728# if !defined(NO_LOCALE_MESSAGES) && defined(LC_MESSAGES)
729# define USE_LOCALE_MESSAGES
730# endif
731# if !defined(NO_LOCALE_MONETARY) && defined(LC_MONETARY)
732# define USE_LOCALE_MONETARY
733# endif
56e45410
KW
734# if !defined(NO_LOCALE_TIME) && defined(LC_TIME)
735# define USE_LOCALE_TIME
736# endif
36477c24 737#endif /* !NO_LOCALE && HAS_SETLOCALE */
a0d0e21e 738
fe14fcc3 739#include <setjmp.h>
79072805 740
a0d0e21e 741#ifdef I_SYS_PARAM
79072805
LW
742# ifdef PARAM_NEEDS_TYPES
743# include <sys/types.h>
744# endif
745# include <sys/param.h>
352d5a3a 746#endif
79072805 747
ce3470dc
AD
748/* On BSD-derived systems, <sys/param.h> defines BSD to a year-month
749 value something like 199306. This may be useful if no more-specific
750 feature test is available.
751*/
752#if defined(BSD)
753# ifndef BSDish
754# define BSDish
755# endif
756#endif
757
dd512de3
AC
758/* Use all the "standard" definitions */
759#include <stdlib.h>
03a14243 760
3f270f98
JH
761/* If this causes problems, set i_unistd=undef in the hint file. */
762#ifdef I_UNISTD
763ee752
AB
763# if defined(__amigaos4__)
764# ifdef I_NETINET_IN
765# include <netinet/in.h>
766# endif
767# endif
3f270f98 768# include <unistd.h>
6e3136a6
AB
769# if defined(__amigaos4__)
770/* Under AmigaOS 4 newlib.library provides an environ. However using
771 * it doesn't give us enough control over inheritance of variables by
772 * subshells etc. so replace with custom version based on abc-shell
773 * code. */
774extern char **myenviron;
775# undef environ
776# define environ myenviron
777# endif
3f270f98
JH
778#endif
779
de8ca8af
NT
780/* for WCOREDUMP */
781#ifdef I_SYS_WAIT
782# include <sys/wait.h>
783#endif
784
9969cdde 785#ifdef __SYMBIAN32__
27da23d5
JH
786# undef _SC_ARG_MAX /* Symbian has _SC_ARG_MAX but no sysconf() */
787#endif
788
21e89b5f 789#if defined(HAS_SYSCALL) && !defined(HAS_SYSCALL_PROTO)
c45598c5 790EXTERN_C int syscall(int, ...);
2ef53570
JH
791#endif
792
21e89b5f 793#if defined(HAS_USLEEP) && !defined(HAS_USLEEP_PROTO)
c45598c5 794EXTERN_C int usleep(unsigned int);
2ef53570
JH
795#endif
796
1109a392
MHM
797#ifdef PERL_CORE
798
799/* macros for correct constant construction */
800# if INTSIZE >= 2
801# define U16_CONST(x) ((U16)x##U)
802# else
803# define U16_CONST(x) ((U16)x##UL)
804# endif
805
806# if INTSIZE >= 4
807# define U32_CONST(x) ((U32)x##U)
808# else
809# define U32_CONST(x) ((U32)x##UL)
810# endif
811
812# ifdef HAS_QUAD
813# if INTSIZE >= 8
814# define U64_CONST(x) ((U64)x##U)
815# elif LONGSIZE >= 8
816# define U64_CONST(x) ((U64)x##UL)
817# elif QUADKIND == QUAD_IS_LONG_LONG
818# define U64_CONST(x) ((U64)x##ULL)
52865553
SH
819# elif QUADKIND == QUAD_IS___INT64
820# define U64_CONST(x) ((U64)x##UI64)
1109a392
MHM
821# else /* best guess we can make */
822# define U64_CONST(x) ((U64)x##UL)
823# endif
824# endif
825
826/* byte-swapping functions for big-/little-endian conversion */
827# define _swab_16_(x) ((U16)( \
828 (((U16)(x) & U16_CONST(0x00ff)) << 8) | \
829 (((U16)(x) & U16_CONST(0xff00)) >> 8) ))
830
831# define _swab_32_(x) ((U32)( \
832 (((U32)(x) & U32_CONST(0x000000ff)) << 24) | \
833 (((U32)(x) & U32_CONST(0x0000ff00)) << 8) | \
834 (((U32)(x) & U32_CONST(0x00ff0000)) >> 8) | \
835 (((U32)(x) & U32_CONST(0xff000000)) >> 24) ))
836
837# ifdef HAS_QUAD
838# define _swab_64_(x) ((U64)( \
839 (((U64)(x) & U64_CONST(0x00000000000000ff)) << 56) | \
840 (((U64)(x) & U64_CONST(0x000000000000ff00)) << 40) | \
841 (((U64)(x) & U64_CONST(0x0000000000ff0000)) << 24) | \
842 (((U64)(x) & U64_CONST(0x00000000ff000000)) << 8) | \
843 (((U64)(x) & U64_CONST(0x000000ff00000000)) >> 8) | \
844 (((U64)(x) & U64_CONST(0x0000ff0000000000)) >> 24) | \
845 (((U64)(x) & U64_CONST(0x00ff000000000000)) >> 40) | \
846 (((U64)(x) & U64_CONST(0xff00000000000000)) >> 56) ))
847# endif
848
9c17f24a
NC
849/* The old value was hard coded at 1008. (4096-16) seems to be a bit faster,
850 at least on FreeBSD. YMMV, so experiment. */
851#ifndef PERL_ARENA_SIZE
852#define PERL_ARENA_SIZE 4080
853#endif
854
ccb2c8b8 855/* Maximum level of recursion */
2b9dff67
RGS
856#ifndef PERL_SUB_DEPTH_WARN
857#define PERL_SUB_DEPTH_WARN 100
ccb2c8b8
RGS
858#endif
859
1109a392
MHM
860#endif /* PERL_CORE */
861
bdf3085f
NC
862/* We no longer default to creating a new SV for GvSV.
863 Do this before embed. */
864#ifndef PERL_CREATE_GVSV
7459f06b
NC
865# ifndef PERL_DONT_CREATE_GVSV
866# define PERL_DONT_CREATE_GVSV
867# endif
bdf3085f
NC
868#endif
869
ca0c25f6
NC
870#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
871#define PERL_USES_PL_PIDSTATUS
872#endif
873
739a0b84 874#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(__SYMBIAN32__)
a62746fa
RGS
875#define PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
876#endif
877
c31fac66
GS
878#define MEM_SIZE Size_t
879
1936d2a7
NC
880/* Round all values passed to malloc up, by default to a multiple of
881 sizeof(size_t)
882*/
883#ifndef PERL_STRLEN_ROUNDUP_QUANTUM
884#define PERL_STRLEN_ROUNDUP_QUANTUM Size_t_size
885#endif
886
f1200559
WH
887/* sv_grow() will expand strings by at least a certain percentage of
888 the previously *used* length to avoid excessive calls to realloc().
889 The default is 25% of the current length.
890*/
891#ifndef PERL_STRLEN_EXPAND_SHIFT
892# define PERL_STRLEN_EXPAND_SHIFT 2
893#endif
894
346712be
AC
895#include <stddef.h>
896#define STRUCT_OFFSET(s,m) offsetof(s,m)
d0b86e2f
BF
897
898/* ptrdiff_t is C11, so undef it under pedantic builds */
899#ifdef PERL_GCC_PEDANTIC
900# undef HAS_PTRDIFF_T
51371543
GS
901#endif
902
9969cdde 903#ifndef __SYMBIAN32__
27da23d5
JH
904# if defined(I_STRING) || defined(__cplusplus)
905# include <string.h>
906# else
907# include <strings.h>
908# endif
51371543
GS
909#endif
910
55497cff
PP
911/* This comes after <stdlib.h> so we don't try to change the standard
912 * library prototypes; we'll use our own in proto.h instead. */
03a14243 913
4633a7c4 914#ifdef MYMALLOC
86058a2d 915# ifdef PERL_POLLUTE_MALLOC
ee13e175 916# ifndef PERL_EXTMALLOC_DEF
86058a2d
GS
917# define Perl_malloc malloc
918# define Perl_calloc calloc
919# define Perl_realloc realloc
920# define Perl_mfree free
ee13e175 921# endif
86058a2d
GS
922# else
923# define EMBEDMYMALLOC /* for compatibility */
924# endif
827e134a 925
651b9576
GS
926# define safemalloc Perl_malloc
927# define safecalloc Perl_calloc
928# define saferealloc Perl_realloc
929# define safefree Perl_mfree
22f7c9c9
JH
930# define CHECK_MALLOC_TOO_LATE_FOR_(code) STMT_START { \
931 if (!PL_tainting && MallocCfg_ptr[MallocCfg_cfg_env_read]) \
932 code; \
933 } STMT_END
934# define CHECK_MALLOC_TOO_LATE_FOR(ch) \
935 CHECK_MALLOC_TOO_LATE_FOR_(MALLOC_TOO_LATE_FOR(ch))
936# define panic_write2(s) write(2, s, strlen(s))
937# define CHECK_MALLOC_TAINT(newval) \
938 CHECK_MALLOC_TOO_LATE_FOR_( \
939 if (newval) { \
acfd4d8e 940 PERL_UNUSED_RESULT(panic_write2("panic: tainting with $ENV{PERL_MALLOC_OPT}\n"));\
22f7c9c9 941 exit(1); })
22f7c9c9 942# define MALLOC_CHECK_TAINT(argc,argv,env) STMT_START { \
b0891165 943 if (doing_taint(argc,argv,env)) { \
22f7c9c9
JH
944 MallocCfg_ptr[MallocCfg_skip_cfg_env] = 1; \
945 }} STMT_END;
f2517201
GS
946#else /* MYMALLOC */
947# define safemalloc safesysmalloc
948# define safecalloc safesyscalloc
949# define saferealloc safesysrealloc
950# define safefree safesysfree
22f7c9c9
JH
951# define CHECK_MALLOC_TOO_LATE_FOR(ch) ((void)0)
952# define CHECK_MALLOC_TAINT(newval) ((void)0)
953# define MALLOC_CHECK_TAINT(argc,argv,env)
55497cff 954#endif /* MYMALLOC */
4633a7c4 955
fe13d51d 956/* diag_listed_as: "-T" is on the #! line, it must also be used on the command line */
27da23d5 957#define TOO_LATE_FOR_(ch,what) Perl_croak(aTHX_ "\"-%c\" is on the #! line, it must also be used on the command line%s", (char)(ch), what)
22f7c9c9
JH
958#define TOO_LATE_FOR(ch) TOO_LATE_FOR_(ch, "")
959#define MALLOC_TOO_LATE_FOR(ch) TOO_LATE_FOR_(ch, " with $ENV{PERL_MALLOC_OPT}")
960#define MALLOC_CHECK_TAINT2(argc,argv) MALLOC_CHECK_TAINT(argc,argv,NULL)
961
fc36a67e 962#ifndef memzero
04322328 963# define memzero(d,l) memset(d,0,l)
d9d8d8de 964#endif
378cc40b 965
ae986130 966#ifdef I_NETINET_IN
79072805 967# include <netinet/in.h>
ae986130
LW
968#endif
969
28e8609d
JH
970#ifdef I_ARPA_INET
971# include <arpa/inet.h>
972#endif
973
1aef975c 974#ifdef I_SYS_STAT
84902520 975# include <sys/stat.h>
1aef975c 976#endif
79072805 977
287a962e
JD
978/* Microsoft VC's sys/stat.h defines all S_Ixxx macros except S_IFIFO.
979 This definition should ideally go into win32/win32.h, but S_IFIFO is
980 used later here in perl.h before win32/win32.h is being included. */
981#if !defined(S_IFIFO) && defined(_S_IFIFO)
982# define S_IFIFO _S_IFIFO
983#endif
984
cc3315ba 985/* The stat macros for Unisoft System V/88 (and derivatives
a0d0e21e
LW
986 like UTekV) are broken, sometimes giving false positives. Undefine
987 them here and let the code below set them to proper values.
988
989 The ghs macro stands for GreenHills Software C-1.8.5 which
990 is the C compiler for sysV88 and the various derivatives.
991 This header file bug is corrected in gcc-2.5.8 and later versions.
992 --Kaveh Ghazi (ghazi@noc.rutgers.edu) 10/3/94. */
993
cc3315ba 994#if defined(m88k) && defined(ghs)
79072805
LW
995# undef S_ISDIR
996# undef S_ISCHR
997# undef S_ISBLK
998# undef S_ISREG
999# undef S_ISFIFO
1000# undef S_ISLNK
ee0007ab 1001#endif
135863df 1002
9245da2a 1003#include <time.h>
663a0e37 1004
fe14fcc3 1005#ifdef I_SYS_TIME
85e6fe83 1006# ifdef I_SYS_TIME_KERNEL
663a0e37
LW
1007# define KERNEL
1008# endif
1009# include <sys/time.h>
85e6fe83 1010# ifdef I_SYS_TIME_KERNEL
663a0e37
LW
1011# undef KERNEL
1012# endif
a687059c 1013#endif
135863df 1014
55497cff 1015#if defined(HAS_TIMES) && defined(I_SYS_TIMES)
85e6fe83 1016# include <sys/times.h>
d9d8d8de 1017#endif
8d063cd8 1018
663a0e37 1019#include <errno.h>
1e743fda 1020
acfe0abc 1021#if defined(WIN32) && defined(PERL_IMPLICIT_SYS)
b4748376
NIS
1022# define WIN32SCK_IS_STDSCK /* don't pull in custom wsock layer */
1023#endif
1024
da70cfc6 1025#if defined(HAS_SOCKET) && !defined(WIN32) /* WIN32 handles sockets via win32.h */
1e743fda
JH
1026# include <sys/socket.h>
1027# if defined(USE_SOCKS) && defined(I_SOCKS)
1028# if !defined(INCLUDE_PROTOTYPES)
1029# define INCLUDE_PROTOTYPES /* for <socks.h> */
1030# define PERL_SOCKS_NEED_PROTOTYPES
1031# endif
1032# include <socks.h>
1033# ifdef PERL_SOCKS_NEED_PROTOTYPES /* keep cpp space clean */
1034# undef INCLUDE_PROTOTYPES
1035# undef PERL_SOCKS_NEED_PROTOTYPES
ed6116ce 1036# endif
d460ef45 1037# endif
1e743fda 1038# ifdef I_NETDB
2986a63f
JH
1039# ifdef NETWARE
1040# include<stdio.h>
1041# endif
1e743fda
JH
1042# include <netdb.h>
1043# endif
1044# ifndef ENOTSOCK
1045# ifdef I_NET_ERRNO
1046# include <net/errno.h>
1047# endif
1048# endif
1049#endif
1050
fa0a29af 1051/* sockatmark() is so new (2001) that many places might have it hidden
29820b6d
MHM
1052 * behind some -D_BLAH_BLAH_SOURCE guard. The __THROW magic is required
1053 * e.g. in Gentoo, see http://bugs.gentoo.org/show_bug.cgi?id=12605 */
fa0a29af 1054#if defined(HAS_SOCKATMARK) && !defined(HAS_SOCKATMARK_PROTO)
29820b6d
MHM
1055# if defined(__THROW) && defined(__GLIBC__)
1056int sockatmark(int) __THROW;
1057# else
2ef53570 1058int sockatmark(int);
29820b6d 1059# endif
2ef53570
JH
1060#endif
1061
7e827271 1062#if defined(__osf__) && defined(__cplusplus) && !defined(_XOPEN_SOURCE_EXTENDED) /* Tru64 "cxx" (C++), see hints/dec_osf.sh for why the _XOPEN_SOURCE_EXTENDED cannot be defined. */
45d3b546
JH
1063EXTERN_C int fchdir(int);
1064EXTERN_C int flock(int, int);
1065EXTERN_C int fseeko(FILE *, off_t, int);
1066EXTERN_C off_t ftello(FILE *);
1067#endif
1068
7e827271 1069#if defined(__SUNPRO_CC) /* SUNWspro CC (C++) */
1ccb7c8d 1070EXTERN_C char *crypt(const char *, const char *);
7e827271 1071EXTERN_C char **environ;
1ccb7c8d
JH
1072#endif
1073
83a6ff2e 1074#if defined(__cplusplus)
ce3470dc 1075# if defined(BSDish)
b91fbb93 1076EXTERN_C char **environ;
83a6ff2e 1077# elif defined(__CYGWIN__)
667e2948
SP
1078EXTERN_C char *crypt(const char *, const char *);
1079#endif
83a6ff2e 1080#endif
667e2948 1081
1e743fda
JH
1082#ifdef SETERRNO
1083# undef SETERRNO /* SOCKS might have defined this */
ed6116ce 1084#endif
f86702cc
PP
1085
1086#ifdef VMS
1087# define SETERRNO(errcode,vmserrcode) \
1088 STMT_START { \
1089 set_errno(errcode); \
1090 set_vaxc_errno(vmserrcode); \
1091 } STMT_END
4ee39169
CS
1092# define dSAVEDERRNO int saved_errno; unsigned saved_vms_errno
1093# define dSAVE_ERRNO int saved_errno = errno; unsigned saved_vms_errno = vaxc$errno
1094# define SAVE_ERRNO ( saved_errno = errno, saved_vms_errno = vaxc$errno )
1095# define RESTORE_ERRNO SETERRNO(saved_errno, saved_vms_errno)
1096
93189314
JH
1097# define LIB_INVARG LIB$_INVARG
1098# define RMS_DIR RMS$_DIR
1099# define RMS_FAC RMS$_FAC
1100# define RMS_FEX RMS$_FEX
1101# define RMS_FNF RMS$_FNF
1102# define RMS_IFI RMS$_IFI
1103# define RMS_ISI RMS$_ISI
1104# define RMS_PRV RMS$_PRV
1105# define SS_ACCVIO SS$_ACCVIO
1106# define SS_DEVOFFLINE SS$_DEVOFFLINE
1107# define SS_IVCHAN SS$_IVCHAN
1108# define SS_NORMAL SS$_NORMAL
8a9f18be 1109# define SS_NOPRIV SS$_NOPRIV
4388f261 1110# define SS_BUFFEROVF SS$_BUFFEROVF
748a9306 1111#else
93189314
JH
1112# define LIB_INVARG 0
1113# define RMS_DIR 0
1114# define RMS_FAC 0
1115# define RMS_FEX 0
1116# define RMS_FNF 0
1117# define RMS_IFI 0
1118# define RMS_ISI 0
1119# define RMS_PRV 0
1120# define SS_ACCVIO 0
1121# define SS_DEVOFFLINE 0
1122# define SS_IVCHAN 0
1123# define SS_NORMAL 0
8a9f18be 1124# define SS_NOPRIV 0
4388f261 1125# define SS_BUFFEROVF 0
748a9306 1126#endif
ed6116ce 1127
6ca940a9
TC
1128#ifdef WIN32
1129# define dSAVEDERRNO int saved_errno; DWORD saved_win32_errno
1130# define dSAVE_ERRNO int saved_errno = errno; DWORD saved_win32_errno = GetLastError()
1131# define SAVE_ERRNO ( saved_errno = errno, saved_win32_errno = GetLastError() )
1132# define RESTORE_ERRNO ( errno = saved_errno, SetLastError(saved_win32_errno) )
1133#endif
1134
1135#ifdef OS2
1136# define dSAVEDERRNO int saved_errno; unsigned long saved_os2_errno
1137# define dSAVE_ERRNO int saved_errno = errno; unsigned long saved_os2_errno = Perl_rc
1138# define SAVE_ERRNO ( saved_errno = errno, saved_os2_errno = Perl_rc )
1139# define RESTORE_ERRNO ( errno = saved_errno, Perl_rc = saved_os2_errno )
1140#endif
1141
1142#ifndef SETERRNO
1143# define SETERRNO(errcode,vmserrcode) (errno = (errcode))
1144#endif
1145
1146#ifndef dSAVEDERRNO
1147# define dSAVEDERRNO int saved_errno
1148# define dSAVE_ERRNO int saved_errno = errno
1149# define SAVE_ERRNO (saved_errno = errno)
1150# define RESTORE_ERRNO (errno = saved_errno)
1151#endif
1152
f5fa9033 1153#define ERRSV GvSVn(PL_errgv)
dfd167e9 1154
b4f8d149 1155/* contains inlined gv_add_by_type */
dfd167e9 1156#define CLEAR_ERRSV() STMT_START { \
b4f8d149
DD
1157 SV ** const svp = &GvSV(PL_errgv); \
1158 if (!*svp) { \
a1b60c8d 1159 *svp = newSVpvs(""); \
b4f8d149
DD
1160 } else if (SvREADONLY(*svp)) { \
1161 SvREFCNT_dec_NN(*svp); \
b4f8d149 1162 *svp = newSVpvs(""); \
dfd167e9 1163 } else { \
b4f8d149 1164 SV *const errsv = *svp; \
a1b60c8d 1165 SvPVCLEAR(errsv); \
b4f8d149 1166 SvPOK_only(errsv); \
dfd167e9
NC
1167 if (SvMAGICAL(errsv)) { \
1168 mg_free(errsv); \
1169 } \
dfd167e9
NC
1170 } \
1171 } STMT_END
1172
1173
414bf5ae
MHM
1174#ifdef PERL_CORE
1175# define DEFSV (0 + GvSVn(PL_defgv))
55b5114f
FC
1176# define DEFSV_set(sv) \
1177 (SvREFCNT_dec(GvSV(PL_defgv)), GvSV(PL_defgv) = SvREFCNT_inc(sv))
1178# define SAVE_DEFSV \
1179 ( \
1180 save_gp(PL_defgv, 0), \
1181 GvINTRO_off(PL_defgv), \
1182 SAVEGENERICSV(GvSV(PL_defgv)), \
1183 GvSV(PL_defgv) = NULL \
1184 )
414bf5ae
MHM
1185#else
1186# define DEFSV GvSVn(PL_defgv)
55b5114f
FC
1187# define DEFSV_set(sv) (GvSV(PL_defgv) = (sv))
1188# define SAVE_DEFSV SAVESPTR(GvSV(PL_defgv))
414bf5ae 1189#endif
38a03e6e 1190
55497cff 1191#ifndef errno
5ff3f7a4
GS
1192 extern int errno; /* ANSI allows errno to be an lvalue expr.
1193 * For example in multithreaded environments
1194 * something like this might happen:
1195 * extern int *_errno(void);
1196 * #define errno (*_errno()) */
d9d8d8de 1197#endif
663a0e37 1198
7e996671
KW
1199#define UNKNOWN_ERRNO_MSG "(unknown)"
1200
b21d8587
AC
1201#if VMS
1202#define Strerror(e) strerror((e), vaxc$errno)
1203#else
1204#define Strerror(e) strerror(e)
35c8bce7 1205#endif
663a0e37 1206
2304df62 1207#ifdef I_SYS_IOCTL
79072805
LW
1208# ifndef _IOCTL_
1209# include <sys/ioctl.h>
1210# endif
a687059c
LW
1211#endif
1212
ee0007ab 1213#if defined(mc300) || defined(mc500) || defined(mc700) || defined(mc6000)
79072805
LW
1214# ifdef HAS_SOCKETPAIR
1215# undef HAS_SOCKETPAIR
1216# endif
2304df62
AD
1217# ifdef I_NDBM
1218# undef I_NDBM
79072805 1219# endif
a687059c
LW
1220#endif
1221
02fc2eee
NC
1222#ifndef HAS_SOCKETPAIR
1223# ifdef HAS_SOCKET
1224# define socketpair Perl_my_socketpair
1225# endif
1226#endif
1227
a687059c 1228#if INTSIZE == 2
79072805
LW
1229# define htoni htons
1230# define ntohi ntohs
a687059c 1231#else
79072805
LW
1232# define htoni htonl
1233# define ntohi ntohl
a687059c
LW
1234#endif
1235
a0d0e21e 1236/* Configure already sets Direntry_t */
35c8bce7 1237#if defined(I_DIRENT)
da5fdda8
AC
1238# include <dirent.h>
1239#elif defined(I_SYS_NDIR)
1240# include <sys/ndir.h>
1241#elif defined(I_SYS_DIR)
1242# include <sys/dir.h>
4633a7c4 1243#endif
a687059c 1244
c623bd54
LW
1245/*
1246 * The following gobbledygook brought to you on behalf of __STDC__.
1247 * (I could just use #ifndef __STDC__, but this is more bulletproof
1248 * in the face of half-implementations.)
1249 */
1250
21e89b5f 1251#if defined(I_SYSMODE)
ca6e1c26
JH
1252#include <sys/mode.h>
1253#endif
1254
c623bd54
LW
1255#ifndef S_IFMT
1256# ifdef _S_IFMT
1257# define S_IFMT _S_IFMT
1258# else
1259# define S_IFMT 0170000
1260# endif
1261#endif
1262
1263#ifndef S_ISDIR
1264# define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
1265#endif
1266
1267#ifndef S_ISCHR
1268# define S_ISCHR(m) ((m & S_IFMT) == S_IFCHR)
1269#endif
1270
1271#ifndef S_ISBLK
fe14fcc3
LW
1272# ifdef S_IFBLK
1273# define S_ISBLK(m) ((m & S_IFMT) == S_IFBLK)
1274# else
1275# define S_ISBLK(m) (0)
1276# endif
c623bd54
LW
1277#endif
1278
1279#ifndef S_ISREG
1280# define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
1281#endif
1282
1283#ifndef S_ISFIFO
fe14fcc3
LW
1284# ifdef S_IFIFO
1285# define S_ISFIFO(m) ((m & S_IFMT) == S_IFIFO)
1286# else
1287# define S_ISFIFO(m) (0)
1288# endif
c623bd54
LW
1289#endif
1290
1291#ifndef S_ISLNK
da5fdda8
AC
1292# ifdef _S_ISLNK
1293# define S_ISLNK(m) _S_ISLNK(m)
1294# elif defined(_S_IFLNK)
1295# define S_ISLNK(m) ((m & S_IFMT) == _S_IFLNK)
1296# elif defined(S_IFLNK)
1297# define S_ISLNK(m) ((m & S_IFMT) == S_IFLNK)
1298# else
1299# define S_ISLNK(m) (0)
1300# endif
c623bd54
LW
1301#endif
1302
1303#ifndef S_ISSOCK
da5fdda8
AC
1304# ifdef _S_ISSOCK
1305# define S_ISSOCK(m) _S_ISSOCK(m)
1306# elif defined(_S_IFSOCK)
1307# define S_ISSOCK(m) ((m & S_IFMT) == _S_IFSOCK)
1308# elif defined(S_IFSOCK)
1309# define S_ISSOCK(m) ((m & S_IFMT) == S_IFSOCK)
1310# else
1311# define S_ISSOCK(m) (0)
1312# endif
c623bd54
LW
1313#endif
1314
1315#ifndef S_IRUSR
1316# ifdef S_IREAD
1317# define S_IRUSR S_IREAD
1318# define S_IWUSR S_IWRITE
1319# define S_IXUSR S_IEXEC
1320# else
1321# define S_IRUSR 0400
1322# define S_IWUSR 0200
1323# define S_IXUSR 0100
1324# endif
fac7cdfc 1325#endif
1326
1327#ifndef S_IRGRP
1328# ifdef S_IRUSR
1329# define S_IRGRP (S_IRUSR>>3)
1330# define S_IWGRP (S_IWUSR>>3)
1331# define S_IXGRP (S_IXUSR>>3)
1332# else
1333# define S_IRGRP 0040
1334# define S_IWGRP 0020
1335# define S_IXGRP 0010
1336# endif
1337#endif
1338
1339#ifndef S_IROTH
1340# ifdef S_IRUSR
1341# define S_IROTH (S_IRUSR>>6)
1342# define S_IWOTH (S_IWUSR>>6)
1343# define S_IXOTH (S_IXUSR>>6)
1344# else
1345# define S_IROTH 0040
1346# define S_IWOTH 0020
1347# define S_IXOTH 0010
1348# endif
c623bd54
LW
1349#endif
1350
1351#ifndef S_ISUID
1352# define S_ISUID 04000
1353#endif
1354
1355#ifndef S_ISGID
1356# define S_ISGID 02000
1357#endif
1358
ca6e1c26
JH
1359#ifndef S_IRWXU
1360# define S_IRWXU (S_IRUSR|S_IWUSR|S_IXUSR)
d460ef45 1361#endif
ca6e1c26
JH
1362
1363#ifndef S_IRWXG
1364# define S_IRWXG (S_IRGRP|S_IWGRP|S_IXGRP)
d460ef45 1365#endif
ca6e1c26
JH
1366
1367#ifndef S_IRWXO
1368# define S_IRWXO (S_IROTH|S_IWOTH|S_IXOTH)
d460ef45 1369#endif
ca6e1c26 1370
b6c36746 1371/* Haiku R1 seems to define S_IREAD and S_IWRITE in <posix/fcntl.h>
c21fb2b8
JH
1372 * which would get included through <sys/file.h >, but that is 3000
1373 * lines in the future. --jhi */
1374
b6c36746 1375#if !defined(S_IREAD) && !defined(__HAIKU__)
ca6e1c26
JH
1376# define S_IREAD S_IRUSR
1377#endif
1378
b6c36746 1379#if !defined(S_IWRITE) && !defined(__HAIKU__)
ca6e1c26
JH
1380# define S_IWRITE S_IWUSR
1381#endif
1382
1383#ifndef S_IEXEC
1384# define S_IEXEC S_IXUSR
1385#endif
1386
a0d0e21e 1387#if defined(cray) || defined(gould) || defined(i860) || defined(pyr)
45d8adaa
LW
1388# define SLOPPYDIVIDE
1389#endif
1390
748a9306
LW
1391#ifdef UV
1392#undef UV
1393#endif
1394
f1519f70
AC
1395/* This used to be conditionally defined based on whether we had a sprintf()
1396 * that correctly returns the string length (as required by C89), but we no
1397 * longer need that. XS modules can (and do) use this name, so it must remain
1398 * a part of the API that's visible to modules. But we no longer document it
1399 * either (because using sprintf() rather than snprintf() is almost always
1400 * a bad idea). */
1401#define my_sprintf sprintf
ce582cee 1402
5b692037
JH
1403/*
1404 * If we have v?snprintf() and the C99 variadic macros, we can just
1405 * use just the v?snprintf(). It is nice to try to trap the buffer
1406 * overflow, however, so if we are DEBUGGING, and we cannot use the
e5afc1ae
DD
1407 * gcc statement expressions, then use the function wrappers which try
1408 * to trap the overflow. If we can use the gcc statement expressions,
1409 * we can try that even with the version that uses the C99 variadic
1410 * macros.
5b692037
JH
1411 */
1412
1208b3dd 1413/* Note that we do not check against snprintf()/vsnprintf() returning
13d66b05
AC
1414 * negative values because that is non-standard behaviour and we now
1415 * assume a working C89 implementation. */
1208b3dd 1416
571ee10c 1417#define PERL_SNPRINTF_CHECK(len, max, api) STMT_START { if ((max) > 0 && (Size_t)len > (max)) Perl_croak_nocontext("panic: %s buffer overflow", STRINGIFY(api)); } STMT_END
e8549682 1418
a4eca1d4
JH
1419#ifdef USE_QUADMATH
1420# define my_snprintf Perl_my_snprintf
1421# define PERL_MY_SNPRINTF_GUARDED
da5fdda8 1422#elif defined(HAS_C99_VARIADIC_MACROS) && !(defined(DEBUGGING) && !defined(PERL_USE_GCC_BRACE_GROUPS)) && !defined(PERL_GCC_PEDANTIC)
5b692037 1423# ifdef PERL_USE_GCC_BRACE_GROUPS
e8549682 1424# define my_snprintf(buffer, max, ...) ({ int len = snprintf(buffer, max, __VA_ARGS__); PERL_SNPRINTF_CHECK(len, max, snprintf); len; })
1208b3dd 1425# define PERL_MY_SNPRINTF_GUARDED
5b692037 1426# else
e8549682 1427# define my_snprintf(buffer, max, ...) snprintf(buffer, max, __VA_ARGS__)
5b692037
JH
1428# endif
1429#else
1430# define my_snprintf Perl_my_snprintf
1208b3dd 1431# define PERL_MY_SNPRINTF_GUARDED
5b692037
JH
1432#endif
1433
a4eca1d4
JH
1434/* There is no quadmath_vsnprintf, and therefore my_vsnprintf()
1435 * dies if called under USE_QUADMATH. */
13d66b05 1436#if defined(HAS_C99_VARIADIC_MACROS) && !(defined(DEBUGGING) && !defined(PERL_USE_GCC_BRACE_GROUPS)) && !defined(PERL_GCC_PEDANTIC)
5b692037 1437# ifdef PERL_USE_GCC_BRACE_GROUPS
e8549682 1438# define my_vsnprintf(buffer, max, ...) ({ int len = vsnprintf(buffer, max, __VA_ARGS__); PERL_SNPRINTF_CHECK(len, max, vsnprintf); len; })
1208b3dd 1439# define PERL_MY_VSNPRINTF_GUARDED
5b692037 1440# else
e8549682 1441# define my_vsnprintf(buffer, max, ...) vsnprintf(buffer, max, __VA_ARGS__)
5b692037
JH
1442# endif
1443#else
1444# define my_vsnprintf Perl_my_vsnprintf
1208b3dd 1445# define PERL_MY_VSNPRINTF_GUARDED
5b692037 1446#endif
d9fad198 1447
e8549682
JH
1448/* You will definitely need to use the PERL_MY_SNPRINTF_POST_GUARD()
1449 * or PERL_MY_VSNPRINTF_POST_GUARD() if you otherwise decide to ignore
1450 * the result of my_snprintf() or my_vsnprintf(). (No, you should not
1451 * completely ignore it: otherwise you cannot know whether your output
1452 * was too long.)
1453 *
1454 * int len = my_sprintf(buf, max, ...);
1455 * PERL_MY_SNPRINTF_POST_GUARD(len, max);
1456 *
1457 * The trick is that in certain platforms [a] the my_sprintf() already
1458 * contains the sanity check, while in certain platforms [b] it needs
1459 * to be done as a separate step. The POST_GUARD is that step-- in [a]
1460 * platforms the POST_GUARD actually does nothing since the check has
1461 * already been done. Watch out for the max being the same in both calls.
1462 *
1463 * If you actually use the snprintf/vsnprintf return value already,
1464 * you assumedly are checking its validity somehow. But you can
1465 * insert the POST_GUARD() also in that case. */
1466
1467#ifndef PERL_MY_SNPRINTF_GUARDED
1468# define PERL_MY_SNPRINTF_POST_GUARD(len, max) PERL_SNPRINTF_CHECK(len, max, snprintf)
1469#else
1470# define PERL_MY_SNPRINTF_POST_GUARD(len, max) PERL_UNUSED_VAR(len)
1471#endif
1472
1473#ifndef PERL_MY_VSNPRINTF_GUARDED
1474# define PERL_MY_VSNPRINTF_POST_GUARD(len, max) PERL_SNPRINTF_CHECK(len, max, vsnprintf)
1475#else
1476# define PERL_MY_VSNPRINTF_POST_GUARD(len, max) PERL_UNUSED_VAR(len)
1477#endif
1478
a6cc4119
SP
1479#ifdef HAS_STRLCAT
1480# define my_strlcat strlcat
1481#else
1482# define my_strlcat Perl_my_strlcat
1483#endif
1484
1485#ifdef HAS_STRLCPY
1486# define my_strlcpy strlcpy
1487#else
1488# define my_strlcpy Perl_my_strlcpy
1489#endif
1490
05f13f53 1491/*
27d4fb96
PP
1492 The IV type is supposed to be long enough to hold any integral
1493 value or a pointer.
1494 --Andy Dougherty August 1996
1495*/
1496
a22e52b9
JH
1497typedef IVTYPE IV;
1498typedef UVTYPE UV;
8175356b 1499
10cc9d2a 1500#if defined(USE_64_BIT_INT) && defined(HAS_QUAD)
6b8eaf93 1501# if QUADKIND == QUAD_IS_INT64_T && defined(INT64_MAX)
5ff3f7a4
GS
1502# define IV_MAX INT64_MAX
1503# define IV_MIN INT64_MIN
1504# define UV_MAX UINT64_MAX
cae7ae48
JH
1505# ifndef UINT64_MIN
1506# define UINT64_MIN 0
1507# endif
5ff3f7a4
GS
1508# define UV_MIN UINT64_MIN
1509# else
1510# define IV_MAX PERL_QUAD_MAX
1511# define IV_MIN PERL_QUAD_MIN
1512# define UV_MAX PERL_UQUAD_MAX
1513# define UV_MIN PERL_UQUAD_MIN
1514# endif
cf2093f6
JH
1515# define IV_IS_QUAD
1516# define UV_IS_QUAD
79072805 1517#else
8175356b 1518# if defined(INT32_MAX) && IVSIZE == 4
5ff3f7a4
GS
1519# define IV_MAX INT32_MAX
1520# define IV_MIN INT32_MIN
716026f9
DL
1521# ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
1522# define UV_MAX UINT32_MAX
1523# else
1524# define UV_MAX 4294967295U
1525# endif
cae7ae48
JH
1526# ifndef UINT32_MIN
1527# define UINT32_MIN 0
1528# endif
5ff3f7a4
GS
1529# define UV_MIN UINT32_MIN
1530# else
1531# define IV_MAX PERL_LONG_MAX
1532# define IV_MIN PERL_LONG_MIN
1533# define UV_MAX PERL_ULONG_MAX
1534# define UV_MIN PERL_ULONG_MIN
1535# endif
8175356b 1536# if IVSIZE == 8
cf2093f6
JH
1537# define IV_IS_QUAD
1538# define UV_IS_QUAD
de1c2614
JH
1539# ifndef HAS_QUAD
1540# define HAS_QUAD
1541# endif
cf2093f6
JH
1542# else
1543# undef IV_IS_QUAD
1544# undef UV_IS_QUAD
9ea40801 1545#if !defined(PERL_CORE) || defined(USING_MSVC6)
e25d460c
NC
1546/* We think that removing this decade-old undef this will cause too much
1547 breakage on CPAN for too little gain. (See RT #119753)
9ea40801
SH
1548 However, we do need HAS_QUAD in the core for use by the drand48 code,
1549 but not for Win32 VC6 because it has poor __int64 support. */
cb4b14d5 1550# undef HAS_QUAD
e25d460c 1551#endif
cf2093f6 1552# endif
79072805 1553#endif
d7d93a81 1554
6313e544
JH
1555#define Size_t_MAX (~(Size_t)0)
1556#define SSize_t_MAX (SSize_t)(~(Size_t)0 >> 1)
9a543cee 1557
cae7ae48 1558#define IV_DIG (BIT_DIGITS(IVSIZE * 8))
22ec83e3 1559#define UV_DIG (BIT_DIGITS(UVSIZE * 8))
56431972 1560
28e5dec8 1561#ifndef NO_PERL_PRESERVE_IVUV
f5c03d33 1562#define PERL_PRESERVE_IVUV /* We like our integers to stay integers. */
28e5dec8
JH
1563#endif
1564
d460ef45 1565/*
26bb67e2
JH
1566 * The macros INT2PTR and NUM2PTR are (despite their names)
1567 * bi-directional: they will convert int/float to or from pointers.
1568 * However the conversion to int/float are named explicitly:
1569 * PTR2IV, PTR2UV, PTR2NV.
1570 *
1571 * For int conversions we do not need two casts if pointers are
1572 * the same size as IV and UV. Otherwise we need an explicit
1573 * cast (PTRV) to avoid compiler warnings.
1574 */
56431972
RB
1575#if (IVSIZE == PTRSIZE) && (UVSIZE == PTRSIZE)
1576# define PTRV UV
1577# define INT2PTR(any,d) (any)(d)
da5fdda8
AC
1578#elif PTRSIZE == LONGSIZE
1579# define PTRV unsigned long
1580# define PTR2ul(p) (unsigned long)(p)
56431972 1581#else
da5fdda8 1582# define PTRV unsigned
e91e3b10
RB
1583#endif
1584
1585#ifndef INT2PTR
56431972 1586# define INT2PTR(any,d) (any)(PTRV)(d)
42718184 1587#endif
e91e3b10
RB
1588
1589#ifndef PTR2ul
1590# define PTR2ul(p) INT2PTR(unsigned long,p)
1591#endif
1592
56431972
RB
1593#define NUM2PTR(any,d) (any)(PTRV)(d)
1594#define PTR2IV(p) INT2PTR(IV,p)
1595#define PTR2UV(p) INT2PTR(UV,p)
1596#define PTR2NV(p) NUM2PTR(NV,p)
e91e3b10 1597#define PTR2nat(p) (PTRV)(p) /* pointer to integer of PTRSIZE */
d460ef45 1598
8141890a
JH
1599/* According to strict ANSI C89 one cannot freely cast between
1600 * data pointers and function (code) pointers. There are at least
1601 * two ways around this. One (used below) is to do two casts,
1602 * first the other pointer to an (unsigned) integer, and then
1603 * the integer to the other pointer. The other way would be
1604 * to use unions to "overlay" the pointers. For an example of
1605 * the latter technique, see union dirpu in struct xpvio in sv.h.
1606 * The only feasible use is probably temporarily storing
1607 * function pointers in a data pointer (such as a void pointer). */
1608
e91e3b10
RB
1609#define DPTR2FPTR(t,p) ((t)PTR2nat(p)) /* data pointer to function pointer */
1610#define FPTR2DPTR(t,p) ((t)PTR2nat(p)) /* function pointer to data pointer */
8141890a 1611
65202027 1612#ifdef USE_LONG_DOUBLE
f3654d06
JH
1613# if LONG_DOUBLESIZE == DOUBLESIZE
1614# define LONG_DOUBLE_EQUALS_DOUBLE
1615# undef USE_LONG_DOUBLE /* Ouch! */
65202027
DS
1616# endif
1617#endif
1618
2d4389e4
JH
1619/* The following is all to get LDBL_DIG, in order to pick a nice
1620 default value for printing floating point numbers in Gconvert.
1621 (see config.h)
1622*/
63b481b9 1623#ifndef HAS_LDBL_DIG
68d4903c 1624# if LONG_DOUBLESIZE == 10
63b481b9
AC
1625# define LDBL_DIG 18 /* assume IEEE */
1626# elif LONG_DOUBLESIZE == 12
68d4903c 1627# define LDBL_DIG 18 /* gcc? */
63b481b9
AC
1628# elif LONG_DOUBLESIZE == 16
1629# define LDBL_DIG 33 /* assume IEEE */
1630# elif LONG_DOUBLESIZE == DOUBLESIZE
1631# define LDBL_DIG DBL_DIG /* bummer */
68d4903c 1632# endif
2d4389e4
JH
1633#endif
1634
a22e52b9 1635typedef NVTYPE NV;
8175356b 1636
792d8dab
JH
1637#ifdef I_IEEEFP
1638# include <ieeefp.h>
1639#endif
923fc586 1640
c32c3de1
JH
1641#ifdef USING_MSVC6
1642/* VC6 has broken NaN semantics: NaN == NaN returns true instead of false,
1643 * and for example NaN < IV_MIN. */
bcd8bfa9
JH
1644# define NAN_COMPARE_BROKEN
1645#endif
1646#if defined(__DECC) && defined(__osf__)
1647/* Also Tru64 cc has broken NaN comparisons. */
1648# define NAN_COMPARE_BROKEN
c32c3de1 1649#endif
6504068e
JH
1650#if defined(__sgi)
1651# define NAN_COMPARE_BROKEN
1652#endif
c32c3de1 1653
65202027 1654#ifdef USE_LONG_DOUBLE
923fc586
JH
1655# ifdef I_SUNMATH
1656# include <sunmath.h>
1657# endif
84e6cb05 1658# if defined(LDBL_DIG)
05b4a618
JH
1659# define NV_DIG LDBL_DIG
1660# ifdef LDBL_MANT_DIG
1661# define NV_MANT_DIG LDBL_MANT_DIG
1662# endif
1663# ifdef LDBL_MIN
1664# define NV_MIN LDBL_MIN
1665# endif
1666# ifdef LDBL_MAX
1667# define NV_MAX LDBL_MAX
1668# endif
1669# ifdef LDBL_MIN_EXP
1670# define NV_MIN_EXP LDBL_MIN_EXP
1671# endif
1672# ifdef LDBL_MAX_EXP
1673# define NV_MAX_EXP LDBL_MAX_EXP
1674# endif
1675# ifdef LDBL_MIN_10_EXP
1676# define NV_MIN_10_EXP LDBL_MIN_10_EXP
1677# endif
1678# ifdef LDBL_MAX_10_EXP
1679# define NV_MAX_10_EXP LDBL_MAX_10_EXP
1680# endif
1681# ifdef LDBL_EPSILON
1682# define NV_EPSILON LDBL_EPSILON
1683# endif
1684# ifdef LDBL_MAX
1685# define NV_MAX LDBL_MAX
20f6aaab 1686/* Having LDBL_MAX doesn't necessarily mean that we have LDBL_MIN... -Allen */
da5fdda8
AC
1687# elif defined(HUGE_VALL)
1688# define NV_MAX HUGE_VALL
f4a14a62
JH
1689# endif
1690# endif
84e6cb05 1691# if defined(HAS_SQRTL)
8a00eddc
JH
1692# define Perl_acos acosl
1693# define Perl_asin asinl
1694# define Perl_atan atanl
940e3d56
JH
1695# define Perl_atan2 atan2l
1696# define Perl_ceil ceill
1697# define Perl_cos cosl
8a00eddc 1698# define Perl_cosh coshl
940e3d56 1699# define Perl_exp expl
55da5e5b 1700/* no Perl_fabs, but there's PERL_ABS */
940e3d56
JH
1701# define Perl_floor floorl
1702# define Perl_fmod fmodl
1703# define Perl_log logl
8a00eddc 1704# define Perl_log10 log10l
940e3d56
JH
1705# define Perl_pow powl
1706# define Perl_sin sinl
8a00eddc 1707# define Perl_sinh sinhl
940e3d56 1708# define Perl_sqrt sqrtl
8a00eddc
JH
1709# define Perl_tan tanl
1710# define Perl_tanh tanhl
68d4903c 1711# endif
a3540c92 1712/* e.g. libsunmath doesn't have modfl and frexpl as of mid-March 2000 */
05b4a618
JH
1713# ifndef Perl_modf
1714# ifdef HAS_MODFL
1715# define Perl_modf(x,y) modfl(x,y)
51997bc3
NC
1716/* eg glibc 2.2 series seems to provide modfl on ppc and arm, but has no
1717 prototype in <math.h> */
05b4a618 1718# ifndef HAS_MODFL_PROTO
a221a8a5 1719EXTERN_C long double modfl(long double, long double *);
05b4a618
JH
1720# endif
1721# elif (defined(HAS_TRUNCL) || defined(HAS_AINTL)) && defined(HAS_COPYSIGNL)
55954f19 1722 extern long double Perl_my_modfl(long double x, long double *ip);
03476e8e 1723# define Perl_modf(x,y) Perl_my_modfl(x,y)
05b4a618 1724# endif
a3540c92 1725# endif
05b4a618
JH
1726# ifndef Perl_frexp
1727# ifdef HAS_FREXPL
1728# define Perl_frexp(x,y) frexpl(x,y)
da5fdda8 1729# elif defined(HAS_ILOGBL) && defined(HAS_SCALBNL)
05b4a618 1730extern long double Perl_my_frexpl(long double x, int *e);
da5fdda8 1731# define Perl_frexp(x,y) Perl_my_frexpl(x,y)
03476e8e 1732# endif
a3540c92 1733# endif
05b4a618
JH
1734# ifndef Perl_ldexp
1735# ifdef HAS_LDEXPL
1736# define Perl_ldexp(x, y) ldexpl(x,y)
da5fdda8
AC
1737# elif defined(HAS_SCALBNL) && FLT_RADIX == 2
1738# define Perl_ldexp(x,y) scalbnl(x,y)
98181445
JH
1739# endif
1740# endif
38dbb4c5 1741# ifndef Perl_isnan
e4c957f4 1742# if defined(HAS_ISNANL) && !(defined(isnan) && defined(HAS_C99))
758a5d79 1743# define Perl_isnan(x) isnanl(x)
a1115378
JH
1744# elif defined(__sgi) && defined(__c99) /* XXX Configure test needed */
1745# define Perl_isnan(x) isnan(x)
758a5d79
JH
1746# endif
1747# endif
1748# ifndef Perl_isinf
e4c957f4 1749# if defined(HAS_ISINFL) && !(defined(isinf) && defined(HAS_C99))
87190886 1750# define Perl_isinf(x) isinfl(x)
a1115378
JH
1751# elif defined(__sgi) && defined(__c99) /* XXX Configure test needed */
1752# define Perl_isinf(x) isinf(x)
bcd8bfa9 1753# elif defined(LDBL_MAX) && !defined(NAN_COMPARE_BROKEN)
efcbf317 1754# define Perl_isinf(x) ((x) > LDBL_MAX || (x) < -LDBL_MAX)
a3540c92
JH
1755# endif
1756# endif
e38461cc
JH
1757# ifndef Perl_isfinite
1758# define Perl_isfinite(x) Perl_isfinitel(x)
1759# endif
84e6cb05
JH
1760#elif defined(USE_QUADMATH) && defined(I_QUADMATH)
1761# include <quadmath.h>
1762# define NV_DIG FLT128_DIG
1763# define NV_MANT_DIG FLT128_MANT_DIG
1764# define NV_MIN FLT128_MIN
1765# define NV_MAX FLT128_MAX
1766# define NV_MIN_EXP FLT128_MIN_EXP
1767# define NV_MAX_EXP FLT128_MAX_EXP
1768# define NV_EPSILON FLT128_EPSILON
1769# define NV_MIN_10_EXP FLT128_MIN_10_EXP
1770# define NV_MAX_10_EXP FLT128_MAX_10_EXP
84e6cb05
JH
1771# define Perl_acos acosq
1772# define Perl_asin asinq
1773# define Perl_atan atanq
1774# define Perl_atan2 atan2q
1775# define Perl_ceil ceilq
1776# define Perl_cos cosq
1777# define Perl_cosh coshq
1778# define Perl_exp expq
1779/* no Perl_fabs, but there's PERL_ABS */
1780# define Perl_floor floorq
1781# define Perl_fmod fmodq
1782# define Perl_log logq
1783# define Perl_log10 log10q
1784# define Perl_pow powq
1785# define Perl_sin sinq
1786# define Perl_sinh sinhq
1787# define Perl_sqrt sqrtq
1788# define Perl_tan tanq
1789# define Perl_tanh tanhq
1790# define Perl_modf(x,y) modfq(x,y)
1791# define Perl_frexp(x,y) frexpq(x,y)
1792# define Perl_ldexp(x, y) ldexpq(x,y)
1793# define Perl_isinf(x) isinfq(x)
1794# define Perl_isnan(x) isnanq(x)
1795# define Perl_isfinite(x) !(isnanq(x) || isinfq(x))
b28053d1
JH
1796# define Perl_fp_class(x) ((x) == 0.0Q ? 0 : isinfq(x) ? 3 : isnanq(x) ? 4 : PERL_ABS(x) < FLT128_MIN ? 2 : 1)
1797# define Perl_fp_class_inf(x) (Perl_fp_class(x) == 3)
1798# define Perl_fp_class_nan(x) (Perl_fp_class(x) == 4)
1799# define Perl_fp_class_norm(x) (Perl_fp_class(x) == 1)
1800# define Perl_fp_class_denorm(x) (Perl_fp_class(x) == 2)
1801# define Perl_fp_class_zero(x) (Perl_fp_class(x) == 0)
65202027 1802#else
2d4389e4 1803# define NV_DIG DBL_DIG
63b481b9
AC
1804# define NV_MANT_DIG DBL_MANT_DIG
1805# define NV_MIN DBL_MIN
1806# define NV_MAX DBL_MAX
1807# define NV_MIN_EXP DBL_MIN_EXP
1808# define NV_MAX_EXP DBL_MAX_EXP
1809# define NV_MIN_10_EXP DBL_MIN_10_EXP
1810# define NV_MAX_10_EXP DBL_MAX_10_EXP
1811# define NV_EPSILON DBL_EPSILON
1812# define NV_MAX DBL_MAX
1813# define NV_MIN DBL_MIN
940e3d56 1814
55da5e5b 1815/* These math interfaces are C89. */
8a00eddc
JH
1816# define Perl_acos acos
1817# define Perl_asin asin
1818# define Perl_atan atan
940e3d56
JH
1819# define Perl_atan2 atan2
1820# define Perl_ceil ceil
1821# define Perl_cos cos
8a00eddc 1822# define Perl_cosh cosh
940e3d56 1823# define Perl_exp exp
55da5e5b 1824/* no Perl_fabs, but there's PERL_ABS */
940e3d56
JH
1825# define Perl_floor floor
1826# define Perl_fmod fmod
1827# define Perl_log log
8a00eddc 1828# define Perl_log10 log10
940e3d56
JH
1829# define Perl_pow pow
1830# define Perl_sin sin
8a00eddc 1831# define Perl_sinh sinh
940e3d56 1832# define Perl_sqrt sqrt
8a00eddc
JH
1833# define Perl_tan tan
1834# define Perl_tanh tanh
940e3d56
JH
1835
1836# define Perl_modf(x,y) modf(x,y)
1837# define Perl_frexp(x,y) frexp(x,y)
1838# define Perl_ldexp(x,y) ldexp(x,y)
1839
1a322af2
JH
1840# ifndef Perl_isnan
1841# ifdef HAS_ISNAN
1842# define Perl_isnan(x) isnan(x)
1843# endif
1844# endif
1845# ifndef Perl_isinf
1846# if defined(HAS_ISINF)
1847# define Perl_isinf(x) isinf(x)
bcd8bfa9 1848# elif defined(DBL_MAX) && !defined(NAN_COMPARE_BROKEN)
efcbf317 1849# define Perl_isinf(x) ((x) > DBL_MAX || (x) < -DBL_MAX)
1a322af2
JH
1850# endif
1851# endif
1852# ifndef Perl_isfinite
1853# ifdef HAS_ISFINITE
1854# define Perl_isfinite(x) isfinite(x)
1855# elif defined(HAS_FINITE)
1856# define Perl_isfinite(x) finite(x)
1857# endif
1858# endif
758a5d79
JH
1859#endif
1860
30404d48
JH
1861/* fpclassify(): C99. It is supposed to be a macro that switches on
1862* the sizeof() of its argument, so there's no need for e.g. fpclassifyl().*/
1863#if !defined(Perl_fp_class) && defined(HAS_FPCLASSIFY)
1864# include <math.h>
25c46df8
JH
1865# if defined(FP_INFINITE) && defined(FP_NAN)
1866# define Perl_fp_class(x) fpclassify(x)
1867# define Perl_fp_class_inf(x) (Perl_fp_class(x)==FP_INFINITE)
1868# define Perl_fp_class_nan(x) (Perl_fp_class(x)==FP_NAN)
1869# define Perl_fp_class_norm(x) (Perl_fp_class(x)==FP_NORMAL)
1870# define Perl_fp_class_denorm(x) (Perl_fp_class(x)==FP_SUBNORMAL)
1871# define Perl_fp_class_zero(x) (Perl_fp_class(x)==FP_ZERO)
1872# elif defined(FP_PLUS_INF) && defined(FP_QNAN)
1873/* Some versions of HP-UX (10.20) have (only) fpclassify() but which is
1874 * actually not the C99 fpclassify, with its own set of return defines. */
1875# define Perl_fp_class(x) fpclassify(x)
1876# define Perl_fp_class_pinf(x) (Perl_fp_class(x)==FP_PLUS_INF)
1877# define Perl_fp_class_ninf(x) (Perl_fp_class(x)==FP_MINUS_INF)
82947af8 1878# define Perl_fp_class_snan(x) (Perl_fp_class(x)==FP_SNAN)
25c46df8
JH
1879# define Perl_fp_class_qnan(x) (Perl_fp_class(x)==FP_QNAN)
1880# define Perl_fp_class_pnorm(x) (Perl_fp_class(x)==FP_PLUS_NORM)
1ae51000 1881# define Perl_fp_class_nnorm(x) (Perl_fp_class(x)==FP_MINUS_NORM)
25c46df8
JH
1882# define Perl_fp_class_pdenorm(x) (Perl_fp_class(x)==FP_PLUS_DENORM)
1883# define Perl_fp_class_ndenorm(x) (Perl_fp_class(x)==FP_MINUS_DENORM)
1884# define Perl_fp_class_pzero(x) (Perl_fp_class(x)==FP_PLUS_ZERO)
1885# define Perl_fp_class_nzero(x) (Perl_fp_class(x)==FP_MINUS_ZERO)
1886# else
1887# undef Perl_fp_class /* Unknown set of defines */
1888# endif
30404d48
JH
1889#endif
1890
25c46df8
JH
1891/* fp_classify(): Legacy: VMS, maybe Unicos? The values, however,
1892 * are identical to the C99 fpclassify(). */
1893#if !defined(Perl_fp_class) && defined(HAS_FP_CLASSIFY)
1894# include <math.h>
558f3e66
CB
1895# ifdef __VMS
1896 /* FP_INFINITE and others are here rather than in math.h as C99 stipulates */
1897# include <fp.h>
56610b8f
CB
1898 /* oh, and the isnormal macro has a typo in it! */
1899# undef isnormal
1900# define isnormal(x) Perl_fp_class_norm(x)
558f3e66 1901# endif
25c46df8
JH
1902# if defined(FP_INFINITE) && defined(FP_NAN)
1903# define Perl_fp_class(x) fp_classify(x)
1904# define Perl_fp_class_inf(x) (Perl_fp_class(x)==FP_INFINITE)
1905# define Perl_fp_class_nan(x) (Perl_fp_class(x)==FP_NAN)
1906# define Perl_fp_class_norm(x) (Perl_fp_class(x)==FP_NORMAL)
1907# define Perl_fp_class_denorm(x) (Perl_fp_class(x)==FP_SUBNORMAL)
1908# define Perl_fp_class_zero(x) (Perl_fp_class(x)==FP_ZERO)
1909# else
1910# undef Perl_fp_class /* Unknown set of defines */
1911# endif
f279e099 1912#endif
205f51d8 1913
30404d48
JH
1914/* Feel free to check with me for the SGI manpages, SGI testing,
1915 * etcetera, if you want to try getting this to work with IRIX.
1916 *
1917 * - Allen <allens@cpan.org> */
1918
1919/* fpclass(): SysV, at least Solaris and some versions of IRIX. */
758a5d79 1920#if !defined(Perl_fp_class) && (defined(HAS_FPCLASS)||defined(HAS_FPCLASSL))
25c46df8
JH
1921/* Solaris and IRIX have fpclass/fpclassl, but they are using
1922 * an enum typedef, not cpp symbols, and Configure doesn't detect that.
1923 * Define some symbols also as cpp symbols so we can detect them. */
033a6f7a 1924# if defined(__sun) || defined(__sgi) /* XXX Configure test instead */
25c46df8
JH
1925# define FP_PINF FP_PINF
1926# define FP_QNAN FP_QNAN
1927# endif
f279e099 1928# include <math.h>
758a5d79
JH
1929# ifdef I_IEEFP
1930# include <ieeefp.h>
1931# endif
1932# ifdef I_FP
1933# include <fp.h>
1934# endif
1935# if defined(USE_LONG_DOUBLE) && defined(HAS_FPCLASSL)
f279e099 1936# define Perl_fp_class(x) fpclassl(x)
758a5d79 1937# else
f279e099
JH
1938# define Perl_fp_class(x) fpclass(x)
1939# endif
25c46df8 1940# if defined(FP_CLASS_PINF) && defined(FP_CLASS_SNAN)
f279e099
JH
1941# define Perl_fp_class_snan(x) (Perl_fp_class(x)==FP_CLASS_SNAN)
1942# define Perl_fp_class_qnan(x) (Perl_fp_class(x)==FP_CLASS_QNAN)
1943# define Perl_fp_class_ninf(x) (Perl_fp_class(x)==FP_CLASS_NINF)
1944# define Perl_fp_class_pinf(x) (Perl_fp_class(x)==FP_CLASS_PINF)
1945# define Perl_fp_class_nnorm(x) (Perl_fp_class(x)==FP_CLASS_NNORM)
1946# define Perl_fp_class_pnorm(x) (Perl_fp_class(x)==FP_CLASS_PNORM)
1947# define Perl_fp_class_ndenorm(x) (Perl_fp_class(x)==FP_CLASS_NDENORM)
1948# define Perl_fp_class_pdenorm(x) (Perl_fp_class(x)==FP_CLASS_PDENORM)
1949# define Perl_fp_class_nzero(x) (Perl_fp_class(x)==FP_CLASS_NZERO)
1950# define Perl_fp_class_pzero(x) (Perl_fp_class(x)==FP_CLASS_PZERO)
25c46df8 1951# elif defined(FP_PINF) && defined(FP_QNAN)
f279e099
JH
1952# define Perl_fp_class_snan(x) (Perl_fp_class(x)==FP_SNAN)
1953# define Perl_fp_class_qnan(x) (Perl_fp_class(x)==FP_QNAN)
1954# define Perl_fp_class_ninf(x) (Perl_fp_class(x)==FP_NINF)
1955# define Perl_fp_class_pinf(x) (Perl_fp_class(x)==FP_PINF)
1956# define Perl_fp_class_nnorm(x) (Perl_fp_class(x)==FP_NNORM)
1957# define Perl_fp_class_pnorm(x) (Perl_fp_class(x)==FP_PNORM)
1958# define Perl_fp_class_ndenorm(x) (Perl_fp_class(x)==FP_NDENORM)
1959# define Perl_fp_class_pdenorm(x) (Perl_fp_class(x)==FP_PDENORM)
1960# define Perl_fp_class_nzero(x) (Perl_fp_class(x)==FP_NZERO)
1961# define Perl_fp_class_pzero(x) (Perl_fp_class(x)==FP_PZERO)
25c46df8
JH
1962# else
1963# undef Perl_fp_class /* Unknown set of defines */
758a5d79 1964# endif
758a5d79
JH
1965#endif
1966
30404d48
JH
1967/* fp_class(): Legacy: at least Tru64, some versions of IRIX. */
1968#if !defined(Perl_fp_class) && (defined(HAS_FP_CLASS)||defined(HAS_FP_CLASSL))
758a5d79
JH
1969# include <math.h>
1970# if !defined(FP_SNAN) && defined(I_FP_CLASS)
1971# include <fp_class.h>
1972# endif
25c46df8 1973# if defined(FP_POS_INF) && defined(FP_QNAN)
033a6f7a 1974# ifdef __sgi /* XXX Configure test instead */
25c46df8
JH
1975# ifdef USE_LONG_DOUBLE
1976# define Perl_fp_class(x) fp_class_l(x)
1977# else
1978# define Perl_fp_class(x) fp_class_d(x)
1979# endif
f279e099 1980# else
25c46df8
JH
1981# if defined(USE_LONG_DOUBLE) && defined(HAS_FP_CLASSL)
1982# define Perl_fp_class(x) fp_classl(x)
1983# else
1984# define Perl_fp_class(x) fp_class(x)
1985# endif
f279e099 1986# endif
25c46df8
JH
1987# if defined(FP_POS_INF) && defined(FP_QNAN)
1988# define Perl_fp_class_snan(x) (Perl_fp_class(x)==FP_SNAN)
1989# define Perl_fp_class_qnan(x) (Perl_fp_class(x)==FP_QNAN)
1990# define Perl_fp_class_ninf(x) (Perl_fp_class(x)==FP_NEG_INF)
1991# define Perl_fp_class_pinf(x) (Perl_fp_class(x)==FP_POS_INF)
1992# define Perl_fp_class_nnorm(x) (Perl_fp_class(x)==FP_NEG_NORM)
1993# define Perl_fp_class_pnorm(x) (Perl_fp_class(x)==FP_POS_NORM)
1994# define Perl_fp_class_ndenorm(x) (Perl_fp_class(x)==FP_NEG_DENORM)
1995# define Perl_fp_class_pdenorm(x) (Perl_fp_class(x)==FP_POS_DENORM)
1996# define Perl_fp_class_nzero(x) (Perl_fp_class(x)==FP_NEG_ZERO)
1997# define Perl_fp_class_pzero(x) (Perl_fp_class(x)==FP_POS_ZERO)
30404d48 1998# else
25c46df8 1999# undef Perl_fp_class /* Unknown set of defines */
30404d48 2000# endif
f279e099 2001# endif
30404d48
JH
2002#endif
2003
052758ae 2004/* class(), _class(): Legacy: AIX. */
758a5d79
JH
2005#if !defined(Perl_fp_class) && defined(HAS_CLASS)
2006# include <math.h>
25c46df8
JH
2007# if defined(FP_PLUS_NORM) && defined(FP_PLUS_INF)
2008# ifndef _cplusplus
2009# define Perl_fp_class(x) class(x)
2010# else
2011# define Perl_fp_class(x) _class(x)
2012# endif
2013# if defined(FP_PLUS_INF) && defined(FP_NANQ)
2014# define Perl_fp_class_snan(x) (Perl_fp_class(x)==FP_NANS)
2015# define Perl_fp_class_qnan(x) (Perl_fp_class(x)==FP_NANQ)
2016# define Perl_fp_class_ninf(x) (Perl_fp_class(x)==FP_MINUS_INF)
2017# define Perl_fp_class_pinf(x) (Perl_fp_class(x)==FP_PLUS_INF)
2018# define Perl_fp_class_nnorm(x) (Perl_fp_class(x)==FP_MINUS_NORM)
2019# define Perl_fp_class_pnorm(x) (Perl_fp_class(x)==FP_PLUS_NORM)
2020# define Perl_fp_class_ndenorm(x) (Perl_fp_class(x)==FP_MINUS_DENORM)
2021# define Perl_fp_class_pdenorm(x) (Perl_fp_class(x)==FP_PLUS_DENORM)
2022# define Perl_fp_class_nzero(x) (Perl_fp_class(x)==FP_MINUS_ZERO)
2023# define Perl_fp_class_pzero(x) (Perl_fp_class(x)==FP_PLUS_ZERO)
2024# else
2025# undef Perl_fp_class /* Unknown set of defines */
2026# endif
758a5d79 2027# endif
30404d48
JH
2028#endif
2029
2030/* Win32: _fpclass(), _isnan(), _finite(). */
796ea9ea
JH
2031#ifdef WIN32
2032# ifndef Perl_isnan
2033# define Perl_isnan(x) _isnan(x)
2034# endif
2035# ifndef Perl_isfinite
2036# define Perl_isfinite(x) _finite(x)
2037# endif
2038# ifndef Perl_fp_class_snan
25c46df8
JH
2039/* No simple way to #define Perl_fp_class because _fpclass()
2040 * returns a set of bits. */
796ea9ea
JH
2041# define Perl_fp_class_snan(x) (_fpclass(x) & _FPCLASS_SNAN)
2042# define Perl_fp_class_qnan(x) (_fpclass(x) & _FPCLASS_QNAN)
82947af8 2043# define Perl_fp_class_nan(x) (_fpclass(x) & (_FPCLASS_SNAN|_FPCLASS_QNAN))
796ea9ea
JH
2044# define Perl_fp_class_ninf(x) (_fpclass(x) & _FPCLASS_NINF))
2045# define Perl_fp_class_pinf(x) (_fpclass(x) & _FPCLASS_PINF))
2046# define Perl_fp_class_inf(x) (_fpclass(x) & (_FPCLASS_NINF|_FPCLASS_PINF))
2047# define Perl_fp_class_nnorm(x) (_fpclass(x) & _FPCLASS_NN)
2048# define Perl_fp_class_pnorm(x) (_fpclass(x) & _FPCLASS_PN)
2049# define Perl_fp_class_norm(x) (_fpclass(x) & (_FPCLASS_NN|_FPCLASS_PN))
2050# define Perl_fp_class_ndenorm(x) (_fpclass(x) & _FPCLASS_ND)
2051# define Perl_fp_class_pdenorm(x) (_fpclass(x) & _FPCLASS_PD)
2052# define Perl_fp_class_denorm(x) (_fpclass(x) & (_FPCLASS_ND|_FPCLASS_PD))
2053# define Perl_fp_class_nzero(x) (_fpclass(x) & _FPCLASS_NZ)
2054# define Perl_fp_class_pzero(x) (_fpclass(x) & _FPCLASS_PZ)
2055# define Perl_fp_class_zero(x) (_fpclass(x) & (_FPCLASS_NZ|_FPCLASS_PZ))
2056# endif
f279e099
JH
2057#endif
2058
2059#if !defined(Perl_fp_class_inf) && \
2060 defined(Perl_fp_class_pinf) && defined(Perl_fp_class_ninf)
2061# define Perl_fp_class_inf(x) \
2062 (Perl_fp_class_pinf(x) || Perl_fp_class_ninf(x))
2063#endif
2064
2065#if !defined(Perl_fp_class_nan) && \
2066 defined(Perl_fp_class_snan) && defined(Perl_fp_class_qnan)
2067# define Perl_fp_class_nan(x) \
2068 (Perl_fp_class_snan(x) || Perl_fp_class_qnan(x))
2069#endif
2070
2071#if !defined(Perl_fp_class_zero) && \
2072 defined(Perl_fp_class_pzero) && defined(Perl_fp_class_nzero)
2073# define Perl_fp_class_zero(x) \
2074 (Perl_fp_class_pzero(x) || Perl_fp_class_nzero(x))
2075#endif
2076
2077#if !defined(Perl_fp_class_norm) && \
2078 defined(Perl_fp_class_pnorm) && defined(Perl_fp_class_nnorm)
2079# define Perl_fp_class_norm(x) \
2080 (Perl_fp_class_pnorm(x) || Perl_fp_class_nnorm(x))
2081#endif
2082
2083#if !defined(Perl_fp_class_denorm) && \
2084 defined(Perl_fp_class_pdenorm) && defined(Perl_fp_class_ndenorm)
2085# define Perl_fp_class_denorm(x) \
2086 (Perl_fp_class_pdenorm(x) || Perl_fp_class_ndenorm(x))
796ea9ea 2087#endif
758a5d79 2088
1a322af2
JH
2089#ifdef UNDER_CE
2090int isnan(double d);
2091#endif
2092
758a5d79 2093#ifndef Perl_isnan
1a322af2
JH
2094# ifdef Perl_fp_class_nan
2095# define Perl_isnan(x) Perl_fp_class_nan(x)
da5fdda8
AC
2096# elif defined(HAS_UNORDERED)
2097# define Perl_isnan(x) unordered((x), 0.0)
758a5d79 2098# else
da5fdda8 2099# define Perl_isnan(x) ((x)!=(x))
758a5d79
JH
2100# endif
2101#endif
2102
1a322af2
JH
2103#ifndef Perl_isinf
2104# ifdef Perl_fp_class_inf
2105# define Perl_isinf(x) Perl_fp_class_inf(x)
2106# endif
ca6c63e1
JH
2107#endif
2108
758a5d79 2109#ifndef Perl_isfinite
25c46df8 2110# if defined(HAS_ISFINITE) && !defined(isfinite)
c496ca58 2111# define Perl_isfinite(x) isfinite((double)(x))
4efd38a4 2112# elif defined(HAS_FINITE)
c496ca58 2113# define Perl_isfinite(x) finite((double)(x))
4efd38a4
JH
2114# elif defined(Perl_fp_class_finite)
2115# define Perl_isfinite(x) Perl_fp_class_finite(x)
758a5d79 2116# else
c496ca58
JH
2117/* For the infinities the multiplication returns nan,
2118 * for the nan the multiplication also returns nan,
2119 * for everything else (that is, finite) zero should be returned. */
4efd38a4 2120# define Perl_isfinite(x) (((x) * 0) == 0)
a3540c92 2121# endif
65202027
DS
2122#endif
2123
25c46df8
JH
2124#ifndef Perl_isinf
2125# if defined(Perl_isfinite) && defined(Perl_isnan)
2126# define Perl_isinf(x) !(Perl_isfinite(x)||Perl_isnan(x))
2127# endif
2128#endif
2129
d3685250
JH
2130/* We need Perl_isfinitel (ends with ell) (if available) even when
2131 * not USE_LONG_DOUBLE because the printf code (sv_catpvfn_flags)
2132 * needs that. */
2133#if defined(HAS_LONG_DOUBLE) && !defined(Perl_isfinitel)
2134/* If isfinite() is a macro and looks like we have C99,
2135 * we assume it's the type-aware C99 isfinite(). */
c06e9e3e 2136# if defined(HAS_ISFINITE) && defined(isfinite) && defined(HAS_C99)
d3685250
JH
2137# define Perl_isfinitel(x) isfinite(x)
2138# elif defined(HAS_ISFINITEL)
2139# define Perl_isfinitel(x) isfinitel(x)
2140# elif defined(HAS_FINITEL)
2141# define Perl_isfinitel(x) finitel(x)
2142# elif defined(HAS_INFL) && defined(HAS_NANL)
2143# define Perl_isfinitel(x) !(isinfl(x)||isnanl(x))
16334e6e 2144# else
99bf9e32 2145# define Perl_isfinitel(x) ((x) * 0 == 0) /* See Perl_isfinite. */
d3685250
JH
2146# endif
2147#endif
2148
a36244b7
JH
2149/* The default is to use Perl's own atof() implementation (in numeric.c).
2150 * Usually that is the one to use but for some platforms (e.g. UNICOS)
2151 * it is however best to use the native implementation of atof.
2152 * You can experiment with using your native one by -DUSE_PERL_ATOF=0.
2153 * Some good tests to try out with either setting are t/base/num.t,
205f51d8
AS
2154 * t/op/numconvert.t, and t/op/pack.t. Note that if using long doubles
2155 * you may need to be using a different function than atof! */
a36244b7
JH
2156
2157#ifndef USE_PERL_ATOF
2158# ifndef _UNICOS
2159# define USE_PERL_ATOF
2160# endif
2161#else
2162# if USE_PERL_ATOF == 0
2163# undef USE_PERL_ATOF
2164# endif
2165#endif
2166
2167#ifdef USE_PERL_ATOF
2168# define Perl_atof(s) Perl_my_atof(s)
2169# define Perl_atof2(s, n) Perl_my_atof2(aTHX_ (s), &(n))
2170#else
2171# define Perl_atof(s) (NV)atof(s)
2172# define Perl_atof2(s, n) ((n) = atof(s))
2173#endif
cf2093f6 2174
99abf803
PP
2175/*
2176 * CHAR_MIN and CHAR_MAX are not included here, as the (char) type may be
2177 * ambiguous. It may be equivalent to (signed char) or (unsigned char)
2178 * depending on local options. Until Configure detects this (or at least
2179 * detects whether the "signed" keyword is available) the CHAR ranges
2180 * will not be included. UCHAR functions normally.
2181 * - kja
2182 */
27d4fb96 2183
6ec488b3
AC
2184#define PERL_UCHAR_MIN ((unsigned char)0)
2185#define PERL_UCHAR_MAX ((unsigned char)UCHAR_MAX)
27d4fb96 2186
6ec488b3
AC
2187#define PERL_USHORT_MIN ((unsigned short)0)
2188#define PERL_USHORT_MAX ((unsigned short)USHRT_MAX)
27d4fb96 2189
6ec488b3
AC
2190#define PERL_SHORT_MAX ((short)SHRT_MAX)
2191#define PERL_SHORT_MIN ((short)SHRT_MIN)
27d4fb96 2192
6ec488b3 2193#define PERL_UINT_MAX ((unsigned int)UINT_MAX)
99abf803 2194#define PERL_UINT_MIN ((unsigned int)0)
27d4fb96 2195
6ec488b3
AC
2196#define PERL_INT_MAX ((int)INT_MAX)
2197#define PERL_INT_MIN ((int)INT_MIN)
27d4fb96 2198
6ec488b3 2199#define PERL_ULONG_MAX ((unsigned long)ULONG_MAX)
99abf803 2200#define PERL_ULONG_MIN ((unsigned long)0L)
27d4fb96 2201
6ec488b3
AC
2202#define PERL_LONG_MAX ((long)LONG_MAX)
2203#define PERL_LONG_MIN ((long)LONG_MIN)
760ac839 2204
d7d93a81 2205#ifdef UV_IS_QUAD
99abf803 2206# define PERL_UQUAD_MAX (~(UV)0)
f1c3b19a 2207# define PERL_UQUAD_MIN ((UV)0)
99abf803 2208# define PERL_QUAD_MAX ((IV) (PERL_UQUAD_MAX >> 1))
a6e633de 2209# define PERL_QUAD_MIN (-PERL_QUAD_MAX - ((3 & -1) == 3))
79072805
LW
2210#endif
2211
ee0007ab 2212typedef MEM_SIZE STRLEN;
450a55e4 2213
79072805
LW
2214typedef struct op OP;
2215typedef struct cop COP;
2216typedef struct unop UNOP;
2f7c6295 2217typedef struct unop_aux UNOP_AUX;
79072805
LW
2218typedef struct binop BINOP;
2219typedef struct listop LISTOP;
2220typedef struct logop LOGOP;
79072805
LW
2221typedef struct pmop PMOP;
2222typedef struct svop SVOP;
7934575e 2223typedef struct padop PADOP;
79072805 2224typedef struct pvop PVOP;
79072805 2225typedef struct loop LOOP;
b46e009d 2226typedef struct methop METHOP;
79072805 2227
7aef8e5b 2228#ifdef PERL_CORE
8be227ab
FC
2229typedef struct opslab OPSLAB;
2230typedef struct opslot OPSLOT;
2231#endif
2232
52db365a 2233typedef struct block_hooks BHK;
1830b3d9 2234typedef struct custom_op XOP;
52db365a 2235
cd1541b2
NC
2236typedef struct interpreter PerlInterpreter;
2237
d2b3f365 2238/* SGI's <sys/sema.h> has struct sv */
cc3315ba 2239#if defined(__sgi)
d2b3f365 2240# define STRUCT_SV perl_sv
b8d3c5db
JH
2241#else
2242# define STRUCT_SV sv
2243#endif
2244typedef struct STRUCT_SV SV;
79072805
LW
2245typedef struct av AV;
2246typedef struct hv HV;
2247typedef struct cv CV;
d2f13c59 2248typedef struct p5rx REGEXP;
79072805 2249typedef struct gp GP;
0c30d9ec 2250typedef struct gv GV;
8990e307 2251typedef struct io IO;
c09156bb 2252typedef struct context PERL_CONTEXT;
79072805
LW
2253typedef struct block BLOCK;
2254
2255typedef struct magic MAGIC;
2256typedef struct xpv XPV;
2257typedef struct xpviv XPVIV;
ff68c719 2258typedef struct xpvuv XPVUV;
79072805
LW
2259typedef struct xpvnv XPVNV;
2260typedef struct xpvmg XPVMG;
2261typedef struct xpvlv XPVLV;
d361b004 2262typedef struct xpvinvlist XINVLIST;
79072805
LW
2263typedef struct xpvav XPVAV;
2264typedef struct xpvhv XPVHV;
2265typedef struct xpvgv XPVGV;
2266typedef struct xpvcv XPVCV;
2267typedef struct xpvbm XPVBM;
2268typedef struct xpvfm XPVFM;
8990e307 2269typedef struct xpvio XPVIO;
79072805
LW
2270typedef struct mgvtbl MGVTBL;
2271typedef union any ANY;
5f7fde29
GS
2272typedef struct ptr_tbl_ent PTR_TBL_ENT_t;
2273typedef struct ptr_tbl PTR_TBL_t;
8cf8f3d1
NIS
2274typedef struct clone_params CLONE_PARAMS;
2275
0f94cb1f 2276/* a pad is currently just an AV; but that might change,
7261499d
FC
2277 * so hide the type. */
2278typedef struct padlist PADLIST;
67634234 2279typedef AV PAD;
9b7476d7 2280typedef struct padnamelist PADNAMELIST;
0f94cb1f 2281typedef struct padname PADNAME;
67634234 2282
5d32d268
DM
2283/* enable PERL_OP_PARENT by default */
2284#if !defined(PERL_OP_PARENT) && !defined(PERL_NO_OP_PARENT)
2285# define PERL_OP_PARENT
2286#endif
2287
93c10d60
FC
2288/* enable PERL_COPY_ON_WRITE by default */
2289#if !defined(PERL_COPY_ON_WRITE) && !defined(PERL_NO_COW)
2290# define PERL_COPY_ON_WRITE
13b0f67d 2291#endif
07d01d6e 2292
93c10d60 2293#ifdef PERL_COPY_ON_WRITE
db2c6cb3 2294# define PERL_ANY_COW
9f351b45
DM
2295#else
2296# define PERL_SAWAMPERSAND
db2c6cb3
FC
2297#endif
2298
40653c20
FC
2299#if defined(PERL_DEBUG_READONLY_OPS) && !defined(USE_ITHREADS)
2300# error PERL_DEBUG_READONLY_OPS only works with ithreads
2301#endif
2302
378cc40b 2303#include "handy.h"
64935bc6 2304#include "charclass_invlists.h"
a0d0e21e 2305
6b8eaf93 2306#if defined(USE_LARGE_FILES) && !defined(NO_64_BIT_RAWIO)
6b8eaf93
JH
2307# if LSEEKSIZE == 8 && !defined(USE_64_BIT_RAWIO)
2308# define USE_64_BIT_RAWIO /* implicit */
2309# endif
4564133c
JH
2310#endif
2311
6b8eaf93
JH
2312/* Notice the use of HAS_FSEEKO: now we are obligated to always use
2313 * fseeko/ftello if possible. Don't go #defining ftell to ftello yourself,
2314 * however, because operating systems like to do that themself. */
2315#ifndef FSEEKSIZE
2316# ifdef HAS_FSEEKO
2317# define FSEEKSIZE LSEEKSIZE
2318# else
2319# define FSEEKSIZE LONGSIZE
d460ef45 2320# endif
6b8eaf93
JH
2321#endif
2322
2323#if defined(USE_LARGE_FILES) && !defined(NO_64_BIT_STDIO)
6b8eaf93
JH
2324# if FSEEKSIZE == 8 && !defined(USE_64_BIT_STDIO)
2325# define USE_64_BIT_STDIO /* implicit */
2326# endif
2327#endif
4564133c 2328
09458382 2329#ifdef USE_64_BIT_RAWIO
d9b3e12d
JH
2330# ifdef HAS_OFF64_T
2331# undef Off_t
2332# define Off_t off64_t
2333# undef LSEEKSIZE
2334# define LSEEKSIZE 8
5ff3f7a4 2335# endif
d9b3e12d
JH
2336/* Most 64-bit environments have defines like _LARGEFILE_SOURCE that
2337 * will trigger defines like the ones below. Some 64-bit environments,
09458382 2338 * however, do not. Therefore we have to explicitly mix and match. */
d9b3e12d
JH
2339# if defined(USE_OPEN64)
2340# define open open64
5ff3f7a4 2341# endif
d9b3e12d
JH
2342# if defined(USE_LSEEK64)
2343# define lseek lseek64
6b8eaf93
JH
2344# else
2345# if defined(USE_LLSEEK)
2346# define lseek llseek
2347# endif
d9b3e12d
JH
2348# endif
2349# if defined(USE_STAT64)
2350# define stat stat64
2351# endif
2352# if defined(USE_FSTAT64)
2353# define fstat fstat64
2354# endif
2355# if defined(USE_LSTAT64)
2356# define lstat lstat64
2357# endif
2358# if defined(USE_FLOCK64)
2359# define flock flock64
2360# endif
2361# if defined(USE_LOCKF64)
2362# define lockf lockf64
2363# endif
2364# if defined(USE_FCNTL64)
2365# define fcntl fcntl64
2366# endif
2367# if defined(USE_TRUNCATE64)
2368# define truncate truncate64
2369# endif
2370# if defined(USE_FTRUNCATE64)
2371# define ftruncate ftruncate64
2372# endif
2373#endif
2374
2375#ifdef USE_64_BIT_STDIO
2376# ifdef HAS_FPOS64_T
2377# undef Fpos_t
2378# define Fpos_t fpos64_t
2379# endif
2380/* Most 64-bit environments have defines like _LARGEFILE_SOURCE that
2381 * will trigger defines like the ones below. Some 64-bit environments,
2382 * however, do not. */
2383# if defined(USE_FOPEN64)
2384# define fopen fopen64
2385# endif
2386# if defined(USE_FSEEK64)
6b8eaf93 2387# define fseek fseek64 /* don't do fseeko here, see perlio.c */
d9b3e12d
JH
2388# endif
2389# if defined(USE_FTELL64)
6b8eaf93 2390# define ftell ftell64 /* don't do ftello here, see perlio.c */
d9b3e12d
JH
2391# endif
2392# if defined(USE_FSETPOS64)
2393# define fsetpos fsetpos64
2394# endif
2395# if defined(USE_FGETPOS64)
2396# define fgetpos fgetpos64
2397# endif
2398# if defined(USE_TMPFILE64)
2399# define tmpfile tmpfile64
2400# endif
2401# if defined(USE_FREOPEN64)
2402# define freopen freopen64
5ff3f7a4
GS
2403# endif
2404#endif
2405
e37778c2 2406#if defined(OS2)
2c2d71f5
JH
2407# include "iperlsys.h"
2408#endif
2409
748a9306 2410#ifdef DOSISH
19848b3f
JH
2411# if defined(OS2)
2412# include "os2ish.h"
2413# else
2414# include "dosish.h"
2415# endif
009819bb 2416#elif defined(VMS)
748a9306 2417# include "vmsish.h"
009819bb 2418#elif defined(PLAN9)
19848b3f 2419# include "./plan9/plan9ish.h"
009819bb 2420#elif defined(__VOS__)
196918b0
PG
2421# ifdef __GNUC__
2422# include "./vos/vosish.h"
2423# else
2424# include "vos/vosish.h"
2425# endif
009819bb 2426#elif defined(__SYMBIAN32__)
27da23d5 2427# include "symbian/symbianish.h"
009819bb 2428#elif defined(__HAIKU__)
df00ff3b 2429# include "haiku/haikuish.h"
009819bb 2430#else
19848b3f 2431# include "unixish.h"
13b6e58c
JH
2432#endif
2433
ea34f6bd 2434#ifdef __amigaos4__
3c0208ad
AB
2435# include "amigaos.h"
2436# undef FD_CLOEXEC /* a lie in AmigaOS */
2437#endif
2438
2946a158 2439/* NSIG logic from Configure --> */
2946a158
JH
2440#ifndef NSIG
2441# ifdef _NSIG
2442# define NSIG (_NSIG)
da5fdda8 2443# elif defined(SIGMAX)
2946a158 2444# define NSIG (SIGMAX+1)
da5fdda8 2445# elif defined(SIG_MAX)
2946a158 2446# define NSIG (SIG_MAX+1)
da5fdda8 2447# elif defined(_SIG_MAX)
2946a158 2448# define NSIG (_SIG_MAX+1)
da5fdda8 2449# elif defined(MAXSIG)
2946a158 2450# define NSIG (MAXSIG+1)
da5fdda8 2451# elif defined(MAX_SIG)
2946a158 2452# define NSIG (MAX_SIG+1)
da5fdda8 2453# elif defined(SIGARRAYSIZE)
2946a158 2454# define NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */
da5fdda8 2455# elif defined(_sys_nsig)
2946a158 2456# define NSIG (_sys_nsig) /* Solaris 2.5 */
da5fdda8
AC
2457# else
2458 /* Default to some arbitrary number that's big enough to get most
2459 * of the common signals. */
2946a158 2460# define NSIG 50
da5fdda8 2461# endif
2946a158
JH
2462#endif
2463/* <-- NSIG logic from Configure */
2464
13b6e58c
JH
2465#ifndef NO_ENVIRON_ARRAY
2466# define USE_ENVIRON_ARRAY
2467#endif
32f822de 2468
c77b533b
JH
2469/*
2470 * initialise to avoid floating-point exceptions from overflow, etc
2471 */
2472#ifndef PERL_FPU_INIT
2473# ifdef HAS_FPSETMASK
2474# if HAS_FLOATINGPOINT_H
2475# include <floatingpoint.h>
2476# endif
ad3a8c67
NC
2477/* Some operating systems have this as a macro, which in turn expands to a comma
2478 expression, and the last sub-expression is something that gets calculated,
2479 and then they have the gall to warn that a value computed is not used. Hence
2480 cast to void. */
2481# define PERL_FPU_INIT (void)fpsetmask(0)
da5fdda8
AC
2482# elif defined(SIGFPE) && defined(SIG_IGN) && !defined(PERL_MICRO)
2483# define PERL_FPU_INIT PL_sigfpe_saved = (Sighandler_t) signal(SIGFPE, SIG_IGN)
2484# define PERL_FPU_PRE_EXEC { Sigsave_t xfpe; rsignal_save(SIGFPE, PL_sigfpe_saved, &xfpe);
2485# define PERL_FPU_POST_EXEC rsignal_restore(SIGFPE, &xfpe); }
c77b533b 2486# else
da5fdda8 2487# define PERL_FPU_INIT
c77b533b
JH
2488# endif
2489#endif
b35112e7
CS
2490#ifndef PERL_FPU_PRE_EXEC
2491# define PERL_FPU_PRE_EXEC {
2492# define PERL_FPU_POST_EXEC }
2493#endif
c77b533b 2494
a308b05a
JH
2495/* In Tru64 the cc -ieee enables the IEEE math but disables traps.
2496 * We need to reenable the "invalid" trap because otherwise generation
2497 * of NaN values leaves the IEEE fp flags in bad state, leaving any further
2498 * fp ops behaving strangely (Inf + 1 resulting in zero, for example). */
2499#ifdef __osf__
2500# include <machine/fpu.h>
2501# define PERL_SYS_FPU_INIT \
2502 STMT_START { \
2503 ieee_set_fp_control(IEEE_TRAP_ENABLE_INV); \
2504 signal(SIGFPE, SIG_IGN); \
2505 } STMT_END
2506#endif
94a08944
JH
2507/* In IRIX the default for Flush to Zero bit is true,
2508 * which means that results going below the minimum of normal
2509 * floating points go to zero, instead of going denormal/subnormal.
2510 * This is unlike almost any other system running Perl, so let's clear it.
2511 * [perl #123767] IRIX64 blead (ddce084a) opbasic/arith.t failure, originally
2512 * [perl #120426] small numbers shouldn't round to zero if they have extra floating digits
2513 *
2514 * XXX The flush-to-zero behaviour should be a Configure scan.
2515 * To change the behaviour usually requires some system-specific
2516 * incantation, though, like the below. */
2517#ifdef __sgi
2518# include <sys/fpu.h>
2519# define PERL_SYS_FPU_INIT \
2520 STMT_START { \
2521 union fpc_csr csr; \
2522 csr.fc_word = get_fpc_csr(); \
2523 csr.fc_struct.flush = 0; \
2524 set_fpc_csr(csr.fc_word); \
2525 } STMT_END
2526#endif
a308b05a
JH
2527
2528#ifndef PERL_SYS_FPU_INIT
2529# define PERL_SYS_FPU_INIT NOOP
2530#endif
2531
cbec8ebe
DM
2532#ifndef PERL_SYS_INIT3_BODY
2533# define PERL_SYS_INIT3_BODY(argvp,argcp,envp) PERL_SYS_INIT_BODY(argvp,argcp)
ed344e4f
IZ
2534#endif
2535
eb533572 2536/*
dcccc8ff
KW
2537=head1 Miscellaneous Functions
2538
4ad9e498 2539=for apidoc Am|void|PERL_SYS_INIT|int *argc|char*** argv
eb533572 2540Provides system-specific tune up of the C runtime environment necessary to
72d33970 2541run Perl interpreters. This should be called only once, before creating
eb533572
DM
2542any Perl interpreters.
2543
4ad9e498 2544=for apidoc Am|void|PERL_SYS_INIT3|int *argc|char*** argv|char*** env
eb533572 2545Provides system-specific tune up of the C runtime environment necessary to
72d33970 2546run Perl interpreters. This should be called only once, before creating
eb533572
DM
2547any Perl interpreters.
2548
2549=for apidoc Am|void|PERL_SYS_TERM|
2550Provides system-specific clean up of the C runtime environment after
72d33970 2551running Perl interpreters. This should be called only once, after
eb533572
DM
2552freeing any remaining Perl interpreters.
2553
2554=cut
2555 */
2556
cbec8ebe
DM
2557#define PERL_SYS_INIT(argc, argv) Perl_sys_init(argc, argv)
2558#define PERL_SYS_INIT3(argc, argv, env) Perl_sys_init3(argc, argv, env)
d0820ef1 2559#define PERL_SYS_TERM() Perl_sys_term()
cbec8ebe 2560
be708cc0
JH
2561#ifndef PERL_WRITE_MSG_TO_CONSOLE
2562# define PERL_WRITE_MSG_TO_CONSOLE(io, msg, len) PerlIO_write(io, msg, len)
2563#endif
2564
0f31cffe
GS
2565#ifndef MAXPATHLEN
2566# ifdef PATH_MAX
b990c8b2
JH
2567# ifdef _POSIX_PATH_MAX
2568# if PATH_MAX > _POSIX_PATH_MAX
09448d78
JH
2569/* POSIX 1990 (and pre) was ambiguous about whether PATH_MAX
2570 * included the null byte or not. Later amendments of POSIX,
2571 * XPG4, the Austin Group, and the Single UNIX Specification
2572 * all explicitly include the null byte in the PATH_MAX.
2573 * Ditto for _POSIX_PATH_MAX. */
2574# define MAXPATHLEN PATH_MAX
b990c8b2 2575# else
09448d78 2576# define MAXPATHLEN _POSIX_PATH_MAX
b990c8b2
JH
2577# endif
2578# else
2579# define MAXPATHLEN (PATH_MAX+1)
2580# endif
0f31cffe 2581# else
d552bce8 2582# define MAXPATHLEN 1024 /* Err on the large side. */
0f31cffe
GS
2583# endif
2584#endif
2585
c80263a8
SH
2586/* In case Configure was not used (we are using a "canned config"
2587 * such as Win32, or a cross-compilation setup, for example) try going
2588 * by the gcc major and minor versions. One useful URL is
2589 * http://www.ohse.de/uwe/articles/gcc-attributes.html,
2590 * but contrary to this information warn_unused_result seems
2591 * not to be in gcc 3.3.5, at least. --jhi
42bf5550
AD
2592 * Also, when building extensions with an installed perl, this allows
2593 * the user to upgrade gcc and get the right attributes, rather than
2594 * relying on the list generated at Configure time. --AD
c80263a8
SH
2595 * Set these up now otherwise we get confused when some of the <*thread.h>
2596 * includes below indirectly pull in <perlio.h> (which needs to know if we
2597 * have HASATTRIBUTE_FORMAT).
2598 */
2599
4d7c7898 2600#ifndef PERL_MICRO
fc2007d4 2601#if defined __GNUC__ && !defined(__INTEL_COMPILER)
6a387721
RGS
2602# if __GNUC__ == 3 && __GNUC_MINOR__ >= 1 || __GNUC__ > 3 /* 3.1 -> */
2603# define HASATTRIBUTE_DEPRECATED
2604# endif
c80263a8
SH
2605# if __GNUC__ >= 3 /* 3.0 -> */ /* XXX Verify this version */
2606# define HASATTRIBUTE_FORMAT
63cdf24b
SH
2607# if defined __MINGW32__
2608# define PRINTF_FORMAT_NULL_OK
2609# endif
c80263a8
SH
2610# endif
2611# if __GNUC__ >= 3 /* 3.0 -> */
2612# define HASATTRIBUTE_MALLOC
2613# endif
2614# if __GNUC__ == 3 && __GNUC_MINOR__ >= 3 || __GNUC__ > 3 /* 3.3 -> */
2615# define HASATTRIBUTE_NONNULL
2616# endif
2617# if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 || __GNUC__ > 2 /* 2.5 -> */
2618# define HASATTRIBUTE_NORETURN
2619# endif
2620# if __GNUC__ >= 3 /* gcc 3.0 -> */
2621# define HASATTRIBUTE_PURE
2622# endif
42bf5550 2623# if __GNUC__ == 3 && __GNUC_MINOR__ >= 4 || __GNUC__ > 3 /* 3.4 -> */
c80263a8
SH
2624# define HASATTRIBUTE_UNUSED
2625# endif
d324fe49
AD
2626# if __GNUC__ == 3 && __GNUC_MINOR__ == 3 && !defined(__cplusplus)
2627# define HASATTRIBUTE_UNUSED /* gcc-3.3, but not g++-3.3. */
2628# endif
c80263a8
SH
2629# if __GNUC__ == 3 && __GNUC_MINOR__ >= 4 || __GNUC__ > 3 /* 3.4 -> */
2630# define HASATTRIBUTE_WARN_UNUSED_RESULT
2631# endif
2632#endif
00f254e2 2633#endif /* #ifndef PERL_MICRO */
c80263a8 2634
3db8f154 2635/* USE_5005THREADS needs to be after unixish.h as <pthread.h> includes
dd96f567 2636 * <sys/signal.h> which defines NSIG - which will stop inclusion of <signal.h>
32f822de
GS
2637 * this results in many functions being undeclared which bothers C++
2638 * May make sense to have threads after "*ish.h" anyway
2639 */
2640
3baee7cc
JH
2641/* clang Thread Safety Analysis/Annotations/Attributes
2642 * http://clang.llvm.org/docs/ThreadSafetyAnalysis.html
2643 *
bdc795f4 2644 * Available since clang 3.6-ish (appeared in 3.4, but shaky still in 3.5).
3baee7cc 2645 * Apple XCode hijacks __clang_major__ and __clang_minor__
bdc795f4
JH
2646 * (6.1 means really clang 3.6), so needs extra hijinks
2647 * (could probably also test the contents of __apple_build_version__).
3baee7cc
JH
2648 */
2649#if defined(USE_ITHREADS) && defined(I_PTHREAD) && \
2650 defined(__clang__) && \
ab0fe36e
DM
2651 !defined(PERL_GLOBAL_STRUCT) && \
2652 !defined(PERL_GLOBAL_STRUCT_PRIVATE) && \
3baee7cc
JH
2653 !defined(SWIG) && \
2654 ((!defined(__apple_build_version__) && \
bdc795f4 2655 ((__clang_major__ == 3 && __clang_minor__ >= 6) || \
8412ccf9 2656 (__clang_major__ >= 4))) || \
3baee7cc 2657 (defined(__apple_build_version__) && \
bdc795f4
JH
2658 ((__clang_major__ == 6 && __clang_minor__ >= 1) || \
2659 (__clang_major__ >= 7))))
3baee7cc
JH
2660# define PERL_TSA__(x) __attribute__((x))
2661# define PERL_TSA_ACTIVE
2662#else
2663# define PERL_TSA__(x) /* No TSA, make TSA attributes no-ops. */
2664# undef PERL_TSA_ACTIVE
2665#endif
2666
2667/* PERL_TSA_CAPABILITY() is used to annotate typedefs.
2668 * typedef old_type PERL_TSA_CAPABILITY("mutex") new_type;
2669 */
2670#define PERL_TSA_CAPABILITY(x) \
2671 PERL_TSA__(capability(x))
2672
2673/* In the below examples the mutex must be lexically visible, usually
2674 * either as global variables, or as function arguments. */
2675
2676/* PERL_TSA_GUARDED_BY() is used to annotate global variables.
2677 *
2678 * Foo foo PERL_TSA_GUARDED_BY(mutex);
2679 */
2680#define PERL_TSA_GUARDED_BY(x) \
2681 PERL_TSA__(guarded_by(x))
2682
2683/* PERL_TSA_PT_GUARDED_BY() is used to annotate global pointers.
2684 * The data _behind_ the pointer is guarded.
2685 *
2686 * Foo* ptr PERL_TSA_PT_GUARDED_BY(mutex);
2687 */
2688#define PERL_TSA_PT_GUARDED_BY(x) \
2689 PERL_TSA__(pt_guarded_by(x))
2690
2691/* PERL_TSA_REQUIRES() is used to annotate functions.
2692 * The caller MUST hold the resource when calling the function.
2693 *
2694 * void Foo() PERL_TSA_REQUIRES(mutex);
2695 */
2696#define PERL_TSA_REQUIRES(x) \
2697 PERL_TSA__(requires_capability(x))
2698
2699/* PERL_TSA_EXCLUDES() is used to annotate functions.
2700 * The caller MUST NOT hold resource when calling the function.
2701 *
2702 * EXCLUDES should be used when the function first acquires
2703 * the resource and then releases it. Use to avoid deadlock.
2704 *
2705 * void Foo() PERL_TSA_EXCLUDES(mutex);
2706 */
2707#define PERL_TSA_EXCLUDES(x) \
2708 PERL_TSA__(locks_excluded(x))
2709
2710/* PERL_TSA_ACQUIRE() is used to annotate functions.
2711 * The caller MUST NOT hold the resource when calling the function,
2712 * and the function will acquire the resource.
2713 *
2714 * void Foo() PERL_TSA_ACQUIRE(mutex);
2715 */
2716#define PERL_TSA_ACQUIRE(x) \
2717 PERL_TSA__(acquire_capability(x))
2718
2719/* PERL_TSA_RELEASE() is used to annotate functions.
2720 * The caller MUST hold the resource when calling the function,
2721 * and the function will release the resource.
2722 *
2723 * void Foo() PERL_TSA_RELEASE(mutex);
2724 */
2725#define PERL_TSA_RELEASE(x) \
2726 PERL_TSA__(release_capability(x))
2727
2728/* PERL_TSA_NO_TSA is used to annotate functions.
2729 * Used when being intentionally unsafe, or when the code is too
2730 * complicated for the analysis. Use sparingly.
2731 *
2732 * void Foo() PERL_TSA_NO_TSA;
2733 */
2734#define PERL_TSA_NO_TSA \
2735 PERL_TSA__(no_thread_safety_analysis)
2736
2737/* There are more annotations/attributes available, see the clang
2738 * documentation for details. */
2739
3db8f154 2740#if defined(USE_ITHREADS)
2986a63f 2741# ifdef NETWARE
da5fdda8
AC
2742# include <nw5thread.h>
2743# elif defined(WIN32)
2744# include <win32thread.h>
2745# elif defined(OS2)
2746# include "os2thread.h"
2747# elif defined(I_MACH_CTHREADS)
2748# include <mach/cthreads.h>
7f3d1cf1
BH
2749typedef cthread_t perl_os_thread;
2750typedef mutex_t perl_mutex;
2751typedef condition_t perl_cond;
2752typedef void * perl_key;
da5fdda8
AC
2753# elif defined(I_PTHREAD) /* Posix threads */
2754# include <pthread.h>
7f3d1cf1 2755typedef pthread_t perl_os_thread;
3baee7cc 2756typedef pthread_mutex_t PERL_TSA_CAPABILITY("mutex") perl_mutex;
7f3d1cf1
BH
2757typedef pthread_cond_t perl_cond;
2758typedef pthread_key_t perl_key;
da5fdda8 2759# endif
3db8f154 2760#endif /* USE_ITHREADS */
c5be433b 2761
3baee7cc
JH
2762#ifdef PERL_TSA_ACTIVE
2763/* Since most pthread mutex interfaces have not been annotated, we
2764 * need to have these wrappers. The NO_TSA annotation is quite ugly
2765 * but it cannot be avoided in plain C, unlike in C++, where one could
2766 * e.g. use ACQUIRE() with no arg on a mutex lock method.
2767 *
2768 * The bodies of these wrappers are in util.c
2769 *
2770 * TODO: however, some platforms are starting to get these clang
2771 * thread safety annotations for pthreads, for example FreeBSD.
2772 * Do we need a way to a bypass these wrappers? */
67083b7b 2773EXTERN_C int perl_tsa_mutex_lock(perl_mutex* mutex)
3baee7cc
JH
2774 PERL_TSA_ACQUIRE(*mutex)
2775 PERL_TSA_NO_TSA;
67083b7b 2776EXTERN_C int perl_tsa_mutex_unlock(perl_mutex* mutex)
3baee7cc
JH
2777 PERL_TSA_RELEASE(*mutex)
2778 PERL_TSA_NO_TSA;
2779#endif
2780
66a93824 2781#if defined(WIN32)
1feb2720 2782# include "win32.h"
c5be433b
GS
2783#endif
2784
2986a63f
JH
2785#ifdef NETWARE
2786# include "netware.h"
2787#endif
2788
e5218da5 2789#define STATUS_UNIX PL_statusvalue
68dc0745 2790#ifdef VMS
6b88bc9c 2791# define STATUS_NATIVE PL_statusvalue_vms
7a7fd8e0
JM
2792/*
2793 * vaxc$errno is only guaranteed to be valid if errno == EVMSERR, otherwise
ff7298cb 2794 * its contents can not be trusted. Unfortunately, Perl seems to check
7a7fd8e0
JM
2795 * it on exit, so it when PL_statusvalue_vms is updated, vaxc$errno should
2796 * be updated also.
2797 */
2798# include <stsdef.h>
2799# include <ssdef.h>
2800/* Presume this because if VMS changes it, it will require a new
2801 * set of APIs for waiting on children for binary compatibility.
2802 */
2803# define child_offset_bits (8)
2804# ifndef C_FAC_POSIX
2805# define C_FAC_POSIX 0x35A000
2806# endif
2807
2808/* STATUS_EXIT - validates and returns a NATIVE exit status code for the
2809 * platform from the existing UNIX or Native status values.
2810 */
2811
37038d91 2812# define STATUS_EXIT \
7a7fd8e0
JM
2813 (((I32)PL_statusvalue_vms == -1 ? SS$_ABORT : PL_statusvalue_vms) | \
2814 (VMSISH_HUSHED ? STS$M_INHIB_MSG : 0))
2815
7a7fd8e0 2816
fb38d079
JM
2817/* STATUS_NATIVE_CHILD_SET - Calculate UNIX status that matches the child
2818 * exit code and shifts the UNIX value over the correct number of bits to
2819 * be a child status. Usually the number of bits is 8, but that could be
2820 * platform dependent. The NATIVE status code is presumed to have either
2821 * from a child process.
7a7fd8e0
JM
2822 */
2823
fb38d079
JM
2824/* This is complicated. The child processes return a true native VMS
2825 status which must be saved. But there is an assumption in Perl that
2826 the UNIX child status has some relationship to errno values, so
00f254e2 2827 Perl tries to translate it to text in some of the tests.
fb38d079
JM
2828 In order to get the string translation correct, for the error, errno
2829 must be EVMSERR, but that generates a different text message
2830 than what the test programs are expecting. So an errno value must
2831 be derived from the native status value when an error occurs.
2832 That will hide the true native status message. With this version of
2833 perl, the true native child status can always be retrieved so that
2834 is not a problem. But in this case, Pl_statusvalue and errno may
2835 have different values in them.
2836 */
7a7fd8e0 2837
fb38d079 2838# define STATUS_NATIVE_CHILD_SET(n) \
68dc0745 2839 STMT_START { \
2fbb330f
JM
2840 I32 evalue = (I32)n; \
2841 if (evalue == EVMSERR) { \
2842 PL_statusvalue_vms = vaxc$errno; \
2843 PL_statusvalue = evalue; \
7a7fd8e0 2844 } else { \
2fbb330f 2845 PL_statusvalue_vms = evalue; \
fb38d079 2846 if (evalue == -1) { \
3280af22 2847 PL_statusvalue = -1; \
7a7fd8e0
JM
2848 PL_statusvalue_vms = SS$_ABORT; /* Should not happen */ \
2849 } else \
fb38d079 2850 PL_statusvalue = Perl_vms_status_to_unix(evalue, 1); \
2fbb330f 2851 set_vaxc_errno(evalue); \
fb38d079
JM
2852 if ((PL_statusvalue_vms & C_FAC_POSIX) == C_FAC_POSIX) \
2853 set_errno(EVMSERR); \
2854 else set_errno(Perl_vms_status_to_unix(evalue, 0)); \
2855 PL_statusvalue = PL_statusvalue << child_offset_bits; \
2fbb330f 2856 } \
68dc0745 2857 } STMT_END
7a7fd8e0 2858
68dc0745 2859# ifdef VMSISH_STATUS
e5218da5 2860# define STATUS_CURRENT (VMSISH_STATUS ? STATUS_NATIVE : STATUS_UNIX)
68dc0745 2861# else
e5218da5 2862# define STATUS_CURRENT STATUS_UNIX
68dc0745 2863# endif
7a7fd8e0
JM
2864
2865 /* STATUS_UNIX_SET - takes a UNIX/POSIX errno value and attempts to update
2866 * the NATIVE status to an equivalent value. Can not be used to translate
2867 * exit code values as exit code values are not guaranteed to have any
2868 * relationship at all to errno values.
2869 * This is used when Perl is forcing errno to have a specific value.
2870 */
e5218da5 2871# define STATUS_UNIX_SET(n) \
68dc0745 2872 STMT_START { \
7a7fd8e0 2873 I32 evalue = (I32)n; \
0968cdad 2874 PL_statusvalue = evalue; \
3280af22 2875 if (PL_statusvalue != -1) { \
0968cdad
JM
2876 if (PL_statusvalue != EVMSERR) { \
2877 PL_statusvalue &= 0xFFFF; \
2878 if (MY_POSIX_EXIT) \
2879 PL_statusvalue_vms=PL_statusvalue ? SS$_ABORT : SS$_NORMAL;\
2880 else PL_statusvalue_vms = Perl_unix_status_to_vms(evalue); \
2881 } \
2882 else { \
2883 PL_statusvalue_vms = vaxc$errno; \
2884 } \
68dc0745 2885 } \
0968cdad
JM
2886 else PL_statusvalue_vms = SS$_ABORT; \
2887 set_vaxc_errno(PL_statusvalue_vms); \
7a7fd8e0
JM
2888 } STMT_END
2889
2890 /* STATUS_UNIX_EXIT_SET - Takes a UNIX/POSIX exit code and sets
e08e1e1d
JM
2891 * the NATIVE error status based on it.
2892 *
2893 * When in the default mode to comply with the Perl VMS documentation,
2894 * 0 is a success and any other code sets the NATIVE status to a failure
2895 * code of SS$_ABORT.
fb38d079
JM
2896 *
2897 * In the new POSIX EXIT mode, native status will be set so that the
2898 * actual exit code will can be retrieved by the calling program or
2899 * shell.
2900 *
2901 * If the exit code is not clearly a UNIX parent or child exit status,
2902 * it will be passed through as a VMS status.
7a7fd8e0
JM
2903 */
2904
fb38d079 2905# define STATUS_UNIX_EXIT_SET(n) \
7a7fd8e0
JM
2906 STMT_START { \
2907 I32 evalue = (I32)n; \
2908 PL_statusvalue = evalue; \
e08e1e1d
JM
2909 if (MY_POSIX_EXIT) { \
2910 if (evalue <= 0xFF00) { \
2911 if (evalue > 0xFF) \
2912 evalue = (evalue >> child_offset_bits) & 0xFF; \
2913 PL_statusvalue_vms = \
2914 (C_FAC_POSIX | (evalue << 3 ) | \
2915 ((evalue == 1) ? (STS$K_ERROR | STS$M_INHIB_MSG) : 1)); \
2916 } else /* forgive them Perl, for they have sinned */ \
2917 PL_statusvalue_vms = evalue; \
2918 } else { \
2919 if (evalue == 0) \
2920 PL_statusvalue_vms = SS$_NORMAL; \
2921 else if (evalue <= 0xFF00) \
2922 PL_statusvalue_vms = SS$_ABORT; \
2923 else { /* forgive them Perl, for they have sinned */ \
2924 if (evalue != EVMSERR) PL_statusvalue_vms = evalue; \
2925 else PL_statusvalue_vms = vaxc$errno; \
2926 /* And obviously used a VMS status value instead of UNIX */ \
2927 PL_statusvalue = EVMSERR; \
2928 } \
2929 set_vaxc_errno(PL_statusvalue_vms); \
2930 } \
68dc0745 2931 } STMT_END
fb38d079 2932
e08e1e1d 2933
6ac6a52b
JM
2934 /* STATUS_EXIT_SET - Takes a NATIVE/UNIX/POSIX exit code
2935 * and sets the NATIVE error status based on it. This special case
2936 * is needed to maintain compatibility with past VMS behavior.
2937 *
2938 * In the default mode on VMS, this number is passed through as
2939 * both the NATIVE and UNIX status. Which makes it different
2940 * that the STATUS_UNIX_EXIT_SET.
2941 *
2942 * In the new POSIX EXIT mode, native status will be set so that the
2943 * actual exit code will can be retrieved by the calling program or
2944 * shell.
2945 *
1a3aec58
JM
2946 * A POSIX exit code is from 0 to 255. If the exit code is higher
2947 * than this, it needs to be assumed that it is a VMS exit code and
2948 * passed through.
6ac6a52b
JM
2949 */
2950
2951# define STATUS_EXIT_SET(n) \
2952 STMT_START { \
2953 I32 evalue = (I32)n; \
2954 PL_statusvalue = evalue; \
2955 if (MY_POSIX_EXIT) \
1a3aec58
JM
2956 if (evalue > 255) PL_statusvalue_vms = evalue; else { \
2957 PL_statusvalue_vms = \
2958 (C_FAC_POSIX | (evalue << 3 ) | \
2959 ((evalue == 1) ? (STS$K_ERROR | STS$M_INHIB_MSG) : 1));} \
6ac6a52b
JM
2960 else \
2961 PL_statusvalue_vms = evalue ? evalue : SS$_NORMAL; \
2962 set_vaxc_errno(PL_statusvalue_vms); \
2963 } STMT_END
2964
fb38d079
JM
2965
2966 /* This macro forces a success status */
7a7fd8e0
JM
2967# define STATUS_ALL_SUCCESS \
2968 (PL_statusvalue = 0, PL_statusvalue_vms = SS$_NORMAL)
fb38d079
JM
2969
2970 /* This macro forces a failure status */
7a7fd8e0
JM
2971# define STATUS_ALL_FAILURE (PL_statusvalue = 1, \
2972 vaxc$errno = PL_statusvalue_vms = MY_POSIX_EXIT ? \
2973 (C_FAC_POSIX | (1 << 3) | STS$K_ERROR | STS$M_INHIB_MSG) : SS$_ABORT)
fb38d079 2974
738ab09f
AB
2975#elif defined(__amigaos4__)
2976 /* A somewhat experimental attempt to simulate posix return code values */
2977# define STATUS_NATIVE PL_statusvalue_posix
2978# define STATUS_NATIVE_CHILD_SET(n) \
2979 STMT_START { \
2980 PL_statusvalue_posix = (n); \
2981 if (PL_statusvalue_posix < 0) { \
2982 PL_statusvalue = -1; \
2983 } \
2984 else { \
2985 PL_statusvalue = n << 8; \
2986 } \
2987 } STMT_END
2988# define STATUS_UNIX_SET(n) \
2989 STMT_START { \
2990 PL_statusvalue = (n); \
2991 if (PL_statusvalue != -1) \
2992 PL_statusvalue &= 0xFFFF; \
2993 } STMT_END
2994# define STATUS_UNIX_EXIT_SET(n) STATUS_UNIX_SET(n)
2995# define STATUS_EXIT_SET(n) STATUS_UNIX_SET(n)
2996# define STATUS_CURRENT STATUS_UNIX
2997# define STATUS_EXIT STATUS_UNIX
2998# define STATUS_ALL_SUCCESS (PL_statusvalue = 0, PL_statusvalue_posix = 0)
2999# define STATUS_ALL_FAILURE (PL_statusvalue = 1, PL_statusvalue_posix = 1)
3000
68dc0745 3001#else
e5218da5 3002# define STATUS_NATIVE PL_statusvalue_posix
e5218da5 3003# if defined(WCOREDUMP)
37038d91 3004# define STATUS_NATIVE_CHILD_SET(n) \
e5218da5
GA
3005 STMT_START { \
3006 PL_statusvalue_posix = (n); \
3007 if (PL_statusvalue_posix == -1) \
3008 PL_statusvalue = -1; \
3009 else { \
3010 PL_statusvalue = \
3011 (WIFEXITED(PL_statusvalue_posix) ? (WEXITSTATUS(PL_statusvalue_posix) << 8) : 0) | \
3012 (WIFSIGNALED(PL_statusvalue_posix) ? (WTERMSIG(PL_statusvalue_posix) & 0x7F) : 0) | \
3013 (WIFSIGNALED(PL_statusvalue_posix) && WCOREDUMP(PL_statusvalue_posix) ? 0x80 : 0); \
3014 } \
3015 } STMT_END
3016# elif defined(WIFEXITED)
37038d91 3017# define STATUS_NATIVE_CHILD_SET(n) \
e5218da5
GA
3018 STMT_START { \
3019 PL_statusvalue_posix = (n); \
3020 if (PL_statusvalue_posix == -1) \
3021 PL_statusvalue = -1; \
3022 else { \
3023 PL_statusvalue = \
3024 (WIFEXITED(PL_statusvalue_posix) ? (WEXITSTATUS(PL_statusvalue_posix) << 8) : 0) | \
3025 (WIFSIGNALED(PL_statusvalue_posix) ? (WTERMSIG(PL_statusvalue_posix) & 0x7F) : 0); \
3026 } \
3027 } STMT_END
3028# else
37038d91 3029# define STATUS_NATIVE_CHILD_SET(n) \
e5218da5
GA
3030 STMT_START { \
3031 PL_statusvalue_posix = (n); \
3032 if (PL_statusvalue_posix == -1) \
3033 PL_statusvalue = -1; \
3034 else { \
3035 PL_statusvalue = \
3036 PL_statusvalue_posix & 0xFFFF; \
3037 } \
3038 } STMT_END
3039# endif
3040# define STATUS_UNIX_SET(n) \
68dc0745 3041 STMT_START { \
3280af22
NIS
3042 PL_statusvalue = (n); \
3043 if (PL_statusvalue != -1) \
3044 PL_statusvalue &= 0xFFFF; \
68dc0745 3045 } STMT_END
7a7fd8e0 3046# define STATUS_UNIX_EXIT_SET(n) STATUS_UNIX_SET(n)
6ac6a52b 3047# define STATUS_EXIT_SET(n) STATUS_UNIX_SET(n)
e5218da5 3048# define STATUS_CURRENT STATUS_UNIX
37038d91 3049# define STATUS_EXIT STATUS_UNIX
e5218da5
GA
3050# define STATUS_ALL_SUCCESS (PL_statusvalue = 0, PL_statusvalue_posix = 0)
3051# define STATUS_ALL_FAILURE (PL_statusvalue = 1, PL_statusvalue_posix = 1)
68dc0745
PP
3052#endif
3053
cc3604b1
GS
3054/* flags in PL_exit_flags for nature of exit() */
3055#define PERL_EXIT_EXPECTED 0x01
31d77e54 3056#define PERL_EXIT_DESTRUCT_END 0x02 /* Run END in perl_destruct */
6136213b
JGM
3057#define PERL_EXIT_WARN 0x04 /* Warn if Perl_my_exit() or Perl_my_failure_exit() called */
3058#define PERL_EXIT_ABORT 0x08 /* Call abort() if Perl_my_exit() or Perl_my_failure_exit() called */
cc3604b1 3059
b79b76e0 3060#ifndef PERL_CORE
a7cb1f99 3061/* format to use for version numbers in file/directory names */
273cf8d1 3062/* XXX move to Configure? */
b79b76e0
NC
3063/* This was only ever used for the current version, and that can be done at
3064 compile time, as PERL_FS_VERSION, so should we just delete it? */
3065# ifndef PERL_FS_VER_FMT
3066# define PERL_FS_VER_FMT "%d.%d.%d"
3067# endif
3068#endif
3069
3070#ifndef PERL_FS_VERSION
3071# define PERL_FS_VERSION PERL_VERSION_STRING
0b94c7bb
GS
3072#endif
3073
45bc9206 3074/* This defines a way to flush all output buffers. This may be a
76549fef
JH
3075 * performance issue, so we allow people to disable it. Also, if
3076 * we are using stdio, there are broken implementations of fflush(NULL)
3077 * out there, Solaris being the most prominent.
45bc9206
GS
3078 */
3079#ifndef PERL_FLUSHALL_FOR_CHILD
97cb92d6 3080# if defined(USE_PERLIO) || defined(FFLUSH_NULL)
66fe083f 3081# define PERL_FLUSHALL_FOR_CHILD PerlIO_flush((PerlIO*)NULL)
da5fdda8
AC
3082# elif defined(FFLUSH_ALL)
3083# define PERL_FLUSHALL_FOR_CHILD my_fflush_all()
767df6a1 3084# else
da5fdda8 3085# define PERL_FLUSHALL_FOR_CHILD NOOP
66fe083f 3086# endif
45bc9206
GS
3087#endif
3088
7766f137
GS
3089#ifndef PERL_WAIT_FOR_CHILDREN
3090# define PERL_WAIT_FOR_CHILDREN NOOP
3091#endif
3092
ba869deb 3093/* the traditional thread-unsafe notion of "current interpreter". */
c5be433b
GS
3094#ifndef PERL_SET_INTERP
3095# define PERL_SET_INTERP(i) (PL_curinterp = (PerlInterpreter*)(i))
3096#endif
3097
3098#ifndef PERL_GET_INTERP
3099# define PERL_GET_INTERP (PL_curinterp)
3100#endif
3101
54aff467 3102#if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_GET_THX)
54aff467 3103# ifdef MULTIPLICITY
ba869deb 3104# define PERL_GET_THX ((PerlInterpreter *)PERL_GET_CONTEXT)
54aff467 3105# endif
ba869deb
GS
3106# define PERL_SET_THX(t) PERL_SET_CONTEXT(t)
3107#endif
3108
00f254e2 3109/*
8896765a 3110 This replaces the previous %_ "hack" by the "%p" hacks.
0dbb1585 3111 All that is required is that the perl source does not
00f254e2
KW
3112 use "%-p" or "%-<number>p" or "%<number>p" formats.
3113 These formats will still work in perl code.
486ec47a 3114 See comments in sv.c for further details.
8896765a 3115
8896765a 3116 Robin Barker 2005-07-14
f46d31f2 3117
00f254e2 3118 No longer use %1p for VDf = %vd. RMB 2007-10-19
0dbb1585 3119*/
8896765a
RB
3120
3121#ifndef SVf_
63ce7bf0 3122# define SVf_(n) "-" STRINGIFY(n) "p"
894356b3
GS
3123#endif
3124
8896765a 3125#ifndef SVf
104abab4 3126# define SVf "-p"
d2560b70
RB
3127#endif
3128
014ead4b 3129#ifndef SVf32
8896765a 3130# define SVf32 SVf_(32)
014ead4b
RB
3131#endif
3132
3133#ifndef SVf256
8896765a
RB
3134# define SVf256 SVf_(256)
3135#endif
3136
be2597df
MHM
3137#define SVfARG(p) ((void*)(p))
3138
20023040
FC
3139#ifndef HEKf
3140# define HEKf "2p"
3141#endif
3142
b8fa5213
FC
3143/* Not ideal, but we cannot easily include a number in an already-numeric
3144 * format sequence. */
3145#ifndef HEKf256
3146# define HEKf256 "3p"
3147#endif
3148
20023040
FC
3149#define HEKfARG(p) ((void*)(p))
3150
b17a0679
FC
3151/* Takes three arguments: is_utf8, length, str */
3152#ifndef UTF8f
61608bb7 3153# define UTF8f "d%" UVuf "%4p"
b17a0679
FC
3154#endif
3155#define UTF8fARG(u,l,p) (int)cBOOL(u), (UV)(l), (void*)(p)
3156
0f94cb1f
FC
3157#define PNf UTF8f
3158#define PNfARG(pn) (int)1, (UV)PadnameLEN(pn), (void *)PadnamePV(pn)
0d1e9135 3159
f46d31f2 3160#ifdef PERL_CORE
486ec47a 3161/* not used; but needed for backward compatibility with XS code? - RMB */
7776bb98 3162# undef UVf
da5fdda8
AC
3163#elif !defined(UVf)
3164# define UVf UVuf
d2560b70
RB
3165#endif
3166
fcdf39cf
RGS
3167#ifdef HASATTRIBUTE_DEPRECATED
3168# define __attribute__deprecated__ __attribute__((deprecated))
3169#endif
0dbb1585
AL
3170#ifdef HASATTRIBUTE_FORMAT
3171# define __attribute__format__(x,y,z) __attribute__((format(x,y,z)))
3172#endif
3173#ifdef HASATTRIBUTE_MALLOC
77785eea 3174# define __attribute__malloc__ __attribute__((__malloc__))
f22be1b8 3175#endif
0dbb1585 3176#ifdef HASATTRIBUTE_NONNULL
abb2c242 3177# define __attribute__nonnull__(a) __attribute__((nonnull(a)))
f22be1b8 3178#endif
0dbb1585 3179#ifdef HASATTRIBUTE_NORETURN
abb2c242 3180# define __attribute__noreturn__ __attribute__((noreturn))
f22be1b8 3181#endif
0dbb1585 3182#ifdef HASATTRIBUTE_PURE
abb2c242 3183# define __attribute__pure__ __attribute__((pure))
f22be1b8 3184#endif
0dbb1585
AL
3185#ifdef HASATTRIBUTE_UNUSED
3186# define __attribute__unused__ __attribute__((unused))
3187#endif
3188#ifdef HASATTRIBUTE_WARN_UNUSED_RESULT
abb2c242 3189# define __attribute__warn_unused_result__ __attribute__((warn_unused_result))
f22be1b8
JH
3190#endif
3191
0dbb1585 3192/* If we haven't defined the attributes yet, define them to blank. */
fcdf39cf
RGS
3193#ifndef __attribute__deprecated__
3194# define __attribute__deprecated__
3195#endif
0dbb1585
AL
3196#ifndef __attribute__format__
3197# define __attribute__format__(x,y,z)
3198#endif
f22be1b8 3199#ifndef __attribute__malloc__
abb2c242 3200# define __attribute__malloc__
f22be1b8
JH
3201#endif
3202#ifndef __attribute__nonnull__
abb2c242 3203# define __attribute__nonnull__(a)
f22be1b8
JH
3204#endif
3205#ifndef __attribute__noreturn__
abb2c242 3206# define __attribute__noreturn__
f22be1b8
JH
3207#endif
3208#ifndef __attribute__pure__
abb2c242 3209# define __attribute__pure__
f22be1b8 3210#endif
0dbb1585
AL
3211#ifndef __attribute__unused__
3212# define __attribute__unused__
3213#endif
f22be1b8 3214#ifndef __attribute__warn_unused_result__
abb2c242
JH
3215# define __attribute__warn_unused_result__
3216#endif
3217
6ff2ec7d
AC
3218#if !defined(DEBUGGING) && !defined(NDEBUG)
3219# define NDEBUG 1
cd13e623 3220#endif
6ff2ec7d 3221#include <assert.h>
cd13e623 3222
0dbb1585
AL
3223/* For functions that are marked as __attribute__noreturn__, it's not
3224 appropriate to call return. In either case, include the lint directive.
3225 */
3226#ifdef HASATTRIBUTE_NORETURN
bc3d2941 3227# define NORETURN_FUNCTION_END NOT_REACHED;
0dbb1585 3228#else
bc3d2941 3229# define NORETURN_FUNCTION_END NOT_REACHED; return 0
abb2c242
JH
3230#endif
3231
cdfeb707
RB
3232/* Some OS warn on NULL format to printf */
3233#ifdef PRINTF_FORMAT_NULL_OK
3234# define __attribute__format__null_ok__(x,y,z) __attribute__format__(x,y,z)
3235#else
00f254e2 3236# define __attribute__format__null_ok__(x,y,z)
cdfeb707
RB
3237#endif
3238
635aebb7 3239#ifdef HAS_BUILTIN_EXPECT
b37c2d43
AL
3240# define EXPECT(expr,val) __builtin_expect(expr,val)
3241#else
3242# define EXPECT(expr,val) (expr)
3243#endif
6d5abc62
NC
3244#define LIKELY(cond) EXPECT(cBOOL(cond),TRUE)
3245#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
635aebb7
AL
3246#ifdef HAS_BUILTIN_CHOOSE_EXPR
3247/* placeholder */
3248#endif
b37c2d43 3249
a36ca6de 3250/* STATIC_ASSERT_DECL/STATIC_ASSERT_STMT are like assert(), but for compile
5074d4c5
LM
3251 time invariants. That is, their argument must be a constant expression that
3252 can be verified by the compiler. This expression can contain anything that's
3253 known to the compiler, e.g. #define constants, enums, or sizeof (...). If
3254 the expression evaluates to 0, compilation fails.
3255 Because they generate no runtime code (i.e. their use is "free"), they're
3256 always active, even under non-DEBUGGING builds.
a36ca6de 3257 STATIC_ASSERT_DECL expands to a declaration and is suitable for use at
5074d4c5
LM
3258 file scope (outside of any function).
3259 STATIC_ASSERT_STMT expands to a statement and is suitable for use inside a
3260 function.
3261*/
7ea096df 3262#if (defined(static_assert) || (defined(__cplusplus) && __cplusplus >= 201103L)) && (!defined(__IBMC__) || __IBMC__ >= 1210)
6d59e610 3263/* static_assert is a macro defined in <assert.h> in C11 or a compiler
a36ca6de
LM
3264 builtin in C++11. But IBM XL C V11 does not support _Static_assert, no
3265 matter what <assert.h> says.
6d59e610 3266*/
a36ca6de 3267# define STATIC_ASSERT_DECL(COND) static_assert(COND, #COND)
6d59e610
LM
3268#else
3269/* We use a bit-field instead of an array because gcc accepts
3270 'typedef char x[n]' where n is not a compile-time constant.
3271 We want to enforce constantness.
3272*/
3273# define STATIC_ASSERT_2(COND, SUFFIX) \
3274 typedef struct { \
3275 unsigned int _static_assertion_failed_##SUFFIX : (COND) ? 1 : -1; \
3276 } _static_assertion_failed_##SUFFIX PERL_UNUSED_DECL
3277# define STATIC_ASSERT_1(COND, SUFFIX) STATIC_ASSERT_2(COND, SUFFIX)
a36ca6de 3278# define STATIC_ASSERT_DECL(COND) STATIC_ASSERT_1(COND, __LINE__)
6d59e610
LM
3279#endif
3280/* We need this wrapper even in C11 because 'case X: static_assert(...);' is an
3281 error (static_assert is a declaration, and only statements can have labels).
3282*/
21aae866 3283#define STATIC_ASSERT_STMT(COND) STMT_START { STATIC_ASSERT_DECL(COND); } STMT_END
ae103e09
DD
3284
3285#ifndef __has_builtin
3286# define __has_builtin(x) 0 /* not a clang style compiler */
3287#endif
3288
3289/* ASSUME is like assert(), but it has a benefit in a release build. It is a
3290 hint to a compiler about a statement of fact in a function call free
3291 expression, which allows the compiler to generate better machine code.
3292 In a debug build, ASSUME(x) is a synonym for assert(x). ASSUME(0) means
3293 the control path is unreachable. In a for loop, ASSUME can be used to hint
b8fda935 3294 that a loop will run at least X times. ASSUME is based off MSVC's __assume
ae103e09
DD
3295 intrinsic function, see its documents for more details.
3296*/
3297
3298#ifndef DEBUGGING
3299# if __has_builtin(__builtin_unreachable) \
d1020b40 3300 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5 || __GNUC__ > 4) /* 4.5 -> */
ae103e09
DD
3301# define ASSUME(x) ((x) ? (void) 0 : __builtin_unreachable())
3302# elif defined(_MSC_VER)
3303# define ASSUME(x) __assume(x)
3304# elif defined(__ARMCC_VERSION) /* untested */
3305# define ASSUME(x) __promise(x)
3306# else
3307/* a random compiler might define assert to its own special optimization token
3308 so pass it through to C lib as a last resort */
3309# define ASSUME(x) assert(x)
3310# endif
3311#else
3312# define ASSUME(x) assert(x)
3313#endif
3314
dbc47c6f
KW
3315#if defined(__sun) /* ASSUME() generates warnings on Solaris */
3316# define NOT_REACHED
2145f4b6
LM
3317#elif defined(DEBUGGING) && (__has_builtin(__builtin_unreachable) \
3318 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5 || __GNUC__ > 4)) /* 4.5 -> */
3319# define NOT_REACHED STMT_START { ASSUME(0); __builtin_unreachable(); } STMT_END
dbc47c6f
KW
3320#else
3321# define NOT_REACHED ASSUME(0)
3322#endif
ae103e09 3323
3fc1aec6
PP
3324/* Some unistd.h's give a prototype for pause() even though
3325 HAS_PAUSE ends up undefined. This causes the #define
d2560b70 3326 below to be rejected by the compiler. Sigh.
3fc1aec6
PP
3327*/
3328#ifdef HAS_PAUSE
3329#define Pause pause
3330#else
3331#define Pause() sleep((32767<<16)+32767)
748a9306
LW
3332#endif
3333
3334#ifndef IOCPARM_LEN
3335# ifdef IOCPARM_MASK
61f1c44b 3336 /* on BSDish systems we're safe */
748a9306 3337# define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
da5fdda8 3338# elif defined(_IOC_SIZE) && defined(__GLIBC__)
301eb2f0 3339 /* on Linux systems we're safe; except when we're not [perl #38223] */
da5fdda8
AC
3340# define IOCPARM_LEN(x) (_IOC_SIZE(x) < 256 ? 256 : _IOC_SIZE(x))
3341# else
748a9306 3342 /* otherwise guess at what's safe */
da5fdda8 3343# define IOCPARM_LEN(x) 256
748a9306 3344# endif
a0d0e21e
LW
3345#endif
3346
1761cee5 3347#if defined(__CYGWIN__)
521e0776
GS
3348/* USEMYBINMODE
3349 * This symbol, if defined, indicates that the program should
16fe6d59 3350 * use the routine my_binmode(FILE *fp, char iotype, int mode) to insure
521e0776
GS
3351 * that a file is in "binary" mode -- that is, that no translation
3352 * of bytes occurs on read or write operations.
3353 */
36894f0b 3354# define USEMYBINMODE /**/
77a40698 3355# include <io.h> /* for setmode() prototype */
16fe6d59 3356# define my_binmode(fp, iotype, mode) \
8298454c 3357 cBOOL(PerlLIO_setmode(fileno(fp), mode) != -1)
5aabfad6
PP
3358#endif
3359
88d10794
JH
3360#ifdef __CYGWIN__
3361void init_os_extras(void);
3362#endif
3363
cea2e8a9
GS
3364#ifdef UNION_ANY_DEFINITION
3365UNION_ANY_DEFINITION;
3366#else
3367union any {
3368 void* any_ptr;
c9728782
DM
3369 SV* any_sv;
3370 SV** any_svp;
3371 GV* any_gv;
3372 AV* any_av;
3373 HV* any_hv;
6ffb8402 3374 OP* any_op;
c9728782
DM
3375 char* any_pv;
3376 char** any_pvp;
cea2e8a9 3377 I32 any_i32;
ae103e09 3378 U32 any_u32;
cea2e8a9 3379 IV any_iv;
c6bf6a65 3380 UV any_uv;
cea2e8a9 3381 long any_long;
9febdf04 3382 bool any_bool;
c76ac1ee 3383 void (*any_dptr) (void*);
acfe0abc 3384 void (*any_dxptr) (pTHX_ void*);
cea2e8a9
GS
3385};
3386#endif
3387
acfe0abc 3388typedef I32 (*filter_t) (pTHX_ int, SV *, int);
cea2e8a9
GS
3389
3390#define FILTER_READ(idx, sv, len) filter_read(idx, sv, len)
5486870f
DM
3391#define FILTER_DATA(idx) \
3392 (PL_parser ? AvARRAY(PL_parser->rsfp_filters)[idx] : NULL)
3393#define FILTER_ISREADER(idx) \
3394 (PL_parser && PL_parser->rsfp_filters \
3395 && idx >= AvFILLp(PL_parser->rsfp_filters))
3396#define PERL_FILTER_EXISTS(i) \
3397 (PL_parser && PL_parser->rsfp_filters \
b9f2b683 3398 && (i) <= av_tindex(PL_parser->rsfp_filters))
cea2e8a9 3399
c5078cac
JH
3400#if defined(_AIX) && !defined(_AIX43)
3401#if defined(USE_REENTRANT) || defined(_REENTRANT) || defined(_THREAD_SAFE)
3402/* We cannot include <crypt.h> to get the struct crypt_data
3403 * because of setkey prototype problems when threading */
3404typedef struct crypt_data { /* straight from /usr/include/crypt.h */
3405 /* From OSF, Not needed in AIX
3406 char C[28], D[28];
3407 */
3408 char E[48];
3409 char KS[16][48];
3410 char block[66];
3411 char iobuf[16];
3412} CRYPTD;
3413#endif /* threading */
3414#endif /* AIX */
3415
fc6bde6f
DD
3416#ifndef PERL_CALLCONV
3417# ifdef __cplusplus
3418# define PERL_CALLCONV extern "C"
3419# else
3420# define PERL_CALLCONV
3421# endif
3422#endif
3423#ifndef PERL_CALLCONV_NO_RET
3424# define PERL_CALLCONV_NO_RET PERL_CALLCONV
3425#endif
3426
3427/* PERL_STATIC_NO_RET is supposed to be equivalent to STATIC on builds that
3428 dont have a noreturn as a declaration specifier
3429*/
3430#ifndef PERL_STATIC_NO_RET
3431# define PERL_STATIC_NO_RET STATIC
3432#endif
3433/* PERL_STATIC_NO_RET is supposed to be equivalent to PERL_STATIC_INLINE on
3434 builds that dont have a noreturn as a declaration specifier
3435*/
3436#ifndef PERL_STATIC_INLINE_NO_RET
3437# define PERL_STATIC_INLINE_NO_RET PERL_STATIC_INLINE
3438#endif
3439
e37778c2 3440#if !defined(OS2)
2c2d71f5
JH
3441# include "iperlsys.h"
3442#endif
504f80c1 3443
9c12f1e5 3444#ifdef __LIBCATAMOUNT__
00f254e2 3445#undef HAS_PASSWD /* unixish.h but not unixish enough. */
9c12f1e5
RGS
3446#undef HAS_GROUP
3447#define FAKE_BIT_BUCKET
3448#endif
3449
d10dc2ae 3450/* [perl #22371] Algorimic Complexity Attack on Perl 5.6.1, 5.8.0.
1a237f4f
YO
3451 * Note that the USE_HASH_SEED and similar defines are *NOT* defined by
3452 * Configure, despite their names being similar to other defines like
3453 * USE_ITHREADS. Configure in fact knows nothing about the randomised
3454 * hashes. Therefore to enable/disable the hash randomisation defines
3455 * use the Configure -Accflags=... instead. */
3456#if !defined(NO_HASH_SEED) && !defined(USE_HASH_SEED)
4546b9e6 3457# define USE_HASH_SEED
504f80c1
JH
3458#endif
3459
598921a7
NC
3460#include "perly.h"
3461
598921a7 3462
654eccd5
JD
3463/* macros to define bit-fields in structs. */
3464#ifndef PERL_BITFIELD8
3465# define PERL_BITFIELD8 unsigned
3466#endif
3467#ifndef PERL_BITFIELD16
3468# define PERL_BITFIELD16 unsigned
3469#endif
3470#ifndef PERL_BITFIELD32
3471# define PERL_BITFIELD32 unsigned
3472#endif
3473
79072805 3474#include "sv.h"
288b8c02 3475#include "regexp.h"
378cc40b 3476#include "util.h"
8d063cd8 3477#include "form.h"
79072805 3478#include "gv.h"
dd2155a4 3479#include "pad.h"
79072805 3480#include "cv.h"
abdd5c84 3481#include "opnames.h"
79072805 3482#include "op.h"
b3ca2e83 3483#include "hv.h"
79072805
LW
3484#include "cop.h"
3485#include "av.h"
79072805
LW
3486#include "mg.h"
3487#include "scope.h"
4438c4b7 3488#include "warnings.h"
a0ed51b3 3489#include "utf8.h"
8d063cd8 3490
0d7d409d
DM
3491/* these would be in doio.h if there was such a file */
3492#define my_stat() my_stat_flags(SV_GMAGIC)
3493#define my_lstat() my_lstat_flags(SV_GMAGIC)
3494
20f4945e 3495/* defined in sv.c, but also used in [ach]v.c */
20f4945e
MHM
3496#undef _XPV_HEAD
3497#undef _XPVMG_HEAD
3498#undef _XPVCV_COMMON
7fae4e64 3499
199e78b7
DM
3500#include "parser.h"
3501
455ece5e 3502typedef struct magic_state MGS; /* struct magic_state defined in mg.c */
76e3520e 3503
2260ab51
KW
3504#if defined(PERL_IN_REGCOMP_C) || defined(PERL_IN_REGEXEC_C)
3505
3506/* These have to be predeclared, as they are used in proto.h which is #included
3507 * before their definitions in regcomp.h. */
3508
3509struct scan_data_t;
7dcac5f6
KW
3510typedef struct regnode_charclass regnode_charclass;
3511
2260ab51 3512struct regnode_charclass_class;
d1d040e5
KW
3513
3514/* A hopefully less confusing name. The sub-classes are all Posix classes only
3515 * used under /l matching */
3516typedef struct regnode_charclass_class regnode_charclass_posixl;
3517
b8f7bb16 3518typedef struct regnode_ssc regnode_ssc;
09b2b2e6
KW
3519typedef struct RExC_state_t RExC_state_t;
3520struct _reg_trie_data;
76e3520e 3521
2260ab51
KW
3522#endif
3523
5f7fde29
GS
3524struct ptr_tbl_ent {
3525 struct ptr_tbl_ent* next;
53c1dcc0 3526 const void* oldval;
5f7fde29 3527 void* newval;
d18c6117
GS
3528};
3529
5f7fde29
GS
3530struct ptr_tbl {
3531 struct ptr_tbl_ent** tbl_ary;
3532 UV tbl_max;
3533 UV tbl_items;
db93c0c4
NC
3534 struct ptr_tbl_arena *tbl_arena;
3535 struct ptr_tbl_ent *tbl_arena_next;
3536 struct ptr_tbl_ent *tbl_arena_end;
d18c6117
GS
3537};
3538
fe14fcc3
LW
3539#if defined(htonl) && !defined(HAS_HTONL)
3540#define HAS_HTONL
ae986130 3541#endif
fe14fcc3
LW
3542#if defined(htons) && !defined(HAS_HTONS)
3543#define HAS_HTONS
ae986130 3544#endif
fe14fcc3
LW
3545#if defined(ntohl) && !defined(HAS_NTOHL)
3546#define HAS_NTOHL
ae986130 3547#endif
fe14fcc3
LW
3548#if defined(ntohs) && !defined(HAS_NTOHS)
3549#define HAS_NTOHS
ae986130 3550#endif
fe14fcc3 3551#ifndef HAS_HTONL
fe14fcc3
LW
3552#define HAS_HTONS
3553#define HAS_HTONL
3554#define HAS_NTOHS
3555#define HAS_NTOHL
8e2d7c44
NC
3556# if (BYTEORDER & 0xffff) == 0x4321
3557/* Big endian system, so ntohl, ntohs, htonl and htons do not need to
3558 re-order their values. However, to behave identically to the alternative
3559 implementations, they should truncate to the correct size. */
3560# define ntohl(x) ((x)&0xFFFFFFFF)
3561# define htonl(x) ntohl(x)
3562# define ntohs(x) ((x)&0xFFFF)
3563# define htons(x) ntohs(x)
e4b03fed
NC
3564# elif BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
3565
3566/* Note that we can't straight out declare our own htonl and htons because
3567 the Win32 build process forcibly undefines HAS_HTONL etc for its miniperl,
3568 to avoid the overhead of initialising the socket subsystem, but the headers
3569 that *declare* the various functions are still seen. If we declare our own
3570 htonl etc they will clash with the declarations in the Win32 headers. */
3571
3572PERL_STATIC_INLINE U32
3573my_swap32(const U32 x) {
3574 return ((x & 0xFF) << 24) | ((x >> 24) & 0xFF)
3575 | ((x & 0x0000FF00) << 8) | ((x & 0x00FF0000) >> 8);
3576}
3577
3578PERL_STATIC_INLINE U16
3579my_swap16(const U16 x) {
3580 return ((x & 0xFF) << 8) | ((x >> 8) & 0xFF);
3581}
3582
3583# define htonl(x) my_swap32(x)
3584# define ntohl(x) my_swap32(x)
3585# define ntohs(x) my_swap16(x)
3586# define htons(x) my_swap16(x)
8e2d7c44 3587# else
691a44df
NC
3588# error "Unsupported byteorder"
3589/* The C pre-processor doesn't let us return the value of BYTEORDER as part of
3590 the error message. Please check the value of the macro BYTEORDER, as defined
3591 in config.h. The values of BYTEORDER we expect are
3592
3593 big endian little endian
3594 32 bit 0x4321 0x1234
3595 64 bit 0x87654321 0x12345678
3596
3597 If you have a system with a different byte order, please see
3598 pod/perlhack.pod for how to submit a patch to add supporting code.
3599*/
3600# endif
a687059c
LW
3601#endif
3602
988174c1
LW
3603/*
3604 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
3605 * -DWS
3606 */
5ae72479 3607#if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
260c6fee
NC
3608/* Little endian system, so vtohl, vtohs, htovl and htovs do not need to
3609 re-order their values. However, to behave identically to the alternative
3610 implementations, they should truncate to the correct size. */
3611# define vtohl(x) ((x)&0xFFFFFFFF)
3612# define vtohs(x) ((x)&0xFFFF)
3613# define htovl(x) vtohl(x)
3614# define htovs(x) vtohs(x)
545872c5 3615#elif BYTEORDER == 0x4321 || BYTEORDER == 0x87654321
988174c1
LW
3616# define vtohl(x) ((((x)&0xFF)<<24) \
3617 +(((x)>>24)&0xFF) \
3618 +(((x)&0x0000FF00)<<8) \
3619 +(((x)&0x00FF0000)>>8) )
3620# define vtohs(x) ((((x)&0xFF)<<8) + (((x)>>8)&0xFF))
3621# define htovl(x) vtohl(x)
3622# define htovs(x) vtohs(x)
545872c5
NC
3623#else
3624# error "Unsupported byteorder"
3625/* If you have need for current perl on PDP-11 or similar, and can help test
3626 that blead keeps working on a mixed-endian system, then see
3627 pod/perlhack.pod for how to submit patches to things working again. */
2a8c3029 3628#endif
988174c1 3629
3bb7c1b4
JH
3630/* *MAX Plus 1. A floating point value.
3631 Hopefully expressed in a way that dodgy floating point can't mess up.
3632 >> 2 rather than 1, so that value is safely less than I32_MAX after 1
3633 is added to it
3634 May find that some broken compiler will want the value cast to I32.
3635 [after the shift, as signed >> may not be as secure as unsigned >>]
3636*/
3637#define I32_MAX_P1 (2.0 * (1 + (((U32)I32_MAX) >> 1)))
3638#define U32_MAX_P1 (4.0 * (1 + ((U32_MAX) >> 2)))
3639/* For compilers that can't correctly cast NVs over 0x7FFFFFFF (or
3640 0x7FFFFFFFFFFFFFFF) to an unsigned integer. In the future, sizeof(UV)
3641 may be greater than sizeof(IV), so don't assume that half max UV is max IV.
3642*/
3643#define U32_MAX_P1_HALF (2.0 * (1 + ((U32_MAX) >> 2)))
ee0007ab 3644
3bb7c1b4
JH
3645#define UV_MAX_P1 (4.0 * (1 + ((UV_MAX) >> 2)))
3646#define IV_MAX_P1 (2.0 * (1 + (((UV)IV_MAX) >> 1)))
3647#define UV_MAX_P1_HALF (2.0 * (1 + ((UV_MAX) >> 2)))
3648
3649/* This may look like unnecessary jumping through hoops, but converting
3650 out of range floating point values to integers *is* undefined behaviour,
3651 and it is starting to bite.
3652*/
3653#ifndef CAST_INLINE
65202027 3654#define I_32(what) (cast_i32((NV)(what)))
3bb7c1b4 3655#define U_32(what) (cast_ulong((NV)(what)))
65202027
DS
3656#define I_V(what) (cast_iv((NV)(what)))
3657#define U_V(what) (cast_uv((NV)(what)))
3bb7c1b4
JH
3658#else
3659#define I_32(n) ((n) < I32_MAX_P1 ? ((n) < I32_MIN ? I32_MIN : (I32) (n)) \
3660 : ((n) < U32_MAX_P1 ? (I32)(U32) (n) \
3661 : ((n) > 0 ? (I32) U32_MAX : 0 /* NaN */)))
3662#define U_32(n) ((n) < 0.0 ? ((n) < I32_MIN ? (UV) I32_MIN : (U32)(I32) (n)) \
3663 : ((n) < U32_MAX_P1 ? (U32) (n) \
3664 : ((n) > 0 ? U32_MAX : 0 /* NaN */)))
5d9574c1
DM
3665#define I_V(n) (LIKELY((n) < IV_MAX_P1) ? (UNLIKELY((n) < IV_MIN) ? IV_MIN : (IV) (n)) \
3666 : (LIKELY((n) < UV_MAX_P1) ? (IV)(UV) (n) \
3bb7c1b4 3667 : ((n) > 0 ? (IV)UV_MAX : 0 /* NaN */)))
5d9574c1
DM
3668#define U_V(n) ((n) < 0.0 ? (UNLIKELY((n) < IV_MIN) ? (UV) IV_MIN : (UV)(IV) (n)) \
3669 : (LIKELY((n) < UV_MAX_P1) ? (UV) (n) \
3bb7c1b4
JH
3670 : ((n) > 0 ? UV_MAX : 0 /* NaN */)))
3671#endif
3672
3673#define U_S(what) ((U16)U_32(what))
3674#define U_I(what) ((unsigned int)U_32(what))
3675#define U_L(what) U_32(what)
ed6116ce 3676
ed140128
AD
3677#ifdef HAS_SIGNBIT
3678# define Perl_signbit signbit
3679#endif
3680
2d4389e4
JH
3681/* These do not care about the fractional part, only about the range. */
3682#define NV_WITHIN_IV(nv) (I_V(nv) >= IV_MIN && I_V(nv) <= IV_MAX)
24db6c0d 3683#define NV_WITHIN_UV(nv) ((nv)>=0.0 && U_V(nv) >= UV_MIN && U_V(nv) <= UV_MAX)
2d4389e4 3684
25da4f38
IZ
3685/* Used with UV/IV arguments: */
3686 /* XXXX: need to speed it up */
3687#define CLUMP_2UV(iv) ((iv) < 0 ? 0 : (UV)(iv))
3688#define CLUMP_2IV(uv) ((uv) > (UV)IV_MAX ? IV_MAX : (IV)(uv))
3689
352d5a3a
LW
3690#ifndef MAXSYSFD
3691# define MAXSYSFD 2
3692#endif
ee0007ab 3693
79072805 3694#ifndef __cplusplus