3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 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."
17 =head1 Magical Functions
19 "Magic" is special data attached to SV structures in order to give them
20 "magical" properties. When any Perl code tries to read from, or assign to,
21 an SV marked as magical, it calls the 'get' or 'set' function associated
22 with that SV's magic. A get is called prior to reading an SV, in order to
23 give it a chance to update its internal value (get on $. writes the line
24 number of the last read filehandle into to the SV's IV slot), while
25 set is called after an SV has been written to, in order to allow it to make
26 use of its changed value (set on $/ copies the SV's new value to the
27 PL_rs global variable).
29 Magic is implemented as a linked list of MAGIC structures attached to the
30 SV. Each MAGIC struct holds the type of the magic, a pointer to an array
31 of functions that implement the get(), set(), length() etc functions,
32 plus space for some flags and pointers. For example, a tied variable has
33 a MAGIC structure that contains a pointer to the object associated with the
42 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
48 #if defined(HAS_SETGROUPS)
55 # include <sys/pstat.h>
58 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
59 Signal_t Perl_csighandler(int sig, siginfo_t *, void *);
61 Signal_t Perl_csighandler(int sig);
65 /* Missing protos on LynxOS */
66 void setruid(uid_t id);
67 void seteuid(uid_t id);
68 void setrgid(uid_t id);
69 void setegid(uid_t id);
73 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
81 /* MGS is typedef'ed to struct magic_state in perl.h */
84 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
89 PERL_ARGS_ASSERT_SAVE_MAGIC;
91 assert(SvMAGICAL(sv));
92 /* Turning READONLY off for a copy-on-write scalar (including shared
93 hash keys) is a bad idea. */
95 sv_force_normal_flags(sv, 0);
97 SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix));
99 mgs = SSPTR(mgs_ix, MGS*);
101 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
102 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
106 if (!(SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK))) {
107 /* No public flags are set, so promote any private flags to public. */
108 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
113 =for apidoc mg_magical
115 Turns on the magical status of an SV. See C<sv_magic>.
121 Perl_mg_magical(pTHX_ SV *sv)
124 PERL_ARGS_ASSERT_MG_MAGICAL;
126 if ((mg = SvMAGIC(sv))) {
129 const MGVTBL* const vtbl = mg->mg_virtual;
131 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
138 } while ((mg = mg->mg_moremagic));
139 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)))
145 /* is this container magic (%ENV, $1 etc), or value magic (pos, taint etc)? */
148 S_is_container_magic(const MAGIC *mg)
151 switch (mg->mg_type) {
154 case PERL_MAGIC_regex_global:
155 case PERL_MAGIC_nkeys:
156 #ifdef USE_LOCALE_COLLATE
157 case PERL_MAGIC_collxfrm:
160 case PERL_MAGIC_taint:
162 case PERL_MAGIC_vstring:
163 case PERL_MAGIC_utf8:
164 case PERL_MAGIC_substr:
165 case PERL_MAGIC_defelem:
166 case PERL_MAGIC_arylen:
168 case PERL_MAGIC_backref:
169 case PERL_MAGIC_arylen_p:
170 case PERL_MAGIC_rhash:
171 case PERL_MAGIC_symtab:
181 Do magic after a value is retrieved from the SV. See C<sv_magic>.
187 Perl_mg_get(pTHX_ SV *sv)
190 const I32 mgs_ix = SSNEW(sizeof(MGS));
191 const bool was_temp = (bool)SvTEMP(sv);
193 MAGIC *newmg, *head, *cur, *mg;
194 /* guard against sv having being freed midway by holding a private
197 PERL_ARGS_ASSERT_MG_GET;
199 /* sv_2mortal has this side effect of turning on the TEMP flag, which can
200 cause the SV's buffer to get stolen (and maybe other stuff).
203 sv_2mortal(SvREFCNT_inc_simple_NN(sv));
208 save_magic(mgs_ix, sv);
210 /* We must call svt_get(sv, mg) for each valid entry in the linked
211 list of magic. svt_get() may delete the current entry, add new
212 magic to the head of the list, or upgrade the SV. AMS 20010810 */
214 newmg = cur = head = mg = SvMAGIC(sv);
216 const MGVTBL * const vtbl = mg->mg_virtual;
218 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
219 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
221 /* guard against magic having been deleted - eg FETCH calling
226 /* Don't restore the flags for this entry if it was deleted. */
227 if (mg->mg_flags & MGf_GSKIP)
228 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
231 mg = mg->mg_moremagic;
234 /* Have we finished with the new entries we saw? Start again
235 where we left off (unless there are more new entries). */
243 /* Were any new entries added? */
244 if (!have_new && (newmg = SvMAGIC(sv)) != head) {
251 restore_magic(INT2PTR(void *, (IV)mgs_ix));
253 if (SvREFCNT(sv) == 1) {
254 /* We hold the last reference to this SV, which implies that the
255 SV was deleted as a side effect of the routines we called. */
264 Do magic after a value is assigned to the SV. See C<sv_magic>.
270 Perl_mg_set(pTHX_ SV *sv)
273 const I32 mgs_ix = SSNEW(sizeof(MGS));
277 PERL_ARGS_ASSERT_MG_SET;
279 save_magic(mgs_ix, sv);
281 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
282 const MGVTBL* vtbl = mg->mg_virtual;
283 nextmg = mg->mg_moremagic; /* it may delete itself */
284 if (mg->mg_flags & MGf_GSKIP) {
285 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
286 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
288 if (PL_localizing == 2 && !S_is_container_magic(mg))
290 if (vtbl && vtbl->svt_set)
291 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
294 restore_magic(INT2PTR(void*, (IV)mgs_ix));
299 =for apidoc mg_length
301 Report on the SV's length. See C<sv_magic>.
307 Perl_mg_length(pTHX_ SV *sv)
313 PERL_ARGS_ASSERT_MG_LENGTH;
315 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
316 const MGVTBL * const vtbl = mg->mg_virtual;
317 if (vtbl && vtbl->svt_len) {
318 const I32 mgs_ix = SSNEW(sizeof(MGS));
319 save_magic(mgs_ix, sv);
320 /* omit MGf_GSKIP -- not changed here */
321 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
322 restore_magic(INT2PTR(void*, (IV)mgs_ix));
328 /* You can't know whether it's UTF-8 until you get the string again...
330 const U8 *s = (U8*)SvPV_const(sv, len);
333 len = utf8_length(s, s + len);
340 Perl_mg_size(pTHX_ SV *sv)
344 PERL_ARGS_ASSERT_MG_SIZE;
346 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
347 const MGVTBL* const vtbl = mg->mg_virtual;
348 if (vtbl && vtbl->svt_len) {
349 const I32 mgs_ix = SSNEW(sizeof(MGS));
351 save_magic(mgs_ix, sv);
352 /* omit MGf_GSKIP -- not changed here */
353 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
354 restore_magic(INT2PTR(void*, (IV)mgs_ix));
361 return AvFILLp((AV *) sv); /* Fallback to non-tied array */
365 Perl_croak(aTHX_ "Size magic not implemented");
374 Clear something magical that the SV represents. See C<sv_magic>.
380 Perl_mg_clear(pTHX_ SV *sv)
382 const I32 mgs_ix = SSNEW(sizeof(MGS));
385 PERL_ARGS_ASSERT_MG_CLEAR;
387 save_magic(mgs_ix, sv);
389 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
390 const MGVTBL* const vtbl = mg->mg_virtual;
391 /* omit GSKIP -- never set here */
393 if (vtbl && vtbl->svt_clear)
394 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
397 restore_magic(INT2PTR(void*, (IV)mgs_ix));
404 Finds the magic pointer for type matching the SV. See C<sv_magic>.
410 Perl_mg_find(pTHX_ const SV *sv, int type)
415 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
416 if (mg->mg_type == type)
426 Copies the magic from one SV to another. See C<sv_magic>.
432 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
437 PERL_ARGS_ASSERT_MG_COPY;
439 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
440 const MGVTBL* const vtbl = mg->mg_virtual;
441 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
442 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
445 const char type = mg->mg_type;
446 if (isUPPER(type) && type != PERL_MAGIC_uvar) {
448 (type == PERL_MAGIC_tied)
450 : (type == PERL_MAGIC_regdata && mg->mg_obj)
453 toLOWER(type), key, klen);
462 =for apidoc mg_localize
464 Copy some of the magic from an existing SV to new localized version of
465 that SV. Container magic (eg %ENV, $1, tie) gets copied, value magic
466 doesn't (eg taint, pos).
472 Perl_mg_localize(pTHX_ SV *sv, SV *nsv)
477 PERL_ARGS_ASSERT_MG_LOCALIZE;
479 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
480 const MGVTBL* const vtbl = mg->mg_virtual;
481 if (!S_is_container_magic(mg))
484 if ((mg->mg_flags & MGf_LOCAL) && vtbl->svt_local)
485 (void)CALL_FPTR(vtbl->svt_local)(aTHX_ nsv, mg);
487 sv_magicext(nsv, mg->mg_obj, mg->mg_type, vtbl,
488 mg->mg_ptr, mg->mg_len);
490 /* container types should remain read-only across localization */
491 SvFLAGS(nsv) |= SvREADONLY(sv);
494 if (SvTYPE(nsv) >= SVt_PVMG && SvMAGIC(nsv)) {
495 SvFLAGS(nsv) |= SvMAGICAL(sv);
505 Free any magic storage used by the SV. See C<sv_magic>.
511 Perl_mg_free(pTHX_ SV *sv)
516 PERL_ARGS_ASSERT_MG_FREE;
518 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
519 const MGVTBL* const vtbl = mg->mg_virtual;
520 moremagic = mg->mg_moremagic;
521 if (vtbl && vtbl->svt_free)
522 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
523 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
524 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
525 Safefree(mg->mg_ptr);
526 else if (mg->mg_len == HEf_SVKEY)
527 SvREFCNT_dec((SV*)mg->mg_ptr);
529 if (mg->mg_flags & MGf_REFCOUNTED)
530 SvREFCNT_dec(mg->mg_obj);
532 SvMAGIC_set(sv, moremagic);
534 SvMAGIC_set(sv, NULL);
542 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
547 PERL_ARGS_ASSERT_MAGIC_REGDATA_CNT;
550 register const REGEXP * const rx = PM_GETRE(PL_curpm);
552 if (mg->mg_obj) { /* @+ */
553 /* return the number possible */
554 return RX_NPARENS(rx);
556 I32 paren = RX_LASTPAREN(rx);
558 /* return the last filled */
560 && (RX_OFFS(rx)[paren].start == -1
561 || RX_OFFS(rx)[paren].end == -1) )
572 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
576 PERL_ARGS_ASSERT_MAGIC_REGDATUM_GET;
579 register const REGEXP * const rx = PM_GETRE(PL_curpm);
581 register const I32 paren = mg->mg_len;
586 if (paren <= (I32)RX_NPARENS(rx) &&
587 (s = RX_OFFS(rx)[paren].start) != -1 &&
588 (t = RX_OFFS(rx)[paren].end) != -1)
591 if (mg->mg_obj) /* @+ */
596 if (i > 0 && RX_MATCH_UTF8(rx)) {
597 const char * const b = RX_SUBBEG(rx);
599 i = utf8_length((U8*)b, (U8*)(b+i));
610 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
612 PERL_ARGS_ASSERT_MAGIC_REGDATUM_SET;
615 Perl_croak(aTHX_ PL_no_modify);
616 NORETURN_FUNCTION_END;
620 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
625 register const REGEXP * rx;
626 const char * const remaining = mg->mg_ptr + 1;
628 PERL_ARGS_ASSERT_MAGIC_LEN;
630 switch (*mg->mg_ptr) {
632 if (*remaining == '\0') { /* ^P */
634 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
636 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
640 case '\015': /* $^MATCH */
641 if (strEQ(remaining, "ATCH")) {
648 paren = RX_BUFF_IDX_PREMATCH;
652 paren = RX_BUFF_IDX_POSTMATCH;
656 paren = RX_BUFF_IDX_FULLMATCH;
658 case '1': case '2': case '3': case '4':
659 case '5': case '6': case '7': case '8': case '9':
660 paren = atoi(mg->mg_ptr);
662 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
664 i = CALLREG_NUMBUF_LENGTH((REGEXP * const)rx, sv, paren);
667 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
670 if (ckWARN(WARN_UNINITIALIZED))
675 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
676 paren = RX_LASTPAREN(rx);
681 case '\016': /* ^N */
682 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
683 paren = RX_LASTCLOSEPAREN(rx);
690 if (!SvPOK(sv) && SvNIOK(sv)) {
698 #define SvRTRIM(sv) STMT_START { \
700 STRLEN len = SvCUR(sv); \
701 char * const p = SvPVX(sv); \
702 while (len > 0 && isSPACE(p[len-1])) \
704 SvCUR_set(sv, len); \
710 Perl_emulate_cop_io(pTHX_ const COP *const c, SV *const sv)
712 PERL_ARGS_ASSERT_EMULATE_COP_IO;
714 if (!(CopHINTS_get(c) & (HINT_LEXICAL_IO_IN|HINT_LEXICAL_IO_OUT)))
715 sv_setsv(sv, &PL_sv_undef);
719 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_IN)) {
720 SV *const value = Perl_refcounted_he_fetch(aTHX_
722 0, "open<", 5, 0, 0);
727 if ((CopHINTS_get(c) & HINT_LEXICAL_IO_OUT)) {
728 SV *const value = Perl_refcounted_he_fetch(aTHX_
730 0, "open>", 5, 0, 0);
738 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
742 register char *s = NULL;
744 const char * const remaining = mg->mg_ptr + 1;
745 const char nextchar = *remaining;
747 PERL_ARGS_ASSERT_MAGIC_GET;
749 switch (*mg->mg_ptr) {
750 case '\001': /* ^A */
751 sv_setsv(sv, PL_bodytarget);
753 case '\003': /* ^C, ^CHILD_ERROR_NATIVE */
754 if (nextchar == '\0') {
755 sv_setiv(sv, (IV)PL_minus_c);
757 else if (strEQ(remaining, "HILD_ERROR_NATIVE")) {
758 sv_setiv(sv, (IV)STATUS_NATIVE);
762 case '\004': /* ^D */
763 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
765 case '\005': /* ^E */
766 if (nextchar == '\0') {
767 #if defined(MACOS_TRADITIONAL)
771 sv_setnv(sv,(double)gMacPerl_OSErr);
772 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
776 # include <descrip.h>
777 # include <starlet.h>
779 $DESCRIPTOR(msgdsc,msg);
780 sv_setnv(sv,(NV) vaxc$errno);
781 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
782 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
787 if (!(_emx_env & 0x200)) { /* Under DOS */
788 sv_setnv(sv, (NV)errno);
789 sv_setpv(sv, errno ? Strerror(errno) : "");
791 if (errno != errno_isOS2) {
792 const int tmp = _syserrno();
793 if (tmp) /* 2nd call to _syserrno() makes it 0 */
796 sv_setnv(sv, (NV)Perl_rc);
797 sv_setpv(sv, os2error(Perl_rc));
801 const DWORD dwErr = GetLastError();
802 sv_setnv(sv, (NV)dwErr);
804 PerlProc_GetOSError(sv, dwErr);
807 sv_setpvn(sv, "", 0);
812 const int saveerrno = errno;
813 sv_setnv(sv, (NV)errno);
814 sv_setpv(sv, errno ? Strerror(errno) : "");
819 SvNOK_on(sv); /* what a wonderful hack! */
821 else if (strEQ(remaining, "NCODING"))
822 sv_setsv(sv, PL_encoding);
824 case '\006': /* ^F */
825 sv_setiv(sv, (IV)PL_maxsysfd);
827 case '\010': /* ^H */
828 sv_setiv(sv, (IV)PL_hints);
830 case '\011': /* ^I */ /* NOT \t in EBCDIC */
831 sv_setpv(sv, PL_inplace); /* Will undefine sv if PL_inplace is NULL */
833 case '\017': /* ^O & ^OPEN */
834 if (nextchar == '\0') {
835 sv_setpv(sv, PL_osname);
838 else if (strEQ(remaining, "PEN")) {
839 Perl_emulate_cop_io(aTHX_ &PL_compiling, sv);
843 if (nextchar == '\0') { /* ^P */
844 sv_setiv(sv, (IV)PL_perldb);
845 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
846 goto do_prematch_fetch;
847 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
848 goto do_postmatch_fetch;
851 case '\023': /* ^S */
852 if (nextchar == '\0') {
853 if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING)
856 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
861 case '\024': /* ^T */
862 if (nextchar == '\0') {
864 sv_setnv(sv, PL_basetime);
866 sv_setiv(sv, (IV)PL_basetime);
869 else if (strEQ(remaining, "AINT"))
870 sv_setiv(sv, PL_tainting
871 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
874 case '\025': /* $^UNICODE, $^UTF8LOCALE, $^UTF8CACHE */
875 if (strEQ(remaining, "NICODE"))
876 sv_setuv(sv, (UV) PL_unicode);
877 else if (strEQ(remaining, "TF8LOCALE"))
878 sv_setuv(sv, (UV) PL_utf8locale);
879 else if (strEQ(remaining, "TF8CACHE"))
880 sv_setiv(sv, (IV) PL_utf8cache);
882 case '\027': /* ^W & $^WARNING_BITS */
883 if (nextchar == '\0')
884 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
885 else if (strEQ(remaining, "ARNING_BITS")) {
886 if (PL_compiling.cop_warnings == pWARN_NONE) {
887 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
889 else if (PL_compiling.cop_warnings == pWARN_STD) {
892 (PL_dowarn & G_WARN_ON) ? WARN_ALLstring : WARN_NONEstring,
896 else if (PL_compiling.cop_warnings == pWARN_ALL) {
897 /* Get the bit mask for $warnings::Bits{all}, because
898 * it could have been extended by warnings::register */
899 HV * const bits=get_hv("warnings::Bits", FALSE);
901 SV ** const bits_all = hv_fetchs(bits, "all", FALSE);
903 sv_setsv(sv, *bits_all);
906 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
910 sv_setpvn(sv, (char *) (PL_compiling.cop_warnings + 1),
911 *PL_compiling.cop_warnings);
916 case '\015': /* $^MATCH */
917 if (strEQ(remaining, "ATCH")) {
918 case '1': case '2': case '3': case '4':
919 case '5': case '6': case '7': case '8': case '9': case '&':
920 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
922 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
923 * XXX Does the new way break anything?
925 paren = atoi(mg->mg_ptr); /* $& is in [0] */
926 CALLREG_NUMBUF_FETCH(rx,paren,sv);
929 sv_setsv(sv,&PL_sv_undef);
933 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
934 if (RX_LASTPAREN(rx)) {
935 CALLREG_NUMBUF_FETCH(rx,RX_LASTPAREN(rx),sv);
939 sv_setsv(sv,&PL_sv_undef);
941 case '\016': /* ^N */
942 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
943 if (RX_LASTCLOSEPAREN(rx)) {
944 CALLREG_NUMBUF_FETCH(rx,RX_LASTCLOSEPAREN(rx),sv);
949 sv_setsv(sv,&PL_sv_undef);
953 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
954 CALLREG_NUMBUF_FETCH(rx,-2,sv);
957 sv_setsv(sv,&PL_sv_undef);
961 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
962 CALLREG_NUMBUF_FETCH(rx,-1,sv);
965 sv_setsv(sv,&PL_sv_undef);
968 if (GvIO(PL_last_in_gv)) {
969 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
974 sv_setiv(sv, (IV)STATUS_CURRENT);
975 #ifdef COMPLEX_STATUS
976 LvTARGOFF(sv) = PL_statusvalue;
977 LvTARGLEN(sv) = PL_statusvalue_vms;
982 if (GvIOp(PL_defoutgv))
983 s = IoTOP_NAME(GvIOp(PL_defoutgv));
987 sv_setpv(sv,GvENAME(PL_defoutgv));
988 sv_catpvs(sv,"_TOP");
992 if (GvIOp(PL_defoutgv))
993 s = IoFMT_NAME(GvIOp(PL_defoutgv));
995 s = GvENAME(PL_defoutgv);
999 if (GvIOp(PL_defoutgv))
1000 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
1003 if (GvIOp(PL_defoutgv))
1004 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
1007 if (GvIOp(PL_defoutgv))
1008 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
1015 sv_setiv(sv, (IV)CopARYBASE_get(PL_curcop));
1018 if (GvIOp(PL_defoutgv))
1019 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
1025 sv_copypv(sv, PL_ors_sv);
1029 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
1030 sv_setpv(sv, errno ? Strerror(errno) : "");
1033 const int saveerrno = errno;
1034 sv_setnv(sv, (NV)errno);
1036 if (errno == errno_isOS2 || errno == errno_isOS2_set)
1037 sv_setpv(sv, os2error(Perl_rc));
1040 sv_setpv(sv, errno ? Strerror(errno) : "");
1045 SvNOK_on(sv); /* what a wonderful hack! */
1048 sv_setiv(sv, (IV)PL_uid);
1051 sv_setiv(sv, (IV)PL_euid);
1054 sv_setiv(sv, (IV)PL_gid);
1057 sv_setiv(sv, (IV)PL_egid);
1059 #ifdef HAS_GETGROUPS
1061 Groups_t *gary = NULL;
1062 I32 i, num_groups = getgroups(0, gary);
1063 Newx(gary, num_groups, Groups_t);
1064 num_groups = getgroups(num_groups, gary);
1065 for (i = 0; i < num_groups; i++)
1066 Perl_sv_catpvf(aTHX_ sv, " %"IVdf, (IV)gary[i]);
1069 (void)SvIOK_on(sv); /* what a wonderful hack! */
1072 #ifndef MACOS_TRADITIONAL
1081 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
1083 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
1085 PERL_ARGS_ASSERT_MAGIC_GETUVAR;
1087 if (uf && uf->uf_val)
1088 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
1093 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
1096 STRLEN len = 0, klen;
1097 const char *s = SvOK(sv) ? SvPV_const(sv,len) : "";
1098 const char * const ptr = MgPV_const(mg,klen);
1101 PERL_ARGS_ASSERT_MAGIC_SETENV;
1103 #ifdef DYNAMIC_ENV_FETCH
1104 /* We just undefd an environment var. Is a replacement */
1105 /* waiting in the wings? */
1107 SV ** const valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE);
1109 s = SvOK(*valp) ? SvPV_const(*valp, len) : "";
1113 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
1114 /* And you'll never guess what the dog had */
1115 /* in its mouth... */
1117 MgTAINTEDDIR_off(mg);
1119 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1120 char pathbuf[256], eltbuf[256], *cp, *elt;
1124 my_strlcpy(eltbuf, s, sizeof(eltbuf));
1126 do { /* DCL$PATH may be a search list */
1127 while (1) { /* as may dev portion of any element */
1128 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
1129 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
1130 cando_by_name(S_IWUSR,0,elt) ) {
1131 MgTAINTEDDIR_on(mg);
1135 if ((cp = strchr(elt, ':')) != NULL)
1137 if (my_trnlnm(elt, eltbuf, j++))
1143 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
1146 if (s && klen == 4 && strEQ(ptr,"PATH")) {
1147 const char * const strend = s + len;
1149 while (s < strend) {
1153 #ifdef VMS /* Hmm. How do we get $Config{path_sep} from C? */
1154 const char path_sep = '|';
1156 const char path_sep = ':';
1158 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
1159 s, strend, path_sep, &i);
1161 if (i >= (I32)sizeof tmpbuf /* too long -- assume the worst */
1163 || !strchr(tmpbuf, ':') /* no colon thus no device name -- assume relative path */
1165 || *tmpbuf != '/' /* no starting slash -- assume relative path */
1167 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
1168 MgTAINTEDDIR_on(mg);
1174 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1180 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
1182 PERL_ARGS_ASSERT_MAGIC_CLEARENV;
1183 PERL_UNUSED_ARG(sv);
1184 my_setenv(MgPV_nolen_const(mg),NULL);
1189 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1192 PERL_ARGS_ASSERT_MAGIC_SET_ALL_ENV;
1193 PERL_UNUSED_ARG(mg);
1195 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1197 if (PL_localizing) {
1200 hv_iterinit((HV*)sv);
1201 while ((entry = hv_iternext((HV*)sv))) {
1203 my_setenv(hv_iterkey(entry, &keylen),
1204 SvPV_nolen_const(hv_iterval((HV*)sv, entry)));
1212 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1215 PERL_ARGS_ASSERT_MAGIC_CLEAR_ALL_ENV;
1216 PERL_UNUSED_ARG(sv);
1217 PERL_UNUSED_ARG(mg);
1219 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1227 #ifdef HAS_SIGPROCMASK
1229 restore_sigmask(pTHX_ SV *save_sv)
1231 const sigset_t * const ossetp = (const sigset_t *) SvPV_nolen_const( save_sv );
1232 (void)sigprocmask(SIG_SETMASK, ossetp, NULL);
1236 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1239 /* Are we fetching a signal entry? */
1240 const I32 i = whichsig(MgPV_nolen_const(mg));
1242 PERL_ARGS_ASSERT_MAGIC_GETSIG;
1246 sv_setsv(sv,PL_psig_ptr[i]);
1248 Sighandler_t sigstate = rsignal_state(i);
1249 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1250 if (PL_sig_handlers_initted && PL_sig_ignoring[i])
1253 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1254 if (PL_sig_handlers_initted && PL_sig_defaulting[i])
1257 /* cache state so we don't fetch it again */
1258 if(sigstate == (Sighandler_t) SIG_IGN)
1259 sv_setpvs(sv,"IGNORE");
1261 sv_setsv(sv,&PL_sv_undef);
1262 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1269 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1271 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1272 * refactoring might be in order.
1275 register const char * const s = MgPV_nolen_const(mg);
1276 PERL_ARGS_ASSERT_MAGIC_CLEARSIG;
1277 PERL_UNUSED_ARG(sv);
1280 if (strEQ(s,"__DIE__"))
1282 else if (strEQ(s,"__WARN__") && PL_warnhook != PERL_WARNHOOK_FATAL)
1285 SV *const to_dec = *svp;
1287 SvREFCNT_dec(to_dec);
1291 /* Are we clearing a signal entry? */
1292 const I32 i = whichsig(s);
1294 #ifdef HAS_SIGPROCMASK
1297 /* Avoid having the signal arrive at a bad time, if possible. */
1300 sigprocmask(SIG_BLOCK, &set, &save);
1302 save_sv = newSVpvn((char *)(&save), sizeof(sigset_t));
1303 SAVEFREESV(save_sv);
1304 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1307 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1308 if (!PL_sig_handlers_initted) Perl_csighandler_init();
1310 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1311 PL_sig_defaulting[i] = 1;
1312 (void)rsignal(i, PL_csighandlerp);
1314 (void)rsignal(i, (Sighandler_t) SIG_DFL);
1316 if(PL_psig_name[i]) {
1317 SvREFCNT_dec(PL_psig_name[i]);
1320 if(PL_psig_ptr[i]) {
1321 SV * const to_dec=PL_psig_ptr[i];
1324 SvREFCNT_dec(to_dec);
1334 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1335 Perl_csighandler(int sig, siginfo_t *sip PERL_UNUSED_DECL, void *uap PERL_UNUSED_DECL)
1337 Perl_csighandler(int sig)
1340 #ifdef PERL_GET_SIG_CONTEXT
1341 dTHXa(PERL_GET_SIG_CONTEXT);
1345 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1346 (void) rsignal(sig, PL_csighandlerp);
1347 if (PL_sig_ignoring[sig]) return;
1349 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1350 if (PL_sig_defaulting[sig])
1351 #ifdef KILL_BY_SIGPRC
1352 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1367 (PL_signals & PERL_SIGNALS_UNSAFE_FLAG))
1368 /* Call the perl level handler now--
1369 * with risk we may be in malloc() etc. */
1370 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1371 (*PL_sighandlerp)(sig, NULL, NULL);
1373 (*PL_sighandlerp)(sig);
1376 /* Set a flag to say this signal is pending, that is awaiting delivery after
1377 * the current Perl opcode completes */
1378 PL_psig_pend[sig]++;
1380 #ifndef SIG_PENDING_DIE_COUNT
1381 # define SIG_PENDING_DIE_COUNT 120
1383 /* And one to say _a_ signal is pending */
1384 if (++PL_sig_pending >= SIG_PENDING_DIE_COUNT)
1385 Perl_croak(aTHX_ "Maximal count of pending signals (%lu) exceeded",
1386 (unsigned long)SIG_PENDING_DIE_COUNT);
1390 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1392 Perl_csighandler_init(void)
1395 if (PL_sig_handlers_initted) return;
1397 for (sig = 1; sig < SIG_SIZE; sig++) {
1398 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1400 PL_sig_defaulting[sig] = 1;
1401 (void) rsignal(sig, PL_csighandlerp);
1403 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1404 PL_sig_ignoring[sig] = 0;
1407 PL_sig_handlers_initted = 1;
1412 Perl_despatch_signals(pTHX)
1417 for (sig = 1; sig < SIG_SIZE; sig++) {
1418 if (PL_psig_pend[sig]) {
1419 PERL_BLOCKSIG_ADD(set, sig);
1420 PL_psig_pend[sig] = 0;
1421 PERL_BLOCKSIG_BLOCK(set);
1422 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1423 (*PL_sighandlerp)(sig, NULL, NULL);
1425 (*PL_sighandlerp)(sig);
1427 PERL_BLOCKSIG_UNBLOCK(set);
1433 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1438 /* Need to be careful with SvREFCNT_dec(), because that can have side
1439 * effects (due to closures). We must make sure that the new disposition
1440 * is in place before it is called.
1444 #ifdef HAS_SIGPROCMASK
1448 register const char *s = MgPV_const(mg,len);
1450 PERL_ARGS_ASSERT_MAGIC_SETSIG;
1453 if (strEQ(s,"__DIE__"))
1455 else if (strEQ(s,"__WARN__"))
1458 Perl_croak(aTHX_ "No such hook: %s", s);
1461 if (*svp != PERL_WARNHOOK_FATAL)
1467 i = whichsig(s); /* ...no, a brick */
1469 if (ckWARN(WARN_SIGNAL))
1470 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1473 #ifdef HAS_SIGPROCMASK
1474 /* Avoid having the signal arrive at a bad time, if possible. */
1477 sigprocmask(SIG_BLOCK, &set, &save);
1479 save_sv = newSVpvn((char *)(&save), sizeof(sigset_t));
1480 SAVEFREESV(save_sv);
1481 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1484 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1485 if (!PL_sig_handlers_initted) Perl_csighandler_init();
1487 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1488 PL_sig_ignoring[i] = 0;
1490 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1491 PL_sig_defaulting[i] = 0;
1493 SvREFCNT_dec(PL_psig_name[i]);
1494 to_dec = PL_psig_ptr[i];
1495 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1496 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1497 PL_psig_name[i] = newSVpvn(s, len);
1498 SvREADONLY_on(PL_psig_name[i]);
1500 if (isGV_with_GP(sv) || SvROK(sv)) {
1502 (void)rsignal(i, PL_csighandlerp);
1503 #ifdef HAS_SIGPROCMASK
1508 *svp = SvREFCNT_inc_simple_NN(sv);
1510 SvREFCNT_dec(to_dec);
1513 s = SvOK(sv) ? SvPV_force(sv,len) : "DEFAULT";
1514 if (strEQ(s,"IGNORE")) {
1516 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1517 PL_sig_ignoring[i] = 1;
1518 (void)rsignal(i, PL_csighandlerp);
1520 (void)rsignal(i, (Sighandler_t) SIG_IGN);
1524 else if (strEQ(s,"DEFAULT") || !*s) {
1526 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1528 PL_sig_defaulting[i] = 1;
1529 (void)rsignal(i, PL_csighandlerp);
1532 (void)rsignal(i, (Sighandler_t) SIG_DFL);
1537 * We should warn if HINT_STRICT_REFS, but without
1538 * access to a known hint bit in a known OP, we can't
1539 * tell whether HINT_STRICT_REFS is in force or not.
1541 if (!strchr(s,':') && !strchr(s,'\''))
1542 Perl_sv_insert_flags(aTHX_ sv, 0, 0, STR_WITH_LEN("main::"),
1545 (void)rsignal(i, PL_csighandlerp);
1547 *svp = SvREFCNT_inc_simple_NN(sv);
1549 #ifdef HAS_SIGPROCMASK
1554 SvREFCNT_dec(to_dec);
1557 #endif /* !PERL_MICRO */
1560 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1565 PERL_ARGS_ASSERT_MAGIC_SETISA;
1566 PERL_UNUSED_ARG(sv);
1568 /* Bail out if destruction is going on */
1569 if(PL_dirty) return 0;
1571 /* Skip _isaelem because _isa will handle it shortly */
1572 if (PL_delaymagic & DM_ARRAY && mg->mg_type == PERL_MAGIC_isaelem)
1575 /* XXX Once it's possible, we need to
1576 detect that our @ISA is aliased in
1577 other stashes, and act on the stashes
1578 of all of the aliases */
1580 /* The first case occurs via setisa,
1581 the second via setisa_elem, which
1582 calls this same magic */
1584 SvTYPE(mg->mg_obj) == SVt_PVGV
1586 : (GV*)mg_find(mg->mg_obj, PERL_MAGIC_isa)->mg_obj
1590 mro_isa_changed_in(stash);
1596 Perl_magic_clearisa(pTHX_ SV *sv, MAGIC *mg)
1601 PERL_ARGS_ASSERT_MAGIC_CLEARISA;
1603 /* Bail out if destruction is going on */
1604 if(PL_dirty) return 0;
1608 /* XXX see comments in magic_setisa */
1610 SvTYPE(mg->mg_obj) == SVt_PVGV
1612 : (GV*)mg_find(mg->mg_obj, PERL_MAGIC_isa)->mg_obj
1616 mro_isa_changed_in(stash);
1622 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1625 PERL_ARGS_ASSERT_MAGIC_SETAMAGIC;
1626 PERL_UNUSED_ARG(sv);
1627 PERL_UNUSED_ARG(mg);
1628 PL_amagic_generation++;
1634 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1636 HV * const hv = (HV*)LvTARG(sv);
1639 PERL_ARGS_ASSERT_MAGIC_GETNKEYS;
1640 PERL_UNUSED_ARG(mg);
1643 (void) hv_iterinit(hv);
1644 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1647 while (hv_iternext(hv))
1652 sv_setiv(sv, (IV)i);
1657 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1659 PERL_ARGS_ASSERT_MAGIC_SETNKEYS;
1660 PERL_UNUSED_ARG(mg);
1662 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1667 /* caller is responsible for stack switching/cleanup */
1669 S_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, const char *meth, I32 flags, int n, SV *val)
1674 PERL_ARGS_ASSERT_MAGIC_METHCALL;
1678 PUSHs(SvTIED_obj(sv, mg));
1681 if (mg->mg_len >= 0)
1682 mPUSHp(mg->mg_ptr, mg->mg_len);
1683 else if (mg->mg_len == HEf_SVKEY)
1684 PUSHs((SV*)mg->mg_ptr);
1686 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1695 return call_method(meth, flags);
1699 S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth)
1703 PERL_ARGS_ASSERT_MAGIC_METHPACK;
1707 PUSHSTACKi(PERLSI_MAGIC);
1709 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1710 sv_setsv(sv, *PL_stack_sp--);
1720 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1722 PERL_ARGS_ASSERT_MAGIC_GETPACK;
1725 mg->mg_flags |= MGf_GSKIP;
1726 magic_methpack(sv,mg,"FETCH");
1731 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1735 PERL_ARGS_ASSERT_MAGIC_SETPACK;
1738 PUSHSTACKi(PERLSI_MAGIC);
1739 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1746 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1748 PERL_ARGS_ASSERT_MAGIC_CLEARPACK;
1750 return magic_methpack(sv,mg,"DELETE");
1755 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1760 PERL_ARGS_ASSERT_MAGIC_SIZEPACK;
1764 PUSHSTACKi(PERLSI_MAGIC);
1765 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1766 sv = *PL_stack_sp--;
1767 retval = SvIV(sv)-1;
1769 Perl_croak(aTHX_ "FETCHSIZE returned a negative value");
1774 return (U32) retval;
1778 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1782 PERL_ARGS_ASSERT_MAGIC_WIPEPACK;
1785 PUSHSTACKi(PERLSI_MAGIC);
1787 XPUSHs(SvTIED_obj(sv, mg));
1789 call_method("CLEAR", G_SCALAR|G_DISCARD);
1797 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1800 const char * const meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1802 PERL_ARGS_ASSERT_MAGIC_NEXTPACK;
1806 PUSHSTACKi(PERLSI_MAGIC);
1809 PUSHs(SvTIED_obj(sv, mg));
1814 if (call_method(meth, G_SCALAR))
1815 sv_setsv(key, *PL_stack_sp--);
1824 Perl_magic_existspack(pTHX_ SV *sv, const MAGIC *mg)
1826 PERL_ARGS_ASSERT_MAGIC_EXISTSPACK;
1828 return magic_methpack(sv,mg,"EXISTS");
1832 Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1836 SV * const tied = SvTIED_obj((SV*)hv, mg);
1837 HV * const pkg = SvSTASH((SV*)SvRV(tied));
1839 PERL_ARGS_ASSERT_MAGIC_SCALARPACK;
1841 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1843 if (HvEITER_get(hv))
1844 /* we are in an iteration so the hash cannot be empty */
1846 /* no xhv_eiter so now use FIRSTKEY */
1847 key = sv_newmortal();
1848 magic_nextpack((SV*)hv, mg, key);
1849 HvEITER_set(hv, NULL); /* need to reset iterator */
1850 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1853 /* there is a SCALAR method that we can call */
1855 PUSHSTACKi(PERLSI_MAGIC);
1861 if (call_method("SCALAR", G_SCALAR))
1862 retval = *PL_stack_sp--;
1864 retval = &PL_sv_undef;
1871 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1874 GV * const gv = PL_DBline;
1875 const I32 i = SvTRUE(sv);
1876 SV ** const svp = av_fetch(GvAV(gv),
1877 atoi(MgPV_nolen_const(mg)), FALSE);
1879 PERL_ARGS_ASSERT_MAGIC_SETDBLINE;
1881 if (svp && SvIOKp(*svp)) {
1882 OP * const o = INT2PTR(OP*,SvIVX(*svp));
1884 /* set or clear breakpoint in the relevant control op */
1886 o->op_flags |= OPf_SPECIAL;
1888 o->op_flags &= ~OPf_SPECIAL;
1895 Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg)
1898 const AV * const obj = (AV*)mg->mg_obj;
1900 PERL_ARGS_ASSERT_MAGIC_GETARYLEN;
1903 sv_setiv(sv, AvFILL(obj) + CopARYBASE_get(PL_curcop));
1911 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1914 AV * const obj = (AV*)mg->mg_obj;
1916 PERL_ARGS_ASSERT_MAGIC_SETARYLEN;
1919 av_fill(obj, SvIV(sv) - CopARYBASE_get(PL_curcop));
1921 if (ckWARN(WARN_MISC))
1922 Perl_warner(aTHX_ packWARN(WARN_MISC),
1923 "Attempt to set length of freed array");
1929 Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
1933 PERL_ARGS_ASSERT_MAGIC_FREEARYLEN_P;
1934 PERL_UNUSED_ARG(sv);
1936 /* during global destruction, mg_obj may already have been freed */
1937 if (PL_in_clean_all)
1940 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
1943 /* arylen scalar holds a pointer back to the array, but doesn't own a
1944 reference. Hence the we (the array) are about to go away with it
1945 still pointing at us. Clear its pointer, else it would be pointing
1946 at free memory. See the comment in sv_magic about reference loops,
1947 and why it can't own a reference to us. */
1954 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1957 SV* const lsv = LvTARG(sv);
1959 PERL_ARGS_ASSERT_MAGIC_GETPOS;
1960 PERL_UNUSED_ARG(mg);
1962 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1963 MAGIC * const found = mg_find(lsv, PERL_MAGIC_regex_global);
1964 if (found && found->mg_len >= 0) {
1965 I32 i = found->mg_len;
1967 sv_pos_b2u(lsv, &i);
1968 sv_setiv(sv, i + CopARYBASE_get(PL_curcop));
1977 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1980 SV* const lsv = LvTARG(sv);
1986 PERL_ARGS_ASSERT_MAGIC_SETPOS;
1987 PERL_UNUSED_ARG(mg);
1989 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1990 found = mg_find(lsv, PERL_MAGIC_regex_global);
1996 #ifdef PERL_OLD_COPY_ON_WRITE
1998 sv_force_normal_flags(lsv, 0);
2000 found = sv_magicext(lsv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
2003 else if (!SvOK(sv)) {
2007 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
2009 pos = SvIV(sv) - CopARYBASE_get(PL_curcop);
2012 ulen = sv_len_utf8(lsv);
2022 else if (pos > (SSize_t)len)
2027 sv_pos_u2b(lsv, &p, 0);
2031 found->mg_len = pos;
2032 found->mg_flags &= ~MGf_MINMATCH;
2038 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
2041 SV * const lsv = LvTARG(sv);
2042 const char * const tmps = SvPV_const(lsv,len);
2043 I32 offs = LvTARGOFF(sv);
2044 I32 rem = LvTARGLEN(sv);
2046 PERL_ARGS_ASSERT_MAGIC_GETSUBSTR;
2047 PERL_UNUSED_ARG(mg);
2050 sv_pos_u2b(lsv, &offs, &rem);
2051 if (offs > (I32)len)
2053 if (rem + offs > (I32)len)
2055 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
2062 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
2066 const char * const tmps = SvPV_const(sv, len);
2067 SV * const lsv = LvTARG(sv);
2068 I32 lvoff = LvTARGOFF(sv);
2069 I32 lvlen = LvTARGLEN(sv);
2071 PERL_ARGS_ASSERT_MAGIC_SETSUBSTR;
2072 PERL_UNUSED_ARG(mg);
2075 sv_utf8_upgrade(lsv);
2076 sv_pos_u2b(lsv, &lvoff, &lvlen);
2077 sv_insert(lsv, lvoff, lvlen, tmps, len);
2078 LvTARGLEN(sv) = sv_len_utf8(sv);
2081 else if (lsv && SvUTF8(lsv)) {
2083 sv_pos_u2b(lsv, &lvoff, &lvlen);
2084 LvTARGLEN(sv) = len;
2085 utf8 = (char*)bytes_to_utf8((U8*)tmps, &len);
2086 sv_insert(lsv, lvoff, lvlen, utf8, len);
2090 sv_insert(lsv, lvoff, lvlen, tmps, len);
2091 LvTARGLEN(sv) = len;
2099 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
2103 PERL_ARGS_ASSERT_MAGIC_GETTAINT;
2104 PERL_UNUSED_ARG(sv);
2106 TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1));
2111 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
2115 PERL_ARGS_ASSERT_MAGIC_SETTAINT;
2116 PERL_UNUSED_ARG(sv);
2118 /* update taint status */
2127 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
2129 SV * const lsv = LvTARG(sv);
2131 PERL_ARGS_ASSERT_MAGIC_GETVEC;
2132 PERL_UNUSED_ARG(mg);
2135 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
2143 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
2145 PERL_ARGS_ASSERT_MAGIC_SETVEC;
2146 PERL_UNUSED_ARG(mg);
2147 do_vecset(sv); /* XXX slurp this routine */
2152 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
2157 PERL_ARGS_ASSERT_MAGIC_GETDEFELEM;
2159 if (LvTARGLEN(sv)) {
2161 SV * const ahv = LvTARG(sv);
2162 HE * const he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
2167 AV* const av = (AV*)LvTARG(sv);
2168 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
2169 targ = AvARRAY(av)[LvTARGOFF(sv)];
2171 if (targ && (targ != &PL_sv_undef)) {
2172 /* somebody else defined it for us */
2173 SvREFCNT_dec(LvTARG(sv));
2174 LvTARG(sv) = SvREFCNT_inc_simple_NN(targ);
2176 SvREFCNT_dec(mg->mg_obj);
2178 mg->mg_flags &= ~MGf_REFCOUNTED;
2183 sv_setsv(sv, targ ? targ : &PL_sv_undef);
2188 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
2190 PERL_ARGS_ASSERT_MAGIC_SETDEFELEM;
2191 PERL_UNUSED_ARG(mg);
2195 sv_setsv(LvTARG(sv), sv);
2196 SvSETMAGIC(LvTARG(sv));
2202 Perl_vivify_defelem(pTHX_ SV *sv)
2208 PERL_ARGS_ASSERT_VIVIFY_DEFELEM;
2210 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
2213 SV * const ahv = LvTARG(sv);
2214 HE * const he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
2217 if (!value || value == &PL_sv_undef)
2218 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(mg->mg_obj));
2221 AV* const av = (AV*)LvTARG(sv);
2222 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
2223 LvTARG(sv) = NULL; /* array can't be extended */
2225 SV* const * const svp = av_fetch(av, LvTARGOFF(sv), TRUE);
2226 if (!svp || (value = *svp) == &PL_sv_undef)
2227 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
2230 SvREFCNT_inc_simple_void(value);
2231 SvREFCNT_dec(LvTARG(sv));
2234 SvREFCNT_dec(mg->mg_obj);
2236 mg->mg_flags &= ~MGf_REFCOUNTED;
2240 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
2242 PERL_ARGS_ASSERT_MAGIC_KILLBACKREFS;
2243 return Perl_sv_kill_backrefs(aTHX_ sv, (AV*)mg->mg_obj);
2247 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
2249 PERL_ARGS_ASSERT_MAGIC_SETMGLOB;
2250 PERL_UNUSED_CONTEXT;
2257 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
2259 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
2261 PERL_ARGS_ASSERT_MAGIC_SETUVAR;
2263 if (uf && uf->uf_set)
2264 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
2269 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2271 const char type = mg->mg_type;
2273 PERL_ARGS_ASSERT_MAGIC_SETREGEXP;
2275 if (type == PERL_MAGIC_qr) {
2276 } else if (type == PERL_MAGIC_bm) {
2280 assert(type == PERL_MAGIC_fm);
2283 return sv_unmagic(sv, type);
2286 #ifdef USE_LOCALE_COLLATE
2288 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
2290 PERL_ARGS_ASSERT_MAGIC_SETCOLLXFRM;
2293 * RenE<eacute> Descartes said "I think not."
2294 * and vanished with a faint plop.
2296 PERL_UNUSED_CONTEXT;
2297 PERL_UNUSED_ARG(sv);
2299 Safefree(mg->mg_ptr);
2305 #endif /* USE_LOCALE_COLLATE */
2307 /* Just clear the UTF-8 cache data. */
2309 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2311 PERL_ARGS_ASSERT_MAGIC_SETUTF8;
2312 PERL_UNUSED_CONTEXT;
2313 PERL_UNUSED_ARG(sv);
2314 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2316 mg->mg_len = -1; /* The mg_len holds the len cache. */
2321 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
2324 register const char *s;
2326 register const REGEXP * rx;
2327 const char * const remaining = mg->mg_ptr + 1;
2331 PERL_ARGS_ASSERT_MAGIC_SET;
2333 switch (*mg->mg_ptr) {
2334 case '\015': /* $^MATCH */
2335 if (strEQ(remaining, "ATCH"))
2337 case '`': /* ${^PREMATCH} caught below */
2339 paren = RX_BUFF_IDX_PREMATCH;
2341 case '\'': /* ${^POSTMATCH} caught below */
2343 paren = RX_BUFF_IDX_POSTMATCH;
2347 paren = RX_BUFF_IDX_FULLMATCH;
2349 case '1': case '2': case '3': case '4':
2350 case '5': case '6': case '7': case '8': case '9':
2351 paren = atoi(mg->mg_ptr);
2353 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
2354 CALLREG_NUMBUF_STORE((REGEXP * const)rx,paren,sv);
2357 /* Croak with a READONLY error when a numbered match var is
2358 * set without a previous pattern match. Unless it's C<local $1>
2360 if (!PL_localizing) {
2361 Perl_croak(aTHX_ PL_no_modify);
2364 case '\001': /* ^A */
2365 sv_setsv(PL_bodytarget, sv);
2367 case '\003': /* ^C */
2368 PL_minus_c = (bool)SvIV(sv);
2371 case '\004': /* ^D */
2373 s = SvPV_nolen_const(sv);
2374 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
2375 DEBUG_x(dump_all());
2377 PL_debug = (SvIV(sv)) | DEBUG_TOP_FLAG;
2380 case '\005': /* ^E */
2381 if (*(mg->mg_ptr+1) == '\0') {
2382 #ifdef MACOS_TRADITIONAL
2383 gMacPerl_OSErr = SvIV(sv);
2386 set_vaxc_errno(SvIV(sv));
2389 SetLastError( SvIV(sv) );
2392 os2_setsyserrno(SvIV(sv));
2394 /* will anyone ever use this? */
2395 SETERRNO(SvIV(sv), 4);
2401 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2403 SvREFCNT_dec(PL_encoding);
2404 if (SvOK(sv) || SvGMAGICAL(sv)) {
2405 PL_encoding = newSVsv(sv);
2412 case '\006': /* ^F */
2413 PL_maxsysfd = SvIV(sv);
2415 case '\010': /* ^H */
2416 PL_hints = SvIV(sv);
2418 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2419 Safefree(PL_inplace);
2420 PL_inplace = SvOK(sv) ? savesvpv(sv) : NULL;
2422 case '\017': /* ^O */
2423 if (*(mg->mg_ptr+1) == '\0') {
2424 Safefree(PL_osname);
2427 TAINT_PROPER("assigning to $^O");
2428 PL_osname = savesvpv(sv);
2431 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2433 const char *const start = SvPV(sv, len);
2434 const char *out = (const char*)memchr(start, '\0', len);
2436 struct refcounted_he *tmp_he;
2439 PL_compiling.cop_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
2441 |= HINT_LOCALIZE_HH | HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT;
2443 /* Opening for input is more common than opening for output, so
2444 ensure that hints for input are sooner on linked list. */
2445 tmp = out ? newSVpvn_flags(out + 1, start + len - out - 1,
2446 SVs_TEMP | SvUTF8(sv))
2447 : newSVpvn_flags("", 0, SVs_TEMP | SvUTF8(sv));
2450 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash,
2451 newSVpvs_flags("open>", SVs_TEMP),
2454 /* The UTF-8 setting is carried over */
2455 sv_setpvn(tmp, start, out ? (STRLEN)(out - start) : len);
2457 PL_compiling.cop_hints_hash
2458 = Perl_refcounted_he_new(aTHX_ tmp_he,
2459 newSVpvs_flags("open<", SVs_TEMP),
2463 case '\020': /* ^P */
2464 if (*remaining == '\0') { /* ^P */
2465 PL_perldb = SvIV(sv);
2466 if (PL_perldb && !PL_DBsingle)
2469 } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */
2471 } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */
2474 case '\024': /* ^T */
2476 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2478 PL_basetime = (Time_t)SvIV(sv);
2481 case '\025': /* ^UTF8CACHE */
2482 if (strEQ(mg->mg_ptr+1, "TF8CACHE")) {
2483 PL_utf8cache = (signed char) sv_2iv(sv);
2486 case '\027': /* ^W & $^WARNING_BITS */
2487 if (*(mg->mg_ptr+1) == '\0') {
2488 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2490 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2491 | (i ? G_WARN_ON : G_WARN_OFF) ;
2494 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2495 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2496 if (!SvPOK(sv) && PL_localizing) {
2497 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2498 PL_compiling.cop_warnings = pWARN_NONE;
2503 int accumulate = 0 ;
2504 int any_fatals = 0 ;
2505 const char * const ptr = SvPV_const(sv, len) ;
2506 for (i = 0 ; i < len ; ++i) {
2507 accumulate |= ptr[i] ;
2508 any_fatals |= (ptr[i] & 0xAA) ;
2511 if (!specialWARN(PL_compiling.cop_warnings))
2512 PerlMemShared_free(PL_compiling.cop_warnings);
2513 PL_compiling.cop_warnings = pWARN_NONE;
2515 /* Yuck. I can't see how to abstract this: */
2516 else if (isWARN_on(((STRLEN *)SvPV_nolen_const(sv)) - 1,
2517 WARN_ALL) && !any_fatals) {
2518 if (!specialWARN(PL_compiling.cop_warnings))
2519 PerlMemShared_free(PL_compiling.cop_warnings);
2520 PL_compiling.cop_warnings = pWARN_ALL;
2521 PL_dowarn |= G_WARN_ONCE ;
2525 const char *const p = SvPV_const(sv, len);
2527 PL_compiling.cop_warnings
2528 = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
2531 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2532 PL_dowarn |= G_WARN_ONCE ;
2540 if (PL_localizing) {
2541 if (PL_localizing == 1)
2542 SAVESPTR(PL_last_in_gv);
2544 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2545 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2548 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2549 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2550 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2553 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2554 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2555 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2558 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv));
2561 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv));
2562 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2563 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2566 IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv));
2570 IO * const io = GvIOp(PL_defoutgv);
2573 if ((SvIV(sv)) == 0)
2574 IoFLAGS(io) &= ~IOf_FLUSH;
2576 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2577 PerlIO *ofp = IoOFP(io);
2579 (void)PerlIO_flush(ofp);
2580 IoFLAGS(io) |= IOf_FLUSH;
2586 SvREFCNT_dec(PL_rs);
2587 PL_rs = newSVsv(sv);
2591 SvREFCNT_dec(PL_ors_sv);
2592 if (SvOK(sv) || SvGMAGICAL(sv)) {
2593 PL_ors_sv = newSVsv(sv);
2601 SvREFCNT_dec(PL_ofs_sv);
2602 if (SvOK(sv) || SvGMAGICAL(sv)) {
2603 PL_ofs_sv = newSVsv(sv);
2610 CopARYBASE_set(&PL_compiling, SvIV(sv));
2613 #ifdef COMPLEX_STATUS
2614 if (PL_localizing == 2) {
2615 PL_statusvalue = LvTARGOFF(sv);
2616 PL_statusvalue_vms = LvTARGLEN(sv);
2620 #ifdef VMSISH_STATUS
2622 STATUS_NATIVE_CHILD_SET((U32)SvIV(sv));
2625 STATUS_UNIX_EXIT_SET(SvIV(sv));
2630 # define PERL_VMS_BANG vaxc$errno
2632 # define PERL_VMS_BANG 0
2634 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2635 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2640 if (PL_delaymagic) {
2641 PL_delaymagic |= DM_RUID;
2642 break; /* don't do magic till later */
2645 (void)setruid((Uid_t)PL_uid);
2648 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2650 #ifdef HAS_SETRESUID
2651 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2653 if (PL_uid == PL_euid) { /* special case $< = $> */
2655 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2656 if (PL_uid != 0 && PerlProc_getuid() == 0)
2657 (void)PerlProc_setuid(0);
2659 (void)PerlProc_setuid(PL_uid);
2661 PL_uid = PerlProc_getuid();
2662 Perl_croak(aTHX_ "setruid() not implemented");
2667 PL_uid = PerlProc_getuid();
2668 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2672 if (PL_delaymagic) {
2673 PL_delaymagic |= DM_EUID;
2674 break; /* don't do magic till later */
2677 (void)seteuid((Uid_t)PL_euid);
2680 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2682 #ifdef HAS_SETRESUID
2683 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2685 if (PL_euid == PL_uid) /* special case $> = $< */
2686 PerlProc_setuid(PL_euid);
2688 PL_euid = PerlProc_geteuid();
2689 Perl_croak(aTHX_ "seteuid() not implemented");
2694 PL_euid = PerlProc_geteuid();
2695 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2699 if (PL_delaymagic) {
2700 PL_delaymagic |= DM_RGID;
2701 break; /* don't do magic till later */
2704 (void)setrgid((Gid_t)PL_gid);
2707 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2709 #ifdef HAS_SETRESGID
2710 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2712 if (PL_gid == PL_egid) /* special case $( = $) */
2713 (void)PerlProc_setgid(PL_gid);
2715 PL_gid = PerlProc_getgid();
2716 Perl_croak(aTHX_ "setrgid() not implemented");
2721 PL_gid = PerlProc_getgid();
2722 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2725 #ifdef HAS_SETGROUPS
2727 const char *p = SvPV_const(sv, len);
2728 Groups_t *gary = NULL;
2733 for (i = 0; i < NGROUPS; ++i) {
2734 while (*p && !isSPACE(*p))
2741 Newx(gary, i + 1, Groups_t);
2743 Renew(gary, i + 1, Groups_t);
2747 (void)setgroups(i, gary);
2750 #else /* HAS_SETGROUPS */
2752 #endif /* HAS_SETGROUPS */
2753 if (PL_delaymagic) {
2754 PL_delaymagic |= DM_EGID;
2755 break; /* don't do magic till later */
2758 (void)setegid((Gid_t)PL_egid);
2761 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2763 #ifdef HAS_SETRESGID
2764 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2766 if (PL_egid == PL_gid) /* special case $) = $( */
2767 (void)PerlProc_setgid(PL_egid);
2769 PL_egid = PerlProc_getegid();
2770 Perl_croak(aTHX_ "setegid() not implemented");
2775 PL_egid = PerlProc_getegid();
2776 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2779 PL_chopset = SvPV_force(sv,len);
2781 #ifndef MACOS_TRADITIONAL
2783 LOCK_DOLLARZERO_MUTEX;
2784 #ifdef HAS_SETPROCTITLE
2785 /* The BSDs don't show the argv[] in ps(1) output, they
2786 * show a string from the process struct and provide
2787 * the setproctitle() routine to manipulate that. */
2788 if (PL_origalen != 1) {
2789 s = SvPV_const(sv, len);
2790 # if __FreeBSD_version > 410001
2791 /* The leading "-" removes the "perl: " prefix,
2792 * but not the "(perl) suffix from the ps(1)
2793 * output, because that's what ps(1) shows if the
2794 * argv[] is modified. */
2795 setproctitle("-%s", s);
2796 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2797 /* This doesn't really work if you assume that
2798 * $0 = 'foobar'; will wipe out 'perl' from the $0
2799 * because in ps(1) output the result will be like
2800 * sprintf("perl: %s (perl)", s)
2801 * I guess this is a security feature:
2802 * one (a user process) cannot get rid of the original name.
2804 setproctitle("%s", s);
2807 #elif defined(__hpux) && defined(PSTAT_SETCMD)
2808 if (PL_origalen != 1) {
2810 s = SvPV_const(sv, len);
2811 un.pst_command = (char *)s;
2812 pstat(PSTAT_SETCMD, un, len, 0, 0);
2815 if (PL_origalen > 1) {
2816 /* PL_origalen is set in perl_parse(). */
2817 s = SvPV_force(sv,len);
2818 if (len >= (STRLEN)PL_origalen-1) {
2819 /* Longer than original, will be truncated. We assume that
2820 * PL_origalen bytes are available. */
2821 Copy(s, PL_origargv[0], PL_origalen-1, char);
2824 /* Shorter than original, will be padded. */
2826 /* Special case for Mac OS X: see [perl #38868] */
2829 /* Is the space counterintuitive? Yes.
2830 * (You were expecting \0?)
2831 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2833 const int pad = ' ';
2835 Copy(s, PL_origargv[0], len, char);
2836 PL_origargv[0][len] = 0;
2837 memset(PL_origargv[0] + len + 1,
2838 pad, PL_origalen - len - 1);
2840 PL_origargv[0][PL_origalen-1] = 0;
2841 for (i = 1; i < PL_origargc; i++)
2845 UNLOCK_DOLLARZERO_MUTEX;
2853 Perl_whichsig(pTHX_ const char *sig)
2855 register char* const* sigv;
2857 PERL_ARGS_ASSERT_WHICHSIG;
2858 PERL_UNUSED_CONTEXT;
2860 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
2861 if (strEQ(sig,*sigv))
2862 return PL_sig_num[sigv - (char* const*)PL_sig_name];
2864 if (strEQ(sig,"CHLD"))
2868 if (strEQ(sig,"CLD"))
2875 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2876 Perl_sighandler(int sig, siginfo_t *sip, void *uap PERL_UNUSED_DECL)
2878 Perl_sighandler(int sig)
2881 #ifdef PERL_GET_SIG_CONTEXT
2882 dTHXa(PERL_GET_SIG_CONTEXT);
2889 SV * const tSv = PL_Sv;
2893 XPV * const tXpv = PL_Xpv;
2895 if (PL_savestack_ix + 15 <= PL_savestack_max)
2897 if (PL_markstack_ptr < PL_markstack_max - 2)
2899 if (PL_scopestack_ix < PL_scopestack_max - 3)
2902 if (!PL_psig_ptr[sig]) {
2903 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2908 /* Max number of items pushed there is 3*n or 4. We cannot fix
2909 infinity, so we fix 4 (in fact 5): */
2911 PL_savestack_ix += 5; /* Protect save in progress. */
2912 SAVEDESTRUCTOR_X(S_unwind_handler_stack, (void*)&flags);
2915 PL_markstack_ptr++; /* Protect mark. */
2917 PL_scopestack_ix += 1;
2918 /* sv_2cv is too complicated, try a simpler variant first: */
2919 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2920 || SvTYPE(cv) != SVt_PVCV) {
2922 cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD);
2925 if (!cv || !CvROOT(cv)) {
2926 if (ckWARN(WARN_SIGNAL))
2927 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2928 PL_sig_name[sig], (gv ? GvENAME(gv)
2935 if(PL_psig_name[sig]) {
2936 sv = SvREFCNT_inc_NN(PL_psig_name[sig]);
2938 #if !defined(PERL_IMPLICIT_CONTEXT)
2942 sv = sv_newmortal();
2943 sv_setpv(sv,PL_sig_name[sig]);
2946 PUSHSTACKi(PERLSI_SIGNAL);
2949 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2951 struct sigaction oact;
2953 if (sigaction(sig, 0, &oact) == 0 && oact.sa_flags & SA_SIGINFO) {
2956 SV *rv = newRV_noinc((SV*)sih);
2957 /* The siginfo fields signo, code, errno, pid, uid,
2958 * addr, status, and band are defined by POSIX/SUSv3. */
2959 (void)hv_stores(sih, "signo", newSViv(sip->si_signo));
2960 (void)hv_stores(sih, "code", newSViv(sip->si_code));
2961 #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. */
2962 hv_stores(sih, "errno", newSViv(sip->si_errno));
2963 hv_stores(sih, "status", newSViv(sip->si_status));
2964 hv_stores(sih, "uid", newSViv(sip->si_uid));
2965 hv_stores(sih, "pid", newSViv(sip->si_pid));
2966 hv_stores(sih, "addr", newSVuv(PTR2UV(sip->si_addr)));
2967 hv_stores(sih, "band", newSViv(sip->si_band));
2971 mPUSHp((char *)sip, sizeof(*sip));
2979 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2982 if (SvTRUE(ERRSV)) {
2984 #ifdef HAS_SIGPROCMASK
2985 /* Handler "died", for example to get out of a restart-able read().
2986 * Before we re-do that on its behalf re-enable the signal which was
2987 * blocked by the system when we entered.
2991 sigaddset(&set,sig);
2992 sigprocmask(SIG_UNBLOCK, &set, NULL);
2994 /* Not clear if this will work */
2995 (void)rsignal(sig, SIG_IGN);
2996 (void)rsignal(sig, PL_csighandlerp);
2998 #endif /* !PERL_MICRO */
2999 Perl_die(aTHX_ NULL);
3003 PL_savestack_ix -= 8; /* Unprotect save in progress. */
3007 PL_scopestack_ix -= 1;
3010 PL_op = myop; /* Apparently not needed... */
3012 PL_Sv = tSv; /* Restore global temporaries. */
3019 S_restore_magic(pTHX_ const void *p)
3022 MGS* const mgs = SSPTR(PTR2IV(p), MGS*);
3023 SV* const sv = mgs->mgs_sv;
3028 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
3030 #ifdef PERL_OLD_COPY_ON_WRITE
3031 /* While magic was saved (and off) sv_setsv may well have seen
3032 this SV as a prime candidate for COW. */
3034 sv_force_normal_flags(sv, 0);
3038 SvFLAGS(sv) |= mgs->mgs_flags;
3041 if (SvGMAGICAL(sv)) {
3042 /* downgrade public flags to private,
3043 and discard any other private flags */
3045 const U32 pubflags = SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK);
3047 SvFLAGS(sv) &= ~( pubflags | (SVp_IOK|SVp_NOK|SVp_POK) );
3048 SvFLAGS(sv) |= ( pubflags << PRIVSHIFT );
3053 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
3055 /* If we're still on top of the stack, pop us off. (That condition
3056 * will be satisfied if restore_magic was called explicitly, but *not*
3057 * if it's being called via leave_scope.)
3058 * The reason for doing this is that otherwise, things like sv_2cv()
3059 * may leave alloc gunk on the savestack, and some code
3060 * (e.g. sighandler) doesn't expect that...
3062 if (PL_savestack_ix == mgs->mgs_ss_ix)
3064 I32 popval = SSPOPINT;
3065 assert(popval == SAVEt_DESTRUCTOR_X);
3066 PL_savestack_ix -= 2;
3068 assert(popval == SAVEt_ALLOC);
3070 PL_savestack_ix -= popval;
3076 S_unwind_handler_stack(pTHX_ const void *p)
3079 const U32 flags = *(const U32*)p;
3081 PERL_ARGS_ASSERT_UNWIND_HANDLER_STACK;
3084 PL_savestack_ix -= 5; /* Unprotect save in progress. */
3085 #if !defined(PERL_IMPLICIT_CONTEXT)
3087 SvREFCNT_dec(PL_sig_sv);
3092 =for apidoc magic_sethint
3094 Triggered by a store to %^H, records the key/value pair to
3095 C<PL_compiling.cop_hints_hash>. It is assumed that hints aren't storing
3096 anything that would need a deep copy. Maybe we should warn if we find a
3102 Perl_magic_sethint(pTHX_ SV *sv, MAGIC *mg)
3105 SV *key = (mg->mg_len == HEf_SVKEY) ? (SV *)mg->mg_ptr
3106 : newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
3108 PERL_ARGS_ASSERT_MAGIC_SETHINT;
3110 /* mg->mg_obj isn't being used. If needed, it would be possible to store
3111 an alternative leaf in there, with PL_compiling.cop_hints being used if
3112 it's NULL. If needed for threads, the alternative could lock a mutex,
3113 or take other more complex action. */
3115 /* Something changed in %^H, so it will need to be restored on scope exit.
3116 Doing this here saves a lot of doing it manually in perl code (and
3117 forgetting to do it, and consequent subtle errors. */
3118 PL_hints |= HINT_LOCALIZE_HH;
3119 PL_compiling.cop_hints_hash
3120 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash, key, sv);
3125 =for apidoc magic_clearhint
3127 Triggered by a delete from %^H, records the key to
3128 C<PL_compiling.cop_hints_hash>.
3133 Perl_magic_clearhint(pTHX_ SV *sv, MAGIC *mg)
3137 PERL_ARGS_ASSERT_MAGIC_CLEARHINT;
3138 PERL_UNUSED_ARG(sv);
3140 assert(mg->mg_len == HEf_SVKEY);
3142 PERL_UNUSED_ARG(sv);
3144 PL_hints |= HINT_LOCALIZE_HH;
3145 PL_compiling.cop_hints_hash
3146 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash,
3147 (SV *)mg->mg_ptr, &PL_sv_placeholder);
3153 * c-indentation-style: bsd
3155 * indent-tabs-mode: t
3158 * ex: set ts=8 sts=4 sw=4 noet: