3 * Copyright (c) 1991-1999, 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 Perl_mg_magical(pTHX_ SV *sv)
67 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
68 MGVTBL* vtbl = mg->mg_virtual;
70 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
74 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
81 Perl_mg_get(pTHX_ SV *sv)
89 mgs_ix = SSNEW(sizeof(MGS));
90 save_magic(mgs_ix, sv);
93 while ((mg = *mgp) != 0) {
94 MGVTBL* vtbl = mg->mg_virtual;
95 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
96 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
97 /* Ignore this magic if it's been deleted */
98 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
99 (mg->mg_flags & MGf_GSKIP))
100 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
102 /* Advance to next magic (complicated by possible deletion) */
103 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
104 mgp = &mg->mg_moremagic;
108 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
111 restore_magic(aTHXo_ (void*)mgs_ix);
116 Perl_mg_set(pTHX_ SV *sv)
123 mgs_ix = SSNEW(sizeof(MGS));
124 save_magic(mgs_ix, sv);
126 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
127 MGVTBL* vtbl = mg->mg_virtual;
128 nextmg = mg->mg_moremagic; /* it may delete itself */
129 if (mg->mg_flags & MGf_GSKIP) {
130 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
131 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
133 if (vtbl && vtbl->svt_set)
134 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
137 restore_magic(aTHXo_ (void*)mgs_ix);
142 Perl_mg_length(pTHX_ SV *sv)
148 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
149 MGVTBL* vtbl = mg->mg_virtual;
150 if (vtbl && vtbl->svt_len) {
153 mgs_ix = SSNEW(sizeof(MGS));
154 save_magic(mgs_ix, sv);
155 /* omit MGf_GSKIP -- not changed here */
156 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
157 restore_magic(aTHXo_ (void*)mgs_ix);
162 junk = SvPV(sv, len);
167 Perl_mg_size(pTHX_ SV *sv)
172 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
173 MGVTBL* vtbl = mg->mg_virtual;
174 if (vtbl && vtbl->svt_len) {
177 mgs_ix = SSNEW(sizeof(MGS));
178 save_magic(mgs_ix, sv);
179 /* omit MGf_GSKIP -- not changed here */
180 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
181 restore_magic(aTHXo_ (void*)mgs_ix);
188 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
193 Perl_croak(aTHX_ "Size magic not implemented");
200 Perl_mg_clear(pTHX_ SV *sv)
205 mgs_ix = SSNEW(sizeof(MGS));
206 save_magic(mgs_ix, sv);
208 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
209 MGVTBL* vtbl = mg->mg_virtual;
210 /* omit GSKIP -- never set here */
212 if (vtbl && vtbl->svt_clear)
213 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
216 restore_magic(aTHXo_ (void*)mgs_ix);
221 Perl_mg_find(pTHX_ SV *sv, int type)
224 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
225 if (mg->mg_type == type)
232 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
236 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
237 if (isUPPER(mg->mg_type)) {
239 mg->mg_type == 'P' ? SvTIED_obj(sv, mg) : mg->mg_obj,
240 toLOWER(mg->mg_type), key, klen);
248 Perl_mg_free(pTHX_ SV *sv)
252 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
253 MGVTBL* vtbl = mg->mg_virtual;
254 moremagic = mg->mg_moremagic;
255 if (vtbl && vtbl->svt_free)
256 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
257 if (mg->mg_ptr && mg->mg_type != 'g')
259 Safefree(mg->mg_ptr);
260 else if (mg->mg_len == HEf_SVKEY)
261 SvREFCNT_dec((SV*)mg->mg_ptr);
262 if (mg->mg_flags & MGf_REFCOUNTED)
263 SvREFCNT_dec(mg->mg_obj);
270 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
275 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
283 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
284 if (mg->mg_obj) /* @+ */
287 return rx->lastparen;
294 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
303 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
307 if (paren <= rx->nparens &&
308 (s = rx->startp[paren]) != -1 &&
309 (t = rx->endp[paren]) != -1)
311 if (mg->mg_obj) /* @+ */
322 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
331 switch (*mg->mg_ptr) {
332 case '1': case '2': case '3': case '4':
333 case '5': case '6': case '7': case '8': case '9': case '&':
334 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
337 paren = atoi(mg->mg_ptr);
339 if (paren <= rx->nparens &&
340 (s1 = rx->startp[paren]) != -1 &&
341 (t1 = rx->endp[paren]) != -1)
350 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
351 paren = rx->lastparen;
357 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
358 if (rx->startp[0] != -1) {
366 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
367 if (rx->endp[0] != -1) {
368 i = rx->sublen - rx->endp[0];
375 return (STRLEN)PL_ofslen;
377 return (STRLEN)PL_orslen;
380 if (!SvPOK(sv) && SvNIOK(sv)) {
390 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
399 switch (*mg->mg_ptr) {
400 case '\001': /* ^A */
401 sv_setsv(sv, PL_bodytarget);
403 case '\003': /* ^C */
404 sv_setiv(sv, (IV)PL_minus_c);
407 case '\004': /* ^D */
408 sv_setiv(sv, (IV)(PL_debug & 32767));
409 #if defined(YYDEBUG) && defined(DEBUGGING)
410 PL_yydebug = (PL_debug & 1);
413 case '\005': /* ^E */
414 #ifdef MACOS_TRADITIONAL
418 sv_setnv(sv,(double)gLastMacOSErr);
419 sv_setpv(sv, gLastMacOSErr ? GetSysErrText(gLastMacOSErr, msg) : "");
424 # include <descrip.h>
425 # include <starlet.h>
427 $DESCRIPTOR(msgdsc,msg);
428 sv_setnv(sv,(NV) vaxc$errno);
429 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
430 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
436 if (!(_emx_env & 0x200)) { /* Under DOS */
437 sv_setnv(sv, (NV)errno);
438 sv_setpv(sv, errno ? Strerror(errno) : "");
440 if (errno != errno_isOS2) {
441 int tmp = _syserrno();
442 if (tmp) /* 2nd call to _syserrno() makes it 0 */
445 sv_setnv(sv, (NV)Perl_rc);
446 sv_setpv(sv, os2error(Perl_rc));
451 DWORD dwErr = GetLastError();
452 sv_setnv(sv, (NV)dwErr);
455 PerlProc_GetOSError(sv, dwErr);
462 sv_setnv(sv, (NV)errno);
463 sv_setpv(sv, errno ? Strerror(errno) : "");
468 SvNOK_on(sv); /* what a wonderful hack! */
470 case '\006': /* ^F */
471 sv_setiv(sv, (IV)PL_maxsysfd);
473 case '\010': /* ^H */
474 sv_setiv(sv, (IV)PL_hints);
476 case '\011': /* ^I */ /* NOT \t in EBCDIC */
478 sv_setpv(sv, PL_inplace);
480 sv_setsv(sv, &PL_sv_undef);
482 case '\017': /* ^O */
483 sv_setpv(sv, PL_osname);
485 case '\020': /* ^P */
486 sv_setiv(sv, (IV)PL_perldb);
488 case '\023': /* ^S */
491 if (PL_lex_state != LEX_NOTPARSING)
499 case '\024': /* ^T */
501 sv_setnv(sv, PL_basetime);
503 sv_setiv(sv, (IV)PL_basetime);
506 case '\027': /* ^W & $^Warnings*/
507 if (*(mg->mg_ptr+1) == '\0')
508 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
509 else if (strEQ(mg->mg_ptr, "\027arnings")) {
510 if (PL_compiling.cop_warnings == WARN_NONE ||
511 PL_compiling.cop_warnings == WARN_STD)
513 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
515 else if (PL_compiling.cop_warnings == WARN_ALL) {
516 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
519 sv_setsv(sv, PL_compiling.cop_warnings);
523 case '1': case '2': case '3': case '4':
524 case '5': case '6': case '7': case '8': case '9': case '&':
525 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
529 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
530 * XXX Does the new way break anything?
532 paren = atoi(mg->mg_ptr);
534 if (paren <= rx->nparens &&
535 (s1 = rx->startp[paren]) != -1 &&
536 (t1 = rx->endp[paren]) != -1)
544 was_tainted = PL_tainted;
549 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
554 sv_setsv(sv,&PL_sv_undef);
557 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
558 paren = rx->lastparen;
562 sv_setsv(sv,&PL_sv_undef);
565 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
566 if ((s = rx->subbeg) && rx->startp[0] != -1) {
571 sv_setsv(sv,&PL_sv_undef);
574 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
575 if (rx->subbeg && rx->endp[0] != -1) {
576 s = rx->subbeg + rx->endp[0];
577 i = rx->sublen - rx->endp[0];
581 sv_setsv(sv,&PL_sv_undef);
585 if (GvIO(PL_last_in_gv)) {
586 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
592 sv_setiv(sv, (IV)STATUS_CURRENT);
593 #ifdef COMPLEX_STATUS
594 LvTARGOFF(sv) = PL_statusvalue;
595 LvTARGLEN(sv) = PL_statusvalue_vms;
600 s = IoTOP_NAME(GvIOp(PL_defoutgv));
604 sv_setpv(sv,GvENAME(PL_defoutgv));
609 s = IoFMT_NAME(GvIOp(PL_defoutgv));
611 s = GvENAME(PL_defoutgv);
616 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
619 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
622 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
630 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
633 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
636 sv_setpvn(sv,PL_ofs,PL_ofslen);
639 sv_setpvn(sv,PL_ors,PL_orslen);
642 sv_setpv(sv,PL_ofmt);
646 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
647 sv_setpv(sv, errno ? Strerror(errno) : "");
650 int saveerrno = errno;
651 sv_setnv(sv, (NV)errno);
653 if (errno == errno_isOS2 || errno == errno_isOS2_set)
654 sv_setpv(sv, os2error(Perl_rc));
657 sv_setpv(sv, errno ? Strerror(errno) : "");
661 SvNOK_on(sv); /* what a wonderful hack! */
664 sv_setiv(sv, (IV)PL_uid);
667 sv_setiv(sv, (IV)PL_euid);
670 sv_setiv(sv, (IV)PL_gid);
672 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
676 sv_setiv(sv, (IV)PL_egid);
678 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
683 Groups_t gary[NGROUPS];
684 i = getgroups(NGROUPS,gary);
686 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
689 SvIOK_on(sv); /* what a wonderful hack! */
693 #ifndef MACOS_TRADITIONAL
699 sv_setsv(sv, thr->errsv);
701 #endif /* USE_THREADS */
707 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
709 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
711 if (uf && uf->uf_val)
712 (*uf->uf_val)(uf->uf_index, sv);
717 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
728 #ifdef DYNAMIC_ENV_FETCH
729 /* We just undefd an environment var. Is a replacement */
730 /* waiting in the wings? */
733 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
734 s = SvPV(*valp, len);
738 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
739 /* And you'll never guess what the dog had */
740 /* in its mouth... */
742 MgTAINTEDDIR_off(mg);
744 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
745 char pathbuf[256], eltbuf[256], *cp, *elt = s;
749 do { /* DCL$PATH may be a search list */
750 while (1) { /* as may dev portion of any element */
751 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
752 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
753 cando_by_name(S_IWUSR,0,elt) ) {
758 if ((cp = strchr(elt, ':')) != Nullch)
760 if (my_trnlnm(elt, eltbuf, j++))
766 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
769 if (s && klen == 4 && strEQ(ptr,"PATH")) {
770 char *strend = s + len;
775 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
778 if (i >= sizeof tmpbuf /* too long -- assume the worst */
780 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
787 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
793 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
796 my_setenv(MgPV(mg,n_a),Nullch);
801 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
804 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
810 magic_clear_all_env(sv,mg);
811 hv_iterinit((HV*)sv);
812 while (entry = hv_iternext((HV*)sv)) {
814 my_setenv(hv_iterkey(entry, &keylen),
815 SvPV(hv_iterval((HV*)sv, entry), n_a));
823 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
826 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
828 # ifdef PERL_IMPLICIT_SYS
832 char *envv = GetEnvironmentStrings();
836 char *end = strchr(cur,'=');
837 if (end && end != cur) {
839 my_setenv(cur,Nullch);
841 cur = end + strlen(end+1)+2;
843 else if ((len = strlen(cur)))
846 FreeEnvironmentStrings(envv);
850 for (i = 0; environ[i]; i++)
851 Safefree(environ[i]);
853 # ifndef PERL_USE_SAFE_PUTENV
856 if (environ == PL_origenviron)
857 environ = (char**)safesysmalloc(sizeof(char*));
859 for (i = 0; environ[i]; i++)
860 safesysfree(environ[i]);
861 # endif /* PERL_USE_SAFE_PUTENV */
867 # endif /* PERL_IMPLICIT_SYS */
873 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
877 /* Are we fetching a signal entry? */
878 i = whichsig(MgPV(mg,n_a));
881 sv_setsv(sv,PL_psig_ptr[i]);
883 Sighandler_t sigstate = rsignal_state(i);
885 /* cache state so we don't fetch it again */
886 if(sigstate == SIG_IGN)
887 sv_setpv(sv,"IGNORE");
889 sv_setsv(sv,&PL_sv_undef);
890 PL_psig_ptr[i] = SvREFCNT_inc(sv);
897 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
901 /* Are we clearing a signal entry? */
902 i = whichsig(MgPV(mg,n_a));
905 SvREFCNT_dec(PL_psig_ptr[i]);
908 if(PL_psig_name[i]) {
909 SvREFCNT_dec(PL_psig_name[i]);
917 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
927 if (strEQ(s,"__DIE__"))
929 else if (strEQ(s,"__WARN__"))
932 Perl_croak(aTHX_ "No such hook: %s", s);
940 i = whichsig(s); /* ...no, a brick */
942 if (ckWARN(WARN_SIGNAL) || strEQ(s,"ALARM"))
943 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
946 SvREFCNT_dec(PL_psig_name[i]);
947 SvREFCNT_dec(PL_psig_ptr[i]);
948 PL_psig_ptr[i] = SvREFCNT_inc(sv);
949 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
950 PL_psig_name[i] = newSVpvn(s, len);
951 SvREADONLY_on(PL_psig_name[i]);
953 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
955 (void)rsignal(i, PL_sighandlerp);
957 *svp = SvREFCNT_inc(sv);
960 s = SvPV_force(sv,len);
961 if (strEQ(s,"IGNORE")) {
963 (void)rsignal(i, SIG_IGN);
967 else if (strEQ(s,"DEFAULT") || !*s) {
969 (void)rsignal(i, SIG_DFL);
975 * We should warn if HINT_STRICT_REFS, but without
976 * access to a known hint bit in a known OP, we can't
977 * tell whether HINT_STRICT_REFS is in force or not.
979 if (!strchr(s,':') && !strchr(s,'\''))
980 sv_insert(sv, 0, 0, "main::", 6);
982 (void)rsignal(i, PL_sighandlerp);
984 *svp = SvREFCNT_inc(sv);
990 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
997 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
999 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1000 PL_amagic_generation++;
1006 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1008 HV *hv = (HV*)LvTARG(sv);
1013 (void) hv_iterinit(hv);
1014 if (! SvTIED_mg((SV*)hv, 'P'))
1018 while (entry = hv_iternext(hv)) {
1024 sv_setiv(sv, (IV)i);
1029 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1032 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1037 /* caller is responsible for stack switching/cleanup */
1039 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1045 PUSHs(SvTIED_obj(sv, mg));
1048 if (mg->mg_len >= 0)
1049 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1050 else if (mg->mg_len == HEf_SVKEY)
1051 PUSHs((SV*)mg->mg_ptr);
1053 else if (mg->mg_type == 'p') {
1054 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1062 return call_method(meth, flags);
1066 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1072 PUSHSTACKi(PERLSI_MAGIC);
1074 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1075 sv_setsv(sv, *PL_stack_sp--);
1085 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1087 magic_methpack(sv,mg,"FETCH");
1089 mg->mg_flags |= MGf_GSKIP;
1094 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1098 PUSHSTACKi(PERLSI_MAGIC);
1099 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1106 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1108 return magic_methpack(sv,mg,"DELETE");
1113 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1120 PUSHSTACKi(PERLSI_MAGIC);
1121 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1122 sv = *PL_stack_sp--;
1123 retval = (U32) SvIV(sv)-1;
1132 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1137 PUSHSTACKi(PERLSI_MAGIC);
1139 XPUSHs(SvTIED_obj(sv, mg));
1141 call_method("CLEAR", G_SCALAR|G_DISCARD);
1148 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1151 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1155 PUSHSTACKi(PERLSI_MAGIC);
1158 PUSHs(SvTIED_obj(sv, mg));
1163 if (call_method(meth, G_SCALAR))
1164 sv_setsv(key, *PL_stack_sp--);
1173 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1175 return magic_methpack(sv,mg,"EXISTS");
1179 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1190 svp = av_fetch(GvAV(gv),
1191 atoi(MgPV(mg,n_a)), FALSE);
1192 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1194 else if (ckWARN_d(WARN_INTERNAL))
1195 Perl_warner(aTHX_ WARN_INTERNAL, "Can't break at that line\n");
1200 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1203 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1208 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1211 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1216 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1218 SV* lsv = LvTARG(sv);
1220 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1221 mg = mg_find(lsv, 'g');
1222 if (mg && mg->mg_len >= 0) {
1226 sv_pos_b2u(lsv, &i);
1227 sv_setiv(sv, i + PL_curcop->cop_arybase);
1236 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1238 SV* lsv = LvTARG(sv);
1246 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1247 mg = mg_find(lsv, 'g');
1251 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1252 mg = mg_find(lsv, 'g');
1254 else if (!SvOK(sv)) {
1258 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1260 pos = SvIV(sv) - PL_curcop->cop_arybase;
1263 ulen = sv_len_utf8(lsv);
1280 sv_pos_u2b(lsv, &p, 0);
1285 mg->mg_flags &= ~MGf_MINMATCH;
1291 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1293 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1295 gv_efullname3(sv,((GV*)sv), "*");
1299 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1304 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1313 if (*s == '*' && s[1])
1315 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1320 GvGP(sv) = gp_ref(GvGP(gv));
1325 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1328 SV *lsv = LvTARG(sv);
1329 char *tmps = SvPV(lsv,len);
1330 I32 offs = LvTARGOFF(sv);
1331 I32 rem = LvTARGLEN(sv);
1335 if (rem + offs > len)
1337 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1342 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1345 char *tmps = SvPV(sv,len);
1346 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1351 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1354 TAINT_IF((mg->mg_len & 1) ||
1355 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
1360 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1363 if (PL_localizing) {
1364 if (PL_localizing == 1)
1369 else if (PL_tainted)
1377 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1379 SV *lsv = LvTARG(sv);
1386 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1391 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1393 do_vecset(sv); /* XXX slurp this routine */
1398 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1401 if (LvTARGLEN(sv)) {
1403 SV *ahv = LvTARG(sv);
1404 if (SvTYPE(ahv) == SVt_PVHV) {
1405 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1410 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1416 AV* av = (AV*)LvTARG(sv);
1417 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1418 targ = AvARRAY(av)[LvTARGOFF(sv)];
1420 if (targ && targ != &PL_sv_undef) {
1421 dTHR; /* just for SvREFCNT_dec */
1422 /* somebody else defined it for us */
1423 SvREFCNT_dec(LvTARG(sv));
1424 LvTARG(sv) = SvREFCNT_inc(targ);
1426 SvREFCNT_dec(mg->mg_obj);
1427 mg->mg_obj = Nullsv;
1428 mg->mg_flags &= ~MGf_REFCOUNTED;
1433 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1438 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1443 sv_setsv(LvTARG(sv), sv);
1444 SvSETMAGIC(LvTARG(sv));
1450 Perl_vivify_defelem(pTHX_ SV *sv)
1452 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1456 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1459 SV *ahv = LvTARG(sv);
1461 if (SvTYPE(ahv) == SVt_PVHV) {
1462 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1467 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1471 if (!value || value == &PL_sv_undef)
1472 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1475 AV* av = (AV*)LvTARG(sv);
1476 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1477 LvTARG(sv) = Nullsv; /* array can't be extended */
1479 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1480 if (!svp || (value = *svp) == &PL_sv_undef)
1481 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1484 (void)SvREFCNT_inc(value);
1485 SvREFCNT_dec(LvTARG(sv));
1488 SvREFCNT_dec(mg->mg_obj);
1489 mg->mg_obj = Nullsv;
1490 mg->mg_flags &= ~MGf_REFCOUNTED;
1494 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1496 AV *av = (AV*)mg->mg_obj;
1497 SV **svp = AvARRAY(av);
1498 I32 i = AvFILLp(av);
1500 if (svp[i] && svp[i] != &PL_sv_undef) {
1501 if (!SvWEAKREF(svp[i]))
1502 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1503 /* XXX Should we check that it hasn't changed? */
1506 SvWEAKREF_off(svp[i]);
1507 svp[i] = &PL_sv_undef;
1515 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1523 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1525 sv_unmagic(sv, 'B');
1531 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1533 sv_unmagic(sv, 'f');
1539 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1541 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1543 if (uf && uf->uf_set)
1544 (*uf->uf_set)(uf->uf_index, sv);
1549 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1551 regexp *re = (regexp *)mg->mg_obj;
1556 #ifdef USE_LOCALE_COLLATE
1558 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1561 * RenE<eacute> Descartes said "I think not."
1562 * and vanished with a faint plop.
1565 Safefree(mg->mg_ptr);
1571 #endif /* USE_LOCALE_COLLATE */
1574 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1580 switch (*mg->mg_ptr) {
1581 case '\001': /* ^A */
1582 sv_setsv(PL_bodytarget, sv);
1584 case '\003': /* ^C */
1585 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1588 case '\004': /* ^D */
1589 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1590 DEBUG_x(dump_all());
1592 case '\005': /* ^E */
1593 #ifdef MACOS_TRADITIONAL
1594 gLastMacOSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1597 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1600 SetLastError( SvIV(sv) );
1603 /* will anyone ever use this? */
1604 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1610 case '\006': /* ^F */
1611 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1613 case '\010': /* ^H */
1614 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1616 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1618 Safefree(PL_inplace);
1620 PL_inplace = savepv(SvPV(sv,len));
1622 PL_inplace = Nullch;
1624 case '\017': /* ^O */
1626 Safefree(PL_osname);
1628 PL_osname = savepv(SvPV(sv,len));
1632 case '\020': /* ^P */
1633 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1634 if (PL_perldb && !PL_DBsingle)
1637 case '\024': /* ^T */
1639 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1641 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1644 case '\027': /* ^W & $^Warnings */
1645 if (*(mg->mg_ptr+1) == '\0') {
1646 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1647 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1648 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1649 | (i ? G_WARN_ON : G_WARN_OFF) ;
1652 else if (strEQ(mg->mg_ptr, "\027arnings")) {
1653 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1654 if (memEQ(SvPVX(sv), WARN_ALLstring, WARNsize)) {
1655 PL_compiling.cop_warnings = WARN_ALL;
1656 PL_dowarn |= G_WARN_ONCE ;
1658 else if (memEQ(SvPVX(sv), WARN_NONEstring, WARNsize))
1659 PL_compiling.cop_warnings = WARN_NONE;
1661 if (specialWARN(PL_compiling.cop_warnings))
1662 PL_compiling.cop_warnings = newSVsv(sv) ;
1664 sv_setsv(PL_compiling.cop_warnings, sv);
1665 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1666 PL_dowarn |= G_WARN_ONCE ;
1672 if (PL_localizing) {
1673 if (PL_localizing == 1)
1674 SAVESPTR(PL_last_in_gv);
1676 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1677 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1680 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1681 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1682 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1685 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1686 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1687 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1690 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1693 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1694 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1695 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1698 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1702 IO *io = GvIOp(PL_defoutgv);
1703 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1704 IoFLAGS(io) &= ~IOf_FLUSH;
1706 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1707 PerlIO *ofp = IoOFP(io);
1709 (void)PerlIO_flush(ofp);
1710 IoFLAGS(io) |= IOf_FLUSH;
1716 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1717 PL_multiline = (i != 0);
1720 SvREFCNT_dec(PL_nrs);
1721 PL_nrs = newSVsv(sv);
1722 SvREFCNT_dec(PL_rs);
1723 PL_rs = SvREFCNT_inc(PL_nrs);
1728 if (SvOK(sv) || SvGMAGICAL(sv)) {
1729 s = SvPV(sv,PL_orslen);
1730 PL_ors = savepvn(s,PL_orslen);
1740 PL_ofs = savepv(SvPV(sv, PL_ofslen));
1745 PL_ofmt = savepv(SvPV(sv,len));
1748 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1751 #ifdef COMPLEX_STATUS
1752 if (PL_localizing == 2) {
1753 PL_statusvalue = LvTARGOFF(sv);
1754 PL_statusvalue_vms = LvTARGLEN(sv);
1758 #ifdef VMSISH_STATUS
1760 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1763 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1766 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1767 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1770 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1771 if (PL_delaymagic) {
1772 PL_delaymagic |= DM_RUID;
1773 break; /* don't do magic till later */
1776 (void)setruid((Uid_t)PL_uid);
1779 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1781 #ifdef HAS_SETRESUID
1782 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1784 if (PL_uid == PL_euid) /* special case $< = $> */
1785 (void)PerlProc_setuid(PL_uid);
1787 PL_uid = PerlProc_getuid();
1788 Perl_croak(aTHX_ "setruid() not implemented");
1793 PL_uid = PerlProc_getuid();
1794 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1797 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1798 if (PL_delaymagic) {
1799 PL_delaymagic |= DM_EUID;
1800 break; /* don't do magic till later */
1803 (void)seteuid((Uid_t)PL_euid);
1806 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1808 #ifdef HAS_SETRESUID
1809 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1811 if (PL_euid == PL_uid) /* special case $> = $< */
1812 PerlProc_setuid(PL_euid);
1814 PL_euid = PerlProc_geteuid();
1815 Perl_croak(aTHX_ "seteuid() not implemented");
1820 PL_euid = PerlProc_geteuid();
1821 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1824 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1825 if (PL_delaymagic) {
1826 PL_delaymagic |= DM_RGID;
1827 break; /* don't do magic till later */
1830 (void)setrgid((Gid_t)PL_gid);
1833 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1835 #ifdef HAS_SETRESGID
1836 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1838 if (PL_gid == PL_egid) /* special case $( = $) */
1839 (void)PerlProc_setgid(PL_gid);
1841 PL_gid = PerlProc_getgid();
1842 Perl_croak(aTHX_ "setrgid() not implemented");
1847 PL_gid = PerlProc_getgid();
1848 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1851 #ifdef HAS_SETGROUPS
1853 char *p = SvPV(sv, len);
1854 Groups_t gary[NGROUPS];
1859 for (i = 0; i < NGROUPS; ++i) {
1860 while (*p && !isSPACE(*p))
1869 (void)setgroups(i, gary);
1871 #else /* HAS_SETGROUPS */
1872 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1873 #endif /* HAS_SETGROUPS */
1874 if (PL_delaymagic) {
1875 PL_delaymagic |= DM_EGID;
1876 break; /* don't do magic till later */
1879 (void)setegid((Gid_t)PL_egid);
1882 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
1884 #ifdef HAS_SETRESGID
1885 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
1887 if (PL_egid == PL_gid) /* special case $) = $( */
1888 (void)PerlProc_setgid(PL_egid);
1890 PL_egid = PerlProc_getegid();
1891 Perl_croak(aTHX_ "setegid() not implemented");
1896 PL_egid = PerlProc_getegid();
1897 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1900 PL_chopset = SvPV_force(sv,len);
1902 #ifndef MACOS_TRADITIONAL
1907 /* See if all the arguments are contiguous in memory */
1908 for (i = 1; i < PL_origargc; i++) {
1909 if (PL_origargv[i] == s + 1
1911 || PL_origargv[i] == s + 2
1916 s += strlen(s); /* this one is ok too */
1921 /* can grab env area too? */
1922 if (PL_origenviron && (PL_origenviron[0] == s + 1
1924 || (PL_origenviron[0] == s + 9 && (s += 8))
1927 my_setenv("NoNe SuCh", Nullch);
1928 /* force copy of environment */
1929 for (i = 0; PL_origenviron[i]; i++)
1930 if (PL_origenviron[i] == s + 1) {
1937 PL_origalen = s - PL_origargv[0];
1939 s = SvPV_force(sv,len);
1941 if (i >= PL_origalen) {
1943 /* don't allow system to limit $0 seen by script */
1944 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
1945 Copy(s, PL_origargv[0], i, char);
1946 s = PL_origargv[0]+i;
1950 Copy(s, PL_origargv[0], i, char);
1951 s = PL_origargv[0]+i;
1953 while (++i < PL_origalen)
1955 s = PL_origargv[0]+i;
1956 for (i = 1; i < PL_origargc; i++)
1957 PL_origargv[i] = Nullch;
1963 sv_setsv(thr->errsv, sv);
1965 #endif /* USE_THREADS */
1972 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
1975 DEBUG_S(PerlIO_printf(Perl_debug_log,
1976 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
1977 PTR2UV(thr), PTR2UV(sv));)
1979 Perl_croak(aTHX_ "panic: magic_mutexfree");
1980 MUTEX_DESTROY(MgMUTEXP(mg));
1981 COND_DESTROY(MgCONDP(mg));
1984 #endif /* USE_THREADS */
1987 Perl_whichsig(pTHX_ char *sig)
1989 register char **sigv;
1991 for (sigv = PL_sig_name+1; *sigv; sigv++)
1992 if (strEQ(sig,*sigv))
1993 return PL_sig_num[sigv - PL_sig_name];
1995 if (strEQ(sig,"CHLD"))
1999 if (strEQ(sig,"CLD"))
2008 Perl_sighandler(int sig)
2014 SV *sv, *tSv = PL_Sv;
2018 I32 o_save_i = PL_savestack_ix, type;
2021 if (PL_savestack_ix + 15 <= PL_savestack_max)
2023 if (PL_markstack_ptr < PL_markstack_max - 2)
2025 if (PL_retstack_ix < PL_retstack_max - 2)
2027 if (PL_scopestack_ix < PL_scopestack_max - 3)
2030 if (!PL_psig_ptr[sig])
2031 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2034 /* Max number of items pushed there is 3*n or 4. We cannot fix
2035 infinity, so we fix 4 (in fact 5): */
2037 PL_savestack_ix += 5; /* Protect save in progress. */
2038 o_save_i = PL_savestack_ix;
2039 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2042 PL_markstack_ptr++; /* Protect mark. */
2045 PL_retstack[PL_retstack_ix] = NULL;
2048 PL_scopestack_ix += 1;
2049 /* sv_2cv is too complicated, try a simpler variant first: */
2050 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2051 || SvTYPE(cv) != SVt_PVCV)
2052 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2054 if (!cv || !CvROOT(cv)) {
2055 if (ckWARN(WARN_SIGNAL))
2056 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2057 PL_sig_name[sig], (gv ? GvENAME(gv)
2064 if(PL_psig_name[sig]) {
2065 sv = SvREFCNT_inc(PL_psig_name[sig]);
2069 sv = sv_newmortal();
2070 sv_setpv(sv,PL_sig_name[sig]);
2073 PUSHSTACKi(PERLSI_SIGNAL);
2078 call_sv((SV*)cv, G_DISCARD);
2083 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2089 PL_scopestack_ix -= 1;
2092 PL_op = myop; /* Apparently not needed... */
2094 PL_Sv = tSv; /* Restore global temporaries. */
2105 restore_magic(pTHXo_ void *p)
2108 MGS* mgs = SSPTR((I32)p, MGS*);
2109 SV* sv = mgs->mgs_sv;
2114 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2117 SvFLAGS(sv) |= mgs->mgs_flags;
2121 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2124 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2126 /* If we're still on top of the stack, pop us off. (That condition
2127 * will be satisfied if restore_magic was called explicitly, but *not*
2128 * if it's being called via leave_scope.)
2129 * The reason for doing this is that otherwise, things like sv_2cv()
2130 * may leave alloc gunk on the savestack, and some code
2131 * (e.g. sighandler) doesn't expect that...
2133 if (PL_savestack_ix == mgs->mgs_ss_ix)
2135 I32 popval = SSPOPINT;
2136 assert(popval == SAVEt_DESTRUCTOR_X);
2137 PL_savestack_ix -= 2;
2139 assert(popval == SAVEt_ALLOC);
2141 PL_savestack_ix -= popval;
2147 unwind_handler_stack(pTHXo_ void *p)
2150 U32 flags = *(U32*)p;
2153 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2154 /* cxstack_ix-- Not needed, die already unwound it. */
2156 SvREFCNT_dec(sig_sv);