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)
583 case '\024': /* ^T */
585 sv_setnv(sv, PL_basetime);
587 sv_setiv(sv, (IV)PL_basetime);
590 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
591 if (*(mg->mg_ptr+1) == '\0')
592 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
593 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
594 if (PL_compiling.cop_warnings == pWARN_NONE ||
595 PL_compiling.cop_warnings == pWARN_STD)
597 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
599 else if (PL_compiling.cop_warnings == pWARN_ALL) {
600 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
603 sv_setsv(sv, PL_compiling.cop_warnings);
607 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
608 sv_setiv(sv, (IV)PL_widesyscalls);
610 case '1': case '2': case '3': case '4':
611 case '5': case '6': case '7': case '8': case '9': case '&':
612 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
616 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
617 * XXX Does the new way break anything?
619 paren = atoi(mg->mg_ptr);
621 if (paren <= rx->nparens &&
622 (s1 = rx->startp[paren]) != -1 &&
623 (t1 = rx->endp[paren]) != -1)
634 was_tainted = PL_tainted;
638 if ((PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE)
643 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
648 sv_setsv(sv,&PL_sv_undef);
651 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
652 paren = rx->lastparen;
656 sv_setsv(sv,&PL_sv_undef);
659 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
660 if ((s = rx->subbeg) && rx->startp[0] != -1) {
665 sv_setsv(sv,&PL_sv_undef);
668 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
669 if (rx->subbeg && rx->endp[0] != -1) {
670 s = rx->subbeg + rx->endp[0];
671 i = rx->sublen - rx->endp[0];
675 sv_setsv(sv,&PL_sv_undef);
679 if (GvIO(PL_last_in_gv)) {
680 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
686 sv_setiv(sv, (IV)STATUS_CURRENT);
687 #ifdef COMPLEX_STATUS
688 LvTARGOFF(sv) = PL_statusvalue;
689 LvTARGLEN(sv) = PL_statusvalue_vms;
694 s = IoTOP_NAME(GvIOp(PL_defoutgv));
698 sv_setpv(sv,GvENAME(PL_defoutgv));
703 s = IoFMT_NAME(GvIOp(PL_defoutgv));
705 s = GvENAME(PL_defoutgv);
710 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
713 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
716 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
724 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
727 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
730 sv_setpvn(sv,PL_ofs,PL_ofslen);
733 sv_setpvn(sv,PL_ors,PL_orslen);
736 sv_setpv(sv,PL_ofmt);
740 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
741 sv_setpv(sv, errno ? Strerror(errno) : "");
744 int saveerrno = errno;
745 sv_setnv(sv, (NV)errno);
747 if (errno == errno_isOS2 || errno == errno_isOS2_set)
748 sv_setpv(sv, os2error(Perl_rc));
751 sv_setpv(sv, errno ? Strerror(errno) : "");
755 SvNOK_on(sv); /* what a wonderful hack! */
758 sv_setiv(sv, (IV)PL_uid);
761 sv_setiv(sv, (IV)PL_euid);
764 sv_setiv(sv, (IV)PL_gid);
766 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
770 sv_setiv(sv, (IV)PL_egid);
772 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
777 Groups_t gary[NGROUPS];
778 i = getgroups(NGROUPS,gary);
780 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
783 (void)SvIOK_on(sv); /* what a wonderful hack! */
787 #ifndef MACOS_TRADITIONAL
793 sv_setsv(sv, thr->errsv);
795 #endif /* USE_THREADS */
801 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
803 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
805 if (uf && uf->uf_val)
806 (*uf->uf_val)(uf->uf_index, sv);
811 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
822 #ifdef DYNAMIC_ENV_FETCH
823 /* We just undefd an environment var. Is a replacement */
824 /* waiting in the wings? */
827 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
828 s = SvPV(*valp, len);
832 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
833 /* And you'll never guess what the dog had */
834 /* in its mouth... */
836 MgTAINTEDDIR_off(mg);
838 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
839 char pathbuf[256], eltbuf[256], *cp, *elt = s;
843 do { /* DCL$PATH may be a search list */
844 while (1) { /* as may dev portion of any element */
845 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
846 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
847 cando_by_name(S_IWUSR,0,elt) ) {
852 if ((cp = strchr(elt, ':')) != Nullch)
854 if (my_trnlnm(elt, eltbuf, j++))
860 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
863 if (s && klen == 4 && strEQ(ptr,"PATH")) {
864 char *strend = s + len;
869 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
872 if (i >= sizeof tmpbuf /* too long -- assume the worst */
874 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
881 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
887 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
890 my_setenv(MgPV(mg,n_a),Nullch);
895 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
898 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
904 magic_clear_all_env(sv,mg);
905 hv_iterinit((HV*)sv);
906 while ((entry = hv_iternext((HV*)sv))) {
908 my_setenv(hv_iterkey(entry, &keylen),
909 SvPV(hv_iterval((HV*)sv, entry), n_a));
917 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
920 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
922 # ifdef PERL_IMPLICIT_SYS
926 char *envv = GetEnvironmentStrings();
930 char *end = strchr(cur,'=');
931 if (end && end != cur) {
933 my_setenv(cur,Nullch);
935 cur = end + strlen(end+1)+2;
937 else if ((len = strlen(cur)))
940 FreeEnvironmentStrings(envv);
944 for (i = 0; environ[i]; i++)
945 safesysfree(environ[i]);
947 # ifndef PERL_USE_SAFE_PUTENV
950 if (environ == PL_origenviron)
951 environ = (char**)safesysmalloc(sizeof(char*));
953 for (i = 0; environ[i]; i++)
954 safesysfree(environ[i]);
955 # endif /* PERL_USE_SAFE_PUTENV */
956 # endif /* __CYGWIN__ */
961 # endif /* PERL_IMPLICIT_SYS */
968 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
972 /* Are we fetching a signal entry? */
973 i = whichsig(MgPV(mg,n_a));
976 sv_setsv(sv,PL_psig_ptr[i]);
978 Sighandler_t sigstate = rsignal_state(i);
980 /* cache state so we don't fetch it again */
981 if(sigstate == SIG_IGN)
982 sv_setpv(sv,"IGNORE");
984 sv_setsv(sv,&PL_sv_undef);
985 PL_psig_ptr[i] = SvREFCNT_inc(sv);
992 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
996 /* Are we clearing a signal entry? */
997 i = whichsig(MgPV(mg,n_a));
1000 SvREFCNT_dec(PL_psig_ptr[i]);
1003 if(PL_psig_name[i]) {
1004 SvREFCNT_dec(PL_psig_name[i]);
1012 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1022 if (strEQ(s,"__DIE__"))
1024 else if (strEQ(s,"__WARN__"))
1027 Perl_croak(aTHX_ "No such hook: %s", s);
1035 i = whichsig(s); /* ...no, a brick */
1037 if (ckWARN(WARN_SIGNAL))
1038 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1041 SvREFCNT_dec(PL_psig_name[i]);
1042 SvREFCNT_dec(PL_psig_ptr[i]);
1043 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1044 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1045 PL_psig_name[i] = newSVpvn(s, len);
1046 SvREADONLY_on(PL_psig_name[i]);
1048 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1050 (void)rsignal(i, PL_sighandlerp);
1052 *svp = SvREFCNT_inc(sv);
1055 s = SvPV_force(sv,len);
1056 if (strEQ(s,"IGNORE")) {
1058 (void)rsignal(i, SIG_IGN);
1062 else if (strEQ(s,"DEFAULT") || !*s) {
1064 (void)rsignal(i, SIG_DFL);
1070 * We should warn if HINT_STRICT_REFS, but without
1071 * access to a known hint bit in a known OP, we can't
1072 * tell whether HINT_STRICT_REFS is in force or not.
1074 if (!strchr(s,':') && !strchr(s,'\''))
1075 sv_insert(sv, 0, 0, "main::", 6);
1077 (void)rsignal(i, PL_sighandlerp);
1079 *svp = SvREFCNT_inc(sv);
1083 #endif /* !PERL_MICRO */
1086 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1088 PL_sub_generation++;
1093 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1095 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1096 PL_amagic_generation++;
1102 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1104 HV *hv = (HV*)LvTARG(sv);
1109 (void) hv_iterinit(hv);
1110 if (! SvTIED_mg((SV*)hv, 'P'))
1114 while ((entry = hv_iternext(hv))) {
1120 sv_setiv(sv, (IV)i);
1125 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1128 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1133 /* caller is responsible for stack switching/cleanup */
1135 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1141 PUSHs(SvTIED_obj(sv, mg));
1144 if (mg->mg_len >= 0)
1145 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1146 else if (mg->mg_len == HEf_SVKEY)
1147 PUSHs((SV*)mg->mg_ptr);
1149 else if (mg->mg_type == 'p') {
1150 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1158 return call_method(meth, flags);
1162 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1168 PUSHSTACKi(PERLSI_MAGIC);
1170 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1171 sv_setsv(sv, *PL_stack_sp--);
1181 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1183 magic_methpack(sv,mg,"FETCH");
1185 mg->mg_flags |= MGf_GSKIP;
1190 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1194 PUSHSTACKi(PERLSI_MAGIC);
1195 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1202 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1204 return magic_methpack(sv,mg,"DELETE");
1209 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1216 PUSHSTACKi(PERLSI_MAGIC);
1217 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1218 sv = *PL_stack_sp--;
1219 retval = (U32) SvIV(sv)-1;
1228 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1233 PUSHSTACKi(PERLSI_MAGIC);
1235 XPUSHs(SvTIED_obj(sv, mg));
1237 call_method("CLEAR", G_SCALAR|G_DISCARD);
1244 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1247 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1251 PUSHSTACKi(PERLSI_MAGIC);
1254 PUSHs(SvTIED_obj(sv, mg));
1259 if (call_method(meth, G_SCALAR))
1260 sv_setsv(key, *PL_stack_sp--);
1269 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1271 return magic_methpack(sv,mg,"EXISTS");
1275 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1286 svp = av_fetch(GvAV(gv),
1287 atoi(MgPV(mg,n_a)), FALSE);
1288 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1294 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1297 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1302 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1305 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1310 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1312 SV* lsv = LvTARG(sv);
1314 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1315 mg = mg_find(lsv, 'g');
1316 if (mg && mg->mg_len >= 0) {
1320 sv_pos_b2u(lsv, &i);
1321 sv_setiv(sv, i + PL_curcop->cop_arybase);
1330 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1332 SV* lsv = LvTARG(sv);
1340 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1341 mg = mg_find(lsv, 'g');
1345 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1346 mg = mg_find(lsv, 'g');
1348 else if (!SvOK(sv)) {
1352 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1354 pos = SvIV(sv) - PL_curcop->cop_arybase;
1357 ulen = sv_len_utf8(lsv);
1372 sv_pos_u2b(lsv, &p, 0);
1377 mg->mg_flags &= ~MGf_MINMATCH;
1383 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1385 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1387 gv_efullname3(sv,((GV*)sv), "*");
1391 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1396 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1405 if (*s == '*' && s[1])
1407 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1412 GvGP(sv) = gp_ref(GvGP(gv));
1417 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1420 SV *lsv = LvTARG(sv);
1421 char *tmps = SvPV(lsv,len);
1422 I32 offs = LvTARGOFF(sv);
1423 I32 rem = LvTARGLEN(sv);
1427 if (rem + offs > len)
1429 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1434 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1437 char *tmps = SvPV(sv,len);
1438 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1443 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1446 TAINT_IF((mg->mg_len & 1) ||
1447 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1452 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1455 if (PL_localizing) {
1456 if (PL_localizing == 1)
1461 else if (PL_tainted)
1469 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1471 SV *lsv = LvTARG(sv);
1478 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1483 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1485 do_vecset(sv); /* XXX slurp this routine */
1490 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1493 if (LvTARGLEN(sv)) {
1495 SV *ahv = LvTARG(sv);
1496 if (SvTYPE(ahv) == SVt_PVHV) {
1497 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1502 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1508 AV* av = (AV*)LvTARG(sv);
1509 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1510 targ = AvARRAY(av)[LvTARGOFF(sv)];
1512 if (targ && targ != &PL_sv_undef) {
1513 dTHR; /* just for SvREFCNT_dec */
1514 /* somebody else defined it for us */
1515 SvREFCNT_dec(LvTARG(sv));
1516 LvTARG(sv) = SvREFCNT_inc(targ);
1518 SvREFCNT_dec(mg->mg_obj);
1519 mg->mg_obj = Nullsv;
1520 mg->mg_flags &= ~MGf_REFCOUNTED;
1525 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1530 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1535 sv_setsv(LvTARG(sv), sv);
1536 SvSETMAGIC(LvTARG(sv));
1542 Perl_vivify_defelem(pTHX_ SV *sv)
1544 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1548 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1551 SV *ahv = LvTARG(sv);
1553 if (SvTYPE(ahv) == SVt_PVHV) {
1554 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1559 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1563 if (!value || value == &PL_sv_undef)
1564 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1567 AV* av = (AV*)LvTARG(sv);
1568 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1569 LvTARG(sv) = Nullsv; /* array can't be extended */
1571 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1572 if (!svp || (value = *svp) == &PL_sv_undef)
1573 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1576 (void)SvREFCNT_inc(value);
1577 SvREFCNT_dec(LvTARG(sv));
1580 SvREFCNT_dec(mg->mg_obj);
1581 mg->mg_obj = Nullsv;
1582 mg->mg_flags &= ~MGf_REFCOUNTED;
1586 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1588 AV *av = (AV*)mg->mg_obj;
1589 SV **svp = AvARRAY(av);
1590 I32 i = AvFILLp(av);
1592 if (svp[i] && svp[i] != &PL_sv_undef) {
1593 if (!SvWEAKREF(svp[i]))
1594 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1595 /* XXX Should we check that it hasn't changed? */
1597 (void)SvOK_off(svp[i]);
1598 SvWEAKREF_off(svp[i]);
1599 svp[i] = &PL_sv_undef;
1607 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1615 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1617 sv_unmagic(sv, 'B');
1623 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1625 sv_unmagic(sv, 'f');
1631 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1633 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1635 if (uf && uf->uf_set)
1636 (*uf->uf_set)(uf->uf_index, sv);
1641 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1643 regexp *re = (regexp *)mg->mg_obj;
1648 #ifdef USE_LOCALE_COLLATE
1650 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1653 * RenE<eacute> Descartes said "I think not."
1654 * and vanished with a faint plop.
1657 Safefree(mg->mg_ptr);
1663 #endif /* USE_LOCALE_COLLATE */
1666 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1672 switch (*mg->mg_ptr) {
1673 case '\001': /* ^A */
1674 sv_setsv(PL_bodytarget, sv);
1676 case '\003': /* ^C */
1677 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1680 case '\004': /* ^D */
1681 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1682 DEBUG_x(dump_all());
1684 case '\005': /* ^E */
1685 #ifdef MACOS_TRADITIONAL
1686 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1689 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1692 SetLastError( SvIV(sv) );
1695 /* will anyone ever use this? */
1696 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1702 case '\006': /* ^F */
1703 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1705 case '\010': /* ^H */
1706 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1708 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1710 Safefree(PL_inplace);
1712 PL_inplace = savepv(SvPV(sv,len));
1714 PL_inplace = Nullch;
1716 case '\017': /* ^O */
1718 Safefree(PL_osname);
1720 PL_osname = savepv(SvPV(sv,len));
1724 case '\020': /* ^P */
1725 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1726 if (PL_perldb && !PL_DBsingle)
1729 case '\024': /* ^T */
1731 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1733 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1736 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1737 if (*(mg->mg_ptr+1) == '\0') {
1738 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1739 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1740 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1741 | (i ? G_WARN_ON : G_WARN_OFF) ;
1744 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1745 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1746 if (!SvPOK(sv) && PL_localizing) {
1747 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1748 PL_compiling.cop_warnings = pWARN_NONE;
1753 int accumulate = 0 ;
1754 int any_fatals = 0 ;
1755 char * ptr = (char*)SvPV(sv, len) ;
1756 for (i = 0 ; i < len ; ++i) {
1757 accumulate |= ptr[i] ;
1758 any_fatals |= (ptr[i] & 0xAA) ;
1761 PL_compiling.cop_warnings = pWARN_NONE;
1762 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1763 PL_compiling.cop_warnings = pWARN_ALL;
1764 PL_dowarn |= G_WARN_ONCE ;
1767 if (specialWARN(PL_compiling.cop_warnings))
1768 PL_compiling.cop_warnings = newSVsv(sv) ;
1770 sv_setsv(PL_compiling.cop_warnings, sv);
1771 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1772 PL_dowarn |= G_WARN_ONCE ;
1778 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1779 PL_widesyscalls = SvTRUE(sv);
1782 if (PL_localizing) {
1783 if (PL_localizing == 1)
1784 SAVESPTR(PL_last_in_gv);
1786 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1787 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1790 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1791 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1792 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1795 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1796 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1797 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1800 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1803 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1804 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1805 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1808 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1812 IO *io = GvIOp(PL_defoutgv);
1813 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1814 IoFLAGS(io) &= ~IOf_FLUSH;
1816 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1817 PerlIO *ofp = IoOFP(io);
1819 (void)PerlIO_flush(ofp);
1820 IoFLAGS(io) |= IOf_FLUSH;
1826 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1827 PL_multiline = (i != 0);
1830 SvREFCNT_dec(PL_nrs);
1831 PL_nrs = newSVsv(sv);
1832 SvREFCNT_dec(PL_rs);
1833 PL_rs = SvREFCNT_inc(PL_nrs);
1838 if (SvOK(sv) || SvGMAGICAL(sv)) {
1839 s = SvPV(sv,PL_orslen);
1840 PL_ors = savepvn(s,PL_orslen);
1850 PL_ofs = savepv(SvPV(sv, PL_ofslen));
1855 PL_ofmt = savepv(SvPV(sv,len));
1858 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1861 #ifdef COMPLEX_STATUS
1862 if (PL_localizing == 2) {
1863 PL_statusvalue = LvTARGOFF(sv);
1864 PL_statusvalue_vms = LvTARGLEN(sv);
1868 #ifdef VMSISH_STATUS
1870 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1873 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1876 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1877 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1880 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1881 if (PL_delaymagic) {
1882 PL_delaymagic |= DM_RUID;
1883 break; /* don't do magic till later */
1886 (void)setruid((Uid_t)PL_uid);
1889 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1891 #ifdef HAS_SETRESUID
1892 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1894 if (PL_uid == PL_euid) /* special case $< = $> */
1895 (void)PerlProc_setuid(PL_uid);
1897 PL_uid = PerlProc_getuid();
1898 Perl_croak(aTHX_ "setruid() not implemented");
1903 PL_uid = PerlProc_getuid();
1904 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1907 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1908 if (PL_delaymagic) {
1909 PL_delaymagic |= DM_EUID;
1910 break; /* don't do magic till later */
1913 (void)seteuid((Uid_t)PL_euid);
1916 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1918 #ifdef HAS_SETRESUID
1919 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1921 if (PL_euid == PL_uid) /* special case $> = $< */
1922 PerlProc_setuid(PL_euid);
1924 PL_euid = PerlProc_geteuid();
1925 Perl_croak(aTHX_ "seteuid() not implemented");
1930 PL_euid = PerlProc_geteuid();
1931 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1934 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1935 if (PL_delaymagic) {
1936 PL_delaymagic |= DM_RGID;
1937 break; /* don't do magic till later */
1940 (void)setrgid((Gid_t)PL_gid);
1943 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1945 #ifdef HAS_SETRESGID
1946 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1948 if (PL_gid == PL_egid) /* special case $( = $) */
1949 (void)PerlProc_setgid(PL_gid);
1951 PL_gid = PerlProc_getgid();
1952 Perl_croak(aTHX_ "setrgid() not implemented");
1957 PL_gid = PerlProc_getgid();
1958 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1961 #ifdef HAS_SETGROUPS
1963 char *p = SvPV(sv, len);
1964 Groups_t gary[NGROUPS];
1969 for (i = 0; i < NGROUPS; ++i) {
1970 while (*p && !isSPACE(*p))
1979 (void)setgroups(i, gary);
1981 #else /* HAS_SETGROUPS */
1982 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1983 #endif /* HAS_SETGROUPS */
1984 if (PL_delaymagic) {
1985 PL_delaymagic |= DM_EGID;
1986 break; /* don't do magic till later */
1989 (void)setegid((Gid_t)PL_egid);
1992 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
1994 #ifdef HAS_SETRESGID
1995 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
1997 if (PL_egid == PL_gid) /* special case $) = $( */
1998 (void)PerlProc_setgid(PL_egid);
2000 PL_egid = PerlProc_getegid();
2001 Perl_croak(aTHX_ "setegid() not implemented");
2006 PL_egid = PerlProc_getegid();
2007 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2010 PL_chopset = SvPV_force(sv,len);
2012 #ifndef MACOS_TRADITIONAL
2014 #ifdef HAS_SETPROCTITLE
2015 /* The BSDs don't show the argv[] in ps(1) output, they
2016 * show a string from the process struct and provide
2017 * the setproctitle() routine to manipulate that. */
2020 # if __FreeBSD_version >= 410001
2021 /* The leading "-" removes the "perl: " prefix,
2022 * but not the "(perl) suffix from the ps(1)
2023 * output, because that's what ps(1) shows if the
2024 * argv[] is modified. */
2025 setproctitle("-%s", s, len + 1);
2026 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2027 /* This doesn't really work if you assume that
2028 * $0 = 'foobar'; will wipe out 'perl' from the $0
2029 * because in ps(1) output the result will be like
2030 * sprintf("perl: %s (perl)", s)
2031 * I guess this is a security feature:
2032 * one (a user process) cannot get rid of the original name.
2034 setproctitle("%s", s);
2041 /* See if all the arguments are contiguous in memory */
2042 for (i = 1; i < PL_origargc; i++) {
2043 if (PL_origargv[i] == s + 1
2045 || PL_origargv[i] == s + 2
2050 s += strlen(s); /* this one is ok too */
2055 /* can grab env area too? */
2056 if (PL_origenviron && (PL_origenviron[0] == s + 1
2058 || (PL_origenviron[0] == s + 9 && (s += 8))
2061 my_setenv("NoNe SuCh", Nullch);
2062 /* force copy of environment */
2063 for (i = 0; PL_origenviron[i]; i++)
2064 if (PL_origenviron[i] == s + 1) {
2071 PL_origalen = s - PL_origargv[0];
2073 s = SvPV_force(sv,len);
2075 if (i >= PL_origalen) {
2077 /* don't allow system to limit $0 seen by script */
2078 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2079 Copy(s, PL_origargv[0], i, char);
2080 s = PL_origargv[0]+i;
2084 Copy(s, PL_origargv[0], i, char);
2085 s = PL_origargv[0]+i;
2087 while (++i < PL_origalen)
2089 s = PL_origargv[0]+i;
2090 for (i = 1; i < PL_origargc; i++)
2091 PL_origargv[i] = Nullch;
2097 sv_setsv(thr->errsv, sv);
2099 #endif /* USE_THREADS */
2106 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2109 DEBUG_S(PerlIO_printf(Perl_debug_log,
2110 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2111 PTR2UV(thr), PTR2UV(sv));)
2113 Perl_croak(aTHX_ "panic: magic_mutexfree");
2114 MUTEX_DESTROY(MgMUTEXP(mg));
2115 COND_DESTROY(MgCONDP(mg));
2118 #endif /* USE_THREADS */
2121 Perl_whichsig(pTHX_ char *sig)
2123 register char **sigv;
2125 for (sigv = PL_sig_name+1; *sigv; sigv++)
2126 if (strEQ(sig,*sigv))
2127 return PL_sig_num[sigv - PL_sig_name];
2129 if (strEQ(sig,"CHLD"))
2133 if (strEQ(sig,"CLD"))
2142 Perl_sighandler(int sig)
2144 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2145 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2152 SV *sv, *tSv = PL_Sv;
2156 I32 o_save_i = PL_savestack_ix;
2159 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2160 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2163 if (PL_savestack_ix + 15 <= PL_savestack_max)
2165 if (PL_markstack_ptr < PL_markstack_max - 2)
2167 if (PL_retstack_ix < PL_retstack_max - 2)
2169 if (PL_scopestack_ix < PL_scopestack_max - 3)
2172 if (!PL_psig_ptr[sig])
2173 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2176 /* Max number of items pushed there is 3*n or 4. We cannot fix
2177 infinity, so we fix 4 (in fact 5): */
2179 PL_savestack_ix += 5; /* Protect save in progress. */
2180 o_save_i = PL_savestack_ix;
2181 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2184 PL_markstack_ptr++; /* Protect mark. */
2187 PL_retstack[PL_retstack_ix] = NULL;
2190 PL_scopestack_ix += 1;
2191 /* sv_2cv is too complicated, try a simpler variant first: */
2192 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2193 || SvTYPE(cv) != SVt_PVCV)
2194 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2196 if (!cv || !CvROOT(cv)) {
2197 if (ckWARN(WARN_SIGNAL))
2198 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2199 PL_sig_name[sig], (gv ? GvENAME(gv)
2206 if(PL_psig_name[sig]) {
2207 sv = SvREFCNT_inc(PL_psig_name[sig]);
2211 sv = sv_newmortal();
2212 sv_setpv(sv,PL_sig_name[sig]);
2215 PUSHSTACKi(PERLSI_SIGNAL);
2220 call_sv((SV*)cv, G_DISCARD);
2225 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2231 PL_scopestack_ix -= 1;
2234 PL_op = myop; /* Apparently not needed... */
2236 PL_Sv = tSv; /* Restore global temporaries. */
2247 restore_magic(pTHXo_ void *p)
2250 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2251 SV* sv = mgs->mgs_sv;
2256 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2259 SvFLAGS(sv) |= mgs->mgs_flags;
2263 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2266 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2268 /* If we're still on top of the stack, pop us off. (That condition
2269 * will be satisfied if restore_magic was called explicitly, but *not*
2270 * if it's being called via leave_scope.)
2271 * The reason for doing this is that otherwise, things like sv_2cv()
2272 * may leave alloc gunk on the savestack, and some code
2273 * (e.g. sighandler) doesn't expect that...
2275 if (PL_savestack_ix == mgs->mgs_ss_ix)
2277 I32 popval = SSPOPINT;
2278 assert(popval == SAVEt_DESTRUCTOR_X);
2279 PL_savestack_ix -= 2;
2281 assert(popval == SAVEt_ALLOC);
2283 PL_savestack_ix -= popval;
2289 unwind_handler_stack(pTHXo_ void *p)
2292 U32 flags = *(U32*)p;
2295 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2296 /* cxstack_ix-- Not needed, die already unwound it. */
2298 SvREFCNT_dec(sig_sv);