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) :
296 (mg->mg_type == 'D' && mg->mg_obj) ? sv : mg->mg_obj,
297 toLOWER(mg->mg_type), key, klen);
307 Free any magic storage used by the SV. See C<sv_magic>.
313 Perl_mg_free(pTHX_ SV *sv)
317 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
318 MGVTBL* vtbl = mg->mg_virtual;
319 moremagic = mg->mg_moremagic;
320 if (vtbl && vtbl->svt_free)
321 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
322 if (mg->mg_ptr && mg->mg_type != 'g')
324 Safefree(mg->mg_ptr);
325 else if (mg->mg_len == HEf_SVKEY)
326 SvREFCNT_dec((SV*)mg->mg_ptr);
327 if (mg->mg_flags & MGf_REFCOUNTED)
328 SvREFCNT_dec(mg->mg_obj);
335 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
340 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
345 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
346 if (mg->mg_obj) /* @+ */
349 return rx->lastparen;
356 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
365 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
369 if (paren <= rx->nparens &&
370 (s = rx->startp[paren]) != -1 &&
371 (t = rx->endp[paren]) != -1)
373 if (mg->mg_obj) /* @+ */
384 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
387 Perl_croak(aTHX_ PL_no_modify);
393 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
401 switch (*mg->mg_ptr) {
402 case '1': case '2': case '3': case '4':
403 case '5': case '6': case '7': case '8': case '9': case '&':
404 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
406 paren = atoi(mg->mg_ptr);
408 if (paren <= rx->nparens &&
409 (s1 = rx->startp[paren]) != -1 &&
410 (t1 = rx->endp[paren]) != -1)
414 if (i > 0 && (PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE) {
415 char *s = rx->subbeg + s1;
416 char *send = rx->subbeg + t1;
429 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
430 paren = rx->lastparen;
436 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
437 if (rx->startp[0] != -1) {
448 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
449 if (rx->endp[0] != -1) {
450 i = rx->sublen - rx->endp[0];
460 return (STRLEN)PL_ofslen;
462 return (STRLEN)PL_orslen;
465 if (!SvPOK(sv) && SvNIOK(sv)) {
475 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
483 switch (*mg->mg_ptr) {
484 case '\001': /* ^A */
485 sv_setsv(sv, PL_bodytarget);
487 case '\003': /* ^C */
488 sv_setiv(sv, (IV)PL_minus_c);
491 case '\004': /* ^D */
492 sv_setiv(sv, (IV)(PL_debug & 32767));
493 #if defined(YYDEBUG) && defined(DEBUGGING)
494 PL_yydebug = (PL_debug & 1);
497 case '\005': /* ^E */
498 #ifdef MACOS_TRADITIONAL
502 sv_setnv(sv,(double)gMacPerl_OSErr);
503 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
508 # include <descrip.h>
509 # include <starlet.h>
511 $DESCRIPTOR(msgdsc,msg);
512 sv_setnv(sv,(NV) vaxc$errno);
513 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
514 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
520 if (!(_emx_env & 0x200)) { /* Under DOS */
521 sv_setnv(sv, (NV)errno);
522 sv_setpv(sv, errno ? Strerror(errno) : "");
524 if (errno != errno_isOS2) {
525 int tmp = _syserrno();
526 if (tmp) /* 2nd call to _syserrno() makes it 0 */
529 sv_setnv(sv, (NV)Perl_rc);
530 sv_setpv(sv, os2error(Perl_rc));
535 DWORD dwErr = GetLastError();
536 sv_setnv(sv, (NV)dwErr);
539 PerlProc_GetOSError(sv, dwErr);
546 sv_setnv(sv, (NV)errno);
547 sv_setpv(sv, errno ? Strerror(errno) : "");
552 SvNOK_on(sv); /* what a wonderful hack! */
554 case '\006': /* ^F */
555 sv_setiv(sv, (IV)PL_maxsysfd);
557 case '\010': /* ^H */
558 sv_setiv(sv, (IV)PL_hints);
560 case '\011': /* ^I */ /* NOT \t in EBCDIC */
562 sv_setpv(sv, PL_inplace);
564 sv_setsv(sv, &PL_sv_undef);
566 case '\017': /* ^O */
567 sv_setpv(sv, PL_osname);
569 case '\020': /* ^P */
570 sv_setiv(sv, (IV)PL_perldb);
572 case '\023': /* ^S */
575 if (PL_lex_state != LEX_NOTPARSING)
578 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
581 case '\024': /* ^T */
583 sv_setnv(sv, PL_basetime);
585 sv_setiv(sv, (IV)PL_basetime);
588 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
589 if (*(mg->mg_ptr+1) == '\0')
590 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
591 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
592 if (PL_compiling.cop_warnings == pWARN_NONE ||
593 PL_compiling.cop_warnings == pWARN_STD)
595 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
597 else if (PL_compiling.cop_warnings == pWARN_ALL) {
598 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
601 sv_setsv(sv, PL_compiling.cop_warnings);
605 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
606 sv_setiv(sv, (IV)PL_widesyscalls);
608 case '1': case '2': case '3': case '4':
609 case '5': case '6': case '7': case '8': case '9': case '&':
610 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
614 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
615 * XXX Does the new way break anything?
617 paren = atoi(mg->mg_ptr);
619 if (paren <= rx->nparens &&
620 (s1 = rx->startp[paren]) != -1 &&
621 (t1 = rx->endp[paren]) != -1)
632 was_tainted = PL_tainted;
636 if ((PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE)
641 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
646 sv_setsv(sv,&PL_sv_undef);
649 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
650 paren = rx->lastparen;
654 sv_setsv(sv,&PL_sv_undef);
657 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
658 if ((s = rx->subbeg) && rx->startp[0] != -1) {
663 sv_setsv(sv,&PL_sv_undef);
666 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
667 if (rx->subbeg && rx->endp[0] != -1) {
668 s = rx->subbeg + rx->endp[0];
669 i = rx->sublen - rx->endp[0];
673 sv_setsv(sv,&PL_sv_undef);
677 if (GvIO(PL_last_in_gv)) {
678 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
684 sv_setiv(sv, (IV)STATUS_CURRENT);
685 #ifdef COMPLEX_STATUS
686 LvTARGOFF(sv) = PL_statusvalue;
687 LvTARGLEN(sv) = PL_statusvalue_vms;
692 s = IoTOP_NAME(GvIOp(PL_defoutgv));
696 sv_setpv(sv,GvENAME(PL_defoutgv));
701 s = IoFMT_NAME(GvIOp(PL_defoutgv));
703 s = GvENAME(PL_defoutgv);
708 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
711 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
714 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
722 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
725 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
728 sv_setpvn(sv,PL_ofs,PL_ofslen);
731 sv_setpvn(sv,PL_ors,PL_orslen);
734 sv_setpv(sv,PL_ofmt);
738 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
739 sv_setpv(sv, errno ? Strerror(errno) : "");
742 int saveerrno = errno;
743 sv_setnv(sv, (NV)errno);
745 if (errno == errno_isOS2 || errno == errno_isOS2_set)
746 sv_setpv(sv, os2error(Perl_rc));
749 sv_setpv(sv, errno ? Strerror(errno) : "");
753 SvNOK_on(sv); /* what a wonderful hack! */
756 sv_setiv(sv, (IV)PL_uid);
759 sv_setiv(sv, (IV)PL_euid);
762 sv_setiv(sv, (IV)PL_gid);
764 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
768 sv_setiv(sv, (IV)PL_egid);
770 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
775 Groups_t gary[NGROUPS];
776 i = getgroups(NGROUPS,gary);
778 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
781 (void)SvIOK_on(sv); /* what a wonderful hack! */
785 #ifndef MACOS_TRADITIONAL
791 sv_setsv(sv, thr->errsv);
793 #endif /* USE_THREADS */
799 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
801 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
803 if (uf && uf->uf_val)
804 (*uf->uf_val)(uf->uf_index, sv);
809 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
820 #ifdef DYNAMIC_ENV_FETCH
821 /* We just undefd an environment var. Is a replacement */
822 /* waiting in the wings? */
825 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
826 s = SvPV(*valp, len);
830 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
831 /* And you'll never guess what the dog had */
832 /* in its mouth... */
834 MgTAINTEDDIR_off(mg);
836 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
837 char pathbuf[256], eltbuf[256], *cp, *elt = s;
841 do { /* DCL$PATH may be a search list */
842 while (1) { /* as may dev portion of any element */
843 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
844 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
845 cando_by_name(S_IWUSR,0,elt) ) {
850 if ((cp = strchr(elt, ':')) != Nullch)
852 if (my_trnlnm(elt, eltbuf, j++))
858 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
861 if (s && klen == 4 && strEQ(ptr,"PATH")) {
862 char *strend = s + len;
867 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
870 if (i >= sizeof tmpbuf /* too long -- assume the worst */
872 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
879 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
885 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
888 my_setenv(MgPV(mg,n_a),Nullch);
893 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
896 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
902 magic_clear_all_env(sv,mg);
903 hv_iterinit((HV*)sv);
904 while ((entry = hv_iternext((HV*)sv))) {
906 my_setenv(hv_iterkey(entry, &keylen),
907 SvPV(hv_iterval((HV*)sv, entry), n_a));
915 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
918 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
920 # ifdef PERL_IMPLICIT_SYS
924 char *envv = GetEnvironmentStrings();
928 char *end = strchr(cur,'=');
929 if (end && end != cur) {
931 my_setenv(cur,Nullch);
933 cur = end + strlen(end+1)+2;
935 else if ((len = strlen(cur)))
938 FreeEnvironmentStrings(envv);
942 for (i = 0; environ[i]; i++)
943 safesysfree(environ[i]);
945 # ifndef PERL_USE_SAFE_PUTENV
948 if (environ == PL_origenviron)
949 environ = (char**)safesysmalloc(sizeof(char*));
951 for (i = 0; environ[i]; i++)
952 safesysfree(environ[i]);
953 # endif /* PERL_USE_SAFE_PUTENV */
954 # endif /* __CYGWIN__ */
959 # endif /* PERL_IMPLICIT_SYS */
966 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
970 /* Are we fetching a signal entry? */
971 i = whichsig(MgPV(mg,n_a));
974 sv_setsv(sv,PL_psig_ptr[i]);
976 Sighandler_t sigstate = rsignal_state(i);
978 /* cache state so we don't fetch it again */
979 if(sigstate == SIG_IGN)
980 sv_setpv(sv,"IGNORE");
982 sv_setsv(sv,&PL_sv_undef);
983 PL_psig_ptr[i] = SvREFCNT_inc(sv);
990 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
994 /* Are we clearing a signal entry? */
995 i = whichsig(MgPV(mg,n_a));
998 SvREFCNT_dec(PL_psig_ptr[i]);
1001 if(PL_psig_name[i]) {
1002 SvREFCNT_dec(PL_psig_name[i]);
1010 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1020 if (strEQ(s,"__DIE__"))
1022 else if (strEQ(s,"__WARN__"))
1025 Perl_croak(aTHX_ "No such hook: %s", s);
1033 i = whichsig(s); /* ...no, a brick */
1035 if (ckWARN(WARN_SIGNAL))
1036 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1039 SvREFCNT_dec(PL_psig_name[i]);
1040 SvREFCNT_dec(PL_psig_ptr[i]);
1041 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1042 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1043 PL_psig_name[i] = newSVpvn(s, len);
1044 SvREADONLY_on(PL_psig_name[i]);
1046 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1048 (void)rsignal(i, PL_sighandlerp);
1050 *svp = SvREFCNT_inc(sv);
1053 s = SvPV_force(sv,len);
1054 if (strEQ(s,"IGNORE")) {
1056 (void)rsignal(i, SIG_IGN);
1060 else if (strEQ(s,"DEFAULT") || !*s) {
1062 (void)rsignal(i, SIG_DFL);
1068 * We should warn if HINT_STRICT_REFS, but without
1069 * access to a known hint bit in a known OP, we can't
1070 * tell whether HINT_STRICT_REFS is in force or not.
1072 if (!strchr(s,':') && !strchr(s,'\''))
1073 sv_insert(sv, 0, 0, "main::", 6);
1075 (void)rsignal(i, PL_sighandlerp);
1077 *svp = SvREFCNT_inc(sv);
1081 #endif /* !PERL_MICRO */
1084 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1086 PL_sub_generation++;
1091 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1093 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1094 PL_amagic_generation++;
1100 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1102 HV *hv = (HV*)LvTARG(sv);
1107 (void) hv_iterinit(hv);
1108 if (! SvTIED_mg((SV*)hv, 'P'))
1112 while ((entry = hv_iternext(hv))) {
1118 sv_setiv(sv, (IV)i);
1123 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1126 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1131 /* caller is responsible for stack switching/cleanup */
1133 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1139 PUSHs(SvTIED_obj(sv, mg));
1142 if (mg->mg_len >= 0)
1143 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1144 else if (mg->mg_len == HEf_SVKEY)
1145 PUSHs((SV*)mg->mg_ptr);
1147 else if (mg->mg_type == 'p') {
1148 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1156 return call_method(meth, flags);
1160 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1166 PUSHSTACKi(PERLSI_MAGIC);
1168 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1169 sv_setsv(sv, *PL_stack_sp--);
1179 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1181 magic_methpack(sv,mg,"FETCH");
1183 mg->mg_flags |= MGf_GSKIP;
1188 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1192 PUSHSTACKi(PERLSI_MAGIC);
1193 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1200 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1202 return magic_methpack(sv,mg,"DELETE");
1207 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1214 PUSHSTACKi(PERLSI_MAGIC);
1215 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1216 sv = *PL_stack_sp--;
1217 retval = (U32) SvIV(sv)-1;
1226 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1231 PUSHSTACKi(PERLSI_MAGIC);
1233 XPUSHs(SvTIED_obj(sv, mg));
1235 call_method("CLEAR", G_SCALAR|G_DISCARD);
1242 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1245 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1249 PUSHSTACKi(PERLSI_MAGIC);
1252 PUSHs(SvTIED_obj(sv, mg));
1257 if (call_method(meth, G_SCALAR))
1258 sv_setsv(key, *PL_stack_sp--);
1267 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1269 return magic_methpack(sv,mg,"EXISTS");
1273 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1284 svp = av_fetch(GvAV(gv),
1285 atoi(MgPV(mg,n_a)), FALSE);
1286 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1292 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1295 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1300 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1303 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1308 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1310 SV* lsv = LvTARG(sv);
1312 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1313 mg = mg_find(lsv, 'g');
1314 if (mg && mg->mg_len >= 0) {
1318 sv_pos_b2u(lsv, &i);
1319 sv_setiv(sv, i + PL_curcop->cop_arybase);
1328 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1330 SV* lsv = LvTARG(sv);
1338 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1339 mg = mg_find(lsv, 'g');
1343 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1344 mg = mg_find(lsv, 'g');
1346 else if (!SvOK(sv)) {
1350 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1352 pos = SvIV(sv) - PL_curcop->cop_arybase;
1355 ulen = sv_len_utf8(lsv);
1370 sv_pos_u2b(lsv, &p, 0);
1375 mg->mg_flags &= ~MGf_MINMATCH;
1381 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1383 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1385 gv_efullname3(sv,((GV*)sv), "*");
1389 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1394 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1403 if (*s == '*' && s[1])
1405 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1410 GvGP(sv) = gp_ref(GvGP(gv));
1415 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1418 SV *lsv = LvTARG(sv);
1419 char *tmps = SvPV(lsv,len);
1420 I32 offs = LvTARGOFF(sv);
1421 I32 rem = LvTARGLEN(sv);
1425 if (rem + offs > len)
1427 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1432 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1435 char *tmps = SvPV(sv,len);
1436 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1441 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1444 TAINT_IF((mg->mg_len & 1) ||
1445 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1450 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1453 if (PL_localizing) {
1454 if (PL_localizing == 1)
1459 else if (PL_tainted)
1467 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1469 SV *lsv = LvTARG(sv);
1476 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1481 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1483 do_vecset(sv); /* XXX slurp this routine */
1488 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1491 if (LvTARGLEN(sv)) {
1493 SV *ahv = LvTARG(sv);
1494 if (SvTYPE(ahv) == SVt_PVHV) {
1495 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1500 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1506 AV* av = (AV*)LvTARG(sv);
1507 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1508 targ = AvARRAY(av)[LvTARGOFF(sv)];
1510 if (targ && targ != &PL_sv_undef) {
1511 dTHR; /* just for SvREFCNT_dec */
1512 /* somebody else defined it for us */
1513 SvREFCNT_dec(LvTARG(sv));
1514 LvTARG(sv) = SvREFCNT_inc(targ);
1516 SvREFCNT_dec(mg->mg_obj);
1517 mg->mg_obj = Nullsv;
1518 mg->mg_flags &= ~MGf_REFCOUNTED;
1523 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1528 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1533 sv_setsv(LvTARG(sv), sv);
1534 SvSETMAGIC(LvTARG(sv));
1540 Perl_vivify_defelem(pTHX_ SV *sv)
1542 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1546 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1549 SV *ahv = LvTARG(sv);
1551 if (SvTYPE(ahv) == SVt_PVHV) {
1552 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1557 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1561 if (!value || value == &PL_sv_undef)
1562 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1565 AV* av = (AV*)LvTARG(sv);
1566 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1567 LvTARG(sv) = Nullsv; /* array can't be extended */
1569 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1570 if (!svp || (value = *svp) == &PL_sv_undef)
1571 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1574 (void)SvREFCNT_inc(value);
1575 SvREFCNT_dec(LvTARG(sv));
1578 SvREFCNT_dec(mg->mg_obj);
1579 mg->mg_obj = Nullsv;
1580 mg->mg_flags &= ~MGf_REFCOUNTED;
1584 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1586 AV *av = (AV*)mg->mg_obj;
1587 SV **svp = AvARRAY(av);
1588 I32 i = AvFILLp(av);
1590 if (svp[i] && svp[i] != &PL_sv_undef) {
1591 if (!SvWEAKREF(svp[i]))
1592 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1593 /* XXX Should we check that it hasn't changed? */
1595 (void)SvOK_off(svp[i]);
1596 SvWEAKREF_off(svp[i]);
1597 svp[i] = &PL_sv_undef;
1605 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1613 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1615 sv_unmagic(sv, 'B');
1621 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1623 sv_unmagic(sv, 'f');
1629 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1631 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1633 if (uf && uf->uf_set)
1634 (*uf->uf_set)(uf->uf_index, sv);
1639 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1641 regexp *re = (regexp *)mg->mg_obj;
1646 #ifdef USE_LOCALE_COLLATE
1648 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1651 * RenE<eacute> Descartes said "I think not."
1652 * and vanished with a faint plop.
1655 Safefree(mg->mg_ptr);
1661 #endif /* USE_LOCALE_COLLATE */
1664 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1670 switch (*mg->mg_ptr) {
1671 case '\001': /* ^A */
1672 sv_setsv(PL_bodytarget, sv);
1674 case '\003': /* ^C */
1675 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1678 case '\004': /* ^D */
1679 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1680 DEBUG_x(dump_all());
1682 case '\005': /* ^E */
1683 #ifdef MACOS_TRADITIONAL
1684 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1687 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1690 SetLastError( SvIV(sv) );
1693 /* will anyone ever use this? */
1694 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1700 case '\006': /* ^F */
1701 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1703 case '\010': /* ^H */
1704 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1706 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1708 Safefree(PL_inplace);
1710 PL_inplace = savepv(SvPV(sv,len));
1712 PL_inplace = Nullch;
1714 case '\017': /* ^O */
1716 Safefree(PL_osname);
1718 PL_osname = savepv(SvPV(sv,len));
1722 case '\020': /* ^P */
1723 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1724 if (PL_perldb && !PL_DBsingle)
1727 case '\024': /* ^T */
1729 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1731 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1734 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1735 if (*(mg->mg_ptr+1) == '\0') {
1736 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1737 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1738 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1739 | (i ? G_WARN_ON : G_WARN_OFF) ;
1742 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1743 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1744 if (!SvPOK(sv) && PL_localizing) {
1745 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1746 PL_compiling.cop_warnings = pWARN_NONE;
1751 int accumulate = 0 ;
1752 int any_fatals = 0 ;
1753 char * ptr = (char*)SvPV(sv, len) ;
1754 for (i = 0 ; i < len ; ++i) {
1755 accumulate |= ptr[i] ;
1756 any_fatals |= (ptr[i] & 0xAA) ;
1759 PL_compiling.cop_warnings = pWARN_NONE;
1760 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1761 PL_compiling.cop_warnings = pWARN_ALL;
1762 PL_dowarn |= G_WARN_ONCE ;
1765 if (specialWARN(PL_compiling.cop_warnings))
1766 PL_compiling.cop_warnings = newSVsv(sv) ;
1768 sv_setsv(PL_compiling.cop_warnings, sv);
1769 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1770 PL_dowarn |= G_WARN_ONCE ;
1776 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1777 PL_widesyscalls = SvTRUE(sv);
1780 if (PL_localizing) {
1781 if (PL_localizing == 1)
1782 SAVESPTR(PL_last_in_gv);
1784 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1785 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1788 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1789 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1790 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1793 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1794 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1795 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1798 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1801 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1802 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1803 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1806 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1810 IO *io = GvIOp(PL_defoutgv);
1811 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1812 IoFLAGS(io) &= ~IOf_FLUSH;
1814 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1815 PerlIO *ofp = IoOFP(io);
1817 (void)PerlIO_flush(ofp);
1818 IoFLAGS(io) |= IOf_FLUSH;
1824 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1825 PL_multiline = (i != 0);
1828 SvREFCNT_dec(PL_nrs);
1829 PL_nrs = newSVsv(sv);
1830 SvREFCNT_dec(PL_rs);
1831 PL_rs = SvREFCNT_inc(PL_nrs);
1836 if (SvOK(sv) || SvGMAGICAL(sv)) {
1837 s = SvPV(sv,PL_orslen);
1838 PL_ors = savepvn(s,PL_orslen);
1848 PL_ofs = savepv(SvPV(sv, PL_ofslen));
1853 PL_ofmt = savepv(SvPV(sv,len));
1856 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1859 #ifdef COMPLEX_STATUS
1860 if (PL_localizing == 2) {
1861 PL_statusvalue = LvTARGOFF(sv);
1862 PL_statusvalue_vms = LvTARGLEN(sv);
1866 #ifdef VMSISH_STATUS
1868 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1871 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1874 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1875 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1878 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1879 if (PL_delaymagic) {
1880 PL_delaymagic |= DM_RUID;
1881 break; /* don't do magic till later */
1884 (void)setruid((Uid_t)PL_uid);
1887 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1889 #ifdef HAS_SETRESUID
1890 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1892 if (PL_uid == PL_euid) /* special case $< = $> */
1893 (void)PerlProc_setuid(PL_uid);
1895 PL_uid = PerlProc_getuid();
1896 Perl_croak(aTHX_ "setruid() not implemented");
1901 PL_uid = PerlProc_getuid();
1902 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1905 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1906 if (PL_delaymagic) {
1907 PL_delaymagic |= DM_EUID;
1908 break; /* don't do magic till later */
1911 (void)seteuid((Uid_t)PL_euid);
1914 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1916 #ifdef HAS_SETRESUID
1917 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1919 if (PL_euid == PL_uid) /* special case $> = $< */
1920 PerlProc_setuid(PL_euid);
1922 PL_euid = PerlProc_geteuid();
1923 Perl_croak(aTHX_ "seteuid() not implemented");
1928 PL_euid = PerlProc_geteuid();
1929 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1932 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1933 if (PL_delaymagic) {
1934 PL_delaymagic |= DM_RGID;
1935 break; /* don't do magic till later */
1938 (void)setrgid((Gid_t)PL_gid);
1941 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1943 #ifdef HAS_SETRESGID
1944 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1946 if (PL_gid == PL_egid) /* special case $( = $) */
1947 (void)PerlProc_setgid(PL_gid);
1949 PL_gid = PerlProc_getgid();
1950 Perl_croak(aTHX_ "setrgid() not implemented");
1955 PL_gid = PerlProc_getgid();
1956 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1959 #ifdef HAS_SETGROUPS
1961 char *p = SvPV(sv, len);
1962 Groups_t gary[NGROUPS];
1967 for (i = 0; i < NGROUPS; ++i) {
1968 while (*p && !isSPACE(*p))
1977 (void)setgroups(i, gary);
1979 #else /* HAS_SETGROUPS */
1980 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1981 #endif /* HAS_SETGROUPS */
1982 if (PL_delaymagic) {
1983 PL_delaymagic |= DM_EGID;
1984 break; /* don't do magic till later */
1987 (void)setegid((Gid_t)PL_egid);
1990 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
1992 #ifdef HAS_SETRESGID
1993 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
1995 if (PL_egid == PL_gid) /* special case $) = $( */
1996 (void)PerlProc_setgid(PL_egid);
1998 PL_egid = PerlProc_getegid();
1999 Perl_croak(aTHX_ "setegid() not implemented");
2004 PL_egid = PerlProc_getegid();
2005 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2008 PL_chopset = SvPV_force(sv,len);
2010 #ifndef MACOS_TRADITIONAL
2012 #ifdef HAS_SETPROCTITLE
2013 /* The BSDs don't show the argv[] in ps(1) output, they
2014 * show a string from the process struct and provide
2015 * the setproctitle() routine to manipulate that. */
2018 # if __FreeBSD_version >= 410001
2019 /* The leading "-" removes the "perl: " prefix,
2020 * but not the "(perl) suffix from the ps(1)
2021 * output, because that's what ps(1) shows if the
2022 * argv[] is modified. */
2023 setproctitle("-%s", s, len + 1);
2024 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2025 /* This doesn't really work if you assume that
2026 * $0 = 'foobar'; will wipe out 'perl' from the $0
2027 * because in ps(1) output the result will be like
2028 * sprintf("perl: %s (perl)", s)
2029 * I guess this is a security feature:
2030 * one (a user process) cannot get rid of the original name.
2032 setproctitle("%s", s);
2039 /* See if all the arguments are contiguous in memory */
2040 for (i = 1; i < PL_origargc; i++) {
2041 if (PL_origargv[i] == s + 1
2043 || PL_origargv[i] == s + 2
2048 s += strlen(s); /* this one is ok too */
2053 /* can grab env area too? */
2054 if (PL_origenviron && (PL_origenviron[0] == s + 1
2056 || (PL_origenviron[0] == s + 9 && (s += 8))
2059 my_setenv("NoNe SuCh", Nullch);
2060 /* force copy of environment */
2061 for (i = 0; PL_origenviron[i]; i++)
2062 if (PL_origenviron[i] == s + 1) {
2069 PL_origalen = s - PL_origargv[0];
2071 s = SvPV_force(sv,len);
2073 if (i >= PL_origalen) {
2075 /* don't allow system to limit $0 seen by script */
2076 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2077 Copy(s, PL_origargv[0], i, char);
2078 s = PL_origargv[0]+i;
2082 Copy(s, PL_origargv[0], i, char);
2083 s = PL_origargv[0]+i;
2085 while (++i < PL_origalen)
2087 s = PL_origargv[0]+i;
2088 for (i = 1; i < PL_origargc; i++)
2089 PL_origargv[i] = Nullch;
2095 sv_setsv(thr->errsv, sv);
2097 #endif /* USE_THREADS */
2104 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2107 DEBUG_S(PerlIO_printf(Perl_debug_log,
2108 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2109 PTR2UV(thr), PTR2UV(sv));)
2111 Perl_croak(aTHX_ "panic: magic_mutexfree");
2112 MUTEX_DESTROY(MgMUTEXP(mg));
2113 COND_DESTROY(MgCONDP(mg));
2116 #endif /* USE_THREADS */
2119 Perl_whichsig(pTHX_ char *sig)
2121 register char **sigv;
2123 for (sigv = PL_sig_name+1; *sigv; sigv++)
2124 if (strEQ(sig,*sigv))
2125 return PL_sig_num[sigv - PL_sig_name];
2127 if (strEQ(sig,"CHLD"))
2131 if (strEQ(sig,"CLD"))
2140 Perl_sighandler(int sig)
2142 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2143 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2150 SV *sv, *tSv = PL_Sv;
2154 I32 o_save_i = PL_savestack_ix;
2157 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2158 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2161 if (PL_savestack_ix + 15 <= PL_savestack_max)
2163 if (PL_markstack_ptr < PL_markstack_max - 2)
2165 if (PL_retstack_ix < PL_retstack_max - 2)
2167 if (PL_scopestack_ix < PL_scopestack_max - 3)
2170 if (!PL_psig_ptr[sig])
2171 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2174 /* Max number of items pushed there is 3*n or 4. We cannot fix
2175 infinity, so we fix 4 (in fact 5): */
2177 PL_savestack_ix += 5; /* Protect save in progress. */
2178 o_save_i = PL_savestack_ix;
2179 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2182 PL_markstack_ptr++; /* Protect mark. */
2185 PL_retstack[PL_retstack_ix] = NULL;
2188 PL_scopestack_ix += 1;
2189 /* sv_2cv is too complicated, try a simpler variant first: */
2190 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2191 || SvTYPE(cv) != SVt_PVCV)
2192 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2194 if (!cv || !CvROOT(cv)) {
2195 if (ckWARN(WARN_SIGNAL))
2196 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2197 PL_sig_name[sig], (gv ? GvENAME(gv)
2204 if(PL_psig_name[sig]) {
2205 sv = SvREFCNT_inc(PL_psig_name[sig]);
2209 sv = sv_newmortal();
2210 sv_setpv(sv,PL_sig_name[sig]);
2213 PUSHSTACKi(PERLSI_SIGNAL);
2218 call_sv((SV*)cv, G_DISCARD);
2223 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2229 PL_scopestack_ix -= 1;
2232 PL_op = myop; /* Apparently not needed... */
2234 PL_Sv = tSv; /* Restore global temporaries. */
2245 restore_magic(pTHXo_ void *p)
2248 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2249 SV* sv = mgs->mgs_sv;
2254 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2257 SvFLAGS(sv) |= mgs->mgs_flags;
2261 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2264 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2266 /* If we're still on top of the stack, pop us off. (That condition
2267 * will be satisfied if restore_magic was called explicitly, but *not*
2268 * if it's being called via leave_scope.)
2269 * The reason for doing this is that otherwise, things like sv_2cv()
2270 * may leave alloc gunk on the savestack, and some code
2271 * (e.g. sighandler) doesn't expect that...
2273 if (PL_savestack_ix == mgs->mgs_ss_ix)
2275 I32 popval = SSPOPINT;
2276 assert(popval == SAVEt_DESTRUCTOR_X);
2277 PL_savestack_ix -= 2;
2279 assert(popval == SAVEt_ALLOC);
2281 PL_savestack_ix -= popval;
2287 unwind_handler_stack(pTHXo_ void *p)
2290 U32 flags = *(U32*)p;
2293 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2294 /* cxstack_ix-- Not needed, die already unwound it. */
2296 SvREFCNT_dec(sig_sv);