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)
621 was_tainted = PL_tainted;
625 if ((PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE)
630 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
635 sv_setsv(sv,&PL_sv_undef);
638 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
639 paren = rx->lastparen;
643 sv_setsv(sv,&PL_sv_undef);
646 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
647 if ((s = rx->subbeg) && rx->startp[0] != -1) {
652 sv_setsv(sv,&PL_sv_undef);
655 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
656 if (rx->subbeg && rx->endp[0] != -1) {
657 s = rx->subbeg + rx->endp[0];
658 i = rx->sublen - rx->endp[0];
662 sv_setsv(sv,&PL_sv_undef);
666 if (GvIO(PL_last_in_gv)) {
667 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
673 sv_setiv(sv, (IV)STATUS_CURRENT);
674 #ifdef COMPLEX_STATUS
675 LvTARGOFF(sv) = PL_statusvalue;
676 LvTARGLEN(sv) = PL_statusvalue_vms;
681 s = IoTOP_NAME(GvIOp(PL_defoutgv));
685 sv_setpv(sv,GvENAME(PL_defoutgv));
690 s = IoFMT_NAME(GvIOp(PL_defoutgv));
692 s = GvENAME(PL_defoutgv);
697 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
700 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
703 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
711 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
714 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
717 sv_setpvn(sv,PL_ofs,PL_ofslen);
720 sv_setpvn(sv,PL_ors,PL_orslen);
723 sv_setpv(sv,PL_ofmt);
727 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
728 sv_setpv(sv, errno ? Strerror(errno) : "");
731 int saveerrno = errno;
732 sv_setnv(sv, (NV)errno);
734 if (errno == errno_isOS2 || errno == errno_isOS2_set)
735 sv_setpv(sv, os2error(Perl_rc));
738 sv_setpv(sv, errno ? Strerror(errno) : "");
742 SvNOK_on(sv); /* what a wonderful hack! */
745 sv_setiv(sv, (IV)PL_uid);
748 sv_setiv(sv, (IV)PL_euid);
751 sv_setiv(sv, (IV)PL_gid);
753 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
757 sv_setiv(sv, (IV)PL_egid);
759 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
764 Groups_t gary[NGROUPS];
765 i = getgroups(NGROUPS,gary);
767 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
770 (void)SvIOK_on(sv); /* what a wonderful hack! */
774 #ifndef MACOS_TRADITIONAL
780 sv_setsv(sv, thr->errsv);
782 #endif /* USE_THREADS */
788 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
790 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
792 if (uf && uf->uf_val)
793 (*uf->uf_val)(uf->uf_index, sv);
798 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
809 #ifdef DYNAMIC_ENV_FETCH
810 /* We just undefd an environment var. Is a replacement */
811 /* waiting in the wings? */
814 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
815 s = SvPV(*valp, len);
819 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
820 /* And you'll never guess what the dog had */
821 /* in its mouth... */
823 MgTAINTEDDIR_off(mg);
825 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
826 char pathbuf[256], eltbuf[256], *cp, *elt = s;
830 do { /* DCL$PATH may be a search list */
831 while (1) { /* as may dev portion of any element */
832 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
833 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
834 cando_by_name(S_IWUSR,0,elt) ) {
839 if ((cp = strchr(elt, ':')) != Nullch)
841 if (my_trnlnm(elt, eltbuf, j++))
847 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
850 if (s && klen == 4 && strEQ(ptr,"PATH")) {
851 char *strend = s + len;
856 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
859 if (i >= sizeof tmpbuf /* too long -- assume the worst */
861 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
868 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
874 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
877 my_setenv(MgPV(mg,n_a),Nullch);
882 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
885 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
891 magic_clear_all_env(sv,mg);
892 hv_iterinit((HV*)sv);
893 while ((entry = hv_iternext((HV*)sv))) {
895 my_setenv(hv_iterkey(entry, &keylen),
896 SvPV(hv_iterval((HV*)sv, entry), n_a));
904 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
907 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
909 # ifdef PERL_IMPLICIT_SYS
913 char *envv = GetEnvironmentStrings();
917 char *end = strchr(cur,'=');
918 if (end && end != cur) {
920 my_setenv(cur,Nullch);
922 cur = end + strlen(end+1)+2;
924 else if ((len = strlen(cur)))
927 FreeEnvironmentStrings(envv);
931 for (i = 0; environ[i]; i++)
932 safesysfree(environ[i]);
934 # ifndef PERL_USE_SAFE_PUTENV
937 if (environ == PL_origenviron)
938 environ = (char**)safesysmalloc(sizeof(char*));
940 for (i = 0; environ[i]; i++)
941 safesysfree(environ[i]);
942 # endif /* PERL_USE_SAFE_PUTENV */
943 # endif /* __CYGWIN__ */
948 # endif /* PERL_IMPLICIT_SYS */
954 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
958 /* Are we fetching a signal entry? */
959 i = whichsig(MgPV(mg,n_a));
962 sv_setsv(sv,PL_psig_ptr[i]);
964 Sighandler_t sigstate = rsignal_state(i);
966 /* cache state so we don't fetch it again */
967 if(sigstate == SIG_IGN)
968 sv_setpv(sv,"IGNORE");
970 sv_setsv(sv,&PL_sv_undef);
971 PL_psig_ptr[i] = SvREFCNT_inc(sv);
978 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
982 /* Are we clearing a signal entry? */
983 i = whichsig(MgPV(mg,n_a));
986 SvREFCNT_dec(PL_psig_ptr[i]);
989 if(PL_psig_name[i]) {
990 SvREFCNT_dec(PL_psig_name[i]);
998 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1008 if (strEQ(s,"__DIE__"))
1010 else if (strEQ(s,"__WARN__"))
1013 Perl_croak(aTHX_ "No such hook: %s", s);
1021 i = whichsig(s); /* ...no, a brick */
1023 if (ckWARN(WARN_SIGNAL))
1024 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1027 SvREFCNT_dec(PL_psig_name[i]);
1028 SvREFCNT_dec(PL_psig_ptr[i]);
1029 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1030 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1031 PL_psig_name[i] = newSVpvn(s, len);
1032 SvREADONLY_on(PL_psig_name[i]);
1034 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1036 (void)rsignal(i, PL_sighandlerp);
1038 *svp = SvREFCNT_inc(sv);
1041 s = SvPV_force(sv,len);
1042 if (strEQ(s,"IGNORE")) {
1044 (void)rsignal(i, SIG_IGN);
1048 else if (strEQ(s,"DEFAULT") || !*s) {
1050 (void)rsignal(i, SIG_DFL);
1056 * We should warn if HINT_STRICT_REFS, but without
1057 * access to a known hint bit in a known OP, we can't
1058 * tell whether HINT_STRICT_REFS is in force or not.
1060 if (!strchr(s,':') && !strchr(s,'\''))
1061 sv_insert(sv, 0, 0, "main::", 6);
1063 (void)rsignal(i, PL_sighandlerp);
1065 *svp = SvREFCNT_inc(sv);
1071 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1073 PL_sub_generation++;
1078 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1080 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1081 PL_amagic_generation++;
1087 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1089 HV *hv = (HV*)LvTARG(sv);
1094 (void) hv_iterinit(hv);
1095 if (! SvTIED_mg((SV*)hv, 'P'))
1099 while ((entry = hv_iternext(hv))) {
1105 sv_setiv(sv, (IV)i);
1110 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1113 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1118 /* caller is responsible for stack switching/cleanup */
1120 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1126 PUSHs(SvTIED_obj(sv, mg));
1129 if (mg->mg_len >= 0)
1130 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1131 else if (mg->mg_len == HEf_SVKEY)
1132 PUSHs((SV*)mg->mg_ptr);
1134 else if (mg->mg_type == 'p') {
1135 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1143 return call_method(meth, flags);
1147 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1153 PUSHSTACKi(PERLSI_MAGIC);
1155 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1156 sv_setsv(sv, *PL_stack_sp--);
1166 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1168 magic_methpack(sv,mg,"FETCH");
1170 mg->mg_flags |= MGf_GSKIP;
1175 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1179 PUSHSTACKi(PERLSI_MAGIC);
1180 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1187 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1189 return magic_methpack(sv,mg,"DELETE");
1194 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1201 PUSHSTACKi(PERLSI_MAGIC);
1202 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1203 sv = *PL_stack_sp--;
1204 retval = (U32) SvIV(sv)-1;
1213 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1218 PUSHSTACKi(PERLSI_MAGIC);
1220 XPUSHs(SvTIED_obj(sv, mg));
1222 call_method("CLEAR", G_SCALAR|G_DISCARD);
1229 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1232 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1236 PUSHSTACKi(PERLSI_MAGIC);
1239 PUSHs(SvTIED_obj(sv, mg));
1244 if (call_method(meth, G_SCALAR))
1245 sv_setsv(key, *PL_stack_sp--);
1254 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1256 return magic_methpack(sv,mg,"EXISTS");
1260 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1271 svp = av_fetch(GvAV(gv),
1272 atoi(MgPV(mg,n_a)), FALSE);
1273 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1275 else if (ckWARN_d(WARN_INTERNAL))
1276 Perl_warner(aTHX_ WARN_INTERNAL, "Can't break at that line\n");
1281 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1284 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1289 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1292 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1297 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1299 SV* lsv = LvTARG(sv);
1301 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1302 mg = mg_find(lsv, 'g');
1303 if (mg && mg->mg_len >= 0) {
1307 sv_pos_b2u(lsv, &i);
1308 sv_setiv(sv, i + PL_curcop->cop_arybase);
1317 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1319 SV* lsv = LvTARG(sv);
1327 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1328 mg = mg_find(lsv, 'g');
1332 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1333 mg = mg_find(lsv, 'g');
1335 else if (!SvOK(sv)) {
1339 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1341 pos = SvIV(sv) - PL_curcop->cop_arybase;
1344 ulen = sv_len_utf8(lsv);
1359 sv_pos_u2b(lsv, &p, 0);
1364 mg->mg_flags &= ~MGf_MINMATCH;
1370 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1372 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1374 gv_efullname3(sv,((GV*)sv), "*");
1378 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1383 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1392 if (*s == '*' && s[1])
1394 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1399 GvGP(sv) = gp_ref(GvGP(gv));
1404 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1407 SV *lsv = LvTARG(sv);
1408 char *tmps = SvPV(lsv,len);
1409 I32 offs = LvTARGOFF(sv);
1410 I32 rem = LvTARGLEN(sv);
1414 if (rem + offs > len)
1416 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1421 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1424 char *tmps = SvPV(sv,len);
1425 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1430 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1433 TAINT_IF((mg->mg_len & 1) ||
1434 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1439 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1442 if (PL_localizing) {
1443 if (PL_localizing == 1)
1448 else if (PL_tainted)
1456 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1458 SV *lsv = LvTARG(sv);
1465 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1470 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1472 do_vecset(sv); /* XXX slurp this routine */
1477 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1480 if (LvTARGLEN(sv)) {
1482 SV *ahv = LvTARG(sv);
1483 if (SvTYPE(ahv) == SVt_PVHV) {
1484 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1489 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1495 AV* av = (AV*)LvTARG(sv);
1496 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1497 targ = AvARRAY(av)[LvTARGOFF(sv)];
1499 if (targ && targ != &PL_sv_undef) {
1500 dTHR; /* just for SvREFCNT_dec */
1501 /* somebody else defined it for us */
1502 SvREFCNT_dec(LvTARG(sv));
1503 LvTARG(sv) = SvREFCNT_inc(targ);
1505 SvREFCNT_dec(mg->mg_obj);
1506 mg->mg_obj = Nullsv;
1507 mg->mg_flags &= ~MGf_REFCOUNTED;
1512 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1517 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1522 sv_setsv(LvTARG(sv), sv);
1523 SvSETMAGIC(LvTARG(sv));
1529 Perl_vivify_defelem(pTHX_ SV *sv)
1531 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1535 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1538 SV *ahv = LvTARG(sv);
1540 if (SvTYPE(ahv) == SVt_PVHV) {
1541 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1546 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1550 if (!value || value == &PL_sv_undef)
1551 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1554 AV* av = (AV*)LvTARG(sv);
1555 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1556 LvTARG(sv) = Nullsv; /* array can't be extended */
1558 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1559 if (!svp || (value = *svp) == &PL_sv_undef)
1560 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1563 (void)SvREFCNT_inc(value);
1564 SvREFCNT_dec(LvTARG(sv));
1567 SvREFCNT_dec(mg->mg_obj);
1568 mg->mg_obj = Nullsv;
1569 mg->mg_flags &= ~MGf_REFCOUNTED;
1573 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1575 AV *av = (AV*)mg->mg_obj;
1576 SV **svp = AvARRAY(av);
1577 I32 i = AvFILLp(av);
1579 if (svp[i] && svp[i] != &PL_sv_undef) {
1580 if (!SvWEAKREF(svp[i]))
1581 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1582 /* XXX Should we check that it hasn't changed? */
1584 (void)SvOK_off(svp[i]);
1585 SvWEAKREF_off(svp[i]);
1586 svp[i] = &PL_sv_undef;
1594 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1602 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1604 sv_unmagic(sv, 'B');
1610 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1612 sv_unmagic(sv, 'f');
1618 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1620 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1622 if (uf && uf->uf_set)
1623 (*uf->uf_set)(uf->uf_index, sv);
1628 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1630 regexp *re = (regexp *)mg->mg_obj;
1635 #ifdef USE_LOCALE_COLLATE
1637 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1640 * RenE<eacute> Descartes said "I think not."
1641 * and vanished with a faint plop.
1644 Safefree(mg->mg_ptr);
1650 #endif /* USE_LOCALE_COLLATE */
1653 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1659 switch (*mg->mg_ptr) {
1660 case '\001': /* ^A */
1661 sv_setsv(PL_bodytarget, sv);
1663 case '\003': /* ^C */
1664 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1667 case '\004': /* ^D */
1668 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1669 DEBUG_x(dump_all());
1671 case '\005': /* ^E */
1672 #ifdef MACOS_TRADITIONAL
1673 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1676 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1679 SetLastError( SvIV(sv) );
1682 /* will anyone ever use this? */
1683 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1689 case '\006': /* ^F */
1690 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1692 case '\010': /* ^H */
1693 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1695 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1697 Safefree(PL_inplace);
1699 PL_inplace = savepv(SvPV(sv,len));
1701 PL_inplace = Nullch;
1703 case '\017': /* ^O */
1705 Safefree(PL_osname);
1707 PL_osname = savepv(SvPV(sv,len));
1711 case '\020': /* ^P */
1712 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1713 if (PL_perldb && !PL_DBsingle)
1716 case '\024': /* ^T */
1718 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1720 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1723 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1724 if (*(mg->mg_ptr+1) == '\0') {
1725 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1726 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1727 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1728 | (i ? G_WARN_ON : G_WARN_OFF) ;
1731 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1732 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1733 if (!SvPOK(sv) && PL_localizing) {
1734 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1735 PL_compiling.cop_warnings = pWARN_NONE;
1738 if (isWARN_on(sv, WARN_ALL) && !isWARNf_on(sv, WARN_ALL)) {
1739 PL_compiling.cop_warnings = pWARN_ALL;
1740 PL_dowarn |= G_WARN_ONCE ;
1744 int accumulate = 0 ;
1745 char * ptr = (char*)SvPV(sv, len) ;
1746 for (i = 0 ; i < len ; ++i)
1747 accumulate += ptr[i] ;
1749 PL_compiling.cop_warnings = pWARN_NONE;
1751 if (specialWARN(PL_compiling.cop_warnings))
1752 PL_compiling.cop_warnings = newSVsv(sv) ;
1754 sv_setsv(PL_compiling.cop_warnings, sv);
1755 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1756 PL_dowarn |= G_WARN_ONCE ;
1761 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1762 PL_widesyscalls = SvTRUE(sv);
1765 if (PL_localizing) {
1766 if (PL_localizing == 1)
1767 SAVESPTR(PL_last_in_gv);
1769 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1770 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1773 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1774 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1775 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1778 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1779 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1780 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1783 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1786 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1787 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1788 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1791 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1795 IO *io = GvIOp(PL_defoutgv);
1796 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1797 IoFLAGS(io) &= ~IOf_FLUSH;
1799 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1800 PerlIO *ofp = IoOFP(io);
1802 (void)PerlIO_flush(ofp);
1803 IoFLAGS(io) |= IOf_FLUSH;
1809 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1810 PL_multiline = (i != 0);
1813 SvREFCNT_dec(PL_nrs);
1814 PL_nrs = newSVsv(sv);
1815 SvREFCNT_dec(PL_rs);
1816 PL_rs = SvREFCNT_inc(PL_nrs);
1821 if (SvOK(sv) || SvGMAGICAL(sv)) {
1822 s = SvPV(sv,PL_orslen);
1823 PL_ors = savepvn(s,PL_orslen);
1833 PL_ofs = savepv(SvPV(sv, PL_ofslen));
1838 PL_ofmt = savepv(SvPV(sv,len));
1841 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1844 #ifdef COMPLEX_STATUS
1845 if (PL_localizing == 2) {
1846 PL_statusvalue = LvTARGOFF(sv);
1847 PL_statusvalue_vms = LvTARGLEN(sv);
1851 #ifdef VMSISH_STATUS
1853 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1856 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1859 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1860 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1863 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1864 if (PL_delaymagic) {
1865 PL_delaymagic |= DM_RUID;
1866 break; /* don't do magic till later */
1869 (void)setruid((Uid_t)PL_uid);
1872 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1874 #ifdef HAS_SETRESUID
1875 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1877 if (PL_uid == PL_euid) /* special case $< = $> */
1878 (void)PerlProc_setuid(PL_uid);
1880 PL_uid = PerlProc_getuid();
1881 Perl_croak(aTHX_ "setruid() not implemented");
1886 PL_uid = PerlProc_getuid();
1887 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1890 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1891 if (PL_delaymagic) {
1892 PL_delaymagic |= DM_EUID;
1893 break; /* don't do magic till later */
1896 (void)seteuid((Uid_t)PL_euid);
1899 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1901 #ifdef HAS_SETRESUID
1902 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1904 if (PL_euid == PL_uid) /* special case $> = $< */
1905 PerlProc_setuid(PL_euid);
1907 PL_euid = PerlProc_geteuid();
1908 Perl_croak(aTHX_ "seteuid() not implemented");
1913 PL_euid = PerlProc_geteuid();
1914 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1917 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1918 if (PL_delaymagic) {
1919 PL_delaymagic |= DM_RGID;
1920 break; /* don't do magic till later */
1923 (void)setrgid((Gid_t)PL_gid);
1926 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1928 #ifdef HAS_SETRESGID
1929 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1931 if (PL_gid == PL_egid) /* special case $( = $) */
1932 (void)PerlProc_setgid(PL_gid);
1934 PL_gid = PerlProc_getgid();
1935 Perl_croak(aTHX_ "setrgid() not implemented");
1940 PL_gid = PerlProc_getgid();
1941 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1944 #ifdef HAS_SETGROUPS
1946 char *p = SvPV(sv, len);
1947 Groups_t gary[NGROUPS];
1952 for (i = 0; i < NGROUPS; ++i) {
1953 while (*p && !isSPACE(*p))
1962 (void)setgroups(i, gary);
1964 #else /* HAS_SETGROUPS */
1965 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1966 #endif /* HAS_SETGROUPS */
1967 if (PL_delaymagic) {
1968 PL_delaymagic |= DM_EGID;
1969 break; /* don't do magic till later */
1972 (void)setegid((Gid_t)PL_egid);
1975 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
1977 #ifdef HAS_SETRESGID
1978 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
1980 if (PL_egid == PL_gid) /* special case $) = $( */
1981 (void)PerlProc_setgid(PL_egid);
1983 PL_egid = PerlProc_getegid();
1984 Perl_croak(aTHX_ "setegid() not implemented");
1989 PL_egid = PerlProc_getegid();
1990 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1993 PL_chopset = SvPV_force(sv,len);
1995 #ifndef MACOS_TRADITIONAL
2000 /* See if all the arguments are contiguous in memory */
2001 for (i = 1; i < PL_origargc; i++) {
2002 if (PL_origargv[i] == s + 1
2004 || PL_origargv[i] == s + 2
2009 s += strlen(s); /* this one is ok too */
2014 /* can grab env area too? */
2015 if (PL_origenviron && (PL_origenviron[0] == s + 1
2017 || (PL_origenviron[0] == s + 9 && (s += 8))
2020 my_setenv("NoNe SuCh", Nullch);
2021 /* force copy of environment */
2022 for (i = 0; PL_origenviron[i]; i++)
2023 if (PL_origenviron[i] == s + 1) {
2030 PL_origalen = s - PL_origargv[0];
2032 s = SvPV_force(sv,len);
2034 if (i >= PL_origalen) {
2036 /* don't allow system to limit $0 seen by script */
2037 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2038 Copy(s, PL_origargv[0], i, char);
2039 s = PL_origargv[0]+i;
2043 Copy(s, PL_origargv[0], i, char);
2044 s = PL_origargv[0]+i;
2046 while (++i < PL_origalen)
2048 s = PL_origargv[0]+i;
2049 for (i = 1; i < PL_origargc; i++)
2050 PL_origargv[i] = Nullch;
2056 sv_setsv(thr->errsv, sv);
2058 #endif /* USE_THREADS */
2065 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2068 DEBUG_S(PerlIO_printf(Perl_debug_log,
2069 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2070 PTR2UV(thr), PTR2UV(sv));)
2072 Perl_croak(aTHX_ "panic: magic_mutexfree");
2073 MUTEX_DESTROY(MgMUTEXP(mg));
2074 COND_DESTROY(MgCONDP(mg));
2077 #endif /* USE_THREADS */
2080 Perl_whichsig(pTHX_ char *sig)
2082 register char **sigv;
2084 for (sigv = PL_sig_name+1; *sigv; sigv++)
2085 if (strEQ(sig,*sigv))
2086 return PL_sig_num[sigv - PL_sig_name];
2088 if (strEQ(sig,"CHLD"))
2092 if (strEQ(sig,"CLD"))
2101 Perl_sighandler(int sig)
2103 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2104 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2111 SV *sv, *tSv = PL_Sv;
2115 I32 o_save_i = PL_savestack_ix;
2118 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2119 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2122 if (PL_savestack_ix + 15 <= PL_savestack_max)
2124 if (PL_markstack_ptr < PL_markstack_max - 2)
2126 if (PL_retstack_ix < PL_retstack_max - 2)
2128 if (PL_scopestack_ix < PL_scopestack_max - 3)
2131 if (!PL_psig_ptr[sig])
2132 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2135 /* Max number of items pushed there is 3*n or 4. We cannot fix
2136 infinity, so we fix 4 (in fact 5): */
2138 PL_savestack_ix += 5; /* Protect save in progress. */
2139 o_save_i = PL_savestack_ix;
2140 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2143 PL_markstack_ptr++; /* Protect mark. */
2146 PL_retstack[PL_retstack_ix] = NULL;
2149 PL_scopestack_ix += 1;
2150 /* sv_2cv is too complicated, try a simpler variant first: */
2151 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2152 || SvTYPE(cv) != SVt_PVCV)
2153 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2155 if (!cv || !CvROOT(cv)) {
2156 if (ckWARN(WARN_SIGNAL))
2157 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2158 PL_sig_name[sig], (gv ? GvENAME(gv)
2165 if(PL_psig_name[sig]) {
2166 sv = SvREFCNT_inc(PL_psig_name[sig]);
2170 sv = sv_newmortal();
2171 sv_setpv(sv,PL_sig_name[sig]);
2174 PUSHSTACKi(PERLSI_SIGNAL);
2179 call_sv((SV*)cv, G_DISCARD);
2184 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2190 PL_scopestack_ix -= 1;
2193 PL_op = myop; /* Apparently not needed... */
2195 PL_Sv = tSv; /* Restore global temporaries. */
2206 restore_magic(pTHXo_ void *p)
2209 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2210 SV* sv = mgs->mgs_sv;
2215 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2218 SvFLAGS(sv) |= mgs->mgs_flags;
2222 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2225 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2227 /* If we're still on top of the stack, pop us off. (That condition
2228 * will be satisfied if restore_magic was called explicitly, but *not*
2229 * if it's being called via leave_scope.)
2230 * The reason for doing this is that otherwise, things like sv_2cv()
2231 * may leave alloc gunk on the savestack, and some code
2232 * (e.g. sighandler) doesn't expect that...
2234 if (PL_savestack_ix == mgs->mgs_ss_ix)
2236 I32 popval = SSPOPINT;
2237 assert(popval == SAVEt_DESTRUCTOR_X);
2238 PL_savestack_ix -= 2;
2240 assert(popval == SAVEt_ALLOC);
2242 PL_savestack_ix -= popval;
2248 unwind_handler_stack(pTHXo_ void *p)
2251 U32 flags = *(U32*)p;
2254 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2255 /* cxstack_ix-- Not needed, die already unwound it. */
2257 SvREFCNT_dec(sig_sv);