3 * Copyright (c) 1991-2000, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
12 * come here, and I don't want to see no more magic,' he said, and fell silent."
19 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
24 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
30 static void restore_magic(pTHXo_ void *p);
31 static void unwind_handler_stack(pTHXo_ void *p);
34 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
42 /* MGS is typedef'ed to struct magic_state in perl.h */
45 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
49 assert(SvMAGICAL(sv));
51 SAVEDESTRUCTOR_X(restore_magic, (void*)mgs_ix);
53 mgs = SSPTR(mgs_ix, MGS*);
55 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
56 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
60 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
64 =for apidoc mg_magical
66 Turns on the magical status of an SV. See C<sv_magic>.
72 Perl_mg_magical(pTHX_ SV *sv)
75 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
76 MGVTBL* vtbl = mg->mg_virtual;
78 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
82 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
91 Do magic after a value is retrieved from the SV. See C<sv_magic>.
97 Perl_mg_get(pTHX_ SV *sv)
105 mgs_ix = SSNEW(sizeof(MGS));
106 save_magic(mgs_ix, sv);
109 while ((mg = *mgp) != 0) {
110 MGVTBL* vtbl = mg->mg_virtual;
111 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
112 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
113 /* Ignore this magic if it's been deleted */
114 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
115 (mg->mg_flags & MGf_GSKIP))
116 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
118 /* Advance to next magic (complicated by possible deletion) */
119 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
120 mgp = &mg->mg_moremagic;
124 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
127 restore_magic(aTHXo_ (void*)mgs_ix);
134 Do magic after a value is assigned to the SV. See C<sv_magic>.
140 Perl_mg_set(pTHX_ SV *sv)
147 mgs_ix = SSNEW(sizeof(MGS));
148 save_magic(mgs_ix, sv);
150 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
151 MGVTBL* vtbl = mg->mg_virtual;
152 nextmg = mg->mg_moremagic; /* it may delete itself */
153 if (mg->mg_flags & MGf_GSKIP) {
154 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
155 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
157 if (vtbl && vtbl->svt_set)
158 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
161 restore_magic(aTHXo_ (void*)mgs_ix);
166 =for apidoc mg_length
168 Report on the SV's length. See C<sv_magic>.
174 Perl_mg_length(pTHX_ SV *sv)
180 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
181 MGVTBL* vtbl = mg->mg_virtual;
182 if (vtbl && vtbl->svt_len) {
185 mgs_ix = SSNEW(sizeof(MGS));
186 save_magic(mgs_ix, sv);
187 /* omit MGf_GSKIP -- not changed here */
188 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
189 restore_magic(aTHXo_ (void*)mgs_ix);
194 junk = SvPV(sv, len);
199 Perl_mg_size(pTHX_ SV *sv)
204 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
205 MGVTBL* vtbl = mg->mg_virtual;
206 if (vtbl && vtbl->svt_len) {
209 mgs_ix = SSNEW(sizeof(MGS));
210 save_magic(mgs_ix, sv);
211 /* omit MGf_GSKIP -- not changed here */
212 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
213 restore_magic(aTHXo_ (void*)mgs_ix);
220 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
225 Perl_croak(aTHX_ "Size magic not implemented");
234 Clear something magical that the SV represents. See C<sv_magic>.
240 Perl_mg_clear(pTHX_ SV *sv)
245 mgs_ix = SSNEW(sizeof(MGS));
246 save_magic(mgs_ix, sv);
248 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
249 MGVTBL* vtbl = mg->mg_virtual;
250 /* omit GSKIP -- never set here */
252 if (vtbl && vtbl->svt_clear)
253 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
256 restore_magic(aTHXo_ (void*)mgs_ix);
263 Finds the magic pointer for type matching the SV. See C<sv_magic>.
269 Perl_mg_find(pTHX_ SV *sv, int type)
272 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
273 if (mg->mg_type == type)
282 Copies the magic from one SV to another. See C<sv_magic>.
288 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
292 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
293 if (isUPPER(mg->mg_type)) {
295 mg->mg_type == 'P' ? SvTIED_obj(sv, mg) : mg->mg_obj,
296 toLOWER(mg->mg_type), key, klen);
306 Free any magic storage used by the SV. See C<sv_magic>.
312 Perl_mg_free(pTHX_ SV *sv)
316 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
317 MGVTBL* vtbl = mg->mg_virtual;
318 moremagic = mg->mg_moremagic;
319 if (vtbl && vtbl->svt_free)
320 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
321 if (mg->mg_ptr && mg->mg_type != 'g')
323 Safefree(mg->mg_ptr);
324 else if (mg->mg_len == HEf_SVKEY)
325 SvREFCNT_dec((SV*)mg->mg_ptr);
326 if (mg->mg_flags & MGf_REFCOUNTED)
327 SvREFCNT_dec(mg->mg_obj);
334 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
339 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
344 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
345 if (mg->mg_obj) /* @+ */
348 return rx->lastparen;
355 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
364 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
368 if (paren <= rx->nparens &&
369 (s = rx->startp[paren]) != -1 &&
370 (t = rx->endp[paren]) != -1)
372 if (mg->mg_obj) /* @+ */
384 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
392 switch (*mg->mg_ptr) {
393 case '1': case '2': case '3': case '4':
394 case '5': case '6': case '7': case '8': case '9': case '&':
395 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
397 paren = atoi(mg->mg_ptr);
399 if (paren <= rx->nparens &&
400 (s1 = rx->startp[paren]) != -1 &&
401 (t1 = rx->endp[paren]) != -1)
405 if (i > 0 && (PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE) {
406 char *s = rx->subbeg + s1;
407 char *send = rx->subbeg + t1;
420 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
421 paren = rx->lastparen;
427 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
428 if (rx->startp[0] != -1) {
439 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
440 if (rx->endp[0] != -1) {
441 i = rx->sublen - rx->endp[0];
451 return (STRLEN)PL_ofslen;
453 return (STRLEN)PL_orslen;
456 if (!SvPOK(sv) && SvNIOK(sv)) {
466 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
474 switch (*mg->mg_ptr) {
475 case '\001': /* ^A */
476 sv_setsv(sv, PL_bodytarget);
478 case '\003': /* ^C */
479 sv_setiv(sv, (IV)PL_minus_c);
482 case '\004': /* ^D */
483 sv_setiv(sv, (IV)(PL_debug & 32767));
484 #if defined(YYDEBUG) && defined(DEBUGGING)
485 PL_yydebug = (PL_debug & 1);
488 case '\005': /* ^E */
489 #ifdef MACOS_TRADITIONAL
493 sv_setnv(sv,(double)gMacPerl_OSErr);
494 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
499 # include <descrip.h>
500 # include <starlet.h>
502 $DESCRIPTOR(msgdsc,msg);
503 sv_setnv(sv,(NV) vaxc$errno);
504 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
505 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
511 if (!(_emx_env & 0x200)) { /* Under DOS */
512 sv_setnv(sv, (NV)errno);
513 sv_setpv(sv, errno ? Strerror(errno) : "");
515 if (errno != errno_isOS2) {
516 int tmp = _syserrno();
517 if (tmp) /* 2nd call to _syserrno() makes it 0 */
520 sv_setnv(sv, (NV)Perl_rc);
521 sv_setpv(sv, os2error(Perl_rc));
526 DWORD dwErr = GetLastError();
527 sv_setnv(sv, (NV)dwErr);
530 PerlProc_GetOSError(sv, dwErr);
537 sv_setnv(sv, (NV)errno);
538 sv_setpv(sv, errno ? Strerror(errno) : "");
543 SvNOK_on(sv); /* what a wonderful hack! */
545 case '\006': /* ^F */
546 sv_setiv(sv, (IV)PL_maxsysfd);
548 case '\010': /* ^H */
549 sv_setiv(sv, (IV)PL_hints);
551 case '\011': /* ^I */ /* NOT \t in EBCDIC */
553 sv_setpv(sv, PL_inplace);
555 sv_setsv(sv, &PL_sv_undef);
557 case '\017': /* ^O */
558 sv_setpv(sv, PL_osname);
560 case '\020': /* ^P */
561 sv_setiv(sv, (IV)PL_perldb);
563 case '\023': /* ^S */
566 if (PL_lex_state != LEX_NOTPARSING)
574 case '\024': /* ^T */
576 sv_setnv(sv, PL_basetime);
578 sv_setiv(sv, (IV)PL_basetime);
581 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
582 if (*(mg->mg_ptr+1) == '\0')
583 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
584 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
585 if (PL_compiling.cop_warnings == pWARN_NONE ||
586 PL_compiling.cop_warnings == pWARN_STD)
588 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
590 else if (PL_compiling.cop_warnings == pWARN_ALL) {
591 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
594 sv_setsv(sv, PL_compiling.cop_warnings);
598 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
599 sv_setiv(sv, (IV)PL_widesyscalls);
601 case '1': case '2': case '3': case '4':
602 case '5': case '6': case '7': case '8': case '9': case '&':
603 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
607 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
608 * XXX Does the new way break anything?
610 paren = atoi(mg->mg_ptr);
612 if (paren <= rx->nparens &&
613 (s1 = rx->startp[paren]) != -1 &&
614 (t1 = rx->endp[paren]) != -1)
625 was_tainted = PL_tainted;
629 if ((PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE)
634 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
639 sv_setsv(sv,&PL_sv_undef);
642 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
643 paren = rx->lastparen;
647 sv_setsv(sv,&PL_sv_undef);
650 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
651 if ((s = rx->subbeg) && rx->startp[0] != -1) {
656 sv_setsv(sv,&PL_sv_undef);
659 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
660 if (rx->subbeg && rx->endp[0] != -1) {
661 s = rx->subbeg + rx->endp[0];
662 i = rx->sublen - rx->endp[0];
666 sv_setsv(sv,&PL_sv_undef);
670 if (GvIO(PL_last_in_gv)) {
671 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
677 sv_setiv(sv, (IV)STATUS_CURRENT);
678 #ifdef COMPLEX_STATUS
679 LvTARGOFF(sv) = PL_statusvalue;
680 LvTARGLEN(sv) = PL_statusvalue_vms;
685 s = IoTOP_NAME(GvIOp(PL_defoutgv));
689 sv_setpv(sv,GvENAME(PL_defoutgv));
694 s = IoFMT_NAME(GvIOp(PL_defoutgv));
696 s = GvENAME(PL_defoutgv);
701 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
704 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
707 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
715 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
718 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
721 sv_setpvn(sv,PL_ofs,PL_ofslen);
724 sv_setpvn(sv,PL_ors,PL_orslen);
727 sv_setpv(sv,PL_ofmt);
731 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
732 sv_setpv(sv, errno ? Strerror(errno) : "");
735 int saveerrno = errno;
736 sv_setnv(sv, (NV)errno);
738 if (errno == errno_isOS2 || errno == errno_isOS2_set)
739 sv_setpv(sv, os2error(Perl_rc));
742 sv_setpv(sv, errno ? Strerror(errno) : "");
746 SvNOK_on(sv); /* what a wonderful hack! */
749 sv_setiv(sv, (IV)PL_uid);
752 sv_setiv(sv, (IV)PL_euid);
755 sv_setiv(sv, (IV)PL_gid);
757 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
761 sv_setiv(sv, (IV)PL_egid);
763 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
768 Groups_t gary[NGROUPS];
769 i = getgroups(NGROUPS,gary);
771 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
774 (void)SvIOK_on(sv); /* what a wonderful hack! */
778 #ifndef MACOS_TRADITIONAL
784 sv_setsv(sv, thr->errsv);
786 #endif /* USE_THREADS */
792 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
794 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
796 if (uf && uf->uf_val)
797 (*uf->uf_val)(uf->uf_index, sv);
802 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
813 #ifdef DYNAMIC_ENV_FETCH
814 /* We just undefd an environment var. Is a replacement */
815 /* waiting in the wings? */
818 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
819 s = SvPV(*valp, len);
823 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
824 /* And you'll never guess what the dog had */
825 /* in its mouth... */
827 MgTAINTEDDIR_off(mg);
829 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
830 char pathbuf[256], eltbuf[256], *cp, *elt = s;
834 do { /* DCL$PATH may be a search list */
835 while (1) { /* as may dev portion of any element */
836 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
837 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
838 cando_by_name(S_IWUSR,0,elt) ) {
843 if ((cp = strchr(elt, ':')) != Nullch)
845 if (my_trnlnm(elt, eltbuf, j++))
851 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
854 if (s && klen == 4 && strEQ(ptr,"PATH")) {
855 char *strend = s + len;
860 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
863 if (i >= sizeof tmpbuf /* too long -- assume the worst */
865 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
872 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
878 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
881 my_setenv(MgPV(mg,n_a),Nullch);
886 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
889 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
895 magic_clear_all_env(sv,mg);
896 hv_iterinit((HV*)sv);
897 while ((entry = hv_iternext((HV*)sv))) {
899 my_setenv(hv_iterkey(entry, &keylen),
900 SvPV(hv_iterval((HV*)sv, entry), n_a));
908 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
911 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
913 # ifdef PERL_IMPLICIT_SYS
917 char *envv = GetEnvironmentStrings();
921 char *end = strchr(cur,'=');
922 if (end && end != cur) {
924 my_setenv(cur,Nullch);
926 cur = end + strlen(end+1)+2;
928 else if ((len = strlen(cur)))
931 FreeEnvironmentStrings(envv);
935 for (i = 0; environ[i]; i++)
936 safesysfree(environ[i]);
938 # ifndef PERL_USE_SAFE_PUTENV
941 if (environ == PL_origenviron)
942 environ = (char**)safesysmalloc(sizeof(char*));
944 for (i = 0; environ[i]; i++)
945 safesysfree(environ[i]);
946 # endif /* PERL_USE_SAFE_PUTENV */
947 # endif /* __CYGWIN__ */
952 # endif /* PERL_IMPLICIT_SYS */
959 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
963 /* Are we fetching a signal entry? */
964 i = whichsig(MgPV(mg,n_a));
967 sv_setsv(sv,PL_psig_ptr[i]);
969 Sighandler_t sigstate = rsignal_state(i);
971 /* cache state so we don't fetch it again */
972 if(sigstate == SIG_IGN)
973 sv_setpv(sv,"IGNORE");
975 sv_setsv(sv,&PL_sv_undef);
976 PL_psig_ptr[i] = SvREFCNT_inc(sv);
983 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
987 /* Are we clearing a signal entry? */
988 i = whichsig(MgPV(mg,n_a));
991 SvREFCNT_dec(PL_psig_ptr[i]);
994 if(PL_psig_name[i]) {
995 SvREFCNT_dec(PL_psig_name[i]);
1003 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1013 if (strEQ(s,"__DIE__"))
1015 else if (strEQ(s,"__WARN__"))
1018 Perl_croak(aTHX_ "No such hook: %s", s);
1026 i = whichsig(s); /* ...no, a brick */
1028 if (ckWARN(WARN_SIGNAL))
1029 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1032 SvREFCNT_dec(PL_psig_name[i]);
1033 SvREFCNT_dec(PL_psig_ptr[i]);
1034 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1035 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1036 PL_psig_name[i] = newSVpvn(s, len);
1037 SvREADONLY_on(PL_psig_name[i]);
1039 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1041 (void)rsignal(i, PL_sighandlerp);
1043 *svp = SvREFCNT_inc(sv);
1046 s = SvPV_force(sv,len);
1047 if (strEQ(s,"IGNORE")) {
1049 (void)rsignal(i, SIG_IGN);
1053 else if (strEQ(s,"DEFAULT") || !*s) {
1055 (void)rsignal(i, SIG_DFL);
1061 * We should warn if HINT_STRICT_REFS, but without
1062 * access to a known hint bit in a known OP, we can't
1063 * tell whether HINT_STRICT_REFS is in force or not.
1065 if (!strchr(s,':') && !strchr(s,'\''))
1066 sv_insert(sv, 0, 0, "main::", 6);
1068 (void)rsignal(i, PL_sighandlerp);
1070 *svp = SvREFCNT_inc(sv);
1074 #endif /* !PERL_MICRO */
1077 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1079 PL_sub_generation++;
1084 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1086 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1087 PL_amagic_generation++;
1093 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1095 HV *hv = (HV*)LvTARG(sv);
1100 (void) hv_iterinit(hv);
1101 if (! SvTIED_mg((SV*)hv, 'P'))
1105 while ((entry = hv_iternext(hv))) {
1111 sv_setiv(sv, (IV)i);
1116 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1119 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1124 /* caller is responsible for stack switching/cleanup */
1126 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1132 PUSHs(SvTIED_obj(sv, mg));
1135 if (mg->mg_len >= 0)
1136 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1137 else if (mg->mg_len == HEf_SVKEY)
1138 PUSHs((SV*)mg->mg_ptr);
1140 else if (mg->mg_type == 'p') {
1141 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1149 return call_method(meth, flags);
1153 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1159 PUSHSTACKi(PERLSI_MAGIC);
1161 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1162 sv_setsv(sv, *PL_stack_sp--);
1172 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1174 magic_methpack(sv,mg,"FETCH");
1176 mg->mg_flags |= MGf_GSKIP;
1181 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1185 PUSHSTACKi(PERLSI_MAGIC);
1186 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1193 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1195 return magic_methpack(sv,mg,"DELETE");
1200 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1207 PUSHSTACKi(PERLSI_MAGIC);
1208 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1209 sv = *PL_stack_sp--;
1210 retval = (U32) SvIV(sv)-1;
1219 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1224 PUSHSTACKi(PERLSI_MAGIC);
1226 XPUSHs(SvTIED_obj(sv, mg));
1228 call_method("CLEAR", G_SCALAR|G_DISCARD);
1235 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1238 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1242 PUSHSTACKi(PERLSI_MAGIC);
1245 PUSHs(SvTIED_obj(sv, mg));
1250 if (call_method(meth, G_SCALAR))
1251 sv_setsv(key, *PL_stack_sp--);
1260 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1262 return magic_methpack(sv,mg,"EXISTS");
1266 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1277 svp = av_fetch(GvAV(gv),
1278 atoi(MgPV(mg,n_a)), FALSE);
1279 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1285 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1288 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1293 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1296 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1301 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1303 SV* lsv = LvTARG(sv);
1305 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1306 mg = mg_find(lsv, 'g');
1307 if (mg && mg->mg_len >= 0) {
1311 sv_pos_b2u(lsv, &i);
1312 sv_setiv(sv, i + PL_curcop->cop_arybase);
1321 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1323 SV* lsv = LvTARG(sv);
1331 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1332 mg = mg_find(lsv, 'g');
1336 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1337 mg = mg_find(lsv, 'g');
1339 else if (!SvOK(sv)) {
1343 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1345 pos = SvIV(sv) - PL_curcop->cop_arybase;
1348 ulen = sv_len_utf8(lsv);
1363 sv_pos_u2b(lsv, &p, 0);
1368 mg->mg_flags &= ~MGf_MINMATCH;
1374 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1376 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1378 gv_efullname3(sv,((GV*)sv), "*");
1382 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1387 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1396 if (*s == '*' && s[1])
1398 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1403 GvGP(sv) = gp_ref(GvGP(gv));
1408 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1411 SV *lsv = LvTARG(sv);
1412 char *tmps = SvPV(lsv,len);
1413 I32 offs = LvTARGOFF(sv);
1414 I32 rem = LvTARGLEN(sv);
1418 if (rem + offs > len)
1420 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1425 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1428 char *tmps = SvPV(sv,len);
1429 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1434 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1437 TAINT_IF((mg->mg_len & 1) ||
1438 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1443 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1446 if (PL_localizing) {
1447 if (PL_localizing == 1)
1452 else if (PL_tainted)
1460 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1462 SV *lsv = LvTARG(sv);
1469 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1474 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1476 do_vecset(sv); /* XXX slurp this routine */
1481 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1484 if (LvTARGLEN(sv)) {
1486 SV *ahv = LvTARG(sv);
1487 if (SvTYPE(ahv) == SVt_PVHV) {
1488 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1493 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1499 AV* av = (AV*)LvTARG(sv);
1500 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1501 targ = AvARRAY(av)[LvTARGOFF(sv)];
1503 if (targ && targ != &PL_sv_undef) {
1504 dTHR; /* just for SvREFCNT_dec */
1505 /* somebody else defined it for us */
1506 SvREFCNT_dec(LvTARG(sv));
1507 LvTARG(sv) = SvREFCNT_inc(targ);
1509 SvREFCNT_dec(mg->mg_obj);
1510 mg->mg_obj = Nullsv;
1511 mg->mg_flags &= ~MGf_REFCOUNTED;
1516 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1521 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1526 sv_setsv(LvTARG(sv), sv);
1527 SvSETMAGIC(LvTARG(sv));
1533 Perl_vivify_defelem(pTHX_ SV *sv)
1535 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1539 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1542 SV *ahv = LvTARG(sv);
1544 if (SvTYPE(ahv) == SVt_PVHV) {
1545 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1550 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1554 if (!value || value == &PL_sv_undef)
1555 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1558 AV* av = (AV*)LvTARG(sv);
1559 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1560 LvTARG(sv) = Nullsv; /* array can't be extended */
1562 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1563 if (!svp || (value = *svp) == &PL_sv_undef)
1564 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1567 (void)SvREFCNT_inc(value);
1568 SvREFCNT_dec(LvTARG(sv));
1571 SvREFCNT_dec(mg->mg_obj);
1572 mg->mg_obj = Nullsv;
1573 mg->mg_flags &= ~MGf_REFCOUNTED;
1577 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1579 AV *av = (AV*)mg->mg_obj;
1580 SV **svp = AvARRAY(av);
1581 I32 i = AvFILLp(av);
1583 if (svp[i] && svp[i] != &PL_sv_undef) {
1584 if (!SvWEAKREF(svp[i]))
1585 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1586 /* XXX Should we check that it hasn't changed? */
1588 (void)SvOK_off(svp[i]);
1589 SvWEAKREF_off(svp[i]);
1590 svp[i] = &PL_sv_undef;
1598 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1606 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1608 sv_unmagic(sv, 'B');
1614 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1616 sv_unmagic(sv, 'f');
1622 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1624 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1626 if (uf && uf->uf_set)
1627 (*uf->uf_set)(uf->uf_index, sv);
1632 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1634 regexp *re = (regexp *)mg->mg_obj;
1639 #ifdef USE_LOCALE_COLLATE
1641 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1644 * RenE<eacute> Descartes said "I think not."
1645 * and vanished with a faint plop.
1648 Safefree(mg->mg_ptr);
1654 #endif /* USE_LOCALE_COLLATE */
1657 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1663 switch (*mg->mg_ptr) {
1664 case '\001': /* ^A */
1665 sv_setsv(PL_bodytarget, sv);
1667 case '\003': /* ^C */
1668 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1671 case '\004': /* ^D */
1672 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1673 DEBUG_x(dump_all());
1675 case '\005': /* ^E */
1676 #ifdef MACOS_TRADITIONAL
1677 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1680 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1683 SetLastError( SvIV(sv) );
1686 /* will anyone ever use this? */
1687 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1693 case '\006': /* ^F */
1694 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1696 case '\010': /* ^H */
1697 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1699 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1701 Safefree(PL_inplace);
1703 PL_inplace = savepv(SvPV(sv,len));
1705 PL_inplace = Nullch;
1707 case '\017': /* ^O */
1709 Safefree(PL_osname);
1711 PL_osname = savepv(SvPV(sv,len));
1715 case '\020': /* ^P */
1716 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1717 if (PL_perldb && !PL_DBsingle)
1720 case '\024': /* ^T */
1722 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1724 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1727 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1728 if (*(mg->mg_ptr+1) == '\0') {
1729 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1730 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1731 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1732 | (i ? G_WARN_ON : G_WARN_OFF) ;
1735 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1736 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1737 if (!SvPOK(sv) && PL_localizing) {
1738 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1739 PL_compiling.cop_warnings = pWARN_NONE;
1742 if (isWARN_on(sv, WARN_ALL) && !isWARNf_on(sv, WARN_ALL)) {
1743 PL_compiling.cop_warnings = pWARN_ALL;
1744 PL_dowarn |= G_WARN_ONCE ;
1748 int accumulate = 0 ;
1749 char * ptr = (char*)SvPV(sv, len) ;
1750 for (i = 0 ; i < len ; ++i)
1751 accumulate += ptr[i] ;
1753 PL_compiling.cop_warnings = pWARN_NONE;
1755 if (specialWARN(PL_compiling.cop_warnings))
1756 PL_compiling.cop_warnings = newSVsv(sv) ;
1758 sv_setsv(PL_compiling.cop_warnings, sv);
1759 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1760 PL_dowarn |= G_WARN_ONCE ;
1765 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1766 PL_widesyscalls = SvTRUE(sv);
1769 if (PL_localizing) {
1770 if (PL_localizing == 1)
1771 SAVESPTR(PL_last_in_gv);
1773 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1774 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1777 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1778 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1779 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1782 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1783 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1784 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1787 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1790 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1791 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1792 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1795 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1799 IO *io = GvIOp(PL_defoutgv);
1800 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1801 IoFLAGS(io) &= ~IOf_FLUSH;
1803 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1804 PerlIO *ofp = IoOFP(io);
1806 (void)PerlIO_flush(ofp);
1807 IoFLAGS(io) |= IOf_FLUSH;
1813 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1814 PL_multiline = (i != 0);
1817 SvREFCNT_dec(PL_nrs);
1818 PL_nrs = newSVsv(sv);
1819 SvREFCNT_dec(PL_rs);
1820 PL_rs = SvREFCNT_inc(PL_nrs);
1825 if (SvOK(sv) || SvGMAGICAL(sv)) {
1826 s = SvPV(sv,PL_orslen);
1827 PL_ors = savepvn(s,PL_orslen);
1837 PL_ofs = savepv(SvPV(sv, PL_ofslen));
1842 PL_ofmt = savepv(SvPV(sv,len));
1845 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1848 #ifdef COMPLEX_STATUS
1849 if (PL_localizing == 2) {
1850 PL_statusvalue = LvTARGOFF(sv);
1851 PL_statusvalue_vms = LvTARGLEN(sv);
1855 #ifdef VMSISH_STATUS
1857 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1860 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1863 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1864 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1867 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1868 if (PL_delaymagic) {
1869 PL_delaymagic |= DM_RUID;
1870 break; /* don't do magic till later */
1873 (void)setruid((Uid_t)PL_uid);
1876 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1878 #ifdef HAS_SETRESUID
1879 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1881 if (PL_uid == PL_euid) /* special case $< = $> */
1882 (void)PerlProc_setuid(PL_uid);
1884 PL_uid = PerlProc_getuid();
1885 Perl_croak(aTHX_ "setruid() not implemented");
1890 PL_uid = PerlProc_getuid();
1891 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1894 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1895 if (PL_delaymagic) {
1896 PL_delaymagic |= DM_EUID;
1897 break; /* don't do magic till later */
1900 (void)seteuid((Uid_t)PL_euid);
1903 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1905 #ifdef HAS_SETRESUID
1906 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1908 if (PL_euid == PL_uid) /* special case $> = $< */
1909 PerlProc_setuid(PL_euid);
1911 PL_euid = PerlProc_geteuid();
1912 Perl_croak(aTHX_ "seteuid() not implemented");
1917 PL_euid = PerlProc_geteuid();
1918 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1921 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1922 if (PL_delaymagic) {
1923 PL_delaymagic |= DM_RGID;
1924 break; /* don't do magic till later */
1927 (void)setrgid((Gid_t)PL_gid);
1930 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1932 #ifdef HAS_SETRESGID
1933 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1935 if (PL_gid == PL_egid) /* special case $( = $) */
1936 (void)PerlProc_setgid(PL_gid);
1938 PL_gid = PerlProc_getgid();
1939 Perl_croak(aTHX_ "setrgid() not implemented");
1944 PL_gid = PerlProc_getgid();
1945 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1948 #ifdef HAS_SETGROUPS
1950 char *p = SvPV(sv, len);
1951 Groups_t gary[NGROUPS];
1956 for (i = 0; i < NGROUPS; ++i) {
1957 while (*p && !isSPACE(*p))
1966 (void)setgroups(i, gary);
1968 #else /* HAS_SETGROUPS */
1969 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1970 #endif /* HAS_SETGROUPS */
1971 if (PL_delaymagic) {
1972 PL_delaymagic |= DM_EGID;
1973 break; /* don't do magic till later */
1976 (void)setegid((Gid_t)PL_egid);
1979 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
1981 #ifdef HAS_SETRESGID
1982 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
1984 if (PL_egid == PL_gid) /* special case $) = $( */
1985 (void)PerlProc_setgid(PL_egid);
1987 PL_egid = PerlProc_getegid();
1988 Perl_croak(aTHX_ "setegid() not implemented");
1993 PL_egid = PerlProc_getegid();
1994 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1997 PL_chopset = SvPV_force(sv,len);
1999 #ifndef MACOS_TRADITIONAL
2001 #ifdef HAS_SETPROCTITLE
2002 /* The BSDs don't show the argv[] in ps(1) output, they
2003 * show a string from the process struct and provide
2004 * the setproctitle() routine to manipulate that. */
2007 # if __FreeBSD_version >= 410001
2008 /* The leading "-" removes the "perl: " prefix,
2009 * but not the "(perl) suffix from the ps(1)
2010 * output, because that's what ps(1) shows if the
2011 * argv[] is modified. */
2012 setproctitle("-%s", s, len + 1);
2013 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2014 /* This doesn't really work if you assume that
2015 * $0 = 'foobar'; will wipe out 'perl' from the $0
2016 * because in ps(1) output the result will be like
2017 * sprintf("perl: %s (perl)", s)
2018 * I guess this is a security feature:
2019 * one (a user process) cannot get rid of the original name.
2021 setproctitle("%s", s);
2028 /* See if all the arguments are contiguous in memory */
2029 for (i = 1; i < PL_origargc; i++) {
2030 if (PL_origargv[i] == s + 1
2032 || PL_origargv[i] == s + 2
2037 s += strlen(s); /* this one is ok too */
2042 /* can grab env area too? */
2043 if (PL_origenviron && (PL_origenviron[0] == s + 1
2045 || (PL_origenviron[0] == s + 9 && (s += 8))
2048 my_setenv("NoNe SuCh", Nullch);
2049 /* force copy of environment */
2050 for (i = 0; PL_origenviron[i]; i++)
2051 if (PL_origenviron[i] == s + 1) {
2058 PL_origalen = s - PL_origargv[0];
2060 s = SvPV_force(sv,len);
2062 if (i >= PL_origalen) {
2064 /* don't allow system to limit $0 seen by script */
2065 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2066 Copy(s, PL_origargv[0], i, char);
2067 s = PL_origargv[0]+i;
2071 Copy(s, PL_origargv[0], i, char);
2072 s = PL_origargv[0]+i;
2074 while (++i < PL_origalen)
2076 s = PL_origargv[0]+i;
2077 for (i = 1; i < PL_origargc; i++)
2078 PL_origargv[i] = Nullch;
2084 sv_setsv(thr->errsv, sv);
2086 #endif /* USE_THREADS */
2093 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2096 DEBUG_S(PerlIO_printf(Perl_debug_log,
2097 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2098 PTR2UV(thr), PTR2UV(sv));)
2100 Perl_croak(aTHX_ "panic: magic_mutexfree");
2101 MUTEX_DESTROY(MgMUTEXP(mg));
2102 COND_DESTROY(MgCONDP(mg));
2105 #endif /* USE_THREADS */
2108 Perl_whichsig(pTHX_ char *sig)
2110 register char **sigv;
2112 for (sigv = PL_sig_name+1; *sigv; sigv++)
2113 if (strEQ(sig,*sigv))
2114 return PL_sig_num[sigv - PL_sig_name];
2116 if (strEQ(sig,"CHLD"))
2120 if (strEQ(sig,"CLD"))
2129 Perl_sighandler(int sig)
2131 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2132 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2139 SV *sv, *tSv = PL_Sv;
2143 I32 o_save_i = PL_savestack_ix;
2146 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2147 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2150 if (PL_savestack_ix + 15 <= PL_savestack_max)
2152 if (PL_markstack_ptr < PL_markstack_max - 2)
2154 if (PL_retstack_ix < PL_retstack_max - 2)
2156 if (PL_scopestack_ix < PL_scopestack_max - 3)
2159 if (!PL_psig_ptr[sig])
2160 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2163 /* Max number of items pushed there is 3*n or 4. We cannot fix
2164 infinity, so we fix 4 (in fact 5): */
2166 PL_savestack_ix += 5; /* Protect save in progress. */
2167 o_save_i = PL_savestack_ix;
2168 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2171 PL_markstack_ptr++; /* Protect mark. */
2174 PL_retstack[PL_retstack_ix] = NULL;
2177 PL_scopestack_ix += 1;
2178 /* sv_2cv is too complicated, try a simpler variant first: */
2179 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2180 || SvTYPE(cv) != SVt_PVCV)
2181 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2183 if (!cv || !CvROOT(cv)) {
2184 if (ckWARN(WARN_SIGNAL))
2185 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2186 PL_sig_name[sig], (gv ? GvENAME(gv)
2193 if(PL_psig_name[sig]) {
2194 sv = SvREFCNT_inc(PL_psig_name[sig]);
2198 sv = sv_newmortal();
2199 sv_setpv(sv,PL_sig_name[sig]);
2202 PUSHSTACKi(PERLSI_SIGNAL);
2207 call_sv((SV*)cv, G_DISCARD);
2212 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2218 PL_scopestack_ix -= 1;
2221 PL_op = myop; /* Apparently not needed... */
2223 PL_Sv = tSv; /* Restore global temporaries. */
2234 restore_magic(pTHXo_ void *p)
2237 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2238 SV* sv = mgs->mgs_sv;
2243 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2246 SvFLAGS(sv) |= mgs->mgs_flags;
2250 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2253 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2255 /* If we're still on top of the stack, pop us off. (That condition
2256 * will be satisfied if restore_magic was called explicitly, but *not*
2257 * if it's being called via leave_scope.)
2258 * The reason for doing this is that otherwise, things like sv_2cv()
2259 * may leave alloc gunk on the savestack, and some code
2260 * (e.g. sighandler) doesn't expect that...
2262 if (PL_savestack_ix == mgs->mgs_ss_ix)
2264 I32 popval = SSPOPINT;
2265 assert(popval == SAVEt_DESTRUCTOR_X);
2266 PL_savestack_ix -= 2;
2268 assert(popval == SAVEt_ALLOC);
2270 PL_savestack_ix -= popval;
2276 unwind_handler_stack(pTHXo_ void *p)
2279 U32 flags = *(U32*)p;
2282 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2283 /* cxstack_ix-- Not needed, die already unwound it. */
2285 SvREFCNT_dec(sig_sv);