added patch, fixed typo, reworked documentation
[p5sagit/p5-mst-13.2.git] / perl.h
1 /*    perl.h
2  *
3  *    Copyright (c) 1987-1997, Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9 #ifndef H_PERL
10 #define H_PERL 1
11 #define OVERLOAD
12
13 #ifdef PERL_FOR_X2P
14 /*
15  * This file is being used for x2p stuff. 
16  * Above symbol is defined via -D in 'x2p/Makefile.SH'
17  * Decouple x2p stuff from some of perls more extreme eccentricities. 
18  */
19 #undef EMBED
20 #undef NO_EMBED
21 #define NO_EMBED
22 #undef MULTIPLICITY
23 #undef USE_STDIO
24 #define USE_STDIO
25 #endif /* PERL_FOR_X2P */
26
27 #ifdef PERL_OBJECT
28
29 /* PERL_OBJECT explained  - DickH and DougL @ ActiveState.com
30
31 Defining PERL_OBJECT turns on creation of a C++ object that
32 contains all writable core perl global variables and functions.
33 Stated another way, all necessary global variables and functions
34 are members of a big C++ object. This object's class is CPerlObj.
35 This allows a Perl Host to have multiple, independent perl
36 interpreters in the same process space. This is very important on
37 Win32 systems as the overhead of process creation is quite high --
38 this could be even higher than the script compile and execute time
39 for small scripts.
40
41 The perl executable implementation on Win32 is composed of perl.exe
42 (the Perl Host) and perlX.dll. (the Perl Core). This allows the
43 same Perl Core to easily be embedded in other applications that use
44 the perl interpreter.
45
46 +-----------+
47 | Perl Host |
48 +-----------+
49       ^
50           |
51           v
52 +-----------+   +-----------+
53 | Perl Core |<->| Extension |
54 +-----------+   +-----------+ ...
55
56 Defining PERL_OBJECT has the following effects:
57
58 PERL CORE
59 1. CPerlObj is defined (this is the PERL_OBJECT)
60 2. all static functions that needed to access either global
61 variables or functions needed are made member functions
62 3. all writable static variables are made member variables
63 4. all global variables and functions are defined as:
64         #define var CPerlObj::Perl_var
65         #define func CPerlObj::Perl_func
66         * these are in objpp.h
67 This necessitated renaming some local variables and functions that
68 had the same name as a global variable or function. This was
69 probably a _good_ thing anyway.
70
71
72 EXTENSIONS
73 1. Access to global variables and perl functions is through a
74 pointer to the PERL_OBJECT. This pointer type is CPerlObj*. This is
75 made transparent to extension developers by the following macros:
76         #define var pPerl->Perl_var
77         #define func pPerl->Perl_func
78         * these are done in ObjXSub.h
79 This requires that the extension be compiled as C++, which means
80 that the code must be ANSI C and not K&R C. For K&R extensions,
81 please see the C API notes located in Win32/GenCAPI.pl. This script
82 creates a PerlCAPI.lib that provides a K & R compatible C interface
83 to the PERL_OBJECT.
84 2. Local variables and functions cannot have the same name as perl's
85 variables or functions since the macros will redefine these. Look for
86 this if you get some strange error message and it does not look like
87 the code that you had written. This often happens with variables that
88 are local to a function.
89
90 PERL HOST
91 1. The perl host is linked with perlX.lib to get perl_alloc. This
92 function will return a pointer to CPerlObj (the PERL_OBJECT). It
93 takes pointers to the various PerlXXX_YYY interfaces (see ipdir.h for
94 information on this).
95 2. The perl host calls the same functions as normally would be
96 called in setting up and running a perl script, except that the
97 functions are now member functions of the PERL_OBJECT.
98
99 */
100
101
102 class CPerlObj;
103
104 #define STATIC
105 #define CPERLscope(x) CPerlObj::x
106 #define CPERLproto CPerlObj *
107 #define CPERLproto_ CPERLproto,
108 #define _CPERLproto ,CPERLproto
109 #define CPERLarg CPerlObj *pPerl
110 #define CPERLarg_ CPERLarg,
111 #define _CPERLarg ,CPERLarg
112 #define PERL_OBJECT_THIS this
113 #define _PERL_OBJECT_THIS ,this
114 #define PERL_OBJECT_THIS_ this,
115 #define CALLRUNOPS (this->*runops)
116
117 #else /* !PERL_OBJECT */
118
119 #define STATIC static
120 #define CPERLscope(x) x
121 #define CPERLproto
122 #define CPERLproto_
123 #define _CPERLproto
124 #define CPERLarg void
125 #define CPERLarg_
126 #define _CPERLarg
127 #define PERL_OBJECT_THIS
128 #define _PERL_OBJECT_THIS
129 #define PERL_OBJECT_THIS_
130 #define CALLRUNOPS runops
131
132 #endif /* PERL_OBJECT */
133
134 #define VOIDUSED 1
135 #include "config.h"
136
137 #include "embed.h"
138
139 #undef START_EXTERN_C
140 #undef END_EXTERN_C
141 #undef EXTERN_C
142 #ifdef __cplusplus
143 #  define START_EXTERN_C extern "C" {
144 #  define END_EXTERN_C }
145 #  define EXTERN_C extern "C"
146 #else
147 #  define START_EXTERN_C 
148 #  define END_EXTERN_C 
149 #  define EXTERN_C
150 #endif
151
152 #ifdef OP_IN_REGISTER
153 #  ifdef __GNUC__
154 #    define stringify_immed(s) #s
155 #    define stringify(s) stringify_immed(s)
156 #ifdef EMBED
157 register struct op *Perl_op asm(stringify(OP_IN_REGISTER));
158 #else
159 register struct op *op asm(stringify(OP_IN_REGISTER));
160 #endif
161 #  endif
162 #endif
163
164 /*
165  * STMT_START { statements; } STMT_END;
166  * can be used as a single statement, as in
167  * if (x) STMT_START { ... } STMT_END; else ...
168  *
169  * Trying to select a version that gives no warnings...
170  */
171 #if !(defined(STMT_START) && defined(STMT_END))
172 # if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(__cplusplus)
173 #   define STMT_START   (void)( /* gcc supports ``({ STATEMENTS; })'' */
174 #   define STMT_END     )
175 # else
176    /* Now which other defined()s do we need here ??? */
177 #  if (VOIDFLAGS) && (defined(sun) || defined(__sun__))
178 #   define STMT_START   if (1)
179 #   define STMT_END     else (void)0
180 #  else
181 #   define STMT_START   do
182 #   define STMT_END     while (0)
183 #  endif
184 # endif
185 #endif
186
187 #define NOOP (void)0
188
189 #define WITH_THR(s) STMT_START { dTHR; s; } STMT_END
190
191 /*
192  * SOFT_CAST can be used for args to prototyped functions to retain some
193  * type checking; it only casts if the compiler does not know prototypes.
194  */
195 #if defined(CAN_PROTOTYPE) && defined(DEBUGGING_COMPILE)
196 #define SOFT_CAST(type) 
197 #else
198 #define SOFT_CAST(type) (type)
199 #endif
200
201 #ifndef BYTEORDER  /* Should never happen -- byteorder is in config.h */
202 #   define BYTEORDER 0x1234
203 #endif
204
205 /* Overall memory policy? */
206 #ifndef CONSERVATIVE
207 #   define LIBERAL 1
208 #endif
209
210 /*
211  * The following contortions are brought to you on behalf of all the
212  * standards, semi-standards, de facto standards, not-so-de-facto standards
213  * of the world, as well as all the other botches anyone ever thought of.
214  * The basic theory is that if we work hard enough here, the rest of the
215  * code can be a lot prettier.  Well, so much for theory.  Sorry, Henry...
216  */
217
218 /* define this once if either system, instead of cluttering up the src */
219 #if defined(MSDOS) || defined(atarist) || defined(WIN32)
220 #define DOSISH 1
221 #endif
222
223 #if defined(__STDC__) || defined(vax11c) || defined(_AIX) || defined(__stdc__) || defined(__cplusplus)
224 # define STANDARD_C 1
225 #endif
226
227 #if defined(__cplusplus) || defined(WIN32) || defined(__sgi) || defined(OS2) || defined(__DGUX)
228 # define DONT_DECLARE_STD 1
229 #endif
230
231 #if defined(HASVOLATILE) || defined(STANDARD_C)
232 #   ifdef __cplusplus
233 #       define VOL              // to temporarily suppress warnings
234 #   else
235 #       define VOL volatile
236 #   endif
237 #else
238 #   define VOL
239 #endif
240
241 #define TAINT           (tainted = TRUE)
242 #define TAINT_NOT       (tainted = FALSE)
243 #define TAINT_IF(c)     if (c) { tainted = TRUE; }
244 #define TAINT_ENV()     if (tainting) { taint_env(); }
245 #define TAINT_PROPER(s) if (tainting) { taint_proper(no_security, s); }
246
247 /* XXX All process group stuff is handled in pp_sys.c.  Should these 
248    defines move there?  If so, I could simplify this a lot. --AD  9/96.
249 */
250 /* Process group stuff changed from traditional BSD to POSIX.
251    perlfunc.pod documents the traditional BSD-style syntax, so we'll
252    try to preserve that, if possible.
253 */
254 #ifdef HAS_SETPGID
255 #  define BSD_SETPGRP(pid, pgrp)        setpgid((pid), (pgrp))
256 #else
257 #  if defined(HAS_SETPGRP) && defined(USE_BSD_SETPGRP)
258 #    define BSD_SETPGRP(pid, pgrp)      setpgrp((pid), (pgrp))
259 #  else
260 #    ifdef HAS_SETPGRP2  /* DG/UX */
261 #      define BSD_SETPGRP(pid, pgrp)    setpgrp2((pid), (pgrp))
262 #    endif
263 #  endif
264 #endif
265 #if defined(BSD_SETPGRP) && !defined(HAS_SETPGRP)
266 #  define HAS_SETPGRP  /* Well, effectively it does . . . */
267 #endif
268
269 /* getpgid isn't POSIX, but at least Solaris and Linux have it, and it makes
270     our life easier :-) so we'll try it.
271 */
272 #ifdef HAS_GETPGID
273 #  define BSD_GETPGRP(pid)              getpgid((pid))
274 #else
275 #  if defined(HAS_GETPGRP) && defined(USE_BSD_GETPGRP)
276 #    define BSD_GETPGRP(pid)            getpgrp((pid))
277 #  else
278 #    ifdef HAS_GETPGRP2  /* DG/UX */
279 #      define BSD_GETPGRP(pid)          getpgrp2((pid))
280 #    endif
281 #  endif
282 #endif
283 #if defined(BSD_GETPGRP) && !defined(HAS_GETPGRP)
284 #  define HAS_GETPGRP  /* Well, effectively it does . . . */
285 #endif
286
287 /* These are not exact synonyms, since setpgrp() and getpgrp() may 
288    have different behaviors, but perl.h used to define USE_BSDPGRP
289    (prior to 5.003_05) so some extension might depend on it.
290 */
291 #if defined(USE_BSD_SETPGRP) || defined(USE_BSD_GETPGRP)
292 #  ifndef USE_BSDPGRP
293 #    define USE_BSDPGRP
294 #  endif
295 #endif
296
297 #ifndef _TYPES_         /* If types.h defines this it's easy. */
298 #   ifndef major                /* Does everyone's types.h define this? */
299 #       include <sys/types.h>
300 #   endif
301 #endif
302
303 #ifdef __cplusplus
304 #  ifndef I_STDARG
305 #    define I_STDARG 1
306 #  endif
307 #endif
308
309 #ifdef I_STDARG
310 #  include <stdarg.h>
311 #else
312 #  ifdef I_VARARGS
313 #    include <varargs.h>
314 #  endif
315 #endif
316
317 #include "perlio.h"
318 #include "perlmem.h"
319 #include "perllio.h"
320 #include "perlsock.h"
321 #include "perlproc.h"
322 #include "perlenv.h"
323 #include "perldir.h"
324
325 #ifdef USE_NEXT_CTYPE
326
327 #if NX_CURRENT_COMPILER_RELEASE >= 400
328 #include <objc/NXCType.h>
329 #else /*  NX_CURRENT_COMPILER_RELEASE < 400 */
330 #include <appkit/NXCType.h>
331 #endif /*  NX_CURRENT_COMPILER_RELEASE >= 400 */
332
333 #else /* !USE_NEXT_CTYPE */
334 #include <ctype.h>
335 #endif /* USE_NEXT_CTYPE */
336
337 #ifdef METHOD   /* Defined by OSF/1 v3.0 by ctype.h */
338 #undef METHOD
339 #endif
340
341 #ifdef I_LOCALE
342 #   include <locale.h>
343 #endif
344
345 #if !defined(NO_LOCALE) && defined(HAS_SETLOCALE)
346 #   define USE_LOCALE
347 #   if !defined(NO_LOCALE_COLLATE) && defined(LC_COLLATE) \
348        && defined(HAS_STRXFRM)
349 #       define USE_LOCALE_COLLATE
350 #   endif
351 #   if !defined(NO_LOCALE_CTYPE) && defined(LC_CTYPE)
352 #       define USE_LOCALE_CTYPE
353 #   endif
354 #   if !defined(NO_LOCALE_NUMERIC) && defined(LC_NUMERIC)
355 #       define USE_LOCALE_NUMERIC
356 #   endif
357 #endif /* !NO_LOCALE && HAS_SETLOCALE */
358
359 #include <setjmp.h>
360
361 #ifdef I_SYS_PARAM
362 #   ifdef PARAM_NEEDS_TYPES
363 #       include <sys/types.h>
364 #   endif
365 #   include <sys/param.h>
366 #endif
367
368
369 /* Use all the "standard" definitions? */
370 #if defined(STANDARD_C) && defined(I_STDLIB)
371 #   include <stdlib.h>
372 #endif
373
374 #define MEM_SIZE Size_t
375
376 /* This comes after <stdlib.h> so we don't try to change the standard
377  * library prototypes; we'll use our own in proto.h instead. */
378
379 #ifdef MYMALLOC
380
381 #   ifdef HIDEMYMALLOC
382 #       define malloc  Mymalloc
383 #       define calloc  Mycalloc
384 #       define realloc Myremalloc
385 #       define free    Myfree
386 Malloc_t Mymalloc _((MEM_SIZE nbytes));
387 Malloc_t Mycalloc _((MEM_SIZE elements, MEM_SIZE size));
388 Malloc_t Myrealloc _((Malloc_t where, MEM_SIZE nbytes));
389 Free_t   Myfree _((Malloc_t where));
390 #   endif
391 #   ifdef EMBEDMYMALLOC
392 #       define malloc  Perl_malloc
393 #       define calloc  Perl_calloc
394 #       define realloc Perl_realloc
395 #       define free    Perl_free
396 Malloc_t Perl_malloc _((MEM_SIZE nbytes));
397 Malloc_t Perl_calloc _((MEM_SIZE elements, MEM_SIZE size));
398 Malloc_t Perl_realloc _((Malloc_t where, MEM_SIZE nbytes));
399 Free_t   Perl_free _((Malloc_t where));
400 #   endif
401
402 #   undef safemalloc
403 #   undef safecalloc
404 #   undef saferealloc
405 #   undef safefree
406 #   define safemalloc  malloc
407 #   define safecalloc  calloc
408 #   define saferealloc realloc
409 #   define safefree    free
410
411 #endif /* MYMALLOC */
412
413 #if defined(STANDARD_C) && defined(I_STDDEF)
414 #   include <stddef.h>
415 #   define STRUCT_OFFSET(s,m)  offsetof(s,m)
416 #else
417 #   define STRUCT_OFFSET(s,m)  (Size_t)(&(((s *)0)->m))
418 #endif
419
420 #if defined(I_STRING) || defined(__cplusplus)
421 #   include <string.h>
422 #else
423 #   include <strings.h>
424 #endif
425
426 #if !defined(HAS_STRCHR) && defined(HAS_INDEX) && !defined(strchr)
427 #define strchr index
428 #define strrchr rindex
429 #endif
430
431 #ifdef I_MEMORY
432 #  include <memory.h>
433 #endif
434
435 #ifdef HAS_MEMCPY
436 #  if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY)
437 #    ifndef memcpy
438         extern char * memcpy _((char*, char*, int));
439 #    endif
440 #  endif
441 #else
442 #   ifndef memcpy
443 #       ifdef HAS_BCOPY
444 #           define memcpy(d,s,l) bcopy(s,d,l)
445 #       else
446 #           define memcpy(d,s,l) my_bcopy(s,d,l)
447 #       endif
448 #   endif
449 #endif /* HAS_MEMCPY */
450
451 #ifdef HAS_MEMSET
452 #  if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY)
453 #    ifndef memset
454         extern char *memset _((char*, int, int));
455 #    endif
456 #  endif
457 #else
458 #  define memset(d,c,l) my_memset(d,c,l)
459 #endif /* HAS_MEMSET */
460
461 #if !defined(HAS_MEMMOVE) && !defined(memmove)
462 #   if defined(HAS_BCOPY) && defined(HAS_SAFE_BCOPY)
463 #       define memmove(d,s,l) bcopy(s,d,l)
464 #   else
465 #       if defined(HAS_MEMCPY) && defined(HAS_SAFE_MEMCPY)
466 #           define memmove(d,s,l) memcpy(d,s,l)
467 #       else
468 #           define memmove(d,s,l) my_bcopy(s,d,l)
469 #       endif
470 #   endif
471 #endif
472
473 #if defined(mips) && defined(ultrix) && !defined(__STDC__)
474 #   undef HAS_MEMCMP
475 #endif
476
477 #if defined(HAS_MEMCMP) && defined(HAS_SANE_MEMCMP)
478 #  if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY)
479 #    ifndef memcmp
480         extern int memcmp _((char*, char*, int));
481 #    endif
482 #  endif
483 #  ifdef BUGGY_MSC
484   #  pragma function(memcmp)
485 #  endif
486 #else
487 #   ifndef memcmp
488 #       define memcmp   my_memcmp
489 #   endif
490 #endif /* HAS_MEMCMP && HAS_SANE_MEMCMP */
491
492 #ifndef memzero
493 #   ifdef HAS_MEMSET
494 #       define memzero(d,l) memset(d,0,l)
495 #   else
496 #       ifdef HAS_BZERO
497 #           define memzero(d,l) bzero(d,l)
498 #       else
499 #           define memzero(d,l) my_bzero(d,l)
500 #       endif
501 #   endif
502 #endif
503
504 #ifndef HAS_BCMP
505 #   ifndef bcmp
506 #       define bcmp(s1,s2,l) memcmp(s1,s2,l)
507 #   endif
508 #endif /* !HAS_BCMP */
509
510 #ifdef I_NETINET_IN
511 #   include <netinet/in.h>
512 #endif
513
514 #if defined(SF_APPEND) && defined(USE_SFIO) && defined(I_SFIO)
515 /* <sfio.h> defines SF_APPEND and <sys/stat.h> might define SF_APPEND
516  * (the neo-BSD seem to do this).  */
517 #   undef SF_APPEND
518 #endif
519
520 #ifdef I_SYS_STAT
521 #   include <sys/stat.h>
522 #endif
523
524 /* The stat macros for Amdahl UTS, Unisoft System V/88 (and derivatives
525    like UTekV) are broken, sometimes giving false positives.  Undefine
526    them here and let the code below set them to proper values.
527
528    The ghs macro stands for GreenHills Software C-1.8.5 which
529    is the C compiler for sysV88 and the various derivatives.
530    This header file bug is corrected in gcc-2.5.8 and later versions.
531    --Kaveh Ghazi (ghazi@noc.rutgers.edu) 10/3/94.  */
532
533 #if defined(uts) || (defined(m88k) && defined(ghs))
534 #   undef S_ISDIR
535 #   undef S_ISCHR
536 #   undef S_ISBLK
537 #   undef S_ISREG
538 #   undef S_ISFIFO
539 #   undef S_ISLNK
540 #endif
541
542 #ifdef I_TIME
543 #   include <time.h>
544 #endif
545
546 #ifdef I_SYS_TIME
547 #   ifdef I_SYS_TIME_KERNEL
548 #       define KERNEL
549 #   endif
550 #   include <sys/time.h>
551 #   ifdef I_SYS_TIME_KERNEL
552 #       undef KERNEL
553 #   endif
554 #endif
555
556 #if defined(HAS_TIMES) && defined(I_SYS_TIMES)
557 #    include <sys/times.h>
558 #endif
559
560 #if defined(HAS_STRERROR) && (!defined(HAS_MKDIR) || !defined(HAS_RMDIR))
561 #   undef HAS_STRERROR
562 #endif
563
564 #ifndef HAS_MKFIFO
565 #  ifndef mkfifo
566 #    define mkfifo(path, mode) (mknod((path), (mode) | S_IFIFO, 0))
567 #  endif
568 #endif /* !HAS_MKFIFO */
569
570 #include <errno.h>
571 #ifdef HAS_SOCKET
572 #   ifdef I_NET_ERRNO
573 #     include <net/errno.h>
574 #   endif
575 #endif
576
577 #ifdef VMS
578 #   define SETERRNO(errcode,vmserrcode) \
579         STMT_START {                    \
580             set_errno(errcode);         \
581             set_vaxc_errno(vmserrcode); \
582         } STMT_END
583 #else
584 #   define SETERRNO(errcode,vmserrcode) errno = (errcode)
585 #endif
586
587 #ifdef USE_THREADS
588 #  define ERRSV (thr->errsv)
589 #  define ERRHV (thr->errhv)
590 #  define DEFSV THREADSV(0)
591 #  define SAVE_DEFSV save_threadsv(0)
592 #else
593 #  define ERRSV GvSV(errgv)
594 #  define ERRHV GvHV(errgv)
595 #  define DEFSV GvSV(defgv)
596 #  define SAVE_DEFSV SAVESPTR(GvSV(defgv))
597 #endif /* USE_THREADS */
598
599 #ifndef errno
600         extern int errno;     /* ANSI allows errno to be an lvalue expr */
601 #endif
602
603 #ifdef HAS_STRERROR
604 #       ifdef VMS
605         char *strerror _((int,...));
606 #       else
607 #ifndef DONT_DECLARE_STD
608         char *strerror _((int));
609 #endif
610 #       endif
611 #       ifndef Strerror
612 #           define Strerror strerror
613 #       endif
614 #else
615 #    ifdef HAS_SYS_ERRLIST
616         extern int sys_nerr;
617         extern char *sys_errlist[];
618 #       ifndef Strerror
619 #           define Strerror(e) \
620                 ((e) < 0 || (e) >= sys_nerr ? "(unknown)" : sys_errlist[e])
621 #       endif
622 #   endif
623 #endif
624
625 #ifdef I_SYS_IOCTL
626 #   ifndef _IOCTL_
627 #       include <sys/ioctl.h>
628 #   endif
629 #endif
630
631 #if defined(mc300) || defined(mc500) || defined(mc700) || defined(mc6000)
632 #   ifdef HAS_SOCKETPAIR
633 #       undef HAS_SOCKETPAIR
634 #   endif
635 #   ifdef I_NDBM
636 #       undef I_NDBM
637 #   endif
638 #endif
639
640 #if INTSIZE == 2
641 #   define htoni htons
642 #   define ntohi ntohs
643 #else
644 #   define htoni htonl
645 #   define ntohi ntohl
646 #endif
647
648 /* Configure already sets Direntry_t */
649 #if defined(I_DIRENT)
650 #   include <dirent.h>
651 #   if defined(NeXT) && defined(I_SYS_DIR) /* NeXT needs dirent + sys/dir.h */
652 #       include <sys/dir.h>
653 #   endif
654 #else
655 #   ifdef I_SYS_NDIR
656 #       include <sys/ndir.h>
657 #   else
658 #       ifdef I_SYS_DIR
659 #           ifdef hp9000s500
660 #               include <ndir.h>        /* may be wrong in the future */
661 #           else
662 #               include <sys/dir.h>
663 #           endif
664 #       endif
665 #   endif
666 #endif
667
668 #ifdef FPUTS_BOTCH
669 /* work around botch in SunOS 4.0.1 and 4.0.2 */
670 #   ifndef fputs
671 #       define fputs(sv,fp) fprintf(fp,"%s",sv)
672 #   endif
673 #endif
674
675 /*
676  * The following gobbledygook brought to you on behalf of __STDC__.
677  * (I could just use #ifndef __STDC__, but this is more bulletproof
678  * in the face of half-implementations.)
679  */
680
681 #ifndef S_IFMT
682 #   ifdef _S_IFMT
683 #       define S_IFMT _S_IFMT
684 #   else
685 #       define S_IFMT 0170000
686 #   endif
687 #endif
688
689 #ifndef S_ISDIR
690 #   define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
691 #endif
692
693 #ifndef S_ISCHR
694 #   define S_ISCHR(m) ((m & S_IFMT) == S_IFCHR)
695 #endif
696
697 #ifndef S_ISBLK
698 #   ifdef S_IFBLK
699 #       define S_ISBLK(m) ((m & S_IFMT) == S_IFBLK)
700 #   else
701 #       define S_ISBLK(m) (0)
702 #   endif
703 #endif
704
705 #ifndef S_ISREG
706 #   define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
707 #endif
708
709 #ifndef S_ISFIFO
710 #   ifdef S_IFIFO
711 #       define S_ISFIFO(m) ((m & S_IFMT) == S_IFIFO)
712 #   else
713 #       define S_ISFIFO(m) (0)
714 #   endif
715 #endif
716
717 #ifndef S_ISLNK
718 #   ifdef _S_ISLNK
719 #       define S_ISLNK(m) _S_ISLNK(m)
720 #   else
721 #       ifdef _S_IFLNK
722 #           define S_ISLNK(m) ((m & S_IFMT) == _S_IFLNK)
723 #       else
724 #           ifdef S_IFLNK
725 #               define S_ISLNK(m) ((m & S_IFMT) == S_IFLNK)
726 #           else
727 #               define S_ISLNK(m) (0)
728 #           endif
729 #       endif
730 #   endif
731 #endif
732
733 #ifndef S_ISSOCK
734 #   ifdef _S_ISSOCK
735 #       define S_ISSOCK(m) _S_ISSOCK(m)
736 #   else
737 #       ifdef _S_IFSOCK
738 #           define S_ISSOCK(m) ((m & S_IFMT) == _S_IFSOCK)
739 #       else
740 #           ifdef S_IFSOCK
741 #               define S_ISSOCK(m) ((m & S_IFMT) == S_IFSOCK)
742 #           else
743 #               define S_ISSOCK(m) (0)
744 #           endif
745 #       endif
746 #   endif
747 #endif
748
749 #ifndef S_IRUSR
750 #   ifdef S_IREAD
751 #       define S_IRUSR S_IREAD
752 #       define S_IWUSR S_IWRITE
753 #       define S_IXUSR S_IEXEC
754 #   else
755 #       define S_IRUSR 0400
756 #       define S_IWUSR 0200
757 #       define S_IXUSR 0100
758 #   endif
759 #   define S_IRGRP (S_IRUSR>>3)
760 #   define S_IWGRP (S_IWUSR>>3)
761 #   define S_IXGRP (S_IXUSR>>3)
762 #   define S_IROTH (S_IRUSR>>6)
763 #   define S_IWOTH (S_IWUSR>>6)
764 #   define S_IXOTH (S_IXUSR>>6)
765 #endif
766
767 #ifndef S_ISUID
768 #   define S_ISUID 04000
769 #endif
770
771 #ifndef S_ISGID
772 #   define S_ISGID 02000
773 #endif
774
775 #ifdef ff_next
776 #   undef ff_next
777 #endif
778
779 #if defined(cray) || defined(gould) || defined(i860) || defined(pyr)
780 #   define SLOPPYDIVIDE
781 #endif
782
783 #ifdef UV
784 #undef UV
785 #endif
786
787 /*  XXX QUAD stuff is not currently supported on most systems.
788     Specifically, perl internals don't support long long.  Among
789     the many problems is that some compilers support long long,
790     but the underlying library functions (such as sprintf) don't.
791     Some things do work (such as quad pack/unpack on convex);
792     also some systems use long long for the fpos_t typedef.  That
793     seems to work too.
794
795     The IV type is supposed to be long enough to hold any integral
796     value or a pointer.
797     --Andy Dougherty    August 1996
798 */
799
800 #ifdef cray
801 #   define Quad_t int
802 #else
803 #   ifdef convex
804 #       define Quad_t long long
805 #   else
806 #       if LONGSIZE == 8
807 #           define Quad_t long
808 #       endif
809 #   endif
810 #endif
811
812 /* XXX Experimental set-up for long long.  Just add -DUSE_LONG_LONG
813    to your ccflags.  --Andy Dougherty   4/1998
814 */
815 #ifdef USE_LONG_LONG
816 #  if defined(HAS_LONG_LONG) && LONGLONGSIZE == 8
817 #    define Quad_t long long
818 #  endif
819 #endif
820
821 #ifdef Quad_t
822 #   define HAS_QUAD
823     typedef Quad_t IV;
824     typedef unsigned Quad_t UV;
825 #   define IV_MAX PERL_QUAD_MAX
826 #   define IV_MIN PERL_QUAD_MIN
827 #   define UV_MAX PERL_UQUAD_MAX
828 #   define UV_MIN PERL_UQUAD_MIN
829 #else
830     typedef long IV;
831     typedef unsigned long UV;
832 #   define IV_MAX PERL_LONG_MAX
833 #   define IV_MIN PERL_LONG_MIN
834 #   define UV_MAX PERL_ULONG_MAX
835 #   define UV_MIN PERL_ULONG_MIN
836 #endif
837
838 /* Previously these definitions used hardcoded figures. 
839  * It is hoped these formula are more portable, although
840  * no data one way or another is presently known to me.
841  * The "PERL_" names are used because these calculated constants
842  * do not meet the ANSI requirements for LONG_MAX, etc., which
843  * need to be constants acceptable to #if - kja
844  *    define PERL_LONG_MAX        2147483647L
845  *    define PERL_LONG_MIN        (-LONG_MAX - 1)
846  *    define PERL ULONG_MAX       4294967295L
847  */
848
849 #ifdef I_LIMITS  /* Needed for cast_xxx() functions below. */
850 #  include <limits.h>
851 #else
852 #ifdef I_VALUES
853 #  include <values.h>
854 #endif
855 #endif
856
857 /*
858  * Try to figure out max and min values for the integral types.  THE CORRECT
859  * SOLUTION TO THIS MESS: ADAPT enquire.c FROM GCC INTO CONFIGURE.  The
860  * following hacks are used if neither limits.h or values.h provide them:
861  * U<TYPE>_MAX: for types >= int: ~(unsigned TYPE)0
862  *              for types <  int:  (unsigned TYPE)~(unsigned)0
863  *      The argument to ~ must be unsigned so that later signed->unsigned
864  *      conversion can't modify the value's bit pattern (e.g. -0 -> +0),
865  *      and it must not be smaller than int because ~ does integral promotion.
866  * <type>_MAX: (<type>) (U<type>_MAX >> 1)
867  * <type>_MIN: -<type>_MAX - <is_twos_complement_architecture: (3 & -1) == 3>.
868  *      The latter is a hack which happens to work on some machines but
869  *      does *not* catch any random system, or things like integer types
870  *      with NaN if that is possible.
871  *
872  * All of the types are explicitly cast to prevent accidental loss of
873  * numeric range, and in the hope that they will be less likely to confuse
874  * over-eager optimizers.
875  *
876  */
877
878 #define PERL_UCHAR_MIN ((unsigned char)0)
879
880 #ifdef UCHAR_MAX
881 #  define PERL_UCHAR_MAX ((unsigned char)UCHAR_MAX)
882 #else
883 #  ifdef MAXUCHAR
884 #    define PERL_UCHAR_MAX ((unsigned char)MAXUCHAR)
885 #  else
886 #    define PERL_UCHAR_MAX       ((unsigned char)~(unsigned)0)
887 #  endif
888 #endif
889  
890 /*
891  * CHAR_MIN and CHAR_MAX are not included here, as the (char) type may be
892  * ambiguous. It may be equivalent to (signed char) or (unsigned char)
893  * depending on local options. Until Configure detects this (or at least
894  * detects whether the "signed" keyword is available) the CHAR ranges
895  * will not be included. UCHAR functions normally.
896  *                                                           - kja
897  */
898
899 #define PERL_USHORT_MIN ((unsigned short)0)
900
901 #ifdef USHORT_MAX
902 #  define PERL_USHORT_MAX ((unsigned short)USHORT_MAX)
903 #else
904 #  ifdef MAXUSHORT
905 #    define PERL_USHORT_MAX ((unsigned short)MAXUSHORT)
906 #  else
907 #    ifdef USHRT_MAX
908 #      define PERL_USHORT_MAX ((unsigned short)USHRT_MAX)
909 #    else
910 #      define PERL_USHORT_MAX       ((unsigned short)~(unsigned)0)
911 #    endif
912 #  endif
913 #endif
914
915 #ifdef SHORT_MAX
916 #  define PERL_SHORT_MAX ((short)SHORT_MAX)
917 #else
918 #  ifdef MAXSHORT    /* Often used in <values.h> */
919 #    define PERL_SHORT_MAX ((short)MAXSHORT)
920 #  else
921 #    ifdef SHRT_MAX
922 #      define PERL_SHORT_MAX ((short)SHRT_MAX)
923 #    else
924 #      define PERL_SHORT_MAX      ((short) (PERL_USHORT_MAX >> 1))
925 #    endif
926 #  endif
927 #endif
928
929 #ifdef SHORT_MIN
930 #  define PERL_SHORT_MIN ((short)SHORT_MIN)
931 #else
932 #  ifdef MINSHORT
933 #    define PERL_SHORT_MIN ((short)MINSHORT)
934 #  else
935 #    ifdef SHRT_MIN
936 #      define PERL_SHORT_MIN ((short)SHRT_MIN)
937 #    else
938 #      define PERL_SHORT_MIN        (-PERL_SHORT_MAX - ((3 & -1) == 3))
939 #    endif
940 #  endif
941 #endif
942
943 #ifdef UINT_MAX
944 #  define PERL_UINT_MAX ((unsigned int)UINT_MAX)
945 #else
946 #  ifdef MAXUINT
947 #    define PERL_UINT_MAX ((unsigned int)MAXUINT)
948 #  else
949 #    define PERL_UINT_MAX       (~(unsigned int)0)
950 #  endif
951 #endif
952
953 #define PERL_UINT_MIN ((unsigned int)0)
954
955 #ifdef INT_MAX
956 #  define PERL_INT_MAX ((int)INT_MAX)
957 #else
958 #  ifdef MAXINT    /* Often used in <values.h> */
959 #    define PERL_INT_MAX ((int)MAXINT)
960 #  else
961 #    define PERL_INT_MAX        ((int)(PERL_UINT_MAX >> 1))
962 #  endif
963 #endif
964
965 #ifdef INT_MIN
966 #  define PERL_INT_MIN ((int)INT_MIN)
967 #else
968 #  ifdef MININT
969 #    define PERL_INT_MIN ((int)MININT)
970 #  else
971 #    define PERL_INT_MIN        (-PERL_INT_MAX - ((3 & -1) == 3))
972 #  endif
973 #endif
974
975 #ifdef ULONG_MAX
976 #  define PERL_ULONG_MAX ((unsigned long)ULONG_MAX)
977 #else
978 #  ifdef MAXULONG
979 #    define PERL_ULONG_MAX ((unsigned long)MAXULONG)
980 #  else
981 #    define PERL_ULONG_MAX       (~(unsigned long)0)
982 #  endif
983 #endif
984
985 #define PERL_ULONG_MIN ((unsigned long)0L)
986
987 #ifdef LONG_MAX
988 #  define PERL_LONG_MAX ((long)LONG_MAX)
989 #else
990 #  ifdef MAXLONG    /* Often used in <values.h> */
991 #    define PERL_LONG_MAX ((long)MAXLONG)
992 #  else
993 #    define PERL_LONG_MAX        ((long) (PERL_ULONG_MAX >> 1))
994 #  endif
995 #endif
996
997 #ifdef LONG_MIN
998 #  define PERL_LONG_MIN ((long)LONG_MIN)
999 #else
1000 #  ifdef MINLONG
1001 #    define PERL_LONG_MIN ((long)MINLONG)
1002 #  else
1003 #    define PERL_LONG_MIN        (-PERL_LONG_MAX - ((3 & -1) == 3))
1004 #  endif
1005 #endif
1006
1007 #ifdef HAS_QUAD
1008
1009 #  ifdef UQUAD_MAX
1010 #    define PERL_UQUAD_MAX ((UV)UQUAD_MAX)
1011 #  else
1012 #    define PERL_UQUAD_MAX      (~(UV)0)
1013 #  endif
1014
1015 #  define PERL_UQUAD_MIN ((UV)0)
1016
1017 #  ifdef QUAD_MAX
1018 #    define PERL_QUAD_MAX ((IV)QUAD_MAX)
1019 #  else
1020 #    define PERL_QUAD_MAX       ((IV) (PERL_UQUAD_MAX >> 1))
1021 #  endif
1022
1023 #  ifdef QUAD_MIN
1024 #    define PERL_QUAD_MIN ((IV)QUAD_MIN)
1025 #  else
1026 #    define PERL_QUAD_MIN       (-PERL_QUAD_MAX - ((3 & -1) == 3))
1027 #  endif
1028
1029 #endif
1030
1031 typedef MEM_SIZE STRLEN;
1032
1033 typedef struct op OP;
1034 typedef struct cop COP;
1035 typedef struct unop UNOP;
1036 typedef struct binop BINOP;
1037 typedef struct listop LISTOP;
1038 typedef struct logop LOGOP;
1039 typedef struct condop CONDOP;
1040 typedef struct pmop PMOP;
1041 typedef struct svop SVOP;
1042 typedef struct gvop GVOP;
1043 typedef struct pvop PVOP;
1044 typedef struct loop LOOP;
1045
1046 typedef struct Outrec Outrec;
1047 typedef struct interpreter PerlInterpreter;
1048 #ifndef __BORLANDC__
1049 typedef struct ff FF;           /* XXX not defined anywhere, should go? */
1050 #endif
1051 typedef struct sv SV;
1052 typedef struct av AV;
1053 typedef struct hv HV;
1054 typedef struct cv CV;
1055 typedef struct regexp REGEXP;
1056 typedef struct gp GP;
1057 typedef struct gv GV;
1058 typedef struct io IO;
1059 typedef struct context PERL_CONTEXT;
1060 typedef struct block BLOCK;
1061
1062 typedef struct magic MAGIC;
1063 typedef struct xrv XRV;
1064 typedef struct xpv XPV;
1065 typedef struct xpviv XPVIV;
1066 typedef struct xpvuv XPVUV;
1067 typedef struct xpvnv XPVNV;
1068 typedef struct xpvmg XPVMG;
1069 typedef struct xpvlv XPVLV;
1070 typedef struct xpvav XPVAV;
1071 typedef struct xpvhv XPVHV;
1072 typedef struct xpvgv XPVGV;
1073 typedef struct xpvcv XPVCV;
1074 typedef struct xpvbm XPVBM;
1075 typedef struct xpvfm XPVFM;
1076 typedef struct xpvio XPVIO;
1077 typedef struct mgvtbl MGVTBL;
1078 typedef union any ANY;
1079
1080 #include "handy.h"
1081
1082 #ifdef PERL_OBJECT
1083 typedef I32 (*filter_t) _((CPerlObj*, int, SV *, int));
1084 #else
1085 typedef I32 (*filter_t) _((int, SV *, int));
1086 #endif
1087
1088 #define FILTER_READ(idx, sv, len)  filter_read(idx, sv, len)
1089 #define FILTER_DATA(idx)           (AvARRAY(rsfp_filters)[idx])
1090 #define FILTER_ISREADER(idx)       (idx >= AvFILLp(rsfp_filters))
1091
1092 #ifdef DOSISH
1093 # if defined(OS2)
1094 #   include "os2ish.h"
1095 # else
1096 #   include "dosish.h"
1097 # endif
1098 #else
1099 # if defined(VMS)
1100 #   include "vmsish.h"
1101 # else
1102 #   if defined(PLAN9)
1103 #     include "./plan9/plan9ish.h"
1104 #   else
1105 #     include "unixish.h"
1106 #   endif
1107 # endif
1108 #endif         
1109
1110 #ifndef FUNC_NAME_TO_PTR
1111 #define FUNC_NAME_TO_PTR(name)          name
1112 #endif
1113
1114 /* 
1115  * USE_THREADS needs to be after unixish.h as <pthread.h> includes
1116  * <sys/signal.h> which defines NSIG - which will stop inclusion of <signal.h>
1117  * this results in many functions being undeclared which bothers C++
1118  * May make sense to have threads after "*ish.h" anyway
1119  */
1120
1121 #ifdef USE_THREADS
1122 #  ifdef FAKE_THREADS
1123 #    include "fakethr.h"
1124 #  else
1125 #    ifdef WIN32
1126 #      include <win32thread.h>
1127 #    else
1128 #      ifdef OS2
1129 #        include "os2thread.h"
1130 #      else
1131 #        include <pthread.h>
1132 typedef pthread_t perl_os_thread;
1133 typedef pthread_mutex_t perl_mutex;
1134 typedef pthread_cond_t perl_cond;
1135 typedef pthread_key_t perl_key;
1136 #      endif /* OS2 */
1137 #    endif /* WIN32 */
1138 #  endif /* FAKE_THREADS */
1139 #endif /* USE_THREADS */
1140
1141
1142   
1143 #ifdef VMS
1144 #   define STATUS_NATIVE        statusvalue_vms
1145 #   define STATUS_NATIVE_EXPORT \
1146         ((I32)statusvalue_vms == -1 ? 44 : statusvalue_vms)
1147 #   define STATUS_NATIVE_SET(n)                                         \
1148         STMT_START {                                                    \
1149             statusvalue_vms = (n);                                      \
1150             if ((I32)statusvalue_vms == -1)                             \
1151                 statusvalue = -1;                                       \
1152             else if (statusvalue_vms & STS$M_SUCCESS)                   \
1153                 statusvalue = 0;                                        \
1154             else if ((statusvalue_vms & STS$M_SEVERITY) == 0)           \
1155                 statusvalue = 1 << 8;                                   \
1156             else                                                        \
1157                 statusvalue = (statusvalue_vms & STS$M_SEVERITY) << 8;  \
1158         } STMT_END
1159 #   define STATUS_POSIX statusvalue
1160 #   ifdef VMSISH_STATUS
1161 #       define STATUS_CURRENT   (VMSISH_STATUS ? STATUS_NATIVE : STATUS_POSIX)
1162 #   else
1163 #       define STATUS_CURRENT   STATUS_POSIX
1164 #   endif
1165 #   define STATUS_POSIX_SET(n)                          \
1166         STMT_START {                                    \
1167             statusvalue = (n);                          \
1168             if (statusvalue != -1) {                    \
1169                 statusvalue &= 0xFFFF;                  \
1170                 statusvalue_vms = statusvalue ? 44 : 1; \
1171             }                                           \
1172             else statusvalue_vms = -1;                  \
1173         } STMT_END
1174 #   define STATUS_ALL_SUCCESS   (statusvalue = 0, statusvalue_vms = 1)
1175 #   define STATUS_ALL_FAILURE   (statusvalue = 1, statusvalue_vms = 44)
1176 #else
1177 #   define STATUS_NATIVE        STATUS_POSIX
1178 #   define STATUS_NATIVE_EXPORT STATUS_POSIX
1179 #   define STATUS_NATIVE_SET    STATUS_POSIX_SET
1180 #   define STATUS_POSIX         statusvalue
1181 #   define STATUS_POSIX_SET(n)          \
1182         STMT_START {                    \
1183             statusvalue = (n);          \
1184             if (statusvalue != -1)      \
1185                 statusvalue &= 0xFFFF;  \
1186         } STMT_END
1187 #   define STATUS_CURRENT STATUS_POSIX
1188 #   define STATUS_ALL_SUCCESS   (statusvalue = 0)
1189 #   define STATUS_ALL_FAILURE   (statusvalue = 1)
1190 #endif
1191
1192 /* Some unistd.h's give a prototype for pause() even though
1193    HAS_PAUSE ends up undefined.  This causes the #define
1194    below to be rejected by the compmiler.  Sigh.
1195 */
1196 #ifdef HAS_PAUSE
1197 #define Pause   pause
1198 #else
1199 #define Pause() sleep((32767<<16)+32767)
1200 #endif
1201
1202 #ifndef IOCPARM_LEN
1203 #   ifdef IOCPARM_MASK
1204         /* on BSDish systes we're safe */
1205 #       define IOCPARM_LEN(x)  (((x) >> 16) & IOCPARM_MASK)
1206 #   else
1207         /* otherwise guess at what's safe */
1208 #       define IOCPARM_LEN(x)   256
1209 #   endif
1210 #endif
1211
1212 union any {
1213     void*       any_ptr;
1214     I32         any_i32;
1215     IV          any_iv;
1216     long        any_long;
1217     void        (CPERLscope(*any_dptr)) _((void*));
1218 #if defined(WIN32) && !defined(PERL_OBJECT)
1219         /* Visual C thinks that a pointer to a member variable is 16 bytes in size. */
1220         char    handle_VC_problem[16];
1221 #endif
1222 };
1223
1224 #ifdef USE_THREADS
1225 #define ARGSproto struct perl_thread *thr
1226 #else
1227 #define ARGSproto void
1228 #endif /* USE_THREADS */
1229
1230 /* Work around some cygwin32 problems with importing global symbols */
1231 #if defined(CYGWIN32) && defined(DLLIMPORT) 
1232 #   include "cw32imp.h"
1233 #endif
1234
1235 #include "regexp.h"
1236 #include "sv.h"
1237 #include "util.h"
1238 #include "form.h"
1239 #include "gv.h"
1240 #include "cv.h"
1241 #include "opcode.h"
1242 #include "op.h"
1243 #include "cop.h"
1244 #include "av.h"
1245 #include "hv.h"
1246 #include "mg.h"
1247 #include "scope.h"
1248 #include "bytecode.h"
1249 #include "byterun.h"
1250
1251 /* Current curly descriptor */
1252 typedef struct curcur CURCUR;
1253 struct curcur {
1254     int         parenfloor;     /* how far back to strip paren data */
1255     int         cur;            /* how many instances of scan we've matched */
1256     int         min;            /* the minimal number of scans to match */
1257     int         max;            /* the maximal number of scans to match */
1258     int         minmod;         /* whether to work our way up or down */
1259     regnode *   scan;           /* the thing to match */
1260     regnode *   next;           /* what has to match after it */
1261     char *      lastloc;        /* where we started matching this scan */
1262     CURCUR *    oldcc;          /* current curly before we started this one */
1263 };
1264
1265 typedef struct _sublex_info SUBLEXINFO;
1266 struct _sublex_info {
1267     I32 super_state;    /* lexer state to save */
1268     I32 sub_inwhat;     /* "lex_inwhat" to use */
1269     OP *sub_op;         /* "lex_op" to use */
1270 };
1271
1272 #ifdef PERL_OBJECT
1273 struct magic_state {
1274     SV* mgs_sv;
1275     U32 mgs_flags;
1276 };
1277 typedef struct magic_state MGS;
1278
1279 typedef struct {
1280     I32 len_min;
1281     I32 len_delta;
1282     I32 pos_min;
1283     I32 pos_delta;
1284     SV *last_found;
1285     I32 last_end;                       /* min value, <0 unless valid. */
1286     I32 last_start_min;
1287     I32 last_start_max;
1288     SV **longest;                       /* Either &l_fixed, or &l_float. */
1289     SV *longest_fixed;
1290     I32 offset_fixed;
1291     SV *longest_float;
1292     I32 offset_float_min;
1293     I32 offset_float_max;
1294     I32 flags;
1295 } scan_data_t;
1296
1297 typedef I32 CHECKPOINT;
1298 #endif /* PERL_OBJECT */
1299
1300 /* work around some libPW problems */
1301 #ifdef DOINIT
1302 EXT char Error[1];
1303 #endif
1304
1305 #if defined(iAPX286) || defined(M_I286) || defined(I80286)
1306 #   define I286
1307 #endif
1308
1309 #if defined(htonl) && !defined(HAS_HTONL)
1310 #define HAS_HTONL
1311 #endif
1312 #if defined(htons) && !defined(HAS_HTONS)
1313 #define HAS_HTONS
1314 #endif
1315 #if defined(ntohl) && !defined(HAS_NTOHL)
1316 #define HAS_NTOHL
1317 #endif
1318 #if defined(ntohs) && !defined(HAS_NTOHS)
1319 #define HAS_NTOHS
1320 #endif
1321 #ifndef HAS_HTONL
1322 #if (BYTEORDER & 0xffff) != 0x4321
1323 #define HAS_HTONS
1324 #define HAS_HTONL
1325 #define HAS_NTOHS
1326 #define HAS_NTOHL
1327 #define MYSWAP
1328 #define htons my_swap
1329 #define htonl my_htonl
1330 #define ntohs my_swap
1331 #define ntohl my_ntohl
1332 #endif
1333 #else
1334 #if (BYTEORDER & 0xffff) == 0x4321
1335 #undef HAS_HTONS
1336 #undef HAS_HTONL
1337 #undef HAS_NTOHS
1338 #undef HAS_NTOHL
1339 #endif
1340 #endif
1341
1342 /*
1343  * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1344  * -DWS
1345  */
1346 #if BYTEORDER != 0x1234
1347 # define HAS_VTOHL
1348 # define HAS_VTOHS
1349 # define HAS_HTOVL
1350 # define HAS_HTOVS
1351 # if BYTEORDER == 0x4321
1352 #  define vtohl(x)      ((((x)&0xFF)<<24)       \
1353                         +(((x)>>24)&0xFF)       \
1354                         +(((x)&0x0000FF00)<<8)  \
1355                         +(((x)&0x00FF0000)>>8)  )
1356 #  define vtohs(x)      ((((x)&0xFF)<<8) + (((x)>>8)&0xFF))
1357 #  define htovl(x)      vtohl(x)
1358 #  define htovs(x)      vtohs(x)
1359 # endif
1360         /* otherwise default to functions in util.c */
1361 #endif
1362
1363 #ifdef CASTNEGFLOAT
1364 #define U_S(what) ((U16)(what))
1365 #define U_I(what) ((unsigned int)(what))
1366 #define U_L(what) ((U32)(what))
1367 #else
1368 EXTERN_C U32 cast_ulong _((double));
1369 #define U_S(what) ((U16)cast_ulong((double)(what)))
1370 #define U_I(what) ((unsigned int)cast_ulong((double)(what)))
1371 #define U_L(what) (cast_ulong((double)(what)))
1372 #endif
1373
1374 #ifdef CASTI32
1375 #define I_32(what) ((I32)(what))
1376 #define I_V(what) ((IV)(what))
1377 #define U_V(what) ((UV)(what))
1378 #else
1379 START_EXTERN_C
1380 I32 cast_i32 _((double));
1381 IV cast_iv _((double));
1382 UV cast_uv _((double));
1383 END_EXTERN_C
1384 #define I_32(what) (cast_i32((double)(what)))
1385 #define I_V(what) (cast_iv((double)(what)))
1386 #define U_V(what) (cast_uv((double)(what)))
1387 #endif
1388
1389 struct Outrec {
1390     I32         o_lines;
1391     char        *o_str;
1392     U32         o_len;
1393 };
1394
1395 #ifndef MAXSYSFD
1396 #   define MAXSYSFD 2
1397 #endif
1398
1399 #ifndef TMPPATH
1400 #  define TMPPATH "/tmp/perl-eXXXXXX"
1401 #endif
1402
1403 #ifndef __cplusplus
1404 Uid_t getuid _((void));
1405 Uid_t geteuid _((void));
1406 Gid_t getgid _((void));
1407 Gid_t getegid _((void));
1408 #endif
1409
1410 #ifdef DEBUGGING
1411 #ifndef Perl_debug_log
1412 #define Perl_debug_log  PerlIO_stderr()
1413 #endif
1414 #define YYDEBUG 1
1415 #define DEB(a)                          a
1416 #define DEBUG(a)   if (debug)           a
1417 #define DEBUG_p(a) if (debug & 1)       a
1418 #define DEBUG_s(a) if (debug & 2)       a
1419 #define DEBUG_l(a) if (debug & 4)       a
1420 #define DEBUG_t(a) if (debug & 8)       a
1421 #define DEBUG_o(a) if (debug & 16)      a
1422 #define DEBUG_c(a) if (debug & 32)      a
1423 #define DEBUG_P(a) if (debug & 64)      a
1424 #define DEBUG_m(a) if (curinterp && debug & 128)        a
1425 #define DEBUG_f(a) if (debug & 256)     a
1426 #define DEBUG_r(a) if (debug & 512)     a
1427 #define DEBUG_x(a) if (debug & 1024)    a
1428 #define DEBUG_u(a) if (debug & 2048)    a
1429 #define DEBUG_L(a) if (debug & 4096)    a
1430 #define DEBUG_H(a) if (debug & 8192)    a
1431 #define DEBUG_X(a) if (debug & 16384)   a
1432 #define DEBUG_D(a) if (debug & 32768)   a
1433 #else
1434 #define DEB(a)
1435 #define DEBUG(a)
1436 #define DEBUG_p(a)
1437 #define DEBUG_s(a)
1438 #define DEBUG_l(a)
1439 #define DEBUG_t(a)
1440 #define DEBUG_o(a)
1441 #define DEBUG_c(a)
1442 #define DEBUG_P(a)
1443 #define DEBUG_m(a)
1444 #define DEBUG_f(a)
1445 #define DEBUG_r(a)
1446 #define DEBUG_x(a)
1447 #define DEBUG_u(a)
1448 #define DEBUG_L(a)
1449 #define DEBUG_H(a)
1450 #define DEBUG_X(a)
1451 #define DEBUG_D(a)
1452 #endif
1453 #define YYMAXDEPTH 300
1454
1455 #ifndef assert  /* <assert.h> might have been included somehow */
1456 #define assert(what)    DEB( {                                          \
1457         if (!(what)) {                                                  \
1458             croak("Assertion failed: file \"%s\", line %d",             \
1459                 __FILE__, __LINE__);                                    \
1460             PerlProc_exit(1);                                                   \
1461         }})
1462 #endif
1463
1464 struct ufuncs {
1465     I32 (*uf_val)_((IV, SV*));
1466     I32 (*uf_set)_((IV, SV*));
1467     IV uf_index;
1468 };
1469
1470 /* Fix these up for __STDC__ */
1471 #ifndef DONT_DECLARE_STD
1472 char *mktemp _((char*));
1473 double atof _((const char*));
1474 #endif
1475
1476 #ifndef STANDARD_C
1477 /* All of these are in stdlib.h or time.h for ANSI C */
1478 Time_t time();
1479 struct tm *gmtime(), *localtime();
1480 char *strchr(), *strrchr();
1481 char *strcpy(), *strcat();
1482 #endif /* ! STANDARD_C */
1483
1484
1485 #ifdef I_MATH
1486 #    include <math.h>
1487 #else
1488 START_EXTERN_C
1489             double exp _((double));
1490             double log _((double));
1491             double log10 _((double));
1492             double sqrt _((double));
1493             double frexp _((double,int*));
1494             double ldexp _((double,int));
1495             double modf _((double,double*));
1496             double sin _((double));
1497             double cos _((double));
1498             double atan2 _((double,double));
1499             double pow _((double,double));
1500 END_EXTERN_C
1501 #endif
1502
1503 #ifndef __cplusplus
1504 #  ifdef __NeXT__ /* or whatever catches all NeXTs */
1505 char *crypt ();       /* Maybe more hosts will need the unprototyped version */
1506 #  else
1507 #    if !defined(WIN32) || !defined(HAVE_DES_FCRYPT)
1508 char *crypt _((const char*, const char*));
1509 #    endif /* !WIN32 && !HAVE_CRYPT_SOURCE */
1510 #  endif /* !__NeXT__ */
1511 #  ifndef DONT_DECLARE_STD
1512 #    ifndef getenv
1513 char *getenv _((const char*));
1514 #    endif /* !getenv */
1515 Off_t lseek _((int,Off_t,int));
1516 #  endif /* !DONT_DECLARE_STD */
1517 char *getlogin _((void));
1518 #endif /* !__cplusplus */
1519
1520 #ifdef UNLINK_ALL_VERSIONS /* Currently only makes sense for VMS */
1521 #define UNLINK unlnk
1522 I32 unlnk _((char*));
1523 #else
1524 #define UNLINK unlink
1525 #endif
1526
1527 #ifndef HAS_SETREUID
1528 #  ifdef HAS_SETRESUID
1529 #    define setreuid(r,e) setresuid(r,e,(Uid_t)-1)
1530 #    define HAS_SETREUID
1531 #  endif
1532 #endif
1533 #ifndef HAS_SETREGID
1534 #  ifdef HAS_SETRESGID
1535 #    define setregid(r,e) setresgid(r,e,(Gid_t)-1)
1536 #    define HAS_SETREGID
1537 #  endif
1538 #endif
1539
1540 typedef Signal_t (*Sighandler_t) _((int));
1541
1542 #ifdef HAS_SIGACTION
1543 typedef struct sigaction Sigsave_t;
1544 #else
1545 typedef Sighandler_t Sigsave_t;
1546 #endif
1547
1548 #define SCAN_DEF 0
1549 #define SCAN_TR 1
1550 #define SCAN_REPL 2
1551
1552 #ifdef DEBUGGING
1553 # ifndef register
1554 #  define register
1555 # endif
1556 # define PAD_SV(po) pad_sv(po)
1557 # define RUNOPS_DEFAULT runops_debug
1558 #else
1559 # define PAD_SV(po) curpad[po]
1560 # define RUNOPS_DEFAULT runops_standard
1561 #endif
1562
1563 #ifdef MYMALLOC
1564 #  define MALLOC_INIT MUTEX_INIT(&malloc_mutex)
1565 #  define MALLOC_TERM MUTEX_DESTROY(&malloc_mutex)
1566 #else
1567 #  define MALLOC_INIT
1568 #  define MALLOC_TERM
1569 #endif
1570
1571
1572 /*
1573  * These need prototyping here because <proto.h> isn't
1574  * included until after runops is initialised.
1575  */
1576
1577 #ifndef PERL_OBJECT
1578 typedef int runops_proc_t _((void));
1579 int runops_standard _((void));
1580 #ifdef DEBUGGING
1581 int runops_debug _((void));
1582 #endif
1583 #endif  /* PERL_OBJECT */
1584
1585 /* _ (for $_) must be first in the following list (DEFSV requires it) */
1586 #define THREADSV_NAMES "_123456789&`'+/.,\\\";^-%=|~:\001\005!@"
1587
1588 /* VMS doesn't use environ array and NeXT has problems with crt0.o globals */
1589 #if !defined(VMS) && !(defined(NeXT) && defined(__DYNAMIC__))
1590 #if !defined(DONT_DECLARE_STD) \
1591         || (defined(__svr4__) && defined(__GNUC__) && defined(sun)) \
1592         || defined(__sgi) || defined(__DGUX)
1593 extern char **  environ;        /* environment variables supplied via exec */
1594 #endif
1595 #else
1596 #  if defined(NeXT) && defined(__DYNAMIC__)
1597
1598 #  include <mach-o/dyld.h>
1599 EXT char *** environ_pointer;
1600 #  define environ (*environ_pointer)
1601 #  endif
1602 #endif /* environ processing */
1603
1604
1605 /* for tmp use in stupid debuggers */
1606 EXT int *       di;
1607 EXT short *     ds;
1608 EXT char *      dc;
1609
1610 /* handy constants */
1611 EXTCONST char warn_uninit[]
1612   INIT("Use of uninitialized value");
1613 EXTCONST char warn_nosemi[]
1614   INIT("Semicolon seems to be missing");
1615 EXTCONST char warn_reserved[]
1616   INIT("Unquoted string \"%s\" may clash with future reserved word");
1617 EXTCONST char warn_nl[]
1618   INIT("Unsuccessful %s on filename containing newline");
1619 EXTCONST char no_wrongref[]
1620   INIT("Can't use %s ref as %s ref");
1621 EXTCONST char no_symref[]
1622   INIT("Can't use string (\"%.32s\") as %s ref while \"strict refs\" in use");
1623 EXTCONST char no_usym[]
1624   INIT("Can't use an undefined value as %s reference");
1625 EXTCONST char no_aelem[]
1626   INIT("Modification of non-creatable array value attempted, subscript %d");
1627 EXTCONST char no_helem[]
1628   INIT("Modification of non-creatable hash value attempted, subscript \"%s\"");
1629 EXTCONST char no_modify[]
1630   INIT("Modification of a read-only value attempted");
1631 EXTCONST char no_mem[]
1632   INIT("Out of memory!\n");
1633 EXTCONST char no_security[]
1634   INIT("Insecure dependency in %s%s");
1635 EXTCONST char no_sock_func[]
1636   INIT("Unsupported socket function \"%s\" called");
1637 EXTCONST char no_dir_func[]
1638   INIT("Unsupported directory function \"%s\" called");
1639 EXTCONST char no_func[]
1640   INIT("The %s function is unimplemented");
1641 EXTCONST char no_myglob[]
1642   INIT("\"my\" variable %s can't be in a package");
1643 EXTCONST char no_restartop[]
1644   INIT("panic: restartop\n");
1645 EXTCONST char no_top_env[]
1646   INIT("panic: top_env\n");
1647
1648 #ifdef DOINIT
1649 EXT char *sig_name[] = { SIG_NAME };
1650 EXT int   sig_num[]  = { SIG_NUM };
1651 EXT SV  * psig_ptr[sizeof(sig_num)/sizeof(*sig_num)];
1652 EXT SV  * psig_name[sizeof(sig_num)/sizeof(*sig_num)];
1653 #else
1654 EXT char *sig_name[];
1655 EXT int   sig_num[];
1656 EXT SV  * psig_ptr[];
1657 EXT SV  * psig_name[];
1658 #endif
1659
1660 /* fast case folding tables */
1661
1662 #ifdef DOINIT
1663 EXTCONST  unsigned char fold[] = {
1664         0,      1,      2,      3,      4,      5,      6,      7,
1665         8,      9,      10,     11,     12,     13,     14,     15,
1666         16,     17,     18,     19,     20,     21,     22,     23,
1667         24,     25,     26,     27,     28,     29,     30,     31,
1668         32,     33,     34,     35,     36,     37,     38,     39,
1669         40,     41,     42,     43,     44,     45,     46,     47,
1670         48,     49,     50,     51,     52,     53,     54,     55,
1671         56,     57,     58,     59,     60,     61,     62,     63,
1672         64,     'a',    'b',    'c',    'd',    'e',    'f',    'g',
1673         'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
1674         'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
1675         'x',    'y',    'z',    91,     92,     93,     94,     95,
1676         96,     'A',    'B',    'C',    'D',    'E',    'F',    'G',
1677         'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
1678         'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
1679         'X',    'Y',    'Z',    123,    124,    125,    126,    127,
1680         128,    129,    130,    131,    132,    133,    134,    135,
1681         136,    137,    138,    139,    140,    141,    142,    143,
1682         144,    145,    146,    147,    148,    149,    150,    151,
1683         152,    153,    154,    155,    156,    157,    158,    159,
1684         160,    161,    162,    163,    164,    165,    166,    167,
1685         168,    169,    170,    171,    172,    173,    174,    175,
1686         176,    177,    178,    179,    180,    181,    182,    183,
1687         184,    185,    186,    187,    188,    189,    190,    191,
1688         192,    193,    194,    195,    196,    197,    198,    199,
1689         200,    201,    202,    203,    204,    205,    206,    207,
1690         208,    209,    210,    211,    212,    213,    214,    215,
1691         216,    217,    218,    219,    220,    221,    222,    223,    
1692         224,    225,    226,    227,    228,    229,    230,    231,
1693         232,    233,    234,    235,    236,    237,    238,    239,
1694         240,    241,    242,    243,    244,    245,    246,    247,
1695         248,    249,    250,    251,    252,    253,    254,    255
1696 };
1697 #else
1698 EXTCONST unsigned char fold[];
1699 #endif
1700
1701 #ifdef DOINIT
1702 EXT unsigned char fold_locale[] = {
1703         0,      1,      2,      3,      4,      5,      6,      7,
1704         8,      9,      10,     11,     12,     13,     14,     15,
1705         16,     17,     18,     19,     20,     21,     22,     23,
1706         24,     25,     26,     27,     28,     29,     30,     31,
1707         32,     33,     34,     35,     36,     37,     38,     39,
1708         40,     41,     42,     43,     44,     45,     46,     47,
1709         48,     49,     50,     51,     52,     53,     54,     55,
1710         56,     57,     58,     59,     60,     61,     62,     63,
1711         64,     'a',    'b',    'c',    'd',    'e',    'f',    'g',
1712         'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
1713         'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
1714         'x',    'y',    'z',    91,     92,     93,     94,     95,
1715         96,     'A',    'B',    'C',    'D',    'E',    'F',    'G',
1716         'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
1717         'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
1718         'X',    'Y',    'Z',    123,    124,    125,    126,    127,
1719         128,    129,    130,    131,    132,    133,    134,    135,
1720         136,    137,    138,    139,    140,    141,    142,    143,
1721         144,    145,    146,    147,    148,    149,    150,    151,
1722         152,    153,    154,    155,    156,    157,    158,    159,
1723         160,    161,    162,    163,    164,    165,    166,    167,
1724         168,    169,    170,    171,    172,    173,    174,    175,
1725         176,    177,    178,    179,    180,    181,    182,    183,
1726         184,    185,    186,    187,    188,    189,    190,    191,
1727         192,    193,    194,    195,    196,    197,    198,    199,
1728         200,    201,    202,    203,    204,    205,    206,    207,
1729         208,    209,    210,    211,    212,    213,    214,    215,
1730         216,    217,    218,    219,    220,    221,    222,    223,    
1731         224,    225,    226,    227,    228,    229,    230,    231,
1732         232,    233,    234,    235,    236,    237,    238,    239,
1733         240,    241,    242,    243,    244,    245,    246,    247,
1734         248,    249,    250,    251,    252,    253,    254,    255
1735 };
1736 #else
1737 EXT unsigned char fold_locale[];
1738 #endif
1739
1740 #ifdef DOINIT
1741 EXTCONST unsigned char freq[] = {       /* letter frequencies for mixed English/C */
1742         1,      2,      84,     151,    154,    155,    156,    157,
1743         165,    246,    250,    3,      158,    7,      18,     29,
1744         40,     51,     62,     73,     85,     96,     107,    118,
1745         129,    140,    147,    148,    149,    150,    152,    153,
1746         255,    182,    224,    205,    174,    176,    180,    217,
1747         233,    232,    236,    187,    235,    228,    234,    226,
1748         222,    219,    211,    195,    188,    193,    185,    184,
1749         191,    183,    201,    229,    181,    220,    194,    162,
1750         163,    208,    186,    202,    200,    218,    198,    179,
1751         178,    214,    166,    170,    207,    199,    209,    206,
1752         204,    160,    212,    216,    215,    192,    175,    173,
1753         243,    172,    161,    190,    203,    189,    164,    230,
1754         167,    248,    227,    244,    242,    255,    241,    231,
1755         240,    253,    169,    210,    245,    237,    249,    247,
1756         239,    168,    252,    251,    254,    238,    223,    221,
1757         213,    225,    177,    197,    171,    196,    159,    4,
1758         5,      6,      8,      9,      10,     11,     12,     13,
1759         14,     15,     16,     17,     19,     20,     21,     22,
1760         23,     24,     25,     26,     27,     28,     30,     31,
1761         32,     33,     34,     35,     36,     37,     38,     39,
1762         41,     42,     43,     44,     45,     46,     47,     48,
1763         49,     50,     52,     53,     54,     55,     56,     57,
1764         58,     59,     60,     61,     63,     64,     65,     66,
1765         67,     68,     69,     70,     71,     72,     74,     75,
1766         76,     77,     78,     79,     80,     81,     82,     83,
1767         86,     87,     88,     89,     90,     91,     92,     93,
1768         94,     95,     97,     98,     99,     100,    101,    102,
1769         103,    104,    105,    106,    108,    109,    110,    111,
1770         112,    113,    114,    115,    116,    117,    119,    120,
1771         121,    122,    123,    124,    125,    126,    127,    128,
1772         130,    131,    132,    133,    134,    135,    136,    137,
1773         138,    139,    141,    142,    143,    144,    145,    146
1774 };
1775 #else
1776 EXTCONST unsigned char freq[];
1777 #endif
1778
1779 #ifdef DEBUGGING
1780 #ifdef DOINIT
1781 EXTCONST char* block_type[] = {
1782         "NULL",
1783         "SUB",
1784         "EVAL",
1785         "LOOP",
1786         "SUBST",
1787         "BLOCK",
1788 };
1789 #else
1790 EXTCONST char* block_type[];
1791 #endif
1792 #endif
1793
1794 /*****************************************************************************/
1795 /* This lexer/parser stuff is currently global since yacc is hard to reenter */
1796 /*****************************************************************************/
1797 /* XXX This needs to be revisited, since BEGIN makes yacc re-enter... */
1798
1799 #include "perly.h"
1800
1801 #define LEX_NOTPARSING          11      /* borrowed from toke.c */
1802
1803 typedef enum {
1804     XOPERATOR,
1805     XTERM,
1806     XREF,
1807     XSTATE,
1808     XBLOCK,
1809     XTERMBLOCK
1810 } expectation;
1811
1812
1813                                 /* Note: the lowest 8 bits are reserved for
1814                                    stuffing into op->op_private */
1815 #define HINT_INTEGER            0x00000001
1816 #define HINT_STRICT_REFS        0x00000002
1817
1818 #define HINT_BLOCK_SCOPE        0x00000100
1819 #define HINT_STRICT_SUBS        0x00000200
1820 #define HINT_STRICT_VARS        0x00000400
1821 #define HINT_LOCALE             0x00000800
1822
1823 /* Various states of an input record separator SV (rs, nrs) */
1824 #define RsSNARF(sv)   (! SvOK(sv))
1825 #define RsSIMPLE(sv)  (SvOK(sv) && SvCUR(sv))
1826 #define RsPARA(sv)    (SvOK(sv) && ! SvCUR(sv))
1827 #define RsRECORD(sv)  (SvROK(sv) && (SvIV(SvRV(sv)) > 0))
1828
1829 /* Set up PERLVAR macros for populating structs */
1830 #define PERLVAR(var,type) type var;
1831 #define PERLVARI(var,type,init) type var;
1832 #define PERLVARIC(var,type,init) type var;
1833
1834 /* Interpreter exitlist entry */
1835 typedef struct exitlistentry {
1836 #ifdef PERL_OBJECT
1837     void (*fn) _((CPerlObj*, void*));
1838 #else
1839     void (*fn) _((void*));
1840 #endif
1841     void *ptr;
1842 } PerlExitListEntry;
1843
1844 #ifdef PERL_OBJECT
1845 extern "C" CPerlObj* perl_alloc _((IPerlMem*, IPerlEnv*, IPerlStdIO*, IPerlLIO*, IPerlDir*, IPerlSock*, IPerlProc*));
1846
1847 typedef int (CPerlObj::*runops_proc_t) _((void));
1848 #undef EXT
1849 #define EXT
1850 #undef EXTCONST
1851 #define EXTCONST
1852 #undef INIT
1853 #define INIT(x)
1854
1855 class CPerlObj {
1856 public:
1857         CPerlObj(IPerlMem*, IPerlEnv*, IPerlStdIO*, IPerlLIO*, IPerlDir*, IPerlSock*, IPerlProc*);
1858         void Init(void);
1859         void* operator new(size_t nSize, IPerlMem *pvtbl);
1860 #endif /* PERL_OBJECT */
1861
1862 #ifdef PERL_GLOBAL_STRUCT
1863 struct perl_vars {
1864 #include "perlvars.h"
1865 };
1866
1867 #ifdef PERL_CORE
1868 EXT struct perl_vars Perl_Vars;
1869 EXT struct perl_vars *Perl_VarsPtr INIT(&Perl_Vars);
1870 #else
1871 #if !defined(__GNUC__) || !defined(WIN32)
1872 EXT
1873 #endif
1874 struct perl_vars *Perl_VarsPtr;
1875 #define Perl_Vars (*((Perl_VarsPtr) ? Perl_VarsPtr : (Perl_VarsPtr =  Perl_GetVars())))
1876 #endif
1877 #endif /* PERL_GLOBAL_STRUCT */
1878
1879 #ifdef MULTIPLICITY
1880 /* If we have multiple interpreters define a struct 
1881    holding variables which must be per-interpreter
1882    If we don't have threads anything that would have 
1883    be per-thread is per-interpreter.
1884 */
1885
1886 struct interpreter {
1887 #ifndef USE_THREADS
1888 #include "thrdvar.h"
1889 #endif
1890 #include "intrpvar.h"
1891 };
1892
1893 #else
1894 struct interpreter {
1895     char broiled;
1896 };
1897 #endif
1898
1899 #ifdef USE_THREADS
1900 /* If we have threads define a struct with all the variables
1901  * that have to be per-thread
1902  */
1903
1904
1905 struct perl_thread {
1906 #include "thrdvar.h"
1907 };
1908
1909 typedef struct perl_thread *Thread;
1910
1911 #else
1912 typedef void *Thread;
1913 #endif
1914
1915 /* Done with PERLVAR macros for now ... */
1916 #undef PERLVAR
1917 #undef PERLVARI
1918 #undef PERLVARIC
1919
1920 #include "thread.h"
1921 #include "pp.h"
1922 #include "proto.h"
1923
1924 #ifdef EMBED
1925 #define Perl_sv_setptrobj(rv,ptr,name) Perl_sv_setref_iv(rv,name,(IV)ptr)
1926 #define Perl_sv_setptrref(rv,ptr) Perl_sv_setref_iv(rv,Nullch,(IV)ptr)
1927 #else
1928 #define sv_setptrobj(rv,ptr,name) sv_setref_iv(rv,name,(IV)ptr)
1929 #define sv_setptrref(rv,ptr) sv_setref_iv(rv,Nullch,(IV)ptr)
1930 #endif
1931
1932 /* The following must follow proto.h as #defines mess up syntax */
1933
1934 #include "embedvar.h"
1935
1936 /* Now include all the 'global' variables 
1937  * If we don't have threads or multiple interpreters
1938  * these include variables that would have been their struct-s 
1939  */
1940
1941 #define PERLVAR(var,type) EXT type var;
1942 #define PERLVARI(var,type,init) EXT type var INIT(init);
1943 #define PERLVARIC(var,type,init) EXTCONST type var INIT(init);
1944
1945 #ifndef PERL_GLOBAL_STRUCT
1946 #include "perlvars.h"
1947 #endif
1948
1949 #ifndef MULTIPLICITY
1950
1951 #ifndef USE_THREADS
1952 #include "thrdvar.h"
1953 #endif
1954
1955 #include "intrpvar.h"
1956 #endif
1957
1958 #ifdef PERL_OBJECT
1959 };
1960
1961 #include "objpp.h"
1962 #ifdef DOINIT
1963 #include "INTERN.h"
1964 #else
1965 #include "EXTERN.h"
1966 #endif
1967 #endif  /* PERL_OBJECT */
1968
1969
1970 #undef PERLVAR
1971 #undef PERLVARI
1972 #undef PERLVARIC
1973
1974 #if defined(HASATTRIBUTE) && defined(WIN32)
1975 /*
1976  * This provides a layer of functions and macros to ensure extensions will
1977  * get to use the same RTL functions as the core.
1978  * It has to go here or #define of printf messes up __attribute__
1979  * stuff in proto.h  
1980  */
1981 #ifndef PERL_OBJECT
1982 #  include <win32iop.h>
1983 #endif  /* PERL_OBJECT */
1984 #endif  /* WIN32 */
1985
1986 #ifdef DOINIT
1987
1988 EXT MGVTBL vtbl_sv =    {magic_get,
1989                                 magic_set,
1990                                         magic_len,
1991                                                 0,      0};
1992 EXT MGVTBL vtbl_env =   {0,     magic_set_all_env,
1993                                 0,      magic_clear_all_env,
1994                                                         0};
1995 EXT MGVTBL vtbl_envelem =       {0,     magic_setenv,
1996                                         0,      magic_clearenv,
1997                                                         0};
1998 EXT MGVTBL vtbl_sig =   {0,     0,               0, 0, 0};
1999 EXT MGVTBL vtbl_sigelem =       {magic_getsig,
2000                                         magic_setsig,
2001                                         0,      magic_clearsig,
2002                                                         0};
2003 EXT MGVTBL vtbl_pack =  {0,     0,      magic_sizepack, magic_wipepack,
2004                                                         0};
2005 EXT MGVTBL vtbl_packelem =      {magic_getpack,
2006                                 magic_setpack,
2007                                         0,      magic_clearpack,
2008                                                         0};
2009 EXT MGVTBL vtbl_dbline =        {0,     magic_setdbline,
2010                                         0,      0,      0};
2011 EXT MGVTBL vtbl_isa =   {0,     magic_setisa,
2012                                         0,      magic_setisa,
2013                                                         0};
2014 EXT MGVTBL vtbl_isaelem =       {0,     magic_setisa,
2015                                         0,      0,      0};
2016 EXT MGVTBL vtbl_arylen =        {magic_getarylen,
2017                                 magic_setarylen,
2018                                         0,      0,      0};
2019 EXT MGVTBL vtbl_glob =  {magic_getglob,
2020                                 magic_setglob,
2021                                         0,      0,      0};
2022 EXT MGVTBL vtbl_mglob = {0,     magic_setmglob,
2023                                         0,      0,      0};
2024 EXT MGVTBL vtbl_nkeys = {magic_getnkeys,
2025                                 magic_setnkeys,
2026                                         0,      0,      0};
2027 EXT MGVTBL vtbl_taint = {magic_gettaint,magic_settaint,
2028                                         0,      0,      0};
2029 EXT MGVTBL vtbl_substr =        {magic_getsubstr, magic_setsubstr,
2030                                         0,      0,      0};
2031 EXT MGVTBL vtbl_vec =   {magic_getvec,
2032                                 magic_setvec,
2033                                         0,      0,      0};
2034 EXT MGVTBL vtbl_pos =   {magic_getpos,
2035                                 magic_setpos,
2036                                         0,      0,      0};
2037 EXT MGVTBL vtbl_bm =    {0,     magic_setbm,
2038                                         0,      0,      0};
2039 EXT MGVTBL vtbl_fm =    {0,     magic_setfm,
2040                                         0,      0,      0};
2041 EXT MGVTBL vtbl_uvar =  {magic_getuvar,
2042                                 magic_setuvar,
2043                                         0,      0,      0};
2044 #ifdef USE_THREADS
2045 EXT MGVTBL vtbl_mutex = {0,     0,      0,      0,      magic_mutexfree};
2046 #endif /* USE_THREADS */
2047 EXT MGVTBL vtbl_defelem = {magic_getdefelem,magic_setdefelem,
2048                                         0,      0,      magic_freedefelem};
2049
2050 EXT MGVTBL vtbl_regexp = {0,magic_unchain,0,0, magic_freeregexp};
2051
2052 #ifdef USE_LOCALE_COLLATE
2053 EXT MGVTBL vtbl_collxfrm = {0,
2054                                 magic_setcollxfrm,
2055                                         0,      0,      0};
2056 #endif
2057
2058 #ifdef OVERLOAD
2059 EXT MGVTBL vtbl_amagic =       {0,     magic_setamagic,
2060                                         0,      0,      magic_setamagic};
2061 EXT MGVTBL vtbl_amagicelem =   {0,     magic_setamagic,
2062                                         0,      0,      magic_setamagic};
2063 #endif /* OVERLOAD */
2064
2065 #else /* !DOINIT */
2066
2067 EXT MGVTBL vtbl_sv;
2068 EXT MGVTBL vtbl_env;
2069 EXT MGVTBL vtbl_envelem;
2070 EXT MGVTBL vtbl_sig;
2071 EXT MGVTBL vtbl_sigelem;
2072 EXT MGVTBL vtbl_pack;
2073 EXT MGVTBL vtbl_packelem;
2074 EXT MGVTBL vtbl_dbline;
2075 EXT MGVTBL vtbl_isa;
2076 EXT MGVTBL vtbl_isaelem;
2077 EXT MGVTBL vtbl_arylen;
2078 EXT MGVTBL vtbl_glob;
2079 EXT MGVTBL vtbl_mglob;
2080 EXT MGVTBL vtbl_nkeys;
2081 EXT MGVTBL vtbl_taint;
2082 EXT MGVTBL vtbl_substr;
2083 EXT MGVTBL vtbl_vec;
2084 EXT MGVTBL vtbl_pos;
2085 EXT MGVTBL vtbl_bm;
2086 EXT MGVTBL vtbl_fm;
2087 EXT MGVTBL vtbl_uvar;
2088
2089 #ifdef USE_THREADS
2090 EXT MGVTBL vtbl_mutex;
2091 #endif /* USE_THREADS */
2092
2093 EXT MGVTBL vtbl_defelem;
2094 EXT MGVTBL vtbl_regexp;
2095
2096 #ifdef USE_LOCALE_COLLATE
2097 EXT MGVTBL vtbl_collxfrm;
2098 #endif
2099
2100 #ifdef OVERLOAD
2101 EXT MGVTBL vtbl_amagic;
2102 EXT MGVTBL vtbl_amagicelem;
2103 #endif /* OVERLOAD */
2104
2105 #endif /* !DOINIT */
2106
2107 #ifdef OVERLOAD
2108
2109 #define NofAMmeth 58
2110 #ifdef DOINIT
2111 EXTCONST char * AMG_names[NofAMmeth] = {
2112   "fallback",   "abs",                  /* "fallback" should be the first. */
2113   "bool",       "nomethod",
2114   "\"\"",       "0+",
2115   "+",          "+=",
2116   "-",          "-=",
2117   "*",          "*=",
2118   "/",          "/=",
2119   "%",          "%=",
2120   "**",         "**=",
2121   "<<",         "<<=",
2122   ">>",         ">>=",
2123   "&",          "&=",
2124   "|",          "|=",
2125   "^",          "^=",
2126   "<",          "<=",
2127   ">",          ">=",
2128   "==",         "!=",
2129   "<=>",        "cmp",
2130   "lt",         "le",
2131   "gt",         "ge",
2132   "eq",         "ne",
2133   "!",          "~",
2134   "++",         "--",
2135   "atan2",      "cos",
2136   "sin",        "exp",
2137   "log",        "sqrt",
2138   "x",          "x=",
2139   ".",          ".=",
2140   "=",          "neg"
2141 };
2142 #else
2143 EXTCONST char * AMG_names[NofAMmeth];
2144 #endif /* def INITAMAGIC */
2145
2146 struct am_table {
2147   long was_ok_sub;
2148   long was_ok_am;
2149   U32 flags;
2150   CV* table[NofAMmeth];
2151   long fallback;
2152 };
2153 struct am_table_short {
2154   long was_ok_sub;
2155   long was_ok_am;
2156   U32 flags;
2157 };
2158 typedef struct am_table AMT;
2159 typedef struct am_table_short AMTS;
2160
2161 #define AMGfallNEVER    1
2162 #define AMGfallNO       2
2163 #define AMGfallYES      3
2164
2165 #define AMTf_AMAGIC             1
2166 #define AMT_AMAGIC(amt)         ((amt)->flags & AMTf_AMAGIC)
2167 #define AMT_AMAGIC_on(amt)      ((amt)->flags |= AMTf_AMAGIC)
2168 #define AMT_AMAGIC_off(amt)     ((amt)->flags &= ~AMTf_AMAGIC)
2169
2170 enum {
2171   fallback_amg, abs_amg,
2172   bool__amg,    nomethod_amg,
2173   string_amg,   numer_amg,
2174   add_amg,      add_ass_amg,
2175   subtr_amg,    subtr_ass_amg,
2176   mult_amg,     mult_ass_amg,
2177   div_amg,      div_ass_amg,
2178   modulo_amg,   modulo_ass_amg,
2179   pow_amg,      pow_ass_amg,
2180   lshift_amg,   lshift_ass_amg,
2181   rshift_amg,   rshift_ass_amg,
2182   band_amg,     band_ass_amg,
2183   bor_amg,      bor_ass_amg,
2184   bxor_amg,     bxor_ass_amg,
2185   lt_amg,       le_amg,
2186   gt_amg,       ge_amg,
2187   eq_amg,       ne_amg,
2188   ncmp_amg,     scmp_amg,
2189   slt_amg,      sle_amg,
2190   sgt_amg,      sge_amg,
2191   seq_amg,      sne_amg,
2192   not_amg,      compl_amg,
2193   inc_amg,      dec_amg,
2194   atan2_amg,    cos_amg,
2195   sin_amg,      exp_amg,
2196   log_amg,      sqrt_amg,
2197   repeat_amg,   repeat_ass_amg,
2198   concat_amg,   concat_ass_amg,
2199   copy_amg,     neg_amg
2200 };
2201
2202 /*
2203  * some compilers like to redefine cos et alia as faster
2204  * (and less accurate?) versions called F_cos et cetera (Quidquid
2205  * latine dictum sit, altum viditur.)  This trick collides with
2206  * the Perl overloading (amg).  The following #defines fool both.
2207  */
2208
2209 #ifdef _FASTMATH
2210 #   ifdef atan2
2211 #       define F_atan2_amg  atan2_amg
2212 #   endif
2213 #   ifdef cos
2214 #       define F_cos_amg    cos_amg
2215 #   endif
2216 #   ifdef exp
2217 #       define F_exp_amg    exp_amg
2218 #   endif
2219 #   ifdef log
2220 #       define F_log_amg    log_amg
2221 #   endif
2222 #   ifdef pow
2223 #       define F_pow_amg    pow_amg
2224 #   endif
2225 #   ifdef sin
2226 #       define F_sin_amg    sin_amg
2227 #   endif
2228 #   ifdef sqrt
2229 #       define F_sqrt_amg   sqrt_amg
2230 #   endif
2231 #endif /* _FASTMATH */
2232
2233 #endif /* OVERLOAD */
2234
2235 #define PERLDB_ALL      0x3f            /* No _NONAME, _GOTO */
2236 #define PERLDBf_SUB     0x01            /* Debug sub enter/exit. */
2237 #define PERLDBf_LINE    0x02            /* Keep line #. */
2238 #define PERLDBf_NOOPT   0x04            /* Switch off optimizations. */
2239 #define PERLDBf_INTER   0x08            /* Preserve more data for
2240                                            later inspections.  */
2241 #define PERLDBf_SUBLINE 0x10            /* Keep subr source lines. */
2242 #define PERLDBf_SINGLE  0x20            /* Start with single-step on. */
2243 #define PERLDBf_NONAME  0x40            /* For _SUB: no name of the subr. */
2244 #define PERLDBf_GOTO    0x80            /* Report goto: call DB::goto. */
2245
2246 #define PERLDB_SUB      (perldb && (perldb & PERLDBf_SUB))
2247 #define PERLDB_LINE     (perldb && (perldb & PERLDBf_LINE))
2248 #define PERLDB_NOOPT    (perldb && (perldb & PERLDBf_NOOPT))
2249 #define PERLDB_INTER    (perldb && (perldb & PERLDBf_INTER))
2250 #define PERLDB_SUBLINE  (perldb && (perldb & PERLDBf_SUBLINE))
2251 #define PERLDB_SINGLE   (perldb && (perldb & PERLDBf_SINGLE))
2252 #define PERLDB_SUB_NN   (perldb && (perldb & (PERLDBf_NONAME)))
2253 #define PERLDB_GOTO     (perldb && (perldb & PERLDBf_GOTO))
2254
2255
2256 #ifdef USE_LOCALE_NUMERIC
2257
2258 #define SET_NUMERIC_STANDARD() \
2259     STMT_START {                                \
2260         if (! numeric_standard)                 \
2261             perl_set_numeric_standard();        \
2262     } STMT_END
2263
2264 #define SET_NUMERIC_LOCAL() \
2265     STMT_START {                                \
2266         if (! numeric_local)                    \
2267             perl_set_numeric_local();           \
2268     } STMT_END
2269
2270 #else /* !USE_LOCALE_NUMERIC */
2271
2272 #define SET_NUMERIC_STANDARD()  /**/
2273 #define SET_NUMERIC_LOCAL()     /**/
2274
2275 #endif /* !USE_LOCALE_NUMERIC */
2276
2277 #if !defined(PERLIO_IS_STDIO) && defined(HASATTRIBUTE)
2278 /* 
2279  * Now we have __attribute__ out of the way 
2280  * Remap printf 
2281  */
2282 #define printf PerlIO_stdoutf
2283 #endif
2284
2285 #ifndef PERL_SCRIPT_MODE
2286 #define PERL_SCRIPT_MODE "r"
2287 #endif
2288
2289 /*
2290  * nice_chunk and nice_chunk size need to be set
2291  * and queried under the protection of sv_mutex
2292  */
2293 #define offer_nice_chunk(chunk, chunk_size) do {        \
2294         LOCK_SV_MUTEX;                                  \
2295         if (!nice_chunk) {                              \
2296             nice_chunk = (char*)(chunk);                \
2297             nice_chunk_size = (chunk_size);             \
2298         }                                               \
2299         else {                                          \
2300             Safefree(chunk);                            \
2301         }                                               \
2302         UNLOCK_SV_MUTEX;                                \
2303     } while (0)
2304
2305 #ifdef HAS_SEM
2306 #   include <sys/ipc.h>
2307 #   include <sys/sem.h>
2308 #   ifndef HAS_UNION_SEMUN      /* Provide the union semun. */
2309     union semun {
2310         int val;
2311         struct semid_ds *buf;
2312         unsigned short *array;
2313     };
2314 #   endif
2315 #   ifdef USE_SEMCTL_SEMUN
2316 #       define Semctl(id, num, cmd, semun) semctl(id, num, cmd, semun)
2317 #   else
2318 #       ifdef USE_SEMCTL_SEMID_DS
2319 #           define Semctl(id, num, cmd, semun) semctl(id, num, cmd, semun.buf)
2320 #       endif
2321 #   endif
2322 #   ifndef Semctl       /* Place our bets on the semun horse. */
2323 #       define Semctl(id, num, cmd, semun) semctl(id, num, cmd, semun)
2324 #   endif
2325 #endif
2326
2327 #endif /* Include guard */