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 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
61 Signal_t Perl_csighandler(int sig, siginfo_t *, void *);
63 Signal_t Perl_csighandler(int sig);
67 /* Missing protos on LynxOS */
68 void setruid(uid_t id);
69 void seteuid(uid_t id);
70 void setrgid(uid_t id);
71 void setegid(uid_t id);
75 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
83 /* MGS is typedef'ed to struct magic_state in perl.h */
86 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
91 PERL_ARGS_ASSERT_SAVE_MAGIC;
93 assert(SvMAGICAL(sv));
94 /* Turning READONLY off for a copy-on-write scalar (including shared
95 hash keys) is a bad idea. */
97 sv_force_normal_flags(sv, 0);
99 SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix));
101 mgs = SSPTR(mgs_ix, MGS*);
103 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
104 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
108 if (!(SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK))) {
109 /* No public flags are set, so promote any private flags to public. */
110 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
115 =for apidoc mg_magical
117 Turns on the magical status of an SV. See C<sv_magic>.
123 Perl_mg_magical(pTHX_ SV *sv)
126 PERL_ARGS_ASSERT_MG_MAGICAL;
128 if ((mg = SvMAGIC(sv))) {
131 const MGVTBL* const vtbl = mg->mg_virtual;
133 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
140 } while ((mg = mg->mg_moremagic));
141 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)))
147 /* is this container magic (%ENV, $1 etc), or value magic (pos, taint etc)? */
150 S_is_container_magic(const MAGIC *mg)
153 switch (mg->mg_type) {
156 case PERL_MAGIC_regex_global:
157 case PERL_MAGIC_nkeys:
158 #ifdef USE_LOCALE_COLLATE
159 case PERL_MAGIC_collxfrm:
162 case PERL_MAGIC_taint:
164 case PERL_MAGIC_vstring:
165 case PERL_MAGIC_utf8:
166 case PERL_MAGIC_substr:
167 case PERL_MAGIC_defelem:
168 case PERL_MAGIC_arylen:
170 case PERL_MAGIC_backref:
171 case PERL_MAGIC_arylen_p:
172 case PERL_MAGIC_rhash:
173 case PERL_MAGIC_symtab:
183 Do magic after a value is retrieved from the SV. See C<sv_magic>.
189 Perl_mg_get(pTHX_ SV *sv)
192 const I32 mgs_ix = SSNEW(sizeof(MGS));
193 const bool was_temp = (bool)SvTEMP(sv);
195 MAGIC *newmg, *head, *cur, *mg;
196 /* guard against sv having being freed midway by holding a private
199 PERL_ARGS_ASSERT_MG_GET;
201 /* sv_2mortal has this side effect of turning on the TEMP flag, which can
202 cause the SV's buffer to get stolen (and maybe other stuff).
205 sv_2mortal(SvREFCNT_inc_simple_NN(sv));
210 save_magic(mgs_ix, sv);
212 /* We must call svt_get(sv, mg) for each valid entry in the linked
213 list of magic. svt_get() may delete the current entry, add new
214 magic to the head of the list, or upgrade the SV. AMS 20010810 */
216 newmg = cur = head = mg = SvMAGIC(sv);
218 const MGVTBL * const vtbl = mg->mg_virtual;
220 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
221 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
223 /* guard against magic having been deleted - eg FETCH calling
228 /* Don't restore the flags for this entry if it was deleted. */
229 if (mg->mg_flags & MGf_GSKIP)
230 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
233 mg = mg->mg_moremagic;
236 /* Have we finished with the new entries we saw? Start again
237 where we left off (unless there are more new entries). */
245 /* Were any new entries added? */
246 if (!have_new && (newmg = SvMAGIC(sv)) != head) {
253 restore_magic(INT2PTR(void *, (IV)mgs_ix));
255 if (SvREFCNT(sv) == 1) {
256 /* We hold the last reference to this SV, which implies that the
257 SV was deleted as a side effect of the routines we called. */
266 Do magic after a value is assigned to the SV. See C<sv_magic>.
272 Perl_mg_set(pTHX_ SV *sv)
275 const I32 mgs_ix = SSNEW(sizeof(MGS));
279 PERL_ARGS_ASSERT_MG_SET;
281 save_magic(mgs_ix, sv);
283 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
284 const MGVTBL* vtbl = mg->mg_virtual;
285 nextmg = mg->mg_moremagic; /* it may delete itself */
286 if (mg->mg_flags & MGf_GSKIP) {
287 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
288 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
290 if (PL_localizing == 2 && !S_is_container_magic(mg))
292 if (vtbl && vtbl->svt_set)
293 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
296 restore_magic(INT2PTR(void*, (IV)mgs_ix));
301 =for apidoc mg_length
303 Report on the SV's length. See C<sv_magic>.
309 Perl_mg_length(pTHX_ SV *sv)
315 PERL_ARGS_ASSERT_MG_LENGTH;
317 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
318 const MGVTBL * const vtbl = mg->mg_virtual;
319 if (vtbl && vtbl->svt_len) {
320 const I32 mgs_ix = SSNEW(sizeof(MGS));
321 save_magic(mgs_ix, sv);
322 /* omit MGf_GSKIP -- not changed here */
323 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
324 restore_magic(INT2PTR(void*, (IV)mgs_ix));
330 /* You can't know whether it's UTF-8 until you get the string again...
332 const U8 *s = (U8*)SvPV_const(sv, len);
335 len = utf8_length(s, s + len);
342 Perl_mg_size(pTHX_ SV *sv)
346 PERL_ARGS_ASSERT_MG_SIZE;
348 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
349 const MGVTBL* const vtbl = mg->mg_virtual;
350 if (vtbl && vtbl->svt_len) {
351 const I32 mgs_ix = SSNEW(sizeof(MGS));
353 save_magic(mgs_ix, sv);
354 /* omit MGf_GSKIP -- not changed here */
355 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
356 restore_magic(INT2PTR(void*, (IV)mgs_ix));
363 return AvFILLp((const AV *) sv); /* Fallback to non-tied array */
367 Perl_croak(aTHX_ "Size magic not implemented");
376 Clear something magical that the SV represents. See C<sv_magic>.
382 Perl_mg_clear(pTHX_ SV *sv)
384 const I32 mgs_ix = SSNEW(sizeof(MGS));
387 PERL_ARGS_ASSERT_MG_CLEAR;
389 save_magic(mgs_ix, sv);
391 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
392 const MGVTBL* const vtbl = mg->mg_virtual;
393 /* omit GSKIP -- never set here */
395 if (vtbl && vtbl->svt_clear)
396 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
399 restore_magic(INT2PTR(void*, (IV)mgs_ix));
406 Finds the magic pointer for type matching the SV. See C<sv_magic>.
412 Perl_mg_find(pTHX_ const SV *sv, int type)
417 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
418 if (mg->mg_type == type)
428 Copies the magic from one SV to another. See C<sv_magic>.
434 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
439 PERL_ARGS_ASSERT_MG_COPY;
441 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
442 const MGVTBL* const vtbl = mg->mg_virtual;
443 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
444 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
447 const char type = mg->mg_type;
448 if (isUPPER(type) && type != PERL_MAGIC_uvar) {
450 (type == PERL_MAGIC_tied)
452 : (type == PERL_MAGIC_regdata && mg->mg_obj)
455 toLOWER(type), key, klen);
464 =for apidoc mg_localize
466 Copy some of the magic from an existing SV to new localized version of that
467 SV. Container magic (eg %ENV, $1, tie) gets copied, value magic doesn't (eg
470 If setmagic is false then no set magic will be called on the new (empty) SV.
471 This typically means that assignment will soon follow (e.g. 'local $x = $y'),
472 and that will handle the magic.
478 Perl_mg_localize(pTHX_ SV *sv, SV *nsv, bool setmagic)
483 PERL_ARGS_ASSERT_MG_LOCALIZE;
485 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
486 const MGVTBL* const vtbl = mg->mg_virtual;
487 if (!S_is_container_magic(mg))
490 if ((mg->mg_flags & MGf_LOCAL) && vtbl->svt_local)
491 (void)CALL_FPTR(vtbl->svt_local)(aTHX_ nsv, mg);
493 sv_magicext(nsv, mg->mg_obj, mg->mg_type, vtbl,
494 mg->mg_ptr, mg->mg_len);
496 /* container types should remain read-only across localization */
497 SvFLAGS(nsv) |= SvREADONLY(sv);
500 if (SvTYPE(nsv) >= SVt_PVMG && SvMAGIC(nsv)) {
501 SvFLAGS(nsv) |= SvMAGICAL(sv);
513 Free any magic storage used by the SV. See C<sv_magic>.
519 Perl_mg_free(pTHX_ SV *sv)
524 PERL_ARGS_ASSERT_MG_FREE;
526 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
527 const MGVTBL* const vtbl = mg->mg_virtual;
528 moremagic = mg->mg_moremagic;
529 if (vtbl && vtbl->svt_free)
530 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
531 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
532 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
533 Safefree(mg->mg_ptr);
534 else if (mg->mg_len == HEf_SVKEY)
535 SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
537 if (mg->mg_flags & MGf_REFCOUNTED)
538 SvREFCNT_dec(mg->mg_obj);
540 SvMAGIC_set(sv, moremagic);
542 SvMAGIC_set(sv, NULL);
550 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
555 PERL_ARGS_ASSERT_MAGIC_REGDATA_CNT;
558 register const REGEXP * const rx = PM_GETRE(PL_curpm);
560 if (mg->mg_obj) { /* @+ */
561 /* return the number possible */
562 return RX_NPARENS(rx);
564 I32 paren = RX_LASTPAREN(rx);
566 /* return the last filled */
568 && (RX_OFFS(rx)[paren].start == -1
569 || RX_OFFS(rx)[paren].end == -1) )
580 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
584 PERL_ARGS_ASSERT_MAGIC_REGDATUM_GET;
587 register const REGEXP * const rx = PM_GETRE(PL_curpm);
589 register const I32 paren = mg->mg_len;
594 if (paren <= (I32)RX_NPARENS(rx) &&
595 (s = RX_OFFS(rx)[paren].start) != -1 &&
596 (t = RX_OFFS(rx)[paren].end) != -1)
599 if (mg->mg_obj) /* @+ */
604 if (i > 0 && RX_MATCH_UTF8(rx)) {
605 const char * const b = RX_SUBBEG(rx);
607 i = utf8_length((U8*)b, (U8*)(b+i));
618 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
620 PERL_ARGS_ASSERT_MAGIC_REGDATUM_SET;
623 Perl_croak(aTHX_ "%s", PL_no_modify);
624 NORETURN_FUNCTION_END;
628 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
633 register const REGEXP * rx;
634 const char * const remaining = mg->mg_ptr + 1;
636 PERL_ARGS_ASSERT_MAGIC_LEN;
638 switch (*mg->mg_ptr) {
640 if (*remaining == '\0') { /* ^P */
642 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
644 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
648 case '\015': /* $^MATCH */
649 if (strEQ(remaining, "ATCH")) {
656 paren = RX_BUFF_IDX_PREMATCH;
660 paren = RX_BUFF_IDX_POSTMATCH;
664 paren = RX_BUFF_IDX_FULLMATCH;
666 case '1': case '2': case '3': case '4':
667 case '5': case '6': case '7': case '8': case '9':
668 paren = atoi(mg->mg_ptr);
670 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
672 i = CALLREG_NUMBUF_LENGTH((REGEXP * const)rx, sv, paren);
675 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
678 if (ckWARN(WARN_UNINITIALIZED))
683 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
684 paren = RX_LASTPAREN(rx);
689 case '\016': /* ^N */
690 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
691 paren = RX_LASTCLOSEPAREN(rx);
698 if (!SvPOK(sv) && SvNIOK(sv)) {
706 #define SvRTRIM(sv) STMT_START { \
708 STRLEN len = SvCUR(sv); \
709 char * const p = SvPVX(sv); \
710 while (len > 0 && isSPACE(p[len-1])) \
712 SvCUR_set(sv, len); \
718 Perl_emulate_cop_io(pTHX_ const COP *const c, SV *const sv)
720 PERL_ARGS_ASSERT_EMULATE_COP_IO;
722 if (!(CopHINTS_get(c) & (HINT_LEXICAL_IO_IN|HINT_LEXICAL_IO_OUT)))
723 sv_setsv(sv, &PL_sv_undef);
727 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_IN)) {
728 SV *const value = Perl_refcounted_he_fetch(aTHX_
730 0, "open<", 5, 0, 0);
735 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_OUT)) {
736 SV *const value = Perl_refcounted_he_fetch(aTHX_
738 0, "open>", 5, 0, 0);
746 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
750 register char *s = NULL;
752 const char * const remaining = mg->mg_ptr + 1;
753 const char nextchar = *remaining;
755 PERL_ARGS_ASSERT_MAGIC_GET;
757 switch (*mg->mg_ptr) {
758 case '\001': /* ^A */
759 sv_setsv(sv, PL_bodytarget);
761 case '\003': /* ^C, ^CHILD_ERROR_NATIVE */
762 if (nextchar == '\0') {
763 sv_setiv(sv, (IV)PL_minus_c);
765 else if (strEQ(remaining, "HILD_ERROR_NATIVE")) {
766 sv_setiv(sv, (IV)STATUS_NATIVE);
770 case '\004': /* ^D */
771 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
773 case '\005': /* ^E */
774 if (nextchar == '\0') {
777 # include <descrip.h>
778 # include <starlet.h>
780 $DESCRIPTOR(msgdsc,msg);
781 sv_setnv(sv,(NV) vaxc$errno);
782 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
783 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
788 if (!(_emx_env & 0x200)) { /* Under DOS */
789 sv_setnv(sv, (NV)errno);
790 sv_setpv(sv, errno ? Strerror(errno) : "");
792 if (errno != errno_isOS2) {
793 const int tmp = _syserrno();
794 if (tmp) /* 2nd call to _syserrno() makes it 0 */
797 sv_setnv(sv, (NV)Perl_rc);
798 sv_setpv(sv, os2error(Perl_rc));
802 const DWORD dwErr = GetLastError();
803 sv_setnv(sv, (NV)dwErr);
805 PerlProc_GetOSError(sv, dwErr);
814 sv_setnv(sv, (NV)errno);
815 sv_setpv(sv, errno ? Strerror(errno) : "");
820 SvNOK_on(sv); /* what a wonderful hack! */
822 else if (strEQ(remaining, "NCODING"))
823 sv_setsv(sv, PL_encoding);
825 case '\006': /* ^F */
826 sv_setiv(sv, (IV)PL_maxsysfd);
828 case '\010': /* ^H */
829 sv_setiv(sv, (IV)PL_hints);
831 case '\011': /* ^I */ /* NOT \t in EBCDIC */
832 sv_setpv(sv, PL_inplace); /* Will undefine sv if PL_inplace is NULL */
834 case '\017': /* ^O & ^OPEN */
835 if (nextchar == '\0') {
836 sv_setpv(sv, PL_osname);
839 else if (strEQ(remaining, "PEN")) {
840 Perl_emulate_cop_io(aTHX_ &PL_compiling, sv);
844 if (nextchar == '\0') { /* ^P */
845 sv_setiv(sv, (IV)PL_perldb);
846 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
847 goto do_prematch_fetch;
848 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
849 goto do_postmatch_fetch;
852 case '\023': /* ^S */
853 if (nextchar == '\0') {
854 if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING)
857 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
862 case '\024': /* ^T */
863 if (nextchar == '\0') {
865 sv_setnv(sv, PL_basetime);
867 sv_setiv(sv, (IV)PL_basetime);
870 else if (strEQ(remaining, "AINT"))
871 sv_setiv(sv, PL_tainting
872 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
875 case '\025': /* $^UNICODE, $^UTF8LOCALE, $^UTF8CACHE */
876 if (strEQ(remaining, "NICODE"))
877 sv_setuv(sv, (UV) PL_unicode);
878 else if (strEQ(remaining, "TF8LOCALE"))
879 sv_setuv(sv, (UV) PL_utf8locale);
880 else if (strEQ(remaining, "TF8CACHE"))
881 sv_setiv(sv, (IV) PL_utf8cache);
883 case '\027': /* ^W & $^WARNING_BITS */
884 if (nextchar == '\0')
885 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
886 else if (strEQ(remaining, "ARNING_BITS")) {
887 if (PL_compiling.cop_warnings == pWARN_NONE) {
888 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
890 else if (PL_compiling.cop_warnings == pWARN_STD) {
893 (PL_dowarn & G_WARN_ON) ? WARN_ALLstring : WARN_NONEstring,
897 else if (PL_compiling.cop_warnings == pWARN_ALL) {
898 /* Get the bit mask for $warnings::Bits{all}, because
899 * it could have been extended by warnings::register */
900 HV * const bits=get_hv("warnings::Bits", 0);
902 SV ** const bits_all = hv_fetchs(bits, "all", FALSE);
904 sv_setsv(sv, *bits_all);
907 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
911 sv_setpvn(sv, (char *) (PL_compiling.cop_warnings + 1),
912 *PL_compiling.cop_warnings);
917 case '\015': /* $^MATCH */
918 if (strEQ(remaining, "ATCH")) {
919 case '1': case '2': case '3': case '4':
920 case '5': case '6': case '7': case '8': case '9': case '&':
921 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
923 * Pre-threads, this was paren = atoi(GvENAME((const GV *)mg->mg_obj));
924 * XXX Does the new way break anything?
926 paren = atoi(mg->mg_ptr); /* $& is in [0] */
927 CALLREG_NUMBUF_FETCH(rx,paren,sv);
930 sv_setsv(sv,&PL_sv_undef);
934 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
935 if (RX_LASTPAREN(rx)) {
936 CALLREG_NUMBUF_FETCH(rx,RX_LASTPAREN(rx),sv);
940 sv_setsv(sv,&PL_sv_undef);
942 case '\016': /* ^N */
943 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
944 if (RX_LASTCLOSEPAREN(rx)) {
945 CALLREG_NUMBUF_FETCH(rx,RX_LASTCLOSEPAREN(rx),sv);
950 sv_setsv(sv,&PL_sv_undef);
954 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
955 CALLREG_NUMBUF_FETCH(rx,-2,sv);
958 sv_setsv(sv,&PL_sv_undef);
962 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
963 CALLREG_NUMBUF_FETCH(rx,-1,sv);
966 sv_setsv(sv,&PL_sv_undef);
969 if (GvIO(PL_last_in_gv)) {
970 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
975 sv_setiv(sv, (IV)STATUS_CURRENT);
976 #ifdef COMPLEX_STATUS
977 LvTARGOFF(sv) = PL_statusvalue;
978 LvTARGLEN(sv) = PL_statusvalue_vms;
983 if (GvIOp(PL_defoutgv))
984 s = IoTOP_NAME(GvIOp(PL_defoutgv));
988 sv_setpv(sv,GvENAME(PL_defoutgv));
989 sv_catpvs(sv,"_TOP");
993 if (GvIOp(PL_defoutgv))
994 s = IoFMT_NAME(GvIOp(PL_defoutgv));
996 s = GvENAME(PL_defoutgv);
1000 if (GvIOp(PL_defoutgv))
1001 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
1004 if (GvIOp(PL_defoutgv))
1005 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
1008 if (GvIOp(PL_defoutgv))
1009 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
1016 sv_setiv(sv, (IV)CopARYBASE_get(PL_curcop));
1019 if (GvIOp(PL_defoutgv))
1020 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
1024 sv_copypv(sv, PL_ors_sv);
1028 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
1029 sv_setpv(sv, errno ? Strerror(errno) : "");
1033 sv_setnv(sv, (NV)errno);
1035 if (errno == errno_isOS2 || errno == errno_isOS2_set)
1036 sv_setpv(sv, os2error(Perl_rc));
1039 sv_setpv(sv, errno ? Strerror(errno) : "");
1044 SvNOK_on(sv); /* what a wonderful hack! */
1047 sv_setiv(sv, (IV)PL_uid);
1050 sv_setiv(sv, (IV)PL_euid);
1053 sv_setiv(sv, (IV)PL_gid);
1056 sv_setiv(sv, (IV)PL_egid);
1058 #ifdef HAS_GETGROUPS
1060 Groups_t *gary = NULL;
1061 I32 i, num_groups = getgroups(0, gary);
1062 Newx(gary, num_groups, Groups_t);
1063 num_groups = getgroups(num_groups, gary);
1064 for (i = 0; i < num_groups; i++)
1065 Perl_sv_catpvf(aTHX_ sv, " %"IVdf, (IV)gary[i]);
1068 (void)SvIOK_on(sv); /* what a wonderful hack! */
1078 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
1080 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
1082 PERL_ARGS_ASSERT_MAGIC_GETUVAR;
1084 if (uf && uf->uf_val)
1085 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
1090 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
1093 STRLEN len = 0, klen;
1094 const char *s = SvOK(sv) ? SvPV_const(sv,len) : "";
1095 const char * const ptr = MgPV_const(mg,klen);
1098 PERL_ARGS_ASSERT_MAGIC_SETENV;
1100 #ifdef DYNAMIC_ENV_FETCH
1101 /* We just undefd an environment var. Is a replacement */
1102 /* waiting in the wings? */
1104 SV ** const valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE);
1106 s = SvOK(*valp) ? SvPV_const(*valp, len) : "";
1110 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
1111 /* And you'll never guess what the dog had */
1112 /* in its mouth... */
1114 MgTAINTEDDIR_off(mg);
1116 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1117 char pathbuf[256], eltbuf[256], *cp, *elt;
1121 my_strlcpy(eltbuf, s, sizeof(eltbuf));
1123 do { /* DCL$PATH may be a search list */
1124 while (1) { /* as may dev portion of any element */
1125 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
1126 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
1127 cando_by_name(S_IWUSR,0,elt) ) {
1128 MgTAINTEDDIR_on(mg);
1132 if ((cp = strchr(elt, ':')) != NULL)
1134 if (my_trnlnm(elt, eltbuf, j++))
1140 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
1143 if (s && klen == 4 && strEQ(ptr,"PATH")) {
1144 const char * const strend = s + len;
1146 while (s < strend) {
1150 #ifdef VMS /* Hmm. How do we get $Config{path_sep} from C? */
1151 const char path_sep = '|';
1153 const char path_sep = ':';
1155 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
1156 s, strend, path_sep, &i);
1158 if (i >= (I32)sizeof tmpbuf /* too long -- assume the worst */
1160 || !strchr(tmpbuf, ':') /* no colon thus no device name -- assume relative path */
1162 || *tmpbuf != '/' /* no starting slash -- assume relative path */
1164 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
1165 MgTAINTEDDIR_on(mg);
1171 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1177 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
1179 PERL_ARGS_ASSERT_MAGIC_CLEARENV;
1180 PERL_UNUSED_ARG(sv);
1181 my_setenv(MgPV_nolen_const(mg),NULL);
1186 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1189 PERL_ARGS_ASSERT_MAGIC_SET_ALL_ENV;
1190 PERL_UNUSED_ARG(mg);
1192 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1194 if (PL_localizing) {
1197 hv_iterinit(MUTABLE_HV(sv));
1198 while ((entry = hv_iternext(MUTABLE_HV(sv)))) {
1200 my_setenv(hv_iterkey(entry, &keylen),
1201 SvPV_nolen_const(hv_iterval(MUTABLE_HV(sv), entry)));
1209 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1212 PERL_ARGS_ASSERT_MAGIC_CLEAR_ALL_ENV;
1213 PERL_UNUSED_ARG(sv);
1214 PERL_UNUSED_ARG(mg);
1216 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1224 #ifdef HAS_SIGPROCMASK
1226 restore_sigmask(pTHX_ SV *save_sv)
1228 const sigset_t * const ossetp = (const sigset_t *) SvPV_nolen_const( save_sv );
1229 (void)sigprocmask(SIG_SETMASK, ossetp, NULL);
1233 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1236 /* Are we fetching a signal entry? */
1237 const I32 i = whichsig(MgPV_nolen_const(mg));
1239 PERL_ARGS_ASSERT_MAGIC_GETSIG;
1243 sv_setsv(sv,PL_psig_ptr[i]);
1245 Sighandler_t sigstate = rsignal_state(i);
1246 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1247 if (PL_sig_handlers_initted && PL_sig_ignoring[i])
1250 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1251 if (PL_sig_handlers_initted && PL_sig_defaulting[i])
1254 /* cache state so we don't fetch it again */
1255 if(sigstate == (Sighandler_t) SIG_IGN)
1256 sv_setpvs(sv,"IGNORE");
1258 sv_setsv(sv,&PL_sv_undef);
1259 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1266 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1268 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1269 * refactoring might be in order.
1272 register const char * const s = MgPV_nolen_const(mg);
1273 PERL_ARGS_ASSERT_MAGIC_CLEARSIG;
1274 PERL_UNUSED_ARG(sv);
1277 if (strEQ(s,"__DIE__"))
1279 else if (strEQ(s,"__WARN__") && PL_warnhook != PERL_WARNHOOK_FATAL)
1282 SV *const to_dec = *svp;
1284 SvREFCNT_dec(to_dec);
1288 /* Are we clearing a signal entry? */
1289 const I32 i = whichsig(s);
1291 #ifdef HAS_SIGPROCMASK
1294 /* Avoid having the signal arrive at a bad time, if possible. */
1297 sigprocmask(SIG_BLOCK, &set, &save);
1299 save_sv = newSVpvn((char *)(&save), sizeof(sigset_t));
1300 SAVEFREESV(save_sv);
1301 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1304 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1305 if (!PL_sig_handlers_initted) Perl_csighandler_init();
1307 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1308 PL_sig_defaulting[i] = 1;
1309 (void)rsignal(i, PL_csighandlerp);
1311 (void)rsignal(i, (Sighandler_t) SIG_DFL);
1313 if(PL_psig_name[i]) {
1314 SvREFCNT_dec(PL_psig_name[i]);
1317 if(PL_psig_ptr[i]) {
1318 SV * const to_dec=PL_psig_ptr[i];
1321 SvREFCNT_dec(to_dec);
1331 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1332 Perl_csighandler(int sig, siginfo_t *sip PERL_UNUSED_DECL, void *uap PERL_UNUSED_DECL)
1334 Perl_csighandler(int sig)
1337 #ifdef PERL_GET_SIG_CONTEXT
1338 dTHXa(PERL_GET_SIG_CONTEXT);
1342 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1343 (void) rsignal(sig, PL_csighandlerp);
1344 if (PL_sig_ignoring[sig]) return;
1346 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1347 if (PL_sig_defaulting[sig])
1348 #ifdef KILL_BY_SIGPRC
1349 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1364 (PL_signals & PERL_SIGNALS_UNSAFE_FLAG))
1365 /* Call the perl level handler now--
1366 * with risk we may be in malloc() etc. */
1367 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1368 (*PL_sighandlerp)(sig, NULL, NULL);
1370 (*PL_sighandlerp)(sig);
1373 /* Set a flag to say this signal is pending, that is awaiting delivery after
1374 * the current Perl opcode completes */
1375 PL_psig_pend[sig]++;
1377 #ifndef SIG_PENDING_DIE_COUNT
1378 # define SIG_PENDING_DIE_COUNT 120
1380 /* And one to say _a_ signal is pending */
1381 if (++PL_sig_pending >= SIG_PENDING_DIE_COUNT)
1382 Perl_croak(aTHX_ "Maximal count of pending signals (%lu) exceeded",
1383 (unsigned long)SIG_PENDING_DIE_COUNT);
1387 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1389 Perl_csighandler_init(void)
1392 if (PL_sig_handlers_initted) return;
1394 for (sig = 1; sig < SIG_SIZE; sig++) {
1395 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1397 PL_sig_defaulting[sig] = 1;
1398 (void) rsignal(sig, PL_csighandlerp);
1400 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1401 PL_sig_ignoring[sig] = 0;
1404 PL_sig_handlers_initted = 1;
1409 Perl_despatch_signals(pTHX)
1414 for (sig = 1; sig < SIG_SIZE; sig++) {
1415 if (PL_psig_pend[sig]) {
1416 PERL_BLOCKSIG_ADD(set, sig);
1417 PL_psig_pend[sig] = 0;
1418 PERL_BLOCKSIG_BLOCK(set);
1419 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1420 (*PL_sighandlerp)(sig, NULL, NULL);
1422 (*PL_sighandlerp)(sig);
1424 PERL_BLOCKSIG_UNBLOCK(set);
1430 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1435 /* Need to be careful with SvREFCNT_dec(), because that can have side
1436 * effects (due to closures). We must make sure that the new disposition
1437 * is in place before it is called.
1441 #ifdef HAS_SIGPROCMASK
1445 register const char *s = MgPV_const(mg,len);
1447 PERL_ARGS_ASSERT_MAGIC_SETSIG;
1450 if (strEQ(s,"__DIE__"))
1452 else if (strEQ(s,"__WARN__"))
1455 Perl_croak(aTHX_ "No such hook: %s", s);
1458 if (*svp != PERL_WARNHOOK_FATAL)
1464 i = whichsig(s); /* ...no, a brick */
1466 if (ckWARN(WARN_SIGNAL))
1467 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1470 #ifdef HAS_SIGPROCMASK
1471 /* Avoid having the signal arrive at a bad time, if possible. */
1474 sigprocmask(SIG_BLOCK, &set, &save);
1476 save_sv = newSVpvn((char *)(&save), sizeof(sigset_t));
1477 SAVEFREESV(save_sv);
1478 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1481 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1482 if (!PL_sig_handlers_initted) Perl_csighandler_init();
1484 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1485 PL_sig_ignoring[i] = 0;
1487 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1488 PL_sig_defaulting[i] = 0;
1490 SvREFCNT_dec(PL_psig_name[i]);
1491 to_dec = PL_psig_ptr[i];
1492 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1493 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1494 PL_psig_name[i] = newSVpvn(s, len);
1495 SvREADONLY_on(PL_psig_name[i]);
1497 if (isGV_with_GP(sv) || SvROK(sv)) {
1499 (void)rsignal(i, PL_csighandlerp);
1500 #ifdef HAS_SIGPROCMASK
1505 *svp = SvREFCNT_inc_simple_NN(sv);
1507 SvREFCNT_dec(to_dec);
1510 s = SvOK(sv) ? SvPV_force(sv,len) : "DEFAULT";
1511 if (strEQ(s,"IGNORE")) {
1513 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1514 PL_sig_ignoring[i] = 1;
1515 (void)rsignal(i, PL_csighandlerp);
1517 (void)rsignal(i, (Sighandler_t) SIG_IGN);
1521 else if (strEQ(s,"DEFAULT") || !*s) {
1523 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1525 PL_sig_defaulting[i] = 1;
1526 (void)rsignal(i, PL_csighandlerp);
1529 (void)rsignal(i, (Sighandler_t) SIG_DFL);
1534 * We should warn if HINT_STRICT_REFS, but without
1535 * access to a known hint bit in a known OP, we can't
1536 * tell whether HINT_STRICT_REFS is in force or not.
1538 if (!strchr(s,':') && !strchr(s,'\''))
1539 Perl_sv_insert_flags(aTHX_ sv, 0, 0, STR_WITH_LEN("main::"),
1542 (void)rsignal(i, PL_csighandlerp);
1544 *svp = SvREFCNT_inc_simple_NN(sv);
1546 #ifdef HAS_SIGPROCMASK
1551 SvREFCNT_dec(to_dec);
1554 #endif /* !PERL_MICRO */
1557 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1562 PERL_ARGS_ASSERT_MAGIC_SETISA;
1563 PERL_UNUSED_ARG(sv);
1565 /* Bail out if destruction is going on */
1566 if(PL_dirty) return 0;
1568 /* Skip _isaelem because _isa will handle it shortly */
1569 if (PL_delaymagic & DM_ARRAY && mg->mg_type == PERL_MAGIC_isaelem)
1572 /* XXX Once it's possible, we need to
1573 detect that our @ISA is aliased in
1574 other stashes, and act on the stashes
1575 of all of the aliases */
1577 /* The first case occurs via setisa,
1578 the second via setisa_elem, which
1579 calls this same magic */
1581 SvTYPE(mg->mg_obj) == SVt_PVGV
1582 ? (const GV *)mg->mg_obj
1583 : (const GV *)mg_find(mg->mg_obj, PERL_MAGIC_isa)->mg_obj
1587 mro_isa_changed_in(stash);
1593 Perl_magic_clearisa(pTHX_ SV *sv, MAGIC *mg)
1598 PERL_ARGS_ASSERT_MAGIC_CLEARISA;
1600 /* Bail out if destruction is going on */
1601 if(PL_dirty) return 0;
1603 av_clear(MUTABLE_AV(sv));
1605 /* XXX see comments in magic_setisa */
1607 SvTYPE(mg->mg_obj) == SVt_PVGV
1608 ? (const GV *)mg->mg_obj
1609 : (const GV *)mg_find(mg->mg_obj, PERL_MAGIC_isa)->mg_obj
1613 mro_isa_changed_in(stash);
1619 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1622 PERL_ARGS_ASSERT_MAGIC_SETAMAGIC;
1623 PERL_UNUSED_ARG(sv);
1624 PERL_UNUSED_ARG(mg);
1625 PL_amagic_generation++;
1631 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1633 HV * const hv = MUTABLE_HV(LvTARG(sv));
1636 PERL_ARGS_ASSERT_MAGIC_GETNKEYS;
1637 PERL_UNUSED_ARG(mg);
1640 (void) hv_iterinit(hv);
1641 if (! SvTIED_mg((const SV *)hv, PERL_MAGIC_tied))
1644 while (hv_iternext(hv))
1649 sv_setiv(sv, (IV)i);
1654 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1656 PERL_ARGS_ASSERT_MAGIC_SETNKEYS;
1657 PERL_UNUSED_ARG(mg);
1659 hv_ksplit(MUTABLE_HV(LvTARG(sv)), SvIV(sv));
1664 /* caller is responsible for stack switching/cleanup */
1666 S_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, const char *meth, I32 flags, int n, SV *val)
1671 PERL_ARGS_ASSERT_MAGIC_METHCALL;
1675 PUSHs(SvTIED_obj(sv, mg));
1678 if (mg->mg_len >= 0)
1679 mPUSHp(mg->mg_ptr, mg->mg_len);
1680 else if (mg->mg_len == HEf_SVKEY)
1681 PUSHs(MUTABLE_SV(mg->mg_ptr));
1683 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1692 return call_method(meth, flags);
1696 S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth)
1700 PERL_ARGS_ASSERT_MAGIC_METHPACK;
1704 PUSHSTACKi(PERLSI_MAGIC);
1706 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1707 sv_setsv(sv, *PL_stack_sp--);
1717 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1719 PERL_ARGS_ASSERT_MAGIC_GETPACK;
1722 mg->mg_flags |= MGf_GSKIP;
1723 magic_methpack(sv,mg,"FETCH");
1728 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1732 PERL_ARGS_ASSERT_MAGIC_SETPACK;
1735 PUSHSTACKi(PERLSI_MAGIC);
1736 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1743 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1745 PERL_ARGS_ASSERT_MAGIC_CLEARPACK;
1747 return magic_methpack(sv,mg,"DELETE");
1752 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1757 PERL_ARGS_ASSERT_MAGIC_SIZEPACK;
1761 PUSHSTACKi(PERLSI_MAGIC);
1762 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1763 sv = *PL_stack_sp--;
1764 retval = SvIV(sv)-1;
1766 Perl_croak(aTHX_ "FETCHSIZE returned a negative value");
1771 return (U32) retval;
1775 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1779 PERL_ARGS_ASSERT_MAGIC_WIPEPACK;
1782 PUSHSTACKi(PERLSI_MAGIC);
1784 XPUSHs(SvTIED_obj(sv, mg));
1786 call_method("CLEAR", G_SCALAR|G_DISCARD);
1794 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1797 const char * const meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1799 PERL_ARGS_ASSERT_MAGIC_NEXTPACK;
1803 PUSHSTACKi(PERLSI_MAGIC);
1806 PUSHs(SvTIED_obj(sv, mg));
1811 if (call_method(meth, G_SCALAR))
1812 sv_setsv(key, *PL_stack_sp--);
1821 Perl_magic_existspack(pTHX_ SV *sv, const MAGIC *mg)
1823 PERL_ARGS_ASSERT_MAGIC_EXISTSPACK;
1825 return magic_methpack(sv,mg,"EXISTS");
1829 Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1833 SV * const tied = SvTIED_obj(MUTABLE_SV(hv), mg);
1834 HV * const pkg = SvSTASH((const SV *)SvRV(tied));
1836 PERL_ARGS_ASSERT_MAGIC_SCALARPACK;
1838 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1840 if (HvEITER_get(hv))
1841 /* we are in an iteration so the hash cannot be empty */
1843 /* no xhv_eiter so now use FIRSTKEY */
1844 key = sv_newmortal();
1845 magic_nextpack(MUTABLE_SV(hv), mg, key);
1846 HvEITER_set(hv, NULL); /* need to reset iterator */
1847 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1850 /* there is a SCALAR method that we can call */
1852 PUSHSTACKi(PERLSI_MAGIC);
1858 if (call_method("SCALAR", G_SCALAR))
1859 retval = *PL_stack_sp--;
1861 retval = &PL_sv_undef;
1868 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1871 GV * const gv = PL_DBline;
1872 const I32 i = SvTRUE(sv);
1873 SV ** const svp = av_fetch(GvAV(gv),
1874 atoi(MgPV_nolen_const(mg)), FALSE);
1876 PERL_ARGS_ASSERT_MAGIC_SETDBLINE;
1878 if (svp && SvIOKp(*svp)) {
1879 OP * const o = INT2PTR(OP*,SvIVX(*svp));
1881 /* set or clear breakpoint in the relevant control op */
1883 o->op_flags |= OPf_SPECIAL;
1885 o->op_flags &= ~OPf_SPECIAL;
1892 Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg)
1895 AV * const obj = MUTABLE_AV(mg->mg_obj);
1897 PERL_ARGS_ASSERT_MAGIC_GETARYLEN;
1900 sv_setiv(sv, AvFILL(obj) + CopARYBASE_get(PL_curcop));
1908 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1911 AV * const obj = MUTABLE_AV(mg->mg_obj);
1913 PERL_ARGS_ASSERT_MAGIC_SETARYLEN;
1916 av_fill(obj, SvIV(sv) - CopARYBASE_get(PL_curcop));
1918 if (ckWARN(WARN_MISC))
1919 Perl_warner(aTHX_ packWARN(WARN_MISC),
1920 "Attempt to set length of freed array");
1926 Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
1930 PERL_ARGS_ASSERT_MAGIC_FREEARYLEN_P;
1931 PERL_UNUSED_ARG(sv);
1933 /* during global destruction, mg_obj may already have been freed */
1934 if (PL_in_clean_all)
1937 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
1940 /* arylen scalar holds a pointer back to the array, but doesn't own a
1941 reference. Hence the we (the array) are about to go away with it
1942 still pointing at us. Clear its pointer, else it would be pointing
1943 at free memory. See the comment in sv_magic about reference loops,
1944 and why it can't own a reference to us. */
1951 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1954 SV* const lsv = LvTARG(sv);
1956 PERL_ARGS_ASSERT_MAGIC_GETPOS;
1957 PERL_UNUSED_ARG(mg);
1959 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1960 MAGIC * const found = mg_find(lsv, PERL_MAGIC_regex_global);
1961 if (found && found->mg_len >= 0) {
1962 I32 i = found->mg_len;
1964 sv_pos_b2u(lsv, &i);
1965 sv_setiv(sv, i + CopARYBASE_get(PL_curcop));
1974 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1977 SV* const lsv = LvTARG(sv);
1983 PERL_ARGS_ASSERT_MAGIC_SETPOS;
1984 PERL_UNUSED_ARG(mg);
1986 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1987 found = mg_find(lsv, PERL_MAGIC_regex_global);
1993 #ifdef PERL_OLD_COPY_ON_WRITE
1995 sv_force_normal_flags(lsv, 0);
1997 found = sv_magicext(lsv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
2000 else if (!SvOK(sv)) {
2004 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
2006 pos = SvIV(sv) - CopARYBASE_get(PL_curcop);
2009 ulen = sv_len_utf8(lsv);
2019 else if (pos > (SSize_t)len)
2024 sv_pos_u2b(lsv, &p, 0);
2028 found->mg_len = pos;
2029 found->mg_flags &= ~MGf_MINMATCH;
2035 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
2038 SV * const lsv = LvTARG(sv);
2039 const char * const tmps = SvPV_const(lsv,len);
2040 I32 offs = LvTARGOFF(sv);
2041 I32 rem = LvTARGLEN(sv);
2043 PERL_ARGS_ASSERT_MAGIC_GETSUBSTR;
2044 PERL_UNUSED_ARG(mg);
2047 sv_pos_u2b(lsv, &offs, &rem);
2048 if (offs > (I32)len)
2050 if (rem + offs > (I32)len)
2052 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
2059 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
2063 const char * const tmps = SvPV_const(sv, len);
2064 SV * const lsv = LvTARG(sv);
2065 I32 lvoff = LvTARGOFF(sv);
2066 I32 lvlen = LvTARGLEN(sv);
2068 PERL_ARGS_ASSERT_MAGIC_SETSUBSTR;
2069 PERL_UNUSED_ARG(mg);
2072 sv_utf8_upgrade(lsv);
2073 sv_pos_u2b(lsv, &lvoff, &lvlen);
2074 sv_insert(lsv, lvoff, lvlen, tmps, len);
2075 LvTARGLEN(sv) = sv_len_utf8(sv);
2078 else if (lsv && SvUTF8(lsv)) {
2080 sv_pos_u2b(lsv, &lvoff, &lvlen);
2081 LvTARGLEN(sv) = len;
2082 utf8 = (char*)bytes_to_utf8((U8*)tmps, &len);
2083 sv_insert(lsv, lvoff, lvlen, utf8, len);
2087 sv_insert(lsv, lvoff, lvlen, tmps, len);
2088 LvTARGLEN(sv) = len;
2096 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
2100 PERL_ARGS_ASSERT_MAGIC_GETTAINT;
2101 PERL_UNUSED_ARG(sv);
2103 TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1));
2108 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
2112 PERL_ARGS_ASSERT_MAGIC_SETTAINT;
2113 PERL_UNUSED_ARG(sv);
2115 /* update taint status */
2124 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
2126 SV * const lsv = LvTARG(sv);
2128 PERL_ARGS_ASSERT_MAGIC_GETVEC;
2129 PERL_UNUSED_ARG(mg);
2132 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
2140 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
2142 PERL_ARGS_ASSERT_MAGIC_SETVEC;
2143 PERL_UNUSED_ARG(mg);
2144 do_vecset(sv); /* XXX slurp this routine */
2149 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
2154 PERL_ARGS_ASSERT_MAGIC_GETDEFELEM;
2156 if (LvTARGLEN(sv)) {
2158 SV * const ahv = LvTARG(sv);
2159 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, FALSE, 0);
2164 AV *const av = MUTABLE_AV(LvTARG(sv));
2165 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
2166 targ = AvARRAY(av)[LvTARGOFF(sv)];
2168 if (targ && (targ != &PL_sv_undef)) {
2169 /* somebody else defined it for us */
2170 SvREFCNT_dec(LvTARG(sv));
2171 LvTARG(sv) = SvREFCNT_inc_simple_NN(targ);
2173 SvREFCNT_dec(mg->mg_obj);
2175 mg->mg_flags &= ~MGf_REFCOUNTED;
2180 sv_setsv(sv, targ ? targ : &PL_sv_undef);
2185 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
2187 PERL_ARGS_ASSERT_MAGIC_SETDEFELEM;
2188 PERL_UNUSED_ARG(mg);
2192 sv_setsv(LvTARG(sv), sv);
2193 SvSETMAGIC(LvTARG(sv));
2199 Perl_vivify_defelem(pTHX_ SV *sv)
2205 PERL_ARGS_ASSERT_VIVIFY_DEFELEM;
2207 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
2210 SV * const ahv = LvTARG(sv);
2211 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, TRUE, 0);
2214 if (!value || value == &PL_sv_undef)
2215 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(mg->mg_obj));
2218 AV *const av = MUTABLE_AV(LvTARG(sv));
2219 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
2220 LvTARG(sv) = NULL; /* array can't be extended */
2222 SV* const * const svp = av_fetch(av, LvTARGOFF(sv), TRUE);
2223 if (!svp || (value = *svp) == &PL_sv_undef)
2224 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
2227 SvREFCNT_inc_simple_void(value);
2228 SvREFCNT_dec(LvTARG(sv));
2231 SvREFCNT_dec(mg->mg_obj);
2233 mg->mg_flags &= ~MGf_REFCOUNTED;
2237 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
2239 PERL_ARGS_ASSERT_MAGIC_KILLBACKREFS;
2240 return Perl_sv_kill_backrefs(aTHX_ sv, MUTABLE_AV(mg->mg_obj));
2244 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
2246 PERL_ARGS_ASSERT_MAGIC_SETMGLOB;
2247 PERL_UNUSED_CONTEXT;
2254 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
2256 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
2258 PERL_ARGS_ASSERT_MAGIC_SETUVAR;
2260 if (uf && uf->uf_set)
2261 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
2266 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2268 const char type = mg->mg_type;
2270 PERL_ARGS_ASSERT_MAGIC_SETREGEXP;
2272 if (type == PERL_MAGIC_qr) {
2273 } else if (type == PERL_MAGIC_bm) {
2277 assert(type == PERL_MAGIC_fm);
2280 return sv_unmagic(sv, type);
2283 #ifdef USE_LOCALE_COLLATE
2285 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
2287 PERL_ARGS_ASSERT_MAGIC_SETCOLLXFRM;
2290 * RenE<eacute> Descartes said "I think not."
2291 * and vanished with a faint plop.
2293 PERL_UNUSED_CONTEXT;
2294 PERL_UNUSED_ARG(sv);
2296 Safefree(mg->mg_ptr);
2302 #endif /* USE_LOCALE_COLLATE */
2304 /* Just clear the UTF-8 cache data. */
2306 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2308 PERL_ARGS_ASSERT_MAGIC_SETUTF8;
2309 PERL_UNUSED_CONTEXT;
2310 PERL_UNUSED_ARG(sv);
2311 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2313 mg->mg_len = -1; /* The mg_len holds the len cache. */
2318 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
2321 register const char *s;
2323 register const REGEXP * rx;
2324 const char * const remaining = mg->mg_ptr + 1;
2328 PERL_ARGS_ASSERT_MAGIC_SET;
2330 switch (*mg->mg_ptr) {
2331 case '\015': /* $^MATCH */
2332 if (strEQ(remaining, "ATCH"))
2334 case '`': /* ${^PREMATCH} caught below */
2336 paren = RX_BUFF_IDX_PREMATCH;
2338 case '\'': /* ${^POSTMATCH} caught below */
2340 paren = RX_BUFF_IDX_POSTMATCH;
2344 paren = RX_BUFF_IDX_FULLMATCH;
2346 case '1': case '2': case '3': case '4':
2347 case '5': case '6': case '7': case '8': case '9':
2348 paren = atoi(mg->mg_ptr);
2350 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
2351 CALLREG_NUMBUF_STORE((REGEXP * const)rx,paren,sv);
2354 /* Croak with a READONLY error when a numbered match var is
2355 * set without a previous pattern match. Unless it's C<local $1>
2357 if (!PL_localizing) {
2358 Perl_croak(aTHX_ "%s", PL_no_modify);
2361 case '\001': /* ^A */
2362 sv_setsv(PL_bodytarget, sv);
2364 case '\003': /* ^C */
2365 PL_minus_c = (bool)SvIV(sv);
2368 case '\004': /* ^D */
2370 s = SvPV_nolen_const(sv);
2371 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
2372 DEBUG_x(dump_all());
2374 PL_debug = (SvIV(sv)) | DEBUG_TOP_FLAG;
2377 case '\005': /* ^E */
2378 if (*(mg->mg_ptr+1) == '\0') {
2380 set_vaxc_errno(SvIV(sv));
2383 SetLastError( SvIV(sv) );
2386 os2_setsyserrno(SvIV(sv));
2388 /* will anyone ever use this? */
2389 SETERRNO(SvIV(sv), 4);
2394 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2396 SvREFCNT_dec(PL_encoding);
2397 if (SvOK(sv) || SvGMAGICAL(sv)) {
2398 PL_encoding = newSVsv(sv);
2405 case '\006': /* ^F */
2406 PL_maxsysfd = SvIV(sv);
2408 case '\010': /* ^H */
2409 PL_hints = SvIV(sv);
2411 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2412 Safefree(PL_inplace);
2413 PL_inplace = SvOK(sv) ? savesvpv(sv) : NULL;
2415 case '\017': /* ^O */
2416 if (*(mg->mg_ptr+1) == '\0') {
2417 Safefree(PL_osname);
2420 TAINT_PROPER("assigning to $^O");
2421 PL_osname = savesvpv(sv);
2424 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2426 const char *const start = SvPV(sv, len);
2427 const char *out = (const char*)memchr(start, '\0', len);
2429 struct refcounted_he *tmp_he;
2432 PL_compiling.cop_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
2434 |= HINT_LOCALIZE_HH | HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
2436 /* Opening for input is more common than opening for output, so
2437 ensure that hints for input are sooner on linked list. */
2438 tmp = out ? newSVpvn_flags(out + 1, start + len - out - 1,
2439 SVs_TEMP | SvUTF8(sv))
2440 : newSVpvs_flags("", SVs_TEMP | SvUTF8(sv));
2443 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash,
2444 newSVpvs_flags("open>", SVs_TEMP),
2447 /* The UTF-8 setting is carried over */
2448 sv_setpvn(tmp, start, out ? (STRLEN)(out - start) : len);
2450 PL_compiling.cop_hints_hash
2451 = Perl_refcounted_he_new(aTHX_ tmp_he,
2452 newSVpvs_flags("open<", SVs_TEMP),
2456 case '\020': /* ^P */
2457 if (*remaining == '\0') { /* ^P */
2458 PL_perldb = SvIV(sv);
2459 if (PL_perldb && !PL_DBsingle)
2462 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
2464 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
2467 case '\024': /* ^T */
2469 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2471 PL_basetime = (Time_t)SvIV(sv);
2474 case '\025': /* ^UTF8CACHE */
2475 if (strEQ(mg->mg_ptr+1, "TF8CACHE")) {
2476 PL_utf8cache = (signed char) sv_2iv(sv);
2479 case '\027': /* ^W & $^WARNING_BITS */
2480 if (*(mg->mg_ptr+1) == '\0') {
2481 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2483 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2484 | (i ? G_WARN_ON : G_WARN_OFF) ;
2487 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2488 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2489 if (!SvPOK(sv) && PL_localizing) {
2490 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2491 PL_compiling.cop_warnings = pWARN_NONE;
2496 int accumulate = 0 ;
2497 int any_fatals = 0 ;
2498 const char * const ptr = SvPV_const(sv, len) ;
2499 for (i = 0 ; i < len ; ++i) {
2500 accumulate |= ptr[i] ;
2501 any_fatals |= (ptr[i] & 0xAA) ;
2504 if (!specialWARN(PL_compiling.cop_warnings))
2505 PerlMemShared_free(PL_compiling.cop_warnings);
2506 PL_compiling.cop_warnings = pWARN_NONE;
2508 /* Yuck. I can't see how to abstract this: */
2509 else if (isWARN_on(((STRLEN *)SvPV_nolen_const(sv)) - 1,
2510 WARN_ALL) && !any_fatals) {
2511 if (!specialWARN(PL_compiling.cop_warnings))
2512 PerlMemShared_free(PL_compiling.cop_warnings);
2513 PL_compiling.cop_warnings = pWARN_ALL;
2514 PL_dowarn |= G_WARN_ONCE ;
2518 const char *const p = SvPV_const(sv, len);
2520 PL_compiling.cop_warnings
2521 = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
2524 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2525 PL_dowarn |= G_WARN_ONCE ;
2533 if (PL_localizing) {
2534 if (PL_localizing == 1)
2535 SAVESPTR(PL_last_in_gv);
2537 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2538 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2541 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2542 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2543 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2546 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2547 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2548 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2551 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv));
2554 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv));
2555 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2556 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2559 IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv));
2563 IO * const io = GvIOp(PL_defoutgv);
2566 if ((SvIV(sv)) == 0)
2567 IoFLAGS(io) &= ~IOf_FLUSH;
2569 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2570 PerlIO *ofp = IoOFP(io);
2572 (void)PerlIO_flush(ofp);
2573 IoFLAGS(io) |= IOf_FLUSH;
2579 SvREFCNT_dec(PL_rs);
2580 PL_rs = newSVsv(sv);
2584 SvREFCNT_dec(PL_ors_sv);
2585 if (SvOK(sv) || SvGMAGICAL(sv)) {
2586 PL_ors_sv = newSVsv(sv);
2593 CopARYBASE_set(&PL_compiling, SvIV(sv));
2596 #ifdef COMPLEX_STATUS
2597 if (PL_localizing == 2) {
2598 PL_statusvalue = LvTARGOFF(sv);
2599 PL_statusvalue_vms = LvTARGLEN(sv);
2603 #ifdef VMSISH_STATUS
2605 STATUS_NATIVE_CHILD_SET((U32)SvIV(sv));
2608 STATUS_UNIX_EXIT_SET(SvIV(sv));
2613 # define PERL_VMS_BANG vaxc$errno
2615 # define PERL_VMS_BANG 0
2617 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2618 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2623 if (PL_delaymagic) {
2624 PL_delaymagic |= DM_RUID;
2625 break; /* don't do magic till later */
2628 (void)setruid((Uid_t)PL_uid);
2631 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2633 #ifdef HAS_SETRESUID
2634 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2636 if (PL_uid == PL_euid) { /* special case $< = $> */
2638 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2639 if (PL_uid != 0 && PerlProc_getuid() == 0)
2640 (void)PerlProc_setuid(0);
2642 (void)PerlProc_setuid(PL_uid);
2644 PL_uid = PerlProc_getuid();
2645 Perl_croak(aTHX_ "setruid() not implemented");
2650 PL_uid = PerlProc_getuid();
2651 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2655 if (PL_delaymagic) {
2656 PL_delaymagic |= DM_EUID;
2657 break; /* don't do magic till later */
2660 (void)seteuid((Uid_t)PL_euid);
2663 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2665 #ifdef HAS_SETRESUID
2666 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2668 if (PL_euid == PL_uid) /* special case $> = $< */
2669 PerlProc_setuid(PL_euid);
2671 PL_euid = PerlProc_geteuid();
2672 Perl_croak(aTHX_ "seteuid() not implemented");
2677 PL_euid = PerlProc_geteuid();
2678 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2682 if (PL_delaymagic) {
2683 PL_delaymagic |= DM_RGID;
2684 break; /* don't do magic till later */
2687 (void)setrgid((Gid_t)PL_gid);
2690 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2692 #ifdef HAS_SETRESGID
2693 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2695 if (PL_gid == PL_egid) /* special case $( = $) */
2696 (void)PerlProc_setgid(PL_gid);
2698 PL_gid = PerlProc_getgid();
2699 Perl_croak(aTHX_ "setrgid() not implemented");
2704 PL_gid = PerlProc_getgid();
2705 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2708 #ifdef HAS_SETGROUPS
2710 const char *p = SvPV_const(sv, len);
2711 Groups_t *gary = NULL;
2716 for (i = 0; i < NGROUPS; ++i) {
2717 while (*p && !isSPACE(*p))
2724 Newx(gary, i + 1, Groups_t);
2726 Renew(gary, i + 1, Groups_t);
2730 (void)setgroups(i, gary);
2733 #else /* HAS_SETGROUPS */
2735 #endif /* HAS_SETGROUPS */
2736 if (PL_delaymagic) {
2737 PL_delaymagic |= DM_EGID;
2738 break; /* don't do magic till later */
2741 (void)setegid((Gid_t)PL_egid);
2744 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2746 #ifdef HAS_SETRESGID
2747 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2749 if (PL_egid == PL_gid) /* special case $) = $( */
2750 (void)PerlProc_setgid(PL_egid);
2752 PL_egid = PerlProc_getegid();
2753 Perl_croak(aTHX_ "setegid() not implemented");
2758 PL_egid = PerlProc_getegid();
2759 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2762 PL_chopset = SvPV_force(sv,len);
2765 LOCK_DOLLARZERO_MUTEX;
2766 #ifdef HAS_SETPROCTITLE
2767 /* The BSDs don't show the argv[] in ps(1) output, they
2768 * show a string from the process struct and provide
2769 * the setproctitle() routine to manipulate that. */
2770 if (PL_origalen != 1) {
2771 s = SvPV_const(sv, len);
2772 # if __FreeBSD_version > 410001
2773 /* The leading "-" removes the "perl: " prefix,
2774 * but not the "(perl) suffix from the ps(1)
2775 * output, because that's what ps(1) shows if the
2776 * argv[] is modified. */
2777 setproctitle("-%s", s);
2778 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2779 /* This doesn't really work if you assume that
2780 * $0 = 'foobar'; will wipe out 'perl' from the $0
2781 * because in ps(1) output the result will be like
2782 * sprintf("perl: %s (perl)", s)
2783 * I guess this is a security feature:
2784 * one (a user process) cannot get rid of the original name.
2786 setproctitle("%s", s);
2789 #elif defined(__hpux) && defined(PSTAT_SETCMD)
2790 if (PL_origalen != 1) {
2792 s = SvPV_const(sv, len);
2793 un.pst_command = (char *)s;
2794 pstat(PSTAT_SETCMD, un, len, 0, 0);
2797 if (PL_origalen > 1) {
2798 /* PL_origalen is set in perl_parse(). */
2799 s = SvPV_force(sv,len);
2800 if (len >= (STRLEN)PL_origalen-1) {
2801 /* Longer than original, will be truncated. We assume that
2802 * PL_origalen bytes are available. */
2803 Copy(s, PL_origargv[0], PL_origalen-1, char);
2806 /* Shorter than original, will be padded. */
2808 /* Special case for Mac OS X: see [perl #38868] */
2811 /* Is the space counterintuitive? Yes.
2812 * (You were expecting \0?)
2813 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2815 const int pad = ' ';
2817 Copy(s, PL_origargv[0], len, char);
2818 PL_origargv[0][len] = 0;
2819 memset(PL_origargv[0] + len + 1,
2820 pad, PL_origalen - len - 1);
2822 PL_origargv[0][PL_origalen-1] = 0;
2823 for (i = 1; i < PL_origargc; i++)
2827 UNLOCK_DOLLARZERO_MUTEX;
2834 Perl_whichsig(pTHX_ const char *sig)
2836 register char* const* sigv;
2838 PERL_ARGS_ASSERT_WHICHSIG;
2839 PERL_UNUSED_CONTEXT;
2841 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
2842 if (strEQ(sig,*sigv))
2843 return PL_sig_num[sigv - (char* const*)PL_sig_name];
2845 if (strEQ(sig,"CHLD"))
2849 if (strEQ(sig,"CLD"))
2856 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2857 Perl_sighandler(int sig, siginfo_t *sip, void *uap PERL_UNUSED_DECL)
2859 Perl_sighandler(int sig)
2862 #ifdef PERL_GET_SIG_CONTEXT
2863 dTHXa(PERL_GET_SIG_CONTEXT);
2870 SV * const tSv = PL_Sv;
2874 XPV * const tXpv = PL_Xpv;
2876 if (PL_savestack_ix + 15 <= PL_savestack_max)
2878 if (PL_markstack_ptr < PL_markstack_max - 2)
2880 if (PL_scopestack_ix < PL_scopestack_max - 3)
2883 if (!PL_psig_ptr[sig]) {
2884 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2889 /* Max number of items pushed there is 3*n or 4. We cannot fix
2890 infinity, so we fix 4 (in fact 5): */
2892 PL_savestack_ix += 5; /* Protect save in progress. */
2893 SAVEDESTRUCTOR_X(S_unwind_handler_stack, (void*)&flags);
2896 PL_markstack_ptr++; /* Protect mark. */
2898 PL_scopestack_ix += 1;
2899 /* sv_2cv is too complicated, try a simpler variant first: */
2900 if (!SvROK(PL_psig_ptr[sig]) || !(cv = MUTABLE_CV(SvRV(PL_psig_ptr[sig])))
2901 || SvTYPE(cv) != SVt_PVCV) {
2903 cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD);
2906 if (!cv || !CvROOT(cv)) {
2907 if (ckWARN(WARN_SIGNAL))
2908 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2909 PL_sig_name[sig], (gv ? GvENAME(gv)
2916 if(PL_psig_name[sig]) {
2917 sv = SvREFCNT_inc_NN(PL_psig_name[sig]);
2919 #if !defined(PERL_IMPLICIT_CONTEXT)
2923 sv = sv_newmortal();
2924 sv_setpv(sv,PL_sig_name[sig]);
2927 PUSHSTACKi(PERLSI_SIGNAL);
2930 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2932 struct sigaction oact;
2934 if (sigaction(sig, 0, &oact) == 0 && oact.sa_flags & SA_SIGINFO) {
2937 SV *rv = newRV_noinc(MUTABLE_SV(sih));
2938 /* The siginfo fields signo, code, errno, pid, uid,
2939 * addr, status, and band are defined by POSIX/SUSv3. */
2940 (void)hv_stores(sih, "signo", newSViv(sip->si_signo));
2941 (void)hv_stores(sih, "code", newSViv(sip->si_code));
2942 #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. */
2943 hv_stores(sih, "errno", newSViv(sip->si_errno));
2944 hv_stores(sih, "status", newSViv(sip->si_status));
2945 hv_stores(sih, "uid", newSViv(sip->si_uid));
2946 hv_stores(sih, "pid", newSViv(sip->si_pid));
2947 hv_stores(sih, "addr", newSVuv(PTR2UV(sip->si_addr)));
2948 hv_stores(sih, "band", newSViv(sip->si_band));
2952 mPUSHp((char *)sip, sizeof(*sip));
2960 call_sv(MUTABLE_SV(cv), G_DISCARD|G_EVAL);
2963 if (SvTRUE(ERRSV)) {
2965 #ifdef HAS_SIGPROCMASK
2966 /* Handler "died", for example to get out of a restart-able read().
2967 * Before we re-do that on its behalf re-enable the signal which was
2968 * blocked by the system when we entered.
2972 sigaddset(&set,sig);
2973 sigprocmask(SIG_UNBLOCK, &set, NULL);
2975 /* Not clear if this will work */
2976 (void)rsignal(sig, SIG_IGN);
2977 (void)rsignal(sig, PL_csighandlerp);
2979 #endif /* !PERL_MICRO */
2980 Perl_die(aTHX_ NULL);
2984 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2988 PL_scopestack_ix -= 1;
2991 PL_op = myop; /* Apparently not needed... */
2993 PL_Sv = tSv; /* Restore global temporaries. */
3000 S_restore_magic(pTHX_ const void *p)
3003 MGS* const mgs = SSPTR(PTR2IV(p), MGS*);
3004 SV* const sv = mgs->mgs_sv;
3009 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
3011 #ifdef PERL_OLD_COPY_ON_WRITE
3012 /* While magic was saved (and off) sv_setsv may well have seen
3013 this SV as a prime candidate for COW. */
3015 sv_force_normal_flags(sv, 0);
3019 SvFLAGS(sv) |= mgs->mgs_flags;
3022 if (SvGMAGICAL(sv)) {
3023 /* downgrade public flags to private,
3024 and discard any other private flags */
3026 const U32 pubflags = SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK);
3028 SvFLAGS(sv) &= ~( pubflags | (SVp_IOK|SVp_NOK|SVp_POK) );
3029 SvFLAGS(sv) |= ( pubflags << PRIVSHIFT );
3034 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
3036 /* If we're still on top of the stack, pop us off. (That condition
3037 * will be satisfied if restore_magic was called explicitly, but *not*
3038 * if it's being called via leave_scope.)
3039 * The reason for doing this is that otherwise, things like sv_2cv()
3040 * may leave alloc gunk on the savestack, and some code
3041 * (e.g. sighandler) doesn't expect that...
3043 if (PL_savestack_ix == mgs->mgs_ss_ix)
3045 I32 popval = SSPOPINT;
3046 assert(popval == SAVEt_DESTRUCTOR_X);
3047 PL_savestack_ix -= 2;
3049 assert(popval == SAVEt_ALLOC);
3051 PL_savestack_ix -= popval;
3057 S_unwind_handler_stack(pTHX_ const void *p)
3060 const U32 flags = *(const U32*)p;
3062 PERL_ARGS_ASSERT_UNWIND_HANDLER_STACK;
3065 PL_savestack_ix -= 5; /* Unprotect save in progress. */
3066 #if !defined(PERL_IMPLICIT_CONTEXT)
3068 SvREFCNT_dec(PL_sig_sv);
3073 =for apidoc magic_sethint
3075 Triggered by a store to %^H, records the key/value pair to
3076 C<PL_compiling.cop_hints_hash>. It is assumed that hints aren't storing
3077 anything that would need a deep copy. Maybe we should warn if we find a
3083 Perl_magic_sethint(pTHX_ SV *sv, MAGIC *mg)
3086 SV *key = (mg->mg_len == HEf_SVKEY) ? MUTABLE_SV(mg->mg_ptr)
3087 : newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
3089 PERL_ARGS_ASSERT_MAGIC_SETHINT;
3091 /* mg->mg_obj isn't being used. If needed, it would be possible to store
3092 an alternative leaf in there, with PL_compiling.cop_hints being used if
3093 it's NULL. If needed for threads, the alternative could lock a mutex,
3094 or take other more complex action. */
3096 /* Something changed in %^H, so it will need to be restored on scope exit.
3097 Doing this here saves a lot of doing it manually in perl code (and
3098 forgetting to do it, and consequent subtle errors. */
3099 PL_hints |= HINT_LOCALIZE_HH;
3100 PL_compiling.cop_hints_hash
3101 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash, key, sv);
3106 =for apidoc magic_clearhint
3108 Triggered by a delete from %^H, records the key to
3109 C<PL_compiling.cop_hints_hash>.
3114 Perl_magic_clearhint(pTHX_ SV *sv, MAGIC *mg)
3118 PERL_ARGS_ASSERT_MAGIC_CLEARHINT;
3119 PERL_UNUSED_ARG(sv);
3121 assert(mg->mg_len == HEf_SVKEY);
3123 PERL_UNUSED_ARG(sv);
3125 PL_hints |= HINT_LOCALIZE_HH;
3126 PL_compiling.cop_hints_hash
3127 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash,
3128 MUTABLE_SV(mg->mg_ptr), &PL_sv_placeholder);
3134 * c-indentation-style: bsd
3136 * indent-tabs-mode: t
3139 * ex: set ts=8 sts=4 sw=4 noet: