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));
61 /* paranoid version of malloc */
63 /* NOTE: Do not call the next three routines directly. Use the macros
64 * in handy.h, so that we can easily redefine everything to do tracking of
65 * allocated hunks back to the original New to track down any memory leaks.
66 * XXX This advice seems to be widely ignored :-( --AD August 1996.
70 safemalloc(MEM_SIZE size)
75 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
78 #endif /* HAS_64K_LIMIT */
81 croak("panic: malloc");
83 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
84 #if !(defined(I286) || defined(atarist))
85 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
87 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
94 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
101 /* paranoid version of realloc */
104 saferealloc(Malloc_t where,MEM_SIZE size)
107 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
109 #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
113 PerlIO_printf(PerlIO_stderr(),
114 "Reallocation too large: %lx\n", size) FLUSH;
117 #endif /* HAS_64K_LIMIT */
119 croak("Null realloc");
122 croak("panic: realloc");
124 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
126 #if !(defined(I286) || defined(atarist))
128 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++);
129 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
133 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++);
134 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
143 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
150 /* safe version of free */
153 safefree(Malloc_t where)
155 #if !(defined(I286) || defined(atarist))
156 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,an++));
158 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,an++));
166 /* safe version of calloc */
169 safecalloc(MEM_SIZE count, MEM_SIZE size)
174 if (size * count > 0xffff) {
175 PerlIO_printf(PerlIO_stderr(),
176 "Allocation too large: %lx\n", size * count) FLUSH;
179 #endif /* HAS_64K_LIMIT */
181 if ((long)size < 0 || (long)count < 0)
182 croak("panic: calloc");
185 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
186 #if !(defined(I286) || defined(atarist))
187 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
189 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
192 memset((void*)ptr, 0, size);
198 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
205 #endif /* !MYMALLOC */
209 #define ALIGN sizeof(long)
212 safexmalloc(I32 x, MEM_SIZE size)
214 register Malloc_t where;
216 where = safemalloc(size + ALIGN);
220 return where + ALIGN;
224 safexrealloc(Malloc_t where, MEM_SIZE size)
226 register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN);
231 safexfree(Malloc_t where)
238 x = where[0] + 100 * where[1];
244 safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
246 register Malloc_t where;
248 where = safexmalloc(x, size * count + ALIGN);
250 memset((void*)where + ALIGN, 0, size * count);
253 return where + ALIGN;
261 for (i = 0; i < MAXXCOUNT; i++) {
262 if (xcount[i] > lastxcount[i]) {
263 PerlIO_printf(PerlIO_stderr(),"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
264 lastxcount[i] = xcount[i];
269 #endif /* LEAKTEST */
271 /* copy a string up to some (non-backslashed) delimiter, if any */
274 delimcpy(register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
277 for (tolen = 0; from < fromend; from++, tolen++) {
279 if (from[1] == delim)
288 else if (*from == delim)
299 /* return ptr to little string in big string, NULL if not found */
300 /* This routine was donated by Corey Satten. */
303 instr(register char *big, register char *little)
305 register char *s, *x;
316 for (x=big,s=little; *s; /**/ ) {
330 /* same as instr but allow embedded nulls */
333 ninstr(register char *big, register char *bigend, char *little, char *lend)
335 register char *s, *x;
336 register I32 first = *little;
337 register char *littleend = lend;
339 if (!first && little >= littleend)
341 if (bigend - big < littleend - little)
343 bigend -= littleend - little++;
344 while (big <= bigend) {
347 for (x=big,s=little; s < littleend; /**/ ) {
359 /* reverse of the above--find last substring */
362 rninstr(register char *big, char *bigend, char *little, char *lend)
364 register char *bigbeg;
365 register char *s, *x;
366 register I32 first = *little;
367 register char *littleend = lend;
369 if (!first && little >= littleend)
372 big = bigend - (littleend - little++);
373 while (big >= bigbeg) {
376 for (x=big+2,s=little; s < littleend; /**/ ) {
389 * Set up for a new ctype locale.
392 perl_new_ctype(char *newctype)
394 #ifdef USE_LOCALE_CTYPE
398 for (i = 0; i < 256; i++) {
400 fold_locale[i] = toLOWER_LC(i);
401 else if (isLOWER_LC(i))
402 fold_locale[i] = toUPPER_LC(i);
407 #endif /* USE_LOCALE_CTYPE */
411 * Set up for a new collation locale.
414 perl_new_collate(char *newcoll)
416 #ifdef USE_LOCALE_COLLATE
419 if (collation_name) {
421 Safefree(collation_name);
422 collation_name = NULL;
423 collation_standard = TRUE;
430 if (! collation_name || strNE(collation_name, newcoll)) {
432 Safefree(collation_name);
433 collation_name = savepv(newcoll);
434 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
437 /* 2: at most so many chars ('a', 'b'). */
438 /* 50: surely no system expands a char more. */
439 #define XFRMBUFSIZE (2 * 50)
440 char xbuf[XFRMBUFSIZE];
441 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
442 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
443 SSize_t mult = fb - fa;
445 croak("strxfrm() gets absurd");
446 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
447 collxfrm_mult = mult;
451 #endif /* USE_LOCALE_COLLATE */
455 * Set up for a new numeric locale.
458 perl_new_numeric(char *newnum)
460 #ifdef USE_LOCALE_NUMERIC
464 Safefree(numeric_name);
466 numeric_standard = TRUE;
467 numeric_local = TRUE;
472 if (! numeric_name || strNE(numeric_name, newnum)) {
473 Safefree(numeric_name);
474 numeric_name = savepv(newnum);
475 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
476 numeric_local = TRUE;
479 #endif /* USE_LOCALE_NUMERIC */
483 perl_set_numeric_standard(void)
485 #ifdef USE_LOCALE_NUMERIC
487 if (! numeric_standard) {
488 setlocale(LC_NUMERIC, "C");
489 numeric_standard = TRUE;
490 numeric_local = FALSE;
493 #endif /* USE_LOCALE_NUMERIC */
497 perl_set_numeric_local(void)
499 #ifdef USE_LOCALE_NUMERIC
501 if (! numeric_local) {
502 setlocale(LC_NUMERIC, numeric_name);
503 numeric_standard = FALSE;
504 numeric_local = TRUE;
507 #endif /* USE_LOCALE_NUMERIC */
512 * Initialize locale awareness.
515 perl_init_i18nl10n(int printwarn)
519 * 1 = set ok or not applicable,
520 * 0 = fallback to C locale,
521 * -1 = fallback to C locale failed
526 #ifdef USE_LOCALE_CTYPE
527 char *curctype = NULL;
528 #endif /* USE_LOCALE_CTYPE */
529 #ifdef USE_LOCALE_COLLATE
530 char *curcoll = NULL;
531 #endif /* USE_LOCALE_COLLATE */
532 #ifdef USE_LOCALE_NUMERIC
534 #endif /* USE_LOCALE_NUMERIC */
535 char *lc_all = getenv("LC_ALL");
536 char *lang = getenv("LANG");
537 bool setlocale_failure = FALSE;
539 #ifdef LOCALE_ENVIRON_REQUIRED
542 * Ultrix setlocale(..., "") fails if there are no environment
543 * variables from which to get a locale name.
550 if (setlocale(LC_ALL, ""))
553 setlocale_failure = TRUE;
555 if (!setlocale_failure)
558 #ifdef USE_LOCALE_CTYPE
559 if (! (curctype = setlocale(LC_CTYPE,
560 (!done && (lang || getenv("LC_CTYPE")))
562 setlocale_failure = TRUE;
563 #endif /* USE_LOCALE_CTYPE */
564 #ifdef USE_LOCALE_COLLATE
565 if (! (curcoll = setlocale(LC_COLLATE,
566 (!done && (lang || getenv("LC_COLLATE")))
568 setlocale_failure = TRUE;
569 #endif /* USE_LOCALE_COLLATE */
570 #ifdef USE_LOCALE_NUMERIC
571 if (! (curnum = setlocale(LC_NUMERIC,
572 (!done && (lang || getenv("LC_NUMERIC")))
574 setlocale_failure = TRUE;
575 #endif /* USE_LOCALE_NUMERIC */
578 #else /* !LOCALE_ENVIRON_REQUIRED */
582 if (! setlocale(LC_ALL, ""))
583 setlocale_failure = TRUE;
585 #ifdef USE_LOCALE_CTYPE
586 curctype = setlocale(LC_CTYPE, Nullch);
587 #endif /* USE_LOCALE_CTYPE */
588 #ifdef USE_LOCALE_COLLATE
589 curcoll = setlocale(LC_COLLATE, Nullch);
590 #endif /* USE_LOCALE_COLLATE */
591 #ifdef USE_LOCALE_NUMERIC
592 curnum = setlocale(LC_NUMERIC, Nullch);
593 #endif /* USE_LOCALE_NUMERIC */
598 #ifdef USE_LOCALE_CTYPE
599 if (! (curctype = setlocale(LC_CTYPE, "")))
600 setlocale_failure = TRUE;
601 #endif /* USE_LOCALE_CTYPE */
602 #ifdef USE_LOCALE_COLLATE
603 if (! (curcoll = setlocale(LC_COLLATE, "")))
604 setlocale_failure = TRUE;
605 #endif /* USE_LOCALE_COLLATE */
606 #ifdef USE_LOCALE_NUMERIC
607 if (! (curnum = setlocale(LC_NUMERIC, "")))
608 setlocale_failure = TRUE;
609 #endif /* USE_LOCALE_NUMERIC */
613 #endif /* !LOCALE_ENVIRON_REQUIRED */
615 if (setlocale_failure) {
617 bool locwarn = (printwarn > 1 ||
619 (!(p = getenv("PERL_BADLANG")) || atoi(p)));
624 PerlIO_printf(PerlIO_stderr(),
625 "perl: warning: Setting locale failed.\n");
629 PerlIO_printf(PerlIO_stderr(),
630 "perl: warning: Setting locale failed for the categories:\n\t");
631 #ifdef USE_LOCALE_CTYPE
633 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
634 #endif /* USE_LOCALE_CTYPE */
635 #ifdef USE_LOCALE_COLLATE
637 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
638 #endif /* USE_LOCALE_COLLATE */
639 #ifdef USE_LOCALE_NUMERIC
641 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
642 #endif /* USE_LOCALE_NUMERIC */
643 PerlIO_printf(PerlIO_stderr(), "\n");
647 PerlIO_printf(PerlIO_stderr(),
648 "perl: warning: Please check that your locale settings:\n");
650 PerlIO_printf(PerlIO_stderr(),
651 "\tLC_ALL = %c%s%c,\n",
653 lc_all ? lc_all : "unset",
658 for (e = environ; *e; e++) {
659 if (strnEQ(*e, "LC_", 3)
660 && strnNE(*e, "LC_ALL=", 7)
661 && (p = strchr(*e, '=')))
662 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
663 (int)(p - *e), *e, p + 1);
667 PerlIO_printf(PerlIO_stderr(),
670 lang ? lang : "unset",
673 PerlIO_printf(PerlIO_stderr(),
674 " are supported and installed on your system.\n");
679 if (setlocale(LC_ALL, "C")) {
681 PerlIO_printf(PerlIO_stderr(),
682 "perl: warning: Falling back to the standard locale (\"C\").\n");
687 PerlIO_printf(PerlIO_stderr(),
688 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
695 #ifdef USE_LOCALE_CTYPE
696 || !(curctype || setlocale(LC_CTYPE, "C"))
697 #endif /* USE_LOCALE_CTYPE */
698 #ifdef USE_LOCALE_COLLATE
699 || !(curcoll || setlocale(LC_COLLATE, "C"))
700 #endif /* USE_LOCALE_COLLATE */
701 #ifdef USE_LOCALE_NUMERIC
702 || !(curnum || setlocale(LC_NUMERIC, "C"))
703 #endif /* USE_LOCALE_NUMERIC */
707 PerlIO_printf(PerlIO_stderr(),
708 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
712 #endif /* ! LC_ALL */
714 #ifdef USE_LOCALE_CTYPE
715 curctype = setlocale(LC_CTYPE, Nullch);
716 #endif /* USE_LOCALE_CTYPE */
717 #ifdef USE_LOCALE_COLLATE
718 curcoll = setlocale(LC_COLLATE, Nullch);
719 #endif /* USE_LOCALE_COLLATE */
720 #ifdef USE_LOCALE_NUMERIC
721 curnum = setlocale(LC_NUMERIC, Nullch);
722 #endif /* USE_LOCALE_NUMERIC */
725 #ifdef USE_LOCALE_CTYPE
726 perl_new_ctype(curctype);
727 #endif /* USE_LOCALE_CTYPE */
729 #ifdef USE_LOCALE_COLLATE
730 perl_new_collate(curcoll);
731 #endif /* USE_LOCALE_COLLATE */
733 #ifdef USE_LOCALE_NUMERIC
734 perl_new_numeric(curnum);
735 #endif /* USE_LOCALE_NUMERIC */
737 #endif /* USE_LOCALE */
742 /* Backwards compatibility. */
744 perl_init_i18nl14n(int printwarn)
746 return perl_init_i18nl10n(printwarn);
749 #ifdef USE_LOCALE_COLLATE
752 * mem_collxfrm() is a bit like strxfrm() but with two important
753 * differences. First, it handles embedded NULs. Second, it allocates
754 * a bit more memory than needed for the transformed data itself.
755 * The real transformed data begins at offset sizeof(collationix).
756 * Please see sv_collxfrm() to see how this is used.
759 mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
762 STRLEN xalloc, xin, xout;
764 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
765 /* the +1 is for the terminating NUL. */
767 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
768 New(171, xbuf, xalloc, char);
772 *(U32*)xbuf = collation_ix;
773 xout = sizeof(collation_ix);
774 for (xin = 0; xin < len; ) {
778 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
781 if (xused < xalloc - xout)
783 xalloc = (2 * xalloc) + 1;
784 Renew(xbuf, xalloc, char);
789 xin += strlen(s + xin) + 1;
792 /* Embedded NULs are understood but silently skipped
793 * because they make no sense in locale collation. */
797 *xlen = xout - sizeof(collation_ix);
806 #endif /* USE_LOCALE_COLLATE */
811 register unsigned char *s;
812 register unsigned char *table;
814 register U32 len = SvCUR(sv);
818 sv_upgrade(sv, SVt_PVBM);
819 if (len > 255 || len == 0) /* TAIL might be on on a zero-length string. */
820 return; /* can't have offsets that big */
822 Sv_Grow(sv,len + 258);
823 table = (unsigned char*)(SvPVX(sv) + len + 1);
825 for (i = 0; i < 256; i++) {
829 while (s >= (unsigned char*)(SvPVX(sv)))
831 if (table[*s] == len)
836 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
839 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
840 for (i = 0; i < len; i++) {
841 if (freq[s[i]] < frequency) {
843 frequency = freq[s[i]];
846 BmRARE(sv) = s[rarest];
847 BmPREVIOUS(sv) = rarest;
848 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
852 fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr)
854 register unsigned char *s;
856 register I32 littlelen;
857 register unsigned char *little;
858 register unsigned char *table;
859 register unsigned char *olds;
860 register unsigned char *oldlittle;
862 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
864 char *l = SvPV(littlestr,len);
866 if (SvTAIL(littlestr)) { /* Can be only 0-len constant
867 substr => we can ignore SvVALID */
870 if ((s = (unsigned char*)ninstr((char*)big, (char*)bigend,
875 if (bigend > big && bigend[-1] == '\n')
876 return (char *)(bigend - 1);
878 return (char *) bigend;
882 return ninstr((char*)big,(char*)bigend, l, l + len);
885 littlelen = SvCUR(littlestr);
886 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
887 if (littlelen > bigend - big)
889 little = (unsigned char*)SvPVX(littlestr);
890 s = bigend - littlelen;
892 && bigend[-1] == '\n'
893 && s[-1] == *little && memEQ((char*)s - 1,(char*)little,littlelen))
894 return (char*)s - 1; /* how sweet it is */
895 else if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
896 return (char*)s; /* how sweet it is */
899 if (littlelen <= 2) {
900 unsigned char c1 = (unsigned char)SvPVX(littlestr)[0];
901 unsigned char c2 = (unsigned char)SvPVX(littlestr)[1];
902 /* This may do extra comparisons if littlelen == 2, but this
903 should be hidden in the noise since we do less indirection. */
907 while (s <= bigend) {
909 && (littlelen == 1 || s[1] == c2)
910 && (!SvTAIL(littlestr)
912 || s[littlelen] == '\n')) /* Automatically multiline */
920 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
921 if (--littlelen >= bigend - big)
924 oldlittle = little = table - 2;
928 if (tmp = table[*s]) {
930 if (bigend - s > tmp) {
935 if ((s += tmp) < bigend)
941 tmp = littlelen; /* less expensive than calling strncmp() */
944 if (*--s == *--little)
947 s = olds + 1; /* here we pay the price for failure */
949 if (s < bigend) /* fake up continue to outer loop */
953 if (SvTAIL(littlestr) /* automatically multiline */
954 && olds + 1 != bigend
963 /* start_shift, end_shift are positive quantities which give offsets
964 of ends of some substring of bigstr.
965 If `last' we want the last occurence.
966 old_posp is the way of communication between consequent calls if
967 the next call needs to find the .
968 The initial *old_posp should be -1.
969 Note that we do not take into account SvTAIL, so it may give wrong
970 positives if _ALL flag is set.
974 screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
976 register unsigned char *s, *x;
977 register unsigned char *big;
979 register I32 previous;
981 register unsigned char *little;
982 register I32 stop_pos;
983 register unsigned char *littleend;
987 ? (pos = screamfirst[BmRARE(littlestr)]) < 0
988 : (((pos = *old_posp), pos += screamnext[pos]) == 0))
990 little = (unsigned char *)(SvPVX(littlestr));
991 littleend = little + SvCUR(littlestr);
993 /* The value of pos we can start at: */
994 previous = BmPREVIOUS(littlestr);
995 big = (unsigned char *)(SvPVX(bigstr));
996 /* The value of pos we can stop at: */
997 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
998 if (previous + start_shift > stop_pos) return Nullch;
999 while (pos < previous + start_shift) {
1000 if (!(pos += screamnext[pos]))
1005 if (pos >= stop_pos) return Nullch;
1006 if (big[pos-previous] != first)
1008 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
1014 if (s == littleend) {
1016 if (!last) return (char *)(big+pos-previous);
1019 } while ( pos += screamnext[pos] );
1020 return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch;
1021 #else /* !POINTERRIGOR */
1024 if (pos >= stop_pos) return Nullch;
1025 if (big[pos] != first)
1027 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
1033 if (s == littleend) {
1035 if (!last) return (char *)(big+pos);
1038 } while ( pos += screamnext[pos] );
1039 return (last && found) ? (char *)(big+(*old_posp)) : Nullch;
1040 #endif /* POINTERRIGOR */
1044 ibcmp(char *s1, char *s2, register I32 len)
1046 register U8 *a = (U8 *)s1;
1047 register U8 *b = (U8 *)s2;
1049 if (*a != *b && *a != fold[*b])
1057 ibcmp_locale(char *s1, char *s2, register I32 len)
1059 register U8 *a = (U8 *)s1;
1060 register U8 *b = (U8 *)s2;
1062 if (*a != *b && *a != fold_locale[*b])
1069 /* copy a string to a safe spot */
1074 register char *newaddr;
1076 New(902,newaddr,strlen(sv)+1,char);
1077 (void)strcpy(newaddr,sv);
1081 /* same thing but with a known length */
1084 savepvn(char *sv, register I32 len)
1086 register char *newaddr;
1088 New(903,newaddr,len+1,char);
1089 Copy(sv,newaddr,len,char); /* might not be null terminated */
1090 newaddr[len] = '\0'; /* is now */
1094 /* the SV for form() and mess() is not kept in an arena */
1102 /* Create as PVMG now, to avoid any upgrading later */
1103 New(905, sv, 1, SV);
1104 Newz(905, any, 1, XPVMG);
1105 SvFLAGS(sv) = SVt_PVMG;
1106 SvANY(sv) = (void*)any;
1107 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1113 form(const char* pat, ...)
1124 va_start(args, pat);
1129 mess_sv = mess_alloc();
1130 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
1132 return SvPVX(mess_sv);
1136 mess(const char *pat, va_list *args)
1139 static char dgd[] = " during global destruction.\n";
1142 mess_sv = mess_alloc();
1144 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
1145 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1150 if (curcop->cop_line)
1151 sv_catpvf(sv, " at %_ line %ld",
1152 GvSV(curcop->cop_filegv), (long)curcop->cop_line);
1153 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1154 bool line_mode = (RsSIMPLE(rs) &&
1155 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
1156 sv_catpvf(sv, ", <%s> %s %ld",
1157 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1158 line_mode ? "line" : "chunk",
1159 (long)IoLINES(GvIOp(last_in_gv)));
1161 sv_catpv(sv, ".\n");
1169 die(const char* pat, ...)
1181 int was_in_eval = in_eval;
1187 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1188 "%p: die: curstack = %p, mainstack = %p\n",
1189 thr, curstack, mainstack));
1190 #endif /* USE_THREADS */
1191 /* We have to switch back to mainstack or die_where may try to pop
1192 * the eval block from the wrong stack if die is being called from a
1193 * signal handler. - dkindred@cs.cmu.edu */
1194 if (curstack != mainstack) {
1196 SWITCHSTACK(curstack, mainstack);
1200 va_start(args, pat);
1204 message = mess(pat, &args);
1208 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1209 "%p: die: message = %s\ndiehook = %p\n",
1210 thr, message, diehook));
1211 #endif /* USE_THREADS */
1213 /* sv_2cv might call croak() */
1214 SV *olddiehook = diehook;
1218 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1220 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1225 msg = newSVpv(message, 0);
1232 perl_call_sv((SV*)cv, G_DISCARD);
1238 restartop = die_where(message);
1240 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1241 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
1242 thr, restartop, was_in_eval, top_env));
1243 #endif /* USE_THREADS */
1244 if ((!restartop && was_in_eval) || top_env->je_prev)
1251 croak(const char* pat, ...)
1255 croak(pat, va_alist)
1268 va_start(args, pat);
1272 message = mess(pat, &args);
1275 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
1276 #endif /* USE_THREADS */
1278 /* sv_2cv might call croak() */
1279 SV *olddiehook = diehook;
1283 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1285 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1290 msg = newSVpv(message, 0);
1297 perl_call_sv((SV*)cv, G_DISCARD);
1303 restartop = die_where(message);
1306 PerlIO_puts(PerlIO_stderr(),message);
1307 (void)PerlIO_flush(PerlIO_stderr());
1313 warn(const char* pat,...)
1328 va_start(args, pat);
1332 message = mess(pat, &args);
1336 /* sv_2cv might call warn() */
1338 SV *oldwarnhook = warnhook;
1342 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1344 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1349 msg = newSVpv(message, 0);
1356 perl_call_sv((SV*)cv, G_DISCARD);
1362 PerlIO_puts(PerlIO_stderr(),message);
1366 (void)PerlIO_flush(PerlIO_stderr());
1369 #ifndef VMS /* VMS' my_setenv() is in VMS.c */
1372 my_setenv(char *nam, char *val)
1374 register I32 i=setenv_getix(nam); /* where does it go? */
1376 if (environ == origenviron) { /* need we copy environment? */
1382 for (max = i; environ[max]; max++) ;
1383 New(901,tmpenv, max+2, char*);
1384 for (j=0; j<max; j++) /* copy environment */
1385 tmpenv[j] = savepv(environ[j]);
1386 tmpenv[max] = Nullch;
1387 environ = tmpenv; /* tell exec where it is now */
1390 Safefree(environ[i]);
1391 while (environ[i]) {
1392 environ[i] = environ[i+1];
1397 if (!environ[i]) { /* does not exist yet */
1398 Renew(environ, i+2, char*); /* just expand it a bit */
1399 environ[i+1] = Nullch; /* make sure it's null terminated */
1402 Safefree(environ[i]);
1403 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
1405 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
1407 /* MS-DOS requires environment variable names to be in uppercase */
1408 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1409 * some utilities and applications may break because they only look
1410 * for upper case strings. (Fixed strupr() bug here.)]
1412 strcpy(environ[i],nam); strupr(environ[i]);
1413 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1417 #else /* if WIN32 */
1420 my_setenv(char *nam,char *val)
1423 #ifdef USE_WIN32_RTL_ENV
1425 register char *envstr;
1426 STRLEN namlen = strlen(nam);
1428 char *oldstr = environ[setenv_getix(nam)];
1430 /* putenv() has totally broken semantics in both the Borland
1431 * and Microsoft CRTLs. They either store the passed pointer in
1432 * the environment without making a copy, or make a copy and don't
1433 * free it. And on top of that, they dont free() old entries that
1434 * are being replaced/deleted. This means the caller must
1435 * free any old entries somehow, or we end up with a memory
1436 * leak every time my_setenv() is called. One might think
1437 * one could directly manipulate environ[], like the UNIX code
1438 * above, but direct changes to environ are not allowed when
1439 * calling putenv(), since the RTLs maintain an internal
1440 * *copy* of environ[]. Bad, bad, *bad* stink.
1451 vallen = strlen(val);
1452 New(904, envstr, namlen + vallen + 3, char);
1453 (void)sprintf(envstr,"%s=%s",nam,val);
1454 (void)putenv(envstr);
1458 Safefree(envstr); /* MSVCRT leaks without this */
1461 #else /* !USE_WIN32_RTL_ENV */
1463 /* The sane way to deal with the environment.
1464 * Has these advantages over putenv() & co.:
1465 * * enables us to store a truly empty value in the
1466 * environment (like in UNIX).
1467 * * we don't have to deal with RTL globals, bugs and leaks.
1469 * Why you may want to enable USE_WIN32_RTL_ENV:
1470 * * environ[] and RTL functions will not reflect changes,
1471 * which might be an issue if extensions want to access
1472 * the env. via RTL. This cuts both ways, since RTL will
1473 * not see changes made by extensions that call the Win32
1474 * functions directly, either.
1477 SetEnvironmentVariable(nam,val);
1485 setenv_getix(char *nam)
1487 register I32 i, len = strlen(nam);
1489 for (i = 0; environ[i]; i++) {
1492 strnicmp(environ[i],nam,len) == 0
1494 strnEQ(environ[i],nam,len)
1496 && environ[i][len] == '=')
1497 break; /* strnEQ must come first to avoid */
1498 } /* potential SEGV's */
1504 #ifdef UNLINK_ALL_VERSIONS
1506 unlnk(f) /* unlink all versions of a file */
1511 for (i = 0; unlink(f) >= 0; i++) ;
1516 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
1518 my_bcopy(register char *from,register char *to,register I32 len)
1522 if (from - to >= 0) {
1530 *(--to) = *(--from);
1538 my_memset(loc,ch,len)
1551 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
1565 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
1567 my_memcmp(s1,s2,len)
1572 register U8 *a = (U8 *)s1;
1573 register U8 *b = (U8 *)s2;
1577 if (tmp = *a++ - *b++)
1582 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
1584 #if defined(I_STDARG) || defined(I_VARARGS)
1587 #ifdef USE_CHAR_VSPRINTF
1592 vsprintf(dest, pat, args)
1599 fakebuf._ptr = dest;
1600 fakebuf._cnt = 32767;
1604 fakebuf._flag = _IOWRT|_IOSTRG;
1605 _doprnt(pat, args, &fakebuf); /* what a kludge */
1606 (void)putc('\0', &fakebuf);
1607 #ifdef USE_CHAR_VSPRINTF
1610 return 0; /* perl doesn't use return value */
1614 #endif /* HAS_VPRINTF */
1615 #endif /* I_VARARGS || I_STDARGS */
1618 #if BYTEORDER != 0x4321
1620 #ifndef CAN_PROTOTYPE
1627 #if (BYTEORDER & 1) == 0
1630 result = ((s & 255) << 8) + ((s >> 8) & 255);
1638 #ifndef CAN_PROTOTYPE
1647 char c[sizeof(long)];
1650 #if BYTEORDER == 0x1234
1651 u.c[0] = (l >> 24) & 255;
1652 u.c[1] = (l >> 16) & 255;
1653 u.c[2] = (l >> 8) & 255;
1657 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1658 croak("Unknown BYTEORDER\n");
1663 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1664 u.c[o & 0xf] = (l >> s) & 255;
1672 #ifndef CAN_PROTOTYPE
1681 char c[sizeof(long)];
1684 #if BYTEORDER == 0x1234
1685 u.c[0] = (l >> 24) & 255;
1686 u.c[1] = (l >> 16) & 255;
1687 u.c[2] = (l >> 8) & 255;
1691 #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
1692 croak("Unknown BYTEORDER\n");
1699 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1700 l |= (u.c[o & 0xf] & 255) << s;
1707 #endif /* BYTEORDER != 0x4321 */
1711 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1712 * If these functions are defined,
1713 * the BYTEORDER is neither 0x1234 nor 0x4321.
1714 * However, this is not assumed.
1718 #define HTOV(name,type) \
1725 char c[sizeof(type)]; \
1729 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1730 u.c[i] = (n >> s) & 0xFF; \
1735 #define VTOH(name,type) \
1742 char c[sizeof(type)]; \
1748 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1749 n += (u.c[i] & 0xFF) << s; \
1754 #if defined(HAS_HTOVS) && !defined(htovs)
1757 #if defined(HAS_HTOVL) && !defined(htovl)
1760 #if defined(HAS_VTOHS) && !defined(vtohs)
1763 #if defined(HAS_VTOHL) && !defined(vtohl)
1767 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1768 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
1770 my_popen(char *cmd, char *mode)
1773 register I32 This, that;
1776 I32 doexec = strNE(cmd,"-");
1780 return my_syspopen(cmd,mode);
1785 This = (*mode == 'w');
1787 if (doexec && tainting) {
1789 taint_proper("Insecure %s%s", "EXEC");
1791 while ((pid = (doexec?vfork():fork())) < 0) {
1792 if (errno != EAGAIN) {
1795 croak("Can't fork");
1806 if (p[THIS] != (*mode == 'r')) {
1807 dup2(p[THIS], *mode == 'r');
1811 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
1817 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
1820 do_exec(cmd); /* may or may not use the shell */
1824 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1825 sv_setiv(GvSV(tmpgv), (IV)getpid());
1827 hv_clear(pidstatus); /* we have no children */
1832 do_execfree(); /* free any memory malloced by child on vfork */
1834 if (p[that] < p[This]) {
1835 dup2(p[This], p[that]);
1839 sv = *av_fetch(fdpid,p[This],TRUE);
1840 (void)SvUPGRADE(sv,SVt_IV);
1843 return PerlIO_fdopen(p[This], mode);
1846 #if defined(atarist) || defined(DJGPP)
1853 /* Needs work for PerlIO ! */
1854 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1855 return popen(PerlIO_exportFILE(cmd, 0), mode);
1859 #endif /* !DOSISH */
1866 struct stat tmpstatbuf;
1868 PerlIO_printf(PerlIO_stderr(),"%s", s);
1869 for (fd = 0; fd < 32; fd++) {
1870 if (Fstat(fd,&tmpstatbuf) >= 0)
1871 PerlIO_printf(PerlIO_stderr()," %d",fd);
1873 PerlIO_printf(PerlIO_stderr(),"\n");
1883 #if defined(HAS_FCNTL) && defined(F_DUPFD)
1887 return fcntl(oldfd, F_DUPFD, newfd);
1889 #define DUP2_MAX_FDS 256
1890 int fdtmp[DUP2_MAX_FDS];
1897 /* good enough for low fd's... */
1898 while ((fd = dup(oldfd)) != newfd && fd >= 0) {
1899 if (fdx >= DUP2_MAX_FDS) {
1907 close(fdtmp[--fdx]);
1914 #ifdef HAS_SIGACTION
1917 rsignal(int signo, Sighandler_t handler)
1919 struct sigaction act, oact;
1921 act.sa_handler = handler;
1922 sigemptyset(&act.sa_mask);
1925 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1927 if (sigaction(signo, &act, &oact) == -1)
1930 return oact.sa_handler;
1934 rsignal_state(int signo)
1936 struct sigaction oact;
1938 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1941 return oact.sa_handler;
1945 rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
1947 struct sigaction act;
1949 act.sa_handler = handler;
1950 sigemptyset(&act.sa_mask);
1953 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1955 return sigaction(signo, &act, save);
1959 rsignal_restore(int signo, Sigsave_t *save)
1961 return sigaction(signo, save, (struct sigaction *)NULL);
1964 #else /* !HAS_SIGACTION */
1967 rsignal(int signo, Sighandler_t handler)
1969 return signal(signo, handler);
1972 static int sig_trapped;
1982 rsignal_state(int signo)
1984 Sighandler_t oldsig;
1987 oldsig = signal(signo, sig_trap);
1988 signal(signo, oldsig);
1990 kill(getpid(), signo);
1995 rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
1997 *save = signal(signo, handler);
1998 return (*save == SIG_ERR) ? -1 : 0;
2002 rsignal_restore(int signo, Sigsave_t *save)
2004 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
2007 #endif /* !HAS_SIGACTION */
2009 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2010 #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
2012 my_pclose(FILE *ptr)
2014 Sigsave_t hstat, istat, qstat;
2021 int saved_vaxc_errno;
2024 int saved_win32_errno;
2027 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
2028 pid = (int)SvIVX(*svp);
2032 if (pid == -1) { /* Opened by popen. */
2033 return my_syspclose(ptr);
2036 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2037 saved_errno = errno;
2039 saved_vaxc_errno = vaxc$errno;
2042 saved_win32_errno = GetLastError();
2046 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2048 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2049 rsignal_save(SIGINT, SIG_IGN, &istat);
2050 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
2052 pid = wait4pid(pid, &status, 0);
2053 } while (pid == -1 && errno == EINTR);
2054 rsignal_restore(SIGHUP, &hstat);
2055 rsignal_restore(SIGINT, &istat);
2056 rsignal_restore(SIGQUIT, &qstat);
2058 SETERRNO(saved_errno, saved_vaxc_errno);
2061 return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status));
2063 #endif /* !DOSISH */
2065 #if !defined(DOSISH) || defined(OS2) || defined(WIN32)
2067 wait4pid(int pid, int *statusp, int flags)
2071 char spid[TYPE_CHARS(int)];
2076 sprintf(spid, "%d", pid);
2077 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2078 if (svp && *svp != &sv_undef) {
2079 *statusp = SvIVX(*svp);
2080 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2087 hv_iterinit(pidstatus);
2088 if (entry = hv_iternext(pidstatus)) {
2089 pid = atoi(hv_iterkey(entry,(I32*)statusp));
2090 sv = hv_iterval(pidstatus,entry);
2091 *statusp = SvIVX(sv);
2092 sprintf(spid, "%d", pid);
2093 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
2098 # ifdef HAS_WAITPID_RUNTIME
2099 if (!HAS_WAITPID_RUNTIME)
2102 return waitpid(pid,statusp,flags);
2104 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
2105 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
2107 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2112 croak("Can't do waitpid with flags");
2114 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
2115 pidgone(result,*statusp);
2123 #endif /* !DOSISH || OS2 || WIN32 */
2127 pidgone(int pid, int status)
2130 char spid[TYPE_CHARS(int)];
2132 sprintf(spid, "%d", pid);
2133 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
2134 (void)SvUPGRADE(sv,SVt_IV);
2139 #if defined(atarist) || defined(OS2) || defined(DJGPP)
2142 int /* Cannot prototype with I32
2151 /* Needs work for PerlIO ! */
2152 FILE *f = PerlIO_findFILE(ptr);
2153 I32 result = pclose(f);
2154 PerlIO_releaseFILE(ptr,f);
2160 repeatcpy(register char *to, register char *from, I32 len, register I32 count)
2163 register char *frombase = from;
2171 while (count-- > 0) {
2172 for (todo = len; todo > 0; todo--) {
2179 #ifndef CASTNEGFLOAT
2187 # define BIGDOUBLE 2147483648.0
2189 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2192 return (unsigned long)f;
2194 return (unsigned long)along;
2201 /* Unfortunately, on some systems the cast_uv() function doesn't
2202 work with the system-supplied definition of ULONG_MAX. The
2203 comparison (f >= ULONG_MAX) always comes out true. It must be a
2204 problem with the compiler constant folding.
2206 In any case, this workaround should be fine on any two's complement
2207 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2209 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2212 /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2214 -- Kenneth Albanowski <kjahds@kjahds.com>
2218 # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
2226 return (I32) I32_MAX;
2228 return (I32) I32_MIN;
2248 return (UV) MY_UV_MAX;
2260 char *fa = strrchr(a,'/');
2261 char *fb = strrchr(b,'/');
2262 struct stat tmpstatbuf1;
2263 struct stat tmpstatbuf2;
2264 SV *tmpsv = sv_newmortal();
2277 sv_setpv(tmpsv, ".");
2279 sv_setpvn(tmpsv, a, fa - a);
2280 if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
2283 sv_setpv(tmpsv, ".");
2285 sv_setpvn(tmpsv, b, fb - b);
2286 if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
2288 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2289 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2291 #endif /* !HAS_RENAME */
2294 scan_oct(char *start, I32 len, I32 *retlen)
2296 register char *s = start;
2297 register UV retval = 0;
2298 bool overflowed = FALSE;
2300 while (len && *s >= '0' && *s <= '7') {
2301 register UV n = retval << 3;
2302 if (!overflowed && (n >> 3) != retval) {
2303 warn("Integer overflow in octal number");
2306 retval = n | (*s++ - '0');
2309 if (dowarn && len && (*s == '8' || *s == '9'))
2310 warn("Illegal octal digit ignored");
2311 *retlen = s - start;
2316 scan_hex(char *start, I32 len, I32 *retlen)
2318 register char *s = start;
2319 register UV retval = 0;
2320 bool overflowed = FALSE;
2323 while (len-- && *s && (tmp = strchr((char *) hexdigit, *s))) {
2324 register UV n = retval << 4;
2325 if (!overflowed && (n >> 4) != retval) {
2326 warn("Integer overflow in hex number");
2329 retval = n | ((tmp - hexdigit) & 15);
2332 *retlen = s - start;
2338 /* Very simplistic scheduler for now */
2342 thr = thr->i.next_run;
2353 perl_cond_signal(cp)
2357 perl_cond cond = *cp;
2362 /* Insert t in the runnable queue just ahead of us */
2363 t->i.next_run = thr->i.next_run;
2364 thr->i.next_run->i.prev_run = t;
2365 t->i.prev_run = thr;
2366 thr->i.next_run = t;
2367 thr->i.wait_queue = 0;
2368 /* Remove from the wait queue */
2374 perl_cond_broadcast(cp)
2378 perl_cond cond, cond_next;
2380 for (cond = *cp; cond; cond = cond_next) {
2382 /* Insert t in the runnable queue just ahead of us */
2383 t->i.next_run = thr->i.next_run;
2384 thr->i.next_run->i.prev_run = t;
2385 t->i.prev_run = thr;
2386 thr->i.next_run = t;
2387 thr->i.wait_queue = 0;
2388 /* Remove from the wait queue */
2389 cond_next = cond->next;
2401 if (thr->i.next_run == thr)
2402 croak("panic: perl_cond_wait called by last runnable thread");
2404 New(666, cond, 1, struct perl_wait_queue);
2408 thr->i.wait_queue = cond;
2409 /* Remove ourselves from runnable queue */
2410 thr->i.next_run->i.prev_run = thr->i.prev_run;
2411 thr->i.prev_run->i.next_run = thr->i.next_run;
2413 #endif /* FAKE_THREADS */
2415 #ifdef OLD_PTHREADS_API
2416 struct perl_thread *
2421 if (pthread_getspecific(thr_key, &t))
2422 croak("panic: pthread_getspecific");
2423 return (struct perl_thread *) t;
2425 #endif /* OLD_PTHREADS_API */
2428 condpair_magic(SV *sv)
2432 SvUPGRADE(sv, SVt_PVMG);
2433 mg = mg_find(sv, 'm');
2437 New(53, cp, 1, condpair_t);
2438 MUTEX_INIT(&cp->mutex);
2439 COND_INIT(&cp->owner_cond);
2440 COND_INIT(&cp->cond);
2443 mg = mg_find(sv, 'm');
2445 /* someone else beat us to initialising it */
2447 MUTEX_DESTROY(&cp->mutex);
2448 COND_DESTROY(&cp->owner_cond);
2449 COND_DESTROY(&cp->cond);
2453 sv_magic(sv, Nullsv, 'm', 0, 0);
2455 mg->mg_ptr = (char *)cp;
2456 mg->mg_len = sizeof(cp);
2458 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
2459 "%p: condpair_magic %p\n", thr, sv));)
2466 * Make a new perl thread structure using t as a prototype. Some of the
2467 * fields for the new thread are copied from the prototype thread, t,
2468 * so t should not be running in perl at the time this function is
2469 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
2470 * thread calling new_struct_thread) clearly satisfies this constraint.
2472 struct perl_thread *
2473 new_struct_thread(struct perl_thread *t)
2475 struct perl_thread *thr;
2480 sv = newSVpv("", 0);
2481 SvGROW(sv, sizeof(struct perl_thread) + 1);
2482 SvCUR_set(sv, sizeof(struct perl_thread));
2483 thr = (Thread) SvPVX(sv);
2485 memset(thr, 0xab, sizeof(struct perl_thread));
2497 curcop = &compiling;
2498 thr->cvcache = newHV();
2499 thr->threadsv = newAV();
2500 thr->specific = newAV();
2501 thr->errsv = newSVpv("", 0);
2502 thr->errhv = newHV();
2503 thr->flags = THRf_R_JOINABLE;
2504 MUTEX_INIT(&thr->mutex);
2506 curcop = t->Tcurcop; /* XXX As good a guess as any? */
2507 defstash = t->Tdefstash; /* XXX maybe these should */
2508 curstash = t->Tcurstash; /* always be set to main? */
2511 /* top_env needs to be non-zero. It points to an area
2512 in which longjmp() stuff is stored, as C callstack
2513 info there at least is thread specific this has to
2514 be per-thread. Otherwise a 'die' in a thread gives
2515 that thread the C stack of last thread to do an eval {}!
2516 See comments in scope.h
2517 Initialize top entry (as in perl.c for main thread)
2519 start_env.je_prev = NULL;
2520 start_env.je_ret = -1;
2521 start_env.je_mustcatch = TRUE;
2522 top_env = &start_env;
2527 tainted = t->Ttainted;
2528 curpm = t->Tcurpm; /* XXX No PMOP ref count */
2529 nrs = newSVsv(t->Tnrs);
2530 rs = newSVsv(t->Trs);
2531 last_in_gv = (GV*)SvREFCNT_inc(t->Tlast_in_gv);
2532 ofslen = t->Tofslen;
2533 ofs = savepvn(t->Tofs, ofslen);
2534 defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
2535 chopset = t->Tchopset;
2536 formtarget = newSVsv(t->Tformtarget);
2537 bodytarget = newSVsv(t->Tbodytarget);
2538 toptarget = newSVsv(t->Ttoptarget);
2540 /* Initialise all per-thread SVs that the template thread used */
2541 svp = AvARRAY(t->threadsv);
2542 for (i = 0; i <= AvFILL(t->threadsv); i++, svp++) {
2543 if (*svp && *svp != &sv_undef) {
2544 SV *sv = newSVsv(*svp);
2545 av_store(thr->threadsv, i, sv);
2546 sv_magic(sv, 0, 0, &threadsv_names[i], 1);
2547 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
2548 "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr));
2551 thr->threadsvp = AvARRAY(thr->threadsv);
2553 MUTEX_LOCK(&threads_mutex);
2555 thr->tid = ++threadnum;
2556 thr->next = t->next;
2559 thr->next->prev = thr;
2560 MUTEX_UNLOCK(&threads_mutex);
2562 #ifdef HAVE_THREAD_INTERN
2563 init_thread_intern(thr);
2564 #endif /* HAVE_THREAD_INTERN */
2567 #endif /* USE_THREADS */
2571 * This hack is to force load of "huge" support from libm.a
2572 * So it is in perl for (say) POSIX to use.
2573 * Needed for SunOS with Sun's 'acc' for example.
2582 #ifdef PERL_GLOBAL_STRUCT