get it building again on win32
[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 iperlsys.h
94 for more 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 CPERLarg CPerlObj *pPerl
109 #define CPERLarg_ CPERLarg,
110 #define _CPERLarg ,CPERLarg
111 #define PERL_OBJECT_THIS this
112 #define _PERL_OBJECT_THIS ,this
113 #define PERL_OBJECT_THIS_ this,
114 #define CALLRUNOPS (this->*runops)
115 #define CALLREGCOMP (this->*regcompp)
116 #define CALLREGEXEC (this->*regexecp)
117
118 #else /* !PERL_OBJECT */
119
120 #define STATIC static
121 #define CPERLscope(x) x
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 #define CALLREGCOMP (*regcompp)
132 #define CALLREGEXEC (*regexecp)
133
134 #endif /* PERL_OBJECT */
135
136 #define VOIDUSED 1
137 #include "config.h"
138
139 #include "embed.h"
140
141 #undef START_EXTERN_C
142 #undef END_EXTERN_C
143 #undef EXTERN_C
144 #ifdef __cplusplus
145 #  define START_EXTERN_C extern "C" {
146 #  define END_EXTERN_C }
147 #  define EXTERN_C extern "C"
148 #else
149 #  define START_EXTERN_C 
150 #  define END_EXTERN_C 
151 #  define EXTERN_C
152 #endif
153
154 #ifdef OP_IN_REGISTER
155 #  ifdef __GNUC__
156 #    define stringify_immed(s) #s
157 #    define stringify(s) stringify_immed(s)
158 #ifdef EMBED
159 register struct op *Perl_op asm(stringify(OP_IN_REGISTER));
160 #else
161 register struct op *op asm(stringify(OP_IN_REGISTER));
162 #endif
163 #  endif
164 #endif
165
166 /*
167  * STMT_START { statements; } STMT_END;
168  * can be used as a single statement, as in
169  * if (x) STMT_START { ... } STMT_END; else ...
170  *
171  * Trying to select a version that gives no warnings...
172  */
173 #if !(defined(STMT_START) && defined(STMT_END))
174 # if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(__cplusplus)
175 #   define STMT_START   (void)( /* gcc supports ``({ STATEMENTS; })'' */
176 #   define STMT_END     )
177 # else
178    /* Now which other defined()s do we need here ??? */
179 #  if (VOIDFLAGS) && (defined(sun) || defined(__sun__))
180 #   define STMT_START   if (1)
181 #   define STMT_END     else (void)0
182 #  else
183 #   define STMT_START   do
184 #   define STMT_END     while (0)
185 #  endif
186 # endif
187 #endif
188
189 #define NOOP (void)0
190
191 #define WITH_THR(s) STMT_START { dTHR; s; } STMT_END
192
193 /*
194  * SOFT_CAST can be used for args to prototyped functions to retain some
195  * type checking; it only casts if the compiler does not know prototypes.
196  */
197 #if defined(CAN_PROTOTYPE) && defined(DEBUGGING_COMPILE)
198 #define SOFT_CAST(type) 
199 #else
200 #define SOFT_CAST(type) (type)
201 #endif
202
203 #ifndef BYTEORDER  /* Should never happen -- byteorder is in config.h */
204 #   define BYTEORDER 0x1234
205 #endif
206
207 /* Overall memory policy? */
208 #ifndef CONSERVATIVE
209 #   define LIBERAL 1
210 #endif
211
212 /*
213  * The following contortions are brought to you on behalf of all the
214  * standards, semi-standards, de facto standards, not-so-de-facto standards
215  * of the world, as well as all the other botches anyone ever thought of.
216  * The basic theory is that if we work hard enough here, the rest of the
217  * code can be a lot prettier.  Well, so much for theory.  Sorry, Henry...
218  */
219
220 /* define this once if either system, instead of cluttering up the src */
221 #if defined(MSDOS) || defined(atarist) || defined(WIN32)
222 #define DOSISH 1
223 #endif
224
225 #if defined(__STDC__) || defined(vax11c) || defined(_AIX) || defined(__stdc__) || defined(__cplusplus)
226 # define STANDARD_C 1
227 #endif
228
229 #if defined(__cplusplus) || defined(WIN32) || defined(__sgi) || defined(OS2) || defined(__DGUX)
230 # define DONT_DECLARE_STD 1
231 #endif
232
233 #if defined(HASVOLATILE) || defined(STANDARD_C)
234 #   ifdef __cplusplus
235 #       define VOL              // to temporarily suppress warnings
236 #   else
237 #       define VOL volatile
238 #   endif
239 #else
240 #   define VOL
241 #endif
242
243 #define TAINT           (tainted = TRUE)
244 #define TAINT_NOT       (tainted = FALSE)
245 #define TAINT_IF(c)     if (c) { tainted = TRUE; }
246 #define TAINT_ENV()     if (tainting) { taint_env(); }
247 #define TAINT_PROPER(s) if (tainting) { taint_proper(no_security, s); }
248
249 /* XXX All process group stuff is handled in pp_sys.c.  Should these 
250    defines move there?  If so, I could simplify this a lot. --AD  9/96.
251 */
252 /* Process group stuff changed from traditional BSD to POSIX.
253    perlfunc.pod documents the traditional BSD-style syntax, so we'll
254    try to preserve that, if possible.
255 */
256 #ifdef HAS_SETPGID
257 #  define BSD_SETPGRP(pid, pgrp)        setpgid((pid), (pgrp))
258 #else
259 #  if defined(HAS_SETPGRP) && defined(USE_BSD_SETPGRP)
260 #    define BSD_SETPGRP(pid, pgrp)      setpgrp((pid), (pgrp))
261 #  else
262 #    ifdef HAS_SETPGRP2  /* DG/UX */
263 #      define BSD_SETPGRP(pid, pgrp)    setpgrp2((pid), (pgrp))
264 #    endif
265 #  endif
266 #endif
267 #if defined(BSD_SETPGRP) && !defined(HAS_SETPGRP)
268 #  define HAS_SETPGRP  /* Well, effectively it does . . . */
269 #endif
270
271 /* getpgid isn't POSIX, but at least Solaris and Linux have it, and it makes
272     our life easier :-) so we'll try it.
273 */
274 #ifdef HAS_GETPGID
275 #  define BSD_GETPGRP(pid)              getpgid((pid))
276 #else
277 #  if defined(HAS_GETPGRP) && defined(USE_BSD_GETPGRP)
278 #    define BSD_GETPGRP(pid)            getpgrp((pid))
279 #  else
280 #    ifdef HAS_GETPGRP2  /* DG/UX */
281 #      define BSD_GETPGRP(pid)          getpgrp2((pid))
282 #    endif
283 #  endif
284 #endif
285 #if defined(BSD_GETPGRP) && !defined(HAS_GETPGRP)
286 #  define HAS_GETPGRP  /* Well, effectively it does . . . */
287 #endif
288
289 /* These are not exact synonyms, since setpgrp() and getpgrp() may 
290    have different behaviors, but perl.h used to define USE_BSDPGRP
291    (prior to 5.003_05) so some extension might depend on it.
292 */
293 #if defined(USE_BSD_SETPGRP) || defined(USE_BSD_GETPGRP)
294 #  ifndef USE_BSDPGRP
295 #    define USE_BSDPGRP
296 #  endif
297 #endif
298
299 #ifndef _TYPES_         /* If types.h defines this it's easy. */
300 #   ifndef major                /* Does everyone's types.h define this? */
301 #       include <sys/types.h>
302 #   endif
303 #endif
304
305 #ifdef __cplusplus
306 #  ifndef I_STDARG
307 #    define I_STDARG 1
308 #  endif
309 #endif
310
311 #ifdef I_STDARG
312 #  include <stdarg.h>
313 #else
314 #  ifdef I_VARARGS
315 #    include <varargs.h>
316 #  endif
317 #endif
318
319 #include "iperlsys.h"
320
321 #ifdef USE_NEXT_CTYPE
322
323 #if NX_CURRENT_COMPILER_RELEASE >= 400
324 #include <objc/NXCType.h>
325 #else /*  NX_CURRENT_COMPILER_RELEASE < 400 */
326 #include <appkit/NXCType.h>
327 #endif /*  NX_CURRENT_COMPILER_RELEASE >= 400 */
328
329 #else /* !USE_NEXT_CTYPE */
330 #include <ctype.h>
331 #endif /* USE_NEXT_CTYPE */
332
333 #ifdef METHOD   /* Defined by OSF/1 v3.0 by ctype.h */
334 #undef METHOD
335 #endif
336
337 #ifdef I_LOCALE
338 #   include <locale.h>
339 #endif
340
341 #if !defined(NO_LOCALE) && defined(HAS_SETLOCALE)
342 #   define USE_LOCALE
343 #   if !defined(NO_LOCALE_COLLATE) && defined(LC_COLLATE) \
344        && defined(HAS_STRXFRM)
345 #       define USE_LOCALE_COLLATE
346 #   endif
347 #   if !defined(NO_LOCALE_CTYPE) && defined(LC_CTYPE)
348 #       define USE_LOCALE_CTYPE
349 #   endif
350 #   if !defined(NO_LOCALE_NUMERIC) && defined(LC_NUMERIC)
351 #       define USE_LOCALE_NUMERIC
352 #   endif
353 #endif /* !NO_LOCALE && HAS_SETLOCALE */
354
355 #include <setjmp.h>
356
357 #ifdef I_SYS_PARAM
358 #   ifdef PARAM_NEEDS_TYPES
359 #       include <sys/types.h>
360 #   endif
361 #   include <sys/param.h>
362 #endif
363
364
365 /* Use all the "standard" definitions? */
366 #if defined(STANDARD_C) && defined(I_STDLIB)
367 #   include <stdlib.h>
368 #endif
369
370 #define MEM_SIZE Size_t
371
372 /* This comes after <stdlib.h> so we don't try to change the standard
373  * library prototypes; we'll use our own in proto.h instead. */
374
375 #ifdef MYMALLOC
376
377 #   ifdef HIDEMYMALLOC
378 #       define malloc  Mymalloc
379 #       define calloc  Mycalloc
380 #       define realloc Myrealloc
381 #       define free    Myfree
382 Malloc_t Mymalloc _((MEM_SIZE nbytes));
383 Malloc_t Mycalloc _((MEM_SIZE elements, MEM_SIZE size));
384 Malloc_t Myrealloc _((Malloc_t where, MEM_SIZE nbytes));
385 Free_t   Myfree _((Malloc_t where));
386 #   endif
387 #   ifdef EMBEDMYMALLOC
388 #       define malloc  Perl_malloc
389 #       define calloc  Perl_calloc
390 #       define realloc Perl_realloc
391 /* VMS' external symbols are case-insensitive, and there's already a */
392 /* perl_free in perl.h */
393 #ifdef VMS
394 #       define free    Perl_myfree
395 #else
396 #       define free    Perl_free
397 #endif
398 Malloc_t Perl_malloc _((MEM_SIZE nbytes));
399 Malloc_t Perl_calloc _((MEM_SIZE elements, MEM_SIZE size));
400 Malloc_t Perl_realloc _((Malloc_t where, MEM_SIZE nbytes));
401 #ifdef VMS
402 Free_t   Perl_myfree _((Malloc_t where));
403 #else
404 Free_t   Perl_free _((Malloc_t where));
405 #endif
406 #   endif
407
408 #   undef safemalloc
409 #   undef safecalloc
410 #   undef saferealloc
411 #   undef safefree
412 #   define safemalloc  malloc
413 #   define safecalloc  calloc
414 #   define saferealloc realloc
415 #   define safefree    free
416
417 #endif /* MYMALLOC */
418
419 #if defined(STANDARD_C) && defined(I_STDDEF)
420 #   include <stddef.h>
421 #   define STRUCT_OFFSET(s,m)  offsetof(s,m)
422 #else
423 #   define STRUCT_OFFSET(s,m)  (Size_t)(&(((s *)0)->m))
424 #endif
425
426 #if defined(I_STRING) || defined(__cplusplus)
427 #   include <string.h>
428 #else
429 #   include <strings.h>
430 #endif
431
432 #if !defined(HAS_STRCHR) && defined(HAS_INDEX) && !defined(strchr)
433 #define strchr index
434 #define strrchr rindex
435 #endif
436
437 #ifdef I_MEMORY
438 #  include <memory.h>
439 #endif
440
441 #ifdef HAS_MEMCPY
442 #  if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY)
443 #    ifndef memcpy
444         extern char * memcpy _((char*, char*, int));
445 #    endif
446 #  endif
447 #else
448 #   ifndef memcpy
449 #       ifdef HAS_BCOPY
450 #           define memcpy(d,s,l) bcopy(s,d,l)
451 #       else
452 #           define memcpy(d,s,l) my_bcopy(s,d,l)
453 #       endif
454 #   endif
455 #endif /* HAS_MEMCPY */
456
457 #ifdef HAS_MEMSET
458 #  if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY)
459 #    ifndef memset
460         extern char *memset _((char*, int, int));
461 #    endif
462 #  endif
463 #else
464 #  define memset(d,c,l) my_memset(d,c,l)
465 #endif /* HAS_MEMSET */
466
467 #if !defined(HAS_MEMMOVE) && !defined(memmove)
468 #   if defined(HAS_BCOPY) && defined(HAS_SAFE_BCOPY)
469 #       define memmove(d,s,l) bcopy(s,d,l)
470 #   else
471 #       if defined(HAS_MEMCPY) && defined(HAS_SAFE_MEMCPY)
472 #           define memmove(d,s,l) memcpy(d,s,l)
473 #       else
474 #           define memmove(d,s,l) my_bcopy(s,d,l)
475 #       endif
476 #   endif
477 #endif
478
479 #if defined(mips) && defined(ultrix) && !defined(__STDC__)
480 #   undef HAS_MEMCMP
481 #endif
482
483 #if defined(HAS_MEMCMP) && defined(HAS_SANE_MEMCMP)
484 #  if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY)
485 #    ifndef memcmp
486         extern int memcmp _((char*, char*, int));
487 #    endif
488 #  endif
489 #  ifdef BUGGY_MSC
490   #  pragma function(memcmp)
491 #  endif
492 #else
493 #   ifndef memcmp
494 #       define memcmp   my_memcmp
495 #   endif
496 #endif /* HAS_MEMCMP && HAS_SANE_MEMCMP */
497
498 #ifndef memzero
499 #   ifdef HAS_MEMSET
500 #       define memzero(d,l) memset(d,0,l)
501 #   else
502 #       ifdef HAS_BZERO
503 #           define memzero(d,l) bzero(d,l)
504 #       else
505 #           define memzero(d,l) my_bzero(d,l)
506 #       endif
507 #   endif
508 #endif
509
510 #ifndef HAS_BCMP
511 #   ifndef bcmp
512 #       define bcmp(s1,s2,l) memcmp(s1,s2,l)
513 #   endif
514 #endif /* !HAS_BCMP */
515
516 #ifdef I_NETINET_IN
517 #   include <netinet/in.h>
518 #endif
519
520 #if defined(SF_APPEND) && defined(USE_SFIO) && defined(I_SFIO)
521 /* <sfio.h> defines SF_APPEND and <sys/stat.h> might define SF_APPEND
522  * (the neo-BSD seem to do this).  */
523 #   undef SF_APPEND
524 #endif
525
526 #ifdef I_SYS_STAT
527 #   include <sys/stat.h>
528 #endif
529
530 /* The stat macros for Amdahl UTS, Unisoft System V/88 (and derivatives
531    like UTekV) are broken, sometimes giving false positives.  Undefine
532    them here and let the code below set them to proper values.
533
534    The ghs macro stands for GreenHills Software C-1.8.5 which
535    is the C compiler for sysV88 and the various derivatives.
536    This header file bug is corrected in gcc-2.5.8 and later versions.
537    --Kaveh Ghazi (ghazi@noc.rutgers.edu) 10/3/94.  */
538
539 #if defined(uts) || (defined(m88k) && defined(ghs))
540 #   undef S_ISDIR
541 #   undef S_ISCHR
542 #   undef S_ISBLK
543 #   undef S_ISREG
544 #   undef S_ISFIFO
545 #   undef S_ISLNK
546 #endif
547
548 #ifdef I_TIME
549 #   include <time.h>
550 #endif
551
552 #ifdef I_SYS_TIME
553 #   ifdef I_SYS_TIME_KERNEL
554 #       define KERNEL
555 #   endif
556 #   include <sys/time.h>
557 #   ifdef I_SYS_TIME_KERNEL
558 #       undef KERNEL
559 #   endif
560 #endif
561
562 #if defined(HAS_TIMES) && defined(I_SYS_TIMES)
563 #    include <sys/times.h>
564 #endif
565
566 #if defined(HAS_STRERROR) && (!defined(HAS_MKDIR) || !defined(HAS_RMDIR))
567 #   undef HAS_STRERROR
568 #endif
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    /* pending resolution of licensing issues, we avoid the erstwhile
1123     * atomic.h everywhere */
1124 #  define EMULATE_ATOMIC_REFCOUNTS
1125
1126 #  ifdef FAKE_THREADS
1127 #    include "fakethr.h"
1128 #  else
1129 #    ifdef WIN32
1130 #      include <win32thread.h>
1131 #    else
1132 #      ifdef OS2
1133 #        include "os2thread.h"
1134 #      else
1135 #        include <pthread.h>
1136 typedef pthread_t perl_os_thread;
1137 typedef pthread_mutex_t perl_mutex;
1138 typedef pthread_cond_t perl_cond;
1139 typedef pthread_key_t perl_key;
1140 #      endif /* OS2 */
1141 #    endif /* WIN32 */
1142 #  endif /* FAKE_THREADS */
1143 #endif /* USE_THREADS */
1144
1145
1146   
1147 #ifdef VMS
1148 #   define STATUS_NATIVE        statusvalue_vms
1149 #   define STATUS_NATIVE_EXPORT \
1150         ((I32)statusvalue_vms == -1 ? 44 : statusvalue_vms)
1151 #   define STATUS_NATIVE_SET(n)                                         \
1152         STMT_START {                                                    \
1153             statusvalue_vms = (n);                                      \
1154             if ((I32)statusvalue_vms == -1)                             \
1155                 statusvalue = -1;                                       \
1156             else if (statusvalue_vms & STS$M_SUCCESS)                   \
1157                 statusvalue = 0;                                        \
1158             else if ((statusvalue_vms & STS$M_SEVERITY) == 0)           \
1159                 statusvalue = 1 << 8;                                   \
1160             else                                                        \
1161                 statusvalue = (statusvalue_vms & STS$M_SEVERITY) << 8;  \
1162         } STMT_END
1163 #   define STATUS_POSIX statusvalue
1164 #   ifdef VMSISH_STATUS
1165 #       define STATUS_CURRENT   (VMSISH_STATUS ? STATUS_NATIVE : STATUS_POSIX)
1166 #   else
1167 #       define STATUS_CURRENT   STATUS_POSIX
1168 #   endif
1169 #   define STATUS_POSIX_SET(n)                          \
1170         STMT_START {                                    \
1171             statusvalue = (n);                          \
1172             if (statusvalue != -1) {                    \
1173                 statusvalue &= 0xFFFF;                  \
1174                 statusvalue_vms = statusvalue ? 44 : 1; \
1175             }                                           \
1176             else statusvalue_vms = -1;                  \
1177         } STMT_END
1178 #   define STATUS_ALL_SUCCESS   (statusvalue = 0, statusvalue_vms = 1)
1179 #   define STATUS_ALL_FAILURE   (statusvalue = 1, statusvalue_vms = 44)
1180 #else
1181 #   define STATUS_NATIVE        STATUS_POSIX
1182 #   define STATUS_NATIVE_EXPORT STATUS_POSIX
1183 #   define STATUS_NATIVE_SET    STATUS_POSIX_SET
1184 #   define STATUS_POSIX         statusvalue
1185 #   define STATUS_POSIX_SET(n)          \
1186         STMT_START {                    \
1187             statusvalue = (n);          \
1188             if (statusvalue != -1)      \
1189                 statusvalue &= 0xFFFF;  \
1190         } STMT_END
1191 #   define STATUS_CURRENT STATUS_POSIX
1192 #   define STATUS_ALL_SUCCESS   (statusvalue = 0)
1193 #   define STATUS_ALL_FAILURE   (statusvalue = 1)
1194 #endif
1195
1196 /* Some unistd.h's give a prototype for pause() even though
1197    HAS_PAUSE ends up undefined.  This causes the #define
1198    below to be rejected by the compmiler.  Sigh.
1199 */
1200 #ifdef HAS_PAUSE
1201 #define Pause   pause
1202 #else
1203 #define Pause() sleep((32767<<16)+32767)
1204 #endif
1205
1206 #ifndef IOCPARM_LEN
1207 #   ifdef IOCPARM_MASK
1208         /* on BSDish systes we're safe */
1209 #       define IOCPARM_LEN(x)  (((x) >> 16) & IOCPARM_MASK)
1210 #   else
1211         /* otherwise guess at what's safe */
1212 #       define IOCPARM_LEN(x)   256
1213 #   endif
1214 #endif
1215
1216 #ifdef UNION_ANY_DEFINITION
1217 UNION_ANY_DEFINITION;
1218 #else
1219 union any {
1220     void*       any_ptr;
1221     I32         any_i32;
1222     IV          any_iv;
1223     long        any_long;
1224     void        (CPERLscope(*any_dptr)) _((void*));
1225 };
1226 #endif
1227
1228 #ifdef USE_THREADS
1229 #define ARGSproto struct perl_thread *thr
1230 #else
1231 #define ARGSproto void
1232 #endif /* USE_THREADS */
1233
1234 /* Work around some cygwin32 problems with importing global symbols */
1235 #if defined(CYGWIN32) && defined(DLLIMPORT) 
1236 #   include "cw32imp.h"
1237 #endif
1238
1239 #include "regexp.h"
1240 #include "sv.h"
1241 #include "util.h"
1242 #include "form.h"
1243 #include "gv.h"
1244 #include "cv.h"
1245 #include "opcode.h"
1246 #include "op.h"
1247 #include "cop.h"
1248 #include "av.h"
1249 #include "hv.h"
1250 #include "mg.h"
1251 #include "scope.h"
1252 #include "bytecode.h"
1253 #include "byterun.h"
1254
1255 /* Current curly descriptor */
1256 typedef struct curcur CURCUR;
1257 struct curcur {
1258     int         parenfloor;     /* how far back to strip paren data */
1259     int         cur;            /* how many instances of scan we've matched */
1260     int         min;            /* the minimal number of scans to match */
1261     int         max;            /* the maximal number of scans to match */
1262     int         minmod;         /* whether to work our way up or down */
1263     regnode *   scan;           /* the thing to match */
1264     regnode *   next;           /* what has to match after it */
1265     char *      lastloc;        /* where we started matching this scan */
1266     CURCUR *    oldcc;          /* current curly before we started this one */
1267 };
1268
1269 typedef struct _sublex_info SUBLEXINFO;
1270 struct _sublex_info {
1271     I32 super_state;    /* lexer state to save */
1272     I32 sub_inwhat;     /* "lex_inwhat" to use */
1273     OP *sub_op;         /* "lex_op" to use */
1274 };
1275
1276 #ifdef PERL_OBJECT
1277 struct magic_state {
1278     SV* mgs_sv;
1279     U32 mgs_flags;
1280 };
1281 typedef struct magic_state MGS;
1282
1283 typedef struct {
1284     I32 len_min;
1285     I32 len_delta;
1286     I32 pos_min;
1287     I32 pos_delta;
1288     SV *last_found;
1289     I32 last_end;                       /* min value, <0 unless valid. */
1290     I32 last_start_min;
1291     I32 last_start_max;
1292     SV **longest;                       /* Either &l_fixed, or &l_float. */
1293     SV *longest_fixed;
1294     I32 offset_fixed;
1295     SV *longest_float;
1296     I32 offset_float_min;
1297     I32 offset_float_max;
1298     I32 flags;
1299 } scan_data_t;
1300
1301 typedef I32 CHECKPOINT;
1302 #endif /* PERL_OBJECT */
1303
1304 /* work around some libPW problems */
1305 #ifdef DOINIT
1306 EXT char Error[1];
1307 #endif
1308
1309 #if defined(iAPX286) || defined(M_I286) || defined(I80286)
1310 #   define I286
1311 #endif
1312
1313 #if defined(htonl) && !defined(HAS_HTONL)
1314 #define HAS_HTONL
1315 #endif
1316 #if defined(htons) && !defined(HAS_HTONS)
1317 #define HAS_HTONS
1318 #endif
1319 #if defined(ntohl) && !defined(HAS_NTOHL)
1320 #define HAS_NTOHL
1321 #endif
1322 #if defined(ntohs) && !defined(HAS_NTOHS)
1323 #define HAS_NTOHS
1324 #endif
1325 #ifndef HAS_HTONL
1326 #if (BYTEORDER & 0xffff) != 0x4321
1327 #define HAS_HTONS
1328 #define HAS_HTONL
1329 #define HAS_NTOHS
1330 #define HAS_NTOHL
1331 #define MYSWAP
1332 #define htons my_swap
1333 #define htonl my_htonl
1334 #define ntohs my_swap
1335 #define ntohl my_ntohl
1336 #endif
1337 #else
1338 #if (BYTEORDER & 0xffff) == 0x4321
1339 #undef HAS_HTONS
1340 #undef HAS_HTONL
1341 #undef HAS_NTOHS
1342 #undef HAS_NTOHL
1343 #endif
1344 #endif
1345
1346 /*
1347  * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1348  * -DWS
1349  */
1350 #if BYTEORDER != 0x1234
1351 # define HAS_VTOHL
1352 # define HAS_VTOHS
1353 # define HAS_HTOVL
1354 # define HAS_HTOVS
1355 # if BYTEORDER == 0x4321
1356 #  define vtohl(x)      ((((x)&0xFF)<<24)       \
1357                         +(((x)>>24)&0xFF)       \
1358                         +(((x)&0x0000FF00)<<8)  \
1359                         +(((x)&0x00FF0000)>>8)  )
1360 #  define vtohs(x)      ((((x)&0xFF)<<8) + (((x)>>8)&0xFF))
1361 #  define htovl(x)      vtohl(x)
1362 #  define htovs(x)      vtohs(x)
1363 # endif
1364         /* otherwise default to functions in util.c */
1365 #endif
1366
1367 #ifdef CASTNEGFLOAT
1368 #define U_S(what) ((U16)(what))
1369 #define U_I(what) ((unsigned int)(what))
1370 #define U_L(what) ((U32)(what))
1371 #else
1372 EXTERN_C U32 cast_ulong _((double));
1373 #define U_S(what) ((U16)cast_ulong((double)(what)))
1374 #define U_I(what) ((unsigned int)cast_ulong((double)(what)))
1375 #define U_L(what) (cast_ulong((double)(what)))
1376 #endif
1377
1378 #ifdef CASTI32
1379 #define I_32(what) ((I32)(what))
1380 #define I_V(what) ((IV)(what))
1381 #define U_V(what) ((UV)(what))
1382 #else
1383 START_EXTERN_C
1384 I32 cast_i32 _((double));
1385 IV cast_iv _((double));
1386 UV cast_uv _((double));
1387 END_EXTERN_C
1388 #define I_32(what) (cast_i32((double)(what)))
1389 #define I_V(what) (cast_iv((double)(what)))
1390 #define U_V(what) (cast_uv((double)(what)))
1391 #endif
1392
1393 struct Outrec {
1394     I32         o_lines;
1395     char        *o_str;
1396     U32         o_len;
1397 };
1398
1399 #ifndef MAXSYSFD
1400 #   define MAXSYSFD 2
1401 #endif
1402
1403 #ifndef TMPPATH
1404 #  define TMPPATH "/tmp/perl-eXXXXXX"
1405 #endif
1406
1407 #ifndef __cplusplus
1408 Uid_t getuid _((void));
1409 Uid_t geteuid _((void));
1410 Gid_t getgid _((void));
1411 Gid_t getegid _((void));
1412 #endif
1413
1414 #ifdef DEBUGGING
1415 #ifndef Perl_debug_log
1416 #define Perl_debug_log  PerlIO_stderr()
1417 #endif
1418 #define YYDEBUG 1
1419 #define DEB(a)                          a
1420 #define DEBUG(a)   if (debug)           a
1421 #define DEBUG_p(a) if (debug & 1)       a
1422 #define DEBUG_s(a) if (debug & 2)       a
1423 #define DEBUG_l(a) if (debug & 4)       a
1424 #define DEBUG_t(a) if (debug & 8)       a
1425 #define DEBUG_o(a) if (debug & 16)      a
1426 #define DEBUG_c(a) if (debug & 32)      a
1427 #define DEBUG_P(a) if (debug & 64)      a
1428 #define DEBUG_m(a) if (curinterp && debug & 128)        a
1429 #define DEBUG_f(a) if (debug & 256)     a
1430 #define DEBUG_r(a) if (debug & 512)     a
1431 #define DEBUG_x(a) if (debug & 1024)    a
1432 #define DEBUG_u(a) if (debug & 2048)    a
1433 #define DEBUG_L(a) if (debug & 4096)    a
1434 #define DEBUG_H(a) if (debug & 8192)    a
1435 #define DEBUG_X(a) if (debug & 16384)   a
1436 #define DEBUG_D(a) if (debug & 32768)   a
1437 #else
1438 #define DEB(a)
1439 #define DEBUG(a)
1440 #define DEBUG_p(a)
1441 #define DEBUG_s(a)
1442 #define DEBUG_l(a)
1443 #define DEBUG_t(a)
1444 #define DEBUG_o(a)
1445 #define DEBUG_c(a)
1446 #define DEBUG_P(a)
1447 #define DEBUG_m(a)
1448 #define DEBUG_f(a)
1449 #define DEBUG_r(a)
1450 #define DEBUG_x(a)
1451 #define DEBUG_u(a)
1452 #define DEBUG_L(a)
1453 #define DEBUG_H(a)
1454 #define DEBUG_X(a)
1455 #define DEBUG_D(a)
1456 #endif
1457 #define YYMAXDEPTH 300
1458
1459 #ifndef assert  /* <assert.h> might have been included somehow */
1460 #define assert(what)    DEB( {                                          \
1461         if (!(what)) {                                                  \
1462             croak("Assertion failed: file \"%s\", line %d",             \
1463                 __FILE__, __LINE__);                                    \
1464             PerlProc_exit(1);                                                   \
1465         }})
1466 #endif
1467
1468 struct ufuncs {
1469     I32 (*uf_val)_((IV, SV*));
1470     I32 (*uf_set)_((IV, SV*));
1471     IV uf_index;
1472 };
1473
1474 /* Fix these up for __STDC__ */
1475 #ifndef DONT_DECLARE_STD
1476 char *mktemp _((char*));
1477 double atof _((const char*));
1478 #endif
1479
1480 #ifndef STANDARD_C
1481 /* All of these are in stdlib.h or time.h for ANSI C */
1482 Time_t time();
1483 struct tm *gmtime(), *localtime();
1484 char *strchr(), *strrchr();
1485 char *strcpy(), *strcat();
1486 #endif /* ! STANDARD_C */
1487
1488
1489 #ifdef I_MATH
1490 #    include <math.h>
1491 #else
1492 START_EXTERN_C
1493             double exp _((double));
1494             double log _((double));
1495             double log10 _((double));
1496             double sqrt _((double));
1497             double frexp _((double,int*));
1498             double ldexp _((double,int));
1499             double modf _((double,double*));
1500             double sin _((double));
1501             double cos _((double));
1502             double atan2 _((double,double));
1503             double pow _((double,double));
1504 END_EXTERN_C
1505 #endif
1506
1507 #ifndef __cplusplus
1508 #  ifdef __NeXT__ /* or whatever catches all NeXTs */
1509 char *crypt ();       /* Maybe more hosts will need the unprototyped version */
1510 #  else
1511 #    if !defined(WIN32) || !defined(HAVE_DES_FCRYPT)
1512 char *crypt _((const char*, const char*));
1513 #    endif /* !WIN32 && !HAVE_CRYPT_SOURCE */
1514 #  endif /* !__NeXT__ */
1515 #  ifndef DONT_DECLARE_STD
1516 #    ifndef getenv
1517 char *getenv _((const char*));
1518 #    endif /* !getenv */
1519 Off_t lseek _((int,Off_t,int));
1520 #  endif /* !DONT_DECLARE_STD */
1521 char *getlogin _((void));
1522 #endif /* !__cplusplus */
1523
1524 #ifdef UNLINK_ALL_VERSIONS /* Currently only makes sense for VMS */
1525 #define UNLINK unlnk
1526 I32 unlnk _((char*));
1527 #else
1528 #define UNLINK unlink
1529 #endif
1530
1531 #ifndef HAS_SETREUID
1532 #  ifdef HAS_SETRESUID
1533 #    define setreuid(r,e) setresuid(r,e,(Uid_t)-1)
1534 #    define HAS_SETREUID
1535 #  endif
1536 #endif
1537 #ifndef HAS_SETREGID
1538 #  ifdef HAS_SETRESGID
1539 #    define setregid(r,e) setresgid(r,e,(Gid_t)-1)
1540 #    define HAS_SETREGID
1541 #  endif
1542 #endif
1543
1544 typedef Signal_t (*Sighandler_t) _((int));
1545
1546 #ifdef HAS_SIGACTION
1547 typedef struct sigaction Sigsave_t;
1548 #else
1549 typedef Sighandler_t Sigsave_t;
1550 #endif
1551
1552 #define SCAN_DEF 0
1553 #define SCAN_TR 1
1554 #define SCAN_REPL 2
1555
1556 #ifdef DEBUGGING
1557 # ifndef register
1558 #  define register
1559 # endif
1560 # define PAD_SV(po) pad_sv(po)
1561 # define RUNOPS_DEFAULT runops_debug
1562 #else
1563 # define PAD_SV(po) curpad[po]
1564 # define RUNOPS_DEFAULT runops_standard
1565 #endif
1566
1567 #ifdef MYMALLOC
1568 #  define MALLOC_INIT MUTEX_INIT(&malloc_mutex)
1569 #  define MALLOC_TERM MUTEX_DESTROY(&malloc_mutex)
1570 #else
1571 #  define MALLOC_INIT
1572 #  define MALLOC_TERM
1573 #endif
1574
1575
1576 /*
1577  * These need prototyping here because <proto.h> isn't
1578  * included until after runops is initialised.
1579  */
1580
1581 #ifndef PERL_OBJECT
1582 typedef int runops_proc_t _((void));
1583 int runops_standard _((void));
1584 #ifdef DEBUGGING
1585 int runops_debug _((void));
1586 #endif
1587 #endif  /* PERL_OBJECT */
1588
1589 /* _ (for $_) must be first in the following list (DEFSV requires it) */
1590 #define THREADSV_NAMES "_123456789&`'+/.,\\\";^-%=|~:\001\005!@"
1591
1592 /* VMS doesn't use environ array and NeXT has problems with crt0.o globals */
1593 #if !defined(VMS) && !(defined(NeXT) && defined(__DYNAMIC__))
1594 #if !defined(DONT_DECLARE_STD) \
1595         || (defined(__svr4__) && defined(__GNUC__) && defined(sun)) \
1596         || defined(__sgi) || defined(__DGUX)
1597 extern char **  environ;        /* environment variables supplied via exec */
1598 #endif
1599 #else
1600 #  if defined(NeXT) && defined(__DYNAMIC__)
1601
1602 #  include <mach-o/dyld.h>
1603 EXT char *** environ_pointer;
1604 #  define environ (*environ_pointer)
1605 #  endif
1606 #endif /* environ processing */
1607
1608
1609 /* for tmp use in stupid debuggers */
1610 EXT int *       di;
1611 EXT short *     ds;
1612 EXT char *      dc;
1613
1614 /* handy constants */
1615 EXTCONST char warn_uninit[]
1616   INIT("Use of uninitialized value");
1617 EXTCONST char warn_nosemi[]
1618   INIT("Semicolon seems to be missing");
1619 EXTCONST char warn_reserved[]
1620   INIT("Unquoted string \"%s\" may clash with future reserved word");
1621 EXTCONST char warn_nl[]
1622   INIT("Unsuccessful %s on filename containing newline");
1623 EXTCONST char no_wrongref[]
1624   INIT("Can't use %s ref as %s ref");
1625 EXTCONST char no_symref[]
1626   INIT("Can't use string (\"%.32s\") as %s ref while \"strict refs\" in use");
1627 EXTCONST char no_usym[]
1628   INIT("Can't use an undefined value as %s reference");
1629 EXTCONST char no_aelem[]
1630   INIT("Modification of non-creatable array value attempted, subscript %d");
1631 EXTCONST char no_helem[]
1632   INIT("Modification of non-creatable hash value attempted, subscript \"%s\"");
1633 EXTCONST char no_modify[]
1634   INIT("Modification of a read-only value attempted");
1635 EXTCONST char no_mem[]
1636   INIT("Out of memory!\n");
1637 EXTCONST char no_security[]
1638   INIT("Insecure dependency in %s%s");
1639 EXTCONST char no_sock_func[]
1640   INIT("Unsupported socket function \"%s\" called");
1641 EXTCONST char no_dir_func[]
1642   INIT("Unsupported directory function \"%s\" called");
1643 EXTCONST char no_func[]
1644   INIT("The %s function is unimplemented");
1645 EXTCONST char no_myglob[]
1646   INIT("\"my\" variable %s can't be in a package");
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 #define HINT_NEW_INTEGER        0x00001000
1824 #define HINT_NEW_FLOAT          0x00002000
1825 #define HINT_NEW_BINARY         0x00004000
1826 #define HINT_NEW_STRING         0x00008000
1827 #define HINT_NEW_RE             0x00010000
1828 #define HINT_LOCALIZE_HH        0x00020000 /* %^H needs to be copied */
1829
1830 #define HINT_RE_TAINT           0x00100000
1831 #define HINT_RE_EVAL            0x00200000
1832
1833 /* Various states of an input record separator SV (rs, nrs) */
1834 #define RsSNARF(sv)   (! SvOK(sv))
1835 #define RsSIMPLE(sv)  (SvOK(sv) && SvCUR(sv))
1836 #define RsPARA(sv)    (SvOK(sv) && ! SvCUR(sv))
1837 #define RsRECORD(sv)  (SvROK(sv) && (SvIV(SvRV(sv)) > 0))
1838
1839 /* Enable variables which are pointers to functions */
1840 #ifdef PERL_OBJECT
1841 typedef regexp*(CPerlObj::*regcomp_t) _((char* exp, char* xend, PMOP* pm));
1842 typedef I32 (CPerlObj::*regexec_t) _((regexp* prog, char* stringarg,
1843                                       char* strend, char* strbeg,
1844                                       I32 minend, SV* screamer, void* data,
1845                                       U32 flags));
1846 #else
1847 typedef regexp*(*regcomp_t) _((char* exp, char* xend, PMOP* pm));
1848 typedef I32 (*regexec_t) _((regexp* prog, char* stringarg, char* strend, char*
1849                             strbeg, I32 minend, SV* screamer, void* data, 
1850                             U32 flags));
1851
1852 EXT regexp*     pregcomp _((char* exp, char* xend, PMOP* pm));
1853 EXT I32         regexec_flags _((regexp* prog, char* stringarg, char* strend,
1854                          char* strbeg, I32 minend, SV* screamer,
1855                          void* data, U32 flags));
1856 #endif
1857
1858 /* Set up PERLVAR macros for populating structs */
1859 #define PERLVAR(var,type) type var;
1860 #define PERLVARI(var,type,init) type var;
1861 #define PERLVARIC(var,type,init) type var;
1862
1863 /* Interpreter exitlist entry */
1864 typedef struct exitlistentry {
1865 #ifdef PERL_OBJECT
1866     void (*fn) _((CPerlObj*, void*));
1867 #else
1868     void (*fn) _((void*));
1869 #endif
1870     void *ptr;
1871 } PerlExitListEntry;
1872
1873 #ifdef PERL_OBJECT
1874 extern "C" CPerlObj* perl_alloc _((IPerlMem*, IPerlEnv*, IPerlStdIO*, IPerlLIO*, IPerlDir*, IPerlSock*, IPerlProc*));
1875
1876 typedef int (CPerlObj::*runops_proc_t) _((void));
1877 #undef EXT
1878 #define EXT
1879 #undef EXTCONST
1880 #define EXTCONST
1881 #undef INIT
1882 #define INIT(x)
1883
1884 class CPerlObj {
1885 public:
1886         CPerlObj(IPerlMem*, IPerlEnv*, IPerlStdIO*, IPerlLIO*, IPerlDir*, IPerlSock*, IPerlProc*);
1887         void Init(void);
1888         void* operator new(size_t nSize, IPerlMem *pvtbl);
1889 #endif /* PERL_OBJECT */
1890
1891 #ifdef PERL_GLOBAL_STRUCT
1892 struct perl_vars {
1893 #include "perlvars.h"
1894 };
1895
1896 #ifdef PERL_CORE
1897 EXT struct perl_vars Perl_Vars;
1898 EXT struct perl_vars *Perl_VarsPtr INIT(&Perl_Vars);
1899 #else
1900 #if !defined(__GNUC__) || !defined(WIN32)
1901 EXT
1902 #endif
1903 struct perl_vars *Perl_VarsPtr;
1904 #define Perl_Vars (*((Perl_VarsPtr) ? Perl_VarsPtr : (Perl_VarsPtr =  Perl_GetVars())))
1905 #endif
1906 #endif /* PERL_GLOBAL_STRUCT */
1907
1908 #ifdef MULTIPLICITY
1909 /* If we have multiple interpreters define a struct 
1910    holding variables which must be per-interpreter
1911    If we don't have threads anything that would have 
1912    be per-thread is per-interpreter.
1913 */
1914
1915 struct interpreter {
1916 #ifndef USE_THREADS
1917 #include "thrdvar.h"
1918 #endif
1919 #include "intrpvar.h"
1920 };
1921
1922 #else
1923 struct interpreter {
1924     char broiled;
1925 };
1926 #endif
1927
1928 #ifdef USE_THREADS
1929 /* If we have threads define a struct with all the variables
1930  * that have to be per-thread
1931  */
1932
1933
1934 struct perl_thread {
1935 #include "thrdvar.h"
1936 };
1937
1938 typedef struct perl_thread *Thread;
1939
1940 #else
1941 typedef void *Thread;
1942 #endif
1943
1944 /* Done with PERLVAR macros for now ... */
1945 #undef PERLVAR
1946 #undef PERLVARI
1947 #undef PERLVARIC
1948
1949 #include "thread.h"
1950 #include "pp.h"
1951 #include "proto.h"
1952
1953 #ifdef EMBED
1954 #define Perl_sv_setptrobj(rv,ptr,name) Perl_sv_setref_iv(rv,name,(IV)ptr)
1955 #define Perl_sv_setptrref(rv,ptr) Perl_sv_setref_iv(rv,Nullch,(IV)ptr)
1956 #else
1957 #define sv_setptrobj(rv,ptr,name) sv_setref_iv(rv,name,(IV)ptr)
1958 #define sv_setptrref(rv,ptr) sv_setref_iv(rv,Nullch,(IV)ptr)
1959 #endif
1960
1961 /* The following must follow proto.h as #defines mess up syntax */
1962
1963 #include "embedvar.h"
1964
1965 /* Now include all the 'global' variables 
1966  * If we don't have threads or multiple interpreters
1967  * these include variables that would have been their struct-s 
1968  */
1969
1970 #define PERLVAR(var,type) EXT type var;
1971 #define PERLVARI(var,type,init) EXT type var INIT(init);
1972 #define PERLVARIC(var,type,init) EXTCONST type var INIT(init);
1973
1974 #ifndef PERL_GLOBAL_STRUCT
1975 #include "perlvars.h"
1976 #endif
1977
1978 #ifndef MULTIPLICITY
1979
1980 #  include "intrpvar.h"
1981 #  ifndef USE_THREADS
1982 #    include "thrdvar.h"
1983 #  endif
1984
1985 #endif
1986
1987 #ifdef PERL_OBJECT
1988 };
1989
1990 #include "objpp.h"
1991 #ifdef DOINIT
1992 #include "INTERN.h"
1993 #else
1994 #include "EXTERN.h"
1995 #endif
1996 #endif  /* PERL_OBJECT */
1997
1998
1999 #undef PERLVAR
2000 #undef PERLVARI
2001 #undef PERLVARIC
2002
2003 #if defined(HASATTRIBUTE) && defined(WIN32)
2004 /*
2005  * This provides a layer of functions and macros to ensure extensions will
2006  * get to use the same RTL functions as the core.
2007  * It has to go here or #define of printf messes up __attribute__
2008  * stuff in proto.h  
2009  */
2010 #ifndef PERL_OBJECT
2011 #  include <win32iop.h>
2012 #endif  /* PERL_OBJECT */
2013 #endif  /* WIN32 */
2014
2015 #ifdef DOINIT
2016
2017 EXT MGVTBL vtbl_sv =    {magic_get,
2018                                 magic_set,
2019                                         magic_len,
2020                                                 0,      0};
2021 EXT MGVTBL vtbl_env =   {0,     magic_set_all_env,
2022                                 0,      magic_clear_all_env,
2023                                                         0};
2024 EXT MGVTBL vtbl_envelem =       {0,     magic_setenv,
2025                                         0,      magic_clearenv,
2026                                                         0};
2027 EXT MGVTBL vtbl_sig =   {0,     0,               0, 0, 0};
2028 EXT MGVTBL vtbl_sigelem =       {magic_getsig,
2029                                         magic_setsig,
2030                                         0,      magic_clearsig,
2031                                                         0};
2032 EXT MGVTBL vtbl_pack =  {0,     0,      magic_sizepack, magic_wipepack,
2033                                                         0};
2034 EXT MGVTBL vtbl_packelem =      {magic_getpack,
2035                                 magic_setpack,
2036                                         0,      magic_clearpack,
2037                                                         0};
2038 EXT MGVTBL vtbl_dbline =        {0,     magic_setdbline,
2039                                         0,      0,      0};
2040 EXT MGVTBL vtbl_isa =   {0,     magic_setisa,
2041                                         0,      magic_setisa,
2042                                                         0};
2043 EXT MGVTBL vtbl_isaelem =       {0,     magic_setisa,
2044                                         0,      0,      0};
2045 EXT MGVTBL vtbl_arylen =        {magic_getarylen,
2046                                 magic_setarylen,
2047                                         0,      0,      0};
2048 EXT MGVTBL vtbl_glob =  {magic_getglob,
2049                                 magic_setglob,
2050                                         0,      0,      0};
2051 EXT MGVTBL vtbl_mglob = {0,     magic_setmglob,
2052                                         0,      0,      0};
2053 EXT MGVTBL vtbl_nkeys = {magic_getnkeys,
2054                                 magic_setnkeys,
2055                                         0,      0,      0};
2056 EXT MGVTBL vtbl_taint = {magic_gettaint,magic_settaint,
2057                                         0,      0,      0};
2058 EXT MGVTBL vtbl_substr =        {magic_getsubstr, magic_setsubstr,
2059                                         0,      0,      0};
2060 EXT MGVTBL vtbl_vec =   {magic_getvec,
2061                                 magic_setvec,
2062                                         0,      0,      0};
2063 EXT MGVTBL vtbl_pos =   {magic_getpos,
2064                                 magic_setpos,
2065                                         0,      0,      0};
2066 EXT MGVTBL vtbl_bm =    {0,     magic_setbm,
2067                                         0,      0,      0};
2068 EXT MGVTBL vtbl_fm =    {0,     magic_setfm,
2069                                         0,      0,      0};
2070 EXT MGVTBL vtbl_uvar =  {magic_getuvar,
2071                                 magic_setuvar,
2072                                         0,      0,      0};
2073 #ifdef USE_THREADS
2074 EXT MGVTBL vtbl_mutex = {0,     0,      0,      0,      magic_mutexfree};
2075 #endif /* USE_THREADS */
2076 EXT MGVTBL vtbl_defelem = {magic_getdefelem,magic_setdefelem,
2077                                         0,      0,      magic_freedefelem};
2078
2079 EXT MGVTBL vtbl_regexp = {0,0,0,0, magic_freeregexp};
2080
2081 #ifdef USE_LOCALE_COLLATE
2082 EXT MGVTBL vtbl_collxfrm = {0,
2083                                 magic_setcollxfrm,
2084                                         0,      0,      0};
2085 #endif
2086
2087 #ifdef OVERLOAD
2088 EXT MGVTBL vtbl_amagic =       {0,     magic_setamagic,
2089                                         0,      0,      magic_setamagic};
2090 EXT MGVTBL vtbl_amagicelem =   {0,     magic_setamagic,
2091                                         0,      0,      magic_setamagic};
2092 #endif /* OVERLOAD */
2093
2094 #else /* !DOINIT */
2095
2096 EXT MGVTBL vtbl_sv;
2097 EXT MGVTBL vtbl_env;
2098 EXT MGVTBL vtbl_envelem;
2099 EXT MGVTBL vtbl_sig;
2100 EXT MGVTBL vtbl_sigelem;
2101 EXT MGVTBL vtbl_pack;
2102 EXT MGVTBL vtbl_packelem;
2103 EXT MGVTBL vtbl_dbline;
2104 EXT MGVTBL vtbl_isa;
2105 EXT MGVTBL vtbl_isaelem;
2106 EXT MGVTBL vtbl_arylen;
2107 EXT MGVTBL vtbl_glob;
2108 EXT MGVTBL vtbl_mglob;
2109 EXT MGVTBL vtbl_nkeys;
2110 EXT MGVTBL vtbl_taint;
2111 EXT MGVTBL vtbl_substr;
2112 EXT MGVTBL vtbl_vec;
2113 EXT MGVTBL vtbl_pos;
2114 EXT MGVTBL vtbl_bm;
2115 EXT MGVTBL vtbl_fm;
2116 EXT MGVTBL vtbl_uvar;
2117
2118 #ifdef USE_THREADS
2119 EXT MGVTBL vtbl_mutex;
2120 #endif /* USE_THREADS */
2121
2122 EXT MGVTBL vtbl_defelem;
2123 EXT MGVTBL vtbl_regexp;
2124
2125 #ifdef USE_LOCALE_COLLATE
2126 EXT MGVTBL vtbl_collxfrm;
2127 #endif
2128
2129 #ifdef OVERLOAD
2130 EXT MGVTBL vtbl_amagic;
2131 EXT MGVTBL vtbl_amagicelem;
2132 #endif /* OVERLOAD */
2133
2134 #endif /* !DOINIT */
2135
2136 #ifdef OVERLOAD
2137
2138 #define NofAMmeth 58
2139 #ifdef DOINIT
2140 EXTCONST char * AMG_names[NofAMmeth] = {
2141   "fallback",   "abs",                  /* "fallback" should be the first. */
2142   "bool",       "nomethod",
2143   "\"\"",       "0+",
2144   "+",          "+=",
2145   "-",          "-=",
2146   "*",          "*=",
2147   "/",          "/=",
2148   "%",          "%=",
2149   "**",         "**=",
2150   "<<",         "<<=",
2151   ">>",         ">>=",
2152   "&",          "&=",
2153   "|",          "|=",
2154   "^",          "^=",
2155   "<",          "<=",
2156   ">",          ">=",
2157   "==",         "!=",
2158   "<=>",        "cmp",
2159   "lt",         "le",
2160   "gt",         "ge",
2161   "eq",         "ne",
2162   "!",          "~",
2163   "++",         "--",
2164   "atan2",      "cos",
2165   "sin",        "exp",
2166   "log",        "sqrt",
2167   "x",          "x=",
2168   ".",          ".=",
2169   "=",          "neg"
2170 };
2171 #else
2172 EXTCONST char * AMG_names[NofAMmeth];
2173 #endif /* def INITAMAGIC */
2174
2175 struct am_table {
2176   long was_ok_sub;
2177   long was_ok_am;
2178   U32 flags;
2179   CV* table[NofAMmeth];
2180   long fallback;
2181 };
2182 struct am_table_short {
2183   long was_ok_sub;
2184   long was_ok_am;
2185   U32 flags;
2186 };
2187 typedef struct am_table AMT;
2188 typedef struct am_table_short AMTS;
2189
2190 #define AMGfallNEVER    1
2191 #define AMGfallNO       2
2192 #define AMGfallYES      3
2193
2194 #define AMTf_AMAGIC             1
2195 #define AMT_AMAGIC(amt)         ((amt)->flags & AMTf_AMAGIC)
2196 #define AMT_AMAGIC_on(amt)      ((amt)->flags |= AMTf_AMAGIC)
2197 #define AMT_AMAGIC_off(amt)     ((amt)->flags &= ~AMTf_AMAGIC)
2198
2199 enum {
2200   fallback_amg, abs_amg,
2201   bool__amg,    nomethod_amg,
2202   string_amg,   numer_amg,
2203   add_amg,      add_ass_amg,
2204   subtr_amg,    subtr_ass_amg,
2205   mult_amg,     mult_ass_amg,
2206   div_amg,      div_ass_amg,
2207   modulo_amg,   modulo_ass_amg,
2208   pow_amg,      pow_ass_amg,
2209   lshift_amg,   lshift_ass_amg,
2210   rshift_amg,   rshift_ass_amg,
2211   band_amg,     band_ass_amg,
2212   bor_amg,      bor_ass_amg,
2213   bxor_amg,     bxor_ass_amg,
2214   lt_amg,       le_amg,
2215   gt_amg,       ge_amg,
2216   eq_amg,       ne_amg,
2217   ncmp_amg,     scmp_amg,
2218   slt_amg,      sle_amg,
2219   sgt_amg,      sge_amg,
2220   seq_amg,      sne_amg,
2221   not_amg,      compl_amg,
2222   inc_amg,      dec_amg,
2223   atan2_amg,    cos_amg,
2224   sin_amg,      exp_amg,
2225   log_amg,      sqrt_amg,
2226   repeat_amg,   repeat_ass_amg,
2227   concat_amg,   concat_ass_amg,
2228   copy_amg,     neg_amg
2229 };
2230
2231 /*
2232  * some compilers like to redefine cos et alia as faster
2233  * (and less accurate?) versions called F_cos et cetera (Quidquid
2234  * latine dictum sit, altum viditur.)  This trick collides with
2235  * the Perl overloading (amg).  The following #defines fool both.
2236  */
2237
2238 #ifdef _FASTMATH
2239 #   ifdef atan2
2240 #       define F_atan2_amg  atan2_amg
2241 #   endif
2242 #   ifdef cos
2243 #       define F_cos_amg    cos_amg
2244 #   endif
2245 #   ifdef exp
2246 #       define F_exp_amg    exp_amg
2247 #   endif
2248 #   ifdef log
2249 #       define F_log_amg    log_amg
2250 #   endif
2251 #   ifdef pow
2252 #       define F_pow_amg    pow_amg
2253 #   endif
2254 #   ifdef sin
2255 #       define F_sin_amg    sin_amg
2256 #   endif
2257 #   ifdef sqrt
2258 #       define F_sqrt_amg   sqrt_amg
2259 #   endif
2260 #endif /* _FASTMATH */
2261
2262 #endif /* OVERLOAD */
2263
2264 #define PERLDB_ALL      0x3f            /* No _NONAME, _GOTO */
2265 #define PERLDBf_SUB     0x01            /* Debug sub enter/exit. */
2266 #define PERLDBf_LINE    0x02            /* Keep line #. */
2267 #define PERLDBf_NOOPT   0x04            /* Switch off optimizations. */
2268 #define PERLDBf_INTER   0x08            /* Preserve more data for
2269                                            later inspections.  */
2270 #define PERLDBf_SUBLINE 0x10            /* Keep subr source lines. */
2271 #define PERLDBf_SINGLE  0x20            /* Start with single-step on. */
2272 #define PERLDBf_NONAME  0x40            /* For _SUB: no name of the subr. */
2273 #define PERLDBf_GOTO    0x80            /* Report goto: call DB::goto. */
2274
2275 #define PERLDB_SUB      (perldb && (perldb & PERLDBf_SUB))
2276 #define PERLDB_LINE     (perldb && (perldb & PERLDBf_LINE))
2277 #define PERLDB_NOOPT    (perldb && (perldb & PERLDBf_NOOPT))
2278 #define PERLDB_INTER    (perldb && (perldb & PERLDBf_INTER))
2279 #define PERLDB_SUBLINE  (perldb && (perldb & PERLDBf_SUBLINE))
2280 #define PERLDB_SINGLE   (perldb && (perldb & PERLDBf_SINGLE))
2281 #define PERLDB_SUB_NN   (perldb && (perldb & (PERLDBf_NONAME)))
2282 #define PERLDB_GOTO     (perldb && (perldb & PERLDBf_GOTO))
2283
2284
2285 #ifdef USE_LOCALE_NUMERIC
2286
2287 #define SET_NUMERIC_STANDARD() \
2288     STMT_START {                                \
2289         if (! numeric_standard)                 \
2290             perl_set_numeric_standard();        \
2291     } STMT_END
2292
2293 #define SET_NUMERIC_LOCAL() \
2294     STMT_START {                                \
2295         if (! numeric_local)                    \
2296             perl_set_numeric_local();           \
2297     } STMT_END
2298
2299 #else /* !USE_LOCALE_NUMERIC */
2300
2301 #define SET_NUMERIC_STANDARD()  /**/
2302 #define SET_NUMERIC_LOCAL()     /**/
2303
2304 #endif /* !USE_LOCALE_NUMERIC */
2305
2306 #if !defined(PERLIO_IS_STDIO) && defined(HASATTRIBUTE)
2307 /* 
2308  * Now we have __attribute__ out of the way 
2309  * Remap printf 
2310  */
2311 #define printf PerlIO_stdoutf
2312 #endif
2313
2314 #ifndef PERL_SCRIPT_MODE
2315 #define PERL_SCRIPT_MODE "r"
2316 #endif
2317
2318 /*
2319  * nice_chunk and nice_chunk size need to be set
2320  * and queried under the protection of sv_mutex
2321  */
2322 #define offer_nice_chunk(chunk, chunk_size) do {        \
2323         LOCK_SV_MUTEX;                                  \
2324         if (!nice_chunk) {                              \
2325             nice_chunk = (char*)(chunk);                \
2326             nice_chunk_size = (chunk_size);             \
2327         }                                               \
2328         else {                                          \
2329             Safefree(chunk);                            \
2330         }                                               \
2331         UNLOCK_SV_MUTEX;                                \
2332     } while (0)
2333
2334 #ifdef HAS_SEM
2335 #   include <sys/ipc.h>
2336 #   include <sys/sem.h>
2337 #   ifndef HAS_UNION_SEMUN      /* Provide the union semun. */
2338     union semun {
2339         int val;
2340         struct semid_ds *buf;
2341         unsigned short *array;
2342     };
2343 #   endif
2344 #   ifdef USE_SEMCTL_SEMUN
2345 #       define Semctl(id, num, cmd, semun) semctl(id, num, cmd, semun)
2346 #   else
2347 #       ifdef USE_SEMCTL_SEMID_DS
2348 #           define Semctl(id, num, cmd, semun) semctl(id, num, cmd, semun.buf)
2349 #       endif
2350 #   endif
2351 #   ifndef Semctl       /* Place our bets on the semun horse. */
2352 #       define Semctl(id, num, cmd, semun) semctl(id, num, cmd, semun)
2353 #   endif
2354 #endif
2355
2356 #endif /* Include guard */