3 * Copyright (c) 1991-2001, 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 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
25 static void restore_magic(pTHXo_ void *p);
26 static void unwind_handler_stack(pTHXo_ void *p);
29 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
37 /* MGS is typedef'ed to struct magic_state in perl.h */
40 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
43 assert(SvMAGICAL(sv));
45 SAVEDESTRUCTOR_X(restore_magic, (void*)mgs_ix);
47 mgs = SSPTR(mgs_ix, MGS*);
49 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
50 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
54 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
58 =for apidoc mg_magical
60 Turns on the magical status of an SV. See C<sv_magic>.
66 Perl_mg_magical(pTHX_ SV *sv)
69 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
70 MGVTBL* vtbl = mg->mg_virtual;
72 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
76 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
85 Do magic after a value is retrieved from the SV. See C<sv_magic>.
91 Perl_mg_get(pTHX_ SV *sv)
98 mgs_ix = SSNEW(sizeof(MGS));
99 save_magic(mgs_ix, sv);
102 while ((mg = *mgp) != 0) {
103 MGVTBL* vtbl = mg->mg_virtual;
104 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
105 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
106 /* Ignore this magic if it's been deleted */
107 if ((mg == (mgp_valid ? *mgp : SvMAGIC(sv))) &&
108 (mg->mg_flags & MGf_GSKIP))
109 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
111 /* Advance to next magic (complicated by possible deletion) */
112 if (mg == (mgp_valid ? *mgp : SvMAGIC(sv))) {
113 mgp = &mg->mg_moremagic;
117 mgp = &SvMAGIC(sv); /* Re-establish pointer after sv_upgrade */
120 restore_magic(aTHXo_ (void*)mgs_ix);
127 Do magic after a value is assigned to the SV. See C<sv_magic>.
133 Perl_mg_set(pTHX_ SV *sv)
139 mgs_ix = SSNEW(sizeof(MGS));
140 save_magic(mgs_ix, sv);
142 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
143 MGVTBL* vtbl = mg->mg_virtual;
144 nextmg = mg->mg_moremagic; /* it may delete itself */
145 if (mg->mg_flags & MGf_GSKIP) {
146 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
147 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
149 if (vtbl && vtbl->svt_set)
150 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
153 restore_magic(aTHXo_ (void*)mgs_ix);
158 =for apidoc mg_length
160 Report on the SV's length. See C<sv_magic>.
166 Perl_mg_length(pTHX_ SV *sv)
172 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
173 MGVTBL* vtbl = mg->mg_virtual;
174 if (vtbl && vtbl->svt_len) {
177 mgs_ix = SSNEW(sizeof(MGS));
178 save_magic(mgs_ix, sv);
179 /* omit MGf_GSKIP -- not changed here */
180 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
181 restore_magic(aTHXo_ (void*)mgs_ix);
186 junk = SvPV(sv, len);
191 Perl_mg_size(pTHX_ SV *sv)
196 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
197 MGVTBL* vtbl = mg->mg_virtual;
198 if (vtbl && vtbl->svt_len) {
201 mgs_ix = SSNEW(sizeof(MGS));
202 save_magic(mgs_ix, sv);
203 /* omit MGf_GSKIP -- not changed here */
204 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
205 restore_magic(aTHXo_ (void*)mgs_ix);
212 len = AvFILLp((AV *) sv); /* Fallback to non-tied array */
217 Perl_croak(aTHX_ "Size magic not implemented");
226 Clear something magical that the SV represents. See C<sv_magic>.
232 Perl_mg_clear(pTHX_ SV *sv)
237 mgs_ix = SSNEW(sizeof(MGS));
238 save_magic(mgs_ix, sv);
240 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
241 MGVTBL* vtbl = mg->mg_virtual;
242 /* omit GSKIP -- never set here */
244 if (vtbl && vtbl->svt_clear)
245 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
248 restore_magic(aTHXo_ (void*)mgs_ix);
255 Finds the magic pointer for type matching the SV. See C<sv_magic>.
261 Perl_mg_find(pTHX_ SV *sv, int type)
266 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
267 if (mg->mg_type == type)
276 Copies the magic from one SV to another. See C<sv_magic>.
282 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
286 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
287 if (isUPPER(mg->mg_type)) {
289 mg->mg_type == 'P' ? SvTIED_obj(sv, mg) :
290 (mg->mg_type == 'D' && mg->mg_obj) ? sv : mg->mg_obj,
291 toLOWER(mg->mg_type), key, klen);
301 Free any magic storage used by the SV. See C<sv_magic>.
307 Perl_mg_free(pTHX_ SV *sv)
311 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
312 MGVTBL* vtbl = mg->mg_virtual;
313 moremagic = mg->mg_moremagic;
314 if (vtbl && vtbl->svt_free)
315 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
316 if (mg->mg_ptr && mg->mg_type != 'g') {
318 Safefree(mg->mg_ptr);
319 else if (mg->mg_len == HEf_SVKEY)
320 SvREFCNT_dec((SV*)mg->mg_ptr);
322 if (mg->mg_flags & MGf_REFCOUNTED)
323 SvREFCNT_dec(mg->mg_obj);
331 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
336 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
340 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
341 if (mg->mg_obj) /* @+ */
344 return rx->lastparen;
351 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
359 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
363 if (paren <= rx->nparens &&
364 (s = rx->startp[paren]) != -1 &&
365 (t = rx->endp[paren]) != -1)
367 if (mg->mg_obj) /* @+ */
372 if (i > 0 && DO_UTF8(PL_reg_sv)) {
373 char *b = rx->subbeg;
374 i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i));
383 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
385 Perl_croak(aTHX_ PL_no_modify);
391 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
398 switch (*mg->mg_ptr) {
399 case '1': case '2': case '3': case '4':
400 case '5': case '6': case '7': case '8': case '9': case '&':
401 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
403 paren = atoi(mg->mg_ptr); /* $& is in [0] */
405 if (paren <= rx->nparens &&
406 (s1 = rx->startp[paren]) != -1 &&
407 (t1 = rx->endp[paren]) != -1)
411 if (i > 0 && DO_UTF8(PL_reg_sv)) {
412 char *s = rx->subbeg + s1;
413 char *send = rx->subbeg + t1;
415 i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send);
418 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
424 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
425 paren = rx->lastparen;
431 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
432 if (rx->startp[0] != -1) {
443 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
444 if (rx->endp[0] != -1) {
445 i = rx->sublen - rx->endp[0];
456 if (!SvPOK(sv) && SvNIOK(sv)) {
466 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
473 switch (*mg->mg_ptr) {
474 case '\001': /* ^A */
475 sv_setsv(sv, PL_bodytarget);
477 case '\003': /* ^C */
478 sv_setiv(sv, (IV)PL_minus_c);
481 case '\004': /* ^D */
482 sv_setiv(sv, (IV)(PL_debug & 32767));
483 #if defined(YYDEBUG) && defined(DEBUGGING)
484 PL_yydebug = (PL_debug & 1);
487 case '\005': /* ^E */
488 #ifdef MACOS_TRADITIONAL
492 sv_setnv(sv,(double)gMacPerl_OSErr);
493 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
498 # include <descrip.h>
499 # include <starlet.h>
501 $DESCRIPTOR(msgdsc,msg);
502 sv_setnv(sv,(NV) vaxc$errno);
503 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
504 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
510 if (!(_emx_env & 0x200)) { /* Under DOS */
511 sv_setnv(sv, (NV)errno);
512 sv_setpv(sv, errno ? Strerror(errno) : "");
514 if (errno != errno_isOS2) {
515 int tmp = _syserrno();
516 if (tmp) /* 2nd call to _syserrno() makes it 0 */
519 sv_setnv(sv, (NV)Perl_rc);
520 sv_setpv(sv, os2error(Perl_rc));
525 DWORD dwErr = GetLastError();
526 sv_setnv(sv, (NV)dwErr);
529 PerlProc_GetOSError(sv, dwErr);
536 sv_setnv(sv, (NV)errno);
537 sv_setpv(sv, errno ? Strerror(errno) : "");
542 SvNOK_on(sv); /* what a wonderful hack! */
544 case '\006': /* ^F */
545 sv_setiv(sv, (IV)PL_maxsysfd);
547 case '\010': /* ^H */
548 sv_setiv(sv, (IV)PL_hints);
550 case '\011': /* ^I */ /* NOT \t in EBCDIC */
552 sv_setpv(sv, PL_inplace);
554 sv_setsv(sv, &PL_sv_undef);
556 case '\017': /* ^O & ^OPEN */
557 if (*(mg->mg_ptr+1) == '\0')
558 sv_setpv(sv, PL_osname);
559 else if (strEQ(mg->mg_ptr, "\017PEN")) {
560 if (!PL_compiling.cop_io)
561 sv_setsv(sv, &PL_sv_undef);
563 sv_setsv(sv, PL_compiling.cop_io);
567 case '\020': /* ^P */
568 sv_setiv(sv, (IV)PL_perldb);
570 case '\023': /* ^S */
572 if (PL_lex_state != LEX_NOTPARSING)
575 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
578 case '\024': /* ^T */
580 sv_setnv(sv, PL_basetime);
582 sv_setiv(sv, (IV)PL_basetime);
585 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
586 if (*(mg->mg_ptr+1) == '\0')
587 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
588 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
589 if (PL_compiling.cop_warnings == pWARN_NONE ||
590 PL_compiling.cop_warnings == pWARN_STD)
592 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
594 else if (PL_compiling.cop_warnings == pWARN_ALL) {
595 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
598 sv_setsv(sv, PL_compiling.cop_warnings);
602 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
603 sv_setiv(sv, (IV)PL_widesyscalls);
605 case '1': case '2': case '3': case '4':
606 case '5': case '6': case '7': case '8': case '9': case '&':
607 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
611 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
612 * XXX Does the new way break anything?
614 paren = atoi(mg->mg_ptr); /* $& is in [0] */
616 if (paren <= rx->nparens &&
617 (s1 = rx->startp[paren]) != -1 &&
618 (t1 = rx->endp[paren]) != -1)
629 was_tainted = PL_tainted;
633 if (DO_UTF8(PL_reg_sv))
638 PL_tainted = (was_tainted || RX_MATCH_TAINTED(rx));
643 sv_setsv(sv,&PL_sv_undef);
646 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
647 paren = rx->lastparen;
651 sv_setsv(sv,&PL_sv_undef);
654 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
655 if ((s = rx->subbeg) && rx->startp[0] != -1) {
660 sv_setsv(sv,&PL_sv_undef);
663 if (PL_curpm && (rx = PL_curpm->op_pmregexp)) {
664 if (rx->subbeg && rx->endp[0] != -1) {
665 s = rx->subbeg + rx->endp[0];
666 i = rx->sublen - rx->endp[0];
670 sv_setsv(sv,&PL_sv_undef);
674 if (GvIO(PL_last_in_gv)) {
675 sv_setiv(sv, (IV)IoLINES(GvIO(PL_last_in_gv)));
681 sv_setiv(sv, (IV)STATUS_CURRENT);
682 #ifdef COMPLEX_STATUS
683 LvTARGOFF(sv) = PL_statusvalue;
684 LvTARGLEN(sv) = PL_statusvalue_vms;
689 s = IoTOP_NAME(GvIOp(PL_defoutgv));
693 sv_setpv(sv,GvENAME(PL_defoutgv));
698 s = IoFMT_NAME(GvIOp(PL_defoutgv));
700 s = GvENAME(PL_defoutgv);
705 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
708 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
711 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
719 WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase));
722 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
729 sv_setpv(sv,PL_ofmt);
733 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
734 sv_setpv(sv, errno ? Strerror(errno) : "");
737 int saveerrno = errno;
738 sv_setnv(sv, (NV)errno);
740 if (errno == errno_isOS2 || errno == errno_isOS2_set)
741 sv_setpv(sv, os2error(Perl_rc));
744 sv_setpv(sv, errno ? Strerror(errno) : "");
748 SvNOK_on(sv); /* what a wonderful hack! */
751 sv_setiv(sv, (IV)PL_uid);
754 sv_setiv(sv, (IV)PL_euid);
757 sv_setiv(sv, (IV)PL_gid);
759 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid);
763 sv_setiv(sv, (IV)PL_egid);
765 Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid);
770 Groups_t gary[NGROUPS];
771 i = getgroups(NGROUPS,gary);
773 Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]);
776 (void)SvIOK_on(sv); /* what a wonderful hack! */
780 #ifndef MACOS_TRADITIONAL
786 sv_setsv(sv, thr->errsv);
788 #endif /* USE_THREADS */
794 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
796 struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr;
798 if (uf && uf->uf_val)
799 (*uf->uf_val)(uf->uf_index, sv);
804 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
815 #ifdef DYNAMIC_ENV_FETCH
816 /* We just undefd an environment var. Is a replacement */
817 /* waiting in the wings? */
820 if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE)))
821 s = SvPV(*valp, len);
825 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
826 /* And you'll never guess what the dog had */
827 /* in its mouth... */
829 MgTAINTEDDIR_off(mg);
831 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
832 char pathbuf[256], eltbuf[256], *cp, *elt = s;
836 do { /* DCL$PATH may be a search list */
837 while (1) { /* as may dev portion of any element */
838 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
839 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
840 cando_by_name(S_IWUSR,0,elt) ) {
845 if ((cp = strchr(elt, ':')) != Nullch)
847 if (my_trnlnm(elt, eltbuf, j++))
853 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
856 if (s && klen == 4 && strEQ(ptr,"PATH")) {
857 char *strend = s + len;
862 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
865 if (i >= sizeof tmpbuf /* too long -- assume the worst */
867 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
874 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
880 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
883 my_setenv(MgPV(mg,n_a),Nullch);
888 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
891 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
896 magic_clear_all_env(sv,mg);
897 hv_iterinit((HV*)sv);
898 while ((entry = hv_iternext((HV*)sv))) {
900 my_setenv(hv_iterkey(entry, &keylen),
901 SvPV(hv_iterval((HV*)sv, entry), n_a));
909 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
911 #if defined(VMS) || defined(EPOC)
912 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
914 # ifdef PERL_IMPLICIT_SYS
918 char *envv = GetEnvironmentStrings();
922 char *end = strchr(cur,'=');
923 if (end && end != cur) {
925 my_setenv(cur,Nullch);
927 cur = end + strlen(end+1)+2;
929 else if ((len = strlen(cur)))
932 FreeEnvironmentStrings(envv);
934 #ifdef USE_ENVIRON_ARRAY
935 # ifndef PERL_USE_SAFE_PUTENV
938 if (environ == PL_origenviron)
939 environ = (char**)safesysmalloc(sizeof(char*));
941 for (i = 0; environ[i]; i++)
942 safesysfree(environ[i]);
943 # endif /* PERL_USE_SAFE_PUTENV */
947 #endif /* USE_ENVIRON_ARRAY */
949 # endif /* PERL_IMPLICIT_SYS */
956 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
960 /* Are we fetching a signal entry? */
961 i = whichsig(MgPV(mg,n_a));
964 sv_setsv(sv,PL_psig_ptr[i]);
966 Sighandler_t sigstate = rsignal_state(i);
968 /* cache state so we don't fetch it again */
969 if(sigstate == SIG_IGN)
970 sv_setpv(sv,"IGNORE");
972 sv_setsv(sv,&PL_sv_undef);
973 PL_psig_ptr[i] = SvREFCNT_inc(sv);
980 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
984 /* Are we clearing a signal entry? */
985 i = whichsig(MgPV(mg,n_a));
988 SvREFCNT_dec(PL_psig_ptr[i]);
991 if(PL_psig_name[i]) {
992 SvREFCNT_dec(PL_psig_name[i]);
1000 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1009 if (strEQ(s,"__DIE__"))
1011 else if (strEQ(s,"__WARN__"))
1014 Perl_croak(aTHX_ "No such hook: %s", s);
1022 i = whichsig(s); /* ...no, a brick */
1024 if (ckWARN(WARN_SIGNAL))
1025 Perl_warner(aTHX_ WARN_SIGNAL, "No such signal: SIG%s", s);
1028 SvREFCNT_dec(PL_psig_name[i]);
1029 SvREFCNT_dec(PL_psig_ptr[i]);
1030 PL_psig_ptr[i] = SvREFCNT_inc(sv);
1031 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1032 PL_psig_name[i] = newSVpvn(s, len);
1033 SvREADONLY_on(PL_psig_name[i]);
1035 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1037 (void)rsignal(i, PL_sighandlerp);
1039 *svp = SvREFCNT_inc(sv);
1042 s = SvPV_force(sv,len);
1043 if (strEQ(s,"IGNORE")) {
1045 (void)rsignal(i, SIG_IGN);
1049 else if (strEQ(s,"DEFAULT") || !*s) {
1051 (void)rsignal(i, SIG_DFL);
1057 * We should warn if HINT_STRICT_REFS, but without
1058 * access to a known hint bit in a known OP, we can't
1059 * tell whether HINT_STRICT_REFS is in force or not.
1061 if (!strchr(s,':') && !strchr(s,'\''))
1062 sv_insert(sv, 0, 0, "main::", 6);
1064 (void)rsignal(i, PL_sighandlerp);
1066 *svp = SvREFCNT_inc(sv);
1070 #endif /* !PERL_MICRO */
1073 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1075 PL_sub_generation++;
1080 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1082 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1083 PL_amagic_generation++;
1089 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1091 HV *hv = (HV*)LvTARG(sv);
1096 (void) hv_iterinit(hv);
1097 if (! SvTIED_mg((SV*)hv, 'P'))
1101 while ((entry = hv_iternext(hv))) {
1107 sv_setiv(sv, (IV)i);
1112 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1115 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1120 /* caller is responsible for stack switching/cleanup */
1122 S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val)
1128 PUSHs(SvTIED_obj(sv, mg));
1131 if (mg->mg_len >= 0)
1132 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1133 else if (mg->mg_len == HEf_SVKEY)
1134 PUSHs((SV*)mg->mg_ptr);
1136 else if (mg->mg_type == 'p') {
1137 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1145 return call_method(meth, flags);
1149 S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth)
1155 PUSHSTACKi(PERLSI_MAGIC);
1157 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1158 sv_setsv(sv, *PL_stack_sp--);
1168 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1170 magic_methpack(sv,mg,"FETCH");
1172 mg->mg_flags |= MGf_GSKIP;
1177 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1181 PUSHSTACKi(PERLSI_MAGIC);
1182 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1189 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1191 return magic_methpack(sv,mg,"DELETE");
1196 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1203 PUSHSTACKi(PERLSI_MAGIC);
1204 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1205 sv = *PL_stack_sp--;
1206 retval = (U32) SvIV(sv)-1;
1215 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1220 PUSHSTACKi(PERLSI_MAGIC);
1222 XPUSHs(SvTIED_obj(sv, mg));
1224 call_method("CLEAR", G_SCALAR|G_DISCARD);
1231 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1234 const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1238 PUSHSTACKi(PERLSI_MAGIC);
1241 PUSHs(SvTIED_obj(sv, mg));
1246 if (call_method(meth, G_SCALAR))
1247 sv_setsv(key, *PL_stack_sp--);
1256 Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
1258 return magic_methpack(sv,mg,"EXISTS");
1262 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1272 svp = av_fetch(GvAV(gv),
1273 atoi(MgPV(mg,n_a)), FALSE);
1274 if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp))))
1280 Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg)
1282 sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase);
1287 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1289 av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase);
1294 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1296 SV* lsv = LvTARG(sv);
1298 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1299 mg = mg_find(lsv, 'g');
1300 if (mg && mg->mg_len >= 0) {
1303 sv_pos_b2u(lsv, &i);
1304 sv_setiv(sv, i + PL_curcop->cop_arybase);
1313 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1315 SV* lsv = LvTARG(sv);
1322 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1323 mg = mg_find(lsv, 'g');
1327 sv_magic(lsv, (SV*)0, 'g', Nullch, 0);
1328 mg = mg_find(lsv, 'g');
1330 else if (!SvOK(sv)) {
1334 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1336 pos = SvIV(sv) - PL_curcop->cop_arybase;
1339 ulen = sv_len_utf8(lsv);
1354 sv_pos_u2b(lsv, &p, 0);
1359 mg->mg_flags &= ~MGf_MINMATCH;
1365 Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg)
1367 if (SvFAKE(sv)) { /* FAKE globs can get coerced */
1369 gv_efullname3(sv,((GV*)sv), "*");
1373 gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */
1378 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1387 if (*s == '*' && s[1])
1389 gv = gv_fetchpv(s,TRUE, SVt_PVGV);
1394 GvGP(sv) = gp_ref(GvGP(gv));
1399 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1402 SV *lsv = LvTARG(sv);
1403 char *tmps = SvPV(lsv,len);
1404 I32 offs = LvTARGOFF(sv);
1405 I32 rem = LvTARGLEN(sv);
1408 sv_pos_u2b(lsv, &offs, &rem);
1411 if (rem + offs > len)
1413 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1420 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1423 char *tmps = SvPV(sv, len);
1424 SV *lsv = LvTARG(sv);
1425 I32 lvoff = LvTARGOFF(sv);
1426 I32 lvlen = LvTARGLEN(sv);
1429 sv_utf8_upgrade(lsv);
1430 sv_pos_u2b(lsv, &lvoff, &lvlen);
1431 sv_insert(lsv, lvoff, lvlen, tmps, len);
1434 else if (SvUTF8(lsv)) {
1435 sv_pos_u2b(lsv, &lvoff, &lvlen);
1436 tmps = (char*)bytes_to_utf8((U8*)tmps, &len);
1437 sv_insert(lsv, lvoff, lvlen, tmps, len);
1441 sv_insert(lsv, lvoff, lvlen, tmps, len);
1447 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1449 TAINT_IF((mg->mg_len & 1) ||
1450 ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */
1455 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1457 if (PL_localizing) {
1458 if (PL_localizing == 1)
1463 else if (PL_tainted)
1471 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1473 SV *lsv = LvTARG(sv);
1480 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1485 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1487 do_vecset(sv); /* XXX slurp this routine */
1492 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1495 if (LvTARGLEN(sv)) {
1497 SV *ahv = LvTARG(sv);
1498 if (SvTYPE(ahv) == SVt_PVHV) {
1499 HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1504 SV **svp = avhv_fetch_ent((AV*)ahv, mg->mg_obj, FALSE, 0);
1510 AV* av = (AV*)LvTARG(sv);
1511 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1512 targ = AvARRAY(av)[LvTARGOFF(sv)];
1514 if (targ && targ != &PL_sv_undef) {
1515 /* somebody else defined it for us */
1516 SvREFCNT_dec(LvTARG(sv));
1517 LvTARG(sv) = SvREFCNT_inc(targ);
1519 SvREFCNT_dec(mg->mg_obj);
1520 mg->mg_obj = Nullsv;
1521 mg->mg_flags &= ~MGf_REFCOUNTED;
1526 sv_setsv(sv, targ ? targ : &PL_sv_undef);
1531 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
1536 sv_setsv(LvTARG(sv), sv);
1537 SvSETMAGIC(LvTARG(sv));
1543 Perl_vivify_defelem(pTHX_ SV *sv)
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)
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 */
1716 if (*(mg->mg_ptr+1) == '\0') {
1718 Safefree(PL_osname);
1720 PL_osname = savepv(SvPV(sv,len));
1724 else if (strEQ(mg->mg_ptr, "\017PEN")) {
1725 if (!PL_compiling.cop_io)
1726 PL_compiling.cop_io = newSVsv(sv);
1728 sv_setsv(PL_compiling.cop_io,sv);
1731 case '\020': /* ^P */
1732 PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1733 if (PL_perldb && !PL_DBsingle)
1736 case '\024': /* ^T */
1738 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
1740 PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1743 case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */
1744 if (*(mg->mg_ptr+1) == '\0') {
1745 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1746 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1747 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
1748 | (i ? G_WARN_ON : G_WARN_OFF) ;
1751 else if (strEQ(mg->mg_ptr, "\027ARNING_BITS")) {
1752 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
1753 if (!SvPOK(sv) && PL_localizing) {
1754 sv_setpvn(sv, WARN_NONEstring, WARNsize);
1755 PL_compiling.cop_warnings = pWARN_NONE;
1760 int accumulate = 0 ;
1761 int any_fatals = 0 ;
1762 char * ptr = (char*)SvPV(sv, len) ;
1763 for (i = 0 ; i < len ; ++i) {
1764 accumulate |= ptr[i] ;
1765 any_fatals |= (ptr[i] & 0xAA) ;
1768 PL_compiling.cop_warnings = pWARN_NONE;
1769 else if (isWARN_on(sv, WARN_ALL) && !any_fatals) {
1770 PL_compiling.cop_warnings = pWARN_ALL;
1771 PL_dowarn |= G_WARN_ONCE ;
1774 if (specialWARN(PL_compiling.cop_warnings))
1775 PL_compiling.cop_warnings = newSVsv(sv) ;
1777 sv_setsv(PL_compiling.cop_warnings, sv);
1778 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
1779 PL_dowarn |= G_WARN_ONCE ;
1785 else if (strEQ(mg->mg_ptr, "\027IDE_SYSTEM_CALLS"))
1786 PL_widesyscalls = SvTRUE(sv);
1789 if (PL_localizing) {
1790 if (PL_localizing == 1)
1791 SAVESPTR(PL_last_in_gv);
1793 else if (SvOK(sv) && GvIO(PL_last_in_gv))
1794 IoLINES(GvIOp(PL_last_in_gv)) = (long)SvIV(sv);
1797 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
1798 IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1799 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1802 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
1803 IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len));
1804 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO);
1807 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1810 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1811 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
1812 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
1815 IoPAGE(GvIOp(PL_defoutgv)) = (long)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1819 IO *io = GvIOp(PL_defoutgv);
1820 if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0)
1821 IoFLAGS(io) &= ~IOf_FLUSH;
1823 if (!(IoFLAGS(io) & IOf_FLUSH)) {
1824 PerlIO *ofp = IoOFP(io);
1826 (void)PerlIO_flush(ofp);
1827 IoFLAGS(io) |= IOf_FLUSH;
1833 i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1834 PL_multiline = (i != 0);
1837 SvREFCNT_dec(PL_nrs);
1838 PL_nrs = newSVsv(sv);
1839 SvREFCNT_dec(PL_rs);
1840 PL_rs = SvREFCNT_inc(PL_nrs);
1844 SvREFCNT_dec(PL_ors_sv);
1845 if (SvOK(sv) || SvGMAGICAL(sv)) {
1846 PL_ors_sv = newSVsv(sv);
1854 SvREFCNT_dec(PL_ofs_sv);
1855 if (SvOK(sv) || SvGMAGICAL(sv)) {
1856 PL_ofs_sv = newSVsv(sv);
1865 PL_ofmt = savepv(SvPV(sv,len));
1868 PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1871 #ifdef COMPLEX_STATUS
1872 if (PL_localizing == 2) {
1873 PL_statusvalue = LvTARGOFF(sv);
1874 PL_statusvalue_vms = LvTARGLEN(sv);
1878 #ifdef VMSISH_STATUS
1880 STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)));
1883 STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv));
1886 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
1887 (SvIV(sv) == EVMSERR) ? 4 : vaxc$errno);
1890 PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1891 if (PL_delaymagic) {
1892 PL_delaymagic |= DM_RUID;
1893 break; /* don't do magic till later */
1896 (void)setruid((Uid_t)PL_uid);
1899 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
1901 #ifdef HAS_SETRESUID
1902 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
1904 if (PL_uid == PL_euid) /* special case $< = $> */
1905 (void)PerlProc_setuid(PL_uid);
1907 PL_uid = PerlProc_getuid();
1908 Perl_croak(aTHX_ "setruid() not implemented");
1913 PL_uid = PerlProc_getuid();
1914 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1917 PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1918 if (PL_delaymagic) {
1919 PL_delaymagic |= DM_EUID;
1920 break; /* don't do magic till later */
1923 (void)seteuid((Uid_t)PL_euid);
1926 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
1928 #ifdef HAS_SETRESUID
1929 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
1931 if (PL_euid == PL_uid) /* special case $> = $< */
1932 PerlProc_setuid(PL_euid);
1934 PL_euid = PerlProc_geteuid();
1935 Perl_croak(aTHX_ "seteuid() not implemented");
1940 PL_euid = PerlProc_geteuid();
1941 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1944 PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1945 if (PL_delaymagic) {
1946 PL_delaymagic |= DM_RGID;
1947 break; /* don't do magic till later */
1950 (void)setrgid((Gid_t)PL_gid);
1953 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
1955 #ifdef HAS_SETRESGID
1956 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
1958 if (PL_gid == PL_egid) /* special case $( = $) */
1959 (void)PerlProc_setgid(PL_gid);
1961 PL_gid = PerlProc_getgid();
1962 Perl_croak(aTHX_ "setrgid() not implemented");
1967 PL_gid = PerlProc_getgid();
1968 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1971 #ifdef HAS_SETGROUPS
1973 char *p = SvPV(sv, len);
1974 Groups_t gary[NGROUPS];
1979 for (i = 0; i < NGROUPS; ++i) {
1980 while (*p && !isSPACE(*p))
1989 (void)setgroups(i, gary);
1991 #else /* HAS_SETGROUPS */
1992 PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv);
1993 #endif /* HAS_SETGROUPS */
1994 if (PL_delaymagic) {
1995 PL_delaymagic |= DM_EGID;
1996 break; /* don't do magic till later */
1999 (void)setegid((Gid_t)PL_egid);
2002 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2004 #ifdef HAS_SETRESGID
2005 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2007 if (PL_egid == PL_gid) /* special case $) = $( */
2008 (void)PerlProc_setgid(PL_egid);
2010 PL_egid = PerlProc_getegid();
2011 Perl_croak(aTHX_ "setegid() not implemented");
2016 PL_egid = PerlProc_getegid();
2017 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2020 PL_chopset = SvPV_force(sv,len);
2022 #ifndef MACOS_TRADITIONAL
2024 #ifdef HAS_SETPROCTITLE
2025 /* The BSDs don't show the argv[] in ps(1) output, they
2026 * show a string from the process struct and provide
2027 * the setproctitle() routine to manipulate that. */
2030 # if __FreeBSD_version >= 410001
2031 /* The leading "-" removes the "perl: " prefix,
2032 * but not the "(perl) suffix from the ps(1)
2033 * output, because that's what ps(1) shows if the
2034 * argv[] is modified. */
2035 setproctitle("-%s", s, len + 1);
2036 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2037 /* This doesn't really work if you assume that
2038 * $0 = 'foobar'; will wipe out 'perl' from the $0
2039 * because in ps(1) output the result will be like
2040 * sprintf("perl: %s (perl)", s)
2041 * I guess this is a security feature:
2042 * one (a user process) cannot get rid of the original name.
2044 setproctitle("%s", s);
2051 /* See if all the arguments are contiguous in memory */
2052 for (i = 1; i < PL_origargc; i++) {
2053 if (PL_origargv[i] == s + 1
2055 || PL_origargv[i] == s + 2
2060 s += strlen(s); /* this one is ok too */
2065 /* can grab env area too? */
2066 if (PL_origenviron && (PL_origenviron[0] == s + 1
2068 || (PL_origenviron[0] == s + 9 && (s += 8))
2071 my_setenv("NoNe SuCh", Nullch);
2072 /* force copy of environment */
2073 for (i = 0; PL_origenviron[i]; i++)
2074 if (PL_origenviron[i] == s + 1) {
2081 PL_origalen = s - PL_origargv[0];
2083 s = SvPV_force(sv,len);
2085 if (i >= PL_origalen) {
2087 /* don't allow system to limit $0 seen by script */
2088 /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */
2089 Copy(s, PL_origargv[0], i, char);
2090 s = PL_origargv[0]+i;
2094 Copy(s, PL_origargv[0], i, char);
2095 s = PL_origargv[0]+i;
2097 while (++i < PL_origalen)
2099 s = PL_origargv[0]+i;
2100 for (i = 1; i < PL_origargc; i++)
2101 PL_origargv[i] = Nullch;
2107 sv_setsv(thr->errsv, sv);
2109 #endif /* USE_THREADS */
2116 Perl_magic_mutexfree(pTHX_ SV *sv, MAGIC *mg)
2118 DEBUG_S(PerlIO_printf(Perl_debug_log,
2119 "0x%"UVxf": magic_mutexfree 0x%"UVxf"\n",
2120 PTR2UV(thr), PTR2UV(sv));)
2122 Perl_croak(aTHX_ "panic: magic_mutexfree");
2123 MUTEX_DESTROY(MgMUTEXP(mg));
2124 COND_DESTROY(MgCONDP(mg));
2127 #endif /* USE_THREADS */
2130 Perl_whichsig(pTHX_ char *sig)
2132 register char **sigv;
2134 for (sigv = PL_sig_name+1; *sigv; sigv++)
2135 if (strEQ(sig,*sigv))
2136 return PL_sig_num[sigv - PL_sig_name];
2138 if (strEQ(sig,"CHLD"))
2142 if (strEQ(sig,"CLD"))
2149 Perl_despatch_signals(pTHX)
2151 #ifndef PERL_OLD_SIGNALS
2152 /* This is just a dummy for now */
2160 Perl_sighandler(int sig)
2162 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2163 dTHXoa(PL_curinterp); /* fake TLS, because signals don't do TLS */
2170 SV *sv, *tSv = PL_Sv;
2174 I32 o_save_i = PL_savestack_ix;
2177 #if defined(WIN32) && defined(PERL_IMPLICIT_CONTEXT)
2178 PERL_SET_THX(aTHXo); /* fake TLS, see above */
2181 if (PL_savestack_ix + 15 <= PL_savestack_max)
2183 if (PL_markstack_ptr < PL_markstack_max - 2)
2185 if (PL_retstack_ix < PL_retstack_max - 2)
2187 if (PL_scopestack_ix < PL_scopestack_max - 3)
2190 if (!PL_psig_ptr[sig])
2191 Perl_die(aTHX_ "Signal SIG%s received, but no signal handler set.\n",
2194 /* Max number of items pushed there is 3*n or 4. We cannot fix
2195 infinity, so we fix 4 (in fact 5): */
2197 PL_savestack_ix += 5; /* Protect save in progress. */
2198 o_save_i = PL_savestack_ix;
2199 SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags);
2202 PL_markstack_ptr++; /* Protect mark. */
2205 PL_retstack[PL_retstack_ix] = NULL;
2208 PL_scopestack_ix += 1;
2209 /* sv_2cv is too complicated, try a simpler variant first: */
2210 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2211 || SvTYPE(cv) != SVt_PVCV)
2212 cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE);
2214 if (!cv || !CvROOT(cv)) {
2215 if (ckWARN(WARN_SIGNAL))
2216 Perl_warner(aTHX_ WARN_SIGNAL, "SIG%s handler \"%s\" not defined.\n",
2217 PL_sig_name[sig], (gv ? GvENAME(gv)
2224 if(PL_psig_name[sig]) {
2225 sv = SvREFCNT_inc(PL_psig_name[sig]);
2229 sv = sv_newmortal();
2230 sv_setpv(sv,PL_sig_name[sig]);
2233 PUSHSTACKi(PERLSI_SIGNAL);
2238 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2241 if (SvTRUE(ERRSV)) {
2242 #ifdef HAS_SIGPROCMASK
2243 /* Handler "died", for example to get out of a restart-able read().
2244 * Before we re-do that on its behalf re-enable the signal which was
2245 * blocked by the system when we entered.
2249 sigaddset(&set,sig);
2250 sigprocmask(SIG_UNBLOCK, &set, NULL);
2252 /* Not clear if this will work */
2253 (void)rsignal(sig, SIG_IGN);
2254 (void)rsignal(sig, PL_sighandlerp);
2256 Perl_die(aTHX_ Nullch);
2260 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2266 PL_scopestack_ix -= 1;
2269 PL_op = myop; /* Apparently not needed... */
2271 PL_Sv = tSv; /* Restore global temporaries. */
2282 restore_magic(pTHXo_ void *p)
2284 MGS* mgs = SSPTR(PTR2IV(p), MGS*);
2285 SV* sv = mgs->mgs_sv;
2290 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2293 SvFLAGS(sv) |= mgs->mgs_flags;
2297 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
2300 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2302 /* If we're still on top of the stack, pop us off. (That condition
2303 * will be satisfied if restore_magic was called explicitly, but *not*
2304 * if it's being called via leave_scope.)
2305 * The reason for doing this is that otherwise, things like sv_2cv()
2306 * may leave alloc gunk on the savestack, and some code
2307 * (e.g. sighandler) doesn't expect that...
2309 if (PL_savestack_ix == mgs->mgs_ss_ix)
2311 I32 popval = SSPOPINT;
2312 assert(popval == SAVEt_DESTRUCTOR_X);
2313 PL_savestack_ix -= 2;
2315 assert(popval == SAVEt_ALLOC);
2317 PL_savestack_ix -= popval;
2323 unwind_handler_stack(pTHXo_ void *p)
2325 U32 flags = *(U32*)p;
2328 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2329 /* cxstack_ix-- Not needed, die already unwound it. */
2331 SvREFCNT_dec(sig_sv);