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)
347 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
348 if (mg->mg_obj) /* @+ */
351 return rx->lastparen;
358 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
367 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
371 if (paren <= rx->nparens &&
372 (s = rx->startp[paren]) != -1 &&
373 (t = rx->endp[paren]) != -1)
375 if (mg->mg_obj) /* @+ */
386 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
395 switch (*mg->mg_ptr) {
396 case '1': case '2': case '3': case '4':
397 case '5': case '6': case '7': case '8': case '9': case '&':
398 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
401 paren = atoi(mg->mg_ptr);
403 if (paren <= rx->nparens &&
404 (s1 = rx->startp[paren]) != -1 &&
405 (t1 = rx->endp[paren]) != -1)
414 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
415 paren = rx->lastparen;
421 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
422 if (rx->startp[0] != -1) {
430 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
431 if (rx->endp[0] != -1) {
432 i = rx->sublen - rx->endp[0];
439 return (STRLEN)PL_ofslen;
441 return (STRLEN)PL_orslen;
444 if (!SvPOK(sv) && SvNIOK(sv)) {
454 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
463 switch (*mg->mg_ptr) {
464 case '\001': /* ^A */
465 sv_setsv(sv, PL_bodytarget);
467 case '\003': /* ^C */
468 sv_setiv(sv, (IV)PL_minus_c);
471 case '\004': /* ^D */
472 sv_setiv(sv, (IV)(PL_debug & 32767));
473 #if defined(YYDEBUG) && defined(DEBUGGING)
474 PL_yydebug = (PL_debug & 1);
477 case '\005': /* ^E */
478 #ifdef MACOS_TRADITIONAL
482 sv_setnv(sv,(double)gLastMacOSErr);
483 sv_setpv(sv, gLastMacOSErr ? GetSysErrText(gLastMacOSErr, msg) : "");
488 # include <descrip.h>
489 # include <starlet.h>
491 $DESCRIPTOR(msgdsc,msg);
492 sv_setnv(sv,(NV) vaxc$errno);
493 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
494 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
500 if (!(_emx_env & 0x200)) { /* Under DOS */
501 sv_setnv(sv, (NV)errno);
502 sv_setpv(sv, errno ? Strerror(errno) : "");
504 if (errno != errno_isOS2) {
505 int tmp = _syserrno();
506 if (tmp) /* 2nd call to _syserrno() makes it 0 */
509 sv_setnv(sv, (NV)Perl_rc);
510 sv_setpv(sv, os2error(Perl_rc));
515 DWORD dwErr = GetLastError();
516 sv_setnv(sv, (NV)dwErr);
519 PerlProc_GetOSError(sv, dwErr);
526 sv_setnv(sv, (NV)errno);
527 sv_setpv(sv, errno ? Strerror(errno) : "");
532 SvNOK_on(sv); /* what a wonderful hack! */
534 case '\006': /* ^F */
535 sv_setiv(sv, (IV)PL_maxsysfd);
537 case '\010': /* ^H */
538 sv_setiv(sv, (IV)PL_hints);
540 case '\011': /* ^I */ /* NOT \t in EBCDIC */
542 sv_setpv(sv, PL_inplace);
544 sv_setsv(sv, &PL_sv_undef);
546 case '\017': /* ^O */
547 sv_setpv(sv, PL_osname);
549 case '\020': /* ^P */
550 sv_setiv(sv, (IV)PL_perldb);
552 case '\023': /* ^S */
555 if (PL_lex_state != LEX_NOTPARSING)
563 case '\024': /* ^T */
565 sv_setnv(sv, PL_basetime);
567 sv_setiv(sv, (IV)PL_basetime);
570 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
571 if (*(mg->mg_ptr+1) == '\0')
572 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
573 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
574 if (PL_compiling.cop_warnings == WARN_NONE ||
575 PL_compiling.cop_warnings == WARN_STD)
577 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
579 else if (PL_compiling.cop_warnings == WARN_ALL) {
580 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
583 sv_setsv(sv, PL_compiling.cop_warnings);
586 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
587 sv_setiv(sv, (IV)PL_widesyscalls);
589 case '1': case '2': case '3': case '4':
590 case '5': case '6': case '7': case '8': case '9': case '&':
591 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
595 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
596 * XXX Does the new way break anything?
598 paren = atoi(mg->mg_ptr);
600 if (paren <= rx->nparens &&
601 (s1 = rx->startp[paren]) != -1 &&
602 (t1 = rx->endp[paren]) != -1)
610 was_tainted = PL_tainted;
614 if ((PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE)
619 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
624 sv_setsv(sv,&PL_sv_undef);
627 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
628 paren = rx->lastparen;
632 sv_setsv(sv,&PL_sv_undef);
635 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
636 if ((s = rx->subbeg) && rx->startp[0] != -1) {
641 sv_setsv(sv,&PL_sv_undef);
644 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
645 if (rx->subbeg && rx->endp[0] != -1) {
646 s = rx->subbeg + rx->endp[0];
647 i = rx->sublen - rx->endp[0];
651 sv_setsv(sv,&PL_sv_undef);
655 if (GvIO(PL_last_in_gv)) {
656 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
662 sv_setiv(sv, (IV)STATUS_CURRENT);
663 #ifdef COMPLEX_STATUS
664 LvTARGOFF(sv) = PL_statusvalue;
665 LvTARGLEN(sv) = PL_statusvalue_vms;
670 s = IoTOP_NAME(GvIOp(PL_defoutgv));
674 sv_setpv(sv,GvENAME(PL_defoutgv));
679 s = IoFMT_NAME(GvIOp(PL_defoutgv));
681 s = GvENAME(PL_defoutgv);
686 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
689 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
692 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
700 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
703 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
706 sv_setpvn(sv,PL_ofs,PL_ofslen);
709 sv_setpvn(sv,PL_ors,PL_orslen);
712 sv_setpv(sv,PL_ofmt);
716 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
717 sv_setpv(sv, errno ? Strerror(errno) : "");
720 int saveerrno = errno;
721 sv_setnv(sv, (NV)errno);
723 if (errno == errno_isOS2 || errno == errno_isOS2_set)
724 sv_setpv(sv, os2error(Perl_rc));
727 sv_setpv(sv, errno ? Strerror(errno) : "");
731 SvNOK_on(sv); /* what a wonderful hack! */
734 sv_setiv(sv, (IV)PL_uid);
737 sv_setiv(sv, (IV)PL_euid);
740 sv_setiv(sv, (IV)PL_gid);
742 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
746 sv_setiv(sv, (IV)PL_egid);
748 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
753 Groups_t gary[NGROUPS];
754 i = getgroups(NGROUPS,gary);
756 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
759 SvIOK_on(sv); /* what a wonderful hack! */
763 #ifndef MACOS_TRADITIONAL
769 sv_setsv(sv, thr->errsv);
771 #endif /* USE_THREADS */
777 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
779 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
781 if (uf && uf->uf_val)
782 (*uf->uf_val)(uf->uf_index, sv);
787 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
798 #ifdef DYNAMIC_ENV_FETCH
799 /* We just undefd an environment var. Is a replacement */
800 /* waiting in the wings? */
803 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
804 s = SvPV(*valp, len);
808 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
809 /* And you'll never guess what the dog had */
810 /* in its mouth... */
812 MgTAINTEDDIR_off(mg);
814 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
815 char pathbuf[256], eltbuf[256], *cp, *elt = s;
819 do { /* DCL$PATH may be a search list */
820 while (1) { /* as may dev portion of any element */
821 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
822 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
823 cando_by_name(S_IWUSR,0,elt) ) {
828 if ((cp = strchr(elt, ':')) != Nullch)
830 if (my_trnlnm(elt, eltbuf, j++))
836 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
839 if (s && klen == 4 && strEQ(ptr,"PATH")) {
840 char *strend = s + len;
845 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
848 if (i >= sizeof tmpbuf /* too long -- assume the worst */
850 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
857 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
863 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
866 my_setenv(MgPV(mg,n_a),Nullch);
871 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
874 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
880 magic_clear_all_env(sv,mg);
881 hv_iterinit((HV*)sv);
882 while (entry = hv_iternext((HV*)sv)) {
884 my_setenv(hv_iterkey(entry, &keylen),
885 SvPV(hv_iterval((HV*)sv, entry), n_a));
893 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
896 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
898 # ifdef PERL_IMPLICIT_SYS
902 char *envv = GetEnvironmentStrings();
906 char *end = strchr(cur,'=');
907 if (end && end != cur) {
909 my_setenv(cur,Nullch);
911 cur = end + strlen(end+1)+2;
913 else if ((len = strlen(cur)))
916 FreeEnvironmentStrings(envv);
920 for (i = 0; environ[i]; i++)
921 safesysfree(environ[i]);
923 # ifndef PERL_USE_SAFE_PUTENV
926 if (environ == PL_origenviron)
927 environ = (char**)safesysmalloc(sizeof(char*));
929 for (i = 0; environ[i]; i++)
930 safesysfree(environ[i]);
931 # endif /* PERL_USE_SAFE_PUTENV */
932 # endif /* __CYGWIN__ */
937 # endif /* PERL_IMPLICIT_SYS */
943 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
947 /* Are we fetching a signal entry? */
948 i = whichsig(MgPV(mg,n_a));
951 sv_setsv(sv,PL_psig_ptr[i]);
953 Sighandler_t sigstate = rsignal_state(i);
955 /* cache state so we don't fetch it again */
956 if(sigstate == SIG_IGN)
957 sv_setpv(sv,"IGNORE");
959 sv_setsv(sv,&PL_sv_undef);
960 PL_psig_ptr[i] = SvREFCNT_inc(sv);
967 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
971 /* Are we clearing a signal entry? */
972 i = whichsig(MgPV(mg,n_a));
975 SvREFCNT_dec(PL_psig_ptr[i]);
978 if(PL_psig_name[i]) {
979 SvREFCNT_dec(PL_psig_name[i]);
987 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
997 if (strEQ(s,"__DIE__"))
999 else if (strEQ(s,"__WARN__"))
1002 Perl_croak(aTHX_ "No such hook: %s", s);
1010 i = whichsig(s); /* ...no, a brick */
1012 if (ckWARN(WARN_SIGNAL))
1013 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1016 SvREFCNT_dec(PL_psig_name[i]);
1017 SvREFCNT_dec(PL_psig_ptr[i]);
1018 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1019 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1020 PL_psig_name[i] = newSVpvn(s, len);
1021 SvREADONLY_on(PL_psig_name[i]);
1023 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1025 (void)rsignal(i, PL_sighandlerp);
1027 *svp = SvREFCNT_inc(sv);
1030 s = SvPV_force(sv,len);
1031 if (strEQ(s,"IGNORE")) {
1033 (void)rsignal(i, SIG_IGN);
1037 else if (strEQ(s,"DEFAULT") || !*s) {
1039 (void)rsignal(i, SIG_DFL);
1045 * We should warn if HINT_STRICT_REFS, but without
1046 * access to a known hint bit in a known OP, we can't
1047 * tell whether HINT_STRICT_REFS is in force or not.
1049 if (!strchr(s,':') && !strchr(s,'\''))
1050 sv_insert(sv, 0, 0, "main::", 6);
1052 (void)rsignal(i, PL_sighandlerp);
1054 *svp = SvREFCNT_inc(sv);
1060 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1062 PL_sub_generation++;
1067 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1069 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1070 PL_amagic_generation++;
1076 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1078 HV *hv = (HV*)LvTARG(sv);
1083 (void) hv_iterinit(hv);
1084 if (! SvTIED_mg((SV*)hv, 'P'))
1088 while (entry = hv_iternext(hv)) {
1094 sv_setiv(sv, (IV)i);
1099 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1102 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1107 /* caller is responsible for stack switching/cleanup */
1109 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1115 PUSHs(SvTIED_obj(sv, mg));
1118 if (mg->mg_len >= 0)
1119 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1120 else if (mg->mg_len == HEf_SVKEY)
1121 PUSHs((SV*)mg->mg_ptr);
1123 else if (mg->mg_type == 'p') {
1124 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1132 return call_method(meth, flags);
1136 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1142 PUSHSTACKi(PERLSI_MAGIC);
1144 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1145 sv_setsv(sv, *PL_stack_sp--);
1155 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1157 magic_methpack(sv,mg,"FETCH");
1159 mg->mg_flags |= MGf_GSKIP;
1164 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1168 PUSHSTACKi(PERLSI_MAGIC);
1169 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1176 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1178 return magic_methpack(sv,mg,"DELETE");
1183 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1190 PUSHSTACKi(PERLSI_MAGIC);
1191 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1192 sv = *PL_stack_sp--;
1193 retval = (U32) SvIV(sv)-1;
1202 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1207 PUSHSTACKi(PERLSI_MAGIC);
1209 XPUSHs(SvTIED_obj(sv, mg));
1211 call_method("CLEAR", G_SCALAR|G_DISCARD);
1218 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1221 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1225 PUSHSTACKi(PERLSI_MAGIC);
1228 PUSHs(SvTIED_obj(sv, mg));
1233 if (call_method(meth, G_SCALAR))
1234 sv_setsv(key, *PL_stack_sp--);
1243 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1245 return magic_methpack(sv,mg,"EXISTS");
1249 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1260 svp = av_fetch(GvAV(gv),
1261 atoi(MgPV(mg,n_a)), FALSE);
1262 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1264 else if (ckWARN_d(WARN_INTERNAL))
1265 Perl_warner(aTHX_ WARN_INTERNAL, "Can't break at that line\n");
1270 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1273 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1278 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1281 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1286 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1288 SV* lsv = LvTARG(sv);
1290 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1291 mg = mg_find(lsv, 'g');
1292 if (mg && mg->mg_len >= 0) {
1296 sv_pos_b2u(lsv, &i);
1297 sv_setiv(sv, i + PL_curcop->cop_arybase);
1306 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1308 SV* lsv = LvTARG(sv);
1316 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1317 mg = mg_find(lsv, 'g');
1321 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1322 mg = mg_find(lsv, 'g');
1324 else if (!SvOK(sv)) {
1328 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1330 pos = SvIV(sv) - PL_curcop->cop_arybase;
1333 ulen = sv_len_utf8(lsv);
1348 sv_pos_u2b(lsv, &p, 0);
1353 mg->mg_flags &= ~MGf_MINMATCH;
1359 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1361 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1363 gv_efullname3(sv,((GV*)sv), "*");
1367 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1372 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1381 if (*s == '*' && s[1])
1383 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1388 GvGP(sv) = gp_ref(GvGP(gv));
1393 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1396 SV *lsv = LvTARG(sv);
1397 char *tmps = SvPV(lsv,len);
1398 I32 offs = LvTARGOFF(sv);
1399 I32 rem = LvTARGLEN(sv);
1403 if (rem + offs > len)
1405 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1410 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1413 char *tmps = SvPV(sv,len);
1414 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1419 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1422 TAINT_IF((mg->mg_len & 1) ||
1423 (mg->mg_len & 2) && mg->mg_obj == sv); /* kludge */
1428 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1431 if (PL_localizing) {
1432 if (PL_localizing == 1)
1437 else if (PL_tainted)
1445 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1447 SV *lsv = LvTARG(sv);
1454 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1459 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1461 do_vecset(sv); /* XXX slurp this routine */
1466 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1469 if (LvTARGLEN(sv)) {
1471 SV *ahv = LvTARG(sv);
1472 if (SvTYPE(ahv) == SVt_PVHV) {
1473 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1478 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1484 AV* av = (AV*)LvTARG(sv);
1485 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1486 targ = AvARRAY(av)[LvTARGOFF(sv)];
1488 if (targ && targ != &PL_sv_undef) {
1489 dTHR; /* just for SvREFCNT_dec */
1490 /* somebody else defined it for us */
1491 SvREFCNT_dec(LvTARG(sv));
1492 LvTARG(sv) = SvREFCNT_inc(targ);
1494 SvREFCNT_dec(mg->mg_obj);
1495 mg->mg_obj = Nullsv;
1496 mg->mg_flags &= ~MGf_REFCOUNTED;
1501 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1506 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1511 sv_setsv(LvTARG(sv), sv);
1512 SvSETMAGIC(LvTARG(sv));
1518 Perl_vivify_defelem(pTHX_ SV *sv)
1520 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1524 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1527 SV *ahv = LvTARG(sv);
1529 if (SvTYPE(ahv) == SVt_PVHV) {
1530 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1535 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1539 if (!value || value == &PL_sv_undef)
1540 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1543 AV* av = (AV*)LvTARG(sv);
1544 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1545 LvTARG(sv) = Nullsv; /* array can't be extended */
1547 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1548 if (!svp || (value = *svp) == &PL_sv_undef)
1549 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1552 (void)SvREFCNT_inc(value);
1553 SvREFCNT_dec(LvTARG(sv));
1556 SvREFCNT_dec(mg->mg_obj);
1557 mg->mg_obj = Nullsv;
1558 mg->mg_flags &= ~MGf_REFCOUNTED;
1562 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1564 AV *av = (AV*)mg->mg_obj;
1565 SV **svp = AvARRAY(av);
1566 I32 i = AvFILLp(av);
1568 if (svp[i] && svp[i] != &PL_sv_undef) {
1569 if (!SvWEAKREF(svp[i]))
1570 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1571 /* XXX Should we check that it hasn't changed? */
1574 SvWEAKREF_off(svp[i]);
1575 svp[i] = &PL_sv_undef;
1583 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1591 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1593 sv_unmagic(sv, 'B');
1599 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1601 sv_unmagic(sv, 'f');
1607 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1609 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1611 if (uf && uf->uf_set)
1612 (*uf->uf_set)(uf->uf_index, sv);
1617 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1619 regexp *re = (regexp *)mg->mg_obj;
1624 #ifdef USE_LOCALE_COLLATE
1626 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1629 * RenE<eacute> Descartes said "I think not."
1630 * and vanished with a faint plop.
1633 Safefree(mg->mg_ptr);
1639 #endif /* USE_LOCALE_COLLATE */
1642 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1648 switch (*mg->mg_ptr) {
1649 case '\001': /* ^A */
1650 sv_setsv(PL_bodytarget, sv);
1652 case '\003': /* ^C */
1653 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1656 case '\004': /* ^D */
1657 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1658 DEBUG_x(dump_all());
1660 case '\005': /* ^E */
1661 #ifdef MACOS_TRADITIONAL
1662 gLastMacOSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1665 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1668 SetLastError( SvIV(sv) );
1671 /* will anyone ever use this? */
1672 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1678 case '\006': /* ^F */
1679 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1681 case '\010': /* ^H */
1682 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1684 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1686 Safefree(PL_inplace);
1688 PL_inplace = savepv(SvPV(sv,len));
1690 PL_inplace = Nullch;
1692 case '\017': /* ^O */
1694 Safefree(PL_osname);
1696 PL_osname = savepv(SvPV(sv,len));
1700 case '\020': /* ^P */
1701 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1702 if (PL_perldb && !PL_DBsingle)
1705 case '\024': /* ^T */
1707 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1709 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1712 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1713 if (*(mg->mg_ptr+1) == '\0') {
1714 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1715 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1716 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1717 | (i ? G_WARN_ON : G_WARN_OFF) ;
1720 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1721 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1722 if (!SvPOK(sv) && PL_localizing) {
1723 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1724 PL_compiling.cop_warnings = WARN_NONE;
1727 if (memEQ(SvPVX(sv), WARN_ALLstring, WARNsize)) {
1728 PL_compiling.cop_warnings = WARN_ALL;
1729 PL_dowarn |= G_WARN_ONCE ;
1731 else if (memEQ(SvPVX(sv), WARN_NONEstring, WARNsize))
1732 PL_compiling.cop_warnings = WARN_NONE;
1734 if (specialWARN(PL_compiling.cop_warnings))
1735 PL_compiling.cop_warnings = newSVsv(sv) ;
1737 sv_setsv(PL_compiling.cop_warnings, sv);
1738 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1739 PL_dowarn |= G_WARN_ONCE ;
1743 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1744 PL_widesyscalls = SvTRUE(sv);
1747 if (PL_localizing) {
1748 if (PL_localizing == 1)
1749 SAVESPTR(PL_last_in_gv);
1751 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1752 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1755 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1756 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1757 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1760 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1761 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1762 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1765 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1768 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1769 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1770 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1773 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1777 IO *io = GvIOp(PL_defoutgv);
1778 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1779 IoFLAGS(io) &= ~IOf_FLUSH;
1781 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1782 PerlIO *ofp = IoOFP(io);
1784 (void)PerlIO_flush(ofp);
1785 IoFLAGS(io) |= IOf_FLUSH;
1791 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1792 PL_multiline = (i != 0);
1795 SvREFCNT_dec(PL_nrs);
1796 PL_nrs = newSVsv(sv);
1797 SvREFCNT_dec(PL_rs);
1798 PL_rs = SvREFCNT_inc(PL_nrs);
1803 if (SvOK(sv) || SvGMAGICAL(sv)) {
1804 s = SvPV(sv,PL_orslen);
1805 PL_ors = savepvn(s,PL_orslen);
1815 PL_ofs = savepv(SvPV(sv, PL_ofslen));
1820 PL_ofmt = savepv(SvPV(sv,len));
1823 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1826 #ifdef COMPLEX_STATUS
1827 if (PL_localizing == 2) {
1828 PL_statusvalue = LvTARGOFF(sv);
1829 PL_statusvalue_vms = LvTARGLEN(sv);
1833 #ifdef VMSISH_STATUS
1835 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1838 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1841 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1842 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1845 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1846 if (PL_delaymagic) {
1847 PL_delaymagic |= DM_RUID;
1848 break; /* don't do magic till later */
1851 (void)setruid((Uid_t)PL_uid);
1854 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1856 #ifdef HAS_SETRESUID
1857 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1859 if (PL_uid == PL_euid) /* special case $< = $> */
1860 (void)PerlProc_setuid(PL_uid);
1862 PL_uid = PerlProc_getuid();
1863 Perl_croak(aTHX_ "setruid() not implemented");
1868 PL_uid = PerlProc_getuid();
1869 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1872 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1873 if (PL_delaymagic) {
1874 PL_delaymagic |= DM_EUID;
1875 break; /* don't do magic till later */
1878 (void)seteuid((Uid_t)PL_euid);
1881 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1883 #ifdef HAS_SETRESUID
1884 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1886 if (PL_euid == PL_uid) /* special case $> = $< */
1887 PerlProc_setuid(PL_euid);
1889 PL_euid = PerlProc_geteuid();
1890 Perl_croak(aTHX_ "seteuid() not implemented");
1895 PL_euid = PerlProc_geteuid();
1896 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1899 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1900 if (PL_delaymagic) {
1901 PL_delaymagic |= DM_RGID;
1902 break; /* don't do magic till later */
1905 (void)setrgid((Gid_t)PL_gid);
1908 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1910 #ifdef HAS_SETRESGID
1911 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1913 if (PL_gid == PL_egid) /* special case $( = $) */
1914 (void)PerlProc_setgid(PL_gid);
1916 PL_gid = PerlProc_getgid();
1917 Perl_croak(aTHX_ "setrgid() not implemented");
1922 PL_gid = PerlProc_getgid();
1923 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1926 #ifdef HAS_SETGROUPS
1928 char *p = SvPV(sv, len);
1929 Groups_t gary[NGROUPS];
1934 for (i = 0; i < NGROUPS; ++i) {
1935 while (*p && !isSPACE(*p))
1944 (void)setgroups(i, gary);
1946 #else /* HAS_SETGROUPS */
1947 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1948 #endif /* HAS_SETGROUPS */
1949 if (PL_delaymagic) {
1950 PL_delaymagic |= DM_EGID;
1951 break; /* don't do magic till later */
1954 (void)setegid((Gid_t)PL_egid);
1957 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
1959 #ifdef HAS_SETRESGID
1960 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
1962 if (PL_egid == PL_gid) /* special case $) = $( */
1963 (void)PerlProc_setgid(PL_egid);
1965 PL_egid = PerlProc_getegid();
1966 Perl_croak(aTHX_ "setegid() not implemented");
1971 PL_egid = PerlProc_getegid();
1972 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1975 PL_chopset = SvPV_force(sv,len);
1977 #ifndef MACOS_TRADITIONAL
1982 /* See if all the arguments are contiguous in memory */
1983 for (i = 1; i < PL_origargc; i++) {
1984 if (PL_origargv[i] == s + 1
1986 || PL_origargv[i] == s + 2
1991 s += strlen(s); /* this one is ok too */
1996 /* can grab env area too? */
1997 if (PL_origenviron && (PL_origenviron[0] == s + 1
1999 || (PL_origenviron[0] == s + 9 && (s += 8))
2002 my_setenv("NoNe SuCh", Nullch);
2003 /* force copy of environment */
2004 for (i = 0; PL_origenviron[i]; i++)
2005 if (PL_origenviron[i] == s + 1) {
2012 PL_origalen = s - PL_origargv[0];
2014 s = SvPV_force(sv,len);
2016 if (i >= PL_origalen) {
2018 /* don't allow system to limit $0 seen by script */
2019 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2020 Copy(s, PL_origargv[0], i, char);
2021 s = PL_origargv[0]+i;
2025 Copy(s, PL_origargv[0], i, char);
2026 s = PL_origargv[0]+i;
2028 while (++i < PL_origalen)
2030 s = PL_origargv[0]+i;
2031 for (i = 1; i < PL_origargc; i++)
2032 PL_origargv[i] = Nullch;
2038 sv_setsv(thr->errsv, sv);
2040 #endif /* USE_THREADS */
2047 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2050 DEBUG_S(PerlIO_printf(Perl_debug_log,
2051 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2052 PTR2UV(thr), PTR2UV(sv));)
2054 Perl_croak(aTHX_ "panic: magic_mutexfree");
2055 MUTEX_DESTROY(MgMUTEXP(mg));
2056 COND_DESTROY(MgCONDP(mg));
2059 #endif /* USE_THREADS */
2062 Perl_whichsig(pTHX_ char *sig)
2064 register char **sigv;
2066 for (sigv = PL_sig_name+1; *sigv; sigv++)
2067 if (strEQ(sig,*sigv))
2068 return PL_sig_num[sigv - PL_sig_name];
2070 if (strEQ(sig,"CHLD"))
2074 if (strEQ(sig,"CLD"))
2083 Perl_sighandler(int sig)
2089 SV *sv, *tSv = PL_Sv;
2093 I32 o_save_i = PL_savestack_ix, type;
2096 if (PL_savestack_ix + 15 <= PL_savestack_max)
2098 if (PL_markstack_ptr < PL_markstack_max - 2)
2100 if (PL_retstack_ix < PL_retstack_max - 2)
2102 if (PL_scopestack_ix < PL_scopestack_max - 3)
2105 if (!PL_psig_ptr[sig])
2106 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2109 /* Max number of items pushed there is 3*n or 4. We cannot fix
2110 infinity, so we fix 4 (in fact 5): */
2112 PL_savestack_ix += 5; /* Protect save in progress. */
2113 o_save_i = PL_savestack_ix;
2114 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2117 PL_markstack_ptr++; /* Protect mark. */
2120 PL_retstack[PL_retstack_ix] = NULL;
2123 PL_scopestack_ix += 1;
2124 /* sv_2cv is too complicated, try a simpler variant first: */
2125 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2126 || SvTYPE(cv) != SVt_PVCV)
2127 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2129 if (!cv || !CvROOT(cv)) {
2130 if (ckWARN(WARN_SIGNAL))
2131 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2132 PL_sig_name[sig], (gv ? GvENAME(gv)
2139 if(PL_psig_name[sig]) {
2140 sv = SvREFCNT_inc(PL_psig_name[sig]);
2144 sv = sv_newmortal();
2145 sv_setpv(sv,PL_sig_name[sig]);
2148 PUSHSTACKi(PERLSI_SIGNAL);
2153 call_sv((SV*)cv, G_DISCARD);
2158 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2164 PL_scopestack_ix -= 1;
2167 PL_op = myop; /* Apparently not needed... */
2169 PL_Sv = tSv; /* Restore global temporaries. */
2180 restore_magic(pTHXo_ void *p)
2183 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2184 SV* sv = mgs->mgs_sv;
2189 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2192 SvFLAGS(sv) |= mgs->mgs_flags;
2196 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2199 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2201 /* If we're still on top of the stack, pop us off. (That condition
2202 * will be satisfied if restore_magic was called explicitly, but *not*
2203 * if it's being called via leave_scope.)
2204 * The reason for doing this is that otherwise, things like sv_2cv()
2205 * may leave alloc gunk on the savestack, and some code
2206 * (e.g. sighandler) doesn't expect that...
2208 if (PL_savestack_ix == mgs->mgs_ss_ix)
2210 I32 popval = SSPOPINT;
2211 assert(popval == SAVEt_DESTRUCTOR_X);
2212 PL_savestack_ix -= 2;
2214 assert(popval == SAVEt_ALLOC);
2216 PL_savestack_ix -= popval;
2222 unwind_handler_stack(pTHXo_ void *p)
2225 U32 flags = *(U32*)p;
2228 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2229 /* cxstack_ix-- Not needed, die already unwound it. */
2231 SvREFCNT_dec(sig_sv);