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.
84 /* MGS is typedef'ed to struct magic_state in perl.h */
87 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
92 PERL_ARGS_ASSERT_SAVE_MAGIC;
94 assert(SvMAGICAL(sv));
95 /* Turning READONLY off for a copy-on-write scalar (including shared
96 hash keys) is a bad idea. */
98 sv_force_normal_flags(sv, 0);
100 SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix));
102 mgs = SSPTR(mgs_ix, MGS*);
104 mgs->mgs_magical = SvMAGICAL(sv);
105 mgs->mgs_readonly = SvREADONLY(sv) != 0;
106 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
110 if (!(SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK))) {
111 /* No public flags are set, so promote any private flags to public. */
112 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
117 =for apidoc mg_magical
119 Turns on the magical status of an SV. See C<sv_magic>.
125 Perl_mg_magical(pTHX_ SV *sv)
128 PERL_ARGS_ASSERT_MG_MAGICAL;
132 if ((mg = SvMAGIC(sv))) {
134 const MGVTBL* const vtbl = mg->mg_virtual;
136 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
143 } while ((mg = mg->mg_moremagic));
144 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)))
150 /* is this container magic (%ENV, $1 etc), or value magic (pos, taint etc)? */
153 S_is_container_magic(const MAGIC *mg)
156 switch (mg->mg_type) {
159 case PERL_MAGIC_regex_global:
160 case PERL_MAGIC_nkeys:
161 #ifdef USE_LOCALE_COLLATE
162 case PERL_MAGIC_collxfrm:
165 case PERL_MAGIC_taint:
167 case PERL_MAGIC_vstring:
168 case PERL_MAGIC_utf8:
169 case PERL_MAGIC_substr:
170 case PERL_MAGIC_defelem:
171 case PERL_MAGIC_arylen:
173 case PERL_MAGIC_backref:
174 case PERL_MAGIC_arylen_p:
175 case PERL_MAGIC_rhash:
176 case PERL_MAGIC_symtab:
186 Do magic after a value is retrieved from the SV. See C<sv_magic>.
192 Perl_mg_get(pTHX_ SV *sv)
195 const I32 mgs_ix = SSNEW(sizeof(MGS));
196 const bool was_temp = (bool)SvTEMP(sv);
198 MAGIC *newmg, *head, *cur, *mg;
199 /* guard against sv having being freed midway by holding a private
202 PERL_ARGS_ASSERT_MG_GET;
204 /* sv_2mortal has this side effect of turning on the TEMP flag, which can
205 cause the SV's buffer to get stolen (and maybe other stuff).
208 sv_2mortal(SvREFCNT_inc_simple_NN(sv));
213 save_magic(mgs_ix, sv);
215 /* We must call svt_get(sv, mg) for each valid entry in the linked
216 list of magic. svt_get() may delete the current entry, add new
217 magic to the head of the list, or upgrade the SV. AMS 20010810 */
219 newmg = cur = head = mg = SvMAGIC(sv);
221 const MGVTBL * const vtbl = mg->mg_virtual;
222 MAGIC * const nextmg = mg->mg_moremagic; /* it may delete itself */
224 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
225 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
227 /* guard against magic having been deleted - eg FETCH calling
230 (SSPTR(mgs_ix, MGS *))->mgs_magical = 0; /* recalculate flags */
234 /* recalculate flags if this entry was deleted. */
235 if (mg->mg_flags & MGf_GSKIP)
236 (SSPTR(mgs_ix, MGS *))->mgs_magical = 0;
242 /* Have we finished with the new entries we saw? Start again
243 where we left off (unless there are more new entries). */
251 /* Were any new entries added? */
252 if (!have_new && (newmg = SvMAGIC(sv)) != head) {
256 (SSPTR(mgs_ix, MGS *))->mgs_magical = 0; /* recalculate flags */
260 restore_magic(INT2PTR(void *, (IV)mgs_ix));
262 if (SvREFCNT(sv) == 1) {
263 /* We hold the last reference to this SV, which implies that the
264 SV was deleted as a side effect of the routines we called. */
273 Do magic after a value is assigned to the SV. See C<sv_magic>.
279 Perl_mg_set(pTHX_ SV *sv)
282 const I32 mgs_ix = SSNEW(sizeof(MGS));
286 PERL_ARGS_ASSERT_MG_SET;
288 save_magic(mgs_ix, sv);
290 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
291 const MGVTBL* vtbl = mg->mg_virtual;
292 nextmg = mg->mg_moremagic; /* it may delete itself */
293 if (mg->mg_flags & MGf_GSKIP) {
294 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
295 (SSPTR(mgs_ix, MGS*))->mgs_magical = 0;
297 if (PL_localizing == 2 && !S_is_container_magic(mg))
299 if (vtbl && vtbl->svt_set)
300 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
303 restore_magic(INT2PTR(void*, (IV)mgs_ix));
308 =for apidoc mg_length
310 Report on the SV's length. See C<sv_magic>.
316 Perl_mg_length(pTHX_ SV *sv)
322 PERL_ARGS_ASSERT_MG_LENGTH;
324 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
325 const MGVTBL * const vtbl = mg->mg_virtual;
326 if (vtbl && vtbl->svt_len) {
327 const I32 mgs_ix = SSNEW(sizeof(MGS));
328 save_magic(mgs_ix, sv);
329 /* omit MGf_GSKIP -- not changed here */
330 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
331 restore_magic(INT2PTR(void*, (IV)mgs_ix));
337 /* You can't know whether it's UTF-8 until you get the string again...
339 const U8 *s = (U8*)SvPV_const(sv, len);
342 len = utf8_length(s, s + len);
349 Perl_mg_size(pTHX_ SV *sv)
353 PERL_ARGS_ASSERT_MG_SIZE;
355 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
356 const MGVTBL* const vtbl = mg->mg_virtual;
357 if (vtbl && vtbl->svt_len) {
358 const I32 mgs_ix = SSNEW(sizeof(MGS));
360 save_magic(mgs_ix, sv);
361 /* omit MGf_GSKIP -- not changed here */
362 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
363 restore_magic(INT2PTR(void*, (IV)mgs_ix));
370 return AvFILLp((const AV *) sv); /* Fallback to non-tied array */
374 Perl_croak(aTHX_ "Size magic not implemented");
383 Clear something magical that the SV represents. See C<sv_magic>.
389 Perl_mg_clear(pTHX_ SV *sv)
391 const I32 mgs_ix = SSNEW(sizeof(MGS));
395 PERL_ARGS_ASSERT_MG_CLEAR;
397 save_magic(mgs_ix, sv);
399 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
400 const MGVTBL* const vtbl = mg->mg_virtual;
401 /* omit GSKIP -- never set here */
403 nextmg = mg->mg_moremagic; /* it may delete itself */
405 if (vtbl && vtbl->svt_clear)
406 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
409 restore_magic(INT2PTR(void*, (IV)mgs_ix));
416 Finds the magic pointer for type matching the SV. See C<sv_magic>.
422 Perl_mg_find(pTHX_ const SV *sv, int type)
427 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
428 if (mg->mg_type == type)
438 Copies the magic from one SV to another. See C<sv_magic>.
444 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
449 PERL_ARGS_ASSERT_MG_COPY;
451 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
452 const MGVTBL* const vtbl = mg->mg_virtual;
453 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
454 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
457 const char type = mg->mg_type;
458 if (isUPPER(type) && type != PERL_MAGIC_uvar) {
460 (type == PERL_MAGIC_tied)
462 : (type == PERL_MAGIC_regdata && mg->mg_obj)
465 toLOWER(type), key, klen);
474 =for apidoc mg_localize
476 Copy some of the magic from an existing SV to new localized version of that
477 SV. Container magic (eg %ENV, $1, tie) gets copied, value magic doesn't (eg
480 If setmagic is false then no set magic will be called on the new (empty) SV.
481 This typically means that assignment will soon follow (e.g. 'local $x = $y'),
482 and that will handle the magic.
488 Perl_mg_localize(pTHX_ SV *sv, SV *nsv, bool setmagic)
493 PERL_ARGS_ASSERT_MG_LOCALIZE;
495 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
496 const MGVTBL* const vtbl = mg->mg_virtual;
497 if (!S_is_container_magic(mg))
500 if ((mg->mg_flags & MGf_LOCAL) && vtbl->svt_local)
501 (void)CALL_FPTR(vtbl->svt_local)(aTHX_ nsv, mg);
503 sv_magicext(nsv, mg->mg_obj, mg->mg_type, vtbl,
504 mg->mg_ptr, mg->mg_len);
506 /* container types should remain read-only across localization */
507 SvFLAGS(nsv) |= SvREADONLY(sv);
510 if (SvTYPE(nsv) >= SVt_PVMG && SvMAGIC(nsv)) {
511 SvFLAGS(nsv) |= SvMAGICAL(sv);
523 Free any magic storage used by the SV. See C<sv_magic>.
529 Perl_mg_free(pTHX_ SV *sv)
534 PERL_ARGS_ASSERT_MG_FREE;
536 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
537 const MGVTBL* const vtbl = mg->mg_virtual;
538 moremagic = mg->mg_moremagic;
539 if (vtbl && vtbl->svt_free)
540 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
541 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
542 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
543 Safefree(mg->mg_ptr);
544 else if (mg->mg_len == HEf_SVKEY)
545 SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
547 if (mg->mg_flags & MGf_REFCOUNTED)
548 SvREFCNT_dec(mg->mg_obj);
550 SvMAGIC_set(sv, moremagic);
552 SvMAGIC_set(sv, NULL);
560 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
565 PERL_ARGS_ASSERT_MAGIC_REGDATA_CNT;
568 register const REGEXP * const rx = PM_GETRE(PL_curpm);
570 if (mg->mg_obj) { /* @+ */
571 /* return the number possible */
572 return RX_NPARENS(rx);
574 I32 paren = RX_LASTPAREN(rx);
576 /* return the last filled */
578 && (RX_OFFS(rx)[paren].start == -1
579 || RX_OFFS(rx)[paren].end == -1) )
590 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
594 PERL_ARGS_ASSERT_MAGIC_REGDATUM_GET;
597 register const REGEXP * const rx = PM_GETRE(PL_curpm);
599 register const I32 paren = mg->mg_len;
604 if (paren <= (I32)RX_NPARENS(rx) &&
605 (s = RX_OFFS(rx)[paren].start) != -1 &&
606 (t = RX_OFFS(rx)[paren].end) != -1)
609 if (mg->mg_obj) /* @+ */
614 if (i > 0 && RX_MATCH_UTF8(rx)) {
615 const char * const b = RX_SUBBEG(rx);
617 i = utf8_length((U8*)b, (U8*)(b+i));
628 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
630 PERL_ARGS_ASSERT_MAGIC_REGDATUM_SET;
633 Perl_croak(aTHX_ "%s", PL_no_modify);
634 NORETURN_FUNCTION_END;
638 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
643 register const REGEXP * rx;
644 const char * const remaining = mg->mg_ptr + 1;
646 PERL_ARGS_ASSERT_MAGIC_LEN;
648 switch (*mg->mg_ptr) {
650 if (*remaining == '\0') { /* ^P */
652 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
654 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
658 case '\015': /* $^MATCH */
659 if (strEQ(remaining, "ATCH")) {
666 paren = RX_BUFF_IDX_PREMATCH;
670 paren = RX_BUFF_IDX_POSTMATCH;
674 paren = RX_BUFF_IDX_FULLMATCH;
676 case '1': case '2': case '3': case '4':
677 case '5': case '6': case '7': case '8': case '9':
678 paren = atoi(mg->mg_ptr);
680 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
682 i = CALLREG_NUMBUF_LENGTH((REGEXP * const)rx, sv, paren);
685 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
688 if (ckWARN(WARN_UNINITIALIZED))
693 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
694 paren = RX_LASTPAREN(rx);
699 case '\016': /* ^N */
700 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
701 paren = RX_LASTCLOSEPAREN(rx);
708 if (!SvPOK(sv) && SvNIOK(sv)) {
716 #define SvRTRIM(sv) STMT_START { \
718 STRLEN len = SvCUR(sv); \
719 char * const p = SvPVX(sv); \
720 while (len > 0 && isSPACE(p[len-1])) \
722 SvCUR_set(sv, len); \
728 Perl_emulate_cop_io(pTHX_ const COP *const c, SV *const sv)
730 PERL_ARGS_ASSERT_EMULATE_COP_IO;
732 if (!(CopHINTS_get(c) & (HINT_LEXICAL_IO_IN|HINT_LEXICAL_IO_OUT)))
733 sv_setsv(sv, &PL_sv_undef);
737 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_IN)) {
738 SV *const value = Perl_refcounted_he_fetch(aTHX_
740 0, "open<", 5, 0, 0);
745 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_OUT)) {
746 SV *const value = Perl_refcounted_he_fetch(aTHX_
748 0, "open>", 5, 0, 0);
756 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
760 register char *s = NULL;
762 const char * const remaining = mg->mg_ptr + 1;
763 const char nextchar = *remaining;
765 PERL_ARGS_ASSERT_MAGIC_GET;
767 switch (*mg->mg_ptr) {
768 case '\001': /* ^A */
769 sv_setsv(sv, PL_bodytarget);
771 case '\003': /* ^C, ^CHILD_ERROR_NATIVE */
772 if (nextchar == '\0') {
773 sv_setiv(sv, (IV)PL_minus_c);
775 else if (strEQ(remaining, "HILD_ERROR_NATIVE")) {
776 sv_setiv(sv, (IV)STATUS_NATIVE);
780 case '\004': /* ^D */
781 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
783 case '\005': /* ^E */
784 if (nextchar == '\0') {
787 # include <descrip.h>
788 # include <starlet.h>
790 $DESCRIPTOR(msgdsc,msg);
791 sv_setnv(sv,(NV) vaxc$errno);
792 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
793 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
798 if (!(_emx_env & 0x200)) { /* Under DOS */
799 sv_setnv(sv, (NV)errno);
800 sv_setpv(sv, errno ? Strerror(errno) : "");
802 if (errno != errno_isOS2) {
803 const int tmp = _syserrno();
804 if (tmp) /* 2nd call to _syserrno() makes it 0 */
807 sv_setnv(sv, (NV)Perl_rc);
808 sv_setpv(sv, os2error(Perl_rc));
812 const DWORD dwErr = GetLastError();
813 sv_setnv(sv, (NV)dwErr);
815 PerlProc_GetOSError(sv, dwErr);
824 sv_setnv(sv, (NV)errno);
825 sv_setpv(sv, errno ? Strerror(errno) : "");
830 SvNOK_on(sv); /* what a wonderful hack! */
832 else if (strEQ(remaining, "NCODING"))
833 sv_setsv(sv, PL_encoding);
835 case '\006': /* ^F */
836 sv_setiv(sv, (IV)PL_maxsysfd);
838 case '\010': /* ^H */
839 sv_setiv(sv, (IV)PL_hints);
841 case '\011': /* ^I */ /* NOT \t in EBCDIC */
842 sv_setpv(sv, PL_inplace); /* Will undefine sv if PL_inplace is NULL */
844 case '\017': /* ^O & ^OPEN */
845 if (nextchar == '\0') {
846 sv_setpv(sv, PL_osname);
849 else if (strEQ(remaining, "PEN")) {
850 Perl_emulate_cop_io(aTHX_ &PL_compiling, sv);
854 if (nextchar == '\0') { /* ^P */
855 sv_setiv(sv, (IV)PL_perldb);
856 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
857 goto do_prematch_fetch;
858 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
859 goto do_postmatch_fetch;
862 case '\023': /* ^S */
863 if (nextchar == '\0') {
864 if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING)
867 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
872 case '\024': /* ^T */
873 if (nextchar == '\0') {
875 sv_setnv(sv, PL_basetime);
877 sv_setiv(sv, (IV)PL_basetime);
880 else if (strEQ(remaining, "AINT"))
881 sv_setiv(sv, PL_tainting
882 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
885 case '\025': /* $^UNICODE, $^UTF8LOCALE, $^UTF8CACHE */
886 if (strEQ(remaining, "NICODE"))
887 sv_setuv(sv, (UV) PL_unicode);
888 else if (strEQ(remaining, "TF8LOCALE"))
889 sv_setuv(sv, (UV) PL_utf8locale);
890 else if (strEQ(remaining, "TF8CACHE"))
891 sv_setiv(sv, (IV) PL_utf8cache);
893 case '\027': /* ^W & $^WARNING_BITS */
894 if (nextchar == '\0')
895 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
896 else if (strEQ(remaining, "ARNING_BITS")) {
897 if (PL_compiling.cop_warnings == pWARN_NONE) {
898 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
900 else if (PL_compiling.cop_warnings == pWARN_STD) {
903 (PL_dowarn & G_WARN_ON) ? WARN_ALLstring : WARN_NONEstring,
907 else if (PL_compiling.cop_warnings == pWARN_ALL) {
908 /* Get the bit mask for $warnings::Bits{all}, because
909 * it could have been extended by warnings::register */
910 HV * const bits=get_hv("warnings::Bits", 0);
912 SV ** const bits_all = hv_fetchs(bits, "all", FALSE);
914 sv_setsv(sv, *bits_all);
917 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
921 sv_setpvn(sv, (char *) (PL_compiling.cop_warnings + 1),
922 *PL_compiling.cop_warnings);
927 case '\015': /* $^MATCH */
928 if (strEQ(remaining, "ATCH")) {
929 case '1': case '2': case '3': case '4':
930 case '5': case '6': case '7': case '8': case '9': case '&':
931 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
933 * Pre-threads, this was paren = atoi(GvENAME((const GV *)mg->mg_obj));
934 * XXX Does the new way break anything?
936 paren = atoi(mg->mg_ptr); /* $& is in [0] */
937 CALLREG_NUMBUF_FETCH(rx,paren,sv);
940 sv_setsv(sv,&PL_sv_undef);
944 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
945 if (RX_LASTPAREN(rx)) {
946 CALLREG_NUMBUF_FETCH(rx,RX_LASTPAREN(rx),sv);
950 sv_setsv(sv,&PL_sv_undef);
952 case '\016': /* ^N */
953 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
954 if (RX_LASTCLOSEPAREN(rx)) {
955 CALLREG_NUMBUF_FETCH(rx,RX_LASTCLOSEPAREN(rx),sv);
960 sv_setsv(sv,&PL_sv_undef);
964 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
965 CALLREG_NUMBUF_FETCH(rx,-2,sv);
968 sv_setsv(sv,&PL_sv_undef);
972 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
973 CALLREG_NUMBUF_FETCH(rx,-1,sv);
976 sv_setsv(sv,&PL_sv_undef);
979 if (GvIO(PL_last_in_gv)) {
980 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
985 sv_setiv(sv, (IV)STATUS_CURRENT);
986 #ifdef COMPLEX_STATUS
987 SvUPGRADE(sv, SVt_PVLV);
988 LvTARGOFF(sv) = PL_statusvalue;
989 LvTARGLEN(sv) = PL_statusvalue_vms;
994 if (GvIOp(PL_defoutgv))
995 s = IoTOP_NAME(GvIOp(PL_defoutgv));
999 sv_setpv(sv,GvENAME(PL_defoutgv));
1000 sv_catpvs(sv,"_TOP");
1004 if (GvIOp(PL_defoutgv))
1005 s = IoFMT_NAME(GvIOp(PL_defoutgv));
1007 s = GvENAME(PL_defoutgv);
1011 if (GvIOp(PL_defoutgv))
1012 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
1015 if (GvIOp(PL_defoutgv))
1016 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
1019 if (GvIOp(PL_defoutgv))
1020 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
1027 sv_setiv(sv, (IV)CopARYBASE_get(PL_curcop));
1030 if (GvIOp(PL_defoutgv))
1031 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
1035 sv_copypv(sv, PL_ors_sv);
1041 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
1043 sv_setnv(sv, (NV)errno);
1046 if (errno == errno_isOS2 || errno == errno_isOS2_set)
1047 sv_setpv(sv, os2error(Perl_rc));
1050 sv_setpv(sv, errno ? Strerror(errno) : "");
1052 SvPOK_on(sv); /* may have got removed during taint processing */
1057 SvNOK_on(sv); /* what a wonderful hack! */
1060 sv_setiv(sv, (IV)PL_uid);
1063 sv_setiv(sv, (IV)PL_euid);
1066 sv_setiv(sv, (IV)PL_gid);
1069 sv_setiv(sv, (IV)PL_egid);
1071 #ifdef HAS_GETGROUPS
1073 Groups_t *gary = NULL;
1074 I32 i, num_groups = getgroups(0, gary);
1075 Newx(gary, num_groups, Groups_t);
1076 num_groups = getgroups(num_groups, gary);
1077 for (i = 0; i < num_groups; i++)
1078 Perl_sv_catpvf(aTHX_ sv, " %"IVdf, (IV)gary[i]);
1081 (void)SvIOK_on(sv); /* what a wonderful hack! */
1091 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
1093 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
1095 PERL_ARGS_ASSERT_MAGIC_GETUVAR;
1097 if (uf && uf->uf_val)
1098 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
1103 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
1106 STRLEN len = 0, klen;
1107 const char *s = SvOK(sv) ? SvPV_const(sv,len) : "";
1108 const char * const ptr = MgPV_const(mg,klen);
1111 PERL_ARGS_ASSERT_MAGIC_SETENV;
1113 #ifdef DYNAMIC_ENV_FETCH
1114 /* We just undefd an environment var. Is a replacement */
1115 /* waiting in the wings? */
1117 SV ** const valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE);
1119 s = SvOK(*valp) ? SvPV_const(*valp, len) : "";
1123 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
1124 /* And you'll never guess what the dog had */
1125 /* in its mouth... */
1127 MgTAINTEDDIR_off(mg);
1129 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1130 char pathbuf[256], eltbuf[256], *cp, *elt;
1134 my_strlcpy(eltbuf, s, sizeof(eltbuf));
1136 do { /* DCL$PATH may be a search list */
1137 while (1) { /* as may dev portion of any element */
1138 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
1139 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
1140 cando_by_name(S_IWUSR,0,elt) ) {
1141 MgTAINTEDDIR_on(mg);
1145 if ((cp = strchr(elt, ':')) != NULL)
1147 if (my_trnlnm(elt, eltbuf, j++))
1153 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
1156 if (s && klen == 4 && strEQ(ptr,"PATH")) {
1157 const char * const strend = s + len;
1159 while (s < strend) {
1163 #ifdef VMS /* Hmm. How do we get $Config{path_sep} from C? */
1164 const char path_sep = '|';
1166 const char path_sep = ':';
1168 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
1169 s, strend, path_sep, &i);
1171 if (i >= (I32)sizeof tmpbuf /* too long -- assume the worst */
1173 || !strchr(tmpbuf, ':') /* no colon thus no device name -- assume relative path */
1175 || *tmpbuf != '/' /* no starting slash -- assume relative path */
1177 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
1178 MgTAINTEDDIR_on(mg);
1184 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1190 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
1192 PERL_ARGS_ASSERT_MAGIC_CLEARENV;
1193 PERL_UNUSED_ARG(sv);
1194 my_setenv(MgPV_nolen_const(mg),NULL);
1199 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1202 PERL_ARGS_ASSERT_MAGIC_SET_ALL_ENV;
1203 PERL_UNUSED_ARG(mg);
1205 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1207 if (PL_localizing) {
1210 hv_iterinit(MUTABLE_HV(sv));
1211 while ((entry = hv_iternext(MUTABLE_HV(sv)))) {
1213 my_setenv(hv_iterkey(entry, &keylen),
1214 SvPV_nolen_const(hv_iterval(MUTABLE_HV(sv), entry)));
1222 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1225 PERL_ARGS_ASSERT_MAGIC_CLEAR_ALL_ENV;
1226 PERL_UNUSED_ARG(sv);
1227 PERL_UNUSED_ARG(mg);
1229 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1237 #ifdef HAS_SIGPROCMASK
1239 restore_sigmask(pTHX_ SV *save_sv)
1241 const sigset_t * const ossetp = (const sigset_t *) SvPV_nolen_const( save_sv );
1242 (void)sigprocmask(SIG_SETMASK, ossetp, NULL);
1246 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1249 /* Are we fetching a signal entry? */
1250 int i = (I16)mg->mg_private;
1252 PERL_ARGS_ASSERT_MAGIC_GETSIG;
1255 mg->mg_private = i = whichsig(MgPV_nolen_const(mg));
1260 sv_setsv(sv,PL_psig_ptr[i]);
1262 Sighandler_t sigstate = rsignal_state(i);
1263 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1264 if (PL_sig_handlers_initted && PL_sig_ignoring[i])
1267 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1268 if (PL_sig_handlers_initted && PL_sig_defaulting[i])
1271 /* cache state so we don't fetch it again */
1272 if(sigstate == (Sighandler_t) SIG_IGN)
1273 sv_setpvs(sv,"IGNORE");
1275 sv_setsv(sv,&PL_sv_undef);
1276 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1283 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1285 PERL_ARGS_ASSERT_MAGIC_CLEARSIG;
1286 PERL_UNUSED_ARG(sv);
1288 magic_setsig(NULL, mg);
1289 return sv_unmagic(sv, mg->mg_type);
1293 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1294 Perl_csighandler(int sig, siginfo_t *sip PERL_UNUSED_DECL, void *uap PERL_UNUSED_DECL)
1296 Perl_csighandler(int sig)
1299 #ifdef PERL_GET_SIG_CONTEXT
1300 dTHXa(PERL_GET_SIG_CONTEXT);
1304 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1305 (void) rsignal(sig, PL_csighandlerp);
1306 if (PL_sig_ignoring[sig]) return;
1308 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1309 if (PL_sig_defaulting[sig])
1310 #ifdef KILL_BY_SIGPRC
1311 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1326 (PL_signals & PERL_SIGNALS_UNSAFE_FLAG))
1327 /* Call the perl level handler now--
1328 * with risk we may be in malloc() or being destructed etc. */
1329 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1330 (*PL_sighandlerp)(sig, NULL, NULL);
1332 (*PL_sighandlerp)(sig);
1335 if (!PL_psig_pend) return;
1336 /* Set a flag to say this signal is pending, that is awaiting delivery after
1337 * the current Perl opcode completes */
1338 PL_psig_pend[sig]++;
1340 #ifndef SIG_PENDING_DIE_COUNT
1341 # define SIG_PENDING_DIE_COUNT 120
1343 /* Add one to say _a_ signal is pending */
1344 if (++PL_sig_pending >= SIG_PENDING_DIE_COUNT)
1345 Perl_croak(aTHX_ "Maximal count of pending signals (%lu) exceeded",
1346 (unsigned long)SIG_PENDING_DIE_COUNT);
1350 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1352 Perl_csighandler_init(void)
1355 if (PL_sig_handlers_initted) return;
1357 for (sig = 1; sig < SIG_SIZE; sig++) {
1358 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1360 PL_sig_defaulting[sig] = 1;
1361 (void) rsignal(sig, PL_csighandlerp);
1363 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1364 PL_sig_ignoring[sig] = 0;
1367 PL_sig_handlers_initted = 1;
1372 Perl_despatch_signals(pTHX)
1377 for (sig = 1; sig < SIG_SIZE; sig++) {
1378 if (PL_psig_pend[sig]) {
1379 PERL_BLOCKSIG_ADD(set, sig);
1380 PL_psig_pend[sig] = 0;
1381 PERL_BLOCKSIG_BLOCK(set);
1382 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1383 (*PL_sighandlerp)(sig, NULL, NULL);
1385 (*PL_sighandlerp)(sig);
1387 PERL_BLOCKSIG_UNBLOCK(set);
1392 /* sv of NULL signifies that we're acting as magic_clearsig. */
1394 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1399 /* Need to be careful with SvREFCNT_dec(), because that can have side
1400 * effects (due to closures). We must make sure that the new disposition
1401 * is in place before it is called.
1405 #ifdef HAS_SIGPROCMASK
1409 register const char *s = MgPV_const(mg,len);
1411 PERL_ARGS_ASSERT_MAGIC_SETSIG;
1414 if (strEQ(s,"__DIE__"))
1416 else if (strEQ(s,"__WARN__")
1417 && (sv ? 1 : PL_warnhook != PERL_WARNHOOK_FATAL)) {
1418 /* Merge the existing behaviours, which are as follows:
1419 magic_setsig, we always set svp to &PL_warnhook
1420 (hence we always change the warnings handler)
1421 For magic_clearsig, we don't change the warnings handler if it's
1422 set to the &PL_warnhook. */
1425 Perl_croak(aTHX_ "No such hook: %s", s);
1428 if (*svp != PERL_WARNHOOK_FATAL)
1434 i = (I16)mg->mg_private;
1436 i = whichsig(s); /* ...no, a brick */
1437 mg->mg_private = (U16)i;
1441 Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1444 #ifdef HAS_SIGPROCMASK
1445 /* Avoid having the signal arrive at a bad time, if possible. */
1448 sigprocmask(SIG_BLOCK, &set, &save);
1450 save_sv = newSVpvn((char *)(&save), sizeof(sigset_t));
1451 SAVEFREESV(save_sv);
1452 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1455 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1456 if (!PL_sig_handlers_initted) Perl_csighandler_init();
1458 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1459 PL_sig_ignoring[i] = 0;
1461 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1462 PL_sig_defaulting[i] = 0;
1464 to_dec = PL_psig_ptr[i];
1466 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1467 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1469 /* Signals don't change name during the program's execution, so once
1470 they're cached in the appropriate slot of PL_psig_name, they can
1473 Ideally we'd find some way of making SVs at (C) compile time, or
1474 at least, doing most of the work. */
1475 if (!PL_psig_name[i]) {
1476 PL_psig_name[i] = newSVpvn(s, len);
1477 SvREADONLY_on(PL_psig_name[i]);
1480 SvREFCNT_dec(PL_psig_name[i]);
1481 PL_psig_name[i] = NULL;
1482 PL_psig_ptr[i] = NULL;
1485 if (sv && (isGV_with_GP(sv) || SvROK(sv))) {
1487 (void)rsignal(i, PL_csighandlerp);
1490 *svp = SvREFCNT_inc_simple_NN(sv);
1492 if (sv && SvOK(sv)) {
1493 s = SvPV_force(sv, len);
1497 if (sv && strEQ(s,"IGNORE")) {
1499 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1500 PL_sig_ignoring[i] = 1;
1501 (void)rsignal(i, PL_csighandlerp);
1503 (void)rsignal(i, (Sighandler_t) SIG_IGN);
1507 else if (!sv || strEQ(s,"DEFAULT") || !len) {
1509 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1510 PL_sig_defaulting[i] = 1;
1511 (void)rsignal(i, PL_csighandlerp);
1513 (void)rsignal(i, (Sighandler_t) SIG_DFL);
1519 * We should warn if HINT_STRICT_REFS, but without
1520 * access to a known hint bit in a known OP, we can't
1521 * tell whether HINT_STRICT_REFS is in force or not.
1523 if (!strchr(s,':') && !strchr(s,'\''))
1524 Perl_sv_insert_flags(aTHX_ sv, 0, 0, STR_WITH_LEN("main::"),
1527 (void)rsignal(i, PL_csighandlerp);
1529 *svp = SvREFCNT_inc_simple_NN(sv);
1533 #ifdef HAS_SIGPROCMASK
1537 SvREFCNT_dec(to_dec);
1540 #endif /* !PERL_MICRO */
1543 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1546 PERL_ARGS_ASSERT_MAGIC_SETISA;
1547 PERL_UNUSED_ARG(sv);
1549 /* Skip _isaelem because _isa will handle it shortly */
1550 if (PL_delaymagic & DM_ARRAY && mg->mg_type == PERL_MAGIC_isaelem)
1553 return magic_clearisa(NULL, mg);
1556 /* sv of NULL signifies that we're acting as magic_setisa. */
1558 Perl_magic_clearisa(pTHX_ SV *sv, MAGIC *mg)
1563 PERL_ARGS_ASSERT_MAGIC_CLEARISA;
1565 /* Bail out if destruction is going on */
1566 if(PL_dirty) return 0;
1569 av_clear(MUTABLE_AV(sv));
1571 /* XXX Once it's possible, we need to
1572 detect that our @ISA is aliased in
1573 other stashes, and act on the stashes
1574 of all of the aliases */
1576 /* The first case occurs via setisa,
1577 the second via setisa_elem, which
1578 calls this same magic */
1580 SvTYPE(mg->mg_obj) == SVt_PVGV
1581 ? (const GV *)mg->mg_obj
1582 : (const GV *)mg_find(mg->mg_obj, PERL_MAGIC_isa)->mg_obj
1586 mro_isa_changed_in(stash);
1592 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1595 PERL_ARGS_ASSERT_MAGIC_SETAMAGIC;
1596 PERL_UNUSED_ARG(sv);
1597 PERL_UNUSED_ARG(mg);
1598 PL_amagic_generation++;
1604 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1606 HV * const hv = MUTABLE_HV(LvTARG(sv));
1609 PERL_ARGS_ASSERT_MAGIC_GETNKEYS;
1610 PERL_UNUSED_ARG(mg);
1613 (void) hv_iterinit(hv);
1614 if (! SvTIED_mg((const SV *)hv, PERL_MAGIC_tied))
1617 while (hv_iternext(hv))
1622 sv_setiv(sv, (IV)i);
1627 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1629 PERL_ARGS_ASSERT_MAGIC_SETNKEYS;
1630 PERL_UNUSED_ARG(mg);
1632 hv_ksplit(MUTABLE_HV(LvTARG(sv)), SvIV(sv));
1637 /* caller is responsible for stack switching/cleanup */
1639 S_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, const char *meth, I32 flags, int n, SV *val)
1644 PERL_ARGS_ASSERT_MAGIC_METHCALL;
1648 PUSHs(SvTIED_obj(sv, mg));
1651 if (mg->mg_len >= 0)
1652 mPUSHp(mg->mg_ptr, mg->mg_len);
1653 else if (mg->mg_len == HEf_SVKEY)
1654 PUSHs(MUTABLE_SV(mg->mg_ptr));
1656 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1665 return call_method(meth, flags);
1669 S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth)
1673 PERL_ARGS_ASSERT_MAGIC_METHPACK;
1677 PUSHSTACKi(PERLSI_MAGIC);
1679 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1680 sv_setsv(sv, *PL_stack_sp--);
1690 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1692 PERL_ARGS_ASSERT_MAGIC_GETPACK;
1695 mg->mg_flags |= MGf_GSKIP;
1696 magic_methpack(sv,mg,"FETCH");
1701 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1705 PERL_ARGS_ASSERT_MAGIC_SETPACK;
1708 PUSHSTACKi(PERLSI_MAGIC);
1709 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1716 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1718 PERL_ARGS_ASSERT_MAGIC_CLEARPACK;
1720 return magic_methpack(sv,mg,"DELETE");
1725 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1730 PERL_ARGS_ASSERT_MAGIC_SIZEPACK;
1734 PUSHSTACKi(PERLSI_MAGIC);
1735 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1736 sv = *PL_stack_sp--;
1737 retval = SvIV(sv)-1;
1739 Perl_croak(aTHX_ "FETCHSIZE returned a negative value");
1744 return (U32) retval;
1748 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1752 PERL_ARGS_ASSERT_MAGIC_WIPEPACK;
1755 PUSHSTACKi(PERLSI_MAGIC);
1757 XPUSHs(SvTIED_obj(sv, mg));
1759 call_method("CLEAR", G_SCALAR|G_DISCARD);
1767 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1770 const char * const meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1772 PERL_ARGS_ASSERT_MAGIC_NEXTPACK;
1776 PUSHSTACKi(PERLSI_MAGIC);
1779 PUSHs(SvTIED_obj(sv, mg));
1784 if (call_method(meth, G_SCALAR))
1785 sv_setsv(key, *PL_stack_sp--);
1794 Perl_magic_existspack(pTHX_ SV *sv, const MAGIC *mg)
1796 PERL_ARGS_ASSERT_MAGIC_EXISTSPACK;
1798 return magic_methpack(sv,mg,"EXISTS");
1802 Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1806 SV * const tied = SvTIED_obj(MUTABLE_SV(hv), mg);
1807 HV * const pkg = SvSTASH((const SV *)SvRV(tied));
1809 PERL_ARGS_ASSERT_MAGIC_SCALARPACK;
1811 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1813 if (HvEITER_get(hv))
1814 /* we are in an iteration so the hash cannot be empty */
1816 /* no xhv_eiter so now use FIRSTKEY */
1817 key = sv_newmortal();
1818 magic_nextpack(MUTABLE_SV(hv), mg, key);
1819 HvEITER_set(hv, NULL); /* need to reset iterator */
1820 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1823 /* there is a SCALAR method that we can call */
1825 PUSHSTACKi(PERLSI_MAGIC);
1831 if (call_method("SCALAR", G_SCALAR))
1832 retval = *PL_stack_sp--;
1834 retval = &PL_sv_undef;
1841 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1844 GV * const gv = PL_DBline;
1845 const I32 i = SvTRUE(sv);
1846 SV ** const svp = av_fetch(GvAV(gv),
1847 atoi(MgPV_nolen_const(mg)), FALSE);
1849 PERL_ARGS_ASSERT_MAGIC_SETDBLINE;
1851 if (svp && SvIOKp(*svp)) {
1852 OP * const o = INT2PTR(OP*,SvIVX(*svp));
1854 /* set or clear breakpoint in the relevant control op */
1856 o->op_flags |= OPf_SPECIAL;
1858 o->op_flags &= ~OPf_SPECIAL;
1865 Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg)
1868 AV * const obj = MUTABLE_AV(mg->mg_obj);
1870 PERL_ARGS_ASSERT_MAGIC_GETARYLEN;
1873 sv_setiv(sv, AvFILL(obj) + CopARYBASE_get(PL_curcop));
1881 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1884 AV * const obj = MUTABLE_AV(mg->mg_obj);
1886 PERL_ARGS_ASSERT_MAGIC_SETARYLEN;
1889 av_fill(obj, SvIV(sv) - CopARYBASE_get(PL_curcop));
1891 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
1892 "Attempt to set length of freed array");
1898 Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
1902 PERL_ARGS_ASSERT_MAGIC_FREEARYLEN_P;
1903 PERL_UNUSED_ARG(sv);
1905 /* during global destruction, mg_obj may already have been freed */
1906 if (PL_in_clean_all)
1909 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
1912 /* arylen scalar holds a pointer back to the array, but doesn't own a
1913 reference. Hence the we (the array) are about to go away with it
1914 still pointing at us. Clear its pointer, else it would be pointing
1915 at free memory. See the comment in sv_magic about reference loops,
1916 and why it can't own a reference to us. */
1923 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1926 SV* const lsv = LvTARG(sv);
1928 PERL_ARGS_ASSERT_MAGIC_GETPOS;
1929 PERL_UNUSED_ARG(mg);
1931 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1932 MAGIC * const found = mg_find(lsv, PERL_MAGIC_regex_global);
1933 if (found && found->mg_len >= 0) {
1934 I32 i = found->mg_len;
1936 sv_pos_b2u(lsv, &i);
1937 sv_setiv(sv, i + CopARYBASE_get(PL_curcop));
1946 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1949 SV* const lsv = LvTARG(sv);
1955 PERL_ARGS_ASSERT_MAGIC_SETPOS;
1956 PERL_UNUSED_ARG(mg);
1958 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1959 found = mg_find(lsv, PERL_MAGIC_regex_global);
1965 #ifdef PERL_OLD_COPY_ON_WRITE
1967 sv_force_normal_flags(lsv, 0);
1969 found = sv_magicext(lsv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
1972 else if (!SvOK(sv)) {
1976 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1978 pos = SvIV(sv) - CopARYBASE_get(PL_curcop);
1981 ulen = sv_len_utf8(lsv);
1991 else if (pos > (SSize_t)len)
1996 sv_pos_u2b(lsv, &p, 0);
2000 found->mg_len = pos;
2001 found->mg_flags &= ~MGf_MINMATCH;
2007 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
2010 SV * const lsv = LvTARG(sv);
2011 const char * const tmps = SvPV_const(lsv,len);
2012 STRLEN offs = LvTARGOFF(sv);
2013 STRLEN rem = LvTARGLEN(sv);
2015 PERL_ARGS_ASSERT_MAGIC_GETSUBSTR;
2016 PERL_UNUSED_ARG(mg);
2019 offs = sv_pos_u2b_flags(lsv, offs, &rem, SV_CONST_RETURN);
2022 if (rem > len - offs)
2024 sv_setpvn(sv, tmps + offs, rem);
2031 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
2035 const char * const tmps = SvPV_const(sv, len);
2036 SV * const lsv = LvTARG(sv);
2037 STRLEN lvoff = LvTARGOFF(sv);
2038 STRLEN lvlen = LvTARGLEN(sv);
2040 PERL_ARGS_ASSERT_MAGIC_SETSUBSTR;
2041 PERL_UNUSED_ARG(mg);
2044 sv_utf8_upgrade(lsv);
2045 lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN);
2046 sv_insert(lsv, lvoff, lvlen, tmps, len);
2047 LvTARGLEN(sv) = sv_len_utf8(sv);
2050 else if (lsv && SvUTF8(lsv)) {
2052 lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN);
2053 LvTARGLEN(sv) = len;
2054 utf8 = (char*)bytes_to_utf8((U8*)tmps, &len);
2055 sv_insert(lsv, lvoff, lvlen, utf8, len);
2059 sv_insert(lsv, lvoff, lvlen, tmps, len);
2060 LvTARGLEN(sv) = len;
2067 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
2071 PERL_ARGS_ASSERT_MAGIC_GETTAINT;
2072 PERL_UNUSED_ARG(sv);
2074 TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1));
2079 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
2083 PERL_ARGS_ASSERT_MAGIC_SETTAINT;
2084 PERL_UNUSED_ARG(sv);
2086 /* update taint status */
2095 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
2097 SV * const lsv = LvTARG(sv);
2099 PERL_ARGS_ASSERT_MAGIC_GETVEC;
2100 PERL_UNUSED_ARG(mg);
2103 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
2111 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
2113 PERL_ARGS_ASSERT_MAGIC_SETVEC;
2114 PERL_UNUSED_ARG(mg);
2115 do_vecset(sv); /* XXX slurp this routine */
2120 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
2125 PERL_ARGS_ASSERT_MAGIC_GETDEFELEM;
2127 if (LvTARGLEN(sv)) {
2129 SV * const ahv = LvTARG(sv);
2130 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, FALSE, 0);
2135 AV *const av = MUTABLE_AV(LvTARG(sv));
2136 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
2137 targ = AvARRAY(av)[LvTARGOFF(sv)];
2139 if (targ && (targ != &PL_sv_undef)) {
2140 /* somebody else defined it for us */
2141 SvREFCNT_dec(LvTARG(sv));
2142 LvTARG(sv) = SvREFCNT_inc_simple_NN(targ);
2144 SvREFCNT_dec(mg->mg_obj);
2146 mg->mg_flags &= ~MGf_REFCOUNTED;
2151 sv_setsv(sv, targ ? targ : &PL_sv_undef);
2156 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
2158 PERL_ARGS_ASSERT_MAGIC_SETDEFELEM;
2159 PERL_UNUSED_ARG(mg);
2163 sv_setsv(LvTARG(sv), sv);
2164 SvSETMAGIC(LvTARG(sv));
2170 Perl_vivify_defelem(pTHX_ SV *sv)
2176 PERL_ARGS_ASSERT_VIVIFY_DEFELEM;
2178 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
2181 SV * const ahv = LvTARG(sv);
2182 HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, TRUE, 0);
2185 if (!value || value == &PL_sv_undef)
2186 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(mg->mg_obj));
2189 AV *const av = MUTABLE_AV(LvTARG(sv));
2190 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
2191 LvTARG(sv) = NULL; /* array can't be extended */
2193 SV* const * const svp = av_fetch(av, LvTARGOFF(sv), TRUE);
2194 if (!svp || (value = *svp) == &PL_sv_undef)
2195 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
2198 SvREFCNT_inc_simple_void(value);
2199 SvREFCNT_dec(LvTARG(sv));
2202 SvREFCNT_dec(mg->mg_obj);
2204 mg->mg_flags &= ~MGf_REFCOUNTED;
2208 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
2210 PERL_ARGS_ASSERT_MAGIC_KILLBACKREFS;
2211 return Perl_sv_kill_backrefs(aTHX_ sv, MUTABLE_AV(mg->mg_obj));
2215 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
2217 PERL_ARGS_ASSERT_MAGIC_SETMGLOB;
2218 PERL_UNUSED_CONTEXT;
2220 if (!isGV_with_GP(sv))
2226 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
2228 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
2230 PERL_ARGS_ASSERT_MAGIC_SETUVAR;
2232 if (uf && uf->uf_set)
2233 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
2238 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2240 const char type = mg->mg_type;
2242 PERL_ARGS_ASSERT_MAGIC_SETREGEXP;
2244 if (type == PERL_MAGIC_qr) {
2245 } else if (type == PERL_MAGIC_bm) {
2249 assert(type == PERL_MAGIC_fm);
2252 return sv_unmagic(sv, type);
2255 #ifdef USE_LOCALE_COLLATE
2257 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
2259 PERL_ARGS_ASSERT_MAGIC_SETCOLLXFRM;
2262 * RenE<eacute> Descartes said "I think not."
2263 * and vanished with a faint plop.
2265 PERL_UNUSED_CONTEXT;
2266 PERL_UNUSED_ARG(sv);
2268 Safefree(mg->mg_ptr);
2274 #endif /* USE_LOCALE_COLLATE */
2276 /* Just clear the UTF-8 cache data. */
2278 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2280 PERL_ARGS_ASSERT_MAGIC_SETUTF8;
2281 PERL_UNUSED_CONTEXT;
2282 PERL_UNUSED_ARG(sv);
2283 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2285 mg->mg_len = -1; /* The mg_len holds the len cache. */
2290 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
2293 register const char *s;
2295 register const REGEXP * rx;
2296 const char * const remaining = mg->mg_ptr + 1;
2300 PERL_ARGS_ASSERT_MAGIC_SET;
2302 switch (*mg->mg_ptr) {
2303 case '\015': /* $^MATCH */
2304 if (strEQ(remaining, "ATCH"))
2306 case '`': /* ${^PREMATCH} caught below */
2308 paren = RX_BUFF_IDX_PREMATCH;
2310 case '\'': /* ${^POSTMATCH} caught below */
2312 paren = RX_BUFF_IDX_POSTMATCH;
2316 paren = RX_BUFF_IDX_FULLMATCH;
2318 case '1': case '2': case '3': case '4':
2319 case '5': case '6': case '7': case '8': case '9':
2320 paren = atoi(mg->mg_ptr);
2322 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
2323 CALLREG_NUMBUF_STORE((REGEXP * const)rx,paren,sv);
2326 /* Croak with a READONLY error when a numbered match var is
2327 * set without a previous pattern match. Unless it's C<local $1>
2329 if (!PL_localizing) {
2330 Perl_croak(aTHX_ "%s", PL_no_modify);
2333 case '\001': /* ^A */
2334 sv_setsv(PL_bodytarget, sv);
2336 case '\003': /* ^C */
2337 PL_minus_c = (bool)SvIV(sv);
2340 case '\004': /* ^D */
2342 s = SvPV_nolen_const(sv);
2343 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
2344 if (DEBUG_x_TEST || DEBUG_B_TEST)
2345 dump_all_perl(!DEBUG_B_TEST);
2347 PL_debug = (SvIV(sv)) | DEBUG_TOP_FLAG;
2350 case '\005': /* ^E */
2351 if (*(mg->mg_ptr+1) == '\0') {
2353 set_vaxc_errno(SvIV(sv));
2356 SetLastError( SvIV(sv) );
2359 os2_setsyserrno(SvIV(sv));
2361 /* will anyone ever use this? */
2362 SETERRNO(SvIV(sv), 4);
2367 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2368 SvREFCNT_dec(PL_encoding);
2369 if (SvOK(sv) || SvGMAGICAL(sv)) {
2370 PL_encoding = newSVsv(sv);
2377 case '\006': /* ^F */
2378 PL_maxsysfd = SvIV(sv);
2380 case '\010': /* ^H */
2381 PL_hints = SvIV(sv);
2383 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2384 Safefree(PL_inplace);
2385 PL_inplace = SvOK(sv) ? savesvpv(sv) : NULL;
2387 case '\017': /* ^O */
2388 if (*(mg->mg_ptr+1) == '\0') {
2389 Safefree(PL_osname);
2392 TAINT_PROPER("assigning to $^O");
2393 PL_osname = savesvpv(sv);
2396 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2398 const char *const start = SvPV(sv, len);
2399 const char *out = (const char*)memchr(start, '\0', len);
2403 PL_compiling.cop_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
2404 PL_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
2406 /* Opening for input is more common than opening for output, so
2407 ensure that hints for input are sooner on linked list. */
2408 tmp = out ? newSVpvn_flags(out + 1, start + len - out - 1,
2410 : newSVpvs_flags("", SvUTF8(sv));
2411 (void)hv_stores(GvHV(PL_hintgv), "open>", tmp);
2414 tmp = newSVpvn_flags(start, out ? (STRLEN)(out - start) : len,
2416 (void)hv_stores(GvHV(PL_hintgv), "open<", tmp);
2420 case '\020': /* ^P */
2421 if (*remaining == '\0') { /* ^P */
2422 PL_perldb = SvIV(sv);
2423 if (PL_perldb && !PL_DBsingle)
2426 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
2428 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
2431 case '\024': /* ^T */
2433 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2435 PL_basetime = (Time_t)SvIV(sv);
2438 case '\025': /* ^UTF8CACHE */
2439 if (strEQ(mg->mg_ptr+1, "TF8CACHE")) {
2440 PL_utf8cache = (signed char) sv_2iv(sv);
2443 case '\027': /* ^W & $^WARNING_BITS */
2444 if (*(mg->mg_ptr+1) == '\0') {
2445 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2447 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2448 | (i ? G_WARN_ON : G_WARN_OFF) ;
2451 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2452 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2453 if (!SvPOK(sv) && PL_localizing) {
2454 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2455 PL_compiling.cop_warnings = pWARN_NONE;
2460 int accumulate = 0 ;
2461 int any_fatals = 0 ;
2462 const char * const ptr = SvPV_const(sv, len) ;
2463 for (i = 0 ; i < len ; ++i) {
2464 accumulate |= ptr[i] ;
2465 any_fatals |= (ptr[i] & 0xAA) ;
2468 if (!specialWARN(PL_compiling.cop_warnings))
2469 PerlMemShared_free(PL_compiling.cop_warnings);
2470 PL_compiling.cop_warnings = pWARN_NONE;
2472 /* Yuck. I can't see how to abstract this: */
2473 else if (isWARN_on(((STRLEN *)SvPV_nolen_const(sv)) - 1,
2474 WARN_ALL) && !any_fatals) {
2475 if (!specialWARN(PL_compiling.cop_warnings))
2476 PerlMemShared_free(PL_compiling.cop_warnings);
2477 PL_compiling.cop_warnings = pWARN_ALL;
2478 PL_dowarn |= G_WARN_ONCE ;
2482 const char *const p = SvPV_const(sv, len);
2484 PL_compiling.cop_warnings
2485 = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
2488 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2489 PL_dowarn |= G_WARN_ONCE ;
2497 if (PL_localizing) {
2498 if (PL_localizing == 1)
2499 SAVESPTR(PL_last_in_gv);
2501 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2502 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2505 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2506 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2507 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2510 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2511 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2512 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2515 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv));
2518 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv));
2519 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2520 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2523 IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv));
2527 IO * const io = GvIOp(PL_defoutgv);
2530 if ((SvIV(sv)) == 0)
2531 IoFLAGS(io) &= ~IOf_FLUSH;
2533 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2534 PerlIO *ofp = IoOFP(io);
2536 (void)PerlIO_flush(ofp);
2537 IoFLAGS(io) |= IOf_FLUSH;
2543 SvREFCNT_dec(PL_rs);
2544 PL_rs = newSVsv(sv);
2547 SvREFCNT_dec(PL_ors_sv);
2548 if (SvOK(sv) || SvGMAGICAL(sv)) {
2549 PL_ors_sv = newSVsv(sv);
2556 CopARYBASE_set(&PL_compiling, SvIV(sv));
2559 #ifdef COMPLEX_STATUS
2560 if (PL_localizing == 2) {
2561 SvUPGRADE(sv, SVt_PVLV);
2562 PL_statusvalue = LvTARGOFF(sv);
2563 PL_statusvalue_vms = LvTARGLEN(sv);
2567 #ifdef VMSISH_STATUS
2569 STATUS_NATIVE_CHILD_SET((U32)SvIV(sv));
2572 STATUS_UNIX_EXIT_SET(SvIV(sv));
2577 # define PERL_VMS_BANG vaxc$errno
2579 # define PERL_VMS_BANG 0
2581 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2582 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2587 if (PL_delaymagic) {
2588 PL_delaymagic |= DM_RUID;
2589 break; /* don't do magic till later */
2592 (void)setruid((Uid_t)PL_uid);
2595 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2597 #ifdef HAS_SETRESUID
2598 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2600 if (PL_uid == PL_euid) { /* special case $< = $> */
2602 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2603 if (PL_uid != 0 && PerlProc_getuid() == 0)
2604 (void)PerlProc_setuid(0);
2606 (void)PerlProc_setuid(PL_uid);
2608 PL_uid = PerlProc_getuid();
2609 Perl_croak(aTHX_ "setruid() not implemented");
2614 PL_uid = PerlProc_getuid();
2615 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2619 if (PL_delaymagic) {
2620 PL_delaymagic |= DM_EUID;
2621 break; /* don't do magic till later */
2624 (void)seteuid((Uid_t)PL_euid);
2627 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2629 #ifdef HAS_SETRESUID
2630 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2632 if (PL_euid == PL_uid) /* special case $> = $< */
2633 PerlProc_setuid(PL_euid);
2635 PL_euid = PerlProc_geteuid();
2636 Perl_croak(aTHX_ "seteuid() not implemented");
2641 PL_euid = PerlProc_geteuid();
2642 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2646 if (PL_delaymagic) {
2647 PL_delaymagic |= DM_RGID;
2648 break; /* don't do magic till later */
2651 (void)setrgid((Gid_t)PL_gid);
2654 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2656 #ifdef HAS_SETRESGID
2657 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2659 if (PL_gid == PL_egid) /* special case $( = $) */
2660 (void)PerlProc_setgid(PL_gid);
2662 PL_gid = PerlProc_getgid();
2663 Perl_croak(aTHX_ "setrgid() not implemented");
2668 PL_gid = PerlProc_getgid();
2669 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2672 #ifdef HAS_SETGROUPS
2674 const char *p = SvPV_const(sv, len);
2675 Groups_t *gary = NULL;
2676 #ifdef _SC_NGROUPS_MAX
2677 int maxgrp = sysconf(_SC_NGROUPS_MAX);
2682 int maxgrp = NGROUPS;
2688 for (i = 0; i < maxgrp; ++i) {
2689 while (*p && !isSPACE(*p))
2696 Newx(gary, i + 1, Groups_t);
2698 Renew(gary, i + 1, Groups_t);
2702 (void)setgroups(i, gary);
2705 #else /* HAS_SETGROUPS */
2707 #endif /* HAS_SETGROUPS */
2708 if (PL_delaymagic) {
2709 PL_delaymagic |= DM_EGID;
2710 break; /* don't do magic till later */
2713 (void)setegid((Gid_t)PL_egid);
2716 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2718 #ifdef HAS_SETRESGID
2719 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2721 if (PL_egid == PL_gid) /* special case $) = $( */
2722 (void)PerlProc_setgid(PL_egid);
2724 PL_egid = PerlProc_getegid();
2725 Perl_croak(aTHX_ "setegid() not implemented");
2730 PL_egid = PerlProc_getegid();
2731 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2734 PL_chopset = SvPV_force(sv,len);
2737 LOCK_DOLLARZERO_MUTEX;
2738 #ifdef HAS_SETPROCTITLE
2739 /* The BSDs don't show the argv[] in ps(1) output, they
2740 * show a string from the process struct and provide
2741 * the setproctitle() routine to manipulate that. */
2742 if (PL_origalen != 1) {
2743 s = SvPV_const(sv, len);
2744 # if __FreeBSD_version > 410001
2745 /* The leading "-" removes the "perl: " prefix,
2746 * but not the "(perl) suffix from the ps(1)
2747 * output, because that's what ps(1) shows if the
2748 * argv[] is modified. */
2749 setproctitle("-%s", s);
2750 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2751 /* This doesn't really work if you assume that
2752 * $0 = 'foobar'; will wipe out 'perl' from the $0
2753 * because in ps(1) output the result will be like
2754 * sprintf("perl: %s (perl)", s)
2755 * I guess this is a security feature:
2756 * one (a user process) cannot get rid of the original name.
2758 setproctitle("%s", s);
2761 #elif defined(__hpux) && defined(PSTAT_SETCMD)
2762 if (PL_origalen != 1) {
2764 s = SvPV_const(sv, len);
2765 un.pst_command = (char *)s;
2766 pstat(PSTAT_SETCMD, un, len, 0, 0);
2769 if (PL_origalen > 1) {
2770 /* PL_origalen is set in perl_parse(). */
2771 s = SvPV_force(sv,len);
2772 if (len >= (STRLEN)PL_origalen-1) {
2773 /* Longer than original, will be truncated. We assume that
2774 * PL_origalen bytes are available. */
2775 Copy(s, PL_origargv[0], PL_origalen-1, char);
2778 /* Shorter than original, will be padded. */
2780 /* Special case for Mac OS X: see [perl #38868] */
2783 /* Is the space counterintuitive? Yes.
2784 * (You were expecting \0?)
2785 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2787 const int pad = ' ';
2789 Copy(s, PL_origargv[0], len, char);
2790 PL_origargv[0][len] = 0;
2791 memset(PL_origargv[0] + len + 1,
2792 pad, PL_origalen - len - 1);
2794 PL_origargv[0][PL_origalen-1] = 0;
2795 for (i = 1; i < PL_origargc; i++)
2799 UNLOCK_DOLLARZERO_MUTEX;
2806 Perl_whichsig(pTHX_ const char *sig)
2808 register char* const* sigv;
2810 PERL_ARGS_ASSERT_WHICHSIG;
2811 PERL_UNUSED_CONTEXT;
2813 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
2814 if (strEQ(sig,*sigv))
2815 return PL_sig_num[sigv - (char* const*)PL_sig_name];
2817 if (strEQ(sig,"CHLD"))
2821 if (strEQ(sig,"CLD"))
2828 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2829 Perl_sighandler(int sig, siginfo_t *sip, void *uap PERL_UNUSED_DECL)
2831 Perl_sighandler(int sig)
2834 #ifdef PERL_GET_SIG_CONTEXT
2835 dTHXa(PERL_GET_SIG_CONTEXT);
2842 SV * const tSv = PL_Sv;
2846 XPV * const tXpv = PL_Xpv;
2848 if (PL_savestack_ix + 15 <= PL_savestack_max)
2850 if (PL_markstack_ptr < PL_markstack_max - 2)
2852 if (PL_scopestack_ix < PL_scopestack_max - 3)
2855 if (!PL_psig_ptr[sig]) {
2856 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2861 /* Max number of items pushed there is 3*n or 4. We cannot fix
2862 infinity, so we fix 4 (in fact 5): */
2864 PL_savestack_ix += 5; /* Protect save in progress. */
2865 SAVEDESTRUCTOR_X(S_unwind_handler_stack, (void*)&flags);
2868 PL_markstack_ptr++; /* Protect mark. */
2870 PL_scopestack_ix += 1;
2871 /* sv_2cv is too complicated, try a simpler variant first: */
2872 if (!SvROK(PL_psig_ptr[sig]) || !(cv = MUTABLE_CV(SvRV(PL_psig_ptr[sig])))
2873 || SvTYPE(cv) != SVt_PVCV) {
2875 cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD);
2878 if (!cv || !CvROOT(cv)) {
2879 Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2880 PL_sig_name[sig], (gv ? GvENAME(gv)
2887 if(PL_psig_name[sig]) {
2888 sv = SvREFCNT_inc_NN(PL_psig_name[sig]);
2890 #if !defined(PERL_IMPLICIT_CONTEXT)
2894 sv = sv_newmortal();
2895 sv_setpv(sv,PL_sig_name[sig]);
2898 PUSHSTACKi(PERLSI_SIGNAL);
2901 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2903 struct sigaction oact;
2905 if (sigaction(sig, 0, &oact) == 0 && oact.sa_flags & SA_SIGINFO) {
2908 SV *rv = newRV_noinc(MUTABLE_SV(sih));
2909 /* The siginfo fields signo, code, errno, pid, uid,
2910 * addr, status, and band are defined by POSIX/SUSv3. */
2911 (void)hv_stores(sih, "signo", newSViv(sip->si_signo));
2912 (void)hv_stores(sih, "code", newSViv(sip->si_code));
2913 #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. */
2914 hv_stores(sih, "errno", newSViv(sip->si_errno));
2915 hv_stores(sih, "status", newSViv(sip->si_status));
2916 hv_stores(sih, "uid", newSViv(sip->si_uid));
2917 hv_stores(sih, "pid", newSViv(sip->si_pid));
2918 hv_stores(sih, "addr", newSVuv(PTR2UV(sip->si_addr)));
2919 hv_stores(sih, "band", newSViv(sip->si_band));
2923 mPUSHp((char *)sip, sizeof(*sip));
2931 call_sv(MUTABLE_SV(cv), G_DISCARD|G_EVAL);
2934 if (SvTRUE(ERRSV)) {
2936 #ifdef HAS_SIGPROCMASK
2937 /* Handler "died", for example to get out of a restart-able read().
2938 * Before we re-do that on its behalf re-enable the signal which was
2939 * blocked by the system when we entered.
2943 sigaddset(&set,sig);
2944 sigprocmask(SIG_UNBLOCK, &set, NULL);
2946 /* Not clear if this will work */
2947 (void)rsignal(sig, SIG_IGN);
2948 (void)rsignal(sig, PL_csighandlerp);
2950 #endif /* !PERL_MICRO */
2951 Perl_die(aTHX_ NULL);
2955 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2959 PL_scopestack_ix -= 1;
2962 PL_op = myop; /* Apparently not needed... */
2964 PL_Sv = tSv; /* Restore global temporaries. */
2971 S_restore_magic(pTHX_ const void *p)
2974 MGS* const mgs = SSPTR(PTR2IV(p), MGS*);
2975 SV* const sv = mgs->mgs_sv;
2980 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2982 #ifdef PERL_OLD_COPY_ON_WRITE
2983 /* While magic was saved (and off) sv_setsv may well have seen
2984 this SV as a prime candidate for COW. */
2986 sv_force_normal_flags(sv, 0);
2989 if (mgs->mgs_readonly)
2991 if (mgs->mgs_magical)
2992 SvFLAGS(sv) |= mgs->mgs_magical;
2995 if (SvGMAGICAL(sv)) {
2996 /* downgrade public flags to private,
2997 and discard any other private flags */
2999 const U32 pubflags = SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK);
3001 SvFLAGS(sv) &= ~( pubflags | (SVp_IOK|SVp_NOK|SVp_POK) );
3002 SvFLAGS(sv) |= ( pubflags << PRIVSHIFT );
3007 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
3009 /* If we're still on top of the stack, pop us off. (That condition
3010 * will be satisfied if restore_magic was called explicitly, but *not*
3011 * if it's being called via leave_scope.)
3012 * The reason for doing this is that otherwise, things like sv_2cv()
3013 * may leave alloc gunk on the savestack, and some code
3014 * (e.g. sighandler) doesn't expect that...
3016 if (PL_savestack_ix == mgs->mgs_ss_ix)
3018 I32 popval = SSPOPINT;
3019 assert(popval == SAVEt_DESTRUCTOR_X);
3020 PL_savestack_ix -= 2;
3022 assert(popval == SAVEt_ALLOC);
3024 PL_savestack_ix -= popval;
3030 S_unwind_handler_stack(pTHX_ const void *p)
3033 const U32 flags = *(const U32*)p;
3035 PERL_ARGS_ASSERT_UNWIND_HANDLER_STACK;
3038 PL_savestack_ix -= 5; /* Unprotect save in progress. */
3039 #if !defined(PERL_IMPLICIT_CONTEXT)
3041 SvREFCNT_dec(PL_sig_sv);
3046 =for apidoc magic_sethint
3048 Triggered by a store to %^H, records the key/value pair to
3049 C<PL_compiling.cop_hints_hash>. It is assumed that hints aren't storing
3050 anything that would need a deep copy. Maybe we should warn if we find a
3056 Perl_magic_sethint(pTHX_ SV *sv, MAGIC *mg)
3059 SV *key = (mg->mg_len == HEf_SVKEY) ? MUTABLE_SV(mg->mg_ptr)
3060 : newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
3062 PERL_ARGS_ASSERT_MAGIC_SETHINT;
3064 /* mg->mg_obj isn't being used. If needed, it would be possible to store
3065 an alternative leaf in there, with PL_compiling.cop_hints being used if
3066 it's NULL. If needed for threads, the alternative could lock a mutex,
3067 or take other more complex action. */
3069 /* Something changed in %^H, so it will need to be restored on scope exit.
3070 Doing this here saves a lot of doing it manually in perl code (and
3071 forgetting to do it, and consequent subtle errors. */
3072 PL_hints |= HINT_LOCALIZE_HH;
3073 PL_compiling.cop_hints_hash
3074 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash, key, sv);
3079 =for apidoc magic_clearhint
3081 Triggered by a delete from %^H, records the key to
3082 C<PL_compiling.cop_hints_hash>.
3087 Perl_magic_clearhint(pTHX_ SV *sv, MAGIC *mg)
3091 PERL_ARGS_ASSERT_MAGIC_CLEARHINT;
3092 PERL_UNUSED_ARG(sv);
3094 assert(mg->mg_len == HEf_SVKEY);
3096 PERL_UNUSED_ARG(sv);
3098 PL_hints |= HINT_LOCALIZE_HH;
3099 PL_compiling.cop_hints_hash
3100 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash,
3101 MUTABLE_SV(mg->mg_ptr), &PL_sv_placeholder);
3106 =for apidoc magic_clearhints
3108 Triggered by clearing %^H, resets C<PL_compiling.cop_hints_hash>.
3113 Perl_magic_clearhints(pTHX_ SV *sv, MAGIC *mg)
3115 PERL_ARGS_ASSERT_MAGIC_CLEARHINTS;
3116 PERL_UNUSED_ARG(sv);
3117 PERL_UNUSED_ARG(mg);
3118 if (PL_compiling.cop_hints_hash) {
3119 Perl_refcounted_he_free(aTHX_ PL_compiling.cop_hints_hash);
3120 PL_compiling.cop_hints_hash = NULL;
3127 * c-indentation-style: bsd
3129 * indent-tabs-mode: t
3132 * ex: set ts=8 sts=4 sw=4 noet: