3 * Copyright (c) 1991-1997, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "Very useful, no doubt, that was to Saruman; yet it seems that he was
12 * not content." --Gandalf
18 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
23 # define SIG_ERR ((Sighandler_t) -1)
26 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
35 /* Put this after #includes because fork and vfork prototypes may
46 # include <sys/file.h>
50 # include <sys/wait.h>
56 static void xstat _((void));
60 static U32 threadnum = 0;
61 #endif /* USE_THREADS */
65 /* paranoid version of malloc */
67 /* NOTE: Do not call the next three routines directly. Use the macros
68 * in handy.h, so that we can easily redefine everything to do tracking of
69 * allocated hunks back to the original New to track down any memory leaks.
70 * XXX This advice seems to be widely ignored :-( --AD August 1996.
80 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
83 #endif /* HAS_64K_LIMIT */
86 croak("panic: malloc");
88 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
89 #if !(defined(I286) || defined(atarist))
90 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
92 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
99 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
105 /* paranoid version of realloc */
108 saferealloc(where,size)
113 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
115 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
119 PerlIO_printf(PerlIO_stderr(),
120 "Reallocation too large: %lx\n", size) FLUSH;
123 #endif /* HAS_64K_LIMIT */
125 croak("Null realloc");
128 croak("panic: realloc");
130 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
132 #if !(defined(I286) || defined(atarist))
134 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++);
135 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
139 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++);
140 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
149 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
155 /* safe version of free */
161 #if !(defined(I286) || defined(atarist))
162 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",where,an++));
164 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",where,an++));
172 /* safe version of calloc */
175 safecalloc(count, size)
182 if (size * count > 0xffff) {
183 PerlIO_printf(PerlIO_stderr(),
184 "Allocation too large: %lx\n", size * count) FLUSH;
187 #endif /* HAS_64K_LIMIT */
189 if ((long)size < 0 || (long)count < 0)
190 croak("panic: calloc");
193 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
194 #if !(defined(I286) || defined(atarist))
195 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
197 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
200 memset((void*)ptr, 0, size);
206 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
212 #endif /* !MYMALLOC */
216 #define ALIGN sizeof(long)
223 register Malloc_t where;
225 where = safemalloc(size + ALIGN);
229 return where + ALIGN;
233 safexrealloc(where,size)
237 register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN);
250 x = where[0] + 100 * where[1];
256 safexcalloc(x,count,size)
261 register Malloc_t where;
263 where = safexmalloc(x, size * count + ALIGN);
265 memset((void*)where + ALIGN, 0, size * count);
268 return where + ALIGN;
276 for (i = 0; i < MAXXCOUNT; i++) {
277 if (xcount[i] > lastxcount[i]) {
278 PerlIO_printf(PerlIO_stderr(),"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
279 lastxcount[i] = xcount[i];
284 #endif /* LEAKTEST */
286 /* copy a string up to some (non-backslashed) delimiter, if any */
289 delimcpy(to, toend, from, fromend, delim, retlen)
291 register char *toend;
293 register char *fromend;
298 for (tolen = 0; from < fromend; from++, tolen++) {
300 if (from[1] == delim)
309 else if (*from == delim)
320 /* return ptr to little string in big string, NULL if not found */
321 /* This routine was donated by Corey Satten. */
326 register char *little;
328 register char *s, *x;
339 for (x=big,s=little; *s; /**/ ) {
353 /* same as instr but allow embedded nulls */
356 ninstr(big, bigend, little, lend)
358 register char *bigend;
362 register char *s, *x;
363 register I32 first = *little;
364 register char *littleend = lend;
366 if (!first && little >= littleend)
368 if (bigend - big < littleend - little)
370 bigend -= littleend - little++;
371 while (big <= bigend) {
374 for (x=big,s=little; s < littleend; /**/ ) {
386 /* reverse of the above--find last substring */
389 rninstr(big, bigend, little, lend)
395 register char *bigbeg;
396 register char *s, *x;
397 register I32 first = *little;
398 register char *littleend = lend;
400 if (!first && little >= littleend)
403 big = bigend - (littleend - little++);
404 while (big >= bigbeg) {
407 for (x=big+2,s=little; s < littleend; /**/ ) {
420 * Set up for a new ctype locale.
423 perl_new_ctype(newctype)
426 #ifdef USE_LOCALE_CTYPE
430 for (i = 0; i < 256; i++) {
432 fold_locale[i] = toLOWER_LC(i);
433 else if (isLOWER_LC(i))
434 fold_locale[i] = toUPPER_LC(i);
439 #endif /* USE_LOCALE_CTYPE */
443 * Set up for a new collation locale.
446 perl_new_collate(newcoll)
449 #ifdef USE_LOCALE_COLLATE
452 if (collation_name) {
454 Safefree(collation_name);
455 collation_name = NULL;
456 collation_standard = TRUE;
463 if (! collation_name || strNE(collation_name, newcoll)) {
465 Safefree(collation_name);
466 collation_name = savepv(newcoll);
467 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
470 /* 2: at most so many chars ('a', 'b'). */
471 /* 50: surely no system expands a char more. */
472 #define XFRMBUFSIZE (2 * 50)
473 char xbuf[XFRMBUFSIZE];
474 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
475 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
476 SSize_t mult = fb - fa;
478 croak("strxfrm() gets absurd");
479 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
480 collxfrm_mult = mult;
484 #endif /* USE_LOCALE_COLLATE */
488 * Set up for a new numeric locale.
491 perl_new_numeric(newnum)
494 #ifdef USE_LOCALE_NUMERIC
498 Safefree(numeric_name);
500 numeric_standard = TRUE;
501 numeric_local = TRUE;
506 if (! numeric_name || strNE(numeric_name, newnum)) {
507 Safefree(numeric_name);
508 numeric_name = savepv(newnum);
509 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
510 numeric_local = TRUE;
513 #endif /* USE_LOCALE_NUMERIC */
517 perl_set_numeric_standard()
519 #ifdef USE_LOCALE_NUMERIC
521 if (! numeric_standard) {
522 setlocale(LC_NUMERIC, "C");
523 numeric_standard = TRUE;
524 numeric_local = FALSE;
527 #endif /* USE_LOCALE_NUMERIC */
531 perl_set_numeric_local()
533 #ifdef USE_LOCALE_NUMERIC
535 if (! numeric_local) {
536 setlocale(LC_NUMERIC, numeric_name);
537 numeric_standard = FALSE;
538 numeric_local = TRUE;
541 #endif /* USE_LOCALE_NUMERIC */
546 * Initialize locale awareness.
549 perl_init_i18nl10n(printwarn)
554 * 1 = set ok or not applicable,
555 * 0 = fallback to C locale,
556 * -1 = fallback to C locale failed
561 #ifdef USE_LOCALE_CTYPE
562 char *curctype = NULL;
563 #endif /* USE_LOCALE_CTYPE */
564 #ifdef USE_LOCALE_COLLATE
565 char *curcoll = NULL;
566 #endif /* USE_LOCALE_COLLATE */
567 #ifdef USE_LOCALE_NUMERIC
569 #endif /* USE_LOCALE_NUMERIC */
570 char *lc_all = getenv("LC_ALL");
571 char *lang = getenv("LANG");
572 bool setlocale_failure = FALSE;
574 #ifdef LOCALE_ENVIRON_REQUIRED
577 * Ultrix setlocale(..., "") fails if there are no environment
578 * variables from which to get a locale name.
585 if (setlocale(LC_ALL, ""))
588 setlocale_failure = TRUE;
590 if (!setlocale_failure)
593 #ifdef USE_LOCALE_CTYPE
594 if (! (curctype = setlocale(LC_CTYPE,
595 (!done && (lang || getenv("LC_CTYPE")))
597 setlocale_failure = TRUE;
598 #endif /* USE_LOCALE_CTYPE */
599 #ifdef USE_LOCALE_COLLATE
600 if (! (curcoll = setlocale(LC_COLLATE,
601 (!done && (lang || getenv("LC_COLLATE")))
603 setlocale_failure = TRUE;
604 #endif /* USE_LOCALE_COLLATE */
605 #ifdef USE_LOCALE_NUMERIC
606 if (! (curnum = setlocale(LC_NUMERIC,
607 (!done && (lang || getenv("LC_NUMERIC")))
609 setlocale_failure = TRUE;
610 #endif /* USE_LOCALE_NUMERIC */
613 #else /* !LOCALE_ENVIRON_REQUIRED */
617 if (! setlocale(LC_ALL, ""))
618 setlocale_failure = TRUE;
620 #ifdef USE_LOCALE_CTYPE
621 curctype = setlocale(LC_CTYPE, Nullch);
622 #endif /* USE_LOCALE_CTYPE */
623 #ifdef USE_LOCALE_COLLATE
624 curcoll = setlocale(LC_COLLATE, Nullch);
625 #endif /* USE_LOCALE_COLLATE */
626 #ifdef USE_LOCALE_NUMERIC
627 curnum = setlocale(LC_NUMERIC, Nullch);
628 #endif /* USE_LOCALE_NUMERIC */
633 #ifdef USE_LOCALE_CTYPE
634 if (! (curctype = setlocale(LC_CTYPE, "")))
635 setlocale_failure = TRUE;
636 #endif /* USE_LOCALE_CTYPE */
637 #ifdef USE_LOCALE_COLLATE
638 if (! (curcoll = setlocale(LC_COLLATE, "")))
639 setlocale_failure = TRUE;
640 #endif /* USE_LOCALE_COLLATE */
641 #ifdef USE_LOCALE_NUMERIC
642 if (! (curnum = setlocale(LC_NUMERIC, "")))
643 setlocale_failure = TRUE;
644 #endif /* USE_LOCALE_NUMERIC */
648 #endif /* !LOCALE_ENVIRON_REQUIRED */
650 if (setlocale_failure) {
652 bool locwarn = (printwarn > 1 ||
654 (!(p = getenv("PERL_BADLANG")) || atoi(p)));
659 PerlIO_printf(PerlIO_stderr(),
660 "perl: warning: Setting locale failed.\n");
664 PerlIO_printf(PerlIO_stderr(),
665 "perl: warning: Setting locale failed for the categories:\n\t");
666 #ifdef USE_LOCALE_CTYPE
668 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
669 #endif /* USE_LOCALE_CTYPE */
670 #ifdef USE_LOCALE_COLLATE
672 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
673 #endif /* USE_LOCALE_COLLATE */
674 #ifdef USE_LOCALE_NUMERIC
676 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
677 #endif /* USE_LOCALE_NUMERIC */
678 PerlIO_printf(PerlIO_stderr(), "\n");
682 PerlIO_printf(PerlIO_stderr(),
683 "perl: warning: Please check that your locale settings:\n");
685 PerlIO_printf(PerlIO_stderr(),
686 "\tLC_ALL = %c%s%c,\n",
688 lc_all ? lc_all : "unset",
693 for (e = environ; *e; e++) {
694 if (strnEQ(*e, "LC_", 3)
695 && strnNE(*e, "LC_ALL=", 7)
696 && (p = strchr(*e, '=')))
697 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
698 (int)(p - *e), *e, p + 1);
702 PerlIO_printf(PerlIO_stderr(),
705 lang ? lang : "unset",
708 PerlIO_printf(PerlIO_stderr(),
709 " are supported and installed on your system.\n");
714 if (setlocale(LC_ALL, "C")) {
716 PerlIO_printf(PerlIO_stderr(),
717 "perl: warning: Falling back to the standard locale (\"C\").\n");
722 PerlIO_printf(PerlIO_stderr(),
723 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
730 #ifdef USE_LOCALE_CTYPE
731 || !(curctype || setlocale(LC_CTYPE, "C"))
732 #endif /* USE_LOCALE_CTYPE */
733 #ifdef USE_LOCALE_COLLATE
734 || !(curcoll || setlocale(LC_COLLATE, "C"))
735 #endif /* USE_LOCALE_COLLATE */
736 #ifdef USE_LOCALE_NUMERIC
737 || !(curnum || setlocale(LC_NUMERIC, "C"))
738 #endif /* USE_LOCALE_NUMERIC */
742 PerlIO_printf(PerlIO_stderr(),
743 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
747 #endif /* ! LC_ALL */
749 #ifdef USE_LOCALE_CTYPE
750 curctype = setlocale(LC_CTYPE, Nullch);
751 #endif /* USE_LOCALE_CTYPE */
752 #ifdef USE_LOCALE_COLLATE
753 curcoll = setlocale(LC_COLLATE, Nullch);
754 #endif /* USE_LOCALE_COLLATE */
755 #ifdef USE_LOCALE_NUMERIC
756 curnum = setlocale(LC_NUMERIC, Nullch);
757 #endif /* USE_LOCALE_NUMERIC */
760 #ifdef USE_LOCALE_CTYPE
761 perl_new_ctype(curctype);
762 #endif /* USE_LOCALE_CTYPE */
764 #ifdef USE_LOCALE_COLLATE
765 perl_new_collate(curcoll);
766 #endif /* USE_LOCALE_COLLATE */
768 #ifdef USE_LOCALE_NUMERIC
769 perl_new_numeric(curnum);
770 #endif /* USE_LOCALE_NUMERIC */
772 #endif /* USE_LOCALE */
777 /* Backwards compatibility. */
779 perl_init_i18nl14n(printwarn)
782 return perl_init_i18nl10n(printwarn);
785 #ifdef USE_LOCALE_COLLATE
788 * mem_collxfrm() is a bit like strxfrm() but with two important
789 * differences. First, it handles embedded NULs. Second, it allocates
790 * a bit more memory than needed for the transformed data itself.
791 * The real transformed data begins at offset sizeof(collationix).
792 * Please see sv_collxfrm() to see how this is used.
795 mem_collxfrm(s, len, xlen)
801 STRLEN xalloc, xin, xout;
803 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
804 /* the +1 is for the terminating NUL. */
806 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
807 New(171, xbuf, xalloc, char);
811 *(U32*)xbuf = collation_ix;
812 xout = sizeof(collation_ix);
813 for (xin = 0; xin < len; ) {
817 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
820 if (xused < xalloc - xout)
822 xalloc = (2 * xalloc) + 1;
823 Renew(xbuf, xalloc, char);
828 xin += strlen(s + xin) + 1;
831 /* Embedded NULs are understood but silently skipped
832 * because they make no sense in locale collation. */
836 *xlen = xout - sizeof(collation_ix);
845 #endif /* USE_LOCALE_COLLATE */
851 register unsigned char *s;
852 register unsigned char *table;
854 register U32 len = SvCUR(sv);
859 return; /* can't have offsets that big */
861 table = (unsigned char*)(SvPVX(sv) + len + 1);
863 for (i = 0; i < 256; i++) {
867 while (s >= (unsigned char*)(SvPVX(sv)))
869 if (table[*s] == len)
873 sv_upgrade(sv, SVt_PVBM);
874 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
877 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
878 for (i = 0; i < len; i++) {
879 if (freq[s[i]] < frequency) {
881 frequency = freq[s[i]];
884 BmRARE(sv) = s[rarest];
885 BmPREVIOUS(sv) = rarest;
886 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
890 fbm_instr(big, bigend, littlestr)
892 register unsigned char *bigend;
895 register unsigned char *s;
897 register I32 littlelen;
898 register unsigned char *little;
899 register unsigned char *table;
900 register unsigned char *olds;
901 register unsigned char *oldlittle;
903 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
905 char *l = SvPV(littlestr,len);
908 return ninstr((char*)big,(char*)bigend, l, l + len);
911 littlelen = SvCUR(littlestr);
912 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
913 if (littlelen > bigend - big)
915 little = (unsigned char*)SvPVX(littlestr);
916 s = bigend - littlelen;
917 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
918 return (char*)s; /* how sweet it is */
919 else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
922 if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
927 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
928 if (--littlelen >= bigend - big)
931 oldlittle = little = table - 2;
935 if (tmp = table[*s]) {
937 if (bigend - s > tmp) {
942 if ((s += tmp) < bigend)
948 tmp = littlelen; /* less expensive than calling strncmp() */
951 if (*--s == *--little)
953 s = olds + 1; /* here we pay the price for failure */
955 if (s < bigend) /* fake up continue to outer loop */
966 screaminstr(bigstr, littlestr)
970 register unsigned char *s, *x;
971 register unsigned char *big;
973 register I32 previous;
975 register unsigned char *little;
976 register unsigned char *bigend;
977 register unsigned char *littleend;
979 if ((pos = screamfirst[BmRARE(littlestr)]) < 0)
981 little = (unsigned char *)(SvPVX(littlestr));
982 littleend = little + SvCUR(littlestr);
984 previous = BmPREVIOUS(littlestr);
985 big = (unsigned char *)(SvPVX(bigstr));
986 bigend = big + SvCUR(bigstr);
987 while (pos < previous) {
988 if (!(pos += screamnext[pos]))
993 if (big[pos-previous] != first)
995 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
1004 return (char *)(big+pos-previous);
1005 } while ( pos += screamnext[pos] );
1006 #else /* !POINTERRIGOR */
1009 if (big[pos] != first)
1011 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
1020 return (char *)(big+pos);
1021 } while ( pos += screamnext[pos] );
1022 #endif /* POINTERRIGOR */
1031 register U8 *a = (U8 *)s1;
1032 register U8 *b = (U8 *)s2;
1034 if (*a != *b && *a != fold[*b])
1042 ibcmp_locale(s1, s2, len)
1046 register U8 *a = (U8 *)s1;
1047 register U8 *b = (U8 *)s2;
1049 if (*a != *b && *a != fold_locale[*b])
1056 /* copy a string to a safe spot */
1062 register char *newaddr;
1064 New(902,newaddr,strlen(sv)+1,char);
1065 (void)strcpy(newaddr,sv);
1069 /* same thing but with a known length */
1076 register char *newaddr;
1078 New(903,newaddr,len+1,char);
1079 Copy(sv,newaddr,len,char); /* might not be null terminated */
1080 newaddr[len] = '\0'; /* is now */
1084 /* the SV for form() and mess() is not kept in an arena */
1092 /* Create as PVMG now, to avoid any upgrading later */
1093 New(905, sv, 1, SV);
1094 Newz(905, any, 1, XPVMG);
1095 SvFLAGS(sv) = SVt_PVMG;
1096 SvANY(sv) = (void*)any;
1097 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1103 form(const char* pat, ...)
1114 va_start(args, pat);
1119 mess_sv = mess_alloc();
1120 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
1122 return SvPVX(mess_sv);
1131 static char dgd[] = " during global destruction.\n";
1134 mess_sv = mess_alloc();
1136 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
1137 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1142 if (curcop->cop_line)
1143 sv_catpvf(sv, " at %_ line %ld",
1144 GvSV(curcop->cop_filegv), (long)curcop->cop_line);
1145 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1146 bool line_mode = (RsSIMPLE(rs) &&
1147 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
1148 sv_catpvf(sv, ", <%s> %s %ld",
1149 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1150 line_mode ? "line" : "chunk",
1151 (long)IoLINES(GvIOp(last_in_gv)));
1153 sv_catpv(sv, ".\n");
1161 die(const char* pat, ...)
1173 I32 oldrunlevel = runlevel;
1174 int was_in_eval = in_eval;
1180 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1181 "%p: die: curstack = %p, mainstack = %p\n",
1182 thr, curstack, mainstack));
1183 #endif /* USE_THREADS */
1184 /* We have to switch back to mainstack or die_where may try to pop
1185 * the eval block from the wrong stack if die is being called from a
1186 * signal handler. - dkindred@cs.cmu.edu */
1187 if (curstack != mainstack) {
1189 SWITCHSTACK(curstack, mainstack);
1193 va_start(args, pat);
1197 message = mess(pat, &args);
1201 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1202 "%p: die: message = %s\ndiehook = %p\n",
1203 thr, message, diehook));
1204 #endif /* USE_THREADS */
1206 /* sv_2cv might call croak() */
1207 SV *olddiehook = diehook;
1211 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1213 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1218 msg = newSVpv(message, 0);
1225 perl_call_sv((SV*)cv, G_DISCARD);
1231 restartop = die_where(message);
1233 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1234 "%p: die: restartop = %p, was_in_eval = %d, oldrunlevel = %d\n",
1235 thr, restartop, was_in_eval, oldrunlevel));
1236 #endif /* USE_THREADS */
1237 if ((!restartop && was_in_eval) || oldrunlevel > 1)
1244 croak(const char* pat, ...)
1248 croak(pat, va_alist)
1261 va_start(args, pat);
1265 message = mess(pat, &args);
1268 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
1269 #endif /* USE_THREADS */
1271 /* sv_2cv might call croak() */
1272 SV *olddiehook = diehook;
1276 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1278 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1283 msg = newSVpv(message, 0);
1290 perl_call_sv((SV*)cv, G_DISCARD);
1296 restartop = die_where(message);
1299 PerlIO_puts(PerlIO_stderr(),message);
1300 (void)PerlIO_flush(PerlIO_stderr());
1306 warn(const char* pat,...)
1321 va_start(args, pat);
1325 message = mess(pat, &args);
1329 /* sv_2cv might call warn() */
1331 SV *oldwarnhook = warnhook;
1335 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1337 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1342 msg = newSVpv(message, 0);
1349 perl_call_sv((SV*)cv, G_DISCARD);
1355 PerlIO_puts(PerlIO_stderr(),message);
1359 (void)PerlIO_flush(PerlIO_stderr());
1362 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1368 register I32 i=setenv_getix(nam); /* where does it go? */
1370 if (environ == origenviron) { /* need we copy environment? */
1376 for (max = i; environ[max]; max++) ;
1377 New(901,tmpenv, max+2, char*);
1378 for (j=0; j<max; j++) /* copy environment */
1379 tmpenv[j] = savepv(environ[j]);
1380 tmpenv[max] = Nullch;
1381 environ = tmpenv; /* tell exec where it is now */
1384 Safefree(environ[i]);
1385 while (environ[i]) {
1386 environ[i] = environ[i+1];
1391 if (!environ[i]) { /* does not exist yet */
1392 Renew(environ, i+2, char*); /* just expand it a bit */
1393 environ[i+1] = Nullch; /* make sure it's null terminated */
1396 Safefree(environ[i]);
1397 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1399 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1401 /* MS-DOS requires environment variable names to be in uppercase */
1402 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1403 * some utilities and applications may break because they only look
1404 * for upper case strings. (Fixed strupr() bug here.)]
1406 strcpy(environ[i],nam); strupr(environ[i]);
1407 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1411 #else /* if WIN32 */
1418 #ifdef USE_WIN32_RTL_ENV
1420 register char *envstr;
1421 STRLEN namlen = strlen(nam);
1423 char *oldstr = environ[setenv_getix(nam)];
1425 /* putenv() has totally broken semantics in both the Borland
1426 * and Microsoft CRTLs. They either store the passed pointer in
1427 * the environment without making a copy, or make a copy and don't
1428 * free it. And on top of that, they dont free() old entries that
1429 * are being replaced/deleted. This means the caller must
1430 * free any old entries somehow, or we end up with a memory
1431 * leak every time my_setenv() is called. One might think
1432 * one could directly manipulate environ[], like the UNIX code
1433 * above, but direct changes to environ are not allowed when
1434 * calling putenv(), since the RTLs maintain an internal
1435 * *copy* of environ[]. Bad, bad, *bad* stink.
1446 vallen = strlen(val);
1447 New(904, envstr, namlen + vallen + 3, char);
1448 (void)sprintf(envstr,"%s=%s",nam,val);
1449 (void)putenv(envstr);
1453 Safefree(envstr); /* MSVCRT leaks without this */
1456 #else /* !USE_WIN32_RTL_ENV */
1458 /* The sane way to deal with the environment.
1459 * Has these advantages over putenv() & co.:
1460 * * enables us to store a truly empty value in the
1461 * environment (like in UNIX).
1462 * * we don't have to deal with RTL globals, bugs and leaks.
1464 * Why you may want to enable USE_WIN32_RTL_ENV:
1465 * * environ[] and RTL functions will not reflect changes,
1466 * which might be an issue if extensions want to access
1467 * the env. via RTL. This cuts both ways, since RTL will
1468 * not see changes made by extensions that call the Win32
1469 * functions directly, either.
1472 SetEnvironmentVariable(nam,val);
1483 register I32 i, len = strlen(nam);
1485 for (i = 0; environ[i]; i++) {
1488 strnicmp(environ[i],nam,len) == 0
1490 strnEQ(environ[i],nam,len)
1492 && environ[i][len] == '=')
1493 break; /* strnEQ must come first to avoid */
1494 } /* potential SEGV's */
1500 #ifdef UNLINK_ALL_VERSIONS
1502 unlnk(f) /* unlink all versions of a file */
1507 for (i = 0; unlink(f) >= 0; i++) ;
1512 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1514 my_bcopy(from,to,len)
1515 register char *from;
1521 if (from - to >= 0) {
1529 *(--to) = *(--from);
1537 my_memset(loc,ch,len)
1550 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1564 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1566 my_memcmp(s1,s2,len)
1571 register U8 *a = (U8 *)s1;
1572 register U8 *b = (U8 *)s2;
1576 if (tmp = *a++ - *b++)
1581 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1583 #if defined(I_STDARG) || defined(I_VARARGS)
1586 #ifdef USE_CHAR_VSPRINTF
1591 vsprintf(dest, pat, args)
1598 fakebuf._ptr = dest;
1599 fakebuf._cnt = 32767;
1603 fakebuf._flag = _IOWRT|_IOSTRG;
1604 _doprnt(pat, args, &fakebuf); /* what a kludge */
1605 (void)putc('\0', &fakebuf);
1606 #ifdef USE_CHAR_VSPRINTF
1609 return 0; /* perl doesn't use return value */
1613 #endif /* HAS_VPRINTF */
1614 #endif /* I_VARARGS || I_STDARGS */
1617 #if BYTEORDER != 0x4321
1619 #ifndef CAN_PROTOTYPE
1626 #if (BYTEORDER & 1) == 0
1629 result = ((s & 255) << 8) + ((s >> 8) & 255);
1637 #ifndef CAN_PROTOTYPE
1646 char c[sizeof(long)];
1649 #if BYTEORDER == 0x1234
1650 u.c[0] = (l >> 24) & 255;
1651 u.c[1] = (l >> 16) & 255;
1652 u.c[2] = (l >> 8) & 255;
1656 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1657 croak("Unknown BYTEORDER\n");
1662 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1663 u.c[o & 0xf] = (l >> s) & 255;
1671 #ifndef CAN_PROTOTYPE
1680 char c[sizeof(long)];
1683 #if BYTEORDER == 0x1234
1684 u.c[0] = (l >> 24) & 255;
1685 u.c[1] = (l >> 16) & 255;
1686 u.c[2] = (l >> 8) & 255;
1690 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1691 croak("Unknown BYTEORDER\n");
1698 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1699 l |= (u.c[o & 0xf] & 255) << s;
1706 #endif /* BYTEORDER != 0x4321 */
1710 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1711 * If these functions are defined,
1712 * the BYTEORDER is neither 0x1234 nor 0x4321.
1713 * However, this is not assumed.
1717 #define HTOV(name,type) \
1724 char c[sizeof(type)]; \
1728 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1729 u.c[i] = (n >> s) & 0xFF; \
1734 #define VTOH(name,type) \
1741 char c[sizeof(type)]; \
1747 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1748 n += (u.c[i] & 0xFF) << s; \
1753 #if defined(HAS_HTOVS) && !defined(htovs)
1756 #if defined(HAS_HTOVL) && !defined(htovl)
1759 #if defined(HAS_VTOHS) && !defined(vtohs)
1762 #if defined(HAS_VTOHL) && !defined(vtohl)
1766 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1767 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1774 register I32 this, that;
1777 I32 doexec = strNE(cmd,"-");
1781 return my_syspopen(cmd,mode);
1786 this = (*mode == 'w');
1788 if (doexec && tainting) {
1790 taint_proper("Insecure %s%s", "EXEC");
1792 while ((pid = (doexec?vfork():fork())) < 0) {
1793 if (errno != EAGAIN) {
1796 croak("Can't fork");
1807 if (p[THIS] != (*mode == 'r')) {
1808 dup2(p[THIS], *mode == 'r');
1812 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1818 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1821 do_exec(cmd); /* may or may not use the shell */
1825 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1826 sv_setiv(GvSV(tmpgv), (IV)getpid());
1828 hv_clear(pidstatus); /* we have no children */
1833 do_execfree(); /* free any memory malloced by child on vfork */
1835 if (p[that] < p[this]) {
1836 dup2(p[this], p[that]);
1840 sv = *av_fetch(fdpid,p[this],TRUE);
1841 (void)SvUPGRADE(sv,SVt_IV);
1844 return PerlIO_fdopen(p[this], mode);
1847 #if defined(atarist) || defined(DJGPP)
1854 /* Needs work for PerlIO ! */
1855 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1856 return popen(PerlIO_exportFILE(cmd, 0), mode);
1860 #endif /* !DOSISH */
1867 struct stat tmpstatbuf;
1869 PerlIO_printf(PerlIO_stderr(),"%s", s);
1870 for (fd = 0; fd < 32; fd++) {
1871 if (Fstat(fd,&tmpstatbuf) >= 0)
1872 PerlIO_printf(PerlIO_stderr()," %d",fd);
1874 PerlIO_printf(PerlIO_stderr(),"\n");
1884 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1888 return fcntl(oldfd, F_DUPFD, newfd);
1890 #define DUP2_MAX_FDS 256
1891 int fdtmp[DUP2_MAX_FDS];
1898 /* good enough for low fd's... */
1899 while ((fd = dup(oldfd)) != newfd && fd >= 0) {
1900 if (fdx >= DUP2_MAX_FDS) {
1908 close(fdtmp[--fdx]);
1915 #ifdef HAS_SIGACTION
1918 rsignal(signo, handler)
1920 Sighandler_t handler;
1922 struct sigaction act, oact;
1924 act.sa_handler = handler;
1925 sigemptyset(&act.sa_mask);
1928 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1930 if (sigaction(signo, &act, &oact) == -1)
1933 return oact.sa_handler;
1937 rsignal_state(signo)
1940 struct sigaction oact;
1942 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1945 return oact.sa_handler;
1949 rsignal_save(signo, handler, save)
1951 Sighandler_t handler;
1954 struct sigaction act;
1956 act.sa_handler = handler;
1957 sigemptyset(&act.sa_mask);
1960 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1962 return sigaction(signo, &act, save);
1966 rsignal_restore(signo, save)
1970 return sigaction(signo, save, (struct sigaction *)NULL);
1973 #else /* !HAS_SIGACTION */
1976 rsignal(signo, handler)
1978 Sighandler_t handler;
1980 return signal(signo, handler);
1983 static int sig_trapped;
1994 rsignal_state(signo)
1997 Sighandler_t oldsig;
2000 oldsig = signal(signo, sig_trap);
2001 signal(signo, oldsig);
2003 kill(getpid(), signo);
2008 rsignal_save(signo, handler, save)
2010 Sighandler_t handler;
2013 *save = signal(signo, handler);
2014 return (*save == SIG_ERR) ? -1 : 0;
2018 rsignal_restore(signo, save)
2022 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
2025 #endif /* !HAS_SIGACTION */
2027 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2028 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
2033 Sigsave_t hstat, istat, qstat;
2040 int saved_vaxc_errno;
2043 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
2044 pid = (int)SvIVX(*svp);
2048 if (pid == -1) { /* Opened by popen. */
2049 return my_syspclose(ptr);
2052 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2053 saved_errno = errno;
2055 saved_vaxc_errno = vaxc$errno;
2059 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2061 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2062 rsignal_save(SIGINT, SIG_IGN, &istat);
2063 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
2065 pid = wait4pid(pid, &status, 0);
2066 } while (pid == -1 && errno == EINTR);
2067 rsignal_restore(SIGHUP, &hstat);
2068 rsignal_restore(SIGINT, &istat);
2069 rsignal_restore(SIGQUIT, &qstat);
2071 SETERRNO(saved_errno, saved_vaxc_errno);
2074 return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status));
2076 #endif /* !DOSISH */
2078 #if !defined(DOSISH) || defined(OS2)
2080 wait4pid(pid,statusp,flags)
2087 char spid[TYPE_CHARS(int)];
2092 sprintf(spid, "%d", pid);
2093 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2094 if (svp && *svp != &sv_undef) {
2095 *statusp = SvIVX(*svp);
2096 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2103 hv_iterinit(pidstatus);
2104 if (entry = hv_iternext(pidstatus)) {
2105 pid = atoi(hv_iterkey(entry,(I32*)statusp));
2106 sv = hv_iterval(pidstatus,entry);
2107 *statusp = SvIVX(sv);
2108 sprintf(spid, "%d", pid);
2109 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2114 # ifdef HAS_WAITPID_RUNTIME
2115 if (!HAS_WAITPID_RUNTIME)
2118 return waitpid(pid,statusp,flags);
2120 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
2121 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2123 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2128 croak("Can't do waitpid with flags");
2130 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
2131 pidgone(result,*statusp);
2139 #endif /* !DOSISH */
2148 char spid[TYPE_CHARS(int)];
2150 sprintf(spid, "%d", pid);
2151 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
2152 (void)SvUPGRADE(sv,SVt_IV);
2157 #if defined(atarist) || defined(OS2) || defined(DJGPP)
2160 int /* Cannot prototype with I32
2169 /* Needs work for PerlIO ! */
2170 FILE *f = PerlIO_findFILE(ptr);
2171 I32 result = pclose(f);
2172 PerlIO_releaseFILE(ptr,f);
2178 repeatcpy(to,from,len,count)
2180 register char *from;
2185 register char *frombase = from;
2193 while (count-- > 0) {
2194 for (todo = len; todo > 0; todo--) {
2201 #ifndef CASTNEGFLOAT
2209 # define BIGDOUBLE 2147483648.0
2211 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2214 return (unsigned long)f;
2216 return (unsigned long)along;
2223 /* Unfortunately, on some systems the cast_uv() function doesn't
2224 work with the system-supplied definition of ULONG_MAX. The
2225 comparison (f >= ULONG_MAX) always comes out true. It must be a
2226 problem with the compiler constant folding.
2228 In any case, this workaround should be fine on any two's complement
2229 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2231 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2234 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2236 -- Kenneth Albanowski <kjahds@kjahds.com>
2240 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2248 return (I32) I32_MAX;
2250 return (I32) I32_MIN;
2270 return (UV) MY_UV_MAX;
2282 char *fa = strrchr(a,'/');
2283 char *fb = strrchr(b,'/');
2284 struct stat tmpstatbuf1;
2285 struct stat tmpstatbuf2;
2286 SV *tmpsv = sv_newmortal();
2299 sv_setpv(tmpsv, ".");
2301 sv_setpvn(tmpsv, a, fa - a);
2302 if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
2305 sv_setpv(tmpsv, ".");
2307 sv_setpvn(tmpsv, b, fb - b);
2308 if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
2310 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2311 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2313 #endif /* !HAS_RENAME */
2316 scan_oct(start, len, retlen)
2321 register char *s = start;
2322 register UV retval = 0;
2323 bool overflowed = FALSE;
2325 while (len && *s >= '0' && *s <= '7') {
2326 register UV n = retval << 3;
2327 if (!overflowed && (n >> 3) != retval) {
2328 warn("Integer overflow in octal number");
2331 retval = n | (*s++ - '0');
2334 if (dowarn && len && (*s == '8' || *s == '9'))
2335 warn("Illegal octal digit ignored");
2336 *retlen = s - start;
2341 scan_hex(start, len, retlen)
2346 register char *s = start;
2347 register UV retval = 0;
2348 bool overflowed = FALSE;
2351 while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
2352 register UV n = retval << 4;
2353 if (!overflowed && (n >> 4) != retval) {
2354 warn("Integer overflow in hex number");
2357 retval = n | (tmp - hexdigit) & 15;
2360 *retlen = s - start;
2366 /* Very simplistic scheduler for now */
2370 thr = thr->i.next_run;
2381 perl_cond_signal(cp)
2385 perl_cond cond = *cp;
2390 /* Insert t in the runnable queue just ahead of us */
2391 t->i.next_run = thr->i.next_run;
2392 thr->i.next_run->i.prev_run = t;
2393 t->i.prev_run = thr;
2394 thr->i.next_run = t;
2395 thr->i.wait_queue = 0;
2396 /* Remove from the wait queue */
2402 perl_cond_broadcast(cp)
2406 perl_cond cond, cond_next;
2408 for (cond = *cp; cond; cond = cond_next) {
2410 /* Insert t in the runnable queue just ahead of us */
2411 t->i.next_run = thr->i.next_run;
2412 thr->i.next_run->i.prev_run = t;
2413 t->i.prev_run = thr;
2414 thr->i.next_run = t;
2415 thr->i.wait_queue = 0;
2416 /* Remove from the wait queue */
2417 cond_next = cond->next;
2429 if (thr->i.next_run == thr)
2430 croak("panic: perl_cond_wait called by last runnable thread");
2432 New(666, cond, 1, struct perl_wait_queue);
2436 thr->i.wait_queue = cond;
2437 /* Remove ourselves from runnable queue */
2438 thr->i.next_run->i.prev_run = thr->i.prev_run;
2439 thr->i.prev_run->i.next_run = thr->i.next_run;
2441 #endif /* FAKE_THREADS */
2443 #ifdef OLD_PTHREADS_API
2449 if (pthread_getspecific(thr_key, &t))
2450 croak("panic: pthread_getspecific");
2451 return (struct thread *) t;
2453 #endif /* OLD_PTHREADS_API */
2461 SvUPGRADE(sv, SVt_PVMG);
2462 mg = mg_find(sv, 'm');
2466 New(53, cp, 1, condpair_t);
2467 MUTEX_INIT(&cp->mutex);
2468 COND_INIT(&cp->owner_cond);
2469 COND_INIT(&cp->cond);
2471 MUTEX_LOCK(&sv_mutex);
2472 mg = mg_find(sv, 'm');
2474 /* someone else beat us to initialising it */
2475 MUTEX_UNLOCK(&sv_mutex);
2476 MUTEX_DESTROY(&cp->mutex);
2477 COND_DESTROY(&cp->owner_cond);
2478 COND_DESTROY(&cp->cond);
2482 sv_magic(sv, Nullsv, 'm', 0, 0);
2484 mg->mg_ptr = (char *)cp;
2485 mg->mg_len = sizeof(cp);
2486 MUTEX_UNLOCK(&sv_mutex);
2487 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
2488 "%p: condpair_magic %p\n", thr, sv));)
2495 * Make a new perl thread structure using t as a prototype. Some of the
2496 * fields for the new thread are copied from the prototype thread, t,
2497 * so t should not be running in perl at the time this function is
2498 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
2499 * thread calling new_struct_thread) clearly satisfies this constraint.
2502 new_struct_thread(t)
2510 sv = newSVpv("", 0);
2511 SvGROW(sv, sizeof(struct thread) + 1);
2512 SvCUR_set(sv, sizeof(struct thread));
2513 thr = (Thread) SvPVX(sv);
2515 memset(thr, 0xab, sizeof(struct thread));
2527 curcop = &compiling;
2528 thr->cvcache = newHV();
2529 thr->magicals = newAV();
2530 thr->specific = newAV();
2531 thr->flags = THRf_R_JOINABLE;
2532 MUTEX_INIT(&thr->mutex);
2534 curcop = t->Tcurcop; /* XXX As good a guess as any? */
2535 defstash = t->Tdefstash; /* XXX maybe these should */
2536 curstash = t->Tcurstash; /* always be set to main? */
2539 /* top_env needs to be non-zero. It points to an area
2540 in which longjmp() stuff is stored, as C callstack
2541 info there at least is thread specific this has to
2542 be per-thread. Otherwise a 'die' in a thread gives
2543 that thread the C stack of last thread to do an eval {}!
2544 See comments in scope.h
2545 Initialize top entry (as in perl.c for main thread)
2547 start_env.je_prev = NULL;
2548 start_env.je_ret = -1;
2549 start_env.je_mustcatch = TRUE;
2550 top_env = &start_env;
2552 runlevel = 0; /* Let entering sub do increment */
2557 tainted = t->Ttainted;
2558 curpm = t->Tcurpm; /* XXX No PMOP ref count */
2559 nrs = newSVsv(t->Tnrs);
2560 rs = newSVsv(t->Trs);
2561 last_in_gv = (GV*)SvREFCNT_inc(t->Tlast_in_gv);
2562 ofslen = t->Tofslen;
2563 ofs = savepvn(t->Tofs, ofslen);
2564 defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
2565 chopset = t->Tchopset;
2566 formtarget = newSVsv(t->Tformtarget);
2567 bodytarget = newSVsv(t->Tbodytarget);
2568 toptarget = newSVsv(t->Ttoptarget);
2570 /* Initialise all per-thread magicals that the template thread used */
2571 svp = AvARRAY(t->magicals);
2572 for (i = 0; i <= AvFILL(t->magicals); i++, svp++) {
2573 if (*svp && *svp != &sv_undef) {
2574 SV *sv = newSVsv(*svp);
2575 av_store(thr->magicals, i, sv);
2576 sv_magic(sv, 0, 0, &per_thread_magicals[i], 1);
2577 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2578 "new_struct_thread: copied magical %d %p->%p\n",i,
2583 MUTEX_LOCK(&threads_mutex);
2585 thr->tid = ++threadnum;
2586 thr->next = t->next;
2589 thr->next->prev = thr;
2590 MUTEX_UNLOCK(&threads_mutex);
2593 * This is highly suspect - new_struct_thread is executed
2594 * by creating thread so pthread_self() or equivalent
2595 * is parent thread not the child.
2596 * In particular this should _NOT_ change dTHR value of calling thread.
2598 * But a good place to have a 'hook' for filling in port-private
2601 #ifdef INIT_THREAD_INTERN
2602 INIT_THREAD_INTERN(thr);
2604 thr->self = pthread_self();
2605 #endif /* INIT_THREAD_INTERN */
2608 #endif /* USE_THREADS */
2612 * This hack is to force load of "huge" support from libm.a
2613 * So it is in perl for (say) POSIX to use.
2614 * Needed for SunOS with Sun's 'acc' for example.