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) /* @+ */
383 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
386 Perl_croak(aTHX_ PL_no_modify);
392 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
400 switch (*mg->mg_ptr) {
401 case '1': case '2': case '3': case '4':
402 case '5': case '6': case '7': case '8': case '9': case '&':
403 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
405 paren = atoi(mg->mg_ptr);
407 if (paren <= rx->nparens &&
408 (s1 = rx->startp[paren]) != -1 &&
409 (t1 = rx->endp[paren]) != -1)
413 if (i > 0 && (PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE) {
414 char *s = rx->subbeg + s1;
415 char *send = rx->subbeg + t1;
428 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
429 paren = rx->lastparen;
435 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
436 if (rx->startp[0] != -1) {
447 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
448 if (rx->endp[0] != -1) {
449 i = rx->sublen - rx->endp[0];
459 return (STRLEN)PL_ofslen;
461 return (STRLEN)PL_orslen;
464 if (!SvPOK(sv) && SvNIOK(sv)) {
474 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
482 switch (*mg->mg_ptr) {
483 case '\001': /* ^A */
484 sv_setsv(sv, PL_bodytarget);
486 case '\003': /* ^C */
487 sv_setiv(sv, (IV)PL_minus_c);
490 case '\004': /* ^D */
491 sv_setiv(sv, (IV)(PL_debug & 32767));
492 #if defined(YYDEBUG) && defined(DEBUGGING)
493 PL_yydebug = (PL_debug & 1);
496 case '\005': /* ^E */
497 #ifdef MACOS_TRADITIONAL
501 sv_setnv(sv,(double)gMacPerl_OSErr);
502 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
507 # include <descrip.h>
508 # include <starlet.h>
510 $DESCRIPTOR(msgdsc,msg);
511 sv_setnv(sv,(NV) vaxc$errno);
512 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
513 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
519 if (!(_emx_env & 0x200)) { /* Under DOS */
520 sv_setnv(sv, (NV)errno);
521 sv_setpv(sv, errno ? Strerror(errno) : "");
523 if (errno != errno_isOS2) {
524 int tmp = _syserrno();
525 if (tmp) /* 2nd call to _syserrno() makes it 0 */
528 sv_setnv(sv, (NV)Perl_rc);
529 sv_setpv(sv, os2error(Perl_rc));
534 DWORD dwErr = GetLastError();
535 sv_setnv(sv, (NV)dwErr);
538 PerlProc_GetOSError(sv, dwErr);
545 sv_setnv(sv, (NV)errno);
546 sv_setpv(sv, errno ? Strerror(errno) : "");
551 SvNOK_on(sv); /* what a wonderful hack! */
553 case '\006': /* ^F */
554 sv_setiv(sv, (IV)PL_maxsysfd);
556 case '\010': /* ^H */
557 sv_setiv(sv, (IV)PL_hints);
559 case '\011': /* ^I */ /* NOT \t in EBCDIC */
561 sv_setpv(sv, PL_inplace);
563 sv_setsv(sv, &PL_sv_undef);
565 case '\017': /* ^O */
566 sv_setpv(sv, PL_osname);
568 case '\020': /* ^P */
569 sv_setiv(sv, (IV)PL_perldb);
571 case '\023': /* ^S */
574 if (PL_lex_state != LEX_NOTPARSING)
582 case '\024': /* ^T */
584 sv_setnv(sv, PL_basetime);
586 sv_setiv(sv, (IV)PL_basetime);
589 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
590 if (*(mg->mg_ptr+1) == '\0')
591 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
592 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
593 if (PL_compiling.cop_warnings == pWARN_NONE ||
594 PL_compiling.cop_warnings == pWARN_STD)
596 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
598 else if (PL_compiling.cop_warnings == pWARN_ALL) {
599 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
602 sv_setsv(sv, PL_compiling.cop_warnings);
606 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
607 sv_setiv(sv, (IV)PL_widesyscalls);
609 case '1': case '2': case '3': case '4':
610 case '5': case '6': case '7': case '8': case '9': case '&':
611 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
615 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
616 * XXX Does the new way break anything?
618 paren = atoi(mg->mg_ptr);
620 if (paren <= rx->nparens &&
621 (s1 = rx->startp[paren]) != -1 &&
622 (t1 = rx->endp[paren]) != -1)
633 was_tainted = PL_tainted;
637 if ((PL_curpm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE)
642 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
647 sv_setsv(sv,&PL_sv_undef);
650 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
651 paren = rx->lastparen;
655 sv_setsv(sv,&PL_sv_undef);
658 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
659 if ((s = rx->subbeg) && rx->startp[0] != -1) {
664 sv_setsv(sv,&PL_sv_undef);
667 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
668 if (rx->subbeg && rx->endp[0] != -1) {
669 s = rx->subbeg + rx->endp[0];
670 i = rx->sublen - rx->endp[0];
674 sv_setsv(sv,&PL_sv_undef);
678 if (GvIO(PL_last_in_gv)) {
679 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
685 sv_setiv(sv, (IV)STATUS_CURRENT);
686 #ifdef COMPLEX_STATUS
687 LvTARGOFF(sv) = PL_statusvalue;
688 LvTARGLEN(sv) = PL_statusvalue_vms;
693 s = IoTOP_NAME(GvIOp(PL_defoutgv));
697 sv_setpv(sv,GvENAME(PL_defoutgv));
702 s = IoFMT_NAME(GvIOp(PL_defoutgv));
704 s = GvENAME(PL_defoutgv);
709 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
712 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
715 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
723 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
726 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
729 sv_setpvn(sv,PL_ofs,PL_ofslen);
732 sv_setpvn(sv,PL_ors,PL_orslen);
735 sv_setpv(sv,PL_ofmt);
739 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
740 sv_setpv(sv, errno ? Strerror(errno) : "");
743 int saveerrno = errno;
744 sv_setnv(sv, (NV)errno);
746 if (errno == errno_isOS2 || errno == errno_isOS2_set)
747 sv_setpv(sv, os2error(Perl_rc));
750 sv_setpv(sv, errno ? Strerror(errno) : "");
754 SvNOK_on(sv); /* what a wonderful hack! */
757 sv_setiv(sv, (IV)PL_uid);
760 sv_setiv(sv, (IV)PL_euid);
763 sv_setiv(sv, (IV)PL_gid);
765 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
769 sv_setiv(sv, (IV)PL_egid);
771 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
776 Groups_t gary[NGROUPS];
777 i = getgroups(NGROUPS,gary);
779 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
782 (void)SvIOK_on(sv); /* what a wonderful hack! */
786 #ifndef MACOS_TRADITIONAL
792 sv_setsv(sv, thr->errsv);
794 #endif /* USE_THREADS */
800 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
802 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
804 if (uf && uf->uf_val)
805 (*uf->uf_val)(uf->uf_index, sv);
810 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
821 #ifdef DYNAMIC_ENV_FETCH
822 /* We just undefd an environment var. Is a replacement */
823 /* waiting in the wings? */
826 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
827 s = SvPV(*valp, len);
831 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
832 /* And you'll never guess what the dog had */
833 /* in its mouth... */
835 MgTAINTEDDIR_off(mg);
837 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
838 char pathbuf[256], eltbuf[256], *cp, *elt = s;
842 do { /* DCL$PATH may be a search list */
843 while (1) { /* as may dev portion of any element */
844 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
845 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
846 cando_by_name(S_IWUSR,0,elt) ) {
851 if ((cp = strchr(elt, ':')) != Nullch)
853 if (my_trnlnm(elt, eltbuf, j++))
859 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
862 if (s && klen == 4 && strEQ(ptr,"PATH")) {
863 char *strend = s + len;
868 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
871 if (i >= sizeof tmpbuf /* too long -- assume the worst */
873 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
880 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
886 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
889 my_setenv(MgPV(mg,n_a),Nullch);
894 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
897 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
903 magic_clear_all_env(sv,mg);
904 hv_iterinit((HV*)sv);
905 while ((entry = hv_iternext((HV*)sv))) {
907 my_setenv(hv_iterkey(entry, &keylen),
908 SvPV(hv_iterval((HV*)sv, entry), n_a));
916 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
919 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
921 # ifdef PERL_IMPLICIT_SYS
925 char *envv = GetEnvironmentStrings();
929 char *end = strchr(cur,'=');
930 if (end && end != cur) {
932 my_setenv(cur,Nullch);
934 cur = end + strlen(end+1)+2;
936 else if ((len = strlen(cur)))
939 FreeEnvironmentStrings(envv);
943 for (i = 0; environ[i]; i++)
944 safesysfree(environ[i]);
946 # ifndef PERL_USE_SAFE_PUTENV
949 if (environ == PL_origenviron)
950 environ = (char**)safesysmalloc(sizeof(char*));
952 for (i = 0; environ[i]; i++)
953 safesysfree(environ[i]);
954 # endif /* PERL_USE_SAFE_PUTENV */
955 # endif /* __CYGWIN__ */
960 # endif /* PERL_IMPLICIT_SYS */
967 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
971 /* Are we fetching a signal entry? */
972 i = whichsig(MgPV(mg,n_a));
975 sv_setsv(sv,PL_psig_ptr[i]);
977 Sighandler_t sigstate = rsignal_state(i);
979 /* cache state so we don't fetch it again */
980 if(sigstate == SIG_IGN)
981 sv_setpv(sv,"IGNORE");
983 sv_setsv(sv,&PL_sv_undef);
984 PL_psig_ptr[i] = SvREFCNT_inc(sv);
991 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
995 /* Are we clearing a signal entry? */
996 i = whichsig(MgPV(mg,n_a));
999 SvREFCNT_dec(PL_psig_ptr[i]);
1002 if(PL_psig_name[i]) {
1003 SvREFCNT_dec(PL_psig_name[i]);
1011 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1021 if (strEQ(s,"__DIE__"))
1023 else if (strEQ(s,"__WARN__"))
1026 Perl_croak(aTHX_ "No such hook: %s", s);
1034 i = whichsig(s); /* ...no, a brick */
1036 if (ckWARN(WARN_SIGNAL))
1037 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1040 SvREFCNT_dec(PL_psig_name[i]);
1041 SvREFCNT_dec(PL_psig_ptr[i]);
1042 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1043 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1044 PL_psig_name[i] = newSVpvn(s, len);
1045 SvREADONLY_on(PL_psig_name[i]);
1047 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1049 (void)rsignal(i, PL_sighandlerp);
1051 *svp = SvREFCNT_inc(sv);
1054 s = SvPV_force(sv,len);
1055 if (strEQ(s,"IGNORE")) {
1057 (void)rsignal(i, SIG_IGN);
1061 else if (strEQ(s,"DEFAULT") || !*s) {
1063 (void)rsignal(i, SIG_DFL);
1069 * We should warn if HINT_STRICT_REFS, but without
1070 * access to a known hint bit in a known OP, we can't
1071 * tell whether HINT_STRICT_REFS is in force or not.
1073 if (!strchr(s,':') && !strchr(s,'\''))
1074 sv_insert(sv, 0, 0, "main::", 6);
1076 (void)rsignal(i, PL_sighandlerp);
1078 *svp = SvREFCNT_inc(sv);
1082 #endif /* !PERL_MICRO */
1085 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1087 PL_sub_generation++;
1092 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1094 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1095 PL_amagic_generation++;
1101 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1103 HV *hv = (HV*)LvTARG(sv);
1108 (void) hv_iterinit(hv);
1109 if (! SvTIED_mg((SV*)hv, 'P'))
1113 while ((entry = hv_iternext(hv))) {
1119 sv_setiv(sv, (IV)i);
1124 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1127 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1132 /* caller is responsible for stack switching/cleanup */
1134 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1140 PUSHs(SvTIED_obj(sv, mg));
1143 if (mg->mg_len >= 0)
1144 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1145 else if (mg->mg_len == HEf_SVKEY)
1146 PUSHs((SV*)mg->mg_ptr);
1148 else if (mg->mg_type == 'p') {
1149 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1157 return call_method(meth, flags);
1161 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1167 PUSHSTACKi(PERLSI_MAGIC);
1169 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1170 sv_setsv(sv, *PL_stack_sp--);
1180 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1182 magic_methpack(sv,mg,"FETCH");
1184 mg->mg_flags |= MGf_GSKIP;
1189 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1193 PUSHSTACKi(PERLSI_MAGIC);
1194 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1201 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1203 return magic_methpack(sv,mg,"DELETE");
1208 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1215 PUSHSTACKi(PERLSI_MAGIC);
1216 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1217 sv = *PL_stack_sp--;
1218 retval = (U32) SvIV(sv)-1;
1227 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1232 PUSHSTACKi(PERLSI_MAGIC);
1234 XPUSHs(SvTIED_obj(sv, mg));
1236 call_method("CLEAR", G_SCALAR|G_DISCARD);
1243 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1246 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1250 PUSHSTACKi(PERLSI_MAGIC);
1253 PUSHs(SvTIED_obj(sv, mg));
1258 if (call_method(meth, G_SCALAR))
1259 sv_setsv(key, *PL_stack_sp--);
1268 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1270 return magic_methpack(sv,mg,"EXISTS");
1274 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1285 svp = av_fetch(GvAV(gv),
1286 atoi(MgPV(mg,n_a)), FALSE);
1287 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1293 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1296 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1301 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1304 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1309 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1311 SV* lsv = LvTARG(sv);
1313 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1314 mg = mg_find(lsv, 'g');
1315 if (mg && mg->mg_len >= 0) {
1319 sv_pos_b2u(lsv, &i);
1320 sv_setiv(sv, i + PL_curcop->cop_arybase);
1329 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1331 SV* lsv = LvTARG(sv);
1339 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1340 mg = mg_find(lsv, 'g');
1344 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1345 mg = mg_find(lsv, 'g');
1347 else if (!SvOK(sv)) {
1351 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1353 pos = SvIV(sv) - PL_curcop->cop_arybase;
1356 ulen = sv_len_utf8(lsv);
1371 sv_pos_u2b(lsv, &p, 0);
1376 mg->mg_flags &= ~MGf_MINMATCH;
1382 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1384 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1386 gv_efullname3(sv,((GV*)sv), "*");
1390 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1395 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1404 if (*s == '*' && s[1])
1406 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1411 GvGP(sv) = gp_ref(GvGP(gv));
1416 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1419 SV *lsv = LvTARG(sv);
1420 char *tmps = SvPV(lsv,len);
1421 I32 offs = LvTARGOFF(sv);
1422 I32 rem = LvTARGLEN(sv);
1426 if (rem + offs > len)
1428 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1433 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1436 char *tmps = SvPV(sv,len);
1437 sv_insert(LvTARG(sv),LvTARGOFF(sv),LvTARGLEN(sv), tmps, len);
1442 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1445 TAINT_IF((mg->mg_len & 1) ||
1446 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1451 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1454 if (PL_localizing) {
1455 if (PL_localizing == 1)
1460 else if (PL_tainted)
1468 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1470 SV *lsv = LvTARG(sv);
1477 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1482 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1484 do_vecset(sv); /* XXX slurp this routine */
1489 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1492 if (LvTARGLEN(sv)) {
1494 SV *ahv = LvTARG(sv);
1495 if (SvTYPE(ahv) == SVt_PVHV) {
1496 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1501 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1507 AV* av = (AV*)LvTARG(sv);
1508 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1509 targ = AvARRAY(av)[LvTARGOFF(sv)];
1511 if (targ && targ != &PL_sv_undef) {
1512 dTHR; /* just for SvREFCNT_dec */
1513 /* somebody else defined it for us */
1514 SvREFCNT_dec(LvTARG(sv));
1515 LvTARG(sv) = SvREFCNT_inc(targ);
1517 SvREFCNT_dec(mg->mg_obj);
1518 mg->mg_obj = Nullsv;
1519 mg->mg_flags &= ~MGf_REFCOUNTED;
1524 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1529 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1534 sv_setsv(LvTARG(sv), sv);
1535 SvSETMAGIC(LvTARG(sv));
1541 Perl_vivify_defelem(pTHX_ SV *sv)
1543 dTHR; /* just for SvREFCNT_inc and SvREFCNT_dec*/
1547 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, 'y')))
1550 SV *ahv = LvTARG(sv);
1552 if (SvTYPE(ahv) == SVt_PVHV) {
1553 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
1558 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, TRUE, 0);
1562 if (!value || value == &PL_sv_undef)
1563 Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a));
1566 AV* av = (AV*)LvTARG(sv);
1567 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
1568 LvTARG(sv) = Nullsv; /* array can't be extended */
1570 SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE);
1571 if (!svp || (value = *svp) == &PL_sv_undef)
1572 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
1575 (void)SvREFCNT_inc(value);
1576 SvREFCNT_dec(LvTARG(sv));
1579 SvREFCNT_dec(mg->mg_obj);
1580 mg->mg_obj = Nullsv;
1581 mg->mg_flags &= ~MGf_REFCOUNTED;
1585 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
1587 AV *av = (AV*)mg->mg_obj;
1588 SV **svp = AvARRAY(av);
1589 I32 i = AvFILLp(av);
1591 if (svp[i] && svp[i] != &PL_sv_undef) {
1592 if (!SvWEAKREF(svp[i]))
1593 Perl_croak(aTHX_ "panic: magic_killbackrefs");
1594 /* XXX Should we check that it hasn't changed? */
1596 (void)SvOK_off(svp[i]);
1597 SvWEAKREF_off(svp[i]);
1598 svp[i] = &PL_sv_undef;
1606 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
1614 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
1616 sv_unmagic(sv, 'B');
1622 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
1624 sv_unmagic(sv, 'f');
1630 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
1632 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
1634 if (uf && uf->uf_set)
1635 (*uf->uf_set)(uf->uf_index, sv);
1640 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
1642 regexp *re = (regexp *)mg->mg_obj;
1647 #ifdef USE_LOCALE_COLLATE
1649 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
1652 * RenE<eacute> Descartes said "I think not."
1653 * and vanished with a faint plop.
1656 Safefree(mg->mg_ptr);
1662 #endif /* USE_LOCALE_COLLATE */
1665 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
1671 switch (*mg->mg_ptr) {
1672 case '\001': /* ^A */
1673 sv_setsv(PL_bodytarget, sv);
1675 case '\003': /* ^C */
1676 PL_minus_c = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1679 case '\004': /* ^D */
1680 PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | 0x80000000;
1681 DEBUG_x(dump_all());
1683 case '\005': /* ^E */
1684 #ifdef MACOS_TRADITIONAL
1685 gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1688 set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1691 SetLastError( SvIV(sv) );
1694 /* will anyone ever use this? */
1695 SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4);
1701 case '\006': /* ^F */
1702 PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1704 case '\010': /* ^H */
1705 PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1707 case '\011': /* ^I */ /* NOT \t in EBCDIC */
1709 Safefree(PL_inplace);
1711 PL_inplace = savepv(SvPV(sv,len));
1713 PL_inplace = Nullch;
1715 case '\017': /* ^O */
1717 Safefree(PL_osname);
1719 PL_osname = savepv(SvPV(sv,len));
1723 case '\020': /* ^P */
1724 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1725 if (PL_perldb && !PL_DBsingle)
1728 case '\024': /* ^T */
1730 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1732 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1735 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1736 if (*(mg->mg_ptr+1) == '\0') {
1737 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1738 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1739 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1740 | (i ? G_WARN_ON : G_WARN_OFF) ;
1743 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1744 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1745 if (!SvPOK(sv) && PL_localizing) {
1746 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1747 PL_compiling.cop_warnings = pWARN_NONE;
1750 if (isWARN_on(sv, WARN_ALL) && !isWARNf_on(sv, WARN_ALL)) {
1751 PL_compiling.cop_warnings = pWARN_ALL;
1752 PL_dowarn |= G_WARN_ONCE ;
1756 int accumulate = 0 ;
1757 char * ptr = (char*)SvPV(sv, len) ;
1758 for (i = 0 ; i < len ; ++i)
1759 accumulate += ptr[i] ;
1761 PL_compiling.cop_warnings = pWARN_NONE;
1763 if (specialWARN(PL_compiling.cop_warnings))
1764 PL_compiling.cop_warnings = newSVsv(sv) ;
1766 sv_setsv(PL_compiling.cop_warnings, sv);
1767 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1768 PL_dowarn |= G_WARN_ONCE ;
1773 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1774 PL_widesyscalls = SvTRUE(sv);
1777 if (PL_localizing) {
1778 if (PL_localizing == 1)
1779 SAVESPTR(PL_last_in_gv);
1781 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1782 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1785 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1786 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1787 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1790 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1791 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1792 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1795 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1798 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1799 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1800 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1803 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1807 IO *io = GvIOp(PL_defoutgv);
1808 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1809 IoFLAGS(io) &= ~IOf_FLUSH;
1811 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1812 PerlIO *ofp = IoOFP(io);
1814 (void)PerlIO_flush(ofp);
1815 IoFLAGS(io) |= IOf_FLUSH;
1821 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1822 PL_multiline = (i != 0);
1825 SvREFCNT_dec(PL_nrs);
1826 PL_nrs = newSVsv(sv);
1827 SvREFCNT_dec(PL_rs);
1828 PL_rs = SvREFCNT_inc(PL_nrs);
1833 if (SvOK(sv) || SvGMAGICAL(sv)) {
1834 s = SvPV(sv,PL_orslen);
1835 PL_ors = savepvn(s,PL_orslen);
1845 PL_ofs = savepv(SvPV(sv, PL_ofslen));
1850 PL_ofmt = savepv(SvPV(sv,len));
1853 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1856 #ifdef COMPLEX_STATUS
1857 if (PL_localizing == 2) {
1858 PL_statusvalue = LvTARGOFF(sv);
1859 PL_statusvalue_vms = LvTARGLEN(sv);
1863 #ifdef VMSISH_STATUS
1865 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1868 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1871 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1872 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1875 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1876 if (PL_delaymagic) {
1877 PL_delaymagic |= DM_RUID;
1878 break; /* don't do magic till later */
1881 (void)setruid((Uid_t)PL_uid);
1884 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1886 #ifdef HAS_SETRESUID
1887 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1889 if (PL_uid == PL_euid) /* special case $< = $> */
1890 (void)PerlProc_setuid(PL_uid);
1892 PL_uid = PerlProc_getuid();
1893 Perl_croak(aTHX_ "setruid() not implemented");
1898 PL_uid = PerlProc_getuid();
1899 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1902 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1903 if (PL_delaymagic) {
1904 PL_delaymagic |= DM_EUID;
1905 break; /* don't do magic till later */
1908 (void)seteuid((Uid_t)PL_euid);
1911 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1913 #ifdef HAS_SETRESUID
1914 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1916 if (PL_euid == PL_uid) /* special case $> = $< */
1917 PerlProc_setuid(PL_euid);
1919 PL_euid = PerlProc_geteuid();
1920 Perl_croak(aTHX_ "seteuid() not implemented");
1925 PL_euid = PerlProc_geteuid();
1926 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1929 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1930 if (PL_delaymagic) {
1931 PL_delaymagic |= DM_RGID;
1932 break; /* don't do magic till later */
1935 (void)setrgid((Gid_t)PL_gid);
1938 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1940 #ifdef HAS_SETRESGID
1941 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1943 if (PL_gid == PL_egid) /* special case $( = $) */
1944 (void)PerlProc_setgid(PL_gid);
1946 PL_gid = PerlProc_getgid();
1947 Perl_croak(aTHX_ "setrgid() not implemented");
1952 PL_gid = PerlProc_getgid();
1953 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1956 #ifdef HAS_SETGROUPS
1958 char *p = SvPV(sv, len);
1959 Groups_t gary[NGROUPS];
1964 for (i = 0; i < NGROUPS; ++i) {
1965 while (*p && !isSPACE(*p))
1974 (void)setgroups(i, gary);
1976 #else /* HAS_SETGROUPS */
1977 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1978 #endif /* HAS_SETGROUPS */
1979 if (PL_delaymagic) {
1980 PL_delaymagic |= DM_EGID;
1981 break; /* don't do magic till later */
1984 (void)setegid((Gid_t)PL_egid);
1987 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
1989 #ifdef HAS_SETRESGID
1990 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
1992 if (PL_egid == PL_gid) /* special case $) = $( */
1993 (void)PerlProc_setgid(PL_egid);
1995 PL_egid = PerlProc_getegid();
1996 Perl_croak(aTHX_ "setegid() not implemented");
2001 PL_egid = PerlProc_getegid();
2002 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2005 PL_chopset = SvPV_force(sv,len);
2007 #ifndef MACOS_TRADITIONAL
2009 #ifdef HAS_SETPROCTITLE
2010 /* The BSDs don't show the argv[] in ps(1) output, they
2011 * show a string from the process struct and provide
2012 * the setproctitle() routine to manipulate that. */
2015 # if __FreeBSD_version >= 410001
2016 /* The leading "-" removes the "perl: " prefix,
2017 * but not the "(perl) suffix from the ps(1)
2018 * output, because that's what ps(1) shows if the
2019 * argv[] is modified. */
2020 setproctitle("-%s", s, len + 1);
2021 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2022 /* This doesn't really work if you assume that
2023 * $0 = 'foobar'; will wipe out 'perl' from the $0
2024 * because in ps(1) output the result will be like
2025 * sprintf("perl: %s (perl)", s)
2026 * I guess this is a security feature:
2027 * one (a user process) cannot get rid of the original name.
2029 setproctitle("%s", s);
2036 /* See if all the arguments are contiguous in memory */
2037 for (i = 1; i < PL_origargc; i++) {
2038 if (PL_origargv[i] == s + 1
2040 || PL_origargv[i] == s + 2
2045 s += strlen(s); /* this one is ok too */
2050 /* can grab env area too? */
2051 if (PL_origenviron && (PL_origenviron[0] == s + 1
2053 || (PL_origenviron[0] == s + 9 && (s += 8))
2056 my_setenv("NoNe SuCh", Nullch);
2057 /* force copy of environment */
2058 for (i = 0; PL_origenviron[i]; i++)
2059 if (PL_origenviron[i] == s + 1) {
2066 PL_origalen = s - PL_origargv[0];
2068 s = SvPV_force(sv,len);
2070 if (i >= PL_origalen) {
2072 /* don't allow system to limit $0 seen by script */
2073 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2074 Copy(s, PL_origargv[0], i, char);
2075 s = PL_origargv[0]+i;
2079 Copy(s, PL_origargv[0], i, char);
2080 s = PL_origargv[0]+i;
2082 while (++i < PL_origalen)
2084 s = PL_origargv[0]+i;
2085 for (i = 1; i < PL_origargc; i++)
2086 PL_origargv[i] = Nullch;
2092 sv_setsv(thr->errsv, sv);
2094 #endif /* USE_THREADS */
2101 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2104 DEBUG_S(PerlIO_printf(Perl_debug_log,
2105 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2106 PTR2UV(thr), PTR2UV(sv));)
2108 Perl_croak(aTHX_ "panic: magic_mutexfree");
2109 MUTEX_DESTROY(MgMUTEXP(mg));
2110 COND_DESTROY(MgCONDP(mg));
2113 #endif /* USE_THREADS */
2116 Perl_whichsig(pTHX_ char *sig)
2118 register char **sigv;
2120 for (sigv = PL_sig_name+1; *sigv; sigv++)
2121 if (strEQ(sig,*sigv))
2122 return PL_sig_num[sigv - PL_sig_name];
2124 if (strEQ(sig,"CHLD"))
2128 if (strEQ(sig,"CLD"))
2137 Perl_sighandler(int sig)
2139 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2140 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2147 SV *sv, *tSv = PL_Sv;
2151 I32 o_save_i = PL_savestack_ix;
2154 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2155 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2158 if (PL_savestack_ix + 15 <= PL_savestack_max)
2160 if (PL_markstack_ptr < PL_markstack_max - 2)
2162 if (PL_retstack_ix < PL_retstack_max - 2)
2164 if (PL_scopestack_ix < PL_scopestack_max - 3)
2167 if (!PL_psig_ptr[sig])
2168 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2171 /* Max number of items pushed there is 3*n or 4. We cannot fix
2172 infinity, so we fix 4 (in fact 5): */
2174 PL_savestack_ix += 5; /* Protect save in progress. */
2175 o_save_i = PL_savestack_ix;
2176 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2179 PL_markstack_ptr++; /* Protect mark. */
2182 PL_retstack[PL_retstack_ix] = NULL;
2185 PL_scopestack_ix += 1;
2186 /* sv_2cv is too complicated, try a simpler variant first: */
2187 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2188 || SvTYPE(cv) != SVt_PVCV)
2189 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2191 if (!cv || !CvROOT(cv)) {
2192 if (ckWARN(WARN_SIGNAL))
2193 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2194 PL_sig_name[sig], (gv ? GvENAME(gv)
2201 if(PL_psig_name[sig]) {
2202 sv = SvREFCNT_inc(PL_psig_name[sig]);
2206 sv = sv_newmortal();
2207 sv_setpv(sv,PL_sig_name[sig]);
2210 PUSHSTACKi(PERLSI_SIGNAL);
2215 call_sv((SV*)cv, G_DISCARD);
2220 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2226 PL_scopestack_ix -= 1;
2229 PL_op = myop; /* Apparently not needed... */
2231 PL_Sv = tSv; /* Restore global temporaries. */
2242 restore_magic(pTHXo_ void *p)
2245 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2246 SV* sv = mgs->mgs_sv;
2251 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2254 SvFLAGS(sv) |= mgs->mgs_flags;
2258 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2261 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2263 /* If we're still on top of the stack, pop us off. (That condition
2264 * will be satisfied if restore_magic was called explicitly, but *not*
2265 * if it's being called via leave_scope.)
2266 * The reason for doing this is that otherwise, things like sv_2cv()
2267 * may leave alloc gunk on the savestack, and some code
2268 * (e.g. sighandler) doesn't expect that...
2270 if (PL_savestack_ix == mgs->mgs_ss_ix)
2272 I32 popval = SSPOPINT;
2273 assert(popval == SAVEt_DESTRUCTOR_X);
2274 PL_savestack_ix -= 2;
2276 assert(popval == SAVEt_ALLOC);
2278 PL_savestack_ix -= popval;
2284 unwind_handler_stack(pTHXo_ void *p)
2287 U32 flags = *(U32*)p;
2290 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2291 /* cxstack_ix-- Not needed, die already unwound it. */
2293 SvREFCNT_dec(sig_sv);