3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * Sam sat on the ground and put his head in his hands. 'I wish I had never
13 * come here, and I don't want to see no more magic,' he said, and fell silent.
15 * [p.363 of _The Lord of the Rings_, II/vii: "The Mirror of Galadriel"]
19 =head1 Magical Functions
21 "Magic" is special data attached to SV structures in order to give them
22 "magical" properties. When any Perl code tries to read from, or assign to,
23 an SV marked as magical, it calls the 'get' or 'set' function associated
24 with that SV's magic. A get is called prior to reading an SV, in order to
25 give it a chance to update its internal value (get on $. writes the line
26 number of the last read filehandle into to the SV's IV slot), while
27 set is called after an SV has been written to, in order to allow it to make
28 use of its changed value (set on $/ copies the SV's new value to the
29 PL_rs global variable).
31 Magic is implemented as a linked list of MAGIC structures attached to the
32 SV. Each MAGIC struct holds the type of the magic, a pointer to an array
33 of functions that implement the get(), set(), length() etc functions,
34 plus space for some flags and pointers. For example, a tied variable has
35 a MAGIC structure that contains a pointer to the object associated with the
44 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
50 #if defined(HAS_SETGROUPS)
57 # include <sys/pstat.h>
60 #ifdef HAS_PRCTL_SET_NAME
61 # include <sys/prctl.h>
64 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
65 Signal_t Perl_csighandler(int sig, siginfo_t *, void *);
67 Signal_t Perl_csighandler(int sig);
71 /* Missing protos on LynxOS */
72 void setruid(uid_t id);
73 void seteuid(uid_t id);
74 void setrgid(uid_t id);
75 void setegid(uid_t id);
79 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
89 /* MGS is typedef'ed to struct magic_state in perl.h */
92 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
98 PERL_ARGS_ASSERT_SAVE_MAGIC;
100 /* we shouldn't really be called here with RC==0, but it can sometimes
101 * happen via mg_clear() (which also shouldn't be called when RC==0,
102 * but it can happen). Handle this case gracefully(ish) by not RC++
103 * and thus avoiding the resultant double free */
104 if (SvREFCNT(sv) > 0) {
105 /* guard against sv getting freed midway through the mg clearing,
106 * by holding a private reference for the duration. */
107 SvREFCNT_inc_simple_void_NN(sv);
111 assert(SvMAGICAL(sv));
112 /* Turning READONLY off for a copy-on-write scalar (including shared
113 hash keys) is a bad idea. */
115 sv_force_normal_flags(sv, 0);
117 SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix));
119 mgs = SSPTR(mgs_ix, MGS*);
121 mgs->mgs_magical = SvMAGICAL(sv);
122 mgs->mgs_readonly = SvREADONLY(sv) != 0;
123 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
124 mgs->mgs_bumped = bumped;
128 if (!(SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK))) {
129 /* No public flags are set, so promote any private flags to public. */
130 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
135 =for apidoc mg_magical
137 Turns on the magical status of an SV. See C<sv_magic>.
143 Perl_mg_magical(pTHX_ SV *sv)
146 PERL_ARGS_ASSERT_MG_MAGICAL;
150 if ((mg = SvMAGIC(sv))) {
152 const MGVTBL* const vtbl = mg->mg_virtual;
154 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
161 } while ((mg = mg->mg_moremagic));
162 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)))
168 /* is this container magic (%ENV, $1 etc), or value magic (pos, taint etc)? */
171 S_is_container_magic(const MAGIC *mg)
174 switch (mg->mg_type) {
177 case PERL_MAGIC_regex_global:
178 case PERL_MAGIC_nkeys:
179 #ifdef USE_LOCALE_COLLATE
180 case PERL_MAGIC_collxfrm:
183 case PERL_MAGIC_taint:
185 case PERL_MAGIC_vstring:
186 case PERL_MAGIC_utf8:
187 case PERL_MAGIC_substr:
188 case PERL_MAGIC_defelem:
189 case PERL_MAGIC_arylen:
191 case PERL_MAGIC_backref:
192 case PERL_MAGIC_arylen_p:
193 case PERL_MAGIC_rhash:
194 case PERL_MAGIC_symtab:
195 case PERL_MAGIC_tied: /* treat as value, so 'local @tied' isn't tied */
196 case PERL_MAGIC_checkcall:
206 Do magic after a value is retrieved from the SV. See C<sv_magic>.
212 Perl_mg_get(pTHX_ SV *sv)
215 const I32 mgs_ix = SSNEW(sizeof(MGS));
217 MAGIC *newmg, *head, *cur, *mg;
219 PERL_ARGS_ASSERT_MG_GET;
221 save_magic(mgs_ix, sv);
223 /* We must call svt_get(sv, mg) for each valid entry in the linked
224 list of magic. svt_get() may delete the current entry, add new
225 magic to the head of the list, or upgrade the SV. AMS 20010810 */
227 newmg = cur = head = mg = SvMAGIC(sv);
229 const MGVTBL * const vtbl = mg->mg_virtual;
230 MAGIC * const nextmg = mg->mg_moremagic; /* it may delete itself */
232 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
233 vtbl->svt_get(aTHX_ sv, mg);
235 /* guard against magic having been deleted - eg FETCH calling
238 (SSPTR(mgs_ix, MGS *))->mgs_magical = 0; /* recalculate flags */
242 /* recalculate flags if this entry was deleted. */
243 if (mg->mg_flags & MGf_GSKIP)
244 (SSPTR(mgs_ix, MGS *))->mgs_magical = 0;
250 /* Have we finished with the new entries we saw? Start again
251 where we left off (unless there are more new entries). */
259 /* Were any new entries added? */
260 if (!have_new && (newmg = SvMAGIC(sv)) != head) {
264 (SSPTR(mgs_ix, MGS *))->mgs_magical = 0; /* recalculate flags */
268 restore_magic(INT2PTR(void *, (IV)mgs_ix));
275 Do magic after a value is assigned to the SV. See C<sv_magic>.
281 Perl_mg_set(pTHX_ SV *sv)
284 const I32 mgs_ix = SSNEW(sizeof(MGS));
288 PERL_ARGS_ASSERT_MG_SET;
290 save_magic(mgs_ix, sv);
292 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
293 const MGVTBL* vtbl = mg->mg_virtual;
294 nextmg = mg->mg_moremagic; /* it may delete itself */
295 if (mg->mg_flags & MGf_GSKIP) {
296 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
297 (SSPTR(mgs_ix, MGS*))->mgs_magical = 0;
299 if (PL_localizing == 2 && (!S_is_container_magic(mg) || sv == DEFSV))
301 if (vtbl && vtbl->svt_set)
302 vtbl->svt_set(aTHX_ sv, mg);
305 restore_magic(INT2PTR(void*, (IV)mgs_ix));
310 =for apidoc mg_length
312 Report on the SV's length. See C<sv_magic>.
318 Perl_mg_length(pTHX_ SV *sv)
324 PERL_ARGS_ASSERT_MG_LENGTH;
326 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
327 const MGVTBL * const vtbl = mg->mg_virtual;
328 if (vtbl && vtbl->svt_len) {
329 const I32 mgs_ix = SSNEW(sizeof(MGS));
330 save_magic(mgs_ix, sv);
331 /* omit MGf_GSKIP -- not changed here */
332 len = vtbl->svt_len(aTHX_ sv, mg);
333 restore_magic(INT2PTR(void*, (IV)mgs_ix));
339 /* You can't know whether it's UTF-8 until you get the string again...
341 const U8 *s = (U8*)SvPV_const(sv, len);
344 len = utf8_length(s, s + len);
351 Perl_mg_size(pTHX_ SV *sv)
355 PERL_ARGS_ASSERT_MG_SIZE;
357 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
358 const MGVTBL* const vtbl = mg->mg_virtual;
359 if (vtbl && vtbl->svt_len) {
360 const I32 mgs_ix = SSNEW(sizeof(MGS));
362 save_magic(mgs_ix, sv);
363 /* omit MGf_GSKIP -- not changed here */
364 len = vtbl->svt_len(aTHX_ sv, mg);
365 restore_magic(INT2PTR(void*, (IV)mgs_ix));
372 return AvFILLp((const AV *) sv); /* Fallback to non-tied array */
376 Perl_croak(aTHX_ "Size magic not implemented");
385 Clear something magical that the SV represents. See C<sv_magic>.
391 Perl_mg_clear(pTHX_ SV *sv)
393 const I32 mgs_ix = SSNEW(sizeof(MGS));
397 PERL_ARGS_ASSERT_MG_CLEAR;
399 save_magic(mgs_ix, sv);
401 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
402 const MGVTBL* const vtbl = mg->mg_virtual;
403 /* omit GSKIP -- never set here */
405 nextmg = mg->mg_moremagic; /* it may delete itself */
407 if (vtbl && vtbl->svt_clear)
408 vtbl->svt_clear(aTHX_ sv, mg);
411 restore_magic(INT2PTR(void*, (IV)mgs_ix));
416 S_mg_findext_flags(pTHX_ const SV *sv, int type, const MGVTBL *vtbl, U32 flags)
425 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
426 if (mg->mg_type == type && (!flags || mg->mg_virtual == vtbl)) {
438 Finds the magic pointer for type matching the SV. See C<sv_magic>.
444 Perl_mg_find(pTHX_ const SV *sv, int type)
446 return S_mg_findext_flags(aTHX_ sv, type, NULL, 0);
450 =for apidoc mg_findext
452 Finds the magic pointer of C<type> with the given C<vtbl> for the C<SV>. See
459 Perl_mg_findext(pTHX_ const SV *sv, int type, const MGVTBL *vtbl)
461 return S_mg_findext_flags(aTHX_ sv, type, vtbl, 1);
467 Copies the magic from one SV to another. See C<sv_magic>.
473 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
478 PERL_ARGS_ASSERT_MG_COPY;
480 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
481 const MGVTBL* const vtbl = mg->mg_virtual;
482 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
483 count += vtbl->svt_copy(aTHX_ sv, mg, nsv, key, klen);
486 const char type = mg->mg_type;
487 if (isUPPER(type) && type != PERL_MAGIC_uvar) {
489 (type == PERL_MAGIC_tied)
491 : (type == PERL_MAGIC_regdata && mg->mg_obj)
494 toLOWER(type), key, klen);
503 =for apidoc mg_localize
505 Copy some of the magic from an existing SV to new localized version of that
506 SV. Container magic (eg %ENV, $1, tie) gets copied, value magic doesn't (eg
509 If setmagic is false then no set magic will be called on the new (empty) SV.
510 This typically means that assignment will soon follow (e.g. 'local $x = $y'),
511 and that will handle the magic.
517 Perl_mg_localize(pTHX_ SV *sv, SV *nsv, bool setmagic)
522 PERL_ARGS_ASSERT_MG_LOCALIZE;
527 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
528 const MGVTBL* const vtbl = mg->mg_virtual;
529 if (!S_is_container_magic(mg))
532 if ((mg->mg_flags & MGf_LOCAL) && vtbl->svt_local)
533 (void)vtbl->svt_local(aTHX_ nsv, mg);
535 sv_magicext(nsv, mg->mg_obj, mg->mg_type, vtbl,
536 mg->mg_ptr, mg->mg_len);
538 /* container types should remain read-only across localization */
539 if (!SvIsCOW(sv)) SvFLAGS(nsv) |= SvREADONLY(sv);
542 if (SvTYPE(nsv) >= SVt_PVMG && SvMAGIC(nsv)) {
543 SvFLAGS(nsv) |= SvMAGICAL(sv);
552 #define mg_free_struct(sv, mg) S_mg_free_struct(aTHX_ sv, mg)
554 S_mg_free_struct(pTHX_ SV *sv, MAGIC *mg)
556 const MGVTBL* const vtbl = mg->mg_virtual;
557 if (vtbl && vtbl->svt_free)
558 vtbl->svt_free(aTHX_ sv, mg);
559 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
560 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
561 Safefree(mg->mg_ptr);
562 else if (mg->mg_len == HEf_SVKEY)
563 SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
565 if (mg->mg_flags & MGf_REFCOUNTED)
566 SvREFCNT_dec(mg->mg_obj);
573 Free any magic storage used by the SV. See C<sv_magic>.
579 Perl_mg_free(pTHX_ SV *sv)
584 PERL_ARGS_ASSERT_MG_FREE;
586 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
587 moremagic = mg->mg_moremagic;
588 mg_free_struct(sv, mg);
589 SvMAGIC_set(sv, moremagic);
591 SvMAGIC_set(sv, NULL);
597 =for apidoc Am|void|mg_free_type|SV *sv|int how
599 Remove any magic of type I<how> from the SV I<sv>. See L</sv_magic>.
605 Perl_mg_free_type(pTHX_ SV *sv, int how)
607 MAGIC *mg, *prevmg, *moremg;
608 PERL_ARGS_ASSERT_MG_FREE_TYPE;
609 for (prevmg = NULL, mg = SvMAGIC(sv); mg; prevmg = mg, mg = moremg) {
611 moremg = mg->mg_moremagic;
612 if (mg->mg_type == how) {
613 /* temporarily move to the head of the magic chain, in case
614 custom free code relies on this historical aspect of mg_free */
616 prevmg->mg_moremagic = moremg;
617 mg->mg_moremagic = SvMAGIC(sv);
620 newhead = mg->mg_moremagic;
621 mg_free_struct(sv, mg);
622 SvMAGIC_set(sv, newhead);
632 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
637 PERL_ARGS_ASSERT_MAGIC_REGDATA_CNT;
640 register const REGEXP * const rx = PM_GETRE(PL_curpm);
642 if (mg->mg_obj) { /* @+ */
643 /* return the number possible */
644 return RX_NPARENS(rx);
646 I32 paren = RX_LASTPAREN(rx);
648 /* return the last filled */
650 && (RX_OFFS(rx)[paren].start == -1
651 || RX_OFFS(rx)[paren].end == -1) )
662 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
666 PERL_ARGS_ASSERT_MAGIC_REGDATUM_GET;
669 register const REGEXP * const rx = PM_GETRE(PL_curpm);
671 register const I32 paren = mg->mg_len;
676 if (paren <= (I32)RX_NPARENS(rx) &&
677 (s = RX_OFFS(rx)[paren].start) != -1 &&
678 (t = RX_OFFS(rx)[paren].end) != -1)
681 if (mg->mg_obj) /* @+ */
686 if (i > 0 && RX_MATCH_UTF8(rx)) {
687 const char * const b = RX_SUBBEG(rx);
689 i = utf8_length((U8*)b, (U8*)(b+i));
700 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
702 PERL_ARGS_ASSERT_MAGIC_REGDATUM_SET;
705 Perl_croak_no_modify(aTHX);
706 NORETURN_FUNCTION_END;
710 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
715 register const REGEXP * rx;
716 const char * const remaining = mg->mg_ptr + 1;
718 PERL_ARGS_ASSERT_MAGIC_LEN;
720 switch (*mg->mg_ptr) {
722 if (*remaining == '\0') { /* ^P */
724 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
726 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
730 case '\015': /* $^MATCH */
731 if (strEQ(remaining, "ATCH")) {
738 paren = RX_BUFF_IDX_PREMATCH;
742 paren = RX_BUFF_IDX_POSTMATCH;
746 paren = RX_BUFF_IDX_FULLMATCH;
748 case '1': case '2': case '3': case '4':
749 case '5': case '6': case '7': case '8': case '9':
750 paren = atoi(mg->mg_ptr);
752 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
754 i = CALLREG_NUMBUF_LENGTH((REGEXP * const)rx, sv, paren);
757 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
760 if (ckWARN(WARN_UNINITIALIZED))
765 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
766 paren = RX_LASTPAREN(rx);
771 case '\016': /* ^N */
772 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
773 paren = RX_LASTCLOSEPAREN(rx);
780 if (!SvPOK(sv) && SvNIOK(sv)) {
788 #define SvRTRIM(sv) STMT_START { \
790 STRLEN len = SvCUR(sv); \
791 char * const p = SvPVX(sv); \
792 while (len > 0 && isSPACE(p[len-1])) \
794 SvCUR_set(sv, len); \
800 Perl_emulate_cop_io(pTHX_ const COP *const c, SV *const sv)
802 PERL_ARGS_ASSERT_EMULATE_COP_IO;
804 if (!(CopHINTS_get(c) & (HINT_LEXICAL_IO_IN|HINT_LEXICAL_IO_OUT)))
805 sv_setsv(sv, &PL_sv_undef);
809 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_IN)) {
810 SV *const value = cop_hints_fetch_pvs(c, "open<", 0);
815 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_OUT)) {
816 SV *const value = cop_hints_fetch_pvs(c, "open>", 0);
824 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
828 register const char *s = NULL;
830 const char * const remaining = mg->mg_ptr + 1;
831 const char nextchar = *remaining;
833 PERL_ARGS_ASSERT_MAGIC_GET;
835 switch (*mg->mg_ptr) {
836 case '\001': /* ^A */
837 sv_setsv(sv, PL_bodytarget);
838 if (SvTAINTED(PL_bodytarget))
841 case '\003': /* ^C, ^CHILD_ERROR_NATIVE */
842 if (nextchar == '\0') {
843 sv_setiv(sv, (IV)PL_minus_c);
845 else if (strEQ(remaining, "HILD_ERROR_NATIVE")) {
846 sv_setiv(sv, (IV)STATUS_NATIVE);
850 case '\004': /* ^D */
851 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
853 case '\005': /* ^E */
854 if (nextchar == '\0') {
857 # include <descrip.h>
858 # include <starlet.h>
860 $DESCRIPTOR(msgdsc,msg);
861 sv_setnv(sv,(NV) vaxc$errno);
862 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
863 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
868 if (!(_emx_env & 0x200)) { /* Under DOS */
869 sv_setnv(sv, (NV)errno);
870 sv_setpv(sv, errno ? Strerror(errno) : "");
872 if (errno != errno_isOS2) {
873 const int tmp = _syserrno();
874 if (tmp) /* 2nd call to _syserrno() makes it 0 */
877 sv_setnv(sv, (NV)Perl_rc);
878 sv_setpv(sv, os2error(Perl_rc));
882 const DWORD dwErr = GetLastError();
883 sv_setnv(sv, (NV)dwErr);
885 PerlProc_GetOSError(sv, dwErr);
894 sv_setnv(sv, (NV)errno);
895 sv_setpv(sv, errno ? Strerror(errno) : "");
900 SvNOK_on(sv); /* what a wonderful hack! */
902 else if (strEQ(remaining, "NCODING"))
903 sv_setsv(sv, PL_encoding);
905 case '\006': /* ^F */
906 sv_setiv(sv, (IV)PL_maxsysfd);
908 case '\007': /* ^GLOBAL_PHASE */
909 if (strEQ(remaining, "LOBAL_PHASE")) {
910 sv_setpvn(sv, PL_phase_names[PL_phase],
911 strlen(PL_phase_names[PL_phase]));
914 case '\010': /* ^H */
915 sv_setiv(sv, (IV)PL_hints);
917 case '\011': /* ^I */ /* NOT \t in EBCDIC */
918 sv_setpv(sv, PL_inplace); /* Will undefine sv if PL_inplace is NULL */
920 case '\017': /* ^O & ^OPEN */
921 if (nextchar == '\0') {
922 sv_setpv(sv, PL_osname);
925 else if (strEQ(remaining, "PEN")) {
926 Perl_emulate_cop_io(aTHX_ &PL_compiling, sv);
930 if (nextchar == '\0') { /* ^P */
931 sv_setiv(sv, (IV)PL_perldb);
932 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
933 goto do_prematch_fetch;
934 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
935 goto do_postmatch_fetch;
938 case '\023': /* ^S */
939 if (nextchar == '\0') {
940 if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING)
943 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
948 case '\024': /* ^T */
949 if (nextchar == '\0') {
951 sv_setnv(sv, PL_basetime);
953 sv_setiv(sv, (IV)PL_basetime);
956 else if (strEQ(remaining, "AINT"))
957 sv_setiv(sv, PL_tainting
958 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
961 case '\025': /* $^UNICODE, $^UTF8LOCALE, $^UTF8CACHE */
962 if (strEQ(remaining, "NICODE"))
963 sv_setuv(sv, (UV) PL_unicode);
964 else if (strEQ(remaining, "TF8LOCALE"))
965 sv_setuv(sv, (UV) PL_utf8locale);
966 else if (strEQ(remaining, "TF8CACHE"))
967 sv_setiv(sv, (IV) PL_utf8cache);
969 case '\027': /* ^W & $^WARNING_BITS */
970 if (nextchar == '\0')
971 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
972 else if (strEQ(remaining, "ARNING_BITS")) {
973 if (PL_compiling.cop_warnings == pWARN_NONE) {
974 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
976 else if (PL_compiling.cop_warnings == pWARN_STD) {
979 (PL_dowarn & G_WARN_ON) ? WARN_ALLstring : WARN_NONEstring,
983 else if (PL_compiling.cop_warnings == pWARN_ALL) {
984 /* Get the bit mask for $warnings::Bits{all}, because
985 * it could have been extended by warnings::register */
986 HV * const bits=get_hv("warnings::Bits", 0);
988 SV ** const bits_all = hv_fetchs(bits, "all", FALSE);
990 sv_setsv(sv, *bits_all);
993 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
997 sv_setpvn(sv, (char *) (PL_compiling.cop_warnings + 1),
998 *PL_compiling.cop_warnings);
1003 case '\015': /* $^MATCH */
1004 if (strEQ(remaining, "ATCH")) {
1005 case '1': case '2': case '3': case '4':
1006 case '5': case '6': case '7': case '8': case '9': case '&':
1007 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
1009 * Pre-threads, this was paren = atoi(GvENAME((const GV *)mg->mg_obj));
1010 * XXX Does the new way break anything?
1012 paren = atoi(mg->mg_ptr); /* $& is in [0] */
1013 CALLREG_NUMBUF_FETCH(rx,paren,sv);
1016 sv_setsv(sv,&PL_sv_undef);
1020 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
1021 if (RX_LASTPAREN(rx)) {
1022 CALLREG_NUMBUF_FETCH(rx,RX_LASTPAREN(rx),sv);
1026 sv_setsv(sv,&PL_sv_undef);
1028 case '\016': /* ^N */
1029 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
1030 if (RX_LASTCLOSEPAREN(rx)) {
1031 CALLREG_NUMBUF_FETCH(rx,RX_LASTCLOSEPAREN(rx),sv);
1036 sv_setsv(sv,&PL_sv_undef);
1040 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
1041 CALLREG_NUMBUF_FETCH(rx,-2,sv);
1044 sv_setsv(sv,&PL_sv_undef);
1048 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
1049 CALLREG_NUMBUF_FETCH(rx,-1,sv);
1052 sv_setsv(sv,&PL_sv_undef);
1055 if (GvIO(PL_last_in_gv)) {
1056 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
1061 sv_setiv(sv, (IV)STATUS_CURRENT);
1062 #ifdef COMPLEX_STATUS
1063 SvUPGRADE(sv, SVt_PVLV);
1064 LvTARGOFF(sv) = PL_statusvalue;
1065 LvTARGLEN(sv) = PL_statusvalue_vms;
1070 if (!isGV_with_GP(PL_defoutgv))
1072 else if (GvIOp(PL_defoutgv))
1073 s = IoTOP_NAME(GvIOp(PL_defoutgv));
1077 sv_setpv(sv,GvENAME(PL_defoutgv));
1078 sv_catpvs(sv,"_TOP");
1082 if (!isGV_with_GP(PL_defoutgv))
1084 else if (GvIOp(PL_defoutgv))
1085 s = IoFMT_NAME(GvIOp(PL_defoutgv));
1087 s = GvENAME(PL_defoutgv);
1091 if (GvIO(PL_defoutgv))
1092 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
1095 if (GvIO(PL_defoutgv))
1096 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
1099 if (GvIO(PL_defoutgv))
1100 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
1107 sv_setiv(sv, (IV)CopARYBASE_get(PL_curcop));
1110 if (GvIO(PL_defoutgv))
1111 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
1115 sv_copypv(sv, PL_ors_sv);
1118 sv_setiv(sv, (IV)PerlProc_getpid());
1125 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
1127 sv_setnv(sv, (NV)errno);
1130 if (errno == errno_isOS2 || errno == errno_isOS2_set)
1131 sv_setpv(sv, os2error(Perl_rc));
1134 sv_setpv(sv, errno ? Strerror(errno) : "");
1136 SvPOK_on(sv); /* may have got removed during taint processing */
1141 SvNOK_on(sv); /* what a wonderful hack! */
1144 sv_setiv(sv, (IV)PL_uid);
1147 sv_setiv(sv, (IV)PL_euid);
1150 sv_setiv(sv, (IV)PL_gid);
1153 sv_setiv(sv, (IV)PL_egid);
1155 #ifdef HAS_GETGROUPS
1157 Groups_t *gary = NULL;
1158 I32 i, num_groups = getgroups(0, gary);
1159 Newx(gary, num_groups, Groups_t);
1160 num_groups = getgroups(num_groups, gary);
1161 for (i = 0; i < num_groups; i++)
1162 Perl_sv_catpvf(aTHX_ sv, " %"IVdf, (IV)gary[i]);
1165 (void)SvIOK_on(sv); /* what a wonderful hack! */
1175 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
1177 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
1179 PERL_ARGS_ASSERT_MAGIC_GETUVAR;
1181 if (uf && uf->uf_val)
1182 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
1187 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
1190 STRLEN len = 0, klen;
1191 const char *s = SvOK(sv) ? SvPV_const(sv,len) : "";
1192 const char * const ptr = MgPV_const(mg,klen);
1195 PERL_ARGS_ASSERT_MAGIC_SETENV;
1197 #ifdef DYNAMIC_ENV_FETCH
1198 /* We just undefd an environment var. Is a replacement */
1199 /* waiting in the wings? */
1201 SV ** const valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE);
1203 s = SvOK(*valp) ? SvPV_const(*valp, len) : "";
1207 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
1208 /* And you'll never guess what the dog had */
1209 /* in its mouth... */
1211 MgTAINTEDDIR_off(mg);
1213 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1214 char pathbuf[256], eltbuf[256], *cp, *elt;
1217 my_strlcpy(eltbuf, s, sizeof(eltbuf));
1219 do { /* DCL$PATH may be a search list */
1220 while (1) { /* as may dev portion of any element */
1221 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
1222 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
1223 cando_by_name(S_IWUSR,0,elt) ) {
1224 MgTAINTEDDIR_on(mg);
1228 if ((cp = strchr(elt, ':')) != NULL)
1230 if (my_trnlnm(elt, eltbuf, j++))
1236 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
1239 if (s && klen == 4 && strEQ(ptr,"PATH")) {
1240 const char * const strend = s + len;
1242 while (s < strend) {
1246 #ifdef VMS /* Hmm. How do we get $Config{path_sep} from C? */
1247 const char path_sep = '|';
1249 const char path_sep = ':';
1251 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
1252 s, strend, path_sep, &i);
1254 if (i >= (I32)sizeof tmpbuf /* too long -- assume the worst */
1256 || !strchr(tmpbuf, ':') /* no colon thus no device name -- assume relative path */
1258 || *tmpbuf != '/' /* no starting slash -- assume relative path */
1260 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
1261 MgTAINTEDDIR_on(mg);
1267 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1273 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
1275 PERL_ARGS_ASSERT_MAGIC_CLEARENV;
1276 PERL_UNUSED_ARG(sv);
1277 my_setenv(MgPV_nolen_const(mg),NULL);
1282 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1285 PERL_ARGS_ASSERT_MAGIC_SET_ALL_ENV;
1286 PERL_UNUSED_ARG(mg);
1288 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1290 if (PL_localizing) {
1293 hv_iterinit(MUTABLE_HV(sv));
1294 while ((entry = hv_iternext(MUTABLE_HV(sv)))) {
1296 my_setenv(hv_iterkey(entry, &keylen),
1297 SvPV_nolen_const(hv_iterval(MUTABLE_HV(sv), entry)));
1305 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1308 PERL_ARGS_ASSERT_MAGIC_CLEAR_ALL_ENV;
1309 PERL_UNUSED_ARG(sv);
1310 PERL_UNUSED_ARG(mg);
1312 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1320 #ifdef HAS_SIGPROCMASK
1322 restore_sigmask(pTHX_ SV *save_sv)
1324 const sigset_t * const ossetp = (const sigset_t *) SvPV_nolen_const( save_sv );
1325 (void)sigprocmask(SIG_SETMASK, ossetp, NULL);
1329 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1332 /* Are we fetching a signal entry? */
1333 int i = (I16)mg->mg_private;
1335 PERL_ARGS_ASSERT_MAGIC_GETSIG;
1338 mg->mg_private = i = whichsig(MgPV_nolen_const(mg));
1343 sv_setsv(sv,PL_psig_ptr[i]);
1345 Sighandler_t sigstate = rsignal_state(i);
1346 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1347 if (PL_sig_handlers_initted && PL_sig_ignoring[i])
1350 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1351 if (PL_sig_handlers_initted && PL_sig_defaulting[i])
1354 /* cache state so we don't fetch it again */
1355 if(sigstate == (Sighandler_t) SIG_IGN)
1356 sv_setpvs(sv,"IGNORE");
1358 sv_setsv(sv,&PL_sv_undef);
1359 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1366 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1368 PERL_ARGS_ASSERT_MAGIC_CLEARSIG;
1370 magic_setsig(NULL, mg);
1371 return sv_unmagic(sv, mg->mg_type);
1375 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1376 Perl_csighandler(int sig, siginfo_t *sip PERL_UNUSED_DECL, void *uap PERL_UNUSED_DECL)
1378 Perl_csighandler(int sig)
1381 #ifdef PERL_GET_SIG_CONTEXT
1382 dTHXa(PERL_GET_SIG_CONTEXT);
1386 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1387 (void) rsignal(sig, PL_csighandlerp);
1388 if (PL_sig_ignoring[sig]) return;
1390 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1391 if (PL_sig_defaulting[sig])
1392 #ifdef KILL_BY_SIGPRC
1393 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1408 (PL_signals & PERL_SIGNALS_UNSAFE_FLAG))
1409 /* Call the perl level handler now--
1410 * with risk we may be in malloc() or being destructed etc. */
1411 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1412 (*PL_sighandlerp)(sig, NULL, NULL);
1414 (*PL_sighandlerp)(sig);
1417 if (!PL_psig_pend) return;
1418 /* Set a flag to say this signal is pending, that is awaiting delivery after
1419 * the current Perl opcode completes */
1420 PL_psig_pend[sig]++;
1422 #ifndef SIG_PENDING_DIE_COUNT
1423 # define SIG_PENDING_DIE_COUNT 120
1425 /* Add one to say _a_ signal is pending */
1426 if (++PL_sig_pending >= SIG_PENDING_DIE_COUNT)
1427 Perl_croak(aTHX_ "Maximal count of pending signals (%lu) exceeded",
1428 (unsigned long)SIG_PENDING_DIE_COUNT);
1432 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1434 Perl_csighandler_init(void)
1437 if (PL_sig_handlers_initted) return;
1439 for (sig = 1; sig < SIG_SIZE; sig++) {
1440 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1442 PL_sig_defaulting[sig] = 1;
1443 (void) rsignal(sig, PL_csighandlerp);
1445 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1446 PL_sig_ignoring[sig] = 0;
1449 PL_sig_handlers_initted = 1;
1453 #if defined HAS_SIGPROCMASK
1455 unblock_sigmask(pTHX_ void* newset)
1457 sigprocmask(SIG_UNBLOCK, (sigset_t*)newset, NULL);
1462 Perl_despatch_signals(pTHX)
1467 for (sig = 1; sig < SIG_SIZE; sig++) {
1468 if (PL_psig_pend[sig]) {
1470 #ifdef HAS_SIGPROCMASK
1471 /* From sigaction(2) (FreeBSD man page):
1472 * | Signal routines normally execute with the signal that
1473 * | caused their invocation blocked, but other signals may
1475 * Emulation of this behavior (from within Perl) is enabled
1479 sigset_t newset, oldset;
1481 sigemptyset(&newset);
1482 sigaddset(&newset, sig);
1483 sigprocmask(SIG_BLOCK, &newset, &oldset);
1484 was_blocked = sigismember(&oldset, sig);
1486 SV* save_sv = newSVpvn((char *)(&newset), sizeof(sigset_t));
1488 SAVEFREESV(save_sv);
1489 SAVEDESTRUCTOR_X(unblock_sigmask, SvPV_nolen(save_sv));
1492 PL_psig_pend[sig] = 0;
1493 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1494 (*PL_sighandlerp)(sig, NULL, NULL);
1496 (*PL_sighandlerp)(sig);
1498 #ifdef HAS_SIGPROCMASK
1507 /* sv of NULL signifies that we're acting as magic_clearsig. */
1509 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1514 /* Need to be careful with SvREFCNT_dec(), because that can have side
1515 * effects (due to closures). We must make sure that the new disposition
1516 * is in place before it is called.
1520 #ifdef HAS_SIGPROCMASK
1524 register const char *s = MgPV_const(mg,len);
1526 PERL_ARGS_ASSERT_MAGIC_SETSIG;
1529 if (strEQ(s,"__DIE__"))
1531 else if (strEQ(s,"__WARN__")
1532 && (sv ? 1 : PL_warnhook != PERL_WARNHOOK_FATAL)) {
1533 /* Merge the existing behaviours, which are as follows:
1534 magic_setsig, we always set svp to &PL_warnhook
1535 (hence we always change the warnings handler)
1536 For magic_clearsig, we don't change the warnings handler if it's
1537 set to the &PL_warnhook. */
1540 Perl_croak(aTHX_ "No such hook: %s", s);
1543 if (*svp != PERL_WARNHOOK_FATAL)
1549 i = (I16)mg->mg_private;
1551 i = whichsig(s); /* ...no, a brick */
1552 mg->mg_private = (U16)i;
1556 Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1559 #ifdef HAS_SIGPROCMASK
1560 /* Avoid having the signal arrive at a bad time, if possible. */
1563 sigprocmask(SIG_BLOCK, &set, &save);
1565 save_sv = newSVpvn((char *)(&save), sizeof(sigset_t));
1566 SAVEFREESV(save_sv);
1567 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1570 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1571 if (!PL_sig_handlers_initted) Perl_csighandler_init();
1573 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1574 PL_sig_ignoring[i] = 0;
1576 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1577 PL_sig_defaulting[i] = 0;
1579 to_dec = PL_psig_ptr[i];
1581 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1582 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1584 /* Signals don't change name during the program's execution, so once
1585 they're cached in the appropriate slot of PL_psig_name, they can
1588 Ideally we'd find some way of making SVs at (C) compile time, or
1589 at least, doing most of the work. */
1590 if (!PL_psig_name[i]) {
1591 PL_psig_name[i] = newSVpvn(s, len);
1592 SvREADONLY_on(PL_psig_name[i]);
1595 SvREFCNT_dec(PL_psig_name[i]);
1596 PL_psig_name[i] = NULL;
1597 PL_psig_ptr[i] = NULL;
1600 if (sv && (isGV_with_GP(sv) || SvROK(sv))) {
1602 (void)rsignal(i, PL_csighandlerp);
1605 *svp = SvREFCNT_inc_simple_NN(sv);
1607 if (sv && SvOK(sv)) {
1608 s = SvPV_force(sv, len);
1612 if (sv && strEQ(s,"IGNORE")) {
1614 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1615 PL_sig_ignoring[i] = 1;
1616 (void)rsignal(i, PL_csighandlerp);
1618 (void)rsignal(i, (Sighandler_t) SIG_IGN);
1622 else if (!sv || strEQ(s,"DEFAULT") || !len) {
1624 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1625 PL_sig_defaulting[i] = 1;
1626 (void)rsignal(i, PL_csighandlerp);
1628 (void)rsignal(i, (Sighandler_t) SIG_DFL);
1634 * We should warn if HINT_STRICT_REFS, but without
1635 * access to a known hint bit in a known OP, we can't
1636 * tell whether HINT_STRICT_REFS is in force or not.
1638 if (!strchr(s,':') && !strchr(s,'\''))
1639 Perl_sv_insert_flags(aTHX_ sv, 0, 0, STR_WITH_LEN("main::"),
1642 (void)rsignal(i, PL_csighandlerp);
1644 *svp = SvREFCNT_inc_simple_NN(sv);
1648 #ifdef HAS_SIGPROCMASK
1652 SvREFCNT_dec(to_dec);
1655 #endif /* !PERL_MICRO */
1658 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1661 PERL_ARGS_ASSERT_MAGIC_SETISA;
1662 PERL_UNUSED_ARG(sv);
1664 /* Skip _isaelem because _isa will handle it shortly */
1665 if (PL_delaymagic & DM_ARRAY_ISA && mg->mg_type == PERL_MAGIC_isaelem)
1668 return magic_clearisa(NULL, mg);
1671 /* sv of NULL signifies that we're acting as magic_setisa. */
1673 Perl_magic_clearisa(pTHX_ SV *sv, MAGIC *mg)
1678 PERL_ARGS_ASSERT_MAGIC_CLEARISA;
1680 /* Bail out if destruction is going on */
1681 if(PL_phase == PERL_PHASE_DESTRUCT) return 0;
1684 av_clear(MUTABLE_AV(sv));
1686 if (SvTYPE(mg->mg_obj) != SVt_PVGV && SvSMAGICAL(mg->mg_obj))
1687 /* This occurs with setisa_elem magic, which calls this
1689 mg = mg_find(mg->mg_obj, PERL_MAGIC_isa);
1691 if (SvTYPE(mg->mg_obj) == SVt_PVAV) { /* multiple stashes */
1692 SV **svp = AvARRAY((AV *)mg->mg_obj);
1693 I32 items = AvFILLp((AV *)mg->mg_obj) + 1;
1695 stash = GvSTASH((GV *)*svp++);
1696 if (stash && HvENAME(stash)) mro_isa_changed_in(stash);
1703 (const GV *)mg->mg_obj
1706 /* The stash may have been detached from the symbol table, so check its
1707 name before doing anything. */
1708 if (stash && HvENAME_get(stash))
1709 mro_isa_changed_in(stash);
1715 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1718 PERL_ARGS_ASSERT_MAGIC_SETAMAGIC;
1719 PERL_UNUSED_ARG(sv);
1720 PERL_UNUSED_ARG(mg);
1721 PL_amagic_generation++;
1727 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1729 HV * const hv = MUTABLE_HV(LvTARG(sv));
1732 PERL_ARGS_ASSERT_MAGIC_GETNKEYS;
1733 PERL_UNUSED_ARG(mg);
1736 (void) hv_iterinit(hv);
1737 if (! SvTIED_mg((const SV *)hv, PERL_MAGIC_tied))
1740 while (hv_iternext(hv))
1745 sv_setiv(sv, (IV)i);
1750 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1752 PERL_ARGS_ASSERT_MAGIC_SETNKEYS;
1753 PERL_UNUSED_ARG(mg);
1755 hv_ksplit(MUTABLE_HV(LvTARG(sv)), SvIV(sv));
1761 =for apidoc magic_methcall
1763 Invoke a magic method (like FETCH).
1765 * sv and mg are the tied thingy and the tie magic;
1766 * meth is the name of the method to call;
1767 * argc is the number of args (in addition to $self) to pass to the method;
1768 the args themselves are any values following the argc argument.
1770 G_DISCARD: invoke method with G_DISCARD flag and don't return a value
1771 G_UNDEF_FILL: fill the stack with argc pointers to PL_sv_undef.
1773 Returns the SV (if any) returned by the method, or NULL on failure.
1780 Perl_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, const char *meth, U32 flags,
1787 PERL_ARGS_ASSERT_MAGIC_METHCALL;
1791 if (flags & G_WRITING_TO_STDERR) {
1795 SAVESPTR(PL_stderrgv);
1799 PUSHSTACKi(PERLSI_MAGIC);
1803 PUSHs(SvTIED_obj(sv, mg));
1804 if (flags & G_UNDEF_FILL) {
1806 PUSHs(&PL_sv_undef);
1808 } else if (argc > 0) {
1810 va_start(args, argc);
1813 SV *const sv = va_arg(args, SV *);
1820 if (flags & G_DISCARD) {
1821 call_method(meth, G_SCALAR|G_DISCARD);
1824 if (call_method(meth, G_SCALAR))
1825 ret = *PL_stack_sp--;
1828 if (flags & G_WRITING_TO_STDERR)
1835 /* wrapper for magic_methcall that creates the first arg */
1838 S_magic_methcall1(pTHX_ SV *sv, const MAGIC *mg, const char *meth, U32 flags,
1844 PERL_ARGS_ASSERT_MAGIC_METHCALL1;
1847 if (mg->mg_len >= 0) {
1848 arg1 = newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
1850 else if (mg->mg_len == HEf_SVKEY)
1851 arg1 = MUTABLE_SV(mg->mg_ptr);
1853 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1854 arg1 = newSViv((IV)(mg->mg_len));
1858 return Perl_magic_methcall(aTHX_ sv, mg, meth, flags, n - 1, val);
1860 return Perl_magic_methcall(aTHX_ sv, mg, meth, flags, n, arg1, val);
1864 S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth)
1869 PERL_ARGS_ASSERT_MAGIC_METHPACK;
1871 ret = magic_methcall1(sv, mg, meth, 0, 1, NULL);
1878 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1880 PERL_ARGS_ASSERT_MAGIC_GETPACK;
1882 if (mg->mg_type == PERL_MAGIC_tiedelem)
1883 mg->mg_flags |= MGf_GSKIP;
1884 magic_methpack(sv,mg,"FETCH");
1889 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1895 PERL_ARGS_ASSERT_MAGIC_SETPACK;
1897 /* in the code C<$tied{foo} = $val>, the "thing" that gets passed to
1898 * STORE() is not $val, but rather a PVLV (the sv in this call), whose
1899 * public flags indicate its value based on copying from $val. Doing
1900 * mg_set() on the PVLV temporarily does SvMAGICAL_off(), then calls us.
1901 * So STORE()'s $_[2] arg is a temporarily disarmed PVLV. This goes
1902 * wrong if $val happened to be tainted, as sv hasn't got magic
1903 * enabled, even though taint magic is in the chain. In which case,
1904 * fake up a temporary tainted value (this is easier than temporarily
1905 * re-enabling magic on sv). */
1907 if (PL_tainting && (tmg = mg_find(sv, PERL_MAGIC_taint))
1908 && (tmg->mg_len & 1))
1910 val = sv_mortalcopy(sv);
1916 magic_methcall1(sv, mg, "STORE", G_DISCARD, 2, val);
1921 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1923 PERL_ARGS_ASSERT_MAGIC_CLEARPACK;
1925 return magic_methpack(sv,mg,"DELETE");
1930 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1936 PERL_ARGS_ASSERT_MAGIC_SIZEPACK;
1938 retsv = magic_methcall1(sv, mg, "FETCHSIZE", 0, 1, NULL);
1940 retval = SvIV(retsv)-1;
1942 Perl_croak(aTHX_ "FETCHSIZE returned a negative value");
1944 return (U32) retval;
1948 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1952 PERL_ARGS_ASSERT_MAGIC_WIPEPACK;
1954 Perl_magic_methcall(aTHX_ sv, mg, "CLEAR", G_DISCARD, 0);
1959 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1964 PERL_ARGS_ASSERT_MAGIC_NEXTPACK;
1966 ret = SvOK(key) ? Perl_magic_methcall(aTHX_ sv, mg, "NEXTKEY", 0, 1, key)
1967 : Perl_magic_methcall(aTHX_ sv, mg, "FIRSTKEY", 0, 0);
1974 Perl_magic_existspack(pTHX_ SV *sv, const MAGIC *mg)
1976 PERL_ARGS_ASSERT_MAGIC_EXISTSPACK;
1978 return magic_methpack(sv,mg,"EXISTS");
1982 Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1986 SV * const tied = SvTIED_obj(MUTABLE_SV(hv), mg);
1987 HV * const pkg = SvSTASH((const SV *)SvRV(tied));
1989 PERL_ARGS_ASSERT_MAGIC_SCALARPACK;
1991 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1993 if (HvEITER_get(hv))
1994 /* we are in an iteration so the hash cannot be empty */
1996 /* no xhv_eiter so now use FIRSTKEY */
1997 key = sv_newmortal();
1998 magic_nextpack(MUTABLE_SV(hv), mg, key);
1999 HvEITER_set(hv, NULL); /* need to reset iterator */
2000 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
2003 /* there is a SCALAR method that we can call */
2004 retval = Perl_magic_methcall(aTHX_ MUTABLE_SV(hv), mg, "SCALAR", 0, 0);
2006 retval = &PL_sv_undef;
2011 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
2014 GV * const gv = PL_DBline;
2015 const I32 i = SvTRUE(sv);
2016 SV ** const svp = av_fetch(GvAV(gv),
2017 atoi(MgPV_nolen_const(mg)), FALSE);
2019 PERL_ARGS_ASSERT_MAGIC_SETDBLINE;
2021 if (svp && SvIOKp(*svp)) {
2022 OP * const o = INT2PTR(OP*,SvIVX(*svp));
2024 /* set or clear breakpoint in the relevant control op */
2026 o->op_flags |= OPf_SPECIAL;
2028 o->op_flags &= ~OPf_SPECIAL;
2035 Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg)
2038 AV * const obj = MUTABLE_AV(mg->mg_obj);
2040 PERL_ARGS_ASSERT_MAGIC_GETARYLEN;
2043 sv_setiv(sv, AvFILL(obj) + CopARYBASE_get(PL_curcop));
2051 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
2054 AV * const obj = MUTABLE_AV(mg->mg_obj);
2056 PERL_ARGS_ASSERT_MAGIC_SETARYLEN;
2059 av_fill(obj, SvIV(sv) - CopARYBASE_get(PL_curcop));
2061 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
2062 "Attempt to set length of freed array");
2068 Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
2072 PERL_ARGS_ASSERT_MAGIC_FREEARYLEN_P;
2073 PERL_UNUSED_ARG(sv);
2075 /* during global destruction, mg_obj may already have been freed */
2076 if (PL_in_clean_all)
2079 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
2082 /* arylen scalar holds a pointer back to the array, but doesn't own a
2083 reference. Hence the we (the array) are about to go away with it
2084 still pointing at us. Clear its pointer, else it would be pointing
2085 at free memory. See the comment in sv_magic about reference loops,
2086 and why it can't own a reference to us. */
2093 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
2096 SV* const lsv = LvTARG(sv);
2098 PERL_ARGS_ASSERT_MAGIC_GETPOS;
2099 PERL_UNUSED_ARG(mg);
2101 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
2102 MAGIC * const found = mg_find(lsv, PERL_MAGIC_regex_global);
2103 if (found && found->mg_len >= 0) {
2104 I32 i = found->mg_len;
2106 sv_pos_b2u(lsv, &i);
2107 sv_setiv(sv, i + CopARYBASE_get(PL_curcop));
2116 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
2119 SV* const lsv = LvTARG(sv);
2125 PERL_ARGS_ASSERT_MAGIC_SETPOS;
2126 PERL_UNUSED_ARG(mg);
2128 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
2129 found = mg_find(lsv, PERL_MAGIC_regex_global);
2135 #ifdef PERL_OLD_COPY_ON_WRITE
2137 sv_force_normal_flags(lsv, 0);
2139 found = sv_magicext(lsv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
2142 else if (!SvOK(sv)) {
2146 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
2148 pos = SvIV(sv) - CopARYBASE_get(PL_curcop);
2151 ulen = sv_len_utf8(lsv);
2161 else if (pos > (SSize_t)len)
2166 sv_pos_u2b(lsv, &p, 0);
2170 found->mg_len = pos;
2171 found->mg_flags &= ~MGf_MINMATCH;
2177 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
2180 SV * const lsv = LvTARG(sv);
2181 const char * const tmps = SvPV_const(lsv,len);
2182 STRLEN offs = LvTARGOFF(sv);
2183 STRLEN rem = LvTARGLEN(sv);
2185 PERL_ARGS_ASSERT_MAGIC_GETSUBSTR;
2186 PERL_UNUSED_ARG(mg);
2189 offs = sv_pos_u2b_flags(lsv, offs, &rem, SV_CONST_RETURN);
2192 if (rem > len - offs)
2194 sv_setpvn(sv, tmps + offs, rem);
2201 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
2205 const char * const tmps = SvPV_const(sv, len);
2206 SV * const lsv = LvTARG(sv);
2207 STRLEN lvoff = LvTARGOFF(sv);
2208 STRLEN lvlen = LvTARGLEN(sv);
2210 PERL_ARGS_ASSERT_MAGIC_SETSUBSTR;
2211 PERL_UNUSED_ARG(mg);
2214 sv_utf8_upgrade(lsv);
2215 lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN);
2216 sv_insert(lsv, lvoff, lvlen, tmps, len);
2217 LvTARGLEN(sv) = sv_len_utf8(sv);
2220 else if (lsv && SvUTF8(lsv)) {
2222 lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN);
2223 LvTARGLEN(sv) = len;
2224 utf8 = (char*)bytes_to_utf8((U8*)tmps, &len);
2225 sv_insert(lsv, lvoff, lvlen, utf8, len);
2229 sv_insert(lsv, lvoff, lvlen, tmps, len);
2230 LvTARGLEN(sv) = len;
2237 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
2241 PERL_ARGS_ASSERT_MAGIC_GETTAINT;
2242 PERL_UNUSED_ARG(sv);
2244 TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1));
2249 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
2253 PERL_ARGS_ASSERT_MAGIC_SETTAINT;
2254 PERL_UNUSED_ARG(sv);
2256 /* update taint status */
2265 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
2267 SV * const lsv = LvTARG(sv);
2269 PERL_ARGS_ASSERT_MAGIC_GETVEC;
2270 PERL_UNUSED_ARG(mg);
2273 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
2281 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
2283 PERL_ARGS_ASSERT_MAGIC_SETVEC;
2284 PERL_UNUSED_ARG(mg);
2285 do_vecset(sv); /* XXX slurp this routine */
2290 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
2295 PERL_ARGS_ASSERT_MAGIC_GETDEFELEM;
2297 if (LvTARGLEN(sv)) {
2299 SV * const ahv = LvTARG(sv);
2300 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, FALSE, 0);
2305 AV *const av = MUTABLE_AV(LvTARG(sv));
2306 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
2307 targ = AvARRAY(av)[LvTARGOFF(sv)];
2309 if (targ && (targ != &PL_sv_undef)) {
2310 /* somebody else defined it for us */
2311 SvREFCNT_dec(LvTARG(sv));
2312 LvTARG(sv) = SvREFCNT_inc_simple_NN(targ);
2314 SvREFCNT_dec(mg->mg_obj);
2316 mg->mg_flags &= ~MGf_REFCOUNTED;
2321 sv_setsv(sv, targ ? targ : &PL_sv_undef);
2326 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
2328 PERL_ARGS_ASSERT_MAGIC_SETDEFELEM;
2329 PERL_UNUSED_ARG(mg);
2333 sv_setsv(LvTARG(sv), sv);
2334 SvSETMAGIC(LvTARG(sv));
2340 Perl_vivify_defelem(pTHX_ SV *sv)
2346 PERL_ARGS_ASSERT_VIVIFY_DEFELEM;
2348 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
2351 SV * const ahv = LvTARG(sv);
2352 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, TRUE, 0);
2355 if (!value || value == &PL_sv_undef)
2356 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(mg->mg_obj));
2359 AV *const av = MUTABLE_AV(LvTARG(sv));
2360 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
2361 LvTARG(sv) = NULL; /* array can't be extended */
2363 SV* const * const svp = av_fetch(av, LvTARGOFF(sv), TRUE);
2364 if (!svp || (value = *svp) == &PL_sv_undef)
2365 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
2368 SvREFCNT_inc_simple_void(value);
2369 SvREFCNT_dec(LvTARG(sv));
2372 SvREFCNT_dec(mg->mg_obj);
2374 mg->mg_flags &= ~MGf_REFCOUNTED;
2378 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
2380 PERL_ARGS_ASSERT_MAGIC_KILLBACKREFS;
2381 Perl_sv_kill_backrefs(aTHX_ sv, MUTABLE_AV(mg->mg_obj));
2386 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
2388 PERL_ARGS_ASSERT_MAGIC_SETMGLOB;
2389 PERL_UNUSED_CONTEXT;
2391 if (!isGV_with_GP(sv))
2397 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
2399 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
2401 PERL_ARGS_ASSERT_MAGIC_SETUVAR;
2403 if (uf && uf->uf_set)
2404 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
2409 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2411 const char type = mg->mg_type;
2413 PERL_ARGS_ASSERT_MAGIC_SETREGEXP;
2415 if (type == PERL_MAGIC_qr) {
2416 } else if (type == PERL_MAGIC_bm) {
2420 assert(type == PERL_MAGIC_fm);
2422 return sv_unmagic(sv, type);
2425 #ifdef USE_LOCALE_COLLATE
2427 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
2429 PERL_ARGS_ASSERT_MAGIC_SETCOLLXFRM;
2432 * RenE<eacute> Descartes said "I think not."
2433 * and vanished with a faint plop.
2435 PERL_UNUSED_CONTEXT;
2436 PERL_UNUSED_ARG(sv);
2438 Safefree(mg->mg_ptr);
2444 #endif /* USE_LOCALE_COLLATE */
2446 /* Just clear the UTF-8 cache data. */
2448 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2450 PERL_ARGS_ASSERT_MAGIC_SETUTF8;
2451 PERL_UNUSED_CONTEXT;
2452 PERL_UNUSED_ARG(sv);
2453 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2455 mg->mg_len = -1; /* The mg_len holds the len cache. */
2460 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
2463 register const char *s;
2465 register const REGEXP * rx;
2466 const char * const remaining = mg->mg_ptr + 1;
2471 PERL_ARGS_ASSERT_MAGIC_SET;
2473 switch (*mg->mg_ptr) {
2474 case '\015': /* $^MATCH */
2475 if (strEQ(remaining, "ATCH"))
2477 case '`': /* ${^PREMATCH} caught below */
2479 paren = RX_BUFF_IDX_PREMATCH;
2481 case '\'': /* ${^POSTMATCH} caught below */
2483 paren = RX_BUFF_IDX_POSTMATCH;
2487 paren = RX_BUFF_IDX_FULLMATCH;
2489 case '1': case '2': case '3': case '4':
2490 case '5': case '6': case '7': case '8': case '9':
2491 paren = atoi(mg->mg_ptr);
2493 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
2494 CALLREG_NUMBUF_STORE((REGEXP * const)rx,paren,sv);
2496 /* Croak with a READONLY error when a numbered match var is
2497 * set without a previous pattern match. Unless it's C<local $1>
2499 if (!PL_localizing) {
2500 Perl_croak_no_modify(aTHX);
2504 case '\001': /* ^A */
2505 sv_setsv(PL_bodytarget, sv);
2506 /* mg_set() has temporarily made sv non-magical */
2508 if ((tmg = mg_find(sv,PERL_MAGIC_taint)) && tmg->mg_len & 1)
2509 SvTAINTED_on(PL_bodytarget);
2511 SvTAINTED_off(PL_bodytarget);
2514 case '\003': /* ^C */
2515 PL_minus_c = cBOOL(SvIV(sv));
2518 case '\004': /* ^D */
2520 s = SvPV_nolen_const(sv);
2521 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
2522 if (DEBUG_x_TEST || DEBUG_B_TEST)
2523 dump_all_perl(!DEBUG_B_TEST);
2525 PL_debug = (SvIV(sv)) | DEBUG_TOP_FLAG;
2528 case '\005': /* ^E */
2529 if (*(mg->mg_ptr+1) == '\0') {
2531 set_vaxc_errno(SvIV(sv));
2534 SetLastError( SvIV(sv) );
2537 os2_setsyserrno(SvIV(sv));
2539 /* will anyone ever use this? */
2540 SETERRNO(SvIV(sv), 4);
2545 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2546 SvREFCNT_dec(PL_encoding);
2547 if (SvOK(sv) || SvGMAGICAL(sv)) {
2548 PL_encoding = newSVsv(sv);
2555 case '\006': /* ^F */
2556 PL_maxsysfd = SvIV(sv);
2558 case '\010': /* ^H */
2559 PL_hints = SvIV(sv);
2561 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2562 Safefree(PL_inplace);
2563 PL_inplace = SvOK(sv) ? savesvpv(sv) : NULL;
2565 case '\017': /* ^O */
2566 if (*(mg->mg_ptr+1) == '\0') {
2567 Safefree(PL_osname);
2570 TAINT_PROPER("assigning to $^O");
2571 PL_osname = savesvpv(sv);
2574 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2576 const char *const start = SvPV(sv, len);
2577 const char *out = (const char*)memchr(start, '\0', len);
2581 PL_compiling.cop_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
2582 PL_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
2584 /* Opening for input is more common than opening for output, so
2585 ensure that hints for input are sooner on linked list. */
2586 tmp = out ? newSVpvn_flags(out + 1, start + len - out - 1,
2588 : newSVpvs_flags("", SvUTF8(sv));
2589 (void)hv_stores(GvHV(PL_hintgv), "open>", tmp);
2592 tmp = newSVpvn_flags(start, out ? (STRLEN)(out - start) : len,
2594 (void)hv_stores(GvHV(PL_hintgv), "open<", tmp);
2598 case '\020': /* ^P */
2599 if (*remaining == '\0') { /* ^P */
2600 PL_perldb = SvIV(sv);
2601 if (PL_perldb && !PL_DBsingle)
2604 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
2606 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
2610 case '\024': /* ^T */
2612 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2614 PL_basetime = (Time_t)SvIV(sv);
2617 case '\025': /* ^UTF8CACHE */
2618 if (strEQ(mg->mg_ptr+1, "TF8CACHE")) {
2619 PL_utf8cache = (signed char) sv_2iv(sv);
2622 case '\027': /* ^W & $^WARNING_BITS */
2623 if (*(mg->mg_ptr+1) == '\0') {
2624 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2626 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2627 | (i ? G_WARN_ON : G_WARN_OFF) ;
2630 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2631 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2632 if (!SvPOK(sv) && PL_localizing) {
2633 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2634 PL_compiling.cop_warnings = pWARN_NONE;
2639 int accumulate = 0 ;
2640 int any_fatals = 0 ;
2641 const char * const ptr = SvPV_const(sv, len) ;
2642 for (i = 0 ; i < len ; ++i) {
2643 accumulate |= ptr[i] ;
2644 any_fatals |= (ptr[i] & 0xAA) ;
2647 if (!specialWARN(PL_compiling.cop_warnings))
2648 PerlMemShared_free(PL_compiling.cop_warnings);
2649 PL_compiling.cop_warnings = pWARN_NONE;
2651 /* Yuck. I can't see how to abstract this: */
2652 else if (isWARN_on(((STRLEN *)SvPV_nolen_const(sv)) - 1,
2653 WARN_ALL) && !any_fatals) {
2654 if (!specialWARN(PL_compiling.cop_warnings))
2655 PerlMemShared_free(PL_compiling.cop_warnings);
2656 PL_compiling.cop_warnings = pWARN_ALL;
2657 PL_dowarn |= G_WARN_ONCE ;
2661 const char *const p = SvPV_const(sv, len);
2663 PL_compiling.cop_warnings
2664 = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
2667 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2668 PL_dowarn |= G_WARN_ONCE ;
2676 if (PL_localizing) {
2677 if (PL_localizing == 1)
2678 SAVESPTR(PL_last_in_gv);
2680 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2681 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2684 if (isGV_with_GP(PL_defoutgv)) {
2685 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2686 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2687 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2691 if (isGV_with_GP(PL_defoutgv)) {
2692 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2693 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2694 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2698 if (isGV_with_GP(PL_defoutgv))
2699 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv));
2702 if (isGV_with_GP(PL_defoutgv)) {
2703 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv));
2704 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2705 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2709 if (isGV_with_GP(PL_defoutgv))
2710 IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv));
2714 IO * const io = GvIO(PL_defoutgv);
2717 if ((SvIV(sv)) == 0)
2718 IoFLAGS(io) &= ~IOf_FLUSH;
2720 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2721 PerlIO *ofp = IoOFP(io);
2723 (void)PerlIO_flush(ofp);
2724 IoFLAGS(io) |= IOf_FLUSH;
2730 SvREFCNT_dec(PL_rs);
2731 PL_rs = newSVsv(sv);
2734 SvREFCNT_dec(PL_ors_sv);
2735 if (SvOK(sv) || SvGMAGICAL(sv)) {
2736 PL_ors_sv = newSVsv(sv);
2743 CopARYBASE_set(&PL_compiling, SvIV(sv));
2746 #ifdef COMPLEX_STATUS
2747 if (PL_localizing == 2) {
2748 SvUPGRADE(sv, SVt_PVLV);
2749 PL_statusvalue = LvTARGOFF(sv);
2750 PL_statusvalue_vms = LvTARGLEN(sv);
2754 #ifdef VMSISH_STATUS
2756 STATUS_NATIVE_CHILD_SET((U32)SvIV(sv));
2759 STATUS_UNIX_EXIT_SET(SvIV(sv));
2764 # define PERL_VMS_BANG vaxc$errno
2766 # define PERL_VMS_BANG 0
2768 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2769 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2774 if (PL_delaymagic) {
2775 PL_delaymagic |= DM_RUID;
2776 break; /* don't do magic till later */
2779 (void)setruid((Uid_t)PL_uid);
2782 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2784 #ifdef HAS_SETRESUID
2785 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2787 if (PL_uid == PL_euid) { /* special case $< = $> */
2789 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2790 if (PL_uid != 0 && PerlProc_getuid() == 0)
2791 (void)PerlProc_setuid(0);
2793 (void)PerlProc_setuid(PL_uid);
2795 PL_uid = PerlProc_getuid();
2796 Perl_croak(aTHX_ "setruid() not implemented");
2801 PL_uid = PerlProc_getuid();
2805 if (PL_delaymagic) {
2806 PL_delaymagic |= DM_EUID;
2807 break; /* don't do magic till later */
2810 (void)seteuid((Uid_t)PL_euid);
2813 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2815 #ifdef HAS_SETRESUID
2816 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2818 if (PL_euid == PL_uid) /* special case $> = $< */
2819 PerlProc_setuid(PL_euid);
2821 PL_euid = PerlProc_geteuid();
2822 Perl_croak(aTHX_ "seteuid() not implemented");
2827 PL_euid = PerlProc_geteuid();
2831 if (PL_delaymagic) {
2832 PL_delaymagic |= DM_RGID;
2833 break; /* don't do magic till later */
2836 (void)setrgid((Gid_t)PL_gid);
2839 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2841 #ifdef HAS_SETRESGID
2842 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2844 if (PL_gid == PL_egid) /* special case $( = $) */
2845 (void)PerlProc_setgid(PL_gid);
2847 PL_gid = PerlProc_getgid();
2848 Perl_croak(aTHX_ "setrgid() not implemented");
2853 PL_gid = PerlProc_getgid();
2856 #ifdef HAS_SETGROUPS
2858 const char *p = SvPV_const(sv, len);
2859 Groups_t *gary = NULL;
2860 #ifdef _SC_NGROUPS_MAX
2861 int maxgrp = sysconf(_SC_NGROUPS_MAX);
2866 int maxgrp = NGROUPS;
2872 for (i = 0; i < maxgrp; ++i) {
2873 while (*p && !isSPACE(*p))
2880 Newx(gary, i + 1, Groups_t);
2882 Renew(gary, i + 1, Groups_t);
2886 (void)setgroups(i, gary);
2889 #else /* HAS_SETGROUPS */
2891 #endif /* HAS_SETGROUPS */
2892 if (PL_delaymagic) {
2893 PL_delaymagic |= DM_EGID;
2894 break; /* don't do magic till later */
2897 (void)setegid((Gid_t)PL_egid);
2900 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2902 #ifdef HAS_SETRESGID
2903 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2905 if (PL_egid == PL_gid) /* special case $) = $( */
2906 (void)PerlProc_setgid(PL_egid);
2908 PL_egid = PerlProc_getegid();
2909 Perl_croak(aTHX_ "setegid() not implemented");
2914 PL_egid = PerlProc_getegid();
2917 PL_chopset = SvPV_force(sv,len);
2920 LOCK_DOLLARZERO_MUTEX;
2921 #ifdef HAS_SETPROCTITLE
2922 /* The BSDs don't show the argv[] in ps(1) output, they
2923 * show a string from the process struct and provide
2924 * the setproctitle() routine to manipulate that. */
2925 if (PL_origalen != 1) {
2926 s = SvPV_const(sv, len);
2927 # if __FreeBSD_version > 410001
2928 /* The leading "-" removes the "perl: " prefix,
2929 * but not the "(perl) suffix from the ps(1)
2930 * output, because that's what ps(1) shows if the
2931 * argv[] is modified. */
2932 setproctitle("-%s", s);
2933 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2934 /* This doesn't really work if you assume that
2935 * $0 = 'foobar'; will wipe out 'perl' from the $0
2936 * because in ps(1) output the result will be like
2937 * sprintf("perl: %s (perl)", s)
2938 * I guess this is a security feature:
2939 * one (a user process) cannot get rid of the original name.
2941 setproctitle("%s", s);
2944 #elif defined(__hpux) && defined(PSTAT_SETCMD)
2945 if (PL_origalen != 1) {
2947 s = SvPV_const(sv, len);
2948 un.pst_command = (char *)s;
2949 pstat(PSTAT_SETCMD, un, len, 0, 0);
2952 if (PL_origalen > 1) {
2953 /* PL_origalen is set in perl_parse(). */
2954 s = SvPV_force(sv,len);
2955 if (len >= (STRLEN)PL_origalen-1) {
2956 /* Longer than original, will be truncated. We assume that
2957 * PL_origalen bytes are available. */
2958 Copy(s, PL_origargv[0], PL_origalen-1, char);
2961 /* Shorter than original, will be padded. */
2963 /* Special case for Mac OS X: see [perl #38868] */
2966 /* Is the space counterintuitive? Yes.
2967 * (You were expecting \0?)
2968 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2970 const int pad = ' ';
2972 Copy(s, PL_origargv[0], len, char);
2973 PL_origargv[0][len] = 0;
2974 memset(PL_origargv[0] + len + 1,
2975 pad, PL_origalen - len - 1);
2977 PL_origargv[0][PL_origalen-1] = 0;
2978 for (i = 1; i < PL_origargc; i++)
2980 #ifdef HAS_PRCTL_SET_NAME
2981 /* Set the legacy process name in addition to the POSIX name on Linux */
2982 if (prctl(PR_SET_NAME, (unsigned long)s, 0, 0, 0) != 0) {
2983 /* diag_listed_as: SKIPME */
2984 Perl_croak(aTHX_ "Can't set $0 with prctl(): %s", Strerror(errno));
2989 UNLOCK_DOLLARZERO_MUTEX;
2996 Perl_whichsig(pTHX_ const char *sig)
2998 register char* const* sigv;
3000 PERL_ARGS_ASSERT_WHICHSIG;
3001 PERL_UNUSED_CONTEXT;
3003 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
3004 if (strEQ(sig,*sigv))
3005 return PL_sig_num[sigv - (char* const*)PL_sig_name];
3007 if (strEQ(sig,"CHLD"))
3011 if (strEQ(sig,"CLD"))
3018 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
3019 Perl_sighandler(int sig, siginfo_t *sip, void *uap)
3021 Perl_sighandler(int sig)
3024 #ifdef PERL_GET_SIG_CONTEXT
3025 dTHXa(PERL_GET_SIG_CONTEXT);
3032 SV * const tSv = PL_Sv;
3036 XPV * const tXpv = PL_Xpv;
3037 I32 old_ss_ix = PL_savestack_ix;
3040 if (!PL_psig_ptr[sig]) {
3041 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
3046 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
3047 /* Max number of items pushed there is 3*n or 4. We cannot fix
3048 infinity, so we fix 4 (in fact 5): */
3049 if (PL_savestack_ix + 15 <= PL_savestack_max) {
3051 PL_savestack_ix += 5; /* Protect save in progress. */
3052 SAVEDESTRUCTOR_X(S_unwind_handler_stack, NULL);
3055 /* sv_2cv is too complicated, try a simpler variant first: */
3056 if (!SvROK(PL_psig_ptr[sig]) || !(cv = MUTABLE_CV(SvRV(PL_psig_ptr[sig])))
3057 || SvTYPE(cv) != SVt_PVCV) {
3059 cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD);
3062 if (!cv || !CvROOT(cv)) {
3063 Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
3064 PL_sig_name[sig], (gv ? GvENAME(gv)
3071 sv = PL_psig_name[sig]
3072 ? SvREFCNT_inc_NN(PL_psig_name[sig])
3073 : newSVpv(PL_sig_name[sig],0);
3077 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) {
3078 /* make sure our assumption about the size of the SAVEs are correct:
3079 * 3 for SAVEDESTRUCTOR_X, 2 for SAVEFREESV */
3080 assert(old_ss_ix + 2 + ((flags & 1) ? 3+5 : 0) == PL_savestack_ix);
3083 PUSHSTACKi(PERLSI_SIGNAL);
3086 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
3088 struct sigaction oact;
3090 if (sigaction(sig, 0, &oact) == 0 && oact.sa_flags & SA_SIGINFO) {
3093 SV *rv = newRV_noinc(MUTABLE_SV(sih));
3094 /* The siginfo fields signo, code, errno, pid, uid,
3095 * addr, status, and band are defined by POSIX/SUSv3. */
3096 (void)hv_stores(sih, "signo", newSViv(sip->si_signo));
3097 (void)hv_stores(sih, "code", newSViv(sip->si_code));
3098 #if 0 /* XXX TODO: Configure scan for the existence of these, but even that does not help if the SA_SIGINFO is not implemented according to the spec. */
3099 hv_stores(sih, "errno", newSViv(sip->si_errno));
3100 hv_stores(sih, "status", newSViv(sip->si_status));
3101 hv_stores(sih, "uid", newSViv(sip->si_uid));
3102 hv_stores(sih, "pid", newSViv(sip->si_pid));
3103 hv_stores(sih, "addr", newSVuv(PTR2UV(sip->si_addr)));
3104 hv_stores(sih, "band", newSViv(sip->si_band));
3108 mPUSHp((char *)sip, sizeof(*sip));
3116 call_sv(MUTABLE_SV(cv), G_DISCARD|G_EVAL);
3119 if (SvTRUE(ERRSV)) {
3121 /* Handler "died", for example to get out of a restart-able read().
3122 * Before we re-do that on its behalf re-enable the signal which was
3123 * blocked by the system when we entered.
3125 #ifdef HAS_SIGPROCMASK
3126 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
3132 sigaddset(&set,sig);
3133 sigprocmask(SIG_UNBLOCK, &set, NULL);
3136 /* Not clear if this will work */
3137 (void)rsignal(sig, SIG_IGN);
3138 (void)rsignal(sig, PL_csighandlerp);
3140 #endif /* !PERL_MICRO */
3144 /* pop any of SAVEFREESV, SAVEDESTRUCTOR_X and "save in progress" */
3145 PL_savestack_ix = old_ss_ix;
3148 PL_op = myop; /* Apparently not needed... */
3150 PL_Sv = tSv; /* Restore global temporaries. */
3157 S_restore_magic(pTHX_ const void *p)
3160 MGS* const mgs = SSPTR(PTR2IV(p), MGS*);
3161 SV* const sv = mgs->mgs_sv;
3167 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
3169 #ifdef PERL_OLD_COPY_ON_WRITE
3170 /* While magic was saved (and off) sv_setsv may well have seen
3171 this SV as a prime candidate for COW. */
3173 sv_force_normal_flags(sv, 0);
3176 if (mgs->mgs_readonly)
3178 if (mgs->mgs_magical)
3179 SvFLAGS(sv) |= mgs->mgs_magical;
3182 if (SvGMAGICAL(sv)) {
3183 /* downgrade public flags to private,
3184 and discard any other private flags */
3186 const U32 pubflags = SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK);
3188 SvFLAGS(sv) &= ~( pubflags | (SVp_IOK|SVp_NOK|SVp_POK) );
3189 SvFLAGS(sv) |= ( pubflags << PRIVSHIFT );
3194 bumped = mgs->mgs_bumped;
3195 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
3197 /* If we're still on top of the stack, pop us off. (That condition
3198 * will be satisfied if restore_magic was called explicitly, but *not*
3199 * if it's being called via leave_scope.)
3200 * The reason for doing this is that otherwise, things like sv_2cv()
3201 * may leave alloc gunk on the savestack, and some code
3202 * (e.g. sighandler) doesn't expect that...
3204 if (PL_savestack_ix == mgs->mgs_ss_ix)
3206 UV popval = SSPOPUV;
3207 assert(popval == SAVEt_DESTRUCTOR_X);
3208 PL_savestack_ix -= 2;
3210 assert((popval & SAVE_MASK) == SAVEt_ALLOC);
3211 PL_savestack_ix -= popval >> SAVE_TIGHT_SHIFT;
3214 if (SvREFCNT(sv) == 1) {
3215 /* We hold the last reference to this SV, which implies that the
3216 SV was deleted as a side effect of the routines we called.
3217 So artificially keep it alive a bit longer.
3218 We avoid turning on the TEMP flag, which can cause the SV's
3219 buffer to get stolen (and maybe other stuff). */
3220 int was_temp = SvTEMP(sv);
3228 SvREFCNT_dec(sv); /* undo the inc in S_save_magic() */
3232 /* clean up the mess created by Perl_sighandler().
3233 * Note that this is only called during an exit in a signal handler;
3234 * a die is trapped by the call_sv() and the SAVEDESTRUCTOR_X manually
3238 S_unwind_handler_stack(pTHX_ const void *p)
3243 PL_savestack_ix -= 5; /* Unprotect save in progress. */
3247 =for apidoc magic_sethint
3249 Triggered by a store to %^H, records the key/value pair to
3250 C<PL_compiling.cop_hints_hash>. It is assumed that hints aren't storing
3251 anything that would need a deep copy. Maybe we should warn if we find a
3257 Perl_magic_sethint(pTHX_ SV *sv, MAGIC *mg)
3260 SV *key = (mg->mg_len == HEf_SVKEY) ? MUTABLE_SV(mg->mg_ptr)
3261 : newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
3263 PERL_ARGS_ASSERT_MAGIC_SETHINT;
3265 /* mg->mg_obj isn't being used. If needed, it would be possible to store
3266 an alternative leaf in there, with PL_compiling.cop_hints being used if
3267 it's NULL. If needed for threads, the alternative could lock a mutex,
3268 or take other more complex action. */
3270 /* Something changed in %^H, so it will need to be restored on scope exit.
3271 Doing this here saves a lot of doing it manually in perl code (and
3272 forgetting to do it, and consequent subtle errors. */
3273 PL_hints |= HINT_LOCALIZE_HH;
3274 CopHINTHASH_set(&PL_compiling,
3275 cophh_store_sv(CopHINTHASH_get(&PL_compiling), key, 0, sv, 0));
3280 =for apidoc magic_clearhint
3282 Triggered by a delete from %^H, records the key to
3283 C<PL_compiling.cop_hints_hash>.
3288 Perl_magic_clearhint(pTHX_ SV *sv, MAGIC *mg)
3292 PERL_ARGS_ASSERT_MAGIC_CLEARHINT;
3293 PERL_UNUSED_ARG(sv);
3295 assert(mg->mg_len == HEf_SVKEY);
3297 PERL_UNUSED_ARG(sv);
3299 PL_hints |= HINT_LOCALIZE_HH;
3300 CopHINTHASH_set(&PL_compiling,
3301 cophh_delete_sv(CopHINTHASH_get(&PL_compiling),
3302 MUTABLE_SV(mg->mg_ptr), 0, 0));
3307 =for apidoc magic_clearhints
3309 Triggered by clearing %^H, resets C<PL_compiling.cop_hints_hash>.
3314 Perl_magic_clearhints(pTHX_ SV *sv, MAGIC *mg)
3316 PERL_ARGS_ASSERT_MAGIC_CLEARHINTS;
3317 PERL_UNUSED_ARG(sv);
3318 PERL_UNUSED_ARG(mg);
3319 cophh_free(CopHINTHASH_get(&PL_compiling));
3320 CopHINTHASH_set(&PL_compiling, cophh_new_empty());
3326 * c-indentation-style: bsd
3328 * indent-tabs-mode: t
3331 * ex: set ts=8 sts=4 sw=4 noet: