3 * Copyright (c) 1991-2000, 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 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
12 * come here, and I don't want to see no more magic,' he said, and fell silent."
19 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
24 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
30 static void restore_magic(pTHXo_ void *p);
31 static void unwind_handler_stack(pTHXo_ void *p);
34 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
42 /* MGS is typedef'ed to struct magic_state in perl.h */
45 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
49 assert(SvMAGICAL(sv));
51 SAVEDESTRUCTOR_X(restore_magic, (void*)mgs_ix);
53 mgs = SSPTR(mgs_ix, MGS*);
55 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
56 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
60 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
64 =for apidoc mg_magical
66 Turns on the magical status of an SV. See C<sv_magic>.
72 Perl_mg_magical(pTHX_ SV *sv)
75 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
76 MGVTBL* vtbl = mg->mg_virtual;
78 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
82 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
91 Do magic after a value is retrieved from the SV. See C<sv_magic>.
97 Perl_mg_get(pTHX_ SV *sv)
105 mgs_ix = SSNEW(sizeof(MGS));
106 save_magic(mgs_ix, sv);
109 while ((mg = *mgp) != 0) {
110 MGVTBL* vtbl = mg->mg_virtual;
111 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
112 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
113 /* Ignore this magic if it's been deleted */
114 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
115 (mg->mg_flags & MGf_GSKIP))
116 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
118 /* Advance to next magic (complicated by possible deletion) */
119 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
120 mgp = &mg->mg_moremagic;
124 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
127 restore_magic(aTHXo_ (void*)mgs_ix);
134 Do magic after a value is assigned to the SV. See C<sv_magic>.
140 Perl_mg_set(pTHX_ SV *sv)
147 mgs_ix = SSNEW(sizeof(MGS));
148 save_magic(mgs_ix, sv);
150 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
151 MGVTBL* vtbl = mg->mg_virtual;
152 nextmg = mg->mg_moremagic; /* it may delete itself */
153 if (mg->mg_flags & MGf_GSKIP) {
154 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
155 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
157 if (vtbl && vtbl->svt_set)
158 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
161 restore_magic(aTHXo_ (void*)mgs_ix);
166 =for apidoc mg_length
168 Report on the SV's length. See C<sv_magic>.
174 Perl_mg_length(pTHX_ SV *sv)
180 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
181 MGVTBL* vtbl = mg->mg_virtual;
182 if (vtbl && vtbl->svt_len) {
185 mgs_ix = SSNEW(sizeof(MGS));
186 save_magic(mgs_ix, sv);
187 /* omit MGf_GSKIP -- not changed here */
188 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
189 restore_magic(aTHXo_ (void*)mgs_ix);
194 junk = SvPV(sv, len);
199 Perl_mg_size(pTHX_ SV *sv)
204 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
205 MGVTBL* vtbl = mg->mg_virtual;
206 if (vtbl && vtbl->svt_len) {
209 mgs_ix = SSNEW(sizeof(MGS));
210 save_magic(mgs_ix, sv);
211 /* omit MGf_GSKIP -- not changed here */
212 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
213 restore_magic(aTHXo_ (void*)mgs_ix);
220 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
225 Perl_croak(aTHX_ "Size magic not implemented");
234 Clear something magical that the SV represents. See C<sv_magic>.
240 Perl_mg_clear(pTHX_ SV *sv)
245 mgs_ix = SSNEW(sizeof(MGS));
246 save_magic(mgs_ix, sv);
248 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
249 MGVTBL* vtbl = mg->mg_virtual;
250 /* omit GSKIP -- never set here */
252 if (vtbl && vtbl->svt_clear)
253 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
256 restore_magic(aTHXo_ (void*)mgs_ix);
263 Finds the magic pointer for type matching the SV. See C<sv_magic>.
269 Perl_mg_find(pTHX_ SV *sv, int type)
272 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
273 if (mg->mg_type == type)
282 Copies the magic from one SV to another. See C<sv_magic>.
288 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
292 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
293 if (isUPPER(mg->mg_type)) {
295 mg->mg_type == 'P' ? SvTIED_obj(sv, mg) : mg->mg_obj,
296 toLOWER(mg->mg_type), key, klen);
306 Free any magic storage used by the SV. See C<sv_magic>.
312 Perl_mg_free(pTHX_ SV *sv)
316 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
317 MGVTBL* vtbl = mg->mg_virtual;
318 moremagic = mg->mg_moremagic;
319 if (vtbl && vtbl->svt_free)
320 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
321 if (mg->mg_ptr && mg->mg_type != 'g')
323 Safefree(mg->mg_ptr);
324 else if (mg->mg_len == HEf_SVKEY)
325 SvREFCNT_dec((SV*)mg->mg_ptr);
326 if (mg->mg_flags & MGf_REFCOUNTED)
327 SvREFCNT_dec(mg->mg_obj);
334 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
339 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
344 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
345 if (mg->mg_obj) /* @+ */
348 return rx->lastparen;
355 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
364 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
368 if (paren <= rx->nparens &&
369 (s = rx->startp[paren]) != -1 &&
370 (t = rx->endp[paren]) != -1)
372 if (mg->mg_obj) /* @+ */
383 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
391 switch (*mg->mg_ptr) {
392 case '1': case '2': case '3': case '4':
393 case '5': case '6': case '7': case '8': case '9': case '&':
394 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
396 paren = atoi(mg->mg_ptr);
398 if (paren <= rx->nparens &&
399 (s1 = rx->startp[paren]) != -1 &&
400 (t1 = rx->endp[paren]) != -1)
404 if (i > 0 && (PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE) {
405 char *s = rx->subbeg + s1;
406 char *send = rx->subbeg + t1;
419 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
420 paren = rx->lastparen;
426 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
427 if (rx->startp[0] != -1) {
438 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
439 if (rx->endp[0] != -1) {
440 i = rx->sublen - rx->endp[0];
450 return (STRLEN)PL_ofslen;
452 return (STRLEN)PL_orslen;
455 if (!SvPOK(sv) && SvNIOK(sv)) {
465 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
473 switch (*mg->mg_ptr) {
474 case '\001': /* ^A */
475 sv_setsv(sv, PL_bodytarget);
477 case '\003': /* ^C */
478 sv_setiv(sv, (IV)PL_minus_c);
481 case '\004': /* ^D */
482 sv_setiv(sv, (IV)(PL_debug & 32767));
483 #if defined(YYDEBUG) && defined(DEBUGGING)
484 PL_yydebug = (PL_debug & 1);
487 case '\005': /* ^E */
488 #ifdef MACOS_TRADITIONAL
492 sv_setnv(sv,(double)gMacPerl_OSErr);
493 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
498 # include <descrip.h>
499 # include <starlet.h>
501 $DESCRIPTOR(msgdsc,msg);
502 sv_setnv(sv,(NV) vaxc$errno);
503 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
504 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
510 if (!(_emx_env & 0x200)) { /* Under DOS */
511 sv_setnv(sv, (NV)errno);
512 sv_setpv(sv, errno ? Strerror(errno) : "");
514 if (errno != errno_isOS2) {
515 int tmp = _syserrno();
516 if (tmp) /* 2nd call to _syserrno() makes it 0 */
519 sv_setnv(sv, (NV)Perl_rc);
520 sv_setpv(sv, os2error(Perl_rc));
525 DWORD dwErr = GetLastError();
526 sv_setnv(sv, (NV)dwErr);
529 PerlProc_GetOSError(sv, dwErr);
536 sv_setnv(sv, (NV)errno);
537 sv_setpv(sv, errno ? Strerror(errno) : "");
542 SvNOK_on(sv); /* what a wonderful hack! */
544 case '\006': /* ^F */
545 sv_setiv(sv, (IV)PL_maxsysfd);
547 case '\010': /* ^H */
548 sv_setiv(sv, (IV)PL_hints);
550 case '\011': /* ^I */ /* NOT \t in EBCDIC */
552 sv_setpv(sv, PL_inplace);
554 sv_setsv(sv, &PL_sv_undef);
556 case '\017': /* ^O */
557 sv_setpv(sv, PL_osname);
559 case '\020': /* ^P */
560 sv_setiv(sv, (IV)PL_perldb);
562 case '\023': /* ^S */
565 if (PL_lex_state != LEX_NOTPARSING)
573 case '\024': /* ^T */
575 sv_setnv(sv, PL_basetime);
577 sv_setiv(sv, (IV)PL_basetime);
580 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
581 if (*(mg->mg_ptr+1) == '\0')
582 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
583 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
584 if (PL_compiling.cop_warnings == pWARN_NONE ||
585 PL_compiling.cop_warnings == pWARN_STD)
587 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
589 else if (PL_compiling.cop_warnings == pWARN_ALL) {
590 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
593 sv_setsv(sv, PL_compiling.cop_warnings);
597 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
598 sv_setiv(sv, (IV)PL_widesyscalls);
600 case '1': case '2': case '3': case '4':
601 case '5': case '6': case '7': case '8': case '9': case '&':
602 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
606 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
607 * XXX Does the new way break anything?
609 paren = atoi(mg->mg_ptr);
611 if (paren <= rx->nparens &&
612 (s1 = rx->startp[paren]) != -1 &&
613 (t1 = rx->endp[paren]) != -1)
624 was_tainted = PL_tainted;
628 if ((PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE)
633 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
638 sv_setsv(sv,&PL_sv_undef);
641 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
642 paren = rx->lastparen;
646 sv_setsv(sv,&PL_sv_undef);
649 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
650 if ((s = rx->subbeg) && rx->startp[0] != -1) {
655 sv_setsv(sv,&PL_sv_undef);
658 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
659 if (rx->subbeg && rx->endp[0] != -1) {
660 s = rx->subbeg + rx->endp[0];
661 i = rx->sublen - rx->endp[0];
665 sv_setsv(sv,&PL_sv_undef);
669 if (GvIO(PL_last_in_gv)) {
670 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
676 sv_setiv(sv, (IV)STATUS_CURRENT);
677 #ifdef COMPLEX_STATUS
678 LvTARGOFF(sv) = PL_statusvalue;
679 LvTARGLEN(sv) = PL_statusvalue_vms;
684 s = IoTOP_NAME(GvIOp(PL_defoutgv));
688 sv_setpv(sv,GvENAME(PL_defoutgv));
693 s = IoFMT_NAME(GvIOp(PL_defoutgv));
695 s = GvENAME(PL_defoutgv);
700 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
703 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
706 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
714 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
717 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
720 sv_setpvn(sv,PL_ofs,PL_ofslen);
723 sv_setpvn(sv,PL_ors,PL_orslen);
726 sv_setpv(sv,PL_ofmt);
730 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
731 sv_setpv(sv, errno ? Strerror(errno) : "");
734 int saveerrno = errno;
735 sv_setnv(sv, (NV)errno);
737 if (errno == errno_isOS2 || errno == errno_isOS2_set)
738 sv_setpv(sv, os2error(Perl_rc));
741 sv_setpv(sv, errno ? Strerror(errno) : "");
745 SvNOK_on(sv); /* what a wonderful hack! */
748 sv_setiv(sv, (IV)PL_uid);
751 sv_setiv(sv, (IV)PL_euid);
754 sv_setiv(sv, (IV)PL_gid);
756 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
760 sv_setiv(sv, (IV)PL_egid);
762 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
767 Groups_t gary[NGROUPS];
768 i = getgroups(NGROUPS,gary);
770 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
773 (void)SvIOK_on(sv); /* what a wonderful hack! */
777 #ifndef MACOS_TRADITIONAL
783 sv_setsv(sv, thr->errsv);
785 #endif /* USE_THREADS */
791 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
793 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
795 if (uf && uf->uf_val)
796 (*uf->uf_val)(uf->uf_index, sv);
801 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
812 #ifdef DYNAMIC_ENV_FETCH
813 /* We just undefd an environment var. Is a replacement */
814 /* waiting in the wings? */
817 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
818 s = SvPV(*valp, len);
822 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
823 /* And you'll never guess what the dog had */
824 /* in its mouth... */
826 MgTAINTEDDIR_off(mg);
828 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
829 char pathbuf[256], eltbuf[256], *cp, *elt = s;
833 do { /* DCL$PATH may be a search list */
834 while (1) { /* as may dev portion of any element */
835 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
836 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
837 cando_by_name(S_IWUSR,0,elt) ) {
842 if ((cp = strchr(elt, ':')) != Nullch)
844 if (my_trnlnm(elt, eltbuf, j++))
850 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
853 if (s && klen == 4 && strEQ(ptr,"PATH")) {
854 char *strend = s + len;
859 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
862 if (i >= sizeof tmpbuf /* too long -- assume the worst */
864 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
871 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
877 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
880 my_setenv(MgPV(mg,n_a),Nullch);
885 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
888 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
894 magic_clear_all_env(sv,mg);
895 hv_iterinit((HV*)sv);
896 while ((entry = hv_iternext((HV*)sv))) {
898 my_setenv(hv_iterkey(entry, &keylen),
899 SvPV(hv_iterval((HV*)sv, entry), n_a));
907 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
910 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
912 # ifdef PERL_IMPLICIT_SYS
916 char *envv = GetEnvironmentStrings();
920 char *end = strchr(cur,'=');
921 if (end && end != cur) {
923 my_setenv(cur,Nullch);
925 cur = end + strlen(end+1)+2;
927 else if ((len = strlen(cur)))
930 FreeEnvironmentStrings(envv);
934 for (i = 0; environ[i]; i++)
935 safesysfree(environ[i]);
937 # ifndef PERL_USE_SAFE_PUTENV
940 if (environ == PL_origenviron)
941 environ = (char**)safesysmalloc(sizeof(char*));
943 for (i = 0; environ[i]; i++)
944 safesysfree(environ[i]);
945 # endif /* PERL_USE_SAFE_PUTENV */
946 # endif /* __CYGWIN__ */
951 # endif /* PERL_IMPLICIT_SYS */
958 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
962 /* Are we fetching a signal entry? */
963 i = whichsig(MgPV(mg,n_a));
966 sv_setsv(sv,PL_psig_ptr[i]);
968 Sighandler_t sigstate = rsignal_state(i);
970 /* cache state so we don't fetch it again */
971 if(sigstate == SIG_IGN)
972 sv_setpv(sv,"IGNORE");
974 sv_setsv(sv,&PL_sv_undef);
975 PL_psig_ptr[i] = SvREFCNT_inc(sv);
982 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
986 /* Are we clearing a signal entry? */
987 i = whichsig(MgPV(mg,n_a));
990 SvREFCNT_dec(PL_psig_ptr[i]);
993 if(PL_psig_name[i]) {
994 SvREFCNT_dec(PL_psig_name[i]);
1002 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1012 if (strEQ(s,"__DIE__"))
1014 else if (strEQ(s,"__WARN__"))
1017 Perl_croak(aTHX_ "No such hook: %s", s);
1025 i = whichsig(s); /* ...no, a brick */
1027 if (ckWARN(WARN_SIGNAL))
1028 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1031 SvREFCNT_dec(PL_psig_name[i]);
1032 SvREFCNT_dec(PL_psig_ptr[i]);
1033 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1034 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1035 PL_psig_name[i] = newSVpvn(s, len);
1036 SvREADONLY_on(PL_psig_name[i]);
1038 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1040 (void)rsignal(i, PL_sighandlerp);
1042 *svp = SvREFCNT_inc(sv);
1045 s = SvPV_force(sv,len);
1046 if (strEQ(s,"IGNORE")) {
1048 (void)rsignal(i, SIG_IGN);
1052 else if (strEQ(s,"DEFAULT") || !*s) {
1054 (void)rsignal(i, SIG_DFL);
1060 * We should warn if HINT_STRICT_REFS, but without
1061 * access to a known hint bit in a known OP, we can't
1062 * tell whether HINT_STRICT_REFS is in force or not.
1064 if (!strchr(s,':') && !strchr(s,'\''))
1065 sv_insert(sv, 0, 0, "main::", 6);
1067 (void)rsignal(i, PL_sighandlerp);
1069 *svp = SvREFCNT_inc(sv);
1073 #endif /* !PERL_MICRO */
1076 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1078 PL_sub_generation++;
1083 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1085 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1086 PL_amagic_generation++;
1092 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1094 HV *hv = (HV*)LvTARG(sv);
1099 (void) hv_iterinit(hv);
1100 if (! SvTIED_mg((SV*)hv, 'P'))
1104 while ((entry = hv_iternext(hv))) {
1110 sv_setiv(sv, (IV)i);
1115 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1118 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1123 /* caller is responsible for stack switching/cleanup */
1125 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1131 PUSHs(SvTIED_obj(sv, mg));
1134 if (mg->mg_len >= 0)
1135 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1136 else if (mg->mg_len == HEf_SVKEY)
1137 PUSHs((SV*)mg->mg_ptr);
1139 else if (mg->mg_type == 'p') {
1140 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1148 return call_method(meth, flags);
1152 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1158 PUSHSTACKi(PERLSI_MAGIC);
1160 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1161 sv_setsv(sv, *PL_stack_sp--);
1171 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1173 magic_methpack(sv,mg,"FETCH");
1175 mg->mg_flags |= MGf_GSKIP;
1180 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1184 PUSHSTACKi(PERLSI_MAGIC);
1185 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1192 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1194 return magic_methpack(sv,mg,"DELETE");
1199 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1206 PUSHSTACKi(PERLSI_MAGIC);
1207 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1208 sv = *PL_stack_sp--;
1209 retval = (U32) SvIV(sv)-1;
1218 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1223 PUSHSTACKi(PERLSI_MAGIC);
1225 XPUSHs(SvTIED_obj(sv, mg));
1227 call_method("CLEAR", G_SCALAR|G_DISCARD);
1234 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1237 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1241 PUSHSTACKi(PERLSI_MAGIC);
1244 PUSHs(SvTIED_obj(sv, mg));
1249 if (call_method(meth, G_SCALAR))
1250 sv_setsv(key, *PL_stack_sp--);
1259 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1261 return magic_methpack(sv,mg,"EXISTS");
1265 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1276 svp = av_fetch(GvAV(gv),
1277 atoi(MgPV(mg,n_a)), FALSE);
1278 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1280 else if (ckWARN_d(WARN_INTERNAL))
1281 Perl_warner(aTHX_ WARN_INTERNAL, "Can't break at that line\n");
1286 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1289 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1294 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1297 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1302 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1304 SV* lsv = LvTARG(sv);
1306 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1307 mg = mg_find(lsv, 'g');
1308 if (mg && mg->mg_len >= 0) {
1312 sv_pos_b2u(lsv, &i);
1313 sv_setiv(sv, i + PL_curcop->cop_arybase);
1322 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1324 SV* lsv = LvTARG(sv);
1332 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1333 mg = mg_find(lsv, 'g');
1337 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1338 mg = mg_find(lsv, 'g');
1340 else if (!SvOK(sv)) {
1344 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1346 pos = SvIV(sv) - PL_curcop->cop_arybase;
1349 ulen = sv_len_utf8(lsv);
1364 sv_pos_u2b(lsv, &p, 0);
1369 mg->mg_flags &= ~MGf_MINMATCH;
1375 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1377 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1379 gv_efullname3(sv,((GV*)sv), "*");
1383 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1388 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1397 if (*s == '*' && s[1])
1399 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1404 GvGP(sv) = gp_ref(GvGP(gv));
1409 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1412 SV *lsv = LvTARG(sv);
1413 char *tmps = SvPV(lsv,len);
1414 I32 offs = LvTARGOFF(sv);
1415 I32 rem = LvTARGLEN(sv);
1419 if (rem + offs > len)
1421 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1426 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1429 char *tmps = SvPV(sv,len);
1430 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1435 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1438 TAINT_IF((mg->mg_len & 1) ||
1439 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1444 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1447 if (PL_localizing) {
1448 if (PL_localizing == 1)
1453 else if (PL_tainted)
1461 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1463 SV *lsv = LvTARG(sv);
1470 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1475 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1477 do_vecset(sv); /* XXX slurp this routine */
1482 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1485 if (LvTARGLEN(sv)) {
1487 SV *ahv = LvTARG(sv);
1488 if (SvTYPE(ahv) == SVt_PVHV) {
1489 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1494 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1500 AV* av = (AV*)LvTARG(sv);
1501 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1502 targ = AvARRAY(av)[LvTARGOFF(sv)];
1504 if (targ && targ != &PL_sv_undef) {
1505 dTHR; /* just for SvREFCNT_dec */
1506 /* somebody else defined it for us */
1507 SvREFCNT_dec(LvTARG(sv));
1508 LvTARG(sv) = SvREFCNT_inc(targ);
1510 SvREFCNT_dec(mg->mg_obj);
1511 mg->mg_obj = Nullsv;
1512 mg->mg_flags &= ~MGf_REFCOUNTED;
1517 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1522 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1527 sv_setsv(LvTARG(sv), sv);
1528 SvSETMAGIC(LvTARG(sv));
1534 Perl_vivify_defelem(pTHX_ SV *sv)
1536 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1540 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1543 SV *ahv = LvTARG(sv);
1545 if (SvTYPE(ahv) == SVt_PVHV) {
1546 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1551 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1555 if (!value || value == &PL_sv_undef)
1556 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1559 AV* av = (AV*)LvTARG(sv);
1560 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1561 LvTARG(sv) = Nullsv; /* array can't be extended */
1563 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1564 if (!svp || (value = *svp) == &PL_sv_undef)
1565 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1568 (void)SvREFCNT_inc(value);
1569 SvREFCNT_dec(LvTARG(sv));
1572 SvREFCNT_dec(mg->mg_obj);
1573 mg->mg_obj = Nullsv;
1574 mg->mg_flags &= ~MGf_REFCOUNTED;
1578 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1580 AV *av = (AV*)mg->mg_obj;
1581 SV **svp = AvARRAY(av);
1582 I32 i = AvFILLp(av);
1584 if (svp[i] && svp[i] != &PL_sv_undef) {
1585 if (!SvWEAKREF(svp[i]))
1586 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1587 /* XXX Should we check that it hasn't changed? */
1589 (void)SvOK_off(svp[i]);
1590 SvWEAKREF_off(svp[i]);
1591 svp[i] = &PL_sv_undef;
1599 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1607 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1609 sv_unmagic(sv, 'B');
1615 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1617 sv_unmagic(sv, 'f');
1623 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1625 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1627 if (uf && uf->uf_set)
1628 (*uf->uf_set)(uf->uf_index, sv);
1633 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1635 regexp *re = (regexp *)mg->mg_obj;
1640 #ifdef USE_LOCALE_COLLATE
1642 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1645 * RenE<eacute> Descartes said "I think not."
1646 * and vanished with a faint plop.
1649 Safefree(mg->mg_ptr);
1655 #endif /* USE_LOCALE_COLLATE */
1658 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1664 switch (*mg->mg_ptr) {
1665 case '\001': /* ^A */
1666 sv_setsv(PL_bodytarget, sv);
1668 case '\003': /* ^C */
1669 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1672 case '\004': /* ^D */
1673 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1674 DEBUG_x(dump_all());
1676 case '\005': /* ^E */
1677 #ifdef MACOS_TRADITIONAL
1678 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1681 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1684 SetLastError( SvIV(sv) );
1687 /* will anyone ever use this? */
1688 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1694 case '\006': /* ^F */
1695 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1697 case '\010': /* ^H */
1698 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1700 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1702 Safefree(PL_inplace);
1704 PL_inplace = savepv(SvPV(sv,len));
1706 PL_inplace = Nullch;
1708 case '\017': /* ^O */
1710 Safefree(PL_osname);
1712 PL_osname = savepv(SvPV(sv,len));
1716 case '\020': /* ^P */
1717 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1718 if (PL_perldb && !PL_DBsingle)
1721 case '\024': /* ^T */
1723 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1725 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1728 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1729 if (*(mg->mg_ptr+1) == '\0') {
1730 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1731 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1732 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1733 | (i ? G_WARN_ON : G_WARN_OFF) ;
1736 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1737 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1738 if (!SvPOK(sv) && PL_localizing) {
1739 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1740 PL_compiling.cop_warnings = pWARN_NONE;
1743 if (isWARN_on(sv, WARN_ALL) && !isWARNf_on(sv, WARN_ALL)) {
1744 PL_compiling.cop_warnings = pWARN_ALL;
1745 PL_dowarn |= G_WARN_ONCE ;
1749 int accumulate = 0 ;
1750 char * ptr = (char*)SvPV(sv, len) ;
1751 for (i = 0 ; i < len ; ++i)
1752 accumulate += ptr[i] ;
1754 PL_compiling.cop_warnings = pWARN_NONE;
1756 if (specialWARN(PL_compiling.cop_warnings))
1757 PL_compiling.cop_warnings = newSVsv(sv) ;
1759 sv_setsv(PL_compiling.cop_warnings, sv);
1760 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1761 PL_dowarn |= G_WARN_ONCE ;
1766 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1767 PL_widesyscalls = SvTRUE(sv);
1770 if (PL_localizing) {
1771 if (PL_localizing == 1)
1772 SAVESPTR(PL_last_in_gv);
1774 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1775 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1778 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1779 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1780 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1783 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1784 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1785 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1788 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1791 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1792 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1793 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1796 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1800 IO *io = GvIOp(PL_defoutgv);
1801 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1802 IoFLAGS(io) &= ~IOf_FLUSH;
1804 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1805 PerlIO *ofp = IoOFP(io);
1807 (void)PerlIO_flush(ofp);
1808 IoFLAGS(io) |= IOf_FLUSH;
1814 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1815 PL_multiline = (i != 0);
1818 SvREFCNT_dec(PL_nrs);
1819 PL_nrs = newSVsv(sv);
1820 SvREFCNT_dec(PL_rs);
1821 PL_rs = SvREFCNT_inc(PL_nrs);
1826 if (SvOK(sv) || SvGMAGICAL(sv)) {
1827 s = SvPV(sv,PL_orslen);
1828 PL_ors = savepvn(s,PL_orslen);
1838 PL_ofs = savepv(SvPV(sv, PL_ofslen));
1843 PL_ofmt = savepv(SvPV(sv,len));
1846 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1849 #ifdef COMPLEX_STATUS
1850 if (PL_localizing == 2) {
1851 PL_statusvalue = LvTARGOFF(sv);
1852 PL_statusvalue_vms = LvTARGLEN(sv);
1856 #ifdef VMSISH_STATUS
1858 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1861 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1864 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1865 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1868 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1869 if (PL_delaymagic) {
1870 PL_delaymagic |= DM_RUID;
1871 break; /* don't do magic till later */
1874 (void)setruid((Uid_t)PL_uid);
1877 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1879 #ifdef HAS_SETRESUID
1880 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1882 if (PL_uid == PL_euid) /* special case $< = $> */
1883 (void)PerlProc_setuid(PL_uid);
1885 PL_uid = PerlProc_getuid();
1886 Perl_croak(aTHX_ "setruid() not implemented");
1891 PL_uid = PerlProc_getuid();
1892 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1895 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1896 if (PL_delaymagic) {
1897 PL_delaymagic |= DM_EUID;
1898 break; /* don't do magic till later */
1901 (void)seteuid((Uid_t)PL_euid);
1904 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1906 #ifdef HAS_SETRESUID
1907 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1909 if (PL_euid == PL_uid) /* special case $> = $< */
1910 PerlProc_setuid(PL_euid);
1912 PL_euid = PerlProc_geteuid();
1913 Perl_croak(aTHX_ "seteuid() not implemented");
1918 PL_euid = PerlProc_geteuid();
1919 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1922 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1923 if (PL_delaymagic) {
1924 PL_delaymagic |= DM_RGID;
1925 break; /* don't do magic till later */
1928 (void)setrgid((Gid_t)PL_gid);
1931 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1933 #ifdef HAS_SETRESGID
1934 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1936 if (PL_gid == PL_egid) /* special case $( = $) */
1937 (void)PerlProc_setgid(PL_gid);
1939 PL_gid = PerlProc_getgid();
1940 Perl_croak(aTHX_ "setrgid() not implemented");
1945 PL_gid = PerlProc_getgid();
1946 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1949 #ifdef HAS_SETGROUPS
1951 char *p = SvPV(sv, len);
1952 Groups_t gary[NGROUPS];
1957 for (i = 0; i < NGROUPS; ++i) {
1958 while (*p && !isSPACE(*p))
1967 (void)setgroups(i, gary);
1969 #else /* HAS_SETGROUPS */
1970 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1971 #endif /* HAS_SETGROUPS */
1972 if (PL_delaymagic) {
1973 PL_delaymagic |= DM_EGID;
1974 break; /* don't do magic till later */
1977 (void)setegid((Gid_t)PL_egid);
1980 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
1982 #ifdef HAS_SETRESGID
1983 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
1985 if (PL_egid == PL_gid) /* special case $) = $( */
1986 (void)PerlProc_setgid(PL_egid);
1988 PL_egid = PerlProc_getegid();
1989 Perl_croak(aTHX_ "setegid() not implemented");
1994 PL_egid = PerlProc_getegid();
1995 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1998 PL_chopset = SvPV_force(sv,len);
2000 #ifndef MACOS_TRADITIONAL
2005 /* See if all the arguments are contiguous in memory */
2006 for (i = 1; i < PL_origargc; i++) {
2007 if (PL_origargv[i] == s + 1
2009 || PL_origargv[i] == s + 2
2014 s += strlen(s); /* this one is ok too */
2019 /* can grab env area too? */
2020 if (PL_origenviron && (PL_origenviron[0] == s + 1
2022 || (PL_origenviron[0] == s + 9 && (s += 8))
2025 my_setenv("NoNe SuCh", Nullch);
2026 /* force copy of environment */
2027 for (i = 0; PL_origenviron[i]; i++)
2028 if (PL_origenviron[i] == s + 1) {
2035 PL_origalen = s - PL_origargv[0];
2037 s = SvPV_force(sv,len);
2039 if (i >= PL_origalen) {
2041 /* don't allow system to limit $0 seen by script */
2042 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2043 Copy(s, PL_origargv[0], i, char);
2044 s = PL_origargv[0]+i;
2048 Copy(s, PL_origargv[0], i, char);
2049 s = PL_origargv[0]+i;
2051 while (++i < PL_origalen)
2053 s = PL_origargv[0]+i;
2054 for (i = 1; i < PL_origargc; i++)
2055 PL_origargv[i] = Nullch;
2061 sv_setsv(thr->errsv, sv);
2063 #endif /* USE_THREADS */
2070 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2073 DEBUG_S(PerlIO_printf(Perl_debug_log,
2074 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2075 PTR2UV(thr), PTR2UV(sv));)
2077 Perl_croak(aTHX_ "panic: magic_mutexfree");
2078 MUTEX_DESTROY(MgMUTEXP(mg));
2079 COND_DESTROY(MgCONDP(mg));
2082 #endif /* USE_THREADS */
2085 Perl_whichsig(pTHX_ char *sig)
2087 register char **sigv;
2089 for (sigv = PL_sig_name+1; *sigv; sigv++)
2090 if (strEQ(sig,*sigv))
2091 return PL_sig_num[sigv - PL_sig_name];
2093 if (strEQ(sig,"CHLD"))
2097 if (strEQ(sig,"CLD"))
2106 Perl_sighandler(int sig)
2108 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2109 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2116 SV *sv, *tSv = PL_Sv;
2120 I32 o_save_i = PL_savestack_ix;
2123 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2124 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2127 if (PL_savestack_ix + 15 <= PL_savestack_max)
2129 if (PL_markstack_ptr < PL_markstack_max - 2)
2131 if (PL_retstack_ix < PL_retstack_max - 2)
2133 if (PL_scopestack_ix < PL_scopestack_max - 3)
2136 if (!PL_psig_ptr[sig])
2137 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2140 /* Max number of items pushed there is 3*n or 4. We cannot fix
2141 infinity, so we fix 4 (in fact 5): */
2143 PL_savestack_ix += 5; /* Protect save in progress. */
2144 o_save_i = PL_savestack_ix;
2145 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2148 PL_markstack_ptr++; /* Protect mark. */
2151 PL_retstack[PL_retstack_ix] = NULL;
2154 PL_scopestack_ix += 1;
2155 /* sv_2cv is too complicated, try a simpler variant first: */
2156 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2157 || SvTYPE(cv) != SVt_PVCV)
2158 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2160 if (!cv || !CvROOT(cv)) {
2161 if (ckWARN(WARN_SIGNAL))
2162 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2163 PL_sig_name[sig], (gv ? GvENAME(gv)
2170 if(PL_psig_name[sig]) {
2171 sv = SvREFCNT_inc(PL_psig_name[sig]);
2175 sv = sv_newmortal();
2176 sv_setpv(sv,PL_sig_name[sig]);
2179 PUSHSTACKi(PERLSI_SIGNAL);
2184 call_sv((SV*)cv, G_DISCARD);
2189 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2195 PL_scopestack_ix -= 1;
2198 PL_op = myop; /* Apparently not needed... */
2200 PL_Sv = tSv; /* Restore global temporaries. */
2211 restore_magic(pTHXo_ void *p)
2214 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2215 SV* sv = mgs->mgs_sv;
2220 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2223 SvFLAGS(sv) |= mgs->mgs_flags;
2227 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2230 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2232 /* If we're still on top of the stack, pop us off. (That condition
2233 * will be satisfied if restore_magic was called explicitly, but *not*
2234 * if it's being called via leave_scope.)
2235 * The reason for doing this is that otherwise, things like sv_2cv()
2236 * may leave alloc gunk on the savestack, and some code
2237 * (e.g. sighandler) doesn't expect that...
2239 if (PL_savestack_ix == mgs->mgs_ss_ix)
2241 I32 popval = SSPOPINT;
2242 assert(popval == SAVEt_DESTRUCTOR_X);
2243 PL_savestack_ix -= 2;
2245 assert(popval == SAVEt_ALLOC);
2247 PL_savestack_ix -= popval;
2253 unwind_handler_stack(pTHXo_ void *p)
2256 U32 flags = *(U32*)p;
2259 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2260 /* cxstack_ix-- Not needed, die already unwound it. */
2262 SvREFCNT_dec(sig_sv);